## CS 596 OODP XForms and Make

[To Lecture Notes Index]
San Diego State University -- This page last updated November 6, 1995

## Sources for Examples

The following examples can be found in
/home/ma/whitney/xformExamples

on Rohan.

## Free Object and OK Animation

### Source Code in One File

```#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();
int getX() { return  xCoordinate;};
void draw( int xOffSet, int yOffSet );
private:
int xCoordinate;
int yCoordinate;
int color;
};

LCircle :: LCircle(int x, int y, int initalRadius , int initialColor ) {
xCoordinate = x;
yCoordinate = y;
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 freePeiceHandler( FL_OBJECT*  freeObject, int event,
FL_Coord mouseX, FL_Coord mouseY,
int key, void* xEvent)
{
switch ( event ) {
case FL_DRAW:
fl_rectf( 20, 100, 360, 180, FL_GREEN );  //
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:
if ( 	mouseX == followMouse.getX() &&
mouseY == followMouse.getY())
break;

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

void yesButtonPressed( FL_OBJECT*  theButton, long NotUsed) {
cout << " Test button \n" << endl;}

//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, "");

20, 20, 360, 180, "The Peices",  freePeiceHandler );
fl_set_object_boxtype( free, FL_FLAT_BOX );

170, 210, 60, 30, "Exit" );
100, 210, 60, 30, "test" );
fl_set_object_callback( button, yesButtonPressed, 0 );

fl_end_form(	);

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

### Makefile

```CFLAGS = -O

LIB     = -L/opt/xforms/FORMS -lforms  -L/opt/X11R5/lib -lX11  -lnsl -lsocket -lm

INCL    = -I/opt/xforms/FORMS  -I/opt/X11R5/lib

CC      = CC

.c.o:
\$(CC) \$(CFLAGS) \$(INCL) -c \$*.c

.o:
\$(CC) \$(CFLAGS) -o \$* \$*.o \$(LIB)

.c:
\$(CC) \$(CFLAGS) \$(INCL) -c \$*.c
\$(CC) \$(CFLAGS) -o \$* \$*.o \$(LIB)

clean:
rm *.o
```

### Source Code in Several Files

#### LCircle.hh

```#ifndef	_LCircle_HH
#define	_LCircle_HH
```
```
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();
int getX() { return  xCoordinate;};
void draw( int xOffSet, int yOffSet );
private:
int xCoordinate;
int yCoordinate;
int color;
};

#endif
```

#### LCircle.cc

```#include "LCircle.hh"
#include <iostream.h>
#include <string.h>
#include "forms.h"

LCircle :: LCircle(int x, int y, int initalRadius , int initialColor ) {
xCoordinate = x;
yCoordinate = y;
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 );
}
```

#### Main.cc

```
#include "LCircle.hh"
#include <iostream.h>
#include <string.h>
#include "forms.h"

//Moving Circles Handler

LCircle  followMouse(20, 20, 20, FL_RED );

int freePeiceHandler( FL_OBJECT*  freeObject, int event,
FL_Coord mouseX, FL_Coord mouseY,
int key, void* xEvent)
{
switch ( event ) {
case FL_DRAW:
fl_rectf( 20, 100, 360, 180, FL_GREEN );  //
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:
if ( 	mouseX == followMouse.getX() &&
mouseY == followMouse.getY())
break;

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

void yesButtonPressed( FL_OBJECT*  theButton, long NotUsed) {
cout << " Test button \n" << endl;}

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, "");

20, 20, 360, 180, "The Peices",  freePeiceHandler );
fl_set_object_boxtype( free, FL_FLAT_BOX );

170, 210, 60, 30, "Exit" );
100, 210, 60, 30, "test" );
fl_set_object_callback( button, yesButtonPressed, 0 );

fl_end_form(	);

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

#### Makefile

```LIB		= -L/opt/xforms/FORMS -lforms  \
-L/opt/X11R5/lib -lX11  -lnsl -lsocket -lm

INCL	= -I/opt/xforms/FORMS  -I/opt/X11R5/lib

CC		= g++

all: LCircle.o  main.cc
\$(CC)  \$(INCL)  LCircle.o main.cc -o main \$(LIB)

LCircle.o: LCircle.cc LCircle.hh
\$(CC)  \$(INCL)  LCircle.cc -c

clean:
rm *.o main
```

## Makefile and FDesign

### Makefile

```LIB		= -L/opt/xforms/FORMS -lforms  \
-L/opt/X11R5/lib -lX11  -lnsl -lsocket -lm

INCL	= -I/opt/xforms/FORMS  -I/opt/X11R5/lib

CC		= g++

all: button.o  button_main.c
\$(CC)  \$(INCL)  button.o button_main.c -o main \$(LIB)

button.o: button.c button_cb.c button.h
\$(CC)  \$(INCL)  button.c -c

clean:
rm *.o
```

### Files Generated by Fdesign

#### button.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_ */
```

#### button.c

```/* Form definition file generated with fdesign. */

#include <string.h>		// Added by R. Whitney
#include "forms.h"
#include "button.h"
#include "button_cb.c"		// Added by R. Whitney

const int notUsed = 0;		// Added by R. Whitney

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);
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;
}
/*---------------------------------------*/
```

#### button_cb.c

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

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

#### button_main.c

```#include <string.h>		// Added by R. Whitney
#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;
}
```