blob: d48a1538fa219724c5455116081c54b8606ec91f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * 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
Bram Moolenaar734a8672019-12-02 22:49:38 +010016// cproto fails on missing include files
Bram Moolenaar82881492012-11-20 16:53:39 +010017#ifndef PROTO
18# ifdef FEAT_TOOLBAR
19# include <photon/PxImage.h>
20# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#endif
22
23#if !defined(__QNX__)
Bram Moolenaar734a8672019-12-02 22:49:38 +010024// Used when generating prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025# define PgColor_t int
26# define PhEvent_t int
27# define PhPoint_t int
28# define PtWidget_t int
29# define Pg_BLACK 0
30# define PtCallbackF_t int
31# define PtCallbackInfo_t int
32# define PhTile_t int
33# define PtWidget_t int
34# define PhImage_t int
35#endif
36
Bram Moolenaard2221132011-07-27 14:09:09 +020037#define RGB(r, g, b) PgRGB(r, g, b)
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaard2221132011-07-27 14:09:09 +020039#define EVENT_BUFFER_SIZE sizeof(PhEvent_t) + 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar734a8672019-12-02 22:49:38 +010041// Some defines for gui_mch_mousehide()
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#define MOUSE_HIDE TRUE
43#define MOUSE_SHOW FALSE
44
Bram Moolenaar734a8672019-12-02 22:49:38 +010045// Optional support for using a PtPanelGroup widget, needs work
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#undef USE_PANEL_GROUP
47
48#ifdef USE_PANEL_GROUP
49static char *empty_title = " ";
50static char **panel_titles = NULL;
51static ushort_t num_panels = 0;
52static short pg_margin_left, pg_margin_right, pg_margin_top, pg_margin_bottom;
53#endif
54
Bram Moolenaar734a8672019-12-02 22:49:38 +010055#define GUI_PH_MARGIN 4 // Size of the bevel
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57#define GUI_PH_MOUSE_TYPE Ph_CURSOR_INSERT
58static PgColor_t gui_ph_mouse_color = Pg_BLACK;
59
60static PhPoint_t gui_ph_raw_offset;
Bram Moolenaar734a8672019-12-02 22:49:38 +010061static PtWidget_t *gui_ph_timer_cursor; // handle cursor blinking
62static PtWidget_t *gui_ph_timer_timeout; // used in gui_mch_wait_for_chars
63static short is_timeout; // Has the timeout occurred?
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
65/*
66 * This is set inside the mouse callback for a right mouse
67 * button click, and used for the popup menus
68 */
69static PhPoint_t abs_mouse;
70
Bram Moolenaar734a8672019-12-02 22:49:38 +010071// Try and avoid redraws while a resize is in progress
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static int is_ignore_draw = FALSE;
73
Bram Moolenaar734a8672019-12-02 22:49:38 +010074// Used for converting to/from utf-8 and other charsets
Bram Moolenaar071d4272004-06-13 20:20:40 +000075static struct PxTransCtrl *charset_translate;
76
77/*
78 * Cursor blink functions.
79 *
80 * This is a simple state machine:
81 * BLINK_NONE not blinking at all
82 * BLINK_OFF blinking, cursor is not shown
83 * BLINK_ON blinking, cursor is shown
84 */
85static enum {
86 BLINK_NONE,
87 BLINK_OFF,
88 BLINK_ON
89} blink_state = BLINK_NONE;
90
91static long_u blink_waittime = 700;
92static long_u blink_ontime = 400;
93static long_u blink_offtime = 250;
94
95static struct
96{
97 int key_sym;
98 char_u vim_code0;
99 char_u vim_code1;
100} special_keys[] =
101{
102 {Pk_Up, 'k', 'u'},
103 {Pk_Down, 'k', 'd'},
104 {Pk_Left, 'k', 'l'},
105 {Pk_Right, 'k', 'r'},
106
107 {Pk_F1, 'k', '1'},
108 {Pk_F2, 'k', '2'},
109 {Pk_F3, 'k', '3'},
110 {Pk_F4, 'k', '4'},
111 {Pk_F5, 'k', '5'},
112 {Pk_F6, 'k', '6'},
113 {Pk_F7, 'k', '7'},
114 {Pk_F8, 'k', '8'},
115 {Pk_F9, 'k', '9'},
116 {Pk_F10, 'k', ';'},
117
118 {Pk_F11, 'F', '1'},
119 {Pk_F12, 'F', '2'},
120 {Pk_F13, 'F', '3'},
121 {Pk_F14, 'F', '4'},
122 {Pk_F15, 'F', '5'},
123 {Pk_F16, 'F', '6'},
124 {Pk_F17, 'F', '7'},
125 {Pk_F18, 'F', '8'},
126 {Pk_F19, 'F', '9'},
127 {Pk_F20, 'F', 'A'},
128
129 {Pk_F21, 'F', 'B'},
130 {Pk_F22, 'F', 'C'},
131 {Pk_F23, 'F', 'D'},
132 {Pk_F24, 'F', 'E'},
133 {Pk_F25, 'F', 'F'},
134 {Pk_F26, 'F', 'G'},
135 {Pk_F27, 'F', 'H'},
136 {Pk_F28, 'F', 'I'},
137 {Pk_F29, 'F', 'J'},
138
139 {Pk_F30, 'F', 'K'},
140 {Pk_F31, 'F', 'L'},
141 {Pk_F32, 'F', 'M'},
142 {Pk_F33, 'F', 'N'},
143 {Pk_F34, 'F', 'O'},
144 {Pk_F35, 'F', 'P'},
145
146 {Pk_Help, '%', '1'},
147 {Pk_BackSpace, 'k', 'b'},
148 {Pk_Insert, 'k', 'I'},
149 {Pk_Delete, 'k', 'D'},
150 {Pk_Home, 'k', 'h'},
151 {Pk_End, '@', '7'},
152 {Pk_Prior, 'k', 'P'},
153 {Pk_Next, 'k', 'N'},
154 {Pk_Print, '%', '9'},
155
156 {Pk_KP_Add, 'K', '6'},
157 {Pk_KP_Subtract,'K', '7'},
158 {Pk_KP_Divide, 'K', '8'},
159 {Pk_KP_Multiply,'K', '9'},
160 {Pk_KP_Enter, 'K', 'A'},
161
Bram Moolenaar734a8672019-12-02 22:49:38 +0100162 {Pk_KP_0, KS_EXTRA, KE_KINS}, // Insert
163 {Pk_KP_Decimal, KS_EXTRA, KE_KDEL}, // Delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaar734a8672019-12-02 22:49:38 +0100165 {Pk_KP_4, 'k', 'l'}, // Left
166 {Pk_KP_6, 'k', 'r'}, // Right
167 {Pk_KP_8, 'k', 'u'}, // Up
168 {Pk_KP_2, 'k', 'd'}, // Down
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
Bram Moolenaar734a8672019-12-02 22:49:38 +0100170 {Pk_KP_7, 'K', '1'}, // Home
171 {Pk_KP_1, 'K', '4'}, // End
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar734a8672019-12-02 22:49:38 +0100173 {Pk_KP_9, 'K', '3'}, // Page Up
174 {Pk_KP_3, 'K', '5'}, // Page Down
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
Bram Moolenaar734a8672019-12-02 22:49:38 +0100176 {Pk_KP_5, '&', '8'}, // Undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Bram Moolenaar734a8672019-12-02 22:49:38 +0100178 // Keys that we want to be able to use any modifier with:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 {Pk_Return, CAR, NUL},
180 {Pk_space, ' ', NUL},
181 {Pk_Tab, TAB, NUL},
182 {Pk_Escape, ESC, NUL},
183 {NL, NL, NUL},
184 {CAR, CAR, NUL},
185
Bram Moolenaar734a8672019-12-02 22:49:38 +0100186 // End of list marker:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {0, 0, 0}
188};
189
190
Bram Moolenaar734a8672019-12-02 22:49:38 +0100191////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193static PtCallbackF_t gui_ph_handle_timer_cursor;
194static PtCallbackF_t gui_ph_handle_timer_timeout;
195
196static PtCallbackF_t gui_ph_handle_window_cb;
197
198static PtCallbackF_t gui_ph_handle_scrollbar;
199static PtCallbackF_t gui_ph_handle_keyboard;
200static PtCallbackF_t gui_ph_handle_mouse;
201static PtCallbackF_t gui_ph_handle_pulldown_menu;
202static PtCallbackF_t gui_ph_handle_menu;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100203static PtCallbackF_t gui_ph_handle_focus; // focus change of text area
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205static PtCallbackF_t gui_ph_handle_menu_resize;
206
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207// When a menu is unrealized, give focus back to vimTextArea
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static PtCallbackF_t gui_ph_handle_menu_unrealized;
209
210#ifdef USE_PANEL_GROUP
Bram Moolenaard2221132011-07-27 14:09:09 +0200211static void gui_ph_get_panelgroup_margins(short*, short*, short*, short*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#endif
213
Bram Moolenaard2221132011-07-27 14:09:09 +0200214static void gui_ph_draw_start(void);
215static void gui_ph_draw_end(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar734a8672019-12-02 22:49:38 +0100217// Set the text for the balloon
Bram Moolenaard2221132011-07-27 14:09:09 +0200218static PtWidget_t * gui_ph_show_tooltip(PtWidget_t *window,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 PtWidget_t *widget,
220 int position,
221 char *text,
222 char *font,
223 PgColor_t fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200224 PgColor_t text_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar734a8672019-12-02 22:49:38 +0100226////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaard2221132011-07-27 14:09:09 +0200228static PtWidget_t * gui_ph_show_tooltip(PtWidget_t *window,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 PtWidget_t *widget,
230 int position,
231 char *text,
232 char *font,
233 PgColor_t fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200234 PgColor_t text_color)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 PtArg_t arg;
237 vimmenu_T *menu;
238 char_u *tooltip;
239
Bram Moolenaard2221132011-07-27 14:09:09 +0200240 PtSetArg(&arg, Pt_ARG_POINTER, &menu, 0);
241 PtGetResources(widget, 1, &arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242
Bram Moolenaar734a8672019-12-02 22:49:38 +0100243 // Override the text and position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 tooltip = text;
Bram Moolenaard2221132011-07-27 14:09:09 +0200246 if (menu != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {
248 int index = MENU_INDEX_TIP;
Bram Moolenaard2221132011-07-27 14:09:09 +0200249 if (menu->strings[ index ] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 tooltip = menu->strings[ index ];
251 }
252
Bram Moolenaard2221132011-07-27 14:09:09 +0200253 return PtInflateBalloon(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 window,
255 widget,
Bram Moolenaar734a8672019-12-02 22:49:38 +0100256 // Don't put the balloon at the bottom,
257 // it gets drawn over by gfx done in the PtRaw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 Pt_BALLOON_TOP,
259 tooltip,
260 font,
261 fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200262 text_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263}
264
265 static void
Bram Moolenaard2221132011-07-27 14:09:09 +0200266gui_ph_resize_container(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267{
268 PhArea_t area;
269
Bram Moolenaard2221132011-07-27 14:09:09 +0200270 PtWidgetArea(gui.vimWindow, &area);
271 PtWidgetPos (gui.vimContainer, &area.pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
Bram Moolenaard2221132011-07-27 14:09:09 +0200273 PtSetResource(gui.vimContainer, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274}
275
276 static int
277gui_ph_handle_menu_resize(
278 PtWidget_t *widget,
279 void *other,
Bram Moolenaard2221132011-07-27 14:09:09 +0200280 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281{
282 PtContainerCallback_t *sizes = info->cbdata;
283 PtWidget_t *container;
284 PhPoint_t below_menu;
285 int_u height;
286
287 height = sizes->new_dim.h;
288
Bram Moolenaar734a8672019-12-02 22:49:38 +0100289 // Because vim treats the toolbar and menubar separately,
290 // and here they're lumped together into a PtToolbarGroup,
291 // we only need either menu_height or toolbar_height set at once
Bram Moolenaard2221132011-07-27 14:09:09 +0200292 if (gui.menu_is_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 {
294 gui.menu_height = height;
295 gui.toolbar_height = 0;
296 }
297#ifdef FEAT_TOOLBAR
298 else
299 gui.toolbar_height = height;
300#endif
301
302 below_menu.x = 0;
303 below_menu.y = height;
304
305#ifdef USE_PANEL_GROUP
306 container = gui.vimPanelGroup;
307#else
308 container = gui.vimContainer;
309#endif
310
Bram Moolenaard2221132011-07-27 14:09:09 +0200311 PtSetResource(container, Pt_ARG_POS, &below_menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313 gui_ph_resize_container();
314
315#ifdef USE_PANEL_GROUP
316 gui_ph_get_panelgroup_margins(
317 &pg_margin_top, &pg_margin_bottom,
Bram Moolenaard2221132011-07-27 14:09:09 +0200318 &pg_margin_left, &pg_margin_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#endif
Bram Moolenaard2221132011-07-27 14:09:09 +0200320 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321}
322
323/*
324 * Pt_ARG_TIMER_REPEAT isn't used because the on & off times
325 * are different
326 */
327 static int
328gui_ph_handle_timer_cursor(
329 PtWidget_t *widget,
330 void *data,
Bram Moolenaard2221132011-07-27 14:09:09 +0200331 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332{
Bram Moolenaard2221132011-07-27 14:09:09 +0200333 if (blink_state == BLINK_ON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 {
335 gui_undraw_cursor();
336 blink_state = BLINK_OFF;
Bram Moolenaard2221132011-07-27 14:09:09 +0200337 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
338 blink_offtime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 }
340 else
341 {
342 gui_update_cursor(TRUE, FALSE);
343 blink_state = BLINK_ON;
Bram Moolenaard2221132011-07-27 14:09:09 +0200344 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
345 blink_ontime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200347 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348}
349
350 static int
351gui_ph_handle_timer_timeout(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
352{
353 is_timeout = TRUE;
354
Bram Moolenaard2221132011-07-27 14:09:09 +0200355 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356}
357
358 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200359gui_ph_handle_window_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
361 PhWindowEvent_t *we = info->cbdata;
362 ushort_t *width, *height;
363
Bram Moolenaard0988c52011-08-10 12:19:04 +0200364 switch (we->event_f) {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 case Ph_WM_CLOSE:
366 gui_shell_closed();
367 break;
368
369 case Ph_WM_FOCUS:
Bram Moolenaar734a8672019-12-02 22:49:38 +0100370 // Just in case it's hidden and needs to be shown
Bram Moolenaard0988c52011-08-10 12:19:04 +0200371 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaard0988c52011-08-10 12:19:04 +0200373 if (we->event_state == Ph_WM_EVSTATE_FOCUS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
375 gui_focus_change(TRUE);
376 gui_mch_start_blink();
377 }
378 else
379 {
380 gui_focus_change(FALSE);
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100381 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 }
383 break;
384
385 case Ph_WM_RESIZE:
Bram Moolenaard0988c52011-08-10 12:19:04 +0200386 PtGetResource(gui.vimWindow, Pt_ARG_WIDTH, &width, 0);
387 PtGetResource(gui.vimWindow, Pt_ARG_HEIGHT, &height, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#ifdef USE_PANEL_GROUP
389 width -= (pg_margin_left + pg_margin_right);
390 height -= (pg_margin_top + pg_margin_bottom);
391#endif
Bram Moolenaard0988c52011-08-10 12:19:04 +0200392 gui_resize_shell(*width, *height);
393 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 is_ignore_draw = FALSE;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200395 PtEndFlux(gui.vimContainer);
396 PtContainerRelease(gui.vimContainer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 break;
398
399 default:
400 break;
401 }
402
Bram Moolenaard2221132011-07-27 14:09:09 +0200403 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404}
405
406 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200407gui_ph_handle_scrollbar(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
409 PtScrollbarCallback_t *scroll;
410 scrollbar_T *sb;
411 int value, dragging = FALSE;
412
413 scroll = info->cbdata;
414
415 sb = (scrollbar_T *) data;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200416 if (sb != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {
418 value = scroll->position;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200419 switch (scroll->action)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
421 case Pt_SCROLL_DRAGGED:
422 dragging = TRUE;
423 break;
424
425 case Pt_SCROLL_SET:
Bram Moolenaar734a8672019-12-02 22:49:38 +0100426 // FIXME: return straight away here?
Bram Moolenaard2221132011-07-27 14:09:09 +0200427 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 break;
429 }
430
431 gui_drag_scrollbar(sb, value, dragging);
432 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200433 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434}
435
436 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200437gui_ph_handle_keyboard(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 PhKeyEvent_t *key;
440 unsigned char string[6];
441 int len, i;
442 int ch, modifiers;
443
Bram Moolenaard0988c52011-08-10 12:19:04 +0200444 key = PhGetData(info->event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446 ch = modifiers = len = 0;
447
Bram Moolenaard0988c52011-08-10 12:19:04 +0200448 if (p_mh)
449 gui_mch_mousehide(MOUSE_HIDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaar734a8672019-12-02 22:49:38 +0100451 // We're a good lil photon program, aren't we? yes we are, yeess wee arrr
Bram Moolenaard0988c52011-08-10 12:19:04 +0200452 if (key->key_flags & Pk_KF_Compose)
Bram Moolenaard2221132011-07-27 14:09:09 +0200453 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454
Bram Moolenaard0988c52011-08-10 12:19:04 +0200455 if ((key->key_flags & Pk_KF_Cap_Valid) &&
456 PkIsKeyDown(key->key_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {
458#ifdef FEAT_MENU
459 /*
460 * Only show the menu if the Alt key is down, and the Shift & Ctrl
461 * keys aren't down, as well as the other conditions
462 */
Bram Moolenaard0988c52011-08-10 12:19:04 +0200463 if (((key->key_mods & Pk_KM_Alt) &&
464 !(key->key_mods & Pk_KM_Shift) &&
465 !(key->key_mods & Pk_KM_Ctrl)) &&
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 gui.menu_is_active &&
Bram Moolenaard0988c52011-08-10 12:19:04 +0200467 (*p_wak == 'y' ||
468 (*p_wak == 'm' &&
469 gui_is_menu_shortcut(key->key_cap))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100471 // Fallthrough and let photon look for the hotkey
Bram Moolenaard2221132011-07-27 14:09:09 +0200472 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 }
474#endif
475
Bram Moolenaar15d63192011-09-14 16:05:15 +0200476 for (i = 0; special_keys[i].key_sym != 0; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200478 if (special_keys[i].key_sym == key->key_cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {
480 len = 0;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200481 if (special_keys[i].vim_code1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 ch = special_keys[i].vim_code0;
483 else
484 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100485 // Detect if a keypad number key has been pressed
486 // and change the key if Num Lock is on
Bram Moolenaard0988c52011-08-10 12:19:04 +0200487 if (key->key_cap >= Pk_KP_Enter && key->key_cap <= Pk_KP_9
488 && (key->key_mods & Pk_KM_Num_Lock))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000490 // FIXME: For now, just map the key to an ascii value
Bram Moolenaar734a8672019-12-02 22:49:38 +0100491 // (see <photon/PkKeyDef.h>)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 ch = key->key_cap - 0xf080;
493 }
494 else
Bram Moolenaard0988c52011-08-10 12:19:04 +0200495 ch = TO_SPECIAL(special_keys[i].vim_code0,
496 special_keys[i].vim_code1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498 break;
499 }
500 }
501
Bram Moolenaard0988c52011-08-10 12:19:04 +0200502 if (key->key_mods & Pk_KM_Ctrl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 modifiers |= MOD_MASK_CTRL;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200504 if (key->key_mods & Pk_KM_Alt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 modifiers |= MOD_MASK_ALT;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200506 if (key->key_mods & Pk_KM_Shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 modifiers |= MOD_MASK_SHIFT;
508
Bram Moolenaar734a8672019-12-02 22:49:38 +0100509 // Is this not a special key?
Bram Moolenaard0988c52011-08-10 12:19:04 +0200510 if (special_keys[i].key_sym == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200512 ch = PhTo8859_1(key);
Bram Moolenaar13505972019-01-24 15:04:48 +0100513 if (ch == -1 || (enc_utf8 && ch > 127))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200515 len = PhKeyToMb(string, key);
516 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 static char buf[6];
519 int src_taken, dst_made;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200520 if (enc_utf8 != TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 {
522 PxTranslateFromUTF(
523 charset_translate,
524 string,
525 len,
526 &src_taken,
527 buf,
528 6,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200529 &dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200531 add_to_input_buf(buf, dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533 else
534 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200535 add_to_input_buf(string, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
537
Bram Moolenaard2221132011-07-27 14:09:09 +0200538 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 ch = key->key_cap;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200542 if (ch < 0xff)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100544 // FIXME: is this the right thing to do?
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200545 if (modifiers & MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
547 modifiers &= ~MOD_MASK_CTRL;
548
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200549 if ((ch >= 'a' && ch <= 'z') ||
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 ch == '[' ||
551 ch == ']' ||
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200552 ch == '\\')
553 ch = Ctrl_chr(ch);
554 else if (ch == '2')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 ch = NUL;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200556 else if (ch == '6')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 ch = 0x1e;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200558 else if (ch == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 ch = 0x1f;
560 else
561 modifiers |= MOD_MASK_CTRL;
562 }
563
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200564 if (modifiers & MOD_MASK_ALT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200566 ch = Meta(ch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 modifiers &= ~MOD_MASK_ALT;
568 }
569 }
570 else
571 {
Bram Moolenaard2221132011-07-27 14:09:09 +0200572 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574 }
575 else
576 modifiers &= ~MOD_MASK_SHIFT;
577 }
578
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200579 ch = simplify_key(ch, &modifiers);
580 if (modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
582 string[ len++ ] = CSI;
583 string[ len++ ] = KS_MODIFIER;
584 string[ len++ ] = modifiers;
585 }
586
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200587 if (IS_SPECIAL(ch))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {
589 string[ len++ ] = CSI;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200590 string[ len++ ] = K_SECOND(ch);
591 string[ len++ ] = K_THIRD(ch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 {
595 string[ len++ ] = ch;
596 }
597
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +0200598 // Check if the key interrupts.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +0200600 int int_ch = check_for_interrupt(ch, modifiers);
601
602 if (int_ch != NUL)
603 {
604 ch = int_ch;
605 string[0] = ch;
606 len = 1;
607 trash_input_buf();
608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610
611 if (len == 1 && string[0] == CSI)
612 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100613 // Turn CSI into K_CSI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 string[ len++ ] = KS_EXTRA;
615 string[ len++ ] = KE_CSI;
616 }
617
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200618 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200620 add_to_input_buf(string, len);
Bram Moolenaard2221132011-07-27 14:09:09 +0200621 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 }
623 }
624
Bram Moolenaard2221132011-07-27 14:09:09 +0200625 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626}
627
628 static int
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200629gui_ph_handle_mouse(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 PhPointerEvent_t *pointer;
632 PhRect_t *pos;
633 int button = 0, repeated_click, modifiers = 0x0;
634 short mouse_x, mouse_y;
635
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200636 pointer = PhGetData(info->event);
637 pos = PhGetRects(info->event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200639 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 /*
642 * Coordinates need to be relative to the base window,
643 * not relative to the vimTextArea widget
644 */
645 mouse_x = pos->ul.x + gui.border_width;
646 mouse_y = pos->ul.y + gui.border_width;
647
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200648 if (info->event->type == Ph_EV_PTR_MOTION_NOBUTTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200650 gui_mouse_moved(mouse_x, mouse_y);
Bram Moolenaard2221132011-07-27 14:09:09 +0200651 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200654 if (pointer->key_mods & Pk_KM_Shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 modifiers |= MOUSE_SHIFT;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200656 if (pointer->key_mods & Pk_KM_Ctrl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 modifiers |= MOUSE_CTRL;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200658 if (pointer->key_mods & Pk_KM_Alt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 modifiers |= MOUSE_ALT;
660
661 /*
662 * FIXME More than one button may be involved, but for
663 * now just deal with one
664 */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200665 if (pointer->buttons & Ph_BUTTON_SELECT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 button = MOUSE_LEFT;
667
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200668 if (pointer->buttons & Ph_BUTTON_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
670 button = MOUSE_RIGHT;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100671 // Need the absolute coordinates for the popup menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 abs_mouse.x = pointer->pos.x;
673 abs_mouse.y = pointer->pos.y;
674 }
675
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200676 if (pointer->buttons & Ph_BUTTON_ADJUST)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 button = MOUSE_MIDDLE;
678
Bram Moolenaar734a8672019-12-02 22:49:38 +0100679 // Catch a real release (not phantom or other releases
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200680 if (info->event->type == Ph_EV_BUT_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 button = MOUSE_RELEASE;
682
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200683 if (info->event->type & Ph_EV_PTR_MOTION_BUTTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 button = MOUSE_DRAG;
685
686#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +0100687 // Vim doesn't use button repeats
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200688 if (info->event->type & Ph_EV_BUT_REPEAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 button = MOUSE_DRAG;
690#endif
691
Bram Moolenaar734a8672019-12-02 22:49:38 +0100692 // Don't do anything if it is one of the phantom mouse release events
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200693 if ((button != MOUSE_RELEASE) ||
694 (info->event->subtype == Ph_EV_RELEASE_REAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {
696 repeated_click = (pointer->click_count >= 2) ? TRUE : FALSE;
697
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200698 gui_send_mouse_event(button , mouse_x, mouse_y, repeated_click, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
700
Bram Moolenaard2221132011-07-27 14:09:09 +0200701 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702}
703
Bram Moolenaar734a8672019-12-02 22:49:38 +0100704/*
705 * Handle a focus change of the PtRaw widget
706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 static int
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200708gui_ph_handle_focus(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200710 if (info->reason == Pt_CB_LOST_FOCUS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200712 PtRemoveEventHandler(gui.vimTextArea, Ph_EV_PTR_MOTION_NOBUTTON,
713 gui_ph_handle_mouse, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200715 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717 else
718 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200719 PtAddEventHandler(gui.vimTextArea, Ph_EV_PTR_MOTION_NOBUTTON,
720 gui_ph_handle_mouse, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200722 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723}
724
725 static void
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200726gui_ph_handle_raw_draw(PtWidget_t *widget, PhTile_t *damage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 PhRect_t *r;
729 PhPoint_t offset;
730 PhPoint_t translation;
731
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200732 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 return;
734
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200735 PtSuperClassDraw(PtBasic, widget, damage);
736 PgGetTranslation(&translation);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 PgClearTranslation();
738
739#if 0
740 /*
Bram Moolenaarccc18222007-05-10 18:25:20 +0000741 * This causes some weird problems, with drawing being done from
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * within this raw drawing function (rather than just simple clearing
743 * and text drawing done by gui_redraw)
744 *
745 * The main problem is when PhBlit is used, and the cursor appearing
746 * in places where it shouldn't
747 */
748 out_flush();
749#endif
750
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200751 PtWidgetOffset(widget, &offset);
752 PhTranslatePoint(&offset, PtWidgetPos(gui.vimTextArea, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754#if 1
Bram Moolenaar734a8672019-12-02 22:49:38 +0100755 // Redraw individual damage regions
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200756 if (damage->next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 damage = damage->next;
758
Bram Moolenaar15d63192011-09-14 16:05:15 +0200759 while (damage != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
761 r = &damage->rect;
762 gui_redraw(
763 r->ul.x - offset.x, r->ul.y - offset.y,
764 r->lr.x - r->ul.x + 1,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200765 r->lr.y - r->ul.y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 damage = damage->next;
767 }
768#else
Bram Moolenaar734a8672019-12-02 22:49:38 +0100769 // Redraw the rectangle that covers all the damaged regions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 r = &damage->rect;
771 gui_redraw(
772 r->ul.x - offset.x, r->ul.y - offset.y,
773 r->lr.x - r->ul.x + 1,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200774 r->lr.y - r->ul.y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200777 PgSetTranslation(&translation, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778}
779
780 static int
781gui_ph_handle_pulldown_menu(
782 PtWidget_t *widget,
783 void *data,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200784 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785{
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200786 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
788 vimmenu_T *menu = (vimmenu_T *) data;
789
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200790 PtPositionMenu(menu->submenu_id, NULL);
791 PtRealizeWidget(menu->submenu_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793
Bram Moolenaard2221132011-07-27 14:09:09 +0200794 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795}
796
Bram Moolenaar734a8672019-12-02 22:49:38 +0100797/*
798 * This is used for pulldown/popup menus and also toolbar buttons
799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 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
Bram Moolenaar734a8672019-12-02 22:49:38 +0100811/*
812 * Stop focus from disappearing into the menubar...
813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 static int
815gui_ph_handle_menu_unrealized(
816 PtWidget_t *widget,
817 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200818 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200820 PtGiveFocus(gui.vimTextArea, NULL);
Bram Moolenaard2221132011-07-27 14:09:09 +0200821 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824 static int
825gui_ph_handle_window_open(
826 PtWidget_t *widget,
827 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200828 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200830 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaard2221132011-07-27 14:09:09 +0200831 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
Bram Moolenaar734a8672019-12-02 22:49:38 +0100834////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836#define DRAW_START gui_ph_draw_start()
837#define DRAW_END gui_ph_draw_end()
838
Bram Moolenaar734a8672019-12-02 22:49:38 +0100839/*
840 * TODO: Set a clipping rect?
841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200843gui_ph_draw_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaara0b19972009-07-01 14:13:18 +0000845 PhGC_t *gc;
846
847 gc = PgGetGC();
Bram Moolenaar15d63192011-09-14 16:05:15 +0200848 PgSetRegion(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)));
849 PgClearClippingsCx(gc);
850 PgClearTranslationCx(gc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar15d63192011-09-14 16:05:15 +0200852 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
853 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar15d63192011-09-14 16:05:15 +0200855 PgSetTranslation(&gui_ph_raw_offset, Pg_RELATIVE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856}
857
858 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200859gui_ph_draw_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
861 gui_ph_raw_offset.x = -gui_ph_raw_offset.x;
862 gui_ph_raw_offset.y = -gui_ph_raw_offset.y;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200863 PgSetTranslation(&gui_ph_raw_offset, Pg_RELATIVE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864}
865
866#ifdef USE_PANEL_GROUP
867 static vimmenu_T *
Bram Moolenaar15d63192011-09-14 16:05:15 +0200868gui_ph_find_buffer_item(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 vimmenu_T *top_level = root_menu;
871 vimmenu_T *items = NULL;
872
Bram Moolenaar15d63192011-09-14 16:05:15 +0200873 while (top_level != NULL &&
874 (STRCMP(top_level->dname, "Buffers") != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 top_level = top_level->next;
876
Bram Moolenaar15d63192011-09-14 16:05:15 +0200877 if (top_level != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {
879 items = top_level->children;
880
Bram Moolenaar15d63192011-09-14 16:05:15 +0200881 while (items != NULL &&
882 (STRCMP(items->dname, name) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 items = items->next;
884 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200885 return items;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886}
887
888 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200889gui_ph_pg_set_buffer_num(int_u buf_num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 int i;
892 char search[16];
893 char *mark;
894
Bram Moolenaar15d63192011-09-14 16:05:15 +0200895 if (gui.vimTextArea == NULL || buf_num == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 return;
897
898 search[0] = '(';
Bram Moolenaar15d63192011-09-14 16:05:15 +0200899 ultoa(buf_num, &search[1], 10);
900 STRCAT(search, ")");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaar15d63192011-09-14 16:05:15 +0200902 for (i = 0; i < num_panels; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100904 // find the last "(" in the panel title and see if the buffer
905 // number in the title matches the one we're looking for
Bram Moolenaar15d63192011-09-14 16:05:15 +0200906 mark = STRRCHR(panel_titles[ i ], '(');
907 if (mark != NULL && STRCMP(mark, search) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200909 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_CURRENT_INDEX,
910 i, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912 }
913}
914
915 static int
916gui_ph_handle_pg_change(
917 PtWidget_t *widget,
918 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200919 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
921 vimmenu_T *menu;
922 PtPanelGroupCallback_t *panel;
923
Bram Moolenaar15d63192011-09-14 16:05:15 +0200924 if (info->event != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
926 panel = info->cbdata;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200927 if (panel->new_panel != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200929 menu = gui_ph_find_buffer_item(panel->new_panel);
930 if (menu)
931 gui_menu_cb(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
933 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200934 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935}
936
937 static void
938gui_ph_get_panelgroup_margins(
939 short *top,
940 short *bottom,
941 short *left,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200942 short *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 unsigned short abs_raw_x, abs_raw_y, abs_panel_x, abs_panel_y;
945 const unsigned short *margin_top, *margin_bottom;
946 const unsigned short *margin_left, *margin_right;
947
Bram Moolenaar15d63192011-09-14 16:05:15 +0200948 PtGetAbsPosition(gui.vimTextArea, &abs_raw_x, &abs_raw_y);
949 PtGetAbsPosition(gui.vimPanelGroup, &abs_panel_x, &abs_panel_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar15d63192011-09-14 16:05:15 +0200951 PtGetResource(gui.vimPanelGroup, Pt_ARG_MARGIN_RIGHT, &margin_right, 0);
952 PtGetResource(gui.vimPanelGroup, Pt_ARG_MARGIN_BOTTOM, &margin_bottom, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
954 abs_raw_x -= abs_panel_x;
955 abs_raw_y -= abs_panel_y;
956
957 *top = abs_raw_y;
958 *bottom = *margin_bottom;
959
960 *left = abs_raw_x;
961 *right = *margin_right;
962}
963
Bram Moolenaar734a8672019-12-02 22:49:38 +0100964/*
965 * Used for the tabs for PtPanelGroup
966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +0200968gui_ph_is_buffer_item(vimmenu_T *menu, vimmenu_T *parent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
970 char *mark;
971
Bram Moolenaar15d63192011-09-14 16:05:15 +0200972 if (STRCMP(parent->dname, "Buffers") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100974 // Look for '(' digits ')'
Bram Moolenaar15d63192011-09-14 16:05:15 +0200975 mark = vim_strchr(menu->dname, '(');
976 if (mark != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {
978 mark++;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200979 while (isdigit(*mark))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 mark++;
981
Bram Moolenaar15d63192011-09-14 16:05:15 +0200982 if (*mark == ')')
Bram Moolenaard2221132011-07-27 14:09:09 +0200983 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
985 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200986 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987}
988
989 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200990gui_ph_pg_add_buffer(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 char **new_titles = NULL;
993
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200994 new_titles = ALLOC_MULT(char *, (num_panels + 1));
Bram Moolenaar15d63192011-09-14 16:05:15 +0200995 if (new_titles != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200997 if (num_panels > 0)
998 memcpy(new_titles, panel_titles, num_panels * sizeof(char **));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000 new_titles[ num_panels++ ] = name;
1001
Bram Moolenaar15d63192011-09-14 16:05:15 +02001002 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, new_titles,
1003 num_panels);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar15d63192011-09-14 16:05:15 +02001005 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 panel_titles = new_titles;
1007 }
1008}
1009
1010 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +02001011gui_ph_pg_remove_buffer(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
1013 int i;
1014 char **new_titles = NULL;
1015
Bram Moolenaar734a8672019-12-02 22:49:38 +01001016 // If there is only 1 panel, we just use the temporary place holder
Bram Moolenaar15d63192011-09-14 16:05:15 +02001017 if (num_panels > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001019 new_titles = ALLOC_MULT(char *, num_panels - 1);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001020 if (new_titles != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 char **s = new_titles;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001023 // Copy all the titles except the one we're removing
Bram Moolenaar15d63192011-09-14 16:05:15 +02001024 for (i = 0; i < num_panels; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001026 if (STRCMP(panel_titles[ i ], name) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 *s++ = panel_titles[ i ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 num_panels--;
1030
Bram Moolenaar15d63192011-09-14 16:05:15 +02001031 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, new_titles,
1032 num_panels);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
Bram Moolenaar15d63192011-09-14 16:05:15 +02001034 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 panel_titles = new_titles;
1036 }
1037 }
1038 else
1039 {
1040 num_panels--;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001041 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, &empty_title,
1042 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Bram Moolenaard23a8232018-02-10 18:45:26 +01001044 VIM_CLEAR(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
1046}
1047
Bram Moolenaar734a8672019-12-02 22:49:38 +01001048/*
1049 * When a buffer item is deleted from the buffer menu
1050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 static int
1052gui_ph_handle_buffer_remove(
1053 PtWidget_t *widget,
1054 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001055 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
1057 vimmenu_T *menu;
1058
Bram Moolenaar15d63192011-09-14 16:05:15 +02001059 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
1061 menu = (vimmenu_T *) data;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001062 gui_ph_pg_remove_buffer(menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064
Bram Moolenaard2221132011-07-27 14:09:09 +02001065 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066}
1067#endif
1068
1069 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001070gui_ph_pane_resize(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001072 if (PtWidgetIsRealized(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
1074 is_ignore_draw = TRUE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001075 PtStartFlux(gui.vimContainer);
1076 PtContainerHold(gui.vimContainer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078
Bram Moolenaard2221132011-07-27 14:09:09 +02001079 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
Bram Moolenaar734a8672019-12-02 22:49:38 +01001082////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 void
Bram Moolenaar15d63192011-09-14 16:05:15 +02001085gui_ph_encoding_changed(int new_encoding)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001087 // Default encoding is latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 char *charset = "latin1";
1089 int i;
1090
1091 struct {
1092 int encoding;
1093 char *name;
1094 } charsets[] = {
1095 { DBCS_JPN, "SHIFT_JIS" },
1096 { DBCS_KOR, "csEUCKR" },
1097 { DBCS_CHT, "big5" },
1098 { DBCS_CHS, "gb" }
1099 };
1100
Bram Moolenaar15d63192011-09-14 16:05:15 +02001101 for (i = 0; i < ARRAY_LENGTH(charsets); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001103 if (new_encoding == charsets[ i ].encoding)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 charset = charsets[ i ].name;
1105 }
1106
Bram Moolenaar15d63192011-09-14 16:05:15 +02001107 charset_translate = PxTranslateSet(charset_translate, charset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Bram Moolenaar734a8672019-12-02 22:49:38 +01001110////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
1112 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001113gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
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 Moolenaar734a8672019-12-02 22:49:38 +01001124 PhDim_t window_size = {100, 100}; // Arbitrary values
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 PhPoint_t pos = {0, 0};
1126
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001127 gui.event_buffer = alloc(EVENT_BUFFER_SIZE);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001128 if (gui.event_buffer == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
Bram Moolenaar734a8672019-12-02 22:49:38 +01001131 // 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
Bram Moolenaar734a8672019-12-02 22:49:38 +01001134 // The +2 is for the 1 pixel dark line on each side
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 gui.border_offset = gui.border_width = GUI_PH_MARGIN + 2;
1136
Bram Moolenaar734a8672019-12-02 22:49:38 +01001137 // 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
Bram Moolenaar734a8672019-12-02 22:49:38 +01001156 // Put in a temporary 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
Bram Moolenaar734a8672019-12-02 22:49:38 +01001166 // 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
Bram Moolenaar734a8672019-12-02 22:49:38 +01001180 // Size for the text area is set in gui_mch_set_text_area_pos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 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
Bram Moolenaar734a8672019-12-02 22:49:38 +01001200 // 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,
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001214 * which will call PtSetResource from gui_mch_new_colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 */
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
Bram Moolenaar734a8672019-12-02 22:49:38 +01001330////////////////////////////////////////////////////////////////////////////
1331// events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
Bram Moolenaar734a8672019-12-02 22:49:38 +01001333/*
1334 * When no events are available, photon will call this function, working is
1335 * set to FALSE, and the gui_mch_update loop will exit.
1336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001338exit_gui_mch_update(void *data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 *(int *)data = FALSE;
Bram Moolenaard2221132011-07-27 14:09:09 +02001341 return Pt_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342}
1343
1344 void
1345gui_mch_update(void)
1346{
1347 int working = TRUE;
1348
Bram Moolenaar15d63192011-09-14 16:05:15 +02001349 PtAppAddWorkProc(NULL, exit_gui_mch_update, &working);
1350 while ((working == TRUE) && !vim_is_input_buf_full())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 PtProcessEvent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352}
1353
1354 int
1355gui_mch_wait_for_chars(int wtime)
1356{
1357 is_timeout = FALSE;
1358
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01001359 if (wtime >= 0)
1360 PtSetResource(gui_ph_timer_timeout, Pt_ARG_TIMER_INITIAL,
1361 wtime == 0 ? 1 : wtime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
Bram Moolenaar15d63192011-09-14 16:05:15 +02001363 while (1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
1365 PtProcessEvent();
Bram Moolenaar15d63192011-09-14 16:05:15 +02001366 if (input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001368 PtSetResource(gui_ph_timer_timeout, Pt_ARG_TIMER_INITIAL, 0, 0);
Bram Moolenaard2221132011-07-27 14:09:09 +02001369 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001371 else if (is_timeout == TRUE)
Bram Moolenaard2221132011-07-27 14:09:09 +02001372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374}
1375
Bram Moolenaar15d63192011-09-14 16:05:15 +02001376#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377/*
1378 * Put up a file requester.
1379 * Returns the selected name in allocated memory, or NULL for Cancel.
1380 * saving, select file to write
1381 * title title for the window
1382 * default_name default name (well duh!)
1383 * ext not used (extension added)
1384 * initdir initial directory, NULL for current dir
1385 * filter not used (file name filter)
1386 */
1387 char_u *
1388gui_mch_browse(
1389 int saving,
1390 char_u *title,
1391 char_u *default_name,
1392 char_u *ext,
1393 char_u *initdir,
1394 char_u *filter)
1395{
1396 PtFileSelectionInfo_t file;
1397 int flags;
1398 char_u *default_path;
1399 char_u *open_text = NULL;
1400
1401 flags = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001402 memset(&file, 0, sizeof(file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaar15d63192011-09-14 16:05:15 +02001404 default_path = alloc(MAXPATHL + 1 + NAME_MAX + 1);
1405 if (default_path != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001407 if (saving == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001409 // Don't need Pt_FSR_CONFIRM_EXISTING, vim will ask anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 flags |= Pt_FSR_NO_FCHECK;
1411 open_text = "&Save";
1412 }
1413
Bram Moolenaar734a8672019-12-02 22:49:38 +01001414 // combine the directory and filename into a single path
Bram Moolenaar15d63192011-09-14 16:05:15 +02001415 if (initdir == NULL || *initdir == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001417 mch_dirname(default_path, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 initdir = default_path;
1419 }
1420 else
1421 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001422 STRCPY(default_path, initdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 initdir = default_path;
1424 }
1425
Bram Moolenaar15d63192011-09-14 16:05:15 +02001426 if (default_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001428 if (default_path[ STRLEN(default_path) - 1 ] != '/')
1429 STRCAT(default_path, "/");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
Bram Moolenaar15d63192011-09-14 16:05:15 +02001431 STRCAT(default_path, default_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433
Bram Moolenaar734a8672019-12-02 22:49:38 +01001434 // TODO: add a filter?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 PtFileSelection(
1436 gui.vimWindow,
1437 NULL,
1438 title,
1439 default_path,
1440 NULL,
1441 open_text,
1442 NULL,
1443 NULL,
1444 &file,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001445 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar15d63192011-09-14 16:05:15 +02001447 vim_free(default_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
Bram Moolenaar15d63192011-09-14 16:05:15 +02001449 if (file.ret == Pt_FSDIALOG_BTN1)
Bram Moolenaard2221132011-07-27 14:09:09 +02001450 return vim_strsave(file.path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
Bram Moolenaard2221132011-07-27 14:09:09 +02001452 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453}
1454#endif
1455
Bram Moolenaar15d63192011-09-14 16:05:15 +02001456#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457static PtWidget_t *gui_ph_dialog_text = NULL;
1458
1459 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001460gui_ph_dialog_close(int button, void *data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 PtModalCtrl_t *modal_ctrl = data;
1463 char_u *dialog_text, *vim_text;
1464
Bram Moolenaar15d63192011-09-14 16:05:15 +02001465 if (gui_ph_dialog_text != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001467 PtGetResource(gui_ph_dialog_text, Pt_ARG_TEXT_STRING, &dialog_text, 0);
1468 PtGetResource(gui_ph_dialog_text, Pt_ARG_POINTER, &vim_text, 0);
1469 STRNCPY(vim_text, dialog_text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471
Bram Moolenaar15d63192011-09-14 16:05:15 +02001472 PtModalUnblock(modal_ctrl, (void *) button);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaard2221132011-07-27 14:09:09 +02001474 return Pt_TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475}
1476
1477 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001478gui_ph_dialog_text_enter(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001480 if (info->reason_subtype == Pt_EDIT_ACTIVATE)
1481 gui_ph_dialog_close(1, data);
Bram Moolenaard2221132011-07-27 14:09:09 +02001482 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483}
1484
1485 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001486gui_ph_dialog_esc(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 PhKeyEvent_t *key;
1489
Bram Moolenaar15d63192011-09-14 16:05:15 +02001490 key = PhGetData(info->event);
1491 if ((key->key_flags & Pk_KF_Cap_Valid) && (key->key_cap == Pk_Escape))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001493 gui_ph_dialog_close(0, data);
Bram Moolenaard2221132011-07-27 14:09:09 +02001494 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
Bram Moolenaard2221132011-07-27 14:09:09 +02001496 return Pt_PROCESS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497}
1498
1499 int
1500gui_mch_dialog(
1501 int type,
1502 char_u *title,
1503 char_u *message,
1504 char_u *buttons,
1505 int default_button,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01001506 char_u *textfield,
1507 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 char_u *str;
1510 char_u **button_array;
1511 char_u *buttons_copy;
1512
1513 int button_count;
1514 int i, len;
1515 int dialog_result = -1;
1516
Bram Moolenaar734a8672019-12-02 22:49:38 +01001517 // FIXME: the vertical option in guioptions is blatantly ignored
1518 // FIXME: so is the type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 button_count = len = i = 0;
1521
Bram Moolenaar15d63192011-09-14 16:05:15 +02001522 if (buttons == NULL || *buttons == NUL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001523 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar734a8672019-12-02 22:49:38 +01001525 // There is one less separator than buttons, so bump up the button count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 button_count = 1;
1527
Bram Moolenaar734a8672019-12-02 22:49:38 +01001528 // Count string length and number of separators
Bram Moolenaar15d63192011-09-14 16:05:15 +02001529 for (str = buttons; *str; str++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 len++;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001532 if (*str == DLG_BUTTON_SEP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 button_count++;
1534 }
1535
Bram Moolenaar15d63192011-09-14 16:05:15 +02001536 if (title == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 title = "Vim";
1538
Bram Moolenaar15d63192011-09-14 16:05:15 +02001539 buttons_copy = alloc(len + 1);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001540 button_array = ALLOC_MULT(char_u *, button_count);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001541 if (buttons_copy != NULL && button_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001543 STRCPY(buttons_copy, buttons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
1545 /*
1546 * Convert DLG_BUTTON_SEP into NUL's and fill in
1547 * button_array with the pointer to each NUL terminated string
1548 */
1549 str = buttons_copy;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001550 for (i = 0; i < button_count; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 button_array[ i ] = str;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001553 for (; *str; str++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001555 if (*str == DLG_BUTTON_SEP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 *str++ = NUL;
1558 break;
1559 }
1560 }
1561 }
1562#ifndef FEAT_GUI_TEXTDIALOG
1563 dialog_result = PtAlert(
1564 gui.vimWindow, NULL,
1565 title,
1566 NULL,
1567 message, NULL,
1568 button_count, (const char **) button_array, NULL,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001569 default_button, 0, Pt_MODAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#else
Bram Moolenaar734a8672019-12-02 22:49:38 +01001571 // Writing the dialog ourselves lets us add extra features, like
1572 // trapping the escape key and returning 0 to vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 int n;
1575 PtArg_t args[5];
1576 PtWidget_t *dialog, *pane;
1577 PtModalCtrl_t modal_ctrl;
1578 PtDialogInfo_t di;
1579
Bram Moolenaar15d63192011-09-14 16:05:15 +02001580 memset(&di, 0, sizeof(di));
1581 memset(&modal_ctrl, 0, sizeof(modal_ctrl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001584 PtSetArg(&args[n++], Pt_ARG_GROUP_ROWS_COLS, 0, 0);
1585 PtSetArg(&args[n++], Pt_ARG_WIDTH, 350, 0);
1586 PtSetArg(&args[n++], Pt_ARG_GROUP_ORIENTATION,
1587 Pt_GROUP_VERTICAL, 0);
1588 PtSetArg(&args[n++], Pt_ARG_GROUP_FLAGS,
1589 Pt_TRUE, Pt_GROUP_NO_KEYS | Pt_GROUP_STRETCH_HORIZONTAL);
1590 PtSetArg(&args[n++], Pt_ARG_CONTAINER_FLAGS, Pt_FALSE, Pt_TRUE);
1591 pane = PtCreateWidget(PtGroup, NULL, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001594 PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, message, 0);
1595 PtCreateWidget(PtLabel, pane, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaar15d63192011-09-14 16:05:15 +02001597 if (textfield != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001600 PtSetArg(&args[n++], Pt_ARG_MAX_LENGTH, IOSIZE - 1, 0);
1601 PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, textfield, 0);
1602 PtSetArg(&args[n++], Pt_ARG_POINTER, textfield, 0);
1603 gui_ph_dialog_text = PtCreateWidget(PtText, pane, n, args);
1604 PtAddCallback(gui_ph_dialog_text, Pt_CB_ACTIVATE,
1605 gui_ph_dialog_text_enter, &modal_ctrl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607
1608 di.parent = gui.vimWindow;
1609 di.pane = pane;
1610 di.title = title;
1611 di.buttons = (const char **) button_array;
1612 di.nbtns = button_count;
1613 di.def_btn = default_button;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001614 // This is just to give the dialog the close button.
1615 // We check for the Escape key ourselves and return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 di.esc_btn = button_count;
1617 di.callback = gui_ph_dialog_close;
1618 di.data = &modal_ctrl;
1619
Bram Moolenaar15d63192011-09-14 16:05:15 +02001620 dialog = PtCreateDialog(&di);
1621 PtAddFilterCallback(dialog, Ph_EV_KEY,
1622 gui_ph_dialog_esc, &modal_ctrl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaar15d63192011-09-14 16:05:15 +02001624 if (gui_ph_dialog_text != NULL)
1625 PtGiveFocus(gui_ph_dialog_text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar734a8672019-12-02 22:49:38 +01001627 // Open dialog, block the vim window and wait for the dialog to close
Bram Moolenaar15d63192011-09-14 16:05:15 +02001628 PtRealizeWidget(dialog);
1629 PtMakeModal(dialog, Ph_CURSOR_NOINPUT, Ph_CURSOR_DEFAULT_COLOR);
1630 dialog_result = (int) PtModalBlock(&modal_ctrl, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar15d63192011-09-14 16:05:15 +02001632 PtDestroyWidget(dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 gui_ph_dialog_text = NULL;
1634 }
1635#endif
1636 }
1637
Bram Moolenaar15d63192011-09-14 16:05:15 +02001638 vim_free(button_array);
1639 vim_free(buttons_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaard2221132011-07-27 14:09:09 +02001641 return dialog_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642}
1643#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01001644////////////////////////////////////////////////////////////////////////////
1645// window size/position/state
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 int
1648gui_mch_get_winpos(int *x, int *y)
1649{
1650 PhPoint_t *pos;
1651
Bram Moolenaar15d63192011-09-14 16:05:15 +02001652 pos = PtWidgetPos(gui.vimWindow, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 *x = pos->x;
1655 *y = pos->y;
1656
Bram Moolenaard2221132011-07-27 14:09:09 +02001657 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658}
1659
1660 void
1661gui_mch_set_winpos(int x, int y)
1662{
1663 PhPoint_t pos = { x, y };
1664
Bram Moolenaar15d63192011-09-14 16:05:15 +02001665 PtSetResource(gui.vimWindow, Pt_ARG_POS, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666}
1667
1668 void
1669gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001670 int min_width, int min_height, int base_width, int base_height,
1671 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 PhDim_t window_size = { width, height };
1674 PhDim_t min_size = { min_width, min_height };
1675
1676#ifdef USE_PANEL_GROUP
1677 window_size.w += pg_margin_left + pg_margin_right;
1678 window_size.h += pg_margin_top + pg_margin_bottom;
1679#endif
1680
Bram Moolenaar15d63192011-09-14 16:05:15 +02001681 PtSetResource(gui.vimWindow, Pt_ARG_MINIMUM_DIM, &min_size, 0);
1682 PtSetResource(gui.vimWindow, Pt_ARG_DIM, &window_size, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaar15d63192011-09-14 16:05:15 +02001684 if (! PtWidgetIsRealized(gui.vimWindow))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 gui_ph_resize_container();
1686}
1687
1688/*
1689 * Return the amount of screen space that hasn't been allocated (such as
1690 * by the shelf).
1691 */
1692 void
1693gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
1694{
1695 PhRect_t console;
1696
Bram Moolenaar15d63192011-09-14 16:05:15 +02001697 PhWindowQueryVisible(Ph_QUERY_WORKSPACE, 0,
1698 PhInputGroup(NULL), &console);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700 *screen_w = console.lr.x - console.ul.x + 1;
1701 *screen_h = console.lr.y - console.ul.y + 1;
1702}
1703
1704 void
1705gui_mch_iconify(void)
1706{
1707 PhWindowEvent_t event;
1708
Bram Moolenaar15d63192011-09-14 16:05:15 +02001709 memset(&event, 0, sizeof (event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 event.event_f = Ph_WM_HIDE;
1711 event.event_state = Ph_WM_EVSTATE_HIDE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001712 event.rid = PtWidgetRid(gui.vimWindow);
1713 PtForwardWindowEvent(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714}
1715
1716#if defined(FEAT_EVAL) || defined(PROTO)
1717/*
1718 * Bring the Vim window to the foreground.
1719 */
1720 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001721gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 PhWindowEvent_t event;
1724
Bram Moolenaar15d63192011-09-14 16:05:15 +02001725 memset(&event, 0, sizeof (event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 event.event_f = Ph_WM_TOFRONT;
1727 event.event_state = Ph_WM_EVSTATE_FFRONT;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001728 event.rid = PtWidgetRid(gui.vimWindow);
1729 PtForwardWindowEvent(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730}
1731#endif
1732
1733 void
1734gui_mch_settitle(char_u *title, char_u *icon)
1735{
1736#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02001737 gui_ph_pg_set_buffer_num(curwin->w_buffer->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02001739 PtSetResource(gui.vimWindow, Pt_ARG_WINDOW_TITLE, title, 0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001740 // Not sure what to do with the icon text, set balloon text somehow?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741}
1742
Bram Moolenaar734a8672019-12-02 22:49:38 +01001743////////////////////////////////////////////////////////////////////////////
1744// Scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746 void
1747gui_mch_set_scrollbar_thumb(scrollbar_T *sb, int val, int size, int max)
1748{
1749 int n = 0;
1750 PtArg_t args[3];
1751
Bram Moolenaar15d63192011-09-14 16:05:15 +02001752 PtSetArg(&args[ n++ ], Pt_ARG_MAXIMUM, max, 0);
1753 PtSetArg(&args[ n++ ], Pt_ARG_SLIDER_SIZE, size, 0);
1754 PtSetArg(&args[ n++ ], Pt_ARG_GAUGE_VALUE, val, 0);
1755 PtSetResources(sb->id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756}
1757
1758 void
1759gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1760{
1761 PhArea_t area = {{ x, y }, { w, h }};
1762
Bram Moolenaar15d63192011-09-14 16:05:15 +02001763 PtSetResource(sb->id, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764}
1765
Bram Moolenaar203ec772020-07-17 20:43:43 +02001766 int
1767gui_mch_get_scrollbar_xpadding(void)
1768{
1769 // TODO: Calculate the padding for adjust scrollbar position when the
1770 // Window is maximized.
1771 return 0;
1772}
1773
1774 int
1775gui_mch_get_scrollbar_ypadding(void)
1776{
1777 // TODO: Calculate the padding for adjust scrollbar position when the
1778 // Window is maximized.
1779 return 0;
1780}
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 void
1783gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1784{
1785 int n = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001786// int anchor_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 PtArg_t args[4];
1788
1789 /*
1790 * Stop the scrollbar from being realized when the parent
1791 * is realized, so it can be explicitly realized by vim.
1792 *
1793 * Also, don't let the scrollbar get focus
1794 */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001795 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_DELAY_REALIZE,
1796 Pt_DELAY_REALIZE | Pt_GETS_FOCUS);
1797 PtSetArg(&args[ n++ ], Pt_ARG_SCROLLBAR_FLAGS, Pt_SCROLLBAR_SHOW_ARROWS, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01001799 // Don't need this anchoring for the scrollbars
Bram Moolenaar15d63192011-09-14 16:05:15 +02001800 if (orient == SBAR_HORIZ)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 anchor_flags = Pt_BOTTOM_ANCHORED_BOTTOM |
1803 Pt_LEFT_ANCHORED_LEFT | Pt_RIGHT_ANCHORED_RIGHT;
1804 }
1805 else
1806 {
1807 anchor_flags = Pt_BOTTOM_ANCHORED_BOTTOM | Pt_TOP_ANCHORED_TOP;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001808 if (sb->wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001810 if (sb == &sb->wp->w_scrollbars[ SBAR_LEFT ])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 anchor_flags |= Pt_LEFT_ANCHORED_LEFT;
1812 else
1813 anchor_flags |= Pt_RIGHT_ANCHORED_RIGHT;
1814 }
1815 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001816 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS, anchor_flags, Pt_IS_ANCHORED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02001818 PtSetArg(&args[ n++ ], Pt_ARG_ORIENTATION,
1819 (orient == SBAR_HORIZ) ? Pt_HORIZONTAL : Pt_VERTICAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02001821 sb->id = PtCreateWidget(PtScrollbar, gui.vimPanelGroup, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001823 sb->id = PtCreateWidget(PtScrollbar, gui.vimContainer, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#endif
1825
Bram Moolenaar15d63192011-09-14 16:05:15 +02001826 PtAddCallback(sb->id, Pt_CB_SCROLLBAR_MOVE, gui_ph_handle_scrollbar, sb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827}
1828
1829 void
1830gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
1831{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001832 if (flag != 0)
1833 PtRealizeWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001835 PtUnrealizeWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836}
1837
1838 void
1839gui_mch_destroy_scrollbar(scrollbar_T *sb)
1840{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001841 PtDestroyWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 sb->id = NULL;
1843}
1844
Bram Moolenaar734a8672019-12-02 22:49:38 +01001845////////////////////////////////////////////////////////////////////////////
1846// Mouse functions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001849// The last set mouse pointer shape is remembered, to be used when it goes
1850// from hidden to not hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851static int last_shape = 0;
1852
Bram Moolenaar734a8672019-12-02 22:49:38 +01001853// Table for shape IDs. Keep in sync with the mshape_names[] table in
1854// misc2.c!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855static int mshape_ids[] =
1856{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001857 Ph_CURSOR_POINTER, // arrow
1858 Ph_CURSOR_NONE, // blank
1859 Ph_CURSOR_INSERT, // beam
1860 Ph_CURSOR_DRAG_VERTICAL, // updown
1861 Ph_CURSOR_DRAG_VERTICAL, // udsizing
1862 Ph_CURSOR_DRAG_HORIZONTAL, // leftright
1863 Ph_CURSOR_DRAG_HORIZONTAL, // lrsizing
1864 Ph_CURSOR_WAIT, // busy
1865 Ph_CURSOR_DONT, // no
1866 Ph_CURSOR_CROSSHAIR, // crosshair
1867 Ph_CURSOR_FINGER, // hand1
1868 Ph_CURSOR_FINGER, // hand2
1869 Ph_CURSOR_FINGER, // pencil
1870 Ph_CURSOR_QUESTION_POINT, // question
1871 Ph_CURSOR_POINTER, // right-arrow
1872 Ph_CURSOR_POINTER, // up-arrow
1873 Ph_CURSOR_POINTER // last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874};
1875
1876 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001877mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
1879 int id;
1880
1881 if (!gui.in_use)
1882 return;
1883
1884 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar15d63192011-09-14 16:05:15 +02001885 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE, Ph_CURSOR_NONE,
1886 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 else
1888 {
1889 if (shape >= MSHAPE_NUMBERED)
1890 id = Ph_CURSOR_POINTER;
1891 else
1892 id = mshape_ids[shape];
1893
Bram Moolenaar15d63192011-09-14 16:05:15 +02001894 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE, id, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 if (shape != MSHAPE_HIDE)
1897 last_shape = shape;
1898}
1899#endif
1900
1901 void
1902gui_mch_mousehide(int hide)
1903{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001904 if (gui.pointer_hidden != hide)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 gui.pointer_hidden = hide;
1907#ifdef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001908 if (hide)
1909 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE,
1910 Ph_CURSOR_NONE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001912 mch_set_mouse_shape(last_shape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001914 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE,
1915 (hide == MOUSE_SHOW) ? GUI_PH_MOUSE_TYPE : Ph_CURSOR_NONE,
1916 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#endif
1918 }
1919}
1920
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001921 void
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001922gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923{
1924 PhCursorInfo_t info;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001925 short ix, iy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar734a8672019-12-02 22:49:38 +01001927 // FIXME: does this return the correct position,
1928 // with respect to the border?
Bram Moolenaar15d63192011-09-14 16:05:15 +02001929 PhQueryCursor(PhInputGroup(NULL), &info);
1930 PtGetAbsPosition(gui.vimTextArea , &ix, &iy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001932 *x = info.pos.x - ix;
1933 *y = info.pos.y - iy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
1936 void
1937gui_mch_setmouse(int x, int y)
1938{
1939 short abs_x, abs_y;
1940
Bram Moolenaar15d63192011-09-14 16:05:15 +02001941 PtGetAbsPosition(gui.vimTextArea, &abs_x, &abs_y);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001942 // Add the border offset?
Bram Moolenaar15d63192011-09-14 16:05:15 +02001943 PhMoveCursorAbs(PhInputGroup(NULL), abs_x + x, abs_y + y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944}
1945
Bram Moolenaar734a8672019-12-02 22:49:38 +01001946////////////////////////////////////////////////////////////////////////////
1947// Colours
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
1949/*
1950 * Return the RGB value of a pixel as a long.
1951 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001952 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953gui_mch_get_rgb(guicolor_T pixel)
1954{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001955 return (guicolor_T)(PgRGB(PgRedValue(pixel),
1956 PgGreenValue(pixel), PgBlueValue(pixel)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957}
1958
1959 void
1960gui_mch_new_colors(void)
1961{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001962#if 0 // Don't bother changing the cursor colour
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 short color_diff;
1964
1965 /*
1966 * If there isn't enough difference between the background colour and
1967 * the mouse pointer colour then change the mouse pointer colour
1968 */
1969 color_diff = gui_get_lightness(gui_ph_mouse_color)
1970 - gui_get_lightness(gui.back_pixel);
1971
Bram Moolenaar15d63192011-09-14 16:05:15 +02001972 if (abs(color_diff) < 64)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 short r, g, b;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001975 // not a great algorithm...
Bram Moolenaar15d63192011-09-14 16:05:15 +02001976 r = PgRedValue(gui_ph_mouse_color) ^ 255;
1977 g = PgGreenValue(gui_ph_mouse_color) ^ 255;
1978 b = PgBlueValue(gui_ph_mouse_color) ^ 255;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980#ifndef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001981 gui_ph_mouse_color = PgRGB(r, g, b);
1982 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_COLOR,
1983 gui_ph_mouse_color, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#endif
1985 }
1986#endif
1987
Bram Moolenaar15d63192011-09-14 16:05:15 +02001988 PtSetResource(gui.vimTextArea, Pt_ARG_FILL_COLOR, gui.back_pixel, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989}
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00001992 * This should be split out into a separate file,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 * every port does basically the same thing.
1994 *
1995 * This is the gui_w32.c version (i think..)
1996 * Return INVALCOLOR when failed.
1997 */
1998
1999 guicolor_T
2000gui_mch_get_color(char_u *name)
2001{
Bram Moolenaarab302212016-04-26 20:59:29 +02002002 return gui_get_color_cmn(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003}
2004
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002005 guicolor_T
2006gui_mch_get_rgb_color(int r, int g, int b)
2007{
2008 return gui_get_rgb_color_cmn(r, g, b);
2009}
2010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 void
2012gui_mch_set_fg_color(guicolor_T color)
2013{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002014 PgSetTextColor(color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015}
2016
2017 void
2018gui_mch_set_bg_color(guicolor_T color)
2019{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002020 PgSetFillColor(color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021}
2022
2023 void
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002024gui_mch_set_sp_color(guicolor_T color)
2025{
2026}
2027
2028 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029gui_mch_invert_rectangle(int row, int col, int nr, int nc)
2030{
2031 PhRect_t rect;
2032
Bram Moolenaar15d63192011-09-14 16:05:15 +02002033 rect.ul.x = FILL_X(col);
2034 rect.ul.y = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
Bram Moolenaar734a8672019-12-02 22:49:38 +01002036 // FIXME: This has an off by one pixel problem
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 rect.lr.x = rect.ul.x + nc * gui.char_width;
2038 rect.lr.y = rect.ul.y + nr * gui.char_height;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002039 if (nc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 rect.lr.x -= 1;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002041 if (nr > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 rect.lr.y -= 1;
2043
2044 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002045 PgSetDrawMode(Pg_DrawModeDSTINVERT);
2046 PgDrawRect(&rect, Pg_DRAW_FILL);
2047 PgSetDrawMode(Pg_DrawModeSRCCOPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 DRAW_END;
2049}
2050
2051 void
2052gui_mch_clear_block(int row1, int col1, int row2, int col2)
2053{
2054 PhRect_t block = {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002055 { FILL_X(col1), FILL_Y(row1) },
2056 { FILL_X(col2 + 1) - 1, FILL_Y(row2 + 1) - 1}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 };
2058
2059 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002060 gui_mch_set_bg_color(gui.back_pixel);
2061 PgDrawRect(&block, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 DRAW_END;
2063}
2064
2065 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002066gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 PhRect_t text_rect = {
2069 { gui.border_width, gui.border_width },
2070 { Columns * gui.char_width + gui.border_width - 1 ,
2071 Rows * gui.char_height + gui.border_width - 1 }
2072 };
2073
Bram Moolenaar15d63192011-09-14 16:05:15 +02002074 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return;
2076
2077 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002078 gui_mch_set_bg_color(gui.back_pixel);
2079 PgDrawRect(&text_rect, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 DRAW_END;
2081}
2082
2083 void
2084gui_mch_delete_lines(int row, int num_lines)
2085{
2086 PhRect_t rect;
2087 PhPoint_t delta;
2088
Bram Moolenaar15d63192011-09-14 16:05:15 +02002089 rect.ul.x = FILL_X(gui.scroll_region_left);
2090 rect.ul.y = FILL_Y(row + num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
Bram Moolenaar15d63192011-09-14 16:05:15 +02002092 rect.lr.x = FILL_X(gui.scroll_region_right + 1) - 1;
2093 rect.lr.y = FILL_Y(gui.scroll_region_bot + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
Bram Moolenaar15d63192011-09-14 16:05:15 +02002095 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
2096 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
2097 PhTranslateRect(&rect, &gui_ph_raw_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099 delta.x = 0;
2100 delta.y = -num_lines * gui.char_height;
2101
2102 PgFlush();
2103
Bram Moolenaar15d63192011-09-14 16:05:15 +02002104 PhBlit(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)), &rect, &delta);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 gui_clear_block(
2107 gui.scroll_region_bot - num_lines + 1,
2108 gui.scroll_region_left,
2109 gui.scroll_region_bot,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002110 gui.scroll_region_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112
2113 void
2114gui_mch_insert_lines(int row, int num_lines)
2115{
2116 PhRect_t rect;
2117 PhPoint_t delta;
2118
Bram Moolenaar15d63192011-09-14 16:05:15 +02002119 rect.ul.x = FILL_X(gui.scroll_region_left);
2120 rect.ul.y = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar15d63192011-09-14 16:05:15 +02002122 rect.lr.x = FILL_X(gui.scroll_region_right + 1) - 1;
2123 rect.lr.y = FILL_Y(gui.scroll_region_bot - num_lines + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
Bram Moolenaar15d63192011-09-14 16:05:15 +02002125 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
2126 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
2127 PhTranslateRect(&rect, &gui_ph_raw_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 delta.x = 0;
2130 delta.y = num_lines * gui.char_height;
2131
2132 PgFlush();
2133
Bram Moolenaar15d63192011-09-14 16:05:15 +02002134 PhBlit(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)) , &rect, &delta);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
Bram Moolenaar15d63192011-09-14 16:05:15 +02002136 gui_clear_block(row, gui.scroll_region_left,
2137 row + num_lines - 1, gui.scroll_region_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138}
2139
2140 void
2141gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
2142{
2143 static char *utf8_buffer = NULL;
2144 static int utf8_len = 0;
2145
Bram Moolenaar15d63192011-09-14 16:05:15 +02002146 PhPoint_t pos = { TEXT_X(col), TEXT_Y(row) };
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 PhRect_t rect;
2148
Bram Moolenaar15d63192011-09-14 16:05:15 +02002149 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 return;
2151
2152 DRAW_START;
2153
Bram Moolenaar15d63192011-09-14 16:05:15 +02002154 if (!(flags & DRAW_TRANSP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 PgDrawIRect(
Bram Moolenaar15d63192011-09-14 16:05:15 +02002157 FILL_X(col), FILL_Y(row),
2158 FILL_X(col + len) - 1, FILL_Y(row + 1) - 1,
2159 Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161
Bram Moolenaar15d63192011-09-14 16:05:15 +02002162 if (flags & DRAW_UNDERL)
2163 PgSetUnderline(gui.norm_pixel, Pg_TRANSPARENT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
Bram Moolenaar13505972019-01-24 15:04:48 +01002165 if (charset_translate != NULL && enc_utf8 == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 int src_taken, dst_made;
2168
Bram Moolenaar734a8672019-12-02 22:49:38 +01002169 // Use a static buffer to avoid large amounts of de/allocations
Bram Moolenaar15d63192011-09-14 16:05:15 +02002170 if (utf8_len < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002172 utf8_buffer = realloc(utf8_buffer, len * MB_LEN_MAX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 utf8_len = len;
2174 }
2175
2176 PxTranslateToUTF(
2177 charset_translate,
2178 s,
2179 len,
2180 &src_taken,
2181 utf8_buffer,
2182 utf8_len,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002183 &dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 s = utf8_buffer;
2185 len = dst_made;
2186 }
2187
Bram Moolenaar15d63192011-09-14 16:05:15 +02002188 PgDrawText(s, len, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
Bram Moolenaar15d63192011-09-14 16:05:15 +02002190 if (flags & DRAW_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002192 // FIXME: try and only calculate these values once...
Bram Moolenaar15d63192011-09-14 16:05:15 +02002193 rect.ul.x = FILL_X(col) + 1;
2194 rect.ul.y = FILL_Y(row);
2195 rect.lr.x = FILL_X(col + len) - 1;
2196 rect.lr.y = FILL_Y(row + 1) - 1;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002197 // PgSetUserClip(NULL) causes the scrollbar to not redraw...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#if 0
2199 pos.x++;
2200
Bram Moolenaar15d63192011-09-14 16:05:15 +02002201 PgSetUserClip(&rect);
2202 PgDrawText(s, len, &pos, 0);
2203 PgSetUserClip(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002205 rect.lr.y -= (p_linespace + 1) / 2;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002206 // XXX: DrawTextArea doesn't work with phditto
Bram Moolenaar15d63192011-09-14 16:05:15 +02002207 PgDrawTextArea(s, len, &rect, Pg_TEXT_BOTTOM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#endif
2209 }
2210
Bram Moolenaar15d63192011-09-14 16:05:15 +02002211 if (flags & DRAW_UNDERL)
2212 PgSetUnderline(Pg_TRANSPARENT, Pg_TRANSPARENT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 DRAW_END;
2215}
2216
Bram Moolenaar734a8672019-12-02 22:49:38 +01002217////////////////////////////////////////////////////////////////////////////
2218// Cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 void
2221gui_mch_draw_hollow_cursor(guicolor_T color)
2222{
2223 PhRect_t r;
2224
Bram Moolenaar734a8672019-12-02 22:49:38 +01002225 // FIXME: Double width characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
Bram Moolenaar15d63192011-09-14 16:05:15 +02002227 r.ul.x = FILL_X(gui.col);
2228 r.ul.y = FILL_Y(gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 r.lr.x = r.ul.x + gui.char_width - 1;
2230 r.lr.y = r.ul.y + gui.char_height - 1;
2231
2232 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002233 PgSetStrokeColor(color);
2234 PgDrawRect(&r, Pg_DRAW_STROKE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 DRAW_END;
2236}
2237
2238 void
2239gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
2240{
2241 PhRect_t r;
2242
Bram Moolenaar15d63192011-09-14 16:05:15 +02002243 r.ul.x = FILL_X(gui.col);
2244 r.ul.y = FILL_Y(gui.row) + gui.char_height - h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 r.lr.x = r.ul.x + w - 1;
2246 r.lr.y = r.ul.y + h - 1;
2247
2248 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002249 gui_mch_set_bg_color(color);
2250 PgDrawRect(&r, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 DRAW_END;
2252}
2253
Bram Moolenaar703a8042016-06-04 16:24:32 +02002254 int
2255gui_mch_is_blinking(void)
2256{
2257 return blink_state != BLINK_NONE;
2258}
2259
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02002260 int
2261gui_mch_is_blink_off(void)
2262{
2263 return blink_state == BLINK_OFF;
2264}
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 void
2267gui_mch_set_blinking(long wait, long on, long off)
2268{
2269 blink_waittime = wait;
2270 blink_ontime = on;
2271 blink_offtime = off;
2272}
2273
2274 void
2275gui_mch_start_blink(void)
2276{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002277 // Only turn on the timer on if none of the times are zero
Bram Moolenaar15d63192011-09-14 16:05:15 +02002278 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002280 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
2281 blink_waittime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 blink_state = BLINK_ON;
2283 gui_update_cursor(TRUE, FALSE);
2284 }
2285}
2286
2287 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002288gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002290 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002292 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 gui_update_cursor(TRUE, FALSE);
2294
2295 blink_state = BLINK_NONE;
2296}
2297
Bram Moolenaar734a8672019-12-02 22:49:38 +01002298////////////////////////////////////////////////////////////////////////////
2299// miscellaneous functions
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 void
2302gui_mch_beep(void)
2303{
2304 PtBeep();
2305}
2306
2307 void
2308gui_mch_flash(int msec)
2309{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002310 PgSetFillXORColor(Pg_BLACK, Pg_WHITE);
2311 PgSetDrawMode(Pg_DRAWMODE_XOR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 gui_mch_clear_all();
2313 gui_mch_flush();
2314
Bram Moolenaar15d63192011-09-14 16:05:15 +02002315 ui_delay((long) msec, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316
2317 gui_mch_clear_all();
Bram Moolenaar15d63192011-09-14 16:05:15 +02002318 PgSetDrawMode(Pg_DRAWMODE_OPAQUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 gui_mch_flush();
2320}
2321
2322 void
2323gui_mch_flush(void)
2324{
2325 PgFlush();
2326}
2327
2328 void
2329gui_mch_set_text_area_pos(int x, int y, int w, int h)
2330{
2331 PhArea_t area = {{x, y}, {w, h}};
2332
Bram Moolenaar15d63192011-09-14 16:05:15 +02002333 PtSetResource(gui.vimTextArea, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334}
2335
2336 int
2337gui_mch_haskey(char_u *name)
2338{
2339 int i;
2340
2341 for (i = 0; special_keys[i].key_sym != 0; i++)
2342 if (name[0] == special_keys[i].vim_code0 &&
2343 name[1] == special_keys[i].vim_code1)
Bram Moolenaard2221132011-07-27 14:09:09 +02002344 return OK;
2345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346}
2347
Bram Moolenaar734a8672019-12-02 22:49:38 +01002348////////////////////////////////////////////////////////////////////////////
2349// Menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351#ifdef FEAT_TOOLBAR
2352#include "toolbar.phi"
2353
2354static PhImage_t *gui_ph_toolbar_images[] = {
2355 &tb_new_phi,
2356 &tb_open_phi,
2357 &tb_save_phi,
2358 &tb_undo_phi,
2359 &tb_redo_phi,
2360 &tb_cut_phi,
2361 &tb_copy_phi,
2362 &tb_paste_phi,
2363 &tb_print_phi,
2364 &tb_help_phi,
2365 &tb_find_phi,
2366 &tb_save_all_phi,
2367 &tb_save_session_phi,
2368 &tb_new_session_phi,
2369 &tb_load_session_phi,
2370 &tb_macro_phi,
2371 &tb_replace_phi,
2372 &tb_close_phi,
2373 &tb_maximize_phi,
2374 &tb_minimize_phi,
2375 &tb_split_phi,
2376 &tb_shell_phi,
2377 &tb_find_prev_phi,
2378 &tb_find_next_phi,
2379 &tb_find_help_phi,
2380 &tb_make_phi,
2381 &tb_jump_phi,
2382 &tb_ctags_phi,
2383 &tb_vsplit_phi,
2384 &tb_maxwidth_phi,
2385 &tb_minwidth_phi
2386};
2387
2388static PhImage_t *
Bram Moolenaar15d63192011-09-14 16:05:15 +02002389gui_ph_toolbar_load_icon(char_u *iconfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 static PhImage_t external_icon;
2392 PhImage_t *temp_phi = NULL;
2393
Bram Moolenaar15d63192011-09-14 16:05:15 +02002394 temp_phi = PxLoadImage(iconfile, NULL);
2395 if (temp_phi != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002397 // The label widget will free the image/palette/etc. for us when
2398 // it's destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 temp_phi->flags |= Ph_RELEASE_IMAGE_ALL;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002400 memcpy(&external_icon, temp_phi, sizeof(external_icon));
2401 free(temp_phi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 temp_phi = &external_icon;
2404 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002405 return temp_phi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406}
2407
2408/*
2409 * This returns either a builtin icon image, an external image or NULL
2410 * if it can't find either. The caller can't and doesn't need to try and
2411 * free() the returned image, and it can't store the image pointer.
2412 * (When setting the Pt_ARG_LABEL_IMAGE resource, the contents of the
2413 * PhImage_t are copied, and the original PhImage_t aren't needed anymore).
2414 */
2415static PhImage_t *
Bram Moolenaar15d63192011-09-14 16:05:15 +02002416gui_ph_toolbar_find_icon(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418 char_u full_pathname[ MAXPATHL + 1 ];
2419 PhImage_t *icon = NULL;
2420
Bram Moolenaar15d63192011-09-14 16:05:15 +02002421 if (menu->icon_builtin == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002423 if (menu->iconfile != NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002424 // TODO: use gui_find_iconfile()
Bram Moolenaar15d63192011-09-14 16:05:15 +02002425 icon = gui_ph_toolbar_load_icon(menu->iconfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
Bram Moolenaar734a8672019-12-02 22:49:38 +01002427 // TODO: Restrict loading to just .png? Search for any format?
Bram Moolenaar15d63192011-09-14 16:05:15 +02002428 if ((icon == NULL) &&
2429 ((gui_find_bitmap(menu->name, full_pathname, "gif") == OK) ||
2430 (gui_find_bitmap(menu->name, full_pathname, "png") == OK)))
2431 icon = gui_ph_toolbar_load_icon(full_pathname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
Bram Moolenaar15d63192011-09-14 16:05:15 +02002433 if (icon != NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002434 return icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436
Bram Moolenaar15d63192011-09-14 16:05:15 +02002437 if (menu->iconidx >= 0 &&
2438 (menu->iconidx < ARRAY_LENGTH(gui_ph_toolbar_images)))
Bram Moolenaard2221132011-07-27 14:09:09 +02002439 return gui_ph_toolbar_images[menu->iconidx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
Bram Moolenaard2221132011-07-27 14:09:09 +02002441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442}
2443#endif
2444
Bram Moolenaar15d63192011-09-14 16:05:15 +02002445#if defined(FEAT_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 void
2447gui_mch_enable_menu(int flag)
2448{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002449 if (flag != 0)
2450 PtRealizeWidget(gui.vimMenuBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002452 PtUnrealizeWidget(gui.vimMenuBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453}
2454
2455 void
2456gui_mch_set_menu_pos(int x, int y, int w, int h)
2457{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002458 // Nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459}
2460
Bram Moolenaar734a8672019-12-02 22:49:38 +01002461/*
2462 * Change the position of a menu button in the parent
2463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +02002465gui_ph_position_menu(PtWidget_t *widget, int priority)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 PtWidget_t *traverse;
2468 vimmenu_T *menu;
2469
Bram Moolenaar15d63192011-09-14 16:05:15 +02002470 traverse = PtWidgetChildBack(PtWidgetParent(widget));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaar734a8672019-12-02 22:49:38 +01002472 // Iterate through the list of widgets in traverse, until
2473 // we find the position we want to insert our widget into
2474 // TODO: traverse from front to back, possible speedup?
Bram Moolenaar15d63192011-09-14 16:05:15 +02002475 while (traverse != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002477 PtGetResource(traverse, Pt_ARG_POINTER, &menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
Bram Moolenaar15d63192011-09-14 16:05:15 +02002479 if (menu != NULL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 priority < menu->priority &&
Bram Moolenaar15d63192011-09-14 16:05:15 +02002481 widget != traverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002483 // Insert the widget before the current traverse widget
Bram Moolenaar15d63192011-09-14 16:05:15 +02002484 PtWidgetInsert(widget, traverse, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return;
2486 }
2487
Bram Moolenaar15d63192011-09-14 16:05:15 +02002488 traverse = PtWidgetBrotherInFront(traverse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490}
2491
Bram Moolenaar734a8672019-12-02 22:49:38 +01002492/*
2493 * the index is ignored because it's not useful for our purposes
2494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 void
2496gui_mch_add_menu(vimmenu_T *menu, int index)
2497{
2498 vimmenu_T *parent = menu->parent;
2499 char_u *accel_key;
2500 char_u mnemonic_str[MB_LEN_MAX];
2501 int n;
2502 PtArg_t args[5];
2503
2504 menu->submenu_id = menu->id = NULL;
2505
Bram Moolenaar15d63192011-09-14 16:05:15 +02002506 if (menu_is_menubar(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508
Bram Moolenaar15d63192011-09-14 16:05:15 +02002509 accel_key = vim_strchr(menu->name, '&');
2510 if (accel_key != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 mnemonic_str[0] = accel_key[1];
2513 mnemonic_str[1] = NUL;
2514 }
2515
Bram Moolenaar734a8672019-12-02 22:49:38 +01002516 // Create the menu button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002518 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2519 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_TEXT, menu->actext, 0);
2520 if (accel_key != NULL)
2521 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_KEY, mnemonic_str, 0);
2522 PtSetArg(&args[ n++ ], Pt_ARG_POINTER, menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar15d63192011-09-14 16:05:15 +02002524 if (parent != NULL)
2525 PtSetArg(&args[ n++ ], Pt_ARG_BUTTON_TYPE, Pt_MENU_RIGHT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaar15d63192011-09-14 16:05:15 +02002527 menu->id = PtCreateWidget(PtMenuButton,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 (parent == NULL) ? gui.vimMenuBar : parent->submenu_id,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002529 n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar15d63192011-09-14 16:05:15 +02002531 PtAddCallback(menu->id, Pt_CB_ARM, gui_ph_handle_pulldown_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaar734a8672019-12-02 22:49:38 +01002533 // Create the actual menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002535 if (parent != NULL)
2536 PtSetArg(&args[ n++ ], Pt_ARG_MENU_FLAGS, Pt_TRUE, Pt_MENU_CHILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaar15d63192011-09-14 16:05:15 +02002538 menu->submenu_id = PtCreateWidget(PtMenu, menu->id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar15d63192011-09-14 16:05:15 +02002540 if (parent == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002542 PtAddCallback(menu->submenu_id, Pt_CB_UNREALIZED,
2543 gui_ph_handle_menu_unrealized, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar15d63192011-09-14 16:05:15 +02002545 if (menu->mnemonic != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002547 PtAddHotkeyHandler(gui.vimWindow, tolower(menu->mnemonic),
2548 Pk_KM_Alt, 0, menu, gui_ph_handle_pulldown_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
2550 }
2551
Bram Moolenaar15d63192011-09-14 16:05:15 +02002552 gui_ph_position_menu(menu->id, menu->priority);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar734a8672019-12-02 22:49:38 +01002554 // Redraw menubar here instead of gui_mch_draw_menubar
Bram Moolenaar15d63192011-09-14 16:05:15 +02002555 if (gui.menu_is_active)
2556 PtRealizeWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002558 else if (menu_is_popup(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002560 menu->submenu_id = PtCreateWidget(PtMenu, gui.vimWindow, 0, NULL);
2561 PtAddCallback(menu->submenu_id, Pt_CB_UNREALIZED,
2562 gui_ph_handle_menu_unrealized, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564}
2565
2566 void
2567gui_mch_add_menu_item(vimmenu_T *menu, int index)
2568{
2569 vimmenu_T *parent = menu->parent;
2570 char_u *accel_key;
2571 char_u mnemonic_str[MB_LEN_MAX];
2572 int n;
2573 PtArg_t args[13];
2574
2575 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002576 PtSetArg(&args[ n++ ], Pt_ARG_POINTER, menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
2578#ifdef FEAT_TOOLBAR
Bram Moolenaar15d63192011-09-14 16:05:15 +02002579 if (menu_is_toolbar(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002581 if (menu_is_separator(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002583 PtSetArg(&args[ n++ ], Pt_ARG_SEP_FLAGS,
2584 Pt_SEP_VERTICAL, Pt_SEP_ORIENTATION);
2585 PtSetArg(&args[ n++ ], Pt_ARG_SEP_TYPE, Pt_ETCHED_IN, 0);
2586 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS,
2587 Pt_TRUE, Pt_ANCHOR_TOP_BOTTOM);
2588 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, 2, 0);
2589 menu->id = PtCreateWidget(PtSeparator, gui.vimToolBar, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 else
2592 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002593 if (strstr((const char *) p_toolbar, "text") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002595 PtSetArg(&args[ n++ ], Pt_ARG_BALLOON_POSITION,
2596 Pt_BALLOON_BOTTOM, 0);
2597 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2598 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_FONT, "TextFont08", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002600 if ((strstr((const char *) p_toolbar, "icons") != NULL) &&
2601 (gui_ph_toolbar_images != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002603 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_IMAGE,
2604 gui_ph_toolbar_find_icon(menu), 0);
2605 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_TYPE, Pt_TEXT_IMAGE, 0);
2606 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_IMAGE_SPACING, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002608 if (strstr((const char *) p_toolbar, "tooltips") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002610 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_BALLOON,
2611 gui_ph_show_tooltip, 0);
2612 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_FLAGS,
2613 Pt_TRUE, Pt_SHOW_BALLOON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002615 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_HEIGHT, 1, 0);
2616 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_WIDTH, 1, 0);
2617 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_FALSE,
2618 Pt_HIGHLIGHTED | Pt_GETS_FOCUS);
2619 PtSetArg(&args[ n++ ], Pt_ARG_FILL_COLOR, Pg_TRANSPARENT, 0);
2620 menu->id = PtCreateWidget(PtButton, gui.vimToolBar, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
Bram Moolenaar15d63192011-09-14 16:05:15 +02002622 PtAddCallback(menu->id, Pt_CB_ACTIVATE, gui_ph_handle_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002624 // Update toolbar if it's open
Bram Moolenaar15d63192011-09-14 16:05:15 +02002625 if (PtWidgetIsRealized(gui.vimToolBar))
2626 PtRealizeWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628 else
2629#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02002630 if (menu_is_separator(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002632 menu->id = PtCreateWidget(PtSeparator, parent->submenu_id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634 else
2635 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002636 accel_key = vim_strchr(menu->name, '&');
2637 if (accel_key != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 mnemonic_str[0] = accel_key[1];
2640 mnemonic_str[1] = NUL;
2641 }
2642
Bram Moolenaar15d63192011-09-14 16:05:15 +02002643 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2644 if (accel_key != NULL)
2645 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_KEY, mnemonic_str,
2646 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaar15d63192011-09-14 16:05:15 +02002648 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_TEXT, menu->actext, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar15d63192011-09-14 16:05:15 +02002650 menu->id = PtCreateWidget(PtMenuButton, parent->submenu_id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar15d63192011-09-14 16:05:15 +02002652 PtAddCallback(menu->id, Pt_CB_ACTIVATE, gui_ph_handle_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02002655 if (gui_ph_is_buffer_item(menu, parent) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002657 PtAddCallback(menu->id, Pt_CB_DESTROYED,
2658 gui_ph_handle_buffer_remove, menu);
2659 gui_ph_pg_add_buffer(menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661#endif
2662 }
2663
Bram Moolenaar15d63192011-09-14 16:05:15 +02002664 gui_ph_position_menu(menu->id, menu->priority);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665}
2666
2667 void
2668gui_mch_destroy_menu(vimmenu_T *menu)
2669{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002670 if (menu->submenu_id != NULL)
2671 PtDestroyWidget(menu->submenu_id);
2672 if (menu->id != NULL)
2673 PtDestroyWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 menu->submenu_id = NULL;
2676 menu->id = NULL;
2677}
2678
2679 void
2680gui_mch_menu_grey(vimmenu_T *menu, int grey)
2681{
2682 long flags, mask, fields;
2683
Bram Moolenaar15d63192011-09-14 16:05:15 +02002684 if (menu->id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return;
2686
Bram Moolenaar15d63192011-09-14 16:05:15 +02002687 flags = PtWidgetFlags(menu->id);
2688 if (PtWidgetIsClass(menu->id, PtMenuButton) &&
2689 PtWidgetIsClass(PtWidgetParent(menu->id), PtMenu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
2691 fields = Pt_FALSE;
2692 mask = Pt_SELECTABLE | Pt_HIGHLIGHTED;
2693 }
2694 else
2695 {
2696 fields = Pt_TRUE;
2697 mask = Pt_BLOCKED | Pt_GHOST;
2698 }
2699
Bram Moolenaar15d63192011-09-14 16:05:15 +02002700 if (! grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 fields = ~fields;
2702
Bram Moolenaar15d63192011-09-14 16:05:15 +02002703 PtSetResource(menu->id, Pt_ARG_FLAGS, fields,
2704 mask);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705}
2706
2707 void
2708gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
2709{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002710 // TODO: [un]realize the widget?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711}
2712
2713 void
2714gui_mch_draw_menubar(void)
2715{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002716 // The only time a redraw is needed is when a menu button
2717 // is added to the menubar, and that is detected and the bar
2718 // redrawn in gui_mch_add_menu_item
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
2721 void
2722gui_mch_show_popupmenu(vimmenu_T *menu)
2723{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002724 PtSetResource(menu->submenu_id, Pt_ARG_POS, &abs_mouse, 0);
2725 PtRealizeWidget(menu->submenu_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726}
2727
2728 void
2729gui_mch_toggle_tearoffs(int enable)
2730{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002731 // No tearoffs yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732}
2733
2734#endif
2735
Bram Moolenaar15d63192011-09-14 16:05:15 +02002736#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 void
2738gui_mch_show_toolbar(int showit)
2739{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002740 if (showit)
2741 PtRealizeWidget(gui.vimToolBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002743 PtUnrealizeWidget(gui.vimToolBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744}
2745#endif
2746
Bram Moolenaar734a8672019-12-02 22:49:38 +01002747////////////////////////////////////////////////////////////////////////////
2748// Fonts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 static GuiFont
2751gui_ph_get_font(
2752 char_u *font_name,
2753 int_u font_flags,
2754 int_u font_size,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002755 // Check whether the resulting font has the font flags and size that
2756 // was asked for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 int_u enforce
2758 )
2759{
2760 char_u *font_tag;
2761 FontQueryInfo info;
2762 int_u style;
2763
Bram Moolenaar15d63192011-09-14 16:05:15 +02002764 font_tag = alloc(MAX_FONT_TAG);
2765 if (font_tag != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002767 if (PfGenerateFontName(font_name, font_flags, font_size,
2768 font_tag) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002770 // Enforce some limits on the font used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 style = PHFONT_INFO_FIXED;
2772
Bram Moolenaar15d63192011-09-14 16:05:15 +02002773 if (enforce & PF_STYLE_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 style |= PHFONT_INFO_BOLD;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002775 if (enforce & PF_STYLE_ANTIALIAS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 style |= PHFONT_INFO_ALIAS;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002777 if (enforce & PF_STYLE_ITALIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 style |= PHFONT_INFO_ITALIC;
2779
Bram Moolenaar15d63192011-09-14 16:05:15 +02002780 PfQueryFontInfo(font_tag, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaar15d63192011-09-14 16:05:15 +02002782 if (info.size == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 font_size = 0;
2784
Bram Moolenaar734a8672019-12-02 22:49:38 +01002785 // Make sure font size matches, and that the font style
2786 // at least has the bits we're checking for
Bram Moolenaar15d63192011-09-14 16:05:15 +02002787 if (font_size == info.size &&
2788 style == (info.style & style))
Bram Moolenaard2221132011-07-27 14:09:09 +02002789 return (GuiFont)font_tag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002791 vim_free(font_tag);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002793 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
2796/*
2797 * Split up the vim font name
2798 *
2799 * vim_font is in the form of
2800 * <name>:s<height>:a:b:i
2801 *
2802 * a = antialias
2803 * b = bold
2804 * i = italic
2805 *
2806 */
2807
2808 static int
2809gui_ph_parse_font_name(
2810 char_u *vim_font,
2811 char_u **font_name,
2812 int_u *font_flags,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002813 int_u *font_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
2815 char_u *mark;
2816 int_u name_len, size;
2817
Bram Moolenaar15d63192011-09-14 16:05:15 +02002818 mark = vim_strchr(vim_font, ':');
2819 if (mark == NULL)
2820 name_len = STRLEN(vim_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002822 name_len = (int_u) (mark - vim_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
Bram Moolenaar15d63192011-09-14 16:05:15 +02002824 *font_name = vim_strnsave(vim_font, name_len);
Bram Moolenaard0988c52011-08-10 12:19:04 +02002825 if (*font_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002827 if (mark != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002829 while (*mark != NUL && *mark++ == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002831 switch (tolower(*mark++))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
2833 case 'a': *font_flags |= PF_STYLE_ANTIALIAS; break;
2834 case 'b': *font_flags |= PF_STYLE_BOLD; break;
2835 case 'i': *font_flags |= PF_STYLE_ITALIC; break;
2836
2837 case 's':
Bram Moolenaar15d63192011-09-14 16:05:15 +02002838 size = getdigits(&mark);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002839 // Restrict the size to some vague limits
Bram Moolenaard0988c52011-08-10 12:19:04 +02002840 if (size < 1 || size > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 size = 8;
2842
2843 *font_size = size;
2844 break;
2845
2846 default:
2847 break;
2848 }
2849 }
2850 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002851 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002853 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854}
2855
2856 int
2857gui_mch_init_font(char_u *vim_font_name, int fontset)
2858{
2859 char_u *font_tag;
2860 char_u *font_name = NULL;
2861 int_u font_flags = 0;
2862 int_u font_size = 12;
2863
2864 FontQueryInfo info;
2865 PhRect_t extent;
2866
Bram Moolenaard0988c52011-08-10 12:19:04 +02002867 if (vim_font_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002869 // Default font
Bram Moolenaara0b19972009-07-01 14:13:18 +00002870 vim_font_name = "PC Terminal";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872
Bram Moolenaar15d63192011-09-14 16:05:15 +02002873 if (STRCMP(vim_font_name, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002875 font_tag = PtFontSelection(gui.vimWindow, NULL, NULL,
2876 "pcterm12", -1, PHFONT_FIXED, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaard0988c52011-08-10 12:19:04 +02002878 if (font_tag == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002879 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar15d63192011-09-14 16:05:15 +02002881 gui_mch_free_font(gui.norm_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 gui.norm_font = font_tag;
2883
Bram Moolenaar15d63192011-09-14 16:05:15 +02002884 PfQueryFontInfo(font_tag, &info);
2885 font_name = vim_strsave(info.font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
2887 else
2888 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002889 if (gui_ph_parse_font_name(vim_font_name, &font_name, &font_flags,
2890 &font_size) == FALSE)
Bram Moolenaard2221132011-07-27 14:09:09 +02002891 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892
Bram Moolenaar15d63192011-09-14 16:05:15 +02002893 font_tag = gui_ph_get_font(font_name, font_flags, font_size, 0);
Bram Moolenaard0988c52011-08-10 12:19:04 +02002894 if (font_tag == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002896 vim_free(font_name);
Bram Moolenaard2221132011-07-27 14:09:09 +02002897 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002899
Bram Moolenaar15d63192011-09-14 16:05:15 +02002900 gui_mch_free_font(gui.norm_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 gui.norm_font = font_tag;
2902 }
2903
Bram Moolenaar15d63192011-09-14 16:05:15 +02002904 gui_mch_free_font(gui.bold_font);
2905 gui.bold_font = gui_ph_get_font(font_name, font_flags | PF_STYLE_BOLD,
2906 font_size, PF_STYLE_BOLD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaar15d63192011-09-14 16:05:15 +02002908 gui_mch_free_font(gui.ital_font);
2909 gui.ital_font = gui_ph_get_font(font_name, font_flags | PF_STYLE_ITALIC,
2910 font_size, PF_STYLE_ITALIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
Bram Moolenaar734a8672019-12-02 22:49:38 +01002912 // This extent was brought to you by the letter 'g'
Bram Moolenaar15d63192011-09-14 16:05:15 +02002913 PfExtentText(&extent, NULL, font_tag, "g", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915 gui.char_width = extent.lr.x - extent.ul.x + 1;
2916 gui.char_height = (- extent.ul.y) + extent.lr.y + 1;
2917 gui.char_ascent = - extent.ul.y;
2918
Bram Moolenaar15d63192011-09-14 16:05:15 +02002919 vim_free(font_name);
Bram Moolenaard2221132011-07-27 14:09:09 +02002920 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922
Bram Moolenaar02743632005-07-25 20:42:36 +00002923/*
2924 * Adjust gui.char_height (after 'linespace' was changed).
2925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 int
Bram Moolenaar02743632005-07-25 20:42:36 +00002927gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 FontQueryInfo info;
2930
Bram Moolenaar15d63192011-09-14 16:05:15 +02002931 PfQueryFontInfo(gui.norm_font, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 gui.char_height = - info.ascender + info.descender + p_linespace;
2934 gui.char_ascent = - info.ascender + p_linespace / 2;
2935
Bram Moolenaard2221132011-07-27 14:09:09 +02002936 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937}
2938
2939 GuiFont
2940gui_mch_get_font(char_u *vim_font_name, int report_error)
2941{
2942 char_u *font_name;
2943 char_u *font_tag;
2944 int_u font_size = 12;
2945 int_u font_flags = 0;
2946
Bram Moolenaar15d63192011-09-14 16:05:15 +02002947 if (gui_ph_parse_font_name(vim_font_name, &font_name, &font_flags,
2948 &font_size) != FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002950 font_tag = gui_ph_get_font(font_name, font_flags, font_size, -1);
2951 vim_free(font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
Bram Moolenaard0988c52011-08-10 12:19:04 +02002953 if (font_tag != NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002954 return (GuiFont)font_tag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
2956
Bram Moolenaard0988c52011-08-10 12:19:04 +02002957 if (report_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002958 semsg(e_unknown_font_str, vim_font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
Bram Moolenaard2221132011-07-27 14:09:09 +02002960 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961}
2962
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002963#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002964/*
2965 * Return the name of font "font" in allocated memory.
2966 * Don't know how to get the actual name, thus use the provided name.
2967 */
2968 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002969gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002970{
2971 if (name == NULL)
2972 return NULL;
2973 return vim_strsave(name);
2974}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002975#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 void
2978gui_mch_set_font(GuiFont font)
2979{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002980 PgSetFont(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981}
2982
2983 void
2984gui_mch_free_font(GuiFont font)
2985{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002986 vim_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988