📄 ch35.htm
字号:
<P>First, the pop-up frame is created with the following lines:</P>
<PRE><FONT COLOR="#0066FF">popup = (Frame) xv_create(frame, FRAME_CMD,
FRAME_LABEL, "Popup",
XV_WIDTH, 100,
XV_HEIGHT, 100,
NULL);
</FONT></PRE>
<P>This call will create the pop-up frame with <TT>frame</TT> as the owner. The pop-up
frame is not displayed immediately. You can create several pop-up frames this way
and display them only when they are needed.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading23<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Note that if you
do not set the <TT>XV_WIDTH</TT> and <TT>XV_HEIGHT</TT> parameters for this <TT>xv_create()</TT>
call, the pop-up screen will occupy the entire screen.
<HR>
</DL>
<P>Next we create a panel for this pop-up with the call</P>
<PRE><FONT COLOR="#0066FF">fpanel = (Panel)xv_get(popup, FRAME_CMD_PANEL,NULL);
</FONT></PRE>
<P>Then we add the Greet and Push Me buttons to this new <TT>fpanel</TT>. This is
done by the <TT>xv_create</TT> calls, which are shown next:</P>
<PRE><FONT COLOR="#0066FF">(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);
</FONT></PRE>
<P>At this point you are ready to create the main application frame, show it, and
go into the main loop. The important call that does this is shown next. The function
<TT>show_popup()</TT> is assigned to be called when the Hello button is pressed.</P>
<PRE><FONT COLOR="#0066FF"> (void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Hello",
PANEL_NOTIFY_PROC, show_popup,
NULL);
</FONT></PRE>
<P>Now look at the functions that are called when each button is pressed. The <TT>show_greeting()</TT>
function simply prints out a string. (You can use your imagination here for the contents
of the string for your own application.) The <TT>show_popup()</TT> function will
use the call to the <TT>xv_set()</TT> function to actually make the pop-up frame
visible.</P>
<PRE><FONT COLOR="#0066FF">xv_set(popup, XV_SHOW, TRUE, NULL);
</FONT></PRE>
<P>Now for the function that will emulate the behavior of pushing in the pin. This
is the <TT>push_it()</TT> function. The <TT>FRAME_CMD_PIN_STATE</TT> parameter requests
the state of the pushpin on the dialog box. The state for the pin is defined as <TT>FRAME_CMD_PIN_IN</TT>
if the pushpin is already pushed in. This is the state for which you check. If the
pushpin is not in this state, it is pushed in with the <TT>xv_set(popup, FRAME_CMD_PIN_STATE,
FRAME_CMD_PIN_IN, NULL);</TT> function call.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading24<FONT COLOR="#000077"><B>TIP: </B></FONT>A command frame
has no resize corners by default. To turn these corners on, set <TT>FRAME_SHOW_RESIZE_CORNERS</TT>
to <TT>TRUE</TT>.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading25<FONT COLOR="#000077">Setting Colors on
Frames</FONT></H3>
<P>The colors on an XView frame object are defaulted to the <TT>OpenWindows.WindowColor</TT>
resource. This resource is inherited by all subframes as well. You can override these
colors with the CMS package. The CMS package is created by a call to <TT>xv_create()</TT>:</P>
<PRE><FONT COLOR="#0066FF">cms = (Cms *) xv_create(parent, CMS, attrs, NULL);
</FONT></PRE>
<P>A CMS can contain as many colors as are allowed in the largest color map you can
create. You can have several color maps referencing the same color; in fact, the
system can share the location of colors between two independent applications. For
this reason, you should allocate all your colors once, at CMS creation, to allocate
all the colors in your color map to prevent another application from changing the
colors you absolutely need.</P>
<P>For example, to create a CMS with four named colors, you would use the following
function call:</P>
<PRE><FONT COLOR="#0066FF">cms = (Cms *)xv_create(parent, CMS,
CMS_SIZE, 4,
CMS_NAMED_COLORS, "Violet", "Yellow", "Blue", "Orange",
NULL);
</FONT></PRE>
<P>The <TT>CMS_SIZE</TT> value asks for a four-entry color table that is indexed
from <TT>0</TT> to <TT>3</TT>, with the values of the named colors <TT>"Violet"</TT>,
<TT>"Yellow"</TT>, <TT>"Blue"</TT>, and <TT>"Orange"</TT>.
The foreground color for a frame is the first indexed color in a CMS, and the background
color for a frame is the last indexed (n-1) color in a CMS. Setting a <TT>CMS_SIZE</TT>
will give you either an error or a monochromatic display. Of course, to avoid runtime
errors you must know that the colors you just specified by name do exist in the <TT>/usr/lib/rgb.txt</TT>
file.</P>
<P>Listing 35.5 is an example of an application that sets the colors. This will let
you set the foreground and background colors of a frame and all its children.
<H3 ALIGN="CENTER"><A NAME="Heading26<FONT COLOR="#000077">Listing <A NAME="Heading2735.5.
Using CMS.</FONT></H3>
<PRE><FONT COLOR="#0066FF">#include <xview/generic.h>
#include <xview/xview.h>
#include <xview/frame.h>
#include <xview/panel.h>
#include <xview/cms.h>
Frame frame;
#define FORE 3
#define BACK 0
int main(int argc, char *argv[])
{
Cms cms;
Panel panel;
void quit();
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
cms = (Cms ) xv_create((int)NULL,CMS, /* NULL -> use the default Frame*/
CMS_SIZE, CMS_CONTROL_COLORS + 4,
CMS_CONTROL_CMS, True,
CMS_NAMED_COLORS, "LightBlue", "Blue", "Red", "Green", NULL,
NULL);
frame = (Frame)xv_create((int)NULL, FRAME,
FRAME_LABEL, argv[1],
XV_WIDTH, 200,
XV_HEIGHT, 100,
NULL);
xv_set(frame,
WIN_CMS, cms,
WIN_FOREGROUND_COLOR, CMS_CONTROL_COLORS + FORE,
WIN_BACKGROUND_COLOR, CMS_CONTROL_COLORS + BACK,
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>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading28<FONT COLOR="#000077"><B>TIP: </B></FONT>You cannot use <TT>xv_get</TT>
with the <TT>CMS_NAMED_COLORS</TT> attribute.
<HR>
</DL>
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading29<FONT COLOR="#000077">CAUTION: </FONT>Use <TT>xv_set()</TT>
to override the colors on a frame. Any color requests on a frame at the time of creation
are overridden by values of the <TT>.Xdefaults</TT> resources values.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading30<FONT COLOR="#000077">Canvases</FONT></H3>
<P>A canvas is an XView object that is used to display items that are too large to
fit on a window. The viewable portion of the image is seen through a viewport or
view window of the object. You can have multiple views of the same data that is stored
on a canvas by splitting each scrollable portion into two or more views. The split
views must all reside on the same canvas because you cannot have multiple views of
canvas data that are not on the same frame.</P>
<P>There are three components of a canvas object:
<UL>
<LI>The Paint window, which contains the actual painted data
<P>
<LI>The View window, which has the scrollbars but no painted data
<P>
<LI>The canvas subwindow, which contains the union of the View window and Paint window
</UL>
<P>Look at a simple example in Listing 35.6 of how to use scrollbars and how to paint
on a paint window. (I have added line numbers for readability.)
<H3 ALIGN="CENTER"><A NAME="Heading31<FONT COLOR="#000077">Listing <A NAME="Heading3235.6.
Using canvases and scrollbars.</FONT></H3>
<PRE><FONT COLOR="#0066FF"> 1 /*
2 ** An example of a scrolled window
3 */
4 #include <X11/Xlib.h>
5 #include <xview/generic.h>
6 #include <xview/xview.h>
7 #include <xview/frame.h>
8 #include <xview/panel.h>
9 #include <xview/canvas.h>
10 #include <xview/scrollbar.h>
11 #include <xview/xv_xrect.h>
12 /*
13 ** Declare our callback functions for this application.
14 */
15 Frame frame;
16 void redraw(Canvas c, Xv_Window pw, Display *dp, Window xwin,
17 Xv_xrectlist *rp) ;
18 int main(int argc, char *argv[])
19 {
20 Canvas canvas;
21 Panel panel;
22 Scrollbar h_s, v_s;
23 void quit();
24 xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
25 frame = (Frame)xv_create((int)NULL, FRAME,
26 FRAME_LABEL, argv[1],
27 XV_WIDTH, 400,
28 XV_HEIGHT, 200,
29 (int)NULL);
30 /*
31 ** Create the canvas.
32 */
33 canvas = (Canvas) xv_create(frame, CANVAS,
34 CANVAS_REPAINT_PROC, redraw,
35 CANVAS_X_PAINT_WINDOW, TRUE,
36 CANVAS_AUTO_SHRINK, FALSE,
37 CANVAS_AUTO_EXPAND, TRUE,
38 CANVAS_WIDTH, 500,
39 CANVAS_HEIGHT, 500,
40 XV_WIDTH, 400,
41 XV_HEIGHT, 200,
42 NULL);
43 /*
44 ** Create the splittable scrollbars
45 */
46 h_s = (Scrollbar)xv_create(canvas, SCROLLBAR,
47 SCROLLBAR_DIRECTION, SCROLLBAR_HORIZONTAL,
48 SCROLLBAR_SPLITTABLE, TRUE,
49 NULL);
50 v_s = (Scrollbar)xv_create(canvas, SCROLLBAR,
51 SCROLLBAR_DIRECTION, SCROLLBAR_VERTICAL,
52 SCROLLBAR_SPLITTABLE, TRUE,
53 NULL);
54 xv_main_loop(frame);
55 exit(0);
56 }
57 void redraw(Canvas c, Xv_Window pw, Display *dp, Window xwin,
58 Xv_xrectlist *rp)
59 {
60 GC gc;
61 int wd, ht;
62 int i;
63 int j;
64 int dx;
65 int dy;
66 gc = DefaultGC(dp, DefaultScreen(dp));
67 wd = (int)xv_get(pw, XV_WIDTH);
68 ht = (int)xv_get(pw, XV_HEIGHT);
69 dy = ht / 10;
70 for (j = 0; j < ht; j += dy)
71 XDrawLine(dp, xwin, gc, j,0,j,ht);
72
73 dx = wd / 10;
74 for (i = 0; i < wd; i += dx)
75 XDrawLine(dp,xwin,gc, 0,i,wd,i);
76 }
77 void quit()
78 {
79 xv_destroy_safe(frame);
80 }
</FONT></PRE>
<P>Lines 33 through 42 create the canvas. The <TT>CANVAS_AUTO_EXPAND</TT> and <TT>CANVAS_AUTO_SHRINK</TT>
parameters maintain the relation of the canvas subwindow and the paint subwindow.
These values default to <TT>TRUE</TT>. When both values are <TT>TRUE</TT>, the canvas
and paint subwindows will expand or shrink based on the size of the window on which
they are being displayed.</P>
<P>Setting the <TT>CANVAS_AUTO_EXPAND</TT> value to <TT>TRUE</TT> enables the paint
subwindow to expand larger than the canvas subwindow. If the canvas subwindow expands
to a bigger size than the paint subwindow, the paint subwindow is expanded to at
least that size as well. If the canvas subwindow size shrinks, the paint subwindow
does not shrink because it is already at the same size or bigger than canvas subwindows
at that time.</P>
<P>Setting the <TT>CANVAS_AUTO_SHRINK</TT> value to <TT>TRUE</TT> forces the canvas
object to always confirm that the paint subwindow's height and width are never greater
than the canvas subwindow. In other words, the size of the paint subwindow is always
changed to be at least the same or less than the size of the canvas subwindow.</P>
<P>You can explicitly set the size of the canvas window with the <TT>CANVAS_WIDTH</TT>
and <TT>CANVAS_HEIGHT</TT> parameters. (See lines 38 and 39.) These canvas dimensions
can be greater than the viewing window dimensions set with <TT>XV_WIDTH</TT> and
<TT>XV_HEIGHT</TT> (lines 40 and 41).</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -