blob: 06c8a6baa7760c68fb343f9d84526f0e467a7920 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sw=4 sts=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Photon GUI support by Julian Kinraid
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 *
9 *
10 * Clipboard support is in os_qnx.c
11 * PhAttach() is called in os_qnx.c:qnx_init()
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_TOOLBAR
17# include <photon/PxImage.h>
18#endif
19
20#if !defined(__QNX__)
21/* Used when generating prototypes. */
22# define PgColor_t int
23# define PhEvent_t int
24# define PhPoint_t int
25# define PtWidget_t int
26# define Pg_BLACK 0
27# define PtCallbackF_t int
28# define PtCallbackInfo_t int
29# define PhTile_t int
30# define PtWidget_t int
31# define PhImage_t int
32#endif
33
34#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(a[0]))
Bram Moolenaard2221132011-07-27 14:09:09 +020035#define RGB(r, g, b) PgRGB(r, g, b)
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaard2221132011-07-27 14:09:09 +020037#define EVENT_BUFFER_SIZE sizeof(PhEvent_t) + 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/* Some defines for gui_mch_mousehide() */
40#define MOUSE_HIDE TRUE
41#define MOUSE_SHOW FALSE
42
43/* Optional support for using a PtPanelGroup widget, needs work */
44#undef USE_PANEL_GROUP
45
46#ifdef USE_PANEL_GROUP
47static char *empty_title = " ";
48static char **panel_titles = NULL;
49static ushort_t num_panels = 0;
50static short pg_margin_left, pg_margin_right, pg_margin_top, pg_margin_bottom;
51#endif
52
53#define GUI_PH_MARGIN 4 /* Size of the bevel */
54
55#define GUI_PH_MOUSE_TYPE Ph_CURSOR_INSERT
56static PgColor_t gui_ph_mouse_color = Pg_BLACK;
57
58static PhPoint_t gui_ph_raw_offset;
59static PtWidget_t *gui_ph_timer_cursor; /* handle cursor blinking */
60static PtWidget_t *gui_ph_timer_timeout; /* used in gui_mch_wait_for_chars */
61static short is_timeout; /* Has the timeout occured? */
62
63/*
64 * This is set inside the mouse callback for a right mouse
65 * button click, and used for the popup menus
66 */
67static PhPoint_t abs_mouse;
68
69/* Try and avoid redraws while a resize is in progress */
70static int is_ignore_draw = FALSE;
71
72/* Used for converting to/from utf-8 and other charsets */
73static struct PxTransCtrl *charset_translate;
74
75/*
76 * Cursor blink functions.
77 *
78 * This is a simple state machine:
79 * BLINK_NONE not blinking at all
80 * BLINK_OFF blinking, cursor is not shown
81 * BLINK_ON blinking, cursor is shown
82 */
83static enum {
84 BLINK_NONE,
85 BLINK_OFF,
86 BLINK_ON
87} blink_state = BLINK_NONE;
88
89static long_u blink_waittime = 700;
90static long_u blink_ontime = 400;
91static long_u blink_offtime = 250;
92
93static struct
94{
95 int key_sym;
96 char_u vim_code0;
97 char_u vim_code1;
98} special_keys[] =
99{
100 {Pk_Up, 'k', 'u'},
101 {Pk_Down, 'k', 'd'},
102 {Pk_Left, 'k', 'l'},
103 {Pk_Right, 'k', 'r'},
104
105 {Pk_F1, 'k', '1'},
106 {Pk_F2, 'k', '2'},
107 {Pk_F3, 'k', '3'},
108 {Pk_F4, 'k', '4'},
109 {Pk_F5, 'k', '5'},
110 {Pk_F6, 'k', '6'},
111 {Pk_F7, 'k', '7'},
112 {Pk_F8, 'k', '8'},
113 {Pk_F9, 'k', '9'},
114 {Pk_F10, 'k', ';'},
115
116 {Pk_F11, 'F', '1'},
117 {Pk_F12, 'F', '2'},
118 {Pk_F13, 'F', '3'},
119 {Pk_F14, 'F', '4'},
120 {Pk_F15, 'F', '5'},
121 {Pk_F16, 'F', '6'},
122 {Pk_F17, 'F', '7'},
123 {Pk_F18, 'F', '8'},
124 {Pk_F19, 'F', '9'},
125 {Pk_F20, 'F', 'A'},
126
127 {Pk_F21, 'F', 'B'},
128 {Pk_F22, 'F', 'C'},
129 {Pk_F23, 'F', 'D'},
130 {Pk_F24, 'F', 'E'},
131 {Pk_F25, 'F', 'F'},
132 {Pk_F26, 'F', 'G'},
133 {Pk_F27, 'F', 'H'},
134 {Pk_F28, 'F', 'I'},
135 {Pk_F29, 'F', 'J'},
136
137 {Pk_F30, 'F', 'K'},
138 {Pk_F31, 'F', 'L'},
139 {Pk_F32, 'F', 'M'},
140 {Pk_F33, 'F', 'N'},
141 {Pk_F34, 'F', 'O'},
142 {Pk_F35, 'F', 'P'},
143
144 {Pk_Help, '%', '1'},
145 {Pk_BackSpace, 'k', 'b'},
146 {Pk_Insert, 'k', 'I'},
147 {Pk_Delete, 'k', 'D'},
148 {Pk_Home, 'k', 'h'},
149 {Pk_End, '@', '7'},
150 {Pk_Prior, 'k', 'P'},
151 {Pk_Next, 'k', 'N'},
152 {Pk_Print, '%', '9'},
153
154 {Pk_KP_Add, 'K', '6'},
155 {Pk_KP_Subtract,'K', '7'},
156 {Pk_KP_Divide, 'K', '8'},
157 {Pk_KP_Multiply,'K', '9'},
158 {Pk_KP_Enter, 'K', 'A'},
159
160 {Pk_KP_0, KS_EXTRA, KE_KINS}, /* Insert */
161 {Pk_KP_Decimal, KS_EXTRA, KE_KDEL}, /* Delete */
162
163 {Pk_KP_4, 'k', 'l'}, /* Left */
164 {Pk_KP_6, 'k', 'r'}, /* Right */
165 {Pk_KP_8, 'k', 'u'}, /* Up */
166 {Pk_KP_2, 'k', 'd'}, /* Down */
167
168 {Pk_KP_7, 'K', '1'}, /* Home */
169 {Pk_KP_1, 'K', '4'}, /* End */
170
171 {Pk_KP_9, 'K', '3'}, /* Page Up */
172 {Pk_KP_3, 'K', '5'}, /* Page Down */
173
174 {Pk_KP_5, '&', '8'}, /* Undo */
175
176 /* Keys that we want to be able to use any modifier with: */
177 {Pk_Return, CAR, NUL},
178 {Pk_space, ' ', NUL},
179 {Pk_Tab, TAB, NUL},
180 {Pk_Escape, ESC, NUL},
181 {NL, NL, NUL},
182 {CAR, CAR, NUL},
183
184 /* End of list marker: */
185 {0, 0, 0}
186};
187
188
189/****************************************************************************/
190
191static PtCallbackF_t gui_ph_handle_timer_cursor;
192static PtCallbackF_t gui_ph_handle_timer_timeout;
193
194static PtCallbackF_t gui_ph_handle_window_cb;
195
196static PtCallbackF_t gui_ph_handle_scrollbar;
197static PtCallbackF_t gui_ph_handle_keyboard;
198static PtCallbackF_t gui_ph_handle_mouse;
199static PtCallbackF_t gui_ph_handle_pulldown_menu;
200static PtCallbackF_t gui_ph_handle_menu;
201static PtCallbackF_t gui_ph_handle_focus; /* focus change of text area */
202
203static PtCallbackF_t gui_ph_handle_menu_resize;
204
205/* When a menu is unrealized, give focus back to vimTextArea */
206static PtCallbackF_t gui_ph_handle_menu_unrealized;
207
208#ifdef USE_PANEL_GROUP
Bram Moolenaard2221132011-07-27 14:09:09 +0200209static void gui_ph_get_panelgroup_margins(short*, short*, short*, short*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#endif
211
212#ifdef FEAT_TOOLBAR
Bram Moolenaard2221132011-07-27 14:09:09 +0200213static PhImage_t *gui_ph_toolbar_find_icon(vimmenu_T *menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#endif
215
Bram Moolenaard2221132011-07-27 14:09:09 +0200216static void gui_ph_draw_start(void);
217static void gui_ph_draw_end(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219/* Set the text for the balloon */
Bram Moolenaard2221132011-07-27 14:09:09 +0200220static PtWidget_t * gui_ph_show_tooltip(PtWidget_t *window,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 PtWidget_t *widget,
222 int position,
223 char *text,
224 char *font,
225 PgColor_t fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200226 PgColor_t text_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228/****************************************************************************/
229
Bram Moolenaard2221132011-07-27 14:09:09 +0200230static PtWidget_t * gui_ph_show_tooltip(PtWidget_t *window,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 PtWidget_t *widget,
232 int position,
233 char *text,
234 char *font,
235 PgColor_t fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200236 PgColor_t text_color)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
238 PtArg_t arg;
239 vimmenu_T *menu;
240 char_u *tooltip;
241
Bram Moolenaard2221132011-07-27 14:09:09 +0200242 PtSetArg(&arg, Pt_ARG_POINTER, &menu, 0);
243 PtGetResources(widget, 1, &arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 /* Override the text and position */
246
247 tooltip = text;
Bram Moolenaard2221132011-07-27 14:09:09 +0200248 if (menu != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {
250 int index = MENU_INDEX_TIP;
Bram Moolenaard2221132011-07-27 14:09:09 +0200251 if (menu->strings[ index ] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 tooltip = menu->strings[ index ];
253 }
254
Bram Moolenaard2221132011-07-27 14:09:09 +0200255 return PtInflateBalloon(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 window,
257 widget,
258 /* Don't put the balloon at the bottom,
259 * it gets drawn over by gfx done in the PtRaw */
260 Pt_BALLOON_TOP,
261 tooltip,
262 font,
263 fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200264 text_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265}
266
267 static void
Bram Moolenaard2221132011-07-27 14:09:09 +0200268gui_ph_resize_container(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269{
270 PhArea_t area;
271
Bram Moolenaard2221132011-07-27 14:09:09 +0200272 PtWidgetArea(gui.vimWindow, &area);
273 PtWidgetPos (gui.vimContainer, &area.pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
Bram Moolenaard2221132011-07-27 14:09:09 +0200275 PtSetResource(gui.vimContainer, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276}
277
278 static int
279gui_ph_handle_menu_resize(
280 PtWidget_t *widget,
281 void *other,
Bram Moolenaard2221132011-07-27 14:09:09 +0200282 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283{
284 PtContainerCallback_t *sizes = info->cbdata;
285 PtWidget_t *container;
286 PhPoint_t below_menu;
287 int_u height;
288
289 height = sizes->new_dim.h;
290
Bram Moolenaarccc18222007-05-10 18:25:20 +0000291 /* Because vim treats the toolbar and menubar separately,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 * and here they're lumped together into a PtToolbarGroup,
293 * we only need either menu_height or toolbar_height set at once */
Bram Moolenaard2221132011-07-27 14:09:09 +0200294 if (gui.menu_is_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {
296 gui.menu_height = height;
297 gui.toolbar_height = 0;
298 }
299#ifdef FEAT_TOOLBAR
300 else
301 gui.toolbar_height = height;
302#endif
303
304 below_menu.x = 0;
305 below_menu.y = height;
306
307#ifdef USE_PANEL_GROUP
308 container = gui.vimPanelGroup;
309#else
310 container = gui.vimContainer;
311#endif
312
Bram Moolenaard2221132011-07-27 14:09:09 +0200313 PtSetResource(container, Pt_ARG_POS, &below_menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315 gui_ph_resize_container();
316
317#ifdef USE_PANEL_GROUP
318 gui_ph_get_panelgroup_margins(
319 &pg_margin_top, &pg_margin_bottom,
Bram Moolenaard2221132011-07-27 14:09:09 +0200320 &pg_margin_left, &pg_margin_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#endif
Bram Moolenaard2221132011-07-27 14:09:09 +0200322 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323}
324
325/*
326 * Pt_ARG_TIMER_REPEAT isn't used because the on & off times
327 * are different
328 */
329 static int
330gui_ph_handle_timer_cursor(
331 PtWidget_t *widget,
332 void *data,
Bram Moolenaard2221132011-07-27 14:09:09 +0200333 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
Bram Moolenaard2221132011-07-27 14:09:09 +0200335 if (blink_state == BLINK_ON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 {
337 gui_undraw_cursor();
338 blink_state = BLINK_OFF;
Bram Moolenaard2221132011-07-27 14:09:09 +0200339 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
340 blink_offtime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
342 else
343 {
344 gui_update_cursor(TRUE, FALSE);
345 blink_state = BLINK_ON;
Bram Moolenaard2221132011-07-27 14:09:09 +0200346 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
347 blink_ontime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200349 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350}
351
352 static int
353gui_ph_handle_timer_timeout(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
354{
355 is_timeout = TRUE;
356
Bram Moolenaard2221132011-07-27 14:09:09 +0200357 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358}
359
360 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200361gui_ph_handle_window_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362{
363 PhWindowEvent_t *we = info->cbdata;
364 ushort_t *width, *height;
365
Bram Moolenaard0988c52011-08-10 12:19:04 +0200366 switch (we->event_f) {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 case Ph_WM_CLOSE:
368 gui_shell_closed();
369 break;
370
371 case Ph_WM_FOCUS:
372 /* Just in case it's hidden and needs to be shown */
Bram Moolenaard0988c52011-08-10 12:19:04 +0200373 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
Bram Moolenaard0988c52011-08-10 12:19:04 +0200375 if (we->event_state == Ph_WM_EVSTATE_FOCUS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {
377 gui_focus_change(TRUE);
378 gui_mch_start_blink();
379 }
380 else
381 {
382 gui_focus_change(FALSE);
383 gui_mch_stop_blink();
384 }
385 break;
386
387 case Ph_WM_RESIZE:
Bram Moolenaard0988c52011-08-10 12:19:04 +0200388 PtGetResource(gui.vimWindow, Pt_ARG_WIDTH, &width, 0);
389 PtGetResource(gui.vimWindow, Pt_ARG_HEIGHT, &height, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#ifdef USE_PANEL_GROUP
391 width -= (pg_margin_left + pg_margin_right);
392 height -= (pg_margin_top + pg_margin_bottom);
393#endif
Bram Moolenaard0988c52011-08-10 12:19:04 +0200394 gui_resize_shell(*width, *height);
395 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 is_ignore_draw = FALSE;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200397 PtEndFlux(gui.vimContainer);
398 PtContainerRelease(gui.vimContainer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 break;
400
401 default:
402 break;
403 }
404
Bram Moolenaard2221132011-07-27 14:09:09 +0200405 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406}
407
408 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200409gui_ph_handle_scrollbar(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
411 PtScrollbarCallback_t *scroll;
412 scrollbar_T *sb;
413 int value, dragging = FALSE;
414
415 scroll = info->cbdata;
416
417 sb = (scrollbar_T *) data;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200418 if (sb != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
420 value = scroll->position;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200421 switch (scroll->action)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {
423 case Pt_SCROLL_DRAGGED:
424 dragging = TRUE;
425 break;
426
427 case Pt_SCROLL_SET:
428 /* FIXME: return straight away here? */
Bram Moolenaard2221132011-07-27 14:09:09 +0200429 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 break;
431 }
432
433 gui_drag_scrollbar(sb, value, dragging);
434 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200435 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436}
437
438 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200439gui_ph_handle_keyboard(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
441 PhKeyEvent_t *key;
442 unsigned char string[6];
443 int len, i;
444 int ch, modifiers;
445
Bram Moolenaard0988c52011-08-10 12:19:04 +0200446 key = PhGetData(info->event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
448 ch = modifiers = len = 0;
449
Bram Moolenaard0988c52011-08-10 12:19:04 +0200450 if (p_mh)
451 gui_mch_mousehide(MOUSE_HIDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453 /* We're a good lil photon program, aren't we? yes we are, yeess wee arrr */
Bram Moolenaard0988c52011-08-10 12:19:04 +0200454 if (key->key_flags & Pk_KF_Compose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 {
Bram Moolenaard2221132011-07-27 14:09:09 +0200456 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 }
458
Bram Moolenaard0988c52011-08-10 12:19:04 +0200459 if ((key->key_flags & Pk_KF_Cap_Valid) &&
460 PkIsKeyDown(key->key_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {
462#ifdef FEAT_MENU
463 /*
464 * Only show the menu if the Alt key is down, and the Shift & Ctrl
465 * keys aren't down, as well as the other conditions
466 */
Bram Moolenaard0988c52011-08-10 12:19:04 +0200467 if (((key->key_mods & Pk_KM_Alt) &&
468 !(key->key_mods & Pk_KM_Shift) &&
469 !(key->key_mods & Pk_KM_Ctrl)) &&
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 gui.menu_is_active &&
Bram Moolenaard0988c52011-08-10 12:19:04 +0200471 (*p_wak == 'y' ||
472 (*p_wak == 'm' &&
473 gui_is_menu_shortcut(key->key_cap))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {
475 /* Fallthrough and let photon look for the hotkey */
Bram Moolenaard2221132011-07-27 14:09:09 +0200476 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 }
478#endif
479
Bram Moolenaar15d63192011-09-14 16:05:15 +0200480 for (i = 0; special_keys[i].key_sym != 0; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200482 if (special_keys[i].key_sym == key->key_cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 {
484 len = 0;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200485 if (special_keys[i].vim_code1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 ch = special_keys[i].vim_code0;
487 else
488 {
489 /* Detect if a keypad number key has been pressed
490 * and change the key if Num Lock is on */
Bram Moolenaard0988c52011-08-10 12:19:04 +0200491 if (key->key_cap >= Pk_KP_Enter && key->key_cap <= Pk_KP_9
492 && (key->key_mods & Pk_KM_Num_Lock))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 {
494 /* FIXME: For now, just map the key to a ascii value
495 * (see <photon/PkKeyDef.h>) */
496 ch = key->key_cap - 0xf080;
497 }
498 else
Bram Moolenaard0988c52011-08-10 12:19:04 +0200499 ch = TO_SPECIAL(special_keys[i].vim_code0,
500 special_keys[i].vim_code1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
502 break;
503 }
504 }
505
Bram Moolenaard0988c52011-08-10 12:19:04 +0200506 if (key->key_mods & Pk_KM_Ctrl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 modifiers |= MOD_MASK_CTRL;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200508 if (key->key_mods & Pk_KM_Alt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 modifiers |= MOD_MASK_ALT;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200510 if (key->key_mods & Pk_KM_Shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 modifiers |= MOD_MASK_SHIFT;
512
513 /* Is this not a special key? */
Bram Moolenaard0988c52011-08-10 12:19:04 +0200514 if (special_keys[i].key_sym == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200516 ch = PhTo8859_1(key);
517 if (ch == -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#ifdef FEAT_MBYTE
Bram Moolenaard0988c52011-08-10 12:19:04 +0200519 || (enc_utf8 && ch > 127)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520#endif
521 )
522 {
523#ifdef FEAT_MBYTE
Bram Moolenaard0988c52011-08-10 12:19:04 +0200524 len = PhKeyToMb(string, key);
525 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
527 static char buf[6];
528 int src_taken, dst_made;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200529 if (enc_utf8 != TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
531 PxTranslateFromUTF(
532 charset_translate,
533 string,
534 len,
535 &src_taken,
536 buf,
537 6,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200538 &dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200540 add_to_input_buf(buf, dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
542 else
543 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200544 add_to_input_buf(string, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546
Bram Moolenaard2221132011-07-27 14:09:09 +0200547 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549 len = 0;
550#endif
551 ch = key->key_cap;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200552 if (ch < 0xff)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
554 /* FIXME: is this the right thing to do? */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200555 if (modifiers & MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 {
557 modifiers &= ~MOD_MASK_CTRL;
558
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200559 if ((ch >= 'a' && ch <= 'z') ||
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 ch == '[' ||
561 ch == ']' ||
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200562 ch == '\\')
563 ch = Ctrl_chr(ch);
564 else if (ch == '2')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 ch = NUL;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200566 else if (ch == '6')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 ch = 0x1e;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200568 else if (ch == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 ch = 0x1f;
570 else
571 modifiers |= MOD_MASK_CTRL;
572 }
573
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200574 if (modifiers & MOD_MASK_ALT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200576 ch = Meta(ch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 modifiers &= ~MOD_MASK_ALT;
578 }
579 }
580 else
581 {
Bram Moolenaard2221132011-07-27 14:09:09 +0200582 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584 }
585 else
586 modifiers &= ~MOD_MASK_SHIFT;
587 }
588
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200589 ch = simplify_key(ch, &modifiers);
590 if (modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {
592 string[ len++ ] = CSI;
593 string[ len++ ] = KS_MODIFIER;
594 string[ len++ ] = modifiers;
595 }
596
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200597 if (IS_SPECIAL(ch))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
599 string[ len++ ] = CSI;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200600 string[ len++ ] = K_SECOND(ch);
601 string[ len++ ] = K_THIRD(ch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603 else
604 {
605 string[ len++ ] = ch;
606 }
607
608 if (len == 1 && ((ch == Ctrl_C && ctrl_c_interrupts)
609 || ch == intr_char))
610 {
611 trash_input_buf();
612 got_int = TRUE;
613 }
614
615 if (len == 1 && string[0] == CSI)
616 {
617 /* Turn CSI into K_CSI. */
618 string[ len++ ] = KS_EXTRA;
619 string[ len++ ] = KE_CSI;
620 }
621
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200622 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200624 add_to_input_buf(string, len);
Bram Moolenaard2221132011-07-27 14:09:09 +0200625 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
627 }
628
Bram Moolenaard2221132011-07-27 14:09:09 +0200629 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630}
631
632 static int
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200633gui_ph_handle_mouse(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 PhPointerEvent_t *pointer;
636 PhRect_t *pos;
637 int button = 0, repeated_click, modifiers = 0x0;
638 short mouse_x, mouse_y;
639
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200640 pointer = PhGetData(info->event);
641 pos = PhGetRects(info->event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200643 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 /*
646 * Coordinates need to be relative to the base window,
647 * not relative to the vimTextArea widget
648 */
649 mouse_x = pos->ul.x + gui.border_width;
650 mouse_y = pos->ul.y + gui.border_width;
651
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200652 if (info->event->type == Ph_EV_PTR_MOTION_NOBUTTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200654 gui_mouse_moved(mouse_x, mouse_y);
Bram Moolenaard2221132011-07-27 14:09:09 +0200655 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200658 if (pointer->key_mods & Pk_KM_Shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 modifiers |= MOUSE_SHIFT;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200660 if (pointer->key_mods & Pk_KM_Ctrl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 modifiers |= MOUSE_CTRL;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200662 if (pointer->key_mods & Pk_KM_Alt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 modifiers |= MOUSE_ALT;
664
665 /*
666 * FIXME More than one button may be involved, but for
667 * now just deal with one
668 */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200669 if (pointer->buttons & Ph_BUTTON_SELECT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 button = MOUSE_LEFT;
671
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200672 if (pointer->buttons & Ph_BUTTON_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
674 button = MOUSE_RIGHT;
675 /* Need the absolute coordinates for the popup menu */
676 abs_mouse.x = pointer->pos.x;
677 abs_mouse.y = pointer->pos.y;
678 }
679
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200680 if (pointer->buttons & Ph_BUTTON_ADJUST)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 button = MOUSE_MIDDLE;
682
683 /* Catch a real release (not phantom or other releases */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200684 if (info->event->type == Ph_EV_BUT_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 button = MOUSE_RELEASE;
686
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200687 if (info->event->type & Ph_EV_PTR_MOTION_BUTTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 button = MOUSE_DRAG;
689
690#if 0
691 /* Vim doesn't use button repeats */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200692 if (info->event->type & Ph_EV_BUT_REPEAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 button = MOUSE_DRAG;
694#endif
695
696 /* Don't do anything if it is one of the phantom mouse release events */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200697 if ((button != MOUSE_RELEASE) ||
698 (info->event->subtype == Ph_EV_RELEASE_REAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 repeated_click = (pointer->click_count >= 2) ? TRUE : FALSE;
701
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200702 gui_send_mouse_event(button , mouse_x, mouse_y, repeated_click, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704
Bram Moolenaard2221132011-07-27 14:09:09 +0200705 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706}
707
708/* Handle a focus change of the PtRaw widget */
709 static int
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200710gui_ph_handle_focus(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200712 if (info->reason == Pt_CB_LOST_FOCUS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200714 PtRemoveEventHandler(gui.vimTextArea, Ph_EV_PTR_MOTION_NOBUTTON,
715 gui_ph_handle_mouse, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200717 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 else
720 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200721 PtAddEventHandler(gui.vimTextArea, Ph_EV_PTR_MOTION_NOBUTTON,
722 gui_ph_handle_mouse, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200724 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727 static void
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200728gui_ph_handle_raw_draw(PtWidget_t *widget, PhTile_t *damage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 PhRect_t *r;
731 PhPoint_t offset;
732 PhPoint_t translation;
733
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200734 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 return;
736
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200737 PtSuperClassDraw(PtBasic, widget, damage);
738 PgGetTranslation(&translation);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 PgClearTranslation();
740
741#if 0
742 /*
Bram Moolenaarccc18222007-05-10 18:25:20 +0000743 * This causes some weird problems, with drawing being done from
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * within this raw drawing function (rather than just simple clearing
745 * and text drawing done by gui_redraw)
746 *
747 * The main problem is when PhBlit is used, and the cursor appearing
748 * in places where it shouldn't
749 */
750 out_flush();
751#endif
752
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200753 PtWidgetOffset(widget, &offset);
754 PhTranslatePoint(&offset, PtWidgetPos(gui.vimTextArea, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756#if 1
757 /* Redraw individual damage regions */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200758 if (damage->next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 damage = damage->next;
760
Bram Moolenaar15d63192011-09-14 16:05:15 +0200761 while (damage != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {
763 r = &damage->rect;
764 gui_redraw(
765 r->ul.x - offset.x, r->ul.y - offset.y,
766 r->lr.x - r->ul.x + 1,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200767 r->lr.y - r->ul.y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 damage = damage->next;
769 }
770#else
771 /* Redraw the rectangle that covers all the damaged regions */
772 r = &damage->rect;
773 gui_redraw(
774 r->ul.x - offset.x, r->ul.y - offset.y,
775 r->lr.x - r->ul.x + 1,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200776 r->lr.y - r->ul.y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200779 PgSetTranslation(&translation, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782 static int
783gui_ph_handle_pulldown_menu(
784 PtWidget_t *widget,
785 void *data,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200786 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200788 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
790 vimmenu_T *menu = (vimmenu_T *) data;
791
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200792 PtPositionMenu(menu->submenu_id, NULL);
793 PtRealizeWidget(menu->submenu_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795
Bram Moolenaard2221132011-07-27 14:09:09 +0200796 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
799/* This is used for pulldown/popup menus and also toolbar buttons */
800 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +0200801gui_ph_handle_menu(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200803 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 vimmenu_T *menu = (vimmenu_T *) data;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200806 gui_menu_cb(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200808 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/* Stop focus from disappearing into the menubar... */
812 static int
813gui_ph_handle_menu_unrealized(
814 PtWidget_t *widget,
815 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200816 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200818 PtGiveFocus(gui.vimTextArea, NULL);
Bram Moolenaard2221132011-07-27 14:09:09 +0200819 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821
822 static int
823gui_ph_handle_window_open(
824 PtWidget_t *widget,
825 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200826 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200828 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaard2221132011-07-27 14:09:09 +0200829 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
832/****************************************************************************/
833
834#define DRAW_START gui_ph_draw_start()
835#define DRAW_END gui_ph_draw_end()
836
837/* TODO: Set a clipping rect? */
838 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200839gui_ph_draw_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaara0b19972009-07-01 14:13:18 +0000841 PhGC_t *gc;
842
843 gc = PgGetGC();
Bram Moolenaar15d63192011-09-14 16:05:15 +0200844 PgSetRegion(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)));
845 PgClearClippingsCx(gc);
846 PgClearTranslationCx(gc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar15d63192011-09-14 16:05:15 +0200848 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
849 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar15d63192011-09-14 16:05:15 +0200851 PgSetTranslation(&gui_ph_raw_offset, Pg_RELATIVE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853
854 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200855gui_ph_draw_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856{
857 gui_ph_raw_offset.x = -gui_ph_raw_offset.x;
858 gui_ph_raw_offset.y = -gui_ph_raw_offset.y;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200859 PgSetTranslation(&gui_ph_raw_offset, Pg_RELATIVE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
862#ifdef USE_PANEL_GROUP
863 static vimmenu_T *
Bram Moolenaar15d63192011-09-14 16:05:15 +0200864gui_ph_find_buffer_item(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 vimmenu_T *top_level = root_menu;
867 vimmenu_T *items = NULL;
868
Bram Moolenaar15d63192011-09-14 16:05:15 +0200869 while (top_level != NULL &&
870 (STRCMP(top_level->dname, "Buffers") != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 top_level = top_level->next;
872
Bram Moolenaar15d63192011-09-14 16:05:15 +0200873 if (top_level != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
875 items = top_level->children;
876
Bram Moolenaar15d63192011-09-14 16:05:15 +0200877 while (items != NULL &&
878 (STRCMP(items->dname, name) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 items = items->next;
880 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200881 return items;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882}
883
884 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200885gui_ph_pg_set_buffer_num(int_u buf_num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
887 int i;
888 char search[16];
889 char *mark;
890
Bram Moolenaar15d63192011-09-14 16:05:15 +0200891 if (gui.vimTextArea == NULL || buf_num == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 return;
893
894 search[0] = '(';
Bram Moolenaar15d63192011-09-14 16:05:15 +0200895 ultoa(buf_num, &search[1], 10);
896 STRCAT(search, ")");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaar15d63192011-09-14 16:05:15 +0200898 for (i = 0; i < num_panels; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
900 /* find the last "(" in the panel title and see if the buffer
901 * number in the title matches the one we're looking for */
Bram Moolenaar15d63192011-09-14 16:05:15 +0200902 mark = STRRCHR(panel_titles[ i ], '(');
903 if (mark != NULL && STRCMP(mark, search) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200905 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_CURRENT_INDEX,
906 i, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
908 }
909}
910
911 static int
912gui_ph_handle_pg_change(
913 PtWidget_t *widget,
914 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200915 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
917 vimmenu_T *menu;
918 PtPanelGroupCallback_t *panel;
919
Bram Moolenaar15d63192011-09-14 16:05:15 +0200920 if (info->event != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
922 panel = info->cbdata;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200923 if (panel->new_panel != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200925 menu = gui_ph_find_buffer_item(panel->new_panel);
926 if (menu)
927 gui_menu_cb(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 }
929 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200930 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931}
932
933 static void
934gui_ph_get_panelgroup_margins(
935 short *top,
936 short *bottom,
937 short *left,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200938 short *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
940 unsigned short abs_raw_x, abs_raw_y, abs_panel_x, abs_panel_y;
941 const unsigned short *margin_top, *margin_bottom;
942 const unsigned short *margin_left, *margin_right;
943
Bram Moolenaar15d63192011-09-14 16:05:15 +0200944 PtGetAbsPosition(gui.vimTextArea, &abs_raw_x, &abs_raw_y);
945 PtGetAbsPosition(gui.vimPanelGroup, &abs_panel_x, &abs_panel_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
Bram Moolenaar15d63192011-09-14 16:05:15 +0200947 PtGetResource(gui.vimPanelGroup, Pt_ARG_MARGIN_RIGHT, &margin_right, 0);
948 PtGetResource(gui.vimPanelGroup, Pt_ARG_MARGIN_BOTTOM, &margin_bottom, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 abs_raw_x -= abs_panel_x;
951 abs_raw_y -= abs_panel_y;
952
953 *top = abs_raw_y;
954 *bottom = *margin_bottom;
955
956 *left = abs_raw_x;
957 *right = *margin_right;
958}
959
960/* Used for the tabs for PtPanelGroup */
961 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +0200962gui_ph_is_buffer_item(vimmenu_T *menu, vimmenu_T *parent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 char *mark;
965
Bram Moolenaar15d63192011-09-14 16:05:15 +0200966 if (STRCMP(parent->dname, "Buffers") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
968 /* Look for '(' digits ')' */
Bram Moolenaar15d63192011-09-14 16:05:15 +0200969 mark = vim_strchr(menu->dname, '(');
970 if (mark != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 mark++;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200973 while (isdigit(*mark))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 mark++;
975
Bram Moolenaar15d63192011-09-14 16:05:15 +0200976 if (*mark == ')')
Bram Moolenaard2221132011-07-27 14:09:09 +0200977 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200980 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982
983 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200984gui_ph_pg_add_buffer(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 char **new_titles = NULL;
987
Bram Moolenaar15d63192011-09-14 16:05:15 +0200988 new_titles = (char **) alloc((num_panels + 1) * sizeof(char **));
989 if (new_titles != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200991 if (num_panels > 0)
992 memcpy(new_titles, panel_titles, num_panels * sizeof(char **));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 new_titles[ num_panels++ ] = name;
995
Bram Moolenaar15d63192011-09-14 16:05:15 +0200996 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, new_titles,
997 num_panels);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar15d63192011-09-14 16:05:15 +0200999 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 panel_titles = new_titles;
1001 }
1002}
1003
1004 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +02001005gui_ph_pg_remove_buffer(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 int i;
1008 char **new_titles = NULL;
1009
1010 /* If there is only 1 panel, we just use the temporary place holder */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001011 if (num_panels > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001013 new_titles = (char **) alloc((num_panels - 1) * sizeof(char **));
1014 if (new_titles != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
1016 char **s = new_titles;
1017 /* Copy all the titles except the one we're removing */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001018 for (i = 0; i < num_panels; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001020 if (STRCMP(panel_titles[ i ], name) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 *s++ = panel_titles[ i ];
1023 }
1024 }
1025 num_panels--;
1026
Bram Moolenaar15d63192011-09-14 16:05:15 +02001027 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, new_titles,
1028 num_panels);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
Bram Moolenaar15d63192011-09-14 16:05:15 +02001030 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 panel_titles = new_titles;
1032 }
1033 }
1034 else
1035 {
1036 num_panels--;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001037 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, &empty_title,
1038 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar15d63192011-09-14 16:05:15 +02001040 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 panel_titles = NULL;
1042 }
1043}
1044
1045/* When a buffer item is deleted from the buffer menu */
1046 static int
1047gui_ph_handle_buffer_remove(
1048 PtWidget_t *widget,
1049 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001050 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 vimmenu_T *menu;
1053
Bram Moolenaar15d63192011-09-14 16:05:15 +02001054 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 {
1056 menu = (vimmenu_T *) data;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001057 gui_ph_pg_remove_buffer(menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059
Bram Moolenaard2221132011-07-27 14:09:09 +02001060 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062#endif
1063
1064 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001065gui_ph_pane_resize(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001067 if (PtWidgetIsRealized(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
1069 is_ignore_draw = TRUE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001070 PtStartFlux(gui.vimContainer);
1071 PtContainerHold(gui.vimContainer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
1073
Bram Moolenaard2221132011-07-27 14:09:09 +02001074 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075}
1076
1077/****************************************************************************/
1078
1079#ifdef FEAT_MBYTE
1080 void
Bram Moolenaar15d63192011-09-14 16:05:15 +02001081gui_ph_encoding_changed(int new_encoding)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 /* Default encoding is latin1 */
1084 char *charset = "latin1";
1085 int i;
1086
1087 struct {
1088 int encoding;
1089 char *name;
1090 } charsets[] = {
1091 { DBCS_JPN, "SHIFT_JIS" },
1092 { DBCS_KOR, "csEUCKR" },
1093 { DBCS_CHT, "big5" },
1094 { DBCS_CHS, "gb" }
1095 };
1096
Bram Moolenaar15d63192011-09-14 16:05:15 +02001097 for (i = 0; i < ARRAY_LENGTH(charsets); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001099 if (new_encoding == charsets[ i ].encoding)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 charset = charsets[ i ].name;
1101 }
1102
Bram Moolenaar15d63192011-09-14 16:05:15 +02001103 charset_translate = PxTranslateSet(charset_translate, charset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105#endif
1106
1107/****************************************************************************/
1108/****************************************************************************/
1109
1110 void
1111gui_mch_prepare(argc, argv)
1112 int *argc;
1113 char **argv;
1114{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001115 PtInit(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116}
1117
1118 int
1119gui_mch_init(void)
1120{
1121 PtArg_t args[10];
1122 int flags = 0, n = 0;
1123
Bram Moolenaarccc18222007-05-10 18:25:20 +00001124 PhDim_t window_size = {100, 100}; /* Arbitrary values */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 PhPoint_t pos = {0, 0};
1126
Bram Moolenaar15d63192011-09-14 16:05:15 +02001127 gui.event_buffer = (PhEvent_t *) alloc(EVENT_BUFFER_SIZE);
1128 if (gui.event_buffer == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
1131 /* Get a translation so we can convert from ISO Latin-1 to UTF */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001132 charset_translate = PxTranslateSet(NULL, "latin1");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134 /* The +2 is for the 1 pixel dark line on each side */
1135 gui.border_offset = gui.border_width = GUI_PH_MARGIN + 2;
1136
1137 /* Handle close events ourselves */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001138 PtSetArg(&args[ n++ ], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_CLOSE);
1139 PtSetArg(&args[ n++ ], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE,
1140 Ph_WM_CLOSE | Ph_WM_RESIZE | Ph_WM_FOCUS);
1141 PtSetArg(&args[ n++ ], Pt_ARG_DIM, &window_size, 0);
1142 gui.vimWindow = PtCreateWidget(PtWindow, NULL, n, args);
1143 if (gui.vimWindow == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001144 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
Bram Moolenaar15d63192011-09-14 16:05:15 +02001146 PtAddCallback(gui.vimWindow, Pt_CB_WINDOW, gui_ph_handle_window_cb, NULL);
1147 PtAddCallback(gui.vimWindow, Pt_CB_WINDOW_OPENING,
1148 gui_ph_handle_window_open, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001151 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS, Pt_ANCHOR_ALL, Pt_IS_ANCHORED);
1152 PtSetArg(&args[ n++ ], Pt_ARG_DIM, &window_size, 0);
1153 PtSetArg(&args[ n++ ], Pt_ARG_POS, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155#ifdef USE_PANEL_GROUP
1156 /* Put in a temprary place holder title */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001157 PtSetArg(&args[ n++ ], Pt_ARG_PG_PANEL_TITLES, &empty_title, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar15d63192011-09-14 16:05:15 +02001159 gui.vimPanelGroup = PtCreateWidget(PtPanelGroup, gui.vimWindow, n, args);
1160 if (gui.vimPanelGroup == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001161 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
Bram Moolenaar15d63192011-09-14 16:05:15 +02001163 PtAddCallback(gui.vimPanelGroup, Pt_CB_PG_PANEL_SWITCHING,
1164 gui_ph_handle_pg_change, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#else
1166 /* Turn off all edge decorations */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001167 PtSetArg(&args[ n++ ], Pt_ARG_BASIC_FLAGS, Pt_FALSE, Pt_ALL);
1168 PtSetArg(&args[ n++ ], Pt_ARG_BEVEL_WIDTH, 0, 0);
1169 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_WIDTH, 0, 0);
1170 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_HEIGHT, 0, 0);
1171 PtSetArg(&args[ n++ ], Pt_ARG_CONTAINER_FLAGS, Pt_TRUE, Pt_AUTO_EXTENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
Bram Moolenaar15d63192011-09-14 16:05:15 +02001173 gui.vimContainer = PtCreateWidget(PtPane, gui.vimWindow, n, args);
1174 if (gui.vimContainer == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001175 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaar15d63192011-09-14 16:05:15 +02001177 PtAddCallback(gui.vimContainer, Pt_CB_RESIZE, gui_ph_pane_resize, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179
1180 /* Size for the text area is set in gui_mch_set_text_area_pos */
1181 n = 0;
1182
Bram Moolenaar15d63192011-09-14 16:05:15 +02001183 PtSetArg(&args[ n++ ], Pt_ARG_RAW_DRAW_F, gui_ph_handle_raw_draw, 1);
1184 PtSetArg(&args[ n++ ], Pt_ARG_BEVEL_WIDTH, GUI_PH_MARGIN, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 /*
1186 * Using focus render also causes the whole widget to be redrawn
1187 * whenever it changes focus, which is very annoying :p
1188 */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001189 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_TRUE,
1190 Pt_GETS_FOCUS | Pt_HIGHLIGHTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#ifndef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001192 PtSetArg(&args[ n++ ], Pt_ARG_CURSOR_TYPE, GUI_PH_MOUSE_TYPE, 0);
1193 PtSetArg(&args[ n++ ], Pt_ARG_CURSOR_COLOR, gui_ph_mouse_color, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#endif
1195
Bram Moolenaar15d63192011-09-14 16:05:15 +02001196 gui.vimTextArea = PtCreateWidget(PtRaw, Pt_DFLT_PARENT, n, args);
1197 if (gui.vimTextArea == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
1200 /* TODO: use PtAddEventHandlers instead? */
1201 /* Not using Ph_EV_BUT_REPEAT because vim wouldn't use it anyway */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001202 PtAddEventHandler(gui.vimTextArea,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 Ph_EV_BUT_PRESS | Ph_EV_BUT_RELEASE | Ph_EV_PTR_MOTION_BUTTON,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001204 gui_ph_handle_mouse, NULL);
1205 PtAddEventHandler(gui.vimTextArea, Ph_EV_KEY,
1206 gui_ph_handle_keyboard, NULL);
1207 PtAddCallback(gui.vimTextArea, Pt_CB_GOT_FOCUS,
1208 gui_ph_handle_focus, NULL);
1209 PtAddCallback(gui.vimTextArea, Pt_CB_LOST_FOCUS,
1210 gui_ph_handle_focus, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
1212 /*
1213 * Now that the text area widget has been created, set up the colours,
1214 * which wil call PtSetResource from gui_mch_new_colors
1215 */
1216
1217 /*
1218 * Create the two timers, not as accurate as using the kernel timer
1219 * functions, but good enough
1220 */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001221 gui_ph_timer_cursor = PtCreateWidget(PtTimer, gui.vimWindow, 0, NULL);
1222 if (gui_ph_timer_cursor == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar15d63192011-09-14 16:05:15 +02001225 gui_ph_timer_timeout = PtCreateWidget(PtTimer, gui.vimWindow, 0, NULL);
1226 if (gui_ph_timer_timeout == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaar15d63192011-09-14 16:05:15 +02001229 PtAddCallback(gui_ph_timer_cursor, Pt_CB_TIMER_ACTIVATE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 gui_ph_handle_timer_cursor, NULL);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001231 PtAddCallback(gui_ph_timer_timeout, Pt_CB_TIMER_ACTIVATE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 gui_ph_handle_timer_timeout, NULL);
1233
1234#ifdef FEAT_MENU
1235 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001236 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, window_size.w, 0);
1237 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS, Pt_ANCHOR_LEFT_RIGHT,
1238 Pt_IS_ANCHORED);
1239 gui.vimToolBarGroup = PtCreateWidget(PtToolbarGroup, gui.vimWindow,
1240 n, args);
1241 if (gui.vimToolBarGroup == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
Bram Moolenaar15d63192011-09-14 16:05:15 +02001244 PtAddCallback(gui.vimToolBarGroup, Pt_CB_RESIZE,
1245 gui_ph_handle_menu_resize, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 n = 0;
1248 flags = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001249 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, window_size.w, 0);
1250 if (! vim_strchr(p_go, GO_MENUS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 flags |= Pt_DELAY_REALIZE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001253 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_TRUE, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001255 gui.vimMenuBar = PtCreateWidget(PtMenuBar, gui.vimToolBarGroup, n, args);
1256 if (gui.vimMenuBar == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
1259# ifdef FEAT_TOOLBAR
1260 n = 0;
1261
Bram Moolenaar15d63192011-09-14 16:05:15 +02001262 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS,
1263 Pt_ANCHOR_LEFT_RIGHT |Pt_TOP_ANCHORED_TOP, Pt_IS_ANCHORED);
1264 PtSetArg(&args[ n++ ], Pt_ARG_RESIZE_FLAGS, Pt_TRUE,
1265 Pt_RESIZE_Y_AS_REQUIRED);
1266 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, window_size.w, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
1268 flags = Pt_GETS_FOCUS;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001269 if (! vim_strchr(p_go, GO_TOOLBAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 flags |= Pt_DELAY_REALIZE;
1271
Bram Moolenaar15d63192011-09-14 16:05:15 +02001272 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_DELAY_REALIZE, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar15d63192011-09-14 16:05:15 +02001274 gui.vimToolBar = PtCreateWidget(PtToolbar, gui.vimToolBarGroup, n, args);
1275 if (gui.vimToolBar == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
1278 /*
1279 * Size for the toolbar is fetched in gui_mch_show_toolbar, after
1280 * the buttons have been added and the toolbar has resized it's height
1281 * for the buttons to fit
1282 */
1283# endif
1284
1285#endif
1286
Bram Moolenaard2221132011-07-27 14:09:09 +02001287 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288}
1289
1290 int
1291gui_mch_init_check(void)
1292{
Bram Moolenaard2221132011-07-27 14:09:09 +02001293 return (is_photon_available == TRUE) ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294}
1295
1296 int
1297gui_mch_open(void)
1298{
1299 gui.norm_pixel = Pg_BLACK;
1300 gui.back_pixel = Pg_WHITE;
1301
1302 set_normal_colors();
1303
1304 gui_check_colors();
1305 gui.def_norm_pixel = gui.norm_pixel;
1306 gui.def_back_pixel = gui.back_pixel;
1307
1308 highlight_gui_started();
1309
1310 if (gui_win_x != -1 && gui_win_y != -1)
1311 gui_mch_set_winpos(gui_win_x, gui_win_y);
1312
Bram Moolenaar15d63192011-09-14 16:05:15 +02001313 return (PtRealizeWidget(gui.vimWindow) == 0) ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314}
1315
1316 void
1317gui_mch_exit(int rc)
1318{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001319 PtDestroyWidget(gui.vimWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
Bram Moolenaar15d63192011-09-14 16:05:15 +02001321 PxTranslateSet(charset_translate, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
Bram Moolenaar15d63192011-09-14 16:05:15 +02001323 vim_free(gui.event_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
1325#ifdef USE_PANEL_GROUPS
Bram Moolenaar15d63192011-09-14 16:05:15 +02001326 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327#endif
1328}
1329
1330/****************************************************************************/
1331/* events */
1332
1333/* When no events are available, photon will call this function, working is
1334 * set to FALSE, and the gui_mch_update loop will exit. */
1335 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001336exit_gui_mch_update(void *data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 *(int *)data = FALSE;
Bram Moolenaard2221132011-07-27 14:09:09 +02001339 return Pt_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340}
1341
1342 void
1343gui_mch_update(void)
1344{
1345 int working = TRUE;
1346
Bram Moolenaar15d63192011-09-14 16:05:15 +02001347 PtAppAddWorkProc(NULL, exit_gui_mch_update, &working);
1348 while ((working == TRUE) && !vim_is_input_buf_full())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
1350 PtProcessEvent();
1351 }
1352}
1353
1354 int
1355gui_mch_wait_for_chars(int wtime)
1356{
1357 is_timeout = FALSE;
1358
Bram Moolenaar15d63192011-09-14 16:05:15 +02001359 if (wtime > 0)
1360 PtSetResource(gui_ph_timer_timeout, Pt_ARG_TIMER_INITIAL, wtime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaar15d63192011-09-14 16:05:15 +02001362 while (1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
1364 PtProcessEvent();
Bram Moolenaar15d63192011-09-14 16:05:15 +02001365 if (input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001367 PtSetResource(gui_ph_timer_timeout, Pt_ARG_TIMER_INITIAL, 0, 0);
Bram Moolenaard2221132011-07-27 14:09:09 +02001368 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001370 else if (is_timeout == TRUE)
Bram Moolenaard2221132011-07-27 14:09:09 +02001371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373}
1374
Bram Moolenaar15d63192011-09-14 16:05:15 +02001375#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376/*
1377 * Put up a file requester.
1378 * Returns the selected name in allocated memory, or NULL for Cancel.
1379 * saving, select file to write
1380 * title title for the window
1381 * default_name default name (well duh!)
1382 * ext not used (extension added)
1383 * initdir initial directory, NULL for current dir
1384 * filter not used (file name filter)
1385 */
1386 char_u *
1387gui_mch_browse(
1388 int saving,
1389 char_u *title,
1390 char_u *default_name,
1391 char_u *ext,
1392 char_u *initdir,
1393 char_u *filter)
1394{
1395 PtFileSelectionInfo_t file;
1396 int flags;
1397 char_u *default_path;
1398 char_u *open_text = NULL;
1399
1400 flags = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001401 memset(&file, 0, sizeof(file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
Bram Moolenaar15d63192011-09-14 16:05:15 +02001403 default_path = alloc(MAXPATHL + 1 + NAME_MAX + 1);
1404 if (default_path != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001406 if (saving == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
1408 /* Don't need Pt_FSR_CONFIRM_EXISTING, vim will ask anyway */
1409 flags |= Pt_FSR_NO_FCHECK;
1410 open_text = "&Save";
1411 }
1412
1413 /* combine the directory and filename into a single path */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001414 if (initdir == NULL || *initdir == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001416 mch_dirname(default_path, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 initdir = default_path;
1418 }
1419 else
1420 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001421 STRCPY(default_path, initdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 initdir = default_path;
1423 }
1424
Bram Moolenaar15d63192011-09-14 16:05:15 +02001425 if (default_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001427 if (default_path[ STRLEN(default_path) - 1 ] != '/')
1428 STRCAT(default_path, "/");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaar15d63192011-09-14 16:05:15 +02001430 STRCAT(default_path, default_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
1432
1433 /* TODO: add a filter? */
1434 PtFileSelection(
1435 gui.vimWindow,
1436 NULL,
1437 title,
1438 default_path,
1439 NULL,
1440 open_text,
1441 NULL,
1442 NULL,
1443 &file,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001444 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
Bram Moolenaar15d63192011-09-14 16:05:15 +02001446 vim_free(default_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
Bram Moolenaar15d63192011-09-14 16:05:15 +02001448 if (file.ret == Pt_FSDIALOG_BTN1)
Bram Moolenaard2221132011-07-27 14:09:09 +02001449 return vim_strsave(file.path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
Bram Moolenaard2221132011-07-27 14:09:09 +02001451 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452}
1453#endif
1454
Bram Moolenaar15d63192011-09-14 16:05:15 +02001455#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456static PtWidget_t *gui_ph_dialog_text = NULL;
1457
1458 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001459gui_ph_dialog_close(int button, void *data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 PtModalCtrl_t *modal_ctrl = data;
1462 char_u *dialog_text, *vim_text;
1463
Bram Moolenaar15d63192011-09-14 16:05:15 +02001464 if (gui_ph_dialog_text != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001466 PtGetResource(gui_ph_dialog_text, Pt_ARG_TEXT_STRING, &dialog_text, 0);
1467 PtGetResource(gui_ph_dialog_text, Pt_ARG_POINTER, &vim_text, 0);
1468 STRNCPY(vim_text, dialog_text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470
Bram Moolenaar15d63192011-09-14 16:05:15 +02001471 PtModalUnblock(modal_ctrl, (void *) button);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaard2221132011-07-27 14:09:09 +02001473 return Pt_TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474}
1475
1476 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001477gui_ph_dialog_text_enter(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001479 if (info->reason_subtype == Pt_EDIT_ACTIVATE)
1480 gui_ph_dialog_close(1, data);
Bram Moolenaard2221132011-07-27 14:09:09 +02001481 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482}
1483
1484 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001485gui_ph_dialog_esc(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 PhKeyEvent_t *key;
1488
Bram Moolenaar15d63192011-09-14 16:05:15 +02001489 key = PhGetData(info->event);
1490 if ((key->key_flags & Pk_KF_Cap_Valid) && (key->key_cap == Pk_Escape))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001492 gui_ph_dialog_close(0, data);
Bram Moolenaard2221132011-07-27 14:09:09 +02001493 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
Bram Moolenaard2221132011-07-27 14:09:09 +02001495 return Pt_PROCESS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496}
1497
1498 int
1499gui_mch_dialog(
1500 int type,
1501 char_u *title,
1502 char_u *message,
1503 char_u *buttons,
1504 int default_button,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01001505 char_u *textfield,
1506 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 char_u *str;
1509 char_u **button_array;
1510 char_u *buttons_copy;
1511
1512 int button_count;
1513 int i, len;
1514 int dialog_result = -1;
1515
1516 /* FIXME: the vertical option in guioptions is blatantly ignored */
1517 /* FIXME: so is the type */
1518
1519 button_count = len = i = 0;
1520
Bram Moolenaar15d63192011-09-14 16:05:15 +02001521 if (buttons == NULL || *buttons == NUL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001522 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 /* There is one less separator than buttons, so bump up the button count */
1525 button_count = 1;
1526
1527 /* Count string length and number of seperators */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001528 for (str = buttons; *str; str++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 len++;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001531 if (*str == DLG_BUTTON_SEP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 button_count++;
1533 }
1534
Bram Moolenaar15d63192011-09-14 16:05:15 +02001535 if (title == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 title = "Vim";
1537
Bram Moolenaar15d63192011-09-14 16:05:15 +02001538 buttons_copy = alloc(len + 1);
1539 button_array = (char_u **) alloc(button_count * sizeof(char_u *));
1540 if (buttons_copy != NULL && button_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001542 STRCPY(buttons_copy, buttons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 /*
1545 * Convert DLG_BUTTON_SEP into NUL's and fill in
1546 * button_array with the pointer to each NUL terminated string
1547 */
1548 str = buttons_copy;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001549 for (i = 0; i < button_count; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 button_array[ i ] = str;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001552 for (; *str; str++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001554 if (*str == DLG_BUTTON_SEP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 *str++ = NUL;
1557 break;
1558 }
1559 }
1560 }
1561#ifndef FEAT_GUI_TEXTDIALOG
1562 dialog_result = PtAlert(
1563 gui.vimWindow, NULL,
1564 title,
1565 NULL,
1566 message, NULL,
1567 button_count, (const char **) button_array, NULL,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001568 default_button, 0, Pt_MODAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#else
1570 /* Writing the dialog ourselves lets us add extra features, like
1571 * trapping the escape key and returning 0 to vim */
1572 {
1573 int n;
1574 PtArg_t args[5];
1575 PtWidget_t *dialog, *pane;
1576 PtModalCtrl_t modal_ctrl;
1577 PtDialogInfo_t di;
1578
Bram Moolenaar15d63192011-09-14 16:05:15 +02001579 memset(&di, 0, sizeof(di));
1580 memset(&modal_ctrl, 0, sizeof(modal_ctrl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001583 PtSetArg(&args[n++], Pt_ARG_GROUP_ROWS_COLS, 0, 0);
1584 PtSetArg(&args[n++], Pt_ARG_WIDTH, 350, 0);
1585 PtSetArg(&args[n++], Pt_ARG_GROUP_ORIENTATION,
1586 Pt_GROUP_VERTICAL, 0);
1587 PtSetArg(&args[n++], Pt_ARG_GROUP_FLAGS,
1588 Pt_TRUE, Pt_GROUP_NO_KEYS | Pt_GROUP_STRETCH_HORIZONTAL);
1589 PtSetArg(&args[n++], Pt_ARG_CONTAINER_FLAGS, Pt_FALSE, Pt_TRUE);
1590 pane = PtCreateWidget(PtGroup, NULL, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
1592 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001593 PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, message, 0);
1594 PtCreateWidget(PtLabel, pane, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar15d63192011-09-14 16:05:15 +02001596 if (textfield != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001599 PtSetArg(&args[n++], Pt_ARG_MAX_LENGTH, IOSIZE - 1, 0);
1600 PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, textfield, 0);
1601 PtSetArg(&args[n++], Pt_ARG_POINTER, textfield, 0);
1602 gui_ph_dialog_text = PtCreateWidget(PtText, pane, n, args);
1603 PtAddCallback(gui_ph_dialog_text, Pt_CB_ACTIVATE,
1604 gui_ph_dialog_text_enter, &modal_ctrl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606
1607 di.parent = gui.vimWindow;
1608 di.pane = pane;
1609 di.title = title;
1610 di.buttons = (const char **) button_array;
1611 di.nbtns = button_count;
1612 di.def_btn = default_button;
1613 /* This is just to give the dialog the close button.
1614 * We check for the Escape key ourselves and return 0 */
1615 di.esc_btn = button_count;
1616 di.callback = gui_ph_dialog_close;
1617 di.data = &modal_ctrl;
1618
Bram Moolenaar15d63192011-09-14 16:05:15 +02001619 dialog = PtCreateDialog(&di);
1620 PtAddFilterCallback(dialog, Ph_EV_KEY,
1621 gui_ph_dialog_esc, &modal_ctrl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaar15d63192011-09-14 16:05:15 +02001623 if (gui_ph_dialog_text != NULL)
1624 PtGiveFocus(gui_ph_dialog_text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 /* Open dialog, block the vim window and wait for the dialog to close */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001627 PtRealizeWidget(dialog);
1628 PtMakeModal(dialog, Ph_CURSOR_NOINPUT, Ph_CURSOR_DEFAULT_COLOR);
1629 dialog_result = (int) PtModalBlock(&modal_ctrl, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar15d63192011-09-14 16:05:15 +02001631 PtDestroyWidget(dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 gui_ph_dialog_text = NULL;
1633 }
1634#endif
1635 }
1636
Bram Moolenaar15d63192011-09-14 16:05:15 +02001637 vim_free(button_array);
1638 vim_free(buttons_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaard2221132011-07-27 14:09:09 +02001640 return dialog_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641}
1642#endif
1643/****************************************************************************/
1644/* window size/position/state */
1645
1646 int
1647gui_mch_get_winpos(int *x, int *y)
1648{
1649 PhPoint_t *pos;
1650
Bram Moolenaar15d63192011-09-14 16:05:15 +02001651 pos = PtWidgetPos(gui.vimWindow, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653 *x = pos->x;
1654 *y = pos->y;
1655
Bram Moolenaard2221132011-07-27 14:09:09 +02001656 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657}
1658
1659 void
1660gui_mch_set_winpos(int x, int y)
1661{
1662 PhPoint_t pos = { x, y };
1663
Bram Moolenaar15d63192011-09-14 16:05:15 +02001664 PtSetResource(gui.vimWindow, Pt_ARG_POS, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665}
1666
1667 void
1668gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001669 int min_width, int min_height, int base_width, int base_height,
1670 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 PhDim_t window_size = { width, height };
1673 PhDim_t min_size = { min_width, min_height };
1674
1675#ifdef USE_PANEL_GROUP
1676 window_size.w += pg_margin_left + pg_margin_right;
1677 window_size.h += pg_margin_top + pg_margin_bottom;
1678#endif
1679
Bram Moolenaar15d63192011-09-14 16:05:15 +02001680 PtSetResource(gui.vimWindow, Pt_ARG_MINIMUM_DIM, &min_size, 0);
1681 PtSetResource(gui.vimWindow, Pt_ARG_DIM, &window_size, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaar15d63192011-09-14 16:05:15 +02001683 if (! PtWidgetIsRealized(gui.vimWindow))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 gui_ph_resize_container();
1685}
1686
1687/*
1688 * Return the amount of screen space that hasn't been allocated (such as
1689 * by the shelf).
1690 */
1691 void
1692gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
1693{
1694 PhRect_t console;
1695
Bram Moolenaar15d63192011-09-14 16:05:15 +02001696 PhWindowQueryVisible(Ph_QUERY_WORKSPACE, 0,
1697 PhInputGroup(NULL), &console);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699 *screen_w = console.lr.x - console.ul.x + 1;
1700 *screen_h = console.lr.y - console.ul.y + 1;
1701}
1702
1703 void
1704gui_mch_iconify(void)
1705{
1706 PhWindowEvent_t event;
1707
Bram Moolenaar15d63192011-09-14 16:05:15 +02001708 memset(&event, 0, sizeof (event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 event.event_f = Ph_WM_HIDE;
1710 event.event_state = Ph_WM_EVSTATE_HIDE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001711 event.rid = PtWidgetRid(gui.vimWindow);
1712 PtForwardWindowEvent(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713}
1714
1715#if defined(FEAT_EVAL) || defined(PROTO)
1716/*
1717 * Bring the Vim window to the foreground.
1718 */
1719 void
1720gui_mch_set_foreground()
1721{
1722 PhWindowEvent_t event;
1723
Bram Moolenaar15d63192011-09-14 16:05:15 +02001724 memset(&event, 0, sizeof (event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 event.event_f = Ph_WM_TOFRONT;
1726 event.event_state = Ph_WM_EVSTATE_FFRONT;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001727 event.rid = PtWidgetRid(gui.vimWindow);
1728 PtForwardWindowEvent(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729}
1730#endif
1731
1732 void
1733gui_mch_settitle(char_u *title, char_u *icon)
1734{
1735#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02001736 gui_ph_pg_set_buffer_num(curwin->w_buffer->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02001738 PtSetResource(gui.vimWindow, Pt_ARG_WINDOW_TITLE, title, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /* Not sure what to do with the icon text, set balloon text somehow? */
1740}
1741
1742/****************************************************************************/
1743/* Scrollbar */
1744
1745 void
1746gui_mch_set_scrollbar_thumb(scrollbar_T *sb, int val, int size, int max)
1747{
1748 int n = 0;
1749 PtArg_t args[3];
1750
Bram Moolenaar15d63192011-09-14 16:05:15 +02001751 PtSetArg(&args[ n++ ], Pt_ARG_MAXIMUM, max, 0);
1752 PtSetArg(&args[ n++ ], Pt_ARG_SLIDER_SIZE, size, 0);
1753 PtSetArg(&args[ n++ ], Pt_ARG_GAUGE_VALUE, val, 0);
1754 PtSetResources(sb->id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757 void
1758gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1759{
1760 PhArea_t area = {{ x, y }, { w, h }};
1761
Bram Moolenaar15d63192011-09-14 16:05:15 +02001762 PtSetResource(sb->id, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763}
1764
1765 void
1766gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1767{
1768 int n = 0;
1769/* int anchor_flags = 0;*/
1770 PtArg_t args[4];
1771
1772 /*
1773 * Stop the scrollbar from being realized when the parent
1774 * is realized, so it can be explicitly realized by vim.
1775 *
1776 * Also, don't let the scrollbar get focus
1777 */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001778 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_DELAY_REALIZE,
1779 Pt_DELAY_REALIZE | Pt_GETS_FOCUS);
1780 PtSetArg(&args[ n++ ], Pt_ARG_SCROLLBAR_FLAGS, Pt_SCROLLBAR_SHOW_ARROWS, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#if 0
1782 /* Don't need this anchoring for the scrollbars */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001783 if (orient == SBAR_HORIZ)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785 anchor_flags = Pt_BOTTOM_ANCHORED_BOTTOM |
1786 Pt_LEFT_ANCHORED_LEFT | Pt_RIGHT_ANCHORED_RIGHT;
1787 }
1788 else
1789 {
1790 anchor_flags = Pt_BOTTOM_ANCHORED_BOTTOM | Pt_TOP_ANCHORED_TOP;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001791 if (sb->wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001793 if (sb == &sb->wp->w_scrollbars[ SBAR_LEFT ])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 anchor_flags |= Pt_LEFT_ANCHORED_LEFT;
1795 else
1796 anchor_flags |= Pt_RIGHT_ANCHORED_RIGHT;
1797 }
1798 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001799 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS, anchor_flags, Pt_IS_ANCHORED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02001801 PtSetArg(&args[ n++ ], Pt_ARG_ORIENTATION,
1802 (orient == SBAR_HORIZ) ? Pt_HORIZONTAL : Pt_VERTICAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02001804 sb->id = PtCreateWidget(PtScrollbar, gui.vimPanelGroup, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001806 sb->id = PtCreateWidget(PtScrollbar, gui.vimContainer, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
Bram Moolenaar15d63192011-09-14 16:05:15 +02001809 PtAddCallback(sb->id, Pt_CB_SCROLLBAR_MOVE, gui_ph_handle_scrollbar, sb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810}
1811
1812 void
1813gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
1814{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001815 if (flag != 0)
1816 PtRealizeWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001818 PtUnrealizeWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820
1821 void
1822gui_mch_destroy_scrollbar(scrollbar_T *sb)
1823{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001824 PtDestroyWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 sb->id = NULL;
1826}
1827
1828/****************************************************************************/
1829/* Mouse functions */
1830
1831#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
1832/* The last set mouse pointer shape is remembered, to be used when it goes
1833 * from hidden to not hidden. */
1834static int last_shape = 0;
1835
1836/* Table for shape IDs. Keep in sync with the mshape_names[] table in
1837 * misc2.c! */
1838static int mshape_ids[] =
1839{
1840 Ph_CURSOR_POINTER, /* arrow */
1841 Ph_CURSOR_NONE, /* blank */
1842 Ph_CURSOR_INSERT, /* beam */
1843 Ph_CURSOR_DRAG_VERTICAL, /* updown */
1844 Ph_CURSOR_DRAG_VERTICAL, /* udsizing */
1845 Ph_CURSOR_DRAG_HORIZONTAL, /* leftright */
1846 Ph_CURSOR_DRAG_HORIZONTAL, /* lrsizing */
1847 Ph_CURSOR_WAIT, /* busy */
1848 Ph_CURSOR_DONT, /* no */
1849 Ph_CURSOR_CROSSHAIR, /* crosshair */
1850 Ph_CURSOR_FINGER, /* hand1 */
1851 Ph_CURSOR_FINGER, /* hand2 */
1852 Ph_CURSOR_FINGER, /* pencil */
1853 Ph_CURSOR_QUESTION_POINT, /* question */
1854 Ph_CURSOR_POINTER, /* right-arrow */
1855 Ph_CURSOR_POINTER, /* up-arrow */
1856 Ph_CURSOR_POINTER /* last one */
1857};
1858
1859 void
1860mch_set_mouse_shape(shape)
1861 int shape;
1862{
1863 int id;
1864
1865 if (!gui.in_use)
1866 return;
1867
1868 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar15d63192011-09-14 16:05:15 +02001869 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE, Ph_CURSOR_NONE,
1870 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 else
1872 {
1873 if (shape >= MSHAPE_NUMBERED)
1874 id = Ph_CURSOR_POINTER;
1875 else
1876 id = mshape_ids[shape];
1877
Bram Moolenaar15d63192011-09-14 16:05:15 +02001878 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE, id, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880 if (shape != MSHAPE_HIDE)
1881 last_shape = shape;
1882}
1883#endif
1884
1885 void
1886gui_mch_mousehide(int hide)
1887{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001888 if (gui.pointer_hidden != hide)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 gui.pointer_hidden = hide;
1891#ifdef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001892 if (hide)
1893 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE,
1894 Ph_CURSOR_NONE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001896 mch_set_mouse_shape(last_shape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001898 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE,
1899 (hide == MOUSE_SHOW) ? GUI_PH_MOUSE_TYPE : Ph_CURSOR_NONE,
1900 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#endif
1902 }
1903}
1904
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001905 void
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001906gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907{
1908 PhCursorInfo_t info;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001909 short ix, iy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
1911 /* FIXME: does this return the correct position,
1912 * with respect to the border? */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001913 PhQueryCursor(PhInputGroup(NULL), &info);
1914 PtGetAbsPosition(gui.vimTextArea , &ix, &iy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001916 *x = info.pos.x - ix;
1917 *y = info.pos.y - iy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918}
1919
1920 void
1921gui_mch_setmouse(int x, int y)
1922{
1923 short abs_x, abs_y;
1924
Bram Moolenaar15d63192011-09-14 16:05:15 +02001925 PtGetAbsPosition(gui.vimTextArea, &abs_x, &abs_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 /* Add the border offset? */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001927 PhMoveCursorAbs(PhInputGroup(NULL), abs_x + x, abs_y + y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928}
1929
1930/****************************************************************************/
1931/* Colours */
1932
1933/*
1934 * Return the RGB value of a pixel as a long.
1935 */
1936 long_u
1937gui_mch_get_rgb(guicolor_T pixel)
1938{
1939 return PgRGB(PgRedValue(pixel), PgGreenValue(pixel), PgBlueValue(pixel));
1940}
1941
1942 void
1943gui_mch_new_colors(void)
1944{
1945#if 0 /* Don't bother changing the cursor colour */
1946 short color_diff;
1947
1948 /*
1949 * If there isn't enough difference between the background colour and
1950 * the mouse pointer colour then change the mouse pointer colour
1951 */
1952 color_diff = gui_get_lightness(gui_ph_mouse_color)
1953 - gui_get_lightness(gui.back_pixel);
1954
Bram Moolenaar15d63192011-09-14 16:05:15 +02001955 if (abs(color_diff) < 64)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 short r, g, b;
1958 /* not a great algorithm... */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001959 r = PgRedValue(gui_ph_mouse_color) ^ 255;
1960 g = PgGreenValue(gui_ph_mouse_color) ^ 255;
1961 b = PgBlueValue(gui_ph_mouse_color) ^ 255;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963#ifndef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001964 gui_ph_mouse_color = PgRGB(r, g, b);
1965 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_COLOR,
1966 gui_ph_mouse_color, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#endif
1968 }
1969#endif
1970
Bram Moolenaar15d63192011-09-14 16:05:15 +02001971 PtSetResource(gui.vimTextArea, Pt_ARG_FILL_COLOR, gui.back_pixel, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972}
1973
1974 static int
1975hex_digit(int c)
1976{
1977 if (VIM_ISDIGIT(c))
Bram Moolenaard2221132011-07-27 14:09:09 +02001978 return c - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 c = TOLOWER_ASC(c);
1980 if (c >= 'a' && c <= 'f')
Bram Moolenaard2221132011-07-27 14:09:09 +02001981 return c - 'a' + 10;
1982 return -1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983}
1984
1985
1986/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00001987 * This should be split out into a separate file,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * every port does basically the same thing.
1989 *
1990 * This is the gui_w32.c version (i think..)
1991 * Return INVALCOLOR when failed.
1992 */
1993
1994 guicolor_T
1995gui_mch_get_color(char_u *name)
1996{
1997 int i;
1998 int r, g, b;
1999
2000
2001 typedef struct GuiColourTable
2002 {
2003 char *name;
2004 guicolor_T colour;
2005 } GuiColourTable;
2006
2007 static GuiColourTable table[] =
2008 {
2009 {"Black", RGB(0x00, 0x00, 0x00)},
Bram Moolenaar092e3c82011-09-07 18:58:29 +02002010 {"DarkGray", RGB(0xA9, 0xA9, 0xA9)},
2011 {"DarkGrey", RGB(0xA9, 0xA9, 0xA9)},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {"Gray", RGB(0xC0, 0xC0, 0xC0)},
2013 {"Grey", RGB(0xC0, 0xC0, 0xC0)},
2014 {"LightGray", RGB(0xD3, 0xD3, 0xD3)},
2015 {"LightGrey", RGB(0xD3, 0xD3, 0xD3)},
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002016 {"Gray10", RGB(0x1A, 0x1A, 0x1A)},
2017 {"Grey10", RGB(0x1A, 0x1A, 0x1A)},
2018 {"Gray20", RGB(0x33, 0x33, 0x33)},
2019 {"Grey20", RGB(0x33, 0x33, 0x33)},
2020 {"Gray30", RGB(0x4D, 0x4D, 0x4D)},
2021 {"Grey30", RGB(0x4D, 0x4D, 0x4D)},
2022 {"Gray40", RGB(0x66, 0x66, 0x66)},
2023 {"Grey40", RGB(0x66, 0x66, 0x66)},
2024 {"Gray50", RGB(0x7F, 0x7F, 0x7F)},
2025 {"Grey50", RGB(0x7F, 0x7F, 0x7F)},
2026 {"Gray60", RGB(0x99, 0x99, 0x99)},
2027 {"Grey60", RGB(0x99, 0x99, 0x99)},
2028 {"Gray70", RGB(0xB3, 0xB3, 0xB3)},
2029 {"Grey70", RGB(0xB3, 0xB3, 0xB3)},
2030 {"Gray80", RGB(0xCC, 0xCC, 0xCC)},
2031 {"Grey80", RGB(0xCC, 0xCC, 0xCC)},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00002032 {"Gray90", RGB(0xE5, 0xE5, 0xE5)},
2033 {"Grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {"White", RGB(0xFF, 0xFF, 0xFF)},
2035 {"DarkRed", RGB(0x80, 0x00, 0x00)},
2036 {"Red", RGB(0xFF, 0x00, 0x00)},
2037 {"LightRed", RGB(0xFF, 0xA0, 0xA0)},
2038 {"DarkBlue", RGB(0x00, 0x00, 0x80)},
2039 {"Blue", RGB(0x00, 0x00, 0xFF)},
Bram Moolenaar092e3c82011-09-07 18:58:29 +02002040 {"LightBlue", RGB(0xAD, 0xD8, 0xE6)},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {"DarkGreen", RGB(0x00, 0x80, 0x00)},
2042 {"Green", RGB(0x00, 0xFF, 0x00)},
Bram Moolenaar092e3c82011-09-07 18:58:29 +02002043 {"LightGreen", RGB(0x90, 0xEE, 0x90)},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {"DarkCyan", RGB(0x00, 0x80, 0x80)},
2045 {"Cyan", RGB(0x00, 0xFF, 0xFF)},
Bram Moolenaar092e3c82011-09-07 18:58:29 +02002046 {"LightCyan", RGB(0xE0, 0xFF, 0xFF)},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {"DarkMagenta", RGB(0x80, 0x00, 0x80)},
2048 {"Magenta", RGB(0xFF, 0x00, 0xFF)},
2049 {"LightMagenta", RGB(0xFF, 0xA0, 0xFF)},
2050 {"Brown", RGB(0x80, 0x40, 0x40)},
2051 {"Yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaar092e3c82011-09-07 18:58:29 +02002052 {"LightYellow", RGB(0xFF, 0xFF, 0xE0)},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {"SeaGreen", RGB(0x2E, 0x8B, 0x57)},
2054 {"Orange", RGB(0xFF, 0xA5, 0x00)},
2055 {"Purple", RGB(0xA0, 0x20, 0xF0)},
2056 {"SlateBlue", RGB(0x6A, 0x5A, 0xCD)},
2057 {"Violet", RGB(0xEE, 0x82, 0xEE)},
2058 };
2059
2060 /* is name #rrggbb format? */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002061 if (name[0] == '#' && STRLEN(name) == 7)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002063 r = hex_digit(name[1]) * 16 + hex_digit(name[2]);
2064 g = hex_digit(name[3]) * 16 + hex_digit(name[4]);
2065 b = hex_digit(name[5]) * 16 + hex_digit(name[6]);
2066 if (r < 0 || g < 0 || b < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 return INVALCOLOR;
Bram Moolenaard2221132011-07-27 14:09:09 +02002068 return RGB(r, g, b);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070
Bram Moolenaar15d63192011-09-14 16:05:15 +02002071 for (i = 0; i < ARRAY_LENGTH(table); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002073 if (STRICMP(name, table[i].name) == 0)
Bram Moolenaard2221132011-07-27 14:09:09 +02002074 return table[i].colour;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076
2077 /*
2078 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
2079 */
2080 {
2081#define LINE_LEN 100
2082 FILE *fd;
2083 char line[LINE_LEN];
2084 char_u *fname;
2085
2086 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
2087 if (fname == NULL)
2088 return INVALCOLOR;
2089
2090 fd = fopen((char *)fname, "rt");
2091 vim_free(fname);
2092 if (fd == NULL)
2093 return INVALCOLOR;
2094
2095 while (!feof(fd))
2096 {
2097 int len;
2098 int pos;
2099 char *color;
2100
2101 fgets(line, LINE_LEN, fd);
2102 len = STRLEN(line);
2103
2104 if (len <= 1 || line[len-1] != '\n')
2105 continue;
2106
2107 line[len-1] = '\0';
2108
2109 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
2110 if (i != 3)
2111 continue;
2112
2113 color = line + pos;
2114
2115 if (STRICMP(color, name) == 0)
2116 {
2117 fclose(fd);
Bram Moolenaard2221132011-07-27 14:09:09 +02002118 return (guicolor_T)RGB(r, g, b);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120 }
2121
2122 fclose(fd);
2123 }
2124
2125
2126 return INVALCOLOR;
2127}
2128
2129 void
2130gui_mch_set_fg_color(guicolor_T color)
2131{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002132 PgSetTextColor(color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133}
2134
2135 void
2136gui_mch_set_bg_color(guicolor_T color)
2137{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002138 PgSetFillColor(color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139}
2140
2141 void
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002142gui_mch_set_sp_color(guicolor_T color)
2143{
2144}
2145
2146 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147gui_mch_invert_rectangle(int row, int col, int nr, int nc)
2148{
2149 PhRect_t rect;
2150
Bram Moolenaar15d63192011-09-14 16:05:15 +02002151 rect.ul.x = FILL_X(col);
2152 rect.ul.y = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /* FIXME: This has an off by one pixel problem */
2155 rect.lr.x = rect.ul.x + nc * gui.char_width;
2156 rect.lr.y = rect.ul.y + nr * gui.char_height;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002157 if (nc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 rect.lr.x -= 1;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002159 if (nr > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 rect.lr.y -= 1;
2161
2162 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002163 PgSetDrawMode(Pg_DrawModeDSTINVERT);
2164 PgDrawRect(&rect, Pg_DRAW_FILL);
2165 PgSetDrawMode(Pg_DrawModeSRCCOPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 DRAW_END;
2167}
2168
2169 void
2170gui_mch_clear_block(int row1, int col1, int row2, int col2)
2171{
2172 PhRect_t block = {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002173 { FILL_X(col1), FILL_Y(row1) },
2174 { FILL_X(col2 + 1) - 1, FILL_Y(row2 + 1) - 1}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 };
2176
2177 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002178 gui_mch_set_bg_color(gui.back_pixel);
2179 PgDrawRect(&block, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 DRAW_END;
2181}
2182
2183 void
2184gui_mch_clear_all()
2185{
2186 PhRect_t text_rect = {
2187 { gui.border_width, gui.border_width },
2188 { Columns * gui.char_width + gui.border_width - 1 ,
2189 Rows * gui.char_height + gui.border_width - 1 }
2190 };
2191
Bram Moolenaar15d63192011-09-14 16:05:15 +02002192 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return;
2194
2195 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002196 gui_mch_set_bg_color(gui.back_pixel);
2197 PgDrawRect(&text_rect, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 DRAW_END;
2199}
2200
2201 void
2202gui_mch_delete_lines(int row, int num_lines)
2203{
2204 PhRect_t rect;
2205 PhPoint_t delta;
2206
Bram Moolenaar15d63192011-09-14 16:05:15 +02002207 rect.ul.x = FILL_X(gui.scroll_region_left);
2208 rect.ul.y = FILL_Y(row + num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
Bram Moolenaar15d63192011-09-14 16:05:15 +02002210 rect.lr.x = FILL_X(gui.scroll_region_right + 1) - 1;
2211 rect.lr.y = FILL_Y(gui.scroll_region_bot + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
Bram Moolenaar15d63192011-09-14 16:05:15 +02002213 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
2214 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
2215 PhTranslateRect(&rect, &gui_ph_raw_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 delta.x = 0;
2218 delta.y = -num_lines * gui.char_height;
2219
2220 PgFlush();
2221
Bram Moolenaar15d63192011-09-14 16:05:15 +02002222 PhBlit(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)), &rect, &delta);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 gui_clear_block(
2225 gui.scroll_region_bot - num_lines + 1,
2226 gui.scroll_region_left,
2227 gui.scroll_region_bot,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002228 gui.scroll_region_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229}
2230
2231 void
2232gui_mch_insert_lines(int row, int num_lines)
2233{
2234 PhRect_t rect;
2235 PhPoint_t delta;
2236
Bram Moolenaar15d63192011-09-14 16:05:15 +02002237 rect.ul.x = FILL_X(gui.scroll_region_left);
2238 rect.ul.y = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar15d63192011-09-14 16:05:15 +02002240 rect.lr.x = FILL_X(gui.scroll_region_right + 1) - 1;
2241 rect.lr.y = FILL_Y(gui.scroll_region_bot - num_lines + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
Bram Moolenaar15d63192011-09-14 16:05:15 +02002243 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
2244 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
2245 PhTranslateRect(&rect, &gui_ph_raw_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247 delta.x = 0;
2248 delta.y = num_lines * gui.char_height;
2249
2250 PgFlush();
2251
Bram Moolenaar15d63192011-09-14 16:05:15 +02002252 PhBlit(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)) , &rect, &delta);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
Bram Moolenaar15d63192011-09-14 16:05:15 +02002254 gui_clear_block(row, gui.scroll_region_left,
2255 row + num_lines - 1, gui.scroll_region_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256}
2257
2258 void
2259gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
2260{
2261 static char *utf8_buffer = NULL;
2262 static int utf8_len = 0;
2263
Bram Moolenaar15d63192011-09-14 16:05:15 +02002264 PhPoint_t pos = { TEXT_X(col), TEXT_Y(row) };
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 PhRect_t rect;
2266
Bram Moolenaar15d63192011-09-14 16:05:15 +02002267 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 return;
2269
2270 DRAW_START;
2271
Bram Moolenaar15d63192011-09-14 16:05:15 +02002272 if (!(flags & DRAW_TRANSP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 PgDrawIRect(
Bram Moolenaar15d63192011-09-14 16:05:15 +02002275 FILL_X(col), FILL_Y(row),
2276 FILL_X(col + len) - 1, FILL_Y(row + 1) - 1,
2277 Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279
Bram Moolenaar15d63192011-09-14 16:05:15 +02002280 if (flags & DRAW_UNDERL)
2281 PgSetUnderline(gui.norm_pixel, Pg_TRANSPARENT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar15d63192011-09-14 16:05:15 +02002283 if (charset_translate != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#ifdef FEAT_MBYTE
2285 && enc_utf8 == 0
2286#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02002287 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 int src_taken, dst_made;
2290
2291 /* Use a static buffer to avoid large amounts of de/allocations */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002292 if (utf8_len < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002294 utf8_buffer = realloc(utf8_buffer, len * MB_LEN_MAX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 utf8_len = len;
2296 }
2297
2298 PxTranslateToUTF(
2299 charset_translate,
2300 s,
2301 len,
2302 &src_taken,
2303 utf8_buffer,
2304 utf8_len,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002305 &dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 s = utf8_buffer;
2307 len = dst_made;
2308 }
2309
Bram Moolenaar15d63192011-09-14 16:05:15 +02002310 PgDrawText(s, len, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaar15d63192011-09-14 16:05:15 +02002312 if (flags & DRAW_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 /* FIXME: try and only calculate these values once... */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002315 rect.ul.x = FILL_X(col) + 1;
2316 rect.ul.y = FILL_Y(row);
2317 rect.lr.x = FILL_X(col + len) - 1;
2318 rect.lr.y = FILL_Y(row + 1) - 1;
2319 /* PgSetUserClip(NULL) causes the scrollbar to not redraw... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#if 0
2321 pos.x++;
2322
Bram Moolenaar15d63192011-09-14 16:05:15 +02002323 PgSetUserClip(&rect);
2324 PgDrawText(s, len, &pos, 0);
2325 PgSetUserClip(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002327 rect.lr.y -= (p_linespace + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 /* XXX: DrawTextArea doesn't work with phditto */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002329 PgDrawTextArea(s, len, &rect, Pg_TEXT_BOTTOM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331 }
2332
Bram Moolenaar15d63192011-09-14 16:05:15 +02002333 if (flags & DRAW_UNDERL)
2334 PgSetUnderline(Pg_TRANSPARENT, Pg_TRANSPARENT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
2336 DRAW_END;
2337}
2338
2339/****************************************************************************/
2340/* Cursor */
2341
2342 void
2343gui_mch_draw_hollow_cursor(guicolor_T color)
2344{
2345 PhRect_t r;
2346
2347 /* FIXME: Double width characters */
2348
Bram Moolenaar15d63192011-09-14 16:05:15 +02002349 r.ul.x = FILL_X(gui.col);
2350 r.ul.y = FILL_Y(gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 r.lr.x = r.ul.x + gui.char_width - 1;
2352 r.lr.y = r.ul.y + gui.char_height - 1;
2353
2354 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002355 PgSetStrokeColor(color);
2356 PgDrawRect(&r, Pg_DRAW_STROKE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 DRAW_END;
2358}
2359
2360 void
2361gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
2362{
2363 PhRect_t r;
2364
Bram Moolenaar15d63192011-09-14 16:05:15 +02002365 r.ul.x = FILL_X(gui.col);
2366 r.ul.y = FILL_Y(gui.row) + gui.char_height - h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 r.lr.x = r.ul.x + w - 1;
2368 r.lr.y = r.ul.y + h - 1;
2369
2370 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002371 gui_mch_set_bg_color(color);
2372 PgDrawRect(&r, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 DRAW_END;
2374}
2375
2376 void
2377gui_mch_set_blinking(long wait, long on, long off)
2378{
2379 blink_waittime = wait;
2380 blink_ontime = on;
2381 blink_offtime = off;
2382}
2383
2384 void
2385gui_mch_start_blink(void)
2386{
2387 /* Only turn on the timer on if none of the times are zero */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002388 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002390 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
2391 blink_waittime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 blink_state = BLINK_ON;
2393 gui_update_cursor(TRUE, FALSE);
2394 }
2395}
2396
2397 void
2398gui_mch_stop_blink(void)
2399{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002400 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
Bram Moolenaar15d63192011-09-14 16:05:15 +02002402 if (blink_state == BLINK_OFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 gui_update_cursor(TRUE, FALSE);
2404
2405 blink_state = BLINK_NONE;
2406}
2407
2408/****************************************************************************/
2409/* miscellaneous functions */
2410
2411 void
2412gui_mch_beep(void)
2413{
2414 PtBeep();
2415}
2416
2417 void
2418gui_mch_flash(int msec)
2419{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002420 PgSetFillXORColor(Pg_BLACK, Pg_WHITE);
2421 PgSetDrawMode(Pg_DRAWMODE_XOR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 gui_mch_clear_all();
2423 gui_mch_flush();
2424
Bram Moolenaar15d63192011-09-14 16:05:15 +02002425 ui_delay((long) msec, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 gui_mch_clear_all();
Bram Moolenaar15d63192011-09-14 16:05:15 +02002428 PgSetDrawMode(Pg_DRAWMODE_OPAQUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 gui_mch_flush();
2430}
2431
2432 void
2433gui_mch_flush(void)
2434{
2435 PgFlush();
2436}
2437
2438 void
2439gui_mch_set_text_area_pos(int x, int y, int w, int h)
2440{
2441 PhArea_t area = {{x, y}, {w, h}};
2442
Bram Moolenaar15d63192011-09-14 16:05:15 +02002443 PtSetResource(gui.vimTextArea, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444}
2445
2446 int
2447gui_mch_haskey(char_u *name)
2448{
2449 int i;
2450
2451 for (i = 0; special_keys[i].key_sym != 0; i++)
2452 if (name[0] == special_keys[i].vim_code0 &&
2453 name[1] == special_keys[i].vim_code1)
Bram Moolenaard2221132011-07-27 14:09:09 +02002454 return OK;
2455 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456}
2457
2458/****************************************************************************/
2459/* Menu */
2460
2461#ifdef FEAT_TOOLBAR
2462#include "toolbar.phi"
2463
2464static PhImage_t *gui_ph_toolbar_images[] = {
2465 &tb_new_phi,
2466 &tb_open_phi,
2467 &tb_save_phi,
2468 &tb_undo_phi,
2469 &tb_redo_phi,
2470 &tb_cut_phi,
2471 &tb_copy_phi,
2472 &tb_paste_phi,
2473 &tb_print_phi,
2474 &tb_help_phi,
2475 &tb_find_phi,
2476 &tb_save_all_phi,
2477 &tb_save_session_phi,
2478 &tb_new_session_phi,
2479 &tb_load_session_phi,
2480 &tb_macro_phi,
2481 &tb_replace_phi,
2482 &tb_close_phi,
2483 &tb_maximize_phi,
2484 &tb_minimize_phi,
2485 &tb_split_phi,
2486 &tb_shell_phi,
2487 &tb_find_prev_phi,
2488 &tb_find_next_phi,
2489 &tb_find_help_phi,
2490 &tb_make_phi,
2491 &tb_jump_phi,
2492 &tb_ctags_phi,
2493 &tb_vsplit_phi,
2494 &tb_maxwidth_phi,
2495 &tb_minwidth_phi
2496};
2497
2498static PhImage_t *
Bram Moolenaar15d63192011-09-14 16:05:15 +02002499gui_ph_toolbar_load_icon(char_u *iconfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 static PhImage_t external_icon;
2502 PhImage_t *temp_phi = NULL;
2503
Bram Moolenaar15d63192011-09-14 16:05:15 +02002504 temp_phi = PxLoadImage(iconfile, NULL);
2505 if (temp_phi != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 /* The label widget will free the image/palette/etc. for us when
2508 * it's destroyed */
2509 temp_phi->flags |= Ph_RELEASE_IMAGE_ALL;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002510 memcpy(&external_icon, temp_phi, sizeof(external_icon));
2511 free(temp_phi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 temp_phi = &external_icon;
2514 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002515 return temp_phi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
2517
2518/*
2519 * This returns either a builtin icon image, an external image or NULL
2520 * if it can't find either. The caller can't and doesn't need to try and
2521 * free() the returned image, and it can't store the image pointer.
2522 * (When setting the Pt_ARG_LABEL_IMAGE resource, the contents of the
2523 * PhImage_t are copied, and the original PhImage_t aren't needed anymore).
2524 */
2525static PhImage_t *
Bram Moolenaar15d63192011-09-14 16:05:15 +02002526gui_ph_toolbar_find_icon(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528 char_u full_pathname[ MAXPATHL + 1 ];
2529 PhImage_t *icon = NULL;
2530
Bram Moolenaar15d63192011-09-14 16:05:15 +02002531 if (menu->icon_builtin == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002533 if (menu->iconfile != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 /* TODO: use gui_find_iconfile() */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002535 icon = gui_ph_toolbar_load_icon(menu->iconfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
2537 /* TODO: Restrict loading to just .png? Search for any format? */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002538 if ((icon == NULL) &&
2539 ((gui_find_bitmap(menu->name, full_pathname, "gif") == OK) ||
2540 (gui_find_bitmap(menu->name, full_pathname, "png") == OK)))
2541 icon = gui_ph_toolbar_load_icon(full_pathname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar15d63192011-09-14 16:05:15 +02002543 if (icon != NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002544 return icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546
Bram Moolenaar15d63192011-09-14 16:05:15 +02002547 if (menu->iconidx >= 0 &&
2548 (menu->iconidx < ARRAY_LENGTH(gui_ph_toolbar_images)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
Bram Moolenaard2221132011-07-27 14:09:09 +02002550 return gui_ph_toolbar_images[menu->iconidx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552
Bram Moolenaard2221132011-07-27 14:09:09 +02002553 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554}
2555#endif
2556
Bram Moolenaar15d63192011-09-14 16:05:15 +02002557#if defined(FEAT_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 void
2559gui_mch_enable_menu(int flag)
2560{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002561 if (flag != 0)
2562 PtRealizeWidget(gui.vimMenuBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002564 PtUnrealizeWidget(gui.vimMenuBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565}
2566
2567 void
2568gui_mch_set_menu_pos(int x, int y, int w, int h)
2569{
2570 /* Nothing */
2571}
2572
2573/* Change the position of a menu button in the parent */
2574 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +02002575gui_ph_position_menu(PtWidget_t *widget, int priority)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
2577 PtWidget_t *traverse;
2578 vimmenu_T *menu;
2579
Bram Moolenaar15d63192011-09-14 16:05:15 +02002580 traverse = PtWidgetChildBack(PtWidgetParent(widget));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 /* Iterate through the list of widgets in traverse, until
2583 * we find the position we want to insert our widget into */
2584 /* TODO: traverse from front to back, possible speedup? */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002585 while (traverse != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002587 PtGetResource(traverse, Pt_ARG_POINTER, &menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
Bram Moolenaar15d63192011-09-14 16:05:15 +02002589 if (menu != NULL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 priority < menu->priority &&
Bram Moolenaar15d63192011-09-14 16:05:15 +02002591 widget != traverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 /* Insert the widget before the current traverse widget */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002594 PtWidgetInsert(widget, traverse, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return;
2596 }
2597
Bram Moolenaar15d63192011-09-14 16:05:15 +02002598 traverse = PtWidgetBrotherInFront(traverse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600}
2601
2602/* the index is ignored because it's not useful for our purposes */
2603 void
2604gui_mch_add_menu(vimmenu_T *menu, int index)
2605{
2606 vimmenu_T *parent = menu->parent;
2607 char_u *accel_key;
2608 char_u mnemonic_str[MB_LEN_MAX];
2609 int n;
2610 PtArg_t args[5];
2611
2612 menu->submenu_id = menu->id = NULL;
2613
Bram Moolenaar15d63192011-09-14 16:05:15 +02002614 if (menu_is_menubar(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616
Bram Moolenaar15d63192011-09-14 16:05:15 +02002617 accel_key = vim_strchr(menu->name, '&');
2618 if (accel_key != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 mnemonic_str[0] = accel_key[1];
2621 mnemonic_str[1] = NUL;
2622 }
2623
2624 /* Create the menu button */
2625 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002626 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2627 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_TEXT, menu->actext, 0);
2628 if (accel_key != NULL)
2629 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_KEY, mnemonic_str, 0);
2630 PtSetArg(&args[ n++ ], Pt_ARG_POINTER, menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar15d63192011-09-14 16:05:15 +02002632 if (parent != NULL)
2633 PtSetArg(&args[ n++ ], Pt_ARG_BUTTON_TYPE, Pt_MENU_RIGHT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
Bram Moolenaar15d63192011-09-14 16:05:15 +02002635 menu->id = PtCreateWidget(PtMenuButton,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 (parent == NULL) ? gui.vimMenuBar : parent->submenu_id,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002637 n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaar15d63192011-09-14 16:05:15 +02002639 PtAddCallback(menu->id, Pt_CB_ARM, gui_ph_handle_pulldown_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 /* Create the actual menu */
2642 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002643 if (parent != NULL)
2644 PtSetArg(&args[ n++ ], Pt_ARG_MENU_FLAGS, Pt_TRUE, Pt_MENU_CHILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
Bram Moolenaar15d63192011-09-14 16:05:15 +02002646 menu->submenu_id = PtCreateWidget(PtMenu, menu->id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaar15d63192011-09-14 16:05:15 +02002648 if (parent == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002650 PtAddCallback(menu->submenu_id, Pt_CB_UNREALIZED,
2651 gui_ph_handle_menu_unrealized, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
Bram Moolenaar15d63192011-09-14 16:05:15 +02002653 if (menu->mnemonic != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002655 PtAddHotkeyHandler(gui.vimWindow, tolower(menu->mnemonic),
2656 Pk_KM_Alt, 0, menu, gui_ph_handle_pulldown_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 }
2659
Bram Moolenaar15d63192011-09-14 16:05:15 +02002660 gui_ph_position_menu(menu->id, menu->priority);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662 /* Redraw menubar here instead of gui_mch_draw_menubar */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002663 if (gui.menu_is_active)
2664 PtRealizeWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002666 else if (menu_is_popup(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002668 menu->submenu_id = PtCreateWidget(PtMenu, gui.vimWindow, 0, NULL);
2669 PtAddCallback(menu->submenu_id, Pt_CB_UNREALIZED,
2670 gui_ph_handle_menu_unrealized, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672}
2673
2674 void
2675gui_mch_add_menu_item(vimmenu_T *menu, int index)
2676{
2677 vimmenu_T *parent = menu->parent;
2678 char_u *accel_key;
2679 char_u mnemonic_str[MB_LEN_MAX];
2680 int n;
2681 PtArg_t args[13];
2682
2683 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002684 PtSetArg(&args[ n++ ], Pt_ARG_POINTER, menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686#ifdef FEAT_TOOLBAR
Bram Moolenaar15d63192011-09-14 16:05:15 +02002687 if (menu_is_toolbar(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002689 if (menu_is_separator(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002691 PtSetArg(&args[ n++ ], Pt_ARG_SEP_FLAGS,
2692 Pt_SEP_VERTICAL, Pt_SEP_ORIENTATION);
2693 PtSetArg(&args[ n++ ], Pt_ARG_SEP_TYPE, Pt_ETCHED_IN, 0);
2694 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS,
2695 Pt_TRUE, Pt_ANCHOR_TOP_BOTTOM);
2696 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, 2, 0);
2697 menu->id = PtCreateWidget(PtSeparator, gui.vimToolBar, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 else
2700 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002701 if (strstr((const char *) p_toolbar, "text") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002703 PtSetArg(&args[ n++ ], Pt_ARG_BALLOON_POSITION,
2704 Pt_BALLOON_BOTTOM, 0);
2705 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2706 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_FONT, "TextFont08", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002708 if ((strstr((const char *) p_toolbar, "icons") != NULL) &&
2709 (gui_ph_toolbar_images != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002711 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_IMAGE,
2712 gui_ph_toolbar_find_icon(menu), 0);
2713 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_TYPE, Pt_TEXT_IMAGE, 0);
2714 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_IMAGE_SPACING, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002716 if (strstr((const char *) p_toolbar, "tooltips") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002718 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_BALLOON,
2719 gui_ph_show_tooltip, 0);
2720 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_FLAGS,
2721 Pt_TRUE, Pt_SHOW_BALLOON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002723 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_HEIGHT, 1, 0);
2724 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_WIDTH, 1, 0);
2725 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_FALSE,
2726 Pt_HIGHLIGHTED | Pt_GETS_FOCUS);
2727 PtSetArg(&args[ n++ ], Pt_ARG_FILL_COLOR, Pg_TRANSPARENT, 0);
2728 menu->id = PtCreateWidget(PtButton, gui.vimToolBar, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar15d63192011-09-14 16:05:15 +02002730 PtAddCallback(menu->id, Pt_CB_ACTIVATE, gui_ph_handle_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732 /* Update toolbar if it's open */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002733 if (PtWidgetIsRealized(gui.vimToolBar))
2734 PtRealizeWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 else
2737#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02002738 if (menu_is_separator(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002740 menu->id = PtCreateWidget(PtSeparator, parent->submenu_id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 else
2743 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002744 accel_key = vim_strchr(menu->name, '&');
2745 if (accel_key != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 mnemonic_str[0] = accel_key[1];
2748 mnemonic_str[1] = NUL;
2749 }
2750
Bram Moolenaar15d63192011-09-14 16:05:15 +02002751 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2752 if (accel_key != NULL)
2753 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_KEY, mnemonic_str,
2754 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar15d63192011-09-14 16:05:15 +02002756 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_TEXT, menu->actext, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar15d63192011-09-14 16:05:15 +02002758 menu->id = PtCreateWidget(PtMenuButton, parent->submenu_id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar15d63192011-09-14 16:05:15 +02002760 PtAddCallback(menu->id, Pt_CB_ACTIVATE, gui_ph_handle_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02002763 if (gui_ph_is_buffer_item(menu, parent) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002765 PtAddCallback(menu->id, Pt_CB_DESTROYED,
2766 gui_ph_handle_buffer_remove, menu);
2767 gui_ph_pg_add_buffer(menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769#endif
2770 }
2771
Bram Moolenaar15d63192011-09-14 16:05:15 +02002772 gui_ph_position_menu(menu->id, menu->priority);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773}
2774
2775 void
2776gui_mch_destroy_menu(vimmenu_T *menu)
2777{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002778 if (menu->submenu_id != NULL)
2779 PtDestroyWidget(menu->submenu_id);
2780 if (menu->id != NULL)
2781 PtDestroyWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 menu->submenu_id = NULL;
2784 menu->id = NULL;
2785}
2786
2787 void
2788gui_mch_menu_grey(vimmenu_T *menu, int grey)
2789{
2790 long flags, mask, fields;
2791
Bram Moolenaar15d63192011-09-14 16:05:15 +02002792 if (menu->id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 return;
2794
Bram Moolenaar15d63192011-09-14 16:05:15 +02002795 flags = PtWidgetFlags(menu->id);
2796 if (PtWidgetIsClass(menu->id, PtMenuButton) &&
2797 PtWidgetIsClass(PtWidgetParent(menu->id), PtMenu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 fields = Pt_FALSE;
2800 mask = Pt_SELECTABLE | Pt_HIGHLIGHTED;
2801 }
2802 else
2803 {
2804 fields = Pt_TRUE;
2805 mask = Pt_BLOCKED | Pt_GHOST;
2806 }
2807
Bram Moolenaar15d63192011-09-14 16:05:15 +02002808 if (! grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 fields = ~fields;
2810
Bram Moolenaar15d63192011-09-14 16:05:15 +02002811 PtSetResource(menu->id, Pt_ARG_FLAGS, fields,
2812 mask);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813}
2814
2815 void
2816gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
2817{
2818 /* TODO: [un]realize the widget? */
2819}
2820
2821 void
2822gui_mch_draw_menubar(void)
2823{
2824 /* The only time a redraw is needed is when a menu button
2825 * is added to the menubar, and that is detected and the bar
2826 * redrawn in gui_mch_add_menu_item
2827 */
2828}
2829
2830 void
2831gui_mch_show_popupmenu(vimmenu_T *menu)
2832{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002833 PtSetResource(menu->submenu_id, Pt_ARG_POS, &abs_mouse, 0);
2834 PtRealizeWidget(menu->submenu_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835}
2836
2837 void
2838gui_mch_toggle_tearoffs(int enable)
2839{
2840 /* No tearoffs yet */
2841}
2842
2843#endif
2844
Bram Moolenaar15d63192011-09-14 16:05:15 +02002845#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 void
2847gui_mch_show_toolbar(int showit)
2848{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002849 if (showit)
2850 PtRealizeWidget(gui.vimToolBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002852 PtUnrealizeWidget(gui.vimToolBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853}
2854#endif
2855
2856/****************************************************************************/
2857/* Fonts */
2858
2859 static GuiFont
2860gui_ph_get_font(
2861 char_u *font_name,
2862 int_u font_flags,
2863 int_u font_size,
2864 /* Check whether the resulting font has the font flags and size that
2865 * was asked for */
2866 int_u enforce
2867 )
2868{
2869 char_u *font_tag;
2870 FontQueryInfo info;
2871 int_u style;
2872
Bram Moolenaar15d63192011-09-14 16:05:15 +02002873 font_tag = alloc(MAX_FONT_TAG);
2874 if (font_tag != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002876 if (PfGenerateFontName(font_name, font_flags, font_size,
2877 font_tag) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
2879 /* Enforce some limits on the font used */
2880 style = PHFONT_INFO_FIXED;
2881
Bram Moolenaar15d63192011-09-14 16:05:15 +02002882 if (enforce & PF_STYLE_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 style |= PHFONT_INFO_BOLD;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002884 if (enforce & PF_STYLE_ANTIALIAS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 style |= PHFONT_INFO_ALIAS;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002886 if (enforce & PF_STYLE_ITALIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 style |= PHFONT_INFO_ITALIC;
2888
Bram Moolenaar15d63192011-09-14 16:05:15 +02002889 PfQueryFontInfo(font_tag, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
Bram Moolenaar15d63192011-09-14 16:05:15 +02002891 if (info.size == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 font_size = 0;
2893
2894 /* Make sure font size matches, and that the font style
2895 * at least has the bits we're checking for */
Bram Moolenaar15d63192011-09-14 16:05:15 +02002896 if (font_size == info.size &&
2897 style == (info.style & style))
Bram Moolenaard2221132011-07-27 14:09:09 +02002898 return (GuiFont)font_tag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002900 vim_free(font_tag);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002902 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903}
2904
2905/*
2906 * Split up the vim font name
2907 *
2908 * vim_font is in the form of
2909 * <name>:s<height>:a:b:i
2910 *
2911 * a = antialias
2912 * b = bold
2913 * i = italic
2914 *
2915 */
2916
2917 static int
2918gui_ph_parse_font_name(
2919 char_u *vim_font,
2920 char_u **font_name,
2921 int_u *font_flags,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002922 int_u *font_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 char_u *mark;
2925 int_u name_len, size;
2926
Bram Moolenaar15d63192011-09-14 16:05:15 +02002927 mark = vim_strchr(vim_font, ':');
2928 if (mark == NULL)
2929 name_len = STRLEN(vim_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002931 name_len = (int_u) (mark - vim_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaar15d63192011-09-14 16:05:15 +02002933 *font_name = vim_strnsave(vim_font, name_len);
Bram Moolenaard0988c52011-08-10 12:19:04 +02002934 if (*font_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002936 if (mark != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002938 while (*mark != NUL && *mark++ == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002940 switch (tolower(*mark++))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
2942 case 'a': *font_flags |= PF_STYLE_ANTIALIAS; break;
2943 case 'b': *font_flags |= PF_STYLE_BOLD; break;
2944 case 'i': *font_flags |= PF_STYLE_ITALIC; break;
2945
2946 case 's':
Bram Moolenaar15d63192011-09-14 16:05:15 +02002947 size = getdigits(&mark);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 /* Restrict the size to some vague limits */
Bram Moolenaard0988c52011-08-10 12:19:04 +02002949 if (size < 1 || size > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 size = 8;
2951
2952 *font_size = size;
2953 break;
2954
2955 default:
2956 break;
2957 }
2958 }
2959 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002960 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002962 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963}
2964
2965 int
2966gui_mch_init_font(char_u *vim_font_name, int fontset)
2967{
2968 char_u *font_tag;
2969 char_u *font_name = NULL;
2970 int_u font_flags = 0;
2971 int_u font_size = 12;
2972
2973 FontQueryInfo info;
2974 PhRect_t extent;
2975
Bram Moolenaard0988c52011-08-10 12:19:04 +02002976 if (vim_font_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
2978 /* Default font */
Bram Moolenaara0b19972009-07-01 14:13:18 +00002979 vim_font_name = "PC Terminal";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981
Bram Moolenaar15d63192011-09-14 16:05:15 +02002982 if (STRCMP(vim_font_name, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002984 font_tag = PtFontSelection(gui.vimWindow, NULL, NULL,
2985 "pcterm12", -1, PHFONT_FIXED, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaard0988c52011-08-10 12:19:04 +02002987 if (font_tag == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002988 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
Bram Moolenaar15d63192011-09-14 16:05:15 +02002990 gui_mch_free_font(gui.norm_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 gui.norm_font = font_tag;
2992
Bram Moolenaar15d63192011-09-14 16:05:15 +02002993 PfQueryFontInfo(font_tag, &info);
2994 font_name = vim_strsave(info.font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 else
2997 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002998 if (gui_ph_parse_font_name(vim_font_name, &font_name, &font_flags,
2999 &font_size) == FALSE)
Bram Moolenaard2221132011-07-27 14:09:09 +02003000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
Bram Moolenaar15d63192011-09-14 16:05:15 +02003002 font_tag = gui_ph_get_font(font_name, font_flags, font_size, 0);
Bram Moolenaard0988c52011-08-10 12:19:04 +02003003 if (font_tag == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02003005 vim_free(font_name);
Bram Moolenaard2221132011-07-27 14:09:09 +02003006 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003008
Bram Moolenaar15d63192011-09-14 16:05:15 +02003009 gui_mch_free_font(gui.norm_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 gui.norm_font = font_tag;
3011 }
3012
Bram Moolenaar15d63192011-09-14 16:05:15 +02003013 gui_mch_free_font(gui.bold_font);
3014 gui.bold_font = gui_ph_get_font(font_name, font_flags | PF_STYLE_BOLD,
3015 font_size, PF_STYLE_BOLD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
Bram Moolenaar15d63192011-09-14 16:05:15 +02003017 gui_mch_free_font(gui.ital_font);
3018 gui.ital_font = gui_ph_get_font(font_name, font_flags | PF_STYLE_ITALIC,
3019 font_size, PF_STYLE_ITALIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 /* This extent was brought to you by the letter 'g' */
Bram Moolenaar15d63192011-09-14 16:05:15 +02003022 PfExtentText(&extent, NULL, font_tag, "g", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024 gui.char_width = extent.lr.x - extent.ul.x + 1;
3025 gui.char_height = (- extent.ul.y) + extent.lr.y + 1;
3026 gui.char_ascent = - extent.ul.y;
3027
Bram Moolenaar15d63192011-09-14 16:05:15 +02003028 vim_free(font_name);
Bram Moolenaard2221132011-07-27 14:09:09 +02003029 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030}
3031
Bram Moolenaar02743632005-07-25 20:42:36 +00003032/*
3033 * Adjust gui.char_height (after 'linespace' was changed).
3034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 int
Bram Moolenaar02743632005-07-25 20:42:36 +00003036gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 FontQueryInfo info;
3039
Bram Moolenaar15d63192011-09-14 16:05:15 +02003040 PfQueryFontInfo(gui.norm_font, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042 gui.char_height = - info.ascender + info.descender + p_linespace;
3043 gui.char_ascent = - info.ascender + p_linespace / 2;
3044
Bram Moolenaard2221132011-07-27 14:09:09 +02003045 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046}
3047
3048 GuiFont
3049gui_mch_get_font(char_u *vim_font_name, int report_error)
3050{
3051 char_u *font_name;
3052 char_u *font_tag;
3053 int_u font_size = 12;
3054 int_u font_flags = 0;
3055
Bram Moolenaar15d63192011-09-14 16:05:15 +02003056 if (gui_ph_parse_font_name(vim_font_name, &font_name, &font_flags,
3057 &font_size) != FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02003059 font_tag = gui_ph_get_font(font_name, font_flags, font_size, -1);
3060 vim_free(font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
Bram Moolenaard0988c52011-08-10 12:19:04 +02003062 if (font_tag != NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02003063 return (GuiFont)font_tag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 }
3065
Bram Moolenaard0988c52011-08-10 12:19:04 +02003066 if (report_error)
Bram Moolenaar15d63192011-09-14 16:05:15 +02003067 EMSG2(e_font, vim_font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
Bram Moolenaard2221132011-07-27 14:09:09 +02003069 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070}
3071
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003072#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003073/*
3074 * Return the name of font "font" in allocated memory.
3075 * Don't know how to get the actual name, thus use the provided name.
3076 */
3077 char_u *
3078gui_mch_get_fontname(font, name)
3079 GuiFont font;
3080 char_u *name;
3081{
3082 if (name == NULL)
3083 return NULL;
3084 return vim_strsave(name);
3085}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00003086#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003087
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 void
3089gui_mch_set_font(GuiFont font)
3090{
Bram Moolenaar15d63192011-09-14 16:05:15 +02003091 PgSetFont(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092}
3093
3094 void
3095gui_mch_free_font(GuiFont font)
3096{
Bram Moolenaar15d63192011-09-14 16:05:15 +02003097 vim_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
3099