3 * Rob Zimmermann. All rights reserved.
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: m_menu.c,v 8.23 1996/12/18 10:26:33 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:26:33 $";
16 #include <sys/queue.h>
18 #include <X11/Intrinsic.h>
19 #include <X11/StringDefs.h>
21 #include <Xm/CascadeB.h>
22 #include <Xm/RowColumn.h>
23 #include <Xm/Separator.h>
24 #include <Xm/FileSB.h>
25 #include <Xm/SelectioB.h>
27 #include <bitstring.h>
30 #include "../common/common.h"
31 #include "../ipc/ip.h"
34 /* save this for creation of children */
35 static Widget main_widget
= NULL
;
37 /* This module defines the menu structure for vi. Each menu
38 * item has an action routine associated with it. For the most
39 * part, those actions will simply call vi_send with vi commands.
40 * others will pop up file selection dialogs and use them for
41 * vi commands, and other will have to have special actions.
44 * vi core will have to let us know when to be sensitive
45 * change VI_STRING to VI_COMMAND so that menu actions cannot
46 * be confusing when in insert mode
47 * need VI_CUT, VI_COPY, and VI_PASTE to perform the appropriate
48 * operations on the visible text of yank buffer. VI_COPY
49 * is likely a NOP, but it will make users happy
52 * implement file selection dialog boxes
53 * implement string prompt dialog boxes (e.g. for 'find')
56 * Widget create_menubar( Widget parent ) creates and returns the
57 * X menu structure. The caller can place this
58 * anywhere in the widget heirarchy.
61 #define BufferSize 1024
64 * __vi_send_command_string --
65 * Utility: Send a menu command to vi
68 * Change VI_STRING to VI_COMMAND so that menu actions cannot be confusing
69 * when in insert mode.
72 * THIS SHOULD GO AWAY -- WE SHOULDN'T SEND UNINTERPRETED STRINGS TO THE
75 * PUBLIC: void __vi_send_command_string __P((String));
78 __vi_send_command_string(str
)
82 char buffer
[BufferSize
];
84 /* Future: Need VI_COMMAND so vi knows this is not text to insert
85 * At that point, appending a cr/lf will not be necessary. For now,
86 * append iff we are a colon or slash command. Of course, if we are in
87 * insert mode, all bets are off.
89 strcpy( buffer
, str
);
93 strcat( buffer
, "\n" );
99 ipb
.len1
= strlen(buffer
);
104 /* Utility: beep for unimplemented command */
106 #if defined(__STDC__)
107 static void send_beep( Widget w
)
109 static void send_beep( w
)
113 XBell( XtDisplay(w
), 0 );
119 * Utility: make a dialog box go Modal
121 * PUBLIC: void __vi_cancel_cb __P((Widget, XtPointer, XtPointer));
123 static Bool have_answer
;
125 __vi_cancel_cb(w
, client_data
, call_data
)
127 XtPointer client_data
, call_data
;
133 * PUBLIC: void __vi_modal_dialog __P((Widget));
136 __vi_modal_dialog( db
)
141 /* post the dialog */
143 XtPopup( XtParent(db
), XtGrabExclusive
);
145 /* wait for a response */
146 ctx
= XtWidgetToApplicationContext(db
);
147 XtAddGrab( XtParent(db
), TRUE
, FALSE
);
148 for ( have_answer
= False
; ! have_answer
; )
149 XtAppProcessEvent( ctx
, XtIMAll
);
152 XtPopdown( XtParent(db
) );
153 XtRemoveGrab( XtParent(db
) );
157 /* Utility: Get a file (using standard File Selection Dialog Box) */
159 static String file_name
;
162 #if defined(__STDC__)
163 static void ok_file_name( Widget w
,
164 XtPointer client_data
,
168 static void ok_file_name( w
, client_data
, call_data
)
170 XtPointer client_data
;
174 XmFileSelectionBoxCallbackStruct
*cbs
;
176 cbs
= (XmFileSelectionBoxCallbackStruct
*) call_data
;
177 XmStringGetLtoR( cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &file_name
);
183 #if defined(__STDC__)
184 static String
get_file( Widget w
, String prompt
)
186 static String
get_file( w
, prompt
)
191 /* make it static so we can reuse it */
194 /* our return parameter */
195 if ( file_name
!= NULL
) {
202 db
= XmCreateFileSelectionDialog( main_widget
, "file", NULL
, 0 );
203 XtAddCallback( db
, XmNokCallback
, ok_file_name
, NULL
);
204 XtAddCallback( db
, XmNcancelCallback
, __vi_cancel_cb
, NULL
);
207 /* use the title as a prompt */
208 XtVaSetValues( XtParent(db
), XmNtitle
, prompt
, 0 );
210 /* wait for a response */
211 __vi_modal_dialog( db
);
220 * Get a file name and send it with the command to the core.
223 file_command(w
, code
, prompt
)
231 if ((file
= get_file(w
, prompt
)) != NULL
) {
234 ipb
.len1
= strlen(file
);
241 * Menu action routines (one per menu entry)
243 * These are in the order in which they appear in the menu structure.
246 ma_edit_file(w
, call_data
, client_data
)
248 XtPointer call_data
, client_data
;
250 file_command(w
, VI_EDIT
, "Edit");
254 ma_split(w
, call_data
, client_data
)
256 XtPointer call_data
, client_data
;
258 file_command(w
, VI_EDITSPLIT
, "Edit");
262 ma_save(w
, call_data
, client_data
)
264 XtPointer call_data
, client_data
;
269 (void)vi_send(NULL
, &ipb
);
273 ma_save_as(w
, call_data
, client_data
)
275 XtPointer call_data
, client_data
;
277 file_command(w
, VI_WRITEAS
, "Save As");
281 ma_wq(w
, call_data
, client_data
)
283 XtPointer call_data
, client_data
;
288 (void)vi_send(NULL
, &ipb
);
292 ma_quit(w
, call_data
, client_data
)
294 XtPointer call_data
, client_data
;
299 (void)vi_send(NULL
, &ipb
);
303 ma_undo(w
, call_data
, client_data
)
305 XtPointer call_data
, client_data
;
310 (void)vi_send(NULL
, &ipb
);
313 #if defined(__STDC__)
314 static void ma_cut( Widget w
,
316 XtPointer client_data
319 static void ma_cut( w
, call_data
, client_data
)
322 XtPointer client_data
;
330 #if defined(__STDC__)
331 static void ma_copy( Widget w
,
333 XtPointer client_data
336 static void ma_copy( w
, call_data
, client_data
)
339 XtPointer client_data
;
347 #if defined(__STDC__)
348 static void ma_paste( Widget w
,
350 XtPointer client_data
353 static void ma_paste( w
, call_data
, client_data
)
356 XtPointer client_data
;
364 ma_find(w
, call_data
, client_data
)
366 XtPointer call_data
, client_data
;
368 __vi_show_search_dialog( main_widget
, "Find" );
372 ma_find_next(w
, call_data
, client_data
)
374 XtPointer call_data
, client_data
;
380 ma_tags(w
, call_data
, client_data
)
382 XtPointer call_data
, client_data
;
384 __vi_show_tags_dialog( main_widget
, "Tag Stack" );
388 ma_tagpop(w
, call_data
, client_data
)
390 XtPointer call_data
, client_data
;
392 __vi_send_command_string( "\024" );
396 ma_tagtop(w
, call_data
, client_data
)
398 XtPointer call_data
, client_data
;
400 __vi_send_command_string( ":tagtop" );
403 #if defined(__STDC__)
404 static void ma_preferences( Widget w
,
406 XtPointer client_data
409 static void ma_preferences( w
, call_data
, client_data
)
412 XtPointer client_data
;
415 __vi_show_options_dialog( main_widget
, "Preferences" );
419 /* Menu construction routines */
424 String accel
; /* for Motif */
425 String accel_text
; /* for the user */
434 static pull_down file_menu
[] = {
435 { "Edit File...", ma_edit_file
, "Alt<Key>e", "Alt+E" },
436 { "", NULL
, NULL
, NULL
},
437 { "Split Window...", ma_split
, NULL
, NULL
},
438 { "", NULL
, NULL
, NULL
},
439 { "Save ", ma_save
, "Alt<Key>s", "Alt+S" },
440 { "Save As...", ma_save_as
, "Shift Alt<Key>s", "Shift+Alt+S" },
441 { "", NULL
, NULL
, NULL
},
442 { "Write and Quit", ma_wq
, "Shift Alt<Key>q", "Shift+Alt+Q" },
443 { "Quit", ma_quit
, "Alt<Key>q", "Alt+Q" },
444 { NULL
, NULL
, NULL
, NULL
},
447 static pull_down edit_menu
[] = {
448 { "Undo", ma_undo
, NULL
, NULL
},
449 { "", NULL
, NULL
, NULL
},
450 { "Cut", ma_cut
, "Alt<Key>x", "Alt+X" },
451 { "Copy", ma_copy
, "Alt<Key>c", "Alt+C" },
452 { "Paste", ma_paste
, "Alt<Key>v", "Alt+V" },
453 { "", NULL
, NULL
, NULL
},
454 { "Find", ma_find
, "Alt<Key>f", "Alt+F" },
455 { "Find Next", ma_find_next
, "Alt<Key>g", "Alt+G" },
456 { NULL
, NULL
, NULL
, NULL
},
459 static pull_down options_menu
[] = {
460 { "Preferences", ma_preferences
, NULL
, NULL
},
461 { "Command Mode Maps", NULL
, NULL
, NULL
},
462 { "Insert Mode Maps", NULL
, NULL
, NULL
},
463 { NULL
, NULL
, NULL
, NULL
},
466 static pull_down tag_menu
[] = {
467 { "Show Tag Stack", ma_tags
, "Alt<Key>t", "Alt+T" },
468 { "", NULL
, NULL
, NULL
},
469 { "Pop Tag", ma_tagpop
, NULL
, NULL
},
470 { "Clear Stack", ma_tagtop
, NULL
, NULL
},
471 { NULL
, NULL
, NULL
, NULL
},
474 static pull_down help_menu
[] = {
475 { NULL
, NULL
, NULL
, NULL
},
478 static menu_bar main_menu
[] = {
479 { 'F', "File", file_menu
},
480 { 'E', "Edit", edit_menu
},
481 { 'O', "Options", options_menu
},
482 { 'T', "Tag", tag_menu
},
483 { 'H', "Help", help_menu
},
488 #if defined(__STDC__)
489 static void add_entries( Widget parent
, pull_down
*actions
)
491 static void add_entries( parent
, actions
)
499 for ( ; actions
->title
!= NULL
; actions
++ ) {
502 if ( *actions
->title
!= '\0' ) {
503 w
= XmCreatePushButton( parent
, actions
->title
, NULL
, 0 );
504 if ( actions
->action
== NULL
)
505 XtSetSensitive( w
, False
);
509 (XtCallbackProc
) actions
->action
,
512 if ( actions
->accel
!= NULL
) {
513 str
= XmStringCreateSimple( actions
->accel_text
);
515 XmNaccelerator
, actions
->accel
,
516 XmNacceleratorText
, str
,
523 w
= XmCreateSeparator( parent
, "separator", NULL
, 0 );
532 * vi_create_menubar --
534 * PUBLIC: Widget vi_create_menubar __P((Widget));
537 vi_create_menubar(parent
)
540 Widget menu
, pull
, button
;
543 /* save this for creation of children */
544 main_widget
= parent
;
546 menu
= XmCreateMenuBar( parent
, "Menu", NULL
, 0 );
548 for ( ptr
=main_menu
; ptr
->title
!= NULL
; ptr
++ ) {
550 pull
= XmCreatePulldownMenu( menu
, "pull", NULL
, 0 );
551 add_entries( pull
, ptr
->actions
);
552 button
= XmCreateCascadeButton( menu
, ptr
->title
, NULL
, 0 );
553 XtVaSetValues( button
, XmNsubMenuId
, pull
, 0 );
555 if ( strcmp( ptr
->title
, "Help" ) == 0 )
556 XtVaSetValues( menu
, XmNmenuHelpWidget
, button
, 0 );
559 /* These screw up accelerator processing. Punt for now */
561 XtVaSetValues( button
, XmNmnemonic
, ptr
->mnemonic
, 0 );
564 XtManageChild( button
);