1 /* $Header: /cvs/emacs/oldXMenu/Create.c,v 1.1 1999/10/03 19:34:56 fx Exp $ */
2 /* Copyright Massachusetts Institute of Technology 1985 */
7 * XMenu: MIT Project Athena, X Window system menu package
9 * XMenuCreate - Creates an X window system menu object.
11 * Author: Tony Della Fera, DEC
20 #ifdef EMACS_BITMAP_FILES
21 #include "../src/bitmaps/dimple1.xbm"
22 #include "../src/bitmaps/dimple3.xbm"
23 #include "../src/bitmaps/gray1.xbm"
24 #include "../src/bitmaps/gray3.xbm"
25 #include "../src/bitmaps/crosswv.xbm"
27 #include "../src/bitmaps/leftptr.xbm"
28 #include "../src/bitmaps/leftpmsk.xbm"
29 #include "../src/bitmaps/rtptr.xbm"
30 #include "../src/bitmaps/rtpmsk.xbm"
31 #include "../src/bitmaps/cntrptr.xbm"
32 #include "../src/bitmaps/cntrpmsk.xbm"
33 #include "../src/bitmaps/stipple.xbm"
38 #include <X11/bitmaps/dimple1>
39 #include <X11/bitmaps/dimple3>
40 #include <X11/bitmaps/gray1>
41 #include <X11/bitmaps/gray3>
42 #include <X11/bitmaps/cross_weave>
44 #include <X11/bitmaps/left_ptr>
45 #include <X11/bitmaps/left_ptrmsk>
46 #include <X11/bitmaps/right_ptr>
47 #include <X11/bitmaps/right_ptrmsk>
48 #include <X11/bitmaps/cntr_ptr>
49 #include <X11/bitmaps/cntr_ptrmsk>
50 #include <X11/bitmaps/stipple>
54 #include "[-.src.bitmaps]dimple1.xbm"
55 #include "[-.src.bitmaps]dimple3.xbm"
56 #include "[-.src.bitmaps]gray1.xbm"
57 #include "[-.src.bitmaps]gray3.xbm"
58 #include "[-.src.bitmaps]crosswv.xbm"
60 #include "[-.src.bitmaps]leftptr.xbm"
61 #include "[-.src.bitmaps]leftpmsk.xbm"
62 #include "[-.src.bitmaps]rtptr.xbm"
63 #include "[-.src.bitmaps]rtpmsk.xbm"
64 #include "[-.src.bitmaps]cntrptr.xbm"
65 #include "[-.src.bitmaps]cntrpmsk.xbm"
66 #include "[-.src.bitmaps]stipple.xbm"
69 #endif /* not EMACS_BITMAP_FILES */
73 #define DEF_MENU_STYLE LEFT
74 #define DEF_MENU_MODE BOX
75 #define DEF_INACT_PNUM 3
76 #define MAX_INACT_PNUM 4
78 #define DEF_P_STYLE CENTER
80 #define DEF_P_EVENTS (EnterWindowMask | ExposureMask)
81 #define DEF_P_FNT_NAME "fixed"
82 #define DEF_P_SPREAD 0.5
83 #define DEF_P_BDR_WIDTH 2
85 #define DEF_S_STYLE LEFT
86 #define DEF_S_EVENTS (EnterWindowMask | LeaveWindowMask)
87 #define DEF_S_FNT_NAME "fixed"
88 #define DEF_S_SPREAD 0.10
89 #define DEF_S_BDR_WIDTH 1
91 #define XASSOC_TABLE_SIZE 64
93 #define TILE_BUF_SIZE 5
97 char *x_get_resource_string ();
102 XAllocDisplayColor(display
, map
, colorName
, color
, junk
)
109 return (colorName
!=0 &&
110 XParseColor(display
, map
, colorName
, color
) &&
111 XAllocColor(display
, map
, color
));
116 XMenuCreate(display
, parent
, def_env
)
117 Display
*display
; /* ID of previously opened display */
118 Window parent
; /* Window ID of the menu's parent window. */
119 register char *def_env
; /* X Defaults program environment name. */
121 register int i
; /* Loop counter. */
122 register int j
; /* Loop counter. */
123 register char *def_val
; /* X Default value temp variable. */
125 register XMenu
*menu
; /* Pointer to the new menu. */
126 XMStyle menu_style
; /* Menu display style. */
127 XMMode menu_mode
; /* Menu display mode. */
128 XMPane
*pane
; /* Pane list header. */
129 XAssocTable
*assoc_tab
; /* XAssocTable pointer. */
131 int freeze
; /* Freeze server mode. */
132 int reverse
; /* Reverse video mode. */
134 XMStyle p_style
; /* Pane display style. */
135 char *p_fnt_name
; /* Flag font name. */
136 XFontStruct
*p_fnt_info
; /* Flag font structure */
137 int p_fnt_pad
; /* Flag font padding in pixels. */
138 double p_spread
; /* Pane spread in flag height fractions. */
139 int p_fnt_height
; /* Pane character height. */
140 int p_bdr_width
; /* Pane border width. */
141 int flag_height
; /* Flag window height. */
142 int p_height
; /* Pane window height. */
143 int p_x_off
; /* Pane X offset. */
144 int p_y_off
; /* Pane Y offset. */
145 GC pane_GC
; /* Pane graphics context. */
147 XMStyle s_style
; /* Selection display style. */
148 char *s_fnt_name
; /* Selection font name. */
149 XFontStruct
*s_fnt_info
; /* Selection font structure. */
150 int s_fnt_pad
; /* Selection font padding in pixels. */
151 int s_fnt_height
; /* Selection font character height */
152 double s_spread
; /* Select spread in line height fractions. */
153 int s_bdr_width
; /* Highlight border width. */
154 int s_height
; /* Selection window height. */
155 int s_x_off
; /* Selection window X offset. */
156 int s_y_off
; /* Selection window Y offset. */
157 GC normal_select_GC
; /* GC used for normal video selection. */
158 GC inverse_select_GC
; /* GC used for inverse video selection. */
159 GC inact_GC
; /* GC for inactive pane header and */
161 GC inact_GC_noexpose
;
163 XColor color_def
; /* Temp color definition holder. */
164 XColor screen_def
; /* Temp screen color definition holder */
165 XColor p_bdr_color
; /* Color of border. */
166 XColor s_bdr_color
; /* Color of highlight. */
167 XColor p_frg_color
; /* Color of pane foreground color. */
168 XColor s_frg_color
; /* Color of selection foreground. */
169 XColor bkgnd_color
; /* Color of background.. */
170 XColor mouse_color
; /* Color of mouse cursor. */
171 Cursor mouse_cursor
; /* Mouse cursor. */
172 Pixmap inact_bitmap
; /* Menu inactive pixmap. */
174 int inact_pnum
; /* Inactive background pattern number. */
176 Pixel p_bdr_pixel
; /* Pane border pixel. */
177 Pixel s_bdr_pixel
; /* Selection border pixel. */
178 Pixel p_frg_pixel
; /* Pane foreground pixel. */
179 Pixel s_frg_pixel
; /* Selection foreground pixel. */
180 Pixel bkgnd_pixel
; /* Menu background pixel. */
185 int status
; /* Return code from XReadBitmapFile. */
187 Pixmap cursor
; /* Cursor pixmap holder. */
188 Pixmap cursor_mask
; /* Cursor mask pixmap holder. */
189 Pixmap stipple_pixmap
; /* Stipple mask for half-tone text. */
190 unsigned long valuemask
;
193 Window root
= RootWindow (display
, DefaultScreen (display
));
196 * Calloc the XMenu structure and the initial pane.
198 menu
= (XMenu
*)calloc(1, sizeof(XMenu
));
200 _XMErrorCode
= XME_CALLOC
;
203 pane
= (XMPane
*)calloc(1, sizeof(XMPane
));
205 _XMErrorCode
= XME_CALLOC
;
210 * Create the XAssocTable
212 assoc_tab
= (XAssocTable
*)XCreateAssocTable(XASSOC_TABLE_SIZE
);
213 if(assoc_tab
== NULL
) {
214 _XMErrorCode
= XME_CREATE_ASSOC
;
219 * Set up the default environment name.
221 if (def_env
== NULL
|| *def_env
== '\0') def_env
= "XMenu";
224 * Set up internal fail-safe defaults.
227 reverse
= DEF_REVERSE
;
228 menu_style
= DEF_MENU_STYLE
;
229 menu_mode
= DEF_MENU_MODE
;
230 inact_pnum
= DEF_INACT_PNUM
;
232 p_style
= DEF_P_STYLE
;
233 p_spread
= DEF_P_SPREAD
;
234 p_fnt_name
= DEF_P_FNT_NAME
;
235 p_bdr_width
= DEF_P_BDR_WIDTH
;
237 s_style
= DEF_S_STYLE
;
238 s_spread
= DEF_S_SPREAD
;
239 s_fnt_name
= DEF_S_FNT_NAME
;
240 s_bdr_width
= DEF_S_BDR_WIDTH
;
243 * Get default values from X.
245 def_val
= x_get_resource_string ("menuFreeze", "MenuFreeze");
246 if (def_val
!= NULL
) {
247 if (strcmp(def_val
, "on") == 0) freeze
= 1;
248 else if (strcmp(def_val
, "off") == 0) freeze
= 0;
251 def_val
= x_get_resource_string ("menuReverseVideo", "MenuReverseVideo");
252 if (def_val
!= NULL
) {
253 if (strcmp(def_val
, "on") == 0) reverse
= 1;
254 else if (strcmp(def_val
, "off") == 0) reverse
= 0;
257 def_val
= x_get_resource_string ("menuStyle", "MenuStyle");
258 if (def_val
!= NULL
) {
259 if (strcmp(def_val
, "right_hand") == 0) menu_style
= RIGHT
;
260 else if (strcmp(def_val
, "left_hand") == 0) menu_style
= LEFT
;
261 else if (strcmp(def_val
, "center") == 0) menu_style
= CENTER
;
264 def_val
= x_get_resource_string ("menuMode", "MenuMode");
265 if (def_val
!= NULL
) {
266 if (strcmp(def_val
, "box") == 0) menu_mode
= BOX
;
267 else if (strcmp(def_val
, "invert") == 0) menu_mode
= INVERT
;
270 def_val
= x_get_resource_string ("menuMouse", "MenuMouse");
273 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
274 XAllocDisplayColor(display
,
275 DefaultColormap(display
, DefaultScreen(display
)),
277 &mouse_color
, &color_def
)
280 XAllocDisplayColor(display
,
281 DefaultColormap(display
, DefaultScreen(display
)),
283 &mouse_color
, &color_def
)
286 else if (XAllocDisplayColor(display
,
287 DefaultColormap(display
, DefaultScreen(display
)),
289 &mouse_color
, &color_def
)
294 def_val
= x_get_resource_string ("menuBackground", "MenuBackground");
297 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
298 XAllocDisplayColor(display
,
299 DefaultColormap(display
, DefaultScreen(display
)),
301 &bkgnd_color
, &color_def
)
304 XAllocDisplayColor(display
,
305 DefaultColormap(display
, DefaultScreen(display
)),
307 &bkgnd_color
, &color_def
)
309 else if (XAllocDisplayColor(display
,
310 DefaultColormap(display
, DefaultScreen(display
)),
312 &bkgnd_color
, &color_def
)
316 def_val
= x_get_resource_string ("menuInactivePattern", "MenuInactivePattern");
317 if (def_val
!= NULL
) {
318 if (strcmp(def_val
, "dimple1") == 0) inact_pnum
= 0;
319 else if (strcmp(def_val
, "dimple3") == 0) inact_pnum
= 1;
320 else if (strcmp(def_val
, "gray1") == 0) inact_pnum
= 2;
321 else if (strcmp(def_val
, "gray3") == 0) inact_pnum
= 3;
322 else if (strcmp(def_val
, "cross_weave") == 0) inact_pnum
= 4;
325 def_val
= x_get_resource_string ("paneStyle", "PaneStyle");
326 if (def_val
!= NULL
) {
327 if (strcmp(def_val
, "flush_left") == 0) p_style
= LEFT
;
328 else if (strcmp(def_val
, "flush_right") == 0) p_style
= RIGHT
;
329 else if (strcmp(def_val
, "center") == 0) p_style
= CENTER
;
332 def_val
= x_get_resource_string ("paneFont", "PaneFont");
333 if (def_val
!= NULL
) p_fnt_name
= def_val
;
335 def_val
= x_get_resource_string ("paneForeground", "PaneForeground");
338 DisplayCells(display
, DefaultScreen(display
)) > 2
340 XAllocDisplayColor(display
, DefaultColormap(display
,
341 DefaultScreen(display
)),
343 &p_frg_color
, &color_def
);
345 else if (reverse
) XAllocDisplayColor(display
,
346 DefaultColormap(display
,
347 DefaultScreen(display
)),
349 &p_frg_color
, &color_def
);
350 else XAllocDisplayColor(display
,
351 DefaultColormap(display
, DefaultScreen(display
)),
353 &p_frg_color
, &color_def
);
355 def_val
= x_get_resource_string ("paneBorder", "PaneBorder");
358 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
359 XAllocDisplayColor(display
,
360 DefaultColormap(display
, DefaultScreen(display
)),
362 &p_bdr_color
, &color_def
)
365 XAllocDisplayColor(display
,
366 DefaultColormap(display
, DefaultScreen(display
)),
368 &p_bdr_color
, &color_def
)
370 else XAllocDisplayColor(display
,
371 DefaultColormap(display
, DefaultScreen(display
)),
373 &p_bdr_color
, &color_def
);
375 def_val
= x_get_resource_string ("paneBorderWidth", "PaneBorderWidth");
376 if (def_val
!= NULL
) p_bdr_width
= atoi(def_val
);
378 def_val
= x_get_resource_string ("paneSpread", "PaneSpread");
379 if (def_val
!= NULL
) p_spread
= atof(def_val
);
381 def_val
= x_get_resource_string ("selectionStyle", "SelectionStyle");
382 if (def_val
!= NULL
) {
383 if (strcmp(def_val
, "flush_left") == 0) s_style
= LEFT
;
384 else if (strcmp(def_val
, "flush_right") == 0) s_style
= RIGHT
;
385 else if (strcmp(def_val
, "center") == 0) s_style
= CENTER
;
388 def_val
= x_get_resource_string ("selectionFont", "SelectionFont");
389 if (def_val
!= NULL
) s_fnt_name
= def_val
;
391 def_val
= x_get_resource_string ("selectionForeground", "SelectionForeground");
394 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
395 XAllocDisplayColor(display
,
396 DefaultColormap(display
, DefaultScreen(display
)),
398 &s_frg_color
, &color_def
)
401 XAllocDisplayColor(display
,
402 DefaultColormap(display
, DefaultScreen(display
)),
404 &s_frg_color
, &color_def
)
406 else if (XAllocDisplayColor(display
,
407 DefaultColormap(display
, DefaultScreen(display
)),
409 &s_frg_color
, &color_def
)
414 def_val
= x_get_resource_string ("selectionBorder", "SelectionBorder");
417 DisplayCells(display
, DefaultScreen(display
)) > 2 &&
418 XAllocDisplayColor(display
,
419 DefaultColormap(display
, DefaultScreen(display
)),
421 &s_bdr_color
, &color_def
)
424 XAllocDisplayColor(display
,
425 DefaultColormap(display
, DefaultScreen(display
)),
427 &s_bdr_color
, &color_def
)
429 else if (XAllocDisplayColor(display
,
430 DefaultColormap(display
, DefaultScreen(display
)),
432 &s_bdr_color
, &color_def
)
436 def_val
= x_get_resource_string ("selectionBorderWidth", "SelectionBorderWidth");
437 if (def_val
!= NULL
) s_bdr_width
= atoi(def_val
);
439 def_val
= x_get_resource_string ("selectionSpread", "SelectionSpread");
440 if (def_val
!= NULL
) s_spread
= atof(def_val
);
443 * Create and store the inactive pattern pixmap.
452 data
= (char *)dimple1_bits
;
453 width
= dimple1_width
;
454 height
= dimple1_height
;
458 data
= (char *)dimple3_bits
;
459 width
= dimple3_width
;
460 height
= dimple3_height
;
464 data
= (char *)gray1_bits
;
466 height
= gray1_height
;
470 data
= (char *)gray3_bits
;
472 height
= gray3_height
;
476 data
= (char *)cross_weave_bits
;
477 width
= cross_weave_width
;
478 height
= cross_weave_height
;
484 _XMErrorCode
= XME_STORE_BITMAP
;
489 XCreatePixmapFromBitmapData
490 (display
, root
, data
, width
, height
,
491 p_frg_color
.pixel
, bkgnd_color
.pixel
,
492 DisplayPlanes (display
, DefaultScreen (display
)));
496 * Load the mouse cursor.
499 switch (menu_style
) {
501 cursor
= XCreateBitmapFromData(display
,
506 cursor_mask
= XCreateBitmapFromData(display
,
511 mouse_cursor
= XCreatePixmapCursor(
514 &mouse_color
, &bkgnd_color
,
518 XFreePixmap(display
, cursor
);
519 XFreePixmap(display
, cursor_mask
);
522 cursor
= XCreateBitmapFromData(display
,
527 cursor_mask
= XCreateBitmapFromData(display
,
531 right_ptrmsk_height
);
532 mouse_cursor
= XCreatePixmapCursor(
535 &mouse_color
, &bkgnd_color
,
539 XFreePixmap(display
, cursor
);
540 XFreePixmap(display
, cursor_mask
);
543 cursor
= XCreateBitmapFromData(display
,
548 cursor_mask
= XCreateBitmapFromData(display
,
553 mouse_cursor
= XCreatePixmapCursor(
556 &mouse_color
, &bkgnd_color
,
560 XFreePixmap(display
, cursor
);
561 XFreePixmap(display
, cursor_mask
);
564 /* Error! Invalid style parameter. */
565 _XMErrorCode
= XME_STYLE_PARAM
;
568 if (mouse_cursor
== _X_FAILURE
) {
569 _XMErrorCode
= XME_CREATE_CURSOR
;
574 * Open the pane and selection fonts.
577 p_fnt_info
= XLoadQueryFont(display
, p_fnt_name
);
578 if (p_fnt_info
== NULL
) {
579 _XMErrorCode
= XME_OPEN_FONT
;
584 s_fnt_info
= XLoadQueryFont(display
, s_fnt_name
);
585 if (s_fnt_info
== NULL
) {
586 _XMErrorCode
= XME_OPEN_FONT
;
590 * Calculate the fixed padding value in pixels for each font.
592 p_fnt_height
= p_fnt_info
->max_bounds
.ascent
+ p_fnt_info
->max_bounds
.descent
;
593 s_fnt_height
= s_fnt_info
->max_bounds
.ascent
+ s_fnt_info
->max_bounds
.descent
;
594 p_fnt_pad
= s_spread
* p_fnt_height
;
595 s_fnt_pad
= s_spread
* s_fnt_height
;
598 * Calculate fixed height and offset requirements.
600 flag_height
= p_fnt_height
+ (p_fnt_pad
<< 1);
603 p_y_off
= flag_height
+ p_bdr_width
;
604 p_x_off
= p_y_off
* p_spread
;
606 s_height
= s_fnt_height
+ (s_fnt_pad
<< 1) + (s_bdr_width
<< 1);
611 * Set up the pane list header.
615 pane
->type
= PL_HEADER
;
619 * Initialize the internal pane and selection creation queues.
624 * Create pane, active, and inactive GC's.
626 values
= (XGCValues
*)malloc(sizeof(XGCValues
));
627 valuemask
= (GCForeground
| GCBackground
| GCFont
| GCLineWidth
);
633 values
->foreground
= p_frg_color
.pixel
;
634 values
->background
= bkgnd_color
.pixel
;
635 values
->font
= p_fnt_info
->fid
;
636 values
->line_width
= p_bdr_width
;
644 * Then normal video selection.
647 values
->foreground
= s_frg_color
.pixel
;
648 values
->background
= bkgnd_color
.pixel
;
649 values
->font
= s_fnt_info
->fid
;
650 values
->line_width
= s_bdr_width
;
651 normal_select_GC
= XCreateGC(display
,
656 * Inverse video selection.
659 values
->foreground
= bkgnd_color
.pixel
;
660 values
->background
= s_frg_color
.pixel
;
661 values
->font
= s_fnt_info
->fid
;
662 values
->line_width
= s_bdr_width
;
663 inverse_select_GC
= XCreateGC(display
,
667 stipple_pixmap
= XCreateBitmapFromData(display
,
674 * Finally, inactive pane header and selections
676 valuemask
|= (GCFillStyle
| GCStipple
);
677 values
->foreground
= s_frg_color
.pixel
;
678 values
->background
= bkgnd_color
.pixel
;
679 values
->font
= s_fnt_info
->fid
;
680 values
->line_width
= s_bdr_width
;
681 values
->fill_style
= FillStippled
;
682 values
->stipple
= stipple_pixmap
;
684 inact_GC
= XCreateGC(display
,
689 valuemask
|= (GCGraphicsExposures
);
690 values
->graphics_exposures
= False
;
691 inact_GC_noexpose
= XCreateGC (display
,
697 * Construct the XMenu object.
699 /* -------------------- Menu data -------------------- */
700 menu
->menu_style
= menu_style
;
701 menu
->menu_mode
= menu_mode
;
702 menu
->freeze
= freeze
;
705 menu
->parent
= parent
;
708 menu
->mouse_cursor
= mouse_cursor
;
709 menu
->assoc_tab
= assoc_tab
;
711 /* -------------------- Pane window data -------------------- */
712 menu
->p_style
= p_style
;
713 menu
->p_events
= DEF_P_EVENTS
;
714 menu
->p_fnt_info
= p_fnt_info
;
715 menu
->p_fnt_pad
= p_fnt_pad
;
716 menu
->p_spread
= p_spread
;
717 menu
->p_bdr_width
= p_bdr_width
;
718 menu
->flag_height
= flag_height
;
720 menu
->p_height
= p_height
;
721 menu
->p_x_off
= p_x_off
;
722 menu
->p_y_off
= p_y_off
;
724 menu
->pane_GC
= pane_GC
;
727 /* -------------------- Selection window data -------------------- */
728 menu
->s_style
= s_style
;
729 menu
->s_events
= DEF_S_EVENTS
;
730 menu
->s_fnt_info
= s_fnt_info
;
731 menu
->s_fnt_pad
= s_fnt_pad
;
732 menu
->s_spread
= s_spread
;
733 menu
->s_bdr_width
= s_bdr_width
; /* unnecessary */
735 menu
->s_height
= s_height
;
736 menu
->s_x_off
= s_x_off
;
737 menu
->s_y_off
= s_y_off
;
739 menu
->normal_select_GC
= normal_select_GC
;
740 menu
->inverse_select_GC
= inverse_select_GC
;
741 menu
->inact_GC
= inact_GC
;
742 /* -------------------- Color data -------------------- */
743 menu
->p_bdr_color
= p_bdr_color
.pixel
;
744 menu
->s_bdr_color
= s_bdr_color
.pixel
;
745 menu
->p_frg_color
= p_frg_color
.pixel
;
746 menu
->s_frg_color
= s_frg_color
.pixel
;
747 menu
->bkgnd_color
= bkgnd_color
.pixel
;
748 /* -------------------- Pixmap data -------------------- */
749 menu
->p_bdr_pixmap
= None
;
750 menu
->s_bdr_pixmap
= None
;
751 menu
->p_frg_pixmap
= None
;
752 menu
->s_frg_pixmap
= None
;
753 menu
->bkgnd_pixmap
= None
;
754 menu
->inact_pixmap
= inact_bitmap
;
757 * Return the completed XMenu.
759 _XMErrorCode
= XME_NO_ERROR
;