On 05/16/12 22:59, Greg Ercolano wrote:
> What's the goal: is it to add up/dn/lt/rt key control
> to the caret position of the text display widget?
>
> If so, I'd think you'd want to derive a class from
> Fl_Text_Display to handle() the key events.
This seems to work reliably wrt to displaying the caret,
and doesn't seem to need the explicit take_focus() call.
This is probably because here we're responding to FL_FOCUS/UNFOCUS,
and I'm calling redraw() on those events to properly show/hide
the cursor, which is probably why Fl_Text_Display wasn't drawing
the caret correctly. (Showing the caret in Fl_Text_Display was
probably never tested during its development)
This example just shows adding caret cursor motion for
up/dn/lt/right keys. It doesn't show copy selection, etc.
But hopefully that too is easily added by checking the state keys,
ie. checking Fl::event_state() for FL_SHIFT, FL_CTRL, etc.
The following should be a good start.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Display.H>
//
// Add up/dn/lt/rt keynav to Fl_Text_Display caret cursor -erco 5/16/12
//
class MyTextDisplay : public Fl_Text_Display {
public:
MyTextDisplay(int X,int Y,int W,int H,const char*L=0) :
Fl_Text_Display(X,Y,W,H,L) {
show_cursor(); // ENABLE CURSOR
cursor_color(0xFF000000); // CURSOR RED
cursor_style(Fl_Text_Display::HEAVY_CURSOR);
//take_focus(); // UNNEEDED
}
int handle(int e) {
int ret = Fl_Text_Display::handle(e); // let text display handle
event first
switch (e) {
case FL_FOCUS:
case FL_UNFOCUS:
case FL_KEYUP:
redraw(); // show/hide cursor
return 1;
case FL_KEYBOARD: {
switch (Fl::event_key()) {
case FL_Up: printf("UP\n"); move_up(); break;
case FL_Down: printf("DOWN\n"); move_down(); break;
case FL_Left: printf("LEFT\n"); move_left(); break;
case FL_Right: printf("RIGHT\n"); move_right(); break;
}
break;
}
}
return(ret);
}
};
int main() {
Fl_Window *win = new Fl_Window(640,480,"Display");
MyTextDisplay *mDisp = new MyTextDisplay(20,20,640-40,480-40);
Fl_Text_Buffer *mBuff = new Fl_Text_Buffer();
mDisp->buffer(mBuff);
mBuff->text("line 0\nline 1\nline 2\nline 3\nline 4\nline 5\n");
mDisp->insert_position(5);
win->end();
win->show();
return Fl::run();
}
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev