📄 ch35.htm
字号:
<UL>
<LI>The <TT>XV</TT> toolkit is initialized as soon as possible in the application
with the <TT>xv_init</TT> call.
<P>
<LI>All attribute values to the <TT>xv_create()</TT> function call are terminated
with a <TT>NULL</TT> parameter.
<P>
<LI>The <TT>(Frame)</TT> cast is used to override the default returned value from
<TT>xv_create()</TT>.
<P>
<LI>The <TT><xview/generic.h></TT> header file is used to get all the required
definitions for the file.
</UL>
<H4 ALIGN="CENTER"><A NAME="Heading14<FONT COLOR="#000077">Initialization</FONT></H4>
<P>You should initialize the XView system as soon as possible in any application.
The <TT>xv_init()</TT> call does this for you. By default, <TT>xv_init()</TT> uses
the <TT>DISPLAY</TT> environment variable for you. By passing the <TT>argc</TT> and
<TT>argv</TT> values you can override the default values for the application from
the command line. You can use <TT>xv_init()</TT> only once in an application; the
libraries ignore all other calls. Normally you'd override the <TT>DISPLAY</TT> variable
if you wanted to display the window on a different machine. <BR>
<BR>
<A HREF="../art/35/35lnx02.jpg"><B>Figure 35.2.</B></A> <I>A sample XView application.
</I><BR>
<BR>
You can use two types of parameters for the first argument to <TT>xv_init()</TT>:
<TT>XV_INIT_ARGS</TT>, which leaves the <TT>argc</TT> and <TT>argv</TT> unchanged,
or <TT>XV_INIT_ARGC_PTR_ARGV</TT>, which modifies <TT>argc</TT> and <TT>argv</TT>
to remove all XView-specific arguments. With <TT>XV_INIT_ARGS</TT>, you pass <TT>argc</TT>
into <TT>xv_init</TT> and with <TT>XV_INIT_ARGC_PTR_ARGV</TT> you pass the address
of <TT>argc</TT> to <TT>xv_init()</TT>.
<H4 ALIGN="CENTER"><A NAME="Heading15<FONT COLOR="#000077">Creating Objects</FONT></H4>
<P>The <TT>xv_create</TT> function is used to create all the objects in an application.
The syntax for the <TT>xv_create</TT> function is</P>
<PRE><FONT COLOR="#0066FF">xv_object xv_create(xv_object owner, xv_package pkg, void *attr)
</FONT></PRE>
<P>where the owner is the parent of the object being created, and of type pkg given
the attributes listed in variable length arguments starting with <TT>attr</TT>. Sometimes
you can use a <TT>NULL</TT> value in place of the owner parameter to indicate that
the owner value can be substituted for screen or server as appropriate. However,
in some calls the owner parameter must point to a valid object, so the <TT>NULL</TT>
value will generate an error.</P>
<P>The attributes for the newly created object inherit their behavior from their
parents. The attributes can be overridden by values included in the variable list
specified in <TT>attr</TT>.</P>
<P>The values of attributes are set in the following decreasing order of precedence:
<UL>
<LI>A call to <TT>xv_set</TT> will override any other type of setting
<P>
<LI>Any command-line arguments
<P>
<LI>Values in the <TT>.Xdefaults</TT> file
<P>
<LI>Values in the attributes of an <TT>xv_create</TT> call
<P>
<LI>Window Manager defaults
</UL>
<H4 ALIGN="CENTER"><A NAME="Heading16<FONT COLOR="#000077">Exiting an Application</FONT></H4>
<P>The best way to get out of an XView application is to destroy the topmost object.
Use the <TT>xv_destroy_safe()</TT> function call, which waits for the destruction
of all derived objects and cleans up after itself. You can also use <TT>xv_destroy()</TT>
to get out immediately with the risk of not giving up system resources but be able
to exit very quickly. If you don't give up resources, they will not be freed for
use by any other applications in the system and will use up valuable memory space.
<H3 ALIGN="CENTER"><A NAME="Heading17<FONT COLOR="#000077">Frames</FONT></H3>
<P>A frame is a container for other windows. A frame manages the geometry of subwindows
that do not overlap. Some examples include canvases, text windows, panels, and scrollbars.
You saw a base frame in the output of <TT>LIST35_2.c</TT> (refer to Figure 35.2 and
Listing 35.2).</P>
<P>Frames enable you to specify three types of outputs on three areas. The topmost
area is the name on the top of the frame called the header. The bottom of the frame
is divided into two sections; one is left-justified and the other is right-justified.
Figure 35.3 shows the output from Listing 35.3, which shows you how to write to these
areas.
<H3 ALIGN="CENTER"><A NAME="Heading18<FONT COLOR="#000077">Listing <A NAME="Heading1935.3.
Header and footer frames.</FONT></H3>
<PRE><FONT COLOR="#0066FF">/*
**
** Listing to show headers and footers.
**
*/
#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, "Title Here",
FRAME_SHOW_FOOTER, TRUE,
FRAME_LEFT_FOOTER, "left side",
FRAME_RIGHT_FOOTER, "right side",
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);
xv_main_loop(frame);
exit(0);
}
void quit()
{
xv_destroy_safe(frame);
}
</FONT></PRE>
<P><A HREF="../art/35/35lnx03.jpg"><B>Figure 35.3.</B></A> <I>Header and footer frames.
</I><BR>
<BR>
The parameters used to create these footers are shown in the following lines:</P>
<PRE><FONT COLOR="#0066FF">Frame frame;
frame = (Frame) xv_create((int)NULL, FRAME,
FRAME_LABEL, "Title Here",
FRAME_SHOW_FOOTER, TRUE,
FRAME_LEFT_FOOTER, "left side",
FRAME_RIGHT_FOOTER, "right side",
XV_WIDTH, 200,
XV_HEIGHT, 100,
NULL);
</FONT></PRE>
<P>You have to turn the footer display on with the <TT>FRAME_SHOW_FOOTER</TT> attribute
set to <TT>TRUE</TT>. The other values in this call actually set the values of the
header and footer.
<H3 ALIGN="CENTER"><A NAME="Heading20<FONT COLOR="#000077">Command Frames</FONT></H3>
<P>Command frames are usually used to perform a quick function and then disappear.
These frames are usually pop-up frames like the pushpin dialog boxes you saw in Chapter
24, "OPEN LOOK and OpenWindows." If the pushpin is pressed in, the dialog
box remains "pinned" to the screen; otherwise, the dialog box will go away
after the user performs the section.</P>
<P>Listing 35.4 shows you a program to create command frames.
<H3 ALIGN="CENTER"><A NAME="Heading21<FONT COLOR="#000077">Listing <A NAME="Heading2235.4.
Using command frames.</FONT></H3>
<PRE><FONT COLOR="#0066FF">/*
** Sample Application to show command frames.
**
*/
#include <xview/generic.h>
#include <xview/xview.h>
#include <xview/frame.h>
#include <xview/panel.h>
/*
** Global Frames
*/
Frame frame;
Frame popup;
/*
**
** Declare the used functions here
**
*/
void show_greeting(Frame *fp);
int show_popup();
int push_it();
void quit();
/*
** The main function
*/
int main(int argc, char *argv[])
{
Panel panel;
Panel fpanel;
/*
** Initialize the toolkit
*/
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
/*
** Create top level frame.
*/
frame = (Frame)xv_create((int)NULL, FRAME,
FRAME_LABEL, "Title Here",
FRAME_SHOW_FOOTER, TRUE,
FRAME_LEFT_FOOTER, "Popup",
FRAME_RIGHT_FOOTER, argv[1],
XV_WIDTH, 200,
XV_HEIGHT, 100,
NULL);
/*
** Create the popup Frame.
*/
popup = (Frame) xv_create(frame, FRAME_CMD,
FRAME_LABEL, "Popup",
XV_WIDTH, 100,
XV_HEIGHT, 100,
NULL);
/*
** Create panel for popup
*/
fpanel = (Panel)xv_get(popup, FRAME_CMD_PANEL,NULL);
/*
** Add buttons to popup
*/
(void) xv_create(fpanel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Greet",
PANEL_NOTIFY_PROC, show_greeting,
NULL);
(void) xv_create(fpanel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Push Me",
PANEL_NOTIFY_PROC, push_it,
NULL);
/*
** Create panel
*/
panel = (Panel)xv_create(frame, PANEL,NULL);
/*
** Add buttons to main application frame
*/
(void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Hello",
PANEL_NOTIFY_PROC, show_popup,
NULL);
(void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Quit",
PANEL_NOTIFY_PROC, quit,
NULL);
xv_main_loop(frame);
exit(0);
}
void quit()
{
xv_destroy_safe(frame);
}
void show_greeting(Frame *fp)
{
printf ("Greet you? How?\n");
}
show_popup(Frame item, Event *ev)
{
xv_set(popup, XV_SHOW, TRUE, NULL);
}
push_it(Panel_item item, Event *ev)
{
int ret;
ret = (int)xv_get(popup, FRAME_CMD_PIN_STATE) ;
if (ret == FRAME_CMD_PIN_IN)
{
printf("Pin already in.. bye\n");
xv_set(popup, XV_SHOW, TRUE, NULL); /* refresh anyway */
}
else
{
printf("Pin out.. pushing it in\n");
xv_set(popup, FRAME_CMD_PIN_STATE, FRAME_CMD_PIN_IN, NULL);
xv_set(popup, XV_SHOW, TRUE, NULL); /* refresh anyway */
}
}
</FONT></PRE>
<P>The output from Listing 35.4 is shown in Figure 35.4.<BR>
<BR>
<A HREF="../art/35/35lnx04.jpg"><B>Figure 35.4.</B></A> <I>Using command frames.</I></P>
<P>Look at the important lines of the program in Listing 35.4 in detail. By examining
these lines you will learn the following:
<UL>
<LI>How to create pop-up menus
<P>
<LI>How to add buttons to a panel
<P>
<LI>How to handle callback functions for <TT>xview</TT> objects
</UL>
<P>There are two frames in this application: <TT>frame</TT> and <TT>popup</TT>. These
frames are declared at the top of the application with the statements</P>
<PRE><FONT COLOR="#0066FF">Frame frame;
Frame popup;
</FONT></PRE>
<P>We also declared the following functions in this application:
<UL>
<LI><TT>void show_greeting(Frame *fp)</TT>: This function is called when the Greeting
button is pressed.
<P>
<LI><TT>int show_popup()</TT>: This function is called when the Hello button is pressed.
<P>
<LI><TT>int push_it()</TT>: This function is called when the Push Me button is pressed.
<P>
<LI><TT>void quit()</TT>: This function is called when the Quit button is pressed.
</UL>
<P>The main <TT>xv_init()</TT> and frame creation for the program is as in Listing
35.3. Let's concentrate on the pop-up menu examples.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -