Presentation is loading. Please wait.

Presentation is loading. Please wait.

FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events.

Similar presentations


Presentation on theme: "FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events."— Presentation transcript:

1 FLTK

2 Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events

3 Installing FLTK 1.1 Linux ◦Package manager Mac < 10.5 ◦Mac Ports *nix ◦./configure && make && make install Visual Studio ◦Open fltk-source/visualc/fltk.dsw and build ◦In fltk-source copy FL folder to vc/include ◦In fltk-source/lib copy all files to vc/lib

4 Linking Visual Studio ◦Add fltk.lib, wsock32.lib, comctl32.lib,fltkgl.lib, fltkforms.lib, and fltkimages.lib (or fltkd.lib, etc.) to linker input *nix ◦`fltk-config --use-gl --use-images --use-forms --cxxflags –ldflags`

5 Widgets Basic visual building blocks which are combined intoan applications GUI

6 Common FLTK Widgets Buttons ◦Includes radio buttons and check boxes Text ◦Display and receive strings Valuators ◦Display and receive numbers Groups ◦Containers such as tabs and group boxes ◦Also includes windows and OpenGL windows

7 Hierarchies and Properties Parent Child relationship ◦Created widgets are added to current group ◦Created group widgets start their own group  Stopped by calling widget->end(); ◦Can manually add widgets to groups Get / Set Methods ◦Change style, properties, values etc. ◦Get: int minimum() ◦Set: void minimum(int)

8 FLTK Hello World #include intmain(intargc, char **argv) { Fl_Window *window = new Fl_Window(300, 180); Fl_Box *box = new Fl_Box(20, 40, 260, 100, "Hello World"); box->box(FL_UP_BOX); window->end(); window->show(argc, argv); return Fl::run(); }

9 FLTK Callbacks Sets a functions to called when the value of a widget changes ◦ void functionName(Fl_Widget*, void*) Called function is passed pointer to the widget that changed and optional pointer to data Can be activated by keyboard shortcut

10 Callback Demo void button_cb(Fl_Widget *widget, void *data) { Fl_Button*button = (Fl_Button*)widget; button->label("Thank you"); } intmain(intargc, char **argv) {... Fl_Button *button = new Fl_Button(50, 70, 200, 40, "Click Me"); button->callback(button_cb);... }

11 Custom Widgets Subclass an existing widget ◦Control widget to get/receive a value ◦Composite widget to hold a list of child widgets and handle them together ◦Can also subclass existing widget and change

12 Custom Widget Composite widget Slider and text box When the value of one changes the other is updated Will use slider internally to store data ◦Easier because already has min, max, etc. Improvements ◦Validate text box input

13 Custom Widget Widget is a composition so we will inherit Fl_Group Class CustomWidget : Fl_Group { Constructor with default FLTK parameters public: CustomWidget(intx, inty, intw, inth, char *l =0) : Fl_Group(x, y, w, h, l);

14 Custom Widget Our two widgets private: Fl_Int_Input*input; Fl_Slider*slider; Slider will store our data ◦Current value ◦Bounds ◦Step size

15 Custom Widget Common slider properties public: int value(); void value(intv); int minimum(); void minimum(int min); int maximum(); void maximum(int max); void bounds(int min, int max);

16 Custom Widget Internal callbacks static void input_cb(Fl_Widget *w, void *d); static void slider_cb(Fl_Widget *w, void *d); void input_cb2(); void slider_cb2();

17 Custom Widget Constructor: Layout intconst in_w = 40; input = new Fl_Int_Input(x, y, in_w, h); slider = new Fl_Slider(x + in_w, y, w- in_w, h); slider->type(FL_HOR_SLIDER);

18 Custom Widget Constructor: Data bounds(1, 100); value(1); Constructor: Callbacks input->when(FL_WHEN_CHANGED); input->callback(input_cb, this); slider->callback(slider_cb, this);

19 Custom Widget Static callbacks void CustomWidget::input_cb(Fl_Widget *w, void *d) { ((CustomWidget*)d)->input_cb2(); } void CustomWidget::slider_cb(Fl_Widget *w, void *d) { ((CustomWidget*)d)->slider_cb2(); }

20 Custom Widget Callbacks: Update the other widget void CustomWidget::input_cb2() { intval; sscanf(input->value(), "%d", &val); slider->value(val); } void CustomWidget::slider_cb2() { char val[16]; sprintf(val, "%d", (int)(slider->value() + 0.5)); input->value(val); }

21 Custom Widget Properties intCustomWidget::value() { return (int)(slider->value() + 0.5); } void CustomWidget::value(intv) { slider->value(v); slider_cb2(); }

22 System Events Focus events ◦Mouse enters/leaves program ◦Program gains/loses focus Clipboard events Widget events ◦Activation/deactivation ◦Show/hide

23 Mouse Events Button pushed down Mouse moved while button pressed (drag) Button release Mouse moved Mouse wheel

24 Keyboard Events Propagate through widgets until handled Key Up/Down ◦Event trigger on both key press and release ◦Sent to widget with focus, then parents, then becomes a shortcut Shortcuts ◦Sent to widget under mouse, then parents, then every widget

25 Custom Widget Events Override inthandle(int event) ◦Return 0 if event unused  Event will continue to be passed around ◦Return 1 if event used  Event is consumed Slider responds to left/right keys ◦Expand to include up/down keys

26 Custom Widget Events intCustomWidget::handle(int event) { if ( event == FL_KEYDOWN ) { if ( Fl::event_key() == FL_Up ) { value(value() + 1); return 1; } else if ( Fl::event_key() == FL_Down ) { value(value() - 1); return 1; } return Fl_Group::handle(event); }

27 FLUID GUI to produce FLTK source code Creates C++ code ◦Compile.fl file into.cxx and.h Can create your entire program

28 Other FLTK Parts Premade dialog boxes ◦File chooser ◦Input ◦Color ◦Alerts Images 2D drawing functions Threads Timers


Download ppt "FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events."

Similar presentations


Ads by Google