Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*-
 * Copyright (c) 1996
 *	Rob Zimmermann.  All rights reserved.
 * Copyright (c) 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#if 0
#ifndef lint
static const char sccsid[] = "Id: m_menu.c,v 8.26 2003/11/05 17:09:59 skimo Exp  (Berkeley) Date: 2003/11/05 17:09:59 ";
#endif /* not lint */
#else
__RCSID("$NetBSD: m_menu.c,v 1.2 2014/01/26 21:43:45 christos Exp $");
#endif

#include <sys/queue.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xm/PushB.h>
#include <Xm/CascadeB.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include <Xm/FileSB.h>
#include <Xm/SelectioB.h>

#include <bitstring.h>
#include <stdio.h>

#undef LOCK_SUCCESS
#include "../common/common.h"
#include "../ipc/ip.h"
#include "m_motif.h"

extern int vi_ofd;

/* save this for creation of children */
static	Widget	main_widget = NULL;

/* This module defines the menu structure for vi.  Each menu
 * item has an action routine associated with it.  For the most
 * part, those actions will simply call vi_send with vi commands.
 * others will pop up file selection dialogs and use them for
 * vi commands, and other will have to have special actions.
 *
 * Future:
 *	vi core will have to let us know when to be sensitive
 *	change VI_STRING to VI_COMMAND so that menu actions cannot
 *		be confusing when in insert mode
 *	need VI_CUT, VI_COPY, and VI_PASTE to perform the appropriate
 *		operations on the visible text of yank buffer.  VI_COPY
 *		is likely a NOP, but it will make users happy
 *	add mnemonics
 *	add accelerators
 *	implement file selection dialog boxes
 *	implement string prompt dialog boxes (e.g. for 'find')
 *
 * Interface:
 *	Widget	create_menubar( Widget parent ) creates and returns the
 *		X menu structure.  The caller can place this
 *		anywhere in the widget heirarchy.
 */

#define	BufferSize	1024

/*
 * __vi_send_command_string --
 *	Utility:  Send a menu command to vi
 *
 * Future:
 * Change VI_STRING to VI_COMMAND so that menu actions cannot be confusing
 * when in insert mode.
 *
 * XXX
 * THIS SHOULD GO AWAY -- WE SHOULDN'T SEND UNINTERPRETED STRINGS TO THE
 * CORE.
 *
 * PUBLIC: void __vi_send_command_string __P((String));
 */
void
__vi_send_command_string(String str)
{
    IP_BUF	ipb;
    char	buffer[BufferSize];

    /* Future:  Need VI_COMMAND so vi knows this is not text to insert
     * At that point, appending a cr/lf will not be necessary.  For now,
     * append iff we are a colon or slash command.  Of course, if we are in
     * insert mode, all bets are off.
     */
    strcpy( buffer, str );
    switch ( *str ) {
	case ':':
	case '/':
	    strcat( buffer, "\n" );
	    break;
    }

    ipb.code = VI_STRING;
    ipb.str1 = buffer;
    ipb.len1 = strlen(buffer);
    vi_send(vi_ofd, "a", &ipb);
}


/* Utility:  beep for unimplemented command */

#if defined(__STDC__)
static	void	send_beep( Widget w )
#else
static	void	send_beep( w )
	Widget	w;
#endif
{
    XBell( XtDisplay(w), 0 );
}


/*
 * __vi_cancel_cb --
 *	Utility:  make a dialog box go Modal
 *
 * PUBLIC: void __vi_cancel_cb __P((Widget, XtPointer, XtPointer));
 */
static	Bool	have_answer;
void
__vi_cancel_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
	have_answer = True;
}

/*
 * PUBLIC: void __vi_modal_dialog __P((Widget));
 */
void
__vi_modal_dialog(Widget db)
{
    XtAppContext	ctx;

    /* post the dialog */
    XtManageChild( db );
    XtPopup( XtParent(db), XtGrabExclusive );

    /* wait for a response */
    ctx = XtWidgetToApplicationContext(db);
    XtAddGrab( XtParent(db), TRUE, FALSE );
    for ( have_answer = False; ! have_answer; )
	XtAppProcessEvent( ctx, XtIMAll );

    /* done with db */
    XtPopdown( XtParent(db) );
    XtRemoveGrab( XtParent(db) );
}


/* Utility:  Get a file (using standard File Selection Dialog Box) */

static	String	file_name;


#if defined(__STDC__)
static	void		ok_file_name( Widget w,
				      XtPointer client_data,
				      XtPointer call_data
				      )
#else
static	void		ok_file_name( w, client_data, call_data )
	Widget		w;
	XtPointer	client_data;
	XtPointer	call_data;
#endif
{
    XmFileSelectionBoxCallbackStruct	*cbs;

    cbs = (XmFileSelectionBoxCallbackStruct *) call_data;
    XmStringGetLtoR( cbs->value, XmSTRING_DEFAULT_CHARSET, &file_name );

    have_answer = True;
}


