Example 25-3. The image.c program
/* image.c -- demonstrate the use of the image package. */
#include <xview/xview.h>
#include "image.h"
main(argc, argv)
int argc;
char *argv[ ];
{
Frame frame;
Image image1, image2;
Pixmap pixmap;
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
if (argc < 2)
puts("specify filename"), exit(1);
/* frame = (Frame)xv_create(NULL, FRAME, NULL); */
if (!(image1 = xv_create(NULL, IMAGE,
XV_WIDTH, 100,
XV_HEIGHT, 100,
SERVER_IMAGE_BITMAP_FILE, argv[1],
NULL)))
puts("unsuccessfully created image1"), exit(1);
if (!(image2 = xv_find(NULL, IMAGE,
SERVER_IMAGE_BITMAP_FILE, argv[1],
NULL)))
puts("unsuccessfully created image2"), exit(1);
printf("image1 %s image2\n",
(image2 != image1)? "matched" : "didn’t match");
pixmap = (Pixmap)xv_get(image1, SERVER_IMAGE_PIXMAP);
if (!(image2 = xv_find(NULL, IMAGE,
SERVER_IMAGE_PIXMAP, pixmap,
NULL)))
puts("unsuccessfully created image2"), exit(1);
printf("image1 %s image2\n",
(image2 != image1)? "matched" : "didn’t match");
/* window_fit(frame); */
/* xv_main_loop(frame); */
}
XView Internals
XView Internals 619
25.11 The Wizzy Package—A Panel Item Extension
This section presents an implementation for a panel item extension. This package is called
the Wizzy package; this example does not actually do anything but is presented to show how
to make extensions to the existing PANEL package. A panel item extension could be used to
define a new panel item. For example, using the methods described in this section, you could
implement a slider item that selects a range of values, rather than a single value. For this ex-
ample, you need to be familiar with the previous examples presented in this chapter and you
should also read Section 7.3.2, “Panel Item Layout,” in Chapter 7, Panels.
25.11.1 The Public Wizzy Header File
The public wizzy.h header file is defined as follows:
/* wizzy.h -- public header file for the Wizzy Xview class. */
#include <xview/xview.h>
#include <xview/panel.h>
extern Xv_pkg xv_panel_wizzy_pkg;
#define WIZZY &xv_panel_wizzy_pkg;
typedef Xv_panel_extension_item Wizzy;
#define ATTR_PKG_WIZZY ATTR_PKG_UNUSED_FIRST
#define WIZZY_ATTR (type, ordinal) ATTR(ATTR_PKG_WIZZY, type, ordinal)
typedef enum {
WIZZY_OFFSET = WIZZY_ATTR(ATTR_INT, 1),
WIZZY_FLAG = WIZZY_ATTR(ATTR_BOOLEAN, 2)
} Wizzy_attr;
25.11.2 The Private Wizzy Header File
The private data is declared for the Wizzy object in the implementation-specific header file,
wizzy_impl.h.
/* wizzy_impl.h -- private header file for the Wizzy Xview class. */
#include "wizzy.h"
typedef struct {
Panel_item public_self; /* pointer back to self */
Rect block; /* a rect this item’s panel value */
GC gc; /* a GC for this item */
int offset; /* an offset for block */
Panel panel; /* Panel that this item is owned by */
int flag; /* some boolean value */
#ifdef WIZZY_CAN_ACCEPT_KBD_FOCUS
620 XView Programming Manual
int has_kbd_focus; /* TRUE or FALSE */
#endif WIZZY_CAN_ACCEPT_KBD_FOCUS
} Wizzy_private;
#define WIZZY_PUBLIC(item) XV_PUBLIC(item)
#define WIZZY_PRIVATE(item) XV_PRIVATE(Wizzy_private, Wizzy, item)
#define BLOCK_WIDTH 16
#define BLOCK_HEIGHT 12
#define INITIAL_OFFSET 10
The only entry required for the Wizzy_private structure is public_self. All other en-
tries illustrate how you could implement the private data structure; they should be replaced
with your item’s private data requirements.
25.11.3 The Wizzy Package Declaration
The package is initialized in the following way:
#include <xview/wizzy.h>
extern Xv_pkg xv_panel_item_pkg;
Pkg_private int wizzy_init();
Pkg_private Xv_opaque wizzy_set_avlist();
Pkg_private Xv_opaque wizzy_get_attr();
Pkg_private int wizzy_destroy();
Xv_pkg xv_panel_wizzy_pkg = {
"Wizzy Item",
ATTR_PKG_WIZZY,
sizeof(Wizzy),
&xv_panel_item_pkg,
wizzy_init,
wizzy_set_avlist,
wizzy_get_attr,
wizzy_destroy,
NULL /* no find proc */
};
25.11.4 The Implementation Files
The implementation file for the Wizzy package is similar to the previous packages. It in-
cludes an initialize method, a set method, a get method, a destroy method, and an additional
panel operations vector table. Panel item handler procedures need to be defined by the pack-
age and placed in the panel operations vector table (see the description of panel item handler
procedures in Section 25.11.9, “Panel Item Handler Procedures”). XView defines the order
of procedures in the panel operations vector table. If you do not define a particular function,
you should place
NULL in the appropriate position for the function. There are fifteen proce-
dures, with one additional procedure reserved for future use. Once these procedures are
XView Internals
XView Internals 621
declared, they can be specified in the Panel_ops table. The declaration for panel-item
handler procedures for the Wizzy Package follow:
static void wizzy_begin_preview();
static void wizzy_update_preview();
static void wizzy_accept_preview();
static void wizzy_cancel_preview();
static void wizzy_accept_menu();
static void wizzy_accept_key();
static void wizzy_clear();
static void wizzy_paint();
static void wizzy_resize();
static void wizzy_remove();
static void wizzy_restore();
static void wizzy_layout();
static void wizzy_accept_kbd_focus();
static void wizzy_yield_kbd_focus();
The panel item operations table itself is declared as follows:
static Panel_ops ops = {
panel_default_handle_event, /* handle_event() */
wizzy_begin_preview, /* begin_preview() */
wizzy_update_preview, /* update_preview() */
wizzy_cancel_preview, /* cancel_preview() */
wizzy_accept_preview, /* accept_preview() */
wizzy_accept_menu, /* accept_menu() */
wizzy_accept_key, /* accept_key() */
wizzy_clear, /* clear() */
wizzy_paint, /* paint() */
wizzy_resize, /* resize() */
wizzy_remove, /* remove() */
wizzy_restore, /* restore() */
wizzy_layout, /* layout() */
#ifdef WIZZY_CAN_ACCEPT_KBD_FOCUS
wizzy_accept_kbd_focus, /* accept_kbd_focus() */
wizzy_yield_kbd_focus, /* yield_kbd_focus() */
#else
NULL, /* accept_kbd_focus() */
NULL, /* yield_kbd_focus() */
#endif WIZZY_CAN_ACCEPT_KBD_FOCUS
NULL /* reserved for future use */
};
The Section 25.11.9, “Panel Item Handler Procedures,” provides a complete explanation of
each panel item handler procedure.
25.11.5 The Wizzy Initialize Method
The task of the initialize routine for the Wizzy class is primarily to initialize the private data
of the Wizzy object and set any create-only attributes.
Pkg_private int
wizzy_init(panel, item, avlist)
Panel panel; /* parent */
622 XView Programming Manual
Panel_item item; /* this object */
Attr_avlist avlist; /* attribute-value pair list */
{
Wizzy_public *item_object = (Wizzy_public *)item; /* this item */
Display *display;
Wizzy_private *dp;
XGCValues gcvalues;
XID xid;
Attr_attribute *attrs;
dp = xv_alloc(Wizzy_private);
/* link the public to the private, link the private to the public */
item_object->private_data = (Xv_opaque)dp;
dp->public_self = item;
/* initialize any non-zero private data members */
display = (Display *)XV_DISPLAY_FROM_WINDOW(panel);
xid = (XID)xv_get(panel, XV_XID);
gcvalues.foreground = BlackPixel(display, 0);
dp->gc = XCreateGC(display, xid, GCForeground, &values);
dp->offset = INITIAL_OFFSET;
dp->panel = panel;
/* Process any create-only attributes from avlist */
for ( attrs = avlist; *avlist; attrs = attr_next(attrs)) {
switch ( (int)attrs[0 ] ) {
/* case <create_only_attr>: */
default:
break;
}
}
xv_set(item, PANEL_OPS_VECTOR, &ops,
#ifdef WIZZY_CAN_ACCEPT_KBD_FOCUS
PANEL_ACCEPT_KEYSTROKE, TRUE,
#endif WIZZY_CAN_ACCEPT_KBD_FOCUS
NULL);
return XV_OK;
}
Once the private data is allocated and the public and private structures are linked to one an-
other, the private data fields are initialized. Then the avlist is scanned for any create-only
attributes.
Next, xv_set is used to store the address of the panel operations vector table and allow the
Wizzy package to accept keyboard input as specified.
XView Internals
XView Internals 623
Get Volume 7A: XView Programming Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.