Example 18-11. Sample conversion procedure
SelectionConvertProc (continued)
} else
/* Call the default selection conversion procedure.
* It handles requests for any pre-registered
* conversions, including TARGETS.
*/
return(sel_convert_proc(sel, target, data, length, format));
}
18.8.1 The Done Procedure
The selection done procedure is called after each conversion has happened. This gives the
application a chance to free up memory.
void
SelectionDoneProc(sel, data, target)
Selection_owner sel;
Xv_opaque data;
Atom target;
{
if (target == ATOM(server, "STRING"))
free((char *)data);
}
Note, in this example, we only alloc data for string requests so we only free it for string data.
18.8.2 The Lose Procedure
The lose procedure lets the selection package lose ownership a selection gracefully. For
example, the user can can be informed that the selection was lost, as in the following
example.
void
SelectionLoseProc(sel)
Selection_owner sel;
{
xv_set(frame, FRAME_LEFT_FOOTER, "Lost Selection...", NULL);
}
Selections
Selections 423
18.9 Sample Selection Requestor Program
Example 18-12 shows three panel items that are created to handle the selection rank, the tar-
get(s) request, and initiating the request. Below them is a textsw that displays the response
to the request.
Example 18-12. Sample selection requestor program
sel_req.c
/*
* sel_req.c: Example of how to make requests to a selection owner for
* the selection contents.
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <xview/xview.h>
#include <xview/frame.h>
#include <xview/panel.h>
#include <xview/textsw.h>
#include <xview/font.h>
#include <xview/sel_pkg.h>
Frame frame;
Textsw textsw;
Xv_Server server;
Panel panel;
Panel_item p_selection,
p_target,
p_request;
Selection_requestor sel;
#define TARGETS 1<<0
#define TIMESTAMP 1<<1
#define LENGTH 1<<2
#define STRING 1<<3
#define DELETE 1<<4
#define LINE "--------------------------------------------"
#define ATOM(server, name) (Atom)xv_get(server, SERVER_ATOM, name)
main(argc, argv)
int argc;
char **argv;
{
void MakeRequest(),
SelectionReplyProc(),
RequestChoice();
Xv_Font font;
server = xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
frame = xv_create((Window)NULL, FRAME,
XV_X, 10,
XV_Y, 10,
XV_LABEL, "Selection Requestor Example",
NULL);
424 XView Programming Manual
Example 18-12. Sample selection requestor program
sel_req.c (continued)
panel = xv_create(frame, PANEL,
PANEL_LAYOUT, PANEL_VERTICAL,
NULL);
p_selection = xv_create(panel, PANEL_TEXT,
PANEL_LABEL_STRING, "Selection:",
PANEL_VALUE_DISPLAY_LENGTH, 40,
PANEL_NOTIFY_PROC, MakeRequest,
NULL);
p_target = xv_create(panel, PANEL_TOGGLE,
PANEL_LABEL_STRING, "Request:",
PANEL_NOTIFY_PROC, RequestChoice,
PANEL_CHOICE_STRINGS, "TARGETS",
"TIMESTAMP",
"LENGTH",
"STRING",
"DELETE",
NULL,
NULL);
p_request = xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Make Request",
PANEL_NOTIFY_PROC, MakeRequest,
PANEL_ITEM_X, xv_cols(panel, 20),
NULL);
window_fit(panel);
textsw = xv_create(frame, TEXTSW,
XV_X, 0,
XV_FONT, font,
WIN_BELOW, panel,
NULL);
/* Create a selection requestor object. */
sel = xv_create(panel, SELECTION_REQUESTOR,
SEL_REPLY_PROC, SelectionReplyProc,
NULL);
window_fit(frame);
xv_main_loop(frame);
exit(0);
}
In the requestor application, a PANEL_TEXT item is created where the selection rank can be
entered by the user. Next, a set of nonexclusive toggle buttons are created to allow the user to
choose the target type for the conversion by the selection owner. A “Make Request” button
is created that will send a request to the selection owner every time it is pressed. A TEXTSW is
created to display the output of the request and lastly, a selection-requestor object, sel, is
created.
Selections
Selections 425
Example 18-13 shows the notify procedure associated for the selection requestor example
sel_req.c. It sets and/or appends selection types to the selection requestor based on the
choices selected by the user.
Example 18-13. Sample requestor notify procedure
void
RequestChoice(item, value, event)
Panel_item item;
unsigned int value;
Event *event;
{
int set = False;
/* Build the request based on the toggle items the user has selected. */
if (value & TARGETS) {
if (set)
xv_set(sel, SEL_APPEND_TYPE_NAMES, "TARGETS", NULL, NULL);
else
xv_set(sel, SEL_TYPE_NAME, "TARGETS", NULL);
set = True;
}
if (value & TIMESTAMP) {
if (set)
xv_set(sel, SEL_APPEND_TYPE_NAMES, "TIMESTAMP", NULL, NULL);
else
xv_set(sel, SEL_TYPE_NAME, "TIMESTAMP", NULL);
set = True;
}
if (value & LENGTH) {
if (set)
xv_set(sel, SEL_APPEND_TYPE_NAMES, "LENGTH", NULL, NULL);
else
xv_set(sel, SEL_TYPE_NAME, "LENGTH", NULL);
set = True;
}
if (value & STRING) {
if (set)
xv_set(sel, SEL_APPEND_TYPE_NAMES, "STRING", NULL, NULL);
else
xv_set(sel, SEL_TYPE_NAME, "STRING", NULL);
set = True;
}
if (value & DELETE) {
if (set)
xv_set(sel, SEL_APPEND_TYPE_NAMES, "DELETE", NULL, NULL);
else
xv_set(sel, SEL_TYPE_NAME, "DELETE", NULL);
set = True;
}
}
426 XView Programming Manual
The notify procedure, shown in Example 18-14, is associated with the “Make Request” but-
ton. It initiates the request by doing three things:
Getting the selection rank.
Setting the selection rank.
Posting a selection request.
Example 18-14. Sample make request notify procedure
void
MakeRequest(item, event)
Panel_item item;
Event *event;
{
if (item == p_selection) {
char *rank = NULL;
/* Set the rank of the selection we are */
/* going to make requests to. */
rank = (char *)xv_get(item, PANEL_VALUE);
xv_set(sel, SEL_RANK_NAME, rank,
NULL);
}
else {
/* Post a non-blocking request to the selection owner. */
sel_post_req(sel);
textsw_erase(textsw, 0, TEXTSW_INFINITY);
}
}
18.9.0.1 Sample reply procedure
Example 18-15 shows the reply procedure for sel_req.c.
Example 18-15. Selection reply procedure
void
SelectionReplyProc(sel, target, type, value, length, format)
Selection_requestor sel;
Atom target;
Atom type;
Xv_opaque value;
unsigned long length;
int format;
{
if (length == SEL_ERROR) {
SelectionError(sel, target, *(int *)value);
return;
}
if (target == ATOM(server, "TARGETS")) {
textsw_insert(textsw,
"Holder will convert the following targets:\n", 43);
/* 43 is the size of the string */
Selections
Selections 427

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.