root/trunk/oldXMenu/Create.c

Revision 4220, 21.7 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to native
Line 
1 /* Copyright    Massachusetts Institute of Technology    1985   */
2
3 #include "copyright.h"
4
5 /*
6 Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
7   2007, 2008 Free Software Foundation, Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING.  If not, write to the
21 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24
25 /*
26  * XMenu:       MIT Project Athena, X Window system menu package
27  *
28  *      XMenuCreate -   Creates an X window system menu object.
29  *
30  *      Author:         Tony Della Fera, DEC
31  *                      January 23, 1986
32  *
33  */
34
35 #include <config.h>
36 #include "XMenuInt.h"
37
38
39 #ifdef EMACS_BITMAP_FILES
40 #include "../src/bitmaps/dimple1.xbm"
41 #include "../src/bitmaps/dimple3.xbm"
42 #include "../src/bitmaps/gray1.xbm"
43 #include "../src/bitmaps/gray3.xbm"
44 #include "../src/bitmaps/crosswv.xbm"
45
46 #include "../src/bitmaps/leftptr.xbm"
47 #include "../src/bitmaps/leftpmsk.xbm"
48 #include "../src/bitmaps/rtptr.xbm"
49 #include "../src/bitmaps/rtpmsk.xbm"
50 #include "../src/bitmaps/cntrptr.xbm"
51 #include "../src/bitmaps/cntrpmsk.xbm"
52 #include "../src/bitmaps/stipple.xbm"
53
54 #else
55 #ifndef VMS
56
57 #include <X11/bitmaps/dimple1>
58 #include <X11/bitmaps/dimple3>
59 #include <X11/bitmaps/gray1>
60 #include <X11/bitmaps/gray3>
61 #include <X11/bitmaps/cross_weave>
62
63 #include <X11/bitmaps/left_ptr>
64 #include <X11/bitmaps/left_ptrmsk>
65 #include <X11/bitmaps/right_ptr>
66 #include <X11/bitmaps/right_ptrmsk>
67 #include <X11/bitmaps/cntr_ptr>
68 #include <X11/bitmaps/cntr_ptrmsk>
69 #include <X11/bitmaps/stipple>
70
71 #else
72
73 #include "[-.src.bitmaps]dimple1.xbm"
74 #include "[-.src.bitmaps]dimple3.xbm"
75 #include "[-.src.bitmaps]gray1.xbm"
76 #include "[-.src.bitmaps]gray3.xbm"
77 #include "[-.src.bitmaps]crosswv.xbm"
78
79 #include "[-.src.bitmaps]leftptr.xbm"
80 #include "[-.src.bitmaps]leftpmsk.xbm"
81 #include "[-.src.bitmaps]rtptr.xbm"
82 #include "[-.src.bitmaps]rtpmsk.xbm"
83 #include "[-.src.bitmaps]cntrptr.xbm"
84 #include "[-.src.bitmaps]cntrpmsk.xbm"
85 #include "[-.src.bitmaps]stipple.xbm"
86
87 #endif /* VMS */
88 #endif /* not EMACS_BITMAP_FILES */
89
90 #define DEF_FREEZE              0
91 #define DEF_REVERSE             0
92 #define DEF_MENU_STYLE          LEFT
93 #define DEF_MENU_MODE           BOX
94 #define DEF_INACT_PNUM          3
95 #define MAX_INACT_PNUM          4
96
97 #define DEF_P_STYLE             CENTER
98
99 #define DEF_P_EVENTS            (EnterWindowMask | ExposureMask)
100 #define DEF_P_FNT_NAME          "fixed"
101 #define DEF_P_SPREAD            0.5
102 #define DEF_P_BDR_WIDTH         2
103
104 #define DEF_S_STYLE             LEFT
105 #define DEF_S_EVENTS            (EnterWindowMask | LeaveWindowMask)
106 #define DEF_S_FNT_NAME          "fixed"
107 #define DEF_S_SPREAD            0.10
108 #define DEF_S_BDR_WIDTH         1
109
110 #define XASSOC_TABLE_SIZE       64
111
112 #define TILE_BUF_SIZE           5
113
114 int atoi();
115 double atof();
116 char *x_get_resource_string ();
117
118
119
120 static Status
121 XAllocDisplayColor(display, map, colorName, color, junk)
122     Display *display;
123     Colormap map;
124     char *colorName;
125     XColor *color;
126     XColor *junk;
127 {
128   return (colorName!=0 &&
129           XParseColor(display, map, colorName, color) &&
130           XAllocColor(display, map, color));
131 }
132
133
134 XMenu *
135 XMenuCreate(display, parent, def_env)
136     Display *display;           /* ID of previously opened display */
137     Window parent;              /* Window ID of the menu's parent window. */
138     register char *def_env;     /* X Defaults program environment name. */
139 {
140   register int i;               /* Loop counter. */
141   register int j;               /* Loop counter. */
142   register char *def_val;       /* X Default value temp variable. */
143
144   register XMenu *menu;         /* Pointer to the new menu. */
145   XMStyle menu_style;           /* Menu display style. */
146   XMMode menu_mode;             /* Menu display mode. */
147   XMPane *pane;                 /* Pane list header. */
148   XAssocTable *assoc_tab;       /* XAssocTable pointer. */
149
150   int freeze;                   /* Freeze server mode. */
151   int reverse;                  /* Reverse video mode. */
152
153   XMStyle p_style;              /* Pane display style. */
154   char *p_fnt_name;             /* Flag font name. */
155   XFontStruct *p_fnt_info;      /* Flag font structure */
156   int p_fnt_pad;                /* Flag font padding in pixels. */
157   double p_spread;              /* Pane spread in flag height fractions. */
158   int p_fnt_height;             /* Pane character height. */
159   int p_bdr_width;              /* Pane border width. */
160   int flag_height;              /* Flag window height. */
161   int p_height;                 /* Pane window height. */
162   int p_x_off;                  /* Pane X offset. */
163   int p_y_off;                  /* Pane Y offset. */
164   GC pane_GC;                   /* Pane graphics context. */
165
166   XMStyle s_style;              /* Selection display style. */
167   char *s_fnt_name;             /* Selection font name. */
168   XFontStruct *s_fnt_info;      /* Selection font structure. */
169   int s_fnt_pad;                /* Selection font padding in pixels. */
170   int s_fnt_height;             /* Selection font character height */
171   double s_spread;              /* Select spread in line height fractions. */
172   int s_bdr_width;              /* Highlight border width. */
173   int s_height;                 /* Selection window height. */
174   int s_x_off;                  /* Selection window X offset. */
175   int s_y_off;                  /* Selection window Y offset. */
176   GC normal_select_GC;          /* GC used for normal video selection. */
177   GC inverse_select_GC;         /* GC used for inverse video selection. */
178   GC inact_GC;                  /* GC for inactive pane header and */
179   /* selections. */
180   GC inact_GC_noexpose;
181
182   XColor color_def;             /* Temp color definition holder. */
183   XColor screen_def;            /* Temp screen color definition holder */
184   XColor p_bdr_color;           /* Color of border. */
185   XColor s_bdr_color;           /* Color of highlight. */
186   XColor p_frg_color;           /* Color of pane foreground color. */
187   XColor s_frg_color;           /* Color of selection foreground. */
188   XColor bkgnd_color;           /* Color of background.. */
189   XColor mouse_color;           /* Color of mouse cursor. */
190   Cursor mouse_cursor;          /* Mouse cursor. */
191   Pixmap inact_bitmap;          /* Menu inactive pixmap. */
192
193   int inact_pnum;               /* Inactive background pattern number. */
194
195   Pixel p_bdr_pixel;            /* Pane border pixel. */
196   Pixel s_bdr_pixel;            /* Selection border pixel. */
197   Pixel p_frg_pixel;            /* Pane foreground pixel. */
198   Pixel s_frg_pixel;            /* Selection foreground pixel. */
199   Pixel bkgnd_pixel;            /* Menu background pixel. */
200
201   int *width, *height;
202   Pixmap *bitmap;
203   int *x_hot, *y_hot;
204   int status;                   /* Return code from XReadBitmapFile. */
205
206   Pixmap cursor;                /* Cursor pixmap holder. */
207   Pixmap cursor_mask;           /* Cursor mask pixmap holder. */
208   Pixmap stipple_pixmap;        /* Stipple mask for half-tone text. */
209   unsigned long valuemask;
210   XGCValues *values;
211
212   Window root = RootWindow (display, DefaultScreen (display));
213
214   /*
215    * Calloc the XMenu structure and the initial pane.
216    */
217   menu = (XMenu *)calloc(1, sizeof(XMenu));
218   if (menu == NULL) {
219     _XMErrorCode = XME_CALLOC;
220     return(NULL);
221   }
222   pane = (XMPane *)calloc(1, sizeof(XMPane));
223   if (pane == NULL) {
224     _XMErrorCode = XME_CALLOC;
225     return(NULL);
226   }
227
228   /*
229    * Create the XAssocTable
230    */
231   assoc_tab = (XAssocTable *)XCreateAssocTable(XASSOC_TABLE_SIZE);
232   if(assoc_tab == NULL) {
233     _XMErrorCode= XME_CREATE_ASSOC;
234     return(NULL);
235   }
236
237   /*
238    * Set up the default environment name.
239    */
240   if (def_env == NULL || *def_env == '\0') def_env = "XMenu";
241
242   /*
243    * Set up internal fail-safe defaults.
244    */
245   freeze = DEF_FREEZE;
246   reverse = DEF_REVERSE;
247   menu_style = DEF_MENU_STYLE;
248   menu_mode = DEF_MENU_MODE;
249   inact_pnum = DEF_INACT_PNUM;
250
251   p_style = DEF_P_STYLE;
252   p_spread = DEF_P_SPREAD;
253   p_fnt_name = DEF_P_FNT_NAME;
254   p_bdr_width = DEF_P_BDR_WIDTH;
255
256   s_style = DEF_S_STYLE;
257   s_spread = DEF_S_SPREAD;
258   s_fnt_name = DEF_S_FNT_NAME;
259   s_bdr_width = DEF_S_BDR_WIDTH;
260
261   /*
262    * Get default values from X.
263    */
264   def_val = x_get_resource_string ("menuFreeze", "MenuFreeze");
265   if (def_val != NULL) {
266     if (strcmp(def_val, "on") == 0) freeze = 1;
267     else if (strcmp(def_val, "off") == 0) freeze = 0;
268   }
269
270   def_val = x_get_resource_string ("menuReverseVideo", "MenuReverseVideo");
271   if (def_val != NULL) {
272     if (strcmp(def_val, "on") == 0) reverse = 1;
273     else if (strcmp(def_val, "off") == 0) reverse = 0;
274   }
275
276   def_val = x_get_resource_string ("menuStyle", "MenuStyle");
277   if (def_val != NULL) {
278     if (strcmp(def_val, "right_hand") == 0) menu_style = RIGHT;
279     else if (strcmp(def_val, "left_hand") == 0) menu_style = LEFT;
280     else if (strcmp(def_val, "center") == 0) menu_style = CENTER;
281   }
282
283   def_val = x_get_resource_string ("menuMode", "MenuMode");
284   if (def_val != NULL) {
285     if (strcmp(def_val, "box") == 0) menu_mode = BOX;
286     else if (strcmp(def_val, "invert") == 0) menu_mode = INVERT;
287   }
288
289   def_val = x_get_resource_string ("menuMouse", "MenuMouse");
290   if (
291       def_val != NULL &&
292       DisplayCells(display, DefaultScreen(display)) > 2 &&
293       XAllocDisplayColor(display,
294                          DefaultColormap(display, DefaultScreen(display)),
295                          def_val,
296                          &mouse_color, &color_def)
297       );
298   else if (reverse &&
299            XAllocDisplayColor(display,
300                               DefaultColormap(display, DefaultScreen(display)),
301                               "white",
302                               &mouse_color, &color_def)
303            );
304
305   else if (XAllocDisplayColor(display,
306                               DefaultColormap(display, DefaultScreen(display)),
307                               "black",
308                               &mouse_color, &color_def)
309            );
310
311   else ;
312
313   def_val = x_get_resource_string ("menuBackground", "MenuBackground");
314   if (
315       def_val != NULL &&
316       DisplayCells(display, DefaultScreen(display)) > 2 &&
317       XAllocDisplayColor(display,
318                          DefaultColormap(display, DefaultScreen(display)),
319                          def_val,
320                          &bkgnd_color, &color_def)
321       );
322   else if (reverse &&
323            XAllocDisplayColor(display,
324                               DefaultColormap(display, DefaultScreen(display)),
325                               "black",
326                               &bkgnd_color, &color_def)
327            );
328   else if (XAllocDisplayColor(display,
329                               DefaultColormap(display, DefaultScreen(display)),
330                               "white",
331                               &bkgnd_color, &color_def)
332            );
333   else;
334
335   def_val = x_get_resource_string ("menuInactivePattern", "MenuInactivePattern");
336   if (def_val != NULL) {
337     if (strcmp(def_val, "dimple1") == 0) inact_pnum = 0;
338     else if (strcmp(def_val, "dimple3") == 0) inact_pnum = 1;
339     else if (strcmp(def_val, "gray1") == 0) inact_pnum = 2;
340     else if (strcmp(def_val, "gray3") == 0) inact_pnum = 3;
341     else if (strcmp(def_val, "cross_weave") == 0) inact_pnum = 4;
342   }
343
344   def_val = x_get_resource_string ("paneStyle", "PaneStyle");
345   if (def_val != NULL) {
346     if (strcmp(def_val, "flush_left") == 0) p_style = LEFT;
347     else if (strcmp(def_val, "flush_right") == 0) p_style = RIGHT;
348     else if (strcmp(def_val, "center") == 0) p_style = CENTER;
349   }
350
351   def_val = x_get_resource_string ("paneFont", "PaneFont");
352   if (def_val != NULL) p_fnt_name = def_val;
353
354   def_val = x_get_resource_string ("paneForeground", "PaneForeground");
355   if (
356       def_val != NULL &&
357       DisplayCells(display, DefaultScreen(display)) > 2
358       )
359     XAllocDisplayColor(display, DefaultColormap(display,
360                                                 DefaultScreen(display)),
361                        def_val,
362                        &p_frg_color, &color_def);
363
364   else if (reverse) XAllocDisplayColor(display,
365                                        DefaultColormap(display,
366                                                        DefaultScreen(display)),
367                                        "white",
368                                        &p_frg_color, &color_def);
369   else XAllocDisplayColor(display,
370                           DefaultColormap(display, DefaultScreen(display)),
371                           "black",
372                           &p_frg_color, &color_def);
373
374   def_val = x_get_resource_string ("paneBorder", "PaneBorder");
375   if (
376       def_val != NULL &&
377       DisplayCells(display, DefaultScreen(display)) > 2 &&
378       XAllocDisplayColor(display,
379                          DefaultColormap(display, DefaultScreen(display)),
380                          def_val,
381                          &p_bdr_color, &color_def)
382       );
383   else if (reverse &&
384            XAllocDisplayColor(display,
385                               DefaultColormap(display, DefaultScreen(display)),
386                               "white",
387                               &p_bdr_color, &color_def)
388            );
389   else XAllocDisplayColor(display,
390                           DefaultColormap(display, DefaultScreen(display)),
391                           "black",
392                           &p_bdr_color, &color_def);
393
394   def_val = x_get_resource_string ("paneBorderWidth", "PaneBorderWidth");
395   if (def_val != NULL) p_bdr_width = atoi(def_val);
396
397   def_val = x_get_resource_string ("paneSpread", "PaneSpread");
398   if (def_val != NULL) p_spread = atof(def_val);
399
400   def_val = x_get_resource_string ("selectionStyle", "SelectionStyle");
401   if (def_val != NULL) {
402     if (strcmp(def_val, "flush_left") == 0) s_style = LEFT;
403     else if (strcmp(def_val, "flush_right") == 0) s_style = RIGHT;
404     else if (strcmp(def_val, "center") == 0) s_style = CENTER;
405   }
406
407   def_val = x_get_resource_string ("selectionFont", "SelectionFont");
408   if (def_val != NULL) s_fnt_name = def_val;
409
410   def_val = x_get_resource_string ("selectionForeground", "SelectionForeground");
411   if (
412       def_val != NULL &&
413       DisplayCells(display, DefaultScreen(display)) > 2 &&
414       XAllocDisplayColor(display,
415                          DefaultColormap(display, DefaultScreen(display)),
416                          def_val,
417                          &s_frg_color, &color_def)
418       );
419   else if (reverse &&
420            XAllocDisplayColor(display,
421                               DefaultColormap(display, DefaultScreen(display)),
422                               "white",
423                               &s_frg_color, &color_def)
424            ) ;
425   else if (XAllocDisplayColor(display,
426                               DefaultColormap(display, DefaultScreen(display)),
427                               "black",
428                               &s_frg_color, &color_def)
429            ) ;
430   else ;
431
432
433   def_val = x_get_resource_string ("selectionBorder", "SelectionBorder");
434   if (
435       def_val != NULL &&
436       DisplayCells(display, DefaultScreen(display)) > 2 &&
437       XAllocDisplayColor(display,
438                          DefaultColormap(display, DefaultScreen(display)),
439                          def_val,
440                          &s_bdr_color, &color_def)
441       ) ;
442   else if (reverse &&
443            XAllocDisplayColor(display,
444                               DefaultColormap(display, DefaultScreen(display)),
445                               "white",
446                               &s_bdr_color, &color_def)
447            ) ;
448   else if (XAllocDisplayColor(display,
449                               DefaultColormap(display, DefaultScreen(display)),
450                               "black",
451                               &s_bdr_color, &color_def)
452            ) ;
453   else ;
454
455   def_val = x_get_resource_string ("selectionBorderWidth", "SelectionBorderWidth");
456   if (def_val != NULL) s_bdr_width = atoi(def_val);
457
458   def_val = x_get_resource_string ("selectionSpread", "SelectionSpread");
459   if (def_val != NULL) s_spread = atof(def_val);
460
461   /*
462    * Create and store the inactive pattern pixmap.
463    */
464   {
465     char *data = NULL;
466     int width, height;
467
468     switch (inact_pnum)
469       {
470       case 0:
471         data = (char *)dimple1_bits;
472         width = dimple1_width;
473         height = dimple1_height;
474         break;
475
476       case 1:
477         data = (char *)dimple3_bits;
478         width = dimple3_width;
479         height = dimple3_height;
480         break;
481
482       case 2:
483         data = (char *)gray1_bits;
484         width = gray1_width;
485         height = gray1_height;
486         break;
487
488       case 3:
489         data = (char *)gray3_bits;
490         width = gray3_width;
491         height = gray3_height;
492         break;
493
494       case 4:
495         data = (char *)cross_weave_bits;
496         width = cross_weave_width;
497         height = cross_weave_height;
498         break;
499       }
500
501     if (! data)
502       {
503         _XMErrorCode = XME_STORE_BITMAP;
504         return(NULL);
505       }
506
507     inact_bitmap =
508       XCreatePixmapFromBitmapData
509         (display, root, data, width, height,
510          p_frg_color.pixel, bkgnd_color.pixel,
511          DisplayPlanes (display, DefaultScreen (display)));
512   }
513
514   /*
515    * Load the mouse cursor.
516    */
517
518   switch (menu_style) {
519   case LEFT:
520     cursor = XCreateBitmapFromData(display,
521                                    root,
522                                    left_ptr_bits,
523                                    left_ptr_width,
524                                    left_ptr_height);
525     cursor_mask = XCreateBitmapFromData(display,
526                                         root,
527                                         left_ptrmsk_bits,
528                                         left_ptrmsk_width,
529                                         left_ptrmsk_height);
530     mouse_cursor = XCreatePixmapCursor(
531                                        <