fltk. objectives install and use fltk widgets ◦callbacks handling event ◦system events ◦mouse...
TRANSCRIPT
![Page 1: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/1.jpg)
FLTKFLTK
![Page 2: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/2.jpg)
ObjectivesObjectives
Install and Use FLTKWidgets
◦CallbacksHandling event
◦System events◦Mouse events◦Keyboard events
![Page 3: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/3.jpg)
Installing FLTK 1.1Installing 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
![Page 4: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/4.jpg)
Linking 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`
![Page 5: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/5.jpg)
WidgetsWidgets
Basic visual building blocks which are combined intoan applications GUI
![Page 6: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/6.jpg)
Common FLTK WidgetsCommon 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
![Page 7: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/7.jpg)
Hierarchies and PropertiesHierarchies 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)
![Page 8: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/8.jpg)
FLTK Hello WorldFLTK Hello World
#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();
}
![Page 9: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/9.jpg)
FLTK CallbacksFLTK 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
![Page 10: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/10.jpg)
Callback DemoCallback 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);...
}
![Page 11: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/11.jpg)
Custom WidgetsCustom 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
![Page 12: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/12.jpg)
Custom WidgetCustom Widget
Composite widgetSlider and text boxWhen the value of one changes the other
is updatedWill use slider internally to store data
◦Easier because already has min, max, etc.Improvements
◦Validate text box input
![Page 13: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/13.jpg)
Custom WidgetCustom Widget
Widget is a composition so we will inherit Fl_Group
Class CustomWidget : Fl_Group {Constructor with default FLTK parameterspublic:CustomWidget(intx, inty, intw, inth, char *l =0) : Fl_Group(x, y, w, h, l);
![Page 14: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/14.jpg)
Custom WidgetCustom Widget
Our two widgetsprivate:Fl_Int_Input*input;Fl_Slider*slider;Slider will store our data
◦Current value◦Bounds◦Step size
![Page 15: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/15.jpg)
Custom WidgetCustom Widget
Common slider propertiespublic:int value();void value(intv);
int minimum();void minimum(int min);
int maximum();void maximum(int max);void bounds(int min, int max);
![Page 16: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/16.jpg)
Custom WidgetCustom Widget
Internal callbacksstatic void input_cb(Fl_Widget *w, void *d);
static void slider_cb(Fl_Widget *w, void *d);
void input_cb2();void slider_cb2();
![Page 17: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/17.jpg)
Custom WidgetCustom Widget
Constructor: Layoutintconst 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);
![Page 18: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/18.jpg)
Custom WidgetCustom Widget
Constructor: Databounds(1, 100);value(1);Constructor: Callbacksinput->when(FL_WHEN_CHANGED);input->callback(input_cb, this);slider->callback(slider_cb, this);
![Page 19: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/19.jpg)
Custom WidgetCustom Widget
Static callbacksvoid 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();
}
![Page 20: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/20.jpg)
Custom WidgetCustom Widget
Callbacks: Update the other widgetvoid 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);
}
![Page 21: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/21.jpg)
Custom WidgetCustom Widget
PropertiesintCustomWidget::value(){return (int)(slider->value() + 0.5);
}
void CustomWidget::value(intv){slider->value(v);slider_cb2();
}
![Page 22: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/22.jpg)
System EventsSystem Events
Focus events◦Mouse enters/leaves program◦Program gains/loses focus
Clipboard eventsWidget events
◦Activation/deactivation◦Show/hide
![Page 23: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/23.jpg)
Mouse EventsMouse Events
Button pushed downMouse moved while button pressed (drag)Button releaseMouse movedMouse wheel
![Page 24: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/24.jpg)
Keyboard EventsKeyboard Events
Propagate through widgets until handledKey Up/Down
◦Event trigger on both key press and release◦Sent to widget with focus, then parents, then
becomes a shortcutShortcuts
◦Sent to widget under mouse, then parents, then every widget
![Page 25: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/25.jpg)
Custom Widget EventsCustom 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 consumedSlider responds to left/right keys
◦Expand to include up/down keys
![Page 26: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/26.jpg)
Custom Widget EventsCustom 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);
}
![Page 27: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/27.jpg)
FLUIDFLUID
GUI to produce FLTK source codeCreates C++ code
◦Compile .fl file into .cxx and .hCan create your entire program
![Page 28: FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f125503460f94c25a0a/html5/thumbnails/28.jpg)
Other FLTK PartsOther FLTK Parts
Premade dialog boxes◦File chooser◦Input◦Color◦Alerts
Images2D drawing functionsThreadsTimers