home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12723 < prev    next >
Encoding:
Text File  |  1992-08-21  |  4.2 KB  |  136 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!sunic!sics.se!eua.ericsson.se!tvtjet
  3. From: tvtjet@eua.ericsson.se (Jorgen Eriksson)
  4. Subject: Q: C++ and Motif/OLIT callback problems
  5. Message-ID: <1992Aug21.140523.5476@eua.ericsson.se>
  6. Sender: news@eua.ericsson.se
  7. Nntp-Posting-Host: euas20c01.eua.ericsson.se
  8. Organization: Ellemtel Telecom Systems Labs, Stockholm, Sweden
  9. Date: Fri, 21 Aug 1992 14:05:23 GMT
  10. Lines: 124
  11.  
  12.  
  13. I have problems with c++ and callbacks. My intention was to create a window
  14. class which contains one or more callback functions, implemented as class
  15. methods. This doesn't work properly. Somewere along the way ( probably down in
  16. Xlib ) the callback parameters are LOST i.e. if the callback funktion is NOT
  17. declared as a static. This is not what i want but it seems to be the only way 
  18. to overcome this strange behaviour. The static function will be common to all
  19. instances of this object. I will not be able to identify from wich instance the
  20. callback originated. I have tried this in OLIT and XVIEW as well and found the 
  21. same problems. I have tried it using X11R4 along with SUN's c++ compiler on a 
  22. SUN (IPX).  Here comes a simple sample program that works, but not the way i 
  23. want (without static).
  24.  
  25. I would very much appreciate if someone has an explanation to this behaviour, or
  26. a nice'n'clean solution to overcome it. Email me.
  27.  
  28. aTdHvAaNnKcSe
  29.  
  30.     /Jorgen ( Jorgen.Eriksson@eua.ericsson.se )
  31.  
  32. ******************* START of CODE *******************************
  33.  
  34. #define    XTFUNCPROTO
  35. #include <stdio.h>
  36. #include <Xm/Xm.h>
  37. #include <Xm/Form.h>
  38. #include <Xm/PushB.h>
  39. #include <Xm/RowColumn.h>
  40.  
  41. /*
  42.         Class definition:
  43.                                 */
  44. class   Win{
  45. public:
  46.         Win(Widget);
  47. private:
  48.         ///////////////////////////////////////////////////////////////////////
  49.         //
  50.         // If ButCB() not is declared static, all parameters is lost (widget id,
  51.         // pointers to client and call data).
  52.         //
  53.         // Remove 'static' and the program will result in a bus error.
  54.         //
  55.         ///////////////////////////////////////////////////////////////////////
  56.         static void     ButCB(Widget, caddr_t, caddr_t);
  57.  
  58.         Widget  push_b1;
  59.         Widget  push_b2;
  60. };
  61.  
  62. /*
  63.         Code:
  64.                         */
  65. void
  66. Win::ButCB(Widget w,caddr_t client_data,caddr_t call_data)
  67. {
  68.         Arg     args[10];
  69.         Widget* w1 = (Widget*) client_data;
  70.         XmString xms1, xms2;
  71.  
  72. /*
  73.         This callback function toggles button labels.
  74.                                                         */
  75.         XtSetArg(args[0], XmNlabelString, &xms1);
  76.         XtGetValues(w, args, 1);
  77.         XtSetArg(args[0], XmNlabelString, &xms2);
  78.         XtGetValues(*w1, args, 1);
  79.  
  80.         XtSetArg(args[0], XmNlabelString, xms2);
  81.         XtSetValues(w, args, 1);
  82.         XtSetArg(args[0], XmNlabelString, xms1);
  83.         XtSetValues(*w1, args, 1);
  84. }
  85.  
  86. Win::Win(Widget parent)
  87. {
  88.         Widget  work_area, form_dia;
  89.         Arg     args[10];
  90.  
  91.         form_dia = XmCreateFormDialog(parent, "Nonsense", args, 0);
  92.         XtManageChild(form_dia);
  93.  
  94.         work_area = XmCreateWorkArea(form_dia, "mw", args, 0);
  95.         XtManageChild(work_area);
  96.  
  97.         push_b1  = XmCreatePushButton(work_area, "Hello", args, 0);
  98.         XtManageChild(push_b1);
  99.         XtAddCallback(push_b1, XmNactivateCallback,(XtCallbackProc)&Win::ButCB,
  100.             (caddr_t)&push_b2);
  101.  
  102.         push_b2  = XmCreatePushButton(work_area, "World", args, 0);
  103.         XtManageChild(push_b2);
  104.         XtAddCallback(push_b2, XmNactivateCallback,(XtCallbackProc)&Win::ButCB,
  105.             (caddr_t)&push_b1);
  106. }
  107.  
  108. /*
  109.         Tiny main:
  110.                         */
  111. main(int argc, char** argv)
  112. {
  113.  
  114.         Win*    win;
  115.         Win*    new_win;
  116.         Widget  app_shell;
  117.         int     n;
  118.         Arg     args[10];
  119.  
  120.         app_shell= XtInitialize(argv[0],"",0,0,(Cardinal *)&argc,argv);
  121.  
  122.         n = 0;
  123.         XtSetArg(args[n], XmNheight, 1); n++;
  124.         XtSetArg(args[n], XmNwidth, 1); n++;
  125.         XtSetArg(args[n], XmNmappedWhenManaged, False); n++;
  126.         XtSetValues(app_shell, args, n);
  127.  
  128.         /* Two separate instances is created: */
  129.         win = new Win(app_shell);
  130.         new_win = new Win(app_shell);
  131.  
  132.         XtRealizeWidget(app_shell);
  133.         XtMainLoop();
  134. }
  135. *************** END of CODE ***********************
  136.