FLTK Tutorial by Xu
Download
Report
Transcript FLTK Tutorial by Xu
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
FLTK website: http://www.fltk.org/index.php
Documentation: http://www.fltk.org/documentation.php
Why FLTK ?
It’s Free Open Source Software
It’s cross platform
It supports OpenGL
It has a graphical user interface builder called FLUID
Install FLTK
http://www.fltk.org/software.php get source code and unzip it
Linux/ Unix/ Mac OSX
Mac Xcode
./configure && make && make install
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
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`
Widgets
Common FLTK Widgets
Buttons
Text box
Display and receive strings
Valuators
Includes radio buttons and check boxes
Display and receive numbers
Groups
Containers such as tabs and group boxes
Also includes windows and OpenGL windows
FLTK Hello World
40
#include <FL/Fl.H>
20
180
//FLTK global class
100
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
260
Int main(int argc, char **argv)
{
300
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();
}
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
Callback Demo
void 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 Widgets
Subclass an existing widget
hold a list of child widgets and handle them together
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.
Main function
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#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_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);
Our two widgets
private:
Fl_Int_Input*input;
Fl_Slider*slider;
Slider will store our data
Current value
Bounds
Step size
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);
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();
Constructor: Layout
Int 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: 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);
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();
}
Callbacks: Update the other widget
void 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);
}
Properties
Int CustomWidget::value()
{
return (int)(slider->value() + 0.5);
}
void CustomWidget::value(intv)
{
slider->value(v);
slider_cb2();
}
System Events
Focus events
Mouse enters/leaves program
Program gains/loses focus
Clipboard events
Widget events
Activation/deactivation
Show/hide
Mouse Events
Button pushed down
Mouse moved while button pressed (drag)
Button release
Mouse moved
Mouse wheel
Keyboard Events
Key Up/Down
FLTK Events
Override int handle(int event)
Return 0 if event unused
Return 1 if event used
Event will continue to be passed around
Event is consumed
Slider 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);
}