1 /*
2 // button.c
3 //
4 //
5 */
6
7 #include "xbook.h"
8
9 typedef struct
10 {
11 Display *display;
12 Window w;
13 Window parent;
14 GC gc;
15 unsigned long fore, back;
16 char text[ BUFSIZE + 1 ];
17 int length;
18 int (*function)();
19 } ButtonStruct;
20
21 #define MAX_BUTTONS 20
22
23 ButtonStruct AppButtons[ MAX_BUTTONS + 1 ];
24 int buttons_used = 0;
25
CreateButton(display,window,x,y,fore,back,font_id,text,function)26 CreateButton( display, window, x, y, fore, back, font_id, text, function )
27 Display *display;
28 Window window;
29 int x, y;
30 unsigned long fore, back;
31 Font font_id;
32 char text[];
33 int (*function) ();
34 {
35 Window w;
36 GC gc;
37
38 if( buttons_used < ( MAX_BUTTONS - 1) )
39 {
40 w = CreateWindow( display, window, x, y,
41 BUTTON_WIDTH, BUTTON_HEIGHT, 0,
42 fore, back,
43 ExposureMask | ButtonPressMask );
44 gc = MakeGC( display, w, fore, back );
45 XSetFont( display, gc, font_id );
46 AppButtons[ buttons_used ].display = display;
47 AppButtons[ buttons_used ].w = w;
48 AppButtons[ buttons_used ].parent = window;
49 AppButtons[ buttons_used ].gc = gc;
50 AppButtons[ buttons_used ].fore = fore;
51 AppButtons[ buttons_used ].back = back;
52 AppButtons[ buttons_used ].function = function;
53 if( strlen( text ) < BUFSIZE )
54 {
55 (void) strcpy( AppButtons[ buttons_used ]. text, text );
56 }
57 else
58 {
59 (void) strncpy( AppButtons[ buttons_used ].text, text,
60 BUFSIZE );
61 AppButtons[ buttons_used ].text[ BUFSIZE ] = '\0';
62 }
63 AppButtons[ buttons_used ].length =
64 strlen( AppButtons[ buttons_used ].text );
65 XFlush( display );
66 buttons_used++;
67 return( True );
68 }
69 return( False);
70 }
71
ButtonEvent(display,event)72 ButtonEvent( display, event )
73 Display *display;
74 XEvent *event;
75 {
76 int which_button;
77
78 switch( event->type )
79 {
80 case Expose:
81 which_button = ButtonFind( display,
82 event->xexpose.window );
83 if(which_button >= 0 )
84 {
85 ButtonRedraw( display, which_button );
86 return ( True );
87 }
88 break;
89 case ButtonPress:
90 which_button = ButtonFind( display,
91 event->xbutton.window );
92 if( which_button >= 0 )
93 {
94 ButtonExec( display, which_button );
95 return( True );
96 }
97 break;
98 }
99 XFlush( display );
100 return( False );
101 }
102
ButtonExec(display,which_button)103 ButtonExec( display, which_button )
104 Display *display;
105 int which_button;
106 {
107 ButtonHighlight( display, which_button );
108 (AppButtons[ which_button ].function)(display,
109 AppButtons[ which_button ].parent );
110 XClearWindow( display,
111 AppButtons[ which_button ].w );
112 ButtonRedraw( display, which_button );
113 }
114
ButtonHighlight(display,which_button)115 ButtonHighlight( display, which_button )
116 Display *display;
117 int which_button;
118 {
119 XFillRectangle( display,
120 AppButtons[ which_button ].w,
121 AppButtons[ which_button ].gc,
122 0, 0,
123 BUTTON_WIDTH - 5, BUTTON_HEIGHT - 5 );
124 SetGC( display,
125 AppButtons[ which_button ].gc,
126 AppButtons[ which_button ].back,
127 AppButtons[ which_button ].fore );
128 ButtonText( display,
129 AppButtons[ which_button ].w,
130 AppButtons[ which_button ].gc,
131 AppButtons[ which_button ].text,
132 AppButtons[ which_button ].length );
133 SetGC( display,
134 AppButtons[ which_button ].gc,
135 AppButtons[ which_button ].fore,
136 AppButtons[ which_button ].back );
137 XFlush( display );
138 }
139
ButtonRedraw(display,which_button)140 ButtonRedraw( display, which_button )
141 Display *display;
142 int which_button;
143 {
144 XDrawRectangle( display,
145 AppButtons[ which_button ].w,
146 AppButtons[ which_button ].gc,
147 0, 0,
148 BUTTON_WIDTH - 6, BUTTON_HEIGHT - 6 );
149 ButtonText( display,
150 AppButtons[ which_button ].w,
151 AppButtons[ which_button ].gc,
152 AppButtons[ which_button ].text,
153 AppButtons[ which_button ].length );
154 }
155
ButtonText(display,window,gc,text,length)156 ButtonText( display, window, gc, text, length )
157 Display *display;
158 Window window;
159 GC gc;
160 char text[];
161 int length;
162 {
163 XDrawImageString( display, window, gc,
164 5, 15,
165 text, length );
166 }
167
ButtonFind(display,window)168 ButtonFind( display, window )
169 Display *display;
170 Window window;
171 {
172 int which_button;
173
174 for( which_button = 0; which_button < buttons_used; which_button++ )
175 {
176 if( ( window == AppButtons[ which_button ].w ) &&
177 ( display == AppButtons[ which_button ].display ) )
178 {
179 return( which_button );
180 }
181 }
182 return ( -1 );
183 }
184