⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rhl33.htm

📁 linux的初学电子书
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BR>

<P>A button item enables a user to select an action. Several types of buttons are available to you as a programmer. Figure 33.6 shows how various buttons are used. There are four distinct examples shown in Figure 33.6:

<BR>

<P><B> <A HREF="33rhl06.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/33rhl06.gif">Figure 33.6. Using buttons.</A></B>

<BR>

<UL>

<LI>The Menu Item is shown as &quot;Y/N/Q&quot;.

<BR>

<BR>

<LI>The 1 of N choice of items from four items.

<BR>

<BR>

<LI>The M of N choice of items from three items to match others.

<BR>

<BR>

<LI>Choosing via four checkboxes.

<BR>

<BR>

</UL>

<P>The listing for generating Figure 33.6 is shown in Listing 33.7. We will go over this listing in detail.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 33.7. Using menus, buttons, and choices.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">/*

** A sampler of some of the choices to present to a user

*/

#include &lt;xview/generic.h&gt;

#include &lt;xview/xview.h&gt;

#include &lt;xview/frame.h&gt;

#include &lt;xview/panel.h&gt;

#include &lt;xview/openmenu.h&gt;

Frame frame;

int menuHandler( Menu item, Menu_item selection);

int selected( Panel_item item, Event *ev);

void quit();

int main(int argc, char *argv[])

{

Rect *rt;

Rect *qrt;

Panel panel;

Panel quitbtn;

Panel oneN;

Panel manyN;

Panel chooser;

Menu menu1;

xv_init(XV_INIT_ARGC_PTR_ARGV, &amp;argc, argv, NULL);

frame = (Frame)xv_create(NULL, FRAME,

FRAME_LABEL, argv[0],

XV_WIDTH, 400,

XV_HEIGHT, 200,

NULL);

panel = (Panel)xv_create(frame, PANEL,NULL);

quitbtn = (Panel)xv_create(panel, PANEL_BUTTON,

PANEL_LABEL_STRING, &quot;Quit&quot;,

PANEL_NOTIFY_PROC, quit,

XV_X, 20,

NULL);

menu1 = (Menu) xv_create(NULL, MENU,

MENU_STRINGS, &quot;Yes&quot;, &quot;No&quot;, &quot;Maybe&quot;, &quot;Bye&quot;, NULL,

MENU_NOTIFY_PROC, menuHandler,

NULL);

xv_create (panel, PANEL_BUTTON,

PANEL_LABEL_STRING, &quot;Y/N/Q&quot;,

PANEL_ITEM_MENU, menu1,

PANEL_NOTIFY_PROC, selected,

NULL);

qrt = (Rect *) xv_get(quitbtn, XV_RECT);

oneN = (Panel) xv_create(panel, PANEL_CHOICE,

PANEL_LABEL_STRING, &quot;1 of N&quot;,

PANEL_CHOICE_STRINGS,

&quot;extra&quot;, &quot;large&quot;, &quot;medium&quot;, &quot;small&quot;, NULL,

XV_X, 20,

XV_Y, rect_bottom(qrt) + 20,

NULL);

rt = (Rect *) xv_get(oneN, XV_RECT);

manyN = (Panel) xv_create(panel, PANEL_CHOICE,

PANEL_LABEL_STRING, &quot;M of N&quot;,

PANEL_CHOICE_STRINGS,

&quot;tomato&quot;, &quot;celery&quot;, &quot;carrot&quot; , NULL,

PANEL_CHOOSE_ONE, FALSE,

XV_X, 20,

XV_Y, rect_bottom(rt) + 20,

NULL);

rt = (Rect *) xv_get(manyN, XV_RECT);

chooser = (Panel) xv_create(panel, PANEL_CHECK_BOX,

PANEL_LAYOUT, PANEL_HORIZONTAL,

PANEL_LABEL_STRING, &quot;Extras&quot;,

PANEL_CHOICE_STRINGS,

&quot;fries&quot;, &quot;potato&quot;, &quot;Q. potato&quot;, &quot;salad&quot; , NULL,

PANEL_CHOOSE_ONE, FALSE, /* Let 'em have it all */

XV_X, 20,

XV_Y, rect_bottom(rt) + 20,

NULL);

xv_main_loop(frame);

exit(0);

}

/*

** This function is called when you select an item

*/

int selected( Panel_item item, Event *ev)

{

printf(&quot; %s .. \n &quot;, xv_get(item, PANEL_LABEL_STRING));

}

/*

** This function handles the menu selection item.

** Shows you how to exit via menu item too.

*/

int menuHandler(Menu item, Menu_item thing)

{

printf(&quot;%s .. \n&quot;, xv_get(thing, MENU_STRING));

if (!strcmp((char *)xv_get(thing,MENU_STRING), &quot;Bye&quot;)) quit();

}

