1 /* Copyright Massachusetts Institute of Technology 1985 */
6 Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 2008, 2009 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
96 char *x_get_resource_string ();
101 XAllocDisplayColor(display
, map
, colorName
, color
, junk
)
108 return (colorName
!=0 &&
109 XParseColor(display
, map
, colorName
, color
) &&
110 XAllocColor(display
, map
, color
));
115 XMenuCreate(display
, parent
, def_env
)
116 Display
*display
; /* ID of previously opened display */
117 Window parent
; /* Window ID of the menu's parent window. */
118 register char *def_env
; /* X Defaults program environment name. */
120 register int i
; /* Loop counter. */
121 register int j
; /* Loop counter. */
122 register char *def_val
; /* X Default value temp variable. */
124 register XMenu
*menu
; /* Pointer to the new menu. */
125 XMStyle menu_style
; /* Menu display style. */
126 XMMode menu_mode
; /* Menu display mode. */
127 XMPane
*pane
; /* Pane list header. */
128 XAssocTable
*assoc_tab
; /* XAssocTable pointer. */
130 int freeze
; /* Freeze server mode. */
131 int reverse
; /* Reverse video mode. */
133 XMStyle p_style
; /* Pane display style. */
134 char *p_fnt_name
; /* Flag font name. */
135 XFontStruct
*p_fnt_info
; /* Flag font structure */
136 int p_fnt_pad
; /* Flag font padding in pixels. */
137 double p_spread
; /* Pane spread in flag height fractions. */
138 int p_fnt_height
; /* Pane character height. */
139 int p_bdr_width
; /* Pane border width. */
140 int flag_height
; /* Flag window height. */
141 int p_height
; /* Pane window height. */
142 int p_x_off
; /* Pane X offset. */
143 int p_y_off
; /* Pane Y offset. */
144 GC pane_GC
; /* Pane graphics context. */
146 XMStyle s_style
; /* Selection display style. */
147 char *s_fnt_name
; /* Selection font name. */
148 XFontStruct
*s_fnt_info
; /* Selection font structure. */
149 int s_fnt_pad
; /* Selection font padding in pixels. */
150 int s_fnt_height
; /* Selection font character height */
151 double s_spread
; /* Select spread in line height fractions. */
152 int s_bdr_width
; /* Highlight border width. */
153 int s_height
; /* Selection window height. */
154 int s_x_off
; /* Selection window X offset. */
155 int s_y_off
; /* Selection window Y offset. */
156 GC normal_select_GC
; /* GC used for normal video selection. */
157 GC inverse_select_GC
; /* GC used for inverse video selection. */
158 GC inact_GC
; /* GC for inactive pane header and */
160 GC inact_GC_noexpose
;
162 XColor color_def
; /* Temp color definition holder. */
163 XColor screen_def
; /* Temp screen color definition holder */
164 XColor p_bdr_color
; /* Color of border. */
165 XColor s_bdr_color
; /* Color of highlight. */
166 XColor p_frg_color
; /* Color of pane foreground color. */
167 XColor s_frg_color
; /* Color of selection foreground. */
168 XColor bkgnd_color
; /* Color of background.. */
169 XColor mouse_color
; /* Color of mouse cursor. */
170 Cursor mouse_cursor
; /* Mouse cursor. */
171 Pixmap inact_bitmap
; /* Menu inactive pixmap. */
173 int inact_pnum
; /* Inactive background pattern number. */
175 Pixel p_bdr_pixel
; /* Pane border pixel. */
176 Pixel s_bdr_pixel
; /* Selection border pixel. */
177 Pixel p_frg_pixel
; /* Pane foreground pixel. */
178 Pixel s_frg_pixel
; /* Selection foreground pixel. */
179 Pixel bkgnd_pixel
; /* Menu background pixel. */
184 int status
; /* Return code from XReadBitmapFile. */
186 Pixmap cursor
; /* Cursor pixmap holder. */
187 Pixmap cursor_mask
; /* Cursor mask pixmap holder. */
188 Pixmap stipple_pixmap
; /* Stipple mask for half-tone text. */
189 unsigned long valuemask
;
192 Window root
= RootWindow (display
, DefaultScreen (display
));
195 * Calloc the XMenu structure and the initial pane.
197 menu
= (XMenu
*)calloc(1, sizeof(XMenu
));
199 _XMErrorCode
= XME_CALLOC
;
202 pane
= (XMPane
*)calloc(1, sizeof(XMPane
));
204 _XMErrorCode
= XME_CALLOC
;
209 * Create the XAssocTable
211 assoc_tab
= (XAssocTable
*)XCreateAssocTable(XASSOC_TABLE_SIZE
);
212 if(assoc_tab
== NULL
) {
213 _XMErrorCode
= XME_CREATE_ASSOC
;
218 * Set up the default environment name.
220 if (def_env
== NULL
|| *def_env
== '\0') def_env
= "XMenu";
223 * Set up internal fail-safe defaults.
226 reverse
= DEF_REVERSE
;
227 menu_style
= DEF_MENU_STYLE
;
228 menu_mode
= DEF_MENU_MODE
;
229 inact_pnum
= DEF_INACT_PNUM
;
231 p_style
= DEF_P_STYLE
;
232 p_spread
= DEF_P_SPREAD
;
233 p_fnt_name
= DEF_P_FNT_NAME
;
234 p_bdr_width
= DEF_P_BDR_WIDTH
;
236 s_style
= DEF_S_STYLE
;
237 s_spread
= DEF_S_SPREAD
;
238 s_fnt_name
= DEF_S_FNT_NAME
;
239 s_bdr_width
= DEF_S_BDR_WIDTH
;
242 * Get default values from X.
244 def_val
= x_get_resource_string ("menuFreeze", "MenuFreeze");
245 if (def_val
!= NULL
) {
246 if (strcmp(def_val
, "on") == 0) freeze
= 1;
247 else if (strcmp(def_val
, "off") == 0) freeze
= 0;
250 def_val
= x_get_resource_string ("menuReverseVideo", "MenuReverseVideo");
251 if (def_val
!= NULL
) {
252 if (strcmp(def_val
, "on") == 0) reverse
= 1;
253 else if (strcmp(def_val
, "off") == 0) reverse
= 0;
256 def_val
= x_get_resource_string ("menuStyle", "MenuStyle");
257 if (def_val
!= NULL
) {
258 if (strcmp(def_val
, "right_hand") == 0) menu_style
= RIGHT
;
259 else if (strcmp(def_val
, "left_hand") == 0) menu_style
= LEFT
;
260 else if (strcmp(def_val
, "center") == 0) menu_style
= CENTER
;
263 def_val
= x_get_resource_string ("menuMode", "MenuMode");
264 if (def_val
!= NULL
) {
265 if (strcmp(def_val
, "box") == 0) menu_mode
= BOX
;
266 else if (strcmp(def_val
, "invert") == 0) menu_mode
= INVERT
;
269 def_val
= x_get_resource_string ("menuMouse", "MenuMouse");
272 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
273 XAllocDisplayColor(display
,
274 DefaultColormap(display
, DefaultScreen(display
)),
276 &mouse_color
, &color_def
)
279 XAllocDisplayColor(display
,
280 DefaultColormap(display
, DefaultScreen(display
)),
282 &mouse_color
, &color_def
)
285 else if (XAllocDisplayColor(display
,
286 DefaultColormap(display
, DefaultScreen(display
)),
288 &mouse_color
, &color_def
)
293 def_val
= x_get_resource_string ("menuBackground", "MenuBackground");
296 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
297 XAllocDisplayColor(display
,
298 DefaultColormap(display
, DefaultScreen(display
)),
300 &bkgnd_color
, &color_def
)
303 XAllocDisplayColor(display
,
304 DefaultColormap(display
, DefaultScreen(display
)),
306 &bkgnd_color
, &color_def
)
308 else if (XAllocDisplayColor(display
,
309 DefaultColormap(display
, DefaultScreen(display
)),
311 &bkgnd_color
, &color_def
)
315 def_val
= x_get_resource_string ("menuInactivePattern", "MenuInactivePattern");
316 if (def_val
!= NULL
) {
317 if (strcmp(def_val
, "dimple1") == 0) inact_pnum
= 0;
318 else if (strcmp(def_val
, "dimple3") == 0) inact_pnum
= 1;
319 else if (strcmp(def_val
, "gray1") == 0) inact_pnum
= 2;
320 else if (strcmp(def_val
, "gray3") == 0) inact_pnum
= 3;
321 else if (strcmp(def_val
, "cross_weave") == 0) inact_pnum
= 4;
324 def_val
= x_get_resource_string ("paneStyle", "PaneStyle");
325 if (def_val
!= NULL
) {
326 if (strcmp(def_val
, "flush_left") == 0) p_style
= LEFT
;
327 else if (strcmp(def_val
, "flush_right") == 0) p_style
= RIGHT
;
328 else if (strcmp(def_val
, "center") == 0) p_style
= CENTER
;
331 def_val
= x_get_resource_string ("paneFont", "PaneFont");
332 if (def_val
!= NULL
) p_fnt_name
= def_val
;
334 def_val
= x_get_resource_string ("paneForeground", "PaneForeground");
337 DisplayCells(display
, DefaultScreen(display
)) > 2
339 XAllocDisplayColor(display
, DefaultColormap(display
,
340 DefaultScreen(display
)),
342 &p_frg_color
, &color_def
);
344 else if (reverse
) XAllocDisplayColor(display
,
345 DefaultColormap(display
,
346 DefaultScreen(display
)),
348 &p_frg_color
, &color_def
);
349 else XAllocDisplayColor(display
,
350 DefaultColormap(display
, DefaultScreen(display
)),
352 &p_frg_color
, &color_def
);
354 def_val
= x_get_resource_string ("paneBorder", "PaneBorder");
357 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
358 XAllocDisplayColor(display
,
359 DefaultColormap(display
, DefaultScreen(display
)),
361 &p_bdr_color
, &color_def
)
364 XAllocDisplayColor(display
,
365 DefaultColormap(display
, DefaultScreen(display
)),
367 &p_bdr_color
, &color_def
)
369 else XAllocDisplayColor(display
,
370 DefaultColormap(display
, DefaultScreen(display
)),
372 &p_bdr_color
, &color_def
);
374 def_val
= x_get_resource_string ("paneBorderWidth", "PaneBorderWidth");
375 if (def_val
!= NULL
) p_bdr_width
= atoi(def_val
);
377 def_val
= x_get_resource_string ("paneSpread", "PaneSpread");
378 if (def_val
!= NULL
) p_spread
= atof(def_val
);
380 def_val
= x_get_resource_string ("selectionStyle", "SelectionStyle");
381 if (def_val
!= NULL
) {
382 if (strcmp(def_val
, "flush_left") == 0) s_style
= LEFT
;
383 else if (strcmp(def_val
, "flush_right") == 0) s_style
= RIGHT
;
384 else if (strcmp(def_val
, "center") == 0) s_style
= CENTER
;
387 def_val
= x_get_resource_string ("selectionFont", "SelectionFont");
388 if (def_val
!= NULL
) s_fnt_name
= def_val
;
390 def_val
= x_get_resource_string ("selectionForeground", "SelectionForeground");
393 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
394 XAllocDisplayColor(display
,
395 DefaultColormap(display
, DefaultScreen(display
)),
397 &s_frg_color
, &color_def
)
400 XAllocDisplayColor(display
,
401 DefaultColormap(display
, DefaultScreen(display
)),
403 &s_frg_color
, &color_def
)
405 else if (XAllocDisplayColor(display
,
406 DefaultColormap(display
, DefaultScreen(display
)),
408 &s_frg_color
, &color_def
)
413 def_val
= x_get_resource_string ("selectionBorder", "SelectionBorder");
416 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
417 XAllocDisplayColor(display
,
418 DefaultColormap(display
, DefaultScreen(display
)),
420 &s_bdr_color
, &color_def
)
423 XAllocDisplayColor(display
,
424 DefaultColormap(display
, DefaultScreen(display
)),
426 &s_bdr_color
, &color_def
)
428 else if (XAllocDisplayColor(display
,
429 DefaultColormap(display
, DefaultScreen(display
)),
431 &s_bdr_color
, &color_def
)
435 def_val
= x_get_resource_string ("selectionBorderWidth", "SelectionBorderWidth");
436 if (def_val
!= NULL
) s_bdr_width
= atoi(def_val
);
438 def_val
= x_get_resource_string ("selectionSpread", "SelectionSpread");
439 if (def_val
!= NULL
) s_spread
= atof(def_val
);
442 * Create and store the inactive pattern pixmap.
451 data
= (char *)dimple1_bits
;
452 width
= dimple1_width
;
453 height
= dimple1_height
;
457 data
= (char *)dimple3_bits
;
458 width
= dimple3_width
;
459 height
= dimple3_height
;
463 data
= (char *)gray1_bits
;
465 height
= gray1_height
;
469 data
= (char *)gray3_bits
;
471 height
= gray3_height
;
475 data
= (char *)cross_weave_bits
;
476 width
= cross_weave_width
;
477 height
= cross_weave_height
;
483 _XMErrorCode
= XME_STORE_BITMAP
;
488 XCreatePixmapFromBitmapData
489 (display
, root
, data
, width
, height
,
490 p_frg_color
.pixel
, bkgnd_color
.pixel
,
491 DisplayPlanes (display
, DefaultScreen (display
)));
495 * Load the mouse cursor.
498 switch (menu_style
) {
500 cursor
= XCreateBitmapFromData(display
,
505 cursor_mask
= XCreateBitmapFromData(display
,
510 mouse_cursor
= XCreatePixmapCursor(
513 &mouse_color
, &bkgnd_color
,
517 XFreePixmap(display
, cursor
);
518 XFreePixmap(display
, cursor_mask
);
521 cursor
= XCreateBitmapFromData(display
,
526 cursor_mask
= XCreateBitmapFromData(display
,
530 right_ptrmsk_height
);
531 mouse_cursor
= XCreatePixmapCursor(
534 &mouse_color
, &bkgnd_color
,
538 XFreePixmap(display
, cursor
);
539 XFreePixmap(display
, cursor_mask
);
542 cursor
= XCreateBitmapFromData(display
,
547 cursor_mask
= XCreateBitmapFromData(display
,
552 mouse_cursor
= XCreatePixmapCursor(
555 &mouse_color
, &bkgnd_color
,
559 XFreePixmap(display
, cursor
);
560 XFreePixmap(display
, cursor_mask
);
563 /* Error! Invalid style parameter. */
564 _XMErrorCode
= XME_STYLE_PARAM
;
567 if (mouse_cursor
== _X_FAILURE
) {
568 _XMErrorCode
= XME_CREATE_CURSOR
;
573 * Open the pane and selection fonts.
576 p_fnt_info
= XLoadQueryFont(display
, p_fnt_name
);
577 if (p_fnt_info
== NULL
) {
578 _XMErrorCode
= XME_OPEN_FONT
;
583 s_fnt_info
= XLoadQueryFont(display
, s_fnt_name
);
584 if (s_fnt_info
== NULL
) {
585 _XMErrorCode
= XME_OPEN_FONT
;
589 * Calculate the fixed padding value in pixels for each font.
591 p_fnt_height
= p_fnt_info
->max_bounds
.ascent
+ p_fnt_info
->max_bounds
.descent
;
592 s_fnt_height
= s_fnt_info
->max_bounds
.ascent
+ s_fnt_info
->max_bounds
.descent
;
593 p_fnt_pad
= s_spread
* p_fnt_height
;
594 s_fnt_pad
= s_spread
* s_fnt_height
;
597 * Calculate fixed height and offset requirements.
599 flag_height
= p_fnt_height
+ (p_fnt_pad
<< 1);
602 p_y_off
= flag_height
+ p_bdr_width
;
603 p_x_off
= p_y_off
* p_spread
;
605 s_height
= s_fnt_height
+ (s_fnt_pad
<< 1) + (s_bdr_width
<< 1);
610 * Set up the pane list header.
614 pane
->type
= PL_HEADER
;
618 * Initialize the internal pane and selection creation queues.
623 * Create pane, active, and inactive GC's.
625 values
= (XGCValues
*)malloc(sizeof(XGCValues
));
626 valuemask
= (GCForeground
| GCBackground
| GCFont
| GCLineWidth
);
632 values
->foreground
= p_frg_color
.pixel
;
633 values
->background
= bkgnd_color
.pixel
;
634 values
->font
= p_fnt_info
->fid
;
635 values
->line_width
= p_bdr_width
;
643 * Then normal video selection.
646 values
->foreground
= s_frg_color
.pixel
;
647 values
->background
= bkgnd_color
.pixel
;
648 values
->font
= s_fnt_info
->fid
;
649 values
->line_width
= s_bdr_width
;
650 normal_select_GC
= XCreateGC(display
,
655 * Inverse video selection.
658 values
->foreground
= bkgnd_color
.pixel
;
659 values
->background
= s_frg_color
.pixel
;
660 values
->font
= s_fnt_info
->fid
;
661 values
->line_width
= s_bdr_width
;
662 inverse_select_GC
= XCreateGC(display
,
666 stipple_pixmap
= XCreateBitmapFromData(display
,
673 * Finally, inactive pane header and selections
675 valuemask
|= (GCFillStyle
| GCStipple
);
676 values
->foreground
= s_frg_color
.pixel
;
677 values
->background
= bkgnd_color
.pixel
;
678 values
->font
= s_fnt_info
->fid
;
679 values
->line_width
= s_bdr_width
;
680 values
->fill_style
= FillStippled
;
681 values
->stipple
= stipple_pixmap
;
683 inact_GC
= XCreateGC(display
,
688 valuemask
|= (GCGraphicsExposures
);
689 values
->graphics_exposures
= False
;
690 inact_GC_noexpose
= XCreateGC (display
,
696 * Construct the XMenu object.
698 /* -------------------- Menu data -------------------- */
699 menu
->menu_style
= menu_style
;
700 menu
->menu_mode
= menu_mode
;
701 menu
->freeze
= freeze
;
704 menu
->parent
= parent
;
707 menu
->mouse_cursor
= mouse_cursor
;
708 menu
->assoc_tab
= assoc_tab
;
710 /* -------------------- Pane window data -------------------- */
711 menu
->p_style
= p_style
;
712 menu
->p_events
= DEF_P_EVENTS
;
713 menu
->p_fnt_info
= p_fnt_info
;
714 menu
->p_fnt_pad
= p_fnt_pad
;
715 menu
->p_spread
= p_spread
;
716 menu
->p_bdr_width
= p_bdr_width
;
717 menu
->flag_height
= flag_height
;
719 menu
->p_height
= p_height
;
720 menu
->p_x_off
= p_x_off
;
721 menu
->p_y_off
= p_y_off
;
723 menu
->pane_GC
= pane_GC
;
726 /* -------------------- Selection window data -------------------- */
727 menu
->s_style
= s_style
;
728 menu
->s_events
= DEF_S_EVENTS
;
729 menu
->s_fnt_info
= s_fnt_info
;
730 menu
->s_fnt_pad
= s_fnt_pad
;
731 menu
->s_spread
= s_spread
;
732 menu
->s_bdr_width
= s_bdr_width
; /* unnecessary */
734 menu
->s_height
= s_height
;
735 menu
->s_x_off
= s_x_off
;
736 menu
->s_y_off
= s_y_off
;
738 menu
->normal_select_GC
= normal_select_GC
;
739 menu
->inverse_select_GC
= inverse_select_GC
;
740 menu
->inact_GC
= inact_GC
;
741 /* -------------------- Color data -------------------- */
742 menu
->p_bdr_color
= p_bdr_color
.pixel
;
743 menu
->s_bdr_color
= s_bdr_color
.pixel
;
744 menu
->p_frg_color
= p_frg_color
.pixel
;
745 menu
->s_frg_color
= s_frg_color
.pixel
;
746 menu
->bkgnd_color
= bkgnd_color
.pixel
;
747 /* -------------------- Pixmap data -------------------- */
748 menu
->p_bdr_pixmap
= None
;
749 menu
->s_bdr_pixmap
= None
;
750 menu
->p_frg_pixmap
= None
;
751 menu
->s_frg_pixmap
= None
;
752 menu
->bkgnd_pixmap
= None
;
753 menu
->inact_pixmap
= inact_bitmap
;
756 * Return the completed XMenu.
758 _XMErrorCode
= XME_NO_ERROR
;
762 /* arch-tag: 6945b7d2-3b13-40b9-8b6e-56b1b20f3463
763 (do not change this comment) */