📄 ch35.htm
字号:
<P>We have to add the include file <TT><xview/scrollbar.h></TT> to get the
definitions for the <TT>scrollbar</TT> package. These are created in lines 46 through
53. Note how we have to create two separate scrollbars, one vertical and one horizontal.</P>
<P>The scrollbars in this example show how they can split to provide multiple, tiled
views of the data in the canvas window. To split a view, press the right mouse button
on a scrollbar and you will be presented with a pop-up menu. Choose the Split View
option to split the view or the Join View option to join two views together. You
will not see a Join View option if a scrollbar does not dissect a view.</P>
<P>You can programmatically split a canvas view even if scrollbars are not present.
Use the <TT>OPENWIN_SPLIT</TT> attribute in an <TT>xv_set()</TT> function call. For
example:</P>
<PRE><FONT COLOR="#0066FF">xv_window xv;
xv = (xv_window)xv_get(canvas,OPENWIN_NTH_VIEW,0);
xv_set(canvas,
OPENWIN_SPLIT,
OPENWIN_SPLIT_VIEW, xv,
OPENWIN_SPLIT_DIRECTION,
OPENWIN_SPLIT_HORIZONTAL,
NULL);
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading33<FONT COLOR="#000077"><B>TIP: </B></FONT>You may want to
group your <TT>xv_set()</TT> function calls into distinct logical calls to set each
type of parameter instead of one long convoluted list of parameters to one <TT>xv_set()</TT>
function. Splitting the code into these groups makes the code easier to read and
debug.
<HR>
</DL>
<P>Note that only <TT>OPENWIN_*</TT> types of attributes are allowed in the <TT>xv_set()</TT>
call with the <TT>OPENWIN_SPLIT</TT> parameter. Do not mix other types of attributes.
To get the first view you can use a value of <TT>0</TT> to the <TT>OPENWIN_NTH_VIEW</TT>
parameter. For the next view, use <TT>1</TT>, and so on. To get an idea of how many
views there are for this canvas use the call</P>
<PRE><FONT COLOR="#0066FF">int number;
number = (int)xv_get(canvas, OPENWIN_NVIEWS);
</FONT></PRE>
<P>To get the paint window to do your own drawing, perhaps in response to other user
input, you can use the <TT>xv_get()</TT> function to get the paint window. For example:</P>
<PRE><FONT COLOR="#0066FF">xv_window xv_paint;
xv_paint = (xv_window)xv_get(canvas, CANVAS_VIEW_PAINT, null);
</FONT></PRE>
<P>Listing 35.6 shows how to use the standard <TT>Xlib</TT> function calls to draw
on the canvas. (See Figure 35.5.) You must use the include file <TT><X/Xlib.h></TT>
for all the definitions and declarations. The <TT>XDrawLine</TT> function used in
this example is somewhat simple. However, this example shows you how to set up your
Graphics Context and use the standard <TT>XDrawLine</TT> function to draw a grid.
You can use other X drawing functions just as easily.
<P>
<A HREF="../art/35/35lnx05.jpg">Figure 35.5.</a> The scrolled window example.
<P><H3 ALIGN="CENTER"><A NAME="Heading34<FONT COLOR="#000077">Buttons</FONT></H3>
<P>A button item enables a user to select an action. Several types of buttons are
available to you as a programmer. Figure 35.6 shows how various buttons are used.
<BR>
<BR>
Four distinct examples are shown in Figure 35.6:
<UL>
<LI>The Menu Item is shown as "Y/N/Q."
<P>
<LI>The 1 of N choice items from four items.
<P>
<LI>The M of N choice of items from three items to match others.
<P>
<LI>Choosing via four checkboxes.
</UL>
<P>The listing for generating Figure 35.6 is shown in Listing 35.7. We will go over
this listing in detail. <BR>
<BR>
<A HREF="../art/35/35lnx06.jpg"><B>Figure 35.6.</B></A><I> Using buttons.</I>
<H3 ALIGN="CENTER"><A NAME="Heading35<FONT COLOR="#000077">Listing <A NAME="Heading3635.7.
Using menus, buttons, and choices.</FONT></H3>
<PRE><FONT COLOR="#0066FF">/*
** A sampler of some of the choices to present to a user
*/
#include <xview/generic.h>
#include <xview/xview.h>
#include <xview/frame.h>
#include <xview/panel.h>
#include <xview/openmenu.h>
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, &argc, argv, NULL);
frame = (Frame)xv_create((int)NULL, FRAME,
FRAME_LABEL, argv[1],
XV_WIDTH, 400,
XV_HEIGHT, 200,
NULL);
panel = (Panel)xv_create(frame, PANEL,NULL);
quitbtn = (Panel)xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Quit",
PANEL_NOTIFY_PROC, quit,
XV_X, 20,
NULL);
menu1 = (Menu) xv_create((int)NULL, MENU,
MENU_STRINGS, "Yes", "No", "Maybe", "Bye", NULL,
MENU_NOTIFY_PROC, menuHandler,
NULL);
xv_create (panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Y/N/Q",
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, "1 of N",
PANEL_CHOICE_STRINGS,
"extra", "large", "medium", "small", 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, "M of N",
PANEL_CHOICE_STRINGS,
"tomato", "celery", "carrot" , 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, "Extras",
PANEL_CHOICE_STRINGS,
"fries", "potato", "Q. potatoe", "salad" , 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(" %s .. \n ", 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("%s .. \n", xv_get(thing, MENU_STRING));
if (!strcmp((char *)xv_get(thing,MENU_STRING), "Bye")) quit();
}
/*
** Make a clean exit.
*/
void quit()
{
xv_destroy_safe(frame);
}
</FONT></PRE>
<P>Take a look at the part where the "Y/N/Q" menu button was created. First
we created the menu items on the menu as <TT>menu1</TT>. Note that we did not display
all of the choices in the menu, just its header.</P>
<PRE><FONT COLOR="#0066FF">menu1 = (Menu) xv_create(NULL, MENU,
MENU_STRINGS, "Yes", "No", "Maybe", "Bye", NULL,
MENU_NOTIFY_PROC, menuHandler,
NULL);
</FONT></PRE>
<P>Then we created the panel button that will house this menu with the following
lines:</P>
<PRE><FONT COLOR="#0066FF">
xv_create (panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Y/N/Q",
PANEL_ITEM_MENU, menu1,
PANEL_NOTIFY_PROC, selected,
NULL);
</FONT></PRE>
<P>That was it. Now you can click the right button on the "Y/N/Q" 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:
<UL>
<LI><TT>menuHandler()</TT>: This function will show on your terminal the menu item
selected.
<P>
<LI><TT>selected()</TT>: This function merely displays the menu item string. You
could just as easily display another menu or other items instead of this simple example.
</UL>
<P>Now look at the example for the "1 of N" 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.</P>
<P>The following lines are used to create this exclusive selection item:</P>
<PRE><FONT COLOR="#0066FF">oneN = (Panel) xv_create(panel, PANEL_CHOICE,
PANEL_LABEL_STRING, "1 of N",
PANEL_CHOICE_STRINGS,
"extra", "large", "medium", "small", NULL,
XV_X, 20,
XV_Y, rect_bottom(qrt) + 20,
NULL);
</FONT></PRE>
<P>Note how we used the core class's <TT>XV_X</TT> and <TT>XV_Y</TT> attributes to
position this box below the Quit button. We got the position as a rectangle (<TT>typedef
Rect</TT>) of the Quit button via the <TT>xv_get</TT> call given the <TT>XV_RECT</TT>
attribute:</P>
<PRE><FONT COLOR="#0066FF">qrt = (Rect *) xv_get(quitbtn, XV_RECT);
</FONT></PRE>
<P>The position given by <TT>XV_X</TT> and <TT>XV_Y</TT> 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.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading37<FONT COLOR="#000077"><B>NOTE: </B></FONT>To position items
generally we can use two functions: <TT>xv_row()</TT> and <TT>xv_col()</TT>. These
functions use the <TT>WIN_ROW_GAP</TT> and <TT>WIN_COLUMN_GAP</TT> to set the spaces
between the items. The following example shows you how to position twelve items on
a panel:
</DL>
<DL>
<DD><FONT COLOR="#0066FF">#define ROW 3<BR>
#define COL 4 <BR>
extern char *name[ROW][COL]; <BR>
int i, j;<BR>
for (i = 0; i < ROW; i++)<BR>
for (j = 0; j < 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>
}</FONT>
<HR>
</DL>
<PRE></PRE>
<P>All items presented in this list are shown with the <TT>NULL</TT>-terminated list
passed in with the <TT>PANEL_CHOICE_STRINGS</TT> attribute. The default function
of <TT>PANEL_CHOICE</TT> 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 <TT>PANEL_CHOOSE_ONE</TT> to <TT>FALSE</TT>. This usage creates
the M of N items shown in the following lines:</P>
<PRE><FONT COLOR="#0066FF">manyN = (Panel) xv_create(panel, PANEL_CHOICE,
PANEL_LABEL_STRING, "M of N",
PANEL_CHOICE_STRINGS,
"tomato", "celery", "carrot" , 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 <TT>XV_RECT</TT> call to position this choice of many
item's button on the screen.</P>
<P>Finally, this example showed you how to use check boxes to create the input items
shown for our (United States) ex-vice president's choices of a side order. Checkboxes
are always non- exclusive. The text to do this is shown in the following lines:</P>
<PRE><FONT COLOR="#0066FF">chooser = (Panel) xv_create(panel, PANEL_CHECK_BOX,
PANEL_LAYOUT, PANEL_HORIZONTAL,
PANEL_LABEL_STRING, "Extras",
PANEL_CHOICE_STRINGS,
"fries", "potato", "Q. potatoe", "salad" , 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 <TT>qv_get</TT> and
<TT>rect_bottom()</TT> calls.
<H3 ALIGN="CENTER"><A NAME="Heading38<FONT COLOR="#000077">List Items</FONT></H3>
<P>Use the <TT>PANEL_LIST</TT> attribute to show lists of items. An example is shown
in Figure 35.7. The corresponding listing is shown in Listing 35.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 <TT>PANEL_LIST_INSERT_DUPLICATE</TT>
to <TT>FALSE</TT>. <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -