OSDN Git Service

touched all tk files to ease next import
[pf3gnuchains/pf3gnuchains4x.git] / tk / mac / tkMacMenus.c
1 /* 
2  * tkMacMenus.c --
3  *
4  *      These calls set up and manage the menubar for the
5  *      Macintosh version of Tk.
6  *
7  * Copyright (c) 1995-1996 Sun Microsystems, Inc.
8  *
9  * See the file "license.terms" for information on usage and redistribution
10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11  *
12  * RCS: @(#) $Id$
13  */
14
15 #include "tcl.h"
16 #include "tclMacInt.h" /* Needed for FSpLocationFromPath */
17 #include "tk.h"
18 #include "tkInt.h"
19 #include "tkMacInt.h"
20
21 /*
22  * The define Status defined by Xlib.h conflicts with the function Status
23  * defined by Devices.h.  We undefine it here to compile.
24  */
25 #undef Status
26 #include <Devices.h>
27 #include <Menus.h>
28 #include <Memory.h>
29 #include <SegLoad.h>
30 #include <StandardFile.h>
31 #include <ToolUtils.h>
32 #include <Balloons.h>
33
34 #define kAppleMenu              256
35 #define kAppleAboutItem         1
36 #define kFileMenu               2
37 #define kEditMenu               3
38
39 #define kSourceItem             1
40 #define kCloseItem              2
41 #define kQuitItem               4
42
43 #define EDIT_CUT                1
44 #define EDIT_COPY               2
45 #define EDIT_PASTE              3
46 #define EDIT_CLEAR              4
47
48 MenuHandle tkAppleMenu;
49 MenuHandle tkFileMenu;
50 MenuHandle tkEditMenu;
51
52 static Tcl_Interp *     gInterp;        /* Interpreter for this application. */
53
54 static void GenerateEditEvent _ANSI_ARGS_((int flag));
55 static void SourceDialog _ANSI_ARGS_((void));
56 \f
57 /*
58  *----------------------------------------------------------------------
59  *
60  * TkMacHandleMenuSelect --
61  *
62  *      Handles events that occur in the Menu bar.
63  *
64  * Results:
65  *      None.
66  *
67  * Side effects:
68  *      None.
69  *
70  *----------------------------------------------------------------------
71  */
72
73 void 
74 TkMacHandleMenuSelect(
75     long mResult,
76     int optionKeyPressed)
77 {
78     short theItem = LoWord(mResult);
79     short theMenu = HiWord(mResult);
80     Str255 name;
81     Tk_Window tkwin;
82     Window window;
83     TkDisplay *dispPtr;
84
85     if (mResult == 0) {
86         TkMacHandleTearoffMenu();
87         TkMacClearMenubarActive();
88         return;
89     }
90
91     switch (theMenu) {
92         
93         case kAppleMenu:
94             switch (theItem) {
95                 case kAppleAboutItem:
96                     {
97                         Tcl_CmdInfo dummy;
98                         
99                         if (optionKeyPressed || gInterp == NULL ||
100                             Tcl_GetCommandInfo(gInterp,
101                                     "tkAboutDialog", &dummy) == 0) {
102                             TkAboutDlg();
103                         } else {
104                             Tcl_Eval(gInterp, "tkAboutDialog");
105                         }
106                         break;
107                     }
108                 default:
109                     GetItem(tkAppleMenu, theItem, name);
110                     HiliteMenu(0);
111                     OpenDeskAcc(name);
112                     return;
113             }
114             break;
115         case kFileMenu:
116             switch (theItem) {
117                 case kSourceItem:
118                     /* TODO: source script */
119                     SourceDialog();
120                     break;
121                 case kCloseItem:
122                     /* Send close event */
123                     if (TkMacHaveAppearance() >= 0x110) {
124                         window = TkMacGetXWindow(FrontNonFloatingWindow());
125                     } else {
126                         window = TkMacGetXWindow(FrontWindow());
127                     }
128                     dispPtr = TkGetDisplayList();
129                     tkwin = Tk_IdToWindow(dispPtr->display, window);
130                     TkGenWMDestroyEvent(tkwin);
131                     break;
132                 case kQuitItem:
133                     /* Exit */
134                     if (optionKeyPressed || gInterp == NULL) {
135                         Tcl_Exit(0);
136                     } else {
137                         Tcl_Eval(gInterp, "exit");
138                     }
139                     break;
140             }
141             break;
142         case kEditMenu:
143             /*
144              * This implementation just send keysyms
145              * the Tk thinks are associated with function keys that
146              * do Cut, Copy & Paste on a Sun keyboard.
147              */
148             GenerateEditEvent(theItem);
149             break;
150         default:
151             TkMacDispatchMenuEvent(theMenu, theItem);
152             TkMacClearMenubarActive();
153             break;
154     }
155
156     /*
157      * Finally we unhighlight the menu.
158      */
159     HiliteMenu(0);
160 } /* TkMacHandleMenuSelect */
161 \f
162 /*
163  *----------------------------------------------------------------------
164  *
165  * TkMacInitMenus --
166  *
167  *      This procedure initializes the Macintosh menu bar.
168  *
169  * Results:
170  *      None.
171  *
172  * Side effects:
173  *      None.
174  *
175  *----------------------------------------------------------------------
176  */
177
178 void 
179 TkMacInitMenus(
180     Tcl_Interp  *interp)
181 {
182     gInterp = interp;
183
184     /* 
185      * At this point, InitMenus() should have already been called. 
186      */
187
188     if (TkMacUseMenuID(256) != TCL_OK) {
189         panic("Menu ID 256 is already in use!");
190     }
191     tkAppleMenu = NewMenu(256, "\p\024");
192     if (tkAppleMenu == NULL) {
193         panic("memory - menus");
194     }
195     InsertMenu(tkAppleMenu, 0);
196     AppendMenu(tkAppleMenu, "\pAbout Tcl & TkÉ");
197     AppendMenu(tkAppleMenu, "\p(-");
198     AddResMenu(tkAppleMenu, 'DRVR');
199
200     if (TkMacUseMenuID(kFileMenu) != TCL_OK) {
201         panic("Menu ID %d is already in use!", kFileMenu);
202     }
203     tkFileMenu = NewMenu(kFileMenu, "\pFile");
204     if (tkFileMenu == NULL) {
205         panic("memory - menus");
206     }
207     InsertMenu(tkFileMenu, 0);
208     AppendMenu(tkFileMenu, "\pSourceÉ");
209     AppendMenu(tkFileMenu, "\pClose/W");
210     AppendMenu(tkFileMenu, "\p(-");
211     AppendMenu(tkFileMenu, "\pQuit/Q");
212
213     if (TkMacUseMenuID(kEditMenu) != TCL_OK) {
214         panic("Menu ID %d is already in use!", kEditMenu);
215     }
216     tkEditMenu = NewMenu(kEditMenu, "\pEdit");
217     if (tkEditMenu == NULL) {
218         panic("memory - menus");
219     }
220     InsertMenu(tkEditMenu, 0);
221     AppendMenu(tkEditMenu, "\pCut/X");
222     AppendMenu(tkEditMenu, "\pCopy/C");
223     AppendMenu(tkEditMenu, "\pPaste/V");
224     AppendMenu(tkEditMenu, "\pClear");
225     if (TkMacUseMenuID(kHMHelpMenuID) != TCL_OK) {
226         panic("Help menu ID %s is already in use!", kHMHelpMenuID);
227     }
228     
229     DrawMenuBar();
230     TkMacSetHelpMenuItemCount();
231     
232     return;
233 }
234 \f
235 /*
236  *----------------------------------------------------------------------
237  *
238  * GenerateEditEvent --
239  *
240  *      Takes an edit menu item and posts the corasponding a virtual 
241  *      event to Tk's event queue.
242  *
243  * Results:
244  *      None.
245  *
246  * Side effects:
247  *      May place events of queue.
248  *
249  *----------------------------------------------------------------------
250  */
251
252 static void 
253 GenerateEditEvent(
254     int flag)
255 {
256     XVirtualEvent event;
257     Point where;
258     Tk_Window tkwin;
259     Window window;
260     TkDisplay *dispPtr;
261
262     if (TkMacHaveAppearance() >= 0x110) {
263         window = TkMacGetXWindow(FrontNonFloatingWindow());
264     } else {
265         window = TkMacGetXWindow(FrontWindow());
266     }
267     dispPtr = TkGetDisplayList();
268     tkwin = Tk_IdToWindow(dispPtr->display, window);
269     tkwin = (Tk_Window) ((TkWindow *) tkwin)->dispPtr->focusPtr;
270     if (tkwin == NULL) {
271         return;
272     }
273
274     event.type = VirtualEvent;
275     event.serial = Tk_Display(tkwin)->request;
276     event.send_event = false;
277     event.display = Tk_Display(tkwin);
278     event.event = Tk_WindowId(tkwin);
279     event.root = XRootWindow(Tk_Display(tkwin), 0);
280     event.subwindow = None;
281     event.time = TkpGetMS();
282     
283     GetMouse(&where);
284     tkwin = Tk_TopCoordsToWindow(tkwin, where.h, where.v, 
285             &event.x, &event.y);
286     LocalToGlobal(&where);
287     event.x_root = where.h;
288     event.y_root = where.v;
289     event.state = TkMacButtonKeyState();
290     event.same_screen = true;
291     
292     switch (flag) {
293         case EDIT_CUT:
294             event.name = Tk_GetUid("Cut");
295             break;
296             
297         case EDIT_COPY:
298             event.name = Tk_GetUid("Copy");
299             break;
300             
301         case EDIT_PASTE:
302             event.name = Tk_GetUid("Paste");
303             break;
304             
305         case EDIT_CLEAR:
306             event.name = Tk_GetUid("Clear");
307             break;
308     }
309     Tk_QueueWindowEvent((XEvent *) &event, TCL_QUEUE_TAIL);
310 }
311 \f
312 /*
313  *----------------------------------------------------------------------
314  *
315  * SourceDialog --
316  *
317  *      Presents a dialog to the user for selecting a Tcl file.  The
318  *      selected file will be sourced into the main interpreter.
319  *
320  * Results:
321  *      None.
322  *
323  * Side effects:
324  *      None.
325  *
326  *----------------------------------------------------------------------
327  */
328
329 static void 
330 SourceDialog()
331 {
332     int result;
333     char *path;
334     char openCmd[] = "tk_getOpenFile -filetypes {\
335             {{TCL Scripts} {.tcl} TEXT} {{Text Files} {} TEXT}}";
336     
337     if (gInterp == NULL) {
338         return;
339     }
340     
341     if (Tcl_Eval(gInterp, openCmd) != TCL_OK) {
342         return;
343     }
344     
345     path = Tcl_GetStringResult(gInterp);
346     
347     if (strlen(path) == 0) {
348         return;
349     }
350     
351     result = Tcl_EvalFile(gInterp, path);
352     if (result == TCL_ERROR) {
353         Tcl_BackgroundError(gInterp);
354     }      
355 }
356
357