diff --git a/CHANGES b/CHANGES index 85bf58619..c60dbf50b 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,10 @@ CHANGES IN FLTK 1.1.0 + - Added syntax highlighting example code to the editor + test program. + - Fl_Text_Display didn't range check style buffer + values. + - Added "dark" color constants (FL_DARK_RED, etc.) - The MacOS font code was missing definitions for fl_font_ and fl_size_. diff --git a/FL/Enumerations.H b/FL/Enumerations.H index cbf81f1f9..92e6f1454 100644 --- a/FL/Enumerations.H +++ b/FL/Enumerations.H @@ -1,5 +1,5 @@ // -// "$Id: Enumerations.H,v 1.18.2.14.2.23 2002/04/26 11:32:37 easysw Exp $" +// "$Id: Enumerations.H,v 1.18.2.14.2.24 2002/06/09 13:35:49 easysw Exp $" // // Enumerations for the Fast Light Tool Kit (FLTK). // @@ -298,6 +298,14 @@ enum Fl_Color { // standard colors FL_BLUE = 216, FL_MAGENTA = 248, FL_CYAN = 223, + FL_DARK_RED = 72, + + FL_DARK_GREEN = 60, + FL_DARK_YELLOW = 76, + FL_DARK_BLUE = 136, + FL_DARK_MAGENTA = 152, + FL_DARK_CYAN = 140, + FL_WHITE = 255 }; @@ -389,5 +397,5 @@ enum Fl_Damage { #endif // -// End of "$Id: Enumerations.H,v 1.18.2.14.2.23 2002/04/26 11:32:37 easysw Exp $". +// End of "$Id: Enumerations.H,v 1.18.2.14.2.24 2002/06/09 13:35:49 easysw Exp $". // diff --git a/FL/Fl_Text_Buffer.H b/FL/Fl_Text_Buffer.H index eee2f5a4a..0c342c062 100644 --- a/FL/Fl_Text_Buffer.H +++ b/FL/Fl_Text_Buffer.H @@ -1,5 +1,5 @@ // -// "$Id: Fl_Text_Buffer.H,v 1.3.2.2 2002/01/01 15:11:28 easysw Exp $" +// "$Id: Fl_Text_Buffer.H,v 1.3.2.3 2002/06/09 13:35:49 easysw Exp $" // // Header file for Fl_Text_Buffer class. // @@ -72,11 +72,11 @@ class FL_EXPORT Fl_Text_Buffer { ~Fl_Text_Buffer(); int length() { return mLength; } - const char* text(); + char* text(); void text(const char* text); - const char* text_range(int start, int end); + char* text_range(int start, int end); char character(int pos); - const char* text_in_rectangle(int start, int end, int rectStart, int rectEnd); + char* text_in_rectangle(int start, int end, int rectStart, int rectEnd); void insert(int pos, const char* text); void append(const char* text) { insert(length(), text); } void remove(int start, int end); @@ -114,7 +114,7 @@ class FL_EXPORT Fl_Text_Buffer { int selection_position(int* start, int* end, int* isRect, int* rectStart, int* rectEnd); - const char* selection_text(); + char* selection_text(); void remove_selection(); void replace_selection(const char* text); void secondary_select(int start, int end); @@ -126,7 +126,7 @@ class FL_EXPORT Fl_Text_Buffer { int secondary_selection_position(int* start, int* end, int* isRect, int* rectStart, int* rectEnd); - const char* secondary_selection_text(); + char* secondary_selection_text(); void remove_secondary_selection(); void replace_secondary_selection(const char* text); void highlight(int start, int end); @@ -136,13 +136,13 @@ class FL_EXPORT Fl_Text_Buffer { int highlight_position(int* start, int* end, int* isRect, int* rectStart, int* rectEnd); - const char* highlight_text(); + char* highlight_text(); void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg); void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg); void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); } - const char* line_text(int pos); + char* line_text(int pos); int line_start(int pos); int line_end(int pos); int word_start(int pos); @@ -198,7 +198,7 @@ class FL_EXPORT Fl_Text_Buffer { void move_gap(int pos); void reallocate_with_gap(int newGapStart, int newGapLen); - const char* selection_text_(Fl_Text_Selection* sel); + char* selection_text_(Fl_Text_Selection* sel); void remove_selection_(Fl_Text_Selection* sel); void replace_selection_(Fl_Text_Selection* sel, const char* text); @@ -238,5 +238,5 @@ class FL_EXPORT Fl_Text_Buffer { #endif // -// End of "$Id: Fl_Text_Buffer.H,v 1.3.2.2 2002/01/01 15:11:28 easysw Exp $". +// End of "$Id: Fl_Text_Buffer.H,v 1.3.2.3 2002/06/09 13:35:49 easysw Exp $". // diff --git a/documentation/enumerations.html b/documentation/enumerations.html index 032eb33d8..c7c1460e1 100644 --- a/documentation/enumerations.html +++ b/documentation/enumerations.html @@ -221,13 +221,19 @@ FLTK standard color cube: diff --git a/src/Fl_Text_Buffer.cxx b/src/Fl_Text_Buffer.cxx index 68aafd9a8..8043f05e0 100644 --- a/src/Fl_Text_Buffer.cxx +++ b/src/Fl_Text_Buffer.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_Text_Buffer.cxx,v 1.9.2.6 2002/05/16 12:47:43 easysw Exp $" +// "$Id: Fl_Text_Buffer.cxx,v 1.9.2.7 2002/06/09 13:35:49 easysw Exp $" // // Copyright 2001-2002 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under @@ -115,7 +115,7 @@ Fl_Text_Buffer::~Fl_Text_Buffer() { ** Get the entire contents of a text buffer. Memory is allocated to contain ** the returned string, which the caller must free. */ -const char * Fl_Text_Buffer::text() { +char * Fl_Text_Buffer::text() { char *t; t = (char *)malloc( mLength + 1 ); @@ -163,7 +163,7 @@ void Fl_Text_Buffer::text( const char *t ) { ** from text buffer "buf". Positions start at 0, and the range does not ** include the character pointed to by "end" */ -const char * Fl_Text_Buffer::text_range( int start, int end ) { +char * Fl_Text_Buffer::text_range( int start, int end ) { char * text; int length, part1Length; @@ -451,7 +451,7 @@ void Fl_Text_Buffer::clear_rectangular( int start, int end, int rectStart, free( (void *) newlineString ); } -const char * Fl_Text_Buffer::text_in_rectangle( int start, int end, +char * Fl_Text_Buffer::text_in_rectangle( int start, int end, int rectStart, int rectEnd ) { int lineStart, selLeft, selRight, len; char *textOut, *outPtr, *retabbedStr; @@ -535,7 +535,7 @@ int Fl_Text_Buffer::selection_position( int *start, int *end, rectEnd ); } -const char * Fl_Text_Buffer::selection_text() { +char * Fl_Text_Buffer::selection_text() { return selection_text_( &mPrimary ); } @@ -575,7 +575,7 @@ int Fl_Text_Buffer::secondary_selection_position( int *start, int *end, rectEnd ); } -const char * Fl_Text_Buffer::secondary_selection_text() { +char * Fl_Text_Buffer::secondary_selection_text() { return selection_text_( &mSecondary ); } @@ -615,7 +615,7 @@ int Fl_Text_Buffer::highlight_position( int *start, int *end, rectEnd ); } -const char * Fl_Text_Buffer::highlight_text() { +char * Fl_Text_Buffer::highlight_text() { return selection_text_( &mHighlight ); } @@ -695,7 +695,7 @@ void Fl_Text_Buffer::remove_modify_callback( Fl_Text_Modify_Cb bufModifiedCB, /* ** Return the text from the entire line containing position "pos" */ -const char * Fl_Text_Buffer::line_text( int pos ) { +char * Fl_Text_Buffer::line_text( int pos ) { return text_range( line_start( pos ), line_end( pos ) ); } @@ -1720,7 +1720,7 @@ int Fl_Text_Selection::includes(int pos, int lineStartPos, int dispIndex) { -const char * Fl_Text_Buffer::selection_text_( Fl_Text_Selection *sel ) { +char * Fl_Text_Buffer::selection_text_( Fl_Text_Selection *sel ) { int start, end, isRect, rectStart, rectEnd; char *text; @@ -2284,5 +2284,5 @@ Fl_Text_Buffer::outputfile(const char *file, int start, int end, int buflen) { // -// End of "$Id: Fl_Text_Buffer.cxx,v 1.9.2.6 2002/05/16 12:47:43 easysw Exp $". +// End of "$Id: Fl_Text_Buffer.cxx,v 1.9.2.7 2002/06/09 13:35:49 easysw Exp $". // diff --git a/src/Fl_Text_Display.cxx b/src/Fl_Text_Display.cxx index 7a493dda9..8a4c85075 100644 --- a/src/Fl_Text_Display.cxx +++ b/src/Fl_Text_Display.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_Text_Display.cxx,v 1.12.2.16 2002/06/06 14:04:53 easysw Exp $" +// "$Id: Fl_Text_Display.cxx,v 1.12.2.17 2002/06/09 13:35:49 easysw Exp $" // // Copyright 2001-2002 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under @@ -1656,6 +1656,9 @@ int Fl_Text_Display::measure_vline( int visLineNum ) { style = ( unsigned char ) mStyleBuffer->character( lineStartPos + i ) - 'A'; + if (style < 0) style = 0; + else if (style >= mNStyles) style = mNStyles - 1; + fl_font( mStyleTable[ style ].font, mStyleTable[ style ].size ); width += ( int ) fl_width( expandedChar, len ); @@ -1948,5 +1951,5 @@ int Fl_Text_Display::handle(int event) { // -// End of "$Id: Fl_Text_Display.cxx,v 1.12.2.16 2002/06/06 14:04:53 easysw Exp $". +// End of "$Id: Fl_Text_Display.cxx,v 1.12.2.17 2002/06/09 13:35:49 easysw Exp $". // diff --git a/test/editor.cxx b/test/editor.cxx index 5319471cb..faed3942a 100644 --- a/test/editor.cxx +++ b/test/editor.cxx @@ -1,5 +1,5 @@ // -// "$Id: editor.cxx,v 1.2.2.3.2.7 2002/01/01 15:11:32 easysw Exp $" +// "$Id: editor.cxx,v 1.2.2.3.2.8 2002/06/09 13:35:49 easysw Exp $" // // A simple text editor program for the Fast Light Tool Kit (FLTK). // @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -50,9 +51,345 @@ int changed = 0; char filename[256] = ""; char title[256]; -Fl_Text_Buffer *textbuf; +Fl_Text_Buffer *textbuf = 0; +// Syntax highlighting stuff... +Fl_Text_Buffer *stylebuf = 0; +Fl_Text_Display::Style_Table_Entry + styletable[] = + { + { FL_BLACK, FL_COURIER, FL_NORMAL_SIZE }, // A - Plain + { FL_DARK_GREEN, FL_COURIER_ITALIC, FL_NORMAL_SIZE }, // B - Line comments + { FL_DARK_GREEN, FL_COURIER_ITALIC, FL_NORMAL_SIZE }, // C - Block comments + { FL_BLUE, FL_COURIER, FL_NORMAL_SIZE }, // D - Strings + { FL_DARK_RED, FL_COURIER, FL_NORMAL_SIZE }, // E - Directives + { FL_DARK_RED, FL_COURIER_BOLD, FL_NORMAL_SIZE }, // F - Types + { FL_BLUE, FL_COURIER_BOLD, FL_NORMAL_SIZE } // G - Keywords + }; +const char *code_keywords[] = // List of known 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 *code_types[] = // List of known 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 +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 +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 == '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'; + } 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_init()' - Initialize the style buffer... +// + +void +style_init(void) { + char *style = new char[textbuf->length() + 1]; + char *text = textbuf->text(); + + + memset(style, 'A', textbuf->length()); + style[textbuf->length()] = '\0'; + + if (stylebuf) delete stylebuf; + + style_parse(text, style, textbuf->length()); + + stylebuf = new Fl_Text_Buffer(textbuf->length()); + stylebuf->text(style); + delete[] style; + free(text); +} + + +// +// 'style_unfinished_cb()' - Update unfinished styles. +// + +void +style_unfinished_cb() { +} + + +// +// 'style_update()' - Update the style buffer... +// + +void +style_update(int pos, // I - Position of update + int nInserted, // I - Number of inserted chars + int nDeleted, // I - Number of deleted chars + int nRestyled, // I - Number of restyled chars + const char *deletedText, // I - Text that was deleted + void *cbArg) { // I - Callback data + 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) { + stylebuf->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'; + + stylebuf->replace(pos, pos + nDeleted, style); + delete[] style; + } else { + // Just delete characters in the style buffer... + stylebuf->remove(pos, pos + nDeleted); + } + + // Select the area that was just updated to avoid unnecessary + // callbacks... + stylebuf->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 = textbuf->line_start(pos); + end = textbuf->line_end(pos + nInserted - nDeleted); + text = textbuf->text_range(start, end); + style = stylebuf->text_range(start, end); + last = style[end - start - 1]; + + style_parse(text, style, end - start); + + stylebuf->replace(start, end, style); + ((Fl_Text_Editor *)cbArg)->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 = textbuf->length(); + text = textbuf->text_range(start, end); + style = stylebuf->text_range(start, end); + + style_parse(text, style, end - start); + + stylebuf->replace(start, end, style); + ((Fl_Text_Editor *)cbArg)->redisplay_range(start, end); + } + + free(text); + free(style); +} + + +// Editor window functions and class... void save_cb(); void saveas_cb(); void find2_cb(Fl_Widget*, void*); @@ -131,6 +468,7 @@ void load_file(char *newfile, int ipos) { else if (!insert) strcpy(filename, newfile); loading = 0; + style_init(); textbuf->call_modify_callbacks(); } @@ -218,6 +556,7 @@ void new_cb(Fl_Widget*, void*) { filename[0] = '\0'; textbuf->select(0, textbuf->length()); textbuf->remove_selection(); + style_init(); changed = 0; textbuf->call_modify_callbacks(); } @@ -390,17 +729,22 @@ Fl_Menu_Item menuitems[] = { }; Fl_Window* new_view() { - EditorWindow* w = new EditorWindow(512, 384, title); + EditorWindow* w = new EditorWindow(660, 400, title); w->begin(); - Fl_Menu_Bar* m = new Fl_Menu_Bar(0, 0, 512, 30); + Fl_Menu_Bar* m = new Fl_Menu_Bar(0, 0, 660, 30); m->copy(menuitems, w); - w->editor = new Fl_Text_Editor(0, 30, 512, 354); + w->editor = new Fl_Text_Editor(0, 30, 660, 370); w->editor->buffer(textbuf); + w->editor->highlight_data(stylebuf, styletable, + sizeof(styletable) / sizeof(styletable[0]), + 'A', style_unfinished_cb, 0); w->editor->textfont(FL_COURIER); w->end(); w->resizable(w->editor); w->callback((Fl_Callback *)close_cb, w); + textbuf->add_modify_callback(changed_cb, w); + textbuf->add_modify_callback(style_update, w->editor); textbuf->call_modify_callbacks(); num_windows++; return w; @@ -408,6 +752,7 @@ Fl_Window* new_view() { int main(int argc, char **argv) { textbuf = new Fl_Text_Buffer; + style_init(); Fl_Window* window = new_view(); @@ -419,5 +764,5 @@ int main(int argc, char **argv) { } // -// End of "$Id: editor.cxx,v 1.2.2.3.2.7 2002/01/01 15:11:32 easysw Exp $". +// End of "$Id: editor.cxx,v 1.2.2.3.2.8 2002/06/09 13:35:49 easysw Exp $". //