Category : Files from Magazines
Archive   : NOV92_1.ZIP
Filename : WIDGET.ASC

 
Output of file : WIDGET.ASC contained in archive : NOV92_1.ZIP
_DEBUGGING MOTIF WIDGETS_
by Kamran Husain

[LISTING ONE]

/* This code is given as an example of sorts for you to write your own Motif
** application. Compile this program with the line:
** acc -g test.c -o testme LineChart.o BarChart.o -lXm -lXt -lX11 -lm
** where
** acc - is the ANSI compiler at your site
** -g - is the debug option (optional)
** -o - specifies the output executable filename
** -lXm - is the Motif Library
** -lXt - is the Intrinsics Library
** -lX11 - is the X11 Library
** -lm - math library.
** LineChart.o and BarChart.o are object files from the
** sources available electronically from Dr Dobbs.
** If you want to step thru this code in a debugger and examine the internals
** of the Widgets themselves, replace the next #include "LineChart.h" line
** outside the comments with
** #include "BarChartP.h"
** #include "BarChart.h"
** #include "LineChartP.h"
** #include "LineChart.h"
*/

/* Some standard include files here. */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "LineChart.h"

/* A macro to facilitate button placement on a form. The x,y are the top left
** corner on the form. The w is the right position relative to the form's left
** side the h is the bottom position relative to the form's top side */
#define MAKE_BTN_ON_FORM(form,btn,x,y,w,h,str) \
n = 0; \
XtSetArg(wars[n], XmNleftAttachment, XmATTACH_POSITION ); n++; \
XtSetArg(wars[n], XmNtopAttachment, XmATTACH_POSITION ); n++; \
XtSetArg(wars[n], XmNbottomAttachment, XmATTACH_POSITION ); n++; \
XtSetArg(wars[n], XmNrightAttachment, XmATTACH_POSITION ); n++; \
XtSetArg(wars[n], XmNrightPosition, (x+w)); n++; \
XtSetArg(wars[n], XmNleftPosition, (x)); n++; \
XtSetArg(wars[n], XmNtopPosition, (y)); n++; \
XtSetArg(wars[n], XmNbottomPosition, (h+y)); n++; \
btn = XmCreatePushButton(form,str,wars,n); \
XtManageChild(btn)
/* Remove the comments below to enable some debug levels.
#define DEBUG_SWAPPING_SCREENS
#define DEBUG_LIMITS
#define DEBUG_SI_COUNTER
*/
#define ScrnWidth 500
#define ScrnHt 400
#define MAXITEMS 10
Widget mainShell, /* Application shell */
mainForm, /* Master form */
plotLChart, /* Line Chart Widget */
plotBChart, /* Bar Chart Widget */
otherPBtn, /* Toggle Push Button */
advancePBtn, /* Advance Data in Lists Push Button */
donePBtn; /* Exit Push Button */
/* Pixels are stored in these. */
unsigned long WhiteColor,
BlackColor,
BlueColor,
RedColor,
GreenColor;

