blob: 3dfb939e29d1e5b47e96bde602bfe31db2df3db5 [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 * Macintosh port by Dany St-Amant
6 * and Axel Kielhorn
Bram Moolenaar79ee3152007-04-26 16:20:50 +00007 * Port to MPW by Bernhard Pruemmer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 * Initial Carbon port by Ammon Skidmore
9 *
10 * Do ":help uganda" in Vim to read copying and usage conditions.
11 * Do ":help credits" in Vim to see a list of people who contributed.
12 * See README.txt for an overview of the Vim source code.
13 */
14
15/*
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +000016 * NOTES: - Vim 7+ does not support classic MacOS. Please use Vim 6.x
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000017 * - Comments mentioning FAQ refer to the book:
18 * "Macworld Mac Programming FAQs" from "IDG Books"
Bram Moolenaar071d4272004-06-13 20:20:40 +000019 */
20
21/*
22 * TODO: Change still to merge from the macvim's iDisk
23 *
24 * error_ga, mch_errmsg, Navigation's changes in gui_mch_browse
25 * uses of MenuItemIndex, changes in gui_mch_set_shellsize,
26 * ScrapManager error handling.
27 * Comments about function remaining to Carbonize.
28 *
29 */
30
Bram Moolenaar734a8672019-12-02 22:49:38 +010031/*
32 * TODO (Jussi)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +000033 * * Clipboard does not work (at least some cases)
34 * * ATSU font rendering has some problems
35 * * Investigate and remove dead code (there is still lots of that)
36 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar734a8672019-12-02 22:49:38 +010038#include <Devices.h> // included first to avoid CR problems
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#include "vim.h"
40
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +000041#define USE_CARBONIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +010042#define USE_AEVENT // Enable AEVENT
43#undef USE_OFFSETED_WINDOW // Debugging feature: start Vim window OFFSETed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaar734a8672019-12-02 22:49:38 +010045// Compile as CodeWarrior External Editor
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#if defined(FEAT_CW_EDITOR) && !defined(USE_AEVENT)
Bram Moolenaar734a8672019-12-02 22:49:38 +010047# define USE_AEVENT // Need Apple Event Support
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#endif
49
Bram Moolenaar734a8672019-12-02 22:49:38 +010050// Vim's Scrap flavor.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000051#define VIMSCRAPFLAVOR 'VIM!'
Bram Moolenaar13505972019-01-24 15:04:48 +010052#define SCRAPTEXTFLAVOR kScrapFlavorTypeUnicode
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000053
Bram Moolenaar071d4272004-06-13 20:20:40 +000054static EventHandlerUPP mouseWheelHandlerUPP = NULL;
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +000055SInt32 gMacSystemVersion;
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +000057#ifdef MACOS_CONVERT
58# define USE_CARBONKEYHANDLER
Bram Moolenaar1b60e502008-03-12 13:40:54 +000059
60static int im_is_active = FALSE;
Bram Moolenaar259f26a2018-05-15 22:25:40 +020061# if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +010062 // TODO: Implement me!
Bram Moolenaar1b60e502008-03-12 13:40:54 +000063static int im_start_row = 0;
64static int im_start_col = 0;
Bram Moolenaar259f26a2018-05-15 22:25:40 +020065# endif
Bram Moolenaar1b60e502008-03-12 13:40:54 +000066
Bram Moolenaar259f26a2018-05-15 22:25:40 +020067# define NR_ELEMS(x) (sizeof(x) / sizeof(x[0]))
Bram Moolenaar1b60e502008-03-12 13:40:54 +000068
69static TSMDocumentID gTSMDocument;
70
71static void im_on_window_switch(int active);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000072static EventHandlerUPP keyEventHandlerUPP = NULL;
Bram Moolenaar1b60e502008-03-12 13:40:54 +000073static EventHandlerUPP winEventHandlerUPP = NULL;
74
75static pascal OSStatus gui_mac_handle_window_activate(
76 EventHandlerCallRef nextHandler, EventRef theEvent, void *data);
77
78static pascal OSStatus gui_mac_handle_text_input(
79 EventHandlerCallRef nextHandler, EventRef theEvent, void *data);
80
81static pascal OSStatus gui_mac_update_input_area(
82 EventHandlerCallRef nextHandler, EventRef theEvent);
83
84static pascal OSStatus gui_mac_unicode_key_event(
85 EventHandlerCallRef nextHandler, EventRef theEvent);
86
Bram Moolenaar26a60b42005-02-22 08:49:11 +000087#endif
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
Bram Moolenaar734a8672019-12-02 22:49:38 +010090// Include some file. TODO: move into os_mac.h
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#include <Menus.h>
92#include <Resources.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#include <Processes.h>
94#ifdef USE_AEVENT
95# include <AppleEvents.h>
96# include <AERegistry.h>
97#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000098# include <Gestalt.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#if UNIVERSAL_INTERFACES_VERSION >= 0x0330
100# include <ControlDefinitions.h>
Bram Moolenaar734a8672019-12-02 22:49:38 +0100101# include <Navigation.h> // Navigation only part of ??
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
103
Bram Moolenaar734a8672019-12-02 22:49:38 +0100104// Help Manager (balloon.h, HM prefixed functions) are not supported
105// under Carbon (Jussi)
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000106# if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +0100107// New Help Interface for Mac, not implemented yet.
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000108# include <MacHelp.h>
109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111/*
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000112 * These seem to be rectangle options. Why are they not found in
113 * headers? (Jussi)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#define kNothing 0
Bram Moolenaar734a8672019-12-02 22:49:38 +0100116#define kCreateEmpty 2 //1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#define kCreateRect 2
118#define kDestroy 3
119
120/*
121 * Dany: Don't like those...
122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#define topLeft(r) (((Point*)&(r))[0])
124#define botRight(r) (((Point*)&(r))[1])
125
126
Bram Moolenaar734a8672019-12-02 22:49:38 +0100127// Time of last mouse click, to detect double-click
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static long lastMouseTick = 0;
129
Bram Moolenaar734a8672019-12-02 22:49:38 +0100130// ???
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131static RgnHandle cursorRgn;
132static RgnHandle dragRgn;
133static Rect dragRect;
134static short dragRectEnbl;
135static short dragRectControl;
136
Bram Moolenaar734a8672019-12-02 22:49:38 +0100137// This variable is set when waiting for an event, which is the only moment
138// scrollbar dragging can be done directly. It's not allowed while commands
139// are executed, because it may move the cursor and that may cause unexpected
140// problems (e.g., while ":s" is working).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static int allow_scrollbar = FALSE;
142
Bram Moolenaar734a8672019-12-02 22:49:38 +0100143// Last mouse click caused contextual menu, (to provide proper release)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static short clickIsPopup;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar734a8672019-12-02 22:49:38 +0100146// Feedback Action for Scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147ControlActionUPP gScrollAction;
148ControlActionUPP gScrollDrag;
149
Bram Moolenaar734a8672019-12-02 22:49:38 +0100150// Keeping track of which scrollbar is being dragged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static ControlHandle dragged_sb = NULL;
152
Bram Moolenaar734a8672019-12-02 22:49:38 +0100153// Vector of char_u --> control index for hotkeys in dialogs
Bram Moolenaarc52da9d2008-03-20 13:39:37 +0000154static short *gDialogHotKeys;
155
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000156static struct
157{
158 FMFontFamily family;
159 FMFontSize size;
160 FMFontStyle style;
161 Boolean isPanelVisible;
162} gFontPanelInfo = { 0, 0, 0, false };
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000163
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000164#ifdef MACOS_CONVERT
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000165# define USE_ATSUI_DRAWING
Bram Moolenaar1b60e502008-03-12 13:40:54 +0000166int p_macatsui_last;
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000167ATSUStyle gFontStyle;
Bram Moolenaar1b60e502008-03-12 13:40:54 +0000168ATSUStyle gWideFontStyle;
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000169Boolean gIsFontFallbackSet;
Bram Moolenaar76b96fc2010-07-17 16:44:59 +0200170UInt32 useAntialias_cached = 0x0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000171#endif
172
Bram Moolenaar734a8672019-12-02 22:49:38 +0100173// Colors Macros
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#define RGB(r,g,b) ((r) << 16) + ((g) << 8) + (b)
175#define Red(c) ((c & 0x00FF0000) >> 16)
176#define Green(c) ((c & 0x0000FF00) >> 8)
177#define Blue(c) ((c & 0x000000FF) >> 0)
178
Bram Moolenaar734a8672019-12-02 22:49:38 +0100179// Key mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar734a8672019-12-02 22:49:38 +0100181#define vk_Esc 0x35 // -> 1B
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar734a8672019-12-02 22:49:38 +0100183#define vk_F1 0x7A // -> 10
184#define vk_F2 0x78 //0x63
185#define vk_F3 0x63 //0x76
186#define vk_F4 0x76 //0x60
187#define vk_F5 0x60 //0x61
188#define vk_F6 0x61 //0x62
189#define vk_F7 0x62 //0x63 ?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#define vk_F8 0x64
191#define vk_F9 0x65
192#define vk_F10 0x6D
193#define vk_F11 0x67
194#define vk_F12 0x6F
195#define vk_F13 0x69
196#define vk_F14 0x6B
197#define vk_F15 0x71
198
Bram Moolenaar734a8672019-12-02 22:49:38 +0100199#define vk_Clr 0x47 // -> 1B (ESC)
200#define vk_Enter 0x4C // -> 03
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar734a8672019-12-02 22:49:38 +0100202#define vk_Space 0x31 // -> 20
203#define vk_Tab 0x30 // -> 09
204#define vk_Return 0x24 // -> 0D
205// This is wrong for OSX, what is it for?
206#define vk_Delete 0X08 // -> 08 BackSpace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaar734a8672019-12-02 22:49:38 +0100208#define vk_Help 0x72 // -> 05
209#define vk_Home 0x73 // -> 01
210#define vk_PageUp 0x74 // -> 0D
211#define vk_FwdDelete 0x75 // -> 7F
212#define vk_End 0x77 // -> 04
213#define vk_PageDown 0x79 // -> 0C
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar734a8672019-12-02 22:49:38 +0100215#define vk_Up 0x7E // -> 1E
216#define vk_Down 0x7D // -> 1F
217#define vk_Left 0x7B // -> 1C
218#define vk_Right 0x7C // -> 1D
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220#define vk_Undo vk_F1
221#define vk_Cut vk_F2
222#define vk_Copy vk_F3
223#define vk_Paste vk_F4
224#define vk_PrintScreen vk_F13
225#define vk_SCrollLock vk_F14
226#define vk_Pause vk_F15
227#define vk_NumLock vk_Clr
228#define vk_Insert vk_Help
229
230#define KeySym char
231
232static struct
233{
234 KeySym key_sym;
235 char_u vim_code0;
236 char_u vim_code1;
237} special_keys[] =
238{
239 {vk_Up, 'k', 'u'},
240 {vk_Down, 'k', 'd'},
241 {vk_Left, 'k', 'l'},
242 {vk_Right, 'k', 'r'},
243
244 {vk_F1, 'k', '1'},
245 {vk_F2, 'k', '2'},
246 {vk_F3, 'k', '3'},
247 {vk_F4, 'k', '4'},
248 {vk_F5, 'k', '5'},
249 {vk_F6, 'k', '6'},
250 {vk_F7, 'k', '7'},
251 {vk_F8, 'k', '8'},
252 {vk_F9, 'k', '9'},
253 {vk_F10, 'k', ';'},
254
255 {vk_F11, 'F', '1'},
256 {vk_F12, 'F', '2'},
257 {vk_F13, 'F', '3'},
258 {vk_F14, 'F', '4'},
259 {vk_F15, 'F', '5'},
260
Bram Moolenaar734a8672019-12-02 22:49:38 +0100261// {XK_Help, '%', '1'},
262// {XK_Undo, '&', '8'},
263// {XK_BackSpace, 'k', 'b'},
264// {vk_Delete, 'k', 'b'},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {vk_Insert, 'k', 'I'},
266 {vk_FwdDelete, 'k', 'D'},
267 {vk_Home, 'k', 'h'},
268 {vk_End, '@', '7'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100269// {XK_Prior, 'k', 'P'},
270// {XK_Next, 'k', 'N'},
271// {XK_Print, '%', '9'},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273 {vk_PageUp, 'k', 'P'},
274 {vk_PageDown, 'k', 'N'},
275
Bram Moolenaar734a8672019-12-02 22:49:38 +0100276 // End of list marker:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {(KeySym)0, 0, 0}
278};
279
280/*
281 * ------------------------------------------------------------
282 * Forward declaration (for those needed)
283 * ------------------------------------------------------------
284 */
285
286#ifdef USE_AEVENT
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000287OSErr HandleUnusedParms(const AppleEvent *theAEvent);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288#endif
289
Bram Moolenaar79ee3152007-04-26 16:20:50 +0000290#ifdef FEAT_GUI_TABLINE
291static void initialise_tabline(void);
292static WindowRef drawer = NULL; // TODO: put into gui.h
293#endif
294
Bram Moolenaar1b60e502008-03-12 13:40:54 +0000295#ifdef USE_ATSUI_DRAWING
296static void gui_mac_set_font_attributes(GuiFont font);
Bram Moolenaar1b60e502008-03-12 13:40:54 +0000297#endif
298
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
300 * ------------------------------------------------------------
301 * Conversion Utility
302 * ------------------------------------------------------------
303 */
304
305/*
306 * C2Pascal_save
307 *
308 * Allocate memory and convert the C-String passed in
309 * into a pascal string
310 *
311 */
312
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000313 char_u *
314C2Pascal_save(char_u *Cstring)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315{
316 char_u *PascalString;
317 int len;
318
319 if (Cstring == NULL)
320 return NULL;
321
322 len = STRLEN(Cstring);
323
Bram Moolenaar734a8672019-12-02 22:49:38 +0100324 if (len > 255) // Truncate if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 len = 255;
326
327 PascalString = alloc(len + 1);
328 if (PascalString != NULL)
329 {
330 mch_memmove(PascalString + 1, Cstring, len);
331 PascalString[0] = len;
332 }
333
334 return PascalString;
335}
336
337/*
338 * C2Pascal_save_and_remove_backslash
339 *
340 * Allocate memory and convert the C-String passed in
341 * into a pascal string. Also remove the backslash at the same time
342 *
343 */
344
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000345 char_u *
346C2Pascal_save_and_remove_backslash(char_u *Cstring)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347{
348 char_u *PascalString;
349 int len;
350 char_u *p, *c;
351
352 len = STRLEN(Cstring);
353
Bram Moolenaar734a8672019-12-02 22:49:38 +0100354 if (len > 255) // Truncate if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 len = 255;
356
357 PascalString = alloc(len + 1);
358 if (PascalString != NULL)
359 {
360 for (c = Cstring, p = PascalString+1, len = 0; (*c != 0) && (len < 255); c++)
361 {
362 if ((*c == '\\') && (c[1] != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 c++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 *p = *c;
365 p++;
366 len++;
367 }
368 PascalString[0] = len;
369 }
370
371 return PascalString;
372}
373
374/*
375 * Convert the modifiers of an Event into vim's modifiers (mouse)
376 */
377
378 int_u
379EventModifiers2VimMouseModifiers(EventModifiers macModifiers)
380{
381 int_u vimModifiers = 0x00;
382
383 if (macModifiers & (shiftKey | rightShiftKey))
384 vimModifiers |= MOUSE_SHIFT;
385 if (macModifiers & (controlKey | rightControlKey))
386 vimModifiers |= MOUSE_CTRL;
387 if (macModifiers & (optionKey | rightOptionKey))
388 vimModifiers |= MOUSE_ALT;
389#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +0100390 // Not yet supported
391 if (macModifiers & (cmdKey)) // There's no rightCmdKey
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 vimModifiers |= MOUSE_CMD;
393#endif
394 return (vimModifiers);
395}
396
397/*
398 * Convert the modifiers of an Event into vim's modifiers (keys)
399 */
400
401 static int_u
402EventModifiers2VimModifiers(EventModifiers macModifiers)
403{
404 int_u vimModifiers = 0x00;
405
406 if (macModifiers & (shiftKey | rightShiftKey))
407 vimModifiers |= MOD_MASK_SHIFT;
408 if (macModifiers & (controlKey | rightControlKey))
409 vimModifiers |= MOD_MASK_CTRL;
410 if (macModifiers & (optionKey | rightOptionKey))
411 vimModifiers |= MOD_MASK_ALT;
412#ifdef USE_CMD_KEY
Bram Moolenaar734a8672019-12-02 22:49:38 +0100413 if (macModifiers & (cmdKey)) // There's no rightCmdKey
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 vimModifiers |= MOD_MASK_CMD;
415#endif
416 return (vimModifiers);
417}
418
Bram Moolenaar734a8672019-12-02 22:49:38 +0100419/*
420 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 * be a positive decimal number, with an optional decimal point (eg, "12", or
422 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
423 * character is stored in *end. The flag "vertical" says whether this
424 * calculation is for a vertical (height) size or a horizontal (width) one.
425 *
426 * From gui_w48.c
427 */
428 static int
429points_to_pixels(char_u *str, char_u **end, int vertical)
430{
431 int pixels;
432 int points = 0;
433 int divisor = 0;
434
435 while (*str)
436 {
437 if (*str == '.' && divisor == 0)
438 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100439 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 divisor = 1;
441 continue;
442 }
443
444 if (!isdigit(*str))
445 break;
446
447 points *= 10;
448 points += *str - '0';
449 divisor *= 10;
450
451 ++str;
452 }
453
454 if (divisor == 0)
455 divisor = 1;
456
457 pixels = points/divisor;
458 *end = str;
459 return pixels;
460}
461
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000462#ifdef MACOS_CONVERT
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000463/*
464 * Deletes all traces of any Windows-style mnemonic text (including any
465 * parentheses) from a menu item and returns the cleaned menu item title.
466 * The caller is responsible for releasing the returned string.
467 */
468 static CFStringRef
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000469menu_title_removing_mnemonic(vimmenu_T *menu)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000470{
471 CFStringRef name;
472 size_t menuTitleLen;
473 CFIndex displayLen;
474 CFRange mnemonicStart;
475 CFRange mnemonicEnd;
476 CFMutableStringRef cleanedName;
477
478 menuTitleLen = STRLEN(menu->dname);
Bram Moolenaar446cb832008-06-24 21:56:24 +0000479 name = (CFStringRef) mac_enc_to_cfstring(menu->dname, menuTitleLen);
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000480
481 if (name)
482 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100483 // Simple mnemonic-removal algorithm, assumes single parenthesized
484 // mnemonic character towards the end of the menu text
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000485 mnemonicStart = CFStringFind(name, CFSTR("("), kCFCompareBackwards);
486 displayLen = CFStringGetLength(name);
487
488 if (mnemonicStart.location != kCFNotFound
489 && (mnemonicStart.location + 2) < displayLen
490 && CFStringGetCharacterAtIndex(name,
491 mnemonicStart.location + 1) == (UniChar)menu->mnemonic)
492 {
493 if (CFStringFindWithOptions(name, CFSTR(")"),
494 CFRangeMake(mnemonicStart.location + 1,
495 displayLen - mnemonicStart.location - 1),
496 kCFCompareBackwards, &mnemonicEnd) &&
497 (mnemonicStart.location + 2) == mnemonicEnd.location)
498 {
499 cleanedName = CFStringCreateMutableCopy(NULL, 0, name);
500 if (cleanedName)
501 {
502 CFStringDelete(cleanedName,
503 CFRangeMake(mnemonicStart.location,
504 mnemonicEnd.location + 1 -
505 mnemonicStart.location));
506
507 CFRelease(name);
508 name = cleanedName;
509 }
510 }
511 }
512 }
513
514 return name;
515}
516#endif
517
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518/*
519 * Convert a list of FSSpec aliases into a list of fullpathname
520 * character strings.
521 */
522
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000523 char_u **
524new_fnames_from_AEDesc(AEDesc *theList, long *numFiles, OSErr *error)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 char_u **fnames = NULL;
527 OSErr newError;
528 long fileCount;
529 FSSpec fileToOpen;
530 long actualSize;
531 AEKeyword dummyKeyword;
532 DescType dummyType;
533
Bram Moolenaar734a8672019-12-02 22:49:38 +0100534 // Get number of files in list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 *error = AECountItems(theList, numFiles);
536 if (*error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000537 return fnames;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
Bram Moolenaar734a8672019-12-02 22:49:38 +0100539 // Allocate the pointer list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200540 fnames = ALLOC_MULT(char_u *, *numFiles);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar734a8672019-12-02 22:49:38 +0100542 // Empty out the list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 for (fileCount = 0; fileCount < *numFiles; fileCount++)
544 fnames[fileCount] = NULL;
545
Bram Moolenaar734a8672019-12-02 22:49:38 +0100546 // Scan the list of FSSpec
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 for (fileCount = 1; fileCount <= *numFiles; fileCount++)
548 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100549 // Get the alias for the nth file, convert to an FSSpec
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 newError = AEGetNthPtr(theList, fileCount, typeFSS,
551 &dummyKeyword, &dummyType,
552 (Ptr) &fileToOpen, sizeof(FSSpec), &actualSize);
553 if (newError)
554 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100555 // Caller is able to clean up
556 // TODO: Should be clean up or not? For safety.
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000557 return fnames;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 }
559
Bram Moolenaar734a8672019-12-02 22:49:38 +0100560 // Convert the FSSpec to a pathname
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000561 fnames[fileCount - 1] = FullPathFromFSSpec_save(fileToOpen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 }
563
564 return (fnames);
565}
566
567/*
568 * ------------------------------------------------------------
569 * CodeWarrior External Editor Support
570 * ------------------------------------------------------------
571 */
572#ifdef FEAT_CW_EDITOR
573
574/*
575 * Handle the Window Search event from CodeWarrior
576 *
577 * Description
578 * -----------
579 *
580 * The IDE sends the Window Search AppleEvent to the editor when it
581 * needs to know whether a particular file is open in the editor.
582 *
583 * Event Reply
584 * -----------
585 *
586 * None. Put data in the location specified in the structure received.
587 *
588 * Remarks
589 * -------
590 *
591 * When the editor receives this event, determine whether the specified
592 * file is open. If it is, return the modification date/time for that file
593 * in the appropriate location specified in the structure. If the file is
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000594 * not opened, put the value fnfErr(file not found) in that location.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 *
596 */
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598typedef struct WindowSearch WindowSearch;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100599struct WindowSearch // for handling class 'KAHL', event 'SRCH', keyDirectObject typeChar
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 FSSpec theFile; // identifies the file
602 long *theDate; // where to put the modification date/time
603};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000605 pascal OSErr
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000606Handle_KAHL_SRCH_AE(
607 const AppleEvent *theAEvent,
608 AppleEvent *theReply,
609 long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 OSErr error = noErr;
612 buf_T *buf;
613 int foundFile = false;
614 DescType typeCode;
615 WindowSearch SearchData;
616 Size actualSize;
617
618 error = AEGetParamPtr(theAEvent, keyDirectObject, typeChar, &typeCode, (Ptr) &SearchData, sizeof(WindowSearch), &actualSize);
619 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000620 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000622 error = HandleUnusedParms(theAEvent);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000624 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar29323592016-07-24 22:04:11 +0200626 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 if (buf->b_ml.ml_mfp != NULL
628 && SearchData.theFile.parID == buf->b_FSSpec.parID
629 && SearchData.theFile.name[0] == buf->b_FSSpec.name[0]
630 && STRNCMP(SearchData.theFile.name, buf->b_FSSpec.name, buf->b_FSSpec.name[0] + 1) == 0)
631 {
632 foundFile = true;
633 break;
634 }
635
636 if (foundFile == false)
637 *SearchData.theDate = fnfErr;
638 else
639 *SearchData.theDate = buf->b_mtime;
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 return error;
642};
643
644/*
645 * Handle the Modified (from IDE to Editor) event from CodeWarrior
646 *
647 * Description
648 * -----------
649 *
650 * The IDE sends this event to the external editor when it wants to
651 * know which files that are open in the editor have been modified.
652 *
653 * Parameters None.
654 * ----------
655 *
656 * Event Reply
657 * -----------
658 * The reply for this event is:
659 *
660 * keyDirectObject typeAEList required
661 * each element in the list is a structure of typeChar
662 *
663 * Remarks
664 * -------
665 *
666 * When building the reply event, include one element in the list for
667 * each open file that has been modified.
668 *
669 */
670
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671typedef struct ModificationInfo ModificationInfo;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100672struct ModificationInfo // for replying to class 'KAHL', event 'MOD ', keyDirectObject typeAEList
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 FSSpec theFile; // identifies the file
675 long theDate; // the date/time the file was last modified
676 short saved; // set this to zero when replying, unused
677};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000679 pascal OSErr
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000680Handle_KAHL_MOD_AE(
681 const AppleEvent *theAEvent,
682 AppleEvent *theReply,
683 long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 OSErr error = noErr;
686 AEDescList replyList;
687 long numFiles;
688 ModificationInfo theFile;
689 buf_T *buf;
690
691 theFile.saved = 0;
692
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000693 error = HandleUnusedParms(theAEvent);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000695 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar734a8672019-12-02 22:49:38 +0100697 // Send the reply
698// replyObject.descriptorType = typeNull;
699// replyObject.dataHandle = nil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
Bram Moolenaar734a8672019-12-02 22:49:38 +0100701// AECreateDesc(typeChar, (Ptr)&title[1], title[0], &data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 error = AECreateList(nil, 0, false, &replyList);
703 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000704 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
706#if 0
707 error = AECountItems(&replyList, &numFiles);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
Bram Moolenaar734a8672019-12-02 22:49:38 +0100709 // AEPutKeyDesc(&replyList, keyAEPnject, &aDesc)
710 // AEPutKeyPtr(&replyList, keyAEPosition, typeChar, (Ptr)&theType,
711 // sizeof(DescType))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712
Bram Moolenaar734a8672019-12-02 22:49:38 +0100713 // AEPutDesc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#endif
715
716 numFiles = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +0200717 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (buf->b_ml.ml_mfp != NULL)
719 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100720 // Add this file to the list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 theFile.theFile = buf->b_FSSpec;
722 theFile.theDate = buf->b_mtime;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100723// theFile.theDate = time(NULL) & (time_t) 0xFFFFFFF0;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000724 error = AEPutPtr(&replyList, numFiles, typeChar, (Ptr) &theFile, sizeof(theFile));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 };
726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#if 0
728 error = AECountItems(&replyList, &numFiles);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#endif
730
Bram Moolenaar734a8672019-12-02 22:49:38 +0100731 // We can add data only if something to reply
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000732 error = AEPutParamDesc(theReply, keyDirectObject, &replyList);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 if (replyList.dataHandle)
735 AEDisposeDesc(&replyList);
736
737 return error;
738};
739
740/*
741 * Handle the Get Text event from CodeWarrior
742 *
743 * Description
744 * -----------
745 *
746 * The IDE sends the Get Text AppleEvent to the editor when it needs
747 * the source code from a file. For example, when the user issues a
748 * Check Syntax or Compile command, the compiler needs access to
749 * the source code contained in the file.
750 *
751 * Event Reply
752 * -----------
753 *
754 * None. Put data in locations specified in the structure received.
755 *
756 * Remarks
757 * -------
758 *
759 * When the editor receives this event, it must set the size of the handle
760 * in theText to fit the data in the file. It must then copy the entire
761 * contents of the specified file into the memory location specified in
762 * theText.
763 *
764 */
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766typedef struct CW_GetText CW_GetText;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100767struct CW_GetText // for handling class 'KAHL', event 'GTTX', keyDirectObject typeChar
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100769 FSSpec theFile; // identifies the file
770 Handle theText; // the location where you return the text (must be resized properly)
771 long *unused; // 0 (not used)
772 long *theDate; // where to put the modification date/time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000775 pascal OSErr
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000776Handle_KAHL_GTTX_AE(
777 const AppleEvent *theAEvent,
778 AppleEvent *theReply,
779 long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
781 OSErr error = noErr;
782 buf_T *buf;
783 int foundFile = false;
784 DescType typeCode;
785 CW_GetText GetTextData;
786 Size actualSize;
787 char_u *line;
788 char_u *fullbuffer = NULL;
789 long linesize;
790 long lineStart;
791 long BufferSize;
792 long lineno;
793
794 error = AEGetParamPtr(theAEvent, keyDirectObject, typeChar, &typeCode, (Ptr) &GetTextData, sizeof(GetTextData), &actualSize);
795
796 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000797 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar29323592016-07-24 22:04:11 +0200799 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (buf->b_ml.ml_mfp != NULL)
801 if (GetTextData.theFile.parID == buf->b_FSSpec.parID)
802 {
803 foundFile = true;
804 break;
805 }
806
807 if (foundFile)
808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100809 BufferSize = 0; // GetHandleSize(GetTextData.theText);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 for (lineno = 0; lineno <= buf->b_ml.ml_line_count; lineno++)
811 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100812 // Must use the right buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 line = ml_get_buf(buf, (linenr_T) lineno, FALSE);
814 linesize = STRLEN(line) + 1;
815 lineStart = BufferSize;
816 BufferSize += linesize;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100817 // Resize handle to linesize+1 to include the linefeed
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000818 SetHandleSize(GetTextData.theText, BufferSize);
819 if (GetHandleSize(GetTextData.theText) != BufferSize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100821 break; // Simple handling for now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
823 else
824 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000825 HLock(GetTextData.theText);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 fullbuffer = (char_u *) *GetTextData.theText;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000827 STRCPY((char_u *)(fullbuffer + lineStart), line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 fullbuffer[BufferSize-1] = '\r';
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000829 HUnlock(GetTextData.theText);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831 }
832 if (fullbuffer != NULL)
833 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000834 HLock(GetTextData.theText);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 fullbuffer[BufferSize-1] = 0;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000836 HUnlock(GetTextData.theText);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 if (foundFile == false)
839 *GetTextData.theDate = fnfErr;
840 else
Bram Moolenaar734a8672019-12-02 22:49:38 +0100841// *GetTextData.theDate = time(NULL) & (time_t) 0xFFFFFFF0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *GetTextData.theDate = buf->b_mtime;
843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000845 error = HandleUnusedParms(theAEvent);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +0000847 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848}
849
850/*
851 *
852 */
853
Bram Moolenaar734a8672019-12-02 22:49:38 +0100854/*
855 * Taken from MoreAppleEvents:ProcessHelpers
856 */
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000857 pascal OSErr
858FindProcessBySignature(
859 const OSType targetType,
860 const OSType targetCreator,
861 ProcessSerialNumberPtr psnPtr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 OSErr anErr = noErr;
864 Boolean lookingForProcess = true;
865
866 ProcessInfoRec infoRec;
867
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000868 infoRec.processInfoLength = sizeof(ProcessInfoRec);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 infoRec.processName = nil;
870 infoRec.processAppSpec = nil;
871
872 psnPtr->lowLongOfPSN = kNoProcess;
873 psnPtr->highLongOfPSN = kNoProcess;
874
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000875 while (lookingForProcess)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000877 anErr = GetNextProcess(psnPtr);
878 if (anErr != noErr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 lookingForProcess = false;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else
881 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000882 anErr = GetProcessInformation(psnPtr, &infoRec);
883 if ((anErr == noErr)
884 && (infoRec.processType == targetType)
885 && (infoRec.processSignature == targetCreator))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 lookingForProcess = false;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
888 }
889
890 return anErr;
891}//end FindProcessBySignature
892
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000893 void
894Send_KAHL_MOD_AE(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000896 OSErr anErr = noErr;
897 AEDesc targetAppDesc = { typeNull, nil };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 ProcessSerialNumber psn = { kNoProcess, kNoProcess };
899 AppleEvent theReply = { typeNull, nil };
900 AESendMode sendMode;
901 AppleEvent theEvent = {typeNull, nil };
902 AEIdleUPP idleProcUPP = nil;
903 ModificationInfo ModData;
904
905
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000906 anErr = FindProcessBySignature('APPL', 'CWIE', &psn);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000907 if (anErr == noErr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000909 anErr = AECreateDesc(typeProcessSerialNumber, &psn,
910 sizeof(ProcessSerialNumber), &targetAppDesc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000912 if (anErr == noErr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
914 anErr = AECreateAppleEvent( 'KAHL', 'MOD ', &targetAppDesc,
915 kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
916 }
917
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000918 AEDisposeDesc(&targetAppDesc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaar734a8672019-12-02 22:49:38 +0100920 // Add the parms
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 ModData.theFile = buf->b_FSSpec;
922 ModData.theDate = buf->b_mtime;
923
924 if (anErr == noErr)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000925 anErr = AEPutParamPtr(&theEvent, keyDirectObject, typeChar, &ModData, sizeof(ModData));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000927 if (idleProcUPP == nil)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 sendMode = kAENoReply;
929 else
930 sendMode = kAEWaitReply;
931
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000932 if (anErr == noErr)
933 anErr = AESend(&theEvent, &theReply, sendMode, kAENormalPriority, kNoTimeOut, idleProcUPP, nil);
934 if (anErr == noErr && sendMode == kAEWaitReply)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100936// anErr = AEHGetHandlerError(&theReply);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000938 (void) AEDisposeDesc(&theReply);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940}
Bram Moolenaar734a8672019-12-02 22:49:38 +0100941#endif // FEAT_CW_EDITOR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943/*
944 * ------------------------------------------------------------
945 * Apple Event Handling procedure
946 * ------------------------------------------------------------
947 */
948#ifdef USE_AEVENT
949
950/*
951 * Handle the Unused parms of an AppleEvent
952 */
953
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000954 OSErr
955HandleUnusedParms(const AppleEvent *theAEvent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 OSErr error;
958 long actualSize;
959 DescType dummyType;
960 AEKeyword missedKeyword;
961
Bram Moolenaar734a8672019-12-02 22:49:38 +0100962 // Get the "missed keyword" attribute from the AppleEvent.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 error = AEGetAttributePtr(theAEvent, keyMissedKeywordAttr,
964 typeKeyword, &dummyType,
965 (Ptr)&missedKeyword, sizeof(missedKeyword),
966 &actualSize);
967
Bram Moolenaar734a8672019-12-02 22:49:38 +0100968 // If the descriptor isn't found, then we got the required parameters.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (error == errAEDescNotFound)
970 {
971 error = noErr;
972 }
973 else
974 {
975#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +0100976 // Why is this removed?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 error = errAEEventNotHandled;
978#endif
979 }
980
981 return error;
982}
983
984
985/*
986 * Handle the ODoc AppleEvent
987 *
988 * Deals with all files dragged to the application icon.
989 *
990 */
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992typedef struct SelectionRange SelectionRange;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100993struct SelectionRange // for handling kCoreClassEvent:kOpenDocuments:keyAEPosition typeChar
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
995 short unused1; // 0 (not used)
996 short lineNum; // line to select (<0 to specify range)
997 long startRange; // start of selection range (if line < 0)
998 long endRange; // end of selection range (if line < 0)
999 long unused2; // 0 (not used)
1000 long theDate; // modification date/time
1001};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001003static long drop_numFiles;
1004static short drop_gotPosition;
1005static SelectionRange drop_thePosition;
1006
1007 static void
1008drop_callback(void *cookie UNUSED)
1009{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001010 // TODO: Handle the goto/select line more cleanly
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001011 if ((drop_numFiles == 1) & (drop_gotPosition))
1012 {
1013 if (drop_thePosition.lineNum >= 0)
1014 {
1015 lnum = drop_thePosition.lineNum + 1;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001016 // oap->motion_type = MLINE;
1017 // setpcmark();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001018 if (lnum < 1L)
1019 lnum = 1L;
1020 else if (lnum > curbuf->b_ml.ml_line_count)
1021 lnum = curbuf->b_ml.ml_line_count;
1022 curwin->w_cursor.lnum = lnum;
1023 curwin->w_cursor.col = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001024 // beginline(BL_SOL | BL_FIX);
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001025 }
1026 else
1027 goto_byte(drop_thePosition.startRange + 1);
1028 }
1029
Bram Moolenaar734a8672019-12-02 22:49:38 +01001030 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001031 update_screen(NOT_VALID);
1032
Bram Moolenaar734a8672019-12-02 22:49:38 +01001033 // Select the text if possible
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001034 if (drop_gotPosition)
1035 {
1036 VIsual_active = TRUE;
1037 VIsual_select = FALSE;
1038 VIsual = curwin->w_cursor;
1039 if (drop_thePosition.lineNum < 0)
1040 {
1041 VIsual_mode = 'v';
1042 goto_byte(drop_thePosition.endRange);
1043 }
1044 else
1045 {
1046 VIsual_mode = 'V';
1047 VIsual.col = 0;
1048 }
1049 }
1050}
1051
Bram Moolenaar734a8672019-12-02 22:49:38 +01001052/*
1053 * The IDE uses the optional keyAEPosition parameter to tell the ed-
1054 * itor the selection range. If lineNum is zero or greater, scroll the text
1055 * to the specified line. If lineNum is less than zero, use the values in
1056 * startRange and endRange to select the specified characters. Scroll
1057 * the text to display the selection. If lineNum, startRange, and
1058 * endRange are all negative, there is no selection range specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001060 pascal OSErr
1061HandleODocAE(const AppleEvent *theAEvent, AppleEvent *theReply, long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063 /*
1064 * TODO: Clean up the code with convert the AppleEvent into
1065 * a ":args"
1066 */
1067 OSErr error = noErr;
1068// OSErr firstError = noErr;
1069// short numErrors = 0;
1070 AEDesc theList;
1071 DescType typeCode;
1072 long numFiles;
1073 // long fileCount;
1074 char_u **fnames;
1075// char_u fname[256];
1076 Size actualSize;
1077 SelectionRange thePosition;
1078 short gotPosition = false;
1079 long lnum;
1080
Bram Moolenaar734a8672019-12-02 22:49:38 +01001081 // the direct object parameter is the list of aliases to files (one or more)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 error = AEGetParamDesc(theAEvent, keyDirectObject, typeAEList, &theList);
1083 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001084 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086
1087 error = AEGetParamPtr(theAEvent, keyAEPosition, typeChar, &typeCode, (Ptr) &thePosition, sizeof(SelectionRange), &actualSize);
1088 if (error == noErr)
1089 gotPosition = true;
1090 if (error == errAEDescNotFound)
1091 error = noErr;
1092 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001093 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095/*
1096 error = AEGetParamDesc(theAEvent, keyAEPosition, typeChar, &thePosition);
1097
1098 if (^error) then
1099 {
1100 if (thePosition.lineNum >= 0)
1101 {
1102 // Goto this line
1103 }
1104 else
1105 {
1106 // Set the range char wise
1107 }
1108 }
1109 */
1110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 fnames = new_fnames_from_AEDesc(&theList, &numFiles, &error);
1113
1114 if (error)
1115 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001116 // TODO: empty fnames[] first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 vim_free(fnames);
1118 return (error);
1119 }
1120
1121 if (starting > 0)
1122 {
1123 int i;
1124 char_u *p;
Bram Moolenaar51b84362007-09-29 11:16:17 +00001125 int fnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
Bram Moolenaar734a8672019-12-02 22:49:38 +01001127 // these are the initial files dropped on the Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 for (i = 0 ; i < numFiles; i++)
1129 {
1130 if (ga_grow(&global_alist.al_ga, 1) == FAIL
1131 || (p = vim_strsave(fnames[i])) == NULL)
1132 mch_exit(2);
1133 else
1134 alist_add(&global_alist, p, 2);
Bram Moolenaar51b84362007-09-29 11:16:17 +00001135 if (fnum == -1)
1136 fnum = GARGLIST[GARGCOUNT - 1].ae_fnum;
1137 }
1138
Bram Moolenaar734a8672019-12-02 22:49:38 +01001139 // If the file name was already in the buffer list we need to switch
1140 // to it.
Bram Moolenaar51b84362007-09-29 11:16:17 +00001141 if (curbuf->b_fnum != fnum)
1142 {
1143 char_u cmd[30];
1144
1145 vim_snprintf((char *)cmd, 30, "silent %dbuffer", fnum);
1146 do_cmdline_cmd(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001148
Bram Moolenaar734a8672019-12-02 22:49:38 +01001149 // Change directory to the location of the first file.
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001150 if (GARGCOUNT > 0
1151 && vim_chdirfile(alist_name(&GARGLIST[0]), "drop") == OK)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001152 shorten_fnames(TRUE);
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 goto finished;
1155 }
1156
Bram Moolenaar734a8672019-12-02 22:49:38 +01001157 // Handle the drop, :edit to get to the file
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001158 drop_numFiles = numFiles;
1159 drop_gotPosition = gotPosition;
1160 drop_thePosition = thePosition;
1161 handle_drop(numFiles, fnames, FALSE, drop_callback, NULL);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 setcursor();
1164 out_flush();
1165
Bram Moolenaar734a8672019-12-02 22:49:38 +01001166 // Fake mouse event to wake from stall
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001167 PostEvent(mouseUp, 0);
1168
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001169finished:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001170 AEDisposeDesc(&theList); // dispose what we allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001172 error = HandleUnusedParms(theAEvent);
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001173 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174}
1175
1176/*
1177 *
1178 */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001179 pascal OSErr
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001180Handle_aevt_oapp_AE(
1181 const AppleEvent *theAEvent,
1182 AppleEvent *theReply,
1183 long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
1185 OSErr error = noErr;
1186
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001187 error = HandleUnusedParms(theAEvent);
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001188 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189}
1190
1191/*
1192 *
1193 */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001194 pascal OSErr
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001195Handle_aevt_quit_AE(
1196 const AppleEvent *theAEvent,
1197 AppleEvent *theReply,
1198 long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
1200 OSErr error = noErr;
1201
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001202 error = HandleUnusedParms(theAEvent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (error)
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001204 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
Bram Moolenaar734a8672019-12-02 22:49:38 +01001206 // Need to fake a :confirm qa
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 do_cmdline_cmd((char_u *)"confirm qa");
1208
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001209 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210}
1211
1212/*
1213 *
1214 */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001215 pascal OSErr
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001216Handle_aevt_pdoc_AE(
1217 const AppleEvent *theAEvent,
1218 AppleEvent *theReply,
1219 long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 OSErr error = noErr;
1222
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001223 error = HandleUnusedParms(theAEvent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001225 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226}
1227
1228/*
1229 * Handling of unknown AppleEvent
1230 *
1231 * (Just get rid of all the parms)
1232 */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001233 pascal OSErr
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001234Handle_unknown_AE(
1235 const AppleEvent *theAEvent,
1236 AppleEvent *theReply,
1237 long refCon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
1239 OSErr error = noErr;
1240
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001241 error = HandleUnusedParms(theAEvent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00001243 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244}
1245
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247/*
1248 * Install the various AppleEvent Handlers
1249 */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001250 OSErr
1251InstallAEHandlers(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 OSErr error;
1254
Bram Moolenaar734a8672019-12-02 22:49:38 +01001255 // install open application handler
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 error = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001257 NewAEEventHandlerUPP(Handle_aevt_oapp_AE), 0, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
Bram Moolenaar734a8672019-12-02 22:49:38 +01001261 // install quit application handler
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 error = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001263 NewAEEventHandlerUPP(Handle_aevt_quit_AE), 0, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
Bram Moolenaar734a8672019-12-02 22:49:38 +01001267 // install open document handler
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 error = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001269 NewAEEventHandlerUPP(HandleODocAE), 0, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
Bram Moolenaar734a8672019-12-02 22:49:38 +01001273 // install print document handler
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 error = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001275 NewAEEventHandlerUPP(Handle_aevt_pdoc_AE), 0, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar734a8672019-12-02 22:49:38 +01001277// Install Core Suite
1278#if 0
1279 error = AEInstallEventHandler(kAECoreSuite, kAEClone,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001280 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 error = AEInstallEventHandler(kAECoreSuite, kAEClose,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001283 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
1285 error = AEInstallEventHandler(kAECoreSuite, kAECountElements,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001286 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 error = AEInstallEventHandler(kAECoreSuite, kAECreateElement,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001289 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 error = AEInstallEventHandler(kAECoreSuite, kAEDelete,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001292 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
1294 error = AEInstallEventHandler(kAECoreSuite, kAEDoObjectsExist,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001295 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297 error = AEInstallEventHandler(kAECoreSuite, kAEGetData,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001298 NewAEEventHandlerUPP(Handle_unknown_AE), kAEGetData, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 error = AEInstallEventHandler(kAECoreSuite, kAEGetDataSize,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001301 NewAEEventHandlerUPP(Handle_unknown_AE), kAEGetDataSize, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302
1303 error = AEInstallEventHandler(kAECoreSuite, kAEGetClassInfo,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001304 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 error = AEInstallEventHandler(kAECoreSuite, kAEGetEventInfo,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001307 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
1309 error = AEInstallEventHandler(kAECoreSuite, kAEMove,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001310 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
1312 error = AEInstallEventHandler(kAECoreSuite, kAESave,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001313 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315 error = AEInstallEventHandler(kAECoreSuite, kAESetData,
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001316 NewAEEventHandlerUPP(Handle_unknown_AE), nil, false);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319#ifdef FEAT_CW_EDITOR
1320 /*
1321 * Bind codewarrior support handlers
1322 */
1323 error = AEInstallEventHandler('KAHL', 'GTTX',
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001324 NewAEEventHandlerUPP(Handle_KAHL_GTTX_AE), 0, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 error = AEInstallEventHandler('KAHL', 'SRCH',
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001328 NewAEEventHandlerUPP(Handle_KAHL_SRCH_AE), 0, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 return error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 error = AEInstallEventHandler('KAHL', 'MOD ',
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001332 NewAEEventHandlerUPP(Handle_KAHL_MOD_AE), 0, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#endif
1334
1335 return error;
1336
1337}
Bram Moolenaar734a8672019-12-02 22:49:38 +01001338#endif // USE_AEVENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001340
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001341/*
1342 * Callback function, installed by InstallFontPanelHandler(), below,
1343 * to handle Font Panel events.
1344 */
1345 static OSStatus
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001346FontPanelHandler(
1347 EventHandlerCallRef inHandlerCallRef,
1348 EventRef inEvent,
1349 void *inUserData)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001350{
1351 if (GetEventKind(inEvent) == kEventFontPanelClosed)
1352 {
1353 gFontPanelInfo.isPanelVisible = false;
1354 return noErr;
1355 }
1356
1357 if (GetEventKind(inEvent) == kEventFontSelection)
1358 {
1359 OSStatus status;
1360 FMFontFamily newFamily;
1361 FMFontSize newSize;
1362 FMFontStyle newStyle;
1363
Bram Moolenaar734a8672019-12-02 22:49:38 +01001364 // Retrieve the font family ID number.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001365 status = GetEventParameter(inEvent, kEventParamFMFontFamily,
1366 /*inDesiredType=*/typeFMFontFamily, /*outActualType=*/NULL,
1367 /*inBufferSize=*/sizeof(FMFontFamily), /*outActualSize=*/NULL,
1368 &newFamily);
1369 if (status == noErr)
1370 gFontPanelInfo.family = newFamily;
1371
Bram Moolenaar734a8672019-12-02 22:49:38 +01001372 // Retrieve the font size.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001373 status = GetEventParameter(inEvent, kEventParamFMFontSize,
1374 typeFMFontSize, NULL, sizeof(FMFontSize), NULL, &newSize);
1375 if (status == noErr)
1376 gFontPanelInfo.size = newSize;
1377
Bram Moolenaar734a8672019-12-02 22:49:38 +01001378 // Retrieve the font style (bold, etc.). Currently unused.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001379 status = GetEventParameter(inEvent, kEventParamFMFontStyle,
1380 typeFMFontStyle, NULL, sizeof(FMFontStyle), NULL, &newStyle);
1381 if (status == noErr)
1382 gFontPanelInfo.style = newStyle;
1383 }
1384 return noErr;
1385}
1386
1387
1388 static void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001389InstallFontPanelHandler(void)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001390{
1391 EventTypeSpec eventTypes[2];
1392 EventHandlerUPP handlerUPP;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001393 // EventHandlerRef handlerRef;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001394
1395 eventTypes[0].eventClass = kEventClassFont;
1396 eventTypes[0].eventKind = kEventFontSelection;
1397 eventTypes[1].eventClass = kEventClassFont;
1398 eventTypes[1].eventKind = kEventFontPanelClosed;
1399
1400 handlerUPP = NewEventHandlerUPP(FontPanelHandler);
1401
1402 InstallApplicationEventHandler(handlerUPP, /*numTypes=*/2, eventTypes,
1403 /*userData=*/NULL, /*handlerRef=*/NULL);
1404}
1405
1406
1407/*
1408 * Fill the buffer pointed to by outName with the name and size
1409 * of the font currently selected in the Font Panel.
1410 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001411#define FONT_STYLE_BUFFER_SIZE 32
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001412 static void
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001413GetFontPanelSelection(char_u *outName)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001414{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001415 Str255 buf;
1416 ByteCount fontNameLen = 0;
1417 ATSUFontID fid;
1418 char_u styleString[FONT_STYLE_BUFFER_SIZE];
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001419
1420 if (!outName)
1421 return;
1422
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001423 if (FMGetFontFamilyName(gFontPanelInfo.family, buf) == noErr)
1424 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001425 // Canonicalize localized font names
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001426 if (FMGetFontFromFontFamilyInstance(gFontPanelInfo.family,
1427 gFontPanelInfo.style, &fid, NULL) != noErr)
1428 return;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001429
Bram Moolenaar734a8672019-12-02 22:49:38 +01001430 // Request font name with Mac encoding (otherwise we could
1431 // get an unwanted utf-16 name)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001432 if (ATSUFindFontName(fid, kFontFullName, kFontMacintoshPlatform,
1433 kFontNoScriptCode, kFontNoLanguageCode,
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001434 255, (char *)outName, &fontNameLen, NULL) != noErr)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001435 return;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001436
Bram Moolenaar734a8672019-12-02 22:49:38 +01001437 // Only encode font size, because style (bold, italic, etc) is
1438 // already part of the font full name
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001439 vim_snprintf((char *)styleString, FONT_STYLE_BUFFER_SIZE, ":h%d",
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001440 gFontPanelInfo.size/*,
1441 ((gFontPanelInfo.style & bold)!=0 ? ":b" : ""),
1442 ((gFontPanelInfo.style & italic)!=0 ? ":i" : ""),
1443 ((gFontPanelInfo.style & underline)!=0 ? ":u" : "")*/);
1444
1445 if ((fontNameLen + STRLEN(styleString)) < 255)
1446 STRCPY(outName + fontNameLen, styleString);
1447 }
1448 else
1449 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001450 *outName = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001451 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001452}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001453
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455/*
1456 * ------------------------------------------------------------
1457 * Unfiled yet
1458 * ------------------------------------------------------------
1459 */
1460
1461/*
1462 * gui_mac_get_menu_item_index
1463 *
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001464 * Returns the index inside the menu where
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 */
Bram Moolenaar734a8672019-12-02 22:49:38 +01001466 short // Should we return MenuItemIndex?
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001467gui_mac_get_menu_item_index(vimmenu_T *pMenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 short index;
1470 short itemIndex = -1;
1471 vimmenu_T *pBrother;
1472
Bram Moolenaar734a8672019-12-02 22:49:38 +01001473 // Only menu without parent are the:
1474 // -menu in the menubar
1475 // -popup menu
1476 // -toolbar (guess)
1477 //
1478 // Which are not items anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (pMenu->parent)
1480 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001481 // Start from the Oldest Brother
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 pBrother = pMenu->parent->children;
1483 index = 1;
1484 while ((pBrother) && (itemIndex == -1))
1485 {
1486 if (pBrother == pMenu)
1487 itemIndex = index;
1488 index++;
1489 pBrother = pBrother->next;
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 return itemIndex;
1493}
1494
1495 static vimmenu_T *
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001496gui_mac_get_vim_menu(short menuID, short itemIndex, vimmenu_T *pMenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 short index;
1499 vimmenu_T *pChildMenu;
1500 vimmenu_T *pElder = pMenu->parent;
1501
1502
Bram Moolenaar734a8672019-12-02 22:49:38 +01001503 // Only menu without parent are the:
1504 // -menu in the menubar
1505 // -popup menu
1506 // -toolbar (guess)
1507 //
1508 // Which are not items anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
1510 if ((pElder) && (pElder->submenu_id == menuID))
1511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 for (index = 1; (index != itemIndex) && (pMenu != NULL); index++)
1513 pMenu = pMenu->next;
1514 }
1515 else
1516 {
1517 for (; pMenu != NULL; pMenu = pMenu->next)
1518 {
1519 if (pMenu->children != NULL)
1520 {
1521 pChildMenu = gui_mac_get_vim_menu
1522 (menuID, itemIndex, pMenu->children);
1523 if (pChildMenu)
1524 {
1525 pMenu = pChildMenu;
1526 break;
1527 }
1528 }
1529 }
1530 }
1531 return pMenu;
1532}
1533
1534/*
1535 * ------------------------------------------------------------
1536 * MacOS Feedback procedures
1537 * ------------------------------------------------------------
1538 */
1539 pascal
1540 void
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001541gui_mac_drag_thumb(ControlHandle theControl, short partCode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 scrollbar_T *sb;
1544 int value, dragging;
1545 ControlHandle theControlToUse;
1546 int dont_scroll_save = dont_scroll;
1547
1548 theControlToUse = dragged_sb;
1549
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001550 sb = gui_find_scrollbar((long) GetControlReference(theControlToUse));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 if (sb == NULL)
1553 return;
1554
Bram Moolenaar734a8672019-12-02 22:49:38 +01001555 // Need to find value by diff between Old Poss New Pos
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001556 value = GetControl32BitValue(theControlToUse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 dragging = (partCode != 0);
1558
Bram Moolenaar734a8672019-12-02 22:49:38 +01001559 // When "allow_scrollbar" is FALSE still need to remember the new
1560 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 dont_scroll = !allow_scrollbar;
1562 gui_drag_scrollbar(sb, value, dragging);
1563 dont_scroll = dont_scroll_save;
1564}
1565
1566 pascal
1567 void
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001568gui_mac_scroll_action(ControlHandle theControl, short partCode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001570 // TODO: have live support
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 scrollbar_T *sb, *sb_info;
1572 long data;
1573 long value;
1574 int page;
1575 int dragging = FALSE;
1576 int dont_scroll_save = dont_scroll;
1577
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001578 sb = gui_find_scrollbar((long)GetControlReference(theControl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 if (sb == NULL)
1581 return;
1582
Bram Moolenaar734a8672019-12-02 22:49:38 +01001583 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 /*
1586 * Careful: need to get scrollbar info out of first (left) scrollbar
1587 * for window, but keep real scrollbar too because we must pass it to
1588 * gui_drag_scrollbar().
1589 */
1590 sb_info = &sb->wp->w_scrollbars[0];
1591
1592 if (sb_info->size > 5)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001593 page = sb_info->size - 2; // use two lines of context
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 else
1595 page = sb_info->size;
1596 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001597 else // Bottom scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 sb_info = sb;
Bram Moolenaar02631462017-09-22 15:20:32 +02001600 page = curwin->w_width - 5;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602
1603 switch (partCode)
1604 {
1605 case kControlUpButtonPart: data = -1; break;
1606 case kControlDownButtonPart: data = 1; break;
1607 case kControlPageDownPart: data = page; break;
1608 case kControlPageUpPart: data = -page; break;
1609 default: data = 0; break;
1610 }
1611
1612 value = sb_info->value + data;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001613// if (value > sb_info->max)
1614// value = sb_info->max;
1615// else if (value < 0)
1616// value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaar734a8672019-12-02 22:49:38 +01001618 // When "allow_scrollbar" is FALSE still need to remember the new
1619 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 dont_scroll = !allow_scrollbar;
1621 gui_drag_scrollbar(sb, value, dragging);
1622 dont_scroll = dont_scroll_save;
1623
1624 out_flush();
1625 gui_mch_set_scrollbar_thumb(sb, value, sb_info->size, sb_info->max);
1626
Bram Moolenaar734a8672019-12-02 22:49:38 +01001627#if 0
1628 if (sb_info->wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 win_T *wp;
1631 int sb_num;
1632
1633 sb_num = 0;
1634 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = W_NEXT(wp))
1635 sb_num++;
1636
1637 if (wp != NULL)
1638 {
1639 current_scrollbar = sb_num;
1640 scrollbar_value = value;
1641 gui_do_scroll();
1642 gui_mch_set_scrollbar_thumb(sb, value, sb_info->size, sb_info->max);
1643 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001644 }
1645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646}
1647
1648/*
1649 * ------------------------------------------------------------
1650 * MacOS Click Handling procedures
1651 * ------------------------------------------------------------
1652 */
1653
1654
1655/*
1656 * Handle a click inside the window, it may happens in the
1657 * scrollbar or the contents.
1658 *
1659 * TODO: Add support for potential TOOLBAR
1660 */
1661 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001662gui_mac_doInContentClick(EventRecord *theEvent, WindowPtr whichWindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 Point thePoint;
1665 int_u vimModifiers;
1666 short thePortion;
1667 ControlHandle theControl;
1668 int vimMouseButton;
1669 short dblClick;
1670
1671 thePoint = theEvent->where;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001672 GlobalToLocal(&thePoint);
1673 SelectWindow(whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001675 thePortion = FindControl(thePoint, whichWindow, &theControl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 if (theControl != NUL)
1678 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001679 // We hit a scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 if (thePortion != kControlIndicatorPart)
1682 {
1683 dragged_sb = theControl;
1684 TrackControl(theControl, thePoint, gScrollAction);
1685 dragged_sb = NULL;
1686 }
1687 else
1688 {
1689 dragged_sb = theControl;
1690#if 1
1691 TrackControl(theControl, thePoint, gScrollDrag);
1692#else
1693 TrackControl(theControl, thePoint, NULL);
1694#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01001695 // pass 0 as the part to tell gui_mac_drag_thumb, that the mouse
1696 // button has been released
1697 gui_mac_drag_thumb(theControl, 0); // Should it be thePortion ? (Dany)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 dragged_sb = NULL;
1699 }
1700 }
1701 else
1702 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001703 // We are inside the contents
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar734a8672019-12-02 22:49:38 +01001705 // Convert the CTRL, OPTION, SHIFT and CMD key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 vimModifiers = EventModifiers2VimMouseModifiers(theEvent->modifiers);
1707
Bram Moolenaar734a8672019-12-02 22:49:38 +01001708 // Defaults to MOUSE_LEFT as there's only one mouse button
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 vimMouseButton = MOUSE_LEFT;
1710
Bram Moolenaar734a8672019-12-02 22:49:38 +01001711 // Convert the CTRL_MOUSE_LEFT to MOUSE_RIGHT
1712 // TODO: NEEDED?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 clickIsPopup = FALSE;
1714
Bram Moolenaare02d7b22007-06-19 14:29:43 +00001715 if (mouse_model_popup() && IsShowContextualMenuClick(theEvent))
1716 {
1717 vimMouseButton = MOUSE_RIGHT;
1718 vimModifiers &= ~MOUSE_CTRL;
1719 clickIsPopup = TRUE;
1720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar734a8672019-12-02 22:49:38 +01001722 // Is it a double click ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 dblClick = ((theEvent->when - lastMouseTick) < GetDblTime());
1724
Bram Moolenaar734a8672019-12-02 22:49:38 +01001725 // Send the mouse click to Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 gui_send_mouse_event(vimMouseButton, thePoint.h,
1727 thePoint.v, dblClick, vimModifiers);
1728
Bram Moolenaar734a8672019-12-02 22:49:38 +01001729 // Create the rectangle around the cursor to detect
1730 // the mouse dragging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01001732 // TODO: Do we need to this even for the contextual menu?
1733 // It may be require for popup_setpos, but for popup?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (vimMouseButton == MOUSE_LEFT)
1735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001737 SetRect(&dragRect, FILL_X(X_2_COL(thePoint.h)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 FILL_Y(Y_2_ROW(thePoint.v)),
1739 FILL_X(X_2_COL(thePoint.h)+1),
1740 FILL_Y(Y_2_ROW(thePoint.v)+1));
1741
1742 dragRectEnbl = TRUE;
1743 dragRectControl = kCreateRect;
1744 }
1745 }
1746}
1747
1748/*
1749 * Handle the click in the titlebar (to move the window)
1750 */
1751 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001752gui_mac_doInDragClick(Point where, WindowPtr whichWindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 Rect movingLimits;
1755 Rect *movingLimitsPtr = &movingLimits;
1756
Bram Moolenaar734a8672019-12-02 22:49:38 +01001757 // TODO: may try to prevent move outside screen?
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001758 movingLimitsPtr = GetRegionBounds(GetGrayRgn(), &movingLimits);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001759 DragWindow(whichWindow, where, movingLimitsPtr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
1762/*
1763 * Handle the click in the grow box
1764 */
1765 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001766gui_mac_doInGrowClick(Point where, WindowPtr whichWindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768
1769 long newSize;
1770 unsigned short newWidth;
1771 unsigned short newHeight;
1772 Rect resizeLimits;
1773 Rect *resizeLimitsPtr = &resizeLimits;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 Rect NewContentRect;
1775
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001776 resizeLimitsPtr = GetRegionBounds(GetGrayRgn(), &resizeLimits);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
Bram Moolenaar734a8672019-12-02 22:49:38 +01001778 // Set the minimum size
1779 // TODO: Should this come from Vim?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 resizeLimits.top = 100;
1781 resizeLimits.left = 100;
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 newSize = ResizeWindow(whichWindow, where, &resizeLimits, &NewContentRect);
1784 newWidth = NewContentRect.right - NewContentRect.left;
1785 newHeight = NewContentRect.bottom - NewContentRect.top;
1786 gui_resize_shell(newWidth, newHeight);
1787 gui_mch_set_bg_color(gui.back_pixel);
Bram Moolenaarafa24992006-03-27 20:58:26 +00001788 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789}
1790
1791/*
1792 * Handle the click in the zoom box
1793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 static void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001795gui_mac_doInZoomClick(EventRecord *theEvent, WindowPtr whichWindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
1797 Rect r;
1798 Point p;
1799 short thePart;
1800
Bram Moolenaar734a8672019-12-02 22:49:38 +01001801 // ideal width is current
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 p.h = Columns * gui.char_width + 2 * gui.border_offset;
1803 if (gui.which_scrollbars[SBAR_LEFT])
1804 p.h += gui.scrollbar_width;
1805 if (gui.which_scrollbars[SBAR_RIGHT])
1806 p.h += gui.scrollbar_width;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001807 // ideal height is as high as we can get
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 p.v = 15 * 1024;
1809
1810 thePart = IsWindowInStandardState(whichWindow, &p, &r)
1811 ? inZoomIn : inZoomOut;
1812
1813 if (!TrackBox(whichWindow, theEvent->where, thePart))
1814 return;
1815
Bram Moolenaar734a8672019-12-02 22:49:38 +01001816 // use returned width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 p.h = r.right - r.left;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001818 // adjust returned height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 p.v = r.bottom - r.top - 2 * gui.border_offset;
1820 if (gui.which_scrollbars[SBAR_BOTTOM])
1821 p.v -= gui.scrollbar_height;
1822 p.v -= p.v % gui.char_height;
1823 p.v += 2 * gui.border_width;
Bram Moolenaard8644bd2011-06-12 20:33:38 +02001824 if (gui.which_scrollbars[SBAR_BOTTOM])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 p.v += gui.scrollbar_height;
1826
1827 ZoomWindowIdeal(whichWindow, thePart, &p);
1828
1829 GetWindowBounds(whichWindow, kWindowContentRgn, &r);
1830 gui_resize_shell(r.right - r.left, r.bottom - r.top);
1831 gui_mch_set_bg_color(gui.back_pixel);
Bram Moolenaarafa24992006-03-27 20:58:26 +00001832 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835/*
1836 * ------------------------------------------------------------
1837 * MacOS Event Handling procedure
1838 * ------------------------------------------------------------
1839 */
1840
1841/*
1842 * Handle the Update Event
1843 */
1844
1845 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001846gui_mac_doUpdateEvent(EventRecord *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 WindowPtr whichWindow;
1849 GrafPtr savePort;
1850 RgnHandle updateRgn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 Rect updateRect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 Rect *updateRectPtr;
1853 Rect rc;
1854 Rect growRect;
1855 RgnHandle saveRgn;
1856
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 updateRgn = NewRgn();
1859 if (updateRgn == NULL)
1860 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar734a8672019-12-02 22:49:38 +01001862 // This could be done by the caller as we
1863 // don't require anything else out of the event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 whichWindow = (WindowPtr) event->message;
1865
Bram Moolenaar734a8672019-12-02 22:49:38 +01001866 // Save Current Port
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001867 GetPort(&savePort);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaar734a8672019-12-02 22:49:38 +01001869 // Select the Window's Port
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001870 SetPortWindowPort(whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaar734a8672019-12-02 22:49:38 +01001872 // Let's update the window
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001873 BeginUpdate(whichWindow);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001874 // Redraw the biggest rectangle covering the area
1875 // to be updated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 GetPortVisibleRegion(GetWindowPort(whichWindow), updateRgn);
1877# if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01001878 // Would be more appropriate to use the following but doesn't
1879 // seem to work under MacOS X (Dany)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 GetWindowRegion(whichWindow, kWindowUpdateRgn, updateRgn);
1881# endif
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001882
Bram Moolenaar734a8672019-12-02 22:49:38 +01001883 // Use the HLock useless in Carbon? Is it harmful?
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001884 HLock((Handle) updateRgn);
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001885
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001886 updateRectPtr = GetRegionBounds(updateRgn, &updateRect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887# if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01001888 // Code from original Carbon Port (using GetWindowRegion.
1889 // I believe the UpdateRgn is already in local (Dany)
1890 GlobalToLocal(&topLeft(updateRect)); // preCarbon?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 GlobalToLocal(&botRight(updateRect));
1892# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01001893 // Update the content (i.e. the text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 gui_redraw(updateRectPtr->left, updateRectPtr->top,
1895 updateRectPtr->right - updateRectPtr->left,
1896 updateRectPtr->bottom - updateRectPtr->top);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001897 // Clear the border areas if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 gui_mch_set_bg_color(gui.back_pixel);
1899 if (updateRectPtr->left < FILL_X(0))
1900 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001901 SetRect(&rc, 0, 0, FILL_X(0), FILL_Y(Rows));
1902 EraseRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 if (updateRectPtr->top < FILL_Y(0))
1905 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001906 SetRect(&rc, 0, 0, FILL_X(Columns), FILL_Y(0));
1907 EraseRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 if (updateRectPtr->right > FILL_X(Columns))
1910 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001911 SetRect(&rc, FILL_X(Columns), 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 FILL_X(Columns) + gui.border_offset, FILL_Y(Rows));
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001913 EraseRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915 if (updateRectPtr->bottom > FILL_Y(Rows))
1916 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001917 SetRect(&rc, 0, FILL_Y(Rows), FILL_X(Columns) + gui.border_offset,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 FILL_Y(Rows) + gui.border_offset);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001919 EraseRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001921 HUnlock((Handle) updateRgn);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001922 DisposeRgn(updateRgn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaar734a8672019-12-02 22:49:38 +01001924 // Update scrollbars
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001925 DrawControls(whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar734a8672019-12-02 22:49:38 +01001927 // Update the GrowBox
1928 // Taken from FAQ 33-27
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 saveRgn = NewRgn();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 GetWindowBounds(whichWindow, kWindowGrowRgn, &growRect);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001931 GetClip(saveRgn);
1932 ClipRect(&growRect);
1933 DrawGrowIcon(whichWindow);
1934 SetClip(saveRgn);
1935 DisposeRgn(saveRgn);
1936 EndUpdate(whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
Bram Moolenaar734a8672019-12-02 22:49:38 +01001938 // Restore original Port
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001939 SetPort(savePort);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941
1942/*
1943 * Handle the activate/deactivate event
1944 * (apply to a window)
1945 */
1946 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001947gui_mac_doActivateEvent(EventRecord *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 WindowPtr whichWindow;
1950
1951 whichWindow = (WindowPtr) event->message;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001952 // Dim scrollbars
Bram Moolenaare02d7b22007-06-19 14:29:43 +00001953 if (whichWindow == gui.VimWindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00001955 ControlRef rootControl;
1956 GetRootControl(gui.VimWindow, &rootControl);
1957 if ((event->modifiers) & activeFlag)
1958 ActivateControl(rootControl);
1959 else
1960 DeactivateControl(rootControl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
Bram Moolenaare02d7b22007-06-19 14:29:43 +00001962
Bram Moolenaar734a8672019-12-02 22:49:38 +01001963 // Activate
Bram Moolenaare02d7b22007-06-19 14:29:43 +00001964 gui_focus_change((event->modifiers) & activeFlag);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965}
1966
1967
1968/*
1969 * Handle the suspend/resume event
1970 * (apply to the application)
1971 */
1972 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001973gui_mac_doSuspendEvent(EventRecord *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001975 // The frontmost application just changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
Bram Moolenaar734a8672019-12-02 22:49:38 +01001977 // NOTE: the suspend may happen before the deactivate
1978 // seen on MacOS X
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
Bram Moolenaar734a8672019-12-02 22:49:38 +01001980 // May not need to change focus as the window will
1981 // get an activate/deactivate event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (event->message & 1)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001983 // Resume
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 gui_focus_change(TRUE);
1985 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01001986 // Suspend
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 gui_focus_change(FALSE);
1988}
1989
1990/*
1991 * Handle the key
1992 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001993#ifdef USE_CARBONKEYHANDLER
Bram Moolenaar1b60e502008-03-12 13:40:54 +00001994 static pascal OSStatus
1995gui_mac_handle_window_activate(
1996 EventHandlerCallRef nextHandler,
1997 EventRef theEvent,
1998 void *data)
1999{
2000 UInt32 eventClass = GetEventClass(theEvent);
2001 UInt32 eventKind = GetEventKind(theEvent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002002
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002003 if (eventClass == kEventClassWindow)
2004 {
2005 switch (eventKind)
2006 {
2007 case kEventWindowActivated:
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002008 im_on_window_switch(TRUE);
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002009 return noErr;
2010
2011 case kEventWindowDeactivated:
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002012 im_on_window_switch(FALSE);
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002013 return noErr;
2014 }
2015 }
2016
2017 return eventNotHandledErr;
2018}
2019
2020 static pascal OSStatus
2021gui_mac_handle_text_input(
2022 EventHandlerCallRef nextHandler,
2023 EventRef theEvent,
2024 void *data)
2025{
2026 UInt32 eventClass = GetEventClass(theEvent);
2027 UInt32 eventKind = GetEventKind(theEvent);
2028
2029 if (eventClass != kEventClassTextInput)
2030 return eventNotHandledErr;
2031
2032 if ((kEventTextInputUpdateActiveInputArea != eventKind) &&
2033 (kEventTextInputUnicodeForKeyEvent != eventKind) &&
2034 (kEventTextInputOffsetToPos != eventKind) &&
2035 (kEventTextInputPosToOffset != eventKind) &&
2036 (kEventTextInputGetSelectedText != eventKind))
2037 return eventNotHandledErr;
2038
2039 switch (eventKind)
2040 {
2041 case kEventTextInputUpdateActiveInputArea:
2042 return gui_mac_update_input_area(nextHandler, theEvent);
2043 case kEventTextInputUnicodeForKeyEvent:
2044 return gui_mac_unicode_key_event(nextHandler, theEvent);
2045
2046 case kEventTextInputOffsetToPos:
2047 case kEventTextInputPosToOffset:
2048 case kEventTextInputGetSelectedText:
2049 break;
2050 }
2051
2052 return eventNotHandledErr;
2053}
2054
2055 static pascal
2056OSStatus gui_mac_update_input_area(
2057 EventHandlerCallRef nextHandler,
2058 EventRef theEvent)
2059{
2060 return eventNotHandledErr;
2061}
2062
Bram Moolenaar734a8672019-12-02 22:49:38 +01002063static int dialog_busy = FALSE; // TRUE when gui_mch_dialog() wants the
2064 // keys
Bram Moolenaard68071d2006-05-02 22:08:30 +00002065
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002066# define INLINE_KEY_BUFFER_SIZE 80
2067 static pascal OSStatus
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002068gui_mac_unicode_key_event(
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002069 EventHandlerCallRef nextHandler,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002070 EventRef theEvent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002071{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002072 // Multibyte-friendly key event handler
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002073 OSStatus err = -1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074 UInt32 actualSize;
2075 UniChar *text;
2076 char_u result[INLINE_KEY_BUFFER_SIZE];
2077 short len = 0;
2078 UInt32 key_sym;
2079 char charcode;
2080 int key_char;
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002081 UInt32 modifiers, vimModifiers;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002082 size_t encLen;
2083 char_u *to = NULL;
2084 Boolean isSpecial = FALSE;
2085 int i;
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002086 EventRef keyEvent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
Bram Moolenaar734a8672019-12-02 22:49:38 +01002088 // Mask the mouse (as per user setting)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002089 if (p_mh)
2090 ObscureCursor();
2091
Bram Moolenaar734a8672019-12-02 22:49:38 +01002092 // Don't use the keys when the dialog wants them.
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002093 if (dialog_busy)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002094 return eventNotHandledErr;
Bram Moolenaard68071d2006-05-02 22:08:30 +00002095
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002096 if (noErr != GetEventParameter(theEvent, kEventParamTextInputSendText,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002097 typeUnicodeText, NULL, 0, &actualSize, NULL))
2098 return eventNotHandledErr;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002099
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002100 text = alloc(actualSize);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002101 if (!text)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002102 return eventNotHandledErr;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002103
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002104 err = GetEventParameter(theEvent, kEventParamTextInputSendText,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002105 typeUnicodeText, NULL, actualSize, NULL, text);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002106 require_noerr(err, done);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002107
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002108 err = GetEventParameter(theEvent, kEventParamTextInputSendKeyboardEvent,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002109 typeEventRef, NULL, sizeof(EventRef), NULL, &keyEvent);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002110 require_noerr(err, done);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002111
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002112 err = GetEventParameter(keyEvent, kEventParamKeyModifiers,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002113 typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002114 require_noerr(err, done);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002115
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002116 err = GetEventParameter(keyEvent, kEventParamKeyCode,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002117 typeUInt32, NULL, sizeof(UInt32), NULL, &key_sym);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002118 require_noerr(err, done);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002119
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002120 err = GetEventParameter(keyEvent, kEventParamKeyMacCharCodes,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002121 typeChar, NULL, sizeof(char), NULL, &charcode);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002122 require_noerr(err, done);
2123
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002124#ifndef USE_CMD_KEY
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002125 if (modifiers & cmdKey)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002126 goto done; // Let system handle Cmd+...
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002127#endif
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002128
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002129 key_char = charcode;
2130 vimModifiers = EventModifiers2VimModifiers(modifiers);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002131
Bram Moolenaar734a8672019-12-02 22:49:38 +01002132 // Find the special key (eg., for cursor keys)
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002133 if (actualSize <= sizeof(UniChar) &&
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002134 ((text[0] < 0x20) || (text[0] == 0x7f)))
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002135 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002136 for (i = 0; special_keys[i].key_sym != (KeySym)0; ++i)
2137 if (special_keys[i].key_sym == key_sym)
2138 {
2139 key_char = TO_SPECIAL(special_keys[i].vim_code0,
2140 special_keys[i].vim_code1);
2141 key_char = simplify_key(key_char,
2142 (int *)&vimModifiers);
2143 isSpecial = TRUE;
2144 break;
2145 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002146 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002147
Bram Moolenaar734a8672019-12-02 22:49:38 +01002148 // Intercept CMD-. and CTRL-c
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002149 if (((modifiers & controlKey) && key_char == 'c') ||
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002150 ((modifiers & cmdKey) && key_char == '.'))
2151 got_int = TRUE;
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002152
2153 if (!isSpecial)
2154 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002155 // remove SHIFT for keys that are already shifted, e.g.,
2156 // '(' and '*'
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002157 if (key_char < 0x100 && !isalpha(key_char) && isprint(key_char))
2158 vimModifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002159
Bram Moolenaar734a8672019-12-02 22:49:38 +01002160 // remove CTRL from keys that already have it
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002161 if (key_char < 0x20)
2162 vimModifiers &= ~MOD_MASK_CTRL;
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002163
Bram Moolenaar734a8672019-12-02 22:49:38 +01002164 // don't process unicode characters here
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002165 if (!IS_SPECIAL(key_char))
2166 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002167 // Following code to simplify and consolidate vimModifiers
2168 // taken liberally from gui_w48.c
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002169 key_char = simplify_key(key_char, (int *)&vimModifiers);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002170
Bram Moolenaar734a8672019-12-02 22:49:38 +01002171 // Interpret META, include SHIFT, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02002172 key_char = extract_modifiers(key_char, (int *)&vimModifiers,
2173 TRUE, NULL);
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002174 if (key_char == CSI)
2175 key_char = K_CSI;
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002176
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002177 if (IS_SPECIAL(key_char))
2178 isSpecial = TRUE;
2179 }
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002180 }
2181
2182 if (vimModifiers)
2183 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002184 result[len++] = CSI;
2185 result[len++] = KS_MODIFIER;
2186 result[len++] = vimModifiers;
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002187 }
2188
2189 if (isSpecial && IS_SPECIAL(key_char))
2190 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002191 result[len++] = CSI;
2192 result[len++] = K_SECOND(key_char);
2193 result[len++] = K_THIRD(key_char);
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002194 }
2195 else
2196 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002197 encLen = actualSize;
2198 to = mac_utf16_to_enc(text, actualSize, &encLen);
2199 if (to)
2200 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002201 // This is basically add_to_input_buf_csi()
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002202 for (i = 0; i < encLen && len < (INLINE_KEY_BUFFER_SIZE-1); ++i)
2203 {
2204 result[len++] = to[i];
2205 if (to[i] == CSI)
2206 {
2207 result[len++] = KS_EXTRA;
2208 result[len++] = (int)KE_CSI;
2209 }
2210 }
2211 vim_free(to);
2212 }
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002213 }
2214
2215 add_to_input_buf(result, len);
2216 err = noErr;
2217
2218done:
2219 vim_free(text);
2220 if (err == noErr)
2221 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002222 // Fake event to wake up WNE (required to get
2223 // key repeat working
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002224 PostEvent(keyUp, 0);
2225 return noErr;
Bram Moolenaaradcb9492006-10-17 10:51:57 +00002226 }
2227
2228 return eventNotHandledErr;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002229}
2230#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 void
2232gui_mac_doKeyEvent(EventRecord *theEvent)
2233{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002234 // TODO: add support for COMMAND KEY
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 long menu;
2236 unsigned char string[20];
2237 short num, i;
2238 short len = 0;
2239 KeySym key_sym;
2240 int key_char;
2241 int modifiers;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002242 int simplify = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243
Bram Moolenaar734a8672019-12-02 22:49:38 +01002244 // Mask the mouse (as per user setting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (p_mh)
2246 ObscureCursor();
2247
Bram Moolenaar734a8672019-12-02 22:49:38 +01002248 // Get the key code and its ASCII representation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 key_sym = ((theEvent->message & keyCodeMask) >> 8);
2250 key_char = theEvent->message & charCodeMask;
2251 num = 1;
2252
Bram Moolenaar734a8672019-12-02 22:49:38 +01002253 // Intercept CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (theEvent->modifiers & controlKey)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (key_char == Ctrl_C && ctrl_c_interrupts)
2257 got_int = TRUE;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002258 else if ((theEvent->modifiers & ~(controlKey|shiftKey)) == 0
2259 && (key_char == '2' || key_char == '6'))
2260 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002261 // CTRL-^ and CTRL-@ don't work in the normal way.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002262 if (key_char == '2')
2263 key_char = Ctrl_AT;
2264 else
2265 key_char = Ctrl_HAT;
2266 theEvent->modifiers = 0;
2267 }
2268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
Bram Moolenaar734a8672019-12-02 22:49:38 +01002270 // Intercept CMD-.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (theEvent->modifiers & cmdKey)
2272 if (key_char == '.')
2273 got_int = TRUE;
2274
Bram Moolenaar734a8672019-12-02 22:49:38 +01002275 // Handle command key as per menu
2276 // TODO: should override be allowed? Require YAO or could use 'winaltkey'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (theEvent->modifiers & cmdKey)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002278 // Only accept CMD alone or with CAPLOCKS and the mouse button.
2279 // Why the mouse button?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if ((theEvent->modifiers & (~(cmdKey | btnState | alphaLock))) == 0)
2281 {
2282 menu = MenuKey(key_char);
2283 if (HiWord(menu))
2284 {
2285 gui_mac_handle_menu(menu);
2286 return;
2287 }
2288 }
2289
Bram Moolenaar734a8672019-12-02 22:49:38 +01002290 // Convert the modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 modifiers = EventModifiers2VimModifiers(theEvent->modifiers);
2292
2293
Bram Moolenaar734a8672019-12-02 22:49:38 +01002294 // Handle special keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01002296 // Why has this been removed?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (!(theEvent->modifiers & (cmdKey | controlKey | rightControlKey)))
2298#endif
2299 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002300 // Find the special key (for non-printable keyt_char)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if ((key_char < 0x20) || (key_char == 0x7f))
2302 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2303 if (special_keys[i].key_sym == key_sym)
2304 {
2305# if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01002306 // We currently don't have not so special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (special_keys[i].vim_code1 == NUL)
2308 key_char = special_keys[i].vim_code0;
2309 else
2310# endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002311 key_char = TO_SPECIAL(special_keys[i].vim_code0,
2312 special_keys[i].vim_code1);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002313 simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 break;
2315 }
2316 }
2317
Bram Moolenaar734a8672019-12-02 22:49:38 +01002318 // For some keys the modifier is included in the char itself.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002319 if (simplify || key_char == TAB || key_char == ' ')
2320 key_char = simplify_key(key_char, &modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
Bram Moolenaar734a8672019-12-02 22:49:38 +01002322 // Add the modifier to the input bu if needed
2323 // Do not want SHIFT-A or CTRL-A with modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (!IS_SPECIAL(key_char)
2325 && key_sym != vk_Space
2326 && key_sym != vk_Tab
2327 && key_sym != vk_Return
2328 && key_sym != vk_Enter
2329 && key_sym != vk_Esc)
2330 {
2331#if 1
Bram Moolenaar734a8672019-12-02 22:49:38 +01002332 // Clear modifiers when only one modifier is set
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002333 if ((modifiers == MOD_MASK_SHIFT)
2334 || (modifiers == MOD_MASK_CTRL)
2335 || (modifiers == MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 modifiers = 0;
2337#else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002338 if (modifiers & MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 modifiers = modifiers & ~MOD_MASK_CTRL;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002340 if (modifiers & MOD_MASK_ALT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 modifiers = modifiers & ~MOD_MASK_ALT;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002342 if (modifiers & MOD_MASK_SHIFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 modifiers = modifiers & ~MOD_MASK_SHIFT;
2344#endif
2345 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002346 if (modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002348 string[len++] = CSI;
2349 string[len++] = KS_MODIFIER;
2350 string[len++] = modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002353 if (IS_SPECIAL(key_char))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002355 string[len++] = CSI;
2356 string[len++] = K_SECOND(key_char);
2357 string[len++] = K_THIRD(key_char);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359 else
2360 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002361 // Convert characters when needed (e.g., from MacRoman to latin1).
2362 // This doesn't work for the NUL byte.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002363 if (input_conv.vc_type != CONV_NONE && key_char > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
2365 char_u from[2], *to;
2366 int l;
2367
2368 from[0] = key_char;
2369 from[1] = NUL;
2370 l = 1;
2371 to = string_convert(&input_conv, from, &l);
2372 if (to != NULL)
2373 {
2374 for (i = 0; i < l && len < 19; i++)
2375 {
2376 if (to[i] == CSI)
2377 {
2378 string[len++] = KS_EXTRA;
2379 string[len++] = KE_CSI;
2380 }
2381 else
2382 string[len++] = to[i];
2383 }
2384 vim_free(to);
2385 }
2386 else
2387 string[len++] = key_char;
2388 }
2389 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 string[len++] = key_char;
2391 }
2392
2393 if (len == 1 && string[0] == CSI)
2394 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002395 // Turn CSI into K_CSI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 string[ len++ ] = KS_EXTRA;
2397 string[ len++ ] = KE_CSI;
2398 }
2399
2400 add_to_input_buf(string, len);
2401}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404/*
2405 * Handle MouseClick
2406 */
2407 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002408gui_mac_doMouseDownEvent(EventRecord *theEvent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
2410 short thePart;
2411 WindowPtr whichWindow;
2412
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002413 thePart = FindWindow(theEvent->where, &whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
Bram Moolenaar79ee3152007-04-26 16:20:50 +00002415#ifdef FEAT_GUI_TABLINE
Bram Moolenaar734a8672019-12-02 22:49:38 +01002416 // prevent that the vim window size changes if it's activated by a
2417 // click into the tab pane
Bram Moolenaar79ee3152007-04-26 16:20:50 +00002418 if (whichWindow == drawer)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002419 return;
Bram Moolenaar79ee3152007-04-26 16:20:50 +00002420#endif
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 switch (thePart)
2423 {
2424 case (inDesk):
Bram Moolenaar734a8672019-12-02 22:49:38 +01002425 // TODO: what to do?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 break;
2427
2428 case (inMenuBar):
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002429 gui_mac_handle_menu(MenuSelect(theEvent->where));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 break;
2431
2432 case (inContent):
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002433 gui_mac_doInContentClick(theEvent, whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 break;
2435
2436 case (inDrag):
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002437 gui_mac_doInDragClick(theEvent->where, whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 break;
2439
2440 case (inGrow):
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002441 gui_mac_doInGrowClick(theEvent->where, whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 break;
2443
2444 case (inGoAway):
2445 if (TrackGoAway(whichWindow, theEvent->where))
2446 gui_shell_closed();
2447 break;
2448
2449 case (inZoomIn):
2450 case (inZoomOut):
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 gui_mac_doInZoomClick(theEvent, whichWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 break;
2453 }
2454}
2455
2456/*
2457 * Handle MouseMoved
2458 * [this event is a moving in and out of a region]
2459 */
2460 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002461gui_mac_doMouseMovedEvent(EventRecord *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 Point thePoint;
2464 int_u vimModifiers;
2465
2466 thePoint = event->where;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002467 GlobalToLocal(&thePoint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 vimModifiers = EventModifiers2VimMouseModifiers(event->modifiers);
2469
2470 if (!Button())
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002471 gui_mouse_moved(thePoint.h, thePoint.v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (!clickIsPopup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 gui_send_mouse_event(MOUSE_DRAG, thePoint.h,
2475 thePoint.v, FALSE, vimModifiers);
2476
Bram Moolenaar734a8672019-12-02 22:49:38 +01002477 // Reset the region from which we move in and out
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002478 SetRect(&dragRect, FILL_X(X_2_COL(thePoint.h)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 FILL_Y(Y_2_ROW(thePoint.v)),
2480 FILL_X(X_2_COL(thePoint.h)+1),
2481 FILL_Y(Y_2_ROW(thePoint.v)+1));
2482
2483 if (dragRectEnbl)
2484 dragRectControl = kCreateRect;
2485
2486}
2487
2488/*
2489 * Handle the mouse release
2490 */
2491 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002492gui_mac_doMouseUpEvent(EventRecord *theEvent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 Point thePoint;
2495 int_u vimModifiers;
2496
Bram Moolenaar734a8672019-12-02 22:49:38 +01002497 // TODO: Properly convert the Contextual menu mouse-up
2498 // Potential source of the double menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 lastMouseTick = theEvent->when;
2500 dragRectEnbl = FALSE;
2501 dragRectControl = kCreateEmpty;
2502 thePoint = theEvent->where;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002503 GlobalToLocal(&thePoint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505 vimModifiers = EventModifiers2VimMouseModifiers(theEvent->modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (clickIsPopup)
2507 {
2508 vimModifiers &= ~MOUSE_CTRL;
2509 clickIsPopup = FALSE;
2510 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002511 gui_send_mouse_event(MOUSE_RELEASE, thePoint.h, thePoint.v, FALSE, vimModifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512}
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 static pascal OSStatus
2515gui_mac_mouse_wheel(EventHandlerCallRef nextHandler, EventRef theEvent,
2516 void *data)
2517{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 Point point;
2519 Rect bounds;
2520 UInt32 mod;
2521 SInt32 delta;
2522 int_u vim_mod;
Bram Moolenaar621e2fd2006-08-22 19:36:17 +00002523 EventMouseWheelAxis axis;
2524
2525 if (noErr == GetEventParameter(theEvent, kEventParamMouseWheelAxis,
2526 typeMouseWheelAxis, NULL, sizeof(axis), NULL, &axis)
2527 && axis != kEventMouseWheelAxisY)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002528 goto bail; // Vim only does up-down scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 if (noErr != GetEventParameter(theEvent, kEventParamMouseWheelDelta,
2531 typeSInt32, NULL, sizeof(SInt32), NULL, &delta))
2532 goto bail;
2533 if (noErr != GetEventParameter(theEvent, kEventParamMouseLocation,
2534 typeQDPoint, NULL, sizeof(Point), NULL, &point))
2535 goto bail;
2536 if (noErr != GetEventParameter(theEvent, kEventParamKeyModifiers,
2537 typeUInt32, NULL, sizeof(UInt32), NULL, &mod))
2538 goto bail;
2539
2540 vim_mod = 0;
2541 if (mod & shiftKey)
2542 vim_mod |= MOUSE_SHIFT;
2543 if (mod & controlKey)
2544 vim_mod |= MOUSE_CTRL;
2545 if (mod & optionKey)
2546 vim_mod |= MOUSE_ALT;
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (noErr == GetWindowBounds(gui.VimWindow, kWindowContentRgn, &bounds))
2549 {
2550 point.h -= bounds.left;
2551 point.v -= bounds.top;
2552 }
2553
2554 gui_send_mouse_event((delta > 0) ? MOUSE_4 : MOUSE_5,
2555 point.h, point.v, FALSE, vim_mod);
2556
Bram Moolenaar734a8672019-12-02 22:49:38 +01002557 // post a bogus event to wake up WaitNextEvent
Bram Moolenaarc236c162008-07-13 17:41:49 +00002558 PostEvent(keyUp, 0);
2559
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return noErr;
2561
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00002562bail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 /*
2564 * when we fail give any additional callback handler a chance to perform
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002565 * its actions
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
2567 return CallNextEventHandler(nextHandler, theEvent);
2568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
Bram Moolenaare00289d2010-08-14 21:56:42 +02002570 void
2571gui_mch_mousehide(int hide)
2572{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002573 // TODO
Bram Moolenaare00289d2010-08-14 21:56:42 +02002574}
2575
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#if 0
2577
2578/*
2579 * This would be the normal way of invoking the contextual menu
2580 * but the Vim API doesn't seem to a support a request to get
2581 * the menu that we should display
2582 */
2583 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002584gui_mac_handle_contextual_menu(EventRecord *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585{
2586/*
2587 * Clone PopUp to use menu
2588 * Create a object descriptor for the current selection
2589 * Call the procedure
2590 */
2591
2592// Call to Handle Popup
2593 OSStatus status = ContextualMenuSelect(CntxMenu, event->where, false, kCMHelpItemNoHelp, "", NULL, &CntxType, &CntxMenuID, &CntxMenuItem);
2594
2595 if (status != noErr)
2596 return;
2597
2598 if (CntxType == kCMMenuItemSelected)
2599 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002600 // Handle the menu CntxMenuID, CntxMenuItem
2601 // The submenu can be handle directly by gui_mac_handle_menu
2602 // But what about the current menu, is the many changed by ContextualMenuSelect
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002603 gui_mac_handle_menu((CntxMenuID << 16) + CntxMenuItem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605 else if (CntxMenuID == kCMShowHelpSelected)
2606 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002607 // Should come up with the help
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609
2610}
2611#endif
2612
2613/*
2614 * Handle menubar selection
2615 */
2616 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002617gui_mac_handle_menu(long menuChoice)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 short menu = HiWord(menuChoice);
2620 short item = LoWord(menuChoice);
2621 vimmenu_T *theVimMenu = root_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
Bram Moolenaar734a8672019-12-02 22:49:38 +01002623 if (menu == 256) // TODO: use constant or gui.xyz
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
2625 if (item == 1)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002626 gui_mch_beep(); // TODO: Popup dialog or do :intro
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628 else if (item != 0)
2629 {
2630 theVimMenu = gui_mac_get_vim_menu(menu, item, root_menu);
2631
2632 if (theVimMenu)
2633 gui_menu_cb(theVimMenu);
2634 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002635 HiliteMenu(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636}
2637
2638/*
2639 * Dispatch the event to proper handler
2640 */
2641
2642 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002643gui_mac_handle_event(EventRecord *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 OSErr error;
2646
Bram Moolenaar734a8672019-12-02 22:49:38 +01002647 // Handle contextual menu right now (if needed)
Bram Moolenaare02d7b22007-06-19 14:29:43 +00002648 if (IsShowContextualMenuClick(event))
2649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650# if 0
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002651 gui_mac_handle_contextual_menu(event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652# else
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002653 gui_mac_doMouseDownEvent(event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654# endif
Bram Moolenaar1b60e502008-03-12 13:40:54 +00002655 return;
Bram Moolenaare02d7b22007-06-19 14:29:43 +00002656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
Bram Moolenaar734a8672019-12-02 22:49:38 +01002658 // Handle normal event
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 switch (event->what)
2660 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002661#ifndef USE_CARBONKEYHANDLER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 case (keyDown):
2663 case (autoKey):
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002664 gui_mac_doKeyEvent(event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 break;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 case (keyUp):
Bram Moolenaar734a8672019-12-02 22:49:38 +01002668 // We don't care about when the key is released
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 break;
2670
2671 case (mouseDown):
2672 gui_mac_doMouseDownEvent(event);
2673 break;
2674
2675 case (mouseUp):
2676 gui_mac_doMouseUpEvent(event);
2677 break;
2678
2679 case (updateEvt):
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002680 gui_mac_doUpdateEvent(event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 break;
2682
2683 case (diskEvt):
Bram Moolenaar734a8672019-12-02 22:49:38 +01002684 // We don't need special handling for disk insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 break;
2686
2687 case (activateEvt):
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002688 gui_mac_doActivateEvent(event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 break;
2690
2691 case (osEvt):
2692 switch ((event->message >> 24) & 0xFF)
2693 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002694 case (0xFA): // mouseMovedMessage
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002695 gui_mac_doMouseMovedEvent(event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002697 case (0x01): // suspendResumeMessage
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002698 gui_mac_doSuspendEvent(event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 break;
2700 }
2701 break;
2702
2703#ifdef USE_AEVENT
2704 case (kHighLevelEvent):
Bram Moolenaar734a8672019-12-02 22:49:38 +01002705 // Someone's talking to us, through AppleEvents
2706 error = AEProcessAppleEvent(event); // TODO: Error Handling
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 break;
2708#endif
2709 }
2710}
2711
2712/*
2713 * ------------------------------------------------------------
2714 * Unknown Stuff
2715 * ------------------------------------------------------------
2716 */
2717
2718
2719 GuiFont
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002720gui_mac_find_font(char_u *font_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 char_u c;
2723 char_u *p;
2724 char_u pFontName[256];
2725 Str255 systemFontname;
2726 short font_id;
2727 short size=9;
2728 GuiFont font;
2729#if 0
2730 char_u *fontNamePtr;
2731#endif
2732
2733 for (p = font_name; ((*p != 0) && (*p != ':')); p++)
2734 ;
2735
2736 c = *p;
2737 *p = 0;
2738
2739#if 1
2740 STRCPY(&pFontName[1], font_name);
2741 pFontName[0] = STRLEN(font_name);
2742 *p = c;
2743
Bram Moolenaar734a8672019-12-02 22:49:38 +01002744 // Get the font name, minus the style suffix (:h, etc)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002745 char_u fontName[256];
2746 char_u *styleStart = vim_strchr(font_name, ':');
2747 size_t fontNameLen = styleStart ? styleStart - font_name : STRLEN(fontName);
2748 vim_strncpy(fontName, font_name, fontNameLen);
2749
2750 ATSUFontID fontRef;
2751 FMFontStyle fontStyle;
2752 font_id = 0;
2753
2754 if (ATSUFindFontFromName(&pFontName[1], pFontName[0], kFontFullName,
2755 kFontMacintoshPlatform, kFontNoScriptCode, kFontNoLanguageCode,
2756 &fontRef) == noErr)
2757 {
2758 if (FMGetFontFamilyInstanceFromFont(fontRef, &font_id, &fontStyle) != noErr)
2759 font_id = 0;
2760 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002761
2762 if (font_id == 0)
2763 {
2764 /*
2765 * Try again, this time replacing underscores in the font name
2766 * with spaces (:set guifont allows the two to be used
2767 * interchangeably; the Font Manager doesn't).
2768 */
2769 int i, changed = FALSE;
2770
2771 for (i = pFontName[0]; i > 0; --i)
2772 {
2773 if (pFontName[i] == '_')
2774 {
2775 pFontName[i] = ' ';
2776 changed = TRUE;
2777 }
2778 }
2779 if (changed)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 if (ATSUFindFontFromName(&pFontName[1], pFontName[0],
2781 kFontFullName, kFontNoPlatformCode, kFontNoScriptCode,
2782 kFontNoLanguageCode, &fontRef) == noErr)
2783 {
2784 if (FMGetFontFamilyInstanceFromFont(fontRef, &font_id, &fontStyle) != noErr)
2785 font_id = 0;
2786 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002787 }
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789#else
Bram Moolenaar734a8672019-12-02 22:49:38 +01002790 // name = C2Pascal_save(menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 fontNamePtr = C2Pascal_save_and_remove_backslash(font_name);
2792
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002793 GetFNum(fontNamePtr, &font_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794#endif
2795
2796
2797 if (font_id == 0)
2798 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002799 // Oups, the system font was it the one the user want
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801 if (FMGetFontFamilyName(systemFont, systemFontname) != noErr)
2802 return NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (!EqualString(pFontName, systemFontname, false, false))
2804 return NOFONT;
2805 }
2806 if (*p == ':')
2807 {
2808 p++;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002809 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 while (*p)
2811 {
2812 switch (*p++)
2813 {
2814 case 'h':
2815 size = points_to_pixels(p, &p, TRUE);
2816 break;
2817 /*
2818 * TODO: Maybe accept width and styles
2819 */
2820 }
2821 while (*p == ':')
2822 p++;
2823 }
2824 }
2825
2826 if (size < 1)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002827 size = 1; // Avoid having a size of 0 with system font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
2829 font = (size << 16) + ((long) font_id & 0xFFFF);
2830
2831 return font;
2832}
2833
2834/*
2835 * ------------------------------------------------------------
Bram Moolenaar720c7102007-05-10 18:07:50 +00002836 * GUI_MCH functionality
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 * ------------------------------------------------------------
2838 */
2839
2840/*
2841 * Parse the GUI related command-line arguments. Any arguments used are
2842 * deleted from argv, and *argc is decremented accordingly. This is called
2843 * when vim is started, whether or not the GUI has been started.
2844 */
2845 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002846gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002848 // TODO: Move most of this stuff toward gui_mch_init
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#ifdef USE_EXE_NAME
2850 FSSpec applDir;
2851# ifndef USE_FIND_BUNDLE_PATH
2852 short applVRefNum;
2853 long applDirID;
2854 Str255 volName;
2855# else
2856 ProcessSerialNumber psn;
2857 FSRef applFSRef;
2858# endif
2859#endif
2860
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861#if 0
2862 InitCursor();
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 RegisterAppearanceClient();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
2866#ifdef USE_AEVENT
2867 (void) InstallAEHandlers();
2868#endif
2869
Bram Moolenaar734a8672019-12-02 22:49:38 +01002870 pomme = NewMenu(256, "\p\024"); // 0x14= = Apple Menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002872 AppendMenu(pomme, "\pAbout VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002874 InsertMenu(pomme, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 DrawMenuBar();
2877
2878
2879#ifndef USE_OFFSETED_WINDOW
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002880 SetRect(&windRect, 10, 48, 10+80*7 + 16, 48+24*11);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881#else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002882 SetRect(&windRect, 300, 40, 300+80*7 + 16, 40+24*11);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#endif
2884
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 CreateNewWindow(kDocumentWindowClass,
2887 kWindowResizableAttribute | kWindowCollapseBoxAttribute,
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002888 &windRect, &gui.VimWindow);
2889 SetPortWindowPort(gui.VimWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
2891 gui.char_width = 7;
2892 gui.char_height = 11;
2893 gui.char_ascent = 6;
2894 gui.num_rows = 24;
2895 gui.num_cols = 80;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002896 gui.in_focus = TRUE; // For the moment -> syn. of front application
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002898 gScrollAction = NewControlActionUPP(gui_mac_scroll_action);
2899 gScrollDrag = NewControlActionUPP(gui_mac_drag_thumb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900
2901 dragRectEnbl = FALSE;
2902 dragRgn = NULL;
2903 dragRectControl = kCreateEmpty;
2904 cursorRgn = NewRgn();
2905#endif
2906#ifdef USE_EXE_NAME
2907# ifndef USE_FIND_BUNDLE_PATH
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002908 HGetVol(volName, &applVRefNum, &applDirID);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002909 // TN2015: mention a possible bad VRefNum
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002910 FSMakeFSSpec(applVRefNum, applDirID, "\p", &applDir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911# else
Bram Moolenaar734a8672019-12-02 22:49:38 +01002912 // OSErr GetApplicationBundleFSSpec(FSSpecPtr theFSSpecPtr)
2913 // of TN2015
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002914 (void)GetCurrentProcess(&psn);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002915 // if (err != noErr) return err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002917 (void)GetProcessBundleLocation(&psn, &applFSRef);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002918 // if (err != noErr) return err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002920 (void)FSGetCatalogInfo(&applFSRef, kFSCatInfoNone, NULL, NULL, &applDir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar734a8672019-12-02 22:49:38 +01002922 // This technic return NIL when we disallow_gui
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923# endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002924 exe_name = FullPathFromFSSpec_save(applDir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926}
2927
2928#ifndef ALWAYS_USE_GUI
2929/*
2930 * Check if the GUI can be started. Called before gvimrc is sourced.
2931 * Return OK or FAIL.
2932 */
2933 int
2934gui_mch_init_check(void)
2935{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002936 // TODO: For MacOS X find a way to return FAIL, if the user logged in
2937 // using the >console
2938 if (disallow_gui) // see main.c for reason to disallow
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 return FAIL;
2940 return OK;
2941}
2942#endif
2943
2944 static OSErr
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00002945receiveHandler(WindowRef theWindow, void *handlerRefCon, DragRef theDrag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
2947 int x, y;
2948 int_u modifiers;
2949 char_u **fnames = NULL;
2950 int count;
2951 int i, j;
2952
Bram Moolenaar734a8672019-12-02 22:49:38 +01002953 // Get drop position, modifiers and count of items
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
2955 Point point;
2956 SInt16 mouseUpModifiers;
2957 UInt16 countItem;
2958
2959 GetDragMouse(theDrag, &point, NULL);
2960 GlobalToLocal(&point);
2961 x = point.h;
2962 y = point.v;
2963 GetDragModifiers(theDrag, NULL, NULL, &mouseUpModifiers);
2964 modifiers = EventModifiers2VimMouseModifiers(mouseUpModifiers);
2965 CountDragItems(theDrag, &countItem);
2966 count = countItem;
2967 }
2968
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002969 fnames = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (fnames == NULL)
2971 return dragNotAcceptedErr;
2972
Bram Moolenaar734a8672019-12-02 22:49:38 +01002973 // Get file names dropped
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 for (i = j = 0; i < count; ++i)
2975 {
2976 DragItemRef item;
2977 OSErr err;
2978 Size size;
2979 FlavorType type = flavorTypeHFS;
2980 HFSFlavor hfsFlavor;
2981
2982 fnames[i] = NULL;
2983 GetDragItemReferenceNumber(theDrag, i + 1, &item);
2984 err = GetFlavorDataSize(theDrag, item, type, &size);
2985 if (err != noErr || size > sizeof(hfsFlavor))
2986 continue;
2987 err = GetFlavorData(theDrag, item, type, &hfsFlavor, &size, 0);
2988 if (err != noErr)
2989 continue;
2990 fnames[j++] = FullPathFromFSSpec_save(hfsFlavor.fileSpec);
2991 }
2992 count = j;
2993
2994 gui_handle_drop(x, y, modifiers, fnames, count);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002995
Bram Moolenaar734a8672019-12-02 22:49:38 +01002996 // Fake mouse event to wake from stall
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002997 PostEvent(mouseUp, 0);
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 return noErr;
3000}
3001
3002/*
3003 * Initialise the GUI. Create all the windows, set up all the call-backs
3004 * etc.
3005 */
3006 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003007gui_mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003009 // TODO: Move most of this stuff toward gui_mch_init
Bram Moolenaar39858af2008-03-12 20:48:13 +00003010 Rect windRect;
3011 MenuHandle pomme;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 EventHandlerRef mouseWheelHandlerRef;
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003013 EventTypeSpec eventTypeSpec;
Bram Moolenaar39858af2008-03-12 20:48:13 +00003014 ControlRef rootControl;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003015
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003016 if (Gestalt(gestaltSystemVersion, &gMacSystemVersion) != noErr)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003017 gMacSystemVersion = 0x1000; // TODO: Default to minimum sensible value
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#if 1
3020 InitCursor();
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 RegisterAppearanceClient();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024#ifdef USE_AEVENT
3025 (void) InstallAEHandlers();
3026#endif
3027
Bram Moolenaar734a8672019-12-02 22:49:38 +01003028 pomme = NewMenu(256, "\p\024"); // 0x14= = Apple Menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003030 AppendMenu(pomme, "\pAbout VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003032 InsertMenu(pomme, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034 DrawMenuBar();
3035
3036
3037#ifndef USE_OFFSETED_WINDOW
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003038 SetRect(&windRect, 10, 48, 10+80*7 + 16, 48+24*11);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003040 SetRect(&windRect, 300, 40, 300+80*7 + 16, 40+24*11);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041#endif
3042
3043 gui.VimWindow = NewCWindow(nil, &windRect, "\pgVim on Macintosh", true,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 zoomDocProc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 (WindowPtr)-1L, true, 0);
Bram Moolenaare02d7b22007-06-19 14:29:43 +00003046 CreateRootControl(gui.VimWindow, &rootControl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 InstallReceiveHandler((DragReceiveHandlerUPP)receiveHandler,
3048 gui.VimWindow, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003049 SetPortWindowPort(gui.VimWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
3051 gui.char_width = 7;
3052 gui.char_height = 11;
3053 gui.char_ascent = 6;
3054 gui.num_rows = 24;
3055 gui.num_cols = 80;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003056 gui.in_focus = TRUE; // For the moment -> syn. of front application
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003058 gScrollAction = NewControlActionUPP(gui_mac_scroll_action);
3059 gScrollDrag = NewControlActionUPP(gui_mac_drag_thumb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaar734a8672019-12-02 22:49:38 +01003061 // Install Carbon event callbacks.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003062 (void)InstallFontPanelHandler();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
3064 dragRectEnbl = FALSE;
3065 dragRgn = NULL;
3066 dragRectControl = kCreateEmpty;
3067 cursorRgn = NewRgn();
3068#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003069 // Display any pending error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 display_errors();
3071
Bram Moolenaar734a8672019-12-02 22:49:38 +01003072 // Get background/foreground colors from system
3073 // TODO: do the appropriate call to get real defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 gui.norm_pixel = 0x00000000;
3075 gui.back_pixel = 0x00FFFFFF;
3076
Bram Moolenaar734a8672019-12-02 22:49:38 +01003077 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
3078 // file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 set_normal_colors();
3080
3081 /*
3082 * Check that none of the colors are the same as the background color.
3083 * Then store the current values as the defaults.
3084 */
3085 gui_check_colors();
3086 gui.def_norm_pixel = gui.norm_pixel;
3087 gui.def_back_pixel = gui.back_pixel;
3088
Bram Moolenaar734a8672019-12-02 22:49:38 +01003089 // Get the colors for the highlight groups (gui_check_colors() might have
3090 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 highlight_gui_started();
3092
3093 /*
3094 * Setting the gui constants
3095 */
3096#ifdef FEAT_MENU
3097 gui.menu_height = 0;
3098#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003099 gui.scrollbar_height = gui.scrollbar_width = 15; // cheat 1 overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 gui.border_offset = gui.border_width = 2;
3101
Bram Moolenaar734a8672019-12-02 22:49:38 +01003102 // If Quartz-style text anti aliasing is available (see
3103 // gui_mch_draw_string() below), enable it for all font sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 vim_setenv((char_u *)"QDTEXT_MINSIZE", (char_u *)"1");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 eventTypeSpec.eventClass = kEventClassMouse;
3107 eventTypeSpec.eventKind = kEventMouseWheelMoved;
3108 mouseWheelHandlerUPP = NewEventHandlerUPP(gui_mac_mouse_wheel);
3109 if (noErr != InstallApplicationEventHandler(mouseWheelHandlerUPP, 1,
3110 &eventTypeSpec, NULL, &mouseWheelHandlerRef))
3111 {
3112 mouseWheelHandlerRef = NULL;
3113 DisposeEventHandlerUPP(mouseWheelHandlerUPP);
3114 mouseWheelHandlerUPP = NULL;
3115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003117#ifdef USE_CARBONKEYHANDLER
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003118 InterfaceTypeList supportedServices = { kUnicodeDocument };
3119 NewTSMDocument(1, supportedServices, &gTSMDocument, 0);
3120
Bram Moolenaar734a8672019-12-02 22:49:38 +01003121 // We don't support inline input yet, use input window by default
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003122 UseInputWindow(gTSMDocument, TRUE);
3123
Bram Moolenaar734a8672019-12-02 22:49:38 +01003124 // Should we activate the document by default?
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003125 // ActivateTSMDocument(gTSMDocument);
3126
3127 EventTypeSpec textEventTypes[] = {
3128 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea },
3129 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent },
3130 { kEventClassTextInput, kEventTextInputPosToOffset },
3131 { kEventClassTextInput, kEventTextInputOffsetToPos },
3132 };
3133
3134 keyEventHandlerUPP = NewEventHandlerUPP(gui_mac_handle_text_input);
3135 if (noErr != InstallApplicationEventHandler(keyEventHandlerUPP,
3136 NR_ELEMS(textEventTypes),
3137 textEventTypes, NULL, NULL))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003138 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003139 DisposeEventHandlerUPP(keyEventHandlerUPP);
3140 keyEventHandlerUPP = NULL;
3141 }
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003142
3143 EventTypeSpec windowEventTypes[] = {
3144 { kEventClassWindow, kEventWindowActivated },
3145 { kEventClassWindow, kEventWindowDeactivated },
3146 };
3147
Bram Moolenaar734a8672019-12-02 22:49:38 +01003148 // Install window event handler to support TSMDocument activate and
3149 // deactivate
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003150 winEventHandlerUPP = NewEventHandlerUPP(gui_mac_handle_window_activate);
3151 if (noErr != InstallWindowEventHandler(gui.VimWindow,
3152 winEventHandlerUPP,
3153 NR_ELEMS(windowEventTypes),
3154 windowEventTypes, NULL, NULL))
3155 {
3156 DisposeEventHandlerUPP(winEventHandlerUPP);
3157 winEventHandlerUPP = NULL;
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#endif
3160
Bram Moolenaar79ee3152007-04-26 16:20:50 +00003161#ifdef FEAT_GUI_TABLINE
3162 /*
3163 * Create the tabline
3164 */
3165 initialise_tabline();
3166#endif
3167
Bram Moolenaar734a8672019-12-02 22:49:38 +01003168 // TODO: Load bitmap if using TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 return OK;
3170}
3171
3172/*
3173 * Called when the foreground or background color has been changed.
3174 */
3175 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003176gui_mch_new_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003178 // TODO:
3179 // This proc is called when Normal is set to a value
3180 // so what must be done? I don't know
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181}
3182
3183/*
3184 * Open the GUI window which was created by a call to gui_mch_init().
3185 */
3186 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003187gui_mch_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 ShowWindow(gui.VimWindow);
3190
3191 if (gui_win_x != -1 && gui_win_y != -1)
3192 gui_mch_set_winpos(gui_win_x, gui_win_y);
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 /*
3195 * Make the GUI the foreground process (in case it was launched
3196 * from the Terminal or via :gui).
3197 */
3198 {
3199 ProcessSerialNumber psn;
3200 if (GetCurrentProcess(&psn) == noErr)
3201 SetFrontProcess(&psn);
3202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 return OK;
3205}
3206
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003207#ifdef USE_ATSUI_DRAWING
3208 static void
3209gui_mac_dispose_atsui_style(void)
3210{
3211 if (p_macatsui && gFontStyle)
3212 ATSUDisposeStyle(gFontStyle);
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003213 if (p_macatsui && gWideFontStyle)
3214 ATSUDisposeStyle(gWideFontStyle);
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003215}
3216#endif
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 void
3219gui_mch_exit(int rc)
3220{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003221 // TODO: find out all what is missing here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 DisposeRgn(cursorRgn);
3223
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003224#ifdef USE_CARBONKEYHANDLER
3225 if (keyEventHandlerUPP)
3226 DisposeEventHandlerUPP(keyEventHandlerUPP);
3227#endif
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (mouseWheelHandlerUPP != NULL)
3230 DisposeEventHandlerUPP(mouseWheelHandlerUPP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003232#ifdef USE_ATSUI_DRAWING
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003233 gui_mac_dispose_atsui_style();
3234#endif
3235
3236#ifdef USE_CARBONKEYHANDLER
3237 FixTSMDocument(gTSMDocument);
3238 DeactivateTSMDocument(gTSMDocument);
3239 DeleteTSMDocument(gTSMDocument);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003240#endif
3241
Bram Moolenaar734a8672019-12-02 22:49:38 +01003242 // Exit to shell?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 exit(rc);
3244}
3245
3246/*
3247 * Get the position of the top left corner of the window.
3248 */
3249 int
3250gui_mch_get_winpos(int *x, int *y)
3251{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003252 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 Rect bounds;
3254 OSStatus status;
3255
Bram Moolenaar734a8672019-12-02 22:49:38 +01003256 // Carbon >= 1.0.2, MacOS >= 8.5
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003257 status = GetWindowBounds(gui.VimWindow, kWindowStructureRgn, &bounds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259 if (status != noErr)
3260 return FAIL;
3261 *x = bounds.left;
3262 *y = bounds.top;
3263 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
3265
3266/*
3267 * Set the position of the top left corner of the window to the given
3268 * coordinates.
3269 */
3270 void
3271gui_mch_set_winpos(int x, int y)
3272{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003273 // TODO: Should make sure the window is move within range
3274 // e.g.: y > ~16 [Menu bar], x > 0, x < screen width
Bram Moolenaarec831732007-08-30 10:51:14 +00003275 MoveWindowStructure(gui.VimWindow, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276}
3277
3278 void
3279gui_mch_set_shellsize(
3280 int width,
3281 int height,
3282 int min_width,
3283 int min_height,
3284 int base_width,
Bram Moolenaarafa24992006-03-27 20:58:26 +00003285 int base_height,
3286 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 CGrafPtr VimPort;
3289 Rect VimBound;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291 if (gui.which_scrollbars[SBAR_LEFT])
3292 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003293 VimPort = GetWindowPort(gui.VimWindow);
3294 GetPortBounds(VimPort, &VimBound);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003295 VimBound.left = -gui.scrollbar_width; // + 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003296 SetPortBounds(VimPort, &VimBound);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003297 // GetWindowBounds(gui.VimWindow, kWindowGlobalPortRgn, &winPortRect); ??
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 else
3300 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003301 VimPort = GetWindowPort(gui.VimWindow);
3302 GetPortBounds(VimPort, &VimBound);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 VimBound.left = 0;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003304 SetPortBounds(VimPort, &VimBound);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306
3307 SizeWindow(gui.VimWindow, width, height, TRUE);
3308
3309 gui_resize_shell(width, height);
3310}
3311
3312/*
3313 * Get the screen dimensions.
3314 * Allow 10 pixels for horizontal borders, 40 for vertical borders.
3315 * Is there no way to find out how wide the borders really are?
Bram Moolenaar720c7102007-05-10 18:07:50 +00003316 * TODO: Add live update of those value on suspend/resume.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 */
3318 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003319gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 GDHandle dominantDevice = GetMainDevice();
3322 Rect screenRect = (**dominantDevice).gdRect;
3323
3324 *screen_w = screenRect.right - 10;
3325 *screen_h = screenRect.bottom - 40;
3326}
3327
3328
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003329/*
3330 * Open the Font Panel and wait for the user to select a font and
3331 * close the panel. Then fill the buffer pointed to by font_name with
3332 * the name and size of the selected font and return the font's handle,
3333 * or NOFONT in case of an error.
3334 */
3335 static GuiFont
3336gui_mac_select_font(char_u *font_name)
3337{
3338 GuiFont selected_font = NOFONT;
3339 OSStatus status;
3340 FontSelectionQDStyle curr_font;
3341
Bram Moolenaar734a8672019-12-02 22:49:38 +01003342 // Initialize the Font Panel with the current font.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003343 curr_font.instance.fontFamily = gui.norm_font & 0xFFFF;
3344 curr_font.size = (gui.norm_font >> 16);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003345 // TODO: set fontStyle once styles are supported in gui_mac_find_font()
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003346 curr_font.instance.fontStyle = 0;
3347 curr_font.hasColor = false;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003348 curr_font.version = 0; // version number of the style structure
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003349 status = SetFontInfoForSelection(kFontSelectionQDType,
3350 /*numStyles=*/1, &curr_font, /*eventTarget=*/NULL);
3351
3352 gFontPanelInfo.family = curr_font.instance.fontFamily;
3353 gFontPanelInfo.style = curr_font.instance.fontStyle;
3354 gFontPanelInfo.size = curr_font.size;
3355
Bram Moolenaar734a8672019-12-02 22:49:38 +01003356 // Pop up the Font Panel.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003357 status = FPShowHideFontPanel();
3358 if (status == noErr)
3359 {
3360 /*
3361 * The Font Panel is modeless. We really need it to be modal,
3362 * so we spin in an event loop until the panel is closed.
3363 */
3364 gFontPanelInfo.isPanelVisible = true;
3365 while (gFontPanelInfo.isPanelVisible)
3366 {
3367 EventRecord e;
3368 WaitNextEvent(everyEvent, &e, /*sleep=*/20, /*mouseRgn=*/NULL);
3369 }
3370
3371 GetFontPanelSelection(font_name);
3372 selected_font = gui_mac_find_font(font_name);
3373 }
3374 return selected_font;
3375}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003376
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003377#ifdef USE_ATSUI_DRAWING
3378 static void
3379gui_mac_create_atsui_style(void)
3380{
3381 if (p_macatsui && gFontStyle == NULL)
3382 {
3383 if (ATSUCreateStyle(&gFontStyle) != noErr)
3384 gFontStyle = NULL;
3385 }
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003386 if (p_macatsui && gWideFontStyle == NULL)
3387 {
3388 if (ATSUCreateStyle(&gWideFontStyle) != noErr)
3389 gWideFontStyle = NULL;
3390 }
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003391
3392 p_macatsui_last = p_macatsui;
3393}
3394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396/*
3397 * Initialise vim to use the font with the given name. Return FAIL if the font
3398 * could not be loaded, OK otherwise.
3399 */
3400 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003401gui_mch_init_font(char_u *font_name, int fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003403 // TODO: Add support for bold italic underline proportional etc...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 Str255 suggestedFont = "\pMonaco";
Bram Moolenaar05159a02005-02-26 23:04:13 +00003405 int suggestedSize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 FontInfo font_info;
3407 short font_id;
3408 GuiFont font;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003409 char_u used_font_name[512];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003411#ifdef USE_ATSUI_DRAWING
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003412 gui_mac_create_atsui_style();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003413#endif
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (font_name == NULL)
3416 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003417 // First try to get the suggested font
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 GetFNum(suggestedFont, &font_id);
3419
3420 if (font_id == 0)
3421 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003422 // Then pickup the standard application font
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 font_id = GetAppFont();
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003424 STRCPY(used_font_name, "default");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003426 else
3427 STRCPY(used_font_name, "Monaco");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 font = (suggestedSize << 16) + ((long) font_id & 0xFFFF);
3429 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003430 else if (STRCMP(font_name, "*") == 0)
3431 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003432 char_u *new_p_guifont;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003433
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003434 font = gui_mac_select_font(used_font_name);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003435 if (font == NOFONT)
3436 return FAIL;
3437
Bram Moolenaar734a8672019-12-02 22:49:38 +01003438 // Set guifont to the name of the selected font.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003439 new_p_guifont = alloc(STRLEN(used_font_name) + 1);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003440 if (new_p_guifont != NULL)
3441 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003442 STRCPY(new_p_guifont, used_font_name);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003443 vim_free(p_guifont);
3444 p_guifont = new_p_guifont;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003445 // Replace spaces in the font name with underscores.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003446 for ( ; *new_p_guifont; ++new_p_guifont)
3447 {
3448 if (*new_p_guifont == ' ')
3449 *new_p_guifont = '_';
3450 }
3451 }
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 else
3454 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003455 font = gui_mac_find_font(font_name);
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003456 vim_strncpy(used_font_name, font_name, sizeof(used_font_name) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 if (font == NOFONT)
3459 return FAIL;
3460 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 gui.norm_font = font;
3463
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003464 hl_set_font_name(used_font_name);
3465
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003466 TextSize(font >> 16);
3467 TextFont(font & 0xFFFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003469 GetFontInfo(&font_info);
3470
3471 gui.char_ascent = font_info.ascent;
3472 gui.char_width = CharWidth('_');
3473 gui.char_height = font_info.ascent + font_info.descent + p_linespace;
3474
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003475#ifdef USE_ATSUI_DRAWING
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003476 if (p_macatsui && gFontStyle)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003477 gui_mac_set_font_attributes(font);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003478#endif
3479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481}
3482
Bram Moolenaar02743632005-07-25 20:42:36 +00003483/*
3484 * Adjust gui.char_height (after 'linespace' was changed).
3485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003487gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 FontInfo font_info;
3490
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003491 GetFontInfo(&font_info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 gui.char_height = font_info.ascent + font_info.descent + p_linespace;
3493 gui.char_ascent = font_info.ascent + p_linespace / 2;
3494 return OK;
3495}
3496
3497/*
3498 * Get a font structure for highlighting.
3499 */
3500 GuiFont
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003501gui_mch_get_font(char_u *name, int giveErrorIfMissing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 GuiFont font;
3504
3505 font = gui_mac_find_font(name);
3506
3507 if (font == NOFONT)
3508 {
3509 if (giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003510 semsg(_(e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return NOFONT;
3512 }
3513 /*
3514 * TODO : Accept only monospace
3515 */
3516
3517 return font;
3518}
3519
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003520#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003522 * Return the name of font "font" in allocated memory.
3523 * Don't know how to get the actual name, thus use the provided name.
3524 */
3525 char_u *
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003526gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003527{
3528 if (name == NULL)
3529 return NULL;
3530 return vim_strsave(name);
3531}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003532#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003533
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003534#ifdef USE_ATSUI_DRAWING
3535 static void
3536gui_mac_set_font_attributes(GuiFont font)
3537{
3538 ATSUFontID fontID;
3539 Fixed fontSize;
3540 Fixed fontWidth;
3541
3542 fontID = font & 0xFFFF;
3543 fontSize = Long2Fix(font >> 16);
3544 fontWidth = Long2Fix(gui.char_width);
3545
3546 ATSUAttributeTag attribTags[] =
3547 {
3548 kATSUFontTag, kATSUSizeTag, kATSUImposeWidthTag,
3549 kATSUMaxATSUITagValue + 1
3550 };
3551
3552 ByteCount attribSizes[] =
3553 {
3554 sizeof(ATSUFontID), sizeof(Fixed), sizeof(fontWidth),
3555 sizeof(font)
3556 };
3557
3558 ATSUAttributeValuePtr attribValues[] =
3559 {
3560 &fontID, &fontSize, &fontWidth, &font
3561 };
3562
3563 if (FMGetFontFromFontFamilyInstance(fontID, 0, &fontID, NULL) == noErr)
3564 {
3565 if (ATSUSetAttributes(gFontStyle,
3566 (sizeof attribTags) / sizeof(ATSUAttributeTag),
3567 attribTags, attribSizes, attribValues) != noErr)
3568 {
3569# ifndef NDEBUG
3570 fprintf(stderr, "couldn't set font style\n");
3571# endif
3572 ATSUDisposeStyle(gFontStyle);
3573 gFontStyle = NULL;
3574 }
3575
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003576 if (has_mbyte)
3577 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003578 // FIXME: we should use a more mbyte sensitive way to support
3579 // wide font drawing
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003580 fontWidth = Long2Fix(gui.char_width * 2);
3581
3582 if (ATSUSetAttributes(gWideFontStyle,
3583 (sizeof attribTags) / sizeof(ATSUAttributeTag),
3584 attribTags, attribSizes, attribValues) != noErr)
3585 {
3586 ATSUDisposeStyle(gWideFontStyle);
3587 gWideFontStyle = NULL;
3588 }
3589 }
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003590 }
3591}
3592#endif
3593
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 * Set the current text font.
3596 */
3597 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003598gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003600#ifdef USE_ATSUI_DRAWING
3601 GuiFont currFont;
3602 ByteCount actualFontByteCount;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003603
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003604 if (p_macatsui && gFontStyle)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003605 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003606 // Avoid setting same font again
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003607 if (ATSUGetAttribute(gFontStyle, kATSUMaxATSUITagValue + 1,
3608 sizeof(font), &currFont, &actualFontByteCount) == noErr
3609 && actualFontByteCount == (sizeof font))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003610 {
3611 if (currFont == font)
3612 return;
3613 }
3614
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003615 gui_mac_set_font_attributes(font);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003616 }
3617
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003618 if (p_macatsui && !gIsFontFallbackSet)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003619 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003620 // Setup automatic font substitution. The user's guifontwide
3621 // is tried first, then the system tries other fonts.
3622#if 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003623 ATSUAttributeTag fallbackTags[] = { kATSULineFontFallbacksTag };
3624 ByteCount fallbackSizes[] = { sizeof(ATSUFontFallbacks) };
3625 ATSUCreateFontFallbacks(&gFontFallbacks);
3626 ATSUSetObjFontFallbacks(gFontFallbacks, );
Bram Moolenaar734a8672019-12-02 22:49:38 +01003627#endif
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003628 if (gui.wide_font)
3629 {
3630 ATSUFontID fallbackFonts;
3631 gIsFontFallbackSet = TRUE;
3632
3633 if (FMGetFontFromFontFamilyInstance(
3634 (gui.wide_font & 0xFFFF),
3635 0,
3636 &fallbackFonts,
3637 NULL) == noErr)
3638 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003639 ATSUSetFontFallbacks((sizeof fallbackFonts)/sizeof(ATSUFontID),
3640 &fallbackFonts,
3641 kATSUSequentialFallbacksPreferred);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003642 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003643// ATSUAttributeValuePtr fallbackValues[] = { };
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003644 }
3645 }
3646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 TextSize(font >> 16);
3648 TextFont(font & 0xFFFF);
3649}
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651/*
3652 * If a font is not going to be used, free its structure.
3653 */
3654 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003655gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
3657 /*
3658 * Free font when "font" is not 0.
3659 * Nothing to do in the current implementation, since
3660 * nothing is allocated for each font used.
3661 */
3662}
3663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664/*
3665 * Return the Pixel value (color) for the given color name. This routine was
3666 * pretty much taken from example code in the Silicon Graphics OSF/Motif
3667 * Programmer's Guide.
3668 * Return INVALCOLOR when failed.
3669 */
3670 guicolor_T
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003671gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003673 // TODO: Add support for the new named color of MacOS 8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 RGBColor MacColor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaarab302212016-04-26 20:59:29 +02003676 if (STRICMP(name, "hilite") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
Bram Moolenaarab302212016-04-26 20:59:29 +02003678 LMGetHiliteRGB(&MacColor);
3679 return (RGB(MacColor.red >> 8, MacColor.green >> 8, MacColor.blue >> 8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaarab302212016-04-26 20:59:29 +02003681 return gui_get_color_cmn(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682}
3683
Bram Moolenaar26af85d2017-07-23 16:45:10 +02003684 guicolor_T
3685gui_mch_get_rgb_color(int r, int g, int b)
3686{
3687 return gui_get_rgb_color_cmn(r, g, b);
3688}
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690/*
3691 * Set the current text foreground color.
3692 */
3693 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003694gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
3696 RGBColor TheColor;
3697
3698 TheColor.red = Red(color) * 0x0101;
3699 TheColor.green = Green(color) * 0x0101;
3700 TheColor.blue = Blue(color) * 0x0101;
3701
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003702 RGBForeColor(&TheColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703}
3704
3705/*
3706 * Set the current text background color.
3707 */
3708 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003709gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 RGBColor TheColor;
3712
3713 TheColor.red = Red(color) * 0x0101;
3714 TheColor.green = Green(color) * 0x0101;
3715 TheColor.blue = Blue(color) * 0x0101;
3716
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003717 RGBBackColor(&TheColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718}
3719
Bram Moolenaar5fe38612005-11-26 23:45:02 +00003720RGBColor specialColor;
3721
Bram Moolenaar916b7af2005-03-16 09:52:38 +00003722/*
Bram Moolenaar5fe38612005-11-26 23:45:02 +00003723 * Set the current text special color.
Bram Moolenaar916b7af2005-03-16 09:52:38 +00003724 */
3725 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003726gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaar916b7af2005-03-16 09:52:38 +00003727{
Bram Moolenaar5fe38612005-11-26 23:45:02 +00003728 specialColor.red = Red(color) * 0x0101;
3729 specialColor.green = Green(color) * 0x0101;
3730 specialColor.blue = Blue(color) * 0x0101;
3731}
3732
3733/*
3734 * Draw undercurl at the bottom of the character cell.
3735 */
3736 static void
3737draw_undercurl(int flags, int row, int col, int cells)
3738{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003739 int x;
3740 int offset;
3741 const static int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
3742 int y = FILL_Y(row + 1) - 1;
Bram Moolenaar5fe38612005-11-26 23:45:02 +00003743
3744 RGBForeColor(&specialColor);
3745
3746 offset = val[FILL_X(col) % 8];
3747 MoveTo(FILL_X(col), y - offset);
3748
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003749 for (x = FILL_X(col); x < FILL_X(col + cells); ++x)
Bram Moolenaar5fe38612005-11-26 23:45:02 +00003750 {
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003751 offset = val[x % 8];
3752 LineTo(x, y - offset);
Bram Moolenaar5fe38612005-11-26 23:45:02 +00003753 }
Bram Moolenaar916b7af2005-03-16 09:52:38 +00003754}
3755
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003756
3757 static void
3758draw_string_QD(int row, int col, char_u *s, int len, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 char_u *tofree = NULL;
3761
3762 if (output_conv.vc_type != CONV_NONE)
3763 {
3764 tofree = string_convert(&output_conv, s, &len);
3765 if (tofree != NULL)
3766 s = tofree;
3767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 /*
3770 * On OS X, try using Quartz-style text antialiasing.
3771 */
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003772 if (gMacSystemVersion >= 0x1020)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003774 // Quartz antialiasing is available only in OS 10.2 and later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 UInt32 qd_flags = (p_antialias ?
3776 kQDUseCGTextRendering | kQDUseCGTextMetrics : 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003777 QDSwapTextFlags(qd_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003780 /*
3781 * When antialiasing we're using srcOr mode, we have to clear the block
3782 * before drawing the text.
3783 * Also needed when 'linespace' is non-zero to remove the cursor and
3784 * underlining.
3785 * But not when drawing transparently.
3786 * The following is like calling gui_mch_clear_block(row, col, row, col +
3787 * len - 1), but without setting the bg color to gui.back_pixel.
3788 */
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003789 if (((gMacSystemVersion >= 0x1020 && p_antialias) || p_linespace != 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003790 && !(flags & DRAW_TRANSP))
3791 {
3792 Rect rc;
3793
3794 rc.left = FILL_X(col);
3795 rc.top = FILL_Y(row);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003796 // Multibyte computation taken from gui_w32.c
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003797 if (has_mbyte)
3798 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003799 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02003800 rc.right = FILL_X(col + mb_string2cells(s, len));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003801 }
3802 else
Bram Moolenaar13505972019-01-24 15:04:48 +01003803 rc.right = FILL_X(col + len) + (col + len == Columns);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003804 rc.bottom = FILL_Y(row + 1);
3805 EraseRect(&rc);
3806 }
3807
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003808 if (gMacSystemVersion >= 0x1020 && p_antialias)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
3810 StyleParameter face;
3811
3812 face = normal;
3813 if (flags & DRAW_BOLD)
3814 face |= bold;
3815 if (flags & DRAW_UNDERL)
3816 face |= underline;
3817 TextFace(face);
3818
Bram Moolenaar734a8672019-12-02 22:49:38 +01003819 // Quartz antialiasing works only in srcOr transfer mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 TextMode(srcOr);
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 MoveTo(TEXT_X(col), TEXT_Y(row));
3823 DrawText((char*)s, 0, len);
3824 }
3825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003827 // Use old-style, non-antialiased QuickDraw text rendering.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003828 TextMode(srcCopy);
3829 TextFace(normal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
Bram Moolenaar734a8672019-12-02 22:49:38 +01003831 // SelectFont(hdc, gui.currFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 if (flags & DRAW_TRANSP)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003834 TextMode(srcOr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003836 MoveTo(TEXT_X(col), TEXT_Y(row));
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003837 DrawText((char *)s, 0, len);
3838
3839 if (flags & DRAW_BOLD)
3840 {
3841 TextMode(srcOr);
3842 MoveTo(TEXT_X(col) + 1, TEXT_Y(row));
3843 DrawText((char *)s, 0, len);
3844 }
3845
3846 if (flags & DRAW_UNDERL)
3847 {
3848 MoveTo(FILL_X(col), FILL_Y(row + 1) - 1);
3849 LineTo(FILL_X(col + len) - 1, FILL_Y(row + 1) - 1);
3850 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02003851 if (flags & DRAW_STRIKE)
3852 {
3853 MoveTo(FILL_X(col), FILL_Y(row + 1) - gui.char_height/2);
3854 LineTo(FILL_X(col + len) - 1, FILL_Y(row + 1) - gui.char_height/2);
3855 }
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003856 }
3857
3858 if (flags & DRAW_UNDERC)
3859 draw_undercurl(flags, row, col, len);
3860
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003861 vim_free(tofree);
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003862}
3863
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003864#ifdef USE_ATSUI_DRAWING
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003865
3866 static void
3867draw_string_ATSUI(int row, int col, char_u *s, int len, int flags)
3868{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003869 // ATSUI requires utf-16 strings
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003870 UniCharCount utf16_len;
3871 UniChar *tofree = mac_enc_to_utf16(s, len, (size_t *)&utf16_len);
3872 utf16_len /= sizeof(UniChar);
3873
Bram Moolenaar734a8672019-12-02 22:49:38 +01003874 // - ATSUI automatically antialiases text (Someone)
3875 // - for some reason it does not work... (Jussi)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003876#ifdef MAC_ATSUI_DEBUG
3877 fprintf(stderr, "row = %d, col = %d, len = %d: '%c'\n",
3878 row, col, len, len == 1 ? s[0] : ' ');
3879#endif
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003880 /*
3881 * When antialiasing we're using srcOr mode, we have to clear the block
3882 * before drawing the text.
3883 * Also needed when 'linespace' is non-zero to remove the cursor and
3884 * underlining.
3885 * But not when drawing transparently.
3886 * The following is like calling gui_mch_clear_block(row, col, row, col +
3887 * len - 1), but without setting the bg color to gui.back_pixel.
3888 */
3889 if ((flags & DRAW_TRANSP) == 0)
3890 {
3891 Rect rc;
3892
3893 rc.left = FILL_X(col);
3894 rc.top = FILL_Y(row);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003895 // Multibyte computation taken from gui_w32.c
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003896 if (has_mbyte)
3897 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003898 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02003899 rc.right = FILL_X(col + mb_string2cells(s, len));
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003900 }
3901 else
3902 rc.right = FILL_X(col + len) + (col + len == Columns);
3903
3904 rc.bottom = FILL_Y(row + 1);
3905 EraseRect(&rc);
3906 }
3907
3908 {
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003909 TextMode(srcCopy);
3910 TextFace(normal);
3911
Bram Moolenaar734a8672019-12-02 22:49:38 +01003912 // SelectFont(hdc, gui.currFont);
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003913 if (flags & DRAW_TRANSP)
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003914 TextMode(srcOr);
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003915
3916 MoveTo(TEXT_X(col), TEXT_Y(row));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003917
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003918 if (gFontStyle && flags & DRAW_BOLD)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003919 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003920 Boolean attValue = true;
3921 ATSUAttributeTag attribTags[] = { kATSUQDBoldfaceTag };
3922 ByteCount attribSizes[] = { sizeof(Boolean) };
3923 ATSUAttributeValuePtr attribValues[] = { &attValue };
3924
3925 ATSUSetAttributes(gFontStyle, 1, attribTags, attribSizes, attribValues);
3926 }
3927
Bram Moolenaar76b96fc2010-07-17 16:44:59 +02003928 UInt32 useAntialias = p_antialias ? kATSStyleApplyAntiAliasing
3929 : kATSStyleNoAntiAliasing;
3930 if (useAntialias != useAntialias_cached)
3931 {
3932 ATSUAttributeTag attribTags[] = { kATSUStyleRenderingOptionsTag };
3933 ByteCount attribSizes[] = { sizeof(UInt32) };
3934 ATSUAttributeValuePtr attribValues[] = { &useAntialias };
3935
3936 if (gFontStyle)
3937 ATSUSetAttributes(gFontStyle, 1, attribTags,
3938 attribSizes, attribValues);
3939 if (gWideFontStyle)
3940 ATSUSetAttributes(gWideFontStyle, 1, attribTags,
3941 attribSizes, attribValues);
3942
3943 useAntialias_cached = useAntialias;
3944 }
3945
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003946 if (has_mbyte)
3947 {
3948 int n, width_in_cell, last_width_in_cell;
3949 UniCharArrayOffset offset = 0;
3950 UniCharCount yet_to_draw = 0;
3951 ATSUTextLayout textLayout;
3952 ATSUStyle textStyle;
3953
3954 last_width_in_cell = 1;
3955 ATSUCreateTextLayout(&textLayout);
3956 ATSUSetTextPointerLocation(textLayout, tofree,
3957 kATSUFromTextBeginning,
3958 kATSUToTextEnd, utf16_len);
3959 /*
3960 ATSUSetRunStyle(textLayout, gFontStyle,
3961 kATSUFromTextBeginning, kATSUToTextEnd); */
3962
Bram Moolenaar734a8672019-12-02 22:49:38 +01003963 // Compute the length in display cells.
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003964 for (n = 0; n < len; n += MB_BYTE2LEN(s[n]))
3965 {
3966 width_in_cell = (*mb_ptr2cells)(s + n);
3967
Bram Moolenaar734a8672019-12-02 22:49:38 +01003968 // probably we are switching from single byte character
3969 // to multibyte characters (which requires more than one
3970 // cell to draw)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003971 if (width_in_cell != last_width_in_cell)
3972 {
3973#ifdef MAC_ATSUI_DEBUG
3974 fprintf(stderr, "\tn = %2d, (%d-%d), offset = %d, yet_to_draw = %d\n",
3975 n, last_width_in_cell, width_in_cell, offset, yet_to_draw);
3976#endif
3977 textStyle = last_width_in_cell > 1 ? gWideFontStyle
3978 : gFontStyle;
3979
3980 ATSUSetRunStyle(textLayout, textStyle, offset, yet_to_draw);
3981 offset += yet_to_draw;
3982 yet_to_draw = 0;
3983 last_width_in_cell = width_in_cell;
3984 }
3985
3986 yet_to_draw++;
3987 }
3988
3989 if (yet_to_draw)
3990 {
3991#ifdef MAC_ATSUI_DEBUG
3992 fprintf(stderr, "\tn = %2d, (%d-%d), offset = %d, yet_to_draw = %d\n",
3993 n, last_width_in_cell, width_in_cell, offset, yet_to_draw);
3994#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003995 // finish the rest style
Bram Moolenaar1b60e502008-03-12 13:40:54 +00003996 textStyle = width_in_cell > 1 ? gWideFontStyle : gFontStyle;
3997 ATSUSetRunStyle(textLayout, textStyle, offset, kATSUToTextEnd);
3998 }
3999
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004000 ATSUSetTransientFontMatching(textLayout, TRUE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004001 ATSUDrawText(textLayout,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004002 kATSUFromTextBeginning, kATSUToTextEnd,
4003 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004004 ATSUDisposeTextLayout(textLayout);
4005 }
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004006 else
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004007 {
4008 ATSUTextLayout textLayout;
4009
4010 if (ATSUCreateTextLayoutWithTextPtr(tofree,
4011 kATSUFromTextBeginning, kATSUToTextEnd,
4012 utf16_len,
4013 (gFontStyle ? 1 : 0), &utf16_len,
4014 (gFontStyle ? &gFontStyle : NULL),
4015 &textLayout) == noErr)
4016 {
4017 ATSUSetTransientFontMatching(textLayout, TRUE);
4018
4019 ATSUDrawText(textLayout,
4020 kATSUFromTextBeginning, kATSUToTextEnd,
4021 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
4022
4023 ATSUDisposeTextLayout(textLayout);
4024 }
4025 }
4026
Bram Moolenaar734a8672019-12-02 22:49:38 +01004027 // drawing is done, now reset bold to normal
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004028 if (gFontStyle && flags & DRAW_BOLD)
4029 {
4030 Boolean attValue = false;
4031
4032 ATSUAttributeTag attribTags[] = { kATSUQDBoldfaceTag };
4033 ByteCount attribSizes[] = { sizeof(Boolean) };
4034 ATSUAttributeValuePtr attribValues[] = { &attValue };
4035
4036 ATSUSetAttributes(gFontStyle, 1, attribTags, attribSizes,
4037 attribValues);
4038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040
Bram Moolenaar5fe38612005-11-26 23:45:02 +00004041 if (flags & DRAW_UNDERC)
4042 draw_undercurl(flags, row, col, len);
4043
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 vim_free(tofree);
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004045}
4046#endif
4047
4048 void
4049gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
4050{
4051#if defined(USE_ATSUI_DRAWING)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004052 if (p_macatsui == 0 && p_macatsui_last != 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004053 // switch from macatsui to nomacatsui
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004054 gui_mac_dispose_atsui_style();
4055 else if (p_macatsui != 0 && p_macatsui_last == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004056 // switch from nomacatsui to macatsui
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004057 gui_mac_create_atsui_style();
4058
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004059 if (p_macatsui)
4060 draw_string_ATSUI(row, col, s, len, flags);
4061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062#endif
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004063 draw_string_QD(row, col, s, len, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064}
4065
4066/*
4067 * Return OK if the key with the termcap name "name" is supported.
4068 */
4069 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004070gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
4072 int i;
4073
4074 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
4075 if (name[0] == special_keys[i].vim_code0 &&
4076 name[1] == special_keys[i].vim_code1)
4077 return OK;
4078 return FAIL;
4079}
4080
4081 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004082gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004084 SysBeep(1); // Should this be 0? (????)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085}
4086
4087 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004088gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004090 // Do a visual beep by reversing the foreground and background colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 Rect rc;
4092
4093 /*
4094 * Note: InvertRect() excludes right and bottom of rectangle.
4095 */
4096 rc.left = 0;
4097 rc.top = 0;
4098 rc.right = gui.num_cols * gui.char_width;
4099 rc.bottom = gui.num_rows * gui.char_height;
4100 InvertRect(&rc);
4101
Bram Moolenaar734a8672019-12-02 22:49:38 +01004102 ui_delay((long)msec, TRUE); // wait for some msec
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
4104 InvertRect(&rc);
4105}
4106
4107/*
4108 * Invert a rectangle from row r, column c, for nr rows and nc columns.
4109 */
4110 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004111gui_mch_invert_rectangle(int r, int c, int nr, int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 Rect rc;
4114
4115 /*
4116 * Note: InvertRect() excludes right and bottom of rectangle.
4117 */
4118 rc.left = FILL_X(c);
4119 rc.top = FILL_Y(r);
4120 rc.right = rc.left + nc * gui.char_width;
4121 rc.bottom = rc.top + nr * gui.char_height;
4122 InvertRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123}
4124
4125/*
4126 * Iconify the GUI window.
4127 */
4128 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004129gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004131 // TODO: find out what could replace iconify
4132 // -window shade?
4133 // -hide application?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134}
4135
4136#if defined(FEAT_EVAL) || defined(PROTO)
4137/*
4138 * Bring the Vim window to the foreground.
4139 */
4140 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004141gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004143 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144}
4145#endif
4146
4147/*
4148 * Draw a cursor without focus.
4149 */
4150 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004151gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 Rect rc;
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 /*
4156 * Note: FrameRect() excludes right and bottom of rectangle.
4157 */
4158 rc.left = FILL_X(gui.col);
4159 rc.top = FILL_Y(gui.row);
4160 rc.right = rc.left + gui.char_width;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004161 if (mb_lefthalve(gui.row, gui.col))
4162 rc.right += gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 rc.bottom = rc.top + gui.char_height;
4164
4165 gui_mch_set_fg_color(color);
4166
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004167 FrameRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168}
4169
4170/*
4171 * Draw part of a cursor, only w pixels wide, and h pixels high.
4172 */
4173 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004174gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 Rect rc;
4177
4178#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01004179 // vertical line should be on the right of current point
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 if (CURSOR_BAR_RIGHT)
4181 rc.left = FILL_X(gui.col + 1) - w;
4182 else
4183#endif
4184 rc.left = FILL_X(gui.col);
4185 rc.top = FILL_Y(gui.row) + gui.char_height - h;
4186 rc.right = rc.left + w;
4187 rc.bottom = rc.top + h;
4188
4189 gui_mch_set_fg_color(color);
4190
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004191 FrameRect(&rc);
4192// PaintRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193}
4194
4195
4196
4197/*
4198 * Catch up with any queued X events. This may put keyboard input into the
4199 * input buffer, call resize call-backs, trigger timers etc. If there is
4200 * nothing in the X event queue (& no timers pending), then we return
4201 * immediately.
4202 */
4203 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004204gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004206 // TODO: find what to do
4207 // maybe call gui_mch_wait_for_chars (0)
4208 // more like look at EventQueue then
4209 // call heart of gui_mch_wait_for_chars;
4210 //
4211 // if (eventther)
4212 // gui_mac_handle_event(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 EventRecord theEvent;
4214
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004215 if (EventAvail(everyEvent, &theEvent))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 if (theEvent.what != nullEvent)
4217 gui_mch_wait_for_chars(0);
4218}
4219
4220/*
4221 * Simple wrapper to neglect more easily the time
4222 * spent inside WaitNextEvent while profiling.
4223 */
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 pascal
4226 Boolean
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004227WaitNextEventWrp(EventMask eventMask, EventRecord *theEvent, UInt32 sleep, RgnHandle mouseRgn)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
4229 if (((long) sleep) < -1)
4230 sleep = 32767;
4231 return WaitNextEvent(eventMask, theEvent, sleep, mouseRgn);
4232}
4233
4234/*
4235 * GUI input routine called by gui_wait_for_chars(). Waits for a character
4236 * from the keyboard.
4237 * wtime == -1 Wait forever.
4238 * wtime == 0 This should never happen.
4239 * wtime > 0 Wait wtime milliseconds for a character.
4240 * Returns OK if a character was found to be available within the given time,
4241 * or FAIL otherwise.
4242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004244gui_mch_wait_for_chars(int wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 EventMask mask = (everyEvent);
4247 EventRecord event;
4248 long entryTick;
4249 long currentTick;
4250 long sleeppyTick;
4251
Bram Moolenaar734a8672019-12-02 22:49:38 +01004252 // If we are providing life feedback with the scrollbar,
4253 // we don't want to try to wait for an event, or else
4254 // there won't be any life feedback.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (dragged_sb != NULL)
4256 return FAIL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004257 // TODO: Check if FAIL is the proper return code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 entryTick = TickCount();
4260
4261 allow_scrollbar = TRUE;
4262
4263 do
4264 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004265#if 0
4266 if (dragRectControl == kCreateEmpty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
4268 dragRgn = NULL;
4269 dragRectControl = kNothing;
4270 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004271 else
4272#endif
4273 if (dragRectControl == kCreateRect)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 dragRgn = cursorRgn;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004276 RectRgn(dragRgn, &dragRect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 dragRectControl = kNothing;
4278 }
4279 /*
4280 * Don't use gui_mch_update() because then we will spin-lock until a
4281 * char arrives, instead we use WaitNextEventWrp() to hang until an
4282 * event arrives. No need to check for input_buf_full because we are
4283 * returning as soon as it contains a single char.
4284 */
Bram Moolenaar734a8672019-12-02 22:49:38 +01004285 // TODO: reduce wtime accordingly???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (wtime > -1)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004287 sleeppyTick = 60 * wtime / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 else
4289 sleeppyTick = 32767;
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004290
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004291 if (WaitNextEventWrp(mask, &event, sleeppyTick, dragRgn))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00004293 gui_mac_handle_event(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (input_available())
4295 {
4296 allow_scrollbar = FALSE;
4297 return OK;
4298 }
4299 }
4300 currentTick = TickCount();
4301 }
4302 while ((wtime == -1) || ((currentTick - entryTick) < 60*wtime/1000));
4303
4304 allow_scrollbar = FALSE;
4305 return FAIL;
4306}
4307
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308/*
4309 * Output routines.
4310 */
4311
Bram Moolenaar734a8672019-12-02 22:49:38 +01004312/*
4313 * Flush any output to the screen
4314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004316gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004318 // TODO: Is anything needed here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319}
4320
4321/*
4322 * Clear a rectangular region of the screen from text pos (row1, col1) to
4323 * (row2, col2) inclusive.
4324 */
4325 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004326gui_mch_clear_block(int row1, int col1, int row2, int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 Rect rc;
4329
4330 /*
4331 * Clear one extra pixel at the far right, for when bold characters have
4332 * spilled over to the next column.
4333 */
4334 rc.left = FILL_X(col1);
4335 rc.top = FILL_Y(row1);
4336 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
4337 rc.bottom = FILL_Y(row2 + 1);
4338
4339 gui_mch_set_bg_color(gui.back_pixel);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004340 EraseRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341}
4342
4343/*
4344 * Clear the whole text window.
4345 */
4346 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004347gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
4349 Rect rc;
4350
4351 rc.left = 0;
4352 rc.top = 0;
4353 rc.right = Columns * gui.char_width + 2 * gui.border_width;
4354 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
4355
4356 gui_mch_set_bg_color(gui.back_pixel);
4357 EraseRect(&rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004358// gui_mch_set_fg_color(gui.norm_pixel);
4359// FrameRect(&rc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360}
4361
4362/*
4363 * Delete the given number of lines from the given row, scrolling up any
4364 * text further down within the scroll region.
4365 */
4366 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004367gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
4369 Rect rc;
4370
Bram Moolenaar734a8672019-12-02 22:49:38 +01004371 // changed without checking!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 rc.left = FILL_X(gui.scroll_region_left);
4373 rc.right = FILL_X(gui.scroll_region_right + 1);
4374 rc.top = FILL_Y(row);
4375 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
4376
4377 gui_mch_set_bg_color(gui.back_pixel);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004378 ScrollRect(&rc, 0, -num_lines * gui.char_height, (RgnHandle) nil);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
4381 gui.scroll_region_left,
4382 gui.scroll_region_bot, gui.scroll_region_right);
4383}
4384
4385/*
4386 * Insert the given number of lines before the given row, scrolling down any
4387 * following text within the scroll region.
4388 */
4389 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004390gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
4392 Rect rc;
4393
4394 rc.left = FILL_X(gui.scroll_region_left);
4395 rc.right = FILL_X(gui.scroll_region_right + 1);
4396 rc.top = FILL_Y(row);
4397 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
4398
4399 gui_mch_set_bg_color(gui.back_pixel);
4400
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004401 ScrollRect(&rc, 0, gui.char_height * num_lines, (RgnHandle) nil);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaar734a8672019-12-02 22:49:38 +01004403 // Update gui.cursor_row if the cursor scrolled or copied over
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (gui.cursor_row >= gui.row
4405 && gui.cursor_col >= gui.scroll_region_left
4406 && gui.cursor_col <= gui.scroll_region_right)
4407 {
4408 if (gui.cursor_row <= gui.scroll_region_bot - num_lines)
4409 gui.cursor_row += num_lines;
4410 else if (gui.cursor_row <= gui.scroll_region_bot)
4411 gui.cursor_is_valid = FALSE;
4412 }
4413
4414 gui_clear_block(row, gui.scroll_region_left,
4415 row + num_lines - 1, gui.scroll_region_right);
4416}
4417
4418 /*
4419 * TODO: add a vim format to the clipboard which remember
4420 * LINEWISE, CHARWISE, BLOCKWISE
4421 */
4422
4423 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02004424clip_mch_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426
4427 Handle textOfClip;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004428 int flavor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 Size scrapSize;
4430 ScrapFlavorFlags scrapFlags;
4431 ScrapRef scrap = nil;
4432 OSStatus error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 int type;
4434 char *searchCR;
4435 char_u *tempclip;
4436
4437
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004438 error = GetCurrentScrap(&scrap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (error != noErr)
4440 return;
4441
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004442 error = GetScrapFlavorFlags(scrap, VIMSCRAPFLAVOR, &scrapFlags);
4443 if (error == noErr)
4444 {
4445 error = GetScrapFlavorSize(scrap, VIMSCRAPFLAVOR, &scrapSize);
4446 if (error == noErr && scrapSize > 1)
4447 flavor = 1;
4448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004450 if (flavor == 0)
4451 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004452 error = GetScrapFlavorFlags(scrap, SCRAPTEXTFLAVOR, &scrapFlags);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004453 if (error != noErr)
4454 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004456 error = GetScrapFlavorSize(scrap, SCRAPTEXTFLAVOR, &scrapSize);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004457 if (error != noErr)
4458 return;
4459 }
4460
4461 ReserveMem(scrapSize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Bram Moolenaar734a8672019-12-02 22:49:38 +01004463 // In CARBON we don't need a Handle, a pointer is good
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004464 textOfClip = NewHandle(scrapSize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
Bram Moolenaar734a8672019-12-02 22:49:38 +01004466 // tempclip = alloc(scrapSize+1);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004467 HLock(textOfClip);
4468 error = GetScrapFlavorData(scrap,
4469 flavor ? VIMSCRAPFLAVOR : SCRAPTEXTFLAVOR,
4470 &scrapSize, *textOfClip);
4471 scrapSize -= flavor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004473 if (flavor)
4474 type = **textOfClip;
4475 else
Bram Moolenaard44347f2011-06-19 01:14:29 +02004476 type = MAUTO;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004477
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004478 tempclip = alloc(scrapSize + 1);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004479 mch_memmove(tempclip, *textOfClip + flavor, scrapSize);
4480 tempclip[scrapSize] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00004482#ifdef MACOS_CONVERT
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004483 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004484 // Convert from utf-16 (clipboard)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004485 size_t encLen = 0;
4486 char_u *to = mac_utf16_to_enc((UniChar *)tempclip, scrapSize, &encLen);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004487
4488 if (to != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004490 scrapSize = encLen;
4491 vim_free(tempclip);
4492 tempclip = to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00004496
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004497 searchCR = (char *)tempclip;
4498 while (searchCR != NULL)
4499 {
4500 searchCR = strchr(searchCR, '\r');
4501 if (searchCR != NULL)
4502 *searchCR = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004504
4505 clip_yank_selection(type, tempclip, scrapSize, cbd);
4506
4507 vim_free(tempclip);
4508 HUnlock(textOfClip);
4509
4510 DisposeHandle(textOfClip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511}
4512
4513 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02004514clip_mch_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
4516 /*
4517 * TODO: Really nothing to do?
4518 */
4519}
4520
4521 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02004522clip_mch_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523{
4524 return OK;
4525}
4526
4527/*
4528 * Send the current selection to the clipboard.
4529 */
4530 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02004531clip_mch_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532{
4533 Handle textOfClip;
4534 long scrapSize;
4535 int type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 ScrapRef scrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 char_u *str = NULL;
4539
4540 if (!cbd->owned)
4541 return;
4542
4543 clip_get_selection(cbd);
4544
4545 /*
4546 * Once we set the clipboard, lose ownership. If another application sets
4547 * the clipboard, we don't want to think that we still own it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 cbd->owned = FALSE;
4550
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004551 type = clip_convert_selection(&str, (long_u *)&scrapSize, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00004553#ifdef MACOS_CONVERT
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004554 size_t utf16_len = 0;
4555 UniChar *to = mac_enc_to_utf16(str, scrapSize, &utf16_len);
4556 if (to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004558 scrapSize = utf16_len;
4559 vim_free(str);
4560 str = (char_u *)to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
4562#endif
4563
4564 if (type >= 0)
4565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 ClearCurrentScrap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004568 textOfClip = NewHandle(scrapSize + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 HLock(textOfClip);
4570
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004571 **textOfClip = type;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004572 mch_memmove(*textOfClip + 1, str, scrapSize);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004573 GetCurrentScrap(&scrap);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004574 PutScrapFlavor(scrap, SCRAPTEXTFLAVOR, kScrapFlavorMaskNone,
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004575 scrapSize, *textOfClip + 1);
4576 PutScrapFlavor(scrap, VIMSCRAPFLAVOR, kScrapFlavorMaskNone,
4577 scrapSize + 1, *textOfClip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 HUnlock(textOfClip);
4579 DisposeHandle(textOfClip);
4580 }
4581
4582 vim_free(str);
4583}
4584
4585 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004586gui_mch_set_text_area_pos(int x, int y, int w, int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587{
4588 Rect VimBound;
4589
Bram Moolenaar734a8672019-12-02 22:49:38 +01004590// HideWindow(gui.VimWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 GetWindowBounds(gui.VimWindow, kWindowGlobalPortRgn, &VimBound);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 if (gui.which_scrollbars[SBAR_LEFT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 VimBound.left = -gui.scrollbar_width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 VimBound.left = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 SetWindowBounds(gui.VimWindow, kWindowGlobalPortRgn, &VimBound);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004600 ShowWindow(gui.VimWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601}
4602
4603/*
4604 * Menu stuff.
4605 */
4606
4607 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004608gui_mch_enable_menu(int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 /*
Bram Moolenaar02743632005-07-25 20:42:36 +00004611 * Menu is always active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 */
4613}
4614
4615 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004616gui_mch_set_menu_pos(int x, int y, int w, int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 /*
Bram Moolenaar02743632005-07-25 20:42:36 +00004619 * The menu is always at the top of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 */
4621}
4622
4623/*
4624 * Add a sub menu to the menu bar.
4625 */
4626 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004627gui_mch_add_menu(vimmenu_T *menu, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 /*
4630 * TODO: Try to use only menu_id instead of both menu_id and menu_handle.
4631 * TODO: use menu->mnemonic and menu->actext
4632 * TODO: Try to reuse menu id
4633 * Carbon Help suggest to use only id between 1 and 235
4634 */
4635 static long next_avail_id = 128;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004636 long menu_after_me = 0; // Default to the end
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004637 CFStringRef name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 short index;
4639 vimmenu_T *parent = menu->parent;
4640 vimmenu_T *brother = menu->next;
4641
Bram Moolenaar734a8672019-12-02 22:49:38 +01004642 // Cannot add a menu if ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if ((parent != NULL && parent->submenu_id == 0))
4644 return;
4645
Bram Moolenaar734a8672019-12-02 22:49:38 +01004646 // menu ID greater than 1024 are reserved for ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (next_avail_id == 1024)
4648 return;
4649
Bram Moolenaar734a8672019-12-02 22:49:38 +01004650 // My brother could be the PopUp, find my real brother
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 while ((brother != NULL) && (!menu_is_menubar(brother->name)))
4652 brother = brother->next;
4653
Bram Moolenaar734a8672019-12-02 22:49:38 +01004654 // Find where to insert the menu (for MenuBar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if ((parent == NULL) && (brother != NULL))
4656 menu_after_me = brother->submenu_id;
4657
Bram Moolenaar734a8672019-12-02 22:49:38 +01004658 // If the menu is not part of the menubar (and its submenus), add it 'nowhere'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (!menu_is_menubar(menu->name))
4660 menu_after_me = hierMenu;
4661
Bram Moolenaar734a8672019-12-02 22:49:38 +01004662 // Convert the name
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00004663#ifdef MACOS_CONVERT
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004664 name = menu_title_removing_mnemonic(menu);
4665#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 name = C2Pascal_save(menu->dname);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (name == NULL)
4669 return;
4670
Bram Moolenaar734a8672019-12-02 22:49:38 +01004671 // Create the menu unless it's the help menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004673 // Carbon suggest use of
4674 // OSStatus CreateNewMenu(MenuID, MenuAttributes, MenuRef *);
4675 // OSStatus SetMenuTitle(MenuRef, ConstStr255Param title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 menu->submenu_id = next_avail_id;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004677 if (CreateNewMenu(menu->submenu_id, 0, (MenuRef *)&menu->submenu_handle) == noErr)
4678 SetMenuTitleWithCFString((MenuRef)menu->submenu_handle, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 next_avail_id++;
4680 }
4681
4682 if (parent == NULL)
4683 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004684 // Adding a menu to the menubar, or in the no mans land (for PopUp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
Bram Moolenaar734a8672019-12-02 22:49:38 +01004686 // TODO: Verify if we could only Insert Menu if really part of the
4687 // menubar The Inserted menu are scanned or the Command-key combos
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
Bram Moolenaar734a8672019-12-02 22:49:38 +01004689 // Insert the menu
4690 InsertMenu(menu->submenu_handle, menu_after_me); // insert before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691#if 1
Bram Moolenaar734a8672019-12-02 22:49:38 +01004692 // Vim should normally update it. TODO: verify
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 DrawMenuBar();
4694#endif
4695 }
4696 else
4697 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004698 // Adding as a submenu
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004700 index = gui_mac_get_menu_item_index(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
Bram Moolenaar734a8672019-12-02 22:49:38 +01004702 // Call InsertMenuItem followed by SetMenuItemText
4703 // to avoid special character recognition by InsertMenuItem
4704 InsertMenuItem(parent->submenu_handle, "\p ", idx); // afterItem
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004705 SetMenuItemTextWithCFString(parent->submenu_handle, idx+1, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 SetItemCmd(parent->submenu_handle, idx+1, 0x1B);
4707 SetItemMark(parent->submenu_handle, idx+1, menu->submenu_id);
4708 InsertMenu(menu->submenu_handle, hierMenu);
4709 }
4710
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004711 CFRelease(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01004714 // Done by Vim later on
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 DrawMenuBar();
4716#endif
4717}
4718
4719/*
4720 * Add a menu item to a menu
4721 */
4722 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004723gui_mch_add_menu_item(vimmenu_T *menu, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004725 CFStringRef name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 vimmenu_T *parent = menu->parent;
4727 int menu_inserted;
4728
Bram Moolenaar734a8672019-12-02 22:49:38 +01004729 // Cannot add item, if the menu have not been created
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (parent->submenu_id == 0)
4731 return;
4732
Bram Moolenaar734a8672019-12-02 22:49:38 +01004733 // Could call SetMenuRefCon [CARBON] to associate with the Menu,
4734 // for older OS call GetMenuItemData (menu, item, isCommandID?, data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
Bram Moolenaar734a8672019-12-02 22:49:38 +01004736 // Convert the name
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00004737#ifdef MACOS_CONVERT
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004738 name = menu_title_removing_mnemonic(menu);
4739#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 name = C2Pascal_save(menu->dname);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaar734a8672019-12-02 22:49:38 +01004743 // Where are just a menu item, so no handle, no id
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 menu->submenu_id = 0;
4745 menu->submenu_handle = NULL;
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 menu_inserted = 0;
4748 if (menu->actext)
4749 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004750 // If the accelerator text for the menu item looks like it describes
4751 // a command key (e.g., "<D-S-t>" or "<C-7>"), display it as the
4752 // item's command equivalent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 int key = 0;
4754 int modifiers = 0;
4755 char_u *p_actext;
4756
4757 p_actext = menu->actext;
Bram Moolenaar459fd782019-10-13 16:43:39 +02004758 key = find_special_key(&p_actext, &modifiers, FALSE, FALSE, FALSE,
4759 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (*p_actext != 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004761 key = 0; // error: trailing text
4762 // find_special_key() returns a keycode with as many of the
4763 // specified modifiers as appropriate already applied (e.g., for
4764 // "<D-C-x>" it returns Ctrl-X as the keycode and MOD_MASK_CMD
4765 // as the only modifier). Since we want to display all of the
4766 // modifiers, we need to convert the keycode back to a printable
4767 // character plus modifiers.
4768 // TODO: Write an alternative find_special_key() that doesn't
4769 // apply modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (key > 0 && key < 32)
4771 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004772 // Convert a control key to an uppercase letter. Note that
4773 // by this point it is no longer possible to distinguish
4774 // between, e.g., Ctrl-S and Ctrl-Shift-S.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 modifiers |= MOD_MASK_CTRL;
4776 key += '@';
4777 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004778 // If the keycode is an uppercase letter, set the Shift modifier.
4779 // If it is a lowercase letter, don't set the modifier, but convert
4780 // the letter to uppercase for display in the menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else if (key >= 'A' && key <= 'Z')
4782 modifiers |= MOD_MASK_SHIFT;
4783 else if (key >= 'a' && key <= 'z')
4784 key += 'A' - 'a';
Bram Moolenaar734a8672019-12-02 22:49:38 +01004785 // Note: keycodes below 0x22 are reserved by Apple.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 if (key >= 0x22 && vim_isprintc_strict(key))
4787 {
4788 int valid = 1;
4789 char_u mac_mods = kMenuNoModifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004790 // Convert Vim modifier codes to Menu Manager equivalents.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (modifiers & MOD_MASK_SHIFT)
4792 mac_mods |= kMenuShiftModifier;
4793 if (modifiers & MOD_MASK_CTRL)
4794 mac_mods |= kMenuControlModifier;
4795 if (!(modifiers & MOD_MASK_CMD))
4796 mac_mods |= kMenuNoCommandModifier;
4797 if (modifiers & MOD_MASK_ALT || modifiers & MOD_MASK_MULTI_CLICK)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004798 valid = 0; // TODO: will Alt someday map to Option?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (valid)
4800 {
4801 char_u item_txt[10];
Bram Moolenaar734a8672019-12-02 22:49:38 +01004802 // Insert the menu item after idx, with its command key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 item_txt[0] = 3; item_txt[1] = ' '; item_txt[2] = '/';
4804 item_txt[3] = key;
4805 InsertMenuItem(parent->submenu_handle, item_txt, idx);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004806 // Set the modifier keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 SetMenuItemModifiers(parent->submenu_handle, idx+1, mac_mods);
4808 menu_inserted = 1;
4809 }
4810 }
4811 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004812 // Call InsertMenuItem followed by SetMenuItemText
4813 // to avoid special character recognition by InsertMenuItem
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if (!menu_inserted)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004815 InsertMenuItem(parent->submenu_handle, "\p ", idx); // afterItem
4816 // Set the menu item name.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004817 SetMenuItemTextWithCFString(parent->submenu_handle, idx+1, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
4819#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01004820 // Called by Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 DrawMenuBar();
4822#endif
4823
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004824 CFRelease(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825}
4826
4827 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004828gui_mch_toggle_tearoffs(int enable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004830 // no tearoff menus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831}
4832
4833/*
4834 * Destroy the machine specific menu widget.
4835 */
4836 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004837gui_mch_destroy_menu(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004839 short index = gui_mac_get_menu_item_index(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
4841 if (index > 0)
4842 {
4843 if (menu->parent)
4844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004846 // For now just don't delete help menu items. (Huh? Dany)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004847 DeleteMenuItem(menu->parent->submenu_handle, index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaar734a8672019-12-02 22:49:38 +01004849 // Delete the Menu if it was a hierarchical Menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (menu->submenu_id != 0)
4851 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004852 DeleteMenu(menu->submenu_id);
4853 DisposeMenu(menu->submenu_handle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857#ifdef DEBUG_MAC_MENU
4858 else
4859 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004860 printf("gmdm 2\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862#endif
4863 }
4864 else
4865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004867 DeleteMenu(menu->submenu_id);
4868 DisposeMenu(menu->submenu_handle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
4870 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004871 // Shouldn't this be already done by Vim. TODO: Check
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 DrawMenuBar();
4873}
4874
4875/*
4876 * Make a menu either grey or not grey.
4877 */
4878 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004879gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004881 // TODO: Check if menu really exists
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004882 short index = gui_mac_get_menu_item_index(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883/*
4884 index = menu->index;
4885*/
4886 if (grey)
4887 {
4888 if (menu->children)
4889 DisableMenuItem(menu->submenu_handle, index);
4890 if (menu->parent)
4891 if (menu->parent->submenu_handle)
4892 DisableMenuItem(menu->parent->submenu_handle, index);
4893 }
4894 else
4895 {
4896 if (menu->children)
4897 EnableMenuItem(menu->submenu_handle, index);
4898 if (menu->parent)
4899 if (menu->parent->submenu_handle)
4900 EnableMenuItem(menu->parent->submenu_handle, index);
4901 }
4902}
4903
4904/*
4905 * Make menu item hidden or not hidden
4906 */
4907 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004908gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004910 // There's no hidden mode on MacOS
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004911 gui_mch_menu_grey(menu, hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912}
4913
4914
4915/*
4916 * This is called after setting all the menus to grey/hidden or not.
4917 */
4918 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004919gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 DrawMenuBar();
4922}
4923
4924
4925/*
4926 * Scrollbar stuff.
4927 */
4928
4929 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004930gui_mch_enable_scrollbar(
4931 scrollbar_T *sb,
4932 int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 if (flag)
4935 ShowControl(sb->id);
4936 else
4937 HideControl(sb->id);
4938
4939#ifdef DEBUG_MAC_SB
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004940 printf("enb_sb (%x) %x\n",sb->id, flag);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#endif
4942}
4943
4944 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004945gui_mch_set_scrollbar_thumb(
4946 scrollbar_T *sb,
4947 long val,
4948 long size,
4949 long max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 SetControl32BitMaximum (sb->id, max);
4952 SetControl32BitMinimum (sb->id, 0);
4953 SetControl32BitValue (sb->id, val);
Bram Moolenaarbbe8c3f2007-04-26 16:40:56 +00004954 SetControlViewSize (sb->id, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#ifdef DEBUG_MAC_SB
Bram Moolenaarea034592016-06-02 22:27:08 +02004956 printf("thumb_sb (%x) %lx, %lx,%lx\n",sb->id, val, size, max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#endif
4958}
4959
4960 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004961gui_mch_set_scrollbar_pos(
4962 scrollbar_T *sb,
4963 int x,
4964 int y,
4965 int w,
4966 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 gui_mch_set_bg_color(gui.back_pixel);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004969#if 0
4970 if (gui.which_scrollbars[SBAR_LEFT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004972 MoveControl(sb->id, x-16, y);
4973 SizeControl(sb->id, w + 1, h);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
4975 else
4976 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004977 MoveControl(sb->id, x, y);
4978 SizeControl(sb->id, w + 1, h);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004979 }
4980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (sb == &gui.bottom_sbar)
4982 h += 1;
4983 else
4984 w += 1;
4985
4986 if (gui.which_scrollbars[SBAR_LEFT])
4987 x -= 15;
4988
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004989 MoveControl(sb->id, x, y);
4990 SizeControl(sb->id, w, h);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#ifdef DEBUG_MAC_SB
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004992 printf("size_sb (%x) %x, %x, %x, %x\n",sb->id, x, y, w, h);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
4994}
4995
4996 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004997gui_mch_create_scrollbar(
4998 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01004999 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
5001 Rect bounds;
5002
5003 bounds.top = -16;
5004 bounds.bottom = -10;
5005 bounds.right = -10;
5006 bounds.left = -16;
5007
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005008 sb->id = NewControl(gui.VimWindow,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 &bounds,
5010 "\pScrollBar",
5011 TRUE,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005012 0, // current
5013 0, // top
5014 0, // bottom
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 kControlScrollBarLiveProc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 (long) sb->ident);
5017#ifdef DEBUG_MAC_SB
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005018 printf("create_sb (%x) %x\n",sb->id, orient);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019#endif
5020}
5021
5022 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005023gui_mch_destroy_scrollbar(scrollbar_T *sb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 gui_mch_set_bg_color(gui.back_pixel);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005026 DisposeControl(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027#ifdef DEBUG_MAC_SB
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005028 printf("dest_sb (%x) \n",sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029#endif
5030}
5031
Bram Moolenaar703a8042016-06-04 16:24:32 +02005032 int
5033gui_mch_is_blinking(void)
5034{
5035 return FALSE;
5036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02005038 int
5039gui_mch_is_blink_off(void)
5040{
5041 return FALSE;
5042}
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044/*
5045 * Cursor blink functions.
5046 *
5047 * This is a simple state machine:
5048 * BLINK_NONE not blinking at all
5049 * BLINK_OFF blinking, cursor is not shown
5050 * BLINK_ON blinking, cursor is shown
5051 */
5052 void
5053gui_mch_set_blinking(long wait, long on, long off)
5054{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005055#if 0
5056 // TODO: TODO: TODO: TODO:
5057 blink_waittime = wait;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 blink_ontime = on;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005059 blink_offtime = off;
5060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061}
5062
5063/*
5064 * Stop the cursor blinking. Show the cursor if it wasn't shown.
5065 */
5066 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01005067gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01005069 if (may_call_gui_update_cursor)
5070 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005071#if 0
5072 // TODO: TODO: TODO: TODO:
5073 gui_w32_rm_blink_timer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 if (blink_state == BLINK_OFF)
5075 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005076 blink_state = BLINK_NONE;
5077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078}
5079
5080/*
5081 * Start the cursor blinking. If it was already blinking, this restarts the
5082 * waiting time and shows the cursor.
5083 */
5084 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005085gui_mch_start_blink(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005088 // TODO: TODO: TODO: TODO:
5089// gui_w32_rm_blink_timer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
Bram Moolenaar734a8672019-12-02 22:49:38 +01005091 // Only switch blinking on if none of the times is zero
5092#if 0
5093 if (blink_waittime && blink_ontime && blink_offtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime,
5096 (TIMERPROC)_OnBlinkTimer);
5097 blink_state = BLINK_ON;
5098 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005099 }
5100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101}
5102
5103/*
5104 * Return the RGB value of a pixel as long.
5105 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02005106 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107gui_mch_get_rgb(guicolor_T pixel)
5108{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02005109 return (guicolor_T)((Red(pixel) << 16) + (Green(pixel) << 8) + Blue(pixel));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110}
5111
5112
5113
5114#ifdef FEAT_BROWSE
5115/*
5116 * Pop open a file browser and return the file selected, in allocated memory,
5117 * or NULL if Cancel is hit.
5118 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
5119 * title - Title message for the file browser dialog.
5120 * dflt - Default name of file.
5121 * ext - Default extension to be added to files without extensions.
5122 * initdir - directory in which to open the browser (NULL = current dir)
5123 * filter - Filter for matched files to choose from.
5124 * Has a format like this:
5125 * "C Files (*.c)\0*.c\0"
5126 * "All Files\0*.*\0\0"
5127 * If these two strings were concatenated, then a choice of two file
5128 * filters will be selectable to the user. Then only matching files will
5129 * be shown in the browser. If NULL, the default allows all files.
5130 *
5131 * *NOTE* - the filter string must be terminated with TWO nulls.
5132 */
5133 char_u *
5134gui_mch_browse(
5135 int saving,
5136 char_u *title,
5137 char_u *dflt,
5138 char_u *ext,
5139 char_u *initdir,
5140 char_u *filter)
5141{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005142 // TODO: Add Ammon's safety check (Dany)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 NavReplyRecord reply;
5144 char_u *fname = NULL;
5145 char_u **fnames = NULL;
5146 long numFiles;
5147 NavDialogOptions navOptions;
5148 OSErr error;
5149
Bram Moolenaar734a8672019-12-02 22:49:38 +01005150 // Get Navigation Service Defaults value
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005151 NavGetDefaultDialogOptions(&navOptions);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
5153
Bram Moolenaar734a8672019-12-02 22:49:38 +01005154 // TODO: If we get a :browse args, set the Multiple bit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 navOptions.dialogOptionFlags = kNavAllowInvisibleFiles
5156 | kNavDontAutoTranslate
5157 | kNavDontAddTranslateItems
Bram Moolenaar734a8672019-12-02 22:49:38 +01005158 // | kNavAllowMultipleFiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 | kNavAllowStationery;
5160
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005161 (void) C2PascalString(title, &navOptions.message);
5162 (void) C2PascalString(dflt, &navOptions.savedFileName);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005163 // Could set clientName?
5164 // windowTitle? (there's no title bar?)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166 if (saving)
5167 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005168 // Change first parm AEDesc (typeFSS) *defaultLocation to match dflt
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005169 NavPutFile(NULL, &reply, &navOptions, NULL, 'TEXT', 'VIM!', NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (!reply.validRecord)
5171 return NULL;
5172 }
5173 else
5174 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005175 // Change first parm AEDesc (typeFSS) *defaultLocation to match dflt
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 NavGetFile(NULL, &reply, &navOptions, NULL, NULL, NULL, NULL, NULL);
5177 if (!reply.validRecord)
5178 return NULL;
5179 }
5180
5181 fnames = new_fnames_from_AEDesc(&reply.selection, &numFiles, &error);
5182
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005183 NavDisposeReply(&reply);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184
5185 if (fnames)
5186 {
5187 fname = fnames[0];
5188 vim_free(fnames);
5189 }
5190
Bram Moolenaar734a8672019-12-02 22:49:38 +01005191 // TODO: Shorten the file name if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 return fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193}
Bram Moolenaar734a8672019-12-02 22:49:38 +01005194#endif // FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196#ifdef FEAT_GUI_DIALOG
5197/*
5198 * Stuff for dialogues
5199 */
5200
5201/*
5202 * Create a dialogue dynamically from the parameter strings.
5203 * type = type of dialogue (question, alert, etc.)
5204 * title = dialogue title. may be NULL for default title.
5205 * message = text to display. Dialogue sizes to accommodate it.
5206 * buttons = '\n' separated list of button captions, default first.
5207 * dfltbutton = number of default button.
5208 *
5209 * This routine returns 1 if the first button is pressed,
5210 * 2 for the second, etc.
5211 *
5212 * 0 indicates Esc was pressed.
5213 * -1 for unexpected error
5214 *
5215 * If stubbing out this fn, return 1.
5216 */
5217
5218typedef struct
5219{
5220 short idx;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005221 short width; // Size of the text in pixel
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 Rect box;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005223} vgmDlgItm; // Vim Gui_Mac.c Dialog Item
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
5225#define MoveRectTo(r,x,y) OffsetRect(r,x-r->left,y-r->top)
5226
5227 static void
5228macMoveDialogItem(
5229 DialogRef theDialog,
5230 short itemNumber,
5231 short X,
5232 short Y,
5233 Rect *inBox)
5234{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235#if 0 // USE_CARBONIZED
5236 // Untested
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005237 MoveDialogItem(theDialog, itemNumber, X, Y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (inBox != nil)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005239 GetDialogItem(theDialog, itemNumber, &itemType, &itemHandle, inBox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240#else
5241 short itemType;
5242 Handle itemHandle;
5243 Rect localBox;
5244 Rect *itemBox = &localBox;
5245
5246 if (inBox != nil)
5247 itemBox = inBox;
5248
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005249 GetDialogItem(theDialog, itemNumber, &itemType, &itemHandle, itemBox);
5250 OffsetRect(itemBox, -itemBox->left, -itemBox->top);
5251 OffsetRect(itemBox, X, Y);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005252 // To move a control (like a button) we need to call both
5253 // MoveControl and SetDialogItem. FAQ 6-18
5254 if (1) //(itemType & kControlDialogItem)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005255 MoveControl((ControlRef) itemHandle, X, Y);
5256 SetDialogItem(theDialog, itemNumber, itemType, itemHandle, itemBox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257#endif
5258}
5259
5260 static void
5261macSizeDialogItem(
5262 DialogRef theDialog,
5263 short itemNumber,
5264 short width,
5265 short height)
5266{
5267 short itemType;
5268 Handle itemHandle;
5269 Rect itemBox;
5270
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005271 GetDialogItem(theDialog, itemNumber, &itemType, &itemHandle, &itemBox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar734a8672019-12-02 22:49:38 +01005273 // When width or height is zero do not change it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (width == 0)
5275 width = itemBox.right - itemBox.left;
5276 if (height == 0)
5277 height = itemBox.bottom - itemBox.top;
5278
Bram Moolenaar734a8672019-12-02 22:49:38 +01005279#if 0 // USE_CARBONIZED
5280 SizeDialogItem(theDialog, itemNumber, width, height); // Untested
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281#else
Bram Moolenaar734a8672019-12-02 22:49:38 +01005282 // Resize the bounding box
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 itemBox.right = itemBox.left + width;
5284 itemBox.bottom = itemBox.top + height;
5285
Bram Moolenaar734a8672019-12-02 22:49:38 +01005286 // To resize a control (like a button) we need to call both
5287 // SizeControl and SetDialogItem. (deducted from FAQ 6-18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (itemType & kControlDialogItem)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005289 SizeControl((ControlRef) itemHandle, width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
Bram Moolenaar734a8672019-12-02 22:49:38 +01005291 // Configure back the item
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005292 SetDialogItem(theDialog, itemNumber, itemType, itemHandle, &itemBox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293#endif
5294}
5295
5296 static void
5297macSetDialogItemText(
5298 DialogRef theDialog,
5299 short itemNumber,
5300 Str255 itemName)
5301{
5302 short itemType;
5303 Handle itemHandle;
5304 Rect itemBox;
5305
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005306 GetDialogItem(theDialog, itemNumber, &itemType, &itemHandle, &itemBox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
5308 if (itemType & kControlDialogItem)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005309 SetControlTitle((ControlRef) itemHandle, itemName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005311 SetDialogItemText(itemHandle, itemName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312}
5313
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005314
Bram Moolenaar734a8672019-12-02 22:49:38 +01005315/*
5316 * ModalDialog() handler for message dialogs that have hotkey accelerators.
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005317 * Expects a mapping of hotkey char to control index in gDialogHotKeys;
5318 * setting gDialogHotKeys to NULL disables any hotkey handling.
5319 */
5320 static pascal Boolean
5321DialogHotkeyFilterProc (
5322 DialogRef theDialog,
5323 EventRecord *event,
5324 DialogItemIndex *itemHit)
5325{
5326 char_u keyHit;
5327
5328 if (event->what == keyDown || event->what == autoKey)
5329 {
5330 keyHit = (event->message & charCodeMask);
5331
5332 if (gDialogHotKeys && gDialogHotKeys[keyHit])
5333 {
5334#ifdef DEBUG_MAC_DIALOG_HOTKEYS
5335 printf("user pressed hotkey '%c' --> item %d\n", keyHit, gDialogHotKeys[keyHit]);
5336#endif
5337 *itemHit = gDialogHotKeys[keyHit];
5338
Bram Moolenaar734a8672019-12-02 22:49:38 +01005339 // When handing off to StdFilterProc, pretend that the user
5340 // clicked the control manually. Note that this is also supposed
5341 // to cause the button to hilite briefly (to give some user
5342 // feedback), but this seems not to actually work (or it's too
5343 // fast to be seen).
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005344 event->what = kEventControlSimulateHit;
5345
Bram Moolenaar734a8672019-12-02 22:49:38 +01005346 return true; // we took care of it
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005347 }
5348
Bram Moolenaar734a8672019-12-02 22:49:38 +01005349 // Defer to the OS's standard behavior for this event.
5350 // This ensures that Enter will still activate the default button.
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005351 return StdFilterProc(theDialog, event, itemHit);
5352 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005353 return false; // Let ModalDialog deal with it
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005354}
5355
5356
Bram Moolenaar734a8672019-12-02 22:49:38 +01005357/*
5358 * TODO: There have been some crashes with dialogs, check your inbox
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005359 * (Jussi)
5360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 int
5362gui_mch_dialog(
5363 int type,
5364 char_u *title,
5365 char_u *message,
5366 char_u *buttons,
5367 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005368 char_u *textfield,
5369 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371 Handle buttonDITL;
5372 Handle iconDITL;
5373 Handle inputDITL;
5374 Handle messageDITL;
5375 Handle itemHandle;
5376 Handle iconHandle;
5377 DialogPtr theDialog;
5378 char_u len;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005379 char_u PascalTitle[256]; // place holder for the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 char_u name[256];
5381 GrafPtr oldPort;
5382 short itemHit;
5383 char_u *buttonChar;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005384 short hotKeys[256]; // map of hotkey -> control ID
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005385 char_u aHotKey;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 Rect box;
5387 short button;
5388 short lastButton;
5389 short itemType;
5390 short useIcon;
5391 short width;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005392 short totalButtonWidth = 0; // the width of all buttons together
5393 // including spacing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 short widestButton = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005395 short dfltButtonEdge = 20; // gut feeling
5396 short dfltElementSpacing = 13; // from IM:V.2-29
5397 short dfltIconSideSpace = 23; // from IM:V.2-29
5398 short maximumWidth = 400; // gut feeling
5399 short maxButtonWidth = 175; // gut feeling
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401 short vertical;
5402 short dialogHeight;
5403 short messageLines = 3;
5404 FontInfo textFontInfo;
5405
5406 vgmDlgItm iconItm;
5407 vgmDlgItm messageItm;
5408 vgmDlgItm inputItm;
5409 vgmDlgItm buttonItm;
5410
5411 WindowRef theWindow;
5412
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005413 ModalFilterUPP dialogUPP;
5414
Bram Moolenaar734a8672019-12-02 22:49:38 +01005415 // Check 'v' flag in 'guioptions': vertical button placement.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL);
5417
Bram Moolenaar734a8672019-12-02 22:49:38 +01005418 // Create a new Dialog Box from template.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005419 theDialog = GetNewDialog(129, nil, (WindowRef) -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaar734a8672019-12-02 22:49:38 +01005421 // Get the WindowRef
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 theWindow = GetDialogWindow(theDialog);
5423
Bram Moolenaar734a8672019-12-02 22:49:38 +01005424 // Hide the window.
5425 // 1. to avoid seeing slow drawing
5426 // 2. to prevent a problem seen while moving dialog item
5427 // within a visible window. (non-Carbon MacOS 9)
5428 // Could be avoided by changing the resource.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005429 HideWindow(theWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
Bram Moolenaar734a8672019-12-02 22:49:38 +01005431 // Change the graphical port to the dialog,
5432 // so we can measure the text with the proper font
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005433 GetPort(&oldPort);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005434 SetPortDialogPort(theDialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
Bram Moolenaar734a8672019-12-02 22:49:38 +01005436 // Get the info about the default text,
5437 // used to calculate the height of the message
5438 // and of the text field
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 GetFontInfo(&textFontInfo);
5440
Bram Moolenaar734a8672019-12-02 22:49:38 +01005441 // Set the dialog title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 if (title != NULL)
5443 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005444 (void) C2PascalString(title, &PascalTitle);
5445 SetWTitle(theWindow, PascalTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
5447
Bram Moolenaar734a8672019-12-02 22:49:38 +01005448 // Creates the buttons and add them to the Dialog Box.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005449 buttonDITL = GetResource('DITL', 130);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 buttonChar = buttons;
5451 button = 0;
5452
Bram Moolenaar734a8672019-12-02 22:49:38 +01005453 // initialize the hotkey mapping
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02005454 vim_memset(hotKeys, 0, sizeof(hotKeys));
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 for (;*buttonChar != 0;)
5457 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005458 // Get the name of the button
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 button++;
5460 len = 0;
5461 for (;((*buttonChar != DLG_BUTTON_SEP) && (*buttonChar != 0) && (len < 255)); buttonChar++)
5462 {
5463 if (*buttonChar != DLG_HOTKEY_CHAR)
5464 name[++len] = *buttonChar;
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005465 else
5466 {
5467 aHotKey = (char_u)*(buttonChar+1);
5468 if (aHotKey >= 'A' && aHotKey <= 'Z')
5469 aHotKey = (char_u)((int)aHotKey + (int)'a' - (int)'A');
5470 hotKeys[aHotKey] = button;
5471#ifdef DEBUG_MAC_DIALOG_HOTKEYS
5472 printf("### hotKey for button %d is '%c'\n", button, aHotKey);
5473#endif
5474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (*buttonChar != 0)
5478 buttonChar++;
5479 name[0] = len;
5480
Bram Moolenaar734a8672019-12-02 22:49:38 +01005481 // Add the button
5482 AppendDITL(theDialog, buttonDITL, overlayDITL); // appendDITLRight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaar734a8672019-12-02 22:49:38 +01005484 // Change the button's name
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005485 macSetDialogItemText(theDialog, button, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
Bram Moolenaar734a8672019-12-02 22:49:38 +01005487 // Resize the button to fit its name
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005488 width = StringWidth(name) + 2 * dfltButtonEdge;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005489 // Limit the size of any button to an acceptable value.
5490 // TODO: Should be based on the message width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (width > maxButtonWidth)
5492 width = maxButtonWidth;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005493 macSizeDialogItem(theDialog, button, width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 totalButtonWidth += width;
5496
5497 if (width > widestButton)
5498 widestButton = width;
5499 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005500 ReleaseResource(buttonDITL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 lastButton = button;
5502
Bram Moolenaar734a8672019-12-02 22:49:38 +01005503 // Add the icon to the Dialog Box.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 iconItm.idx = lastButton + 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005505 iconDITL = GetResource('DITL', 131);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 switch (type)
5507 {
Bram Moolenaarb05034a2010-09-21 17:34:31 +02005508 case VIM_GENERIC:
5509 case VIM_INFO:
5510 case VIM_QUESTION: useIcon = kNoteIcon; break;
5511 case VIM_WARNING: useIcon = kCautionIcon; break;
5512 case VIM_ERROR: useIcon = kStopIcon; break;
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +01005513 default: useIcon = kStopIcon;
Bram Moolenaarb05034a2010-09-21 17:34:31 +02005514 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005515 AppendDITL(theDialog, iconDITL, overlayDITL);
5516 ReleaseResource(iconDITL);
5517 GetDialogItem(theDialog, iconItm.idx, &itemType, &itemHandle, &box);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005518 // TODO: Should the item be freed?
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005519 iconHandle = GetIcon(useIcon);
5520 SetDialogItem(theDialog, iconItm.idx, itemType, iconHandle, &box);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
Bram Moolenaar734a8672019-12-02 22:49:38 +01005522 // Add the message to the Dialog box.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 messageItm.idx = lastButton + 2;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005524 messageDITL = GetResource('DITL', 132);
5525 AppendDITL(theDialog, messageDITL, overlayDITL);
5526 ReleaseResource(messageDITL);
5527 GetDialogItem(theDialog, messageItm.idx, &itemType, &itemHandle, &box);
5528 (void) C2PascalString(message, &name);
5529 SetDialogItemText(itemHandle, name);
5530 messageItm.width = StringWidth(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Bram Moolenaar734a8672019-12-02 22:49:38 +01005532 // Add the input box if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (textfield != NULL)
5534 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005535 // Cheat for now reuse the message and convert to text edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 inputItm.idx = lastButton + 3;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005537 inputDITL = GetResource('DITL', 132);
5538 AppendDITL(theDialog, inputDITL, overlayDITL);
5539 ReleaseResource(inputDITL);
5540 GetDialogItem(theDialog, inputItm.idx, &itemType, &itemHandle, &box);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005541// SetDialogItem(theDialog, inputItm.idx, kEditTextDialogItem, itemHandle, &box);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005542 (void) C2PascalString(textfield, &name);
5543 SetDialogItemText(itemHandle, name);
5544 inputItm.width = StringWidth(name);
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005545
Bram Moolenaar734a8672019-12-02 22:49:38 +01005546 // Hotkeys don't make sense if there's a text field
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005547 gDialogHotKeys = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005549 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01005550 // Install hotkey table
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005551 gDialogHotKeys = (short *)&hotKeys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Bram Moolenaar734a8672019-12-02 22:49:38 +01005553 // Set the <ENTER> and <ESC> button.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005554 SetDialogDefaultItem(theDialog, dfltbutton);
5555 SetDialogCancelItem(theDialog, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaar734a8672019-12-02 22:49:38 +01005557 // Reposition element
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
Bram Moolenaar734a8672019-12-02 22:49:38 +01005559 // Check if we need to force vertical
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (totalButtonWidth > maximumWidth)
5561 vertical = TRUE;
5562
Bram Moolenaar734a8672019-12-02 22:49:38 +01005563 // Place icon
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005564 macMoveDialogItem(theDialog, iconItm.idx, dfltIconSideSpace, dfltElementSpacing, &box);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 iconItm.box.right = box.right;
5566 iconItm.box.bottom = box.bottom;
5567
Bram Moolenaar734a8672019-12-02 22:49:38 +01005568 // Place Message
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 messageItm.box.left = iconItm.box.right + dfltIconSideSpace;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005570 macSizeDialogItem(theDialog, messageItm.idx, 0, messageLines * (textFontInfo.ascent + textFontInfo.descent));
5571 macMoveDialogItem(theDialog, messageItm.idx, messageItm.box.left, dfltElementSpacing, &messageItm.box);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar734a8672019-12-02 22:49:38 +01005573 // Place Input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 if (textfield != NULL)
5575 {
5576 inputItm.box.left = messageItm.box.left;
5577 inputItm.box.top = messageItm.box.bottom + dfltElementSpacing;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005578 macSizeDialogItem(theDialog, inputItm.idx, 0, textFontInfo.ascent + textFontInfo.descent);
5579 macMoveDialogItem(theDialog, inputItm.idx, inputItm.box.left, inputItm.box.top, &inputItm.box);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005580 // Convert the static text into a text edit.
5581 // For some reason this change need to be done last (Dany)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005582 GetDialogItem(theDialog, inputItm.idx, &itemType, &itemHandle, &inputItm.box);
5583 SetDialogItem(theDialog, inputItm.idx, kEditTextDialogItem, itemHandle, &inputItm.box);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 SelectDialogItemText(theDialog, inputItm.idx, 0, 32767);
5585 }
5586
Bram Moolenaar734a8672019-12-02 22:49:38 +01005587 // Place Button
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (textfield != NULL)
5589 {
5590 buttonItm.box.left = inputItm.box.left;
5591 buttonItm.box.top = inputItm.box.bottom + dfltElementSpacing;
5592 }
5593 else
5594 {
5595 buttonItm.box.left = messageItm.box.left;
5596 buttonItm.box.top = messageItm.box.bottom + dfltElementSpacing;
5597 }
5598
5599 for (button=1; button <= lastButton; button++)
5600 {
5601
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005602 macMoveDialogItem(theDialog, button, buttonItm.box.left, buttonItm.box.top, &box);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005603 // With vertical, it's better to have all buttons the same length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (vertical)
5605 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005606 macSizeDialogItem(theDialog, button, widestButton, 0);
5607 GetDialogItem(theDialog, button, &itemType, &itemHandle, &box);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005609 // Calculate position of next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (vertical)
5611 buttonItm.box.top = box.bottom + dfltElementSpacing;
5612 else
5613 buttonItm.box.left = box.right + dfltElementSpacing;
5614 }
5615
Bram Moolenaar734a8672019-12-02 22:49:38 +01005616 // Resize the dialog box
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 dialogHeight = box.bottom + dfltElementSpacing;
5618 SizeWindow(theWindow, maximumWidth, dialogHeight, TRUE);
5619
Bram Moolenaar734a8672019-12-02 22:49:38 +01005620 // Magic resize
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005621 AutoSizeDialog(theDialog);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005622 // Need a horizontal resize anyway so not that useful
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
Bram Moolenaar734a8672019-12-02 22:49:38 +01005624 // Display it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 ShowWindow(theWindow);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005626// BringToFront(theWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 SelectWindow(theWindow);
5628
Bram Moolenaar734a8672019-12-02 22:49:38 +01005629// DrawDialog(theDialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630#if 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005631 GetPort(&oldPort);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005632 SetPortDialogPort(theDialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633#endif
5634
Bram Moolenaard68071d2006-05-02 22:08:30 +00005635#ifdef USE_CARBONKEYHANDLER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005636 // Avoid that we use key events for the main window.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005637 dialog_busy = TRUE;
5638#endif
5639
Bram Moolenaar734a8672019-12-02 22:49:38 +01005640 // Prepare the shortcut-handling filterProc for handing to the dialog
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005641 dialogUPP = NewModalFilterUPP(DialogHotkeyFilterProc);
5642
Bram Moolenaar734a8672019-12-02 22:49:38 +01005643 // Hang until one of the button is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 do
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005645 ModalDialog(dialogUPP, &itemHit);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01005646 while ((itemHit < 1) || (itemHit > lastButton));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaard68071d2006-05-02 22:08:30 +00005648#ifdef USE_CARBONKEYHANDLER
5649 dialog_busy = FALSE;
5650#endif
5651
Bram Moolenaar734a8672019-12-02 22:49:38 +01005652 // Copy back the text entered by the user into the param
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (textfield != NULL)
5654 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005655 GetDialogItem(theDialog, inputItm.idx, &itemType, &itemHandle, &box);
5656 GetDialogItemText(itemHandle, (char_u *) &name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657#if IOSIZE < 256
Bram Moolenaar734a8672019-12-02 22:49:38 +01005658 // Truncate the name to IOSIZE if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (name[0] > IOSIZE)
5660 name[0] = IOSIZE - 1;
5661#endif
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005662 vim_strncpy(textfield, &name[1], name[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664
Bram Moolenaar734a8672019-12-02 22:49:38 +01005665 // Restore the original graphical port
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005666 SetPort(oldPort);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
Bram Moolenaar734a8672019-12-02 22:49:38 +01005668 // Free the modal filterProc
Bram Moolenaarc52da9d2008-03-20 13:39:37 +00005669 DisposeRoutineDescriptor(dialogUPP);
5670
Bram Moolenaar734a8672019-12-02 22:49:38 +01005671 // Get ride of the dialog (free memory)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005672 DisposeDialog(theDialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
5674 return itemHit;
5675/*
Bram Moolenaarb05034a2010-09-21 17:34:31 +02005676 * Useful thing which could be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 * SetDialogTimeout(): Auto click a button after timeout
5678 * SetDialogTracksCursor() : Get the I-beam cursor over input box
5679 * MoveDialogItem(): Probably better than SetDialogItem
5680 * SizeDialogItem(): (but is it Carbon Only?)
Bram Moolenaar14d0e792007-08-30 08:35:35 +00005681 * AutoSizeDialog(): Magic resize of dialog based on text length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 */
5683}
Bram Moolenaar734a8672019-12-02 22:49:38 +01005684#endif // FEAT_DIALOG_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686/*
5687 * Display the saved error message(s).
5688 */
5689#ifdef USE_MCH_ERRMSG
5690 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005691display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 char *p;
5694 char_u pError[256];
5695
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005696 if (error_ga.ga_data == NULL)
5697 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
Bram Moolenaar734a8672019-12-02 22:49:38 +01005699 // avoid putting up a message box with blanks only
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005700 for (p = (char *)error_ga.ga_data; *p; ++p)
5701 if (!isspace(*p))
5702 {
5703 if (STRLEN(p) > 255)
5704 pError[0] = 255;
5705 else
5706 pError[0] = STRLEN(p);
5707
5708 STRNCPY(&pError[1], p, pError[0]);
5709 ParamText(pError, nil, nil, nil);
5710 Alert(128, nil);
5711 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005712 // TODO: handled message longer than 256 chars
5713 // use auto-sizeable alert
5714 // or dialog with scrollbars (TextEdit zone)
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005715 }
5716 ga_clear(&error_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717}
5718#endif
5719
5720/*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005721 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005723 void
5724gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 Point where;
5727
5728 GetMouse(&where);
5729
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005730 *x = where.h;
5731 *y = where.v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732}
5733
5734 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005735gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005737 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01005739 // From FAQ 3-11
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
5741 CursorDevicePtr myMouse;
5742 Point where;
5743
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005744 if ( NGetTrapAddress(_CursorDeviceDispatch, ToolTrap)
5745 != NGetTrapAddress(_Unimplemented, ToolTrap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005747 // New way
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
5749 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005750 * Get first device with one button.
5751 * This will probably be the standard mouse
5752 * start at head of cursor dev list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 *
5754 */
5755
5756 myMouse = nil;
5757
5758 do
5759 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005760 // Get the next cursor device
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 CursorDeviceNextDevice(&myMouse);
5762 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005763 while ((myMouse != nil) && (myMouse->cntButtons != 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005765 CursorDeviceMoveTo(myMouse, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 else
5768 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005769 // Old way
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 where.h = x;
5771 where.v = y;
5772
5773 *(Point *)RawMouse = where;
5774 *(Point *)MTemp = where;
5775 *(Ptr) CrsrNew = 0xFFFF;
5776 }
5777#endif
5778}
5779
5780 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005781gui_mch_show_popupmenu(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783/*
5784 * Clone PopUp to use menu
5785 * Create a object descriptor for the current selection
5786 * Call the procedure
5787 */
5788
5789 MenuHandle CntxMenu;
5790 Point where;
5791 OSStatus status;
5792 UInt32 CntxType;
5793 SInt16 CntxMenuID;
5794 UInt16 CntxMenuItem;
5795 Str255 HelpName = "";
5796 GrafPtr savePort;
5797
Bram Moolenaar734a8672019-12-02 22:49:38 +01005798 // Save Current Port: On MacOS X we seem to lose the port
5799 GetPort(&savePort); //OSX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005801 GetMouse(&where);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005802 LocalToGlobal(&where); //OSX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 CntxMenu = menu->submenu_handle;
5804
Bram Moolenaar734a8672019-12-02 22:49:38 +01005805 // TODO: Get the text selection from Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar734a8672019-12-02 22:49:38 +01005807 // Call to Handle Popup
Bram Moolenaar48c2c9a2007-03-08 19:34:12 +00005808 status = ContextualMenuSelect(CntxMenu, where, false, kCMHelpItemRemoveHelp,
Bram Moolenaaradcb9492006-10-17 10:51:57 +00005809 HelpName, NULL, &CntxType, &CntxMenuID, &CntxMenuItem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 if (status == noErr)
5812 {
5813 if (CntxType == kCMMenuItemSelected)
5814 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005815 // Handle the menu CntxMenuID, CntxMenuItem
5816 // The submenu can be handle directly by gui_mac_handle_menu
5817 // But what about the current menu, is the menu changed by
5818 // ContextualMenuSelect
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005819 gui_mac_handle_menu((CntxMenuID << 16) + CntxMenuItem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 }
5821 else if (CntxMenuID == kCMShowHelpSelected)
5822 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005823 // Should come up with the help
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
5825 }
5826
Bram Moolenaar734a8672019-12-02 22:49:38 +01005827 // Restore original Port
5828 SetPort(savePort); //OSX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829}
5830
5831#if defined(FEAT_CW_EDITOR) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005832// TODO: Is it need for MACOS_X? (Dany)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 void
5834mch_post_buffer_write(buf_T *buf)
5835{
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005836 GetFSSpecFromPath(buf->b_ffname, &buf->b_FSSpec);
5837 Send_KAHL_MOD_AE(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838}
5839#endif
5840
5841#ifdef FEAT_TITLE
5842/*
5843 * Set the window title and icon.
5844 * (The icon is not taken care of).
5845 */
5846 void
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005847gui_mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005849 // TODO: Get vim to make sure maxlen (from p_titlelen) is smaller
5850 // that 256. Even better get it to fit nicely in the titlebar.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00005851#ifdef MACOS_CONVERT
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005852 CFStringRef windowTitle;
5853 size_t windowTitleLen;
5854#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 char_u *pascalTitle;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaar734a8672019-12-02 22:49:38 +01005858 if (title == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 return;
5860
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00005861#ifdef MACOS_CONVERT
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005862 windowTitleLen = STRLEN(title);
Bram Moolenaar446cb832008-06-24 21:56:24 +00005863 windowTitle = (CFStringRef)mac_enc_to_cfstring(title, windowTitleLen);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005864
5865 if (windowTitle)
5866 {
5867 SetWindowTitleWithCFString(gui.VimWindow, windowTitle);
5868 CFRelease(windowTitle);
5869 }
5870#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 pascalTitle = C2Pascal_save(title);
5872 if (pascalTitle != NULL)
5873 {
5874 SetWTitle(gui.VimWindow, pascalTitle);
5875 vim_free(pascalTitle);
5876 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878}
5879#endif
5880
5881/*
Bram Moolenaarb05034a2010-09-21 17:34:31 +02005882 * Transferred from os_mac.c for MacOS X using os_unix.c prep work
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 */
5884
5885 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005886C2PascalString(char_u *CString, Str255 *PascalString)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 char_u *PascalPtr = (char_u *) PascalString;
5889 int len;
5890 int i;
5891
5892 PascalPtr[0] = 0;
5893 if (CString == NULL)
5894 return 0;
5895
5896 len = STRLEN(CString);
5897 if (len > 255)
5898 len = 255;
5899
5900 for (i = 0; i < len; i++)
5901 PascalPtr[i+1] = CString[i];
5902
5903 PascalPtr[0] = len;
5904
5905 return 0;
5906}
5907
5908 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005909GetFSSpecFromPath(char_u *file, FSSpec *fileFSSpec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005911 // From FAQ 8-12
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 Str255 filePascal;
5913 CInfoPBRec myCPB;
5914 OSErr err;
5915
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005916 (void) C2PascalString(file, &filePascal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918 myCPB.dirInfo.ioNamePtr = filePascal;
5919 myCPB.dirInfo.ioVRefNum = 0;
5920 myCPB.dirInfo.ioFDirIndex = 0;
5921 myCPB.dirInfo.ioDrDirID = 0;
5922
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005923 err= PBGetCatInfo(&myCPB, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaar734a8672019-12-02 22:49:38 +01005925 // vRefNum, dirID, name
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005926 FSMakeFSSpec(0, 0, filePascal, fileFSSpec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Bram Moolenaar734a8672019-12-02 22:49:38 +01005928 // TODO: Use an error code mechanism
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 return 0;
5930}
5931
5932/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005933 * Convert a FSSpec to a full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 */
5935
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005936char_u *FullPathFromFSSpec_save(FSSpec file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 /*
5939 * TODO: Add protection for 256 char max.
5940 */
5941
5942 CInfoPBRec theCPB;
5943 char_u fname[256];
5944 char_u *filenamePtr = fname;
5945 OSErr error;
5946 int folder = 1;
5947#ifdef USE_UNIXFILENAME
5948 SInt16 dfltVol_vRefNum;
5949 SInt32 dfltVol_dirID;
5950 FSRef refFile;
5951 OSStatus status;
5952 UInt32 pathSize = 256;
5953 char_u pathname[256];
5954 char_u *path = pathname;
5955#else
5956 Str255 directoryName;
5957 char_u temporary[255];
5958 char_u *temporaryPtr = temporary;
5959#endif
5960
5961#ifdef USE_UNIXFILENAME
Bram Moolenaar734a8672019-12-02 22:49:38 +01005962 // Get the default volume
5963 // TODO: Remove as this only work if Vim is on the Boot Volume
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005964 error=HGetVol(NULL, &dfltVol_vRefNum, &dfltVol_dirID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
5966 if (error)
5967 return NULL;
5968#endif
5969
Bram Moolenaar734a8672019-12-02 22:49:38 +01005970 // Start filling fname with file.name
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005971 vim_strncpy(filenamePtr, &file.name[1], file.name[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
Bram Moolenaar734a8672019-12-02 22:49:38 +01005973 // Get the info about the file specified in FSSpec
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 theCPB.dirInfo.ioFDirIndex = 0;
5975 theCPB.dirInfo.ioNamePtr = file.name;
5976 theCPB.dirInfo.ioVRefNum = file.vRefNum;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005977 //theCPB.hFileInfo.ioDirID = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 theCPB.dirInfo.ioDrDirID = file.parID;
5979
Bram Moolenaar734a8672019-12-02 22:49:38 +01005980 // As ioFDirIndex = 0, get the info of ioNamePtr,
5981 // which is relative to ioVrefNum, ioDirID
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005982 error = PBGetCatInfo(&theCPB, false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Bram Moolenaar734a8672019-12-02 22:49:38 +01005984 // If we are called for a new file we expect fnfErr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 if ((error) && (error != fnfErr))
5986 return NULL;
5987
Bram Moolenaar734a8672019-12-02 22:49:38 +01005988 // Check if it's a file or folder
5989 // default to file if file don't exist
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (((theCPB.hFileInfo.ioFlAttrib & ioDirMask) == 0) || (error))
Bram Moolenaar734a8672019-12-02 22:49:38 +01005991 folder = 0; // It's not a folder
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 else
5993 folder = 1;
5994
5995#ifdef USE_UNIXFILENAME
5996 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005997 * The functions used here are available in Carbon, but do nothing on
5998 * MacOS 8 and 9.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 */
6000 if (error == fnfErr)
6001 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006002 // If the file to be saved does not already exist, it isn't possible
6003 // to convert its FSSpec into an FSRef. But we can construct an
6004 // FSSpec for the file's parent folder (since we have its volume and
6005 // directory IDs), and since that folder does exist, we can convert
6006 // that FSSpec into an FSRef, convert the FSRef in turn into a path,
6007 // and, finally, append the filename.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 FSSpec dirSpec;
6009 FSRef dirRef;
6010 Str255 emptyFilename = "\p";
6011 error = FSMakeFSSpec(theCPB.dirInfo.ioVRefNum,
6012 theCPB.dirInfo.ioDrDirID, emptyFilename, &dirSpec);
6013 if (error)
6014 return NULL;
6015
6016 error = FSpMakeFSRef(&dirSpec, &dirRef);
6017 if (error)
6018 return NULL;
6019
6020 status = FSRefMakePath(&dirRef, (UInt8*)path, pathSize);
6021 if (status)
6022 return NULL;
6023
6024 STRCAT(path, "/");
6025 STRCAT(path, filenamePtr);
6026 }
6027 else
6028 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006029 // If the file to be saved already exists, we can get its full path
6030 // by converting its FSSpec into an FSRef.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006031 error=FSpMakeFSRef(&file, &refFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (error)
6033 return NULL;
6034
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006035 status=FSRefMakePath(&refFile, (UInt8 *) path, pathSize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 if (status)
6037 return NULL;
6038 }
6039
Bram Moolenaar734a8672019-12-02 22:49:38 +01006040 // Add a slash at the end if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (folder)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006042 STRCAT(path, "/");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006044 return (vim_strsave(path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045#else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006046 // TODO: Get rid of all USE_UNIXFILENAME below
6047 // Set ioNamePtr, it's the same area which is always reused.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 theCPB.dirInfo.ioNamePtr = directoryName;
6049
Bram Moolenaar734a8672019-12-02 22:49:38 +01006050 // Trick for first entry, set ioDrParID to the first value
6051 // we want for ioDrDirID
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 theCPB.dirInfo.ioDrParID = file.parID;
6053 theCPB.dirInfo.ioDrDirID = file.parID;
6054
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006055 if ((TRUE) && (file.parID != fsRtDirID /*fsRtParID*/))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 do
6057 {
6058 theCPB.dirInfo.ioFDirIndex = -1;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006059 // theCPB.dirInfo.ioNamePtr = directoryName; Already done above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 theCPB.dirInfo.ioVRefNum = file.vRefNum;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006061 // theCPB.dirInfo.ioDirID = irrelevant when ioFDirIndex = -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 theCPB.dirInfo.ioDrDirID = theCPB.dirInfo.ioDrParID;
6063
Bram Moolenaar734a8672019-12-02 22:49:38 +01006064 // As ioFDirIndex = -1, get the info of ioDrDirID,
6065 // *ioNamePtr[0 TO 31] will be updated
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006066 error = PBGetCatInfo(&theCPB,false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068 if (error)
6069 return NULL;
6070
Bram Moolenaar734a8672019-12-02 22:49:38 +01006071 // Put the new directoryName in front of the current fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 STRCPY(temporaryPtr, filenamePtr);
Bram Moolenaarbbebc852005-07-18 21:47:53 +00006073 vim_strncpy(filenamePtr, &directoryName[1], directoryName[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 STRCAT(filenamePtr, ":");
6075 STRCAT(filenamePtr, temporaryPtr);
6076 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01006077#if 1 // def USE_UNIXFILENAME
6078 while ((theCPB.dirInfo.ioDrParID != fsRtDirID)
6079 /* && (theCPB.dirInfo.ioDrDirID != fsRtDirID)*/);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080#else
6081 while (theCPB.dirInfo.ioDrDirID != fsRtDirID);
6082#endif
6083
Bram Moolenaar734a8672019-12-02 22:49:38 +01006084 // Get the information about the volume on which the file reside
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 theCPB.dirInfo.ioFDirIndex = -1;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006086 // theCPB.dirInfo.ioNamePtr = directoryName; Already done above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 theCPB.dirInfo.ioVRefNum = file.vRefNum;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006088 // theCPB.dirInfo.ioDirID = irrelevant when ioFDirIndex = -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 theCPB.dirInfo.ioDrDirID = theCPB.dirInfo.ioDrParID;
6090
Bram Moolenaar734a8672019-12-02 22:49:38 +01006091 // As ioFDirIndex = -1, get the info of ioDrDirID,
6092 // *ioNamePtr[0 TO 31] will be updated
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006093 error = PBGetCatInfo(&theCPB,false);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
6095 if (error)
6096 return NULL;
6097
Bram Moolenaar734a8672019-12-02 22:49:38 +01006098 // For MacOS Classic always add the volume name
6099 // For MacOS X add the volume name preceded by "Volumes"
6100 // when we are not referring to the boot volume
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#ifdef USE_UNIXFILENAME
6102 if (file.vRefNum != dfltVol_vRefNum)
6103#endif
6104 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006105 // Add the volume name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 STRCPY(temporaryPtr, filenamePtr);
Bram Moolenaarbbebc852005-07-18 21:47:53 +00006107 vim_strncpy(filenamePtr, &directoryName[1], directoryName[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 STRCAT(filenamePtr, ":");
6109 STRCAT(filenamePtr, temporaryPtr);
6110
6111#ifdef USE_UNIXFILENAME
6112 STRCPY(temporaryPtr, filenamePtr);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006113 filenamePtr[0] = 0; // NULL terminate the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 STRCAT(filenamePtr, "Volumes:");
6115 STRCAT(filenamePtr, temporaryPtr);
6116#endif
6117 }
6118
Bram Moolenaar734a8672019-12-02 22:49:38 +01006119 // Append final path separator if it's a folder
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (folder)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006121 STRCAT(fname, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
Bram Moolenaar734a8672019-12-02 22:49:38 +01006123 // As we use Unix File Name for MacOS X convert it
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#ifdef USE_UNIXFILENAME
Bram Moolenaar734a8672019-12-02 22:49:38 +01006125 // Need to insert leading /
6126 // TODO: get the above code to use directly the /
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 STRCPY(&temporaryPtr[1], filenamePtr);
6128 temporaryPtr[0] = '/';
6129 STRCPY(filenamePtr, temporaryPtr);
6130 {
6131 char *p;
6132 for (p = fname; *p; p++)
6133 if (*p == ':')
6134 *p = '/';
6135 }
6136#endif
6137
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006138 return (vim_strsave(fname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139#endif
6140}
6141
Bram Moolenaar13505972019-01-24 15:04:48 +01006142#if defined(USE_CARBONKEYHANDLER) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143/*
6144 * Input Method Control functions.
6145 */
6146
6147/*
6148 * Notify cursor position to IM.
6149 */
6150 void
6151im_set_position(int row, int col)
6152{
Bram Moolenaar259f26a2018-05-15 22:25:40 +02006153# if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01006154 // TODO: Implement me!
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006155 im_start_row = row;
6156 im_start_col = col;
Bram Moolenaar259f26a2018-05-15 22:25:40 +02006157# endif
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006158}
6159
6160static ScriptLanguageRecord gTSLWindow;
6161static ScriptLanguageRecord gTSLInsert;
6162static ScriptLanguageRecord gTSLDefault = { 0, 0 };
6163
6164static Component gTSCWindow;
6165static Component gTSCInsert;
6166static Component gTSCDefault;
6167
6168static int im_initialized = 0;
6169
6170 static void
6171im_on_window_switch(int active)
6172{
6173 ScriptLanguageRecord *slptr = NULL;
6174 OSStatus err;
6175
6176 if (! gui.in_use)
6177 return;
6178
6179 if (im_initialized == 0)
6180 {
6181 im_initialized = 1;
6182
Bram Moolenaar734a8672019-12-02 22:49:38 +01006183 // save default TSM component (should be U.S.) to default
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006184 GetDefaultInputMethodOfClass(&gTSCDefault, &gTSLDefault,
6185 kKeyboardInputMethodClass);
6186 }
6187
6188 if (active == TRUE)
6189 {
6190 im_is_active = TRUE;
6191 ActivateTSMDocument(gTSMDocument);
6192 slptr = &gTSLWindow;
6193
6194 if (slptr)
6195 {
6196 err = SetDefaultInputMethodOfClass(gTSCWindow, slptr,
6197 kKeyboardInputMethodClass);
6198 if (err == noErr)
6199 err = SetTextServiceLanguage(slptr);
6200
6201 if (err == noErr)
6202 KeyScript(slptr->fScript | smKeyForceKeyScriptMask);
6203 }
6204 }
6205 else
6206 {
6207 err = GetTextServiceLanguage(&gTSLWindow);
6208 if (err == noErr)
6209 slptr = &gTSLWindow;
6210
6211 if (slptr)
6212 GetDefaultInputMethodOfClass(&gTSCWindow, slptr,
6213 kKeyboardInputMethodClass);
6214
6215 im_is_active = FALSE;
6216 DeactivateTSMDocument(gTSMDocument);
6217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218}
6219
6220/*
6221 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6222 */
6223 void
6224im_set_active(int active)
6225{
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006226 ScriptLanguageRecord *slptr = NULL;
6227 OSStatus err;
6228
Bram Moolenaar819edbe2017-11-25 17:14:33 +01006229 if (!gui.in_use)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006230 return;
6231
6232 if (im_initialized == 0)
6233 {
6234 im_initialized = 1;
6235
Bram Moolenaar734a8672019-12-02 22:49:38 +01006236 // save default TSM component (should be U.S.) to default
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006237 GetDefaultInputMethodOfClass(&gTSCDefault, &gTSLDefault,
6238 kKeyboardInputMethodClass);
6239 }
6240
6241 if (active == TRUE)
6242 {
6243 im_is_active = TRUE;
6244 ActivateTSMDocument(gTSMDocument);
6245 slptr = &gTSLInsert;
6246
6247 if (slptr)
6248 {
6249 err = SetDefaultInputMethodOfClass(gTSCInsert, slptr,
6250 kKeyboardInputMethodClass);
6251 if (err == noErr)
6252 err = SetTextServiceLanguage(slptr);
6253
6254 if (err == noErr)
6255 KeyScript(slptr->fScript | smKeyForceKeyScriptMask);
6256 }
6257 }
6258 else
6259 {
6260 err = GetTextServiceLanguage(&gTSLInsert);
6261 if (err == noErr)
6262 slptr = &gTSLInsert;
6263
6264 if (slptr)
6265 GetDefaultInputMethodOfClass(&gTSCInsert, slptr,
6266 kKeyboardInputMethodClass);
6267
Bram Moolenaar734a8672019-12-02 22:49:38 +01006268 // restore to default when switch to normal mode, so than we could
6269 // enter commands easier
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006270 SetDefaultInputMethodOfClass(gTSCDefault, &gTSLDefault,
6271 kKeyboardInputMethodClass);
6272 SetTextServiceLanguage(&gTSLDefault);
6273
6274 im_is_active = FALSE;
6275 DeactivateTSMDocument(gTSMDocument);
6276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277}
6278
6279/*
6280 * Get IM status. When IM is on, return not 0. Else return 0.
6281 */
6282 int
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00006283im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006285 if (! gui.in_use)
6286 return 0;
6287
6288 return im_is_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289}
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006290
Bram Moolenaar13505972019-01-24 15:04:48 +01006291#endif
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006292
6293
6294
6295#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
6296// drawer implementation
6297static MenuRef contextMenu = NULL;
6298enum
6299{
Bram Moolenaar43acbce2016-02-27 15:21:32 +01006300 kTabContextMenuId = 42
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006301};
6302
6303// the caller has to CFRelease() the returned string
6304 static CFStringRef
6305getTabLabel(tabpage_T *page)
6306{
6307 get_tabline_label(page, FALSE);
6308#ifdef MACOS_CONVERT
Bram Moolenaar446cb832008-06-24 21:56:24 +00006309 return (CFStringRef)mac_enc_to_cfstring(NameBuff, STRLEN(NameBuff));
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006310#else
6311 // TODO: check internal encoding?
6312 return CFStringCreateWithCString(kCFAllocatorDefault, (char *)NameBuff,
6313 kCFStringEncodingMacRoman);
6314#endif
6315}
6316
6317
6318#define DRAWER_SIZE 150
6319#define DRAWER_INSET 16
6320
6321static ControlRef dataBrowser = NULL;
6322
6323// when the tabline is hidden, vim doesn't call update_tabline(). When
Bram Moolenaarb05034a2010-09-21 17:34:31 +02006324// the tabline is shown again, show_tabline() is called before update_tabline(),
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006325// and because of this, the tab labels and vim's internal tabs are out of sync
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006326// for a very short time. to prevent inconsistent state, we store the labels
6327// of the tabs, not pointers to the tabs (which are invalid for a short time).
6328static CFStringRef *tabLabels = NULL;
6329static int tabLabelsSize = 0;
6330
6331enum
6332{
6333 kTabsColumn = 'Tabs'
6334};
6335
6336 static int
6337getTabCount(void)
6338{
6339 tabpage_T *tp;
6340 int numTabs = 0;
6341
Bram Moolenaar29323592016-07-24 22:04:11 +02006342 FOR_ALL_TABPAGES(tp)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006343 ++numTabs;
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006344 return numTabs;
6345}
6346
6347// data browser item display callback
6348 static OSStatus
6349dbItemDataCallback(ControlRef browser,
6350 DataBrowserItemID itemID,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006351 DataBrowserPropertyID property /* column id */,
6352 DataBrowserItemDataRef itemData,
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006353 Boolean changeValue)
6354{
6355 OSStatus status = noErr;
6356
6357 // assert(property == kTabsColumn); // why is this violated??
6358
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006359 // changeValue is true if we have a modifiable list and data was changed.
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006360 // In our case, it's always false.
6361 // (that is: if (changeValue) updateInternalData(); else return
6362 // internalData();
6363 if (!changeValue)
6364 {
6365 CFStringRef str;
6366
6367 assert(itemID - 1 >= 0 && itemID - 1 < tabLabelsSize);
6368 str = tabLabels[itemID - 1];
6369 status = SetDataBrowserItemDataText(itemData, str);
6370 }
6371 else
6372 status = errDataBrowserPropertyNotSupported;
6373
6374 return status;
6375}
6376
6377// data browser action callback
6378 static void
6379dbItemNotificationCallback(ControlRef browser,
6380 DataBrowserItemID item,
6381 DataBrowserItemNotification message)
6382{
6383 switch (message)
6384 {
6385 case kDataBrowserItemSelected:
6386 send_tabline_event(item);
6387 break;
6388 }
6389}
6390
6391// callbacks needed for contextual menu:
6392 static void
6393dbGetContextualMenuCallback(ControlRef browser,
6394 MenuRef *menu,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006395 UInt32 *helpType,
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006396 CFStringRef *helpItemString,
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006397 AEDesc *selection)
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006398{
6399 // on mac os 9: kCMHelpItemNoHelp, but it's not the same
6400 *helpType = kCMHelpItemRemoveHelp; // OS X only ;-)
6401 *helpItemString = NULL;
6402
6403 *menu = contextMenu;
6404}
6405
6406 static void
6407dbSelectContextualMenuCallback(ControlRef browser,
6408 MenuRef menu,
6409 UInt32 selectionType,
6410 SInt16 menuID,
6411 MenuItemIndex menuItem)
6412{
6413 if (selectionType == kCMMenuItemSelected)
6414 {
6415 MenuCommand command;
6416 GetMenuItemCommandID(menu, menuItem, &command);
6417
6418 // get tab that was selected when the context menu appeared
6419 // (there is always one tab selected). TODO: check if the context menu
6420 // isn't opened on an item but on empty space (has to be possible some
6421 // way, the finder does it too ;-) )
6422 Handle items = NewHandle(0);
6423 if (items != NULL)
6424 {
6425 int numItems;
6426
6427 GetDataBrowserItems(browser, kDataBrowserNoItem, false,
6428 kDataBrowserItemIsSelected, items);
6429 numItems = GetHandleSize(items) / sizeof(DataBrowserItemID);
6430 if (numItems > 0)
6431 {
6432 int idx;
6433 DataBrowserItemID *itemsPtr;
6434
6435 HLock(items);
6436 itemsPtr = (DataBrowserItemID *)*items;
6437 idx = itemsPtr[0];
6438 HUnlock(items);
6439 send_tabline_menu_event(idx, command);
6440 }
6441 DisposeHandle(items);
6442 }
6443 }
6444}
6445
6446// focus callback of the data browser to always leave focus in vim
6447 static OSStatus
6448dbFocusCallback(EventHandlerCallRef handler, EventRef event, void *data)
6449{
6450 assert(GetEventClass(event) == kEventClassControl
6451 && GetEventKind(event) == kEventControlSetFocusPart);
6452
6453 return paramErr;
6454}
6455
6456
6457// drawer callback to resize data browser to drawer size
6458 static OSStatus
6459drawerCallback(EventHandlerCallRef handler, EventRef event, void *data)
6460{
6461 switch (GetEventKind(event))
6462 {
6463 case kEventWindowBoundsChanged: // move or resize
6464 {
6465 UInt32 attribs;
6466 GetEventParameter(event, kEventParamAttributes, typeUInt32,
6467 NULL, sizeof(attribs), NULL, &attribs);
6468 if (attribs & kWindowBoundsChangeSizeChanged) // resize
6469 {
6470 Rect r;
6471 GetWindowBounds(drawer, kWindowContentRgn, &r);
6472 SetRect(&r, 0, 0, r.right - r.left, r.bottom - r.top);
6473 SetControlBounds(dataBrowser, &r);
6474 SetDataBrowserTableViewNamedColumnWidth(dataBrowser,
6475 kTabsColumn, r.right);
6476 }
6477 }
6478 break;
6479 }
6480
6481 return eventNotHandledErr;
6482}
6483
6484// Load DataBrowserChangeAttributes() dynamically on tiger (and better).
6485// This way the code works on 10.2 and 10.3 as well (it doesn't have the
6486// blue highlights in the list view on these systems, though. Oh well.)
6487
6488
6489#import <mach-o/dyld.h>
6490
6491enum { kMyDataBrowserAttributeListViewAlternatingRowColors = (1 << 1) };
6492
6493 static OSStatus
6494myDataBrowserChangeAttributes(ControlRef inDataBrowser,
6495 OptionBits inAttributesToSet,
6496 OptionBits inAttributesToClear)
6497{
6498 long osVersion;
6499 char *symbolName;
6500 NSSymbol symbol = NULL;
6501 OSStatus (*dataBrowserChangeAttributes)(ControlRef inDataBrowser,
6502 OptionBits inAttributesToSet, OptionBits inAttributesToClear);
6503
6504 Gestalt(gestaltSystemVersion, &osVersion);
6505 if (osVersion < 0x1040) // only supported for 10.4 (and up)
6506 return noErr;
6507
6508 // C name mangling...
6509 symbolName = "_DataBrowserChangeAttributes";
6510 if (!NSIsSymbolNameDefined(symbolName)
6511 || (symbol = NSLookupAndBindSymbol(symbolName)) == NULL)
6512 return noErr;
6513
6514 dataBrowserChangeAttributes = NSAddressOfSymbol(symbol);
6515 if (dataBrowserChangeAttributes == NULL)
6516 return noErr; // well...
6517 return dataBrowserChangeAttributes(inDataBrowser,
6518 inAttributesToSet, inAttributesToClear);
6519}
6520
6521 static void
6522initialise_tabline(void)
6523{
6524 Rect drawerRect = { 0, 0, 0, DRAWER_SIZE };
6525 DataBrowserCallbacks dbCallbacks;
6526 EventTypeSpec focusEvent = {kEventClassControl, kEventControlSetFocusPart};
6527 EventTypeSpec resizeEvent = {kEventClassWindow, kEventWindowBoundsChanged};
6528 DataBrowserListViewColumnDesc colDesc;
6529
6530 // drawers have to have compositing enabled
6531 CreateNewWindow(kDrawerWindowClass,
6532 kWindowStandardHandlerAttribute
6533 | kWindowCompositingAttribute
6534 | kWindowResizableAttribute
6535 | kWindowLiveResizeAttribute,
6536 &drawerRect, &drawer);
6537
6538 SetThemeWindowBackground(drawer, kThemeBrushDrawerBackground, true);
6539 SetDrawerParent(drawer, gui.VimWindow);
6540 SetDrawerOffsets(drawer, kWindowOffsetUnchanged, DRAWER_INSET);
6541
6542
6543 // create list view embedded in drawer
6544 CreateDataBrowserControl(drawer, &drawerRect, kDataBrowserListView,
6545 &dataBrowser);
6546
6547 dbCallbacks.version = kDataBrowserLatestCallbacks;
6548 InitDataBrowserCallbacks(&dbCallbacks);
6549 dbCallbacks.u.v1.itemDataCallback =
6550 NewDataBrowserItemDataUPP(dbItemDataCallback);
6551 dbCallbacks.u.v1.itemNotificationCallback =
6552 NewDataBrowserItemNotificationUPP(dbItemNotificationCallback);
6553 dbCallbacks.u.v1.getContextualMenuCallback =
6554 NewDataBrowserGetContextualMenuUPP(dbGetContextualMenuCallback);
6555 dbCallbacks.u.v1.selectContextualMenuCallback =
6556 NewDataBrowserSelectContextualMenuUPP(dbSelectContextualMenuCallback);
6557
6558 SetDataBrowserCallbacks(dataBrowser, &dbCallbacks);
6559
6560 SetDataBrowserListViewHeaderBtnHeight(dataBrowser, 0); // no header
6561 SetDataBrowserHasScrollBars(dataBrowser, false, true); // only vertical
6562 SetDataBrowserSelectionFlags(dataBrowser,
6563 kDataBrowserSelectOnlyOne | kDataBrowserNeverEmptySelectionSet);
6564 SetDataBrowserTableViewHiliteStyle(dataBrowser,
6565 kDataBrowserTableViewFillHilite);
6566 Boolean b = false;
6567 SetControlData(dataBrowser, kControlEntireControl,
6568 kControlDataBrowserIncludesFrameAndFocusTag, sizeof(b), &b);
6569
6570 // enable blue background in data browser (this is only in 10.4 and vim
6571 // has to support older osx versions as well, so we have to load this
6572 // function dynamically)
6573 myDataBrowserChangeAttributes(dataBrowser,
6574 kMyDataBrowserAttributeListViewAlternatingRowColors, 0);
6575
6576 // install callback that keeps focus in vim and away from the data browser
6577 InstallControlEventHandler(dataBrowser, dbFocusCallback, 1, &focusEvent,
6578 NULL, NULL);
6579
6580 // install callback that keeps data browser at the size of the drawer
6581 InstallWindowEventHandler(drawer, drawerCallback, 1, &resizeEvent,
6582 NULL, NULL);
6583
6584 // add "tabs" column to data browser
6585 colDesc.propertyDesc.propertyID = kTabsColumn;
6586 colDesc.propertyDesc.propertyType = kDataBrowserTextType;
6587
6588 // add if items can be selected (?): kDataBrowserListViewSelectionColumn
6589 colDesc.propertyDesc.propertyFlags = kDataBrowserDefaultPropertyFlags;
6590
6591 colDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
6592 colDesc.headerBtnDesc.minimumWidth = 100;
6593 colDesc.headerBtnDesc.maximumWidth = 150;
6594 colDesc.headerBtnDesc.titleOffset = 0;
6595 colDesc.headerBtnDesc.titleString = CFSTR("Tabs");
6596 colDesc.headerBtnDesc.initialOrder = kDataBrowserOrderIncreasing;
6597 colDesc.headerBtnDesc.btnFontStyle.flags = 0; // use default font
6598 colDesc.headerBtnDesc.btnContentInfo.contentType = kControlContentTextOnly;
6599
6600 AddDataBrowserListViewColumn(dataBrowser, &colDesc, 0);
6601
6602 // create tabline popup menu required by vim docs (see :he tabline-menu)
6603 CreateNewMenu(kTabContextMenuId, 0, &contextMenu);
Bram Moolenaar29547192018-12-11 20:39:19 +01006604 AppendMenuItemTextWithCFString(contextMenu, CFSTR("Close Tab"), 0,
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006605 TABLINE_MENU_CLOSE, NULL);
6606 AppendMenuItemTextWithCFString(contextMenu, CFSTR("New Tab"), 0,
6607 TABLINE_MENU_NEW, NULL);
6608 AppendMenuItemTextWithCFString(contextMenu, CFSTR("Open Tab..."), 0,
6609 TABLINE_MENU_OPEN, NULL);
6610}
6611
6612
6613/*
6614 * Show or hide the tabline.
6615 */
6616 void
6617gui_mch_show_tabline(int showit)
6618{
6619 if (showit == 0)
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006620 CloseDrawer(drawer, true);
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006621 else
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006622 OpenDrawer(drawer, kWindowEdgeRight, true);
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006623}
6624
6625/*
6626 * Return TRUE when tabline is displayed.
6627 */
6628 int
6629gui_mch_showing_tabline(void)
6630{
6631 WindowDrawerState state = GetDrawerState(drawer);
6632
6633 return state == kWindowDrawerOpen || state == kWindowDrawerOpening;
6634}
6635
6636/*
6637 * Update the labels of the tabline.
6638 */
6639 void
6640gui_mch_update_tabline(void)
6641{
6642 tabpage_T *tp;
6643 int numTabs = getTabCount();
6644 int nr = 1;
6645 int curtabidx = 1;
6646
6647 // adjust data browser
6648 if (tabLabels != NULL)
6649 {
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006650 int i;
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006651
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006652 for (i = 0; i < tabLabelsSize; ++i)
6653 CFRelease(tabLabels[i]);
6654 free(tabLabels);
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006655 }
6656 tabLabels = (CFStringRef *)malloc(numTabs * sizeof(CFStringRef));
6657 tabLabelsSize = numTabs;
6658
6659 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
6660 {
6661 if (tp == curtab)
6662 curtabidx = nr;
Bram Moolenaar1b60e502008-03-12 13:40:54 +00006663 tabLabels[nr-1] = getTabLabel(tp);
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006664 }
6665
6666 RemoveDataBrowserItems(dataBrowser, kDataBrowserNoItem, 0, NULL,
6667 kDataBrowserItemNoProperty);
6668 // data browser uses ids 1, 2, 3, ... numTabs per default, so we
6669 // can pass NULL for the id array
6670 AddDataBrowserItems(dataBrowser, kDataBrowserNoItem, numTabs, NULL,
6671 kDataBrowserItemNoProperty);
6672
6673 DataBrowserItemID item = curtabidx;
6674 SetDataBrowserSelectedItems(dataBrowser, 1, &item, kDataBrowserItemsAssign);
6675}
6676
6677/*
6678 * Set the current tab to "nr". First tab is 1.
6679 */
6680 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01006681gui_mch_set_curtab(int nr)
Bram Moolenaar79ee3152007-04-26 16:20:50 +00006682{
6683 DataBrowserItemID item = nr;
6684 SetDataBrowserSelectedItems(dataBrowser, 1, &item, kDataBrowserItemsAssign);
6685
6686 // TODO: call something like this?: (or restore scroll position, or...)
6687 RevealDataBrowserItem(dataBrowser, item, kTabsColumn,
6688 kDataBrowserRevealOnly);
6689}
6690
6691#endif // FEAT_GUI_TABLINE