1 / 28

FLTK Tutorial

FLTK Tutorial. Introduction Installation Widgets Handling Event System events Mouse events Keyboard events. Introduction. The Fast Light Tool Kit (“FLTK”, pronounced “ fulltick ”) is a cross-platform C++ GUI toolkit FLTK was originally developed by Mr. Bill Spitzak

macy
Download Presentation

FLTK Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FLTK Tutorial

  2. Introduction • Installation • Widgets • Handling Event • System events • Mouse events • Keyboard events

  3. Introduction • The Fast Light Tool Kit (“FLTK”, pronounced “fulltick”) is a cross-platform C++ GUI toolkit • FLTK was originally developed by Mr. Bill Spitzak • FLTK website: http://www.fltk.org/index.php • Documentation: http://www.fltk.org/documentation.php

  4. Why FLTK ? • It’s Free Open Source Software • It’s cross platform • It supports OpenGL • It has a graphical user interface builder called FLUID

  5. Install FLTK • http://www.fltk.org/software.phpget source code and unzip it • Linux/ Unix/ Mac OSX • ./configure && make && make install • Mac Xcode • Xcode project file can be found in fltk-source/ide/ • Windows Visual Studio • Open fltk-source/ide/VisualC/fltk.sln and build • In fltk-source copy FL folder to vc/include • In fltk-source/lib copy all files to vc/lib

  6. Linking • Visual Studio • Add fltk.lib, wsock32.lib, comctl32.lib,fltkgl.lib, fltkforms.lib, and fltkimages.lib (or fltkd.lib, etc.) to linker input • Linux/ Unix • `fltk-config --use-gl --use-images --use-forms --cxxflags –ldflags`

  7. Widgets

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

  9. FLTK Hello World 40 20 180 100 #include <FL/Fl.H> //FLTK global class #include <FL/Fl_Window.H> #include <FL/Fl_Box.H> Int main(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); //tell FLTK that we will not add any more widgets to window window->end(); //show the window and enter the FLTK event loop: window->show(argc, argv); return Fl::run(); } 260 300

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

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

  12. Custom Widgets • Subclass an existing widget • hold a list of child widgets and handle them together

  13. 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.

  14. Main function #include <FL/Fl.H> #include <FL/Fl_Window.H> #include "CustomWidget.h" int main(intargc, 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(); }

  15. Widget is a composition so we will inherit Fl_Group Class CustomWidget : Fl_Group { • Constructor with default FLTK parameters public: CustomWidget(int x, int y, int w, int h, char *l =0) : Fl_Group(x, y, w, h, l);

  16. Our two widgets private: Fl_Int_Input*input; Fl_Slider*slider; • Slider will store our data • Current value • Bounds • Step size

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

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

  19. Constructor: Layout Intconstin_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);

  20. Constructor: Data bounds(1, 100); value(1); • Constructor: Callbacks //The callback is done each time the text is changed by the user input->when(FL_WHEN_CHANGED); input->callback(input_cb, this); slider->callback(slider_cb, this);

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

  22. 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); }

  23. Properties IntCustomWidget::value() { return (int)(slider->value() + 0.5); } void CustomWidget::value(intv) { slider->value(v); slider_cb2(); }

  24. System Events • Focus events • Mouse enters/leaves program • Program gains/loses focus • Clipboard events • Widget events • Activation/deactivation • Show/hide

  25. Mouse Events • Button pushed down • Mouse moved while button pressed (drag) • Button release • Mouse moved • Mouse wheel

  26. Keyboard Events • Key Up/Down

  27. FLTK Events • Override int handle(intevent) • Return 0 if event unused • Event will continue to be passed around • Return 1 if event used • Event is consumed • Slider responds to up/down keys

  28. 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); }

More Related