#if defined(__STDC__)
static	String	get_file( Widget w, String prompt )
#else
static	String	get_file( w, prompt )
	Widget	w;
	String	prompt;
#endif
{
    /* make it static so we can reuse it */
    static	Widget	db;

    /* our return parameter */
    if ( file_name != NULL ) {
	XtFree( file_name );
	file_name = NULL;
    }

    /* create one? */
    if ( db == NULL ){ 
	db = XmCreateFileSelectionDialog( main_widget, "file", NULL, 0 );
	XtAddCallback( db, XmNokCallback, ok_file_name, NULL );
	XtAddCallback( db, XmNcancelCallback, __vi_cancel_cb, NULL );
    }

    /* use the title as a prompt */
    XtVaSetValues( XtParent(db), XmNtitle, prompt, 0 );

    /* wait for a response */
    __vi_modal_dialog( db );

    /* done */
    return file_name;
}


/*
 * file_command --
 *	Get a file name and send it with the command to the core.
 */
static void
file_command(Widget w, int code, String prompt)
{
	IP_BUF ipb;
	char *file;

	if ((file = get_file(w, prompt)) != NULL) {
		ipb.code = code;
		ipb.str1 = file;
		ipb.len1 = strlen(file);
		vi_send(vi_ofd, "a", &ipb);
	}
}


/*
 * Menu action routines (one per menu entry)
 *
 * These are in the order in which they appear in the menu structure.
 */
static void
ma_edit_file(Widget w, XtPointer call_data, XtPointer client_data)
{
	file_command(w, VI_EDIT, "Edit");
}

static void
ma_split(Widget w, XtPointer call_data, XtPointer client_data)
{
	file_command(w, VI_EDITSPLIT, "Edit");
}

static void
ma_save(Widget w, XtPointer call_data, XtPointer client_data)
{
	IP_BUF ipb;

	ipb.code = VI_WRITE;
	(void)vi_send(vi_ofd, NULL, &ipb);
}

static void
ma_save_as(Widget w, XtPointer call_data, XtPointer client_data)
{
	file_command(w, VI_WRITEAS, "Save As");
}

static void
ma_wq(Widget w, XtPointer call_data, XtPointer client_data)
{
	IP_BUF ipb;

	ipb.code = VI_WQ;
	(void)vi_send(vi_ofd, NULL, &ipb);
}

static void
ma_quit(Widget w, XtPointer call_data, XtPointer client_data)
{
	IP_BUF ipb;

	ipb.code = VI_QUIT;
	(void)vi_send(vi_ofd, NULL, &ipb);
}

static void
ma_undo(Widget w, XtPointer call_data, XtPointer client_data)
{
	IP_BUF ipb;

	ipb.code = VI_UNDO;
	(void)vi_send(vi_ofd, NULL, &ipb);
}

#if defined(__STDC__)
static void	ma_cut(	Widget w,
			XtPointer call_data,
			XtPointer client_data
			)
#else
static	void		ma_cut( w, call_data, client_data )
	Widget		w;
	XtPointer	call_data;
	XtPointer	client_data;
#endif
{
    /* future */
    send_beep( w );
}


#if defined(__STDC__)
static	void	ma_copy(	Widget w,
				XtPointer call_data,
				XtPointer client_data
				)
#else
static	void		ma_copy( w, call_data, client_data )
	Widget		w;
	XtPointer	call_data;
	XtPointer	client_data;
#endif
{
    /* future */
    send_beep( w );
}


#if defined(__STDC__)
static	void	ma_paste(	Widget w,
				XtPointer call_data,
				XtPointer client_data
				)
#else
static	void		ma_paste( w, call_data, client_data )
	Widget		w;
	XtPointer	call_data;
	XtPointer	client_data;
#endif
{
    /* future */
    send_beep( w );
}

static void
ma_find(Widget w, XtPointer call_data, XtPointer client_data)
{
	__vi_show_search_dialog( main_widget, "Find" );
}

static void
ma_find_next(Widget w, XtPointer call_data, XtPointer client_data)
{
	__vi_search( w );
}

static void
ma_tags(Widget w, XtPointer call_data, XtPointer client_data)
{
	__vi_show_tags_dialog( main_widget, "Tag Stack" );
}

static void
ma_tagpop(Widget w, XtPointer call_data, XtPointer client_data)
{
	__vi_send_command_string( "\024" );
}

static void
ma_tagtop(Widget w, XtPointer call_data, XtPointer client_data)
{
	__vi_send_command_string( ":tagtop" );
}

#if defined(__STDC__)
static void	ma_preferences(	Widget w,
				XtPointer call_data,
				XtPointer client_data
				)
#else
static	void		ma_preferences( w, call_data, client_data )
	Widget		w;
	XtPointer	call_data;
	XtPointer	client_data;
#endif
{
	__vi_show_options_dialog( main_widget, "Preferences" );
}


/* Menu construction routines */

typedef	struct {
    String	title;
    void	(*action)();
    String	accel;		/* for Motif */
    String	accel_text;	/* for the user */
} pull_down;

typedef	struct {
    char	mnemonic;
    String	title;
    pull_down	*actions;
} menu_bar;

