📄 ch34.htm
字号:
<PRE><FONT COLOR="#0066FF">/**
*** This is a sample partial listing of how to check if the
*** data returned on an XtGetValues and an XtSetValues
*** call is a copy or a reference.
***/
#include "Xm/Text.h"
..
Widget w;
XmString x1, x2, x3;
x3 = XmStringCreateLtoR("test", XmFONTLIST_DEFAULT_TAG);
XmTextSetString(w,x3);
...
x1 = XmTextGetString(w);
x2 = XmTextGetString(w);
XtWarning(" Checking SetValues");
if (x1 != x3)
XtWarning("Widget keeps a copy ! Free original!");
else
XtWarning("Widget does not keep a copy! Do NOT free original");
XtWarning(" Checking GetValues");
if (x1 == x2)
XtWarning("Widget returns a copy! Do NOT free");
else
XtWarning("Widget does not return a copy! You should free it ");
</FONT></PRE>
<P>The <TT>XtWarning()</TT> message is especially useful for debugging the progress
of programs. The message is relayed to the <TT>stderr</TT> of the invoking application.
If this is an <TT>xterm</TT>, you will see an error message on that terminal window.
If no <TT>stderr</TT> is available for the invoke mechanism, the message is lost.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading20<FONT COLOR="#000077"><B>TIP: </B></FONT>The <TT>XtSetArg</TT>
macro is defined as
</DL>
<DL>
<DD><FONT COLOR="#0066FF">#define XtSetArg(arg,n,d) \<BR>
((void)((arg).name = (n).(arg).value = (XtArgVal)(d)))</FONT>
</DL>
<PRE></PRE>
<BLOCKQUOTE>
<P>Do not use <TT>XtSetArg(arg[n++]</TT>... because this will increment <TT>n</TT>
twice.
<HR>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading21<FONT COLOR="#000077">The XmPushButton
Widget Class</FONT></H3>
<P>The <TT>XmPushButton</TT> is perhaps the most heavily used Widget in Motif. Listings
34.1 and 34.2 show the basic usage for PushButton. When a button is pressed in the
PushButton area, the button goes into an armed state (a state between not pressed
and going to pressed). The color of the button changes to reflect this state, and
you can set this color by using <TT>XmNarmColor</TT>. This color is shown when the
<TT>XmNfillOnArm</TT> resource is set to TRUE.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading22<FONT COLOR="#000077"><B>TIP: </B></FONT>If the <TT>armColor</TT>
for a PushButton does not seem to be working, try setting the <TT>XmNfillOnArm</TT>
resource to TRUE.
<HR>
</DL>
<P>The callback functions for a PushButton are the following: <TT>XmNarmCallback</TT>:
Called when a PushButton is armed.</P>
<P><TT>XmNactivateCallback</TT>: Called when a button is released in the Widget area
while the Widget is armed. This is not invoked if the pointer is outside the Widget
when the button is released.<BR>
<BR>
<TT>XmNdisarmCallback</TT>: Called when a button is released with the pointer outside
the Widget area while the Widget is armed.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading23<FONT COLOR="#000077"><B>TIP:</B> </FONT>If a callback has
more than one function registered for a Widget, all of the functions will be called,
but not necessarily in the order they were registered. Also, do not rely on the same
order being preserved on other systems. If you want more than one function performed
during a callback, sandwich them in one function call.
<HR>
</DL>
<P>In Listing 34.2, you saw how a callback function was added to a PushButton with
the <TT>XtAddCallback</TT> function. The same method can be used to call other functions
for other actions such as the <TT>XmNdisarmCallback</TT>.
<H3 ALIGN="CENTER"><A NAME="Heading24<FONT COLOR="#000077">The XmToggleButton
Widget Class</FONT></H3>
<P>This class is a subclass of the <TT>XmLabel</TT> Widget class. You can have two
types of buttons: one of many or n of many. When using one of many, the user can
make only one selection from many items. (See Figure 34.5.) When using n of many,
the user can select many options. (See Fig- ure 34.6.) Note the way the buttons are
drawn, with one of many shown as diamonds and n of many shown as boxes. <BR>
<BR>
<A HREF="../art/34/34lnx05.jpg"><FONT COLOR="#0033FF"><B>Figure 34.5.</B></FONT></A>
<I>Using one of many toggle buttons.</I></P>
<P><A HREF="../art/34/34lnx06.jpg"><FONT COLOR="#0033FF"><B>Figure 34.6.</B></FONT></A>
<I>Using n of many toggle buttons.</I></P>
<P>The resources for this Widget include the following: <TT>XmNindicatorType</TT>:
Determines the style. Can be set to <TT>XmN_OF_MANY</TT> or <TT>XmONE_OF_MANY</TT>
(the default).</P>
<P><TT>XmNspacing</TT>: The number of pixels between the button and its label.</P>
<P><TT>XmNfillOnSelect</TT>: The color of the button changes to reflect a set when
the <TT>XmNfillOnArm</TT> resource is set to TRUE.</P>
<P><TT>XmNfillColor</TT>: The color to show when set.<BR>
<BR>
<TT>XmNset</TT>: A Boolean resource indicating whether the button is set or not.
If this resource is set from a program, the button will automatically reflect the
change.<BR>
<BR>
It's easier to use the convenience functions <TT>XmToggleButtonGetState(Widget w)</TT>to
get the Boolean state for a Widget, and <TT>XmToggleButtonSetState(Widget w, Boolean
b)</TT>to set the value for a ToggleButton Widget.</P>
<P>Similar to the PushButton class, the ToggleButton class has three callbacks: <TT>XmNarmCallback</TT>:
Called when the ToggleButton is armed.</P>
<P><TT>XmNvalueChangedCallback</TT>: Called when a button is released in the Widgets
area while the Widget is armed. This is not invoked if the pointer is outside the
Widget when the button is released.<BR>
<BR>
<TT>XmNdisarmCallback</TT>: Called when a button is released with the pointer outside
the Widget area while the Widget was armed.<BR>
<BR>
For the callbacks, the data passed into the Callback function is a structure of type:</P>
<PRE><FONT COLOR="#0066FF">typedef struct {
int reason;
Xevent *event;
int set;
} XmToggleButtonCallbackStruct;
</FONT></PRE>
<P>The reason for the callback is one of the following events: <TT>XmCR_ARM</TT>,
<TT>XmCR_DISARM</TT>, or <TT>XmCR_ACTIVATE</TT>. The event is a pointer to <TT>XEvent,</TT>
which caused this callback. The set value is 0 if the item is not set, or nonzero
if set. Look at Listing 34.3, which shows the use of ToggleButton. The ToggleButtons
are arranged in one column via the RowColumn Widget, discussed later in this chapter.
<H3 ALIGN="CENTER"><A NAME="Heading25<FONT COLOR="#000077">Listing <A NAME="Heading2634.3.
Using ToggleButton in Motif.</FONT></H3>
<PRE><FONT COLOR="#0066FF">/*
** This is a typical Motif application that demonstrates the use of
** a ToggleButton Widget(s) stored on a RowColumn Widget.
*/
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/ToggleB.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
void bye(Widget w, XtPointer clientdata, XtPointer calldata);
#define MAX_BTNS 4
int main(int argc, char **argv)
{
Widget top;
XtAppContext app;
Widget aForm;
Widget aButton;
Widget aRowCol;
char str[32];
Widget aToggle[MAX_BTNS];
Arg args[5];
int i;
/**
*** Initialize the toolkit.
**/
top = XtAppInitialize(&app, "KBH", NULL, 0, (Cardinal *)&argc,
argv, NULL, args, 0);
/**
*** Create a Form on this top-level Widget. This is a nice Widget
*** to place other Widgets on top of.
**/
aForm = XtVaCreateManagedWidget("Form1",
xmFormWidgetClass, top,
XmNheight,150,
XmNwidth,100,
NULL);
/**
*** Add a button on the form you just created. Note how this Button
*** Widget is connected to the form that it resides on. Only
*** left, right, and bottom edges are attached to the form. The
*** top edge of the button is not connected to the form.
**/
aButton = XtVaCreateManagedWidget("Push to Exit",
xmPushButtonWidgetClass, aForm,
XmNheight,20,
XmNleftAttachment,XmATTACH_FORM,
XmNrightAttachment,XmATTACH_FORM,
XmNbottomAttachment,XmATTACH_FORM,
NULL);
#define DO_RADIO
/**
*** A quick intro to the hierarchy in Motif.
*** Let's create a RowColumn Widget to place all ToggleButtons.
*** Note how the RowColumn button attaches itself to
*** the top of the button.
**/
aRowCol = XtVaCreateManagedWidget("rowcol",
xmRowColumnWidgetClass, aForm,
#ifdef DO_RADIO
XmNradioBehavior, TRUE,
XmNradioAlwaysOne, TRUE,
#endif
XmNleftAttachment,XmATTACH_FORM,
XmNrightAttachment,XmATTACH_FORM,
XmNtopAttachment,XmATTACH_FORM,
XmNbottomAttachment,XmATTACH_WIDGET,
NULL);
/**
*** Make ToggleButtons on this form called RowCol. Attach them all to the
*** RowColumn Widget on top of the form.
***
*** Note the radioBehavior setting
**/
for (i=0; i< MAX_BTNS; i++)
{
sprintf(str,"Button %d",i);
aToggle[i] = XtVaCreateManagedWidget(str,
xmToggleButtonWidgetClass, aRowCol,
XmNradioBehavior, TRUE,
NULL);
}
XmToggleButtonSetState(aToggle[0],TRUE, FALSE);
/**
*** Call the function "bye" when the PushButton receives
*** an activate message; i.e. when the pointer is moved to
*** the button and Button1 is pressed and released.
**/
XtAddCallback( aButton, XmNactivateCallback, bye, (XtPointer) NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -