home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
m
/
master12.zip
/
mastering
/
pushbtns.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-21
|
10KB
|
375 lines
/*
* Include File Declarations
*/
#include <Xm/MainW.h>
#include <Xm/PushBG.h>
#include <Xm/PushB.h>
#include <Xm/CascadeB.h>
#include <Xm/DialogS.h>
#include <Xm/MessageB.h>
#include <Xm/RowColumn.h>
#include <Xm/Label.h>
#include <Xm/Frame.h>
#include <Xm/DrawingA.h>
#include "bitmaps/flagup"
#include "bitmaps/flagdown"
#define XBMu_BITS flagup_bits
#define XBMu_WIDTH flagup_width
#define XBMu_HEIGHT flagup_height
#define XBMd_BITS flagdown_bits
#define XBMd_WIDTH flagdown_width
#define XBMd_HEIGHT flagdown_height
#define MAX_ARGS 20
#define BUFFERSZ 1000
/*
* Global Variables
*/
static XmStringCharSet charset = (XmStringCharSet) XmFONTLIST_DEFAULT_TAG;
Widget appshell, drawarea;
Widget pushbtn, pushbtng;
Widget ablabel, abglabel;
Arg args [20];
int n;
Pixmap px_up;
Pixmap px_down;
Pixmap gbr;
/*-------------------------------------------------------------
** Forward Declarations
*/
void main();
void activateCB1 ();
void activateCB2 ();
Widget Create_Draw_Dialog ();
Widget CreateHelp ();
void HelpCB ();
void QuitCB ();
static XtArgVal GetPixel(colorstr)
char *colorstr;
{
XrmValue from, to;
from.size = strlen(colorstr) +1;
if (from.size < sizeof(String)) from.size = sizeof(String);
from.addr = colorstr;
to.addr = NULL;
XtConvert(appshell, XmRString, &from, XmRPixel, &to);
return ((XtArgVal) *((XtArgVal *) to.addr));
}
/*-------------------------------------------------------------
** HelpCB - callback for help button
*/
void HelpCB (w, client_data, call_data)
Widget w; /* widget id */
XtPointer client_data; /* data from application */
XtPointer call_data; /* data from widget class */
{
Widget message_box; /* message box */
/* Create help window. */
message_box = CreateHelp (w);
/* Display help window. */
XtManageChild (message_box);
}
/*-------------------------------------------------------------
** CreateHelp - create help window
*/
Widget CreateHelp (parent)
Widget parent; /* parent widget */
{
Widget button;
Widget message_box; /* Message Dialog */
Arg args[MAX_ARGS]; /* arg list */
register int n; /* arg count */
static char message[BUFFERSZ]; /* help text */
XmString title_string = NULL;
XmString message_string = NULL;
XmString button_string = NULL;
/* Generate message to display. */
sprintf (message, "\
This program uses a pushbutton and a pushbutton gadget to pop up\n\
an XmDialogShell. When you press one of the buttons, a dialog box\n\
pops up with a drawing area widget that displays a canned drawing area.\n\
You can display as many of these as you want. To terminate the program,\n\
press the 'File' button and then the 'Exit' button.\0");
/* Create the compound strings */
message_string = XmStringCreateLtoR (message, charset);
button_string = XmStringCreateLtoR ("Close", charset);
title_string = XmStringCreateLtoR ("pushbtns help", charset);
/* Create message box dialog. */
n = 0;
XtSetArg (args[n], XmNdialogTitle, title_string); n++;
XtSetArg (args[n], XmNokLabelString, button_string); n++;
XtSetArg (args[n], XmNmessageString, message_string); n++;
message_box = XmCreateMessageDialog (parent, "helpbox", args, n);
button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
XtUnmanageChild (button);
button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
XtUnmanageChild (button);
/* Free strings and return message box. */
if (title_string) XmStringFree (title_string);
if (message_string) XmStringFree (message_string);
if (button_string) XmStringFree (button_string);
return (message_box);
}
/*-------------------------------------------------------------
** QuitCB - callback for quit button
*/
void QuitCB (w, client_data, call_data)
Widget w; /* widget id */
XtPointer client_data; /* data from application */
XtPointer call_data; /* data from widget class */
{
/* Terminate the application. */
exit (0);
}
/***********************************************************************
*** Create_Draw_Dialog ***
***********************************************************************/
Widget Create_Draw_Dialog (parent)
Widget parent;
{
Widget dlog_shell;
/*
* Create a dialog shell
*/
dlog_shell = XmCreateDialogShell (parent, "dlog_shell", NULL, 0);
/*
* Create a drawing area
*/
n = 0;
XtSetArg(args[n], XmNwidth, 155); n++;
XtSetArg(args[n], XmNheight, 155); n++;
drawarea = XmCreateDrawingArea (dlog_shell, "drawarea", args, n);
XtManageChild(drawarea);
}
/***********************************************************************
*** activateCB1 ***
***********************************************************************/
void activateCB1(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
Widget draw_area_dialog;
draw_area_dialog = Create_Draw_Dialog (appshell);
n=0;
XtSetArg(args[n], XmNbackgroundPixmap, px_up); n++;
XtSetValues (drawarea, args, n);
printf("DrawingArea background set to flag up.\n");
}
/***********************************************************************
*** activateCB2 ***
***********************************************************************/
void activateCB2(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
Widget draw_area_dialog;
draw_area_dialog = Create_Draw_Dialog (appshell);
n=0;
XtSetArg(args[n], XmNbackgroundPixmap, px_down); n++;
XtSetValues (drawarea, args, n);
printf("DrawingArea background set to flag down.\n");
}
/***********************************************************************
*** MAIN ***
***********************************************************************/
void main(argc, argv)
unsigned int argc;
char **argv;
{
Widget button, cascade, frame;
Widget rowcol, main_window, menu_bar, menu_pane;
char *text1 = "Pushbutton";
char *text2 = "Pushbutton Gadget";
XmString label_text1, label_text2, pblabel, pbglabel;
XtAppContext app_context;
Display *display;
/*
* Initialize the appshell shell widget
*/
appshell = XtAppInitialize(&app_context, "Pushbtns", NULL, 0, &argc,
argv, NULL, args, 0);
/*
* Get the display ID for the bitmap to pixmap conversion
*/
display = XtDisplay (appshell);
/* Create main window.
*/
n = 0;
main_window = XmCreateMainWindow (appshell, "main1", args, n);
XtManageChild (main_window);
/* Create menu bar in main window. */
n = 0;
menu_bar = XmCreateMenuBar (main_window, "menu_bar", args, n);
XtManageChild (menu_bar);
/* Create File pulldown menu. */
n = 0;
menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane", args, n);
n = 0;
button = XmCreatePushButtonGadget (menu_pane, "Exit", args, n);
XtManageChild (button);
XtAddCallback (button, XmNactivateCallback, QuitCB, NULL);
n = 0;
XtSetArg (args[n], XmNsubMenuId, menu_pane); n++;
cascade = XmCreateCascadeButton (menu_bar, "File", args, n);
XtManageChild (cascade);
/* Create Help button. */
n = 0;
cascade = XmCreateCascadeButton (menu_bar, "Help", args, n);
XtManageChild (cascade);
XtAddCallback (cascade, XmNactivateCallback, HelpCB, NULL);
n = 0;
XtSetArg (args[n], XmNmenuHelpWidget, cascade); n++;
XtSetValues (menu_bar, args, n);
/*
* Create a frame widget
*/
n = 0;
XtSetArg(args[n], XmNmarginWidth, 15); n++;
XtSetArg(args[n], XmNmarginHeight, 15); n++;
frame = XmCreateFrame (main_window, "frame", args, n);
XtManageChild(frame);
/* Set main window areas */
XmMainWindowSetAreas (main_window, menu_bar, NULL, NULL, NULL, frame);
/*
* Get pixmaps from bitmaps
*/
px_up =XCreatePixmapFromBitmapData(display,
DefaultRootWindow(display),
XBMu_BITS, XBMu_WIDTH,XBMu_HEIGHT,
GetPixel("red"),
GetPixel("white"),
DefaultDepth(display,DefaultScreen(display)));
px_down = XCreatePixmapFromBitmapData(display,
DefaultRootWindow(display),
XBMd_BITS, XBMd_WIDTH,XBMd_HEIGHT,
GetPixel("red"),
GetPixel("white"),
DefaultDepth(display,DefaultScreen(display)));
/*
* Create a row column manager
*/
n = 0;
XtSetArg(args[n], XmNnumColumns, 2); n++;
XtSetArg(args[n], XmNpacking, XmPACK_COLUMN); n++;
XtSetArg(args[n], XmNspacing, 5); n++;
rowcol = XmCreateRowColumn(frame, "rowcol", args, n);
XtManageChild(rowcol);
/*
* Set up compound strings for the label widgets and button labels
*/
label_text1 = XmStringCreateLtoR(text1, XmFONTLIST_DEFAULT_TAG);
label_text2 = XmStringCreateLtoR(text2, XmFONTLIST_DEFAULT_TAG);
pblabel = XmStringCreateLtoR("FLAG UP", XmFONTLIST_DEFAULT_TAG);
pbglabel = XmStringCreateLtoR("FLAG DOWN", XmFONTLIST_DEFAULT_TAG);
/*
* Create a label widget for the push button
*/
n = 0;
XtSetArg(args[n], XmNlabelString, label_text1); n++;
ablabel = XmCreateLabel(rowcol, "ablabel", args, n);
XtManageChild(ablabel);
XmStringFree (label_text1);
/*
* Create a push button
*/
n = 0;
XtSetArg(args[n], XmNlabelString, pblabel); n++;
pushbtn = XmCreatePushButton(rowcol, "pushbtn", args, n);
XtManageChild(pushbtn);
XmStringFree (pblabel);
/*
* Add Callback for this button
*/
XtAddCallback (pushbtn, XmNactivateCallback, activateCB1, NULL);
/*
* Create a label widget for the pushbutton gadget
*/
n = 0;
XtSetArg(args[n], XmNlabelString, label_text2); n++;
abglabel = XmCreateLabel(rowcol, "abglabel", args, n);
XtManageChild(abglabel);
XmStringFree (label_text2);
/*
* Create a pushbutton gadget
*/
n = 0;
XtSetArg(args[n], XmNlabelString, pbglabel); n++;
pushbtng = XmCreatePushButtonGadget (rowcol, "pushbtng", args, n);
XtManageChild(pushbtng);
XmStringFree (pbglabel);
/*
* Add Callback for this button
*/
XtAddCallback (pushbtng, XmNactivateCallback, activateCB2, NULL);
/*
* Realize the appshell widget, which displays all children
*/
XtRealizeWidget(appshell);
/*
* Go into a loop and wait for input
*/
XtAppMainLoop(app_context);
}