SDSU CS 596 OODP
More XForms

[To Lecture Notes Index]
San Diego State University -- This page last updated Oct. 17, 1995
----------

Contents of More XForms Lecture

  1. References
  2. Menus
  3. Drawing (Free Objects)
    1. Simple Example - Tracking the Mouse
    2. Stupid Example - Drawing Rectangle
    3. Example - Moving Circles
    4. Example - Animation
  4. FDesigner - Files
    1. XXX.h
    2. XXX.c
    3. XXX_cb.c
    4. XXX_main.c
    5. XXX.fd

References


Forms Library: A Graphical User Interface Toolkit for X by T. C. Zhao and Mark Overmars., 1995

/opt/xforms/FORMS/forms.h

Menus


FL_OBJECT* fl_add_menu( Type, XCoordinate,
YCoordinate, width, height, label );

Types
FL_PUSH_MENU
FL_PULLDOWN_MENU
FL_TOUCH_MENU	Menu appears when mouse inside it


void fl_set_menu( FL_OBJECT* menu, const char* MenuStr )

void fl_addto_menu( FL_OBJECT* menu, MenuStr )

void fl_replace_menu_item( menu, int number, MenuStr )

Menu String

Item 1 | Item 2 | Item 3 | ... | Item N


void fl_delete_menu( FL_OBJECT* menu, int number )

void fl_clear_menu( FL_OBJECT* menu )

void fl_delete_menu( FL_OBJECT* menu, int number )

void fl_set_menu_item_mode(menu, int number, mode )

Modes
FL_PUP_NONE	Normal
FL_PUP_GRAY	Entry is grayed out and disabled
FL_PUP_BOX	Entry has empty box to the left
FL_PUP_CHECK	Entry has a check box to the left

Menu Action

int fl_get_menu( FL_OBJECT* menu )

Returns number of item selected by user

Menu Example
#include <iostream.h>
#include <string.h>
#include "forms.h"

void menuSelected( FL_OBJECT*  theMenu, long NotUsed) {
	cout 	<< "The user selected " 
			<< fl_get_menu( theMenu )
			<< endl;
};

int  main( int argc,  char *argv[] )
{
	FL_FORM *form;
	FL_OBJECT  *button,  *menu;

	fl_initialize( argv[0],  "FormDemo",  0,  0 , &argc,  argv );

	form = fl_bgn_form( FL_UP_BOX, 440, 380 );
		menu = fl_add_menu( FL_PUSH_MENU, 
						10, 350, 110, 30, "Which Game" );
		fl_set_object_callback( menu , menuSelected, 0 );

		button = fl_add_button( FL_NORMAL_BUTTON, 
						310, 20, 110, 30, "Exit" );
	fl_end_form(	);

	fl_set_menu( menu, "English | Italian | Polish | Turkish " );
	fl_set_menu_item_mode( menu, 2, FL_PUP_NONE );

	fl_show_form( form, FL_PLACE_CENTER, 
					FL_FULLBORDER, NULL );

	fl_do_forms(	);
	return 0;
}

Drawing (Free Objects)


OBJECT *fl_add_free( type,  MouseXCoordinate, 
					MouseYCoordinate, width,
					height, label, (*handle)() );

type
FL_NORMAL_FREE 	

Receives events:
FL_DRAW, FL_PUSH, FL_RELEASE, FL_ENTER, FL_LEAVE, FL_MOUSE, FL_MOTION,
FL_INACTIVE_FREE 	
Receives event FL_DRAW
FL_INPUT_FREE 	

FL_NORMAL_FREE plus receives
FL_FOCUS, FL_UNFOCUS, FL_KEYBOARD,
FL_CONTINUOUS_FREE 	

FL_NORMAL_FREE plus receives
FL_STEP
FL_ALL_FREE	

Receives all events
Events

FL_DRAW Object needs to be redrawn
FL_PUSH Mouse button pushed in object
FL_RELEASE Mouse button released in object
FL_ENTER Mouse entered object
FL_LEAVE Mouse left object
FL_MOUSE Mouse position changed, gives mouse coordinates
Sent between FL_PUSH and FL_RELEASE

