Appendix A. Example Programs for SGI Enhanced Widgets

This appendix contains example programs for some of the SGI extended IRIS IM widgets.

Makefiles are provided for some of these examples, but to use these examples, you need to:

This appendix provides example programs for:

Example Program for Color Chooser

/* 


 * colortest.c --
 * demonstration of quick-and-easy use of the color
 * chooser widget.
 */

#include <stdio.h>

#include <Xm/Xm.h>

#include <Xm/Label.h>
#include <Xm/Form.h>
#include <Sgm/ColorC.h>

static void ColorCallback();
Widget label, colorc;
XtAppContext app;

#if 0

int sgidladd()
{
  return 1;
}
#endif

main (argc, argv)
int argc;
char *argv[];
{
  Widget toplevel, form;
  Arg args[25];
  int ac = 0;

  toplevel = XtVaAppInitialize(&app, argv[0], NULL, 0, &argc, argv, NULL, NULL);
  if (toplevel == (Widget)NULL) {
    printf("AppInitialize failed!\n");
    exit(1);
  }

  colorc = SgCreateColorChooserDialog(toplevel, "colorc", NULL, 0);
  XtAddCallback(colorc, XmNapplyCallback, ColorCallback, (XtPointer)NULL);
  XtManageChild(colorc);

  form = XmCreateForm(toplevel, "Form", NULL, 0);
  XtManageChild(form);

  label = XmCreateLabel(form, "I am a color!", NULL, 0);
  XtManageChild(label);
  ac = 0;

  XtRealizeWidget(toplevel);
  XtAppMainLoop(app);
}

void ColorCallback(w, client_data, call_data)
Widget w;
XtPointer client_data, call_data;
{
  Pixel white;  /* fallback */
  SgColorChooserCallbackStruct *cbs =(SgColorChooserCallbackStruct *)call_data;
  Display *dpy = XtDisplay(label);
  Screen *scr = XtScreen(label);
  /*
   * If we were willing to use private structure members,
   * we could be sure to get the correct colormap by using
   * label->core.colormap.  For this demo, however,
   * the default colormap will suffice in most cases.
   */
  Colormap colormap = XDefaultColormapOfScreen(scr);
  XColor mycolor;
  Arg args[1];

  white = WhitePixelOfScreen(scr);

  mycolor.red = (unsigned short)(cbs->r<<8);
  mycolor.green = (unsigned short)(cbs->g<<8);
  mycolor.blue = (unsigned short)(cbs->b<<8);
  mycolor.flags = (DoRed | DoGreen | DoBlue);

  if (XAllocColor(dpy, colormap, &mycolor)) {
    XtSetArg(args[0], XmNbackground, mycolor.pixel);
  }
  else {
    fprintf(stderr, "No more colors!\n"); fflush(stderr);
    XtSetArg(args[0], XmNbackground, white);
  }

  XtSetValues(label, args, 1);
}

Makefile for colortest.c



ROOT = /
MYLIBS =
XLIBS = -lSgw -lSgm -lXm -lXt -lX11 -lgl
SYSLIBS = -lPW -lm -ll -ly
INCLUDES = -I. -I$(ROOT)usr/include

LDFLAGS = -L -L. -L$(ROOT)usr/lib $(MYLIBS) $(XLIBS) $(SYSLIBS)

all: colortest

colortest: colortest.o
        cc -o colortest colortest.o $(LDFLAGS)

colortest.o: colortest.c
        cc -g $(INCLUDES) -DDEBUG -D_NO_PROTO -c colortest.c

Example Program for Dial

/* 


 * Mytest.c --
 * create and manage a dial widget.
 * Test its resource settings through menu/button actions.
 */

#include <stdio.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/DialogS.h>
#include <Xm/Label.h>
#include <Sgm/Dial.h>

/*
 * Test framework procedures and globals.
 */

#ifdef _NO_PROTO
static void DragCallback();
#else
static void DragCallback(Widget w, void *client_data, void *call_data);
#endif /* _NO_PROTO */

XtAppContext app;

