1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved		by Bram Moolenaar
4  *				GUI/Motif support by Robert Webb
5  *
6  * Do ":help uganda"  in Vim to read copying and usage conditions.
7  * Do ":help credits" in Vim to see a list of people who contributed.
8  * See README.txt for an overview of the Vim source code.
9  */
10 
11 #include "vim.h"
12 
13 #include <Xm/Form.h>
14 #include <Xm/RowColumn.h>
15 #include <Xm/PushB.h>
16 #include <Xm/Text.h>
17 #include <Xm/TextF.h>
18 #include <Xm/Separator.h>
19 #include <Xm/Label.h>
20 #include <Xm/CascadeB.h>
21 #include <Xm/ScrollBar.h>
22 #include <Xm/MenuShell.h>
23 #include <Xm/DrawingA.h>
24 #if (XmVersion >= 1002)
25 # include <Xm/RepType.h>
26 #endif
27 #include <Xm/Frame.h>
28 #include <Xm/LabelG.h>
29 #include <Xm/ToggleBG.h>
30 #include <Xm/SeparatoG.h>
31 #include <Xm/XmP.h>
32 
33 #include <X11/keysym.h>
34 #include <X11/Xatom.h>
35 #include <X11/StringDefs.h>
36 #include <X11/Intrinsic.h>
37 #ifdef HAVE_X11_XPM_H
38 # if defined(VMS)
39 #  include <xpm.h>
40 # else
41 #  include <X11/xpm.h>
42 # endif
43 #else
44 # ifdef HAVE_XM_XPMP_H
45 #  include <Xm/XpmP.h>
46 # endif
47 #endif
48 #ifdef HAVE_XM_NOTEBOOK_H
49 # include <Xm/Notebook.h>
50 #endif
51 
52 #include "gui_xmebw.h"	// for our Enhanced Button Widget
53 
54 #if defined(FEAT_GUI_DIALOG) && defined(HAVE_XPM)
55 # include "../pixmaps/alert.xpm"
56 # include "../pixmaps/error.xpm"
57 # include "../pixmaps/generic.xpm"
58 # include "../pixmaps/info.xpm"
59 # include "../pixmaps/quest.xpm"
60 #endif
61 
62 #define MOTIF_POPUP
63 
64 extern Widget vimShell;
65 
66 static Widget vimForm;
67 static Widget textAreaForm;
68 Widget textArea;
69 #ifdef FEAT_TOOLBAR
70 static Widget toolBarFrame;
71 static Widget toolBar;
72 #endif
73 #ifdef FEAT_GUI_TABLINE
74 static Widget	tabLine;
75 static Widget	tabLine_menu = 0;
76 static int	showing_tabline = 0;
77 #endif
78 #ifdef FEAT_FOOTER
79 static Widget footer;
80 #endif
81 #ifdef FEAT_MENU
82 # if (XmVersion >= 1002)
83 // remember the last set value for the tearoff item
84 static int tearoff_val = (int)XmTEAR_OFF_ENABLED;
85 # endif
86 static Widget menuBar;
87 #endif
88 
89 #ifdef FEAT_TOOLBAR
90 # ifdef FEAT_FOOTER
91 static void toolbarbutton_enter_cb(Widget, XtPointer, XEvent *, Boolean *);
92 static void toolbarbutton_leave_cb(Widget, XtPointer, XEvent *, Boolean *);
93 # endif
94 static void reset_focus(void);
95 #endif
96 
97 static void gui_motif_menu_colors(Widget id);
98 static void gui_motif_scroll_colors(Widget id);
99 
100 #if (XmVersion >= 1002)
101 # define STRING_TAG  XmFONTLIST_DEFAULT_TAG
102 #else
103 # define STRING_TAG  XmSTRING_DEFAULT_CHARSET
104 #endif
105 
106 /*
107  * Call-back routines.
108  */
109 
110     static void
scroll_cb(Widget w UNUSED,XtPointer client_data,XtPointer call_data)111 scroll_cb(Widget w UNUSED, XtPointer client_data, XtPointer call_data)
112 {
113     scrollbar_T *sb;
114     long	value;
115     int		dragging;
116 
117     sb = gui_find_scrollbar((long)client_data);
118 
119     value = ((XmScrollBarCallbackStruct *)call_data)->value;
120     dragging = (((XmScrollBarCallbackStruct *)call_data)->reason ==
121 							      (int)XmCR_DRAG);
122     gui_drag_scrollbar(sb, value, dragging);
123 }
124 
125 #ifdef FEAT_GUI_TABLINE
126     static void
tabline_cb(Widget w UNUSED,XtPointer client_data UNUSED,XtPointer call_data)127 tabline_cb(
128     Widget	w UNUSED,
129     XtPointer	client_data UNUSED,
130     XtPointer	call_data)
131 {
132     XmNotebookCallbackStruct *nptr;
133 
134     nptr = (XmNotebookCallbackStruct *)call_data;
135     if (nptr->reason != (int)XmCR_NONE)
136 	send_tabline_event(nptr->page_number);
137 }
138 
139     static void
tabline_button_cb(Widget w,XtPointer client_data UNUSED,XtPointer call_data UNUSED)140 tabline_button_cb(
141     Widget	w,
142     XtPointer	client_data UNUSED,
143     XtPointer	call_data UNUSED)
144 {
145     int		cmd, tab_idx;
146 
147     XtVaGetValues(w, XmNuserData, &cmd, NULL);
148     XtVaGetValues(tabLine_menu, XmNuserData, &tab_idx, NULL);
149 
150     send_tabline_menu_event(tab_idx, cmd);
151 }
152 
153 /*
154  * Tabline single mouse click timeout handler
155  */
156     static void
motif_tabline_timer_cb(XtPointer timed_out,XtIntervalId * interval_id UNUSED)157 motif_tabline_timer_cb (
158     XtPointer		timed_out,
159     XtIntervalId	*interval_id UNUSED)
160 {
161     *((int *)timed_out) = TRUE;
162 }
163 
164 /*
165  * check if the tabline tab scroller is clicked
166  */
167     static int
tabline_scroller_clicked(char * scroller_name,XButtonPressedEvent * event)168 tabline_scroller_clicked(
169     char		*scroller_name,
170     XButtonPressedEvent *event)
171 {
172     Widget	tab_scroll_w;
173     Position	pos_x, pos_y;
174     Dimension	width, height;
175 
176     tab_scroll_w = XtNameToWidget(tabLine, scroller_name);
177     if (tab_scroll_w != (Widget)0) {
178 	XtVaGetValues(tab_scroll_w, XmNx, &pos_x, XmNy, &pos_y, XmNwidth,
179 		      &width, XmNheight, &height, NULL);
180 	if (pos_x >= 0) {
181 	    // Tab scroller (next) is visible
182 	    if ((event->x >= pos_x) && (event->x <= pos_x + width) &&
183 		(event->y >= pos_y) && (event->y <= pos_y + height)) {
184 		// Clicked on the scroller
185 		return TRUE;
186 	    }
187 	}
188     }
189     return FALSE;
190 }
191 
192     static void
tabline_menu_cb(Widget w,XtPointer closure UNUSED,XEvent * e,Boolean * continue_dispatch UNUSED)193 tabline_menu_cb(
194     Widget	w,
195     XtPointer	closure UNUSED,
196     XEvent	*e,
197     Boolean	*continue_dispatch UNUSED)
198 {
199     Widget			tab_w;
200     XButtonPressedEvent		*event;
201     int				tab_idx = 0;
202     WidgetList			children;
203     Cardinal			numChildren;
204     static XtIntervalId		timer = (XtIntervalId)0;
205     static int			timed_out = TRUE;
206 
207     event = (XButtonPressedEvent *)e;
208 
209     if (event->button == Button1)
210     {
211 	if (tabline_scroller_clicked("MajorTabScrollerNext", event)
212 	    || tabline_scroller_clicked("MajorTabScrollerPrevious", event))
213 	    return;
214 
215 	if (!timed_out)
216 	{
217 	    XtRemoveTimeOut(timer);
218 	    timed_out = TRUE;
219 
220 	    /*
221 	     * Double click on the tabline gutter, add a new tab
222 	     */
223 	    send_tabline_menu_event(0, TABLINE_MENU_NEW);
224 	}
225 	else
226 	{
227 	    /*
228 	     * Single click on the tabline gutter, start a timer to check
229 	     * for double clicks
230 	     */
231 	    timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
232 				    motif_tabline_timer_cb, &timed_out);
233 	    timed_out = FALSE;
234 	}
235 	return;
236     }
237 
238     if (event->button != Button3)
239 	return;
240 
241     // When ignoring events don't show the menu.
242     if (hold_gui_events
243 # ifdef FEAT_CMDWIN
244 	    || cmdwin_type != 0
245 # endif
246        )
247 	return;
248 
249     if (event->subwindow != None)
250     {
251 	tab_w = XtWindowToWidget(XtDisplay(w), event->subwindow);
252 	// LINTED: avoid warning: dubious operation on enum
253 	if (tab_w != (Widget)0 && XmIsPushButton(tab_w))
254 	    XtVaGetValues(tab_w, XmNpageNumber, &tab_idx, NULL);
255     }
256 
257     XtVaSetValues(tabLine_menu, XmNuserData, tab_idx, NULL);
258     XtVaGetValues(tabLine_menu, XmNchildren, &children, XmNnumChildren,
259 		  &numChildren, NULL);
260     XtManageChildren(children, numChildren);
261     XmMenuPosition(tabLine_menu, (XButtonPressedEvent *)e) ;
262     XtManageChild(tabLine_menu);
263 }
264 
265     static void
tabline_balloon_cb(BalloonEval * beval,int state UNUSED)266 tabline_balloon_cb(BalloonEval *beval, int state UNUSED)
267 {
268     int		nr;
269     tabpage_T	*tp;
270 
271     if (beval->target == (Widget)0)
272 	return;
273 
274     XtVaGetValues(beval->target, XmNpageNumber, &nr, NULL);
275     tp = find_tabpage(nr);
276     if (tp == NULL)
277 	return;
278 
279     get_tabline_label(tp, TRUE);
280     gui_mch_post_balloon(beval, NameBuff);
281 }
282 
283 #endif
284 
285 /*
286  * End of call-back routines
287  */
288 
289 /*
290  * Implement three dimensional shading of insensitive labels.
291  * By Marcin Dalecki.
292  */
293 
294 #include <Xm/XmP.h>
295 #include <Xm/LabelP.h>
296 
297 static XtExposeProc old_label_expose = NULL;
298 
299     static void
label_expose(Widget _w,XEvent * _event,Region _region)300 label_expose(Widget _w, XEvent *_event, Region _region)
301 {
302     GC		    insensitiveGC;
303     XmLabelWidget   lw = (XmLabelWidget)_w;
304     unsigned char   label_type = (int)XmSTRING;
305 
306     XtVaGetValues(_w, XmNlabelType, &label_type, (XtPointer)0);
307 
308     if (XtIsSensitive(_w) || label_type != (int)XmSTRING)
309 	(*old_label_expose)(_w, _event, _region);
310     else
311     {
312 	XGCValues   values;
313 	XtGCMask    mask;
314 	XtGCMask    dynamic;
315 	XFontStruct *fs;
316 
317 	_XmFontListGetDefaultFont(lw->label.font, &fs);
318 
319 	// FIXME: we should be doing the whole drawing ourself here.
320 	insensitiveGC = lw->label.insensitive_GC;
321 
322 	mask = GCForeground | GCBackground | GCGraphicsExposures;
323 	dynamic = GCClipMask | GCClipXOrigin | GCClipYOrigin;
324 	values.graphics_exposures = False;
325 
326 	if (fs != 0)
327 	{
328 	    mask |= GCFont;
329 	    values.font = fs->fid;
330 	}
331 
332 	if (lw->primitive.top_shadow_pixmap != None
333 		&& lw->primitive.top_shadow_pixmap != XmUNSPECIFIED_PIXMAP)
334 	{
335 	    mask |= GCFillStyle | GCTile;
336 	    values.fill_style = FillTiled;
337 	    values.tile = lw->primitive.top_shadow_pixmap;
338 	}
339 
340 	lw->label.TextRect.x += 1;
341 	lw->label.TextRect.y += 1;
342 	if (lw->label._acc_text != 0)
343 	{
344 	    lw->label.acc_TextRect.x += 1;
345 	    lw->label.acc_TextRect.y += 1;
346 	}
347 
348 	values.foreground = lw->primitive.top_shadow_color;
349 	values.background = lw->core.background_pixel;
350 
351 	lw->label.insensitive_GC = XtAllocateGC((Widget)lw, 0, mask,
352 					       &values, dynamic, (XtGCMask)0);
353 	(*old_label_expose)(_w, _event, _region);
354 	XtReleaseGC(_w, lw->label.insensitive_GC);
355 
356 	lw->label.TextRect.x -= 1;
357 	lw->label.TextRect.y -= 1;
358 	if (lw->label._acc_text != 0)
359 	{
360 	    lw->label.acc_TextRect.x -= 1;
361 	    lw->label.acc_TextRect.y -= 1;
362 	}
363 
364 	values.foreground = lw->primitive.bottom_shadow_color;
365 	values.background = lw->core.background_pixel;
366 
367 	lw->label.insensitive_GC = XtAllocateGC((Widget) lw, 0, mask,
368 					       &values, dynamic, (XtGCMask)0);
369 	(*old_label_expose)(_w, _event, _region);
370 	XtReleaseGC(_w, lw->label.insensitive_GC);
371 
372 	lw->label.insensitive_GC = insensitiveGC;
373     }
374 }
375 
376 /*
377  * Create all the motif widgets necessary.
378  */
379     void
gui_x11_create_widgets(void)380 gui_x11_create_widgets(void)
381 {
382 #ifdef FEAT_GUI_TABLINE
383     Widget	button, scroller;
384     Arg		args[10];
385     int		n;
386     XmString	xms;
387 #endif
388 
389     /*
390      * Install the 3D shade effect drawing routines.
391      */
392     if (old_label_expose == NULL)
393     {
394 	old_label_expose = xmLabelWidgetClass->core_class.expose;
395 	xmLabelWidgetClass->core_class.expose = label_expose;
396     }
397 
398     /*
399      * Start out by adding the configured border width into the border offset
400      */
401     gui.border_offset = gui.border_width;
402 
403     /*
404      * Install the tearOffModel resource converter.
405      */
406 #if (XmVersion >= 1002)
407     XmRepTypeInstallTearOffModelConverter();
408 #endif
409 
410     // Make sure the "Quit" menu entry of the window manager is ignored
411     XtVaSetValues(vimShell, XmNdeleteResponse, XmDO_NOTHING, NULL);
412 
413     vimForm = XtVaCreateManagedWidget("vimForm",
414 	xmFormWidgetClass, vimShell,
415 	XmNborderWidth, 0,
416 	XmNhighlightThickness, 0,
417 	XmNshadowThickness, 0,
418 	XmNmarginWidth, 0,
419 	XmNmarginHeight, 0,
420 	XmNresizePolicy, XmRESIZE_ANY,
421 	NULL);
422     gui_motif_menu_colors(vimForm);
423 
424 #ifdef FEAT_MENU
425     {
426 	Arg al[7]; // Make sure there is enough room for arguments!
427 	int ac = 0;
428 
429 # if (XmVersion >= 1002)
430 	XtSetArg(al[ac], XmNtearOffModel, tearoff_val); ac++;
431 # endif
432 	XtSetArg(al[ac], XmNleftAttachment,  XmATTACH_FORM); ac++;
433 	XtSetArg(al[ac], XmNtopAttachment,   XmATTACH_FORM); ac++;
434 	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
435 # ifndef FEAT_TOOLBAR
436 	// Always stick to right hand side.
437 	XtSetArg(al[ac], XmNrightOffset, 0); ac++;
438 # endif
439 	XtSetArg(al[ac], XmNmarginHeight, 0); ac++;
440 	menuBar = XmCreateMenuBar(vimForm, "menuBar", al, ac);
441 	XtManageChild(menuBar);
442 
443 	// Remember the default colors, needed for ":hi clear".
444 	XtVaGetValues(menuBar,
445 	    XmNbackground, &gui.menu_def_bg_pixel,
446 	    XmNforeground, &gui.menu_def_fg_pixel,
447 	    NULL);
448 	gui_motif_menu_colors(menuBar);
449     }
450 #endif
451 
452 #ifdef FEAT_TOOLBAR
453     /*
454      * Create an empty ToolBar. We should get buttons defined from menu.vim.
455      */
456     toolBarFrame = XtVaCreateWidget("toolBarFrame",
457 	xmFrameWidgetClass, vimForm,
458 	XmNshadowThickness, 0,
459 	XmNmarginHeight, 0,
460 	XmNmarginWidth, 0,
461 	XmNleftAttachment, XmATTACH_FORM,
462 	XmNrightAttachment, XmATTACH_FORM,
463 	NULL);
464     gui_motif_menu_colors(toolBarFrame);
465 
466     toolBar = XtVaCreateManagedWidget("toolBar",
467 	xmRowColumnWidgetClass, toolBarFrame,
468 	XmNchildType, XmFRAME_WORKAREA_CHILD,
469 	XmNrowColumnType, XmWORK_AREA,
470 	XmNorientation, XmHORIZONTAL,
471 	XmNtraversalOn, False,
472 	XmNisHomogeneous, False,
473 	XmNpacking, XmPACK_TIGHT,
474 	XmNspacing, 0,
475 	XmNshadowThickness, 0,
476 	XmNhighlightThickness, 0,
477 	XmNmarginHeight, 0,
478 	XmNmarginWidth, 0,
479 	XmNadjustLast, True,
480 	NULL);
481     gui_motif_menu_colors(toolBar);
482 
483 #endif
484 
485 #ifdef FEAT_GUI_TABLINE
486     // Create the Vim GUI tabline
487     n = 0;
488     XtSetArg(args[n], XmNbindingType, XmNONE); n++;
489     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
490     XtSetArg(args[n], XmNbackPageSize, XmNONE); n++;
491     XtSetArg(args[n], XmNbackPageNumber, 0); n++;
492     XtSetArg(args[n], XmNbackPagePlacement, XmTOP_RIGHT); n++;
493     XtSetArg(args[n], XmNmajorTabSpacing, 0); n++;
494     XtSetArg(args[n], XmNshadowThickness, 0); n++;
495     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
496     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
497     tabLine = XmCreateNotebook(vimForm, "Vim tabline", args, n);
498 
499     XtAddCallback(tabLine, XmNpageChangedCallback, (XtCallbackProc)tabline_cb,
500 			NULL);
501     XtAddEventHandler(tabLine, ButtonPressMask, False,
502 			(XtEventHandler)tabline_menu_cb, NULL);
503 
504     gui.tabline_height = TABLINE_HEIGHT;
505 
506     /*
507      * Set the size of the minor next/prev scrollers to zero, so
508      * that they are not displayed. Due to a bug in OpenMotif 2.3,
509      * even if these children widget are unmanaged, they are again
510      * managed by the Notebook widget and the notebook widget geometry
511      * is adjusted to account for the minor scroller widgets.
512      */
513     scroller = XtNameToWidget(tabLine, "MinorTabScrollerNext");
514     XtVaSetValues(scroller, XmNwidth, 0, XmNresizable, False,
515 		  XmNtraversalOn, False, NULL);
516     scroller = XtNameToWidget(tabLine, "MinorTabScrollerPrevious");
517     XtVaSetValues(scroller, XmNwidth, 0, XmNresizable, False,
518 		  XmNtraversalOn, False, NULL);
519 
520     // Create the tabline popup menu
521     tabLine_menu = XmCreatePopupMenu(tabLine, "tabline popup", NULL, 0);
522 
523     // Add the buttons to the tabline popup menu
524     n = 0;
525     XtSetArg(args[n], XmNuserData, TABLINE_MENU_CLOSE); n++;
526     xms = XmStringCreate((char *)"Close tab", STRING_TAG);
527     XtSetArg(args[n], XmNlabelString, xms); n++;
528     button = XmCreatePushButton(tabLine_menu, "Close", args, n);
529     XtAddCallback(button, XmNactivateCallback,
530 		  (XtCallbackProc)tabline_button_cb, NULL);
531     XmStringFree(xms);
532 
533     n = 0;
534     XtSetArg(args[n], XmNuserData, TABLINE_MENU_NEW); n++;
535     xms = XmStringCreate((char *)"New Tab", STRING_TAG);
536     XtSetArg(args[n], XmNlabelString, xms); n++;
537     button = XmCreatePushButton(tabLine_menu, "New Tab", args, n);
538     XtAddCallback(button, XmNactivateCallback,
539 		  (XtCallbackProc)tabline_button_cb, NULL);
540     XmStringFree(xms);
541 
542     n = 0;
543     XtSetArg(args[n], XmNuserData, TABLINE_MENU_OPEN); n++;
544     xms = XmStringCreate((char *)"Open tab...", STRING_TAG);
545     XtSetArg(args[n], XmNlabelString, xms); n++;
546     button = XmCreatePushButton(tabLine_menu, "Open tab...", args, n);
547     XtAddCallback(button, XmNactivateCallback,
548 		  (XtCallbackProc)tabline_button_cb, NULL);
549     XmStringFree(xms);
550 #endif
551 
552     textAreaForm = XtVaCreateManagedWidget("textAreaForm",
553 	xmFormWidgetClass, vimForm,
554 	XmNleftAttachment, XmATTACH_FORM,
555 	XmNrightAttachment, XmATTACH_FORM,
556 	XmNbottomAttachment, XmATTACH_FORM,
557 	XmNtopAttachment, XmATTACH_FORM,
558 	XmNmarginWidth, 0,
559 	XmNmarginHeight, 0,
560 	XmNresizePolicy, XmRESIZE_ANY,
561 	NULL);
562     gui_motif_scroll_colors(textAreaForm);
563 
564     textArea = XtVaCreateManagedWidget("textArea",
565 	xmDrawingAreaWidgetClass, textAreaForm,
566 	XmNforeground, gui.norm_pixel,
567 	XmNbackground, gui.back_pixel,
568 	XmNleftAttachment, XmATTACH_FORM,
569 	XmNtopAttachment, XmATTACH_FORM,
570 	XmNrightAttachment, XmATTACH_FORM,
571 	XmNbottomAttachment, XmATTACH_FORM,
572 
573 	/*
574 	 * These take some control away from the user, but avoids making them
575 	 * add resources to get a decent looking setup.
576 	 */
577 	XmNborderWidth, 0,
578 	XmNhighlightThickness, 0,
579 	XmNshadowThickness, 0,
580 	NULL);
581 
582 #ifdef FEAT_FOOTER
583     /*
584      * Create the Footer.
585      */
586     footer = XtVaCreateWidget("footer",
587 	xmLabelGadgetClass, vimForm,
588 	XmNalignment, XmALIGNMENT_BEGINNING,
589 	XmNmarginHeight, 0,
590 	XmNmarginWidth, 0,
591 	XmNtraversalOn, False,
592 	XmNrecomputeSize, False,
593 	XmNleftAttachment, XmATTACH_FORM,
594 	XmNleftOffset, 5,
595 	XmNrightAttachment, XmATTACH_FORM,
596 	XmNbottomAttachment, XmATTACH_FORM,
597 	NULL);
598     gui_mch_set_footer((char_u *) "");
599 #endif
600 
601     /*
602      * Install the callbacks.
603      */
604     gui_x11_callbacks(textArea, vimForm);
605 
606     // Pretend we don't have input focus, we will get an event if we do.
607     gui.in_focus = FALSE;
608 }
609 
610 /*
611  * Called when the GUI is not going to start after all.
612  */
613     void
gui_x11_destroy_widgets(void)614 gui_x11_destroy_widgets(void)
615 {
616     textArea = NULL;
617 #ifdef FEAT_MENU
618     menuBar = NULL;
619 #endif
620 }
621 
622     void
gui_mch_set_text_area_pos(int x UNUSED,int y UNUSED,int w UNUSED,int h UNUSED)623 gui_mch_set_text_area_pos(
624     int	    x UNUSED,
625     int	    y UNUSED,
626     int	    w UNUSED,
627     int	    h UNUSED)
628 {
629 #ifdef FEAT_TOOLBAR
630     // Give keyboard focus to the textArea instead of the toolbar.
631     reset_focus();
632 #endif
633 }
634 
635     void
gui_x11_set_back_color(void)636 gui_x11_set_back_color(void)
637 {
638     if (textArea != NULL)
639 #if (XmVersion >= 1002)
640 	XmChangeColor(textArea, gui.back_pixel);
641 #else
642 	XtVaSetValues(textArea,
643 		  XmNbackground, gui.back_pixel,
644 		  NULL);
645 #endif
646 }
647 
648 /*
649  * Manage dialog centered on pointer. This could be used by the Athena code as
650  * well.
651  */
652     void
manage_centered(Widget dialog_child)653 manage_centered(Widget dialog_child)
654 {
655     Widget shell = XtParent(dialog_child);
656     Window root, child;
657     unsigned int mask;
658     unsigned int width, height, border_width, depth;
659     int x, y, win_x, win_y, maxX, maxY;
660     Boolean mappedWhenManaged;
661 
662     // Temporarily set value of XmNmappedWhenManaged
663     // to stop the dialog from popping up right away
664     XtVaGetValues(shell, XmNmappedWhenManaged, &mappedWhenManaged, NULL);
665     XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
666 
667     XtManageChild(dialog_child);
668 
669     // Get the pointer position (x, y)
670     XQueryPointer(XtDisplay(shell), XtWindow(shell), &root, &child,
671 		  &x, &y, &win_x, &win_y, &mask);
672 
673     // Translate the pointer position (x, y) into a position for the new
674     // window that will place the pointer at its center
675     XGetGeometry(XtDisplay(shell), XtWindow(shell), &root, &win_x, &win_y,
676 		 &width, &height, &border_width, &depth);
677     width += 2 * border_width;
678     height += 2 * border_width;
679     x -= width / 2;
680     y -= height / 2;
681 
682     // Ensure that the dialog remains on screen
683     maxX = XtScreen(shell)->width - width;
684     maxY = XtScreen(shell)->height - height;
685     if (x < 0)
686 	x = 0;
687     if (x > maxX)
688 	x = maxX;
689     if (y < 0)
690 	y = 0;
691     if (y > maxY)
692 	y = maxY;
693 
694     // Set desired window position in the DialogShell
695     XtVaSetValues(shell, XmNx, x, XmNy, y, NULL);
696 
697     // Map the widget
698     XtMapWidget(shell);
699 
700     // Restore the value of XmNmappedWhenManaged
701     XtVaSetValues(shell, XmNmappedWhenManaged, mappedWhenManaged, NULL);
702 }
703 
704 #if defined(FEAT_MENU) || defined(FEAT_GUI_DIALOG) || defined(PROTO)
705 
706 /*
707  * Encapsulate the way an XmFontList is created.
708  */
709     XmFontList
gui_motif_create_fontlist(XFontStruct * font)710 gui_motif_create_fontlist(XFontStruct *font)
711 {
712     XmFontList font_list;
713 
714 # if (XmVersion <= 1001)
715     // Motif 1.1 method
716     font_list = XmFontListCreate(font, STRING_TAG);
717 # else
718     // Motif 1.2 method
719     XmFontListEntry font_list_entry;
720 
721     font_list_entry = XmFontListEntryCreate(STRING_TAG, XmFONT_IS_FONT,
722 					    (XtPointer)font);
723     font_list = XmFontListAppendEntry(NULL, font_list_entry);
724     XmFontListEntryFree(&font_list_entry);
725 # endif
726     return font_list;
727 }
728 
729 # if ((XmVersion > 1001) && defined(FEAT_XFONTSET)) || defined(PROTO)
730     XmFontList
gui_motif_fontset2fontlist(XFontSet * fontset)731 gui_motif_fontset2fontlist(XFontSet *fontset)
732 {
733     XmFontList font_list;
734 
735     // Motif 1.2 method
736     XmFontListEntry font_list_entry;
737 
738     font_list_entry = XmFontListEntryCreate(STRING_TAG,
739 					    XmFONT_IS_FONTSET,
740 					    (XtPointer)*fontset);
741     font_list = XmFontListAppendEntry(NULL, font_list_entry);
742     XmFontListEntryFree(&font_list_entry);
743     return font_list;
744 }
745 # endif
746 
747 #endif
748 
749 #if defined(FEAT_MENU) || defined(PROTO)
750 /*
751  * Menu stuff.
752  */
753 
754 static void gui_motif_add_actext(vimmenu_T *menu);
755 #if (XmVersion >= 1002)
756 static void toggle_tearoff(Widget wid);
757 static void gui_mch_recurse_tearoffs(vimmenu_T *menu);
758 #endif
759 static void submenu_change(vimmenu_T *mp, int colors);
760 
761 static void do_set_mnemonics(int enable);
762 static int menu_enabled = TRUE;
763 
764     void
gui_mch_enable_menu(int flag)765 gui_mch_enable_menu(int flag)
766 {
767     if (flag)
768     {
769 	XtManageChild(menuBar);
770 #ifdef FEAT_TOOLBAR
771 	if (XtIsManaged(XtParent(toolBar)))
772 	{
773 	    // toolBar is attached to top form
774 	    XtVaSetValues(XtParent(toolBar),
775 		XmNtopAttachment, XmATTACH_WIDGET,
776 		XmNtopWidget, menuBar,
777 		NULL);
778 #ifdef FEAT_GUI_TABLINE
779 	    if (showing_tabline)
780 	    {
781 		XtVaSetValues(tabLine,
782 			      XmNtopAttachment, XmATTACH_WIDGET,
783 			      XmNtopWidget, XtParent(toolBar),
784 			      NULL);
785 		XtVaSetValues(textAreaForm,
786 			      XmNtopAttachment, XmATTACH_WIDGET,
787 			      XmNtopWidget, tabLine,
788 			      NULL);
789 	    }
790 	    else
791 #endif
792 		XtVaSetValues(textAreaForm,
793 			      XmNtopAttachment, XmATTACH_WIDGET,
794 			      XmNtopWidget, XtParent(toolBar),
795 			      NULL);
796 	}
797 	else
798 #endif
799 	{
800 #ifdef FEAT_GUI_TABLINE
801 	    if (showing_tabline)
802 	    {
803 		XtVaSetValues(tabLine,
804 			      XmNtopAttachment, XmATTACH_WIDGET,
805 			      XmNtopWidget, menuBar,
806 			      NULL);
807 		XtVaSetValues(textAreaForm,
808 			      XmNtopAttachment, XmATTACH_WIDGET,
809 			      XmNtopWidget, tabLine,
810 			      NULL);
811 	    }
812 	    else
813 #endif
814 		XtVaSetValues(textAreaForm,
815 			      XmNtopAttachment, XmATTACH_WIDGET,
816 			      XmNtopWidget, menuBar,
817 			      NULL);
818 	}
819     }
820     else
821     {
822 	XtUnmanageChild(menuBar);
823 #ifdef FEAT_TOOLBAR
824 	if (XtIsManaged(XtParent(toolBar)))
825 	{
826 	    XtVaSetValues(XtParent(toolBar),
827 		XmNtopAttachment, XmATTACH_FORM,
828 		NULL);
829 #ifdef FEAT_GUI_TABLINE
830 	    if (showing_tabline)
831 	    {
832 		XtVaSetValues(tabLine,
833 			      XmNtopAttachment, XmATTACH_WIDGET,
834 			      XmNtopWidget, XtParent(toolBar),
835 			      NULL);
836 		XtVaSetValues(textAreaForm,
837 			      XmNtopAttachment, XmATTACH_WIDGET,
838 			      XmNtopWidget, tabLine,
839 			      NULL);
840 	    }
841 	    else
842 #endif
843 		XtVaSetValues(textAreaForm,
844 			      XmNtopAttachment, XmATTACH_WIDGET,
845 			      XmNtopWidget, XtParent(toolBar),
846 			      NULL);
847 	}
848 	else
849 #endif
850 	{
851 #ifdef FEAT_GUI_TABLINE
852 	    if (showing_tabline)
853 	    {
854 		XtVaSetValues(tabLine,
855 			      XmNtopAttachment, XmATTACH_FORM,
856 			      NULL);
857 		XtVaSetValues(textAreaForm,
858 			      XmNtopAttachment, XmATTACH_WIDGET,
859 			      XmNtopWidget, tabLine,
860 			      NULL);
861 	    }
862 	    else
863 #endif
864 		XtVaSetValues(textAreaForm,
865 			      XmNtopAttachment, XmATTACH_FORM,
866 			      NULL);
867 	}
868     }
869 
870 }
871 
872 /*
873  * Enable or disable mnemonics for the toplevel menus.
874  */
875     void
gui_motif_set_mnemonics(int enable)876 gui_motif_set_mnemonics(int enable)
877 {
878     /*
879      * Don't enable menu mnemonics when the menu bar is disabled, LessTif
880      * crashes when using a mnemonic then.
881      */
882     if (!menu_enabled)
883 	enable = FALSE;
884     do_set_mnemonics(enable);
885 }
886 
887     static void
do_set_mnemonics(int enable)888 do_set_mnemonics(int enable)
889 {
890     vimmenu_T	*menu;
891 
892     FOR_ALL_MENUS(menu)
893 	if (menu->id != (Widget)0)
894 	    XtVaSetValues(menu->id,
895 		    XmNmnemonic, enable ? menu->mnemonic : NUL,
896 		    NULL);
897 }
898 
899     void
gui_mch_add_menu(vimmenu_T * menu,int idx)900 gui_mch_add_menu(vimmenu_T *menu, int idx)
901 {
902     XmString	label;
903     Widget	shell;
904     vimmenu_T	*parent = menu->parent;
905 
906 #ifdef MOTIF_POPUP
907     if (menu_is_popup(menu->name))
908     {
909 	Arg arg[2];
910 	int n = 0;
911 
912 	// Only create the popup menu when it's actually used, otherwise there
913 	// is a delay when using the right mouse button.
914 # if (XmVersion <= 1002)
915 	if (mouse_model_popup())
916 # endif
917 	{
918 	    if (gui.menu_bg_pixel != INVALCOLOR)
919 	    {
920 		XtSetArg(arg[0], XmNbackground, gui.menu_bg_pixel); n++;
921 	    }
922 	    if (gui.menu_fg_pixel != INVALCOLOR)
923 	    {
924 		XtSetArg(arg[1], XmNforeground, gui.menu_fg_pixel); n++;
925 	    }
926 	    menu->submenu_id = XmCreatePopupMenu(textArea, "contextMenu",
927 								      arg, n);
928 	    menu->id = (Widget)0;
929 	}
930 	return;
931     }
932 #endif
933 
934     if (!menu_is_menubar(menu->name)
935 	    || (parent != NULL && parent->submenu_id == (Widget)0))
936 	return;
937 
938     label = XmStringCreate((char *)menu->dname, STRING_TAG);
939     if (label == NULL)
940 	return;
941     menu->id = XtVaCreateWidget("subMenu",
942 	    xmCascadeButtonWidgetClass,
943 	    (parent == NULL) ? menuBar : parent->submenu_id,
944 	    XmNlabelString, label,
945 	    XmNmnemonic, p_wak[0] == 'n' ? NUL : menu->mnemonic,
946 #if (XmVersion >= 1002)
947 	    // submenu: count the tearoff item (needed for LessTif)
948 	    XmNpositionIndex, idx + (parent != NULL
949 			   && tearoff_val == (int)XmTEAR_OFF_ENABLED ? 1 : 0),
950 #endif
951 	    NULL);
952     gui_motif_menu_colors(menu->id);
953     gui_motif_menu_fontlist(menu->id);
954     XmStringFree(label);
955 
956     if (menu->id == (Widget)0)		// failed
957 	return;
958 
959     // add accelerator text
960     gui_motif_add_actext(menu);
961 
962     shell = XtVaCreateWidget("subMenuShell",
963 	xmMenuShellWidgetClass, menu->id,
964 	XmNwidth, 1,
965 	XmNheight, 1,
966 	NULL);
967     gui_motif_menu_colors(shell);
968     menu->submenu_id = XtVaCreateWidget("rowColumnMenu",
969 	xmRowColumnWidgetClass, shell,
970 	XmNrowColumnType, XmMENU_PULLDOWN,
971 	NULL);
972     gui_motif_menu_colors(menu->submenu_id);
973 
974     if (menu->submenu_id == (Widget)0)		// failed
975 	return;
976 
977 #if (XmVersion >= 1002)
978     // Set the colors for the tear off widget
979     toggle_tearoff(menu->submenu_id);
980 #endif
981 
982     XtVaSetValues(menu->id,
983 	XmNsubMenuId, menu->submenu_id,
984 	NULL);
985 
986     /*
987      * The "Help" menu is a special case, and should be placed at the far
988      * right hand side of the menu-bar.  It's recognized by its high priority.
989      */
990     if (parent == NULL && menu->priority >= 9999)
991 	XtVaSetValues(menuBar,
992 		XmNmenuHelpWidget, menu->id,
993 		NULL);
994 
995     /*
996      * When we add a top-level item to the menu bar, we can figure out how
997      * high the menu bar should be.
998      */
999     if (parent == NULL)
1000 	gui_mch_compute_menu_height(menu->id);
1001 }
1002 
1003 
1004 /*
1005  * Add mnemonic and accelerator text to a menu button.
1006  */
1007     static void
gui_motif_add_actext(vimmenu_T * menu)1008 gui_motif_add_actext(vimmenu_T *menu)
1009 {
1010     XmString	label;
1011 
1012     // Add accelerator text, if there is one
1013     if (menu->actext != NULL && menu->id != (Widget)0)
1014     {
1015 	label = XmStringCreate((char *)menu->actext, STRING_TAG);
1016 	if (label == NULL)
1017 	    return;
1018 	XtVaSetValues(menu->id, XmNacceleratorText, label, NULL);
1019 	XmStringFree(label);
1020     }
1021 }
1022 
1023     void
gui_mch_toggle_tearoffs(int enable)1024 gui_mch_toggle_tearoffs(int enable)
1025 {
1026 #if (XmVersion >= 1002)
1027     if (enable)
1028 	tearoff_val = (int)XmTEAR_OFF_ENABLED;
1029     else
1030 	tearoff_val = (int)XmTEAR_OFF_DISABLED;
1031     toggle_tearoff(menuBar);
1032     gui_mch_recurse_tearoffs(root_menu);
1033 #endif
1034 }
1035 
1036 #if (XmVersion >= 1002)
1037 /*
1038  * Set the tearoff for one menu widget on or off, and set the color of the
1039  * tearoff widget.
1040  */
1041     static void
toggle_tearoff(Widget wid)1042 toggle_tearoff(Widget wid)
1043 {
1044     Widget	w;
1045 
1046     XtVaSetValues(wid, XmNtearOffModel, tearoff_val, NULL);
1047     if (tearoff_val == (int)XmTEAR_OFF_ENABLED
1048 	    && (w = XmGetTearOffControl(wid)) != (Widget)0)
1049 	gui_motif_menu_colors(w);
1050 }
1051 
1052     static void
gui_mch_recurse_tearoffs(vimmenu_T * menu)1053 gui_mch_recurse_tearoffs(vimmenu_T *menu)
1054 {
1055     while (menu != NULL)
1056     {
1057 	if (!menu_is_popup(menu->name))
1058 	{
1059 	    if (menu->submenu_id != (Widget)0)
1060 		toggle_tearoff(menu->submenu_id);
1061 	    gui_mch_recurse_tearoffs(menu->children);
1062 	}
1063 	menu = menu->next;
1064     }
1065 }
1066 #endif
1067 
1068     int
gui_mch_text_area_extra_height(void)1069 gui_mch_text_area_extra_height(void)
1070 {
1071     Dimension	shadowHeight;
1072 
1073     XtVaGetValues(textAreaForm, XmNshadowThickness, &shadowHeight, NULL);
1074     return shadowHeight;
1075 }
1076 
1077 /*
1078  * Compute the height of the menu bar.
1079  * We need to check all the items for their position and height, for the case
1080  * there are several rows, and/or some characters extend higher or lower.
1081  */
1082     void
gui_mch_compute_menu_height(Widget id)1083 gui_mch_compute_menu_height(
1084     Widget	id)		    // can be NULL when deleting menu
1085 {
1086     Dimension	y, maxy;
1087     Dimension	margin, shadow;
1088     vimmenu_T	*mp;
1089     static Dimension	height = 21;	// normal height of a menu item
1090 
1091     /*
1092      * Get the height of the new item, before managing it, because it will
1093      * still reflect the font size.  After managing it depends on the menu
1094      * height, which is what we just wanted to get!.
1095      */
1096     if (id != (Widget)0)
1097 	XtVaGetValues(id, XmNheight, &height, NULL);
1098 
1099     // Find any menu Widget, to be able to call XtManageChild()
1100     else
1101 	FOR_ALL_MENUS(mp)
1102 	    if (mp->id != (Widget)0 && menu_is_menubar(mp->name))
1103 	    {
1104 		id = mp->id;
1105 		break;
1106 	    }
1107 
1108     /*
1109      * Now manage the menu item, to make them all be positioned (makes an
1110      * extra row when needed, removes it when not needed).
1111      */
1112     if (id != (Widget)0)
1113 	XtManageChild(id);
1114 
1115     /*
1116      * Now find the menu item that is the furthest down, and get its position.
1117      */
1118     maxy = 0;
1119     FOR_ALL_MENUS(mp)
1120     {
1121 	if (mp->id != (Widget)0 && menu_is_menubar(mp->name))
1122 	{
1123 	    XtVaGetValues(mp->id, XmNy, &y, NULL);
1124 	    if (y > maxy)
1125 		maxy = y;
1126 	}
1127     }
1128 
1129     XtVaGetValues(menuBar,
1130 	XmNmarginHeight, &margin,
1131 	XmNshadowThickness, &shadow,
1132 	NULL);
1133 
1134     /*
1135      * This computation is the result of trial-and-error:
1136      * maxy =	The maximum position of an item; required for when there are
1137      *		two or more rows
1138      * height = height of an item, before managing it;	Hopefully this will
1139      *		change with the font height.  Includes shadow-border.
1140      * shadow =	shadow-border; must be subtracted from the height.
1141      * margin = margin around the menu buttons;  Must be added.
1142      * Add 4 for the underlining of shortcut keys.
1143      */
1144     gui.menu_height = maxy + height - 2 * shadow + 2 * margin + 4;
1145 
1146     // Somehow the menu bar doesn't resize automatically.  Set it here,
1147     // even though this is a catch 22.  Don't do this when starting up,
1148     // somehow the menu gets very high then.
1149     if (gui.shell_created)
1150 	XtVaSetValues(menuBar, XmNheight, gui.menu_height, NULL);
1151 }
1152 
1153 #ifdef FEAT_TOOLBAR
1154 
1155 /*
1156  * Icons used by the toolbar code.
1157  */
1158 #include "gui_x11_pm.h"
1159 
1160 static char **get_toolbar_pixmap(vimmenu_T *menu, char **fname);
1161 
1162 /*
1163  * Read an Xpm file.  Return OK or FAIL.
1164  */
1165     static int
check_xpm(char_u * path)1166 check_xpm(char_u *path)
1167 {
1168     XpmAttributes attrs;
1169     int		status;
1170     Pixmap	mask;
1171     Pixmap	map;
1172 
1173     attrs.valuemask = 0;
1174 
1175     // Create the "sensitive" pixmap
1176     status = XpmReadFileToPixmap(gui.dpy,
1177 	    RootWindow(gui.dpy, DefaultScreen(gui.dpy)),
1178 	    (char *)path, &map, &mask, &attrs);
1179     XpmFreeAttributes(&attrs);
1180 
1181     if (status == XpmSuccess)
1182 	return OK;
1183     return FAIL;
1184 }
1185 
1186 
1187 /*
1188  * Allocated a pixmap for toolbar menu "menu".
1189  * When it's to be read from a file, "fname" is set to the file name
1190  * (in allocated memory).
1191  * Return a blank pixmap if it fails.
1192  */
1193     static char **
get_toolbar_pixmap(vimmenu_T * menu,char ** fname)1194 get_toolbar_pixmap(vimmenu_T *menu, char **fname)
1195 {
1196     char_u	buf[MAXPATHL];		// buffer storing expanded pathname
1197     char	**xpm = NULL;		// xpm array
1198     int		res;
1199 
1200     *fname = NULL;
1201     buf[0] = NUL;			// start with NULL path
1202 
1203     if (menu->iconfile != NULL)
1204     {
1205 	// Use the "icon="  argument.
1206 	gui_find_iconfile(menu->iconfile, buf, "xpm");
1207 	res = check_xpm(buf);
1208 
1209 	// If it failed, try using the menu name.
1210 	if (res == FAIL && gui_find_bitmap(menu->name, buf, "xpm") == OK)
1211 	    res = check_xpm(buf);
1212 	if (res == OK)
1213 	{
1214 	    *fname = (char *)vim_strsave(buf);
1215 	    return tb_blank_xpm;
1216 	}
1217     }
1218 
1219     if (menu->icon_builtin || gui_find_bitmap(menu->name, buf, "xpm") == FAIL)
1220     {
1221 	if (menu->iconidx >= 0 && menu->iconidx
1222 	       < (int)(sizeof(built_in_pixmaps) / sizeof(built_in_pixmaps[0])))
1223 	    xpm = built_in_pixmaps[menu->iconidx];
1224 	else
1225 	    xpm = tb_blank_xpm;
1226     }
1227 
1228     return xpm;
1229 }
1230 
1231 /*
1232  * Add arguments for the toolbar pixmap to a menu item.
1233  */
1234     static int
add_pixmap_args(vimmenu_T * menu,Arg * args,int n)1235 add_pixmap_args(vimmenu_T *menu, Arg *args, int n)
1236 {
1237     vim_free(menu->xpm_fname);
1238     menu->xpm = get_toolbar_pixmap(menu, &menu->xpm_fname);
1239     if (menu->xpm == NULL)
1240     {
1241 	XtSetArg(args[n], XmNlabelType, XmSTRING); n++;
1242     }
1243     else
1244     {
1245 	if (menu->xpm_fname != NULL)
1246 	{
1247 	    XtSetArg(args[n], XmNpixmapFile, menu->xpm_fname); n++;
1248 	}
1249 	XtSetArg(args[n], XmNpixmapData, menu->xpm); n++;
1250 	XtSetArg(args[n], XmNlabelLocation, XmBOTTOM); n++;
1251     }
1252     return n;
1253 }
1254 #endif // FEAT_TOOLBAR
1255 
1256     void
gui_mch_add_menu_item(vimmenu_T * menu,int idx)1257 gui_mch_add_menu_item(vimmenu_T *menu, int idx)
1258 {
1259     XmString	label;
1260     vimmenu_T	*parent = menu->parent;
1261 
1262 # ifdef EBCDIC
1263     menu->mnemonic = 0;
1264 # endif
1265 
1266 # if (XmVersion <= 1002)
1267     // Don't add Popup menu items when the popup menu isn't used.
1268     if (menu_is_child_of_popup(menu) && !mouse_model_popup())
1269 	return;
1270 # endif
1271 
1272 # ifdef FEAT_TOOLBAR
1273     if (menu_is_toolbar(parent->name))
1274     {
1275 	WidgetClass	type;
1276 	XmString	xms = NULL;    // fallback label if pixmap not found
1277 	int		n;
1278 	Arg		args[18];
1279 
1280 	n = 0;
1281 	if (menu_is_separator(menu->name))
1282 	{
1283 	    char	*cp;
1284 	    Dimension	wid;
1285 
1286 	    /*
1287 	     * A separator has the format "-sep%d[:%d]-". The optional :%d is
1288 	     * a width specifier. If no width is specified then we choose one.
1289 	     */
1290 	    cp = (char *)vim_strchr(menu->name, ':');
1291 	    if (cp != NULL)
1292 		wid = (Dimension)atoi(++cp);
1293 	    else
1294 		wid = 4;
1295 
1296 	    type = xmSeparatorWidgetClass;
1297 	    XtSetArg(args[n], XmNwidth, wid); n++;
1298 	    XtSetArg(args[n], XmNminWidth, wid); n++;
1299 	    XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
1300 	    XtSetArg(args[n], XmNseparatorType, XmSHADOW_ETCHED_IN); n++;
1301 	}
1302 	else
1303 	{
1304 	    // Without shadows one can't sense whatever the button has been
1305 	    // pressed or not! However we want to save a bit of space...
1306 	    // Need the highlightThickness to see the focus.
1307 	    XtSetArg(args[n], XmNhighlightThickness, 1); n++;
1308 	    XtSetArg(args[n], XmNhighlightOnEnter, True); n++;
1309 	    XtSetArg(args[n], XmNmarginWidth, 0); n++;
1310 	    XtSetArg(args[n], XmNmarginHeight, 0); n++;
1311 	    XtSetArg(args[n], XmNtraversalOn, False); n++;
1312 	    // Set the label here, so that we can switch between icons/text
1313 	    // by changing the XmNlabelType resource.
1314 	    xms = XmStringCreate((char *)menu->dname, STRING_TAG);
1315 	    XtSetArg(args[n], XmNlabelString, xms); n++;
1316 
1317 	    n = add_pixmap_args(menu, args, n);
1318 
1319 	    type = xmEnhancedButtonWidgetClass;
1320 	}
1321 
1322 	XtSetArg(args[n], XmNpositionIndex, idx); n++;
1323 	if (menu->id == NULL)
1324 	{
1325 	    menu->id = XtCreateManagedWidget((char *)menu->dname,
1326 			type, toolBar, args, n);
1327 	    if (menu->id != NULL && type == xmEnhancedButtonWidgetClass)
1328 	    {
1329 		XtAddCallback(menu->id,
1330 			XmNactivateCallback, gui_x11_menu_cb, menu);
1331 # ifdef FEAT_FOOTER
1332 		XtAddEventHandler(menu->id, EnterWindowMask, False,
1333 			toolbarbutton_enter_cb, menu);
1334 		XtAddEventHandler(menu->id, LeaveWindowMask, False,
1335 			toolbarbutton_leave_cb, menu);
1336 # endif
1337 	    }
1338 	}
1339 	else
1340 	    XtSetValues(menu->id, args, n);
1341 	if (xms != NULL)
1342 	    XmStringFree(xms);
1343 
1344 # ifdef FEAT_BEVAL_GUI
1345 	gui_mch_menu_set_tip(menu);
1346 # endif
1347 
1348 	menu->parent = parent;
1349 	menu->submenu_id = NULL;
1350 	// When adding first item to toolbar it might have to be enabled .
1351 	if (!XtIsManaged(XtParent(toolBar))
1352 		    && vim_strchr(p_go, GO_TOOLBAR) != NULL)
1353 	    gui_mch_show_toolbar(TRUE);
1354 	gui.toolbar_height = gui_mch_compute_toolbar_height();
1355 	return;
1356     } // toolbar menu item
1357 # endif
1358 
1359     // No parent, must be a non-menubar menu
1360     if (parent->submenu_id == (Widget)0)
1361 	return;
1362 
1363     menu->submenu_id = (Widget)0;
1364 
1365     // Add menu separator
1366     if (menu_is_separator(menu->name))
1367     {
1368 	menu->id = XtVaCreateWidget("subMenu",
1369 		xmSeparatorGadgetClass, parent->submenu_id,
1370 #if (XmVersion >= 1002)
1371 		// count the tearoff item (needed for LessTif)
1372 		XmNpositionIndex, idx + (tearoff_val == (int)XmTEAR_OFF_ENABLED
1373 								     ? 1 : 0),
1374 #endif
1375 		NULL);
1376 	gui_motif_menu_colors(menu->id);
1377 	return;
1378     }
1379 
1380     label = XmStringCreate((char *)menu->dname, STRING_TAG);
1381     if (label == NULL)
1382 	return;
1383     menu->id = XtVaCreateWidget("subMenu",
1384 	xmPushButtonWidgetClass, parent->submenu_id,
1385 	XmNlabelString, label,
1386 	XmNmnemonic, menu->mnemonic,
1387 #if (XmVersion >= 1002)
1388 	// count the tearoff item (needed for LessTif)
1389 	XmNpositionIndex, idx + (tearoff_val == (int)XmTEAR_OFF_ENABLED
1390 								     ? 1 : 0),
1391 #endif
1392 	NULL);
1393     gui_motif_menu_colors(menu->id);
1394     gui_motif_menu_fontlist(menu->id);
1395     XmStringFree(label);
1396 
1397     if (menu->id != (Widget)0)
1398     {
1399 	XtAddCallback(menu->id, XmNactivateCallback, gui_x11_menu_cb,
1400 		(XtPointer)menu);
1401 	// add accelerator text
1402 	gui_motif_add_actext(menu);
1403     }
1404 }
1405 
1406 #if (XmVersion <= 1002) || defined(PROTO)
1407 /*
1408  * This function will destroy/create the popup menus dynamically,
1409  * according to the value of 'mousemodel'.
1410  * This will fix the "right mouse button freeze" that occurs when
1411  * there exists a popup menu but it isn't managed.
1412  */
1413     void
gui_motif_update_mousemodel(vimmenu_T * menu)1414 gui_motif_update_mousemodel(vimmenu_T *menu)
1415 {
1416     int		idx = 0;
1417 
1418     // When GUI hasn't started the menus have not been created.
1419     if (!gui.in_use)
1420       return;
1421 
1422     while (menu)
1423     {
1424       if (menu->children != NULL)
1425       {
1426 	  if (menu_is_popup(menu->name))
1427 	  {
1428 	      if (mouse_model_popup())
1429 	      {
1430 		  // Popup menu will be used.  Create the popup menus.
1431 		  gui_mch_add_menu(menu, idx);
1432 		  gui_motif_update_mousemodel(menu->children);
1433 	      }
1434 	      else
1435 	      {
1436 		  // Popup menu will not be used.  Destroy the popup menus.
1437 		  gui_motif_update_mousemodel(menu->children);
1438 		  gui_mch_destroy_menu(menu);
1439 	      }
1440 	  }
1441       }
1442       else if (menu_is_child_of_popup(menu))
1443       {
1444 	  if (mouse_model_popup())
1445 	      gui_mch_add_menu_item(menu, idx);
1446 	  else
1447 	      gui_mch_destroy_menu(menu);
1448       }
1449       menu = menu->next;
1450       ++idx;
1451     }
1452 }
1453 #endif
1454 
1455     void
gui_mch_new_menu_colors(void)1456 gui_mch_new_menu_colors(void)
1457 {
1458     if (menuBar == (Widget)0)
1459 	return;
1460     gui_motif_menu_colors(menuBar);
1461 #ifdef FEAT_TOOLBAR
1462     gui_motif_menu_colors(toolBarFrame);
1463     gui_motif_menu_colors(toolBar);
1464 #endif
1465 
1466     submenu_change(root_menu, TRUE);
1467 }
1468 
1469     void
gui_mch_new_menu_font(void)1470 gui_mch_new_menu_font(void)
1471 {
1472     if (menuBar == (Widget)0)
1473 	return;
1474     submenu_change(root_menu, FALSE);
1475     {
1476 	Dimension   height;
1477 	Position w, h;
1478 
1479 	XtVaGetValues(menuBar, XmNheight, &height, NULL);
1480 	gui.menu_height = height;
1481 
1482 	XtVaGetValues(vimShell, XtNwidth, &w, XtNheight, &h, NULL);
1483 	gui_resize_shell(w, h
1484 #ifdef FEAT_XIM
1485 		- xim_get_status_area_height()
1486 #endif
1487 		     );
1488     }
1489     gui_set_shellsize(FALSE, TRUE, RESIZE_VERT);
1490     ui_new_shellsize();
1491 }
1492 
1493 #if defined(FEAT_BEVAL_GUI) || defined(PROTO)
1494     void
gui_mch_new_tooltip_font(void)1495 gui_mch_new_tooltip_font(void)
1496 {
1497 # ifdef FEAT_TOOLBAR
1498     vimmenu_T   *menu;
1499 
1500     if (toolBar == (Widget)0)
1501 	return;
1502 
1503     menu = gui_find_menu((char_u *)"ToolBar");
1504     if (menu != NULL)
1505 	submenu_change(menu, FALSE);
1506 # endif
1507 }
1508 
1509     void
gui_mch_new_tooltip_colors(void)1510 gui_mch_new_tooltip_colors(void)
1511 {
1512 # ifdef FEAT_TOOLBAR
1513     vimmenu_T   *toolbar;
1514 
1515     if (toolBar == (Widget)0)
1516 	return;
1517 
1518     toolbar = gui_find_menu((char_u *)"ToolBar");
1519     if (toolbar != NULL)
1520 	submenu_change(toolbar, TRUE);
1521 # endif
1522 }
1523 #endif
1524 
1525     static void
submenu_change(vimmenu_T * menu,int colors)1526 submenu_change(
1527     vimmenu_T	*menu,
1528     int		colors)		// TRUE for colors, FALSE for font
1529 {
1530     vimmenu_T	*mp;
1531 
1532     for (mp = menu; mp != NULL; mp = mp->next)
1533     {
1534 	if (mp->id != (Widget)0)
1535 	{
1536 	    if (colors)
1537 	    {
1538 		gui_motif_menu_colors(mp->id);
1539 #ifdef FEAT_TOOLBAR
1540 		// For a toolbar item: Free the pixmap and allocate a new one,
1541 		// so that the background color is right.
1542 		if (mp->xpm != NULL)
1543 		{
1544 		    int		n = 0;
1545 		    Arg		args[18];
1546 
1547 		    n = add_pixmap_args(mp, args, n);
1548 		    XtSetValues(mp->id, args, n);
1549 		}
1550 # ifdef FEAT_BEVAL_GUI
1551 		// If we have a tooltip, then we need to change its font
1552 		if (mp->tip != NULL)
1553 		{
1554 		    Arg args[2];
1555 
1556 		    args[0].name = XmNbackground;
1557 		    args[0].value = gui.tooltip_bg_pixel;
1558 		    args[1].name = XmNforeground;
1559 		    args[1].value = gui.tooltip_fg_pixel;
1560 		    XtSetValues(mp->tip->balloonLabel, &args[0], XtNumber(args));
1561 		}
1562 # endif
1563 #endif
1564 	    }
1565 	    else
1566 	    {
1567 		gui_motif_menu_fontlist(mp->id);
1568 #ifdef FEAT_BEVAL_GUI
1569 		// If we have a tooltip, then we need to change its font
1570 		if (mp->tip != NULL)
1571 		{
1572 		    Arg args[1];
1573 
1574 		    args[0].name = XmNfontList;
1575 		    args[0].value = (XtArgVal)gui_motif_fontset2fontlist(
1576 						    &gui.tooltip_fontset);
1577 		    XtSetValues(mp->tip->balloonLabel, &args[0], XtNumber(args));
1578 		}
1579 #endif
1580 	    }
1581 	}
1582 
1583 	if (mp->children != NULL)
1584 	{
1585 #if (XmVersion >= 1002)
1586 	    // Set the colors/font for the tear off widget
1587 	    if (mp->submenu_id != (Widget)0)
1588 	    {
1589 		if (colors)
1590 		    gui_motif_menu_colors(mp->submenu_id);
1591 		else
1592 		    gui_motif_menu_fontlist(mp->submenu_id);
1593 		toggle_tearoff(mp->submenu_id);
1594 	    }
1595 #endif
1596 	    // Set the colors for the children
1597 	    submenu_change(mp->children, colors);
1598 	}
1599     }
1600 }
1601 
1602 /*
1603  * Destroy the machine specific menu widget.
1604  */
1605     void
gui_mch_destroy_menu(vimmenu_T * menu)1606 gui_mch_destroy_menu(vimmenu_T *menu)
1607 {
1608     // Please be sure to destroy the parent widget first (i.e. menu->id).
1609     // On the other hand, problems have been reported that the submenu must be
1610     // deleted first...
1611     //
1612     // This code should be basically identical to that in the file gui_athena.c
1613     // because they are both Xt based.
1614     if (menu->submenu_id != (Widget)0)
1615     {
1616 	XtDestroyWidget(menu->submenu_id);
1617 	menu->submenu_id = (Widget)0;
1618     }
1619 
1620     if (menu->id != (Widget)0)
1621     {
1622 	Widget	    parent;
1623 
1624 	parent = XtParent(menu->id);
1625 #if defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL_GUI)
1626 	if (parent == toolBar && menu->tip != NULL)
1627 	{
1628 	    // We try to destroy this before the actual menu, because there are
1629 	    // callbacks, etc. that will be unregistered during the tooltip
1630 	    // destruction.
1631 	    //
1632 	    // If you call "gui_mch_destroy_beval_area()" after destroying
1633 	    // menu->id, then the tooltip's window will have already been
1634 	    // deallocated by Xt, and unknown behaviour will ensue (probably
1635 	    // a core dump).
1636 	    gui_mch_destroy_beval_area(menu->tip);
1637 	    menu->tip = NULL;
1638 	}
1639 #endif
1640 	XtDestroyWidget(menu->id);
1641 	menu->id = (Widget)0;
1642 	if (parent == menuBar)
1643 	    gui_mch_compute_menu_height((Widget)0);
1644 #ifdef FEAT_TOOLBAR
1645 	else if (parent == toolBar)
1646 	{
1647 	    Cardinal    num_children;
1648 
1649 	    // When removing last toolbar item, don't display the toolbar.
1650 	    XtVaGetValues(toolBar, XmNnumChildren, &num_children, NULL);
1651 	    if (num_children == 0)
1652 		gui_mch_show_toolbar(FALSE);
1653 	    else
1654 		gui.toolbar_height = gui_mch_compute_toolbar_height();
1655 	}
1656 #endif
1657     }
1658 }
1659 
1660     void
gui_mch_show_popupmenu(vimmenu_T * menu UNUSED)1661 gui_mch_show_popupmenu(vimmenu_T *menu UNUSED)
1662 {
1663 #ifdef MOTIF_POPUP
1664     XmMenuPosition(menu->submenu_id, gui_x11_get_last_mouse_event());
1665     XtManageChild(menu->submenu_id);
1666 #endif
1667 }
1668 
1669 #endif // FEAT_MENU
1670 
1671 /*
1672  * Set the menu and scrollbar colors to their default values.
1673  */
1674     void
gui_mch_def_colors(void)1675 gui_mch_def_colors(void)
1676 {
1677     if (gui.in_use)
1678     {
1679 	// Use the values saved when starting up.  These should come from the
1680 	// window manager or a resources file.
1681 	gui.menu_fg_pixel = gui.menu_def_fg_pixel;
1682 	gui.menu_bg_pixel = gui.menu_def_bg_pixel;
1683 	gui.scroll_fg_pixel = gui.scroll_def_fg_pixel;
1684 	gui.scroll_bg_pixel = gui.scroll_def_bg_pixel;
1685 #ifdef FEAT_BEVAL_GUI
1686 	gui.tooltip_fg_pixel =
1687 			gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1688 	gui.tooltip_bg_pixel =
1689 			gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1690 #endif
1691     }
1692 }
1693 
1694 
1695 /*
1696  * Scrollbar stuff.
1697  */
1698 
1699     void
gui_mch_set_scrollbar_thumb(scrollbar_T * sb,long val,long size,long max)1700 gui_mch_set_scrollbar_thumb(
1701     scrollbar_T *sb,
1702     long	val,
1703     long	size,
1704     long	max)
1705 {
1706     if (sb->id != (Widget)0)
1707 	XtVaSetValues(sb->id,
1708 		  XmNvalue, val,
1709 		  XmNsliderSize, size,
1710 		  XmNpageIncrement, (size > 2 ? size - 2 : 1),
1711 		  XmNmaximum, max + 1,	    // Motif has max one past the end
1712 		  NULL);
1713 }
1714 
1715     void
gui_mch_set_scrollbar_pos(scrollbar_T * sb,int x,int y,int w,int h)1716 gui_mch_set_scrollbar_pos(
1717     scrollbar_T *sb,
1718     int		x,
1719     int		y,
1720     int		w,
1721     int		h)
1722 {
1723     if (sb->id != (Widget)0)
1724     {
1725 	if (sb->type == SBAR_LEFT || sb->type == SBAR_RIGHT)
1726 	{
1727 	    if (y == 0)
1728 		h -= gui.border_offset;
1729 	    else
1730 		y -= gui.border_offset;
1731 	    XtVaSetValues(sb->id,
1732 			  XmNtopOffset, y,
1733 			  XmNbottomOffset, -y - h,
1734 			  XmNwidth, w,
1735 			  NULL);
1736 	}
1737 	else
1738 	    XtVaSetValues(sb->id,
1739 			  XmNtopOffset, y,
1740 			  XmNleftOffset, x,
1741 			  XmNrightOffset, gui.which_scrollbars[SBAR_RIGHT]
1742 						    ? gui.scrollbar_width : 0,
1743 			  XmNheight, h,
1744 			  NULL);
1745 	XtManageChild(sb->id);
1746     }
1747 }
1748 
1749     int
gui_mch_get_scrollbar_xpadding(void)1750 gui_mch_get_scrollbar_xpadding(void)
1751 {
1752     // TODO: Calculate the padding for adjust scrollbar position when the
1753     // Window is maximized.
1754     return 0;
1755 }
1756 
1757     int
gui_mch_get_scrollbar_ypadding(void)1758 gui_mch_get_scrollbar_ypadding(void)
1759 {
1760     // TODO: Calculate the padding for adjust scrollbar position when the
1761     // Window is maximized.
1762     return 0;
1763 }
1764 
1765     void
gui_mch_enable_scrollbar(scrollbar_T * sb,int flag)1766 gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
1767 {
1768     Arg		args[16];
1769     int		n;
1770 
1771     if (sb->id != (Widget)0)
1772     {
1773 	n = 0;
1774 	if (flag)
1775 	{
1776 	    switch (sb->type)
1777 	    {
1778 		case SBAR_LEFT:
1779 		    XtSetArg(args[n], XmNleftOffset, gui.scrollbar_width); n++;
1780 		    break;
1781 
1782 		case SBAR_RIGHT:
1783 		    XtSetArg(args[n], XmNrightOffset, gui.scrollbar_width); n++;
1784 		    break;
1785 
1786 		case SBAR_BOTTOM:
1787 		    XtSetArg(args[n], XmNbottomOffset, gui.scrollbar_height);n++;
1788 		    break;
1789 	    }
1790 	    XtSetValues(textArea, args, n);
1791 	    XtManageChild(sb->id);
1792 	}
1793 	else
1794 	{
1795 	    if (!gui.which_scrollbars[sb->type])
1796 	    {
1797 		// The scrollbars of this type are all disabled, adjust the
1798 		// textArea attachment offset.
1799 		switch (sb->type)
1800 		{
1801 		    case SBAR_LEFT:
1802 			XtSetArg(args[n], XmNleftOffset, 0); n++;
1803 			break;
1804 
1805 		    case SBAR_RIGHT:
1806 			XtSetArg(args[n], XmNrightOffset, 0); n++;
1807 			break;
1808 
1809 		    case SBAR_BOTTOM:
1810 			XtSetArg(args[n], XmNbottomOffset, 0);n++;
1811 			break;
1812 		}
1813 		XtSetValues(textArea, args, n);
1814 	    }
1815 	    XtUnmanageChild(sb->id);
1816 	}
1817     }
1818 }
1819 
1820     void
gui_mch_create_scrollbar(scrollbar_T * sb,int orient)1821 gui_mch_create_scrollbar(
1822     scrollbar_T *sb,
1823     int		orient)	// SBAR_VERT or SBAR_HORIZ
1824 {
1825     Arg		args[16];
1826     int		n;
1827 
1828     n = 0;
1829     XtSetArg(args[n], XmNminimum, 0); n++;
1830     XtSetArg(args[n], XmNorientation,
1831 	    (orient == SBAR_VERT) ? XmVERTICAL : XmHORIZONTAL); n++;
1832 
1833     switch (sb->type)
1834     {
1835 	case SBAR_LEFT:
1836 	    XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
1837 	    XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_FORM); n++;
1838 	    XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
1839 	    break;
1840 
1841 	case SBAR_RIGHT:
1842 	    XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
1843 	    XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_FORM); n++;
1844 	    XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
1845 	    break;
1846 
1847 	case SBAR_BOTTOM:
1848 	    XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
1849 	    XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
1850 	    XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
1851 	    break;
1852     }
1853 
1854     sb->id = XtCreateWidget("scrollBar",
1855 	    xmScrollBarWidgetClass, textAreaForm, args, n);
1856 
1857     // Remember the default colors, needed for ":hi clear".
1858     if (gui.scroll_def_bg_pixel == (guicolor_T)0
1859 	    && gui.scroll_def_fg_pixel == (guicolor_T)0)
1860 	XtVaGetValues(sb->id,
1861 		XmNbackground, &gui.scroll_def_bg_pixel,
1862 		XmNforeground, &gui.scroll_def_fg_pixel,
1863 		NULL);
1864 
1865     if (sb->id != (Widget)0)
1866     {
1867 	gui_mch_set_scrollbar_colors(sb);
1868 	XtAddCallback(sb->id, XmNvalueChangedCallback,
1869 		      scroll_cb, (XtPointer)sb->ident);
1870 	XtAddCallback(sb->id, XmNdragCallback,
1871 		      scroll_cb, (XtPointer)sb->ident);
1872 	XtAddEventHandler(sb->id, KeyPressMask, FALSE, gui_x11_key_hit_cb,
1873 	    (XtPointer)0);
1874     }
1875 }
1876 
1877     void
gui_mch_destroy_scrollbar(scrollbar_T * sb)1878 gui_mch_destroy_scrollbar(scrollbar_T *sb)
1879 {
1880     if (sb->id != (Widget)0)
1881 	XtDestroyWidget(sb->id);
1882 }
1883 
1884     void
gui_mch_set_scrollbar_colors(scrollbar_T * sb)1885 gui_mch_set_scrollbar_colors(scrollbar_T *sb)
1886 {
1887     if (sb->id != (Widget)0)
1888     {
1889 	if (gui.scroll_bg_pixel != INVALCOLOR)
1890 	{
1891 #if (XmVersion>=1002)
1892 	    XmChangeColor(sb->id, gui.scroll_bg_pixel);
1893 #else
1894 	    XtVaSetValues(sb->id,
1895 		    XmNtroughColor, gui.scroll_bg_pixel,
1896 		    NULL);
1897 #endif
1898 	}
1899 
1900 	if (gui.scroll_fg_pixel != INVALCOLOR)
1901 	    XtVaSetValues(sb->id,
1902 		    XmNforeground, gui.scroll_fg_pixel,
1903 #if (XmVersion<1002)
1904 		    XmNbackground, gui.scroll_fg_pixel,
1905 #endif
1906 		    NULL);
1907     }
1908 
1909     // This is needed for the rectangle below the vertical scrollbars.
1910     if (sb == &gui.bottom_sbar && textAreaForm != (Widget)0)
1911 	gui_motif_scroll_colors(textAreaForm);
1912 }
1913 
1914 /*
1915  * Miscellaneous stuff:
1916  */
1917 
1918     Window
gui_x11_get_wid(void)1919 gui_x11_get_wid(void)
1920 {
1921     return(XtWindow(textArea));
1922 }
1923 
1924 /*
1925  * Look for a widget in the widget tree w, with a mnemonic matching keycode.
1926  * When one is found, simulate a button press on that widget and give it the
1927  * keyboard focus.  If the mnemonic is on a label, look in the userData field
1928  * of the label to see if it points to another widget, and give that the focus.
1929  */
1930     static void
do_mnemonic(Widget w,unsigned int keycode)1931 do_mnemonic(Widget w, unsigned int keycode)
1932 {
1933     WidgetList	    children;
1934     int		    numChildren, i;
1935     Boolean	    isMenu;
1936     KeySym	    mnemonic = '\0';
1937     char	    mneString[2];
1938     Widget	    userData;
1939     unsigned char   rowColType;
1940 
1941     if (XtIsComposite(w))
1942     {
1943 	if (XtClass(w) == xmRowColumnWidgetClass)
1944 	{
1945 	    XtVaGetValues(w, XmNrowColumnType, &rowColType, NULL);
1946 	    isMenu = (rowColType != (unsigned char)XmWORK_AREA);
1947 	}
1948 	else
1949 	    isMenu = False;
1950 	if (!isMenu)
1951 	{
1952 	    XtVaGetValues(w, XmNchildren, &children, XmNnumChildren,
1953 			  &numChildren, NULL);
1954 	    for (i = 0; i < numChildren; i++)
1955 		do_mnemonic(children[i], keycode);
1956 	}
1957     }
1958     else
1959     {
1960 	XtVaGetValues(w, XmNmnemonic, &mnemonic, NULL);
1961 	if (mnemonic != '\0')
1962 	{
1963 	    mneString[0] = mnemonic;
1964 	    mneString[1] = '\0';
1965 	    if (XKeysymToKeycode(XtDisplay(XtParent(w)),
1966 				       XStringToKeysym(mneString)) == keycode)
1967 	    {
1968 		if (XtClass(w) == xmLabelWidgetClass
1969 			|| XtClass(w) == xmLabelGadgetClass)
1970 		{
1971 		    XtVaGetValues(w, XmNuserData, &userData, NULL);
1972 		    if (userData != NULL && XtIsWidget(userData))
1973 			XmProcessTraversal(userData, XmTRAVERSE_CURRENT);
1974 		}
1975 		else
1976 		{
1977 		    XKeyPressedEvent keyEvent;
1978 
1979 		    XmProcessTraversal(w, XmTRAVERSE_CURRENT);
1980 
1981 		    CLEAR_FIELD(keyEvent);
1982 		    keyEvent.type = KeyPress;
1983 		    keyEvent.serial = 1;
1984 		    keyEvent.send_event = True;
1985 		    keyEvent.display = XtDisplay(w);
1986 		    keyEvent.window = XtWindow(w);
1987 		    XtCallActionProc(w, "Activate", (XEvent *) & keyEvent,
1988 								     NULL, 0);
1989 		}
1990 	    }
1991 	}
1992     }
1993 }
1994 
1995 /*
1996  * Callback routine for dialog mnemonic processing.
1997  */
1998     static void
mnemonic_event(Widget w,XtPointer call_data UNUSED,XKeyEvent * event,Boolean * b UNUSED)1999 mnemonic_event(
2000 	Widget	    w,
2001 	XtPointer   call_data UNUSED,
2002 	XKeyEvent   *event,
2003 	Boolean	    *b UNUSED)
2004 {
2005     do_mnemonic(w, event->keycode);
2006 }
2007 
2008 
2009 /*
2010  * Search the widget tree under w for widgets with mnemonics.  When found, add
2011  * a passive grab to the dialog widget for the mnemonic character, thus
2012  * directing mnemonic events to the dialog widget.
2013  */
2014     static void
add_mnemonic_grabs(Widget dialog,Widget w)2015 add_mnemonic_grabs(Widget dialog, Widget w)
2016 {
2017     char	    mneString[2];
2018     WidgetList	    children;
2019     int		    numChildren, i;
2020     Boolean	    isMenu;
2021     KeySym	    mnemonic = '\0';
2022     unsigned char   rowColType;
2023 
2024     if (XtIsComposite(w))
2025     {
2026 	if (XtClass(w) == xmRowColumnWidgetClass)
2027 	{
2028 	    XtVaGetValues(w, XmNrowColumnType, &rowColType, NULL);
2029 	    isMenu = (rowColType != (unsigned char)XmWORK_AREA);
2030 	}
2031 	else
2032 	    isMenu = False;
2033 	if (!isMenu)
2034 	{
2035 	    XtVaGetValues(w, XmNchildren, &children, XmNnumChildren,
2036 							  &numChildren, NULL);
2037 	    for (i = 0; i < numChildren; i++)
2038 		add_mnemonic_grabs(dialog, children[i]);
2039 	}
2040     }
2041     else
2042     {
2043 	XtVaGetValues(w, XmNmnemonic, &mnemonic, NULL);
2044 	if (mnemonic != '\0')
2045 	{
2046 	    mneString[0] = mnemonic;
2047 	    mneString[1] = '\0';
2048 	    XtGrabKey(dialog, XKeysymToKeycode(XtDisplay(dialog),
2049 						  XStringToKeysym(mneString)),
2050 		    Mod1Mask, True, GrabModeAsync, GrabModeAsync);
2051 	}
2052     }
2053 }
2054 
2055 /*
2056  * Add a handler for mnemonics in a dialog.  Motif itself only handles
2057  * mnemonics in menus. Mnemonics added or changed after this call will be
2058  * ignored.
2059  *
2060  * To add a mnemonic to a text field or list, set the XmNmnemonic resource on
2061  * the appropriate label and set the XmNuserData resource of the label to the
2062  * widget to get the focus when the mnemonic is typed.
2063  */
2064     static void
activate_dialog_mnemonics(Widget dialog)2065 activate_dialog_mnemonics(Widget dialog)
2066 {
2067     if (!dialog)
2068 	return;
2069 
2070     XtAddEventHandler(dialog, KeyPressMask, False,
2071 			   (XtEventHandler) mnemonic_event, (XtPointer) NULL);
2072     add_mnemonic_grabs(dialog, dialog);
2073 }
2074 
2075 /*
2076  * Removes the event handler and key-grabs for dialog mnemonic handling.
2077  */
2078     static void
suppress_dialog_mnemonics(Widget dialog)2079 suppress_dialog_mnemonics(Widget dialog)
2080 {
2081     if (!dialog)
2082 	return;
2083 
2084     XtUngrabKey(dialog, AnyKey, Mod1Mask);
2085     XtRemoveEventHandler(dialog, KeyPressMask, False,
2086 			   (XtEventHandler) mnemonic_event, (XtPointer) NULL);
2087 }
2088 
2089 #if defined(FEAT_BROWSE) || defined(FEAT_GUI_DIALOG)
2090 /*
2091  * Use the 'guifont' or 'guifontset' as a fontlist for a dialog widget.
2092  */
2093     static void
set_fontlist(Widget id)2094 set_fontlist(Widget id)
2095 {
2096     XmFontList fl;
2097 
2098 #ifdef FONTSET_ALWAYS
2099     if (gui.fontset != NOFONTSET)
2100     {
2101 	fl = gui_motif_fontset2fontlist((XFontSet *)&gui.fontset);
2102 	if (fl != NULL)
2103 	{
2104 	    if (XtIsManaged(id))
2105 	    {
2106 		XtUnmanageChild(id);
2107 		XtVaSetValues(id, XmNfontList, fl, NULL);
2108 		// We should force the widget to recalculate its
2109 		// geometry now.
2110 		XtManageChild(id);
2111 	    }
2112 	    else
2113 		XtVaSetValues(id, XmNfontList, fl, NULL);
2114 	    XmFontListFree(fl);
2115 	}
2116     }
2117 #else
2118     if (gui.norm_font != NOFONT)
2119     {
2120 	fl = gui_motif_create_fontlist((XFontStruct *)gui.norm_font);
2121 	if (fl != NULL)
2122 	{
2123 	    if (XtIsManaged(id))
2124 	    {
2125 		XtUnmanageChild(id);
2126 		XtVaSetValues(id, XmNfontList, fl, NULL);
2127 		// We should force the widget to recalculate its
2128 		// geometry now.
2129 		XtManageChild(id);
2130 	    }
2131 	    else
2132 		XtVaSetValues(id, XmNfontList, fl, NULL);
2133 	    XmFontListFree(fl);
2134 	}
2135     }
2136 #endif
2137 }
2138 #endif
2139 
2140 #if defined(FEAT_BROWSE) || defined(PROTO)
2141 
2142 /*
2143  * file selector related stuff
2144  */
2145 
2146 #include <Xm/FileSB.h>
2147 #include <Xm/XmStrDefs.h>
2148 
2149 typedef struct dialog_callback_arg
2150 {
2151     char *  args;   // not used right now
2152     int	    id;
2153 } dcbarg_T;
2154 
2155 static Widget dialog_wgt;
2156 static char *browse_fname = NULL;
2157 static XmStringCharSet charset = (XmStringCharSet) XmSTRING_DEFAULT_CHARSET;
2158 				// used to set up XmStrings
2159 
2160 static void DialogCancelCB(Widget, XtPointer, XtPointer);
2161 static void DialogAcceptCB(Widget, XtPointer, XtPointer);
2162 
2163 /*
2164  * This function is used to translate the predefined label text of the
2165  * precomposed dialogs. We do this explicitly to allow:
2166  *
2167  * - usage of gettext for translation, as in all the other places.
2168  *
2169  * - equalize the messages between different GUI implementations as far as
2170  * possible.
2171  */
2172     static void
set_predefined_label(Widget parent,String name,char * new_label)2173 set_predefined_label(Widget parent, String name, char *new_label)
2174 {
2175     XmString	str;
2176     Widget	w;
2177     char_u	*p, *next;
2178     KeySym	mnemonic = NUL;
2179 
2180     w = XtNameToWidget(parent, name);
2181 
2182     if (!w)
2183 	return;
2184 
2185     p = vim_strsave((char_u *)new_label);
2186     if (p == NULL)
2187 	return;
2188     for (next = p; *next; ++next)
2189     {
2190 	if (*next == DLG_HOTKEY_CHAR)
2191 	{
2192 	    int len = STRLEN(next);
2193 
2194 	    if (len > 0)
2195 	    {
2196 		mch_memmove(next, next + 1, len);
2197 		mnemonic = next[0];
2198 	    }
2199 	}
2200     }
2201 
2202     str = XmStringCreate((char *)p, STRING_TAG);
2203     vim_free(p);
2204 
2205     if (str != NULL)
2206     {
2207 	XtVaSetValues(w,
2208 		XmNlabelString, str,
2209 		XmNmnemonic, mnemonic,
2210 		NULL);
2211 	XmStringFree(str);
2212     }
2213     gui_motif_menu_fontlist(w);
2214 }
2215 
2216     static void
set_predefined_fontlist(Widget parent,String name)2217 set_predefined_fontlist(Widget parent, String name)
2218 {
2219     Widget w;
2220     w = XtNameToWidget(parent, name);
2221 
2222     if (!w)
2223 	return;
2224 
2225     set_fontlist(w);
2226 }
2227 
2228 /*
2229  * Put up a file requester.
2230  * Returns the selected name in allocated memory, or NULL for Cancel.
2231  */
2232     char_u *
gui_mch_browse(int saving UNUSED,char_u * title,char_u * dflt,char_u * ext UNUSED,char_u * initdir,char_u * filter)2233 gui_mch_browse(
2234     int		saving UNUSED,	// select file to write
2235     char_u	*title,		// title for the window
2236     char_u	*dflt,		// default name
2237     char_u	*ext UNUSED,	// not used (extension added)
2238     char_u	*initdir,	// initial directory, NULL for current dir
2239     char_u	*filter)	// file name filter
2240 {
2241     char_u	dirbuf[MAXPATHL];
2242     char_u	dfltbuf[MAXPATHL];
2243     char_u	*pattern;
2244     char_u	*tofree = NULL;
2245 
2246     // There a difference between the resource name and value, Therefore, we
2247     // avoid to (ab-)use the (maybe internationalized!) dialog title as a
2248     // dialog name.
2249 
2250     dialog_wgt = XmCreateFileSelectionDialog(vimShell, "browseDialog", NULL, 0);
2251 
2252     if (initdir == NULL || *initdir == NUL)
2253     {
2254 	mch_dirname(dirbuf, MAXPATHL);
2255 	initdir = dirbuf;
2256     }
2257 
2258     if (dflt == NULL)
2259 	dflt = (char_u *)"";
2260     else if (STRLEN(initdir) + STRLEN(dflt) + 2 < MAXPATHL)
2261     {
2262 	// The default selection should be the full path, "dflt" is only the
2263 	// file name.
2264 	STRCPY(dfltbuf, initdir);
2265 	add_pathsep(dfltbuf);
2266 	STRCAT(dfltbuf, dflt);
2267 	dflt = dfltbuf;
2268     }
2269 
2270     // Can only use one pattern for a file name.  Get the first pattern out of
2271     // the filter.  An empty pattern means everything matches.
2272     if (filter == NULL)
2273 	pattern = (char_u *)"";
2274     else
2275     {
2276 	char_u	*s, *p;
2277 
2278 	s = filter;
2279 	for (p = filter; *p != NUL; ++p)
2280 	{
2281 	    if (*p == '\t')	// end of description, start of pattern
2282 		s = p + 1;
2283 	    if (*p == ';' || *p == '\n')	// end of (first) pattern
2284 		break;
2285 	}
2286 	pattern = vim_strnsave(s, p - s);
2287 	tofree = pattern;
2288 	if (pattern == NULL)
2289 	    pattern = (char_u *)"";
2290     }
2291 
2292     XtVaSetValues(dialog_wgt,
2293 	XtVaTypedArg,
2294 	    XmNdirectory, XmRString, (char *)initdir, STRLEN(initdir) + 1,
2295 	XtVaTypedArg,
2296 	    XmNdirSpec,	XmRString, (char *)dflt, STRLEN(dflt) + 1,
2297 	XtVaTypedArg,
2298 	    XmNpattern,	XmRString, (char *)pattern, STRLEN(pattern) + 1,
2299 	XtVaTypedArg,
2300 	    XmNdialogTitle, XmRString, (char *)title, STRLEN(title) + 1,
2301 	NULL);
2302 
2303     set_predefined_label(dialog_wgt, "Apply", _("&Filter"));
2304     set_predefined_label(dialog_wgt, "Cancel", _("&Cancel"));
2305     set_predefined_label(dialog_wgt, "Dir", _("Directories"));
2306     set_predefined_label(dialog_wgt, "FilterLabel", _("Filter"));
2307     set_predefined_label(dialog_wgt, "Help", _("&Help"));
2308     set_predefined_label(dialog_wgt, "Items", _("Files"));
2309     set_predefined_label(dialog_wgt, "OK", _("&OK"));
2310     set_predefined_label(dialog_wgt, "Selection", _("Selection"));
2311 
2312     // This is to save us from silly external settings using not fixed with
2313     // fonts for file selection.
2314     set_predefined_fontlist(dialog_wgt, "DirListSW.DirList");
2315     set_predefined_fontlist(dialog_wgt, "ItemsListSW.ItemsList");
2316 
2317     gui_motif_menu_colors(dialog_wgt);
2318     if (gui.scroll_bg_pixel != INVALCOLOR)
2319 	XtVaSetValues(dialog_wgt, XmNtroughColor, gui.scroll_bg_pixel, NULL);
2320 
2321     XtAddCallback(dialog_wgt, XmNokCallback, DialogAcceptCB, (XtPointer)0);
2322     XtAddCallback(dialog_wgt, XmNcancelCallback, DialogCancelCB, (XtPointer)0);
2323     // We have no help in this window, so hide help button
2324     XtUnmanageChild(XmFileSelectionBoxGetChild(dialog_wgt,
2325 					(unsigned char)XmDIALOG_HELP_BUTTON));
2326 
2327     manage_centered(dialog_wgt);
2328     activate_dialog_mnemonics(dialog_wgt);
2329 
2330     // sit in a loop until the dialog box has gone away
2331     do
2332     {
2333 	XtAppProcessEvent(XtWidgetToApplicationContext(dialog_wgt),
2334 	    (XtInputMask)XtIMAll);
2335     } while (XtIsManaged(dialog_wgt));
2336 
2337     suppress_dialog_mnemonics(dialog_wgt);
2338     XtDestroyWidget(dialog_wgt);
2339     vim_free(tofree);
2340 
2341     if (browse_fname == NULL)
2342 	return NULL;
2343     return vim_strsave((char_u *)browse_fname);
2344 }
2345 
2346 /*
2347  * The code below was originally taken from
2348  *	/usr/examples/motif/xmsamplers/xmeditor.c
2349  * on Digital Unix 4.0d, but heavily modified.
2350  */
2351 
2352 /*
2353  * Process callback from Dialog cancel actions.
2354  */
2355     static void
DialogCancelCB(Widget w UNUSED,XtPointer client_data UNUSED,XtPointer call_data UNUSED)2356 DialogCancelCB(
2357     Widget	w UNUSED,		//  widget id
2358     XtPointer	client_data UNUSED,	//  data from application
2359     XtPointer	call_data UNUSED)	//  data from widget class
2360 {
2361     if (browse_fname != NULL)
2362     {
2363 	XtFree(browse_fname);
2364 	browse_fname = NULL;
2365     }
2366     XtUnmanageChild(dialog_wgt);
2367 }
2368 
2369 /*
2370  * Process callback from Dialog actions.
2371  */
2372     static void
DialogAcceptCB(Widget w UNUSED,XtPointer client_data UNUSED,XtPointer call_data)2373 DialogAcceptCB(
2374     Widget	w UNUSED,		//  widget id
2375     XtPointer	client_data UNUSED,	//  data from application
2376     XtPointer	call_data)		//  data from widget class
2377 {
2378     XmFileSelectionBoxCallbackStruct *fcb;
2379 
2380     if (browse_fname != NULL)
2381     {
2382 	XtFree(browse_fname);
2383 	browse_fname = NULL;
2384     }
2385     fcb = (XmFileSelectionBoxCallbackStruct *)call_data;
2386 
2387     // get the filename from the file selection box
2388     XmStringGetLtoR(fcb->value, charset, &browse_fname);
2389 
2390     // popdown the file selection box
2391     XtUnmanageChild(dialog_wgt);
2392 }
2393 
2394 #endif // FEAT_BROWSE
2395 
2396 #if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2397 
2398 static int	dialogStatus;
2399 
2400 /*
2401  * Callback function for the textfield.  When CR is hit this works like
2402  * hitting the "OK" button, ESC like "Cancel".
2403  */
2404     static void
keyhit_callback(Widget w,XtPointer client_data UNUSED,XEvent * event,Boolean * cont UNUSED)2405 keyhit_callback(
2406     Widget		w,
2407     XtPointer		client_data UNUSED,
2408     XEvent		*event,
2409     Boolean		*cont UNUSED)
2410 {
2411     char	buf[2];
2412     KeySym	key_sym;
2413 
2414     if (XLookupString(&(event->xkey), buf, 2, &key_sym, NULL) == 1)
2415     {
2416 	if (*buf == CAR)
2417 	    dialogStatus = 1;
2418 	else if (*buf == ESC)
2419 	    dialogStatus = 2;
2420     }
2421     if ((key_sym == XK_Left || key_sym == XK_Right)
2422 	    && !(event->xkey.state & ShiftMask))
2423 	XmTextFieldClearSelection(w, XtLastTimestampProcessed(gui.dpy));
2424 }
2425 
2426     static void
butproc(Widget w UNUSED,XtPointer client_data,XtPointer call_data UNUSED)2427 butproc(
2428     Widget	w UNUSED,
2429     XtPointer	client_data,
2430     XtPointer	call_data UNUSED)
2431 {
2432     dialogStatus = (int)(long)client_data + 1;
2433 }
2434 
2435 #ifdef HAVE_XPM
2436 
2437     static Widget
create_pixmap_label(Widget parent,String name,char ** data,ArgList args,Cardinal arg)2438 create_pixmap_label(
2439     Widget	parent,
2440     String	name,
2441     char	**data,
2442     ArgList	args,
2443     Cardinal	arg)
2444 {
2445     Widget		label;
2446     Display		*dsp;
2447     Screen		*scr;
2448     int			depth;
2449     Pixmap		pixmap = 0;
2450     XpmAttributes	attr;
2451     Boolean		rs;
2452     XpmColorSymbol	color[5] =
2453     {
2454 	{"none", NULL, 0},
2455 	{"iconColor1", NULL, 0},
2456 	{"bottomShadowColor", NULL, 0},
2457 	{"topShadowColor", NULL, 0},
2458 	{"selectColor", NULL, 0}
2459     };
2460 
2461     label = XmCreateLabelGadget(parent, name, args, arg);
2462 
2463     /*
2464      * We need to be careful here, since in case of gadgets, there is
2465      * no way to get the background color directly from the widget itself.
2466      * In such cases we get it from The Core part of his parent instead.
2467      */
2468     dsp = XtDisplayOfObject(label);
2469     scr = XtScreenOfObject(label);
2470     XtVaGetValues(XtIsSubclass(label, coreWidgetClass)
2471 	    ?  label : XtParent(label),
2472 		  XmNdepth, &depth,
2473 		  XmNbackground, &color[0].pixel,
2474 		  XmNforeground, &color[1].pixel,
2475 		  XmNbottomShadowColor, &color[2].pixel,
2476 		  XmNtopShadowColor, &color[3].pixel,
2477 		  XmNhighlight, &color[4].pixel,
2478 		  NULL);
2479 
2480     attr.valuemask = XpmColorSymbols | XpmCloseness | XpmDepth;
2481     attr.colorsymbols = color;
2482     attr.numsymbols = 5;
2483     attr.closeness = 65535;
2484     attr.depth = depth;
2485     XpmCreatePixmapFromData(dsp, RootWindowOfScreen(scr),
2486 		    data, &pixmap, NULL, &attr);
2487 
2488     XtVaGetValues(label, XmNrecomputeSize, &rs, NULL);
2489     XtVaSetValues(label, XmNrecomputeSize, True, NULL);
2490     XtVaSetValues(label,
2491 	    XmNlabelType, XmPIXMAP,
2492 	    XmNlabelPixmap, pixmap,
2493 	    NULL);
2494     XtVaSetValues(label, XmNrecomputeSize, rs, NULL);
2495 
2496     return label;
2497 }
2498 #endif
2499 
2500     int
gui_mch_dialog(int type UNUSED,char_u * title,char_u * message,char_u * button_names,int dfltbutton,char_u * textfield,int ex_cmd UNUSED)2501 gui_mch_dialog(
2502     int		type UNUSED,
2503     char_u	*title,
2504     char_u	*message,
2505     char_u	*button_names,
2506     int		dfltbutton,
2507     char_u	*textfield,		// buffer of size IOSIZE
2508     int		ex_cmd UNUSED)
2509 {
2510     char_u		*buts;
2511     char_u		*p, *next;
2512     XtAppContext	app;
2513     XmString		label;
2514     int			butcount;
2515     Widget		w;
2516     Widget		dialogform = NULL;
2517     Widget		form = NULL;
2518     Widget		dialogtextfield = NULL;
2519     Widget		*buttons;
2520     Widget		sep_form = NULL;
2521     Boolean		vertical;
2522     Widget		separator = NULL;
2523     int			n;
2524     Arg			args[6];
2525 #ifdef HAVE_XPM
2526     char		**icon_data = NULL;
2527     Widget		dialogpixmap = NULL;
2528 #endif
2529 
2530     if (title == NULL)
2531 	title = (char_u *)_("Vim dialog");
2532 
2533     // if our pointer is currently hidden, then we should show it.
2534     gui_mch_mousehide(FALSE);
2535 
2536     dialogform = XmCreateFormDialog(vimShell, (char *)"dialog", NULL, 0);
2537 
2538     // Check 'v' flag in 'guioptions': vertical button placement.
2539     vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL);
2540 
2541     // Set the title of the Dialog window
2542     label = XmStringCreateSimple((char *)title);
2543     if (label == NULL)
2544 	return -1;
2545     XtVaSetValues(dialogform,
2546 	    XmNdialogTitle, label,
2547 	    XmNhorizontalSpacing, 4,
2548 	    XmNverticalSpacing, vertical ? 0 : 4,
2549 	    NULL);
2550     XmStringFree(label);
2551 
2552     // make a copy, so that we can insert NULs
2553     buts = vim_strsave(button_names);
2554     if (buts == NULL)
2555 	return -1;
2556 
2557     // Count the number of buttons and allocate buttons[].
2558     butcount = 1;
2559     for (p = buts; *p; ++p)
2560 	if (*p == DLG_BUTTON_SEP)
2561 	    ++butcount;
2562     buttons = ALLOC_MULT(Widget, butcount);
2563     if (buttons == NULL)
2564     {
2565 	vim_free(buts);
2566 	return -1;
2567     }
2568 
2569     /*
2570      * Create the buttons.
2571      */
2572     sep_form = (Widget) 0;
2573     p = buts;
2574     for (butcount = 0; *p; ++butcount)
2575     {
2576 	KeySym mnemonic = NUL;
2577 
2578 	for (next = p; *next; ++next)
2579 	{
2580 	    if (*next == DLG_HOTKEY_CHAR)
2581 	    {
2582 		int len = STRLEN(next);
2583 
2584 		if (len > 0)
2585 		{
2586 		    mch_memmove(next, next + 1, len);
2587 		    mnemonic = next[0];
2588 		}
2589 	    }
2590 	    if (*next == DLG_BUTTON_SEP)
2591 	    {
2592 		*next++ = NUL;
2593 		break;
2594 	    }
2595 	}
2596 	label = XmStringCreate(_((char *)p), STRING_TAG);
2597 	if (label == NULL)
2598 	    break;
2599 
2600 	buttons[butcount] = XtVaCreateManagedWidget("button",
2601 		xmPushButtonWidgetClass, dialogform,
2602 		XmNlabelString, label,
2603 		XmNmnemonic, mnemonic,
2604 		XmNbottomAttachment, XmATTACH_FORM,
2605 		XmNbottomOffset, 4,
2606 		XmNshowAsDefault, butcount == dfltbutton - 1,
2607 		XmNdefaultButtonShadowThickness, 1,
2608 		NULL);
2609 	XmStringFree(label);
2610 	gui_motif_menu_fontlist(buttons[butcount]);
2611 
2612 	// Layout properly.
2613 
2614 	if (butcount > 0)
2615 	{
2616 	    if (vertical)
2617 		XtVaSetValues(buttons[butcount],
2618 			XmNtopWidget, buttons[butcount - 1],
2619 			NULL);
2620 	    else
2621 	    {
2622 		if (*next == NUL)
2623 		{
2624 		    XtVaSetValues(buttons[butcount],
2625 			    XmNrightAttachment, XmATTACH_FORM,
2626 			    XmNrightOffset, 4,
2627 			    NULL);
2628 
2629 		    // fill in a form as invisible separator
2630 		    sep_form = XtVaCreateWidget("separatorForm",
2631 			    xmFormWidgetClass,	dialogform,
2632 			    XmNleftAttachment, XmATTACH_WIDGET,
2633 			    XmNleftWidget, buttons[butcount - 1],
2634 			    XmNrightAttachment, XmATTACH_WIDGET,
2635 			    XmNrightWidget, buttons[butcount],
2636 			    XmNbottomAttachment, XmATTACH_FORM,
2637 			    XmNbottomOffset, 4,
2638 			    NULL);
2639 		    XtManageChild(sep_form);
2640 		}
2641 		else
2642 		{
2643 		    XtVaSetValues(buttons[butcount],
2644 			    XmNleftAttachment, XmATTACH_WIDGET,
2645 			    XmNleftWidget, buttons[butcount - 1],
2646 			    NULL);
2647 		}
2648 	    }
2649 	}
2650 	else if (!vertical)
2651 	{
2652 	    if (*next == NUL)
2653 	    {
2654 		XtVaSetValues(buttons[0],
2655 			XmNrightAttachment, XmATTACH_FORM,
2656 			XmNrightOffset, 4,
2657 			NULL);
2658 
2659 		// fill in a form as invisible separator
2660 		sep_form = XtVaCreateWidget("separatorForm",
2661 			xmFormWidgetClass, dialogform,
2662 			XmNleftAttachment, XmATTACH_FORM,
2663 			XmNleftOffset, 4,
2664 			XmNrightAttachment, XmATTACH_WIDGET,
2665 			XmNrightWidget, buttons[0],
2666 			XmNbottomAttachment, XmATTACH_FORM,
2667 			XmNbottomOffset, 4,
2668 			NULL);
2669 		XtManageChild(sep_form);
2670 	    }
2671 	    else
2672 		XtVaSetValues(buttons[0],
2673 			XmNleftAttachment, XmATTACH_FORM,
2674 			XmNleftOffset, 4,
2675 			NULL);
2676 	}
2677 
2678 	XtAddCallback(buttons[butcount], XmNactivateCallback,
2679 			  (XtCallbackProc)butproc, (XtPointer)(long)butcount);
2680 	p = next;
2681     }
2682     vim_free(buts);
2683 
2684     separator = (Widget) 0;
2685     if (butcount > 0)
2686     {
2687 	// Create the separator for beauty.
2688 	n = 0;
2689 	XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
2690 	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
2691 	XtSetArg(args[n], XmNbottomWidget, buttons[0]); n++;
2692 	XtSetArg(args[n], XmNbottomOffset, 4); n++;
2693 	XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
2694 	XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
2695 	separator = XmCreateSeparatorGadget(dialogform, "separator", args, n);
2696 	XtManageChild(separator);
2697     }
2698 
2699     if (textfield != NULL)
2700     {
2701 	dialogtextfield = XtVaCreateWidget("textField",
2702 		xmTextFieldWidgetClass, dialogform,
2703 		XmNleftAttachment, XmATTACH_FORM,
2704 		XmNrightAttachment, XmATTACH_FORM,
2705 		NULL);
2706 	if (butcount > 0)
2707 	    XtVaSetValues(dialogtextfield,
2708 		    XmNbottomAttachment, XmATTACH_WIDGET,
2709 		    XmNbottomWidget, separator,
2710 		    NULL);
2711 	else
2712 	    XtVaSetValues(dialogtextfield,
2713 		    XmNbottomAttachment, XmATTACH_FORM,
2714 		    NULL);
2715 
2716 	set_fontlist(dialogtextfield);
2717 	XmTextFieldSetString(dialogtextfield, (char *)textfield);
2718 	XtManageChild(dialogtextfield);
2719 	XtAddEventHandler(dialogtextfield, KeyPressMask, False,
2720 			    (XtEventHandler)keyhit_callback, (XtPointer)NULL);
2721     }
2722 
2723     // Form holding both message and pixmap labels
2724     form = XtVaCreateWidget("separatorForm",
2725 	    xmFormWidgetClass, dialogform,
2726 	    XmNleftAttachment, XmATTACH_FORM,
2727 	    XmNrightAttachment, XmATTACH_FORM,
2728 	    XmNtopAttachment, XmATTACH_FORM,
2729 	    NULL);
2730     XtManageChild(form);
2731 
2732 #ifdef HAVE_XPM
2733     // Add a pixmap, left of the message.
2734     switch (type)
2735     {
2736 	case VIM_GENERIC:
2737 	    icon_data = generic_xpm;
2738 	    break;
2739 	case VIM_ERROR:
2740 	    icon_data = error_xpm;
2741 	    break;
2742 	case VIM_WARNING:
2743 	    icon_data = alert_xpm;
2744 	    break;
2745 	case VIM_INFO:
2746 	    icon_data = info_xpm;
2747 	    break;
2748 	case VIM_QUESTION:
2749 	    icon_data = quest_xpm;
2750 	    break;
2751 	default:
2752 	    icon_data = generic_xpm;
2753     }
2754 
2755     n = 0;
2756     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
2757     XtSetArg(args[n], XmNtopOffset, 8); n++;
2758     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
2759     XtSetArg(args[n], XmNbottomOffset, 8); n++;
2760     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
2761     XtSetArg(args[n], XmNleftOffset, 8); n++;
2762 
2763     dialogpixmap = create_pixmap_label(form, "dialogPixmap",
2764 	    icon_data, args, n);
2765     XtManageChild(dialogpixmap);
2766 #endif
2767 
2768     // Create the dialog message.
2769     // Since LessTif is apparently having problems with the creation of
2770     // properly localized string, we use LtoR here. The symptom is that the
2771     // string sill not show properly in multiple lines as it does in native
2772     // Motif.
2773     label = XmStringCreateLtoR((char *)message, STRING_TAG);
2774     if (label == NULL)
2775 	return -1;
2776     w = XtVaCreateManagedWidget("dialogMessage",
2777 				xmLabelGadgetClass, form,
2778 				XmNlabelString, label,
2779 				XmNalignment, XmALIGNMENT_BEGINNING,
2780 				XmNtopAttachment, XmATTACH_FORM,
2781 				XmNtopOffset, 8,
2782 #ifdef HAVE_XPM
2783 				XmNleftAttachment, XmATTACH_WIDGET,
2784 				XmNleftWidget, dialogpixmap,
2785 #else
2786 				XmNleftAttachment, XmATTACH_FORM,
2787 #endif
2788 				XmNleftOffset, 8,
2789 				XmNrightAttachment, XmATTACH_FORM,
2790 				XmNrightOffset, 8,
2791 				XmNbottomAttachment, XmATTACH_FORM,
2792 				XmNbottomOffset, 8,
2793 				NULL);
2794     XmStringFree(label);
2795     set_fontlist(w);
2796 
2797     if (textfield != NULL)
2798     {
2799 	XtVaSetValues(form,
2800 		XmNbottomAttachment, XmATTACH_WIDGET,
2801 		XmNbottomWidget, dialogtextfield,
2802 		NULL);
2803     }
2804     else
2805     {
2806 	if (butcount > 0)
2807 	    XtVaSetValues(form,
2808 		    XmNbottomAttachment, XmATTACH_WIDGET,
2809 		    XmNbottomWidget, separator,
2810 		    NULL);
2811 	else
2812 	    XtVaSetValues(form,
2813 		    XmNbottomAttachment, XmATTACH_FORM,
2814 		    NULL);
2815     }
2816 
2817     if (dfltbutton < 1)
2818 	dfltbutton = 1;
2819     if (dfltbutton > butcount)
2820 	dfltbutton = butcount;
2821     XtVaSetValues(dialogform,
2822 	    XmNdefaultButton, buttons[dfltbutton - 1], NULL);
2823     if (textfield != NULL)
2824 	XtVaSetValues(dialogform, XmNinitialFocus, dialogtextfield, NULL);
2825     else
2826 	XtVaSetValues(dialogform, XmNinitialFocus, buttons[dfltbutton - 1],
2827 									NULL);
2828 
2829     manage_centered(dialogform);
2830     activate_dialog_mnemonics(dialogform);
2831 
2832     if (textfield != NULL && *textfield != NUL)
2833     {
2834 	// This only works after the textfield has been realised.
2835 	XmTextFieldSetSelection(dialogtextfield,
2836 			 (XmTextPosition)0, (XmTextPosition)STRLEN(textfield),
2837 					   XtLastTimestampProcessed(gui.dpy));
2838 	XmTextFieldSetCursorPosition(dialogtextfield,
2839 					   (XmTextPosition)STRLEN(textfield));
2840     }
2841 
2842     app = XtWidgetToApplicationContext(dialogform);
2843 
2844     // Loop until a button is pressed or the dialog is killed somehow.
2845     dialogStatus = -1;
2846     for (;;)
2847     {
2848 	XtAppProcessEvent(app, (XtInputMask)XtIMAll);
2849 	if (dialogStatus >= 0 || !XtIsManaged(dialogform))
2850 	    break;
2851     }
2852 
2853     vim_free(buttons);
2854 
2855     if (textfield != NULL)
2856     {
2857 	p = (char_u *)XmTextGetString(dialogtextfield);
2858 	if (p == NULL || dialogStatus < 0)
2859 	    *textfield = NUL;
2860 	else
2861 	    vim_strncpy(textfield, p, IOSIZE - 1);
2862 	XtFree((char *)p);
2863     }
2864 
2865     suppress_dialog_mnemonics(dialogform);
2866     XtDestroyWidget(dialogform);
2867 
2868     return dialogStatus;
2869 }
2870 #endif // FEAT_GUI_DIALOG
2871 
2872 #if defined(FEAT_FOOTER) || defined(PROTO)
2873 
2874     static int
gui_mch_compute_footer_height(void)2875 gui_mch_compute_footer_height(void)
2876 {
2877     Dimension	height;		    // total Toolbar height
2878     Dimension	top;		    // XmNmarginTop
2879     Dimension	bottom;		    // XmNmarginBottom
2880     Dimension	shadow;		    // XmNshadowThickness
2881 
2882     XtVaGetValues(footer,
2883 	    XmNheight, &height,
2884 	    XmNmarginTop, &top,
2885 	    XmNmarginBottom, &bottom,
2886 	    XmNshadowThickness, &shadow,
2887 	    NULL);
2888 
2889     return (int) height + top + bottom + (shadow << 1);
2890 }
2891 
2892     void
gui_mch_enable_footer(int showit)2893 gui_mch_enable_footer(int showit)
2894 {
2895     if (showit)
2896     {
2897 	gui.footer_height = gui_mch_compute_footer_height();
2898 	XtManageChild(footer);
2899     }
2900     else
2901     {
2902 	gui.footer_height = 0;
2903 	XtUnmanageChild(footer);
2904     }
2905     XtVaSetValues(textAreaForm, XmNbottomOffset, gui.footer_height, NULL);
2906 }
2907 
2908     void
gui_mch_set_footer(char_u * s)2909 gui_mch_set_footer(char_u *s)
2910 {
2911     XmString	xms;
2912 
2913     xms = XmStringCreate((char *)s, STRING_TAG);
2914     if (xms != NULL)
2915     {
2916 	XtVaSetValues(footer, XmNlabelString, xms, NULL);
2917 	XmStringFree(xms);
2918     }
2919 }
2920 
2921 #endif
2922 
2923 
2924 #if defined(FEAT_TOOLBAR) || defined(PROTO)
2925     void
gui_mch_show_toolbar(int showit)2926 gui_mch_show_toolbar(int showit)
2927 {
2928     Cardinal	numChildren;	    // how many children toolBar has
2929 
2930     if (toolBar == (Widget)0)
2931 	return;
2932     XtVaGetValues(toolBar, XmNnumChildren, &numChildren, NULL);
2933     if (showit && numChildren > 0)
2934     {
2935 	// Assume that we want to show the toolbar if p_toolbar contains
2936 	// valid option settings, therefore p_toolbar must not be NULL.
2937 	WidgetList  children;
2938 
2939 	XtVaGetValues(toolBar, XmNchildren, &children, NULL);
2940 	{
2941 	    void    (*action)(BalloonEval *);
2942 	    int	    text = 0;
2943 
2944 	    if (strstr((const char *)p_toolbar, "tooltips"))
2945 		action = &gui_mch_enable_beval_area;
2946 	    else
2947 		action = &gui_mch_disable_beval_area;
2948 	    if (strstr((const char *)p_toolbar, "text"))
2949 		text = 1;
2950 	    else if (strstr((const char *)p_toolbar, "icons"))
2951 		text = -1;
2952 	    if (text != 0)
2953 	    {
2954 		vimmenu_T   *toolbar;
2955 		vimmenu_T   *cur;
2956 
2957 		FOR_ALL_MENUS(toolbar)
2958 		    if (menu_is_toolbar(toolbar->dname))
2959 			break;
2960 		// Assumption: toolbar is NULL if there is no toolbar,
2961 		//	       otherwise it contains the toolbar menu structure.
2962 		//
2963 		// Assumption: "numChildren" == the number of items in the list
2964 		//	       of items beginning with toolbar->children.
2965 		if (toolbar)
2966 		{
2967 		    for (cur = toolbar->children; cur; cur = cur->next)
2968 		    {
2969 			Arg	    args[1];
2970 			int	    n = 0;
2971 
2972 			// Enable/Disable tooltip (OK to enable while
2973 			// currently enabled).
2974 			if (cur->tip != NULL)
2975 			    (*action)(cur->tip);
2976 			if (!menu_is_separator(cur->name))
2977 			{
2978 			    if (text == 1 || cur->xpm == NULL)
2979 			    {
2980 				XtSetArg(args[n], XmNlabelType, XmSTRING);
2981 				++n;
2982 			    }
2983 			    if (cur->id != NULL)
2984 			    {
2985 				XtUnmanageChild(cur->id);
2986 				XtSetValues(cur->id, args, n);
2987 				XtManageChild(cur->id);
2988 			    }
2989 			}
2990 		    }
2991 		}
2992 	    }
2993 	}
2994 	gui.toolbar_height = gui_mch_compute_toolbar_height();
2995 	XtManageChild(XtParent(toolBar));
2996 #ifdef FEAT_GUI_TABLINE
2997 	if (showing_tabline)
2998 	{
2999 	    XtVaSetValues(tabLine,
3000 			  XmNtopAttachment, XmATTACH_WIDGET,
3001 			  XmNtopWidget, XtParent(toolBar),
3002 			  NULL);
3003 	    XtVaSetValues(textAreaForm,
3004 			  XmNtopAttachment, XmATTACH_WIDGET,
3005 			  XmNtopWidget, tabLine,
3006 			  NULL);
3007 	}
3008 	else
3009 #endif
3010 	    XtVaSetValues(textAreaForm,
3011 			  XmNtopAttachment, XmATTACH_WIDGET,
3012 			  XmNtopWidget, XtParent(toolBar),
3013 			  NULL);
3014 	if (XtIsManaged(menuBar))
3015 	    XtVaSetValues(XtParent(toolBar),
3016 		    XmNtopAttachment, XmATTACH_WIDGET,
3017 		    XmNtopWidget, menuBar,
3018 		    NULL);
3019 	else
3020 	    XtVaSetValues(XtParent(toolBar),
3021 		    XmNtopAttachment, XmATTACH_FORM,
3022 		    NULL);
3023     }
3024     else
3025     {
3026 	gui.toolbar_height = 0;
3027 	if (XtIsManaged(menuBar))
3028 	{
3029 #ifdef FEAT_GUI_TABLINE
3030 	    if (showing_tabline)
3031 	    {
3032 		XtVaSetValues(tabLine,
3033 			      XmNtopAttachment, XmATTACH_WIDGET,
3034 			      XmNtopWidget, menuBar,
3035 			      NULL);
3036 		XtVaSetValues(textAreaForm,
3037 			      XmNtopAttachment, XmATTACH_WIDGET,
3038 			      XmNtopWidget, tabLine,
3039 			      NULL);
3040 	    }
3041 	    else
3042 #endif
3043 		XtVaSetValues(textAreaForm,
3044 			      XmNtopAttachment, XmATTACH_WIDGET,
3045 			      XmNtopWidget, menuBar,
3046 			      NULL);
3047 	}
3048 	else
3049 	{
3050 #ifdef FEAT_GUI_TABLINE
3051 	    if (showing_tabline)
3052 	    {
3053 		XtVaSetValues(tabLine,
3054 			      XmNtopAttachment, XmATTACH_FORM,
3055 			      NULL);
3056 		XtVaSetValues(textAreaForm,
3057 			      XmNtopAttachment, XmATTACH_WIDGET,
3058 			      XmNtopWidget, tabLine,
3059 			      NULL);
3060 	    }
3061 	    else
3062 #endif
3063 		XtVaSetValues(textAreaForm,
3064 			      XmNtopAttachment, XmATTACH_FORM,
3065 			      NULL);
3066 	}
3067 
3068 	XtUnmanageChild(XtParent(toolBar));
3069     }
3070     gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3071 }
3072 
3073 /*
3074  * A toolbar button has been pushed; now reset the input focus
3075  * such that the user can type page up/down etc. and have the
3076  * input go to the editor window, not the button
3077  */
3078     static void
reset_focus(void)3079 reset_focus(void)
3080 {
3081     if (textArea != NULL)
3082 	XmProcessTraversal(textArea, XmTRAVERSE_CURRENT);
3083 }
3084 
3085     int
gui_mch_compute_toolbar_height(void)3086 gui_mch_compute_toolbar_height(void)
3087 {
3088     Dimension	borders;
3089     Dimension	height;		    // total Toolbar height
3090     Dimension	whgt;		    // height of each widget
3091     WidgetList	children;	    // list of toolBar's children
3092     Cardinal	numChildren;	    // how many children toolBar has
3093     int		i;
3094 
3095     borders = 0;
3096     height = 0;
3097     if (toolBar != (Widget)0 && toolBarFrame != (Widget)0)
3098     {				    // get height of XmFrame parent
3099 	Dimension	fst;
3100 	Dimension	fmh;
3101 	Dimension	tst;
3102 	Dimension	tmh;
3103 
3104 	XtVaGetValues(toolBarFrame,
3105 		XmNshadowThickness, &fst,
3106 		XmNmarginHeight, &fmh,
3107 		NULL);
3108 	borders += fst + fmh;
3109 	XtVaGetValues(toolBar,
3110 		XmNshadowThickness, &tst,
3111 		XmNmarginHeight, &tmh,
3112 		XmNchildren, &children,
3113 		XmNnumChildren, &numChildren, NULL);
3114 	borders += tst + tmh;
3115 	for (i = 0; i < (int)numChildren; i++)
3116 	{
3117 	    whgt = 0;
3118 	    XtVaGetValues(children[i], XmNheight, &whgt, NULL);
3119 	    if (height < whgt)
3120 		height = whgt;
3121 	}
3122     }
3123 #ifdef LESSTIF_VERSION
3124     // Hack: When starting up we get wrong dimensions.
3125     if (height < 10)
3126 	height = 24;
3127 #endif
3128 
3129     return (int)(height + (borders << 1));
3130 }
3131 
3132     void
motif_get_toolbar_colors(Pixel * bgp,Pixel * fgp,Pixel * bsp,Pixel * tsp,Pixel * hsp)3133 motif_get_toolbar_colors(
3134     Pixel       *bgp,
3135     Pixel       *fgp,
3136     Pixel       *bsp,
3137     Pixel       *tsp,
3138     Pixel       *hsp)
3139 {
3140     XtVaGetValues(toolBar,
3141 	    XmNbackground, bgp,
3142 	    XmNforeground, fgp,
3143 	    XmNbottomShadowColor, bsp,
3144 	    XmNtopShadowColor, tsp,
3145 	    XmNhighlightColor, hsp,
3146 	    NULL);
3147 }
3148 
3149 # ifdef FEAT_FOOTER
3150 /*
3151  * The next toolbar enter/leave callbacks should really do balloon help.  But
3152  * I have to use footer help for backwards compatibility.  Hopefully both will
3153  * get implemented and the user will have a choice.
3154  */
3155     static void
toolbarbutton_enter_cb(Widget w UNUSED,XtPointer client_data,XEvent * event UNUSED,Boolean * cont UNUSED)3156 toolbarbutton_enter_cb(
3157     Widget	w UNUSED,
3158     XtPointer	client_data,
3159     XEvent	*event UNUSED,
3160     Boolean	*cont UNUSED)
3161 {
3162     vimmenu_T	*menu = (vimmenu_T *) client_data;
3163 
3164     if (menu->strings[MENU_INDEX_TIP] != NULL)
3165     {
3166 	if (vim_strchr(p_go, GO_FOOTER) != NULL)
3167 	    gui_mch_set_footer(menu->strings[MENU_INDEX_TIP]);
3168     }
3169 }
3170 
3171     static void
toolbarbutton_leave_cb(Widget w UNUSED,XtPointer client_data UNUSED,XEvent * event UNUSED,Boolean * cont UNUSED)3172 toolbarbutton_leave_cb(
3173     Widget	w UNUSED,
3174     XtPointer	client_data UNUSED,
3175     XEvent	*event UNUSED,
3176     Boolean	*cont UNUSED)
3177 {
3178     gui_mch_set_footer((char_u *) "");
3179 }
3180 # endif
3181 #endif
3182 
3183 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3184 /*
3185  * Show or hide the tabline.
3186  */
3187     void
gui_mch_show_tabline(int showit)3188 gui_mch_show_tabline(int showit)
3189 {
3190     if (tabLine == (Widget)0)
3191 	return;
3192 
3193     if (!showit != !showing_tabline)
3194     {
3195 	if (showit)
3196 	{
3197 	    XtManageChild(tabLine);
3198 	    XtUnmanageChild(XtNameToWidget(tabLine, "PageScroller"));
3199 	    XtUnmanageChild(XtNameToWidget(tabLine, "MinorTabScrollerNext"));
3200 	    XtUnmanageChild(XtNameToWidget(tabLine,
3201 					   "MinorTabScrollerPrevious"));
3202 #ifdef FEAT_MENU
3203 # ifdef FEAT_TOOLBAR
3204 	    if (XtIsManaged(XtParent(toolBar)))
3205 		XtVaSetValues(tabLine,
3206 			      XmNtopAttachment, XmATTACH_WIDGET,
3207 			      XmNtopWidget, XtParent(toolBar), NULL);
3208 	    else
3209 # endif
3210 		if (XtIsManaged(menuBar))
3211 		XtVaSetValues(tabLine,
3212 			      XmNtopAttachment, XmATTACH_WIDGET,
3213 			      XmNtopWidget, menuBar, NULL);
3214 	    else
3215 #endif
3216 		XtVaSetValues(tabLine,
3217 			      XmNtopAttachment, XmATTACH_FORM, NULL);
3218 	    XtVaSetValues(textAreaForm,
3219 			  XmNtopAttachment, XmATTACH_WIDGET,
3220 			  XmNtopWidget, tabLine,
3221 			  NULL);
3222 	}
3223 	else
3224 	{
3225 	    XtUnmanageChild(tabLine);
3226 #ifdef FEAT_MENU
3227 # ifdef FEAT_TOOLBAR
3228 	    if (XtIsManaged(XtParent(toolBar)))
3229 		XtVaSetValues(textAreaForm,
3230 			      XmNtopAttachment, XmATTACH_WIDGET,
3231 			      XmNtopWidget, XtParent(toolBar), NULL);
3232 	    else
3233 # endif
3234 		if (XtIsManaged(menuBar))
3235 		XtVaSetValues(textAreaForm,
3236 			      XmNtopAttachment, XmATTACH_WIDGET,
3237 			      XmNtopWidget, menuBar, NULL);
3238 	    else
3239 #endif
3240 		XtVaSetValues(textAreaForm,
3241 			      XmNtopAttachment, XmATTACH_FORM, NULL);
3242 	}
3243 	showing_tabline = showit;
3244     }
3245 }
3246 
3247 /*
3248  * Return TRUE when tabline is displayed.
3249  */
3250     int
gui_mch_showing_tabline(void)3251 gui_mch_showing_tabline(void)
3252 {
3253     return tabLine != (Widget)0 && showing_tabline;
3254 }
3255 
3256 /*
3257  * Update the labels of the tabline.
3258  */
3259     void
gui_mch_update_tabline(void)3260 gui_mch_update_tabline(void)
3261 {
3262     tabpage_T		*tp;
3263     int			nr = 1, n;
3264     Arg			args[10];
3265     int			curtabidx = 0, currentpage;
3266     Widget		tab;
3267     XmNotebookPageInfo	page_info;
3268     XmNotebookPageStatus page_status;
3269     int			last_page, tab_count;
3270     XmString		label_str;
3271     char		*label_cstr;
3272     BalloonEval		*beval;
3273 
3274     if (tabLine == (Widget)0)
3275 	return;
3276 
3277     // Add a label for each tab page.  They all contain the same text area.
3278     for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3279     {
3280 	if (tp == curtab)
3281 	    curtabidx = nr;
3282 
3283 	page_status = XmNotebookGetPageInfo(tabLine, nr, &page_info);
3284 	if (page_status == XmPAGE_INVALID
3285 		|| page_info.major_tab_widget == (Widget)0)
3286 	{
3287 	    // Add the tab
3288 	    n = 0;
3289 	    XtSetArg(args[n], XmNnotebookChildType, XmMAJOR_TAB); n++;
3290 	    XtSetArg(args[n], XmNtraversalOn, False); n++;
3291 	    XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
3292 	    XtSetArg(args[n], XmNhighlightThickness, 1); n++;
3293 	    XtSetArg(args[n], XmNshadowThickness , 1); n++;
3294 	    tab = XmCreatePushButton(tabLine, "-Empty-", args, n);
3295 	    XtManageChild(tab);
3296 	    beval = gui_mch_create_beval_area(tab, NULL, tabline_balloon_cb,
3297 									NULL);
3298 	    XtVaSetValues(tab, XmNuserData, beval, NULL);
3299 	}
3300 	else
3301 	    tab = page_info.major_tab_widget;
3302 
3303 	XtVaSetValues(tab, XmNpageNumber, nr, NULL);
3304 
3305 	/*
3306 	 * Change the label text only if it is different
3307 	 */
3308 	XtVaGetValues(tab, XmNlabelString, &label_str, NULL);
3309 	if (XmStringGetLtoR(label_str, XmSTRING_DEFAULT_CHARSET, &label_cstr))
3310 	{
3311 	    get_tabline_label(tp, FALSE);
3312 	    if (STRCMP(label_cstr, NameBuff) != 0)
3313 	    {
3314 		XtVaSetValues(tab, XtVaTypedArg, XmNlabelString, XmRString,
3315 			      NameBuff, STRLEN(NameBuff) + 1, NULL);
3316 		/*
3317 		 * Force a resize of the tab label button
3318 		 */
3319 		XtUnmanageChild(tab);
3320 		XtManageChild(tab);
3321 	    }
3322 	    XtFree(label_cstr);
3323 	}
3324     }
3325 
3326     tab_count = nr - 1;
3327 
3328     XtVaGetValues(tabLine, XmNlastPageNumber, &last_page, NULL);
3329 
3330     // Remove any old labels.
3331     while (nr <= last_page)
3332     {
3333 	if (XmNotebookGetPageInfo(tabLine, nr, &page_info) != XmPAGE_INVALID
3334 	    && page_info.page_number == nr
3335 	    && page_info.major_tab_widget != (Widget)0)
3336 	{
3337 	    XtVaGetValues(page_info.major_tab_widget, XmNuserData, &beval, NULL);
3338 	    if (beval != NULL)
3339 		gui_mch_destroy_beval_area(beval);
3340 	    XtUnmanageChild(page_info.major_tab_widget);
3341 	    XtDestroyWidget(page_info.major_tab_widget);
3342 	}
3343 	nr++;
3344     }
3345 
3346     XtVaSetValues(tabLine, XmNlastPageNumber, tab_count, NULL);
3347 
3348     XtVaGetValues(tabLine, XmNcurrentPageNumber, &currentpage, NULL);
3349     if (currentpage != curtabidx)
3350 	XtVaSetValues(tabLine, XmNcurrentPageNumber, curtabidx, NULL);
3351 }
3352 
3353 /*
3354  * Set the current tab to "nr".  First tab is 1.
3355  */
3356     void
gui_mch_set_curtab(int nr)3357 gui_mch_set_curtab(int nr)
3358 {
3359     int		currentpage;
3360 
3361     if (tabLine == (Widget)0)
3362 	return;
3363 
3364     XtVaGetValues(tabLine, XmNcurrentPageNumber, &currentpage, NULL);
3365     if (currentpage != nr)
3366 	XtVaSetValues(tabLine, XmNcurrentPageNumber, nr, NULL);
3367 }
3368 #endif
3369 
3370 /*
3371  * Set the colors of Widget "id" to the menu colors.
3372  */
3373     static void
gui_motif_menu_colors(Widget id)3374 gui_motif_menu_colors(Widget id)
3375 {
3376     if (gui.menu_bg_pixel != INVALCOLOR)
3377 #if (XmVersion >= 1002)
3378 	XmChangeColor(id, gui.menu_bg_pixel);
3379 #else
3380 	XtVaSetValues(id, XmNbackground, gui.menu_bg_pixel, NULL);
3381 #endif
3382     if (gui.menu_fg_pixel != INVALCOLOR)
3383 	XtVaSetValues(id, XmNforeground, gui.menu_fg_pixel, NULL);
3384 }
3385 
3386 /*
3387  * Set the colors of Widget "id" to the scrollbar colors.
3388  */
3389     static void
gui_motif_scroll_colors(Widget id)3390 gui_motif_scroll_colors(Widget id)
3391 {
3392     if (gui.scroll_bg_pixel != INVALCOLOR)
3393 #if (XmVersion >= 1002)
3394 	XmChangeColor(id, gui.scroll_bg_pixel);
3395 #else
3396 	XtVaSetValues(id, XmNbackground, gui.scroll_bg_pixel, NULL);
3397 #endif
3398     if (gui.scroll_fg_pixel != INVALCOLOR)
3399 	XtVaSetValues(id, XmNforeground, gui.scroll_fg_pixel, NULL);
3400 }
3401 
3402 /*
3403  * Set the fontlist for Widget "id" to use gui.menu_fontset or gui.menu_font.
3404  */
3405     void
gui_motif_menu_fontlist(Widget id UNUSED)3406 gui_motif_menu_fontlist(Widget id UNUSED)
3407 {
3408 #ifdef FEAT_MENU
3409 #ifdef FONTSET_ALWAYS
3410     if (gui.menu_fontset != NOFONTSET)
3411     {
3412 	XmFontList fl;
3413 
3414 	fl = gui_motif_fontset2fontlist((XFontSet *)&gui.menu_fontset);
3415 	if (fl != NULL)
3416 	{
3417 	    if (XtIsManaged(id))
3418 	    {
3419 		XtUnmanageChild(id);
3420 		XtVaSetValues(id, XmNfontList, fl, NULL);
3421 		// We should force the widget to recalculate its
3422 		// geometry now.
3423 		XtManageChild(id);
3424 	    }
3425 	    else
3426 		XtVaSetValues(id, XmNfontList, fl, NULL);
3427 	    XmFontListFree(fl);
3428 	}
3429     }
3430 #else
3431     if (gui.menu_font != NOFONT)
3432     {
3433 	XmFontList fl;
3434 
3435 	fl = gui_motif_create_fontlist((XFontStruct *)gui.menu_font);
3436 	if (fl != NULL)
3437 	{
3438 	    if (XtIsManaged(id))
3439 	    {
3440 		XtUnmanageChild(id);
3441 		XtVaSetValues(id, XmNfontList, fl, NULL);
3442 		// We should force the widget to recalculate its
3443 		// geometry now.
3444 		XtManageChild(id);
3445 	    }
3446 	    else
3447 		XtVaSetValues(id, XmNfontList, fl, NULL);
3448 	    XmFontListFree(fl);
3449 	}
3450     }
3451 #endif
3452 #endif
3453 }
3454 
3455 
3456 /*
3457  * We don't create it twice for the sake of speed.
3458  */
3459 
3460 typedef struct _SharedFindReplace
3461 {
3462     Widget dialog;	// the main dialog widget
3463     Widget wword;	// 'Exact match' check button
3464     Widget mcase;	// 'match case' check button
3465     Widget up;		// search direction 'Up' radio button
3466     Widget down;	// search direction 'Down' radio button
3467     Widget what;	// 'Find what' entry text widget
3468     Widget with;	// 'Replace with' entry text widget
3469     Widget find;	// 'Find Next' action button
3470     Widget replace;	// 'Replace With' action button
3471     Widget all;		// 'Replace All' action button
3472     Widget undo;	// 'Undo' action button
3473 
3474     Widget cancel;
3475 } SharedFindReplace;
3476 
3477 static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3478 static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3479 
3480     static void
find_replace_destroy_callback(Widget w UNUSED,XtPointer client_data,XtPointer call_data UNUSED)3481 find_replace_destroy_callback(
3482     Widget	w UNUSED,
3483     XtPointer	client_data,
3484     XtPointer	call_data UNUSED)
3485 {
3486     SharedFindReplace *cd = (SharedFindReplace *)client_data;
3487 
3488     if (cd != NULL)
3489        // suppress_dialog_mnemonics(cd->dialog);
3490 	cd->dialog = (Widget)0;
3491 }
3492 
3493     static void
find_replace_dismiss_callback(Widget w UNUSED,XtPointer client_data,XtPointer call_data UNUSED)3494 find_replace_dismiss_callback(
3495     Widget	w UNUSED,
3496     XtPointer	client_data,
3497     XtPointer	call_data UNUSED)
3498 {
3499     SharedFindReplace *cd = (SharedFindReplace *)client_data;
3500 
3501     if (cd != NULL)
3502 	XtUnmanageChild(cd->dialog);
3503 }
3504 
3505     static void
entry_activate_callback(Widget w UNUSED,XtPointer client_data,XtPointer call_data UNUSED)3506 entry_activate_callback(
3507     Widget	w UNUSED,
3508     XtPointer	client_data,
3509     XtPointer	call_data UNUSED)
3510 {
3511     XmProcessTraversal((Widget)client_data, XmTRAVERSE_CURRENT);
3512 }
3513 
3514     static void
find_replace_callback(Widget w UNUSED,XtPointer client_data,XtPointer call_data UNUSED)3515 find_replace_callback(
3516     Widget	w UNUSED,
3517     XtPointer	client_data,
3518     XtPointer	call_data UNUSED)
3519 {
3520     long_u	flags = (long_u)client_data;
3521     char	*find_text, *repl_text;
3522     Boolean	direction_down = TRUE;
3523     Boolean	wword;
3524     Boolean	mcase;
3525     SharedFindReplace *sfr;
3526 
3527     if (flags == FRD_UNDO)
3528     {
3529 	char_u	*save_cpo = p_cpo;
3530 
3531 	// No need to be Vi compatible here.
3532 	p_cpo = empty_option;
3533 	u_undo(1);
3534 	p_cpo = save_cpo;
3535 	gui_update_screen();
3536 	return;
3537     }
3538 
3539     // Get the search/replace strings from the dialog
3540     if (flags == FRD_FINDNEXT)
3541     {
3542 	repl_text = NULL;
3543 	sfr = &find_widgets;
3544     }
3545     else
3546     {
3547 	repl_text = XmTextFieldGetString(repl_widgets.with);
3548 	sfr = &repl_widgets;
3549     }
3550     find_text = XmTextFieldGetString(sfr->what);
3551     XtVaGetValues(sfr->down, XmNset, &direction_down, NULL);
3552     XtVaGetValues(sfr->wword, XmNset, &wword, NULL);
3553     XtVaGetValues(sfr->mcase, XmNset, &mcase, NULL);
3554     if (wword)
3555 	flags |= FRD_WHOLE_WORD;
3556     if (mcase)
3557 	flags |= FRD_MATCH_CASE;
3558 
3559     (void)gui_do_findrepl((int)flags, (char_u *)find_text, (char_u *)repl_text,
3560 							      direction_down);
3561 
3562     if (find_text != NULL)
3563 	XtFree(find_text);
3564     if (repl_text != NULL)
3565 	XtFree(repl_text);
3566 }
3567 
3568     static void
find_replace_keypress(Widget w UNUSED,SharedFindReplace * frdp,XKeyEvent * event,Boolean * b UNUSED)3569 find_replace_keypress(
3570     Widget		w UNUSED,
3571     SharedFindReplace	*frdp,
3572     XKeyEvent		*event,
3573     Boolean		*b UNUSED)
3574 {
3575     KeySym keysym;
3576 
3577     if (frdp == NULL)
3578 	return;
3579 
3580     keysym = XLookupKeysym(event, 0);
3581 
3582     // the scape key pops the whole dialog down
3583     if (keysym == XK_Escape)
3584 	XtUnmanageChild(frdp->dialog);
3585 }
3586 
3587     static void
set_label(Widget w,char * label)3588 set_label(Widget w, char *label)
3589 {
3590     XmString	str;
3591     char_u	*p, *next;
3592     KeySym	mnemonic = NUL;
3593 
3594     if (!w)
3595 	return;
3596 
3597     p = vim_strsave((char_u *)label);
3598     if (p == NULL)
3599 	return;
3600     for (next = p; *next; ++next)
3601     {
3602 	if (*next == DLG_HOTKEY_CHAR)
3603 	{
3604 	    int len = STRLEN(next);
3605 
3606 	    if (len > 0)
3607 	    {
3608 		mch_memmove(next, next + 1, len);
3609 		mnemonic = next[0];
3610 	    }
3611 	}
3612     }
3613 
3614     str = XmStringCreateSimple((char *)p);
3615     vim_free(p);
3616     if (str)
3617     {
3618 	XtVaSetValues(w,
3619 		XmNlabelString, str,
3620 		XmNmnemonic, mnemonic,
3621 		NULL);
3622 	XmStringFree(str);
3623     }
3624     gui_motif_menu_fontlist(w);
3625 }
3626 
3627     static void
find_replace_dialog_create(char_u * arg,int do_replace)3628 find_replace_dialog_create(char_u *arg, int do_replace)
3629 {
3630     SharedFindReplace	*frdp;
3631     Widget		separator;
3632     Widget		input_form;
3633     Widget		button_form;
3634     Widget		toggle_form;
3635     Widget		frame;
3636     XmString		str;
3637     int			n;
3638     Arg			args[6];
3639     int			wword = FALSE;
3640     int			mcase = !p_ic;
3641     Dimension		width;
3642     Dimension		widest;
3643     char_u		*entry_text;
3644 
3645     frdp = do_replace ? &repl_widgets : &find_widgets;
3646 
3647     // Get the search string to use.
3648     entry_text = get_find_dialog_text(arg, &wword, &mcase);
3649 
3650     // If the dialog already exists, just raise it.
3651     if (frdp->dialog)
3652     {
3653 	gui_motif_synch_fonts();
3654 
3655 	// If the window is already up, just pop it to the top
3656 	if (XtIsManaged(frdp->dialog))
3657 	    XMapRaised(XtDisplay(frdp->dialog),
3658 					    XtWindow(XtParent(frdp->dialog)));
3659 	else
3660 	    XtManageChild(frdp->dialog);
3661 	XtPopup(XtParent(frdp->dialog), XtGrabNone);
3662 	XmProcessTraversal(frdp->what, XmTRAVERSE_CURRENT);
3663 
3664 	if (entry_text != NULL)
3665 	    XmTextFieldSetString(frdp->what, (char *)entry_text);
3666 	vim_free(entry_text);
3667 
3668 	XtVaSetValues(frdp->wword, XmNset, wword, NULL);
3669 	return;
3670     }
3671 
3672     // Create a fresh new dialog window
3673     if (do_replace)
3674 	 str = XmStringCreateSimple(_("VIM - Search and Replace..."));
3675     else
3676 	 str = XmStringCreateSimple(_("VIM - Search..."));
3677 
3678     n = 0;
3679     XtSetArg(args[n], XmNautoUnmanage, False); n++;
3680     XtSetArg(args[n], XmNnoResize, True); n++;
3681     XtSetArg(args[n], XmNdialogTitle, str); n++;
3682 
3683     frdp->dialog = XmCreateFormDialog(vimShell, "findReplaceDialog", args, n);
3684     XmStringFree(str);
3685     XtAddCallback(frdp->dialog, XmNdestroyCallback,
3686 	    find_replace_destroy_callback, frdp);
3687 
3688     button_form = XtVaCreateWidget("buttonForm",
3689 	    xmFormWidgetClass,	frdp->dialog,
3690 	    XmNrightAttachment, XmATTACH_FORM,
3691 	    XmNrightOffset, 4,
3692 	    XmNtopAttachment, XmATTACH_FORM,
3693 	    XmNtopOffset, 4,
3694 	    XmNbottomAttachment, XmATTACH_FORM,
3695 	    XmNbottomOffset, 4,
3696 	    NULL);
3697 
3698     frdp->find = XtVaCreateManagedWidget("findButton",
3699 	    xmPushButtonWidgetClass, button_form,
3700 	    XmNsensitive, True,
3701 	    XmNtopAttachment, XmATTACH_FORM,
3702 	    XmNleftAttachment, XmATTACH_FORM,
3703 	    XmNrightAttachment, XmATTACH_FORM,
3704 	    NULL);
3705     set_label(frdp->find, _("Find &Next"));
3706 
3707     XtAddCallback(frdp->find, XmNactivateCallback,
3708 	    find_replace_callback,
3709 	    (do_replace ? (XtPointer)FRD_R_FINDNEXT : (XtPointer)FRD_FINDNEXT));
3710 
3711     if (do_replace)
3712     {
3713 	frdp->replace = XtVaCreateManagedWidget("replaceButton",
3714 		xmPushButtonWidgetClass, button_form,
3715 		XmNtopAttachment, XmATTACH_WIDGET,
3716 		XmNtopWidget, frdp->find,
3717 		XmNleftAttachment, XmATTACH_FORM,
3718 		XmNrightAttachment, XmATTACH_FORM,
3719 		NULL);
3720 	set_label(frdp->replace, _("&Replace"));
3721 	XtAddCallback(frdp->replace, XmNactivateCallback,
3722 		find_replace_callback, (XtPointer)FRD_REPLACE);
3723 
3724 	frdp->all = XtVaCreateManagedWidget("replaceAllButton",
3725 		xmPushButtonWidgetClass, button_form,
3726 		XmNtopAttachment, XmATTACH_WIDGET,
3727 		XmNtopWidget, frdp->replace,
3728 		XmNleftAttachment, XmATTACH_FORM,
3729 		XmNrightAttachment, XmATTACH_FORM,
3730 		NULL);
3731 	set_label(frdp->all, _("Replace &All"));
3732 	XtAddCallback(frdp->all, XmNactivateCallback,
3733 		find_replace_callback, (XtPointer)FRD_REPLACEALL);
3734 
3735 	frdp->undo = XtVaCreateManagedWidget("undoButton",
3736 		xmPushButtonWidgetClass, button_form,
3737 		XmNtopAttachment, XmATTACH_WIDGET,
3738 		XmNtopWidget, frdp->all,
3739 		XmNleftAttachment, XmATTACH_FORM,
3740 		XmNrightAttachment, XmATTACH_FORM,
3741 		NULL);
3742 	set_label(frdp->undo, _("&Undo"));
3743 	XtAddCallback(frdp->undo, XmNactivateCallback,
3744 		find_replace_callback, (XtPointer)FRD_UNDO);
3745     }
3746 
3747     frdp->cancel = XtVaCreateManagedWidget("closeButton",
3748 	    xmPushButtonWidgetClass, button_form,
3749 	    XmNleftAttachment, XmATTACH_FORM,
3750 	    XmNrightAttachment, XmATTACH_FORM,
3751 	    XmNbottomAttachment, XmATTACH_FORM,
3752 	    NULL);
3753     set_label(frdp->cancel, _("&Cancel"));
3754     XtAddCallback(frdp->cancel, XmNactivateCallback,
3755 	    find_replace_dismiss_callback, frdp);
3756     gui_motif_menu_fontlist(frdp->cancel);
3757 
3758     XtManageChild(button_form);
3759 
3760     n = 0;
3761     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
3762     XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
3763     XtSetArg(args[n], XmNrightWidget, button_form); n++;
3764     XtSetArg(args[n], XmNrightOffset, 4); n++;
3765     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
3766     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
3767     separator = XmCreateSeparatorGadget(frdp->dialog, "separator", args, n);
3768     XtManageChild(separator);
3769 
3770     input_form = XtVaCreateWidget("inputForm",
3771 	    xmFormWidgetClass,	frdp->dialog,
3772 	    XmNleftAttachment, XmATTACH_FORM,
3773 	    XmNleftOffset, 4,
3774 	    XmNrightAttachment, XmATTACH_WIDGET,
3775 	    XmNrightWidget, separator,
3776 	    XmNrightOffset, 4,
3777 	    XmNtopAttachment, XmATTACH_FORM,
3778 	    XmNtopOffset, 4,
3779 	    NULL);
3780 
3781     {
3782 	Widget label_what;
3783 	Widget label_with = (Widget)0;
3784 
3785 	str = XmStringCreateSimple(_("Find what:"));
3786 	label_what = XtVaCreateManagedWidget("whatLabel",
3787 		xmLabelGadgetClass, input_form,
3788 		XmNlabelString, str,
3789 		XmNleftAttachment,	XmATTACH_FORM,
3790 		XmNtopAttachment, XmATTACH_FORM,
3791 		XmNtopOffset, 4,
3792 		NULL);
3793 	XmStringFree(str);
3794 	gui_motif_menu_fontlist(label_what);
3795 
3796 	frdp->what = XtVaCreateManagedWidget("whatText",
3797 		xmTextFieldWidgetClass, input_form,
3798 		XmNtopAttachment, XmATTACH_FORM,
3799 		XmNrightAttachment, XmATTACH_FORM,
3800 		XmNleftAttachment, XmATTACH_FORM,
3801 		NULL);
3802 
3803 	if (do_replace)
3804 	{
3805 	    frdp->with = XtVaCreateManagedWidget("withText",
3806 		    xmTextFieldWidgetClass,	input_form,
3807 		    XmNtopAttachment, XmATTACH_WIDGET,
3808 		    XmNtopWidget, frdp->what,
3809 		    XmNtopOffset, 4,
3810 		    XmNleftAttachment, XmATTACH_FORM,
3811 		    XmNrightAttachment, XmATTACH_FORM,
3812 		    XmNbottomAttachment, XmATTACH_FORM,
3813 		    NULL);
3814 
3815 	    XtAddCallback(frdp->with, XmNactivateCallback,
3816 		    find_replace_callback, (XtPointer) FRD_R_FINDNEXT);
3817 
3818 	    str = XmStringCreateSimple(_("Replace with:"));
3819 	    label_with = XtVaCreateManagedWidget("withLabel",
3820 		    xmLabelGadgetClass, input_form,
3821 		    XmNlabelString, str,
3822 		    XmNleftAttachment, XmATTACH_FORM,
3823 		    XmNtopAttachment, XmATTACH_WIDGET,
3824 		    XmNtopWidget, frdp->what,
3825 		    XmNtopOffset, 4,
3826 		    XmNbottomAttachment, XmATTACH_FORM,
3827 		    NULL);
3828 	    XmStringFree(str);
3829 	    gui_motif_menu_fontlist(label_with);
3830 
3831 	    /*
3832 	     * Make the entry activation only change the input focus onto the
3833 	     * with item.
3834 	     */
3835 	    XtAddCallback(frdp->what, XmNactivateCallback,
3836 		    entry_activate_callback, frdp->with);
3837 	    XtAddEventHandler(frdp->with, KeyPressMask, False,
3838 			    (XtEventHandler)find_replace_keypress,
3839 			    (XtPointer) frdp);
3840 
3841 	}
3842 	else
3843 	{
3844 	    /*
3845 	     * Make the entry activation do the search.
3846 	     */
3847 	    XtAddCallback(frdp->what, XmNactivateCallback,
3848 		    find_replace_callback, (XtPointer)FRD_FINDNEXT);
3849 	}
3850 	XtAddEventHandler(frdp->what, KeyPressMask, False,
3851 			    (XtEventHandler)find_replace_keypress,
3852 			    (XtPointer)frdp);
3853 
3854 	// Get the maximum width between the label widgets and line them up.
3855 	n = 0;
3856 	XtSetArg(args[n], XmNwidth, &width); n++;
3857 	XtGetValues(label_what, args, n);
3858 	widest = width;
3859 	if (do_replace)
3860 	{
3861 	    XtGetValues(label_with, args, n);
3862 	    if (width > widest)
3863 		widest = width;
3864 	}
3865 
3866 	XtVaSetValues(frdp->what, XmNleftOffset, widest, NULL);
3867 	if (do_replace)
3868 	    XtVaSetValues(frdp->with, XmNleftOffset, widest, NULL);
3869 
3870     }
3871 
3872     XtManageChild(input_form);
3873 
3874     {
3875 	Widget radio_box;
3876 	Widget w;
3877 
3878 	frame = XtVaCreateWidget("directionFrame",
3879 		xmFrameWidgetClass, frdp->dialog,
3880 		XmNtopAttachment, XmATTACH_WIDGET,
3881 		XmNtopWidget, input_form,
3882 		XmNtopOffset, 4,
3883 		XmNbottomAttachment, XmATTACH_FORM,
3884 		XmNbottomOffset, 4,
3885 		XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
3886 		XmNrightWidget, input_form,
3887 		NULL);
3888 
3889 	str = XmStringCreateSimple(_("Direction"));
3890 	w = XtVaCreateManagedWidget("directionFrameLabel",
3891 		xmLabelGadgetClass, frame,
3892 		XmNlabelString, str,
3893 		XmNchildHorizontalAlignment, XmALIGNMENT_BEGINNING,
3894 		XmNchildType, XmFRAME_TITLE_CHILD,
3895 		NULL);
3896 	XmStringFree(str);
3897 	gui_motif_menu_fontlist(w);
3898 
3899 	radio_box = XmCreateRadioBox(frame, "radioBox",
3900 		(ArgList)NULL, 0);
3901 
3902 	str = XmStringCreateSimple( _("Up"));
3903 	frdp->up = XtVaCreateManagedWidget("upRadioButton",
3904 		xmToggleButtonGadgetClass, radio_box,
3905 		XmNlabelString, str,
3906 		XmNset, False,
3907 		NULL);
3908 	XmStringFree(str);
3909 	gui_motif_menu_fontlist(frdp->up);
3910 
3911 	str = XmStringCreateSimple(_("Down"));
3912 	frdp->down = XtVaCreateManagedWidget("downRadioButton",
3913 		xmToggleButtonGadgetClass, radio_box,
3914 		XmNlabelString, str,
3915 		XmNset, True,
3916 		NULL);
3917 	XmStringFree(str);
3918 	gui_motif_menu_fontlist(frdp->down);
3919 
3920 	XtManageChild(radio_box);
3921 	XtManageChild(frame);
3922     }
3923 
3924     toggle_form = XtVaCreateWidget("toggleForm",
3925 	    xmFormWidgetClass,	frdp->dialog,
3926 	    XmNleftAttachment, XmATTACH_FORM,
3927 	    XmNleftOffset, 4,
3928 	    XmNrightAttachment, XmATTACH_WIDGET,
3929 	    XmNrightWidget, frame,
3930 	    XmNrightOffset, 4,
3931 	    XmNtopAttachment, XmATTACH_WIDGET,
3932 	    XmNtopWidget, input_form,
3933 	    XmNtopOffset, 4,
3934 	    XmNbottomAttachment, XmATTACH_FORM,
3935 	    XmNbottomOffset, 4,
3936 	    NULL);
3937 
3938     str = XmStringCreateSimple(_("Match whole word only"));
3939     frdp->wword = XtVaCreateManagedWidget("wordToggle",
3940 	    xmToggleButtonGadgetClass, toggle_form,
3941 	    XmNlabelString, str,
3942 	    XmNtopAttachment, XmATTACH_FORM,
3943 	    XmNtopOffset, 4,
3944 	    XmNleftAttachment, XmATTACH_FORM,
3945 	    XmNleftOffset, 4,
3946 	    XmNset, wword,
3947 	    NULL);
3948     XmStringFree(str);
3949 
3950     str = XmStringCreateSimple(_("Match case"));
3951     frdp->mcase = XtVaCreateManagedWidget("caseToggle",
3952 	    xmToggleButtonGadgetClass, toggle_form,
3953 	    XmNlabelString, str,
3954 	    XmNleftAttachment, XmATTACH_FORM,
3955 	    XmNleftOffset, 4,
3956 	    XmNtopAttachment, XmATTACH_WIDGET,
3957 	    XmNtopWidget, frdp->wword,
3958 	    XmNtopOffset, 4,
3959 	    XmNset, mcase,
3960 	    NULL);
3961     XmStringFree(str);
3962     gui_motif_menu_fontlist(frdp->wword);
3963     gui_motif_menu_fontlist(frdp->mcase);
3964 
3965     XtManageChild(toggle_form);
3966 
3967     if (entry_text != NULL)
3968 	XmTextFieldSetString(frdp->what, (char *)entry_text);
3969     vim_free(entry_text);
3970 
3971     gui_motif_synch_fonts();
3972 
3973     manage_centered(frdp->dialog);
3974     activate_dialog_mnemonics(frdp->dialog);
3975     XmProcessTraversal(frdp->what, XmTRAVERSE_CURRENT);
3976 }
3977 
3978    void
gui_mch_find_dialog(exarg_T * eap)3979 gui_mch_find_dialog(exarg_T *eap)
3980 {
3981     if (!gui.in_use)
3982 	return;
3983 
3984     find_replace_dialog_create(eap->arg, FALSE);
3985 }
3986 
3987 
3988     void
gui_mch_replace_dialog(exarg_T * eap)3989 gui_mch_replace_dialog(exarg_T *eap)
3990 {
3991     if (!gui.in_use)
3992 	return;
3993 
3994     find_replace_dialog_create(eap->arg, TRUE);
3995 }
3996 
3997 /*
3998  * Synchronize all gui elements, which are dependant upon the
3999  * main text font used. Those are in esp. the find/replace dialogs.
4000  * If you don't understand why this should be needed, please try to
4001  * search for "pi\xea\xb6\xe6" in iso8859-2.
4002  */
4003     void
gui_motif_synch_fonts(void)4004 gui_motif_synch_fonts(void)
4005 {
4006     SharedFindReplace *frdp;
4007     int		    do_replace;
4008     XFontStruct	    *font;
4009     XmFontList	    font_list;
4010 
4011     // FIXME: Unless we find out how to create a XmFontList from a XFontSet,
4012     // we just give up here on font synchronization.
4013     font = (XFontStruct *)gui.norm_font;
4014     if (font == NULL)
4015 	return;
4016 
4017     font_list = gui_motif_create_fontlist(font);
4018 
4019     // OK this loop is a bit tricky...
4020     for (do_replace = 0; do_replace <= 1; ++do_replace)
4021     {
4022 	frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
4023 	if (frdp->dialog)
4024 	{
4025 	    XtVaSetValues(frdp->what, XmNfontList, font_list, NULL);
4026 	    if (do_replace)
4027 		XtVaSetValues(frdp->with, XmNfontList, font_list, NULL);
4028 	}
4029     }
4030 
4031     XmFontListFree(font_list);
4032 }
4033