1 /* Copyright Massachusetts Institute of Technology 1985 */
6 Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 2008, 2009, 2010 Free Software Foundation, Inc.
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 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 * XMenu: MIT Project Athena, X Window system menu package
26 * XMenuCreate - Creates an X window system menu object.
28 * Author: Tony Della Fera, DEC
37 #ifdef EMACS_BITMAP_FILES
38 #include "../src/bitmaps/dimple1.xbm"
39 #include "../src/bitmaps/dimple3.xbm"
40 #include "../src/bitmaps/gray1.xbm"
41 #include "../src/bitmaps/gray3.xbm"
42 #include "../src/bitmaps/crosswv.xbm"
44 #include "../src/bitmaps/leftptr.xbm"
45 #include "../src/bitmaps/leftpmsk.xbm"
46 #include "../src/bitmaps/rtptr.xbm"
47 #include "../src/bitmaps/rtpmsk.xbm"
48 #include "../src/bitmaps/cntrptr.xbm"
49 #include "../src/bitmaps/cntrpmsk.xbm"
50 #include "../src/bitmaps/stipple.xbm"
54 #include <X11/bitmaps/dimple1>
55 #include <X11/bitmaps/dimple3>
56 #include <X11/bitmaps/gray1>
57 #include <X11/bitmaps/gray3>
58 #include <X11/bitmaps/cross_weave>
60 #include <X11/bitmaps/left_ptr>
61 #include <X11/bitmaps/left_ptrmsk>
62 #include <X11/bitmaps/right_ptr>
63 #include <X11/bitmaps/right_ptrmsk>
64 #include <X11/bitmaps/cntr_ptr>
65 #include <X11/bitmaps/cntr_ptrmsk>
66 #include <X11/bitmaps/stipple>
68 #endif /* not EMACS_BITMAP_FILES */
72 #define DEF_MENU_STYLE LEFT
73 #define DEF_MENU_MODE BOX
74 #define DEF_INACT_PNUM 3
75 #define MAX_INACT_PNUM 4
77 #define DEF_P_STYLE CENTER
79 #define DEF_P_EVENTS (EnterWindowMask | ExposureMask)
80 #define DEF_P_FNT_NAME "fixed"
81 #define DEF_P_SPREAD 0.5
82 #define DEF_P_BDR_WIDTH 2
84 #define DEF_S_STYLE LEFT
85 #define DEF_S_EVENTS (EnterWindowMask | LeaveWindowMask)
86 #define DEF_S_FNT_NAME "fixed"
87 #define DEF_S_SPREAD 0.10
88 #define DEF_S_BDR_WIDTH 1
90 #define XASSOC_TABLE_SIZE 64
92 #define TILE_BUF_SIZE 5
94 int atoi(const char *);
95 double atof(const char *);
96 char *x_get_resource_string (char *attribute
, char *class);
101 XAllocDisplayColor(Display
*display
, Colormap map
, char *colorName
, XColor
*color
, XColor
*junk
)
103 return (colorName
!=0 &&
104 XParseColor(display
, map
, colorName
, color
) &&
105 XAllocColor(display
, map
, color
));
110 XMenuCreate(Display
*display
, Window parent
, register char *def_env
)
111 /* ID of previously opened display */
112 /* Window ID of the menu's parent window. */
113 /* X Defaults program environment name. */
115 register int i
; /* Loop counter. */
116 register int j
; /* Loop counter. */
117 register char *def_val
; /* X Default value temp variable. */
119 register XMenu
*menu
; /* Pointer to the new menu. */
120 XMStyle menu_style
; /* Menu display style. */
121 XMMode menu_mode
; /* Menu display mode. */
122 XMPane
*pane
; /* Pane list header. */
123 XAssocTable
*assoc_tab
; /* XAssocTable pointer. */
125 int freeze
; /* Freeze server mode. */
126 int reverse
; /* Reverse video mode. */
128 XMStyle p_style
; /* Pane display style. */
129 char *p_fnt_name
; /* Flag font name. */
130 XFontStruct
*p_fnt_info
; /* Flag font structure */
131 int p_fnt_pad
; /* Flag font padding in pixels. */
132 double p_spread
; /* Pane spread in flag height fractions. */
133 int p_fnt_height
; /* Pane character height. */
134 int p_bdr_width
; /* Pane border width. */
135 int flag_height
; /* Flag window height. */
136 int p_height
; /* Pane window height. */
137 int p_x_off
; /* Pane X offset. */
138 int p_y_off
; /* Pane Y offset. */
139 GC pane_GC
; /* Pane graphics context. */
141 XMStyle s_style
; /* Selection display style. */
142 char *s_fnt_name
; /* Selection font name. */
143 XFontStruct
*s_fnt_info
; /* Selection font structure. */
144 int s_fnt_pad
; /* Selection font padding in pixels. */
145 int s_fnt_height
; /* Selection font character height */
146 double s_spread
; /* Select spread in line height fractions. */
147 int s_bdr_width
; /* Highlight border width. */
148 int s_height
; /* Selection window height. */
149 int s_x_off
; /* Selection window X offset. */
150 int s_y_off
; /* Selection window Y offset. */
151 GC normal_select_GC
; /* GC used for normal video selection. */
152 GC inverse_select_GC
; /* GC used for inverse video selection. */
153 GC inact_GC
; /* GC for inactive pane header and */
155 GC inact_GC_noexpose
;
157 XColor color_def
; /* Temp color definition holder. */
158 XColor screen_def
; /* Temp screen color definition holder */
159 XColor p_bdr_color
; /* Color of border. */
160 XColor s_bdr_color
; /* Color of highlight. */
161 XColor p_frg_color
; /* Color of pane foreground color. */
162 XColor s_frg_color
; /* Color of selection foreground. */
163 XColor bkgnd_color
; /* Color of background.. */
164 XColor mouse_color
; /* Color of mouse cursor. */
165 Cursor mouse_cursor
; /* Mouse cursor. */
166 Pixmap inact_bitmap
; /* Menu inactive pixmap. */
168 int inact_pnum
; /* Inactive background pattern number. */
170 Pixel p_bdr_pixel
; /* Pane border pixel. */
171 Pixel s_bdr_pixel
; /* Selection border pixel. */
172 Pixel p_frg_pixel
; /* Pane foreground pixel. */
173 Pixel s_frg_pixel
; /* Selection foreground pixel. */
174 Pixel bkgnd_pixel
; /* Menu background pixel. */
179 int status
; /* Return code from XReadBitmapFile. */
181 Pixmap cursor
; /* Cursor pixmap holder. */
182 Pixmap cursor_mask
; /* Cursor mask pixmap holder. */
183 Pixmap stipple_pixmap
; /* Stipple mask for half-tone text. */
184 unsigned long valuemask
;
187 Window root
= RootWindow (display
, DefaultScreen (display
));
190 * Calloc the XMenu structure and the initial pane.
192 menu
= (XMenu
*)calloc(1, sizeof(XMenu
));
194 _XMErrorCode
= XME_CALLOC
;
197 pane
= (XMPane
*)calloc(1, sizeof(XMPane
));
199 _XMErrorCode
= XME_CALLOC
;
204 * Create the XAssocTable
206 assoc_tab
= (XAssocTable
*)XCreateAssocTable(XASSOC_TABLE_SIZE
);
207 if(assoc_tab
== NULL
) {
208 _XMErrorCode
= XME_CREATE_ASSOC
;
213 * Set up the default environment name.
215 if (def_env
== NULL
|| *def_env
== '\0') def_env
= "XMenu";
218 * Set up internal fail-safe defaults.
221 reverse
= DEF_REVERSE
;
222 menu_style
= DEF_MENU_STYLE
;
223 menu_mode
= DEF_MENU_MODE
;
224 inact_pnum
= DEF_INACT_PNUM
;
226 p_style
= DEF_P_STYLE
;
227 p_spread
= DEF_P_SPREAD
;
228 p_fnt_name
= DEF_P_FNT_NAME
;
229 p_bdr_width
= DEF_P_BDR_WIDTH
;
231 s_style
= DEF_S_STYLE
;
232 s_spread
= DEF_S_SPREAD
;
233 s_fnt_name
= DEF_S_FNT_NAME
;
234 s_bdr_width
= DEF_S_BDR_WIDTH
;
237 * Get default values from X.
239 def_val
= x_get_resource_string ("menuFreeze", "MenuFreeze");
240 if (def_val
!= NULL
) {
241 if (strcmp(def_val
, "on") == 0) freeze
= 1;
242 else if (strcmp(def_val
, "off") == 0) freeze
= 0;
245 def_val
= x_get_resource_string ("menuReverseVideo", "MenuReverseVideo");
246 if (def_val
!= NULL
) {
247 if (strcmp(def_val
, "on") == 0) reverse
= 1;
248 else if (strcmp(def_val
, "off") == 0) reverse
= 0;
251 def_val
= x_get_resource_string ("menuStyle", "MenuStyle");
252 if (def_val
!= NULL
) {
253 if (strcmp(def_val
, "right_hand") == 0) menu_style
= RIGHT
;
254 else if (strcmp(def_val
, "left_hand") == 0) menu_style
= LEFT
;
255 else if (strcmp(def_val
, "center") == 0) menu_style
= CENTER
;
258 def_val
= x_get_resource_string ("menuMode", "MenuMode");
259 if (def_val
!= NULL
) {
260 if (strcmp(def_val
, "box") == 0) menu_mode
= BOX
;
261 else if (strcmp(def_val
, "invert") == 0) menu_mode
= INVERT
;
264 def_val
= x_get_resource_string ("menuMouse", "MenuMouse");
267 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
268 XAllocDisplayColor(display
,
269 DefaultColormap(display
, DefaultScreen(display
)),
271 &mouse_color
, &color_def
)
274 XAllocDisplayColor(display
,
275 DefaultColormap(display
, DefaultScreen(display
)),
277 &mouse_color
, &color_def
)
280 else if (XAllocDisplayColor(display
,
281 DefaultColormap(display
, DefaultScreen(display
)),
283 &mouse_color
, &color_def
)
288 def_val
= x_get_resource_string ("menuBackground", "MenuBackground");
291 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
292 XAllocDisplayColor(display
,
293 DefaultColormap(display
, DefaultScreen(display
)),
295 &bkgnd_color
, &color_def
)
298 XAllocDisplayColor(display
,
299 DefaultColormap(display
, DefaultScreen(display
)),
301 &bkgnd_color
, &color_def
)
303 else if (XAllocDisplayColor(display
,
304 DefaultColormap(display
, DefaultScreen(display
)),
306 &bkgnd_color
, &color_def
)
310 def_val
= x_get_resource_string ("menuInactivePattern", "MenuInactivePattern");
311 if (def_val
!= NULL
) {
312 if (strcmp(def_val
, "dimple1") == 0) inact_pnum
= 0;
313 else if (strcmp(def_val
, "dimple3") == 0) inact_pnum
= 1;
314 else if (strcmp(def_val
, "gray1") == 0) inact_pnum
= 2;
315 else if (strcmp(def_val
, "gray3") == 0) inact_pnum
= 3;
316 else if (strcmp(def_val
, "cross_weave") == 0) inact_pnum
= 4;
319 def_val
= x_get_resource_string ("paneStyle", "PaneStyle");
320 if (def_val
!= NULL
) {
321 if (strcmp(def_val
, "flush_left") == 0) p_style
= LEFT
;
322 else if (strcmp(def_val
, "flush_right") == 0) p_style
= RIGHT
;
323 else if (strcmp(def_val
, "center") == 0) p_style
= CENTER
;
326 def_val
= x_get_resource_string ("paneFont", "PaneFont");
327 if (def_val
!= NULL
) p_fnt_name
= def_val
;
329 def_val
= x_get_resource_string ("paneForeground", "PaneForeground");
332 DisplayCells(display
, DefaultScreen(display
)) > 2
334 XAllocDisplayColor(display
, DefaultColormap(display
,
335 DefaultScreen(display
)),
337 &p_frg_color
, &color_def
);
339 else if (reverse
) XAllocDisplayColor(display
,
340 DefaultColormap(display
,
341 DefaultScreen(display
)),
343 &p_frg_color
, &color_def
);
344 else XAllocDisplayColor(display
,
345 DefaultColormap(display
, DefaultScreen(display
)),
347 &p_frg_color
, &color_def
);
349 def_val
= x_get_resource_string ("paneBorder", "PaneBorder");
352 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
353 XAllocDisplayColor(display
,
354 DefaultColormap(display
, DefaultScreen(display
)),
356 &p_bdr_color
, &color_def
)
359 XAllocDisplayColor(display
,
360 DefaultColormap(display
, DefaultScreen(display
)),
362 &p_bdr_color
, &color_def
)
364 else XAllocDisplayColor(display
,
365 DefaultColormap(display
, DefaultScreen(display
)),
367 &p_bdr_color
, &color_def
);
369 def_val
= x_get_resource_string ("paneBorderWidth", "PaneBorderWidth");
370 if (def_val
!= NULL
) p_bdr_width
= atoi(def_val
);
372 def_val
= x_get_resource_string ("paneSpread", "PaneSpread");
373 if (def_val
!= NULL
) p_spread
= atof(def_val
);
375 def_val
= x_get_resource_string ("selectionStyle", "SelectionStyle");
376 if (def_val
!= NULL
) {
377 if (strcmp(def_val
, "flush_left") == 0) s_style
= LEFT
;
378 else if (strcmp(def_val
, "flush_right") == 0) s_style
= RIGHT
;
379 else if (strcmp(def_val
, "center") == 0) s_style
= CENTER
;
382 def_val
= x_get_resource_string ("selectionFont", "SelectionFont");
383 if (def_val
!= NULL
) s_fnt_name
= def_val
;
385 def_val
= x_get_resource_string ("selectionForeground", "SelectionForeground");
388 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
389 XAllocDisplayColor(display
,
390 DefaultColormap(display
, DefaultScreen(display
)),
392 &s_frg_color
, &color_def
)
395 XAllocDisplayColor(display
,
396 DefaultColormap(display
, DefaultScreen(display
)),
398 &s_frg_color
, &color_def
)
400 else if (XAllocDisplayColor(display
,
401 DefaultColormap(display
, DefaultScreen(display
)),
403 &s_frg_color
, &color_def
)
408 def_val
= x_get_resource_string ("selectionBorder", "SelectionBorder");
411 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
412 XAllocDisplayColor(display
,
413 DefaultColormap(display
, DefaultScreen(display
)),
415 &s_bdr_color
, &color_def
)
418 XAllocDisplayColor(display
,
419 DefaultColormap(display
, DefaultScreen(display
)),
421 &s_bdr_color
, &color_def
)
423 else if (XAllocDisplayColor(display
,
424 DefaultColormap(display
, DefaultScreen(display
)),
426 &s_bdr_color
, &color_def
)
430 def_val
= x_get_resource_string ("selectionBorderWidth", "SelectionBorderWidth");
431 if (def_val
!= NULL
) s_bdr_width
= atoi(def_val
);
433 def_val
= x_get_resource_string ("selectionSpread", "SelectionSpread");
434 if (def_val
!= NULL
) s_spread
= atof(def_val
);
437 * Create and store the inactive pattern pixmap.
446 data
= (char *)dimple1_bits
;
447 width
= dimple1_width
;
448 height
= dimple1_height
;
452 data
= (char *)dimple3_bits
;
453 width
= dimple3_width
;
454 height
= dimple3_height
;
458 data
= (char *)gray1_bits
;
460 height
= gray1_height
;
464 data
= (char *)gray3_bits
;
466 height
= gray3_height
;
470 data
= (char *)cross_weave_bits
;
471 width
= cross_weave_width
;
472 height
= cross_weave_height
;
478 _XMErrorCode
= XME_STORE_BITMAP
;
483 XCreatePixmapFromBitmapData
484 (display
, root
, data
, width
, height
,
485 p_frg_color
.pixel
, bkgnd_color
.pixel
,
486 DisplayPlanes (display
, DefaultScreen (display
)));
490 * Load the mouse cursor.
493 switch (menu_style
) {
495 cursor
= XCreateBitmapFromData(display
,
500 cursor_mask
= XCreateBitmapFromData(display
,
505 mouse_cursor
= XCreatePixmapCursor(
508 &mouse_color
, &bkgnd_color
,
512 XFreePixmap(display
, cursor
);
513 XFreePixmap(display
, cursor_mask
);
516 cursor
= XCreateBitmapFromData(display
,
521 cursor_mask
= XCreateBitmapFromData(display
,
525 right_ptrmsk_height
);
526 mouse_cursor
= XCreatePixmapCursor(
529 &mouse_color
, &bkgnd_color
,
533 XFreePixmap(display
, cursor
);
534 XFreePixmap(display
, cursor_mask
);
537 cursor
= XCreateBitmapFromData(display
,
542 cursor_mask
= XCreateBitmapFromData(display
,
547 mouse_cursor
= XCreatePixmapCursor(
550 &mouse_color
, &bkgnd_color
,
554 XFreePixmap(display
, cursor
);
555 XFreePixmap(display
, cursor_mask
);
558 /* Error! Invalid style parameter. */
559 _XMErrorCode
= XME_STYLE_PARAM
;
562 if (mouse_cursor
== _X_FAILURE
) {
563 _XMErrorCode
= XME_CREATE_CURSOR
;
568 * Open the pane and selection fonts.
571 p_fnt_info
= XLoadQueryFont(display
, p_fnt_name
);
572 if (p_fnt_info
== NULL
) {
573 _XMErrorCode
= XME_OPEN_FONT
;
578 s_fnt_info
= XLoadQueryFont(display
, s_fnt_name
);
579 if (s_fnt_info
== NULL
) {
580 _XMErrorCode
= XME_OPEN_FONT
;
584 * Calculate the fixed padding value in pixels for each font.
586 p_fnt_height
= p_fnt_info
->max_bounds
.ascent
+ p_fnt_info
->max_bounds
.descent
;
587 s_fnt_height
= s_fnt_info
->max_bounds
.ascent
+ s_fnt_info
->max_bounds
.descent
;
588 p_fnt_pad
= s_spread
* p_fnt_height
;
589 s_fnt_pad
= s_spread
* s_fnt_height
;
592 * Calculate fixed height and offset requirements.
594 flag_height
= p_fnt_height
+ (p_fnt_pad
<< 1);
597 p_y_off
= flag_height
+ p_bdr_width
;
598 p_x_off
= p_y_off
* p_spread
;
600 s_height
= s_fnt_height
+ (s_fnt_pad
<< 1) + (s_bdr_width
<< 1);
605 * Set up the pane list header.
609 pane
->type
= PL_HEADER
;
613 * Initialize the internal pane and selection creation queues.
618 * Create pane, active, and inactive GC's.
620 values
= (XGCValues
*)malloc(sizeof(XGCValues
));
621 valuemask
= (GCForeground
| GCBackground
| GCFont
| GCLineWidth
);
627 values
->foreground
= p_frg_color
.pixel
;
628 values
->background
= bkgnd_color
.pixel
;
629 values
->font
= p_fnt_info
->fid
;
630 values
->line_width
= p_bdr_width
;
638 * Then normal video selection.
641 values
->foreground
= s_frg_color
.pixel
;
642 values
->background
= bkgnd_color
.pixel
;
643 values
->font
= s_fnt_info
->fid
;
644 values
->line_width
= s_bdr_width
;
645 normal_select_GC
= XCreateGC(display
,
650 * Inverse video selection.
653 values
->foreground
= bkgnd_color
.pixel
;
654 values
->background
= s_frg_color
.pixel
;
655 values
->font
= s_fnt_info
->fid
;
656 values
->line_width
= s_bdr_width
;
657 inverse_select_GC
= XCreateGC(display
,
661 stipple_pixmap
= XCreateBitmapFromData(display
,
668 * Finally, inactive pane header and selections
670 valuemask
|= (GCFillStyle
| GCStipple
);
671 values
->foreground
= s_frg_color
.pixel
;
672 values
->background
= bkgnd_color
.pixel
;
673 values
->font
= s_fnt_info
->fid
;
674 values
->line_width
= s_bdr_width
;
675 values
->fill_style
= FillStippled
;
676 values
->stipple
= stipple_pixmap
;
678 inact_GC
= XCreateGC(display
,
683 valuemask
|= (GCGraphicsExposures
);
684 values
->graphics_exposures
= False
;
685 inact_GC_noexpose
= XCreateGC (display
,
691 * Construct the XMenu object.
693 /* -------------------- Menu data -------------------- */
694 menu
->menu_style
= menu_style
;
695 menu
->menu_mode
= menu_mode
;
696 menu
->freeze
= freeze
;
699 menu
->parent
= parent
;
702 menu
->mouse_cursor
= mouse_cursor
;
703 menu
->assoc_tab
= assoc_tab
;
705 /* -------------------- Pane window data -------------------- */
706 menu
->p_style
= p_style
;
707 menu
->p_events
= DEF_P_EVENTS
;
708 menu
->p_fnt_info
= p_fnt_info
;
709 menu
->p_fnt_pad
= p_fnt_pad
;
710 menu
->p_spread
= p_spread
;
711 menu
->p_bdr_width
= p_bdr_width
;
712 menu
->flag_height
= flag_height
;
714 menu
->p_height
= p_height
;
715 menu
->p_x_off
= p_x_off
;
716 menu
->p_y_off
= p_y_off
;
718 menu
->pane_GC
= pane_GC
;
721 /* -------------------- Selection window data -------------------- */
722 menu
->s_style
= s_style
;
723 menu
->s_events
= DEF_S_EVENTS
;
724 menu
->s_fnt_info
= s_fnt_info
;
725 menu
->s_fnt_pad
= s_fnt_pad
;
726 menu
->s_spread
= s_spread
;
727 menu
->s_bdr_width
= s_bdr_width
; /* unnecessary */
729 menu
->s_height
= s_height
;
730 menu
->s_x_off
= s_x_off
;
731 menu
->s_y_off
= s_y_off
;
733 menu
->normal_select_GC
= normal_select_GC
;
734 menu
->inverse_select_GC
= inverse_select_GC
;
735 menu
->inact_GC
= inact_GC
;
736 /* -------------------- Color data -------------------- */
737 menu
->p_bdr_color
= p_bdr_color
.pixel
;
738 menu
->s_bdr_color
= s_bdr_color
.pixel
;
739 menu
->p_frg_color
= p_frg_color
.pixel
;
740 menu
->s_frg_color
= s_frg_color
.pixel
;
741 menu
->bkgnd_color
= bkgnd_color
.pixel
;
742 /* -------------------- Pixmap data -------------------- */
743 menu
->p_bdr_pixmap
= None
;
744 menu
->s_bdr_pixmap
= None
;
745 menu
->p_frg_pixmap
= None
;
746 menu
->s_frg_pixmap
= None
;
747 menu
->bkgnd_pixmap
= None
;
748 menu
->inact_pixmap
= inact_bitmap
;
751 * Return the completed XMenu.
753 _XMErrorCode
= XME_NO_ERROR
;
757 /* arch-tag: 6945b7d2-3b13-40b9-8b6e-56b1b20f3463
758 (do not change this comment) */