main (argc, argv)
int argc;
char *argv[];
{
  Widget toplevel, form, dial, label;
  Arg args[25];
  int ac = 0;

  /*
   * Create and realize our top level window,
   * with all the menus and buttons for user input.
   */
  toplevel = XtVaAppInitialize(&app, "Dialtest", NULL, 0, &argc, argv, NULL, NULL);
  if (toplevel == (Widget)NULL) {
    printf("AppInitialize failed!\n");
    exit(1);
  }

  form = XmCreateForm(toplevel, "Form", NULL, 0);

  /* Set up arguments for our widget. */
  ac = 0;
  XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNtopAttachment, XmATTACH_FORM); ac++;

  /*
   * We use all-default settings.
   * Do not set any of the dial-specific resources.
   */
  dial = SgCreateDial(form, "dial", args, ac);
  XtManageChild(dial);

  ac = 0;
  XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
  XtSetArg(args[ac], XmNtopWidget, dial); ac++;
  XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNlabelString, XmStringCreateSimple("0")); ac++;
  label = XmCreateLabel(form, "valueLabel", args, ac);
  XtManageChild(label);

  /*
   * Set up callback for the dial.
   */
  XtAddCallback(dial, XmNdragCallback, DragCallback, label);

  XtManageChild(form);
  XtRealizeWidget(toplevel);
  XtAppMainLoop(app);
}

void DragCallback(w, client_data, call_data)
Widget w;
XtPointer client_data, call_data;
{
  SgDialCallbackStruct *cbs = (SgDialCallbackStruct *) call_data;
  Widget label = (Widget)client_data;
  static char new_label[256];
  Arg args[2];
  int ac = 0;

  if ((cbs != NULL) && (label != (Widget)NULL)) { 
    sprintf(new_label, "%d", cbs->position);
    XtSetArg(args[ac], XmNlabelString, XmStringCreateSimple(new_label)); ac++;
    XtSetValues(label, args, ac);
  }
}

Example Program for Drop Pocket

/* 


 *  Demonstrate the use of the DropPocket
 */

#include <Xm/Form.h>
#include <Xm/PushB.h>
#include <Sgm/DropPocket.h>

static void droppedCB(Widget w, XtPointer clientData, XtPointer cbs ) {
  SgDropPocketCallbackStruct * dcbs = (SgDropPocketCallbackStruct *)cbs;
  char * name;

  if (dcbs->iconName)
    if (!XmStringGetLtoR( dcbs->iconName, XmFONTLIST_DEFAULT_TAG, &name))
      name = NULL;

  printf("Dropped file: %s\nFull Data: %s\n", name, dcbs->iconData );
  XtFree( name );
}

main( int argc, char * argv[] ) {
  Widget toplevel, exitB, dp, topRC;
  XtAppContext app;

  XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL);
  toplevel = XtVaAppInitialize( &app, "DropPocket", NULL, 0, &argc, argv, NULL, NULL);
  topRC = XtVaCreateManagedWidget( "topRC", xmFormWidgetClass, toplevel, NULL);
  dp = XtVaCreateManagedWidget("dp",
                               sgDropPocketWidgetClass, topRC,
                               XmNtopAttachment, XmATTACH_FORM,
                               XmNbottomAttachment, XmATTACH_FORM,
                               XmNleftAttachment, XmATTACH_FORM,
                               XmNrightAttachment, XmATTACH_FORM,
                               XmNheight, 100,
                               XmNwidth, 100,
                               NULL);
  XtAddCallback( dp, SgNiconUpdateCallback, droppedCB, NULL);
  XtRealizeWidget( toplevel );
  XtAppMainLoop( app );
}

Makefile for Drop Pocket Example

#!smake 


#
include /usr/include/make/commondefs

HFILES = \\p        DropPocketP.h \\p        DropPocket.h 

CFILES = \\p        DropPocket.c


TARGETS = dpt

CVERSION = -xansi
MALLOC = /d2/stuff/lib/Malloc
CVERSION = -xansi
OPTIMIZER = -g 
#-I$(MALLOC) -wlint,-pf -woff 813,826,828 

LLDLIBS = -lSgm -lXm -lXt -lX11 -lPW
#LLDLIBS = -u malloc -u XtRealloc -u XtMalloc -u XtCalloc -L /d2/stuff/lib
           -ldbmalloc -lSgm -lXm -lXt -lX11

LCDEFS = -DFUNCPROTO -DDEBUG

targets: $(TARGETS)

include $(COMMONRULES)

#dpt: dpTest.o $(OBJECTS)
#       $(CC) -o $@ dpTest.o $(OBJECTS) $(LDFLAGS)

dpt: dpTest.o
        $(CC) -o $@ dpTest.o $(LDFLAGS)

#dpt2: dpTest2.o $(OBJECTS)
#       $(CC) -o $@ dpTest2.o $(OBJECTS) $(LDFLAGS)

dpt2: dpTest2.o
        $(CC) -o $@ dpTest2.o $(LDFLAGS)

#dpt3: dpTest3.o $(OBJECTS)
#       $(CC) -o $@ dpTest3.o $(OBJECTS) $(LDFLAGS)

