Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!decwrl!sun-barr!newstop!sun!argv From: argv@turnpike.Eng.Sun.COM (Dan Heller) Newsgroups: comp.windows.x.motif Subject: Re: Toggle Button Woes Message-ID: <142439@sun.Eng.Sun.COM> Date: 13 Sep 90 23:15:22 GMT References: <270@sps.com> Sender: news@sun.Eng.Sun.COM Organization: O'Reilly && Associates Lines: 68 In article <270@sps.com> arm@sps.com (Annette Myjak) writes: > i want to add a toggle button to a standard selection box dialog. (the > documentation says i can add one child to the work area of a selection > box dialog.) You don't neven need to go that far (altho it's technically correct). I have found that simply creating a new widget with the dialog widget as its parent works perfectly for me. This should probably go under more testing (lest I get some feedback fromk someone), but it seems reasonable (my only hisitation is that the dialog is a bulletinboard widget and therefore needs to be better educated about the new toggle button. Nevertheless, I use something like: dialog = XmCreateDialog(...); XtVaCreateManagedWidget("toggle", xmToggleButtonGadgetClass, dialog, /* resource values */ NULL); Notice the "parent" is the dialog shell. Moving along (since that really wasn't your question). > so far so good. the toggle button is there, and the procedure for the > XmNvalueChangedCallback is getting executed. > > now the strange stuff. when i added the callback, i used: > > XtAddCallback (toggle, XmNvalueChangedCallback, procedure, global_val); ^^^^^^^^^^ Notice: you used a "global", so the value of the variable at the time the function is called will be used -- not the time the *callback* is actually performed. "I know", you might be saying. However, since you are saying that the client_data (2nd) parameter of the callback routine is showing 0 (not 71), I suspect that the global variable has not been initialized to 71 (as you seem to think). By passing the value of the global rather than the address of the variable, you are in fact passing a constant to that function. This constant will never change throughout the life of the application. Rule of thumb -- *never* pass variables *as* callback_data. Always use the -address- of the variable (global or otherwise). In addition to not having the problem you describe above, you can pass the address of any global value--data structure, or char or int. The _address_ of the variable will always work as the client_data parameter. However, you must realize that the client_data should be declared appropriately: void callback_func(widget, variable, call_data) Widget widget; int *variable; caddr_t call_data; This is assuming your global variable is an int, of course. You must dereference it accordingly in the routine. Lastly -- I recognize that I might be mistaken about my analysis of your problem. If you can tell me that you are actually doing: XtAddCallback(widget, XmNvalueChangedCallback, func, 71); and the client_data for "func" is indeed 0, then you have a problem :-) -- dan ---------------------------------------------------- O'Reilly && Associates argv@sun.com / argv@ora.com Opinions expressed reflect those of the author only.