Page 1 of 1

C++ FLTK BUTTONS TUTORIAL C++ FLTK BUTTONS TUTORIAL Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 31 August 2009 - 04:32 AM

C++ FLTK BUTTONS TUTORIAL


WHAT YOU WILL LEARN IN THIS TUTORIAL:

1. You will learn about FLTK widgets.

2. You will learn about FLTK buttons.

• I. INTRODUCTION

Hello; nice to meet you! Welcome to the “C++ FLTK Buttons Tutorial.”

• II. WIDGET

A widget is a control that defines interaction between a program and a graphical user interface (GUI). A button is a widget that invokes a callback when it is clicked. A menu is a vector of buttons. In_box and Out_box are two Widgets for getting text into and out of your program.

A widget has two functions:

1. show() makes the Widget visible.

A Widget starts out visible.

2. hide() makes the Widget invisible.

• III. BUTTON EXAMPLE 1

Copy and past the following FLTK button example program into your integrated development environment (IDE), then build, and debug the program:

//************************************
//C++ FLTK Buttons Tutorial
//Button Example 1
//************************************
#include <iostream>
#include <FL/Fl.H>
#include <FL/Fl_window.H>
#include <FL/Fl_Button.H>
using namespace std;

void button_cb( Fl_Widget*, void* );  

void make_window()
{
   Fl_Window* win= new Fl_Window( 500,400, "C++ FLTK Buttons Tutorial - Example 1" );
   win->begin();  
	  Fl_Button* but = new Fl_Button( 0, 0, 40, 40, "ON" );
   win->end();
   but -> callback( ( Fl_Callback* ) button_cb );
   win->show();
}

void button_cb( Fl_Widget* obj , void* )
{
   obj->label( "OFF" ); 
   
   obj->resize( 0,0,40,40 ); 
   obj->redraw();
}

int main( int argc, char* argv[] )
{
   make_window();
   return Fl::run();
}



Fl_Window* win= new Fl_Window( 500,400, "C++ FLTK Buttons Tutorial - Example 1" );



Creates a pointer to the new window object setting the width, height and the name in the title bar. Keyword new allocates heap memory for the object.

win->begin();  



States until you reach win->end() new widgets will be added as children to the current Fl_window.

 Fl_Button* but = new Fl_Button( 0, 0, 40, 40, "ON" );



Creates a button, which is a child of the parent window, with input parameters (x, y, width, height, label) where x, y are the position in pixels relative to the top left corner (0,0).

The button is the first child of the window with index 0. Only objects derived from Fl_Group have this child parent relationship.

Deleting the parent, Fl_Window, will automatically delete all the children of the window. For example, if we delete “win,” “but” will be deleted automatically.

win->end();



Sets the current group to the parent of the window.

but -> callback( ( Fl_Callback* ) button_cb );



“button_cb” is the name of our callback function. It is generally accepted programming practice to start or end callback function names with ”cb.”

Callbacks are a means of executing code when an event occurs. In order to use its base classes callback member function, “but” points to an Fl_Button, derived from Fl_Widget.

win->show();



Makes the window on the screen visible.

void button_cb( Fl_Widget* obj , void* )



Declares the callback function and since we passed null by default we do not need void*; however, we used it as a placeholder anyway.

The only two widget members that automatically call redraw are label() and value(). A manual call of redraw is required by everything else.

 obj->resize( 0,0,40,40 ); 



Resizes the button.

obj->redraw();



Redraws the widget.

return Fl::run();



The function run() returns 0 and ends the program.

• IV. BUTTON EXAMPLE 2

Copy and past the following FLTK example program into your IDE, then build, and debug the program:

//************************************
//C++ FLTK Buttons Tutorial
//Button Example 2
//************************************
#include <iostream>
#include <FL/Fl.H>
#include <FL/Fl_window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Output.H>
#include <cstdlib>
using namespace std;

void copy_cb( Fl_Widget*, void* );  
void close_cb( Fl_Widget*, void* ); 
void make_window();

int main( int argc, char* argv[] )
{
   make_window();
   return Fl::run();
}

void make_window()
{
   Fl_Window* win= new Fl_Window(600,250, "C++ FLTK Buttons Tutorial - Example 2");

   win->begin();		
	  Fl_Button*  copy = new Fl_Button( 350, 0,  70, 30, "C&opy" ); //child 0
	  Fl_Button* close = new Fl_Button( 430, 0,  70, 30, "&Quit" );   //child 1
	  Fl_Input*	   inp = new Fl_Input( 20, 0, 140, 30, "In" );		 //child 2
	  Fl_Output*	out = new Fl_Output( 200, 0, 140, 30, "Out" );	//child 3
   win->end();

   copy->callback( (Fl_Callback*) copy_cb );
   close->callback( (Fl_Callback*) close_cb);
   win->show();
 }