/*

** Make a clean exit.

*/

void quit()

{

xv_destroy_safe(frame);

}</FONT></PRE>

<P>Take a look at the part where the &quot;Y/N/Q&quot; menu button was created. First we created the menu items on the menu as menu1. Note that we did not display all of the choices in the menu, just its header.

<BR>

<PRE>

<FONT COLOR="#000080">menu1 = (Menu) xv_create(NULL, MENU,

MENU_STRINGS, &quot;Yes&quot;, &quot;No&quot;, &quot;Maybe&quot;, &quot;Bye&quot;, NULL,

MENU_NOTIFY_PROC, menuHandler,

NULL);</FONT></PRE>

<P>Then we created the panel button that will house this menu with the following lines:

<BR>

<PRE>

<FONT COLOR="#000080">xv_create (panel, PANEL_BUTTON,

PANEL_LABEL_STRING, &quot;Y/N/Q&quot;,

PANEL_ITEM_MENU, menu1,

PANEL_NOTIFY_PROC, selected,

NULL);</FONT></PRE>

<P>That was it. Now you can click the right button on the &quot;Y/N/Q&quot; button to get the selection items as a pull-down menu. If you click the left button, the first item in the menu item will be displayed momentarily and selected. Two functions are 
assigned as callbacks in the previous code segments:

<BR>

<UL>

<LI>menuHandler(): This function will show on your terminal the menu item selected.

<BR>

<BR>

<LI>selected(): This function merely displays the menu item string. You could just as easily display another menu or other items instead of this simple example.

<BR>

<BR>

</UL>

<P>Now look at the example for the &quot;1 of N&quot; selection. As the name of this item suggests, you can choose only one of a given number of items. This is called an exclusive selection.

<BR>

<P>The following lines are used to create this exclusive selection item:

<BR>

<PRE>

<FONT COLOR="#000080">oneN = (Panel) xv_create(panel, PANEL_CHOICE,

PANEL_LABEL_STRING, &quot;1 of N&quot;,

PANEL_CHOICE_STRINGS,

&quot;extra&quot;, &quot;large&quot;, &quot;medium&quot;, &quot;small&quot;, NULL,

XV_X, 20,

XV_Y, rect_bottom(qrt) + 20,

NULL);</FONT></PRE>

<P>Note how we used the core class's XV_X and XV_Y attributes to position this box below the Quit button. We got the position as a rectangle (typedef Rect) of the Quit button via the xv_get call given the XV_RECT attribute:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">qrt = (Rect *) xv_get(quitbtn, XV_RECT);</FONT></PRE>

<P>The position given by XV_X and XV_Y was relative to the top-left position of the panel. This given position is known as absolute positioning because we are using hard-coded numbers to position items.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>To position items generally we can use two functions: xv_row() and xv_col(). These functions use the WIN_ROW_GAP and WIN_COLUMN_GAP to set the spaces between the items. The following example shows you how to position twelve items on a panel:

<BR>#define ROW 3

<BR>#define COL 4

<BR>extern char *name[ROW][COL];

<BR>int i, j;

<BR>for (i = 0; i &lt; ROW; i++)

<BR> for (j = 0; j &lt; COL; j++)

<BR> {

<BR> xv_create(panel, PANEL_BUTTON,

<BR> XV_X, xv_col(panel,j),

<BR> XV_Y, xv_row(panel,i),

<BR> PANEL_LABEL_STRING, name[i][j],

<BR> NULL);

<BR> }</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>All items presented in this list are shown with the NULL-terminated list passed in with the PANEL_CHOICE_STRINGS attribute. The default function of PANEL_CHOICE is to enable only one selection. To get more than one selection if you have a list of 
choices, you can follow the same procedure you used for the exclusive selection panel. The difference between 1 of M and M of N lies in setting the value of the PANEL_CHOOSE_ONE to FALSE. This usage creates the M of N items shown in the following lines:

<BR>

<PRE>

<FONT COLOR="#000080">manyN = (Panel) xv_create(panel, PANEL_CHOICE,

PANEL_LABEL_STRING, &quot;M of N&quot;,

PANEL_CHOICE_STRINGS,

&quot;tomato&quot;, &quot;celery&quot;, &quot;carrot&quot; , NULL,

PANEL_CHOOSE_ONE, FALSE,

XV_X, 20,

XV_Y, rect_bottom(rt) + 20,

NULL);</FONT></PRE>

<P>With 1 of M, we use the XV_RECT call to position this choice of many item's button on the screen.

<BR>

<P>Finally, this example showed you how to use check boxes to create the input items shown for our choices of a side order. Checkboxes are always non-exclusive. The text to do this is shown in the following lines:

