unx46.htm

来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,180 行 · 第 1/5 页

HTM
2,180
字号

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> The destruction of a widget is a bit complicated and will be discussed in detail in Chapter 47, &quot;UNIX Graphical User Interfaces for Programmers.&quot;

<BR></NOTE>

<HR ALIGN=CENTER>

<P>This should sound familiar to C++ programmers. True polymorphism is somewhat harder to find in widgets. This is all done in C. For C++ programmers, the class data structure is to the class of an object as the instance data structure is to the instance 
of an object.

<BR></P>

<P>A widget is really a pointer to a data structure when viewed in a debugger. This data structure is allocated on the creation of a widget and is destroyed when a widget is destroyed.

<BR></P>

<P>Let's look at a typical application screen to see some widgets in action. You will work with a demo application called xmdialogs, shown in Figure 46.11. The widgets shown here are described later in this chapter. The xmdialogs application can be found 
in the /usr/bin/X11 directory. If you do not have this application, you can still learn about working with widgets by applying these concepts to different applications.

<BR></P>

<P>

<BR><B><A HREF="46unx11.gif">Figure 46.11. The </B><B><I>xmdialogs</I></B><B> demo application.</A></B>

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Don't worry if you can't find this application on your machine. You will develop the components for this application in the next chapter. If you have the Motif 1.2 release from Metro Link, (305) 
938-0283, you will have this in your demos directory.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Figure 46.11 shows a menu bar, a file selection list with scroll bars, an option button, some radio and toggle buttons, some push buttons, labels, and a text display dialog.

<BR></P>

<P>The Actions and Help items are shown on a menu bar. By moving the pointer to either of these items and pressing Button1, you will be presented with a menu of options very similar in operation to the window and root menu.

<BR></P>

<P>Under this menu bar is a list of items in a scrollable list. This widget is of the type XmList. The XmList lets you keep a selection of items in a visible list. It has scroll bars to allow the user to scroll the list if the entire list is not visible. A 

programmer can set the number of items that are visible at one time. If you resize the window and if the list box sizes itself proportionately with the window, the number of visible items in the list may change.

<BR></P>

<P>To select an item, move the pointer to the item of your choice and press Button1 once. The item is highlighted in a darker color. Some lists allow you to select more than one item, some just one item. In this application you select only one type of 
dialog box. Figure 46.11 shows that the bulletin board item is the selected item.

<BR></P>

<P>The scroll bars on the side of the list widget are of the class XmScrollbar. A scroll bar is either a horizontal or vertical rectangle. There is a raised box in the rectangle, called the slider box. This slider moves within the larger rectangle. The 
moveable space for the slider bar is called the scroll region. The size of the slider bar to the scroll region is proportional to the size of the work area to the total area being viewed.

<BR></P>

<P>The XmScrollBar rectangle has an arrow at each end. The arrows point out from the rectangle and in opposite directions. You can use the arrow keys to move the slider bar within the scroll region.

<BR></P>

<OL>

<LI>Move the mouse to the slider bar arrow.

<BR>

<BR></LI>

<LI>Click Button1.

<BR>

<BR></LI>

<LI>The slider bar moves closer to the arrow. The slider moves as close as possible to the arrow being clicked in the scroll area.

<BR>

<BR></LI>

<LI>Release Button1.

<BR>

<BR></LI></OL>

<P>You can also move the slider bar by dragging with the mouse:

<BR></P>

<OL>

<LI>Move the pointer onto the slider bar.

<BR>

<BR></LI>

<LI>Press Button1.

<BR>

<BR></LI>

<LI>Move the pointer up or down for a vertical scroll bar. Move the pointer left or right for a horizontal scroll bar.

<BR>

<BR>The contents of the work area as well as the slider bar should scroll with the movement of the pointer. The viewable portion is the work area.

<BR>

<BR></LI>

<LI>Release Button1 when list area contains the desired viewing data.

<BR>

<BR></LI></OL>

<P>Now move your cursor to the selection item of the resize policy button. When you click this button, you are presented with a pop-up menu containing the types of resize policies for the dialog box you want to create. When you press the button, a menu 
pops out and presents a list of options. You make the selection with your pointer by moving the pointer to that button and releasing it. The menu disappears and your selection is displayed in the box. In Figure 46.11 the resize policy is set to any. This 
is known as an Option Button.

<BR></P>

<P>Note the diamond-shaped buttons and selections below this current menu. This is a list of one of four possible selections for the dialog box. One of the items is shown in a lighter gray color. This is known as being grayed out, and the option is a not a 

