1 /* Copyright Massachusetts Institute of Technology 1985 */
6 Copyright (C) 1993-1994, 2001-2011 Free Software Foundation, Inc.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 * XMenu: MIT Project Athena, X Window system menu package
25 * XMenuCreate - Creates an X window system menu object.
27 * Author: Tony Della Fera, DEC
36 #ifdef EMACS_BITMAP_FILES
37 #include "../src/bitmaps/dimple1.xbm"
38 #include "../src/bitmaps/dimple3.xbm"
39 #include "../src/bitmaps/gray1.xbm"
40 #include "../src/bitmaps/gray3.xbm"
41 #include "../src/bitmaps/crosswv.xbm"
43 #include "../src/bitmaps/leftptr.xbm"
44 #include "../src/bitmaps/leftpmsk.xbm"
45 #include "../src/bitmaps/rtptr.xbm"
46 #include "../src/bitmaps/rtpmsk.xbm"
47 #include "../src/bitmaps/cntrptr.xbm"
48 #include "../src/bitmaps/cntrpmsk.xbm"
49 #include "../src/bitmaps/stipple.xbm"
53 #include <X11/bitmaps/dimple1>
54 #include <X11/bitmaps/dimple3>
55 #include <X11/bitmaps/gray1>
56 #include <X11/bitmaps/gray3>
57 #include <X11/bitmaps/cross_weave>
59 #include <X11/bitmaps/left_ptr>
60 #include <X11/bitmaps/left_ptrmsk>
61 #include <X11/bitmaps/right_ptr>
62 #include <X11/bitmaps/right_ptrmsk>
63 #include <X11/bitmaps/cntr_ptr>
64 #include <X11/bitmaps/cntr_ptrmsk>
65 #include <X11/bitmaps/stipple>
67 #endif /* not EMACS_BITMAP_FILES */
71 #define DEF_MENU_STYLE LEFT
72 #define DEF_MENU_MODE BOX
73 #define DEF_INACT_PNUM 3
74 #define MAX_INACT_PNUM 4
76 #define DEF_P_STYLE CENTER
78 #define DEF_P_EVENTS (EnterWindowMask | ExposureMask)
79 #define DEF_P_FNT_NAME "fixed"
80 #define DEF_P_SPREAD 0.5
81 #define DEF_P_BDR_WIDTH 2
83 #define DEF_S_STYLE LEFT
84 #define DEF_S_EVENTS (EnterWindowMask | LeaveWindowMask)
85 #define DEF_S_FNT_NAME "fixed"
86 #define DEF_S_SPREAD 0.10
87 #define DEF_S_BDR_WIDTH 1
89 #define XASSOC_TABLE_SIZE 64
91 #define TILE_BUF_SIZE 5
93 int atoi(const char *);
94 double atof(const char *);
95 char *x_get_resource_string (char *attribute
, char *class);
100 XAllocDisplayColor(Display
*display
, Colormap map
, char *colorName
, XColor
*color
, XColor
*junk
)
102 return (colorName
!=0 &&
103 XParseColor(display
, map
, colorName
, color
) &&
104 XAllocColor(display
, map
, color
));
109 XMenuCreate(Display
*display
, Window parent
, register char *def_env
)
110 /* ID of previously opened display */
111 /* Window ID of the menu's parent window. */
112 /* X Defaults program environment name. */
114 register int i
; /* Loop counter. */
115 register int j
; /* Loop counter. */
116 register char *def_val
; /* X Default value temp variable. */
118 register XMenu
*menu
; /* Pointer to the new menu. */
119 XMStyle menu_style
; /* Menu display style. */
120 XMMode menu_mode
; /* Menu display mode. */
121 XMPane
*pane
; /* Pane list header. */
122 XAssocTable
*assoc_tab
; /* XAssocTable pointer. */
124 int freeze
; /* Freeze server mode. */
125 int reverse
; /* Reverse video mode. */
127 XMStyle p_style
; /* Pane display style. */
128 char *p_fnt_name
; /* Flag font name. */
129 XFontStruct
*p_fnt_info
; /* Flag font structure */
130 int p_fnt_pad
; /* Flag font padding in pixels. */
131 double p_spread
; /* Pane spread in flag height fractions. */
132 int p_fnt_height
; /* Pane character height. */
133 int p_bdr_width
; /* Pane border width. */
134 int flag_height
; /* Flag window height. */
135 int p_height
; /* Pane window height. */
136 int p_x_off
; /* Pane X offset. */
137 int p_y_off
; /* Pane Y offset. */
138 GC pane_GC
; /* Pane graphics context. */
140 XMStyle s_style
; /* Selection display style. */
141 char *s_fnt_name
; /* Selection font name. */
142 XFontStruct
*s_fnt_info
; /* Selection font structure. */
143 int s_fnt_pad
; /* Selection font padding in pixels. */
144 int s_fnt_height
; /* Selection font character height */
145 double s_spread
; /* Select spread in line height fractions. */
146 int s_bdr_width
; /* Highlight border width. */
147 int s_height
; /* Selection window height. */
148 int s_x_off
; /* Selection window X offset. */
149 int s_y_off
; /* Selection window Y offset. */
150 GC normal_select_GC
; /* GC used for normal video selection. */
151 GC inverse_select_GC
; /* GC used for inverse video selection. */
152 GC inact_GC
; /* GC for inactive pane header and */
154 GC inact_GC_noexpose
;
156 XColor color_def
; /* Temp color definition holder. */
157 XColor screen_def
; /* Temp screen color definition holder */
158 XColor p_bdr_color
; /* Color of border. */
159 XColor s_bdr_color
; /* Color of highlight. */
160 XColor p_frg_color
; /* Color of pane foreground color. */
161 XColor s_frg_color
; /* Color of selection foreground. */
162 XColor bkgnd_color
; /* Color of background.. */
163 XColor mouse_color
; /* Color of mouse cursor. */
164 Cursor mouse_cursor
; /* Mouse cursor. */
165 Pixmap inact_bitmap
; /* Menu inactive pixmap. */
167 int inact_pnum
; /* Inactive background pattern number. */
169 Pixel p_bdr_pixel
; /* Pane border pixel. */
170 Pixel s_bdr_pixel
; /* Selection border pixel. */
171 Pixel p_frg_pixel
; /* Pane foreground pixel. */
172 Pixel s_frg_pixel
; /* Selection foreground pixel. */
173 Pixel bkgnd_pixel
; /* Menu background pixel. */
178 int status
; /* Return code from XReadBitmapFile. */
180 Pixmap cursor
; /* Cursor pixmap holder. */
181 Pixmap cursor_mask
; /* Cursor mask pixmap holder. */
182 Pixmap stipple_pixmap
; /* Stipple mask for half-tone text. */
183 unsigned long valuemask
;
186 Window root
= RootWindow (display
, DefaultScreen (display
));
189 * Calloc the XMenu structure and the initial pane.
191 menu
= (XMenu
*)calloc(1, sizeof(XMenu
));
193 _XMErrorCode
= XME_CALLOC
;
196 pane
= (XMPane
*)calloc(1, sizeof(XMPane
));
198 _XMErrorCode
= XME_CALLOC
;
203 * Create the XAssocTable
205 assoc_tab
= (XAssocTable
*)XCreateAssocTable(XASSOC_TABLE_SIZE
);
206 if(assoc_tab
== NULL
) {
207 _XMErrorCode
= XME_CREATE_ASSOC
;
212 * Set up the default environment name.
214 if (def_env
== NULL
|| *def_env
== '\0') def_env
= "XMenu";
217 * Set up internal fail-safe defaults.
220 reverse
= DEF_REVERSE
;
221 menu_style
= DEF_MENU_STYLE
;
222 menu_mode
= DEF_MENU_MODE
;
223 inact_pnum
= DEF_INACT_PNUM
;
225 p_style
= DEF_P_STYLE
;
226 p_spread
= DEF_P_SPREAD
;
227 p_fnt_name
= DEF_P_FNT_NAME
;
228 p_bdr_width
= DEF_P_BDR_WIDTH
;
230 s_style
= DEF_S_STYLE
;
231 s_spread
= DEF_S_SPREAD
;
232 s_fnt_name
= DEF_S_FNT_NAME
;
233 s_bdr_width
= DEF_S_BDR_WIDTH
;
236 * Get default values from X.
238 def_val
= x_get_resource_string ("menuFreeze", "MenuFreeze");
239 if (def_val
!= NULL
) {
240 if (strcmp(def_val
, "on") == 0) freeze
= 1;
241 else if (strcmp(def_val
, "off") == 0) freeze
= 0;
244 def_val
= x_get_resource_string ("menuReverseVideo", "MenuReverseVideo");
245 if (def_val
!= NULL
) {
246 if (strcmp(def_val
, "on") == 0) reverse
= 1;
247 else if (strcmp(def_val
, "off") == 0) reverse
= 0;
250 def_val
= x_get_resource_string ("menuStyle", "MenuStyle");
251 if (def_val
!= NULL
) {
252 if (strcmp(def_val
, "right_hand") == 0) menu_style
= RIGHT
;
253 else if (strcmp(def_val
, "left_hand") == 0) menu_style
= LEFT
;
254 else if (strcmp(def_val
, "center") == 0) menu_style
= CENTER
;
257 def_val
= x_get_resource_string ("menuMode", "MenuMode");
258 if (def_val
!= NULL
) {
259 if (strcmp(def_val
, "box") == 0) menu_mode
= BOX
;
260 else if (strcmp(def_val
, "invert") == 0) menu_mode
= INVERT
;
263 def_val
= x_get_resource_string ("menuMouse", "MenuMouse");
266 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
267 XAllocDisplayColor(display
,
268 DefaultColormap(display
, DefaultScreen(display
)),
270 &mouse_color
, &color_def
)
273 XAllocDisplayColor(display
,
274 DefaultColormap(display
, DefaultScreen(display
)),
276 &mouse_color
, &color_def
)
279 else if (XAllocDisplayColor(display
,
280 DefaultColormap(display
, DefaultScreen(display
)),
282 &mouse_color
, &color_def
)
287 def_val
= x_get_resource_string ("menuBackground", "MenuBackground");
290 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
291 XAllocDisplayColor(display
,
292 DefaultColormap(display
, DefaultScreen(display
)),
294 &bkgnd_color
, &color_def
)
297 XAllocDisplayColor(display
,
298 DefaultColormap(display
, DefaultScreen(display
)),
300 &bkgnd_color
, &color_def
)
302 else if (XAllocDisplayColor(display
,
303 DefaultColormap(display
, DefaultScreen(display
)),
305 &bkgnd_color
, &color_def
)
309 def_val
= x_get_resource_string ("menuInactivePattern", "MenuInactivePattern");
310 if (def_val
!= NULL
) {
311 if (strcmp(def_val
, "dimple1") == 0) inact_pnum
= 0;
312 else if (strcmp(def_val
, "dimple3") == 0) inact_pnum
= 1;
313 else if (strcmp(def_val
, "gray1") == 0) inact_pnum
= 2;
314 else if (strcmp(def_val
, "gray3") == 0) inact_pnum
= 3;
315 else if (strcmp(def_val
, "cross_weave") == 0) inact_pnum
= 4;
318 def_val
= x_get_resource_string ("paneStyle", "PaneStyle");
319 if (def_val
!= NULL
) {
320 if (strcmp(def_val
, "flush_left") == 0) p_style
= LEFT
;
321 else if (strcmp(def_val
, "flush_right") == 0) p_style
= RIGHT
;
322 else if (strcmp(def_val
, "center") == 0) p_style
= CENTER
;
325 def_val
= x_get_resource_string ("paneFont", "PaneFont");
326 if (def_val
!= NULL
) p_fnt_name
= def_val
;
328 def_val
= x_get_resource_string ("paneForeground", "PaneForeground");
331 DisplayCells(display
, DefaultScreen(display
)) > 2
333 XAllocDisplayColor(display
, DefaultColormap(display
,
334 DefaultScreen(display
)),
336 &p_frg_color
, &color_def
);
338 else if (reverse
) XAllocDisplayColor(display
,
339 DefaultColormap(display
,
340 DefaultScreen(display
)),
342 &p_frg_color
, &color_def
);
343 else XAllocDisplayColor(display
,
344 DefaultColormap(display
, DefaultScreen(display
)),
346 &p_frg_color
, &color_def
);
348 def_val
= x_get_resource_string ("paneBorder", "PaneBorder");
351 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
352 XAllocDisplayColor(display
,
353 DefaultColormap(display
, DefaultScreen(display
)),
355 &p_bdr_color
, &color_def
)
358 XAllocDisplayColor(display
,
359 DefaultColormap(display
, DefaultScreen(display
)),
361 &p_bdr_color
, &color_def
)
363 else XAllocDisplayColor(display
,
364 DefaultColormap(display
, DefaultScreen(display
)),
366 &p_bdr_color
, &color_def
);
368 def_val
= x_get_resource_string ("paneBorderWidth", "PaneBorderWidth");
369 if (def_val
!= NULL
) p_bdr_width
= atoi(def_val
);
371 def_val
= x_get_resource_string ("paneSpread", "PaneSpread");
372 if (def_val
!= NULL
) p_spread
= atof(def_val
);
374 def_val
= x_get_resource_string ("selectionStyle", "SelectionStyle");
375 if (def_val
!= NULL
) {
376 if (strcmp(def_val
, "flush_left") == 0) s_style
= LEFT
;
377 else if (strcmp(def_val
, "flush_right") == 0) s_style
= RIGHT
;
378 else if (strcmp(def_val
, "center") == 0) s_style
= CENTER
;
381 def_val
= x_get_resource_string ("selectionFont", "SelectionFont");
382 if (def_val
!= NULL
) s_fnt_name
= def_val
;
384 def_val
= x_get_resource_string ("selectionForeground", "SelectionForeground");
387 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
388 XAllocDisplayColor(display
,
389 DefaultColormap(display
, DefaultScreen(display
)),
391 &s_frg_color
, &color_def
)
394 XAllocDisplayColor(display
,
395 DefaultColormap(display
, DefaultScreen(display
)),
397 &s_frg_color
, &color_def
)
399 else if (XAllocDisplayColor(display
,
400 DefaultColormap(display
, DefaultScreen(display
)),
402 &s_frg_color
, &color_def
)
407 def_val
= x_get_resource_string ("selectionBorder", "SelectionBorder");
410 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
411 XAllocDisplayColor(display
,
412 DefaultColormap(display
, DefaultScreen(display
)),
414 &s_bdr_color
, &color_def
)
417 XAllocDisplayColor(display
,
418 DefaultColormap(display
, DefaultScreen(display
)),
420 &s_bdr_color
, &color_def
)
422 else if (XAllocDisplayColor(display
,
423 DefaultColormap(display
, DefaultScreen(display
)),
425 &s_bdr_color
, &color_def
)
429 def_val
= x_get_resource_string ("selectionBorderWidth", "SelectionBorderWidth");
430 if (def_val
!= NULL
) s_bdr_width
= atoi(def_val
);
432 def_val
= x_get_resource_string ("selectionSpread", "SelectionSpread");
433 if (def_val
!= NULL
) s_spread
= atof(def_val
);
436 * Create and store the inactive pattern pixmap.
445 data
= (char *)dimple1_bits
;
446 width
= dimple1_width
;
447 height
= dimple1_height
;
451 data
= (char *)dimple3_bits
;
452 width
= dimple3_width
;
453 height
= dimple3_height
;
457 data
= (char *)gray1_bits
;
459 height
= gray1_height
;
463 data
= (char *)gray3_bits
;
465 height
= gray3_height
;
469 data
= (char *)cross_weave_bits
;
470 width
= cross_weave_width
;
471 height
= cross_weave_height
;
477 _XMErrorCode
= XME_STORE_BITMAP
;
482 XCreatePixmapFromBitmapData
483 (display
, root
, data
, width
, height
,
484 p_frg_color
.pixel
, bkgnd_color
.pixel
,
485 DisplayPlanes (display
, DefaultScreen (display
)));
489 * Load the mouse cursor.
492 switch (menu_style
) {
494 cursor
= XCreateBitmapFromData(display
,
499 cursor_mask
= XCreateBitmapFromData(display
,
504 mouse_cursor
= XCreatePixmapCursor(
507 &mouse_color
, &bkgnd_color
,
511 XFreePixmap(display
, cursor
);
512 XFreePixmap(display
, cursor_mask
);
515 cursor
= XCreateBitmapFromData(display
,
520 cursor_mask
= XCreateBitmapFromData(display
,
524 right_ptrmsk_height
);
525 mouse_cursor
= XCreatePixmapCursor(
528 &mouse_color
, &bkgnd_color
,
532 XFreePixmap(display
, cursor
);
533 XFreePixmap(display
, cursor_mask
);
536 cursor
= XCreateBitmapFromData(display
,
541 cursor_mask
= XCreateBitmapFromData(display
,
546 mouse_cursor
= XCreatePixmapCursor(
549 &mouse_color
, &bkgnd_color
,
553 XFreePixmap(display
, cursor
);
554 XFreePixmap(display
, cursor_mask
);
557 /* Error! Invalid style parameter. */
558 _XMErrorCode
= XME_STYLE_PARAM
;
561 if (mouse_cursor
== _X_FAILURE
) {
562 _XMErrorCode
= XME_CREATE_CURSOR
;
567 * Open the pane and selection fonts.
570 p_fnt_info
= XLoadQueryFont(display
, p_fnt_name
);
571 if (p_fnt_info
== NULL
) {
572 _XMErrorCode
= XME_OPEN_FONT
;
577 s_fnt_info
= XLoadQueryFont(display
, s_fnt_name
);
578 if (s_fnt_info
== NULL
) {
579 _XMErrorCode
= XME_OPEN_FONT
;
583 * Calculate the fixed padding value in pixels for each font.
585 p_fnt_height
= p_fnt_info
->max_bounds
.ascent
+ p_fnt_info
->max_bounds
.descent
;
586 s_fnt_height
= s_fnt_info
->max_bounds
.ascent
+ s_fnt_info
->max_bounds
.descent
;
587 p_fnt_pad
= s_spread
* p_fnt_height
;
588 s_fnt_pad
= s_spread
* s_fnt_height
;
591 * Calculate fixed height and offset requirements.
593 flag_height
= p_fnt_height
+ (p_fnt_pad
<< 1);
596 p_y_off
= flag_height
+ p_bdr_width
;
597 p_x_off
= p_y_off
* p_spread
;
599 s_height
= s_fnt_height
+ (s_fnt_pad
<< 1) + (s_bdr_width
<< 1);
604 * Set up the pane list header.
608 pane
->type
= PL_HEADER
;
612 * Initialize the internal pane and selection creation queues.
617 * Create pane, active, and inactive GC's.
619 values
= (XGCValues
*)malloc(sizeof(XGCValues
));
620 valuemask
= (GCForeground
| GCBackground
| GCFont
| GCLineWidth
);
626 values
->foreground
= p_frg_color
.pixel
;
627 values
->background
= bkgnd_color
.pixel
;
628 values
->font
= p_fnt_info
->fid
;
629 values
->line_width
= p_bdr_width
;
637 * Then normal video selection.
640 values
->foreground
= s_frg_color
.pixel
;
641 values
->background
= bkgnd_color
.pixel
;
642 values
->font
= s_fnt_info
->fid
;
643 values
->line_width
= s_bdr_width
;
644 normal_select_GC
= XCreateGC(display
,
649 * Inverse video selection.
652 values
->foreground
= bkgnd_color
.pixel
;
653 values
->background
= s_frg_color
.pixel
;
654 values
->font
= s_fnt_info
->fid
;
655 values
->line_width
= s_bdr_width
;
656 inverse_select_GC
= XCreateGC(display
,
660 stipple_pixmap
= XCreateBitmapFromData(display
,
667 * Finally, inactive pane header and selections
669 valuemask
|= (GCFillStyle
| GCStipple
);
670 values
->foreground
= s_frg_color
.pixel
;
671 values
->background
= bkgnd_color
.pixel
;
672 values
->font
= s_fnt_info
->fid
;
673 values
->line_width
= s_bdr_width
;
674 values
->fill_style
= FillStippled
;
675 values
->stipple
= stipple_pixmap
;
677 inact_GC
= XCreateGC(display
,
682 valuemask
|= (GCGraphicsExposures
);
683 values
->graphics_exposures
= False
;
684 inact_GC_noexpose
= XCreateGC (display
,
690 * Construct the XMenu object.
692 /* -------------------- Menu data -------------------- */
693 menu
->menu_style
= menu_style
;
694 menu
->menu_mode
= menu_mode
;
695 menu
->freeze
= freeze
;
698 menu
->parent
= parent
;
701 menu
->mouse_cursor
= mouse_cursor
;
702 menu
->assoc_tab
= assoc_tab
;
704 /* -------------------- Pane window data -------------------- */
705 menu
->p_style
= p_style
;
706 menu
->p_events
= DEF_P_EVENTS
;
707 menu
->p_fnt_info
= p_fnt_info
;
708 menu
->p_fnt_pad
= p_fnt_pad
;
709 menu
->p_spread
= p_spread
;
710 menu
->p_bdr_width
= p_bdr_width
;
711 menu
->flag_height
= flag_height
;
713 menu
->p_height
= p_height
;
714 menu
->p_x_off
= p_x_off
;
715 menu
->p_y_off
= p_y_off
;
717 menu
->pane_GC
= pane_GC
;
720 /* -------------------- Selection window data -------------------- */
721 menu
->s_style
= s_style
;
722 menu
->s_events
= DEF_S_EVENTS
;
723 menu
->s_fnt_info
= s_fnt_info
;
724 menu
->s_fnt_pad
= s_fnt_pad
;
725 menu
->s_spread
= s_spread
;
726 menu
->s_bdr_width
= s_bdr_width
; /* unnecessary */
728 menu
->s_height
= s_height
;
729 menu
->s_x_off
= s_x_off
;
730 menu
->s_y_off
= s_y_off
;
732 menu
->normal_select_GC
= normal_select_GC
;
733 menu
->inverse_select_GC
= inverse_select_GC
;
734 menu
->inact_GC
= inact_GC
;
735 /* -------------------- Color data -------------------- */
736 menu
->p_bdr_color
= p_bdr_color
.pixel
;
737 menu
->s_bdr_color
= s_bdr_color
.pixel
;
738 menu
->p_frg_color
= p_frg_color
.pixel
;
739 menu
->s_frg_color
= s_frg_color
.pixel
;
740 menu
->bkgnd_color
= bkgnd_color
.pixel
;
741 /* -------------------- Pixmap data -------------------- */
742 menu
->p_bdr_pixmap
= None
;
743 menu
->s_bdr_pixmap
= None
;
744 menu
->p_frg_pixmap
= None
;
745 menu
->s_frg_pixmap
= None
;
746 menu
->bkgnd_pixmap
= None
;
747 menu
->inact_pixmap
= inact_bitmap
;
750 * Return the completed XMenu.
752 _XMErrorCode
= XME_NO_ERROR
;