void copy_cb( Fl_Widget* obj , void* )
{
   Fl_Button* button=(Fl_Button*)obj;
   const char* temp;   
   temp = (  (Fl_Input*)(button->parent()->child(2))  )->value();
   (  (Fl_Output*)(button->parent()->child(3))  )->value(temp);
}

void close_cb( Fl_Widget* obj, void*)
{
   exit(0);
}



The copy and Quit buttons can be invoked by pressing ALT+c and ALT+q. Placing an "&" in front of the letter in the label parameter in the constructor achieved this form of invoking.

Communicating widgets is demonstrated when "Copy" is pressed and the program copies whatever is in "In" to "Out."

void copy_cb( Fl_Widget* obj , void* )
{
   Fl_Button* button=(Fl_Button*)obj;



“obj” is an Fl_Widget input parameter and “copy_cb” is an Fl_Button callback function; therefore, “obj” refers to an Fl_Button. We cast “obj” to the Fl_Button pointer called “button.”

temp = (  (Fl_Input*)(button->parent()->child(2))  )->value();
(  (Fl_Output*)(button->parent()->child(3))  )->value(temp);



A temporary variable temp is set to the value of the Fl_Output widget.

• V. BUTTON EXAMPLE 3

Copy, past and debug the following FLTK example program. We will use it to see how call backs are done in classes:

//************************************
//C++ FLTK Buttons Tutorial
//Button Example 3
//************************************
#include <iostream>
#include <FL/Fl.H>
#include <FL/Fl_window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Output.H>
#include <cstdlib>
using namespace std;
  
class SimpleWindow : public Fl_Window{ 
 
   public: 
	  SimpleWindow( int w, int h, const char* title ); 
	  ~SimpleWindow(); 
	  Fl_Button* copy; 
	  Fl_Button* quit; 
	  Fl_Input* inp; 
	  Fl_Output* out; 
	 
   private: 
	  static void cb_copy( Fl_Button*, void* ); 
	  inline void cb_copy_i( Fl_Button*, void* ); 
   
	  static void cb_quit( Fl_Button*, void* ); 
	  inline void cb_quit_i( Fl_Button*, void* ); 
};

int main( int argc, char* argv[] )
{
   SimpleWindow win( 600,250,"C++ FLTK Buttons Tutorial - Example 3" ); 
   return Fl::run(); 
} 

SimpleWindow::SimpleWindow( int w, int h, const char* title ):Fl_Window( w,h,title )
{ 
  begin(); 
	  copy = new Fl_Button( 375, 0,  70, 30, "C&opy" ); 
	  copy->callback(( Fl_Callback*)cb_copy, this ); 
	  
	  quit = new Fl_Button( 450, 0,  70, 30, "&Quit" ); 
	  quit->callback(( Fl_Callback*)cb_quit, this ); 
	
	  inp = new Fl_Input( 40, 0, 140, 30, "Input:" ); 
	  out = new Fl_Output( 230, 0, 140, 30, "Output:" );
   end();
   resizable( this );
   show();
} 

SimpleWindow::~SimpleWindow()
{

} 

void SimpleWindow::cb_copy( Fl_Button* obj, void* v )
{  
   SimpleWindow* T=( SimpleWindow* )v; 
   T->cb_copy_i(obj,v); 
} 

void SimpleWindow::cb_copy_i( Fl_Button* , void* )
{ 
   out->value( inp->value() );   
} 

void SimpleWindow::cb_quit( Fl_Button*obj , void* v )
{ 
   SimpleWindow* T=( SimpleWindow* )v; 
   T->cb_quit_i( obj,v ); 
} 

void SimpleWindow::cb_quit_i( Fl_Button* , void* )
{ 
   exit(0);  
} 



Callbacks in a class can only be static.

The get/set functions are overloaded with respect to their return and input parameters.

Get functions have no input parameter. inp->value() gets or returns the value of the const char* widget. The out->value(const char*) sets the value of the outobject.

The “this” pointer is implicit; therefore, you do not have to put a pointer in front of begin(), end(), show() etc.

copy->callback(( Fl_Callback*)cb_copy, this ); 



Utilizes the user data void*.

The address of the class instance () is passed by the “this” pointer and provides access to the entire class in the callback.

• VI. VOID* POINTERS

void* is used to transmit an address between pieces of code that do not know each others type.

A pointer that can point to anything is a void* pointer. A void* pointer has no type; therefore, casting such as a static_cast explicit type conversion is used in the callback function.

Void pointers do not know the size of the object to which they are pointing, they just contain the address; therefore, a void* can never be dereferenced.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1