Initial integration with editor-based CodeEditor class (only the widget
callback editor right now) git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4152 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
parent
8a5eef2917
commit
e852b736ad
2
CHANGES
2
CHANGES
@ -2,6 +2,8 @@ CHANGES IN FLTK 1.1.7
|
||||
|
||||
- Documentation fixes (STR #648, STR #692, STR #730, STR
|
||||
#744, STR #745)
|
||||
- FLUID now highlights code in the widget callback and
|
||||
code editors.
|
||||
- FLUID now supports printing of windows.
|
||||
- Fixed inactive drawing of border, embossed, and
|
||||
engraved box types.
|
||||
|
361
fluid/CodeEditor.cxx
Normal file
361
fluid/CodeEditor.cxx
Normal file
@ -0,0 +1,361 @@
|
||||
//
|
||||
// "$Id$"
|
||||
//
|
||||
// Code editor widget for the Fast Light Tool Kit (FLTK).
|
||||
//
|
||||
// Copyright 1998-2005 by Bill Spitzak and others.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Library General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Library General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
// USA.
|
||||
//
|
||||
// Please report all bugs and problems on the following page:
|
||||
//
|
||||
// http://www.fltk.org/str.php
|
||||
//
|
||||
|
||||
//
|
||||
// Include necessary headers...
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "CodeEditor.h"
|
||||
|
||||
|
||||
Fl_Text_Display::Style_Table_Entry CodeEditor::
|
||||
styletable[] = { // Style table
|
||||
{ FL_BLACK, FL_COURIER, 11 }, // A - Plain
|
||||
{ FL_DARK_GREEN, FL_COURIER_ITALIC, 11 }, // B - Line comments
|
||||
{ FL_DARK_GREEN, FL_COURIER_ITALIC, 11 }, // C - Block comments
|
||||
{ FL_BLUE, FL_COURIER, 11 }, // D - Strings
|
||||
{ FL_DARK_RED, FL_COURIER, 11 }, // E - Directives
|
||||
{ FL_DARK_RED, FL_COURIER_BOLD, 11 }, // F - Types
|
||||
{ FL_BLUE, FL_COURIER_BOLD, 11 } // G - Keywords
|
||||
};
|
||||
const char * const CodeEditor::
|
||||
code_keywords[] = { // Sorted list of C/C++ keywords...
|
||||
"and",
|
||||
"and_eq",
|
||||
"asm",
|
||||
"bitand",
|
||||
"bitor",
|
||||
"break",
|
||||
"case",
|
||||
"catch",
|
||||
"compl",
|
||||
"continue",
|
||||
"default",
|
||||
"delete",
|
||||
"do",
|
||||
"else",
|
||||
"false",
|
||||
"for",
|
||||
"goto",
|
||||
"if",
|
||||
"new",
|
||||
"not",
|
||||
"not_eq",
|
||||
"operator",
|
||||
"or",
|
||||
"or_eq",
|
||||
"return",
|
||||
"switch",
|
||||
"template",
|
||||
"this",
|
||||
"throw",
|
||||
"true",
|
||||
"try",
|
||||
"while",
|
||||
"xor",
|
||||
"xor_eq"
|
||||
};
|
||||
const char * const CodeEditor::
|
||||
code_types[] = { // Sorted list of C/C++ types...
|
||||
"auto",
|
||||
"bool",
|
||||
"char",
|
||||
"class",
|
||||
"const",
|
||||
"const_cast",
|
||||
"double",
|
||||
"dynamic_cast",
|
||||
"enum",
|
||||
"explicit",
|
||||
"extern",
|
||||
"float",
|
||||
"friend",
|
||||
"inline",
|
||||
"int",
|
||||
"long",
|
||||
"mutable",
|
||||
"namespace",
|
||||
"private",
|
||||
"protected",
|
||||
"public",
|
||||
"register",
|
||||
"short",
|
||||
"signed",
|
||||
"sizeof",
|
||||
"static",
|
||||
"static_cast",
|
||||
"struct",
|
||||
"template",
|
||||
"typedef",
|
||||
"typename",
|
||||
"union",
|
||||
"unsigned",
|
||||
"virtual",
|
||||
"void",
|
||||
"volatile"
|
||||
};
|
||||
|
||||
|
||||
// 'compare_keywords()' - Compare two keywords...
|
||||
int CodeEditor::compare_keywords(const void *a, const void *b) {
|
||||
return (strcmp(*((const char **)a), *((const char **)b)));
|
||||
}
|
||||
|
||||
// 'style_parse()' - Parse text and produce style data.
|
||||
void CodeEditor::style_parse(const char *text, char *style, int length) {
|
||||
char current;
|
||||
int col;
|
||||
int last;
|
||||
char buf[255],
|
||||
*bufptr;
|
||||
const char *temp;
|
||||
|
||||
for (current = *style, col = 0, last = 0; length > 0; length --, text ++) {
|
||||
if (current == 'B') current = 'A';
|
||||
if (current == 'A') {
|
||||
// Check for directives, comments, strings, and keywords...
|
||||
if (col == 0 && *text == '#') {
|
||||
// Set style to directive
|
||||
current = 'E';
|
||||
} else if (strncmp(text, "//", 2) == 0) {
|
||||
current = 'B';
|
||||
for (; length > 0 && *text != '\n'; length --, text ++) *style++ = 'B';
|
||||
|
||||
if (length == 0) break;
|
||||
} else if (strncmp(text, "/*", 2) == 0) {
|
||||
current = 'C';
|
||||
} else if (strncmp(text, "\\\"", 2) == 0) {
|
||||
// Quoted quote...
|
||||
*style++ = current;
|
||||
*style++ = current;
|
||||
text ++;
|
||||
length --;
|
||||
col += 2;
|
||||
continue;
|
||||
} else if (*text == '\"') {
|
||||
current = 'D';
|
||||
} else if (!last && islower(*text)) {
|
||||
// Might be a keyword...
|
||||
for (temp = text, bufptr = buf;
|
||||
islower(*temp) && bufptr < (buf + sizeof(buf) - 1);
|
||||
*bufptr++ = *temp++);
|
||||
|
||||
if (!islower(*temp)) {
|
||||
*bufptr = '\0';
|
||||
|
||||
bufptr = buf;
|
||||
|
||||
if (bsearch(&bufptr, code_types,
|
||||
sizeof(code_types) / sizeof(code_types[0]),
|
||||
sizeof(code_types[0]), compare_keywords)) {
|
||||
while (text < temp) {
|
||||
*style++ = 'F';
|
||||
text ++;
|
||||
length --;
|
||||
col ++;
|
||||
}
|
||||
|
||||
text --;
|
||||
length ++;
|
||||
last = 1;
|
||||
continue;
|
||||
} else if (bsearch(&bufptr, code_keywords,
|
||||
sizeof(code_keywords) / sizeof(code_keywords[0]),
|
||||
sizeof(code_keywords[0]), compare_keywords)) {
|
||||
while (text < temp) {
|
||||
*style++ = 'G';
|
||||
text ++;
|
||||
length --;
|
||||
col ++;
|
||||
}
|
||||
|
||||
text --;
|
||||
length ++;
|
||||
last = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (current == 'C' && strncmp(text, "*/", 2) == 0) {
|
||||
// Close a C comment...
|
||||
*style++ = current;
|
||||
*style++ = current;
|
||||
text ++;
|
||||
length --;
|
||||
current = 'A';
|
||||
col += 2;
|
||||
continue;
|
||||
} else if (current == 'D') {
|
||||
// Continuing in string...
|
||||
if (strncmp(text, "\\\"", 2) == 0) {
|
||||
// Quoted end quote...
|
||||
*style++ = current;
|
||||
*style++ = current;
|
||||
text ++;
|
||||
length --;
|
||||
col += 2;
|
||||
continue;
|
||||
} else if (*text == '\"') {
|
||||
// End quote...
|
||||
*style++ = current;
|
||||
col ++;
|
||||
current = 'A';
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy style info...
|
||||
if (current == 'A' && (*text == '{' || *text == '}')) *style++ = 'G';
|
||||
else *style++ = current;
|
||||
col ++;
|
||||
|
||||
last = isalnum(*text) || *text == '.';
|
||||
|
||||
if (*text == '\n') {
|
||||
// Reset column and possibly reset the style
|
||||
col = 0;
|
||||
if (current == 'B' || current == 'E') current = 'A';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 'style_unfinished_cb()' - Update unfinished styles.
|
||||
void CodeEditor::style_unfinished_cb(int, void*) { }
|
||||
|
||||
// 'style_update()' - Update the style buffer...
|
||||
void CodeEditor::style_update(int pos, int nInserted, int nDeleted,
|
||||
int /*nRestyled*/, const char * /*deletedText*/,
|
||||
void *cbArg) {
|
||||
CodeEditor *editor = (CodeEditor *)cbArg;
|
||||
int start, // Start of text
|
||||
end; // End of text
|
||||
char last, // Last style on line
|
||||
*style, // Style data
|
||||
*text; // Text data
|
||||
|
||||
|
||||
// If this is just a selection change, just unselect the style buffer...
|
||||
if (nInserted == 0 && nDeleted == 0) {
|
||||
editor->mStyleBuffer->unselect();
|
||||
return;
|
||||
}
|
||||
|
||||
// Track changes in the text buffer...
|
||||
if (nInserted > 0) {
|
||||
// Insert characters into the style buffer...
|
||||
style = new char[nInserted + 1];
|
||||
memset(style, 'A', nInserted);
|
||||
style[nInserted] = '\0';
|
||||
|
||||
editor->mStyleBuffer->replace(pos, pos + nDeleted, style);
|
||||
delete[] style;
|
||||
} else {
|
||||
// Just delete characters in the style buffer...
|
||||
editor->mStyleBuffer->remove(pos, pos + nDeleted);
|
||||
}
|
||||
|
||||
// Select the area that was just updated to avoid unnecessary
|
||||
// callbacks...
|
||||
editor->mStyleBuffer->select(pos, pos + nInserted - nDeleted);
|
||||
|
||||
// Re-parse the changed region; we do this by parsing from the
|
||||
// beginning of the line of the changed region to the end of
|
||||
// the line of the changed region... Then we check the last
|
||||
// style character and keep updating if we have a multi-line
|
||||
// comment character...
|
||||
start = editor->mBuffer->line_start(pos);
|
||||
end = editor->mBuffer->line_end(pos + nInserted);
|
||||
text = editor->mBuffer->text_range(start, end);
|
||||
style = editor->mStyleBuffer->text_range(start, end);
|
||||
last = style[end - start - 1];
|
||||
|
||||
style_parse(text, style, end - start);
|
||||
|
||||
editor->mStyleBuffer->replace(start, end, style);
|
||||
editor->redisplay_range(start, end);
|
||||
|
||||
if (last != style[end - start - 1]) {
|
||||
// The last character on the line changed styles, so reparse the
|
||||
// remainder of the buffer...
|
||||
free(text);
|
||||
free(style);
|
||||
|
||||
end = editor->mBuffer->length();
|
||||
text = editor->mBuffer->text_range(start, end);
|
||||
style = editor->mStyleBuffer->text_range(start, end);
|
||||
|
||||
style_parse(text, style, end - start);
|
||||
|
||||
editor->mStyleBuffer->replace(start, end, style);
|
||||
editor->redisplay_range(start, end);
|
||||
}
|
||||
|
||||
free(text);
|
||||
free(style);
|
||||
}
|
||||
|
||||
CodeEditor::CodeEditor(int X, int Y, int W, int H, const char *L) :
|
||||
Fl_Text_Editor(X, Y, W, H, L) {
|
||||
buffer(new Fl_Text_Buffer);
|
||||
|
||||
char *style = new char[mBuffer->length() + 1];
|
||||
char *text = mBuffer->text();
|
||||
|
||||
memset(style, 'A', mBuffer->length());
|
||||
style[mBuffer->length()] = '\0';
|
||||
|
||||
highlight_data(new Fl_Text_Buffer(mBuffer->length()), styletable,
|
||||
sizeof(styletable) / sizeof(styletable[0]),
|
||||
'A', style_unfinished_cb, this);
|
||||
|
||||
style_parse(text, style, mBuffer->length());
|
||||
|
||||
mStyleBuffer->text(style);
|
||||
delete[] style;
|
||||
free(text);
|
||||
|
||||
mBuffer->add_modify_callback(style_update, this);
|
||||
}
|
||||
|
||||
CodeEditor::~CodeEditor() {
|
||||
Fl_Text_Buffer *buf = mStyleBuffer;
|
||||
// style_buffer(0);
|
||||
delete buf;
|
||||
|
||||
buf = mBuffer;
|
||||
buffer(0);
|
||||
delete buf;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// End of "$Id$".
|
||||
//
|
74
fluid/CodeEditor.h
Normal file
74
fluid/CodeEditor.h
Normal file
@ -0,0 +1,74 @@
|
||||
//
|
||||
// "$Id$"
|
||||
//
|
||||
// Code editor widget for the Fast Light Tool Kit (FLTK).
|
||||
//
|
||||
// Copyright 1998-2005 by Bill Spitzak and others.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Library General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Library General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
// USA.
|
||||
//
|
||||
// Please report all bugs and problems on the following page:
|
||||
//
|
||||
// http://www.fltk.org/str.php
|
||||
//
|
||||
|
||||
#ifndef CodeEditor_h
|
||||
# define CodeEditor_h
|
||||
|
||||
//
|
||||
// Include necessary headers...
|
||||
//
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <ctype.h>
|
||||
# include <FL/Fl.H>
|
||||
# include <FL/Fl_Text_Buffer.H>
|
||||
# include <FL/Fl_Text_Editor.H>
|
||||
|
||||
|
||||
class CodeEditor : public Fl_Text_Editor {
|
||||
static Fl_Text_Display::Style_Table_Entry styletable[];
|
||||
static const char * const code_keywords[];
|
||||
static const char * const code_types[];
|
||||
|
||||
|
||||
// 'compare_keywords()' - Compare two keywords...
|
||||
static int compare_keywords(const void *a, const void *b);
|
||||
|
||||
// 'style_parse()' - Parse text and produce style data.
|
||||
static void style_parse(const char *text, char *style, int length);
|
||||
|
||||
// 'style_unfinished_cb()' - Update unfinished styles.
|
||||
static void style_unfinished_cb(int, void*);
|
||||
|
||||
// 'style_update()' - Update the style buffer...
|
||||
static void style_update(int pos, int nInserted, int nDeleted,
|
||||
int /*nRestyled*/, const char * /*deletedText*/,
|
||||
void *cbArg);
|
||||
|
||||
public:
|
||||
|
||||
CodeEditor(int X, int Y, int W, int H, const char *L=0);
|
||||
~CodeEditor();
|
||||
};
|
||||
|
||||
#endif // !CodeEditor_h
|
||||
|
||||
//
|
||||
// End of "$Id$".
|
||||
//
|
@ -979,7 +979,7 @@ void align_cb(Fl_Button* i, void *v) {
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
void callback_cb(Fl_Text_Editor* i, void *v) {
|
||||
void callback_cb(CodeEditor* i, void *v) {
|
||||
if (v == LOAD) {
|
||||
const char *cbtext = current_widget->callback();
|
||||
i->buffer()->text( cbtext ? cbtext : "" );
|
||||
|
@ -24,6 +24,7 @@
|
||||
#
|
||||
|
||||
CPPFILES = \
|
||||
CodeEditor.cxx \
|
||||
Fl_Function_Type.cxx \
|
||||
Fl_Group_Type.cxx \
|
||||
Fl_Menu_Type.cxx \
|
||||
|
@ -578,17 +578,20 @@ Fl_Double_Window* make_widget_panel() {
|
||||
}
|
||||
o->end();
|
||||
}
|
||||
{ Fl_Text_Editor* o = new Fl_Text_Editor(100, 169, 295, 91, "Callback:");
|
||||
{ CodeEditor* o = new CodeEditor(100, 169, 295, 91, "Callback:");
|
||||
o->tooltip("The callback function or code for the widget.");
|
||||
o->box(FL_DOWN_BOX);
|
||||
o->color(FL_BACKGROUND2_COLOR);
|
||||
o->selection_color(FL_SELECTION_COLOR);
|
||||
o->labeltype(FL_NORMAL_LABEL);
|
||||
o->labelfont(1);
|
||||
o->labelsize(11);
|
||||
o->labelcolor(FL_BLACK);
|
||||
o->textfont(4);
|
||||
o->textsize(11);
|
||||
o->callback((Fl_Callback*)callback_cb);
|
||||
o->align(FL_ALIGN_LEFT);
|
||||
o->buffer(new Fl_Text_Buffer());
|
||||
o->textfont(FL_COURIER);
|
||||
o->when(FL_WHEN_RELEASE);
|
||||
}
|
||||
{ Fl_Group* o = new Fl_Group(95, 265, 303, 45);
|
||||
o->labelsize(11);
|
||||
|
@ -33,7 +33,7 @@ comment {//
|
||||
|
||||
Function {make_widget_panel()} {open
|
||||
} {
|
||||
Fl_Window {} {open selected
|
||||
Fl_Window {} {open
|
||||
xywh {632 21 405 345} type Double labelsize 11 resizable hotspot visible
|
||||
} {
|
||||
Fl_Tabs {} {
|
||||
@ -437,10 +437,10 @@ Function {make_widget_panel()} {open
|
||||
}
|
||||
Fl_Text_Editor {} {
|
||||
label {Callback:}
|
||||
callback callback_cb
|
||||
callback callback_cb selected
|
||||
tooltip {The callback function or code for the widget.} xywh {100 169 295 91} box DOWN_BOX labelfont 1 labelsize 11 align 4 textfont 4 textsize 11
|
||||
code0 {o->buffer(new Fl_Text_Buffer());}
|
||||
code1 {o->textfont(FL_COURIER);}
|
||||
code0 {\#include "CodeEditor.h"}
|
||||
class CodeEditor
|
||||
}
|
||||
Fl_Group {} {
|
||||
callback propagate_load open
|
||||
|
@ -90,8 +90,8 @@ extern void name_cb(Fl_Input*, void*);
|
||||
extern void name_public_cb(Fl_Light_Button*, void*);
|
||||
extern void v_input_cb(Fl_Input*, void*);
|
||||
extern Fl_Input *v_input[4];
|
||||
#include <FL/Fl_Text_Editor.H>
|
||||
extern void callback_cb(Fl_Text_Editor*, void*);
|
||||
#include "CodeEditor.h"
|
||||
extern void callback_cb(CodeEditor*, void*);
|
||||
extern void user_data_cb(Fl_Input*, void*);
|
||||
extern Fl_Menu_Item whenmenu[];
|
||||
extern void when_cb(Fl_Choice*, void*);
|
||||
|
Loading…
Reference in New Issue
Block a user