290 likes | 532 Views
FLTK. Install and Use FLTK Widgets Callbacks Handling event System events Mouse events Keyboard events. Objectives. Linux Package manager Mac < 10.5 Mac Ports *nix ./configure && make && make install Visual Studio Open fltk-source/visualc/fltk.dsw and build
E N D
Install and Use FLTK • Widgets • Callbacks • Handling event • System events • Mouse events • Keyboard events Objectives
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 Installing FLTK 1.1
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` Linking
Basic visual building blocks which are combined intoan applications GUI 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 Common FLTK Widgets
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) Hierarchies and Properties
#include <FL/Fl.H> #include <FL/Fl_Window.H> #include <FL/Fl_Box.H> 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(); } FLTK Hello World
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 FLTK Callbacks
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); ... } Callback Demo
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 Custom Widgets
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 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); Custom Widget
Our two widgets private: Fl_Int_Input*input; Fl_Slider*slider; • Slider will store our data • Current value • Bounds • Step size 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); 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(); 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); 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); 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(); } 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); } Custom Widget
Properties intCustomWidget::value() { return (int)(slider->value() + 0.5); } void CustomWidget::value(intv) { slider->value(v); slider_cb2(); } Custom Widget
Focus events • Mouse enters/leaves program • Program gains/loses focus • Clipboard events • Widget events • Activation/deactivation • Show/hide System Events
Button pushed down • Mouse moved while button pressed (drag) • Button release • Mouse moved • Mouse wheel Mouse 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 Keyboard 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 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); } Custom Widget Events
GUI to produce FLTK source code • Creates C++ code • Compile .fl file into .cxx and .h • Can create your entire program FLUID
Premade dialog boxes • File chooser • Input • Color • Alerts • Images • 2D drawing functions • Threads • Timers Other FLTK Parts