/* File motif1.cc C++, Motif and Xt introductory program demonstrating: 0. Motif / Xt program structure 1. setting resources statically w/ XtVaCreateManagedWidget() 2. setting resources dynamically w/ XtVaSetValues() 3. event processing w/ translation table 4. event processing w/ callback function Mike Barnes This example was adapted from Heller's hello.c program in chapter 2 and from Douglas A. Young's memo2.c (pg 41) program in OSF Motif Edition The X window System Programming and Applications with Xt", Prentise Hall, 1994. Written by Dan Heller and Paula Ferguson. Copyright 1994, O'Reilly & Associates, Inc. Permission to use, copy, and modify this program without * restriction is hereby granted, as long as this copyright notice appears in each copy of the program source code. This program is freely distributable without licensing fees and is provided without guarantee or warrantee expressed or implied. This program is -not- in the public domain. */ #include #include #include // for exit() #include // prototypes for event handlers: // actions, translation tables, callbacks void quitAction (Widget w, XEvent * ev, String * params, Cardinal * numParams); void keyPressed (Widget w, XEvent * ev, String * params, Cardinal * numParams); XtCallbackProc button_pushed(Widget, XtPointer, XtPointer); static XtActionsRec actionsTable[] = { { "bye", quitAction }, { "keyPressed", keyPressed }, }; static char defaultTranslations [] = "Q: bye() \n\ P: keyPressed(of P a compiled translation)"; XmString myLabel[2]; int main(int argc, char ** argv) { Widget topLevel, button; XtAppContext app; XtTranslations transTable; // compiled translations XtSetLanguageProc (NULL, NULL, NULL); XmString hi = XmStringCreateLocalized ("Hi CS 585"); topLevel = XtVaAppInitialize (&app, "Hi", NULL, 0, &argc, argv, NULL, NULL, 0); // Register action functions and compile table XtAppAddActions(app, actionsTable, XtNumber(actionsTable)); transTable = XtParseTranslationTable(defaultTranslations); myLabel[0] = XmStringCreateLocalized ("Push here to say hello"); myLabel[1] = XmStringCreateLocalized ("Hello CS585 !"); button = XtVaCreateManagedWidget ("pushme", xmPushButtonWidgetClass, topLevel, XmNlabelString, myLabel[0], NULL); XtAddCallback (button, XmNactivateCallback, (XtCallbackProc) button_pushed, NULL); // merge new translations XtAugmentTranslations (button, transTable); XtRealizeWidget (topLevel); XtAppMainLoop (app); // free string resources freeStrings(); return 0; } void freeStrings() { cout << "freeing string resources" << endl; for(int i = 0; i < 2; i++) XmStringFree (myLabel[i]); } void quitAction(Widget w, XEvent * ev, String * params, Cardinal * numParams) { cout << "Key Q was pressed -- so exit" << endl; freeStrings(); exit(0); } void changeLable(Widget widget) { static int toggle = 0; toggle = ++toggle % 2; XtVaSetValues(widget, XmNlabelString, myLabel[toggle], NULL); cout << "Hello Yourself!" << endl; } void keyPressed(Widget w, XEvent * ev, String * params, Cardinal * numParams) { int i = 0; cout << "label changed by key press "; while(params[i] != NULL) { cout << " " << params[i]; i++; } cout << endl; changeLable(w); } XtCallbackProc button_pushed(Widget widget, XtPointer client_data, XtPointer call_data) { cout << "label changed by mouse" << endl; changeLable(widget); }