FL_FOCUS Input got focused on this object
FL_UNFOCUS Input no longer focused on this object
FL_KEYBOARD A key was pressed
FL_MOTION Mouse moved, gives mouse coordinates
Sent between FL_ENTER and FL_LEAVE

FL_STEP Sent all the time, for animation
FL_SHORTCUT hotkeys for object were pressed
FL_FREEMEM
FL_OTHER other
FL_DRAWLABEL Object label needs to be redrawn


Handling Objects
int handle( *object, event, mouseX, MouseY, int KeyPressed, void* Xevent );


handle returns 0 if status of object has not changed
handle returns 1 if status of object has changed

Return 1 if you want fl_do_forms to return this object

Mouse coordinates are relative to form not object


Object Information
object->x	object coordinate and size
object->y
object->w
object->h
object->belowmouse	object is below the mouse
object->pushed	object is being pushed by mouse
object->focus	object has input focus

Drawing Figures
fl_rectf	filled rectangle
fl_rectbound	filled rectangle with black border
fl_rect	outline of rectangle 
void fl_rectf( x, y, width, height, color )
void fl_rectbound( x, y, width, height, color )
void fl_rect( x, y, width, height, color )

void fl_roundrectf( x, y, width, height, color ) 
void fl_roundrect( x, y, width, height, color ) 

void fl_line( x, y, deltaX, deltaY, color ) 

/* line attributes */
void fl_linewidth( int );
void fl_linestyle( int );

void fl_ovalf( x, y, width, height, color )
void fl_ovalbound( x, y, width, height, color ) 
void fl_ovall( x, y, width, height, color ) 

void fl_circf( x, y, radius, color )  
void fl_circ( x, y, radius, color )  

Simple Example - Tracking the Mouse

#include <iostream.h>
#include <string.h>
#include "forms.h"

int freeObjectHandler( FL_OBJECT*  freeObject, int event,
					FL_Coord mouseX, FL_Coord mouseY,
					int key, void* xEvent) 
{
	switch ( event ) {
	case FL_MOTION:
		cout << " Mouse X " << mouseX 
			<< " Y " << mouseY << endl;
	};
	return 0;
};

int  main( int argc,  char *argv[] )
{
	FL_FORM *form;
	FL_OBJECT  *button,  *free;

	fl_initialize( argv[0],  "FormDemo",  0,  0 , &argc,  argv );

	form = fl_bgn_form( FL_UP_BOX, 400, 300 );
		free= fl_add_free( FL_NORMAL_FREE, 
			20, 20, 360, 180, "The Board", freeObjectHandler );

		button = fl_add_button( FL_NORMAL_BUTTON, 
						200, 20, 60, 30, "Exit" );
	fl_end_form(	);

	fl_show_form( form, FL_PLACE_CENTER, 
					FL_FULLBORDER, NULL );

	fl_do_forms(	);
	return 0;
}

Stupid Example - Drawing Rectangle

#include <iostream.h>
#include <string.h>
#include "forms.h"

int freeObjectHandler( FL_OBJECT*  freeObject, int event,
					FL_Coord mouseX, FL_Coord mouseY,
					int key, void* xEvent) 
{
	switch ( event ) {
	case FL_DRAW:
		fl_rectf( 100, 150, 30, 30, FL_RED );
	};
	return 0;
};

int  main( int argc,  char *argv[] )
{
	FL_FORM *form;
	FL_OBJECT  *button,  *free;
	fl_initialize( argv[0],  "FormDemo",  0,  0 , &argc,  argv );

	form = fl_bgn_form( FL_UP_BOX, 400, 300 );
		fl_add_frame(FL_DOWN_FRAME, 20, 20, 360, 180, "");

		free= fl_add_free( FL_NORMAL_FREE, 
			20, 20, 360, 180, "The Board", freeObjectHandler );
		fl_set_object_boxtype( free, FL_FLAT_BOX );

		button = fl_add_button( FL_NORMAL_BUTTON, 
						200, 20, 60, 30, "Exit" );
	fl_end_form(	);

	fl_show_form( form, FL_PLACE_CENTER, 
					FL_FULLBORDER, NULL );
	fl_do_forms(	);
	return 0;
}

