#include #include #include #include #include #include class ImgBox: public Fl_Box { public: ImgBox(int x, int y, int w, int h); protected: void draw(); int handle(int e); private: }; #define MAX_X 399 #define MAX_Y 199 typedef struct { Fl_Window *window; Fl_Group *bg; Fl_Button* ab; Fl_Button* bb; ImgBox* ibox; } widgets; widgets w; typedef enum { A_MODE, B_MODE, } mode; Fl_Input* intxt=0; mode curr_mode = A_MODE; void ab_callback(Fl_Widget* wd, void* data); void bb_callback(Fl_Widget* wd, void* data); void mainwin_callback(Fl_Widget* wd, void* data); void reset_all_button_color(); void rem_input_box(); void create_input_box(int mouse_x, int mouse_y); int main(int argc, char **argv); //****************** void reset_all_button_color() { w.ab->color(FL_BACKGROUND_COLOR); w.ab->redraw(); w.bb->color(FL_BACKGROUND_COLOR); w.bb->redraw(); } void create_input_box(int mouse_x, int mouse_y) { intxt = new Fl_Input(w.ibox->x()+mouse_x, w.ibox->y()+mouse_y, 100, 24); Fl::focus(intxt); w.window->add(*intxt); intxt->redraw(); } void rem_input_box() { if(intxt != NULL) { ::w.window->remove(*intxt); //if mouse cursor is on the input box, it becomes I cursor. //when we type into input text box, mouse cursor I disappears. //if, while typing in input text box, we press escape button, //1. mouse cursor will anyway disappear for our typing, //2. input text box will disappear for escape key press, //then we have no cursor at all in the whole window. //so below code will help in restoring cursor for the window. //::w.window->cursor(FL_CURSOR_DEFAULT); //for cursor disappearing issue Fl::delete_widget(intxt); intxt = 0; } } //****************** ImgBox::ImgBox(int x, int y, int w, int h):Fl_Box(x, y, w, h) { } int ImgBox::handle(int e) { if (curr_mode == A_MODE) { if(e == FL_PUSH && Fl::event_button() == FL_LEFT_MOUSE) { int mouse_x = Fl::event_x() - Fl_Box::x(); int mouse_y = Fl::event_y() - Fl_Box::y(); rem_input_box(); create_input_box(mouse_x, mouse_y); redraw(); return 1; } if(e == FL_RELEASE && Fl::event_button() == FL_LEFT_MOUSE) { return 1; } } return(Fl_Box::handle(e)); } void ImgBox::draw() { Fl_Box::draw(); } //****************** void ab_callback(Fl_Widget* wd, void* data) { curr_mode = A_MODE; reset_all_button_color(); w.ab->color(FL_BLUE); Fl::focus(w.ab); w.ab->redraw(); } void bb_callback(Fl_Widget* wd, void* data) { if(intxt != NULL) { rem_input_box(); ::w.ibox->redraw(); } curr_mode = B_MODE; reset_all_button_color(); w.bb->color(FL_BLUE); Fl::focus(w.bb); w.bb->redraw(); } void mainwin_callback(Fl_Widget* wd, void* data) { if (Fl::event()==FL_SHORTCUT && Fl::event_key()==FL_Escape) { if(curr_mode == A_MODE) { bb_callback(0, 0); } else { ab_callback(0, 0); } w.ibox->redraw(); return; } else { exit(0); } } //****************** int main(int argc, char **argv) { int m=10; int bgw=MAX_X+1, bgh=25; int bw=75, bh=25; int ibw=MAX_X+1, ibh=MAX_Y+1; int ww=0; int wh=0; w.window = new Fl_Window(5, 5, ww, wh, "Test"); { w.bg = new Fl_Group(m, m, bgw, bgh); { w.ab = new Fl_Button(w.bg->x() +(bw+m)*0, w.bg->y(), bw, bh, "A"); w.ab->callback(ab_callback); w.ab->color(FL_BLUE); w.bb = new Fl_Button(w.bg->x() +(bw+m)*1, w.bg->y(), bw, bh, "B"); w.bb->callback(bb_callback); } w.bg->end(); w.ibox = new ImgBox(m, m+bgh+m, ibw, ibh); w.ibox->box(FL_FLAT_BOX); w.ibox->color(FL_WHITE); ww = m+bgw+m; wh = m+bgh+m+ibh+m; w.window->size(ww, wh); } w.window->end(); w.window->callback(mainwin_callback); w.window->show(); return Fl::run(); }