<BR>

<PRE>

<FONT COLOR="#000080">chooser = (Panel) xv_create(panel, PANEL_CHECK_BOX,

PANEL_LAYOUT, PANEL_HORIZONTAL,

PANEL_LABEL_STRING, &quot;Extras&quot;,

PANEL_CHOICE_STRINGS,

&quot;fries&quot;, &quot;potato&quot;, &quot;Q. potato&quot;, &quot;salad&quot; , NULL,

XV_X, 20,

XV_Y, rect_bottom(rt) + 20,

NULL);</FONT></PRE>

<P>This set of checkboxes was also positioned to align with the qv_get and rect_bottom() calls.

<BR>

<BR>

<A NAME="E68E288"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>List Items</B></FONT></CENTER></H3>

<BR>

<P>Use the PANEL_LIST attribute to show lists of items. An example is shown in Figure 33.7. The corresponding listing is shown in Listing 33.8. Lists enable you to insert text (and graphics as glyphs) in them. You can have duplicates in a list. If you do 
not want to allow duplicates, set the PANEL_LIST_INSERT_DUPLICATE to FALSE.

<BR>

<P><B> <A HREF="33rhl07.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/33rhl07.gif">Figure 33.7. Using lists to display data.</A></B>

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 33.8. Using lists to display data.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">/*

** Using Lists

*/

#include &lt;xview/generic.h&gt;

#include &lt;xview/xview.h&gt;

#include &lt;xview/frame.h&gt;

#include &lt;xview/panel.h&gt;

Frame frame;

int main(int argc, char *argv[])

{

Panel panel;

void quit();

xv_init(XV_INIT_ARGC_PTR_ARGV, &amp;argc, argv, NULL);

frame = (Frame)xv_create(NULL, FRAME,

FRAME_LABEL, argv[0],

XV_WIDTH, 200,

XV_HEIGHT, 100,

NULL);

panel = (Panel)xv_create(frame, PANEL,NULL);

 (void) xv_create(panel, PANEL_BUTTON,

PANEL_LABEL_STRING, &quot;Quit&quot;,

PANEL_NOTIFY_PROC, quit,

NULL);

 (void) xv_create(panel, PANEL_LIST,

PANEL_LIST_STRINGS,

&quot;Business&quot;, &quot;Economics&quot;, &quot;History&quot;,

&quot;Literature&quot;, &quot;TomFoolery&quot;, &quot;Math&quot;,

&quot;Computer Sci.&quot;, &quot;Engineering&quot;, NULL,

NULL);

xv_main_loop(frame);

exit(0);

}

void quit()

{

xv_destroy_safe(frame);

}</FONT></PRE>

<P>Lists are ordered from 0 and up, so the first row is 0, the second row is 1, and so on. To delete the rows 7 through 9 from a long list, use the xv_set function:

<BR>

<PRE>

<FONT COLOR="#000080">xv_set(list_item,

PANEL_LIST_DELETE_ROWS, 6, 3

NULL);</FONT></PRE>

<P>In the preceding example you are requesting that 3 rows be deleted starting from row index number 6 (which is the seventh row). All other rows are adjusted upward after these rows are deleted.

<BR>

<P>To insert items into this list you can use PANEL_LIST_INSERT and PANEL_LIST_STRING calls. If you wanted to replace the third row with a string pointed to by a pointer called buffer, you would use the following function call:

<BR>

<PRE>

<FONT COLOR="#000080">xv_set(list_item,

PANEL_LIST_DELETE, 2,

PANEL_LIST_INSERT, 2,

PANEL_LIST_STRING, buffer,

NULL);</FONT></PRE>

<P>The PANEL_NOTIFY_PROC function for a list is called when an item is selected, deselected, added, or deleted. The prototype for this function call is

<BR>

<PRE>

<FONT COLOR="#000080">listCallBack(

Panel_item item,

char *string,

Xv_opaque client_data,

Panel_list_op op,

Event *event,

int row);</FONT></PRE>

<P>The item is the panel list itself in this function call. The string is the label for the row, or NULL if no item is defined in the list for the row. The opaque client_data is a user-specified value specified at list creation time with the 
PANEL_LIST_CLIENT_DATA parameter. For example, the line

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">PANEL_LIST_CLIENT_DATA, 2, &quot;Hello&quot;,</FONT></PRE>

<P>will assign the value of client_data to 2 for the row with the string &quot;Hello&quot; in it. Each client_data value must be assigned one line at a time.

<BR>

<P>The op parameter can be one of the following values:

<BR>

<UL>

<LI>PANEL_LIST_OP_SELECT when the row is selected

<BR>

<BR>

⌨️ 快捷键说明

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