Example - Moving Circles

#include <iostream.h>
#include <string.h>
#include "forms.h"
class LCircle {
	public:
		LCircle( int x = 0, int y = 0, 
				int initalRadius = 1, int initialColor = 1 );
		void setCenter( int x, int y );
		void setY( int y);
		void setColor( int newColor );
		int getY();
		void draw( int xOffSet, int yOffSet );
	private:
		int xCoordinate;
		int yCoordinate;
		int radius;
		int color;
};

LCircle :: LCircle(int x, int y, int initalRadius , int initialColor ) {
	xCoordinate = x;
	yCoordinate = y;
	radius = initalRadius;
	color = initialColor;
}

void LCircle :: setY( int y) { 
	yCoordinate = y; 
};

void LCircle :: setColor( int newColor ) {
	color = newColor ; 
};

int  LCircle :: getY( ) {
	return yCoordinate;
};

void LCircle :: setCenter( int x, int y ) {
	xCoordinate = x;
	yCoordinate = y;
};

void LCircle :: draw( int xOffSet, int yOffSet ) {
	fl_circf( xCoordinate + xOffSet, 
			yCoordinate  + yOffSet , radius, color ); 
}
//Moving Circles Handler

LCircle  followMouse(20, 20, 20, FL_RED );
int freeObjectHandler( FL_OBJECT*  freeObject, int event,
					FL_Coord mouseX, FL_Coord mouseY,
					int key, void* xEvent) 
{
	switch ( event ) {
	case FL_DRAW:
		followMouse.draw( 0, 0 );
		break;

	case FL_PUSH:
		cout << " Mouse Pressed, Start Drawing " << endl;
		break;

	case FL_RELEASE:
		cout << " Mouse Released, Stop Drawing " << endl;
		break;

	case FL_MOUSE:
		followMouse.setCenter( mouseX, mouseY );
		fl_redraw_object( freeObject );
		break;
	};
	return 0;
};

//Moving Circles Main


int  main( int argc,  char *argv[] )
{
	FL_FORM *form;
	FL_OBJECT  *button,  *free;
	fl_initialize( argv[0],  "FormDemo",  0,  0 , &argc,  argv );

	form = fl_bgn_form( FL_UP_BOX, 400, 300 );
		fl_add_frame(FL_DOWN_FRAME, 20, 20, 360, 180, "");

		free= fl_add_free( FL_NORMAL_FREE, 
			20, 20, 360, 180, "The Board", freeObjectHandler );
		fl_set_object_boxtype( free, FL_FLAT_BOX );

		button = fl_add_button( FL_NORMAL_BUTTON, 
						200, 20, 60, 30, "Exit" );
	fl_end_form(	);

	fl_show_form( form, FL_PLACE_CENTER, 
					FL_FULLBORDER, NULL );
	fl_do_forms(	);
	return 0;
}

Example - Animation

#include <iostream.h>
#include <string.h>
#include "forms.h"
class LCircle {
	// Not Shown
};

LCircle  followMouse(20, 20, 20, FL_RED );
int freeObjectHandler( FL_OBJECT*  freeObject, int event,
					FL_Coord mouseX, FL_Coord mouseY,
					int key, void* xEvent) 
{
	switch ( event ) {
	case FL_DRAW:
		followMouse.draw( 0, 0 );
		break;

	case FL_PUSH:
		followMouse.setCenter( mouseX, mouseY );
		fl_redraw_object( freeObject );
		break;

	case FL_STEP:
		followMouse.setY( followMouse.getY() + 1 );
		fl_redraw_object( freeObject );
		break;
	};
	return 0;
};

//Animation Main


