File: x11menu.c

package info (click to toggle)
vile 9.2y-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,700 kB
  • ctags: 7,341
  • sloc: ansic: 78,427; lex: 4,726; perl: 2,922; cpp: 2,710; sh: 2,612; makefile: 715; awk: 212
file content (417 lines) | stat: -rw-r--r-- 10,518 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/************************************************************************/
/*  File        : menu.c                                                */
/*  Description : Implement a Motif MenuBar into Mxvile                 */
/*                Parse a File (environment variable XVILE_MENU)        */
/*                and create it at the top of the window                */
/*  Auteur      : Philippe CHASSANY                                     */
/*  Date        : 20/02/97                                              */
/************************************************************************/

/*
 * $Header: /usr/build/vile/vile/RCS/x11menu.c,v 1.3 2002/01/20 23:46:32 tom Exp $
 */

#define NEED_X_INCLUDES 1

/* Vile includes */
#include "estruct.h"
#include "edef.h"

#if DISP_X11

#if MOTIF_WIDGETS
#include <Xm/MainW.h>
#include <Xm/CascadeB.h>
#include <Xm/SeparatoG.h>
#include <Xm/RowColumn.h>
#include <Xm/PushBG.h>
#include <Xm/ToggleB.h>
#endif

#if ATHENA_WIDGETS
#include <X11/Xaw/Form.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SmeLine.h>
#include <X11/Xaw/SmeBSB.h>
#endif

#define MY_MENUS struct MyMenus
MY_MENUS {
    MY_MENUS *next;
    Widget parent;
    Widget child;
};

/* Locals */
static Widget cascade;
static MY_MENUS *my_menus;

/************************************************************************/
/* Motif function to make a cascade button into a menubar               */
/************************************************************************/
void *
gui_make_menu(void *menubar, char *nom, int the_class GCC_UNUSED)
{
    MY_MENUS *remember;
    Widget pm;
    Widget menub = (Widget) menubar;
#if MOTIF_WIDGETS
    XmString xms;
    char str[50];

    sprintf(str, "%sMenu", nom);
    pm = (Widget) XmCreatePulldownMenu(menub, str, NULL, 0);
#if OPT_MENUS_COLORED
    XtVaSetValues(pm,
		  XtNforeground, x_menu_foreground(),
		  XtNbackground, x_menu_background(),
		  NULL);
    XtVaSetValues(menub,
		  XtNforeground, x_menu_foreground(),
		  XtNbackground, x_menu_background(),
		  NULL);
#endif /* OPT_MENUS_COLORED */

    xms = XmStringCreateSimple(nom);

    cascade = XtVaCreateManagedWidget("menuHeader",
				      xmCascadeButtonWidgetClass, menub,
				      XmNlabelString, xms,
				      XmNsubMenuId, pm,
#if OPT_MENUS_COLORED
				      XtNforeground, x_menu_foreground(),
				      XtNbackground, x_menu_background(),
#endif /* OPT_MENUS_COLORED */
				      NULL);
    XmStringFree(xms);

    if (the_class == 'H') {
	XtVaSetValues(menub,
		      XmNmenuHelpWidget, cascade,
		      NULL);
    }
#endif /* MOTIF_WIDGETS */
#if ATHENA_WIDGETS
    static Widget last;
    String str = XtMalloc(strlen(nom) + 10);

    sprintf(str, "%sMenu", nom);
    pm = XtVaCreatePopupShell(str,
			      simpleMenuWidgetClass,
			      menub,
			      XtNgeometry, NULL,
			      NULL);

    cascade = XtVaCreateManagedWidget("menuHeader",
				      menuButtonWidgetClass,
				      menub,
				      XtNheight, x_menu_height(),
				      XtNlabel, nom,
				      XtNfromHoriz, last,
				      XtNmenuName, str,
				      NULL);
    last = cascade;

#endif /* ATHENA_WIDGETS */

    /* remember the widgets we created, so we can destroy them */
    remember = typecalloc(MY_MENUS);
    remember->parent = pm;
    remember->child = cascade;
    remember->next = my_menus;
    my_menus = remember;

    return (void *) pm;
}