dpt3: dpTest3.o
        $(CC) -o $@ dpTest3.o $(LDFLAGS)

#tdt: tdt.o $(OBJECTS)
#       $(CC) -o $@ tdt.o $(OBJECTS) $(LDFLAGS)

tdt: tdt.o
        $(CC) -o $@ tdt.o $(LDFLAGS)

depend:
        makedepend -- $(CFLAGS) -- $(HFILES) $(CFILES)

Example Program for Finder

/* 


 * Finder.c demonstrates the use of the SgFinder widget
 */
#include <stdlib.h>
#include <stdio.h>
#include <Xm/RowColumn.h>
#include <Xm/Label.h>
#include <Sgm/Finder.h>
#include <Sgm/DynaMenu.h>

static char * items[] = { "Archer's favorite songs:",
                            "Draft dodger rag",
                            "Le Roi Renaud",
                            "/usr/sbin",
                            "/lib/libc.so.1",
                            "Calvinist Headgear Expressway",
                          };

static void valueChangeCB( Widget w, XtPointer clientData, XmAnyCallbackStruct * cbs) {
  printf("App value change callback\n");
}

static void activateCB( Widget w, XtPointer clientData, XmAnyCallbackStruct * cbs) {
  printf("App activate callback\n");
}
main( int argc, char * argv[] ) {
  Widget toplevel, rc, label, finder, history;
  XtAppContext app;
  XmString * list;
  int listSize, i;

  XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL);
  toplevel = XtVaAppInitialize( &app, "Finder", NULL, 0, &argc, argv, NULL, NULL);
  rc = XtVaCreateWidget( "rc",
                        xmRowColumnWidgetClass, toplevel,
                        XmNresizeWidth, False,
                        XmNresizeHeight, True,
                        NULL);

  /* create the original list for the historyMenu */
  listSize = XtNumber( items );
  list = (XmString *)XtMalloc( sizeof(XmString) * listSize);
  for (i = 0; i < listSize; i++)
    list[ i ] = XmStringCreateLocalized( items[ i ] );

  label = XtVaCreateManagedWidget( "Things:",
                                  xmLabelWidgetClass, rc,
                                  NULL);
  finder = XtVaCreateManagedWidget("finder", sgFinderWidgetClass, rc, NULL);
  history = SgFinderGetChild( finder, SgFINDER_HISTORY_MENUBAR );
  if (history && SgIsDynaMenu( history )) {
    XtVaSetValues( history,
                  SgNhistoryListItems, list,
                  SgNhistoryListItemCount, listSize,
                  NULL);
  }

  for (i = 0; i < listSize; i++)
    if (list[ i ])
      XmStringFree(list[ i ]);
  if (list)
    XtFree( (char *)list );

  XtAddCallback( finder, XmNvalueChangedCallback, (XtCallbackProc)valueChangeCB, finder);
  XtAddCallback( finder, XmNactivateCallback, (XtCallbackProc)activateCB, finder);

  XtManageChild( rc );
  XtRealizeWidget( toplevel );
  XtAppMainLoop( app );
}

Example Program for History Button (Dynamenu)

#include <Sgm/DynaMenu.h> 



#include <Xm/RowColumn.h>

