highlight a line of text in Text_Display?

91 views
Skip to first unread message

Vimal Parikh

unread,
Dec 2, 2013, 6:27:42 PM12/2/13
to fltkg...@googlegroups.com
I have a simple Text_Display widget and want to highlight multiple lines of text.
I have tried using 'highlight(start,end)' function of text buffer but it doesn't show any highlighting.

What is the right way to do this? Is there an example code?

Also, Is there a way to choose the color of the highlight ? 


Sanel Zukan

unread,
Dec 3, 2013, 5:28:25 AM12/3/13
to fltkg...@googlegroups.com
You can use http://www.fltk.org/doc-1.3/editor.html as starting point;
there are explained details how to make own editor with syntax
highlighting.

If you are interested in more advanced approach with easy adding new
languages you can check https://github.com/sanel/Fl_Highlight_Editor.

Best.

Greg Ercolano

unread,
Dec 4, 2013, 9:39:10 AM12/4/13
to fltkg...@googlegroups.com
On 12/2/13 6:27 PM, Vimal Parikh wrote:
> I have a simple Text_Display widget and want to highlight multiple lines of text.
> I have tried using 'highlight(start,end)' function of text buffer but it doesn't show any highlighting.

Do you find if you resize the window, the highlight appears?

You probably just need to call redraw() after making changes
(such as setting the highlight()) on either the widget or window
so that it redraws with the highlight.

> Is there an example code?

FWIW, I started with this simple example from my cheat page:
http://seriss.com/people/erco/fltk/#Fl_Text_Display
..and added two lines to demonstrate the highlight()
and selection_color() calls (highlighted with comments in the below code).

> Also, Is there a way to choose the color of the highlight ?

Fl_Text_Display::selection_color()

Though I /think/ it's applying some kind of contrast algorithm
to the color supplied; in my tests with the below code, it's
picking a lighter color than the one I specify.

In the below code I don't need to call redraw() because I'm
setting the highlight as part of the initialization (the window
hasn't drawn yet, so the redraw() is implied). But if you change
the highlight due to an event or callback, calling redraw on the
display widget or window is advised.


#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Display.H>
int main() {
Fl_Window *win = new Fl_Window(640, 480);
Fl_Text_Buffer *buff = new Fl_Text_Buffer();
Fl_Text_Display *disp = new Fl_Text_Display(20, 20, 640-40, 480-40, "Display");
disp->buffer(buff);
win->resizable(*disp);
win->show();
buff->text("line 0\nline 1\nline 2\n"
"line 3\nline 4\nline 5\n"
"line 6\nline 7\nline 8\n"
"line 9\nline 10\nline 11\n"
"line 12\nline 13\nline 14\n"
"line 15\nline 16\nline 17\n"
"line 18\nline 19\nline 20\n"
"line 21\nline 22\nline 23\n");
buff->highlight(10, 150); // *** highlight from char #10 to char #150
disp->selection_color(FL_RED); // *** change selection color
return(Fl::run());
}

Reply all
Reply to author
Forward
0 new messages