/* Storage Area for the dummy data.
int dummyOne[MAXITEMS] = { -10. -5, 0, 15, 20, 15 ,0, -5, -10, -5 };
int dummyTwo[MAXITEMS] = { 4, 13, 13, 10 ,11, 9, 4, -11, -13, -11 };
*/
static int siCounter = MAXITEMS - 1;
int one[MAXITEMS];
int two[MAXITEMS];
/* Declare the functions and callbacks here. Note that I do not use any
** parameters into the callbacks. Please refer to the Motif manual for
** details on these parameters if you need to use them. */
void exitCB();
int toggleCB();
int advanceCB();
void createDummyData(int count, int *oneptr, int *twoptr);
unsigned long str2pix(char *spec, Widget w);
int main(int argc, char *argv[])
{
Arg wars[24]; /* for argument passing */
XGCValues gcv; /* for creation for GC */
XtGCMask gcmask; /* for GC flags */
char strName[24]; /* Dummy storage area */
int FormHt = 88; /* On the main form */
int margin = 10; /* Distance between buttons */
int dw = 20; /* Width of each button */
int dh = 10; /* Height of each button */
int h1 = margin; /* temporary var initialize */
int h2; /* temporary variables */
int DispHt, DispWd, ivalue, n, i, imax, imin, imid;
/* Initialize toolkit */
mainShell = XtInitialize(argv[0],"Demo", NULL, NULL, &argc, argv) ;
n =0;
XtSetArg(wars[n], XmNwidth, ScrnWidth); n++;
XtSetArg(wars[n], XmNheight, ScrnHt); n++;
XtSetArg(wars[n], XmNiconic, False ); n++;
XtSetArg(wars[n], XmNiconName, "TestMe"); n++;
XtSetValues(mainShell,wars,n);
XtRealizeWidget(mainShell);
/* Place the Form on the Shell. */
n=0;
XtSetArg(wars[n], XmNwidth, ScrnWidth); n++;
XtSetArg(wars[n], XmNheight, ScrnHt); n++;
mainForm = XmCreateForm(mainShell,"mainForm",wars,n);
XtManageChild( mainForm );
/* Use macros to place PushButtons on Form. Attach callbacks too. */
MAKE_BTN_ON_FORM(mainForm,otherPBtn,h1,FormHt,dw,dh,"Toggle");
h2 = h1 + margin + dw;
MAKE_BTN_ON_FORM(mainForm,advancePBtn,h2,FormHt,dw,dh,"Advance");
h2 += margin + dw;
MAKE_BTN_ON_FORM(mainForm,donePBtn,h2,FormHt,dw,dh,"Done");
XtAddCallback(otherPBtn,XmNactivateCallback, toggleCB, NULL);
XtAddCallback(advancePBtn,XmNactivateCallback, advanceCB, NULL);
XtAddCallback(donePBtn,XmNactivateCallback, exit, NULL);
createDummyData(MAXITEMS, one, two);
/* Calculate extents of this dummy data. */
imax = -100; imin = 100;
for (i=0; i< MAXITEMS;i++)
{
ivalue = one[i];
if (imax < ivalue) imax = ivalue;
if (imin > ivalue) imin = ivalue;
ivalue = two[i];
if (imax < ivalue) imax = ivalue;
if (imin > ivalue) imin = ivalue;
}
imid = imin + 1;
#ifdef DEBUG_LIMITS
/* Tell the user where you are. */
printf("\n Min = %d Mid = %d Max = %d", imin,imid,imax);
#endif
/* Allocate the colors here. */
WhiteColor = str2pix("White", mainForm);
BlackColor = str2pix("Black", mainForm);
BlueColor = str2pix("Blue", mainForm);
RedColor = str2pix("Red", mainForm);
GreenColor = str2pix("Green", mainForm);
sprintf(strName,"%d,%d",imax,imin);
/* Define and declare the Line Chart Widget. */
n = 0;
XtSetArg(wars[n], XmNheight, DispHt); n++;
XtSetArg(wars[n], XmNwidth, DispWd); n++;
XtSetArg(wars[n], XmNleftAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNrightAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNtopAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNbottomAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNleftPosition, 5 ); n++;
XtSetArg(wars[n], XmNrightPosition, 95); n++;
XtSetArg(wars[n], XmNtopPosition, 5 ); n++;
XtSetArg(wars[n], XmNbottomPosition, 75 ); n++;
XtSetArg(wars[n], XmNforeground, BlackColor); n++;
XtSetArg(wars[n], XmNbackground, WhiteColor); n++;
XtSetArg(wars[n], XtNitems, one); n++;
XtSetArg(wars[n], XtNauxItems, two); n++;
XtSetArg(wars[n], XtNitemCount, MAXITEMS); n++;
XtSetArg(wars[n], XtNmaxItemCount, MAXITEMS); n++;
XtSetArg(wars[n], XtNmaxValue, imax); n++;
XtSetArg(wars[n], XtNmidValue, imid); n++;
XtSetArg(wars[n], XtNminValue, imin); n++;
XtSetArg(wars[n], XtNzeroColor, BlueColor); n++;
XtSetArg(wars[n], XtNpositiveColor, BlueColor); n++;
XtSetArg(wars[n], XtNnegativeColor, RedColor); n++;
plotLChart = XtCreateWidget("LineChartType",
XvlinechartWidgetClass, mainForm, wars, n);
imid = (imax + imin) / 2;
#ifdef DEBUG_LIMITS
printf("\n Min = %d Mid = %d Max = %d", imin,imid,imax);
#endif
/* Define and declare the Bar Chart Widget. */
n = 0;
XtSetArg(wars[n], XmNheight, DispHt); n++;
XtSetArg(wars[n], XmNwidth, DispWd); n++;
XtSetArg(wars[n], XmNforeground, BlackColor); n++;
XtSetArg(wars[n], XmNbackground, WhiteColor); n++;
XtSetArg(wars[n], XmNleftAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNrightAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNtopAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNbottomAttachment, XmATTACH_POSITION ); n++;
XtSetArg(wars[n], XmNleftPosition, 5 ); n++;
XtSetArg(wars[n], XmNrightPosition, 95); n++;
XtSetArg(wars[n], XmNtopPosition, 5 ); n++;
XtSetArg(wars[n], XmNbottomPosition, 75 ); n++;
XtSetArg(wars[n], XtNitems, one); n++;
XtSetArg(wars[n], XtNauxItems, two); n++;
XtSetArg(wars[n], XtNitemCount, MAXITEMS); n++;
XtSetArg(wars[n], XtNmaxItemCount, MAXITEMS); n++;
XtSetArg(wars[n], XtNmaxValue, imax); n++;
XtSetArg(wars[n], XtNmidValue, imid); n++;
XtSetArg(wars[n], XtNminValue, imin); n++;
XtSetArg(wars[n], XtNmaxShowValue, imax); n++;
XtSetArg(wars[n], XtNminShowValue, imin); n++;
XtSetArg(wars[n], XtNzeroColor, BlueColor); n++;
XtSetArg(wars[n], XtNpositiveColor, BlueColor); n++;
XtSetArg(wars[n], XtNnegativeColor, RedColor); n++;
plotBChart = XtCreateManagedWidget("BarChartDisplay",
XvbarchartWidgetClass, mainForm, wars, n);
/* Show the widgets on the form and go into a loop */
XtManageChild(mainForm);
XtRealizeWidget(mainShell);
XtMainLoop();
}
/* Terminate the application. */
void exitCB()
{
exit(0);
}
/* It's easier to use a flag here instead of calling XtIsManaged() on every
** widget. This flag could index a circular list of Widgets and thus your
** ToggleCB() function would go something like:
** ...
** XtUnmanage(YourList[ToggleFlag]);
** ToggleFlag++;
** if (ToggleFlag >= LengthOfList) ToggleFlag =0;
** XtManage(YourList[ToggleFlag]);
** ...
** where "YourList" is an array of widgets of length "LengthOfList". */
static ToggleFlag = 1;
/* Callback for function to toggle the type of display. */
int toggleCB()
{
if (ToggleFlag)
{
#ifdef DEBUG_SWAPPING_SCREENS
printf("\n Debug statement goes here ");
#endif
XtUnmanageChild(plotBChart);
XtManageChild(plotLChart);
ToggleFlag = 0;
}
else {
#ifdef DEBUG_SWAPPING_SCREENS
printf("\n Debug statement goes here ");
#endif
XtManageChild(plotBChart);
XtUnmanageChild(plotLChart);
ToggleFlag = 1;
}
}
/* Convenience to load in colors for the application. Returns pixel value
** given the following inputs: char *spec; color name
** Widget w; a widget pointer
*/
unsigned long str2pix(char *spec, Widget w)
{
Colormap cmap;
XColor color_struct; /* For this color */
Display *dpy; /* Curent display */
static void *ht = NULL; /* hash table pointer */
/* Get Display */
dpy = XtDisplay(w);
/* Get color map */
cmap = XDefaultColormap(dpy, DefaultScreen(dpy));
/* Parse the color specification */
if (!XParseColor(dpy, cmap, spec, &color_struct))
{
printf("Invalid color name (%s)\n",spec);
exit(1);
}
/* Try to allocate the color */
if (!XAllocColor(dpy, cmap, &color_struct))
{
printf("Cannot allocate color in colormap\n");
exit(1);
}
/* Return the allocated color */
return(color_struct.pixel);
}
/* Cycle the data thru itself when button is pressed on "Advance" */
int advanceCB()
{
if (siCounter < 1) siCounter = MAXITEMS;
#ifdef DEBUG_SI_COUNTER
printf("\n siCounter = %d", siCounter);
#endif
siCounter--;
XvBarChartAddItem(plotLChart, one[siCounter]);
XvBarChartAddItem(plotBChart, two[siCounter]);
}
/* Create some data in an array. */
void createDummyData(int count, int *oneptr, int *twoptr)
{
int i;
for (i = 0; i < count/2; i++)
{
oneptr[i] = (i * 2) + 10;
twoptr[i] = (i * 3) + 5;
}
for (i = count/2; i < count; i++)
{
oneptr[i] = (i * 2) - 10;
twoptr[i] = (i * 3) - 5;
}
}



  3 Responses to “Category : Files from Magazines
Archive   : NOV92_1.ZIP
Filename : WIDGET.ASC

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/