valid option at the time. The option for the work area is disabled. You can select one of the other three options. These items are grouped together with a rectangular frame drawn around them. Usually buttons are grouped together in Motif this way when 
their functionality falls in the same group of actions. The actions are similar to the buttons on an old radio: Push one button and the rest in the row of buttons all come up. This is why these are referred to as radio buttons.

<BR></P>

<P>Look at the two buttons called auto manage and default position. These are toggle buttons for this application. When you select one button, the other is not influenced at all. The functionality provided by each button is completely independent of that 
of the other. Do you see the difference between radio buttons and toggle buttons?

<BR></P>

<P>Sometimes the scroll bar is used on either side of a drawing area. This is called a scrolled window and belongs to the XmScrolledWindow class. This widget can hold graphics instead of a list of items. The XmScrolledWindow is used primarily to view large 

graphics items in a small window, whereas XmList is used to show a list of items from which the user can select.

<BR></P>

<P>Under the toggle buttons, you will see four push buttons. When a push button is pressed, the colors on the border of the button reverse. Furthermore, the color of the pressed rec-tangle changes to show the user action. Push buttons are used to invoke 
some sort of action. When you select the file selection dialog from the list and press the push button to manage it, the display shown in Figure 46.12 appears. This is the standard file selection box under Motif, and you will see it for most applications.

<BR></P>

<P>

<BR><B><A HREF="46unx12.gif">Figure 46.12. A typical File Selection dialog </B><B>box.</A></B>

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I28" NAME="I28">

<FONT SIZE=4><B>Other Types of Widgets</B>

<BR></FONT></A></CENTER></H3>

<P>The Motif toolkit also supplies the widgets described in the following sections.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I29" NAME="I29">

<FONT SIZE=3><B>XmDialogShell</B>

<BR></FONT></A></CENTER></H4>

<P>This is a subclass of TransientShell. Instances of this class are used from modal or modeless dialog boxes. Modality refers to whether the user may interact with other windows while the dialog box is being displayed. A modal dialog box prevents you from 

moving on until you are finished with the dialog box. A modeless dialog box lets you work with other boxes. A File Selection dialog box is a modeless dialog box.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I30" NAME="I30">

<FONT SIZE=3><B>XmMenuShell</B>

<BR></FONT></A></CENTER></H4>

<P>Instances of this class are used to create menu panes for pop-up and pull-down menus. This is derived from the OverrideShell.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I31" NAME="I31">

<FONT SIZE=3><B>VendorShell</B>

<BR></FONT></A></CENTER></H4>

<P>This is a subclass of WMShell. It provides the interface to a window manager. These are provided by specific systems vendors, hence the name.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I32" NAME="I32">

<FONT SIZE=4><B>Other Display Widgets</B>

<BR></FONT></A></CENTER></H3>

<P>These display widgets are used to provide user interaction tools via buttons, arrows, scroll bars, etc. This list is by no means complete since vendors and end users can create their own versions of widgets or brand new widgets. Some examples of 
commercially available widgets include spreadsheet widgets, bar chart widgets, gauges, and so forth. These widgets provide a consistent interface and are therefore easy to include in Motif applications. Some of the standard widgets are listed here.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I33" NAME="I33">

<FONT SIZE=3><B>XmArrowButton</B>

<BR></FONT></A></CENTER></H4>

<P>This is a directional arrow with a border around it. A programmer can modify the direction of the arrow, as well as the thickness and color of the border, by setting the widget's parameters. If you look at the ends of a scroll bar, you will see two 
examples of such a widget.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I34" NAME="I34">

<FONT SIZE=3><B>XmDrawnButton</B>

<BR></FONT></A></CENTER></H4>

<P>A DrawnButton provides a rectangular area with a border for the programmer. The programmer can size, redraw, or reposition text or graphics within this window. This widget provides hooks to set parameters for its border appearance, as well as to attach 

functions for accepting user inputs.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I35" NAME="I35">

<FONT SIZE=3><B>XmLabel</B>

<BR></FONT></A></CENTER></H4>

<P>This is a rectangular box consisting of either text or graphics. It is instantiated but is also used as a base class for all button widgets. A label's text can be multiline, multifont, or even multidirectional. In the xmdialogs example, this would be 
the labels Active Dialog and the Motif Dialog widgets.

<BR></P>

<P>Many features of labels can be modified such as fonts, foreground and background colors, and alignment (left, center, or right justification). In fact, this can even store a pixmap graphic image.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I36" NAME="I36">