/************************************************************************/
/* Motif function to make a button into a cascade (with accelarator)    */
/************************************************************************/
void *
gui_add_menu_item(void *pm, char *nom, char *accel GCC_UNUSED, int the_class)
{
    Widget w;
#if MOTIF_WIDGETS
    XmString xms_accl;
    XmString xms_name;
    WidgetClass wc;

    switch (the_class) {
    case 'S':
	wc = xmSeparatorGadgetClass;
	break;
    default:
	/* FALLTHRU */
    case 'B':
	wc = xmPushButtonGadgetClass;
	break;
    }

    if (accel != NULL)
	xms_accl = XmStringCreateSimple(accel);
    else
	xms_accl = XmStringCreateSimple("");

    xms_name = XmStringCreateSimple(nom);
    w = XtVaCreateManagedWidget("menuEntry",
				wc,
				pm,
				XmNacceleratorText, xms_accl,
				XmNlabelString, xms_name,
				NULL);

    XmStringFree(xms_accl);
    XmStringFree(xms_name);

    return w;
#endif /* MOTIF_WIDGETS */
#if ATHENA_WIDGETS
    w = XtVaCreateManagedWidget("menuEntry",
				((the_class == 'B')
				 ? smeBSBObjectClass
				 : smeLineObjectClass),
				pm,
				XtNlabel, nom,
				NULL);

#endif /* ATHENA_WIDGETS */
    return w;
}

/************************************************************************/
/* Function called when a buffer button is clicked into the menu        */
/* A buffer button takes part from the list tokens (L)                  */
/************************************************************************/
static void
list_proc_back(Widget w GCC_UNUSED, XtPointer bname, XtPointer call GCC_UNUSED)
{
    int num_buff;
    int oldflag = im_waiting(-1);

#if MOTIF_WIDGETS
    char *accel;
    XmString xms;
    XtVaGetValues(w, XmNacceleratorText, &xms, NULL);
#ifndef XmFONTLIST_DEFAULT_TAG
#define XmFONTLIST_DEFAULT_TAG "XmFONTLIST_DEFAULT_TAG_STRING"
#endif
    XmStringGetLtoR(xms, XmFONTLIST_DEFAULT_TAG, &accel);
    num_buff = atoi(&accel[1]);
#endif
#if ATHENA_WIDGETS
    num_buff = (int) bname;
#endif

    if (vile_is_busy)
	return;

    (void) histbuff(TRUE, num_buff);
    (void) im_waiting(oldflag);
    (void) update(TRUE);
}

/************************************************************************/
/* Function called when a button is clicked into the menu               */
/* Bind and Action are tested                                           */
/************************************************************************/
static void
proc_back(Widget w GCC_UNUSED, XtPointer arg, XtPointer call GCC_UNUSED)
{
    char *macro_action = (char *) arg;
    ActionFunc fact;
    int oldflag = im_waiting(-1);
    int exec_flag = TRUE;
    char *s;

    TRACE(("Macro/Action=%s\n", macro_action));

#if OPT_WORKING
    if (vile_is_busy)
	return;
#endif

    if ((s = vlmenu_is_cmd(macro_action)) != 0) {
	macro_action = s;
	exec_flag = FALSE;
    }

    if (vlmenu_is_bind(macro_action)) {
	docmd(macro_action, exec_flag, FALSE, 1);
    } else {
	/* Action predefined */
	fact = vlmenu_action_func(macro_action);
	if (fact != NULL)
	    fact(macro_action);
    }

    (void) im_waiting(oldflag);
    (void) update(TRUE);
}

void
gui_add_func_callback(void *w, void *closure)
{
#if MOTIF_WIDGETS
    XtAddCallback((Widget) w, XmNactivateCallback, proc_back, closure);
#endif
#if ATHENA_WIDGETS
    XtAddCallback((Widget) w, XtNcallback, proc_back, closure);
#endif /* ATHENA_WIDGETS */
}