static	pull_down	file_menu[] = {
    { "Edit File...",		ma_edit_file,	"Alt<Key>e",	"Alt+E"	},
    { "",			NULL,		NULL,	NULL	},
    { "Split Window...",	ma_split,	NULL,	NULL	},
    { "",			NULL,		NULL,	NULL	},
    { "Save ",			ma_save,	"Alt<Key>s",	"Alt+S"	},
    { "Save As...",		ma_save_as,	"Shift Alt<Key>s",	"Shift+Alt+S"	},
    { "",			NULL,		NULL,	NULL	},
    { "Write and Quit",		ma_wq,		"Shift Alt<Key>q",	"Shift+Alt+Q"	},
    { "Quit",			ma_quit,	"Alt<Key>q",	"Alt+Q"	},
    { NULL,			NULL,		NULL,	NULL	},
};

static	pull_down	edit_menu[] = {
    { "Undo",			ma_undo,	NULL,	NULL	},
    { "",			NULL,		NULL,	NULL	},
    { "Cut",			ma_cut,		"Alt<Key>x",	"Alt+X"	},
    { "Copy",			ma_copy,	"Alt<Key>c",	"Alt+C"	},
    { "Paste",			ma_paste,	"Alt<Key>v",	"Alt+V"	},
    { "",			NULL,		NULL,	NULL	},
    { "Find",			ma_find,	"Alt<Key>f",	"Alt+F"	},
    { "Find Next",		ma_find_next,	"Alt<Key>g",	"Alt+G"	},
    { NULL,			NULL,		NULL,	NULL	},
};

static	pull_down	options_menu[] = {
    { "Preferences",		ma_preferences,	NULL,	NULL	},
    { "Command Mode Maps",	NULL,		NULL,	NULL	},
    { "Insert Mode Maps",	NULL,		NULL,	NULL	},
    { NULL,			NULL,		NULL,	NULL	},
};

static	pull_down	tag_menu[] = {
    { "Show Tag Stack",		ma_tags,	"Alt<Key>t",	"Alt+T"	},
    { "",			NULL,		NULL,	NULL	},
    { "Pop Tag",		ma_tagpop,	NULL,	NULL	},
    { "Clear Stack",		ma_tagtop,	NULL,	NULL	},
    { NULL,			NULL,		NULL,	NULL	},
};

static	pull_down	help_menu[] = {
    { NULL,			NULL,		NULL,	NULL	},
};

static	menu_bar	main_menu[] = {
    { 'F',	"File",		file_menu	},
    { 'E',	"Edit", 	edit_menu	},
    { 'O',	"Options",	options_menu	},
    { 'T',	"Tag",		tag_menu	},
    { 'H',	"Help",		help_menu	},
    { 0,	NULL,		NULL		},
};


#if defined(__STDC__)
static	void	add_entries( Widget parent, pull_down *actions )
#else
static	void		add_entries( parent, actions )
	Widget		parent;
	pull_down	*actions;
#endif
{
    Widget	w;
    XmString	str;

    for ( ; actions->title != NULL; actions++ ) {

	/* a separator? */
	if ( *actions->title != '\0' ) {
	    w = XmCreatePushButton( parent, actions->title, NULL, 0 );
	    if ( actions->action == NULL )
		XtSetSensitive( w, False );
	    else
		XtAddCallback(  w,
				XmNactivateCallback,
				(XtCallbackProc) actions->action,
				actions
				);
	    if ( actions->accel != NULL ) {
		str = XmStringCreateSimple( actions->accel_text );
		XtVaSetValues(	w,
				XmNaccelerator,		actions->accel,
				XmNacceleratorText,	str,
				0
				);
		XmStringFree( str );
	    }
	}
	else {
	    w = XmCreateSeparator( parent, "separator", NULL, 0 );
	}

	XtManageChild( w );

    }
}

/*
 * vi_create_menubar --
 *
 * PUBLIC: Widget vi_create_menubar __P((Widget));
 */
Widget
vi_create_menubar(Widget parent)
{
    Widget	menu, pull, button;
    menu_bar	*ptr;

    /* save this for creation of children */
    main_widget = parent;

    menu = XmCreateMenuBar( parent, "Menu", NULL, 0 );

    for ( ptr=main_menu; ptr->title != NULL; ptr++ ) {

	pull = XmCreatePulldownMenu( menu, "pull", NULL, 0 );
	add_entries( pull, ptr->actions );
	button = XmCreateCascadeButton( menu, ptr->title, NULL, 0 );
	XtVaSetValues( button, XmNsubMenuId, pull, 0 );

	if ( strcmp( ptr->title, "Help" ) == 0 )
	    XtVaSetValues( menu, XmNmenuHelpWidget, button, 0 );

#if 0
	/* These screw up accelerator processing.  Punt for now */
	if ( ptr->mnemonic )
	    XtVaSetValues( button, XmNmnemonic, ptr->mnemonic, 0 );
#endif

	XtManageChild( button );
    }

    return menu;
}