FLTK Tutorial.  Introduction  Installation  Widgets  Handling Event  System events...

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of FLTK Tutorial.  Introduction  Installation  Widgets  Handling Event  System events...

FLTK Tutorial

FLTK TutorialIntroductionInstallationWidgetsHandling EventSystem eventsMouse eventsKeyboard eventsIntroduction

The Fast Light Tool Kit (FLTK, pronounced fulltick) is a cross-platform C++ GUI toolkitFLTK was originally developed by Mr. Bill Spitzak FLTK website: http://www.fltk.org/index.phpDocumentation: http://www.fltk.org/documentation.phpWhy FLTK ?Its Free Open Source SoftwareIts cross platformIt supports OpenGLIt has a graphical user interface builder called FLUIDInstall FLTK

http://www.fltk.org/software.php get source code and unzip itLinux/ Unix/ Mac OSX./configure && make && make installMac XcodeXcode project file can be found in fltk-source/ide/Windows Visual StudioOpen fltk-source/ide/VisualC/fltk.sln and buildIn fltk-source copy FL folder to vc/includeIn fltk-source/lib copy all files to vc/lib

LinkingVisual StudioAdd fltk.lib, wsock32.lib, comctl32.lib,fltkgl.lib, fltkforms.lib, and fltkimages.lib (or fltkd.lib, etc.) to linker inputLinux/ Unix`fltk-config --use-gl --use-images --use-forms --cxxflags ldflags`Widgets

Common FLTK WidgetsButtonsIncludes radio buttons and check boxesText boxDisplay and receive stringsValuatorsDisplay and receive numbersGroupsContainers such as tabs and group boxesAlso includes windows and OpenGL windowsFLTK Hello World#include //FLTK global class#include #include

Int main(int argc, 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);//tell FLTK that we will not add any more widgets to windowwindow->end();//show the window and enter the FLTK event loop:window->show(argc, argv);return Fl::run();}

1803002040260100FLTK CallbacksSets a functions to call when the value of a widget changesvoid functionName(Fl_Widget*, void*)Called function passed pointer to the widget that changed and optional pointer to dataCan be activated by keyboard shortcut

Callback Demovoid button_cb(Fl_Widget *widget, void *data){Fl_Button *button = (Fl_Button*)widget; button->label("Thank you");}

Int main(int argc, char **argv){...Fl_Button *button = new Fl_Button(50, 70, 200, 40, "Click Me");button->callback(button_cb);...}

Custom WidgetsSubclass an existing widgethold a list of child widgets and handle them together

Custom WidgetComposite widgetSlider and text boxWhen the value of one changes the other is updatedWill use slider internally to store dataEasier because already has min, max, etc.

Main function#include #include

#include "CustomWidget.h"

int main(int argc, char **argv){Fl_Window *window = new Fl_Window(300, 120);CustomWidget *customWidget = new CustomWidget(50, 50, 200, 20);window->end();window->show(argc, argv);return Fl::run();}

Widget is a composition so we will inherit Fl_GroupClass CustomWidget : Fl_Group {Constructor with default FLTK parameterspublic:CustomWidget(int x, int y, int w, int h, char *l =0) : Fl_Group(x, y, w, h, l);

Our two widgetsprivate:Fl_Int_Input*input;Fl_Slider*slider;Slider will store our dataCurrent valueBoundsStep size

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);

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();

Constructor: LayoutInt const 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);

Constructor: Databounds(1, 100);value(1);Constructor: Callbacks//The callback is done each time the text is changed by the userinput->when(FL_WHEN_CHANGED);input->callback(input_cb, this);slider->callback(slider_cb, this);

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();}

Callbacks: Update the other widgetvoid CustomWidget::input_cb2(){Int val;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);}

PropertiesInt CustomWidget::value(){return (int)(slider->value() + 0.5);}

void CustomWidget::value(intv){slider->value(v);slider_cb2();}

System EventsFocus eventsMouse enters/leaves programProgram gains/loses focusClipboard eventsWidget eventsActivation/deactivationShow/hide

Mouse EventsButton pushed downMouse moved while button pressed (drag)Button releaseMouse movedMouse wheel

Keyboard EventsKey Up/Down

FLTK EventsOverride int handle(int event)Return 0 if event unusedEvent will continue to be passed aroundReturn 1 if event usedEvent is consumedSlider responds to up/down keys

Int CustomWidget::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);}