static char * items[] = { "illegal smile", "/usr/people/stone",
                          "Fish and whistle", "help I'm trapped in the
                           machine", "9th & Hennepin" };

static void dynaPushCB( Widget w, XtPointer clientData, XtPointer cbd ) {
  SgDynaMenuCallbackStruct * cbs = (SgDynaMenuCallbackStruct *) cbd;
  int num = cbs->button_number;
  printf("Selected item number %d\n", num);
}

main( int argc, char * argv[] ) {
  XtAppContext app = NULL;
  Widget toplevel, rc, dynaMenu;
  XmString * list;
  int listSize, i;

  toplevel = XtVaAppInitialize( &app, "DynaMenu", NULL, 0, &argc,argv, NULL, NULL);
  rc = XtVaCreateManagedWidget( "rc", xmRowColumnWidgetClass, toplevel, NULL);

  /* create the original list for the dynaMenu */
  listSize = XtNumber( items );
  list = (XmString *)XtMalloc( sizeof(XmString) * (unsigned int)listSize);
  for (i = 0; i < listSize; i++)
    list[ i ] = XmStringCreateLocalized( items[ i ] );

  dynaMenu = XtVaCreateManagedWidget("dynaMenu",
                                     sgDynaMenuWidgetClass, rc,
                                     SgNhistoryListItems, list,
                                     SgNhistoryListItemCount, listSize,
                                     NULL);
  XtAddCallback( dynaMenu, SgNdynaPushCallback, dynaPushCB, NULL);

  for (i = 0; i < listSize; i++)
    XmStringFree( list[ i ] );
  XtFree( (char *)list );

  XtRealizeWidget( toplevel );
  XtAppMainLoop( app );
}

Example Program for ThumbWheel

/* 


 * Thumbwheel.c --
 * create and manage a thumbwheel.
 */

#include <stdio.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/DialogS.h>
#include <Xm/Label.h>
#include <Sgm/ThumbWheel.h>

/*
 * Test framework procedures and globals.
 */

#ifdef _NO_PROTO
static void DragCallback();
#else
static void DragCallback(Widget w, void *client_data, void *call_data);
#endif /* _NO_PROTO */

XtAppContext app;

main (argc, argv)
int argc;
char *argv[];
{
  Widget toplevel, form, thumbwheel, label;
  Arg args[25];
  int ac = 0;

  /*
   * Create and realize our top level window,
   * with all the menus and buttons for user input.
   */
  toplevel = XtVaAppInitialize( &app, "Thumbwheeltest", NULL, 0, &argc, argv, NULL, NULL);
  if (toplevel == (Widget)NULL) {
    printf("AppInitialize failed!\n");
    exit(1);
  }

  form = XmCreateForm(toplevel, "Form", NULL, 0);

  /* Set up arguments for our widget. */
  ac = 0;
  XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNtopAttachment, XmATTACH_FORM); ac++;

  /*
   * We use all-default settings, with the exception of orientation.
   * Do not set any other thumbwheel-specific resources.
   */
  ac = 0;
  XtSetArg(args[ac], XmNorientation, XmHORIZONTAL); ac++;
  thumbwheel = SgCreateThumbWheel(form, "thumbwheel", args, ac);
  XtManageChild(thumbwheel);

  ac = 0;
  XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
  XtSetArg(args[ac], XmNtopWidget, thumbwheel); ac++;
  XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
  XtSetArg(args[ac], XmNlabelString, XmStringCreateSimple("0")); ac++;
  label = XmCreateLabel(form, "valueLabel", args, ac);
  XtManageChild(label);

  /*
   * Set up callback for the thumbwheel.
   */
  XtAddCallback(thumbwheel, XmNdragCallback, DragCallback, label);

  XtManageChild(form);
  XtRealizeWidget(toplevel);
  XtAppMainLoop(app);
}

void DragCallback(w, client_data, call_data)
Widget w;
XtPointer client_data, call_data;
{
  SgThumbWheelCallbackStruct *cbs = (SgThumbWheelCallbackStruct *) call_data;
  Widget label = (Widget)client_data;
  static char new_label[256];
  Arg args[2];
  int ac = 0;

  if ((cbs != NULL) && (label != (Widget)NULL)) { 
    sprintf(new_label, "%d", cbs->value);
    XtSetArg(args[ac], XmNlabelString, XmStringCreateSimple(new_label)); ac++;
    XtSetValues(label, args, ac);
  }
}

Example Program for File Selection Box

To run this program, add these lines to your .Xdefaults file:

fsb*sgiMode: true
fsb*useSchemes: all

then type:

xrdb -load

Here's the sample program:

/*-------  fsb.c  -------*/
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/PushB.h>
#include <stdlib.h>
#include <stdio.h>
#include <Xm/FileSB.h>


void printDirF( Widget w, XtPointer clientData, XmFileSelectionBoxCallbackStruct * cbs) {

  char * filename = NULL, * dirname = NULL;

  XmStringGetLtoR( cbs->value, XmFONTLIST_DEFAULT_TAG, &filename);

  XmStringGetLtoR( cbs->dir, XmFONTLIST_DEFAULT_TAG, &dirname);

  printf(“Filename selected: %s\n”, filename);

  if (filename)
    XtFree( filename );
  if (dirname)
    XtFree( dirname );
}

static void showDialog( Widget w, XtPointer clientData, XtPointer callData) {

  Widget dialog = (Widget) clientData;
  XtManageChild( dialog );

}

