fltk. objectives install and use fltk widgets ◦callbacks handling event ◦system events ◦mouse...
TRANSCRIPT
FLTKFLTK
ObjectivesObjectives
Install and Use FLTKWidgets
◦CallbacksHandling event
◦System events◦Mouse events◦Keyboard events
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
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`
WidgetsWidgets
Basic visual building blocks which are combined intoan applications GUI
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
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)
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();
}
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
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);...
}
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
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
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);
Custom WidgetCustom Widget
Our two widgetsprivate:Fl_Int_Input*input;Fl_Slider*slider;Slider will store our data
◦Current value◦Bounds◦Step size
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);
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();
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);
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);
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();
}
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);
}
Custom WidgetCustom Widget
PropertiesintCustomWidget::value(){return (int)(slider->value() + 0.5);
}
void CustomWidget::value(intv){slider->value(v);slider_cb2();
}
System EventsSystem Events
Focus events◦Mouse enters/leaves program◦Program gains/loses focus
Clipboard eventsWidget events
◦Activation/deactivation◦Show/hide
Mouse EventsMouse Events
Button pushed downMouse moved while button pressed (drag)Button releaseMouse movedMouse wheel
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
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
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);
}
FLUIDFLUID
GUI to produce FLTK source codeCreates C++ code
◦Compile .fl file into .cxx and .hCan create your entire program
Other FLTK PartsOther FLTK Parts
Premade dialog boxes◦File chooser◦Input◦Color◦Alerts
Images2D drawing functionsThreadsTimers