<FONT SIZE=3><B>XmPushButton</B>

<BR></FONT></A></CENTER></H4>

<P>This is a text label or pixmap with a border around it. This widget accepts keystrokes or mouse button presses. In the xmdialogs example these are the create, destroy, manage, and unmanage buttons. When a button has focus, it has a heavy border.

<BR></P>

<P>Press the Enter key or a pointer button when the button has focus. Move the cursor to the button. Press a key or button and hold it down. You have armed the button. The color on the button changes and the border colors reverse. This gives the impression 

that the button has been pressed inward. When you release the button, the button reverts to its original state.

<BR></P>

<P>When a mouse button is pressed in this widget, the foreground and background colors of the widget usually invert. This simulates the pressing of a button.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I37" NAME="I37">

<FONT SIZE=3><B>XmSeparator</B>

<BR></FONT></A></CENTER></H4>

<P>This is used to create a line between functional sections of a screen. There is really not much users can do with this widget except position it on the screen.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I38" NAME="I38">

<FONT SIZE=3><B>XmText</B>

<BR></FONT></A></CENTER></H4>

<P>This is used to create a fully functional multiline text editor on a screen. The user can select text by dragging the mouse from one location to another while Button1 is pressed. Users can also click anywhere on the widget to mark the insertion point. 
If the text widget is enabled for user input, the user can type at the insertion point and insert the text into the text widget.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I39" NAME="I39">

<FONT SIZE=3><B>Pull-down Menus</B>

<BR></FONT></A></CENTER></H4>

<P>These are rectangular areas in the window that allow users to select from a group of items. The items are generally laid out in push buttons. You can select a push button either by moving the mouse to that selection or by pressing Alt+K, where K is the 

letter in the menu button that is underlined. In the xmdialogs function, the Meta+F key selects the file item, and Meta+H selects the Help item.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I40" NAME="I40">

<FONT SIZE=3><B>Pop-Up Menus</B>

<BR></FONT></A></CENTER></H4>

<P>The Motif root window menu is a good example of a pop-up menu. When you press the mouse button, a menu is displayed. You can select the items in the menu by moving the cursor onto the item and pressing Button1.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I41" NAME="I41">

<FONT SIZE=3><B>Xmscale</B>

<BR></FONT></A></CENTER></H4>

<P>The Xmscale widget is used to display the value of a data item between two extremes. It can also be used to accept user input. A scale widget has a scroll region very similar to the scroll bar. However, it does not have the arrow buttons at either end.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I42" NAME="I42">

<FONT SIZE=3><B>XmScrolledWindow</B>

<BR></FONT></A></CENTER></H4>

<P>This is a combination of a horizontal scroll bar, vertical scroll bar, and a drawing area. If the size of the drawing area fits within the window, you will not see the scroll bars. If the size of the drawing area is greater than the visible area of the 

scrolled window, then you will see either the horizontal scroll bar, the vertical scroll bar, or both. You can then use the scroll bars to move the visible portion on top of the drawing area. This is known as panning the window.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I43" NAME="I43">

<FONT SIZE=3><B>XmFrame</B>

<BR></FONT></A></CENTER></H4>

<P>This is a simple widget used to put a consistent border around one single widget. A frame can only hold one widget at a time.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I44" NAME="I44">

<FONT SIZE=3><B>XmRowColumn</B>

<BR></FONT></A></CENTER></H4>

<P>This is a general-purpose widget organizer. The widget can lay out its widget collection in a variety of ways, including the following:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>Row major</P>

<TD>

<P>This is where all widgets on the row column widget are stored until one row fills up, and a new row is created when another widget is added that will not fit on this row. The creation of a new row is sometimes called wrap around.</P>

<TR>

<TD>

<P>Column major</P>

<TD>

<P>This is the same as a row major, but it wraps around in a columnar fashion.</P></TABLE>

<P>You can specify the width of each column to be that of the widest widget, the number of fixed columns, the packing (whether all widgets should be packed as closely as possible), or determined individually by each widget.

<BR></P>

<P>As mentioned earlier, there are several other widgets available in the Motif widget set. You can see the complete listing and their options in The Programmers' Reference Manual from the Open Software Foundation (OSF).

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I45" NAME="I45">

<FONT SIZE=3><B>Gadgets</B>

<BR></FONT></A></CENTER></H4>

<P>Motif widgets create a window in X Windows. A complex Motif application can cr

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?