main (int argc, char *argv[]) {
  Widget toplevel, fsb, b1, b2, rc;
  XtAppContext app;
  XmString textStr;

  XtSetLanguageProc( NULL, (XtLanguageProc)NULL, NULL);

  toplevel = XtVaAppInitialize( &app, “Fsb”, NULL, 0, &argc, argv, NULL, NULL);

  rc = XtVaCreateManagedWidget( “rc”, xmFormWidgetClass, toplevel, NULL);

  /* Set up a dialog */
  if (argc > 1) {

    b1 = XtVaCreateManagedWidget( “FSB”,
                                  xmPushButtonWidgetClass,
                                  rc,
                                  XmNtopAttachment,
                                  XmATTACH_FORM,
                                  XmNbottomAttachment,
                                  XmATTACH_FORM,
                                  XmNleftAttachment,
                                  XmATTACH_FORM,
                                  XmNrightAttachment,
                                  XmATTACH_FORM,
                                  NULL);

    fsb = XmCreateFileSelectionDialog( b1, “FSB Dialog”, NULL, 0);

    XtAddCallback( b1, XmNactivateCallback, showDialog, fsb);

  } else {
    fsb = XmCreateFileSelectionBox( rc, “Select A File”, NULL, 0);
    XtVaSetValues( fsb,
                   XmNtopAttachment, XmATTACH_FORM,
                   XmNbottomAttachment, XmATTACH_FORM,
                   XmNleftAttachment, XmATTACH_FORM,
                   XmNrightAttachment, XmATTACH_FORM,
                   NULL);
    XtManageChild( fsb );

  }

  XtAddCallback( fsb, XmNokCallback, (XtCallbackProc)printDirF, fsb);
  XtAddCallback( fsb, XmNcancelCallback, (XtCallbackProc)exit, NULL);

  XtRealizeWidget( toplevel );
  XtAppMainLoop( app );

}

Makefile for File Selection Box Example Program

#!smake 


#
include /usr/include/make/commondefs

CFILES =  fsb.c

TARGETS = fsb

CVERSION = -xansi
OPTIMIZER = -g

LLDLIBS =  -lSgm  -lXm -lXt -lX11 -lPW

LCDEFS = -DFUNCPROTO -DDEBUG

LCINCS = -I. -I$(MOTIF_HEADERS)

targets: $(TARGETS)

include $(COMMONRULES)

fsb: $(OBJECTS)
$(CC) -o $@ $(OBJECTS) $(LDFLAGS)

Example Programs for Scale (Percent Done Indicator) Widget

The following code produces a simple motif scale widget:

/* progress.c */ 
/* cc -o progress progress.c -lXm -lXt */
 #include <Xm/Scale.h>
 void main(int argc, char** argv) {
    Widget toplevel, scale;
    XtAppContext app_context;
    Arg args[5];
    int nargs=0;

    toplevel = XtAppInitialize(&app_context, “Progress”, 
                        NULL, 0, &argc, argv, NULL, NULL, 0);
 
    XtSetArg(args[nargs], XmNvalue, 50); nargs++;
    XtSetArg(args[nargs], XmNorientation, XmHORIZONTAL); nargs++;
    scale = XmCreateScale(toplevel, “scale”, args, nargs);
    XtManageChild(scale);

    XtRealizeWidget(toplevel);
    XtAppMainLoop(app_context);
}

The following resource file (named “Progress”) produces the slanted, thermometer look of the SGI percent done indicator. Also see the IRIS Viewkit VkProgressDialog class.

!Progress - App-default resources for the progress sample program 

*sgiMode: true
*useSchemes: all 

!Change the appearance o  the slider
*scale.sliderVisual:  flat_foreground
*scale.slidingMode:   thermometer
*scale.slanted:       true

!Set the correct scheme colors
Progress*scale*foreground:    SGI_DYNAMIC BasicBackground
Progress*scale*troughColor:   SGI_DYNAMIC TextFieldBackground

Example Program for LED Widget



/* ledbutton.c                                                 */		
/* cc -o ledbutton ledbutton.c -lXm -lXt                        */
/* ledbutton -xrm “*sgiMode: true” -xrm “*useSchemes: all”       */

#include <Xm/ToggleB.h>

void main(int argc, char** argv)
{
   Widget toplevel, toggle;
   XtAppContext app_context;
   Arg args[5];
   int nargs=0;
   
   toplevel = XtAppInitialize(&app_context, “LEDButton”, 
		              NULL, 0, &argc, argv, NULL, NULL, 0);

   XtSetArg(args[nargs], XmNindicatorSize, 10);  nargs++;
   XtSetArg(args[nargs], XmNindicatorType, Xm3D_N_OF_MANY);  nargs++;
   toggle = XmCreateToggleButton(toplevel, “toggle”, args, nargs);
   XtManageChild(toggle);

   XtRealizeWidget(toplevel);
   XtAppMainLoop(app_context);
}