blob: 5e18f20c8e4eb421619a0cb232a41d07d93d95f0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaarf7506ca2017-02-25 16:01:49 +010011#include "vim.h"
12
Bram Moolenaar071d4272004-06-13 20:20:40 +000013#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>
Bram Moolenaarc6fe9192006-04-09 21:54:49 +000031#include <Xm/XmP.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33#include <X11/keysym.h>
34#include <X11/Xatom.h>
35#include <X11/StringDefs.h>
36#include <X11/Intrinsic.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef HAVE_X11_XPM_H
Bram Moolenaar88c86eb2019-01-17 17:13:30 +010038# if defined(VMS)
39# include <xpm.h>
40# else
41# include <X11/xpm.h>
42# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#else
44# ifdef HAVE_XM_XPMP_H
45# include <Xm/XpmP.h>
46# endif
47#endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000048#ifdef HAVE_XM_NOTEBOOK_H
49# include <Xm/Notebook.h>
50#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar734a8672019-12-02 22:49:38 +010052#include "gui_xmebw.h" // for our Enhanced Button Widget
Bram Moolenaarb7fcef52005-01-02 11:31:05 +000053
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#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
64extern Widget vimShell;
65
66static Widget vimForm;
67static Widget textAreaForm;
68Widget textArea;
69#ifdef FEAT_TOOLBAR
70static Widget toolBarFrame;
71static Widget toolBar;
72#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000073#ifdef FEAT_GUI_TABLINE
74static Widget tabLine;
75static Widget tabLine_menu = 0;
76static int showing_tabline = 0;
77#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#ifdef FEAT_MENU
79# if (XmVersion >= 1002)
Bram Moolenaar734a8672019-12-02 22:49:38 +010080// remember the last set value for the tearoff item
Bram Moolenaar071d4272004-06-13 20:20:40 +000081static int tearoff_val = (int)XmTEAR_OFF_ENABLED;
82# endif
83static Widget menuBar;
84#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#ifdef FEAT_TOOLBAR
Bram Moolenaard25c16e2016-01-29 22:13:30 +010087static void reset_focus(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static void gui_motif_menu_colors(Widget id);
91static void gui_motif_scroll_colors(Widget id);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93#if (XmVersion >= 1002)
94# define STRING_TAG XmFONTLIST_DEFAULT_TAG
95#else
96# define STRING_TAG XmSTRING_DEFAULT_CHARSET
97#endif
98
99/*
100 * Call-back routines.
101 */
102
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 static void
Bram Moolenaarc1ab6762016-01-30 19:45:49 +0100104scroll_cb(Widget w UNUSED, XtPointer client_data, XtPointer call_data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
106 scrollbar_T *sb;
107 long value;
108 int dragging;
109
110 sb = gui_find_scrollbar((long)client_data);
111
112 value = ((XmScrollBarCallbackStruct *)call_data)->value;
113 dragging = (((XmScrollBarCallbackStruct *)call_data)->reason ==
114 (int)XmCR_DRAG);
115 gui_drag_scrollbar(sb, value, dragging);
116}
117
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000118#ifdef FEAT_GUI_TABLINE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000119 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100120tabline_cb(
121 Widget w UNUSED,
122 XtPointer client_data UNUSED,
123 XtPointer call_data)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000124{
125 XmNotebookCallbackStruct *nptr;
126
127 nptr = (XmNotebookCallbackStruct *)call_data;
128 if (nptr->reason != (int)XmCR_NONE)
129 send_tabline_event(nptr->page_number);
130}
131
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000132 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100133tabline_button_cb(
134 Widget w,
135 XtPointer client_data UNUSED,
136 XtPointer call_data UNUSED)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000137{
Bram Moolenaar9dd42a62022-03-24 18:04:49 +0000138 XtPointer cmd, tab_idx;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000139
140 XtVaGetValues(w, XmNuserData, &cmd, NULL);
141 XtVaGetValues(tabLine_menu, XmNuserData, &tab_idx, NULL);
142
Bram Moolenaar9dd42a62022-03-24 18:04:49 +0000143 send_tabline_menu_event((int)(long)tab_idx, (int)(long)cmd);
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000144}
145
146/*
147 * Tabline single mouse click timeout handler
148 */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000149 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100150motif_tabline_timer_cb (
151 XtPointer timed_out,
152 XtIntervalId *interval_id UNUSED)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000153{
154 *((int *)timed_out) = TRUE;
155}
156
157/*
158 * check if the tabline tab scroller is clicked
159 */
160 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100161tabline_scroller_clicked(
162 char *scroller_name,
163 XButtonPressedEvent *event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000164{
165 Widget tab_scroll_w;
166 Position pos_x, pos_y;
167 Dimension width, height;
168
169 tab_scroll_w = XtNameToWidget(tabLine, scroller_name);
170 if (tab_scroll_w != (Widget)0) {
171 XtVaGetValues(tab_scroll_w, XmNx, &pos_x, XmNy, &pos_y, XmNwidth,
172 &width, XmNheight, &height, NULL);
173 if (pos_x >= 0) {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100174 // Tab scroller (next) is visible
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000175 if ((event->x >= pos_x) && (event->x <= pos_x + width) &&
176 (event->y >= pos_y) && (event->y <= pos_y + height)) {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100177 // Clicked on the scroller
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000178 return TRUE;
179 }
180 }
181 }
182 return FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000183}
184
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000185 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100186tabline_menu_cb(
187 Widget w,
188 XtPointer closure UNUSED,
189 XEvent *e,
190 Boolean *continue_dispatch UNUSED)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000191{
192 Widget tab_w;
193 XButtonPressedEvent *event;
194 int tab_idx = 0;
195 WidgetList children;
196 Cardinal numChildren;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000197 static XtIntervalId timer = (XtIntervalId)0;
198 static int timed_out = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000199
200 event = (XButtonPressedEvent *)e;
201
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000202 if (event->button == Button1)
203 {
204 if (tabline_scroller_clicked("MajorTabScrollerNext", event)
205 || tabline_scroller_clicked("MajorTabScrollerPrevious", event))
206 return;
207
208 if (!timed_out)
209 {
210 XtRemoveTimeOut(timer);
211 timed_out = TRUE;
212
213 /*
214 * Double click on the tabline gutter, add a new tab
215 */
216 send_tabline_menu_event(0, TABLINE_MENU_NEW);
217 }
218 else
219 {
220 /*
221 * Single click on the tabline gutter, start a timer to check
222 * for double clicks
223 */
224 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
225 motif_tabline_timer_cb, &timed_out);
226 timed_out = FALSE;
227 }
228 return;
229 }
230
Martin Tournoij7904fa42022-10-04 16:28:45 +0100231 if (event->button == Button2)
232 {
233 // Middle mouse click on tabpage label closes that tab.
234 XtVaGetValues(tabLine_menu, XmNuserData, &tab_idx, NULL);
235 send_tabline_menu_event(tab_idx, (int)TABLINE_MENU_CLOSE);
236 return;
237 }
238
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000239 if (event->button != Button3)
240 return;
241
Bram Moolenaar734a8672019-12-02 22:49:38 +0100242 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +0100243 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000244 return;
245
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000246 if (event->subwindow != None)
247 {
248 tab_w = XtWindowToWidget(XtDisplay(w), event->subwindow);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100249 // LINTED: avoid warning: dubious operation on enum
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000250 if (tab_w != (Widget)0 && XmIsPushButton(tab_w))
251 XtVaGetValues(tab_w, XmNpageNumber, &tab_idx, NULL);
252 }
253
Bram Moolenaar9dd42a62022-03-24 18:04:49 +0000254 XtVaSetValues(tabLine_menu, XmNuserData, (XtPointer)(long)tab_idx, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000255 XtVaGetValues(tabLine_menu, XmNchildren, &children, XmNnumChildren,
256 &numChildren, NULL);
257 XtManageChildren(children, numChildren);
258 XmMenuPosition(tabLine_menu, (XButtonPressedEvent *)e) ;
259 XtManageChild(tabLine_menu);
260}
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000261
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000262 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100263tabline_balloon_cb(BalloonEval *beval, int state UNUSED)
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000264{
265 int nr;
266 tabpage_T *tp;
267
268 if (beval->target == (Widget)0)
269 return;
270
271 XtVaGetValues(beval->target, XmNpageNumber, &nr, NULL);
272 tp = find_tabpage(nr);
273 if (tp == NULL)
274 return;
275
276 get_tabline_label(tp, TRUE);
277 gui_mch_post_balloon(beval, NameBuff);
278}
279
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000280#endif
281
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282/*
283 * End of call-back routines
284 */
285
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000286/*
287 * Implement three dimensional shading of insensitive labels.
Bram Moolenaara0a83be2005-01-04 21:26:43 +0000288 * By Marcin Dalecki.
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000289 */
290
291#include <Xm/XmP.h>
292#include <Xm/LabelP.h>
293
294static XtExposeProc old_label_expose = NULL;
295
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000296 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100297label_expose(Widget _w, XEvent *_event, Region _region)
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000298{
299 GC insensitiveGC;
300 XmLabelWidget lw = (XmLabelWidget)_w;
Bram Moolenaarb7fcef52005-01-02 11:31:05 +0000301 unsigned char label_type = (int)XmSTRING;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000302
303 XtVaGetValues(_w, XmNlabelType, &label_type, (XtPointer)0);
304
Bram Moolenaarb7fcef52005-01-02 11:31:05 +0000305 if (XtIsSensitive(_w) || label_type != (int)XmSTRING)
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000306 (*old_label_expose)(_w, _event, _region);
307 else
308 {
309 XGCValues values;
310 XtGCMask mask;
311 XtGCMask dynamic;
312 XFontStruct *fs;
313
314 _XmFontListGetDefaultFont(lw->label.font, &fs);
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316 // FIXME: we should be doing the whole drawing ourself here.
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000317 insensitiveGC = lw->label.insensitive_GC;
318
319 mask = GCForeground | GCBackground | GCGraphicsExposures;
320 dynamic = GCClipMask | GCClipXOrigin | GCClipYOrigin;
321 values.graphics_exposures = False;
322
323 if (fs != 0)
324 {
325 mask |= GCFont;
326 values.font = fs->fid;
327 }
328
329 if (lw->primitive.top_shadow_pixmap != None
330 && lw->primitive.top_shadow_pixmap != XmUNSPECIFIED_PIXMAP)
331 {
332 mask |= GCFillStyle | GCTile;
333 values.fill_style = FillTiled;
334 values.tile = lw->primitive.top_shadow_pixmap;
335 }
336
337 lw->label.TextRect.x += 1;
338 lw->label.TextRect.y += 1;
339 if (lw->label._acc_text != 0)
340 {
341 lw->label.acc_TextRect.x += 1;
342 lw->label.acc_TextRect.y += 1;
343 }
344
345 values.foreground = lw->primitive.top_shadow_color;
346 values.background = lw->core.background_pixel;
347
Bram Moolenaarb7fcef52005-01-02 11:31:05 +0000348 lw->label.insensitive_GC = XtAllocateGC((Widget)lw, 0, mask,
349 &values, dynamic, (XtGCMask)0);
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000350 (*old_label_expose)(_w, _event, _region);
351 XtReleaseGC(_w, lw->label.insensitive_GC);
352
353 lw->label.TextRect.x -= 1;
354 lw->label.TextRect.y -= 1;
355 if (lw->label._acc_text != 0)
356 {
357 lw->label.acc_TextRect.x -= 1;
358 lw->label.acc_TextRect.y -= 1;
359 }
360
361 values.foreground = lw->primitive.bottom_shadow_color;
362 values.background = lw->core.background_pixel;
363
Bram Moolenaarb7fcef52005-01-02 11:31:05 +0000364 lw->label.insensitive_GC = XtAllocateGC((Widget) lw, 0, mask,
365 &values, dynamic, (XtGCMask)0);
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000366 (*old_label_expose)(_w, _event, _region);
367 XtReleaseGC(_w, lw->label.insensitive_GC);
368
369 lw->label.insensitive_GC = insensitiveGC;
370 }
371}
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373/*
374 * Create all the motif widgets necessary.
375 */
376 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100377gui_x11_create_widgets(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000379#ifdef FEAT_GUI_TABLINE
Bram Moolenaar18144c82006-04-12 21:52:12 +0000380 Widget button, scroller;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000381 Arg args[10];
382 int n;
383 XmString xms;
384#endif
385
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000386 /*
387 * Install the 3D shade effect drawing routines.
388 */
389 if (old_label_expose == NULL)
390 {
391 old_label_expose = xmLabelWidgetClass->core_class.expose;
392 xmLabelWidgetClass->core_class.expose = label_expose;
393 }
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000394
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 /*
396 * Start out by adding the configured border width into the border offset
397 */
398 gui.border_offset = gui.border_width;
399
400 /*
401 * Install the tearOffModel resource converter.
402 */
403#if (XmVersion >= 1002)
404 XmRepTypeInstallTearOffModelConverter();
405#endif
406
Bram Moolenaar734a8672019-12-02 22:49:38 +0100407 // Make sure the "Quit" menu entry of the window manager is ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 XtVaSetValues(vimShell, XmNdeleteResponse, XmDO_NOTHING, NULL);
409
410 vimForm = XtVaCreateManagedWidget("vimForm",
411 xmFormWidgetClass, vimShell,
412 XmNborderWidth, 0,
413 XmNhighlightThickness, 0,
414 XmNshadowThickness, 0,
415 XmNmarginWidth, 0,
416 XmNmarginHeight, 0,
417 XmNresizePolicy, XmRESIZE_ANY,
418 NULL);
419 gui_motif_menu_colors(vimForm);
420
421#ifdef FEAT_MENU
422 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100423 Arg al[7]; // Make sure there is enough room for arguments!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 int ac = 0;
425
426# if (XmVersion >= 1002)
427 XtSetArg(al[ac], XmNtearOffModel, tearoff_val); ac++;
428# endif
429 XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
430 XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
431 XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
432# ifndef FEAT_TOOLBAR
Bram Moolenaar734a8672019-12-02 22:49:38 +0100433 // Always stick to right hand side.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 XtSetArg(al[ac], XmNrightOffset, 0); ac++;
435# endif
Bram Moolenaarb7fcef52005-01-02 11:31:05 +0000436 XtSetArg(al[ac], XmNmarginHeight, 0); ac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 menuBar = XmCreateMenuBar(vimForm, "menuBar", al, ac);
438 XtManageChild(menuBar);
439
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 gui_motif_menu_colors(menuBar);
441 }
442#endif
443
444#ifdef FEAT_TOOLBAR
445 /*
446 * Create an empty ToolBar. We should get buttons defined from menu.vim.
447 */
448 toolBarFrame = XtVaCreateWidget("toolBarFrame",
449 xmFrameWidgetClass, vimForm,
450 XmNshadowThickness, 0,
451 XmNmarginHeight, 0,
452 XmNmarginWidth, 0,
453 XmNleftAttachment, XmATTACH_FORM,
454 XmNrightAttachment, XmATTACH_FORM,
455 NULL);
456 gui_motif_menu_colors(toolBarFrame);
457
458 toolBar = XtVaCreateManagedWidget("toolBar",
459 xmRowColumnWidgetClass, toolBarFrame,
460 XmNchildType, XmFRAME_WORKAREA_CHILD,
461 XmNrowColumnType, XmWORK_AREA,
462 XmNorientation, XmHORIZONTAL,
463 XmNtraversalOn, False,
464 XmNisHomogeneous, False,
465 XmNpacking, XmPACK_TIGHT,
466 XmNspacing, 0,
467 XmNshadowThickness, 0,
468 XmNhighlightThickness, 0,
469 XmNmarginHeight, 0,
470 XmNmarginWidth, 0,
471 XmNadjustLast, True,
472 NULL);
473 gui_motif_menu_colors(toolBar);
474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475#endif
476
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000477#ifdef FEAT_GUI_TABLINE
Bram Moolenaar734a8672019-12-02 22:49:38 +0100478 // Create the Vim GUI tabline
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000479 n = 0;
480 XtSetArg(args[n], XmNbindingType, XmNONE); n++;
481 XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
482 XtSetArg(args[n], XmNbackPageSize, XmNONE); n++;
483 XtSetArg(args[n], XmNbackPageNumber, 0); n++;
484 XtSetArg(args[n], XmNbackPagePlacement, XmTOP_RIGHT); n++;
485 XtSetArg(args[n], XmNmajorTabSpacing, 0); n++;
486 XtSetArg(args[n], XmNshadowThickness, 0); n++;
487 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
488 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
489 tabLine = XmCreateNotebook(vimForm, "Vim tabline", args, n);
490
491 XtAddCallback(tabLine, XmNpageChangedCallback, (XtCallbackProc)tabline_cb,
492 NULL);
493 XtAddEventHandler(tabLine, ButtonPressMask, False,
494 (XtEventHandler)tabline_menu_cb, NULL);
495
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000496 gui.tabline_height = TABLINE_HEIGHT;
497
Bram Moolenaar18144c82006-04-12 21:52:12 +0000498 /*
499 * Set the size of the minor next/prev scrollers to zero, so
500 * that they are not displayed. Due to a bug in OpenMotif 2.3,
501 * even if these children widget are unmanaged, they are again
502 * managed by the Notebook widget and the notebook widget geometry
503 * is adjusted to account for the minor scroller widgets.
504 */
505 scroller = XtNameToWidget(tabLine, "MinorTabScrollerNext");
506 XtVaSetValues(scroller, XmNwidth, 0, XmNresizable, False,
507 XmNtraversalOn, False, NULL);
508 scroller = XtNameToWidget(tabLine, "MinorTabScrollerPrevious");
509 XtVaSetValues(scroller, XmNwidth, 0, XmNresizable, False,
510 XmNtraversalOn, False, NULL);
511
Bram Moolenaar29547192018-12-11 20:39:19 +0100512 // Create the tabline popup menu
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000513 tabLine_menu = XmCreatePopupMenu(tabLine, "tabline popup", NULL, 0);
514
Bram Moolenaar29547192018-12-11 20:39:19 +0100515 // Add the buttons to the tabline popup menu
516 n = 0;
Bram Moolenaar9dd42a62022-03-24 18:04:49 +0000517 XtSetArg(args[n], XmNuserData, (XtPointer)TABLINE_MENU_CLOSE); n++;
Bram Moolenaar29547192018-12-11 20:39:19 +0100518 xms = XmStringCreate((char *)"Close tab", STRING_TAG);
519 XtSetArg(args[n], XmNlabelString, xms); n++;
520 button = XmCreatePushButton(tabLine_menu, "Close", args, n);
521 XtAddCallback(button, XmNactivateCallback,
522 (XtCallbackProc)tabline_button_cb, NULL);
523 XmStringFree(xms);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000524
525 n = 0;
Bram Moolenaar9dd42a62022-03-24 18:04:49 +0000526 XtSetArg(args[n], XmNuserData, (XtPointer)TABLINE_MENU_NEW); n++;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000527 xms = XmStringCreate((char *)"New Tab", STRING_TAG);
528 XtSetArg(args[n], XmNlabelString, xms); n++;
529 button = XmCreatePushButton(tabLine_menu, "New Tab", args, n);
530 XtAddCallback(button, XmNactivateCallback,
531 (XtCallbackProc)tabline_button_cb, NULL);
532 XmStringFree(xms);
533
534 n = 0;
Bram Moolenaar9dd42a62022-03-24 18:04:49 +0000535 XtSetArg(args[n], XmNuserData, (XtPointer)TABLINE_MENU_OPEN); n++;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000536 xms = XmStringCreate((char *)"Open tab...", STRING_TAG);
537 XtSetArg(args[n], XmNlabelString, xms); n++;
538 button = XmCreatePushButton(tabLine_menu, "Open tab...", args, n);
539 XtAddCallback(button, XmNactivateCallback,
540 (XtCallbackProc)tabline_button_cb, NULL);
541 XmStringFree(xms);
542#endif
543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 textAreaForm = XtVaCreateManagedWidget("textAreaForm",
545 xmFormWidgetClass, vimForm,
546 XmNleftAttachment, XmATTACH_FORM,
547 XmNrightAttachment, XmATTACH_FORM,
548 XmNbottomAttachment, XmATTACH_FORM,
549 XmNtopAttachment, XmATTACH_FORM,
550 XmNmarginWidth, 0,
551 XmNmarginHeight, 0,
552 XmNresizePolicy, XmRESIZE_ANY,
553 NULL);
554 gui_motif_scroll_colors(textAreaForm);
555
556 textArea = XtVaCreateManagedWidget("textArea",
557 xmDrawingAreaWidgetClass, textAreaForm,
558 XmNforeground, gui.norm_pixel,
559 XmNbackground, gui.back_pixel,
560 XmNleftAttachment, XmATTACH_FORM,
561 XmNtopAttachment, XmATTACH_FORM,
562 XmNrightAttachment, XmATTACH_FORM,
563 XmNbottomAttachment, XmATTACH_FORM,
564
565 /*
566 * These take some control away from the user, but avoids making them
567 * add resources to get a decent looking setup.
568 */
569 XmNborderWidth, 0,
570 XmNhighlightThickness, 0,
571 XmNshadowThickness, 0,
572 NULL);
573
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 /*
575 * Install the callbacks.
576 */
577 gui_x11_callbacks(textArea, vimForm);
578
Bram Moolenaar734a8672019-12-02 22:49:38 +0100579 // Pretend we don't have input focus, we will get an event if we do.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 gui.in_focus = FALSE;
581}
582
583/*
584 * Called when the GUI is not going to start after all.
585 */
586 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100587gui_x11_destroy_widgets(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 textArea = NULL;
590#ifdef FEAT_MENU
591 menuBar = NULL;
592#endif
593}
594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100596gui_mch_set_text_area_pos(
597 int x UNUSED,
598 int y UNUSED,
599 int w UNUSED,
600 int h UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602#ifdef FEAT_TOOLBAR
Bram Moolenaar734a8672019-12-02 22:49:38 +0100603 // Give keyboard focus to the textArea instead of the toolbar.
Bram Moolenaarf9980f12005-01-03 20:58:59 +0000604 reset_focus();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605#endif
606}
607
608 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100609gui_x11_set_back_color(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (textArea != NULL)
612#if (XmVersion >= 1002)
613 XmChangeColor(textArea, gui.back_pixel);
614#else
615 XtVaSetValues(textArea,
616 XmNbackground, gui.back_pixel,
617 NULL);
618#endif
619}
620
621/*
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100622 * Manage dialog centered on pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 */
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000624 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100625manage_centered(Widget dialog_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 Widget shell = XtParent(dialog_child);
628 Window root, child;
629 unsigned int mask;
630 unsigned int width, height, border_width, depth;
631 int x, y, win_x, win_y, maxX, maxY;
632 Boolean mappedWhenManaged;
633
Bram Moolenaar734a8672019-12-02 22:49:38 +0100634 // Temporarily set value of XmNmappedWhenManaged
635 // to stop the dialog from popping up right away
Bram Moolenaar46784652008-06-20 09:40:11 +0000636 XtVaGetValues(shell, XmNmappedWhenManaged, &mappedWhenManaged, NULL);
637 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 XtManageChild(dialog_child);
640
Bram Moolenaar734a8672019-12-02 22:49:38 +0100641 // Get the pointer position (x, y)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 XQueryPointer(XtDisplay(shell), XtWindow(shell), &root, &child,
643 &x, &y, &win_x, &win_y, &mask);
644
Bram Moolenaar734a8672019-12-02 22:49:38 +0100645 // Translate the pointer position (x, y) into a position for the new
646 // window that will place the pointer at its center
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 XGetGeometry(XtDisplay(shell), XtWindow(shell), &root, &win_x, &win_y,
648 &width, &height, &border_width, &depth);
649 width += 2 * border_width;
650 height += 2 * border_width;
651 x -= width / 2;
652 y -= height / 2;
653
Bram Moolenaar734a8672019-12-02 22:49:38 +0100654 // Ensure that the dialog remains on screen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 maxX = XtScreen(shell)->width - width;
656 maxY = XtScreen(shell)->height - height;
657 if (x < 0)
658 x = 0;
659 if (x > maxX)
660 x = maxX;
661 if (y < 0)
662 y = 0;
663 if (y > maxY)
664 y = maxY;
665
Bram Moolenaar734a8672019-12-02 22:49:38 +0100666 // Set desired window position in the DialogShell
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 XtVaSetValues(shell, XmNx, x, XmNy, y, NULL);
668
Bram Moolenaar734a8672019-12-02 22:49:38 +0100669 // Map the widget
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 XtMapWidget(shell);
671
Bram Moolenaar734a8672019-12-02 22:49:38 +0100672 // Restore the value of XmNmappedWhenManaged
Bram Moolenaar46784652008-06-20 09:40:11 +0000673 XtVaSetValues(shell, XmNmappedWhenManaged, mappedWhenManaged, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674}
675
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100676#if defined(FEAT_MENU) || defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678/*
679 * Encapsulate the way an XmFontList is created.
680 */
681 XmFontList
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100682gui_motif_create_fontlist(XFontStruct *font)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 XmFontList font_list;
685
686# if (XmVersion <= 1001)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100687 // Motif 1.1 method
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 font_list = XmFontListCreate(font, STRING_TAG);
689# else
Bram Moolenaar734a8672019-12-02 22:49:38 +0100690 // Motif 1.2 method
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 XmFontListEntry font_list_entry;
692
693 font_list_entry = XmFontListEntryCreate(STRING_TAG, XmFONT_IS_FONT,
694 (XtPointer)font);
695 font_list = XmFontListAppendEntry(NULL, font_list_entry);
696 XmFontListEntryFree(&font_list_entry);
697# endif
698 return font_list;
699}
700
701# if ((XmVersion > 1001) && defined(FEAT_XFONTSET)) || defined(PROTO)
702 XmFontList
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100703gui_motif_fontset2fontlist(XFontSet *fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 XmFontList font_list;
706
Bram Moolenaar734a8672019-12-02 22:49:38 +0100707 // Motif 1.2 method
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 XmFontListEntry font_list_entry;
709
710 font_list_entry = XmFontListEntryCreate(STRING_TAG,
711 XmFONT_IS_FONTSET,
712 (XtPointer)*fontset);
713 font_list = XmFontListAppendEntry(NULL, font_list_entry);
714 XmFontListEntryFree(&font_list_entry);
715 return font_list;
716}
717# endif
718
719#endif
720
721#if defined(FEAT_MENU) || defined(PROTO)
722/*
723 * Menu stuff.
724 */
725
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100726static void gui_motif_add_actext(vimmenu_T *menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#if (XmVersion >= 1002)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100728static void toggle_tearoff(Widget wid);
729static void gui_mch_recurse_tearoffs(vimmenu_T *menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100731static void submenu_change(vimmenu_T *mp, int colors);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100733static void do_set_mnemonics(int enable);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734static int menu_enabled = TRUE;
735
736 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100737gui_mch_enable_menu(int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738{
739 if (flag)
740 {
741 XtManageChild(menuBar);
742#ifdef FEAT_TOOLBAR
743 if (XtIsManaged(XtParent(toolBar)))
744 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100745 // toolBar is attached to top form
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 XtVaSetValues(XtParent(toolBar),
747 XmNtopAttachment, XmATTACH_WIDGET,
748 XmNtopWidget, menuBar,
749 NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000750#ifdef FEAT_GUI_TABLINE
751 if (showing_tabline)
752 {
753 XtVaSetValues(tabLine,
754 XmNtopAttachment, XmATTACH_WIDGET,
755 XmNtopWidget, XtParent(toolBar),
756 NULL);
757 XtVaSetValues(textAreaForm,
758 XmNtopAttachment, XmATTACH_WIDGET,
759 XmNtopWidget, tabLine,
760 NULL);
761 }
762 else
763#endif
764 XtVaSetValues(textAreaForm,
765 XmNtopAttachment, XmATTACH_WIDGET,
766 XmNtopWidget, XtParent(toolBar),
767 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 }
769 else
770#endif
771 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000772#ifdef FEAT_GUI_TABLINE
773 if (showing_tabline)
774 {
775 XtVaSetValues(tabLine,
776 XmNtopAttachment, XmATTACH_WIDGET,
777 XmNtopWidget, menuBar,
778 NULL);
779 XtVaSetValues(textAreaForm,
780 XmNtopAttachment, XmATTACH_WIDGET,
781 XmNtopWidget, tabLine,
782 NULL);
783 }
784 else
785#endif
786 XtVaSetValues(textAreaForm,
787 XmNtopAttachment, XmATTACH_WIDGET,
788 XmNtopWidget, menuBar,
789 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 }
792 else
793 {
794 XtUnmanageChild(menuBar);
795#ifdef FEAT_TOOLBAR
796 if (XtIsManaged(XtParent(toolBar)))
797 {
798 XtVaSetValues(XtParent(toolBar),
799 XmNtopAttachment, XmATTACH_FORM,
800 NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000801#ifdef FEAT_GUI_TABLINE
802 if (showing_tabline)
803 {
804 XtVaSetValues(tabLine,
805 XmNtopAttachment, XmATTACH_WIDGET,
806 XmNtopWidget, XtParent(toolBar),
807 NULL);
808 XtVaSetValues(textAreaForm,
809 XmNtopAttachment, XmATTACH_WIDGET,
810 XmNtopWidget, tabLine,
811 NULL);
812 }
813 else
814#endif
815 XtVaSetValues(textAreaForm,
816 XmNtopAttachment, XmATTACH_WIDGET,
817 XmNtopWidget, XtParent(toolBar),
818 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820 else
821#endif
822 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000823#ifdef FEAT_GUI_TABLINE
824 if (showing_tabline)
825 {
826 XtVaSetValues(tabLine,
827 XmNtopAttachment, XmATTACH_FORM,
828 NULL);
829 XtVaSetValues(textAreaForm,
830 XmNtopAttachment, XmATTACH_WIDGET,
831 XmNtopWidget, tabLine,
832 NULL);
833 }
834 else
835#endif
836 XtVaSetValues(textAreaForm,
837 XmNtopAttachment, XmATTACH_FORM,
838 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 }
841
842}
843
844/*
845 * Enable or disable mnemonics for the toplevel menus.
846 */
847 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100848gui_motif_set_mnemonics(int enable)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850 /*
851 * Don't enable menu mnemonics when the menu bar is disabled, LessTif
852 * crashes when using a mnemonic then.
853 */
854 if (!menu_enabled)
855 enable = FALSE;
856 do_set_mnemonics(enable);
857}
858
859 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100860do_set_mnemonics(int enable)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
862 vimmenu_T *menu;
863
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200864 FOR_ALL_MENUS(menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (menu->id != (Widget)0)
866 XtVaSetValues(menu->id,
867 XmNmnemonic, enable ? menu->mnemonic : NUL,
868 NULL);
869}
870
871 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100872gui_mch_add_menu(vimmenu_T *menu, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 XmString label;
875 Widget shell;
876 vimmenu_T *parent = menu->parent;
877
878#ifdef MOTIF_POPUP
879 if (menu_is_popup(menu->name))
880 {
881 Arg arg[2];
882 int n = 0;
883
Bram Moolenaar734a8672019-12-02 22:49:38 +0100884 // Only create the popup menu when it's actually used, otherwise there
885 // is a delay when using the right mouse button.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886# if (XmVersion <= 1002)
887 if (mouse_model_popup())
888# endif
889 {
890 if (gui.menu_bg_pixel != INVALCOLOR)
Bram Moolenaar7795bfe2020-09-20 19:57:15 +0200891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 XtSetArg(arg[0], XmNbackground, gui.menu_bg_pixel); n++;
Bram Moolenaar7795bfe2020-09-20 19:57:15 +0200893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (gui.menu_fg_pixel != INVALCOLOR)
Bram Moolenaar7795bfe2020-09-20 19:57:15 +0200895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 XtSetArg(arg[1], XmNforeground, gui.menu_fg_pixel); n++;
Bram Moolenaar7795bfe2020-09-20 19:57:15 +0200897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 menu->submenu_id = XmCreatePopupMenu(textArea, "contextMenu",
899 arg, n);
900 menu->id = (Widget)0;
901 }
902 return;
903 }
904#endif
905
906 if (!menu_is_menubar(menu->name)
907 || (parent != NULL && parent->submenu_id == (Widget)0))
908 return;
909
910 label = XmStringCreate((char *)menu->dname, STRING_TAG);
911 if (label == NULL)
912 return;
913 menu->id = XtVaCreateWidget("subMenu",
914 xmCascadeButtonWidgetClass,
915 (parent == NULL) ? menuBar : parent->submenu_id,
916 XmNlabelString, label,
917 XmNmnemonic, p_wak[0] == 'n' ? NUL : menu->mnemonic,
918#if (XmVersion >= 1002)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100919 // submenu: count the tearoff item (needed for LessTif)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 XmNpositionIndex, idx + (parent != NULL
921 && tearoff_val == (int)XmTEAR_OFF_ENABLED ? 1 : 0),
922#endif
923 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 XmStringFree(label);
925
Bram Moolenaar734a8672019-12-02 22:49:38 +0100926 if (menu->id == (Widget)0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 return;
928
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +0100929 // The "Help" menu is a special case, and should be placed at the far
930 // right hand side of the menu-bar. It's recognized by its high priority.
931 if (parent == NULL && menu->priority >= 9999)
932 XtVaSetValues(menuBar,
933 XmNmenuHelpWidget, menu->id,
934 NULL);
935
936 gui_motif_menu_colors(menu->id);
937 gui_motif_menu_fontlist(menu->id);
938
Bram Moolenaar734a8672019-12-02 22:49:38 +0100939 // add accelerator text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 gui_motif_add_actext(menu);
941
942 shell = XtVaCreateWidget("subMenuShell",
943 xmMenuShellWidgetClass, menu->id,
944 XmNwidth, 1,
945 XmNheight, 1,
946 NULL);
947 gui_motif_menu_colors(shell);
948 menu->submenu_id = XtVaCreateWidget("rowColumnMenu",
949 xmRowColumnWidgetClass, shell,
950 XmNrowColumnType, XmMENU_PULLDOWN,
951 NULL);
952 gui_motif_menu_colors(menu->submenu_id);
953
Bram Moolenaar734a8672019-12-02 22:49:38 +0100954 if (menu->submenu_id == (Widget)0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 return;
956
957#if (XmVersion >= 1002)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100958 // Set the colors for the tear off widget
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 toggle_tearoff(menu->submenu_id);
960#endif
961
962 XtVaSetValues(menu->id,
963 XmNsubMenuId, menu->submenu_id,
964 NULL);
965
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +0100966 // When we add a top-level item to the menu bar, we can figure out how
967 // high the menu bar should be.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (parent == NULL)
969 gui_mch_compute_menu_height(menu->id);
970}
971
972
973/*
974 * Add mnemonic and accelerator text to a menu button.
975 */
976 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100977gui_motif_add_actext(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 XmString label;
980
Bram Moolenaar734a8672019-12-02 22:49:38 +0100981 // Add accelerator text, if there is one
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000982 if (menu->actext == NULL || menu->id == (Widget)0)
983 return;
984
985 label = XmStringCreate((char *)menu->actext, STRING_TAG);
986 if (label == NULL)
987 return;
988 XtVaSetValues(menu->id, XmNacceleratorText, label, NULL);
989 XmStringFree(label);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990}
991
992 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100993gui_mch_toggle_tearoffs(int enable)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
995#if (XmVersion >= 1002)
996 if (enable)
997 tearoff_val = (int)XmTEAR_OFF_ENABLED;
998 else
999 tearoff_val = (int)XmTEAR_OFF_DISABLED;
1000 toggle_tearoff(menuBar);
1001 gui_mch_recurse_tearoffs(root_menu);
1002#endif
1003}
1004
1005#if (XmVersion >= 1002)
1006/*
1007 * Set the tearoff for one menu widget on or off, and set the color of the
1008 * tearoff widget.
1009 */
1010 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001011toggle_tearoff(Widget wid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
1013 Widget w;
1014
1015 XtVaSetValues(wid, XmNtearOffModel, tearoff_val, NULL);
1016 if (tearoff_val == (int)XmTEAR_OFF_ENABLED
1017 && (w = XmGetTearOffControl(wid)) != (Widget)0)
1018 gui_motif_menu_colors(w);
1019}
1020
1021 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001022gui_mch_recurse_tearoffs(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
1024 while (menu != NULL)
1025 {
1026 if (!menu_is_popup(menu->name))
1027 {
1028 if (menu->submenu_id != (Widget)0)
1029 toggle_tearoff(menu->submenu_id);
1030 gui_mch_recurse_tearoffs(menu->children);
1031 }
1032 menu = menu->next;
1033 }
1034}
1035#endif
1036
1037 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001038gui_mch_text_area_extra_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
1040 Dimension shadowHeight;
1041
1042 XtVaGetValues(textAreaForm, XmNshadowThickness, &shadowHeight, NULL);
1043 return shadowHeight;
1044}
1045
1046/*
1047 * Compute the height of the menu bar.
1048 * We need to check all the items for their position and height, for the case
1049 * there are several rows, and/or some characters extend higher or lower.
1050 */
1051 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001052gui_mch_compute_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01001053 Widget id) // can be NULL when deleting menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 Dimension y, maxy;
1056 Dimension margin, shadow;
1057 vimmenu_T *mp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001058 static Dimension height = 21; // normal height of a menu item
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 /*
1061 * Get the height of the new item, before managing it, because it will
1062 * still reflect the font size. After managing it depends on the menu
1063 * height, which is what we just wanted to get!.
1064 */
1065 if (id != (Widget)0)
1066 XtVaGetValues(id, XmNheight, &height, NULL);
1067
Bram Moolenaar734a8672019-12-02 22:49:38 +01001068 // Find any menu Widget, to be able to call XtManageChild()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 else
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001070 FOR_ALL_MENUS(mp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (mp->id != (Widget)0 && menu_is_menubar(mp->name))
1072 {
1073 id = mp->id;
1074 break;
1075 }
1076
1077 /*
1078 * Now manage the menu item, to make them all be positioned (makes an
1079 * extra row when needed, removes it when not needed).
1080 */
1081 if (id != (Widget)0)
1082 XtManageChild(id);
1083
1084 /*
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01001085 * Now find the menu item that is the furthest down, and get its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
1087 maxy = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001088 FOR_ALL_MENUS(mp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
1090 if (mp->id != (Widget)0 && menu_is_menubar(mp->name))
1091 {
1092 XtVaGetValues(mp->id, XmNy, &y, NULL);
1093 if (y > maxy)
1094 maxy = y;
1095 }
1096 }
1097
1098 XtVaGetValues(menuBar,
1099 XmNmarginHeight, &margin,
1100 XmNshadowThickness, &shadow,
1101 NULL);
1102
1103 /*
1104 * This computation is the result of trial-and-error:
1105 * maxy = The maximum position of an item; required for when there are
1106 * two or more rows
1107 * height = height of an item, before managing it; Hopefully this will
1108 * change with the font height. Includes shadow-border.
1109 * shadow = shadow-border; must be subtracted from the height.
1110 * margin = margin around the menu buttons; Must be added.
1111 * Add 4 for the underlining of shortcut keys.
1112 */
1113 gui.menu_height = maxy + height - 2 * shadow + 2 * margin + 4;
1114
Bram Moolenaar734a8672019-12-02 22:49:38 +01001115 // Somehow the menu bar doesn't resize automatically. Set it here,
1116 // even though this is a catch 22. Don't do this when starting up,
1117 // somehow the menu gets very high then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (gui.shell_created)
1119 XtVaSetValues(menuBar, XmNheight, gui.menu_height, NULL);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001120}
1121
Bram Moolenaarb23c3382005-01-31 19:09:12 +00001122#ifdef FEAT_TOOLBAR
1123
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001124/*
1125 * Icons used by the toolbar code.
1126 */
1127#include "gui_x11_pm.h"
1128
Bram Moolenaard25c16e2016-01-29 22:13:30 +01001129static char **get_toolbar_pixmap(vimmenu_T *menu, char **fname);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001130
1131/*
1132 * Read an Xpm file. Return OK or FAIL.
1133 */
1134 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001135check_xpm(char_u *path)
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001136{
1137 XpmAttributes attrs;
1138 int status;
1139 Pixmap mask;
1140 Pixmap map;
1141
1142 attrs.valuemask = 0;
1143
Bram Moolenaar734a8672019-12-02 22:49:38 +01001144 // Create the "sensitive" pixmap
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001145 status = XpmReadFileToPixmap(gui.dpy,
1146 RootWindow(gui.dpy, DefaultScreen(gui.dpy)),
1147 (char *)path, &map, &mask, &attrs);
1148 XpmFreeAttributes(&attrs);
1149
1150 if (status == XpmSuccess)
1151 return OK;
1152 return FAIL;
1153}
1154
1155
1156/*
1157 * Allocated a pixmap for toolbar menu "menu".
Bram Moolenaar7c626922005-02-07 22:01:03 +00001158 * When it's to be read from a file, "fname" is set to the file name
1159 * (in allocated memory).
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001160 * Return a blank pixmap if it fails.
1161 */
1162 static char **
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001163get_toolbar_pixmap(vimmenu_T *menu, char **fname)
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001164{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001165 char_u buf[MAXPATHL]; // buffer storing expanded pathname
1166 char **xpm = NULL; // xpm array
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001167 int res;
1168
Bram Moolenaar7c626922005-02-07 22:01:03 +00001169 *fname = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001170 buf[0] = NUL; // start with NULL path
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001171
1172 if (menu->iconfile != NULL)
1173 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001174 // Use the "icon=" argument.
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001175 gui_find_iconfile(menu->iconfile, buf, "xpm");
1176 res = check_xpm(buf);
1177
Bram Moolenaar734a8672019-12-02 22:49:38 +01001178 // If it failed, try using the menu name.
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001179 if (res == FAIL && gui_find_bitmap(menu->name, buf, "xpm") == OK)
1180 res = check_xpm(buf);
1181 if (res == OK)
Bram Moolenaar7c626922005-02-07 22:01:03 +00001182 {
1183 *fname = (char *)vim_strsave(buf);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001184 return tb_blank_xpm;
Bram Moolenaar7c626922005-02-07 22:01:03 +00001185 }
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001186 }
1187
1188 if (menu->icon_builtin || gui_find_bitmap(menu->name, buf, "xpm") == FAIL)
1189 {
1190 if (menu->iconidx >= 0 && menu->iconidx
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001191 < (int)(sizeof(built_in_pixmaps) / sizeof(built_in_pixmaps[0])))
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001192 xpm = built_in_pixmaps[menu->iconidx];
1193 else
1194 xpm = tb_blank_xpm;
1195 }
1196
1197 return xpm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198}
Bram Moolenaar7c626922005-02-07 22:01:03 +00001199
1200/*
1201 * Add arguments for the toolbar pixmap to a menu item.
1202 */
1203 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001204add_pixmap_args(vimmenu_T *menu, Arg *args, int n)
Bram Moolenaar7c626922005-02-07 22:01:03 +00001205{
1206 vim_free(menu->xpm_fname);
1207 menu->xpm = get_toolbar_pixmap(menu, &menu->xpm_fname);
1208 if (menu->xpm == NULL)
1209 {
1210 XtSetArg(args[n], XmNlabelType, XmSTRING); n++;
1211 }
1212 else
1213 {
1214 if (menu->xpm_fname != NULL)
Bram Moolenaar26cd3062020-09-20 21:13:27 +02001215 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00001216 XtSetArg(args[n], XmNpixmapFile, menu->xpm_fname); n++;
Bram Moolenaar26cd3062020-09-20 21:13:27 +02001217 }
Bram Moolenaar7c626922005-02-07 22:01:03 +00001218 XtSetArg(args[n], XmNpixmapData, menu->xpm); n++;
1219 XtSetArg(args[n], XmNlabelLocation, XmBOTTOM); n++;
1220 }
1221 return n;
1222}
Bram Moolenaar734a8672019-12-02 22:49:38 +01001223#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
1225 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001226gui_mch_add_menu_item(vimmenu_T *menu, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227{
1228 XmString label;
1229 vimmenu_T *parent = menu->parent;
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231# if (XmVersion <= 1002)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001232 // Don't add Popup menu items when the popup menu isn't used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (menu_is_child_of_popup(menu) && !mouse_model_popup())
1234 return;
1235# endif
1236
1237# ifdef FEAT_TOOLBAR
1238 if (menu_is_toolbar(parent->name))
1239 {
1240 WidgetClass type;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001241 XmString xms = NULL; // fallback label if pixmap not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 int n;
1243 Arg args[18];
1244
1245 n = 0;
1246 if (menu_is_separator(menu->name))
1247 {
1248 char *cp;
1249 Dimension wid;
1250
1251 /*
1252 * A separator has the format "-sep%d[:%d]-". The optional :%d is
1253 * a width specifier. If no width is specified then we choose one.
1254 */
1255 cp = (char *)vim_strchr(menu->name, ':');
1256 if (cp != NULL)
1257 wid = (Dimension)atoi(++cp);
1258 else
1259 wid = 4;
1260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 type = xmSeparatorWidgetClass;
1262 XtSetArg(args[n], XmNwidth, wid); n++;
1263 XtSetArg(args[n], XmNminWidth, wid); n++;
1264 XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001265 XtSetArg(args[n], XmNseparatorType, XmSHADOW_ETCHED_IN); n++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 else
1268 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001269 // Without shadows one can't sense whatever the button has been
1270 // pressed or not! However we want to save a bit of space...
1271 // Need the highlightThickness to see the focus.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001272 XtSetArg(args[n], XmNhighlightThickness, 1); n++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 XtSetArg(args[n], XmNhighlightOnEnter, True); n++;
1274 XtSetArg(args[n], XmNmarginWidth, 0); n++;
1275 XtSetArg(args[n], XmNmarginHeight, 0); n++;
Bram Moolenaarf9980f12005-01-03 20:58:59 +00001276 XtSetArg(args[n], XmNtraversalOn, False); n++;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001277 // Set the label here, so that we can switch between icons/text
1278 // by changing the XmNlabelType resource.
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001279 xms = XmStringCreate((char *)menu->dname, STRING_TAG);
1280 XtSetArg(args[n], XmNlabelString, xms); n++;
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001281
Bram Moolenaar7c626922005-02-07 22:01:03 +00001282 n = add_pixmap_args(menu, args, n);
1283
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001284 type = xmEnhancedButtonWidgetClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286
1287 XtSetArg(args[n], XmNpositionIndex, idx); n++;
1288 if (menu->id == NULL)
1289 {
1290 menu->id = XtCreateManagedWidget((char *)menu->dname,
1291 type, toolBar, args, n);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001292 if (menu->id != NULL && type == xmEnhancedButtonWidgetClass)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {
1294 XtAddCallback(menu->id,
1295 XmNactivateCallback, gui_x11_menu_cb, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
1297 }
1298 else
1299 XtSetValues(menu->id, args, n);
1300 if (xms != NULL)
1301 XmStringFree(xms);
1302
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001303# ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 gui_mch_menu_set_tip(menu);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
1307 menu->parent = parent;
1308 menu->submenu_id = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001309 // When adding first item to toolbar it might have to be enabled .
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (!XtIsManaged(XtParent(toolBar))
1311 && vim_strchr(p_go, GO_TOOLBAR) != NULL)
1312 gui_mch_show_toolbar(TRUE);
1313 gui.toolbar_height = gui_mch_compute_toolbar_height();
1314 return;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001315 } // toolbar menu item
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316# endif
1317
Bram Moolenaar734a8672019-12-02 22:49:38 +01001318 // No parent, must be a non-menubar menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 if (parent->submenu_id == (Widget)0)
1320 return;
1321
1322 menu->submenu_id = (Widget)0;
1323
Bram Moolenaar734a8672019-12-02 22:49:38 +01001324 // Add menu separator
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (menu_is_separator(menu->name))
1326 {
1327 menu->id = XtVaCreateWidget("subMenu",
1328 xmSeparatorGadgetClass, parent->submenu_id,
1329#if (XmVersion >= 1002)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001330 // count the tearoff item (needed for LessTif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 XmNpositionIndex, idx + (tearoff_val == (int)XmTEAR_OFF_ENABLED
1332 ? 1 : 0),
1333#endif
1334 NULL);
1335 gui_motif_menu_colors(menu->id);
1336 return;
1337 }
1338
1339 label = XmStringCreate((char *)menu->dname, STRING_TAG);
1340 if (label == NULL)
1341 return;
1342 menu->id = XtVaCreateWidget("subMenu",
1343 xmPushButtonWidgetClass, parent->submenu_id,
1344 XmNlabelString, label,
1345 XmNmnemonic, menu->mnemonic,
1346#if (XmVersion >= 1002)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001347 // count the tearoff item (needed for LessTif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 XmNpositionIndex, idx + (tearoff_val == (int)XmTEAR_OFF_ENABLED
1349 ? 1 : 0),
1350#endif
1351 NULL);
1352 gui_motif_menu_colors(menu->id);
1353 gui_motif_menu_fontlist(menu->id);
1354 XmStringFree(label);
1355
1356 if (menu->id != (Widget)0)
1357 {
1358 XtAddCallback(menu->id, XmNactivateCallback, gui_x11_menu_cb,
1359 (XtPointer)menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001360 // add accelerator text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 gui_motif_add_actext(menu);
1362 }
1363}
1364
1365#if (XmVersion <= 1002) || defined(PROTO)
1366/*
1367 * This function will destroy/create the popup menus dynamically,
1368 * according to the value of 'mousemodel'.
1369 * This will fix the "right mouse button freeze" that occurs when
1370 * there exists a popup menu but it isn't managed.
1371 */
1372 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001373gui_motif_update_mousemodel(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 int idx = 0;
1376
Bram Moolenaar734a8672019-12-02 22:49:38 +01001377 // When GUI hasn't started the menus have not been created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (!gui.in_use)
1379 return;
1380
1381 while (menu)
1382 {
1383 if (menu->children != NULL)
1384 {
1385 if (menu_is_popup(menu->name))
1386 {
1387 if (mouse_model_popup())
1388 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001389 // Popup menu will be used. Create the popup menus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 gui_mch_add_menu(menu, idx);
1391 gui_motif_update_mousemodel(menu->children);
1392 }
1393 else
1394 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001395 // Popup menu will not be used. Destroy the popup menus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 gui_motif_update_mousemodel(menu->children);
1397 gui_mch_destroy_menu(menu);
1398 }
1399 }
1400 }
1401 else if (menu_is_child_of_popup(menu))
1402 {
1403 if (mouse_model_popup())
1404 gui_mch_add_menu_item(menu, idx);
1405 else
1406 gui_mch_destroy_menu(menu);
1407 }
1408 menu = menu->next;
1409 ++idx;
1410 }
1411}
1412#endif
1413
1414 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001415gui_mch_new_menu_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 if (menuBar == (Widget)0)
1418 return;
1419 gui_motif_menu_colors(menuBar);
1420#ifdef FEAT_TOOLBAR
1421 gui_motif_menu_colors(toolBarFrame);
1422 gui_motif_menu_colors(toolBar);
1423#endif
1424
Bram Moolenaarf9980f12005-01-03 20:58:59 +00001425 submenu_change(root_menu, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426}
1427
1428 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001429gui_mch_new_menu_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 if (menuBar == (Widget)0)
1432 return;
Bram Moolenaarf9980f12005-01-03 20:58:59 +00001433 submenu_change(root_menu, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
1435 Dimension height;
1436 Position w, h;
1437
1438 XtVaGetValues(menuBar, XmNheight, &height, NULL);
1439 gui.menu_height = height;
1440
1441 XtVaGetValues(vimShell, XtNwidth, &w, XtNheight, &h, NULL);
1442 gui_resize_shell(w, h
1443#ifdef FEAT_XIM
1444 - xim_get_status_area_height()
1445#endif
1446 );
1447 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001448 gui_set_shellsize(FALSE, TRUE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 ui_new_shellsize();
1450}
1451
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001452#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001454gui_mch_new_tooltip_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
1456# ifdef FEAT_TOOLBAR
1457 vimmenu_T *menu;
1458
1459 if (toolBar == (Widget)0)
1460 return;
1461
1462 menu = gui_find_menu((char_u *)"ToolBar");
1463 if (menu != NULL)
Bram Moolenaarf9980f12005-01-03 20:58:59 +00001464 submenu_change(menu, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465# endif
1466}
1467
1468 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001469gui_mch_new_tooltip_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471# ifdef FEAT_TOOLBAR
1472 vimmenu_T *toolbar;
1473
1474 if (toolBar == (Widget)0)
1475 return;
1476
1477 toolbar = gui_find_menu((char_u *)"ToolBar");
1478 if (toolbar != NULL)
Bram Moolenaarf9980f12005-01-03 20:58:59 +00001479 submenu_change(toolbar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480# endif
1481}
1482#endif
1483
1484 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001485submenu_change(
1486 vimmenu_T *menu,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001487 int colors) // TRUE for colors, FALSE for font
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 vimmenu_T *mp;
1490
1491 for (mp = menu; mp != NULL; mp = mp->next)
1492 {
1493 if (mp->id != (Widget)0)
1494 {
1495 if (colors)
1496 {
1497 gui_motif_menu_colors(mp->id);
1498#ifdef FEAT_TOOLBAR
Bram Moolenaar734a8672019-12-02 22:49:38 +01001499 // For a toolbar item: Free the pixmap and allocate a new one,
1500 // so that the background color is right.
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001501 if (mp->xpm != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001503 int n = 0;
1504 Arg args[18];
1505
Bram Moolenaar7c626922005-02-07 22:01:03 +00001506 n = add_pixmap_args(mp, args, n);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00001507 XtSetValues(mp->id, args, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001509# ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +01001510 // If we have a tooltip, then we need to change its font
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (mp->tip != NULL)
1512 {
1513 Arg args[2];
1514
1515 args[0].name = XmNbackground;
1516 args[0].value = gui.tooltip_bg_pixel;
1517 args[1].name = XmNforeground;
1518 args[1].value = gui.tooltip_fg_pixel;
1519 XtSetValues(mp->tip->balloonLabel, &args[0], XtNumber(args));
1520 }
1521# endif
1522#endif
1523 }
1524 else
1525 {
1526 gui_motif_menu_fontlist(mp->id);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001527#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +01001528 // If we have a tooltip, then we need to change its font
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (mp->tip != NULL)
1530 {
1531 Arg args[1];
1532
1533 args[0].name = XmNfontList;
1534 args[0].value = (XtArgVal)gui_motif_fontset2fontlist(
1535 &gui.tooltip_fontset);
1536 XtSetValues(mp->tip->balloonLabel, &args[0], XtNumber(args));
1537 }
1538#endif
1539 }
1540 }
1541
1542 if (mp->children != NULL)
1543 {
1544#if (XmVersion >= 1002)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001545 // Set the colors/font for the tear off widget
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (mp->submenu_id != (Widget)0)
1547 {
1548 if (colors)
1549 gui_motif_menu_colors(mp->submenu_id);
1550 else
1551 gui_motif_menu_fontlist(mp->submenu_id);
1552 toggle_tearoff(mp->submenu_id);
1553 }
1554#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01001555 // Set the colors for the children
Bram Moolenaarf9980f12005-01-03 20:58:59 +00001556 submenu_change(mp->children, colors);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 }
1558 }
1559}
1560
1561/*
1562 * Destroy the machine specific menu widget.
1563 */
1564 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001565gui_mch_destroy_menu(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001567 // Please be sure to destroy the parent widget first (i.e. menu->id).
1568 // On the other hand, problems have been reported that the submenu must be
1569 // deleted first...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (menu->submenu_id != (Widget)0)
1571 {
1572 XtDestroyWidget(menu->submenu_id);
1573 menu->submenu_id = (Widget)0;
1574 }
1575
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001576 if (menu->id == (Widget)0)
1577 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001579 Widget parent;
1580
1581 parent = XtParent(menu->id);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001582#if defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL_GUI)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001583 if (parent == toolBar && menu->tip != NULL)
1584 {
1585 // We try to destroy this before the actual menu, because there are
1586 // callbacks, etc. that will be unregistered during the tooltip
1587 // destruction.
1588 //
1589 // If you call "gui_mch_destroy_beval_area()" after destroying
1590 // menu->id, then the tooltip's window will have already been
1591 // deallocated by Xt, and unknown behaviour will ensue (probably
1592 // a core dump).
1593 gui_mch_destroy_beval_area(menu->tip);
1594 menu->tip = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001596#endif
1597 XtDestroyWidget(menu->id);
1598 menu->id = (Widget)0;
1599 if (parent == menuBar)
1600 gui_mch_compute_menu_height((Widget)0);
1601#ifdef FEAT_TOOLBAR
1602 else if (parent == toolBar)
1603 {
1604 Cardinal num_children;
1605
1606 // When removing last toolbar item, don't display the toolbar.
1607 XtVaGetValues(toolBar, XmNnumChildren, &num_children, NULL);
1608 if (num_children == 0)
1609 gui_mch_show_toolbar(FALSE);
1610 else
1611 gui.toolbar_height = gui_mch_compute_toolbar_height();
1612 }
1613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614}
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001617gui_mch_show_popupmenu(vimmenu_T *menu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619#ifdef MOTIF_POPUP
1620 XmMenuPosition(menu->submenu_id, gui_x11_get_last_mouse_event());
1621 XtManageChild(menu->submenu_id);
1622#endif
1623}
1624
Bram Moolenaar734a8672019-12-02 22:49:38 +01001625#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627/*
1628 * Set the menu and scrollbar colors to their default values.
1629 */
1630 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001631gui_mch_def_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001633 if (!gui.in_use)
1634 return;
1635
1636 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1637 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1638 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1639 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001640#ifdef FEAT_BEVAL_GUI
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001641 gui.tooltip_fg_pixel =
1642 gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1643 gui.tooltip_bg_pixel =
1644 gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646}
1647
1648
1649/*
1650 * Scrollbar stuff.
1651 */
1652
1653 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001654gui_mch_set_scrollbar_thumb(
1655 scrollbar_T *sb,
1656 long val,
1657 long size,
1658 long max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 if (sb->id != (Widget)0)
1661 XtVaSetValues(sb->id,
1662 XmNvalue, val,
1663 XmNsliderSize, size,
1664 XmNpageIncrement, (size > 2 ? size - 2 : 1),
Bram Moolenaar734a8672019-12-02 22:49:38 +01001665 XmNmaximum, max + 1, // Motif has max one past the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 NULL);
1667}
1668
1669 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001670gui_mch_set_scrollbar_pos(
1671 scrollbar_T *sb,
1672 int x,
1673 int y,
1674 int w,
1675 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001677 if (sb->id == (Widget)0)
1678 return;
1679
1680 if (sb->type == SBAR_LEFT || sb->type == SBAR_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001682 if (y == 0)
1683 h -= gui.border_offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001685 y -= gui.border_offset;
1686 XtVaSetValues(sb->id,
1687 XmNtopOffset, y,
1688 XmNbottomOffset, -y - h,
1689 XmNwidth, w,
1690 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001692 else
1693 XtVaSetValues(sb->id,
1694 XmNtopOffset, y,
1695 XmNleftOffset, x,
1696 XmNrightOffset, gui.which_scrollbars[SBAR_RIGHT]
1697 ? gui.scrollbar_width : 0,
1698 XmNheight, h,
1699 NULL);
1700 XtManageChild(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701}
1702
Bram Moolenaar203ec772020-07-17 20:43:43 +02001703 int
1704gui_mch_get_scrollbar_xpadding(void)
1705{
qsmodo28f1a512022-02-07 15:57:50 +00001706 int xpad;
1707 Dimension tw, ww;
1708 Position tx;
1709
1710 XtVaGetValues(textArea, XtNwidth, &tw, XtNx, &tx, NULL);
1711 XtVaGetValues(vimShell, XtNwidth, &ww, NULL);
1712 xpad = ww - tw - tx - gui.scrollbar_width;
1713 return (xpad < 0) ? 0 : xpad;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001714}
1715
1716 int
1717gui_mch_get_scrollbar_ypadding(void)
1718{
qsmodo28f1a512022-02-07 15:57:50 +00001719 int ypad;
1720 Dimension th, wh;
1721 Position ty;
1722
1723 XtVaGetValues(textArea, XtNheight, &th, XtNy, &ty, NULL);
1724 XtVaGetValues(vimShell, XtNheight, &wh, NULL);
1725 ypad = wh - th - ty - gui.scrollbar_height;
1726 return (ypad < 0) ? 0 : ypad;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001727}
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001730gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
1732 Arg args[16];
1733 int n;
1734
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001735 if (sb->id == (Widget)0)
1736 return;
1737
1738 n = 0;
1739 if (flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001741 switch (sb->type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001743 case SBAR_LEFT:
1744 XtSetArg(args[n], XmNleftOffset, gui.scrollbar_width); n++;
1745 break;
1746
1747 case SBAR_RIGHT:
1748 XtSetArg(args[n], XmNrightOffset, gui.scrollbar_width); n++;
1749 break;
1750
1751 case SBAR_BOTTOM:
1752 XtSetArg(args[n], XmNbottomOffset, gui.scrollbar_height);n++;
1753 break;
1754 }
1755 XtSetValues(textArea, args, n);
1756 XtManageChild(sb->id);
1757 }
1758 else
1759 {
1760 if (!gui.which_scrollbars[sb->type])
1761 {
1762 // The scrollbars of this type are all disabled, adjust the
1763 // textArea attachment offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 switch (sb->type)
1765 {
1766 case SBAR_LEFT:
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001767 XtSetArg(args[n], XmNleftOffset, 0); n++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 break;
1769
1770 case SBAR_RIGHT:
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001771 XtSetArg(args[n], XmNrightOffset, 0); n++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 break;
1773
1774 case SBAR_BOTTOM:
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001775 XtSetArg(args[n], XmNbottomOffset, 0);n++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 break;
1777 }
1778 XtSetValues(textArea, args, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001780 XtUnmanageChild(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 }
1782}
1783
1784 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001785gui_mch_create_scrollbar(
1786 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001787 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 Arg args[16];
1790 int n;
1791
1792 n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 XtSetArg(args[n], XmNminimum, 0); n++;
1794 XtSetArg(args[n], XmNorientation,
1795 (orient == SBAR_VERT) ? XmVERTICAL : XmHORIZONTAL); n++;
1796
1797 switch (sb->type)
1798 {
1799 case SBAR_LEFT:
1800 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
1801 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_FORM); n++;
1802 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
1803 break;
1804
1805 case SBAR_RIGHT:
1806 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
1807 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_FORM); n++;
1808 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
1809 break;
1810
1811 case SBAR_BOTTOM:
1812 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
1813 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
1814 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
1815 break;
1816 }
1817
1818 sb->id = XtCreateWidget("scrollBar",
1819 xmScrollBarWidgetClass, textAreaForm, args, n);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001820 if (sb->id == (Widget)0)
1821 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001823 gui_mch_set_scrollbar_colors(sb);
1824 XtAddCallback(sb->id, XmNvalueChangedCallback,
1825 scroll_cb, (XtPointer)sb->ident);
1826 XtAddCallback(sb->id, XmNdragCallback,
1827 scroll_cb, (XtPointer)sb->ident);
1828 XtAddEventHandler(sb->id, KeyPressMask, FALSE, gui_x11_key_hit_cb,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 (XtPointer)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830}
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001833gui_mch_destroy_scrollbar(scrollbar_T *sb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 if (sb->id != (Widget)0)
1836 XtDestroyWidget(sb->id);
1837}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001840gui_mch_set_scrollbar_colors(scrollbar_T *sb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 if (sb->id != (Widget)0)
1843 {
1844 if (gui.scroll_bg_pixel != INVALCOLOR)
1845 {
1846#if (XmVersion>=1002)
Bram Moolenaare8504392022-03-13 14:45:03 +00001847 // This should not only set the through color but also adjust
1848 // related colors, such as shadows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 XmChangeColor(sb->id, gui.scroll_bg_pixel);
Bram Moolenaare8504392022-03-13 14:45:03 +00001850#endif
1851
1852 // Set the through color directly, in case XmChangeColor() decided
1853 // to change it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 XtVaSetValues(sb->id,
1855 XmNtroughColor, gui.scroll_bg_pixel,
1856 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858
1859 if (gui.scroll_fg_pixel != INVALCOLOR)
1860 XtVaSetValues(sb->id,
1861 XmNforeground, gui.scroll_fg_pixel,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 XmNbackground, gui.scroll_fg_pixel,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 NULL);
1864 }
1865
Bram Moolenaar734a8672019-12-02 22:49:38 +01001866 // This is needed for the rectangle below the vertical scrollbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (sb == &gui.bottom_sbar && textAreaForm != (Widget)0)
1868 gui_motif_scroll_colors(textAreaForm);
1869}
1870
1871/*
1872 * Miscellaneous stuff:
1873 */
1874
1875 Window
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001876gui_x11_get_wid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
1878 return(XtWindow(textArea));
1879}
1880
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001881/*
1882 * Look for a widget in the widget tree w, with a mnemonic matching keycode.
1883 * When one is found, simulate a button press on that widget and give it the
1884 * keyboard focus. If the mnemonic is on a label, look in the userData field
1885 * of the label to see if it points to another widget, and give that the focus.
1886 */
1887 static void
1888do_mnemonic(Widget w, unsigned int keycode)
1889{
1890 WidgetList children;
1891 int numChildren, i;
1892 Boolean isMenu;
1893 KeySym mnemonic = '\0';
1894 char mneString[2];
1895 Widget userData;
1896 unsigned char rowColType;
1897
1898 if (XtIsComposite(w))
1899 {
1900 if (XtClass(w) == xmRowColumnWidgetClass)
1901 {
Bram Moolenaar46784652008-06-20 09:40:11 +00001902 XtVaGetValues(w, XmNrowColumnType, &rowColType, NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001903 isMenu = (rowColType != (unsigned char)XmWORK_AREA);
1904 }
1905 else
1906 isMenu = False;
1907 if (!isMenu)
1908 {
1909 XtVaGetValues(w, XmNchildren, &children, XmNnumChildren,
Bram Moolenaar46784652008-06-20 09:40:11 +00001910 &numChildren, NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001911 for (i = 0; i < numChildren; i++)
1912 do_mnemonic(children[i], keycode);
1913 }
1914 }
1915 else
1916 {
Bram Moolenaar46784652008-06-20 09:40:11 +00001917 XtVaGetValues(w, XmNmnemonic, &mnemonic, NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001918 if (mnemonic != '\0')
1919 {
1920 mneString[0] = mnemonic;
1921 mneString[1] = '\0';
1922 if (XKeysymToKeycode(XtDisplay(XtParent(w)),
1923 XStringToKeysym(mneString)) == keycode)
1924 {
1925 if (XtClass(w) == xmLabelWidgetClass
1926 || XtClass(w) == xmLabelGadgetClass)
1927 {
Bram Moolenaar46784652008-06-20 09:40:11 +00001928 XtVaGetValues(w, XmNuserData, &userData, NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001929 if (userData != NULL && XtIsWidget(userData))
1930 XmProcessTraversal(userData, XmTRAVERSE_CURRENT);
1931 }
1932 else
1933 {
1934 XKeyPressedEvent keyEvent;
1935
1936 XmProcessTraversal(w, XmTRAVERSE_CURRENT);
1937
Bram Moolenaara80faa82020-04-12 19:37:17 +02001938 CLEAR_FIELD(keyEvent);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001939 keyEvent.type = KeyPress;
1940 keyEvent.serial = 1;
1941 keyEvent.send_event = True;
1942 keyEvent.display = XtDisplay(w);
1943 keyEvent.window = XtWindow(w);
1944 XtCallActionProc(w, "Activate", (XEvent *) & keyEvent,
1945 NULL, 0);
1946 }
1947 }
1948 }
1949 }
1950}
1951
1952/*
1953 * Callback routine for dialog mnemonic processing.
1954 */
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001955 static void
Bram Moolenaar9dbe7012021-03-29 20:10:26 +02001956mnemonic_event(
1957 Widget w,
1958 XtPointer call_data UNUSED,
1959 XKeyEvent *event,
1960 Boolean *b UNUSED)
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001961{
1962 do_mnemonic(w, event->keycode);
1963}
1964
1965
1966/*
1967 * Search the widget tree under w for widgets with mnemonics. When found, add
1968 * a passive grab to the dialog widget for the mnemonic character, thus
1969 * directing mnemonic events to the dialog widget.
1970 */
1971 static void
1972add_mnemonic_grabs(Widget dialog, Widget w)
1973{
1974 char mneString[2];
1975 WidgetList children;
1976 int numChildren, i;
1977 Boolean isMenu;
1978 KeySym mnemonic = '\0';
1979 unsigned char rowColType;
1980
1981 if (XtIsComposite(w))
1982 {
1983 if (XtClass(w) == xmRowColumnWidgetClass)
1984 {
Bram Moolenaar46784652008-06-20 09:40:11 +00001985 XtVaGetValues(w, XmNrowColumnType, &rowColType, NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001986 isMenu = (rowColType != (unsigned char)XmWORK_AREA);
1987 }
1988 else
1989 isMenu = False;
1990 if (!isMenu)
1991 {
1992 XtVaGetValues(w, XmNchildren, &children, XmNnumChildren,
Bram Moolenaar46784652008-06-20 09:40:11 +00001993 &numChildren, NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001994 for (i = 0; i < numChildren; i++)
1995 add_mnemonic_grabs(dialog, children[i]);
1996 }
1997 }
1998 else
1999 {
Bram Moolenaar46784652008-06-20 09:40:11 +00002000 XtVaGetValues(w, XmNmnemonic, &mnemonic, NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002001 if (mnemonic != '\0')
2002 {
2003 mneString[0] = mnemonic;
2004 mneString[1] = '\0';
2005 XtGrabKey(dialog, XKeysymToKeycode(XtDisplay(dialog),
2006 XStringToKeysym(mneString)),
2007 Mod1Mask, True, GrabModeAsync, GrabModeAsync);
2008 }
2009 }
2010}
2011
2012/*
2013 * Add a handler for mnemonics in a dialog. Motif itself only handles
2014 * mnemonics in menus. Mnemonics added or changed after this call will be
2015 * ignored.
2016 *
2017 * To add a mnemonic to a text field or list, set the XmNmnemonic resource on
2018 * the appropriate label and set the XmNuserData resource of the label to the
2019 * widget to get the focus when the mnemonic is typed.
2020 */
2021 static void
2022activate_dialog_mnemonics(Widget dialog)
2023{
2024 if (!dialog)
2025 return;
2026
2027 XtAddEventHandler(dialog, KeyPressMask, False,
2028 (XtEventHandler) mnemonic_event, (XtPointer) NULL);
2029 add_mnemonic_grabs(dialog, dialog);
2030}
2031
2032/*
2033 * Removes the event handler and key-grabs for dialog mnemonic handling.
2034 */
2035 static void
2036suppress_dialog_mnemonics(Widget dialog)
2037{
2038 if (!dialog)
2039 return;
2040
2041 XtUngrabKey(dialog, AnyKey, Mod1Mask);
2042 XtRemoveEventHandler(dialog, KeyPressMask, False,
2043 (XtEventHandler) mnemonic_event, (XtPointer) NULL);
2044}
2045
2046#if defined(FEAT_BROWSE) || defined(FEAT_GUI_DIALOG)
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002047/*
2048 * Use the 'guifont' or 'guifontset' as a fontlist for a dialog widget.
2049 */
2050 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002051set_fontlist(Widget id)
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002052{
2053 XmFontList fl;
2054
2055#ifdef FONTSET_ALWAYS
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00002056 if (gui.fontset != NOFONTSET)
2057 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002058 fl = gui_motif_fontset2fontlist((XFontSet *)&gui.fontset);
2059 if (fl != NULL)
2060 {
2061 if (XtIsManaged(id))
2062 {
2063 XtUnmanageChild(id);
2064 XtVaSetValues(id, XmNfontList, fl, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002065 // We should force the widget to recalculate its
2066 // geometry now.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002067 XtManageChild(id);
2068 }
2069 else
2070 XtVaSetValues(id, XmNfontList, fl, NULL);
2071 XmFontListFree(fl);
2072 }
2073 }
2074#else
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00002075 if (gui.norm_font != NOFONT)
2076 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002077 fl = gui_motif_create_fontlist((XFontStruct *)gui.norm_font);
2078 if (fl != NULL)
2079 {
2080 if (XtIsManaged(id))
2081 {
2082 XtUnmanageChild(id);
2083 XtVaSetValues(id, XmNfontList, fl, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002084 // We should force the widget to recalculate its
2085 // geometry now.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002086 XtManageChild(id);
2087 }
2088 else
2089 XtVaSetValues(id, XmNfontList, fl, NULL);
2090 XmFontListFree(fl);
2091 }
2092 }
2093#endif
2094}
2095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097#if defined(FEAT_BROWSE) || defined(PROTO)
2098
2099/*
2100 * file selector related stuff
2101 */
2102
2103#include <Xm/FileSB.h>
2104#include <Xm/XmStrDefs.h>
2105
2106typedef struct dialog_callback_arg
2107{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002108 char * args; // not used right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 int id;
2110} dcbarg_T;
2111
2112static Widget dialog_wgt;
2113static char *browse_fname = NULL;
2114static XmStringCharSet charset = (XmStringCharSet) XmSTRING_DEFAULT_CHARSET;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002115 // used to set up XmStrings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
Bram Moolenaard25c16e2016-01-29 22:13:30 +01002117static void DialogCancelCB(Widget, XtPointer, XtPointer);
2118static void DialogAcceptCB(Widget, XtPointer, XtPointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120/*
2121 * This function is used to translate the predefined label text of the
2122 * precomposed dialogs. We do this explicitly to allow:
2123 *
2124 * - usage of gettext for translation, as in all the other places.
2125 *
2126 * - equalize the messages between different GUI implementations as far as
2127 * possible.
2128 */
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002129 static void
2130set_predefined_label(Widget parent, String name, char *new_label)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002132 XmString str;
2133 Widget w;
2134 char_u *p, *next;
2135 KeySym mnemonic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 w = XtNameToWidget(parent, name);
2138
2139 if (!w)
2140 return;
2141
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002142 p = vim_strsave((char_u *)new_label);
2143 if (p == NULL)
2144 return;
2145 for (next = p; *next; ++next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002147 if (*next == DLG_HOTKEY_CHAR)
2148 {
2149 int len = STRLEN(next);
2150
2151 if (len > 0)
2152 {
2153 mch_memmove(next, next + 1, len);
2154 mnemonic = next[0];
2155 }
2156 }
2157 }
2158
2159 str = XmStringCreate((char *)p, STRING_TAG);
2160 vim_free(p);
2161
2162 if (str != NULL)
2163 {
2164 XtVaSetValues(w,
2165 XmNlabelString, str,
2166 XmNmnemonic, mnemonic,
2167 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 XmStringFree(str);
2169 }
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002170 gui_motif_menu_fontlist(w);
2171}
2172
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002173 static void
2174set_predefined_fontlist(Widget parent, String name)
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002175{
2176 Widget w;
2177 w = XtNameToWidget(parent, name);
2178
2179 if (!w)
2180 return;
2181
2182 set_fontlist(w);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183}
2184
2185/*
2186 * Put up a file requester.
2187 * Returns the selected name in allocated memory, or NULL for Cancel.
2188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002190gui_mch_browse(
Bram Moolenaar734a8672019-12-02 22:49:38 +01002191 int saving UNUSED, // select file to write
2192 char_u *title, // title for the window
2193 char_u *dflt, // default name
2194 char_u *ext UNUSED, // not used (extension added)
2195 char_u *initdir, // initial directory, NULL for current dir
2196 char_u *filter) // file name filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 char_u dirbuf[MAXPATHL];
2199 char_u dfltbuf[MAXPATHL];
2200 char_u *pattern;
2201 char_u *tofree = NULL;
2202
Bram Moolenaar734a8672019-12-02 22:49:38 +01002203 // There a difference between the resource name and value, Therefore, we
2204 // avoid to (ab-)use the (maybe internationalized!) dialog title as a
2205 // dialog name.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002206
2207 dialog_wgt = XmCreateFileSelectionDialog(vimShell, "browseDialog", NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 if (initdir == NULL || *initdir == NUL)
2210 {
2211 mch_dirname(dirbuf, MAXPATHL);
2212 initdir = dirbuf;
2213 }
2214
2215 if (dflt == NULL)
2216 dflt = (char_u *)"";
2217 else if (STRLEN(initdir) + STRLEN(dflt) + 2 < MAXPATHL)
2218 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002219 // The default selection should be the full path, "dflt" is only the
2220 // file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 STRCPY(dfltbuf, initdir);
2222 add_pathsep(dfltbuf);
2223 STRCAT(dfltbuf, dflt);
2224 dflt = dfltbuf;
2225 }
2226
Bram Moolenaar734a8672019-12-02 22:49:38 +01002227 // Can only use one pattern for a file name. Get the first pattern out of
2228 // the filter. An empty pattern means everything matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 if (filter == NULL)
2230 pattern = (char_u *)"";
2231 else
2232 {
2233 char_u *s, *p;
2234
2235 s = filter;
2236 for (p = filter; *p != NUL; ++p)
2237 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002238 if (*p == '\t') // end of description, start of pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 s = p + 1;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002240 if (*p == ';' || *p == '\n') // end of (first) pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 break;
2242 }
2243 pattern = vim_strnsave(s, p - s);
2244 tofree = pattern;
2245 if (pattern == NULL)
2246 pattern = (char_u *)"";
2247 }
2248
2249 XtVaSetValues(dialog_wgt,
2250 XtVaTypedArg,
2251 XmNdirectory, XmRString, (char *)initdir, STRLEN(initdir) + 1,
2252 XtVaTypedArg,
2253 XmNdirSpec, XmRString, (char *)dflt, STRLEN(dflt) + 1,
2254 XtVaTypedArg,
2255 XmNpattern, XmRString, (char *)pattern, STRLEN(pattern) + 1,
2256 XtVaTypedArg,
2257 XmNdialogTitle, XmRString, (char *)title, STRLEN(title) + 1,
2258 NULL);
2259
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002260 set_predefined_label(dialog_wgt, "Apply", _("&Filter"));
2261 set_predefined_label(dialog_wgt, "Cancel", _("&Cancel"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 set_predefined_label(dialog_wgt, "Dir", _("Directories"));
2263 set_predefined_label(dialog_wgt, "FilterLabel", _("Filter"));
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002264 set_predefined_label(dialog_wgt, "Help", _("&Help"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 set_predefined_label(dialog_wgt, "Items", _("Files"));
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002266 set_predefined_label(dialog_wgt, "OK", _("&OK"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 set_predefined_label(dialog_wgt, "Selection", _("Selection"));
2268
Bram Moolenaar734a8672019-12-02 22:49:38 +01002269 // This is to save us from silly external settings using not fixed with
2270 // fonts for file selection.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002271 set_predefined_fontlist(dialog_wgt, "DirListSW.DirList");
2272 set_predefined_fontlist(dialog_wgt, "ItemsListSW.ItemsList");
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 gui_motif_menu_colors(dialog_wgt);
2275 if (gui.scroll_bg_pixel != INVALCOLOR)
2276 XtVaSetValues(dialog_wgt, XmNtroughColor, gui.scroll_bg_pixel, NULL);
2277
2278 XtAddCallback(dialog_wgt, XmNokCallback, DialogAcceptCB, (XtPointer)0);
2279 XtAddCallback(dialog_wgt, XmNcancelCallback, DialogCancelCB, (XtPointer)0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002280 // We have no help in this window, so hide help button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 XtUnmanageChild(XmFileSelectionBoxGetChild(dialog_wgt,
2282 (unsigned char)XmDIALOG_HELP_BUTTON));
2283
2284 manage_centered(dialog_wgt);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002285 activate_dialog_mnemonics(dialog_wgt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
Bram Moolenaar734a8672019-12-02 22:49:38 +01002287 // sit in a loop until the dialog box has gone away
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 do
2289 {
2290 XtAppProcessEvent(XtWidgetToApplicationContext(dialog_wgt),
2291 (XtInputMask)XtIMAll);
2292 } while (XtIsManaged(dialog_wgt));
2293
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002294 suppress_dialog_mnemonics(dialog_wgt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 XtDestroyWidget(dialog_wgt);
2296 vim_free(tofree);
2297
2298 if (browse_fname == NULL)
2299 return NULL;
2300 return vim_strsave((char_u *)browse_fname);
2301}
2302
2303/*
2304 * The code below was originally taken from
2305 * /usr/examples/motif/xmsamplers/xmeditor.c
2306 * on Digital Unix 4.0d, but heavily modified.
2307 */
2308
2309/*
2310 * Process callback from Dialog cancel actions.
2311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002313DialogCancelCB(
Bram Moolenaar734a8672019-12-02 22:49:38 +01002314 Widget w UNUSED, // widget id
2315 XtPointer client_data UNUSED, // data from application
2316 XtPointer call_data UNUSED) // data from widget class
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 if (browse_fname != NULL)
2319 {
2320 XtFree(browse_fname);
2321 browse_fname = NULL;
2322 }
2323 XtUnmanageChild(dialog_wgt);
2324}
2325
2326/*
2327 * Process callback from Dialog actions.
2328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002330DialogAcceptCB(
Bram Moolenaar734a8672019-12-02 22:49:38 +01002331 Widget w UNUSED, // widget id
2332 XtPointer client_data UNUSED, // data from application
2333 XtPointer call_data) // data from widget class
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 XmFileSelectionBoxCallbackStruct *fcb;
2336
2337 if (browse_fname != NULL)
2338 {
2339 XtFree(browse_fname);
2340 browse_fname = NULL;
2341 }
2342 fcb = (XmFileSelectionBoxCallbackStruct *)call_data;
2343
Bram Moolenaar734a8672019-12-02 22:49:38 +01002344 // get the filename from the file selection box
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 XmStringGetLtoR(fcb->value, charset, &browse_fname);
2346
Bram Moolenaar734a8672019-12-02 22:49:38 +01002347 // popdown the file selection box
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 XtUnmanageChild(dialog_wgt);
2349}
2350
Bram Moolenaar734a8672019-12-02 22:49:38 +01002351#endif // FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2354
2355static int dialogStatus;
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357/*
2358 * Callback function for the textfield. When CR is hit this works like
2359 * hitting the "OK" button, ESC like "Cancel".
2360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002362keyhit_callback(
2363 Widget w,
2364 XtPointer client_data UNUSED,
2365 XEvent *event,
2366 Boolean *cont UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 char buf[2];
2369 KeySym key_sym;
2370
2371 if (XLookupString(&(event->xkey), buf, 2, &key_sym, NULL) == 1)
2372 {
2373 if (*buf == CAR)
2374 dialogStatus = 1;
2375 else if (*buf == ESC)
2376 dialogStatus = 2;
2377 }
2378 if ((key_sym == XK_Left || key_sym == XK_Right)
2379 && !(event->xkey.state & ShiftMask))
2380 XmTextFieldClearSelection(w, XtLastTimestampProcessed(gui.dpy));
2381}
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002384butproc(
2385 Widget w UNUSED,
2386 XtPointer client_data,
2387 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
2389 dialogStatus = (int)(long)client_data + 1;
2390}
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef HAVE_XPM
2393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 static Widget
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002395create_pixmap_label(
2396 Widget parent,
2397 String name,
2398 char **data,
2399 ArgList args,
2400 Cardinal arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
2402 Widget label;
2403 Display *dsp;
2404 Screen *scr;
2405 int depth;
2406 Pixmap pixmap = 0;
2407 XpmAttributes attr;
2408 Boolean rs;
2409 XpmColorSymbol color[5] =
2410 {
2411 {"none", NULL, 0},
2412 {"iconColor1", NULL, 0},
2413 {"bottomShadowColor", NULL, 0},
2414 {"topShadowColor", NULL, 0},
2415 {"selectColor", NULL, 0}
2416 };
2417
2418 label = XmCreateLabelGadget(parent, name, args, arg);
2419
2420 /*
Bram Moolenaar933eb392007-05-10 17:52:45 +00002421 * We need to be careful here, since in case of gadgets, there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 * no way to get the background color directly from the widget itself.
2423 * In such cases we get it from The Core part of his parent instead.
2424 */
2425 dsp = XtDisplayOfObject(label);
2426 scr = XtScreenOfObject(label);
2427 XtVaGetValues(XtIsSubclass(label, coreWidgetClass)
2428 ? label : XtParent(label),
2429 XmNdepth, &depth,
2430 XmNbackground, &color[0].pixel,
2431 XmNforeground, &color[1].pixel,
2432 XmNbottomShadowColor, &color[2].pixel,
2433 XmNtopShadowColor, &color[3].pixel,
2434 XmNhighlight, &color[4].pixel,
2435 NULL);
2436
2437 attr.valuemask = XpmColorSymbols | XpmCloseness | XpmDepth;
2438 attr.colorsymbols = color;
2439 attr.numsymbols = 5;
2440 attr.closeness = 65535;
2441 attr.depth = depth;
2442 XpmCreatePixmapFromData(dsp, RootWindowOfScreen(scr),
2443 data, &pixmap, NULL, &attr);
2444
2445 XtVaGetValues(label, XmNrecomputeSize, &rs, NULL);
2446 XtVaSetValues(label, XmNrecomputeSize, True, NULL);
2447 XtVaSetValues(label,
2448 XmNlabelType, XmPIXMAP,
2449 XmNlabelPixmap, pixmap,
2450 NULL);
2451 XtVaSetValues(label, XmNrecomputeSize, rs, NULL);
2452
2453 return label;
2454}
2455#endif
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002458gui_mch_dialog(
2459 int type UNUSED,
2460 char_u *title,
2461 char_u *message,
2462 char_u *button_names,
2463 int dfltbutton,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002464 char_u *textfield, // buffer of size IOSIZE
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002465 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 char_u *buts;
2468 char_u *p, *next;
2469 XtAppContext app;
2470 XmString label;
2471 int butcount;
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002472 Widget w;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 Widget dialogform = NULL;
2474 Widget form = NULL;
2475 Widget dialogtextfield = NULL;
2476 Widget *buttons;
2477 Widget sep_form = NULL;
2478 Boolean vertical;
2479 Widget separator = NULL;
2480 int n;
2481 Arg args[6];
2482#ifdef HAVE_XPM
2483 char **icon_data = NULL;
2484 Widget dialogpixmap = NULL;
2485#endif
2486
2487 if (title == NULL)
2488 title = (char_u *)_("Vim dialog");
2489
Bram Moolenaar734a8672019-12-02 22:49:38 +01002490 // if our pointer is currently hidden, then we should show it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 gui_mch_mousehide(FALSE);
2492
2493 dialogform = XmCreateFormDialog(vimShell, (char *)"dialog", NULL, 0);
2494
Bram Moolenaar734a8672019-12-02 22:49:38 +01002495 // Check 'v' flag in 'guioptions': vertical button placement.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL);
2497
Bram Moolenaar734a8672019-12-02 22:49:38 +01002498 // Set the title of the Dialog window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 label = XmStringCreateSimple((char *)title);
2500 if (label == NULL)
2501 return -1;
2502 XtVaSetValues(dialogform,
2503 XmNdialogTitle, label,
2504 XmNhorizontalSpacing, 4,
2505 XmNverticalSpacing, vertical ? 0 : 4,
2506 NULL);
2507 XmStringFree(label);
2508
Bram Moolenaar734a8672019-12-02 22:49:38 +01002509 // make a copy, so that we can insert NULs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 buts = vim_strsave(button_names);
2511 if (buts == NULL)
2512 return -1;
2513
Bram Moolenaar734a8672019-12-02 22:49:38 +01002514 // Count the number of buttons and allocate buttons[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 butcount = 1;
2516 for (p = buts; *p; ++p)
2517 if (*p == DLG_BUTTON_SEP)
2518 ++butcount;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002519 buttons = ALLOC_MULT(Widget, butcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (buttons == NULL)
2521 {
2522 vim_free(buts);
2523 return -1;
2524 }
2525
2526 /*
2527 * Create the buttons.
2528 */
2529 sep_form = (Widget) 0;
2530 p = buts;
2531 for (butcount = 0; *p; ++butcount)
2532 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002533 KeySym mnemonic = NUL;
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 for (next = p; *next; ++next)
2536 {
2537 if (*next == DLG_HOTKEY_CHAR)
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002538 {
2539 int len = STRLEN(next);
2540
2541 if (len > 0)
2542 {
2543 mch_memmove(next, next + 1, len);
2544 mnemonic = next[0];
2545 }
2546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (*next == DLG_BUTTON_SEP)
2548 {
2549 *next++ = NUL;
2550 break;
2551 }
2552 }
2553 label = XmStringCreate(_((char *)p), STRING_TAG);
2554 if (label == NULL)
2555 break;
2556
2557 buttons[butcount] = XtVaCreateManagedWidget("button",
2558 xmPushButtonWidgetClass, dialogform,
2559 XmNlabelString, label,
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002560 XmNmnemonic, mnemonic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 XmNbottomAttachment, XmATTACH_FORM,
2562 XmNbottomOffset, 4,
2563 XmNshowAsDefault, butcount == dfltbutton - 1,
2564 XmNdefaultButtonShadowThickness, 1,
2565 NULL);
2566 XmStringFree(label);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002567 gui_motif_menu_fontlist(buttons[butcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar734a8672019-12-02 22:49:38 +01002569 // Layout properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571 if (butcount > 0)
2572 {
2573 if (vertical)
2574 XtVaSetValues(buttons[butcount],
2575 XmNtopWidget, buttons[butcount - 1],
2576 NULL);
2577 else
2578 {
2579 if (*next == NUL)
2580 {
2581 XtVaSetValues(buttons[butcount],
2582 XmNrightAttachment, XmATTACH_FORM,
2583 XmNrightOffset, 4,
2584 NULL);
2585
Bram Moolenaar734a8672019-12-02 22:49:38 +01002586 // fill in a form as invisible separator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 sep_form = XtVaCreateWidget("separatorForm",
2588 xmFormWidgetClass, dialogform,
2589 XmNleftAttachment, XmATTACH_WIDGET,
2590 XmNleftWidget, buttons[butcount - 1],
2591 XmNrightAttachment, XmATTACH_WIDGET,
2592 XmNrightWidget, buttons[butcount],
2593 XmNbottomAttachment, XmATTACH_FORM,
2594 XmNbottomOffset, 4,
2595 NULL);
2596 XtManageChild(sep_form);
2597 }
2598 else
2599 {
2600 XtVaSetValues(buttons[butcount],
2601 XmNleftAttachment, XmATTACH_WIDGET,
2602 XmNleftWidget, buttons[butcount - 1],
2603 NULL);
2604 }
2605 }
2606 }
2607 else if (!vertical)
2608 {
2609 if (*next == NUL)
2610 {
2611 XtVaSetValues(buttons[0],
2612 XmNrightAttachment, XmATTACH_FORM,
2613 XmNrightOffset, 4,
2614 NULL);
2615
Bram Moolenaar734a8672019-12-02 22:49:38 +01002616 // fill in a form as invisible separator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 sep_form = XtVaCreateWidget("separatorForm",
2618 xmFormWidgetClass, dialogform,
2619 XmNleftAttachment, XmATTACH_FORM,
2620 XmNleftOffset, 4,
2621 XmNrightAttachment, XmATTACH_WIDGET,
2622 XmNrightWidget, buttons[0],
2623 XmNbottomAttachment, XmATTACH_FORM,
2624 XmNbottomOffset, 4,
2625 NULL);
2626 XtManageChild(sep_form);
2627 }
2628 else
2629 XtVaSetValues(buttons[0],
2630 XmNleftAttachment, XmATTACH_FORM,
2631 XmNleftOffset, 4,
2632 NULL);
2633 }
2634
2635 XtAddCallback(buttons[butcount], XmNactivateCallback,
2636 (XtCallbackProc)butproc, (XtPointer)(long)butcount);
2637 p = next;
2638 }
2639 vim_free(buts);
2640
2641 separator = (Widget) 0;
2642 if (butcount > 0)
2643 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002644 // Create the separator for beauty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 n = 0;
2646 XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
2647 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
2648 XtSetArg(args[n], XmNbottomWidget, buttons[0]); n++;
2649 XtSetArg(args[n], XmNbottomOffset, 4); n++;
2650 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
2651 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
2652 separator = XmCreateSeparatorGadget(dialogform, "separator", args, n);
2653 XtManageChild(separator);
2654 }
2655
2656 if (textfield != NULL)
2657 {
2658 dialogtextfield = XtVaCreateWidget("textField",
2659 xmTextFieldWidgetClass, dialogform,
2660 XmNleftAttachment, XmATTACH_FORM,
2661 XmNrightAttachment, XmATTACH_FORM,
2662 NULL);
2663 if (butcount > 0)
2664 XtVaSetValues(dialogtextfield,
2665 XmNbottomAttachment, XmATTACH_WIDGET,
2666 XmNbottomWidget, separator,
2667 NULL);
2668 else
2669 XtVaSetValues(dialogtextfield,
2670 XmNbottomAttachment, XmATTACH_FORM,
2671 NULL);
2672
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002673 set_fontlist(dialogtextfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 XmTextFieldSetString(dialogtextfield, (char *)textfield);
2675 XtManageChild(dialogtextfield);
2676 XtAddEventHandler(dialogtextfield, KeyPressMask, False,
2677 (XtEventHandler)keyhit_callback, (XtPointer)NULL);
2678 }
2679
Bram Moolenaar734a8672019-12-02 22:49:38 +01002680 // Form holding both message and pixmap labels
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 form = XtVaCreateWidget("separatorForm",
2682 xmFormWidgetClass, dialogform,
2683 XmNleftAttachment, XmATTACH_FORM,
2684 XmNrightAttachment, XmATTACH_FORM,
2685 XmNtopAttachment, XmATTACH_FORM,
2686 NULL);
2687 XtManageChild(form);
2688
2689#ifdef HAVE_XPM
Bram Moolenaar734a8672019-12-02 22:49:38 +01002690 // Add a pixmap, left of the message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 switch (type)
2692 {
2693 case VIM_GENERIC:
2694 icon_data = generic_xpm;
2695 break;
2696 case VIM_ERROR:
2697 icon_data = error_xpm;
2698 break;
2699 case VIM_WARNING:
2700 icon_data = alert_xpm;
2701 break;
2702 case VIM_INFO:
2703 icon_data = info_xpm;
2704 break;
2705 case VIM_QUESTION:
2706 icon_data = quest_xpm;
2707 break;
2708 default:
2709 icon_data = generic_xpm;
2710 }
2711
2712 n = 0;
2713 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
2714 XtSetArg(args[n], XmNtopOffset, 8); n++;
2715 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
2716 XtSetArg(args[n], XmNbottomOffset, 8); n++;
2717 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
2718 XtSetArg(args[n], XmNleftOffset, 8); n++;
2719
2720 dialogpixmap = create_pixmap_label(form, "dialogPixmap",
2721 icon_data, args, n);
2722 XtManageChild(dialogpixmap);
2723#endif
2724
Bram Moolenaar734a8672019-12-02 22:49:38 +01002725 // Create the dialog message.
2726 // Since LessTif is apparently having problems with the creation of
2727 // properly localized string, we use LtoR here. The symptom is that the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002728 // string is not shown properly in multiple lines as it does in native
Bram Moolenaar734a8672019-12-02 22:49:38 +01002729 // Motif.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002730 label = XmStringCreateLtoR((char *)message, STRING_TAG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (label == NULL)
2732 return -1;
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002733 w = XtVaCreateManagedWidget("dialogMessage",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 xmLabelGadgetClass, form,
2735 XmNlabelString, label,
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002736 XmNalignment, XmALIGNMENT_BEGINNING,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 XmNtopAttachment, XmATTACH_FORM,
2738 XmNtopOffset, 8,
2739#ifdef HAVE_XPM
2740 XmNleftAttachment, XmATTACH_WIDGET,
2741 XmNleftWidget, dialogpixmap,
2742#else
2743 XmNleftAttachment, XmATTACH_FORM,
2744#endif
2745 XmNleftOffset, 8,
2746 XmNrightAttachment, XmATTACH_FORM,
2747 XmNrightOffset, 8,
2748 XmNbottomAttachment, XmATTACH_FORM,
2749 XmNbottomOffset, 8,
2750 NULL);
2751 XmStringFree(label);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002752 set_fontlist(w);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
2754 if (textfield != NULL)
2755 {
2756 XtVaSetValues(form,
2757 XmNbottomAttachment, XmATTACH_WIDGET,
2758 XmNbottomWidget, dialogtextfield,
2759 NULL);
2760 }
2761 else
2762 {
2763 if (butcount > 0)
2764 XtVaSetValues(form,
2765 XmNbottomAttachment, XmATTACH_WIDGET,
2766 XmNbottomWidget, separator,
2767 NULL);
2768 else
2769 XtVaSetValues(form,
2770 XmNbottomAttachment, XmATTACH_FORM,
2771 NULL);
2772 }
2773
2774 if (dfltbutton < 1)
2775 dfltbutton = 1;
2776 if (dfltbutton > butcount)
2777 dfltbutton = butcount;
2778 XtVaSetValues(dialogform,
2779 XmNdefaultButton, buttons[dfltbutton - 1], NULL);
2780 if (textfield != NULL)
2781 XtVaSetValues(dialogform, XmNinitialFocus, dialogtextfield, NULL);
2782 else
2783 XtVaSetValues(dialogform, XmNinitialFocus, buttons[dfltbutton - 1],
2784 NULL);
2785
2786 manage_centered(dialogform);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002787 activate_dialog_mnemonics(dialogform);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 if (textfield != NULL && *textfield != NUL)
2790 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002791 // This only works after the textfield has been realised.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 XmTextFieldSetSelection(dialogtextfield,
2793 (XmTextPosition)0, (XmTextPosition)STRLEN(textfield),
2794 XtLastTimestampProcessed(gui.dpy));
2795 XmTextFieldSetCursorPosition(dialogtextfield,
2796 (XmTextPosition)STRLEN(textfield));
2797 }
2798
2799 app = XtWidgetToApplicationContext(dialogform);
2800
Bram Moolenaar734a8672019-12-02 22:49:38 +01002801 // Loop until a button is pressed or the dialog is killed somehow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 dialogStatus = -1;
2803 for (;;)
2804 {
2805 XtAppProcessEvent(app, (XtInputMask)XtIMAll);
2806 if (dialogStatus >= 0 || !XtIsManaged(dialogform))
2807 break;
2808 }
2809
2810 vim_free(buttons);
2811
2812 if (textfield != NULL)
2813 {
2814 p = (char_u *)XmTextGetString(dialogtextfield);
2815 if (p == NULL || dialogStatus < 0)
2816 *textfield = NUL;
2817 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002818 vim_strncpy(textfield, p, IOSIZE - 1);
Bram Moolenaar103e6ef2010-05-13 16:31:25 +02002819 XtFree((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
2821
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002822 suppress_dialog_mnemonics(dialogform);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 XtDestroyWidget(dialogform);
2824
2825 return dialogStatus;
2826}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002827#endif // FEAT_GUI_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#if defined(FEAT_TOOLBAR) || defined(PROTO)
2830 void
2831gui_mch_show_toolbar(int showit)
2832{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002833 Cardinal numChildren; // how many children toolBar has
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 if (toolBar == (Widget)0)
2836 return;
2837 XtVaGetValues(toolBar, XmNnumChildren, &numChildren, NULL);
2838 if (showit && numChildren > 0)
2839 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002840 // Assume that we want to show the toolbar if p_toolbar contains
2841 // valid option settings, therefore p_toolbar must not be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 WidgetList children;
2843
2844 XtVaGetValues(toolBar, XmNchildren, &children, NULL);
2845 {
2846 void (*action)(BalloonEval *);
2847 int text = 0;
2848
2849 if (strstr((const char *)p_toolbar, "tooltips"))
2850 action = &gui_mch_enable_beval_area;
2851 else
2852 action = &gui_mch_disable_beval_area;
2853 if (strstr((const char *)p_toolbar, "text"))
2854 text = 1;
2855 else if (strstr((const char *)p_toolbar, "icons"))
2856 text = -1;
2857 if (text != 0)
2858 {
2859 vimmenu_T *toolbar;
2860 vimmenu_T *cur;
2861
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002862 FOR_ALL_MENUS(toolbar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (menu_is_toolbar(toolbar->dname))
2864 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002865 // Assumption: toolbar is NULL if there is no toolbar,
2866 // otherwise it contains the toolbar menu structure.
2867 //
2868 // Assumption: "numChildren" == the number of items in the list
2869 // of items beginning with toolbar->children.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (toolbar)
2871 {
2872 for (cur = toolbar->children; cur; cur = cur->next)
2873 {
2874 Arg args[1];
2875 int n = 0;
2876
Bram Moolenaar734a8672019-12-02 22:49:38 +01002877 // Enable/Disable tooltip (OK to enable while
2878 // currently enabled).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (cur->tip != NULL)
2880 (*action)(cur->tip);
2881 if (!menu_is_separator(cur->name))
2882 {
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00002883 if (text == 1 || cur->xpm == NULL)
2884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 XtSetArg(args[n], XmNlabelType, XmSTRING);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00002886 ++n;
2887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (cur->id != NULL)
2889 {
2890 XtUnmanageChild(cur->id);
2891 XtSetValues(cur->id, args, n);
2892 XtManageChild(cur->id);
2893 }
2894 }
2895 }
2896 }
2897 }
2898 }
2899 gui.toolbar_height = gui_mch_compute_toolbar_height();
2900 XtManageChild(XtParent(toolBar));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002901#ifdef FEAT_GUI_TABLINE
2902 if (showing_tabline)
2903 {
2904 XtVaSetValues(tabLine,
2905 XmNtopAttachment, XmATTACH_WIDGET,
2906 XmNtopWidget, XtParent(toolBar),
2907 NULL);
2908 XtVaSetValues(textAreaForm,
2909 XmNtopAttachment, XmATTACH_WIDGET,
2910 XmNtopWidget, tabLine,
2911 NULL);
2912 }
2913 else
2914#endif
2915 XtVaSetValues(textAreaForm,
2916 XmNtopAttachment, XmATTACH_WIDGET,
2917 XmNtopWidget, XtParent(toolBar),
2918 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (XtIsManaged(menuBar))
2920 XtVaSetValues(XtParent(toolBar),
2921 XmNtopAttachment, XmATTACH_WIDGET,
2922 XmNtopWidget, menuBar,
2923 NULL);
2924 else
2925 XtVaSetValues(XtParent(toolBar),
2926 XmNtopAttachment, XmATTACH_FORM,
2927 NULL);
2928 }
2929 else
2930 {
2931 gui.toolbar_height = 0;
2932 if (XtIsManaged(menuBar))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002933 {
2934#ifdef FEAT_GUI_TABLINE
2935 if (showing_tabline)
2936 {
2937 XtVaSetValues(tabLine,
2938 XmNtopAttachment, XmATTACH_WIDGET,
2939 XmNtopWidget, menuBar,
2940 NULL);
2941 XtVaSetValues(textAreaForm,
2942 XmNtopAttachment, XmATTACH_WIDGET,
2943 XmNtopWidget, tabLine,
2944 NULL);
2945 }
2946 else
2947#endif
2948 XtVaSetValues(textAreaForm,
2949 XmNtopAttachment, XmATTACH_WIDGET,
2950 XmNtopWidget, menuBar,
2951 NULL);
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002954 {
2955#ifdef FEAT_GUI_TABLINE
2956 if (showing_tabline)
2957 {
2958 XtVaSetValues(tabLine,
2959 XmNtopAttachment, XmATTACH_FORM,
2960 NULL);
2961 XtVaSetValues(textAreaForm,
2962 XmNtopAttachment, XmATTACH_WIDGET,
2963 XmNtopWidget, tabLine,
2964 NULL);
2965 }
2966 else
2967#endif
2968 XtVaSetValues(textAreaForm,
2969 XmNtopAttachment, XmATTACH_FORM,
2970 NULL);
2971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
2973 XtUnmanageChild(XtParent(toolBar));
2974 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002975 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978/*
2979 * A toolbar button has been pushed; now reset the input focus
2980 * such that the user can type page up/down etc. and have the
2981 * input go to the editor window, not the button
2982 */
2983 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002984reset_focus(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 if (textArea != NULL)
2987 XmProcessTraversal(textArea, XmTRAVERSE_CURRENT);
2988}
2989
2990 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002991gui_mch_compute_toolbar_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00002993 Dimension borders;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002994 Dimension height; // total Toolbar height
2995 Dimension whgt; // height of each widget
2996 WidgetList children; // list of toolBar's children
2997 Cardinal numChildren; // how many children toolBar has
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 int i;
2999
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003000 borders = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (toolBar != (Widget)0 && toolBarFrame != (Widget)0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003003 { // get height of XmFrame parent
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003004 Dimension fst;
3005 Dimension fmh;
3006 Dimension tst;
3007 Dimension tmh;
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 XtVaGetValues(toolBarFrame,
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003010 XmNshadowThickness, &fst,
3011 XmNmarginHeight, &fmh,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 NULL);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003013 borders += fst + fmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 XtVaGetValues(toolBar,
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003015 XmNshadowThickness, &tst,
3016 XmNmarginHeight, &tmh,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 XmNchildren, &children,
3018 XmNnumChildren, &numChildren, NULL);
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003019 borders += tst + tmh;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003020 for (i = 0; i < (int)numChildren; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 {
3022 whgt = 0;
3023 XtVaGetValues(children[i], XmNheight, &whgt, NULL);
3024 if (height < whgt)
3025 height = whgt;
3026 }
3027 }
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003028#ifdef LESSTIF_VERSION
Bram Moolenaar734a8672019-12-02 22:49:38 +01003029 // Hack: When starting up we get wrong dimensions.
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003030 if (height < 10)
3031 height = 24;
3032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003034 return (int)(height + (borders << 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035}
3036
Bram Moolenaar7c626922005-02-07 22:01:03 +00003037 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003038motif_get_toolbar_colors(
3039 Pixel *bgp,
3040 Pixel *fgp,
3041 Pixel *bsp,
3042 Pixel *tsp,
3043 Pixel *hsp)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003044{
3045 XtVaGetValues(toolBar,
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003046 XmNbackground, bgp,
3047 XmNforeground, fgp,
3048 XmNbottomShadowColor, bsp,
3049 XmNtopShadowColor, tsp,
3050 XmNhighlightColor, hsp,
3051 NULL);
Bram Moolenaar7c626922005-02-07 22:01:03 +00003052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053#endif
3054
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003055#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3056/*
3057 * Show or hide the tabline.
3058 */
3059 void
3060gui_mch_show_tabline(int showit)
3061{
3062 if (tabLine == (Widget)0)
3063 return;
3064
3065 if (!showit != !showing_tabline)
3066 {
3067 if (showit)
3068 {
3069 XtManageChild(tabLine);
3070 XtUnmanageChild(XtNameToWidget(tabLine, "PageScroller"));
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003071 XtUnmanageChild(XtNameToWidget(tabLine, "MinorTabScrollerNext"));
3072 XtUnmanageChild(XtNameToWidget(tabLine,
3073 "MinorTabScrollerPrevious"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003074#ifdef FEAT_MENU
3075# ifdef FEAT_TOOLBAR
3076 if (XtIsManaged(XtParent(toolBar)))
3077 XtVaSetValues(tabLine,
3078 XmNtopAttachment, XmATTACH_WIDGET,
3079 XmNtopWidget, XtParent(toolBar), NULL);
3080 else
3081# endif
3082 if (XtIsManaged(menuBar))
3083 XtVaSetValues(tabLine,
3084 XmNtopAttachment, XmATTACH_WIDGET,
3085 XmNtopWidget, menuBar, NULL);
3086 else
3087#endif
3088 XtVaSetValues(tabLine,
3089 XmNtopAttachment, XmATTACH_FORM, NULL);
3090 XtVaSetValues(textAreaForm,
3091 XmNtopAttachment, XmATTACH_WIDGET,
3092 XmNtopWidget, tabLine,
3093 NULL);
3094 }
3095 else
3096 {
3097 XtUnmanageChild(tabLine);
3098#ifdef FEAT_MENU
3099# ifdef FEAT_TOOLBAR
3100 if (XtIsManaged(XtParent(toolBar)))
3101 XtVaSetValues(textAreaForm,
3102 XmNtopAttachment, XmATTACH_WIDGET,
3103 XmNtopWidget, XtParent(toolBar), NULL);
3104 else
3105# endif
3106 if (XtIsManaged(menuBar))
3107 XtVaSetValues(textAreaForm,
3108 XmNtopAttachment, XmATTACH_WIDGET,
3109 XmNtopWidget, menuBar, NULL);
3110 else
3111#endif
3112 XtVaSetValues(textAreaForm,
3113 XmNtopAttachment, XmATTACH_FORM, NULL);
3114 }
3115 showing_tabline = showit;
3116 }
3117}
3118
3119/*
3120 * Return TRUE when tabline is displayed.
3121 */
3122 int
3123gui_mch_showing_tabline(void)
3124{
3125 return tabLine != (Widget)0 && showing_tabline;
3126}
3127
3128/*
3129 * Update the labels of the tabline.
3130 */
3131 void
3132gui_mch_update_tabline(void)
3133{
3134 tabpage_T *tp;
3135 int nr = 1, n;
3136 Arg args[10];
3137 int curtabidx = 0, currentpage;
3138 Widget tab;
3139 XmNotebookPageInfo page_info;
3140 XmNotebookPageStatus page_status;
3141 int last_page, tab_count;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003142 XmString label_str;
3143 char *label_cstr;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003144 BalloonEval *beval;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003145
3146 if (tabLine == (Widget)0)
3147 return;
3148
Bram Moolenaar734a8672019-12-02 22:49:38 +01003149 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003150 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3151 {
3152 if (tp == curtab)
3153 curtabidx = nr;
3154
3155 page_status = XmNotebookGetPageInfo(tabLine, nr, &page_info);
3156 if (page_status == XmPAGE_INVALID
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003157 || page_info.major_tab_widget == (Widget)0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003158 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003159 // Add the tab
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003160 n = 0;
3161 XtSetArg(args[n], XmNnotebookChildType, XmMAJOR_TAB); n++;
3162 XtSetArg(args[n], XmNtraversalOn, False); n++;
3163 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
3164 XtSetArg(args[n], XmNhighlightThickness, 1); n++;
3165 XtSetArg(args[n], XmNshadowThickness , 1); n++;
3166 tab = XmCreatePushButton(tabLine, "-Empty-", args, n);
3167 XtManageChild(tab);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003168 beval = gui_mch_create_beval_area(tab, NULL, tabline_balloon_cb,
3169 NULL);
3170 XtVaSetValues(tab, XmNuserData, beval, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003171 }
3172 else
3173 tab = page_info.major_tab_widget;
3174
3175 XtVaSetValues(tab, XmNpageNumber, nr, NULL);
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003176
3177 /*
3178 * Change the label text only if it is different
3179 */
3180 XtVaGetValues(tab, XmNlabelString, &label_str, NULL);
3181 if (XmStringGetLtoR(label_str, XmSTRING_DEFAULT_CHARSET, &label_cstr))
3182 {
Bram Moolenaar57657d82006-04-21 22:12:41 +00003183 get_tabline_label(tp, FALSE);
3184 if (STRCMP(label_cstr, NameBuff) != 0)
3185 {
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003186 XtVaSetValues(tab, XtVaTypedArg, XmNlabelString, XmRString,
3187 NameBuff, STRLEN(NameBuff) + 1, NULL);
3188 /*
3189 * Force a resize of the tab label button
3190 */
3191 XtUnmanageChild(tab);
3192 XtManageChild(tab);
3193 }
3194 XtFree(label_cstr);
3195 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003196 }
3197
3198 tab_count = nr - 1;
3199
3200 XtVaGetValues(tabLine, XmNlastPageNumber, &last_page, NULL);
3201
Bram Moolenaar734a8672019-12-02 22:49:38 +01003202 // Remove any old labels.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003203 while (nr <= last_page)
3204 {
3205 if (XmNotebookGetPageInfo(tabLine, nr, &page_info) != XmPAGE_INVALID
3206 && page_info.page_number == nr
3207 && page_info.major_tab_widget != (Widget)0)
3208 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003209 XtVaGetValues(page_info.major_tab_widget, XmNuserData, &beval, NULL);
3210 if (beval != NULL)
3211 gui_mch_destroy_beval_area(beval);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003212 XtUnmanageChild(page_info.major_tab_widget);
3213 XtDestroyWidget(page_info.major_tab_widget);
3214 }
3215 nr++;
3216 }
3217
3218 XtVaSetValues(tabLine, XmNlastPageNumber, tab_count, NULL);
3219
3220 XtVaGetValues(tabLine, XmNcurrentPageNumber, &currentpage, NULL);
3221 if (currentpage != curtabidx)
3222 XtVaSetValues(tabLine, XmNcurrentPageNumber, curtabidx, NULL);
3223}
3224
3225/*
3226 * Set the current tab to "nr". First tab is 1.
3227 */
3228 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003229gui_mch_set_curtab(int nr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003230{
3231 int currentpage;
3232
3233 if (tabLine == (Widget)0)
3234 return;
3235
3236 XtVaGetValues(tabLine, XmNcurrentPageNumber, &currentpage, NULL);
3237 if (currentpage != nr)
3238 XtVaSetValues(tabLine, XmNcurrentPageNumber, nr, NULL);
3239}
3240#endif
3241
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242/*
3243 * Set the colors of Widget "id" to the menu colors.
3244 */
3245 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003246gui_motif_menu_colors(Widget id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 if (gui.menu_bg_pixel != INVALCOLOR)
3249#if (XmVersion >= 1002)
3250 XmChangeColor(id, gui.menu_bg_pixel);
3251#else
3252 XtVaSetValues(id, XmNbackground, gui.menu_bg_pixel, NULL);
3253#endif
3254 if (gui.menu_fg_pixel != INVALCOLOR)
3255 XtVaSetValues(id, XmNforeground, gui.menu_fg_pixel, NULL);
3256}
3257
3258/*
3259 * Set the colors of Widget "id" to the scrollbar colors.
3260 */
3261 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003262gui_motif_scroll_colors(Widget id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 if (gui.scroll_bg_pixel != INVALCOLOR)
3265#if (XmVersion >= 1002)
3266 XmChangeColor(id, gui.scroll_bg_pixel);
3267#else
3268 XtVaSetValues(id, XmNbackground, gui.scroll_bg_pixel, NULL);
3269#endif
3270 if (gui.scroll_fg_pixel != INVALCOLOR)
3271 XtVaSetValues(id, XmNforeground, gui.scroll_fg_pixel, NULL);
3272}
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274/*
3275 * Set the fontlist for Widget "id" to use gui.menu_fontset or gui.menu_font.
3276 */
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003277 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003278gui_motif_menu_fontlist(Widget id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003280#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#ifdef FONTSET_ALWAYS
3282 if (gui.menu_fontset != NOFONTSET)
3283 {
3284 XmFontList fl;
3285
3286 fl = gui_motif_fontset2fontlist((XFontSet *)&gui.menu_fontset);
3287 if (fl != NULL)
3288 {
3289 if (XtIsManaged(id))
3290 {
3291 XtUnmanageChild(id);
3292 XtVaSetValues(id, XmNfontList, fl, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003293 // We should force the widget to recalculate its
3294 // geometry now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 XtManageChild(id);
3296 }
3297 else
3298 XtVaSetValues(id, XmNfontList, fl, NULL);
3299 XmFontListFree(fl);
3300 }
3301 }
3302#else
3303 if (gui.menu_font != NOFONT)
3304 {
3305 XmFontList fl;
3306
3307 fl = gui_motif_create_fontlist((XFontStruct *)gui.menu_font);
3308 if (fl != NULL)
3309 {
3310 if (XtIsManaged(id))
3311 {
3312 XtUnmanageChild(id);
3313 XtVaSetValues(id, XmNfontList, fl, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003314 // We should force the widget to recalculate its
3315 // geometry now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 XtManageChild(id);
3317 }
3318 else
3319 XtVaSetValues(id, XmNfontList, fl, NULL);
3320 XmFontListFree(fl);
3321 }
3322 }
3323#endif
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
3328/*
3329 * We don't create it twice for the sake of speed.
3330 */
3331
3332typedef struct _SharedFindReplace
3333{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003334 Widget dialog; // the main dialog widget
3335 Widget wword; // 'Exact match' check button
3336 Widget mcase; // 'match case' check button
3337 Widget up; // search direction 'Up' radio button
3338 Widget down; // search direction 'Down' radio button
3339 Widget what; // 'Find what' entry text widget
3340 Widget with; // 'Replace with' entry text widget
3341 Widget find; // 'Find Next' action button
3342 Widget replace; // 'Replace With' action button
3343 Widget all; // 'Replace All' action button
3344 Widget undo; // 'Undo' action button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
3346 Widget cancel;
3347} SharedFindReplace;
3348
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003349static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3350static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003353find_replace_destroy_callback(
3354 Widget w UNUSED,
3355 XtPointer client_data,
3356 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 SharedFindReplace *cd = (SharedFindReplace *)client_data;
3359
Bram Moolenaarb7fcef52005-01-02 11:31:05 +00003360 if (cd != NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003361 // suppress_dialog_mnemonics(cd->dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 cd->dialog = (Widget)0;
3363}
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003366find_replace_dismiss_callback(
3367 Widget w UNUSED,
3368 XtPointer client_data,
3369 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 SharedFindReplace *cd = (SharedFindReplace *)client_data;
3372
3373 if (cd != NULL)
3374 XtUnmanageChild(cd->dialog);
3375}
3376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003378entry_activate_callback(
3379 Widget w UNUSED,
3380 XtPointer client_data,
3381 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 XmProcessTraversal((Widget)client_data, XmTRAVERSE_CURRENT);
3384}
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003387find_replace_callback(
3388 Widget w UNUSED,
3389 XtPointer client_data,
3390 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 long_u flags = (long_u)client_data;
3393 char *find_text, *repl_text;
3394 Boolean direction_down = TRUE;
3395 Boolean wword;
3396 Boolean mcase;
3397 SharedFindReplace *sfr;
3398
3399 if (flags == FRD_UNDO)
3400 {
3401 char_u *save_cpo = p_cpo;
3402
Bram Moolenaar734a8672019-12-02 22:49:38 +01003403 // No need to be Vi compatible here.
Bram Moolenaarbb0956f2021-01-03 22:12:15 +01003404 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 u_undo(1);
3406 p_cpo = save_cpo;
3407 gui_update_screen();
3408 return;
3409 }
3410
Bram Moolenaar734a8672019-12-02 22:49:38 +01003411 // Get the search/replace strings from the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (flags == FRD_FINDNEXT)
3413 {
3414 repl_text = NULL;
3415 sfr = &find_widgets;
3416 }
3417 else
3418 {
3419 repl_text = XmTextFieldGetString(repl_widgets.with);
3420 sfr = &repl_widgets;
3421 }
3422 find_text = XmTextFieldGetString(sfr->what);
3423 XtVaGetValues(sfr->down, XmNset, &direction_down, NULL);
3424 XtVaGetValues(sfr->wword, XmNset, &wword, NULL);
3425 XtVaGetValues(sfr->mcase, XmNset, &mcase, NULL);
3426 if (wword)
3427 flags |= FRD_WHOLE_WORD;
3428 if (mcase)
3429 flags |= FRD_MATCH_CASE;
3430
3431 (void)gui_do_findrepl((int)flags, (char_u *)find_text, (char_u *)repl_text,
3432 direction_down);
3433
3434 if (find_text != NULL)
3435 XtFree(find_text);
3436 if (repl_text != NULL)
3437 XtFree(repl_text);
3438}
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003441find_replace_keypress(
3442 Widget w UNUSED,
3443 SharedFindReplace *frdp,
Bram Moolenaar9dbe7012021-03-29 20:10:26 +02003444 XKeyEvent *event,
3445 Boolean *b UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 KeySym keysym;
3448
3449 if (frdp == NULL)
3450 return;
3451
3452 keysym = XLookupKeysym(event, 0);
3453
Bram Moolenaar734a8672019-12-02 22:49:38 +01003454 // the scape key pops the whole dialog down
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (keysym == XK_Escape)
3456 XtUnmanageChild(frdp->dialog);
3457}
3458
3459 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003460set_label(Widget w, char *label)
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003461{
3462 XmString str;
3463 char_u *p, *next;
3464 KeySym mnemonic = NUL;
3465
3466 if (!w)
3467 return;
3468
Bram Moolenaar3dbcd0c2013-06-22 13:00:16 +02003469 p = vim_strsave((char_u *)label);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003470 if (p == NULL)
3471 return;
3472 for (next = p; *next; ++next)
3473 {
3474 if (*next == DLG_HOTKEY_CHAR)
3475 {
3476 int len = STRLEN(next);
3477
3478 if (len > 0)
3479 {
3480 mch_memmove(next, next + 1, len);
3481 mnemonic = next[0];
3482 }
3483 }
3484 }
3485
3486 str = XmStringCreateSimple((char *)p);
3487 vim_free(p);
3488 if (str)
3489 {
3490 XtVaSetValues(w,
3491 XmNlabelString, str,
3492 XmNmnemonic, mnemonic,
3493 NULL);
3494 XmStringFree(str);
3495 }
3496 gui_motif_menu_fontlist(w);
3497}
3498
3499 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003500find_replace_dialog_create(char_u *arg, int do_replace)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
3502 SharedFindReplace *frdp;
3503 Widget separator;
3504 Widget input_form;
3505 Widget button_form;
3506 Widget toggle_form;
3507 Widget frame;
3508 XmString str;
3509 int n;
3510 Arg args[6];
3511 int wword = FALSE;
3512 int mcase = !p_ic;
3513 Dimension width;
3514 Dimension widest;
3515 char_u *entry_text;
3516
3517 frdp = do_replace ? &repl_widgets : &find_widgets;
3518
Bram Moolenaar734a8672019-12-02 22:49:38 +01003519 // Get the search string to use.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 entry_text = get_find_dialog_text(arg, &wword, &mcase);
3521
Bram Moolenaar734a8672019-12-02 22:49:38 +01003522 // If the dialog already exists, just raise it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (frdp->dialog)
3524 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003525 gui_motif_synch_fonts();
3526
Bram Moolenaar734a8672019-12-02 22:49:38 +01003527 // If the window is already up, just pop it to the top
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (XtIsManaged(frdp->dialog))
3529 XMapRaised(XtDisplay(frdp->dialog),
3530 XtWindow(XtParent(frdp->dialog)));
3531 else
3532 XtManageChild(frdp->dialog);
3533 XtPopup(XtParent(frdp->dialog), XtGrabNone);
3534 XmProcessTraversal(frdp->what, XmTRAVERSE_CURRENT);
3535
3536 if (entry_text != NULL)
3537 XmTextFieldSetString(frdp->what, (char *)entry_text);
3538 vim_free(entry_text);
3539
3540 XtVaSetValues(frdp->wword, XmNset, wword, NULL);
3541 return;
3542 }
3543
Bram Moolenaar734a8672019-12-02 22:49:38 +01003544 // Create a fresh new dialog window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (do_replace)
3546 str = XmStringCreateSimple(_("VIM - Search and Replace..."));
3547 else
3548 str = XmStringCreateSimple(_("VIM - Search..."));
3549
3550 n = 0;
3551 XtSetArg(args[n], XmNautoUnmanage, False); n++;
3552 XtSetArg(args[n], XmNnoResize, True); n++;
3553 XtSetArg(args[n], XmNdialogTitle, str); n++;
3554
3555 frdp->dialog = XmCreateFormDialog(vimShell, "findReplaceDialog", args, n);
3556 XmStringFree(str);
3557 XtAddCallback(frdp->dialog, XmNdestroyCallback,
3558 find_replace_destroy_callback, frdp);
3559
3560 button_form = XtVaCreateWidget("buttonForm",
3561 xmFormWidgetClass, frdp->dialog,
3562 XmNrightAttachment, XmATTACH_FORM,
3563 XmNrightOffset, 4,
3564 XmNtopAttachment, XmATTACH_FORM,
3565 XmNtopOffset, 4,
3566 XmNbottomAttachment, XmATTACH_FORM,
3567 XmNbottomOffset, 4,
3568 NULL);
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 frdp->find = XtVaCreateManagedWidget("findButton",
3571 xmPushButtonWidgetClass, button_form,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 XmNsensitive, True,
3573 XmNtopAttachment, XmATTACH_FORM,
3574 XmNleftAttachment, XmATTACH_FORM,
3575 XmNrightAttachment, XmATTACH_FORM,
3576 NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003577 set_label(frdp->find, _("Find &Next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 XtAddCallback(frdp->find, XmNactivateCallback,
3580 find_replace_callback,
Bram Moolenaare9e3b572008-01-22 10:06:48 +00003581 (do_replace ? (XtPointer)FRD_R_FINDNEXT : (XtPointer)FRD_FINDNEXT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 if (do_replace)
3584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 frdp->replace = XtVaCreateManagedWidget("replaceButton",
3586 xmPushButtonWidgetClass, button_form,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 XmNtopAttachment, XmATTACH_WIDGET,
3588 XmNtopWidget, frdp->find,
3589 XmNleftAttachment, XmATTACH_FORM,
3590 XmNrightAttachment, XmATTACH_FORM,
3591 NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003592 set_label(frdp->replace, _("&Replace"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 XtAddCallback(frdp->replace, XmNactivateCallback,
3594 find_replace_callback, (XtPointer)FRD_REPLACE);
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 frdp->all = XtVaCreateManagedWidget("replaceAllButton",
3597 xmPushButtonWidgetClass, button_form,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 XmNtopAttachment, XmATTACH_WIDGET,
3599 XmNtopWidget, frdp->replace,
3600 XmNleftAttachment, XmATTACH_FORM,
3601 XmNrightAttachment, XmATTACH_FORM,
3602 NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003603 set_label(frdp->all, _("Replace &All"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 XtAddCallback(frdp->all, XmNactivateCallback,
3605 find_replace_callback, (XtPointer)FRD_REPLACEALL);
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 frdp->undo = XtVaCreateManagedWidget("undoButton",
3608 xmPushButtonWidgetClass, button_form,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 XmNtopAttachment, XmATTACH_WIDGET,
3610 XmNtopWidget, frdp->all,
3611 XmNleftAttachment, XmATTACH_FORM,
3612 XmNrightAttachment, XmATTACH_FORM,
3613 NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003614 set_label(frdp->undo, _("&Undo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 XtAddCallback(frdp->undo, XmNactivateCallback,
3616 find_replace_callback, (XtPointer)FRD_UNDO);
3617 }
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 frdp->cancel = XtVaCreateManagedWidget("closeButton",
3620 xmPushButtonWidgetClass, button_form,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 XmNleftAttachment, XmATTACH_FORM,
3622 XmNrightAttachment, XmATTACH_FORM,
3623 XmNbottomAttachment, XmATTACH_FORM,
3624 NULL);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003625 set_label(frdp->cancel, _("&Cancel"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 XtAddCallback(frdp->cancel, XmNactivateCallback,
3627 find_replace_dismiss_callback, frdp);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003628 gui_motif_menu_fontlist(frdp->cancel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 XtManageChild(button_form);
3631
3632 n = 0;
3633 XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
3634 XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
3635 XtSetArg(args[n], XmNrightWidget, button_form); n++;
3636 XtSetArg(args[n], XmNrightOffset, 4); n++;
3637 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
3638 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
3639 separator = XmCreateSeparatorGadget(frdp->dialog, "separator", args, n);
3640 XtManageChild(separator);
3641
3642 input_form = XtVaCreateWidget("inputForm",
3643 xmFormWidgetClass, frdp->dialog,
3644 XmNleftAttachment, XmATTACH_FORM,
3645 XmNleftOffset, 4,
3646 XmNrightAttachment, XmATTACH_WIDGET,
3647 XmNrightWidget, separator,
3648 XmNrightOffset, 4,
3649 XmNtopAttachment, XmATTACH_FORM,
3650 XmNtopOffset, 4,
3651 NULL);
3652
3653 {
3654 Widget label_what;
3655 Widget label_with = (Widget)0;
3656
3657 str = XmStringCreateSimple(_("Find what:"));
3658 label_what = XtVaCreateManagedWidget("whatLabel",
3659 xmLabelGadgetClass, input_form,
3660 XmNlabelString, str,
3661 XmNleftAttachment, XmATTACH_FORM,
3662 XmNtopAttachment, XmATTACH_FORM,
3663 XmNtopOffset, 4,
3664 NULL);
3665 XmStringFree(str);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003666 gui_motif_menu_fontlist(label_what);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 frdp->what = XtVaCreateManagedWidget("whatText",
3669 xmTextFieldWidgetClass, input_form,
3670 XmNtopAttachment, XmATTACH_FORM,
3671 XmNrightAttachment, XmATTACH_FORM,
3672 XmNleftAttachment, XmATTACH_FORM,
3673 NULL);
3674
3675 if (do_replace)
3676 {
3677 frdp->with = XtVaCreateManagedWidget("withText",
3678 xmTextFieldWidgetClass, input_form,
3679 XmNtopAttachment, XmATTACH_WIDGET,
3680 XmNtopWidget, frdp->what,
3681 XmNtopOffset, 4,
3682 XmNleftAttachment, XmATTACH_FORM,
3683 XmNrightAttachment, XmATTACH_FORM,
3684 XmNbottomAttachment, XmATTACH_FORM,
3685 NULL);
3686
3687 XtAddCallback(frdp->with, XmNactivateCallback,
3688 find_replace_callback, (XtPointer) FRD_R_FINDNEXT);
3689
3690 str = XmStringCreateSimple(_("Replace with:"));
3691 label_with = XtVaCreateManagedWidget("withLabel",
3692 xmLabelGadgetClass, input_form,
3693 XmNlabelString, str,
3694 XmNleftAttachment, XmATTACH_FORM,
3695 XmNtopAttachment, XmATTACH_WIDGET,
3696 XmNtopWidget, frdp->what,
3697 XmNtopOffset, 4,
3698 XmNbottomAttachment, XmATTACH_FORM,
3699 NULL);
3700 XmStringFree(str);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003701 gui_motif_menu_fontlist(label_with);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
3703 /*
3704 * Make the entry activation only change the input focus onto the
3705 * with item.
3706 */
3707 XtAddCallback(frdp->what, XmNactivateCallback,
3708 entry_activate_callback, frdp->with);
3709 XtAddEventHandler(frdp->with, KeyPressMask, False,
3710 (XtEventHandler)find_replace_keypress,
3711 (XtPointer) frdp);
3712
3713 }
3714 else
3715 {
3716 /*
3717 * Make the entry activation do the search.
3718 */
3719 XtAddCallback(frdp->what, XmNactivateCallback,
3720 find_replace_callback, (XtPointer)FRD_FINDNEXT);
3721 }
3722 XtAddEventHandler(frdp->what, KeyPressMask, False,
3723 (XtEventHandler)find_replace_keypress,
3724 (XtPointer)frdp);
3725
Bram Moolenaar734a8672019-12-02 22:49:38 +01003726 // Get the maximum width between the label widgets and line them up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 n = 0;
3728 XtSetArg(args[n], XmNwidth, &width); n++;
3729 XtGetValues(label_what, args, n);
3730 widest = width;
3731 if (do_replace)
3732 {
3733 XtGetValues(label_with, args, n);
3734 if (width > widest)
3735 widest = width;
3736 }
3737
3738 XtVaSetValues(frdp->what, XmNleftOffset, widest, NULL);
3739 if (do_replace)
3740 XtVaSetValues(frdp->with, XmNleftOffset, widest, NULL);
3741
3742 }
3743
3744 XtManageChild(input_form);
3745
3746 {
3747 Widget radio_box;
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003748 Widget w;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 frame = XtVaCreateWidget("directionFrame",
3751 xmFrameWidgetClass, frdp->dialog,
3752 XmNtopAttachment, XmATTACH_WIDGET,
3753 XmNtopWidget, input_form,
3754 XmNtopOffset, 4,
3755 XmNbottomAttachment, XmATTACH_FORM,
3756 XmNbottomOffset, 4,
3757 XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
3758 XmNrightWidget, input_form,
3759 NULL);
3760
3761 str = XmStringCreateSimple(_("Direction"));
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003762 w = XtVaCreateManagedWidget("directionFrameLabel",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 xmLabelGadgetClass, frame,
3764 XmNlabelString, str,
3765 XmNchildHorizontalAlignment, XmALIGNMENT_BEGINNING,
3766 XmNchildType, XmFRAME_TITLE_CHILD,
3767 NULL);
3768 XmStringFree(str);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003769 gui_motif_menu_fontlist(w);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771 radio_box = XmCreateRadioBox(frame, "radioBox",
3772 (ArgList)NULL, 0);
3773
3774 str = XmStringCreateSimple( _("Up"));
3775 frdp->up = XtVaCreateManagedWidget("upRadioButton",
3776 xmToggleButtonGadgetClass, radio_box,
3777 XmNlabelString, str,
3778 XmNset, False,
3779 NULL);
3780 XmStringFree(str);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003781 gui_motif_menu_fontlist(frdp->up);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
3783 str = XmStringCreateSimple(_("Down"));
3784 frdp->down = XtVaCreateManagedWidget("downRadioButton",
3785 xmToggleButtonGadgetClass, radio_box,
3786 XmNlabelString, str,
3787 XmNset, True,
3788 NULL);
3789 XmStringFree(str);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003790 gui_motif_menu_fontlist(frdp->down);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 XtManageChild(radio_box);
3793 XtManageChild(frame);
3794 }
3795
3796 toggle_form = XtVaCreateWidget("toggleForm",
3797 xmFormWidgetClass, frdp->dialog,
3798 XmNleftAttachment, XmATTACH_FORM,
3799 XmNleftOffset, 4,
3800 XmNrightAttachment, XmATTACH_WIDGET,
3801 XmNrightWidget, frame,
3802 XmNrightOffset, 4,
3803 XmNtopAttachment, XmATTACH_WIDGET,
3804 XmNtopWidget, input_form,
3805 XmNtopOffset, 4,
3806 XmNbottomAttachment, XmATTACH_FORM,
3807 XmNbottomOffset, 4,
3808 NULL);
3809
3810 str = XmStringCreateSimple(_("Match whole word only"));
3811 frdp->wword = XtVaCreateManagedWidget("wordToggle",
3812 xmToggleButtonGadgetClass, toggle_form,
3813 XmNlabelString, str,
3814 XmNtopAttachment, XmATTACH_FORM,
3815 XmNtopOffset, 4,
3816 XmNleftAttachment, XmATTACH_FORM,
3817 XmNleftOffset, 4,
3818 XmNset, wword,
3819 NULL);
3820 XmStringFree(str);
3821
3822 str = XmStringCreateSimple(_("Match case"));
3823 frdp->mcase = XtVaCreateManagedWidget("caseToggle",
3824 xmToggleButtonGadgetClass, toggle_form,
3825 XmNlabelString, str,
3826 XmNleftAttachment, XmATTACH_FORM,
3827 XmNleftOffset, 4,
3828 XmNtopAttachment, XmATTACH_WIDGET,
3829 XmNtopWidget, frdp->wword,
3830 XmNtopOffset, 4,
3831 XmNset, mcase,
3832 NULL);
3833 XmStringFree(str);
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003834 gui_motif_menu_fontlist(frdp->wword);
3835 gui_motif_menu_fontlist(frdp->mcase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 XtManageChild(toggle_form);
3838
3839 if (entry_text != NULL)
3840 XmTextFieldSetString(frdp->what, (char *)entry_text);
3841 vim_free(entry_text);
3842
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003843 gui_motif_synch_fonts();
3844
3845 manage_centered(frdp->dialog);
3846 activate_dialog_mnemonics(frdp->dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 XmProcessTraversal(frdp->what, XmTRAVERSE_CURRENT);
3848}
3849
3850 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003851gui_mch_find_dialog(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
3853 if (!gui.in_use)
3854 return;
3855
3856 find_replace_dialog_create(eap->arg, FALSE);
3857}
3858
3859
3860 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003861gui_mch_replace_dialog(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
3863 if (!gui.in_use)
3864 return;
3865
3866 find_replace_dialog_create(eap->arg, TRUE);
3867}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003868
3869/*
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003870 * Synchronize all gui elements, which are dependent upon the
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003871 * main text font used. Those are in esp. the find/replace dialogs.
3872 * If you don't understand why this should be needed, please try to
Bram Moolenaar305598b2016-01-30 13:53:36 +01003873 * search for "pi\xea\xb6\xe6" in iso8859-2.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003874 */
3875 void
3876gui_motif_synch_fonts(void)
3877{
3878 SharedFindReplace *frdp;
3879 int do_replace;
3880 XFontStruct *font;
3881 XmFontList font_list;
3882
Bram Moolenaar734a8672019-12-02 22:49:38 +01003883 // FIXME: Unless we find out how to create a XmFontList from a XFontSet,
3884 // we just give up here on font synchronization.
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003885 font = (XFontStruct *)gui.norm_font;
3886 if (font == NULL)
3887 return;
3888
3889 font_list = gui_motif_create_fontlist(font);
3890
Bram Moolenaar734a8672019-12-02 22:49:38 +01003891 // OK this loop is a bit tricky...
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003892 for (do_replace = 0; do_replace <= 1; ++do_replace)
3893 {
3894 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
3895 if (frdp->dialog)
3896 {
3897 XtVaSetValues(frdp->what, XmNfontList, font_list, NULL);
3898 if (do_replace)
3899 XtVaSetValues(frdp->with, XmNfontList, font_list, NULL);
3900 }
3901 }
3902
3903 XmFontListFree(font_list);
3904}