scout%zyzzyva@SUN.COM (David LaVallee) (10/05/88)
What follows is a shar file that contains a NeWS server program (cn.cps)
and a C program (cn.c). The program cn is an example of getting three
types of common input from the window system into a C client.
MENU: C recieves an index token corresponding to the menu choice
CLICK: mouse clicks send C their scaled x,y location
KEYBOARD: <CR> sends C a string that the user was editing
in the NeWS window.
--scout
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
# Makefile
# cn.c
# cn.cps
# This archive created: Fri Sep 23 08:48:45 1988
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'Makefile'
then
echo shar: "will not over-write existing file 'Makefile'"
else
cat << \SHAR_EOF > 'Makefile'
CFILES = cn.c
CPSFILES = cn.cps
LIBS = -I/usr/NeWS/include -L/usr/NeWS/lib -lcps
CFLAGS = -O
cn: $(CFILES) $(CPSFILES)
cps $(CPSFILES)
$(CC) $(CFILES) $(CFLAGS) $(LIBS)
SHAR_EOF
fi
if test -f 'cn.c'
then
echo shar: "will not over-write existing file 'cn.c'"
else
cat << \SHAR_EOF > 'cn.c'
/*
* cn --C to NeWS example program
*
* This program shows input to a C program through a network window
* under NeWS. Three types of input are illustrated: mouse (scaled to
* window co-ordinates), menu (returns int), and keyboard (keyboard
* input is edited and buffered and echoed in the NeWS server. The
* input string is "flushed" to the C client in a single packet
* on a <CR>).
*
* David A. LaVallee Sun Microsystems, Inc.
* cps cn.cps;cc cn.c -o cn -I$NEWSHOME/include -L$NEWSHOME/lib -lcps
* cps cn.cps;cc cn.c -o cn.4 -I$NEWSHOME/include -L$NEWSHOME/lib-sparc -lcps
*/
#include "cn.h"
int mouse_x, mouse_y, menu_index;
char kbstring[300];
main() {
start_window();
window_input_loop();
exit(0);
}
start_window() {
ps_open_PostScript();
ps_initialize();
ps_flush_PostScript();
}
window_input_loop() {
printf("starting input loop\n");
while (!psio_eof(PostScript)) {
if (ps_kbinput(kbstring)) {
printf("%s\n", kbstring);
}
else if (ps_mouse_click(&mouse_x, &mouse_y)) {
printf("Mouse at %d, %d\n", mouse_x, mouse_y);
}
else if (ps_menu_choice(&menu_index)) {
printf("Menu index %d\n", menu_index);
}
else
exit(-1);
};
}
SHAR_EOF
fi
if test -f 'cn.cps'
then
echo shar: "will not over-write existing file 'cn.cps'"
else
cat << \SHAR_EOF > 'cn.cps'
%!
% keyboard example program 2
#define MOUSETAG 100
#define MENUTAG 101
#define KBTAG 102
cdef ps_mouse_click(int mouse_x, mouse_y) => MOUSETAG (mouse_x, mouse_y)
cdef ps_menu_choice(int menu_index) => MENUTAG (menu_index)
cdef ps_kbinput(string kbstring) => KBTAG (kbstring)
cdef ps_initialize()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TEXT STORAGE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/inputtext () def
/textx 40 def
/initx 40 def
/texty 15 def
/inity 15 def
/textfont /Times-Roman findfont 24 scalefont def
/cleartext {
can setcanvas 1 setgray
initx inity moveto inputtext show
/inputtext () def
0 setgray
/textx initx store
/texty inity store
} def
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% KEY HANDLING PROCEEDURES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/addchar {
cvis dup
inputtext exch append
/inputtext exch store
textfont setfont
can setcanvas
textx texty moveto show
currentpoint /texty exch def /textx exch def
textx texty setcursorlocation
} def
/deletechar {
inputtext () ne {
inputtext dup length 1 sub get cvis
inputtext dup length 1 sub 0 exch getinterval
can setcanvas textfont setfont
initx inity moveto dup stringwidth pop 0 rmoveto exch
currentpoint /texty exch store /textx exch store
1 setgray show 0 setgray
/inputtext exch store
textx texty setcursorlocation
} if
} def
/deleteline {
cleartext
textx texty setcursorlocation
} def
/returnkey {
KBTAG tagprint inputtext typedprint cleartext
textx texty setcursorlocation
} def
/handlers 200 dict dup begin
0 1 127 {
dup [ exch /addchar cvx ] cvx def
} for
13 {returnkey} def
/EditBackChar {deletechar} def
/EditBackLine {deleteline} def
end def
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% INITIALIZE A WINDOW
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/repair {
/Symbol findfont 24 scalefont setfont
initx inity moveto
(/) dup stringwidth pop neg 0 rmoveto show
/Times-Roman findfont 24 scalefont setfont
inputtext show
} def
/win framebuffer /new DefaultWindow send def
/reshapefromuser win send
{
/PaintClient {repair} def
/FrameLabel (C example program) def
/ClientMenu [
(first choice) {MENUTAG tagprint 0 typedprint}
(second choice) {MENUTAG tagprint 1 typedprint}
] /new DefaultMenu send def
} win send
/map win send
/can win /ClientCanvas get def
/MouseClickEventMgr [
PointButton {
MOUSETAG tagprint
begin
can setcanvas
XLocation typedprint
YLocation typedprint
end
} /DownTransition can eventmgrinterest
] forkeventmgr def
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% KEYBOARD INPUT LOOP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{
can setcanvas
currentcanvas addkbdinterests pop
currentcanvas addeditkeysinterest pop
{
awaitevent /Name get dup
handlers exch known {
handlers exch get exec
}
{
pop
} ifelse
} loop
} fork
SHAR_EOF
fi
exit 0
# End of shell archive