/************************************************************************/
/* Function called when the cascade containing the List (buffers)       */
/* is clicked => Post the PullDown associated                           */
/* It's dirty ...                                                       */
/************************************************************************/
static void
post_buffer_list(Widget w GCC_UNUSED,
		 XtPointer client GCC_UNUSED,
		 XEvent * ev GCC_UNUSED,
		 Boolean * ok GCC_UNUSED)
{
    static int in_item_menu_list = 0;	/* number allocated */
    static int nb_item_menu_list = 0;	/* number in use */
    static Widget *pm_buffer;

    int i, n = nb_item_menu_list;
    BUFFER *bp;
    char string[NBUFN + 2 + NFILEN], temp[1 + NFILEN], *p;
    Widget pm = (Widget) client;

    TRACE(("post_buffer_list\n"));
    nb_item_menu_list = 0;

    for_each_buffer(bp) {
	if (b_is_temporary(bp))	/* cf: hist_show() */
	    continue;

	p = shorten_path(strcpy(temp, bp->b_fname), FALSE);
	sprintf(string, "%-*.*s %s", NBUFN - 1, NBUFN - 1,
		bp->b_bname, p);

	sprintf(temp, "_%d", nb_item_menu_list);
	TRACE(("ACCEL(%s) = %s\n", temp, string));

	if (nb_item_menu_list + 2 >= in_item_menu_list) {
	    int m = in_item_menu_list;
	    in_item_menu_list = (in_item_menu_list + 3) * 2;
	    if (pm_buffer != 0)
		pm_buffer = typereallocn(Widget, pm_buffer, in_item_menu_list);
	    else
		pm_buffer = typeallocn(Widget, in_item_menu_list);
	    while (m < in_item_menu_list)
		pm_buffer[m++] = 0;
	}

	if (pm_buffer[nb_item_menu_list] == 0) {
	    pm_buffer[nb_item_menu_list] = gui_add_menu_item(pm, string,
							     temp, 'B');
	} else {
#if MOTIF_WIDGETS
	    XmString xms = XmStringCreateSimple(string);
	    XtVaSetValues(pm_buffer[nb_item_menu_list],
			  XmNlabelString, xms, NULL);
	    XmStringFree(xms);
	    XtRemoveCallback(pm_buffer[nb_item_menu_list],
			     XmNactivateCallback, list_proc_back, NULL);
#endif
#if ATHENA_WIDGETS
	    XtVaSetValues(pm_buffer[nb_item_menu_list],
			  XtNlabel, string, NULL);
	    XtRemoveAllCallbacks(pm_buffer[nb_item_menu_list],
				 XtNcallback);
#endif
	}

#if MOTIF_WIDGETS
	XtAddCallback(pm_buffer[nb_item_menu_list],
		      XmNactivateCallback,
		      list_proc_back,
		      NULL);
#endif
#if ATHENA_WIDGETS
	XtAddCallback(pm_buffer[nb_item_menu_list],
		      XtNcallback,
		      list_proc_back,
		      (XtPointer) nb_item_menu_list);
#endif
	nb_item_menu_list++;
    }
    for (i = nb_item_menu_list; i < n; i++) {
	XtDestroyWidget(pm_buffer[i]);
	pm_buffer[i] = NULL;
    }

#if MOTIF_WIDGETS
    XmUpdateDisplay(pm);
#endif
}

void
gui_add_list_callback(void *pm)
{
    if (cascade != 0)
	XtAddEventHandler(cascade,
			  ButtonPressMask,
			  False,
			  post_buffer_list,
			  (Widget) pm);
}

/*
 * Hide/show menus, useful for avoiding thrashing while regenering menus
 */
int
gui_hide_menus(int f GCC_UNUSED, int n GCC_UNUSED)
{
    Widget w = x_menu_widget();

    if (w != 0) {
	XtUnmapWidget(w);
	return TRUE;
    }
    return FALSE;
}

int
gui_show_menus(int f GCC_UNUSED, int n GCC_UNUSED)
{
    Widget w = x_menu_widget();

    if (w != 0) {
	XtMapWidget(w);
	return TRUE;
    }
    return FALSE;
}

/*
 * Remove children of the current menu widget, so we can regenerate them.
 */
int
gui_remove_menus(int f GCC_UNUSED, int n GCC_UNUSED)
{
    gui_hide_menus(f, n);
    while (my_menus != 0) {
	MY_MENUS *next = my_menus->next;
	XtUnmanageChild(my_menus->parent);
	XtDestroyWidget(my_menus->parent);
	XtDestroyWidget(my_menus->child);
	free(my_menus);
	my_menus = next;
    }
    return FALSE;
}

/*
 * (Re)create the menus.
 */
int
gui_create_menus(void)
{
    return do_menu(x_menu_widget());
}

#endif /* DISP_X11 */