Initial import of the CDE 2.1.30 sources from the Open Group.
This commit is contained in:
33
cde/lib/tt/demo/edit_demo/Makefile
Normal file
33
cde/lib/tt/demo/edit_demo/Makefile
Normal file
@@ -0,0 +1,33 @@
|
||||
# $XConsortium: Makefile /main/6 1996/05/13 12:04:04 drk $
|
||||
# %% (c) Copyright 1993, 1994 Hewlett-Packard Company
|
||||
# %% (c) Copyright 1993, 1994 International Business Machines Corp.
|
||||
# %% (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
|
||||
# %% (c) Copyright 1993, 1994 Novell, Inc.
|
||||
|
||||
OPENWINHOME=/usr/openwin
|
||||
MOTIFHOME=/usr/dt
|
||||
|
||||
CFLAGS += -g
|
||||
CPPFLAGS += -I$(MOTIFHOME)/include -I$(OPENWINHOME)/include
|
||||
LDFLAGS += -L$(MOTIFHOME)/lib -L$(OPENWINHOME)/lib
|
||||
LDLIBS += -lXm -lXt -ltt -lX11 -lgen
|
||||
|
||||
.KEEP_STATE:
|
||||
|
||||
Sun_EditDemo: cntl edit edit.types
|
||||
|
||||
edit:
|
||||
LD_RUN_PATH=$(MOTIFHOME)/lib:$(OPENWINHOME)/lib $(LINK.c) -o $@ edit.c $(LDLIBS)
|
||||
cntl:
|
||||
LD_RUN_PATH=$(MOTIFHOME)/lib:$(OPENWINHOME)/lib $(LINK.c) -o $@ cntl.c $(LDLIBS)
|
||||
|
||||
edit.types: edit.types.model
|
||||
tt_type_comp '-DTOOLTALK_DEMO_DIR=\"`pwd`\"' edit.types.model
|
||||
@echo ToolTalk types installed for Sun_EditDemo.
|
||||
@echo Remember to \"kill -USR2\" the ttsession process so the new
|
||||
@echo types will be active.
|
||||
|
||||
clean:
|
||||
rm -f cntl edit core edit.types
|
||||
|
||||
FRC:
|
||||
23
cde/lib/tt/demo/edit_demo/Sun_EditDemo_opnums.h
Normal file
23
cde/lib/tt/demo/edit_demo/Sun_EditDemo_opnums.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
|
||||
/*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
|
||||
/*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
|
||||
/*%% (c) Copyright 1993, 1994 Novell, Inc. */
|
||||
/*%% $XConsortium: Sun_EditDemo_opnums.h /main/3 1995/10/23 09:46:15 rswiston $ */
|
||||
/*
|
||||
*
|
||||
* Sun_EditDemo_opnums.h
|
||||
*
|
||||
* Copyright (c) 1990 by Sun Microsystems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Symbolic definitions for all the opnums used by edit.c. This allows
|
||||
* both the edit.types file and edit.c file to share the same
|
||||
* definitions.
|
||||
*/
|
||||
#define SUN_EDITDEMO_EDIT 0
|
||||
#define SUN_EDITDEMO_SAVE 1
|
||||
#define SUN_EDITDEMO_SAVE_AS 2
|
||||
#define SUN_EDITDEMO_CLOSE 3
|
||||
#define SUN_EDITDEMO_HILITE_OBJ 4
|
||||
|
||||
955
cde/lib/tt/demo/edit_demo/cntl.c
Normal file
955
cde/lib/tt/demo/edit_demo/cntl.c
Normal file
@@ -0,0 +1,955 @@
|
||||
/*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
|
||||
/*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
|
||||
/*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
|
||||
/*%% (c) Copyright 1993, 1994 Novell, Inc. */
|
||||
/*%% $TOG: cntl.c /main/4 1998/05/18 16:55:06 rafi $ */
|
||||
/*
|
||||
* cntl.c
|
||||
*
|
||||
* Copyright (c) 1993 by Sun Microsystems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* An example controller for the remote-control editor defined in edit.c.
|
||||
* Puts up a simple panel with a text field to enter the filename to
|
||||
* operate on and sends out ToolTalk messages to start up an editor to
|
||||
* carry out the operations. Also allows browsing any sub-file specs
|
||||
* defined in the file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <Xm/Xm.h>
|
||||
#include <Xm/MainW.h>
|
||||
#include <Xm/CascadeB.h>
|
||||
#include <Xm/DialogS.h>
|
||||
#include <Xm/Form.h>
|
||||
#include <Xm/Label.h>
|
||||
#include <Xm/List.h>
|
||||
#include <Xm/PushB.h>
|
||||
#include <Xm/Text.h>
|
||||
#include <Xm/Protocols.h>
|
||||
#include <desktop/tt_c.h>
|
||||
|
||||
#define TITLE_LINE_HEIGHT 25
|
||||
|
||||
Display *dpy;
|
||||
XtAppContext app;
|
||||
|
||||
Widget cntl_ui_base_window;
|
||||
Widget cntl_ui_base_controls;
|
||||
Widget cntl_ui_file_button;
|
||||
Widget cntl_ui_file_label;
|
||||
Widget cntl_ui_file_field;
|
||||
Widget cntl_ui_message;
|
||||
Widget cntl_ui_file_menu;
|
||||
|
||||
Widget cntl_ui_saveas_popup;
|
||||
Widget cntl_ui_saveas_controls;
|
||||
Widget cntl_ui_save_as_dlabel;
|
||||
Widget cntl_ui_save_as_directory;
|
||||
Widget cntl_ui_new_flabel;
|
||||
Widget cntl_ui_new_filename;
|
||||
Widget cntl_ui_save_as_button;
|
||||
|
||||
Widget cntl_ui_obj_popup;
|
||||
Widget cntl_ui_obj_controls;
|
||||
Widget cntl_ui_olist_label;
|
||||
Widget cntl_ui_olist;
|
||||
Widget cntl_ui_hilite_button;
|
||||
Widget cntl_ui_obj_field;
|
||||
|
||||
char *cntl_objid = (char *)0;
|
||||
|
||||
Tt_scope msg_scope = TT_FILE_IN_SESSION;
|
||||
|
||||
void
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
void cntl_ui_initialize();
|
||||
|
||||
/*
|
||||
* Initialize Motif.
|
||||
*/
|
||||
XtToolkitInitialize();
|
||||
app = XtCreateApplicationContext();
|
||||
dpy = XtOpenDisplay(app, 0, 0, "cntl", 0, 0, &argc, argv);
|
||||
/*
|
||||
* Initialize user interface components.
|
||||
*/
|
||||
cntl_ui_initialize();
|
||||
|
||||
if (! cntl_init_tt()) {
|
||||
fprintf(stderr,"%s: Can't initialize ToolTalk\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
/*
|
||||
* Turn control over to Motif.
|
||||
*/
|
||||
XtAppMainLoop(app);
|
||||
tt_close();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
is_window_showing(widget)
|
||||
Widget widget;
|
||||
{
|
||||
return(XtIsManaged(widget)) ;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
get_screen_size(widget, width, height)
|
||||
Widget widget;
|
||||
int *width, *height;
|
||||
{
|
||||
Display *dpy = XtDisplay(widget);
|
||||
int screen = DefaultScreen(dpy);
|
||||
|
||||
*width = DisplayWidth(dpy, screen);
|
||||
*height = DisplayHeight(dpy, screen);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
force_popup_on_screen(popup, px, py)
|
||||
Widget popup;
|
||||
int *px, *py;
|
||||
{
|
||||
Dimension popup_width, popup_height;
|
||||
Position left, top;
|
||||
int n, x, y, screen_width, screen_height;
|
||||
|
||||
x = *px;
|
||||
y = *py;
|
||||
|
||||
/* Get the screen size. */
|
||||
|
||||
get_screen_size(popup, &screen_width, &screen_height);
|
||||
|
||||
XtVaGetValues(popup,
|
||||
XmNwidth, &popup_width,
|
||||
XmNheight, &popup_height,
|
||||
0);
|
||||
|
||||
/* Make sure frame does not go off side of screen. */
|
||||
|
||||
n = x + (int) popup_width;
|
||||
if (n > screen_width) x -= (n - screen_width);
|
||||
else if (x < 0) x = 0;
|
||||
|
||||
/* Make sure frame doen't go off top or bottom. */
|
||||
|
||||
n = y + (int) popup_height;
|
||||
if (n > screen_height) y -= n - screen_height;
|
||||
else if (y < 0) y = 0;
|
||||
|
||||
/* Set location and return. */
|
||||
|
||||
left = (Position) x;
|
||||
top = (Position) y;
|
||||
XtVaSetValues(popup,
|
||||
XmNx, left,
|
||||
XmNy, top,
|
||||
0);
|
||||
|
||||
*px = x;
|
||||
*py = y;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
position_popup(base, popup)
|
||||
Widget base, popup;
|
||||
{
|
||||
int bw, bx, by, px, py;
|
||||
int screen_width, screen_height;
|
||||
Position base_x, base_y, popup_x, popup_y;
|
||||
Dimension base_width, base_height, popup_width, popup_height;
|
||||
|
||||
XtVaGetValues(base,
|
||||
XmNx, &base_x,
|
||||
XmNy, &base_y,
|
||||
XmNwidth, &base_width,
|
||||
XmNheight, &base_height,
|
||||
0);
|
||||
bx = (int) base_x;
|
||||
by = (int) base_y;
|
||||
bw = (int) base_width;
|
||||
|
||||
XtVaGetValues(popup,
|
||||
XmNx, &popup_x,
|
||||
XmNy, &popup_y,
|
||||
XmNwidth, &popup_width,
|
||||
XmNheight, &popup_height,
|
||||
0);
|
||||
|
||||
px = (int) popup_x;
|
||||
py = (int) popup_y;
|
||||
|
||||
get_screen_size(popup, &screen_width, &screen_height);
|
||||
px = bx + bw + 5;
|
||||
py = by - TITLE_LINE_HEIGHT;
|
||||
force_popup_on_screen(popup, &px, &py);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
show_popup(widget)
|
||||
Widget widget;
|
||||
{
|
||||
if (!is_window_showing(widget))
|
||||
position_popup(cntl_ui_base_window, widget) ;
|
||||
XtMapWidget(widget);
|
||||
XtManageChild(widget);
|
||||
XtPopup(XtParent(widget), XtGrabNone);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
dismiss_popup(widget, client_data, cbs)
|
||||
Widget widget;
|
||||
XtPointer client_data;
|
||||
XmAnyCallbackStruct *cbs;
|
||||
{
|
||||
XtUnmapWidget(widget);
|
||||
XtUnmanageChild(widget);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
add_delete_callback(widget)
|
||||
Widget widget;
|
||||
{
|
||||
Atom WM_DELETE_WINDOW;
|
||||
|
||||
WM_DELETE_WINDOW = XmInternAtom(XtDisplay(widget),
|
||||
"WM_DELETE_WINDOW", False);
|
||||
XmAddWMProtocolCallback(widget, WM_DELETE_WINDOW,
|
||||
(XtCallbackProc) dismiss_popup, NULL);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
write_footer(message)
|
||||
char *message;
|
||||
{
|
||||
XmString label;
|
||||
|
||||
label = XmStringCreateSimple(message);
|
||||
XtVaSetValues(cntl_ui_message, XmNlabelString, label, 0);
|
||||
XmStringFree(label);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Initialize our ToolTalk environment.
|
||||
*/
|
||||
int
|
||||
cntl_init_tt()
|
||||
{
|
||||
char *procid = tt_open();
|
||||
int ttfd;
|
||||
void cntl_receive_tt_message();
|
||||
Tt_pattern pat;
|
||||
Tt_callback_action cntl_update_obj_panel_callback();
|
||||
|
||||
|
||||
if (tt_pointer_error(procid) != TT_OK) {
|
||||
return 0;
|
||||
}
|
||||
ttfd = tt_fd();
|
||||
|
||||
/* register a dynamic pattern to catch whenever the remote */
|
||||
/* editor creates a sub-file object so that we can update our */
|
||||
/* object query panel. */
|
||||
|
||||
pat = tt_pattern_create();
|
||||
tt_pattern_op_add(pat, "Sun_EditDemo_new_object");
|
||||
tt_pattern_scope_add(pat, msg_scope);
|
||||
tt_pattern_category_set(pat, TT_OBSERVE);
|
||||
tt_pattern_callback_add(pat, cntl_update_obj_panel_callback);
|
||||
if (tt_pattern_register(pat) != TT_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
XtAppAddInput(app, ttfd, (XtPointer) XtInputReadMask,
|
||||
cntl_receive_tt_message, 0);
|
||||
|
||||
tt_session_join(tt_default_session());
|
||||
|
||||
tt_free(procid);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Handle any incoming ToolTalk messages. Since all the messages we send
|
||||
* out have callback functions, no further action is required after doing
|
||||
* a tt_message_receive.
|
||||
*/
|
||||
void
|
||||
cntl_receive_tt_message(client_data, fid, id)
|
||||
XtPointer client_data;
|
||||
int *fid;
|
||||
XtInputId *id;
|
||||
{
|
||||
Tt_message msg;
|
||||
|
||||
msg = tt_message_receive();
|
||||
if (msg != NULL && tt_pointer_error(msg) == TT_ERR_NOMP) {
|
||||
fprintf(stderr,"ToolTalk server down.\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Callback for our dynamic pattern informing us when an object has
|
||||
* been added to a file. We use this information to update the file
|
||||
* objects panel if it is up.
|
||||
*/
|
||||
Tt_callback_action
|
||||
cntl_update_obj_panel_callback(m, p)
|
||||
Tt_message m;
|
||||
Tt_pattern p;
|
||||
{
|
||||
int mark;
|
||||
|
||||
mark = tt_mark();
|
||||
|
||||
if (cntl_is_same_file(tt_message_file(m),
|
||||
XmTextGetString(cntl_ui_file_field))) {
|
||||
|
||||
/* update the objects panel if it's showing */
|
||||
/* objects for the same file as the one in the */
|
||||
/* message. */
|
||||
cntl_update_obj_panel();
|
||||
}
|
||||
|
||||
/*
|
||||
* no further action required for this message. Destroy it
|
||||
* and return TT_CALLBACK_PROCESSED so no other callbacks will
|
||||
* be run for the message.
|
||||
*/
|
||||
tt_message_destroy(m);
|
||||
tt_release(mark);
|
||||
return TT_CALLBACK_PROCESSED;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Default callback for all the ToolTalk messages we send.
|
||||
*/
|
||||
Tt_callback_action
|
||||
cntl_msg_callback(m, p)
|
||||
Tt_message m;
|
||||
Tt_pattern p;
|
||||
{
|
||||
int mark;
|
||||
char msg[255];
|
||||
char *errstr;
|
||||
|
||||
|
||||
mark = tt_mark();
|
||||
switch (tt_message_state(m)) {
|
||||
case TT_STARTED:
|
||||
write_footer("Starting editor...");
|
||||
break;
|
||||
case TT_HANDLED:
|
||||
write_footer("");
|
||||
break;
|
||||
case TT_FAILED:
|
||||
errstr = tt_message_status_string(m);
|
||||
if (tt_pointer_error(errstr) == TT_OK && errstr) {
|
||||
sprintf(msg,"%s failed: %s", tt_message_op(m), errstr);
|
||||
} else if (tt_message_status(m) == TT_ERR_NO_MATCH) {
|
||||
sprintf(msg,"%s failed: Couldn't contact editor",
|
||||
tt_message_op(m),
|
||||
tt_status_message(tt_message_status(m)));
|
||||
} else {
|
||||
sprintf(msg,"%s failed: %s",
|
||||
tt_message_op(m),
|
||||
tt_status_message(tt_message_status(m)));
|
||||
}
|
||||
write_footer(msg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* no further action required for this message. Destroy it
|
||||
* and return TT_CALLBACK_PROCESSED so no other callbacks will
|
||||
* be run for the message.
|
||||
*/
|
||||
tt_message_destroy(m);
|
||||
tt_release(mark);
|
||||
return TT_CALLBACK_PROCESSED;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Handle the edit actions by sending out a message to Sun_EditDemo to
|
||||
* start an editing session.
|
||||
*/
|
||||
void
|
||||
cntl_edit(file)
|
||||
char *file;
|
||||
{
|
||||
Tt_message msg;
|
||||
|
||||
write_footer("");
|
||||
msg = tt_prequest_create(msg_scope, "Sun_EditDemo_edit");
|
||||
tt_message_file_set(msg, file);
|
||||
tt_message_callback_add(msg, cntl_msg_callback);
|
||||
tt_message_send(msg);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Handle the save actions by sending out a message to the Sun_EditDemo
|
||||
* editing this file to save the file.
|
||||
*/
|
||||
void
|
||||
cntl_save(file)
|
||||
char *file;
|
||||
{
|
||||
Tt_message msg;
|
||||
|
||||
write_footer("");
|
||||
msg = tt_prequest_create(msg_scope, "Sun_EditDemo_save");
|
||||
tt_message_file_set(msg, file);
|
||||
tt_message_callback_add(msg, cntl_msg_callback);
|
||||
tt_message_send(msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Shut down the Sun_EditDemo that is editing this file.
|
||||
*/
|
||||
void
|
||||
cntl_close(file)
|
||||
char *file;
|
||||
{
|
||||
Tt_message msg;
|
||||
|
||||
write_footer("");
|
||||
msg = tt_prequest_create(msg_scope, "Sun_EditDemo_close");
|
||||
tt_message_file_set(msg, file);
|
||||
tt_message_callback_add(msg, cntl_msg_callback);
|
||||
tt_message_send(msg);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Instruct the Sun_EditDemo that is editing this file to save the file
|
||||
* under a new name.
|
||||
*/
|
||||
void
|
||||
cntl_save_as(file, dir, newfile)
|
||||
char *file;
|
||||
char *dir;
|
||||
char *newfile;
|
||||
{
|
||||
Tt_message msg;
|
||||
char buf[255];
|
||||
int dirlen;
|
||||
|
||||
msg = tt_prequest_create(msg_scope, "Sun_EditDemo_save_as");
|
||||
|
||||
/* construct new filename out of directory and new file */
|
||||
/* fields. */
|
||||
|
||||
dirlen = strlen(dir);
|
||||
if (dirlen > 0 && dir[dirlen - 1] == '/') {
|
||||
sprintf(buf,"%s%s", dir, newfile);
|
||||
} else {
|
||||
sprintf(buf,"%s/%s", dir, newfile);
|
||||
}
|
||||
|
||||
tt_message_arg_add(msg, TT_IN, "string", buf);
|
||||
tt_message_file_set(msg, file);
|
||||
tt_message_callback_add(msg, cntl_msg_callback);
|
||||
tt_message_send(msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Handler for `file_menu (Edit)'.
|
||||
*/
|
||||
void
|
||||
cntl_ui_edit(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
write_footer("");
|
||||
cntl_edit(XmTextGetString(cntl_ui_file_field));
|
||||
}
|
||||
|
||||
/*
|
||||
* Handler for `file_menu (Save)'.
|
||||
*/
|
||||
void
|
||||
cntl_ui_save(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
write_footer("");
|
||||
cntl_save(XmTextGetString(cntl_ui_file_field));
|
||||
}
|
||||
|
||||
/*
|
||||
* Handler for `file_menu (Save as...)'.
|
||||
*/
|
||||
cntl_ui_save_as(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
write_footer("");
|
||||
show_popup(cntl_ui_saveas_popup);
|
||||
}
|
||||
|
||||
/*
|
||||
* Handler for `file_menu (Close)'.
|
||||
*/
|
||||
cntl_ui_close(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
write_footer("");
|
||||
cntl_close(XmTextGetString(cntl_ui_file_field));
|
||||
}
|
||||
|
||||
/*
|
||||
* Callback function for `save_as_button'.
|
||||
*/
|
||||
void
|
||||
cntl_ui_save_as_button_handler(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
write_footer("");
|
||||
cntl_save_as(XmTextGetString(cntl_ui_file_field),
|
||||
XmTextGetString(cntl_ui_save_as_directory),
|
||||
XmTextGetString(cntl_ui_new_filename));
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to insert the objid given into the scrolling lists of objects
|
||||
* for a file. Used inside tt_file_objects_query as it iterates through
|
||||
* all the ToolTalk objects in a file.
|
||||
*/
|
||||
Tt_filter_action
|
||||
cntl_gather_specs(objid, list_count, acc)
|
||||
char *objid;
|
||||
void *list_count;
|
||||
void *acc;
|
||||
{
|
||||
int *i = (int *)list_count;
|
||||
XmString label;
|
||||
|
||||
label = XmStringCreateSimple(objid);
|
||||
XmListAddItem(cntl_ui_olist, label, *i);
|
||||
XmStringFree(label);
|
||||
|
||||
*i = (*i + 1);
|
||||
|
||||
/* continue processing */
|
||||
return TT_FILTER_CONTINUE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Called to update the scrolling list of objects for a file. Uses
|
||||
* tt_file_objects_query to find all the ToolTalk objects.
|
||||
*/
|
||||
int
|
||||
cntl_update_obj_panel()
|
||||
{
|
||||
static int list_item = 0;
|
||||
char *file;
|
||||
int i;
|
||||
|
||||
cntl_objid = (char *)0;
|
||||
|
||||
for (i = list_item; i >= 0; i--) {
|
||||
XmListDeletePos(cntl_ui_olist, i);
|
||||
}
|
||||
|
||||
list_item = 0;
|
||||
file = XmTextGetString(cntl_ui_file_field);
|
||||
if (tt_file_objects_query(file,
|
||||
(Tt_filter_function)cntl_gather_specs,
|
||||
&list_item, NULL) != TT_OK) {
|
||||
write_footer("Couldn't query objects for file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Callback function for "File objects..." menu item.
|
||||
*/
|
||||
void
|
||||
cntl_ui_file_objects(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
char *string;
|
||||
|
||||
write_footer("");
|
||||
string = XmTextGetString(cntl_ui_file_field);
|
||||
if (cntl_update_obj_panel()) {
|
||||
(void) tt_file_join(XmTextGetString(cntl_ui_file_field));
|
||||
show_popup(cntl_ui_obj_popup);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Notify callback function for `cntl_ui_olist'.
|
||||
*/
|
||||
void
|
||||
cntl_ui_olist_handler(widget, client_data, cbs)
|
||||
Widget widget;
|
||||
XtPointer client_data;
|
||||
XmListCallbackStruct *cbs;
|
||||
{
|
||||
char *string;
|
||||
|
||||
string = XmStringUnparse(cbs->item, NULL, XmCHARSET_TEXT, XmCHARSET_TEXT,
|
||||
NULL, 0, XmOUTPUT_ALL);
|
||||
cntl_objid = strdup(string);
|
||||
XtFree (string);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Notify callback function for `cntl_ui_hilite_button'.
|
||||
*/
|
||||
void
|
||||
cntl_ui_hilite_button_handler(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
Tt_message msg;
|
||||
|
||||
if (cntl_objid == (char *)0) {
|
||||
write_footer("No object id selected");
|
||||
return;
|
||||
}
|
||||
msg = tt_orequest_create(cntl_objid, "hilite_obj");
|
||||
tt_message_arg_add(msg, TT_IN, "string", cntl_objid);
|
||||
tt_message_callback_add(msg, cntl_msg_callback);
|
||||
tt_message_send(msg);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Initialize ui environment.
|
||||
*/
|
||||
void
|
||||
cntl_ui_initialize()
|
||||
{
|
||||
Arg args[20];
|
||||
int n;
|
||||
Widget but;
|
||||
XmString label;
|
||||
void cntl_ui_saveas_popup_initialize();
|
||||
void cntl_ui_obj_popup_initialize();
|
||||
|
||||
cntl_ui_base_window = XtAppCreateShell(0, "base_frame",
|
||||
applicationShellWidgetClass, dpy, 0, 0 );
|
||||
XtVaSetValues(cntl_ui_base_window,
|
||||
XmNwidth, 339,
|
||||
XmNheight, 100,
|
||||
XmNtitle, "Sun_EditDemo_cntl",
|
||||
0);
|
||||
|
||||
cntl_ui_base_controls = XtVaCreateManagedWidget("controls",
|
||||
xmFormWidgetClass, cntl_ui_base_window, 0 );
|
||||
|
||||
n = 0;
|
||||
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNleftOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNrightOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNtopOffset, 5); n++;
|
||||
cntl_ui_file_menu = (Widget) XmCreateMenuBar(cntl_ui_base_controls,
|
||||
"menubar", args, n);
|
||||
|
||||
cntl_ui_file_button = (Widget) XmCreatePulldownMenu(cntl_ui_file_menu,
|
||||
"File", NULL, 0);
|
||||
label = XmStringCreateSimple("File");
|
||||
XtVaCreateManagedWidget("File",
|
||||
xmCascadeButtonWidgetClass, cntl_ui_file_menu,
|
||||
XmNlabelString, label,
|
||||
XmNmnemonic, 'F',
|
||||
XmNsubMenuId, cntl_ui_file_button,
|
||||
0);
|
||||
XmStringFree(label);
|
||||
but = XtVaCreateManagedWidget("Edit",
|
||||
xmPushButtonWidgetClass, cntl_ui_file_button, 0);
|
||||
XtAddCallback(but, XmNactivateCallback,
|
||||
(XtCallbackProc) cntl_ui_edit, 0);
|
||||
but = XtVaCreateManagedWidget("Save",
|
||||
xmPushButtonWidgetClass, cntl_ui_file_button, 0);
|
||||
XtAddCallback(but, XmNactivateCallback,
|
||||
(XtCallbackProc) cntl_ui_save, 0);
|
||||
but = XtVaCreateManagedWidget("Save as...",
|
||||
xmPushButtonWidgetClass, cntl_ui_file_button, 0);
|
||||
XtAddCallback(but, XmNactivateCallback,
|
||||
(XtCallbackProc) cntl_ui_save_as, 0);
|
||||
but = XtVaCreateManagedWidget("File Objects...",
|
||||
xmPushButtonWidgetClass, cntl_ui_file_button, 0);
|
||||
XtAddCallback(but, XmNactivateCallback,
|
||||
(XtCallbackProc) cntl_ui_file_objects, 0);
|
||||
but = XtVaCreateManagedWidget("Close",
|
||||
xmPushButtonWidgetClass, cntl_ui_file_button, 0);
|
||||
XtAddCallback(but, XmNactivateCallback,
|
||||
(XtCallbackProc) cntl_ui_close, 0);
|
||||
XtManageChild(cntl_ui_file_menu);
|
||||
|
||||
cntl_ui_file_label = XtVaCreateManagedWidget("File:",
|
||||
xmLabelWidgetClass, cntl_ui_base_controls,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, cntl_ui_file_menu,
|
||||
XmNtopOffset, 5,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNleftOffset, 5,
|
||||
0);
|
||||
label = XmStringCreateSimple("File:");
|
||||
XtVaSetValues(cntl_ui_file_label, XmNlabelString, label, 0);
|
||||
XmStringFree(label);
|
||||
|
||||
cntl_ui_file_field = XtVaCreateManagedWidget("text",
|
||||
xmTextWidgetClass, cntl_ui_base_controls,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, cntl_ui_file_menu,
|
||||
XmNtopOffset, 5,
|
||||
XmNleftAttachment, XmATTACH_WIDGET,
|
||||
XmNleftWidget, cntl_ui_file_label,
|
||||
XmNleftOffset, 5,
|
||||
XmNrightAttachment, XmATTACH_FORM,
|
||||
XmNrightOffset, 5,
|
||||
0);
|
||||
label = XmStringCreateSimple("");
|
||||
XtVaSetValues(cntl_ui_file_field, XmNlabelString, label, 0);
|
||||
XmStringFree(label);
|
||||
|
||||
cntl_ui_message = XtVaCreateManagedWidget("message",
|
||||
xmLabelWidgetClass, cntl_ui_base_controls,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, cntl_ui_file_field,
|
||||
XmNtopOffset, 10,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNleftOffset, 5,
|
||||
XmNrightAttachment, XmATTACH_FORM,
|
||||
XmNrightOffset, 5,
|
||||
XmNbottomAttachment, XmATTACH_FORM,
|
||||
XmNbottomOffset, 5,
|
||||
0);
|
||||
label = XmStringCreateSimple("");
|
||||
XtVaSetValues(cntl_ui_message, XmNlabelString, label, 0);
|
||||
XmStringFree(label);
|
||||
|
||||
XtRealizeWidget(cntl_ui_base_window);
|
||||
|
||||
cntl_ui_saveas_popup_initialize();
|
||||
cntl_ui_obj_popup_initialize();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
cntl_ui_saveas_popup_initialize()
|
||||
{
|
||||
Arg args[10];
|
||||
int n;
|
||||
XmString label;
|
||||
|
||||
n = 0;
|
||||
XtSetArg(args[n], XmNtitle, "Sun_EditMenu_cntl: Save As"); n++;
|
||||
XtSetArg(args[n], XmNmappedWhenManaged, False); n++;
|
||||
XtSetArg(args[n], XmNdeleteResponse, XmDO_NOTHING); n++;
|
||||
cntl_ui_saveas_popup = XmCreateDialogShell(cntl_ui_base_window,
|
||||
"saveas", args, n);
|
||||
|
||||
cntl_ui_saveas_controls = XtVaCreateManagedWidget("controls",
|
||||
xmFormWidgetClass, cntl_ui_saveas_popup,
|
||||
XmNrubberPositioning, False,
|
||||
XmNresizePolicy, XmRESIZE_ANY,
|
||||
XmNautoUnmanage, False,
|
||||
XmNwidth, 350,
|
||||
XmNheight, 120,
|
||||
0);
|
||||
|
||||
label = XmStringCreateSimple("Directory:");
|
||||
cntl_ui_save_as_dlabel = XtVaCreateManagedWidget("dlabel",
|
||||
xmLabelWidgetClass, cntl_ui_saveas_controls,
|
||||
XmNtopAttachment, XmATTACH_FORM,
|
||||
XmNtopOffset, 5,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNleftOffset, 5,
|
||||
XmNlabelString, label,
|
||||
0);
|
||||
XmStringFree(label);
|
||||
|
||||
cntl_ui_save_as_directory = XtVaCreateManagedWidget("text",
|
||||
xmTextWidgetClass, cntl_ui_saveas_controls,
|
||||
XmNcolumns, 35,
|
||||
XmNtopAttachment, XmATTACH_FORM,
|
||||
XmNtopOffset, 5,
|
||||
XmNleftAttachment, XmATTACH_WIDGET,
|
||||
XmNleftWidget, cntl_ui_save_as_dlabel,
|
||||
XmNleftOffset, 5,
|
||||
XmNrightAttachment, XmATTACH_FORM,
|
||||
XmNrightOffset, 5,
|
||||
0);
|
||||
|
||||
label = XmStringCreateSimple("Filename:");
|
||||
cntl_ui_new_flabel = XtVaCreateManagedWidget("flabel",
|
||||
xmLabelWidgetClass, cntl_ui_saveas_controls,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, cntl_ui_save_as_directory,
|
||||
XmNtopOffset, 5,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNleftOffset, 5,
|
||||
XmNlabelString, label,
|
||||
0);
|
||||
XmStringFree(label);
|
||||
|
||||
label = XmStringCreateSimple("");
|
||||
cntl_ui_new_filename = XtVaCreateManagedWidget("text",
|
||||
xmTextWidgetClass, cntl_ui_saveas_controls,
|
||||
XmNcolumns, 35,
|
||||
XmNlabelString, label,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, cntl_ui_save_as_directory,
|
||||
XmNtopOffset, 5,
|
||||
XmNleftAttachment, XmATTACH_WIDGET,
|
||||
XmNleftWidget, cntl_ui_save_as_dlabel,
|
||||
XmNleftOffset, 5,
|
||||
XmNrightAttachment, XmATTACH_FORM,
|
||||
XmNrightOffset, 5,
|
||||
0);
|
||||
XmStringFree(label);
|
||||
|
||||
label = XmStringCreateSimple("Save");
|
||||
cntl_ui_save_as_button = XtVaCreateManagedWidget("button",
|
||||
xmPushButtonWidgetClass, cntl_ui_saveas_controls,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNleftOffset, 5,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, cntl_ui_new_filename,
|
||||
XmNtopOffset, 5,
|
||||
XmNbottomAttachment, XmATTACH_FORM,
|
||||
XmNbottomOffset, 5,
|
||||
XmNlabelString, label,
|
||||
0);
|
||||
XmStringFree(label);
|
||||
XtAddCallback(cntl_ui_save_as_button,
|
||||
XmNactivateCallback, cntl_ui_save_as_button_handler, 0);
|
||||
XtManageChild(cntl_ui_saveas_controls);
|
||||
add_delete_callback(cntl_ui_saveas_popup);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
cntl_ui_obj_popup_initialize()
|
||||
{
|
||||
int n;
|
||||
Arg args[20];
|
||||
XmString label;
|
||||
|
||||
n = 0;
|
||||
XtSetArg(args[n], XmNtitle, "Sun_EditMenu_cntl: File Objects"); n++;
|
||||
XtSetArg(args[n], XmNmappedWhenManaged, False); n++;
|
||||
XtSetArg(args[n], XmNdeleteResponse, XmDO_NOTHING); n++;
|
||||
cntl_ui_obj_popup = XmCreateDialogShell(cntl_ui_base_window,
|
||||
"object", args, n);
|
||||
|
||||
cntl_ui_obj_controls = XtVaCreateManagedWidget("controls",
|
||||
xmFormWidgetClass, cntl_ui_obj_popup,
|
||||
XmNrubberPositioning, False,
|
||||
XmNresizePolicy, XmRESIZE_ANY,
|
||||
XmNautoUnmanage, False,
|
||||
XmNwidth, 350,
|
||||
XmNheight, 120,
|
||||
0);
|
||||
|
||||
n = 0;
|
||||
label = XmStringCreateSimple("File Objects:");
|
||||
XtSetArg(args[n], XmNlabelString, label); n++;
|
||||
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNleftOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNtopOffset, 5); n++;
|
||||
cntl_ui_olist_label =
|
||||
XmCreateLabel(cntl_ui_obj_controls, "label", args, n);
|
||||
XmStringFree(label);
|
||||
XtManageChild(cntl_ui_olist_label);
|
||||
|
||||
n = 0;
|
||||
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNtopOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNvisibleItemCount, 4); n++;
|
||||
XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
|
||||
XtSetArg(args[n], XmNleftWidget, cntl_ui_olist_label); n++;
|
||||
XtSetArg(args[n], XmNleftOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNrightOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNresizable, True); n++;
|
||||
cntl_ui_olist =
|
||||
XmCreateScrolledList(cntl_ui_obj_controls, "olist", args, n);
|
||||
XtAddCallback(cntl_ui_olist,
|
||||
XmNsingleSelectionCallback, cntl_ui_olist_handler, 0);
|
||||
XtManageChild(cntl_ui_olist);
|
||||
|
||||
label = XmStringCreateSimple("Display");
|
||||
cntl_ui_hilite_button = XtVaCreateManagedWidget("button",
|
||||
xmPushButtonWidgetClass, cntl_ui_obj_controls,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, cntl_ui_olist,
|
||||
XmNtopOffset, 5,
|
||||
XmNbottomAttachment, XmATTACH_FORM,
|
||||
XmNbottomOffset, 5,
|
||||
XmNlabelString, label,
|
||||
0);
|
||||
XmStringFree(label);
|
||||
XtAddCallback(cntl_ui_hilite_button,
|
||||
XmNactivateCallback, cntl_ui_hilite_button_handler, 0);
|
||||
XtManageChild(cntl_ui_obj_controls);
|
||||
add_delete_callback(cntl_ui_obj_popup);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
cntl_is_same_file(f1, f2)
|
||||
char *f1;
|
||||
char *f2;
|
||||
{
|
||||
struct stat f1_stat;
|
||||
struct stat f2_stat;
|
||||
|
||||
if (!strcmp(f1,f2)) {
|
||||
return(1);
|
||||
}
|
||||
|
||||
return((0 == stat(f1, &f1_stat)) &&
|
||||
(0 == stat(f2, &f2_stat)) &&
|
||||
(f1_stat.st_ino == f2_stat.st_ino) &&
|
||||
(f1_stat.st_dev == f2_stat.st_dev));
|
||||
}
|
||||
774
cde/lib/tt/demo/edit_demo/edit.c
Normal file
774
cde/lib/tt/demo/edit_demo/edit.c
Normal file
@@ -0,0 +1,774 @@
|
||||
/*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
|
||||
/*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
|
||||
/*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
|
||||
/*%% (c) Copyright 1993, 1994 Novell, Inc. */
|
||||
/*%% $XConsortium: edit.c /main/3 1995/10/23 09:46:34 rswiston $ */
|
||||
/*
|
||||
* edit.c
|
||||
*
|
||||
* Copyright (c) 1993 by Sun Microsystems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Implementation of a simple "remote-control" editor. Accepts requests
|
||||
* to edit, save, and close files as well as hilite "objects" created in
|
||||
* the file. Objects are kept track of by wrapping them in C-style
|
||||
* comments with their respective object ids.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <Xm/Xm.h>
|
||||
#include <Xm/Label.h>
|
||||
#include <Xm/Form.h>
|
||||
#include <Xm/PushB.h>
|
||||
#include <Xm/Text.h>
|
||||
#include <desktop/tt_c.h>
|
||||
#include "Sun_EditDemo_opnums.h"
|
||||
|
||||
Display *dpy;
|
||||
XtAppContext app;
|
||||
char current_file[MAXPATHLEN];
|
||||
int do_close = 0;
|
||||
int text_modified = -1;
|
||||
|
||||
Widget edit_ui_base_window;
|
||||
Widget edit_ui_textpane;
|
||||
Widget edit_ui_panel;
|
||||
Widget edit_ui_obj_button;
|
||||
Widget edit_ui_message;
|
||||
|
||||
|
||||
void
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
char *dfile;
|
||||
|
||||
/*
|
||||
* Initialize Motif.
|
||||
*/
|
||||
XtToolkitInitialize();
|
||||
app = XtCreateApplicationContext();
|
||||
dpy = XtOpenDisplay(app, 0, 0, "edit", 0, 0, &argc, argv );
|
||||
|
||||
/*
|
||||
* Initialize user interface components.
|
||||
*/
|
||||
edit_ui_initialize();
|
||||
if (! edit_init_tt()) {
|
||||
fprintf(stderr,"%s: Can't initialize ToolTalk\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
/*
|
||||
* Turn control over to Motif.
|
||||
*/
|
||||
while (! do_close) {
|
||||
XEvent event;
|
||||
XtAppNextEvent(app, &event);
|
||||
XtDispatchEvent(&event);
|
||||
}
|
||||
if ((dfile = tt_default_file()) != (char *)0) {
|
||||
tt_file_quit(dfile);
|
||||
}
|
||||
tt_close();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
write_footer(message)
|
||||
char *message;
|
||||
{
|
||||
XmString label;
|
||||
|
||||
label = XmStringCreateSimple(message);
|
||||
XtVaSetValues(edit_ui_message, XmNlabelString, label, 0);
|
||||
XmStringFree(label);
|
||||
}
|
||||
|
||||
|
||||
Tt_status
|
||||
read_file(widget, file)
|
||||
Widget widget;
|
||||
char *file;
|
||||
{
|
||||
char buf[BUFSIZ], *text;
|
||||
struct stat statb;
|
||||
int len;
|
||||
FILE *fp;
|
||||
Tt_status status;
|
||||
|
||||
/*
|
||||
* Make sure the file is a regular text file and open it.
|
||||
*/
|
||||
|
||||
if (stat(file, &statb) == -1 || (statb.st_mode & S_IFMT) != S_IFREG ||
|
||||
!(fp = fopen(file, "r"))) {
|
||||
perror(file);
|
||||
return TT_ERR_FILE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Put the contents of the file in the Text widget by allocating
|
||||
* enough space for the entire file, reading the file into the space,
|
||||
* and using XmTextSetString() to show the file.
|
||||
*/
|
||||
|
||||
strcpy(current_file, file);
|
||||
len = (int) statb.st_size;
|
||||
if (!(text = XtMalloc((unsigned)(len+1)))) {
|
||||
sprintf(buf, "%s: XtMalloc(%ld) failed", file, len);
|
||||
XmTextSetString(widget, buf);
|
||||
} else {
|
||||
if (fread(text, sizeof(char), len, fp) != len) {
|
||||
status = TT_ERR_FILE;
|
||||
} else {
|
||||
status = TT_OK;
|
||||
text[len] = 0;
|
||||
XmTextSetString(widget, text);
|
||||
}
|
||||
}
|
||||
XtFree(text);
|
||||
fclose(fp);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/* Write the contents of a text widget to a file. */
|
||||
|
||||
Tt_status
|
||||
write_file(widget, file)
|
||||
Widget widget;
|
||||
char *file;
|
||||
{
|
||||
char *text;
|
||||
int len;
|
||||
FILE *fp;
|
||||
Tt_status status;
|
||||
|
||||
if (!(fp = fopen(file, "w"))) {
|
||||
perror(file);
|
||||
return TT_ERR_FILE;
|
||||
}
|
||||
|
||||
/* Saving -- get text from the text widget. */
|
||||
|
||||
text = XmTextGetString(widget);
|
||||
len = (int) XmTextGetLastPosition(widget);
|
||||
|
||||
/* Write it to file (check for error). */
|
||||
|
||||
if (fwrite(text, sizeof(char), len, fp) != len) {
|
||||
status = TT_ERR_FILE;
|
||||
} else {
|
||||
status = TT_OK;
|
||||
}
|
||||
fclose(fp);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Initialize our ToolTalk environment.
|
||||
*/
|
||||
int
|
||||
edit_init_tt()
|
||||
{
|
||||
int mark;
|
||||
char *procid = tt_open();
|
||||
int ttfd;
|
||||
void edit_receive_tt_message();
|
||||
|
||||
mark = tt_mark();
|
||||
|
||||
if (tt_pointer_error(procid) != TT_OK) {
|
||||
return 0;
|
||||
}
|
||||
if (tt_ptype_declare("Sun_EditDemo") != TT_OK) {
|
||||
fprintf(stderr,"Sun_EditDemo is not an installed ptype.\n");
|
||||
return 0;
|
||||
}
|
||||
ttfd = tt_fd();
|
||||
XtAppAddInput(app, ttfd, (XtPointer) XtInputReadMask,
|
||||
edit_receive_tt_message, 0);
|
||||
|
||||
tt_session_join(tt_default_session());
|
||||
|
||||
/*
|
||||
* Note that without tt_mark() and tt_release(), the above
|
||||
* combination would leak storage -- tt_default_session() returns
|
||||
* a copy owned by the application, but since we don't assign the
|
||||
* pointer to a variable we could not free it explicitly.
|
||||
*/
|
||||
|
||||
tt_release(mark);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Handle any incoming ToolTalk messages.
|
||||
*/
|
||||
void
|
||||
edit_receive_tt_message(client_data, fid, id)
|
||||
XtPointer client_data;
|
||||
int *fid;
|
||||
XtInputId *id;
|
||||
{
|
||||
Tt_message msg_in;
|
||||
int opnum;
|
||||
int opstatus;
|
||||
char *file;
|
||||
|
||||
msg_in = tt_message_receive();
|
||||
if (msg_in == NULL) return;
|
||||
if (tt_pointer_error(msg_in) == TT_ERR_NOMP) {
|
||||
fprintf(stderr,"ToolTalk server down.\n");
|
||||
exit(0);
|
||||
}
|
||||
switch (tt_message_opnum(msg_in)) {
|
||||
case SUN_EDITDEMO_EDIT:
|
||||
opstatus = edit_edit(msg_in);
|
||||
break;
|
||||
case SUN_EDITDEMO_SAVE:
|
||||
opstatus = edit_save(msg_in);
|
||||
break;
|
||||
case SUN_EDITDEMO_SAVE_AS:
|
||||
opstatus = edit_save(msg_in);
|
||||
break;
|
||||
case SUN_EDITDEMO_CLOSE:
|
||||
opstatus = edit_close(msg_in);
|
||||
break;
|
||||
case SUN_EDITDEMO_HILITE_OBJ:
|
||||
opstatus = edit_hilite_obj(msg_in);
|
||||
break;
|
||||
default:
|
||||
/* don't know what else to do with this message */
|
||||
tt_message_reject(msg_in);
|
||||
break;
|
||||
}
|
||||
if (opstatus == 0) {
|
||||
tt_message_fail(msg_in);
|
||||
} else {
|
||||
tt_message_reply(msg_in);
|
||||
}
|
||||
tt_message_destroy(msg_in);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Handle the "edit" op.
|
||||
*/
|
||||
int
|
||||
edit_edit(msg)
|
||||
Tt_message msg;
|
||||
{
|
||||
int mark = tt_mark();
|
||||
char *file = tt_message_file(msg);
|
||||
char *dfile = tt_default_file();
|
||||
char buf[255];
|
||||
|
||||
if (access(file, R_OK) != 0) {
|
||||
tt_message_status_set(msg, TT_ERR_FILE);
|
||||
tt_message_status_string_set(msg,"Can't open file for read.");
|
||||
tt_release(mark);
|
||||
return 0;
|
||||
} else {
|
||||
if (dfile == (char *)0 || 0 != strcmp(dfile, file)) {
|
||||
/* if not already editing this file, load it */
|
||||
/* in. */
|
||||
tt_default_file_set(file);
|
||||
tt_file_join(file);
|
||||
read_file(edit_ui_textpane, file);
|
||||
sprintf(buf,"Sun_EditDemo_edit: (%s)", file);
|
||||
XtVaSetValues(edit_ui_base_window,
|
||||
XmNtitle, buf, 0);
|
||||
}
|
||||
tt_release(mark);
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Handle the "save" op.
|
||||
*/
|
||||
int
|
||||
edit_save(msg)
|
||||
Tt_message msg;
|
||||
{
|
||||
int mark = tt_mark();
|
||||
char *new_file;
|
||||
|
||||
if (text_modified == 0) {
|
||||
/* no save is needed */
|
||||
tt_release(mark);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (tt_message_opnum(msg) == SUN_EDITDEMO_SAVE) {
|
||||
if (write_file(edit_ui_textpane, current_file) == TT_OK) {
|
||||
text_modified = 0;
|
||||
tt_message_reply(msg);
|
||||
tt_release(mark);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
/* handle SAVE_AS */
|
||||
new_file = tt_message_arg_val(msg, 0);
|
||||
if (write_file(edit_ui_textpane, new_file) == TT_OK) {
|
||||
tt_file_quit(tt_default_file());
|
||||
tt_default_file_set(new_file);
|
||||
tt_file_join(new_file);
|
||||
read_file(edit_ui_textpane, new_file);
|
||||
tt_release(mark);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* couldn't complete operation */
|
||||
tt_message_status_set(msg, TT_ERR_FILE);
|
||||
tt_message_status_string_set(msg, "Couldn't save file");
|
||||
tt_release(mark);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Handle the "close" op.
|
||||
*/
|
||||
int
|
||||
edit_close(msg)
|
||||
Tt_message msg;
|
||||
{
|
||||
Atom wmchs;
|
||||
Window w;
|
||||
XClientMessageEvent xev;
|
||||
|
||||
if (text_modified > 0) {
|
||||
tt_message_status_set(msg, TT_ERR_FILE);
|
||||
tt_message_status_string_set(msg, "File has been modified");
|
||||
return 0;
|
||||
} else {
|
||||
do_close = 1;
|
||||
|
||||
/* Send an event to force the event loop to "click on" and quit the program. */
|
||||
|
||||
w = XtWindow(edit_ui_base_window);
|
||||
wmchs = XInternAtom(dpy, "WM_CHANGE_STATE", True);
|
||||
xev.type = ClientMessage;
|
||||
xev.message_type = wmchs;
|
||||
xev.window = w;
|
||||
xev.format = 32;
|
||||
xev.data.l[0] = IconicState;
|
||||
XSendEvent(dpy, w, True, (SubstructureRedirectMask |
|
||||
SubstructureNotifyMask), &xev) ;
|
||||
XFlush(dpy);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle the "hilite_obj" op.
|
||||
*/
|
||||
int
|
||||
edit_hilite_obj(msg)
|
||||
Tt_message msg;
|
||||
{
|
||||
int mark = tt_mark();
|
||||
char *objid = tt_message_arg_val(msg, 0);
|
||||
char obj_start_text[100];
|
||||
char obj_end_text[100];
|
||||
|
||||
if (tt_message_status(msg) == TT_WRN_START_MESSAGE
|
||||
&& edit_edit(msg) == 0) {
|
||||
/* we were started to hilite an object but couldn't load the */
|
||||
/* file into the textpane. */
|
||||
tt_release(mark);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* expect objects to be wrapped by appropiately formatted */
|
||||
/* C-style comments. */
|
||||
|
||||
sprintf(obj_start_text," /* begin_object(%s) */", objid);
|
||||
sprintf(obj_end_text," /* end_object(%s) */", objid);
|
||||
|
||||
if (select_region(edit_ui_textpane,
|
||||
obj_start_text,
|
||||
obj_end_text) == 1) {
|
||||
tt_release(mark);
|
||||
return 1;
|
||||
} else {
|
||||
tt_message_status_set(msg, TT_ERR_OBJID);
|
||||
tt_message_status_string_set(msg,"Couldn't find object");
|
||||
|
||||
tt_release(mark);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Make a ToolTalk spec out of the selected text in this textpane. Once
|
||||
* the spec is succesfully created and written to a database, wrap the
|
||||
* text with C-style comments in order to delimit the object and send out
|
||||
* a notification that an object has been created in this file.
|
||||
*/
|
||||
void
|
||||
edit_ui_make_object(widget, client_data, call_data)
|
||||
Widget widget;
|
||||
XtPointer client_data, call_data;
|
||||
{
|
||||
int mark = tt_mark();
|
||||
char *objid;
|
||||
char *file;
|
||||
char *sel;
|
||||
XmTextPosition first, last;
|
||||
char obj_start_text[100];
|
||||
char obj_end_text[100];
|
||||
Tt_message msg;
|
||||
|
||||
if (! get_selection(edit_ui_textpane, &sel, &first, &last)) {
|
||||
write_footer("First select some text");
|
||||
tt_release(mark);
|
||||
return;
|
||||
}
|
||||
file = tt_default_file();
|
||||
|
||||
if (file == (char *)0) {
|
||||
write_footer("Not editing any file");
|
||||
tt_release(mark);
|
||||
return;
|
||||
}
|
||||
|
||||
/* create a new spec */
|
||||
|
||||
objid = tt_spec_create(tt_default_file());
|
||||
if (tt_pointer_error(objid) != TT_OK) {
|
||||
write_footer("Couldn't create object");
|
||||
tt_release(mark);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* set its otype */
|
||||
|
||||
tt_spec_type_set(objid, "Sun_EditDemo_object");
|
||||
if (tt_spec_write(objid) != TT_OK) {
|
||||
write_footer("Couldn't write out object");
|
||||
tt_release(mark);
|
||||
return;
|
||||
}
|
||||
|
||||
/* wrap spec's contents (the selected text) with C-style */
|
||||
/* comments. */
|
||||
|
||||
sprintf(obj_start_text," /* begin_object(%s) */", objid);
|
||||
sprintf(obj_end_text," /* end_object(%s) */", objid);
|
||||
(void)wrap_selection(edit_ui_textpane,
|
||||
obj_start_text, obj_end_text);
|
||||
|
||||
/* now send out a notification that we've added a new object */
|
||||
|
||||
msg = tt_pnotice_create(TT_FILE_IN_SESSION,"Sun_EditDemo_new_object");
|
||||
tt_message_file_set(msg, file);
|
||||
tt_message_send(msg);
|
||||
|
||||
tt_release(mark);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Get the current selection. Returns 1 if the selection is in the passed
|
||||
* in textsw.
|
||||
*/
|
||||
int
|
||||
get_selection(widget, selection, first_ptr, last_ptr)
|
||||
Widget widget;
|
||||
char **selection;
|
||||
XmTextPosition *first_ptr, *last_ptr;
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
ptr = XmTextGetSelection(widget);
|
||||
if (ptr == NULL) return 0;
|
||||
*selection = malloc(strlen(ptr) + 1);
|
||||
if (*selection == (char *)0) {
|
||||
return -1;
|
||||
}
|
||||
(void)strcpy(*selection, ptr);
|
||||
XmTextGetSelectionPosition(widget, first_ptr, last_ptr);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
reverse_str(str)
|
||||
char *str;
|
||||
{
|
||||
char c;
|
||||
int i, n;
|
||||
|
||||
n = strlen(str);
|
||||
for (i = 0; i < n/2; i++) {
|
||||
c = str[i];
|
||||
str[i] = str[n-i];
|
||||
str[n-i] = c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
text_find_bytes(widget, first, last, str, reverse)
|
||||
Widget widget;
|
||||
XmTextPosition *first, *last;
|
||||
int reverse;
|
||||
char *str;
|
||||
{
|
||||
char *ptr, *text;
|
||||
int n;
|
||||
|
||||
text = XmTextGetString(widget);
|
||||
if (reverse) {
|
||||
reverse_str(text);
|
||||
reverse_str(str);
|
||||
}
|
||||
ptr = strstr(&text[*first], str);
|
||||
if (ptr == NULL) return -1;
|
||||
n = strlen(str);
|
||||
if (reverse) {
|
||||
*last = (XmTextPosition) ptr;
|
||||
*first = (XmTextPosition) ptr - n;
|
||||
} else {
|
||||
*first = (XmTextPosition) ptr;
|
||||
*last = (XmTextPosition) ptr + n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Selects region between "begin" and "end" in textsw. Returns 1 if
|
||||
* successful.
|
||||
*/
|
||||
int
|
||||
select_region(widget, begin, end)
|
||||
Widget widget;
|
||||
char *begin;
|
||||
char *end;
|
||||
{
|
||||
XmTextPosition bfirst, blast, efirst, elast;
|
||||
XmTextPosition inspoint;
|
||||
int status;
|
||||
|
||||
inspoint = XmTextGetInsertionPosition(widget);
|
||||
/* Find the "begin" text */
|
||||
|
||||
/* first search forward */
|
||||
bfirst = inspoint;
|
||||
status = text_find_bytes(widget, &bfirst, &blast, begin, 0);
|
||||
if (status == -1) {
|
||||
bfirst = inspoint;
|
||||
/* search failed, search backwards */
|
||||
status = text_find_bytes(widget, &bfirst, &blast, begin, 1);
|
||||
if (status == -1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the "end" text */
|
||||
|
||||
efirst = inspoint;
|
||||
/* first search forward */
|
||||
status = text_find_bytes(widget, &efirst, &elast, end, 0);
|
||||
if (status == -1) {
|
||||
efirst = inspoint;
|
||||
/* search failed, search backwards */
|
||||
status = text_find_bytes(widget, &efirst, &elast, end, 1);
|
||||
if (status == -1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
XmTextSetSelection(widget,
|
||||
(inspoint = (blast < elast) ? blast : elast),
|
||||
(efirst > bfirst) ? efirst : bfirst,
|
||||
CurrentTime);
|
||||
|
||||
XmTextSetInsertionPosition(widget, inspoint);
|
||||
XmTextShowPosition(widget, inspoint);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Wraps the selected text in textsw with the begin and end strings
|
||||
* supplied. Returns 1 if successful.
|
||||
*/
|
||||
int
|
||||
wrap_selection(widget, begin, end)
|
||||
Widget widget;
|
||||
char *begin;
|
||||
char *end;
|
||||
{
|
||||
char *buf;
|
||||
char *sel;
|
||||
XmTextPosition sel_first, sel_last;
|
||||
int sel_status;
|
||||
|
||||
if (! (sel_status = get_selection(widget,
|
||||
&sel, &sel_first, &sel_last))) {
|
||||
return sel_status;
|
||||
}
|
||||
|
||||
buf = malloc(strlen(sel) + strlen(begin) + strlen(end) + 3);
|
||||
if (buf == (char *)0) {
|
||||
return -1;
|
||||
}
|
||||
sprintf(buf,"%s\n%s\n%s", begin, sel, end);
|
||||
XmTextReplace(widget, sel_first, sel_last + 1, buf);
|
||||
free(buf);
|
||||
free(sel);
|
||||
|
||||
return sel_status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Increment a flag every time this routine is called. Note that it's
|
||||
* called once, when the text is initially loaded, so the flag is initially
|
||||
* -1, to counter this.
|
||||
*/
|
||||
text_modify(widget, client_data, cbs)
|
||||
Widget widget;
|
||||
XtPointer client_data;
|
||||
XmTextVerifyCallbackStruct *cbs;
|
||||
{
|
||||
text_modified++;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Initialize our ui environment.
|
||||
*/
|
||||
edit_ui_initialize()
|
||||
{
|
||||
Arg args[20];
|
||||
Pixmap icon;
|
||||
XmString label;
|
||||
int n, screen;
|
||||
|
||||
static unsigned short icon_bits[] = {
|
||||
0x3FFF,0xFF00,
|
||||
0x2000,0x0180,
|
||||
0x2000,0x0140,
|
||||
0x2000,0x0120,
|
||||
0x2000,0x0110,
|
||||
0x2000,0x0108,
|
||||
0x2000,0x0104,
|
||||
0x2000,0x01FE,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x2000,0x0006,
|
||||
0x3FFF,0xFFFE,
|
||||
0x1FFF,0xFFFE
|
||||
};
|
||||
|
||||
edit_ui_base_window = XtAppCreateShell(0, "base_frame",
|
||||
applicationShellWidgetClass, dpy, 0, 0 );
|
||||
screen = DefaultScreen(dpy);
|
||||
icon = XCreatePixmapFromBitmapData(dpy, RootWindow(dpy, screen),
|
||||
(char *) icon_bits, 32, 32, 1, 0, 1);
|
||||
XtVaSetValues(edit_ui_base_window,
|
||||
XmNwidth, 509,
|
||||
XmNheight, 420,
|
||||
XmNtitle, "Sun_EditDemo_edit",
|
||||
XmNiconName, "Sun_EditDemo_edit",
|
||||
XmNiconPixmap, icon,
|
||||
0);
|
||||
|
||||
edit_ui_panel = XtVaCreateManagedWidget("panel",
|
||||
xmFormWidgetClass, edit_ui_base_window, 0);
|
||||
|
||||
label = XmStringCreateSimple("Make object");
|
||||
edit_ui_obj_button = XtVaCreateManagedWidget("button",
|
||||
xmPushButtonWidgetClass, edit_ui_panel,
|
||||
XmNtopAttachment, XmATTACH_FORM,
|
||||
XmNtopOffset, 5,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNleftOffset, 5,
|
||||
XmNlabelString, label,
|
||||
0);
|
||||
XmStringFree(label);
|
||||
XtAddCallback(edit_ui_obj_button,
|
||||
XmNactivateCallback, edit_ui_make_object, 0);
|
||||
|
||||
XtManageChild(edit_ui_panel);
|
||||
|
||||
n = 0;
|
||||
XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
|
||||
XtSetArg(args[n], XmNresizable, True); n++;
|
||||
XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
|
||||
XtSetArg(args[n], XmNtopWidget, edit_ui_obj_button); n++;
|
||||
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(args[n], XmNleftOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNrightOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNtopOffset, 5); n++;
|
||||
XtSetArg(args[n], XmNrows, 24); n++;
|
||||
XtSetArg(args[n], XmNcolumns, 80); n++;
|
||||
edit_ui_textpane = XmCreateScrolledText(edit_ui_panel, "textpane",
|
||||
args, n);
|
||||
|
||||
XtAddCallback(edit_ui_textpane, XmNmodifyVerifyCallback,
|
||||
text_modify, 0);
|
||||
edit_ui_message = XtVaCreateManagedWidget("message",
|
||||
xmLabelWidgetClass, edit_ui_panel,
|
||||
XmNtopAttachment, XmATTACH_WIDGET,
|
||||
XmNtopWidget, edit_ui_textpane,
|
||||
XmNtopOffset, 10,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNleftOffset, 5,
|
||||
XmNrightAttachment, XmATTACH_FORM,
|
||||
XmNrightOffset, 5,
|
||||
XmNbottomAttachment, XmATTACH_FORM,
|
||||
XmNbottomOffset, 5,
|
||||
0);
|
||||
label = XmStringCreateSimple("");
|
||||
XtVaSetValues(edit_ui_message, XmNlabelString, label, 0);
|
||||
XmStringFree(label);
|
||||
|
||||
XtManageChild(edit_ui_textpane);
|
||||
XtRealizeWidget(edit_ui_base_window);
|
||||
}
|
||||
39
cde/lib/tt/demo/edit_demo/edit.types.model
Normal file
39
cde/lib/tt/demo/edit_demo/edit.types.model
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
*
|
||||
* edit.types
|
||||
*
|
||||
* Copyright (c) 1990 by Sun Microsystems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This files shows the ptype and otype definitions used by edit.c
|
||||
*/
|
||||
#include "Sun_EditDemo_opnums.h"
|
||||
|
||||
ptype Sun_EditDemo {
|
||||
start TOOLTALK_DEMO_DIR "/edit";
|
||||
handle:
|
||||
/* edit file named in message, start editor if necessary */
|
||||
file_in_session Sun_EditDemo_edit(void)
|
||||
=> start opnum=SUN_EDITDEMO_EDIT;
|
||||
|
||||
/* tell editor viewing file in message to save file */
|
||||
file_in_session Sun_EditDemo_save(void)
|
||||
=> opnum=SUN_EDITDEMO_SAVE;
|
||||
|
||||
/* save file named in message to new filename */
|
||||
file_in_session Sun_EditDemo_save_as(in string new_filename)
|
||||
=> opnum=SUN_EDITDEMO_SAVE_AS;
|
||||
|
||||
/* bring down editor viewing file in message */
|
||||
file_in_session Sun_EditDemo_close(void)
|
||||
=> opnum=SUN_EDITDEMO_CLOSE;
|
||||
};
|
||||
|
||||
|
||||
otype Sun_EditDemo_object {
|
||||
handle:
|
||||
/* hilite object given by objid, starts an editor if necessary */
|
||||
hilite_obj(in string objid)
|
||||
=> Sun_EditDemo file_in_session start opnum=SUN_EDITDEMO_HILITE_OBJ;
|
||||
};
|
||||
1
cde/lib/tt/demo/edit_demo/edit.types.model.deps
Normal file
1
cde/lib/tt/demo/edit_demo/edit.types.model.deps
Normal file
@@ -0,0 +1 @@
|
||||
: ./Sun_EditDemo_opnums.h
|
||||
Reference in New Issue
Block a user