Page 1 of 1

C++ CLASS DERIVED FROM FLTK WIDGET TUTORIAL PART 1 C++ CLASS DERIVED FROM FLTK WIDGET TUTORIAL PART 1 Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

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

Posted 06 September 2009 - 02:33 AM

C++ CLASS DERIVED FROM FLTK WIDGET TUTORIAL PART 1



YOU WILL LEARN HOW TO CODE A C++ FLTK:

1. Blank window.

2. Window button.

3. userdata.

• I. INTRODUCTION

Hello; nice to meet you! Welcome to the “C++ CLASS DERIVED FROM FLTK WIDGET TUTORIAL PART 1.”

This tutorial is a very brief overview of information presented by Dr. Bjarne Stroustrup in his book “Programming Principles and Practices Using C++,” Addison-Wesley 2009, ISBN 978-0321543721.

The web site for his book and the appropriate FLTX header files is:

http://www.stroustrup.com/Programming/

• II. EXAMPLE 1

Please copy and paste Example 1 into your IDE then build and debug.

//************************************
//Exampe 1
//Blank Window
//************************************
#include <Fl/Fl.H>			//Required by all FLTK programs.
#include <Fl/Fl_window.H>	 //Required for window class.

int main()
{
	Fl_Window win(600,400);   //Creates window width 600 height 400.
	win.show();			   //Tells FLTK to show the window.
	return(Fl::run());		//Returns value of FLTK command loop, this
							  //function returns only when all windows are closed.
}



Example 1 creates an empty window with no window title and no buttons.

There is one include file for every class in FLTK. For example:

#include <Fl/Fl_window.H> 



Is the include file for the Fl_Window class.

In the following statement:

 Fl_Window win(600,400);



win is the object name instantiated by the class window.

When an object of the class is instantiated the parentheses following the object name enclose the arguments required by the class constructor. In this case the first argument is the width and the second argument is the height. The third argument is an optional title character string.

return(Fl::run()); 



Returns the value of FLTK command loop, this function returns only when all windows are closed.

• III. EXAMPLE 2

Please copy and paste Example 2 into your IDE then build and debug.

//Exampe 2
//Adding a button
//************************************
#include <Fl/Fl.H>			//Required by all FLTK programs.
#include <Fl/Fl_window.H>	 //Required for window class.
#include <FL/FL_Button.H>	 //Required for button class.

int main()
{
	Fl_Window win(600,400,"FLTK is easy to learn and fun to use.");   //Creates window width 600 height 400.
	Fl_Button button(5,5,75,30,"Click Me");  //Button arguments starting point(x,y),width,heigth,"label."
	win.show();			   //Tells FLTK to show the window.
	return(Fl::run());		//Returns value of FLTK command loop, this
							  //function returns only when all windows are closed.
}



Below the window definition we added a call for a button.

Fl_Button button(5,5,75,30,"Click Me"); 



The button is a rectangle. A rectangle is defined by a (x,y) starting point for its top left corner of the rectangle and its width and its height. The first two arguments are the top left corner (x,y) starting point inside of the window. The third and fourth arguments are the width and height respectively. The fifth argument is a character string title for the button.

Since we are using the Button class we added an include file for button:

#include <FL/FL_Button.H> 



And, we added a title to the window.

Fl_Window win(600,400,"FLTK is easy to learn and fun to use.");   



• IV. EXAMPLE 3

Please copy and paste Example 3 into your IDE then build and debug.

//************************************
//Exampe 3
//Adding a button callback.
//************************************
#include <Fl/Fl.H>			//Required by all FLTK programs.
#include <Fl/Fl_window.H>	 //Required for window class.
#include <FL/FL_Button.H>	 //Required for button class.
#include <stdio.h>			//Required for display.

void My_Callback(Fl_Widget*w, void* userdata)
{
	 fprintf(stderr, "Button works!\n");
}

int main()
{
	Fl_Window win(600,400,"FLTK is fun to learn and easy to use.");   //Creates window width 600 height 400.
	Fl_Button button(5,5,75,30,"Click Me");  //Button arguments starting point(x,y),width,heigth,"label."
	button.callback(My_Callback);
	win.show();			   //Tells FLTK to show the window.
	return(Fl::run());		//Returns value of FLTK command loop, this
							  //function returns only when all windows are closed.
}



We want the button to do something. To make the button do something we have to add a callback.

void My_Callback(Fl_Widget*w, void* userdata)
{
	 fprintf(stderr, "Button works!\n");
}



Callbacks in FLTK always return a void. We declared the callback named My_Callback. The first two arguments are a widget pointer, which is a pointer to the widget that invoked the callback, and a void pointer to userdata which can be any kind of data that you want.

Below the button definition we attach the callback to the button.

button.callback(My_Callback);



Now when the button is clicked the callback will display on the black background window, "FLTK is fun to learn and easy to use."

To get the display to work we added:

#include <stdio.h>



• V. EXAMPLE 4

Please copy and paste Example 4 into your IDE then build and debug.

//************************************
//Exampe 4
//Passing userdata
//************************************
#include <Fl/Fl.H>			//Required by all FLTK programs.
#include <Fl/Fl_window.H>	 //Required for window class.
#include <FL/FL_Button.H>	 //Required for button class.
#include <stdio.h>			//Required for display.

void My_Callback(Fl_Widget*w, void* userdata)
{
	 fprintf(stderr, "Button Clicked! Userdata passed is %d\n", (int)userdata);
}

int main()
{
	Fl_Window win(600,400,"FLTK is fun to learn and easy to use.");   //Creates window width 600 height 400.
	Fl_Button button(5,5,75,30,"Click Me");  //Button arguments starting point(x,y),width,heigth,"label."
	button.callback(My_Callback, (void*)54321);
	win.show();			   //Tells FLTK to show the window.
	return(Fl::run());		//Returns value of FLTK command loop, this
							  //function returns only when all windows are closed.
}



We want to pass userdata. userdata is passed as an optional second argument to the callback. The userdata can be any type of data you want as long as it is one piece of data that can be cast to a void pointer.


button.callback(My_Callback, (void*)54321);



When you push the button, it calls the callback function, and passes the 54321 data as the userdata and the function can do with that 54321 userdata whatever it wants.


void My_Callback(Fl_Widget*w, void* userdata)
{
	 fprintf(stderr, "Button Clicked! Userdata passed is %d\n", (int)userdata);
}



In our example the 54321 userdata is printed to the black background window when the button is clicked.

You might have to click and drag the grey foreground window out of the way to see the black background window. Remember the display will be on the black background window not the grey foreground window.

See you in Part 2.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1