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());
}