📄 ch35.htm
字号:
<BR>
<A HREF="../art/35/35lnx07.jpg">Figure 35.7.</A> <I>Using lists to display data.</I>
<H3 ALIGN="CENTER"><A NAME="Heading39<FONT COLOR="#000077">Listing <A NAME="Heading4035.8.
Using lists to display data.</FONT></H3>
<PRE><FONT COLOR="#0066FF">/*
** Using Lists
*/
#include <xview/generic.h>
#include <xview/xview.h>
#include <xview/frame.h>
#include <xview/panel.h>
Frame frame;
int main(int argc, char *argv[])
{
Panel panel;
void quit();
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
frame = (Frame)xv_create((int)NULL, FRAME,
FRAME_LABEL, argv[1],
XV_WIDTH, 200,
XV_HEIGHT, 100,
NULL);
panel = (Panel)xv_create(frame, PANEL,NULL);
(void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Quit",
PANEL_NOTIFY_PROC, quit,
NULL);
(void) xv_create(panel, PANEL_LIST,
PANEL_LIST_STRINGS,
"Business", "Economics", "History",
"Literature", "TomFoolery", "Math",
"Computer Sci.", "Engineering", 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 <TT>xv_set</TT> function:</P>
<PRE><FONT COLOR="#0066FF">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.</P>
<P>To insert items into this list you can use <TT>PANEL_LIST_INSERT</TT> and <TT>PANEL_LIST_STRING</TT>
calls. If you wanted to replace the third row with a string pointed to by a pointer
called <TT>buffer</TT>, you would use the following function call:</P>
<PRE><FONT COLOR="#0066FF">xv_set(list_item,
PANEL_LIST_DELETE, 2,
PANEL_LIST_INSERT, 2,
PANEL_LIST_STRING, buffer,
NULL);
</FONT></PRE>
<P>The <TT>PANEL_NOTIFY_PROC</TT> function for a list is called when an item is selected,
deselected, added, or deleted. The prototype for this function call is</P>
<PRE><FONT COLOR="#0066FF">listCallBack(
Panel_item item,
char *string,
Xv_opaque client_data,
Panel_list_op op,
Event *event,
int row);
</FONT></PRE>
<P>The <TT>item</TT> is the panel list itself in this function call. The <TT>string</TT>
is the label for the row, or <TT>NULL</TT> if no item is defined in the list for
the row. The opaque <TT>client_data</TT> is a user-specified value specified at list
creation time with the <TT>PANEL_LIST_CLIENT_DATA</TT> parameter. For example, the
line</P>
<PRE><FONT COLOR="#0066FF">PANEL_LIST_CLIENT_DATA, 2, "Hello",
</FONT></PRE>
<P>will assign the value of <TT>client_data</TT> to 2 for the row with the string
<TT>"Hello"</TT> in it. Each <TT>client_data</TT> value must be assigned
one line at a time.</P>
<P>The <TT>op</TT> parameter can be one of the following values:
<UL>
<LI><TT>PANEL_LIST_OP_SELECT</TT> when the row is selected
<P>
<LI><TT>PANEL_LIST_OP_DESELECT</TT> when the row is deselected
<P>
<LI><TT>PANEL_LIST_OP_VALIDATE</TT> when a new row is added
<P>
<LI><TT>PANEL_LIST_OP_DELETE</TT> when the row has been deleted
</UL>
<P>You can take action based on the value of the <TT>op</TT> parameter in one handy
function or have this function call other functions. For example, the following pseudocode
illustrates how you could handle the <TT>op</TT> parameter:</P>
<PRE><FONT COLOR="#0066FF">switch (op)
{
case PANEL_LIST_OP_SELECT: selectHandler();
break;
case PANEL_LIST_OP_DESELECT: unSelectHandler();
break;
case PANEL_LIST_OP_VALIDATE: addRowHandler();
break;
case PANEL_LIST_OP_DELETE: deleteRowHandler();
break;
}
</FONT></PRE>
<H3 ALIGN="CENTER"><A NAME="Heading41<FONT COLOR="#000077">Scale Bars</FONT></H3>
<P>Now look at how you create slider bars so the user can set the value of a variable.
An example of this application is shown in Figure 35.8 and a corresponding listing
is given in Listing 35.9.
<H3 ALIGN="CENTER"><A NAME="Heading42<FONT COLOR="#000077">Listing <A NAME="Heading4335.9.
Using slider control.</FONT></H3>
<PRE><FONT COLOR="#0066FF">#include <xview/generic.h>
#include <xview/xview.h>
#include <xview/frame.h>
#include <xview/panel.h>
Frame frame;
Panel_item stationName;
void display_setting(Panel_item, int value, Event *ev);
int main(int argc, char *argv[])
{
Panel panel;
Panel_item slider;
void quit();
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, 100,
NULL);
panel = (Panel)xv_create(frame, PANEL,
PANEL_LAYOUT, PANEL_VERTICAL,
NULL);
(void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Quit",
PANEL_NOTIFY_PROC, quit,
NULL);
slider = xv_create (panel, PANEL_SLIDER,
PANEL_LABEL_STRING, "Radio Station",
PANEL_MIN_VALUE, 88,
PANEL_MAX_VALUE, 108,
PANEL_NOTIFY_PROC, display_setting,
PANEL_VALUE,99,
PANEL_NOTIFY_LEVEL, PANEL_ALL, /* not just at the end */
PANEL_SHOW_RANGE, TRUE,
PANEL_TICKS,10,
PANEL_SLIDER_WIDTH, 100,
NULL);
stationName = xv_create(panel, PANEL_MESSAGE,
PANEL_LABEL_STRING, "sample",
NULL);
xv_main_loop(frame);
exit(0);
}
void quit()
{
xv_destroy_safe(frame);
}
/*
** This function is called when the slider value is changed.
*/
void display_setting(Panel_item item, int value, Event *ev)
{
switch (value)
{
case 89: xv_set(stationName,
PANEL_LABEL_STRING,"Classical", NULL); break;
case 91: xv_set(stationName,
PANEL_LABEL_STRING,"Industrial", NULL); break;
case 93: xv_set(stationName,
PANEL_LABEL_STRING,"Country", NULL); break;
case 95: xv_set(stationName,
PANEL_LABEL_STRING,"Soft Rock", NULL); break;
case 101: xv_set(stationName,
PANEL_LABEL_STRING,"Roll N Roll", NULL); break;
case 104: xv_set(stationName,
PANEL_LABEL_STRING,"Pop", NULL); break;
case 107: xv_set(stationName,
PANEL_LABEL_STRING,"Alternative", NULL); break;
default: xv_set(stationName,
PANEL_LABEL_STRING,"bzzz", NULL); break; }
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
}
</FONT></PRE>
<P>
<A HREF="../art/35/35lnx08.jpg">Figure 35.8.</a> Using sliders.
<P>To create a slider, assign the <TT>PANEL_SLIDER</TT>
value to the <TT>xv_create()</TT> function call. How the slider is used and displayed
is governed by the following attributes:
<UL>
<LI><TT>PANEL_MIN_VALUE</TT> and <TT>PANEL_MAX_VALUE</TT>: The range of values that
this slider can take. These values have to be integers. For the example in this book
we used 88 and 108.
<P>
<LI><TT>PANEL_SHOW_RANGE</TT>: Sets the slider to show the value of the ranges allowed
for the selection.
<P>
<LI><TT>PANEL_NOTIFY_LEVEL</TT>: Can be set to one of two values: <TT>PANEL_ALL</TT>
if the callback procedure is called while the slider is moving, or <TT>PANEL_DONE</TT>
only when the pointer button is released.
<P>
<LI><TT>PANEL_DIRECTION</TT>: Can be used to set the orientation of the slider to
either horizontal or vertical.
<P>
<LI><TT>PANEL_TICKS</TT>: The number of ticks that show on the display. Set it to
0 if you do not want ticks to be shown. The number of ticks is adjusted as you size
the slider. You fix the width of the slider by setting the <TT>PANEL_SLIDER_WIDTH</TT>
to 100 (refer to List- ing 35.8).
</UL>
<P>You can edit the selection value by clicking it and using the keyboard. This value
will change the location of the slider as soon as you press the Enter key. Error
values will be ignored.</P>
<P>Note how a message label displays the station name as the slider is being moved.
To set the value of this label, make a call to <TT>xv_set()</TT> and give the attribute
<TT>PANEL_LABEL_STRING</TT> a string value. For example, if the alue of the slider
is 89, you can set the message to <TT>"Classical"</TT>, as shown in the
following lines:</P>
<PRE><FONT COLOR="#0066FF">case 89: xv_set(stationName,
PANEL_LABEL_STRING,"Classical", NULL); break;
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading44<FONT COLOR="#000077"><B>TIP:</B> </FONT>You can create a
gauge by using the <TT>PANEL_GAUGE</TT> package instead of <TT>PANEL_SLIDER</TT>.
The dimensions of the gauge are set by the <TT>PANEL_GAUGE_WIDTH</TT> and <TT>PANEL_GAUGE_HEIGHT</TT>
attributes. A user cannot change the value of the slider on a gauge because a gauge
can be used only as a feedback item.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading45<FONT COLOR="#000077">Text Windows</FONT></H3>
<P>XView has a lot of options for displaying data. This section will cover only a
few portions of this feature. Please refer to the man pages for Text in <TT>/usr/openwin/man</TT>.
Let's get started with some of the basics, though. A sample application is shown
in Listing 35.10, and its corresponding output is shown in Figure 35.9.
<H3 ALIGN="CENTER"><A NAME="Heading46<FONT COLOR="#000077">Listing <A NAME="Heading4735.10.
Using text items.</FONT></H3>
<PRE><FONT COLOR="#0066FF">#include <xview/generic.h>
#include <xview/xview.h>
#include <xview/frame.h>
#include <xview/panel.h>
Frame frame;
int main(int argc, char *argv[])
{
Panel panel;
void quit();
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
frame = (Frame)xv_create((int)NULL, FRAME,
FRAME_LABEL, argv[1],
XV_WIDTH, 300,
XV_HEIGHT, 300,
NULL);
panel = (Panel)xv_create(frame, PANEL,NULL);
(void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Quit",
PANEL_NOTIFY_PROC, quit,
NULL);
xv_create(panel, PANEL_TEXT,
PANEL_LABEL_STRING, "Single",
PANEL_VALUE, "Single Line of Text",
NULL);
xv_create(panel, PANEL_MULTILINE_TEXT,
PANEL_LABEL_STRING, "Multi",
PANEL_DISPLAY_ROWS, 3,
PANEL_VALUE_DISPLAY_LENGTH, 30,
PANEL_VALUE, "Multiple Lines of Text \
in this example \
This is a line 1\
This is a line 2\
This is a line 3\
of some long string",
NULL);
xv_main_loop(frame);
exit(0);
}
void quit()
{
xv_destroy_safe(frame);
}
</FONT></PRE>
<P>We created a single panel text entry item with the following lines
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -