1 /*
2  * wmslib/include/but/but.h, part of wmslib (Library functions)
3  * Copyright (C) 1994-1995 William Shubert.
4  * See "configure.h.in" for more copyright information.
5  *
6  * Includes for auxiliary button code.
7  */
8 
9 #ifndef  _BUT_BUT_H_
10 #define  _BUT_BUT_H_  1
11 
12 #include <wms.h>
13 #include <wms/snd.h>
14 #if  HAVE_SYS_SELECT_H
15 #include <sys/select.h>
16 #endif
17 
18 /**********************************************************************
19  * Forward declarations
20  **********************************************************************/
21 typedef struct ButTimer_struct ButTimer;
22 typedef struct ButWin_struct ButWin;
23 typedef struct ButLnet_struct ButLnet;
24 typedef struct ButRnet_struct ButRnet;
25 typedef struct ButEnv_struct  ButEnv;
26 typedef struct But_struct  But;
27 
28 #include <but/rcur.h>
29 
30 /**********************************************************************
31  * Constants
32  **********************************************************************/
33 #define  BUT_DCLICK  350  /* Time between double clicks; should probably */
34                           /*   be settable by the user.                  */
35 
36 /**********************************************************************
37  * Types
38  **********************************************************************/
39 #define  BUTOUT_STOPWAIT   0x01
40 #define  BUTOUT_ERR        0x02
41 #define  BUTOUT_CAUGHT     0x04
42 #define  BUTOUT_STILLBUTS  0x08
43 #define  BUTOUT_IGNORE     0x10  /* Used by canvases. */
44 typedef uint  ButOut;
45 typedef ButOut  ButWinFunc(struct ButWin_struct *win);
46 
47 
48 typedef struct ButAction_struct  {
49   ButOut  (*mMove)(But *but, int x, int y);
50   ButOut  (*mLeave)(But *but);
51   ButOut  (*mPress)(But *but, int butnum, int x, int y);
52   ButOut  (*mRelease)(But *but, int butnum, int x, int y);
53   ButOut  (*kPress)(But *but, const char *str, KeySym sym);
54   ButOut  (*kRelease)(But *but, const char *str, KeySym sym);
55   void    (*draw)(But *but, int x, int y, int w, int h);
56   ButOut  (*destroy)(But *but);
57   void    (*newFlags)(But *but, uint nfl);
58   ButOut  (*netMessage)(But *but, void *msg, int msgLen);
59   /* resize returns TRUE if redraws are needed for old & new position. */
60   bool    (*resize)(But *but, int oldX, int oldY, int oldW, int oldH);
61 } ButAction;
62 
63 typedef struct ButColor_struct {
64   int  red, green, blue, greyLevel;
65 } ButColor;
66 
67 typedef struct ButSet_struct  {
68   int  numButs;
69   bool  dynamic;  /* If TRUE, then buts should be freed when you're done. */
70   But  **buts;
71   int  maxButs;
72   MAGIC_STRUCT
73 } ButSet;
74 
75 
76 typedef struct ButWinArea_struct  {
77   int x, y, w, h;
78 } ButWinArea;
79 
80 
81 struct ButWin_struct  {
82   struct ButWin_struct  *parent;  /* NULL unless you're a canvas. */
83   struct But_struct  *parentBut;  /* NULL unless you're a canvas. */
84   void  *packet, *iPacket;
85   Window  win, physWin;
86   struct ButWin_struct  *iconWin;
87 
88   const char *name;
89   bool  iconic, isIcon;
90   int  x,y,w,h, xOff, yOff, minWRatio, minHRatio, maxWRatio, maxHRatio;
91   int  minW, minH, maxW, maxH, wStep, hStep;
92   int  logicalW, logicalH;  /* Same as w&h except for canvases. */
93 
94   bool  resized, resizeNeeded, redrawReady;
95   ButWinArea  *redraws;
96   int  numRedraws, maxRedraws;
97 
98   int  id;
99   bool  mapped;
100   ButWinFunc  *unmap, *map, *resize, *destroy, *quit;
101   ButEnv  *env;
102   int  minLayer, maxLayer;
103   struct ButSet_struct  butsNoDraw;
104   But  *lock, *butIn;
105   But *keyBut;
106   int  numCols, maxCols;
107   struct ButCol_struct  *cols;
108   MAGIC_STRUCT
109 };
110 
111 typedef struct ButWrite_struct  {
112   double  width;
113   void  *packet;
114   void (*draw)(void *packet, ButWin *win, int x, int y, int w, int h);
115 } ButWrite;
116 
117 #define  BUTWRITE_MINPRINT  '\t'
118 #define  BUTWRITE_MAXCHARS  (BUTWRITE_MINPRINT*256-256)
119 
120 typedef struct ButFdCallback_struct  {
121   ButOut  (*callback)(void *packet, int fd);
122   void  *packet;
123 } ButFdCallback;
124 
125 struct  ButEnv_struct  {
126   char  *protocol;
127   void  *packet;
128   Display  *dpy;
129   int  (*shutdown)(Display *dpy);
130   GC  gc, gc2;
131   Pixmap  stipDisable;
132   bool  colorp;  /* TRUE if color, FALSE if b/w. */
133   int  numColors;
134   ulong  *colors;
135   Pixmap greyMaps[17];
136   Pixmap *colorPmaps;
137   int  numFonts;
138   XFontStruct **fonts;
139   int  font0h, depth, rootW, rootH;
140   int  stdButBw;
141   Atom  prop;
142   bool  (*sReq)(ButEnv *env, XSelectionRequestEvent *xsre);
143   int  (*sClear)(ButEnv *env);
144   int  (*sNotify)(ButEnv *env, XSelectionEvent *xsnot);
145   struct ButWin_struct  **winlist;
146   int  minWindows, wllen, wlmax;
147   But  *butIn, *lockBut;
148   struct ButWin_struct  *last_mwin;
149   int  last_mx, last_my;
150   ButEnv  *next;
151   struct ButWrite_struct  write[BUTWRITE_MAXCHARS];
152   uint  keyModifiers;
153   Time  eventTime;  /* But & kbd PRESS or RELEASE or MOVE events. */
154   int  eventNum;   /* But & kbd PRESS events ONLY. */
155 
156   int  maxFd, maxGFds[3];
157   fd_set  fMasks[3];
158   ButFdCallback  *fCallbacks[3];
159 
160   /* These are set up in the rc (remote cusor) file. */
161   int  cur_mnum, new_mnum;
162   Cursor  cursors[BUTCUR_NUM];
163   Pixmap  cpic[BUTCUR_NUM], cmask[BUTCUR_NUM], *cstore;
164   struct ButRnet_struct  **partners;
165 
166   int  partner, numPartners;
167   int  maxButIds, maxWinIds;
168   But  **id2But;
169   struct ButWin_struct  **id2Win;
170 
171   ButCur  curnum, curlast;
172   Window  curwin;
173   But  *curhold;
174   MAGIC_STRUCT
175 };
176 
177 
178 typedef struct ButKey_struct  {
179   KeySym key;
180   uint  modifiers;
181   uint  modMask;
182 } ButKey;
183 
184 
185 struct But_struct  {
186   void  *uPacket;  /* Packet of user information.        */
187   void  *iPacket;  /* Packet unique to this button type. */
188   struct ButWin_struct  *win;
189   int  layer;
190   int  x, y, w, h, id;
191   unsigned  flags;
192   const ButKey  *keys;  /* A list of keys that map to this button. */
193   const ButAction *action;
194   void  (*destroyCallback)(struct But_struct *but);
195   MAGIC_STRUCT
196 };
197 
198 typedef struct ButRow_struct {
199   int  startY, numButs, maxButs;
200   But  **buts;
201   MAGIC_STRUCT
202 } ButRow;
203 
204 typedef struct ButCol_struct {
205   int  startX;
206   int  numRows, maxRows;
207   struct ButRow_struct  *rows;
208   MAGIC_STRUCT
209 } ButCol;
210 
211 
212 #define  butEnv_setXFg(env, colornum)                            \
213   do  {                                                          \
214     if (env->colorp)                                             \
215       XSetForeground(env->dpy, env->gc, env->colors[colornum]);  \
216     else                                                         \
217       XSetTile(env->dpy, env->gc, env->colorPmaps[colornum]);    \
218   } while (0)
219 
220 #define  butEnv_stdFill(env)                        \
221   do  {                                             \
222     if (env->colorp)                                \
223       XSetFillStyle(env->dpy, env->gc, FillSolid);  \
224     else                                            \
225       XSetFillStyle(env->dpy, env->gc, FillTiled);  \
226   } while (0)
227 
228 #define  butEnv_setXFg2(env, colornum)                            \
229   do  {                                                           \
230     if (env->colorp)                                              \
231       XSetForeground(env->dpy, env->gc2, env->colors[colornum]);  \
232     else                                                          \
233       XSetTile(env->dpy, env->gc2, env->colorPmaps[colornum]);    \
234   } while (0)
235 
236 #define  butEnv_stdFill2(env)                        \
237   do  {                                              \
238     if (env->colorp)                                 \
239       XSetFillStyle(env->dpy, env->gc2, FillSolid);  \
240     else                                             \
241       XSetFillStyle(env->dpy, env->gc2, FillTiled);  \
242   } while (0)
243 
244 extern Atom  but_myProp, but_wmDeleteWindow, but_wmProtocols;
245 extern struct ButTimer_struct  *but_timerList;
246 extern bool  but_inEvent;
247 
248 
249 extern void  butWin_turnOnTimers(ButWin *win);
250 extern void  butWin_turnOffTimers(ButWin *win);
251 
252 extern ButOut  butWin_delete(ButWin *win);
253 extern ButOut  but_dList(But *but);
254 extern ButOut  butWin_dList(ButWin *win);
255 
256 /* From "write.c" */
257 extern void  butWin_write(ButWin *win, int x, int y, const char *text,
258 			  int font);
259 
260 /* Functions available to the buttons. */
261 extern void  butEnv_deinit(ButEnv *env);
262 extern But   *but_create(ButWin *win, void *iPacket,
263 			 const ButAction *action);
264 extern void  but_init(But *but);
265 extern ButOut   but_delete(But *but);
266 #define  but_getFlags(b)  ((b)->flags)
267 extern void  but_newFlags(But *but, uint newflags);
268 extern void  but_flags(But *but, uint newflags);  /* Private. */
269 extern void  but_draw(But *but);
270 extern void  butWin_redraw(ButWin *win, int x, int y, int w, int h);
271 extern void  butWin_performDraws(ButWin *win);
272 extern void  but_resize(But *but, int x, int y, int w, int h);
273 extern ButOut  butWin_mMove(ButWin *win, int x, int y);
274 extern ButOut  butWin_mPress(ButWin *win, int x, int y, int butnum);
275 extern ButOut  butWin_mRelease(ButWin *win, int x, int y, int butnum);
276 extern ButOut  butWin_kPress(ButWin *win, const char *kstring, KeySym sym);
277 extern ButOut  butWin_kRelease(ButWin *env, const char *kstring, KeySym sym);
278 
279 /* Functions for internal use only. */
280 /* In but_finder.c */
281 void  butSet_destroy(ButSet *butset);
282 extern ButWin  *butEnv_findWin(ButEnv *env, Window win);
283 extern void  butWin_addToTable(ButWin *win);
284 extern void  butWin_rmFromTable(ButWin *win);
285 extern void  but_addToTable(But *but);
286 extern void  but_delFromTable(But *but);
287 extern void  butWin_findButSet(ButWin *win, int x, int y, ButSet *butset);
288 extern void  butWin_findButSetInRegion(ButWin *win, int x,int y,
289 				       int w,int h, ButSet *butset);
290 extern But  *butWin_findButsInRegion(ButWin *win, int x,int y, int w,int h,
291 				     bool (*action)(But *but, void *packet),
292 				     void *packet);
293 void  butSet_addBut(ButSet *butset, But *but);
294 void  butSet_delBut(ButSet *butset, But *but);
295 
296 
297 /* From "ctext.c" */
298 extern Snd  but_downSnd, but_upSnd;
299 
300 
301 /**********************************************************************
302  * Forward declarations
303  **********************************************************************/
304 
305 /* These are all bits for the flags arguments to button functions. */
306 #define  BUT_DRAWABLE    0x0001  /* Can be drawn. */
307 #define  BUT_PRESSABLE   0x0002  /* Can be pressed. */
308 #define  BUT_TWITCHABLE  0x0004  /* Changes when mouse goes over it. */
309 #define  BUT_KEYED       0x0008  /* Keys pressed land in this button. */
310 #define  BUT_PRESSTHRU   0x0010
311 
312 #define  BUT_PRESSED     0x0020
313 #define  BUT_TWITCHED    0x0040
314 #define  BUT_KEYPRESSED  0x0080
315 #define  BUT_KEYACTIVE   0x0100  /* Read only; set if recvs keypresses. */
316 #define  BUT_TABLED      0x0200  /* Private.  Don't touch. */
317 #define  BUT_LOCKED      0x0400
318 #define  BUT_OPAQUE      0x0800  /* Solidly fills rectangle. */
319 #define  BUT_DEAD        0x1000  /* Marked for death! */
320 #define  BUT_NETPRESS    0x2000
321 #define  BUT_NETTWITCH   0x4000
322 #define  BUT_NETKEY      0x8000
323 #define  BUT_MAXBITS  16
324 #define  BUT_NODRAW     (BUT_DRAWABLE<<BUT_MAXBITS)    /* Write only. */
325 #define  BUT_NOPRESS    (BUT_PRESSABLE<<BUT_MAXBITS)   /* Write only. */
326 #define  BUT_NOTWITCH   (BUT_TWITCHABLE<<BUT_MAXBITS)  /* Write only. */
327 #define  BUT_NOKEY      (BUT_KEYED<<BUT_MAXBITS)       /* Write only. */
328 #define  BUT_NOTHRU     (BUT_PRESSTHRU<<BUT_MAXBITS)   /* Write only. */
329 #define  BUT_NETMASK    0x00e0
330 #define  BUT_NETSHIFT   8
331 
332 #define  BUT_FG       0   /* The standard foreground for buttons. */
333 #define  BUT_BG       1   /* The standard background for buttons. */
334 #define  BUT_PBG      2   /* Background for pressed buttons. */
335 #define  BUT_HIBG     3   /* Highlighted buttons backgrounds. */
336 #define  BUT_LIT      4   /* "Lit up" edges of buttons. */
337 #define  BUT_SHAD     5   /* "Shadowed" edges of buttons. */
338 #define  BUT_ENTERBG  6   /* bg for text the user is typing. */
339 #define  BUT_SELBG    7   /* bg for selected text. */
340 #define  BUT_CHOICE   8   /* bg for chosen things. */
341 #define  BUT_WHITE    9   /* These are always white and black. */
342 #define  BUT_BLACK    10
343 
344 #define  BUT_DCOLORS  11  /* The number of default colors. */
345 
346 #define  butEnv_packet(env)  ((env)->packet)
347 #define  butEnv_dpy(env)     ((env)->dpy)
348 #define  butEnv_gc(env)      ((env)->gc)
349 #define  butEnv_color(env, cnum)   ((env)->colors[cnum])
350 #define  butEnv_isColor(env) ((env)->colorp)
351 #define  butEnv_pixmap(env, cnum)  ((env)->colorPmaps[cnum])
352 #define  butEnv_fontStr(env, fnum) ((env)->fonts[fnum])
353 #define  butEnv_stdBw(env)   ((env)->stdButBw)
354 #define  butEnv_keyModifiers(env)  ((env)->keyModifiers)
355 
356 #define  butWin_packet(win)  ((win)->packet)
357 #define  butWin_env(win)     ((win)->env)
358 #define  butWin_xwin(bwin)   ((bwin)->win)
359 #define  butWin_w(win)       ((win)->logicalW)
360 #define  butWin_h(win)       ((win)->logicalH)
361 #define  butWin_viewW(win)   ((win)->w)
362 #define  butWin_viewH(win)   ((win)->h)
363 #define  butWin_x(win)       ((win)->x)
364 #define  butWin_y(win)       ((win)->y)
365 #define  butWin_xoff(win)    ((win)->xOff)
366 #define  butWin_yoff(win)    ((win)->yOff)
367 #define  butWin_setPacket(win, p)  ((win)->packet = (p))
368 #define  butWin_setQuit(win, q)  ((win)->quit = (q))
369 #define  butWin_setDestroy(win, d)  ((win)->destroy = (d))
370 
371 #define  but_win(but)         ((but)->win)
372 #define  but_packet(but)      ((but)->uPacket)
373 #define  but_setPacket(but, val)  ((but)->uPacket = (val))
374 #define  but_x(but)           ((but)->x)
375 #define  but_y(but)           ((but)->y)
376 #define  but_w(but)           ((but)->w)
377 #define  but_h(but)           ((but)->h)
378 #define  but_setDestroyCallback(but, v)  ((but)->destroyCallback = (v))
379 
380 
381 /*
382  * but_events keeps servicing events until one returns BUT_OUT_STOPWAIT.
383  */
384 extern void  butEnv_events(ButEnv *env);
385 extern ButEnv  *butEnv_create(const char *protocol, const char *dpyname,
386 			      int shutdown(Display *dpy));
387 extern ButEnv  *butEnv_createNoDpy(const char *protocol);
388 /*
389  * but_env_init() returns:
390  *   0 - Black and white display or color=FALSE.
391  *   1 - Couldn't allocate standard colors.  Failed.
392  *   2 - Color successful.
393  *   3 - Truecolor display.  Color will always be successful.
394  */
395 extern int  butEnv_init(ButEnv *env, void *packet, const char *atomname,
396 			bool color);
397 extern void  butEnv_destroy(ButEnv *env);
398 extern bool  butEnv_setFont(ButEnv *env, int fontnum, const char *fontname,
399 			    int fparam);
400 #define  butEnv_fontH(env, fnum)  \
401           ((env)->fonts[fnum]->ascent + (env)->fonts[fnum]->descent)
402 /* drawall is typically used after changing fonts, etc. */
403 extern void  butEnv_drawAll(ButEnv *env);
404 extern void  butEnv_resizeAll(ButEnv *env);
405 extern void  but_draw(But *but);
406 extern void  but_destroy(But *but);
407 extern void  but_resize(But *but, int x, int y, int w, int h);
408 
409 #define  butEnv_localContext(env)  ((env)->localContext)
410 #define  butEnv_context(env)       ((env)->context)
411 extern void  but_setId(But *but, int id);
412 extern void  butWin_setId(ButWin *win, int id);
413 
414 #define  BUT_READFILE   0
415 #define  BUT_WRITEFILE  1
416 #define  BUT_XFILE      2
417 extern void  butEnv_addFile(ButEnv *env, int group, int fd, void *packet,
418 			    ButOut (*callback)(void *packet, int fd));
419 extern void  butEnv_rmFile(ButEnv *env, int group, int fd);
420 
421 extern ButWin  *butWin_iCreate(void *packet, ButEnv *env,
422 			       const char *name, int w, int h,
423 			       ButWin **iWin, bool iconic, int iW, int iH,
424 			       ButWinFunc *unmap,
425 			       ButWinFunc *map,
426 			       ButWinFunc *resize,
427 			       ButWinFunc *iResize,
428 			       ButWinFunc *destroy);
429 #define  butWin_create(p,e,n,w,h,un,ma,re,de)  \
430   butWin_iCreate(p,e,n,w,h,NULL,FALSE,0,0,un,ma,re,NULL,de)
431 extern void  butWin_setGeom(ButWin *win, const char *geometry);
432 extern void  butWin_setX(ButWin *win, int x);
433 extern void  butWin_setY(ButWin *win, int y);
434 extern void  butWin_setMinW(ButWin *win, int minW);
435 extern void  butWin_setMinH(ButWin *win, int minH);
436 extern void  butWin_setMaxW(ButWin *win, int maxW);
437 extern void  butWin_setMaxH(ButWin *win, int maxH);
438 extern void  butWin_setWHRatio(ButWin *win, int w, int h);
439 extern void  butWin_setWHRatios(ButWin *win,
440 				int minW, int minH, int maxW, int maxH);
441 extern void  butWin_setWStep(ButWin *win, int wStep);
442 extern void  butWin_setHStep(ButWin *win, int hStep);
443 #define  butWin_getMinW(w)   ((w)->minW)
444 #define  butWin_getMinH(w)   ((w)->minH)
445 #define  butWin_getMaxW(w)   ((w)->maxW)
446 #define  butWin_getMaxH(w)   ((w)->maxH)
447 #define  butWin_getWStep(w)  ((w)->wStep)
448 #define  butWin_getHStep(w)  ((w)->hStep)
449 extern void  butWin_activate(ButWin *win);
450 extern void  butWin_checkDims(ButWin *win);  /* Private. */
451 extern void  butWin_resize(ButWin *win, int newW, int newH);
452 extern void  butWin_destroy(ButWin *xw);
453 
454 /* grey is 0..16 */
455 extern ButColor  butColor_create(int r, int g, int b, int grey);
456 /* The output color will have bw_greyscale same as c1's. */
457 extern ButColor  butColor_mix(ButColor c1, int r1,
458 			      ButColor c2, int r2);
459 extern bool  butEnv_setColor(ButEnv *env, int colornum, ButColor color);
460 XImage  *butEnv_imageCreate(ButEnv *env, int w, int h);
461 void  butEnv_imageDestroy(XImage *image);
462 
463 /* From "i_general.c" */
464 extern void  but_setFlags(But *but, uint fch);
465 extern void  but_setKeys(But *but, const ButKey *keys);
466 
467 /* From "but_write.c" */
468 extern void  butEnv_setChar(ButEnv *env, double wratio, const char *id,
469 			    void (*draw)(void *packet, ButWin *win,
470 					 int x, int y, int w, int h),
471 			    void *packet);
472 extern int  butEnv_textWidth(ButEnv *env, const char *text, int fnum);
473 extern int  butEnv_charWidth(ButEnv *env, const char *text, int fnum);
474 
475 /* From "but_textin.c" */
476 
477 #define  BUT_NOCHANGE  (1 << (sizeof(int)*8-2))
478 
479 #endif  /* _BUT_BUT_H_ */
480