From c3cde61e9879825c1547e366e2b9becedc1179e7 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Sun, 9 Jun 2002 18:28:49 +0000 Subject: Add documentation for using styles in text editor. Add placeholder for style attributes - hidden + underlined - for future use. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@2301 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- FL/Fl_Text_Display.H | 18 ++- documentation/editor.html | 286 +++++++++++++++++++++++++++++++++++++++++++++- src/Fl_Text_Display.cxx | 18 ++- test/editor.cxx | 13 +-- 4 files changed, 315 insertions(+), 20 deletions(-) diff --git a/FL/Fl_Text_Display.H b/FL/Fl_Text_Display.H index 754cc10cb..575748758 100644 --- a/FL/Fl_Text_Display.H +++ b/FL/Fl_Text_Display.H @@ -1,5 +1,5 @@ // -// "$Id: Fl_Text_Display.H,v 1.4.2.7 2002/06/08 12:51:38 easysw Exp $" +// "$Id: Fl_Text_Display.H,v 1.4.2.8 2002/06/09 18:28:48 easysw Exp $" // // Header file for Fl_Text_Display class. // @@ -55,10 +55,18 @@ class Fl_Text_Display: public Fl_Group { typedef void (*Unfinished_Style_Cb)(); + // style attributes - currently not implemented! + enum { + ATTR_NONE = 0, + ATTR_UNDERLINE = 1, + ATTR_HIDDEN = 2 + }; + struct FL_EXPORT Style_Table_Entry { - Fl_Color color; - Fl_Font font; - int size; + Fl_Color color; + Fl_Font font; + int size; + unsigned attr; }; FL_EXPORT Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0); @@ -234,5 +242,5 @@ class Fl_Text_Display: public Fl_Group { #endif // -// End of "$Id: Fl_Text_Display.H,v 1.4.2.7 2002/06/08 12:51:38 easysw Exp $". +// End of "$Id: Fl_Text_Display.H,v 1.4.2.8 2002/06/09 18:28:48 easysw Exp $". // diff --git a/documentation/editor.html b/documentation/editor.html index 3fb5c43b0..56de1ad71 100644 --- a/documentation/editor.html +++ b/documentation/editor.html @@ -105,7 +105,7 @@ Fl_Menu_Item menuitems[] = { Fl_Menu_Bar widget and assign the menus to it with:

@@ -118,7 +118,7 @@ m->copy(menuitems); widget to edit the text: @@ -598,5 +598,287 @@ The final editor window should look like the image in Figure 4-2.

The completed editor window.
Figure 4-2: The completed editor window

+

Advanced Features

+ +

Now that we've implemented the basic functionality, it is +time to show off some of the advanced features of the +Fl_Text_Editor widget. + +

Syntax Highlighting

+ +

The Fl_Text_Editor widget supports highlighting +of text with different fonts, colors, and sizes. The +implementation is based on the excellent NEdit text editor core, which +uses a parallel "style" buffer which tracks the font, color, and +size of the text that is drawn. + +

Styles are defined using the +Fl_Text_Display::Style_Table_Entry structure +defined in <FL/Fl_Text_Display.H>: + +

+ +

The color member sets the color for the text, +the font member sets the FLTK font index to use, +and the size member sets the pixel size of the +text. The attr member is currently not used. + +

For our text editor we'll define 7 styles for plain code, +comments, keywords, and preprocessor directives: + +

+ +

You'll notice that the comments show a letter next to each +style - each style in the style buffer is referenced using a +character starting with the letter 'A'. + +

You call the highlight_data() method to associate the +style data and buffer with the text editor widget: + +

+ +

Finally, you need to add a callback to the main text buffer so +that changes to the text buffer are mirrored in the style buffer: + +

+ +

The style_update() function, like the change_cb() +function described earlier, is called whenever text is added or removed from +the text buffer. It mirrors the changes in the style buffer and then updates +the style data as necessary: + +

+ +

The style_parse() function scans a copy of the +text in the buffer and generates the necessary style characters +for display. It assumes that parsing begins at the start of a line: + +

+ + diff --git a/src/Fl_Text_Display.cxx b/src/Fl_Text_Display.cxx index 8a4c85075..89011d023 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.17 2002/06/09 13:35:49 easysw Exp $" +// "$Id: Fl_Text_Display.cxx,v 1.12.2.18 2002/06/09 18:28:49 easysw Exp $" // // Copyright 2001-2002 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under @@ -1045,7 +1045,11 @@ void Fl_Text_Display::draw_string( int style, int X, int Y, int toX, Fl_Color background; if ( style & STYLE_LOOKUP_MASK ) { - styleRec = &mStyleTable[ ( style & STYLE_LOOKUP_MASK ) - 'A' ]; + int si = (style & STYLE_LOOKUP_MASK) - 'A'; + if (si < 0) si = 0; + else if (si >= mNStyles) si = mNStyles - 1; + + styleRec = mStyleTable + si; font = styleRec->font; size = styleRec->size; @@ -1234,8 +1238,12 @@ int Fl_Text_Display::string_width( const char *string, int length, int style ) { int size; if ( style & STYLE_LOOKUP_MASK ) { - font = mStyleTable[ ( style & STYLE_LOOKUP_MASK ) - 'A' ].font; - size = mStyleTable[ ( style & STYLE_LOOKUP_MASK ) - 'A' ].size; + int si = (style & STYLE_LOOKUP_MASK) - 'A'; + if (si < 0) si = 0; + else if (si >= mNStyles) si = mNStyles - 1; + + font = mStyleTable[si].font; + size = mStyleTable[si].size; } else { font = textfont(); size = textsize(); @@ -1951,5 +1959,5 @@ int Fl_Text_Display::handle(int event) { // -// End of "$Id: Fl_Text_Display.cxx,v 1.12.2.17 2002/06/09 13:35:49 easysw Exp $". +// End of "$Id: Fl_Text_Display.cxx,v 1.12.2.18 2002/06/09 18:28:49 easysw Exp $". // diff --git a/test/editor.cxx b/test/editor.cxx index faed3942a..fd1f54a25 100644 --- a/test/editor.cxx +++ b/test/editor.cxx @@ -1,5 +1,5 @@ // -// "$Id: editor.cxx,v 1.2.2.3.2.8 2002/06/09 13:35:49 easysw Exp $" +// "$Id: editor.cxx,v 1.2.2.3.2.9 2002/06/09 18:28:49 easysw Exp $" // // A simple text editor program for the Fast Light Tool Kit (FLTK). // @@ -57,8 +57,7 @@ Fl_Text_Buffer *textbuf = 0; // Syntax highlighting stuff... Fl_Text_Buffer *stylebuf = 0; Fl_Text_Display::Style_Table_Entry - styletable[] = - { + styletable[] = { // Style table { 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 @@ -67,8 +66,7 @@ Fl_Text_Display::Style_Table_Entry { 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... - { +const char *code_keywords[] = { // List of known C/C++ keywords... "and", "and_eq", "asm", @@ -104,8 +102,7 @@ const char *code_keywords[] = // List of known C/C++ keywords... "xor", "xor_eq" }; -const char *code_types[] = // List of known C/C++ types... - { +const char *code_types[] = { // List of known C/C++ types... "auto", "bool", "char", @@ -764,5 +761,5 @@ int main(int argc, char **argv) { } // -// End of "$Id: editor.cxx,v 1.2.2.3.2.8 2002/06/09 13:35:49 easysw Exp $". +// End of "$Id: editor.cxx,v 1.2.2.3.2.9 2002/06/09 18:28:49 easysw Exp $". // -- cgit v1.2.3