Back to index

im-sdk  12.3.91
LookupAux.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include <stdlib.h>
00043 #include <X11/Intrinsic.h>
00044 #include <X11/Shell.h>
00045 #include <Xm/MenuShell.h>
00046 #include <Xm/DialogS.h>
00047 #include <Xm/RowColumn.h>
00048 #include <Xm/PanedW.h>
00049 #include <Xm/MainW.h>
00050 #include <Xm/Form.h>
00051 #include <Xm/ScrollBar.h>
00052 #include <Xm/ScrolledW.h>
00053 #include <Xm/List.h>
00054 #include <Xm/Label.h>
00055 #include <Xm/PushB.h>
00056 #include <Xm/MwmUtil.h>
00057 
00058 #include "xaux_common.h"
00059 #include "xaux_ext_common.h"
00060 
00061 #include "sampleja3_xaux.h"
00062 
00063 /* Define Aux class */
00064 xaux_class_t xaux_class = {
00065     "com.sun.sampleja3.LookupAux", 2,
00066     NULL,
00067     (Window) 0, (Window) 0,
00068     (Atom) 0, (Atom) 0, (Atom) 0, {(Atom) 0}, 0, {(Atom) 0}, 0,
00069     NULL,
00070 };
00071 
00072 #define       SAMPLEJA3_LOOKUP_START      1
00073 #define       SAMPLEJA3_LOOKUP_DRAW       2
00074 
00075 Widget toplevel;
00076 Widget list;
00077 Widget list_scrolledwin;
00078 Widget controlpane;
00079 Widget maincontrolpane;
00080 Widget popup_button;
00081 Widget prev_button, next_button, test_button;
00082 Widget buttonpane;
00083 Widget dialog_shellform;
00084 
00085 void xt_ext_event_handler(Widget, XtPointer, XEvent *, Boolean * b);
00086 
00087 static xaux_class_t *xc_ = 0;
00088 static int im_ = 0;
00089 static int ic_ = 0;
00090 
00091 /* Event Handler */
00092 void
00093 xt_ext_event_handler(
00094                    Widget w,
00095                    XtPointer client_data,
00096                    XEvent * event,
00097                    Boolean * b)
00098 {
00099 
00100     switch (event->type) {
00101        case ClientMessage:
00102        xaux_ext_process_client_message(XtDisplay(w),
00103                                  (XClientMessageEvent *) event);
00104        break;
00105     default:
00106        break;
00107     }
00108 
00109     return;
00110 }
00111 
00112 /* Methods - Start */
00113 Bool
00114 xaux_ext_Start(
00115               xaux_class_t * xc,
00116               aux_ext_data_t * aux_ext_data)
00117 {
00118     return True;
00119 }
00120 
00121 int choice_per_window;
00122 int ncolumns;
00123 int nrows;
00124 int drawUpDirection;
00125 int n_choices;
00126 int index_of_first_candidate;
00127 int index_of_last_candidate;
00128 int index_of_current_candidate;
00129 
00130 int popuped = 0;
00131 
00132 void popup_cb();
00133 void popdown_cb();
00134 
00135 /* Methods - Draw */
00136 Bool
00137 xaux_ext_Draw(
00138              xaux_class_t * xc,
00139              aux_ext_data_t * aux_ext_data)
00140 {
00141     size_t i;
00142     char *sp;
00143     Bool rv = True;
00144     char buf[256];
00145     Display *display = XtDisplay(toplevel);
00146     int x, y;
00147     Window w_none;
00148     XWindowAttributes attr;
00149     char **candidates;
00150     char tmp[256];
00151 
00152     im_ = aux_ext_data->im;
00153     ic_ = aux_ext_data->ic;
00154     xc_ = xc;
00155 
00156     printf("ext_Draw im:0x%x, ic:0x%x\n", aux_ext_data->im, aux_ext_data->ic);
00157 
00158     strcpy(buf, (const char *) aux_ext_data->aux_name);
00159 
00160     printf("ext_Draw[%s] im:0x%x, ic:0x%x\n",
00161           buf, aux_ext_data->im, aux_ext_data->ic);
00162 
00163     for (i = 0; i < aux_ext_data->integer_count; i++) {
00164        printf("\tiv[%02d]=%d\n", i, aux_ext_data->integer_list[i]);
00165     }
00166 
00167 #if 0
00168     for (i = 0; i < aux_ext_data->string_count; i++) {
00169        memset(tmp, 0, sizeof(char) * 256);
00170        strncpy(tmp, aux_ext_data->string_list[i].ptr, aux_ext_data->string_list[i].length);
00171     }
00172 #endif
00173 
00174     switch (aux_ext_data->integer_list[0]) {
00175     case SAMPLEJA3_AUXLUC_DONE:
00176        popdown_cb(0, 0, 0);
00177        break;
00178 
00179     case SAMPLEJA3_AUXLUC_START:
00180 
00181        choice_per_window = aux_ext_data->integer_list[1];
00182        ncolumns = aux_ext_data->integer_list[2];
00183        nrows = aux_ext_data->integer_list[3];
00184        drawUpDirection = aux_ext_data->integer_list[4];
00185 
00186        break;
00187 
00188     case SAMPLEJA3_AUXLUC_DRAW:
00189 
00190        index_of_current_candidate = aux_ext_data->integer_list[1];
00191        n_choices = aux_ext_data->integer_list[2];
00192        index_of_first_candidate = aux_ext_data->integer_list[3];
00193        index_of_last_candidate = aux_ext_data->integer_list[4];
00194 
00195        if (!popuped) {
00196            XGetWindowAttributes(display, aux_ext_data->clientwin, &attr);
00197            XTranslateCoordinates(display, aux_ext_data->clientwin, attr.root,
00198                       -(attr.border_width), -(attr.border_width),
00199                               &x, &y, &w_none);
00200            XtMoveWidget(toplevel, x + aux_ext_data->point.x, y + aux_ext_data->point.y + 20);
00201            popup_cb((Widget) 0, (XtPointer) NULL, (XtPointer) NULL);
00202        }
00203        candidates = (char **) calloc(aux_ext_data->string_count, sizeof(char *));
00204        for (i = 0; i < aux_ext_data->string_count; i++) {
00205            candidates[i] = (char *) calloc(1, aux_ext_data->string_list[i].length + 1);
00206            strncpy(candidates[i], (char *) aux_ext_data->string_list[i].ptr,
00207                   aux_ext_data->string_list[i].length);
00208        }
00209 
00210        util_SetListItems(list, candidates, aux_ext_data->string_count);
00211        for (i = 0; i < aux_ext_data->string_count; i++) {
00212            free(candidates[i]);
00213        }
00214        free(candidates);
00215 
00216     case SAMPLEJA3_AUXLUC_UPDATE:
00217        index_of_current_candidate = aux_ext_data->integer_list[1];
00218        util_SetSelectPost(list, index_of_current_candidate + 1);
00219        break;
00220 
00221     }
00222     return True;
00223 }
00224 
00225 /* Methods - Done */
00226 Bool
00227 xaux_ext_Done(
00228              xaux_class_t * xc,
00229              aux_ext_data_t * aux_ext_data)
00230 {
00231     popdown_cb(0, 0, 0);
00232     return True;
00233 }
00234 
00235 
00236 static XrmOptionDescRec optionDescList[] = {
00237     {"-session", "*session", XrmoptionSepArg, (XPointer) NULL}
00238 };
00239 
00240 void next_candidate();
00241 
00242 void
00243 ListEventHandler(
00244                Widget w,
00245                XtPointer client_data,
00246                XEvent * event,
00247                Boolean * b)
00248 {
00249     XKeyEvent *key;
00250     int keycode, keychar, state;
00251     int integers[4];
00252 
00253     switch (event->type) {
00254     case KeyRelease:
00255        /*
00256         * KeyEvent that happens on List will be sent to htt_server
00257         * as AUX event
00258         */
00259        KeyEventToVirtualKey((XKeyEvent *) event, &keycode, &keychar, &state);
00260        integers[0] = SAMPLEJA3_AUXSETVAL_KEYPRESS;
00261        integers[1] = keycode;
00262        integers[2] = keychar;
00263        integers[3] = state;
00264 
00265        ReturnToEngine(4, integers, 0, 0);
00266        break;
00267     default:
00268        break;
00269     }
00270     return;
00271 }
00272 
00273 void
00274 select_cb(Widget widget, XtPointer clientData, XtPointer callData)
00275 {
00276 #define IM_CTRL_MASK    (1 << 1)
00277     int integers[4];
00278     int row = util_GetSelectedPos(widget);
00279 
00280     integers[0] = SAMPLEJA3_AUXSETVAL_KEYPRESS;
00281     integers[1] = '0' + row;
00282     integers[2] = '0' + row;
00283     integers[3] = IM_CTRL_MASK;
00284 
00285     ReturnToEngine(4, integers, 0, 0);
00286 }
00287 
00288 void
00289 dselect_cb(Widget widget, XtPointer clientData, XtPointer callData)
00290 {
00291     int integers[4];
00292     int row = util_GetSelectedPos(widget);
00293 
00294     integers[0] = SAMPLEJA3_AUXSETVAL_KEYPRESS;
00295     integers[1] = '0' + row;
00296     integers[2] = '0' + row;
00297     integers[3] = 0;
00298 
00299     ReturnToEngine(4, integers, 0, 0);
00300 }
00301 
00302 
00303 void
00304 popdown_cb(Widget widget, XtPointer clientData, XtPointer callData)
00305 {
00306     popuped = 0;
00307     XtUnmapWidget(toplevel);
00308 }
00309 
00310 void
00311 popup_cb(Widget widget, XtPointer clientData, XtPointer callData)
00312 {
00313     popuped = 1;
00314     XtManageChild(dialog_shellform);
00315     XtMapWidget(toplevel);
00316     XtSetKeyboardFocus(list, None);
00317 }
00318 
00319 void
00320 next_cb(Widget widget, XtPointer clientData, XtPointer callData)
00321 {
00322     int integers[4];
00323 
00324     integers[0] = SAMPLEJA3_AUXSETVAL_KEYPRESS;
00325     integers[1] = ' ';
00326     integers[2] = ' ';
00327     integers[3] = 0;
00328 
00329     ReturnToEngine(4, integers, 0, 0);
00330 }
00331 
00332 void
00333 test_cb(Widget widget, XtPointer clientData, XtPointer callData)
00334 {
00335     char *strings[4];
00336     int integers[4];
00337     integers[0] = 1;
00338     integers[1] = 2;        /* IM_VK_UP */
00339     integers[2] = 3;
00340     integers[3] = 4;
00341 
00342     strings[0] = "test_string1";
00343     strings[1] = "test_string2";
00344     strings[2] = "test_string3";
00345     strings[3] = "test_string4";
00346 
00347     ReturnToEngine(4, integers, 4, strings);
00348 }
00349 
00350 void
00351 prev_cb(Widget widget, XtPointer clientData, XtPointer callData)
00352 {
00353     int integers[4];
00354     integers[0] = SAMPLEJA3_AUXSETVAL_KEYPRESS;
00355     integers[1] = 0x26;            /* IM_VK_UP */
00356     integers[2] = 0x26;
00357     integers[3] = 0;
00358 
00359     ReturnToEngine(4, integers, 0, 0);
00360 }
00361 
00362 CreateUI(Widget parent)
00363 {
00364     WidgetList children = NULL;
00365     int numChildren = 0;
00366     XmString label_xmstring = NULL;
00367     String title_str = NULL;
00368     int n;
00369     Arg args[30];
00370 
00371     dialog_shellform = XtVaCreateManagedWidget("dtb_module_dialog_shellform",
00372                                           xmFormWidgetClass,
00373                                           parent,
00374                                           XmNnoResize, True,
00375                                      XmNdefaultPosition, False,
00376                                           XmNmarginHeight, 0,
00377                                           XmNmarginWidth, 0,
00378                                           NULL);
00379 
00380     n = 0;
00381     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
00382     XtSetArg(args[n], XmNleftOffset, 10); n++;
00383     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
00384     XtSetArg(args[n], XmNrightOffset, 10); n++;
00385     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
00386     XtSetArg(args[n], XmNtopOffset, 10); n++;
00387     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
00388     controlpane = XmCreateRowColumn(dialog_shellform, "controlpane", args, n);
00389 
00390     n = 0;
00391     XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmAUTOMATIC); ++n;
00392     XtSetArg(args[n], XmNlistSizePolicy, XmVARIABLE); ++n;
00393     XtSetArg(args[n], XmNselectionPolicy, XmBROWSE_SELECT); ++n;
00394     XtSetArg(args[n], XmNvisibleItemCount, 9); ++n;
00395     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); ++n;
00396     XtSetArg(args[n], XmNrightOffset, 10); ++n;
00397     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); ++n;
00398     XtSetArg(args[n], XmNleftOffset, 6); ++n;
00399     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); ++n;
00400     XtSetArg(args[n], XmNtopOffset, 10); ++n;
00401     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); ++n;
00402     XtSetArg(args[n], XmNx, 6); ++n;
00403     XtSetArg(args[n], XmNy, 10); ++n;
00404     list = XmCreateScrolledList(controlpane, "list", args, n);
00405 
00406     list_scrolledwin = XtParent(list);
00407 
00408     XtUnmanageChild(list_scrolledwin);
00409 
00410     n = 0;
00411     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
00412     XtSetArg(args[n], XmNleftOffset, 10); n++;
00413     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
00414     XtSetArg(args[n], XmNrightOffset, 10); n++;
00415     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
00416     XtSetArg(args[n], XmNtopOffset, 10); n++;
00417     XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
00418 
00419     buttonpane = XmCreateRowColumn(controlpane, "buttonpane", args, n);
00420 
00421     label_xmstring = XmStringCreateLocalized(" ^ ");
00422     prev_button = XtVaCreateManagedWidget("sort_button2",
00423                                      xmPushButtonWidgetClass,
00424                                      buttonpane,
00425                                      XmNrecomputeSize, True,
00426                                  XmNlabelString, label_xmstring,
00427                                      NULL);
00428     XmStringFree(label_xmstring);
00429 
00430     label_xmstring = XmStringCreateLocalized(" V ");
00431     next_button = XtVaCreateManagedWidget("sort_button2",
00432                                      xmPushButtonWidgetClass,
00433                                      buttonpane,
00434                                      XmNrecomputeSize, True,
00435                                  XmNlabelString, label_xmstring,
00436                                      NULL);
00437     XmStringFree(label_xmstring);
00438 
00439     label_xmstring = XmStringCreateLocalized("Test");
00440     test_button = XtVaCreateManagedWidget("test_button",
00441                                      xmPushButtonWidgetClass,
00442                                      buttonpane,
00443                                      XmNrecomputeSize, True,
00444                                  XmNlabelString, label_xmstring,
00445                                      NULL);
00446     XmStringFree(label_xmstring);
00447 
00448 
00449     XtAddCallback(prev_button, XmNactivateCallback, prev_cb, NULL);
00450     XtAddCallback(next_button, XmNactivateCallback, next_cb, NULL);
00451     XtAddCallback(test_button, XmNactivateCallback, test_cb, NULL);
00452 
00453     XtManageChild(buttonpane);
00454     XtManageChild(controlpane);
00455     XtManageChild(list);
00456 
00457     XtVaGetValues(controlpane, XmNchildren, &children, XmNnumChildren, &numChildren, NULL);
00458     XtManageChildren(children, numChildren);
00459 
00460     XtAddEventHandler(list, KeyReleaseMask, False, ListEventHandler, NULL);
00461 #if    0
00462     XtAddCallback(list, XmNbrowseSelectionCallback, select_cb, NULL);
00463 #endif
00464     XtAddCallback(list, XmNdefaultActionCallback, dselect_cb, NULL);
00465 }
00466 
00467 int
00468 main(
00469      int argc,
00470      char **argv
00471 )
00472 {
00473     XtAppContext app = (XtAppContext) NULL;
00474     String *fallback_resources = (String *) NULL;
00475     ArgList get_resources_args = (ArgList) NULL;
00476     Cardinal num_get_resources_args = (Cardinal) 0;
00477     XtLanguageProc language_proc = (XtLanguageProc) NULL;
00478     XtPointer language_proc_client_data = (XtPointer) NULL;
00479 
00480     int narg;
00481     Arg args[10];
00482 
00483     XtSetLanguageProc((XtAppContext) NULL, NULL, NULL);
00484 
00485     narg = 0;
00486     XtSetArg(args[narg], XmNmwmFunctions, MWM_FUNC_MOVE | MWM_FUNC_RESIZE); narg++;
00487     XtSetArg(args[narg], XmNtitle, "Motif Lookup"); narg++;
00488 
00489     toplevel = XtAppInitialize(&app, "Project",
00490                         optionDescList, XtNumber(optionDescList),
00491                             &argc, argv, fallback_resources,
00492                             args, narg);
00493 
00494     CreateUI(toplevel);
00495 
00496     XtSetMappedWhenManaged(toplevel, FALSE);
00497 
00498     XtRealizeWidget(toplevel);
00499 
00500     /* Register Aux class */
00501     if (xaux_ext_init_classes(XtDisplay(toplevel),
00502                      &xaux_class, XtWindow(toplevel)) == False) {
00503        return 2;
00504     }
00505 
00506     /* Register event handler */
00507     XtAddEventHandler(toplevel, PropertyChangeMask, False,
00508                     xt_ext_event_handler, NULL);
00509 
00510     XtAddEventHandler(toplevel, NoEventMask, True,
00511                     xt_ext_event_handler, NULL);
00512 
00513     XtAppMainLoop(app);
00514     return 0;
00515 }
00516 
00517 util_SetListItems(Widget w, char **candidates, int num_candidates)
00518 {
00519     int n;
00520     XmString xmstrings[50];
00521     char buf[255];
00522     for (n = 0; n < num_candidates; n++) {
00523        sprintf(buf, "%d. %s", n + 1, candidates[n]);
00524        xmstrings[n] = XmStringCreateLocalized(buf);
00525     }
00526     XtVaSetValues(w,
00527                 XmNitems, (XtArgVal) xmstrings,
00528                 XmNitemCount, (XtArgVal) num_candidates,
00529                 NULL);
00530 }
00531 
00532 util_GetSelectedPos(Widget w)
00533 {
00534     int *p;
00535     int count;
00536     XmListGetSelectedPos(w, &p, &count);
00537     if (count == 1) {
00538        return (int) *p;
00539     }
00540     return 0;
00541 }
00542 
00543 util_SetSelectPost(Widget w, int position)
00544 {
00545     XmListSelectPos(w, position, False);
00546 }
00547 
00548 ReturnToEngine(
00549               int integer_count,
00550               int *integers,
00551               int string_count,
00552               char **strings
00553 )
00554 {
00555     int i;
00556 
00557     aux_ext_data_t *aux_data;
00558 
00559     if (!im_ || !ic_ || !xc_)
00560        return;
00561 
00562     if (!integer_count && !string_count)
00563        return;
00564 
00565     aux_data = (aux_ext_data_t *) calloc(1, sizeof(aux_ext_data_t));
00566 
00567     aux_data->im = im_;
00568     aux_data->ic = ic_;
00569     aux_data->integer_count = integer_count;
00570     aux_data->string_count = string_count;
00571 
00572     if (aux_data->integer_count) {
00573        aux_data->integer_list = (int *) calloc(1, sizeof(int) * aux_data->integer_count);
00574        for (i = 0; i < aux_data->integer_count; i++) {
00575            aux_data->integer_list[i] = integers[i];
00576        }
00577     }
00578     if (aux_data->string_count) {
00579        aux_data->string_list = (aux_ext_string_t *) calloc(1,
00580                sizeof(aux_ext_string_t) * aux_data->string_count);
00581        for (i = 0; i < aux_data->string_count; i++) {
00582            aux_ext_string_t *p = &aux_data->string_list[i];
00583            p->length = strlen(strings[i]);
00584            p->ptr = (unsigned char *) strdup(strings[i]);
00585        }
00586     }
00587     xaux_ext_SetValue(XtDisplay(toplevel), xc_, aux_data);
00588 
00589     if (aux_data->integer_list)
00590        free(aux_data->integer_list);
00591     if (aux_data->string_list) {
00592        for (i = 0; i < aux_data->string_count; i++) {
00593            aux_ext_string_t *p = &aux_data->string_list[i];
00594            if (p && p->ptr)
00595               free(p->ptr);
00596        }
00597        free(aux_data->string_list);
00598     }
00599     free(aux_data);
00600 }