int  main( int argc,  char *argv[] )
{
	FL_FORM *form;
	FL_OBJECT  *button,  *free;
	fl_initialize( argv[0],  "FormDemo",  0,  0 , &argc,  argv );

	form = fl_bgn_form( FL_UP_BOX, 400, 300 );
		fl_add_frame(FL_DOWN_FRAME, 20, 20, 360, 180, "");

		free= fl_add_free( FL_CONTINUOUS_FREE, 
			20, 20, 360, 180, "The Board", freeObjectHandler );
		fl_set_object_boxtype( free, FL_FLAT_BOX );

		button = fl_add_button( FL_NORMAL_BUTTON, 
						200, 20, 60, 30, "Exit" );
	fl_end_form(	);

	fl_show_form( form, FL_PLACE_CENTER, 
					FL_FULLBORDER, NULL );
	fl_do_forms(	);
	return 0;
}

FDesigner - Files


XXX.h

#ifndef FD_buttonTest_h_
#define FD_buttonTest_h_
/* Header file generated with fdesign. */

/**** Callback routines ****/

extern void buttonPushed( FL_OBJECT *,  long );


/**** Forms and Objects ****/

typedef struct {
	FL_FORM *buttonTest;
	FL_OBJECT *PushButton;
	void *vdata;
	long ldata;
} FD_buttonTest;

extern FD_buttonTest * create_form_buttonTest( void );

#endif /* FD_buttonTest_h_ */

XXX.c

/* Form definition file generated with fdesign. */

#include "forms.h"
#include "button.h"

FD_buttonTest *create_form_buttonTest( void )
{
	FL_OBJECT *obj;
	FD_buttonTest *fdui = ( FD_buttonTest * ) 
				fl_calloc( 1,  sizeof( FD_buttonTest ) );

	fdui->buttonTest = fl_bgn_form( FL_NO_BOX,  320,  250 );

		obj = fl_add_box( FL_UP_BOX, 0, 0, 320, 250, "" );
		fdui->PushButton = obj = fl_add_button( 
							FL_NORMAL_BUTTON, 
							80, 80, 160, 100, "Push Me" );
		fl_set_button_shortcut( obj, "x", 1 );
		fl_set_object_callback( obj, buttonPushed, notUsed );
	fl_end_form(	);

	return fdui;
}
/*---------------------------------------*/

XXX_cb.c

Callback
#include "forms.h"
#include "button.h"

/* callbacks for form buttonTest */
void buttonPushed( FL_OBJECT *ob,  long data )
{
	/* fill-in code for callback */
}

XXX_main.c

#include "forms.h"
#include "button.h"

int main( int argc,  char *argv[] )
{
	 FD_buttonTest *fd_buttonTest;

	 fl_initialize( argv[0],  0,  0,  0,  &argc,  argv );
	 fd_buttonTest = create_form_buttonTest(	);

	 /* fill-in form initialization code */

	 /* show the first form */
	 fl_show_form( fd_buttonTest->buttonTest, 
				FL_PLACE_CENTER, 
				FL_FULLBORDER, "buttonTest" );
	 fl_do_forms(	);
	 return 0;
}

XXX.fd


Magic: 12322

Internal Form Definition File
    (do not change)

Number of forms: 1
Unit of measure: FL_COORD_PIXEL

=============== FORM ===============
Name: buttonTest
Width: 320
Height: 250
Number of Objects: 2

--------------------
class: FL_BOX
type: UP_BOX
box: 0 0 320 250
boxtype: FL_UP_BOX
colors: FL_COL1 FL_COL1
alignment: FL_ALIGN_CENTER
style: FL_NORMAL_STYLE
size: FL_DEFAULT_SIZE
lcol: FL_BLACK
label: 
shortcut: 
resize: FL_RESIZE_ALL
gravity: ForgetGravity ForgetGravity
name: 
callback: 
argument: 

--------------------
class: FL_BUTTON
type: NORMAL_BUTTON
box: 80 80 160 100
boxtype: FL_UP_BOX
colors: FL_COL1 FL_COL1
alignment: FL_ALIGN_CENTER
style: FL_NORMAL_STYLE
size: FL_DEFAULT_SIZE
lcol: FL_BLACK
label: Push Me
shortcut: x
resize: FL_RESIZE_ALL
gravity: ForgetGravity ForgetGravity
name: PushButton
callback: buttonPushed
argument: notUsed

==============================
create_the_forms

----------