fltk_test_10

#ifndef EVENTBOX_H

#define EVENTBOX_H

#include <FL/Fl_Box.h>

class EventBox : public Fl_Box

{

public:

// Constructor

EventBox(int t, int l, int width, int height);

// Event handler

int handle(int e);

        virtual ~EventBox();

    protected:

    private:

};

#endif // EVENTBOX_H

///////////////////////////////////////////////////////////////////////////////////////

#include "eventbox.h"

//***********************************************************************

//

// * Constructor

//=======================================================================

EventBox::EventBox(int t, int l, int width, int height)

         :Fl_Box(FL_UP_BOX, t, l, width, height, "")

//=======================================================================

{

// Set up the label

labelfont(FL_ITALIC);

// Set up the label size

labelsize(24);

// Set up the label style

labeltype(FL_SHADOW_LABEL);

// Set up text inside the box

label("Hello");

}

//***********************************************************************

//

// * Handle the event happening in this box

//=======================================================================

int EventBox::handle(int e)

//=======================================================================

{

switch(e) {

case FL_ENTER: // When the cursor enters this box

// Change the color to red

color (FL_RED);

// Change the label to black

labelcolor(FL_BLACK);

// Notify window to redraw is needed

damage(1);

return 1;

case FL_LEAVE: // When the cursor leave this box

// Change the color to gray

color(FL_GRAY);

// Change the label to black

labelcolor(FL_BLACK);

// Notify window to redraw is needed

damage(1);

return 1;

case FL_PUSH: // When mouse button is pushing

// Change the color to blue

labelcolor(FL_BLUE);

// Notify window to redraw is needed

damage(1);

return 1;

case FL_RELEASE: // When release the mouse button

// Change the label color to black

labelcolor(FL_BLACK);

damage(1);

return 1;

case FL_DRAG: // When drag the mouse button

// Set the label to yellow

labelcolor(FL_YELLOW);

damage(1);

return 1;

default:

return Fl_Box::handle(e);

};

}

EventBox::~EventBox()

{

    //dtor

}

///////////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <FL/Fl.H>  // For Fundatmental FLTK operation

#include <FL/Fl_Window.H> // For the Fl_Winow class

#include <FL/Fl_Double_Window.H> // For the Fl_Double_Window class

#include <FL/Fl_Value_Slider.H> // For the Slider class

#include <FL/Fl_Button.H> // For the button class

#include "eventbox.h"

using namespace std;

Fl_Window*       win = NULL;     // The main window pointer

Fl_Button*       button = NULL;  // The button pointer

EventBox*        box = NULL;     // The pointer to the box

Fl_Value_Slider* slider = NULL;  // The pointer to the slider who will change label size

//****************************************************************************

//

// * The slider callback function

//=============================================================================

void Slider_Callback(Fl_Widget *w)

//=============================================================================

{

int label_size = slider->value();

printf("The label size is %d\n", label_size);

box->labelsize(label_size);

box->damage(1);

}

//****************************************************************************

//

// * The button callback function

//=============================================================================

void Button_Callback(Fl_Widget *w)

//=============================================================================

{

static bool flag = true;   // Flag to control whether print out "Hello" or "Bye"

if(flag) {

box->label("Bye");

flag = false;

}

else {

box->label("Hello");

flag = true;

}

box->damage(1);

}

int main()

{

// Set up the container window

win = new Fl_Window(300, 300, "Example Window");

    //... Add components into window and also initialize each component.

box = new EventBox(40, 20, 220, 220);

// make the box show up

box->show();

// Put in Slider and text indicator

// The time slider

slider = new Fl_Value_Slider(20, 270, 190, 25);

// Set up the type of the slider

slider->type(FL_HOR_SLIDER);

// Set up the boundary of the slider

slider->bounds(1.0, 24.0);

// Set up the default value

slider->value(25.0);

// Set up the callback function

slider->callback(Slider_Callback);

// Show up the slider

slider->show();

// put a button in

button = new Fl_Button(220, 270, 75, 25, "Change");

button->callback(Button_Callback);

// The end of window component definition

win->end();

// make the window show up

win->show();

return Fl::run();

}