blob: 963cabc2767bec468b27a74da638bd9ac664776c [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
37#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(a[0]))
Bram Moolenaard2221132011-07-27 14:09:09 +020038#define RGB(r, g, b) PgRGB(r, g, b)
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaard2221132011-07-27 14:09:09 +020040#define EVENT_BUFFER_SIZE sizeof(PhEvent_t) + 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar734a8672019-12-02 22:49:38 +010042// Some defines for gui_mch_mousehide()
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#define MOUSE_HIDE TRUE
44#define MOUSE_SHOW FALSE
45
Bram Moolenaar734a8672019-12-02 22:49:38 +010046// Optional support for using a PtPanelGroup widget, needs work
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#undef USE_PANEL_GROUP
48
49#ifdef USE_PANEL_GROUP
50static char *empty_title = " ";
51static char **panel_titles = NULL;
52static ushort_t num_panels = 0;
53static short pg_margin_left, pg_margin_right, pg_margin_top, pg_margin_bottom;
54#endif
55
Bram Moolenaar734a8672019-12-02 22:49:38 +010056#define GUI_PH_MARGIN 4 // Size of the bevel
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58#define GUI_PH_MOUSE_TYPE Ph_CURSOR_INSERT
59static PgColor_t gui_ph_mouse_color = Pg_BLACK;
60
61static PhPoint_t gui_ph_raw_offset;
Bram Moolenaar734a8672019-12-02 22:49:38 +010062static PtWidget_t *gui_ph_timer_cursor; // handle cursor blinking
63static PtWidget_t *gui_ph_timer_timeout; // used in gui_mch_wait_for_chars
64static short is_timeout; // Has the timeout occurred?
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * This is set inside the mouse callback for a right mouse
68 * button click, and used for the popup menus
69 */
70static PhPoint_t abs_mouse;
71
Bram Moolenaar734a8672019-12-02 22:49:38 +010072// Try and avoid redraws while a resize is in progress
Bram Moolenaar071d4272004-06-13 20:20:40 +000073static int is_ignore_draw = FALSE;
74
Bram Moolenaar734a8672019-12-02 22:49:38 +010075// Used for converting to/from utf-8 and other charsets
Bram Moolenaar071d4272004-06-13 20:20:40 +000076static struct PxTransCtrl *charset_translate;
77
78/*
79 * Cursor blink functions.
80 *
81 * This is a simple state machine:
82 * BLINK_NONE not blinking at all
83 * BLINK_OFF blinking, cursor is not shown
84 * BLINK_ON blinking, cursor is shown
85 */
86static enum {
87 BLINK_NONE,
88 BLINK_OFF,
89 BLINK_ON
90} blink_state = BLINK_NONE;
91
92static long_u blink_waittime = 700;
93static long_u blink_ontime = 400;
94static long_u blink_offtime = 250;
95
96static struct
97{
98 int key_sym;
99 char_u vim_code0;
100 char_u vim_code1;
101} special_keys[] =
102{
103 {Pk_Up, 'k', 'u'},
104 {Pk_Down, 'k', 'd'},
105 {Pk_Left, 'k', 'l'},
106 {Pk_Right, 'k', 'r'},
107
108 {Pk_F1, 'k', '1'},
109 {Pk_F2, 'k', '2'},
110 {Pk_F3, 'k', '3'},
111 {Pk_F4, 'k', '4'},
112 {Pk_F5, 'k', '5'},
113 {Pk_F6, 'k', '6'},
114 {Pk_F7, 'k', '7'},
115 {Pk_F8, 'k', '8'},
116 {Pk_F9, 'k', '9'},
117 {Pk_F10, 'k', ';'},
118
119 {Pk_F11, 'F', '1'},
120 {Pk_F12, 'F', '2'},
121 {Pk_F13, 'F', '3'},
122 {Pk_F14, 'F', '4'},
123 {Pk_F15, 'F', '5'},
124 {Pk_F16, 'F', '6'},
125 {Pk_F17, 'F', '7'},
126 {Pk_F18, 'F', '8'},
127 {Pk_F19, 'F', '9'},
128 {Pk_F20, 'F', 'A'},
129
130 {Pk_F21, 'F', 'B'},
131 {Pk_F22, 'F', 'C'},
132 {Pk_F23, 'F', 'D'},
133 {Pk_F24, 'F', 'E'},
134 {Pk_F25, 'F', 'F'},
135 {Pk_F26, 'F', 'G'},
136 {Pk_F27, 'F', 'H'},
137 {Pk_F28, 'F', 'I'},
138 {Pk_F29, 'F', 'J'},
139
140 {Pk_F30, 'F', 'K'},
141 {Pk_F31, 'F', 'L'},
142 {Pk_F32, 'F', 'M'},
143 {Pk_F33, 'F', 'N'},
144 {Pk_F34, 'F', 'O'},
145 {Pk_F35, 'F', 'P'},
146
147 {Pk_Help, '%', '1'},
148 {Pk_BackSpace, 'k', 'b'},
149 {Pk_Insert, 'k', 'I'},
150 {Pk_Delete, 'k', 'D'},
151 {Pk_Home, 'k', 'h'},
152 {Pk_End, '@', '7'},
153 {Pk_Prior, 'k', 'P'},
154 {Pk_Next, 'k', 'N'},
155 {Pk_Print, '%', '9'},
156
157 {Pk_KP_Add, 'K', '6'},
158 {Pk_KP_Subtract,'K', '7'},
159 {Pk_KP_Divide, 'K', '8'},
160 {Pk_KP_Multiply,'K', '9'},
161 {Pk_KP_Enter, 'K', 'A'},
162
Bram Moolenaar734a8672019-12-02 22:49:38 +0100163 {Pk_KP_0, KS_EXTRA, KE_KINS}, // Insert
164 {Pk_KP_Decimal, KS_EXTRA, KE_KDEL}, // Delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar734a8672019-12-02 22:49:38 +0100166 {Pk_KP_4, 'k', 'l'}, // Left
167 {Pk_KP_6, 'k', 'r'}, // Right
168 {Pk_KP_8, 'k', 'u'}, // Up
169 {Pk_KP_2, 'k', 'd'}, // Down
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170
Bram Moolenaar734a8672019-12-02 22:49:38 +0100171 {Pk_KP_7, 'K', '1'}, // Home
172 {Pk_KP_1, 'K', '4'}, // End
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
Bram Moolenaar734a8672019-12-02 22:49:38 +0100174 {Pk_KP_9, 'K', '3'}, // Page Up
175 {Pk_KP_3, 'K', '5'}, // Page Down
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar734a8672019-12-02 22:49:38 +0100177 {Pk_KP_5, '&', '8'}, // Undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar734a8672019-12-02 22:49:38 +0100179 // Keys that we want to be able to use any modifier with:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 {Pk_Return, CAR, NUL},
181 {Pk_space, ' ', NUL},
182 {Pk_Tab, TAB, NUL},
183 {Pk_Escape, ESC, NUL},
184 {NL, NL, NUL},
185 {CAR, CAR, NUL},
186
Bram Moolenaar734a8672019-12-02 22:49:38 +0100187 // End of list marker:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 {0, 0, 0}
189};
190
191
Bram Moolenaar734a8672019-12-02 22:49:38 +0100192////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194static PtCallbackF_t gui_ph_handle_timer_cursor;
195static PtCallbackF_t gui_ph_handle_timer_timeout;
196
197static PtCallbackF_t gui_ph_handle_window_cb;
198
199static PtCallbackF_t gui_ph_handle_scrollbar;
200static PtCallbackF_t gui_ph_handle_keyboard;
201static PtCallbackF_t gui_ph_handle_mouse;
202static PtCallbackF_t gui_ph_handle_pulldown_menu;
203static PtCallbackF_t gui_ph_handle_menu;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204static PtCallbackF_t gui_ph_handle_focus; // focus change of text area
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206static PtCallbackF_t gui_ph_handle_menu_resize;
207
Bram Moolenaar734a8672019-12-02 22:49:38 +0100208// When a menu is unrealized, give focus back to vimTextArea
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209static PtCallbackF_t gui_ph_handle_menu_unrealized;
210
211#ifdef USE_PANEL_GROUP
Bram Moolenaard2221132011-07-27 14:09:09 +0200212static void gui_ph_get_panelgroup_margins(short*, short*, short*, short*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#endif
214
Bram Moolenaard2221132011-07-27 14:09:09 +0200215static void gui_ph_draw_start(void);
216static void gui_ph_draw_end(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Bram Moolenaar734a8672019-12-02 22:49:38 +0100218// Set the text for the balloon
Bram Moolenaard2221132011-07-27 14:09:09 +0200219static PtWidget_t * gui_ph_show_tooltip(PtWidget_t *window,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 PtWidget_t *widget,
221 int position,
222 char *text,
223 char *font,
224 PgColor_t fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200225 PgColor_t text_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar734a8672019-12-02 22:49:38 +0100227////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
Bram Moolenaard2221132011-07-27 14:09:09 +0200229static PtWidget_t * gui_ph_show_tooltip(PtWidget_t *window,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 PtWidget_t *widget,
231 int position,
232 char *text,
233 char *font,
234 PgColor_t fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200235 PgColor_t text_color)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 PtArg_t arg;
238 vimmenu_T *menu;
239 char_u *tooltip;
240
Bram Moolenaard2221132011-07-27 14:09:09 +0200241 PtSetArg(&arg, Pt_ARG_POINTER, &menu, 0);
242 PtGetResources(widget, 1, &arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaar734a8672019-12-02 22:49:38 +0100244 // Override the text and position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245
246 tooltip = text;
Bram Moolenaard2221132011-07-27 14:09:09 +0200247 if (menu != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {
249 int index = MENU_INDEX_TIP;
Bram Moolenaard2221132011-07-27 14:09:09 +0200250 if (menu->strings[ index ] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 tooltip = menu->strings[ index ];
252 }
253
Bram Moolenaard2221132011-07-27 14:09:09 +0200254 return PtInflateBalloon(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 window,
256 widget,
Bram Moolenaar734a8672019-12-02 22:49:38 +0100257 // Don't put the balloon at the bottom,
258 // it gets drawn over by gfx done in the PtRaw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 Pt_BALLOON_TOP,
260 tooltip,
261 font,
262 fill_color,
Bram Moolenaard2221132011-07-27 14:09:09 +0200263 text_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264}
265
266 static void
Bram Moolenaard2221132011-07-27 14:09:09 +0200267gui_ph_resize_container(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 PhArea_t area;
270
Bram Moolenaard2221132011-07-27 14:09:09 +0200271 PtWidgetArea(gui.vimWindow, &area);
272 PtWidgetPos (gui.vimContainer, &area.pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
Bram Moolenaard2221132011-07-27 14:09:09 +0200274 PtSetResource(gui.vimContainer, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275}
276
277 static int
278gui_ph_handle_menu_resize(
279 PtWidget_t *widget,
280 void *other,
Bram Moolenaard2221132011-07-27 14:09:09 +0200281 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
283 PtContainerCallback_t *sizes = info->cbdata;
284 PtWidget_t *container;
285 PhPoint_t below_menu;
286 int_u height;
287
288 height = sizes->new_dim.h;
289
Bram Moolenaar734a8672019-12-02 22:49:38 +0100290 // Because vim treats the toolbar and menubar separately,
291 // and here they're lumped together into a PtToolbarGroup,
292 // we only need either menu_height or toolbar_height set at once
Bram Moolenaard2221132011-07-27 14:09:09 +0200293 if (gui.menu_is_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {
295 gui.menu_height = height;
296 gui.toolbar_height = 0;
297 }
298#ifdef FEAT_TOOLBAR
299 else
300 gui.toolbar_height = height;
301#endif
302
303 below_menu.x = 0;
304 below_menu.y = height;
305
306#ifdef USE_PANEL_GROUP
307 container = gui.vimPanelGroup;
308#else
309 container = gui.vimContainer;
310#endif
311
Bram Moolenaard2221132011-07-27 14:09:09 +0200312 PtSetResource(container, Pt_ARG_POS, &below_menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 gui_ph_resize_container();
315
316#ifdef USE_PANEL_GROUP
317 gui_ph_get_panelgroup_margins(
318 &pg_margin_top, &pg_margin_bottom,
Bram Moolenaard2221132011-07-27 14:09:09 +0200319 &pg_margin_left, &pg_margin_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#endif
Bram Moolenaard2221132011-07-27 14:09:09 +0200321 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322}
323
324/*
325 * Pt_ARG_TIMER_REPEAT isn't used because the on & off times
326 * are different
327 */
328 static int
329gui_ph_handle_timer_cursor(
330 PtWidget_t *widget,
331 void *data,
Bram Moolenaard2221132011-07-27 14:09:09 +0200332 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaard2221132011-07-27 14:09:09 +0200334 if (blink_state == BLINK_ON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
336 gui_undraw_cursor();
337 blink_state = BLINK_OFF;
Bram Moolenaard2221132011-07-27 14:09:09 +0200338 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
339 blink_offtime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 }
341 else
342 {
343 gui_update_cursor(TRUE, FALSE);
344 blink_state = BLINK_ON;
Bram Moolenaard2221132011-07-27 14:09:09 +0200345 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
346 blink_ontime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200348 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349}
350
351 static int
352gui_ph_handle_timer_timeout(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
353{
354 is_timeout = TRUE;
355
Bram Moolenaard2221132011-07-27 14:09:09 +0200356 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357}
358
359 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200360gui_ph_handle_window_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361{
362 PhWindowEvent_t *we = info->cbdata;
363 ushort_t *width, *height;
364
Bram Moolenaard0988c52011-08-10 12:19:04 +0200365 switch (we->event_f) {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 case Ph_WM_CLOSE:
367 gui_shell_closed();
368 break;
369
370 case Ph_WM_FOCUS:
Bram Moolenaar734a8672019-12-02 22:49:38 +0100371 // Just in case it's hidden and needs to be shown
Bram Moolenaard0988c52011-08-10 12:19:04 +0200372 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
Bram Moolenaard0988c52011-08-10 12:19:04 +0200374 if (we->event_state == Ph_WM_EVSTATE_FOCUS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
376 gui_focus_change(TRUE);
377 gui_mch_start_blink();
378 }
379 else
380 {
381 gui_focus_change(FALSE);
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100382 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384 break;
385
386 case Ph_WM_RESIZE:
Bram Moolenaard0988c52011-08-10 12:19:04 +0200387 PtGetResource(gui.vimWindow, Pt_ARG_WIDTH, &width, 0);
388 PtGetResource(gui.vimWindow, Pt_ARG_HEIGHT, &height, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#ifdef USE_PANEL_GROUP
390 width -= (pg_margin_left + pg_margin_right);
391 height -= (pg_margin_top + pg_margin_bottom);
392#endif
Bram Moolenaard0988c52011-08-10 12:19:04 +0200393 gui_resize_shell(*width, *height);
394 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 is_ignore_draw = FALSE;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200396 PtEndFlux(gui.vimContainer);
397 PtContainerRelease(gui.vimContainer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 break;
399
400 default:
401 break;
402 }
403
Bram Moolenaard2221132011-07-27 14:09:09 +0200404 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405}
406
407 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200408gui_ph_handle_scrollbar(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
410 PtScrollbarCallback_t *scroll;
411 scrollbar_T *sb;
412 int value, dragging = FALSE;
413
414 scroll = info->cbdata;
415
416 sb = (scrollbar_T *) data;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200417 if (sb != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {
419 value = scroll->position;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200420 switch (scroll->action)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {
422 case Pt_SCROLL_DRAGGED:
423 dragging = TRUE;
424 break;
425
426 case Pt_SCROLL_SET:
Bram Moolenaar734a8672019-12-02 22:49:38 +0100427 // FIXME: return straight away here?
Bram Moolenaard2221132011-07-27 14:09:09 +0200428 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 break;
430 }
431
432 gui_drag_scrollbar(sb, value, dragging);
433 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200434 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435}
436
437 static int
Bram Moolenaard0988c52011-08-10 12:19:04 +0200438gui_ph_handle_keyboard(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 PhKeyEvent_t *key;
441 unsigned char string[6];
442 int len, i;
443 int ch, modifiers;
444
Bram Moolenaard0988c52011-08-10 12:19:04 +0200445 key = PhGetData(info->event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 ch = modifiers = len = 0;
448
Bram Moolenaard0988c52011-08-10 12:19:04 +0200449 if (p_mh)
450 gui_mch_mousehide(MOUSE_HIDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
Bram Moolenaar734a8672019-12-02 22:49:38 +0100452 // We're a good lil photon program, aren't we? yes we are, yeess wee arrr
Bram Moolenaard0988c52011-08-10 12:19:04 +0200453 if (key->key_flags & Pk_KF_Compose)
Bram Moolenaard2221132011-07-27 14:09:09 +0200454 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
Bram Moolenaard0988c52011-08-10 12:19:04 +0200456 if ((key->key_flags & Pk_KF_Cap_Valid) &&
457 PkIsKeyDown(key->key_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {
459#ifdef FEAT_MENU
460 /*
461 * Only show the menu if the Alt key is down, and the Shift & Ctrl
462 * keys aren't down, as well as the other conditions
463 */
Bram Moolenaard0988c52011-08-10 12:19:04 +0200464 if (((key->key_mods & Pk_KM_Alt) &&
465 !(key->key_mods & Pk_KM_Shift) &&
466 !(key->key_mods & Pk_KM_Ctrl)) &&
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 gui.menu_is_active &&
Bram Moolenaard0988c52011-08-10 12:19:04 +0200468 (*p_wak == 'y' ||
469 (*p_wak == 'm' &&
470 gui_is_menu_shortcut(key->key_cap))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100472 // Fallthrough and let photon look for the hotkey
Bram Moolenaard2221132011-07-27 14:09:09 +0200473 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 }
475#endif
476
Bram Moolenaar15d63192011-09-14 16:05:15 +0200477 for (i = 0; special_keys[i].key_sym != 0; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200479 if (special_keys[i].key_sym == key->key_cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 {
481 len = 0;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200482 if (special_keys[i].vim_code1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 ch = special_keys[i].vim_code0;
484 else
485 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100486 // Detect if a keypad number key has been pressed
487 // and change the key if Num Lock is on
Bram Moolenaard0988c52011-08-10 12:19:04 +0200488 if (key->key_cap >= Pk_KP_Enter && key->key_cap <= Pk_KP_9
489 && (key->key_mods & Pk_KM_Num_Lock))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100491 // FIXME: For now, just map the key to a ascii value
492 // (see <photon/PkKeyDef.h>)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 ch = key->key_cap - 0xf080;
494 }
495 else
Bram Moolenaard0988c52011-08-10 12:19:04 +0200496 ch = TO_SPECIAL(special_keys[i].vim_code0,
497 special_keys[i].vim_code1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 }
499 break;
500 }
501 }
502
Bram Moolenaard0988c52011-08-10 12:19:04 +0200503 if (key->key_mods & Pk_KM_Ctrl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 modifiers |= MOD_MASK_CTRL;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200505 if (key->key_mods & Pk_KM_Alt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 modifiers |= MOD_MASK_ALT;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200507 if (key->key_mods & Pk_KM_Shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 modifiers |= MOD_MASK_SHIFT;
509
Bram Moolenaar734a8672019-12-02 22:49:38 +0100510 // Is this not a special key?
Bram Moolenaard0988c52011-08-10 12:19:04 +0200511 if (special_keys[i].key_sym == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200513 ch = PhTo8859_1(key);
Bram Moolenaar13505972019-01-24 15:04:48 +0100514 if (ch == -1 || (enc_utf8 && ch > 127))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
Bram Moolenaard0988c52011-08-10 12:19:04 +0200516 len = PhKeyToMb(string, key);
517 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 {
519 static char buf[6];
520 int src_taken, dst_made;
Bram Moolenaard0988c52011-08-10 12:19:04 +0200521 if (enc_utf8 != TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
523 PxTranslateFromUTF(
524 charset_translate,
525 string,
526 len,
527 &src_taken,
528 buf,
529 6,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200530 &dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200532 add_to_input_buf(buf, dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
534 else
535 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200536 add_to_input_buf(string, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
538
Bram Moolenaard2221132011-07-27 14:09:09 +0200539 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 ch = key->key_cap;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200543 if (ch < 0xff)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100545 // FIXME: is this the right thing to do?
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200546 if (modifiers & MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {
548 modifiers &= ~MOD_MASK_CTRL;
549
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200550 if ((ch >= 'a' && ch <= 'z') ||
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 ch == '[' ||
552 ch == ']' ||
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200553 ch == '\\')
554 ch = Ctrl_chr(ch);
555 else if (ch == '2')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 ch = NUL;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200557 else if (ch == '6')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 ch = 0x1e;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200559 else if (ch == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 ch = 0x1f;
561 else
562 modifiers |= MOD_MASK_CTRL;
563 }
564
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200565 if (modifiers & MOD_MASK_ALT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200567 ch = Meta(ch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 modifiers &= ~MOD_MASK_ALT;
569 }
570 }
571 else
572 {
Bram Moolenaard2221132011-07-27 14:09:09 +0200573 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575 }
576 else
577 modifiers &= ~MOD_MASK_SHIFT;
578 }
579
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200580 ch = simplify_key(ch, &modifiers);
581 if (modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583 string[ len++ ] = CSI;
584 string[ len++ ] = KS_MODIFIER;
585 string[ len++ ] = modifiers;
586 }
587
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200588 if (IS_SPECIAL(ch))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
590 string[ len++ ] = CSI;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200591 string[ len++ ] = K_SECOND(ch);
592 string[ len++ ] = K_THIRD(ch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594 else
595 {
596 string[ len++ ] = ch;
597 }
598
599 if (len == 1 && ((ch == Ctrl_C && ctrl_c_interrupts)
600 || ch == intr_char))
601 {
602 trash_input_buf();
603 got_int = TRUE;
604 }
605
606 if (len == 1 && string[0] == CSI)
607 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100608 // Turn CSI into K_CSI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 string[ len++ ] = KS_EXTRA;
610 string[ len++ ] = KE_CSI;
611 }
612
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200613 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200615 add_to_input_buf(string, len);
Bram Moolenaard2221132011-07-27 14:09:09 +0200616 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
618 }
619
Bram Moolenaard2221132011-07-27 14:09:09 +0200620 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621}
622
623 static int
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200624gui_ph_handle_mouse(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 PhPointerEvent_t *pointer;
627 PhRect_t *pos;
628 int button = 0, repeated_click, modifiers = 0x0;
629 short mouse_x, mouse_y;
630
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200631 pointer = PhGetData(info->event);
632 pos = PhGetRects(info->event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200634 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 /*
637 * Coordinates need to be relative to the base window,
638 * not relative to the vimTextArea widget
639 */
640 mouse_x = pos->ul.x + gui.border_width;
641 mouse_y = pos->ul.y + gui.border_width;
642
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200643 if (info->event->type == Ph_EV_PTR_MOTION_NOBUTTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200645 gui_mouse_moved(mouse_x, mouse_y);
Bram Moolenaard2221132011-07-27 14:09:09 +0200646 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200649 if (pointer->key_mods & Pk_KM_Shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 modifiers |= MOUSE_SHIFT;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200651 if (pointer->key_mods & Pk_KM_Ctrl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 modifiers |= MOUSE_CTRL;
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200653 if (pointer->key_mods & Pk_KM_Alt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 modifiers |= MOUSE_ALT;
655
656 /*
657 * FIXME More than one button may be involved, but for
658 * now just deal with one
659 */
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200660 if (pointer->buttons & Ph_BUTTON_SELECT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 button = MOUSE_LEFT;
662
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200663 if (pointer->buttons & Ph_BUTTON_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 button = MOUSE_RIGHT;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100666 // Need the absolute coordinates for the popup menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 abs_mouse.x = pointer->pos.x;
668 abs_mouse.y = pointer->pos.y;
669 }
670
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200671 if (pointer->buttons & Ph_BUTTON_ADJUST)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 button = MOUSE_MIDDLE;
673
Bram Moolenaar734a8672019-12-02 22:49:38 +0100674 // Catch a real release (not phantom or other releases
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200675 if (info->event->type == Ph_EV_BUT_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 button = MOUSE_RELEASE;
677
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200678 if (info->event->type & Ph_EV_PTR_MOTION_BUTTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 button = MOUSE_DRAG;
680
681#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +0100682 // Vim doesn't use button repeats
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200683 if (info->event->type & Ph_EV_BUT_REPEAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 button = MOUSE_DRAG;
685#endif
686
Bram Moolenaar734a8672019-12-02 22:49:38 +0100687 // Don't do anything if it is one of the phantom mouse release events
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200688 if ((button != MOUSE_RELEASE) ||
689 (info->event->subtype == Ph_EV_RELEASE_REAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
691 repeated_click = (pointer->click_count >= 2) ? TRUE : FALSE;
692
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200693 gui_send_mouse_event(button , mouse_x, mouse_y, repeated_click, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
695
Bram Moolenaard2221132011-07-27 14:09:09 +0200696 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaar734a8672019-12-02 22:49:38 +0100699/*
700 * Handle a focus change of the PtRaw widget
701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 static int
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200703gui_ph_handle_focus(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200705 if (info->reason == Pt_CB_LOST_FOCUS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200707 PtRemoveEventHandler(gui.vimTextArea, Ph_EV_PTR_MOTION_NOBUTTON,
708 gui_ph_handle_mouse, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200710 gui_mch_mousehide(MOUSE_SHOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 }
712 else
713 {
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200714 PtAddEventHandler(gui.vimTextArea, Ph_EV_PTR_MOTION_NOBUTTON,
715 gui_ph_handle_mouse, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200717 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718}
719
720 static void
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200721gui_ph_handle_raw_draw(PtWidget_t *widget, PhTile_t *damage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 PhRect_t *r;
724 PhPoint_t offset;
725 PhPoint_t translation;
726
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200727 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 return;
729
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200730 PtSuperClassDraw(PtBasic, widget, damage);
731 PgGetTranslation(&translation);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 PgClearTranslation();
733
734#if 0
735 /*
Bram Moolenaarccc18222007-05-10 18:25:20 +0000736 * This causes some weird problems, with drawing being done from
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * within this raw drawing function (rather than just simple clearing
738 * and text drawing done by gui_redraw)
739 *
740 * The main problem is when PhBlit is used, and the cursor appearing
741 * in places where it shouldn't
742 */
743 out_flush();
744#endif
745
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200746 PtWidgetOffset(widget, &offset);
747 PhTranslatePoint(&offset, PtWidgetPos(gui.vimTextArea, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748
749#if 1
Bram Moolenaar734a8672019-12-02 22:49:38 +0100750 // Redraw individual damage regions
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200751 if (damage->next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 damage = damage->next;
753
Bram Moolenaar15d63192011-09-14 16:05:15 +0200754 while (damage != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 r = &damage->rect;
757 gui_redraw(
758 r->ul.x - offset.x, r->ul.y - offset.y,
759 r->lr.x - r->ul.x + 1,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200760 r->lr.y - r->ul.y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 damage = damage->next;
762 }
763#else
Bram Moolenaar734a8672019-12-02 22:49:38 +0100764 // Redraw the rectangle that covers all the damaged regions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 r = &damage->rect;
766 gui_redraw(
767 r->ul.x - offset.x, r->ul.y - offset.y,
768 r->lr.x - r->ul.x + 1,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200769 r->lr.y - r->ul.y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200772 PgSetTranslation(&translation, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773}
774
775 static int
776gui_ph_handle_pulldown_menu(
777 PtWidget_t *widget,
778 void *data,
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200779 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200781 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
783 vimmenu_T *menu = (vimmenu_T *) data;
784
Bram Moolenaar0d55ff12011-09-07 19:09:01 +0200785 PtPositionMenu(menu->submenu_id, NULL);
786 PtRealizeWidget(menu->submenu_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788
Bram Moolenaard2221132011-07-27 14:09:09 +0200789 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
Bram Moolenaar734a8672019-12-02 22:49:38 +0100792/*
793 * This is used for pulldown/popup menus and also toolbar buttons
794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +0200796gui_ph_handle_menu(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200798 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
800 vimmenu_T *menu = (vimmenu_T *) data;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200801 gui_menu_cb(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200803 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804}
805
Bram Moolenaar734a8672019-12-02 22:49:38 +0100806/*
807 * Stop focus from disappearing into the menubar...
808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 static int
810gui_ph_handle_menu_unrealized(
811 PtWidget_t *widget,
812 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200813 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200815 PtGiveFocus(gui.vimTextArea, NULL);
Bram Moolenaard2221132011-07-27 14:09:09 +0200816 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817}
818
819 static int
820gui_ph_handle_window_open(
821 PtWidget_t *widget,
822 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200823 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaar15d63192011-09-14 16:05:15 +0200825 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaard2221132011-07-27 14:09:09 +0200826 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827}
828
Bram Moolenaar734a8672019-12-02 22:49:38 +0100829////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831#define DRAW_START gui_ph_draw_start()
832#define DRAW_END gui_ph_draw_end()
833
Bram Moolenaar734a8672019-12-02 22:49:38 +0100834/*
835 * TODO: Set a clipping rect?
836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200838gui_ph_draw_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
Bram Moolenaara0b19972009-07-01 14:13:18 +0000840 PhGC_t *gc;
841
842 gc = PgGetGC();
Bram Moolenaar15d63192011-09-14 16:05:15 +0200843 PgSetRegion(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)));
844 PgClearClippingsCx(gc);
845 PgClearTranslationCx(gc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaar15d63192011-09-14 16:05:15 +0200847 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
848 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar15d63192011-09-14 16:05:15 +0200850 PgSetTranslation(&gui_ph_raw_offset, Pg_RELATIVE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851}
852
853 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200854gui_ph_draw_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 gui_ph_raw_offset.x = -gui_ph_raw_offset.x;
857 gui_ph_raw_offset.y = -gui_ph_raw_offset.y;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200858 PgSetTranslation(&gui_ph_raw_offset, Pg_RELATIVE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861#ifdef USE_PANEL_GROUP
862 static vimmenu_T *
Bram Moolenaar15d63192011-09-14 16:05:15 +0200863gui_ph_find_buffer_item(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 vimmenu_T *top_level = root_menu;
866 vimmenu_T *items = NULL;
867
Bram Moolenaar15d63192011-09-14 16:05:15 +0200868 while (top_level != NULL &&
869 (STRCMP(top_level->dname, "Buffers") != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 top_level = top_level->next;
871
Bram Moolenaar15d63192011-09-14 16:05:15 +0200872 if (top_level != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
874 items = top_level->children;
875
Bram Moolenaar15d63192011-09-14 16:05:15 +0200876 while (items != NULL &&
877 (STRCMP(items->dname, name) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 items = items->next;
879 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200880 return items;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881}
882
883 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200884gui_ph_pg_set_buffer_num(int_u buf_num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
886 int i;
887 char search[16];
888 char *mark;
889
Bram Moolenaar15d63192011-09-14 16:05:15 +0200890 if (gui.vimTextArea == NULL || buf_num == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return;
892
893 search[0] = '(';
Bram Moolenaar15d63192011-09-14 16:05:15 +0200894 ultoa(buf_num, &search[1], 10);
895 STRCAT(search, ")");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar15d63192011-09-14 16:05:15 +0200897 for (i = 0; i < num_panels; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100899 // find the last "(" in the panel title and see if the buffer
900 // number in the title matches the one we're looking for
Bram Moolenaar15d63192011-09-14 16:05:15 +0200901 mark = STRRCHR(panel_titles[ i ], '(');
902 if (mark != NULL && STRCMP(mark, search) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200904 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_CURRENT_INDEX,
905 i, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 }
908}
909
910 static int
911gui_ph_handle_pg_change(
912 PtWidget_t *widget,
913 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200914 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 vimmenu_T *menu;
917 PtPanelGroupCallback_t *panel;
918
Bram Moolenaar15d63192011-09-14 16:05:15 +0200919 if (info->event != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 panel = info->cbdata;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200922 if (panel->new_panel != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200924 menu = gui_ph_find_buffer_item(panel->new_panel);
925 if (menu)
926 gui_menu_cb(menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200929 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930}
931
932 static void
933gui_ph_get_panelgroup_margins(
934 short *top,
935 short *bottom,
936 short *left,
Bram Moolenaar15d63192011-09-14 16:05:15 +0200937 short *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 unsigned short abs_raw_x, abs_raw_y, abs_panel_x, abs_panel_y;
940 const unsigned short *margin_top, *margin_bottom;
941 const unsigned short *margin_left, *margin_right;
942
Bram Moolenaar15d63192011-09-14 16:05:15 +0200943 PtGetAbsPosition(gui.vimTextArea, &abs_raw_x, &abs_raw_y);
944 PtGetAbsPosition(gui.vimPanelGroup, &abs_panel_x, &abs_panel_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar15d63192011-09-14 16:05:15 +0200946 PtGetResource(gui.vimPanelGroup, Pt_ARG_MARGIN_RIGHT, &margin_right, 0);
947 PtGetResource(gui.vimPanelGroup, Pt_ARG_MARGIN_BOTTOM, &margin_bottom, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 abs_raw_x -= abs_panel_x;
950 abs_raw_y -= abs_panel_y;
951
952 *top = abs_raw_y;
953 *bottom = *margin_bottom;
954
955 *left = abs_raw_x;
956 *right = *margin_right;
957}
958
Bram Moolenaar734a8672019-12-02 22:49:38 +0100959/*
960 * Used for the tabs for PtPanelGroup
961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +0200963gui_ph_is_buffer_item(vimmenu_T *menu, vimmenu_T *parent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 char *mark;
966
Bram Moolenaar15d63192011-09-14 16:05:15 +0200967 if (STRCMP(parent->dname, "Buffers") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100969 // Look for '(' digits ')'
Bram Moolenaar15d63192011-09-14 16:05:15 +0200970 mark = vim_strchr(menu->dname, '(');
971 if (mark != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 {
973 mark++;
Bram Moolenaar15d63192011-09-14 16:05:15 +0200974 while (isdigit(*mark))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 mark++;
976
Bram Moolenaar15d63192011-09-14 16:05:15 +0200977 if (*mark == ')')
Bram Moolenaard2221132011-07-27 14:09:09 +0200978 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 }
Bram Moolenaard2221132011-07-27 14:09:09 +0200981 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983
984 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +0200985gui_ph_pg_add_buffer(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987 char **new_titles = NULL;
988
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200989 new_titles = ALLOC_MULT(char *, (num_panels + 1));
Bram Moolenaar15d63192011-09-14 16:05:15 +0200990 if (new_titles != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
Bram Moolenaar15d63192011-09-14 16:05:15 +0200992 if (num_panels > 0)
993 memcpy(new_titles, panel_titles, num_panels * sizeof(char **));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 new_titles[ num_panels++ ] = name;
996
Bram Moolenaar15d63192011-09-14 16:05:15 +0200997 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, new_titles,
998 num_panels);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar15d63192011-09-14 16:05:15 +02001000 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 panel_titles = new_titles;
1002 }
1003}
1004
1005 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +02001006gui_ph_pg_remove_buffer(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
1008 int i;
1009 char **new_titles = NULL;
1010
Bram Moolenaar734a8672019-12-02 22:49:38 +01001011 // If there is only 1 panel, we just use the temporary place holder
Bram Moolenaar15d63192011-09-14 16:05:15 +02001012 if (num_panels > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001014 new_titles = ALLOC_MULT(char *, num_panels - 1);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001015 if (new_titles != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
1017 char **s = new_titles;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001018 // Copy all the titles except the one we're removing
Bram Moolenaar15d63192011-09-14 16:05:15 +02001019 for (i = 0; i < num_panels; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001021 if (STRCMP(panel_titles[ i ], name) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 *s++ = panel_titles[ i ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 num_panels--;
1025
Bram Moolenaar15d63192011-09-14 16:05:15 +02001026 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, new_titles,
1027 num_panels);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
Bram Moolenaar15d63192011-09-14 16:05:15 +02001029 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 panel_titles = new_titles;
1031 }
1032 }
1033 else
1034 {
1035 num_panels--;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001036 PtSetResource(gui.vimPanelGroup, Pt_ARG_PG_PANEL_TITLES, &empty_title,
1037 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaard23a8232018-02-10 18:45:26 +01001039 VIM_CLEAR(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041}
1042
Bram Moolenaar734a8672019-12-02 22:49:38 +01001043/*
1044 * When a buffer item is deleted from the buffer menu
1045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 static int
1047gui_ph_handle_buffer_remove(
1048 PtWidget_t *widget,
1049 void *data,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001050 PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 vimmenu_T *menu;
1053
Bram Moolenaar15d63192011-09-14 16:05:15 +02001054 if (data != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 {
1056 menu = (vimmenu_T *) data;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001057 gui_ph_pg_remove_buffer(menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059
Bram Moolenaard2221132011-07-27 14:09:09 +02001060 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062#endif
1063
1064 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001065gui_ph_pane_resize(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001067 if (PtWidgetIsRealized(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
1069 is_ignore_draw = TRUE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001070 PtStartFlux(gui.vimContainer);
1071 PtContainerHold(gui.vimContainer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
1073
Bram Moolenaard2221132011-07-27 14:09:09 +02001074 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075}
1076
Bram Moolenaar734a8672019-12-02 22:49:38 +01001077////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 void
Bram Moolenaar15d63192011-09-14 16:05:15 +02001080gui_ph_encoding_changed(int new_encoding)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001082 // Default encoding is latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 char *charset = "latin1";
1084 int i;
1085
1086 struct {
1087 int encoding;
1088 char *name;
1089 } charsets[] = {
1090 { DBCS_JPN, "SHIFT_JIS" },
1091 { DBCS_KOR, "csEUCKR" },
1092 { DBCS_CHT, "big5" },
1093 { DBCS_CHS, "gb" }
1094 };
1095
Bram Moolenaar15d63192011-09-14 16:05:15 +02001096 for (i = 0; i < ARRAY_LENGTH(charsets); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001098 if (new_encoding == charsets[ i ].encoding)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 charset = charsets[ i ].name;
1100 }
1101
Bram Moolenaar15d63192011-09-14 16:05:15 +02001102 charset_translate = PxTranslateSet(charset_translate, charset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
Bram Moolenaar734a8672019-12-02 22:49:38 +01001105////////////////////////////////////////////////////////////////////////////
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
1107 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001108gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001110 PtInit(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111}
1112
1113 int
1114gui_mch_init(void)
1115{
1116 PtArg_t args[10];
1117 int flags = 0, n = 0;
1118
Bram Moolenaar734a8672019-12-02 22:49:38 +01001119 PhDim_t window_size = {100, 100}; // Arbitrary values
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 PhPoint_t pos = {0, 0};
1121
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001122 gui.event_buffer = alloc(EVENT_BUFFER_SIZE);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001123 if (gui.event_buffer == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001124 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar734a8672019-12-02 22:49:38 +01001126 // Get a translation so we can convert from ISO Latin-1 to UTF
Bram Moolenaar15d63192011-09-14 16:05:15 +02001127 charset_translate = PxTranslateSet(NULL, "latin1");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
Bram Moolenaar734a8672019-12-02 22:49:38 +01001129 // The +2 is for the 1 pixel dark line on each side
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 gui.border_offset = gui.border_width = GUI_PH_MARGIN + 2;
1131
Bram Moolenaar734a8672019-12-02 22:49:38 +01001132 // Handle close events ourselves
Bram Moolenaar15d63192011-09-14 16:05:15 +02001133 PtSetArg(&args[ n++ ], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_CLOSE);
1134 PtSetArg(&args[ n++ ], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE,
1135 Ph_WM_CLOSE | Ph_WM_RESIZE | Ph_WM_FOCUS);
1136 PtSetArg(&args[ n++ ], Pt_ARG_DIM, &window_size, 0);
1137 gui.vimWindow = PtCreateWidget(PtWindow, NULL, n, args);
1138 if (gui.vimWindow == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001139 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaar15d63192011-09-14 16:05:15 +02001141 PtAddCallback(gui.vimWindow, Pt_CB_WINDOW, gui_ph_handle_window_cb, NULL);
1142 PtAddCallback(gui.vimWindow, Pt_CB_WINDOW_OPENING,
1143 gui_ph_handle_window_open, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001146 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS, Pt_ANCHOR_ALL, Pt_IS_ANCHORED);
1147 PtSetArg(&args[ n++ ], Pt_ARG_DIM, &window_size, 0);
1148 PtSetArg(&args[ n++ ], Pt_ARG_POS, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150#ifdef USE_PANEL_GROUP
Bram Moolenaar734a8672019-12-02 22:49:38 +01001151 // Put in a temporary place holder title
Bram Moolenaar15d63192011-09-14 16:05:15 +02001152 PtSetArg(&args[ n++ ], Pt_ARG_PG_PANEL_TITLES, &empty_title, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
Bram Moolenaar15d63192011-09-14 16:05:15 +02001154 gui.vimPanelGroup = PtCreateWidget(PtPanelGroup, gui.vimWindow, n, args);
1155 if (gui.vimPanelGroup == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001156 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar15d63192011-09-14 16:05:15 +02001158 PtAddCallback(gui.vimPanelGroup, Pt_CB_PG_PANEL_SWITCHING,
1159 gui_ph_handle_pg_change, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160#else
Bram Moolenaar734a8672019-12-02 22:49:38 +01001161 // Turn off all edge decorations
Bram Moolenaar15d63192011-09-14 16:05:15 +02001162 PtSetArg(&args[ n++ ], Pt_ARG_BASIC_FLAGS, Pt_FALSE, Pt_ALL);
1163 PtSetArg(&args[ n++ ], Pt_ARG_BEVEL_WIDTH, 0, 0);
1164 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_WIDTH, 0, 0);
1165 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_HEIGHT, 0, 0);
1166 PtSetArg(&args[ n++ ], Pt_ARG_CONTAINER_FLAGS, Pt_TRUE, Pt_AUTO_EXTENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
Bram Moolenaar15d63192011-09-14 16:05:15 +02001168 gui.vimContainer = PtCreateWidget(PtPane, gui.vimWindow, n, args);
1169 if (gui.vimContainer == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001170 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaar15d63192011-09-14 16:05:15 +02001172 PtAddCallback(gui.vimContainer, Pt_CB_RESIZE, gui_ph_pane_resize, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174
Bram Moolenaar734a8672019-12-02 22:49:38 +01001175 // Size for the text area is set in gui_mch_set_text_area_pos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 n = 0;
1177
Bram Moolenaar15d63192011-09-14 16:05:15 +02001178 PtSetArg(&args[ n++ ], Pt_ARG_RAW_DRAW_F, gui_ph_handle_raw_draw, 1);
1179 PtSetArg(&args[ n++ ], Pt_ARG_BEVEL_WIDTH, GUI_PH_MARGIN, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 /*
1181 * Using focus render also causes the whole widget to be redrawn
1182 * whenever it changes focus, which is very annoying :p
1183 */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001184 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_TRUE,
1185 Pt_GETS_FOCUS | Pt_HIGHLIGHTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#ifndef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001187 PtSetArg(&args[ n++ ], Pt_ARG_CURSOR_TYPE, GUI_PH_MOUSE_TYPE, 0);
1188 PtSetArg(&args[ n++ ], Pt_ARG_CURSOR_COLOR, gui_ph_mouse_color, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#endif
1190
Bram Moolenaar15d63192011-09-14 16:05:15 +02001191 gui.vimTextArea = PtCreateWidget(PtRaw, Pt_DFLT_PARENT, n, args);
1192 if (gui.vimTextArea == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
Bram Moolenaar734a8672019-12-02 22:49:38 +01001195 // TODO: use PtAddEventHandlers instead?
1196 // Not using Ph_EV_BUT_REPEAT because vim wouldn't use it anyway
Bram Moolenaar15d63192011-09-14 16:05:15 +02001197 PtAddEventHandler(gui.vimTextArea,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 Ph_EV_BUT_PRESS | Ph_EV_BUT_RELEASE | Ph_EV_PTR_MOTION_BUTTON,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001199 gui_ph_handle_mouse, NULL);
1200 PtAddEventHandler(gui.vimTextArea, Ph_EV_KEY,
1201 gui_ph_handle_keyboard, NULL);
1202 PtAddCallback(gui.vimTextArea, Pt_CB_GOT_FOCUS,
1203 gui_ph_handle_focus, NULL);
1204 PtAddCallback(gui.vimTextArea, Pt_CB_LOST_FOCUS,
1205 gui_ph_handle_focus, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
1207 /*
1208 * Now that the text area widget has been created, set up the colours,
1209 * which wil call PtSetResource from gui_mch_new_colors
1210 */
1211
1212 /*
1213 * Create the two timers, not as accurate as using the kernel timer
1214 * functions, but good enough
1215 */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001216 gui_ph_timer_cursor = PtCreateWidget(PtTimer, gui.vimWindow, 0, NULL);
1217 if (gui_ph_timer_cursor == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001218 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar15d63192011-09-14 16:05:15 +02001220 gui_ph_timer_timeout = PtCreateWidget(PtTimer, gui.vimWindow, 0, NULL);
1221 if (gui_ph_timer_timeout == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001222 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar15d63192011-09-14 16:05:15 +02001224 PtAddCallback(gui_ph_timer_cursor, Pt_CB_TIMER_ACTIVATE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 gui_ph_handle_timer_cursor, NULL);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001226 PtAddCallback(gui_ph_timer_timeout, Pt_CB_TIMER_ACTIVATE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 gui_ph_handle_timer_timeout, NULL);
1228
1229#ifdef FEAT_MENU
1230 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001231 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, window_size.w, 0);
1232 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS, Pt_ANCHOR_LEFT_RIGHT,
1233 Pt_IS_ANCHORED);
1234 gui.vimToolBarGroup = PtCreateWidget(PtToolbarGroup, gui.vimWindow,
1235 n, args);
1236 if (gui.vimToolBarGroup == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Bram Moolenaar15d63192011-09-14 16:05:15 +02001239 PtAddCallback(gui.vimToolBarGroup, Pt_CB_RESIZE,
1240 gui_ph_handle_menu_resize, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242 n = 0;
1243 flags = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001244 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, window_size.w, 0);
1245 if (! vim_strchr(p_go, GO_MENUS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 flags |= Pt_DELAY_REALIZE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001248 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_TRUE, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001250 gui.vimMenuBar = PtCreateWidget(PtMenuBar, gui.vimToolBarGroup, n, args);
1251 if (gui.vimMenuBar == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001252 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
1254# ifdef FEAT_TOOLBAR
1255 n = 0;
1256
Bram Moolenaar15d63192011-09-14 16:05:15 +02001257 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS,
1258 Pt_ANCHOR_LEFT_RIGHT |Pt_TOP_ANCHORED_TOP, Pt_IS_ANCHORED);
1259 PtSetArg(&args[ n++ ], Pt_ARG_RESIZE_FLAGS, Pt_TRUE,
1260 Pt_RESIZE_Y_AS_REQUIRED);
1261 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, window_size.w, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 flags = Pt_GETS_FOCUS;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001264 if (! vim_strchr(p_go, GO_TOOLBAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 flags |= Pt_DELAY_REALIZE;
1266
Bram Moolenaar15d63192011-09-14 16:05:15 +02001267 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_DELAY_REALIZE, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar15d63192011-09-14 16:05:15 +02001269 gui.vimToolBar = PtCreateWidget(PtToolbar, gui.vimToolBarGroup, n, args);
1270 if (gui.vimToolBar == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
1273 /*
1274 * Size for the toolbar is fetched in gui_mch_show_toolbar, after
1275 * the buttons have been added and the toolbar has resized it's height
1276 * for the buttons to fit
1277 */
1278# endif
1279
1280#endif
1281
Bram Moolenaard2221132011-07-27 14:09:09 +02001282 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283}
1284
1285 int
1286gui_mch_init_check(void)
1287{
Bram Moolenaard2221132011-07-27 14:09:09 +02001288 return (is_photon_available == TRUE) ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289}
1290
1291 int
1292gui_mch_open(void)
1293{
1294 gui.norm_pixel = Pg_BLACK;
1295 gui.back_pixel = Pg_WHITE;
1296
1297 set_normal_colors();
1298
1299 gui_check_colors();
1300 gui.def_norm_pixel = gui.norm_pixel;
1301 gui.def_back_pixel = gui.back_pixel;
1302
1303 highlight_gui_started();
1304
1305 if (gui_win_x != -1 && gui_win_y != -1)
1306 gui_mch_set_winpos(gui_win_x, gui_win_y);
1307
Bram Moolenaar15d63192011-09-14 16:05:15 +02001308 return (PtRealizeWidget(gui.vimWindow) == 0) ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309}
1310
1311 void
1312gui_mch_exit(int rc)
1313{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001314 PtDestroyWidget(gui.vimWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
Bram Moolenaar15d63192011-09-14 16:05:15 +02001316 PxTranslateSet(charset_translate, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
Bram Moolenaar15d63192011-09-14 16:05:15 +02001318 vim_free(gui.event_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320#ifdef USE_PANEL_GROUPS
Bram Moolenaar15d63192011-09-14 16:05:15 +02001321 vim_free(panel_titles);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#endif
1323}
1324
Bram Moolenaar734a8672019-12-02 22:49:38 +01001325////////////////////////////////////////////////////////////////////////////
1326// events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
Bram Moolenaar734a8672019-12-02 22:49:38 +01001328/*
1329 * When no events are available, photon will call this function, working is
1330 * set to FALSE, and the gui_mch_update loop will exit.
1331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001333exit_gui_mch_update(void *data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 *(int *)data = FALSE;
Bram Moolenaard2221132011-07-27 14:09:09 +02001336 return Pt_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337}
1338
1339 void
1340gui_mch_update(void)
1341{
1342 int working = TRUE;
1343
Bram Moolenaar15d63192011-09-14 16:05:15 +02001344 PtAppAddWorkProc(NULL, exit_gui_mch_update, &working);
1345 while ((working == TRUE) && !vim_is_input_buf_full())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 PtProcessEvent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347}
1348
1349 int
1350gui_mch_wait_for_chars(int wtime)
1351{
1352 is_timeout = FALSE;
1353
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01001354 if (wtime >= 0)
1355 PtSetResource(gui_ph_timer_timeout, Pt_ARG_TIMER_INITIAL,
1356 wtime == 0 ? 1 : wtime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar15d63192011-09-14 16:05:15 +02001358 while (1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
1360 PtProcessEvent();
Bram Moolenaar15d63192011-09-14 16:05:15 +02001361 if (input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001363 PtSetResource(gui_ph_timer_timeout, Pt_ARG_TIMER_INITIAL, 0, 0);
Bram Moolenaard2221132011-07-27 14:09:09 +02001364 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001366 else if (is_timeout == TRUE)
Bram Moolenaard2221132011-07-27 14:09:09 +02001367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369}
1370
Bram Moolenaar15d63192011-09-14 16:05:15 +02001371#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372/*
1373 * Put up a file requester.
1374 * Returns the selected name in allocated memory, or NULL for Cancel.
1375 * saving, select file to write
1376 * title title for the window
1377 * default_name default name (well duh!)
1378 * ext not used (extension added)
1379 * initdir initial directory, NULL for current dir
1380 * filter not used (file name filter)
1381 */
1382 char_u *
1383gui_mch_browse(
1384 int saving,
1385 char_u *title,
1386 char_u *default_name,
1387 char_u *ext,
1388 char_u *initdir,
1389 char_u *filter)
1390{
1391 PtFileSelectionInfo_t file;
1392 int flags;
1393 char_u *default_path;
1394 char_u *open_text = NULL;
1395
1396 flags = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001397 memset(&file, 0, sizeof(file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaar15d63192011-09-14 16:05:15 +02001399 default_path = alloc(MAXPATHL + 1 + NAME_MAX + 1);
1400 if (default_path != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001402 if (saving == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001404 // Don't need Pt_FSR_CONFIRM_EXISTING, vim will ask anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 flags |= Pt_FSR_NO_FCHECK;
1406 open_text = "&Save";
1407 }
1408
Bram Moolenaar734a8672019-12-02 22:49:38 +01001409 // combine the directory and filename into a single path
Bram Moolenaar15d63192011-09-14 16:05:15 +02001410 if (initdir == NULL || *initdir == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001412 mch_dirname(default_path, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 initdir = default_path;
1414 }
1415 else
1416 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001417 STRCPY(default_path, initdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 initdir = default_path;
1419 }
1420
Bram Moolenaar15d63192011-09-14 16:05:15 +02001421 if (default_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001423 if (default_path[ STRLEN(default_path) - 1 ] != '/')
1424 STRCAT(default_path, "/");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaar15d63192011-09-14 16:05:15 +02001426 STRCAT(default_path, default_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428
Bram Moolenaar734a8672019-12-02 22:49:38 +01001429 // TODO: add a filter?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 PtFileSelection(
1431 gui.vimWindow,
1432 NULL,
1433 title,
1434 default_path,
1435 NULL,
1436 open_text,
1437 NULL,
1438 NULL,
1439 &file,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001440 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar15d63192011-09-14 16:05:15 +02001442 vim_free(default_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaar15d63192011-09-14 16:05:15 +02001444 if (file.ret == Pt_FSDIALOG_BTN1)
Bram Moolenaard2221132011-07-27 14:09:09 +02001445 return vim_strsave(file.path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
Bram Moolenaard2221132011-07-27 14:09:09 +02001447 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448}
1449#endif
1450
Bram Moolenaar15d63192011-09-14 16:05:15 +02001451#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452static PtWidget_t *gui_ph_dialog_text = NULL;
1453
1454 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001455gui_ph_dialog_close(int button, void *data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 PtModalCtrl_t *modal_ctrl = data;
1458 char_u *dialog_text, *vim_text;
1459
Bram Moolenaar15d63192011-09-14 16:05:15 +02001460 if (gui_ph_dialog_text != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001462 PtGetResource(gui_ph_dialog_text, Pt_ARG_TEXT_STRING, &dialog_text, 0);
1463 PtGetResource(gui_ph_dialog_text, Pt_ARG_POINTER, &vim_text, 0);
1464 STRNCPY(vim_text, dialog_text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466
Bram Moolenaar15d63192011-09-14 16:05:15 +02001467 PtModalUnblock(modal_ctrl, (void *) button);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaard2221132011-07-27 14:09:09 +02001469 return Pt_TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470}
1471
1472 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001473gui_ph_dialog_text_enter(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001475 if (info->reason_subtype == Pt_EDIT_ACTIVATE)
1476 gui_ph_dialog_close(1, data);
Bram Moolenaard2221132011-07-27 14:09:09 +02001477 return Pt_CONTINUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478}
1479
1480 static int
Bram Moolenaar15d63192011-09-14 16:05:15 +02001481gui_ph_dialog_esc(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 PhKeyEvent_t *key;
1484
Bram Moolenaar15d63192011-09-14 16:05:15 +02001485 key = PhGetData(info->event);
1486 if ((key->key_flags & Pk_KF_Cap_Valid) && (key->key_cap == Pk_Escape))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001488 gui_ph_dialog_close(0, data);
Bram Moolenaard2221132011-07-27 14:09:09 +02001489 return Pt_CONSUME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
Bram Moolenaard2221132011-07-27 14:09:09 +02001491 return Pt_PROCESS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492}
1493
1494 int
1495gui_mch_dialog(
1496 int type,
1497 char_u *title,
1498 char_u *message,
1499 char_u *buttons,
1500 int default_button,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01001501 char_u *textfield,
1502 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 char_u *str;
1505 char_u **button_array;
1506 char_u *buttons_copy;
1507
1508 int button_count;
1509 int i, len;
1510 int dialog_result = -1;
1511
Bram Moolenaar734a8672019-12-02 22:49:38 +01001512 // FIXME: the vertical option in guioptions is blatantly ignored
1513 // FIXME: so is the type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
1515 button_count = len = i = 0;
1516
Bram Moolenaar15d63192011-09-14 16:05:15 +02001517 if (buttons == NULL || *buttons == NUL)
Bram Moolenaard2221132011-07-27 14:09:09 +02001518 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar734a8672019-12-02 22:49:38 +01001520 // There is one less separator than buttons, so bump up the button count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 button_count = 1;
1522
Bram Moolenaar734a8672019-12-02 22:49:38 +01001523 // Count string length and number of separators
Bram Moolenaar15d63192011-09-14 16:05:15 +02001524 for (str = buttons; *str; str++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 len++;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001527 if (*str == DLG_BUTTON_SEP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 button_count++;
1529 }
1530
Bram Moolenaar15d63192011-09-14 16:05:15 +02001531 if (title == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 title = "Vim";
1533
Bram Moolenaar15d63192011-09-14 16:05:15 +02001534 buttons_copy = alloc(len + 1);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001535 button_array = ALLOC_MULT(char_u *, button_count);
Bram Moolenaar15d63192011-09-14 16:05:15 +02001536 if (buttons_copy != NULL && button_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001538 STRCPY(buttons_copy, buttons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
1540 /*
1541 * Convert DLG_BUTTON_SEP into NUL's and fill in
1542 * button_array with the pointer to each NUL terminated string
1543 */
1544 str = buttons_copy;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001545 for (i = 0; i < button_count; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 {
1547 button_array[ i ] = str;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001548 for (; *str; str++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001550 if (*str == DLG_BUTTON_SEP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 *str++ = NUL;
1553 break;
1554 }
1555 }
1556 }
1557#ifndef FEAT_GUI_TEXTDIALOG
1558 dialog_result = PtAlert(
1559 gui.vimWindow, NULL,
1560 title,
1561 NULL,
1562 message, NULL,
1563 button_count, (const char **) button_array, NULL,
Bram Moolenaar15d63192011-09-14 16:05:15 +02001564 default_button, 0, Pt_MODAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#else
Bram Moolenaar734a8672019-12-02 22:49:38 +01001566 // Writing the dialog ourselves lets us add extra features, like
1567 // trapping the escape key and returning 0 to vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569 int n;
1570 PtArg_t args[5];
1571 PtWidget_t *dialog, *pane;
1572 PtModalCtrl_t modal_ctrl;
1573 PtDialogInfo_t di;
1574
Bram Moolenaar15d63192011-09-14 16:05:15 +02001575 memset(&di, 0, sizeof(di));
1576 memset(&modal_ctrl, 0, sizeof(modal_ctrl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001579 PtSetArg(&args[n++], Pt_ARG_GROUP_ROWS_COLS, 0, 0);
1580 PtSetArg(&args[n++], Pt_ARG_WIDTH, 350, 0);
1581 PtSetArg(&args[n++], Pt_ARG_GROUP_ORIENTATION,
1582 Pt_GROUP_VERTICAL, 0);
1583 PtSetArg(&args[n++], Pt_ARG_GROUP_FLAGS,
1584 Pt_TRUE, Pt_GROUP_NO_KEYS | Pt_GROUP_STRETCH_HORIZONTAL);
1585 PtSetArg(&args[n++], Pt_ARG_CONTAINER_FLAGS, Pt_FALSE, Pt_TRUE);
1586 pane = PtCreateWidget(PtGroup, NULL, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001589 PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, message, 0);
1590 PtCreateWidget(PtLabel, pane, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
Bram Moolenaar15d63192011-09-14 16:05:15 +02001592 if (textfield != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001595 PtSetArg(&args[n++], Pt_ARG_MAX_LENGTH, IOSIZE - 1, 0);
1596 PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, textfield, 0);
1597 PtSetArg(&args[n++], Pt_ARG_POINTER, textfield, 0);
1598 gui_ph_dialog_text = PtCreateWidget(PtText, pane, n, args);
1599 PtAddCallback(gui_ph_dialog_text, Pt_CB_ACTIVATE,
1600 gui_ph_dialog_text_enter, &modal_ctrl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602
1603 di.parent = gui.vimWindow;
1604 di.pane = pane;
1605 di.title = title;
1606 di.buttons = (const char **) button_array;
1607 di.nbtns = button_count;
1608 di.def_btn = default_button;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001609 // This is just to give the dialog the close button.
1610 // We check for the Escape key ourselves and return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 di.esc_btn = button_count;
1612 di.callback = gui_ph_dialog_close;
1613 di.data = &modal_ctrl;
1614
Bram Moolenaar15d63192011-09-14 16:05:15 +02001615 dialog = PtCreateDialog(&di);
1616 PtAddFilterCallback(dialog, Ph_EV_KEY,
1617 gui_ph_dialog_esc, &modal_ctrl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar15d63192011-09-14 16:05:15 +02001619 if (gui_ph_dialog_text != NULL)
1620 PtGiveFocus(gui_ph_dialog_text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar734a8672019-12-02 22:49:38 +01001622 // Open dialog, block the vim window and wait for the dialog to close
Bram Moolenaar15d63192011-09-14 16:05:15 +02001623 PtRealizeWidget(dialog);
1624 PtMakeModal(dialog, Ph_CURSOR_NOINPUT, Ph_CURSOR_DEFAULT_COLOR);
1625 dialog_result = (int) PtModalBlock(&modal_ctrl, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar15d63192011-09-14 16:05:15 +02001627 PtDestroyWidget(dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 gui_ph_dialog_text = NULL;
1629 }
1630#endif
1631 }
1632
Bram Moolenaar15d63192011-09-14 16:05:15 +02001633 vim_free(button_array);
1634 vim_free(buttons_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaard2221132011-07-27 14:09:09 +02001636 return dialog_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637}
1638#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01001639////////////////////////////////////////////////////////////////////////////
1640// window size/position/state
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642 int
1643gui_mch_get_winpos(int *x, int *y)
1644{
1645 PhPoint_t *pos;
1646
Bram Moolenaar15d63192011-09-14 16:05:15 +02001647 pos = PtWidgetPos(gui.vimWindow, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 *x = pos->x;
1650 *y = pos->y;
1651
Bram Moolenaard2221132011-07-27 14:09:09 +02001652 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653}
1654
1655 void
1656gui_mch_set_winpos(int x, int y)
1657{
1658 PhPoint_t pos = { x, y };
1659
Bram Moolenaar15d63192011-09-14 16:05:15 +02001660 PtSetResource(gui.vimWindow, Pt_ARG_POS, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661}
1662
1663 void
1664gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001665 int min_width, int min_height, int base_width, int base_height,
1666 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
1668 PhDim_t window_size = { width, height };
1669 PhDim_t min_size = { min_width, min_height };
1670
1671#ifdef USE_PANEL_GROUP
1672 window_size.w += pg_margin_left + pg_margin_right;
1673 window_size.h += pg_margin_top + pg_margin_bottom;
1674#endif
1675
Bram Moolenaar15d63192011-09-14 16:05:15 +02001676 PtSetResource(gui.vimWindow, Pt_ARG_MINIMUM_DIM, &min_size, 0);
1677 PtSetResource(gui.vimWindow, Pt_ARG_DIM, &window_size, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar15d63192011-09-14 16:05:15 +02001679 if (! PtWidgetIsRealized(gui.vimWindow))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 gui_ph_resize_container();
1681}
1682
1683/*
1684 * Return the amount of screen space that hasn't been allocated (such as
1685 * by the shelf).
1686 */
1687 void
1688gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
1689{
1690 PhRect_t console;
1691
Bram Moolenaar15d63192011-09-14 16:05:15 +02001692 PhWindowQueryVisible(Ph_QUERY_WORKSPACE, 0,
1693 PhInputGroup(NULL), &console);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695 *screen_w = console.lr.x - console.ul.x + 1;
1696 *screen_h = console.lr.y - console.ul.y + 1;
1697}
1698
1699 void
1700gui_mch_iconify(void)
1701{
1702 PhWindowEvent_t event;
1703
Bram Moolenaar15d63192011-09-14 16:05:15 +02001704 memset(&event, 0, sizeof (event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 event.event_f = Ph_WM_HIDE;
1706 event.event_state = Ph_WM_EVSTATE_HIDE;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001707 event.rid = PtWidgetRid(gui.vimWindow);
1708 PtForwardWindowEvent(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709}
1710
1711#if defined(FEAT_EVAL) || defined(PROTO)
1712/*
1713 * Bring the Vim window to the foreground.
1714 */
1715 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001716gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 PhWindowEvent_t event;
1719
Bram Moolenaar15d63192011-09-14 16:05:15 +02001720 memset(&event, 0, sizeof (event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 event.event_f = Ph_WM_TOFRONT;
1722 event.event_state = Ph_WM_EVSTATE_FFRONT;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001723 event.rid = PtWidgetRid(gui.vimWindow);
1724 PtForwardWindowEvent(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725}
1726#endif
1727
1728 void
1729gui_mch_settitle(char_u *title, char_u *icon)
1730{
1731#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02001732 gui_ph_pg_set_buffer_num(curwin->w_buffer->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02001734 PtSetResource(gui.vimWindow, Pt_ARG_WINDOW_TITLE, title, 0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001735 // Not sure what to do with the icon text, set balloon text somehow?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736}
1737
Bram Moolenaar734a8672019-12-02 22:49:38 +01001738////////////////////////////////////////////////////////////////////////////
1739// Scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
1741 void
1742gui_mch_set_scrollbar_thumb(scrollbar_T *sb, int val, int size, int max)
1743{
1744 int n = 0;
1745 PtArg_t args[3];
1746
Bram Moolenaar15d63192011-09-14 16:05:15 +02001747 PtSetArg(&args[ n++ ], Pt_ARG_MAXIMUM, max, 0);
1748 PtSetArg(&args[ n++ ], Pt_ARG_SLIDER_SIZE, size, 0);
1749 PtSetArg(&args[ n++ ], Pt_ARG_GAUGE_VALUE, val, 0);
1750 PtSetResources(sb->id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751}
1752
1753 void
1754gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1755{
1756 PhArea_t area = {{ x, y }, { w, h }};
1757
Bram Moolenaar15d63192011-09-14 16:05:15 +02001758 PtSetResource(sb->id, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759}
1760
1761 void
1762gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1763{
1764 int n = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001765// int anchor_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 PtArg_t args[4];
1767
1768 /*
1769 * Stop the scrollbar from being realized when the parent
1770 * is realized, so it can be explicitly realized by vim.
1771 *
1772 * Also, don't let the scrollbar get focus
1773 */
Bram Moolenaar15d63192011-09-14 16:05:15 +02001774 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_DELAY_REALIZE,
1775 Pt_DELAY_REALIZE | Pt_GETS_FOCUS);
1776 PtSetArg(&args[ n++ ], Pt_ARG_SCROLLBAR_FLAGS, Pt_SCROLLBAR_SHOW_ARROWS, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#if 0
Bram Moolenaar734a8672019-12-02 22:49:38 +01001778 // Don't need this anchoring for the scrollbars
Bram Moolenaar15d63192011-09-14 16:05:15 +02001779 if (orient == SBAR_HORIZ)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
1781 anchor_flags = Pt_BOTTOM_ANCHORED_BOTTOM |
1782 Pt_LEFT_ANCHORED_LEFT | Pt_RIGHT_ANCHORED_RIGHT;
1783 }
1784 else
1785 {
1786 anchor_flags = Pt_BOTTOM_ANCHORED_BOTTOM | Pt_TOP_ANCHORED_TOP;
Bram Moolenaar15d63192011-09-14 16:05:15 +02001787 if (sb->wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02001789 if (sb == &sb->wp->w_scrollbars[ SBAR_LEFT ])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 anchor_flags |= Pt_LEFT_ANCHORED_LEFT;
1791 else
1792 anchor_flags |= Pt_RIGHT_ANCHORED_RIGHT;
1793 }
1794 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02001795 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS, anchor_flags, Pt_IS_ANCHORED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02001797 PtSetArg(&args[ n++ ], Pt_ARG_ORIENTATION,
1798 (orient == SBAR_HORIZ) ? Pt_HORIZONTAL : Pt_VERTICAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02001800 sb->id = PtCreateWidget(PtScrollbar, gui.vimPanelGroup, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001802 sb->id = PtCreateWidget(PtScrollbar, gui.vimContainer, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#endif
1804
Bram Moolenaar15d63192011-09-14 16:05:15 +02001805 PtAddCallback(sb->id, Pt_CB_SCROLLBAR_MOVE, gui_ph_handle_scrollbar, sb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806}
1807
1808 void
1809gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
1810{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001811 if (flag != 0)
1812 PtRealizeWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001814 PtUnrealizeWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815}
1816
1817 void
1818gui_mch_destroy_scrollbar(scrollbar_T *sb)
1819{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001820 PtDestroyWidget(sb->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 sb->id = NULL;
1822}
1823
Bram Moolenaar734a8672019-12-02 22:49:38 +01001824////////////////////////////////////////////////////////////////////////////
1825// Mouse functions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
1827#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001828// The last set mouse pointer shape is remembered, to be used when it goes
1829// from hidden to not hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830static int last_shape = 0;
1831
Bram Moolenaar734a8672019-12-02 22:49:38 +01001832// Table for shape IDs. Keep in sync with the mshape_names[] table in
1833// misc2.c!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834static int mshape_ids[] =
1835{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001836 Ph_CURSOR_POINTER, // arrow
1837 Ph_CURSOR_NONE, // blank
1838 Ph_CURSOR_INSERT, // beam
1839 Ph_CURSOR_DRAG_VERTICAL, // updown
1840 Ph_CURSOR_DRAG_VERTICAL, // udsizing
1841 Ph_CURSOR_DRAG_HORIZONTAL, // leftright
1842 Ph_CURSOR_DRAG_HORIZONTAL, // lrsizing
1843 Ph_CURSOR_WAIT, // busy
1844 Ph_CURSOR_DONT, // no
1845 Ph_CURSOR_CROSSHAIR, // crosshair
1846 Ph_CURSOR_FINGER, // hand1
1847 Ph_CURSOR_FINGER, // hand2
1848 Ph_CURSOR_FINGER, // pencil
1849 Ph_CURSOR_QUESTION_POINT, // question
1850 Ph_CURSOR_POINTER, // right-arrow
1851 Ph_CURSOR_POINTER, // up-arrow
1852 Ph_CURSOR_POINTER // last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853};
1854
1855 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001856mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int id;
1859
1860 if (!gui.in_use)
1861 return;
1862
1863 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar15d63192011-09-14 16:05:15 +02001864 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE, Ph_CURSOR_NONE,
1865 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 else
1867 {
1868 if (shape >= MSHAPE_NUMBERED)
1869 id = Ph_CURSOR_POINTER;
1870 else
1871 id = mshape_ids[shape];
1872
Bram Moolenaar15d63192011-09-14 16:05:15 +02001873 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE, id, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875 if (shape != MSHAPE_HIDE)
1876 last_shape = shape;
1877}
1878#endif
1879
1880 void
1881gui_mch_mousehide(int hide)
1882{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001883 if (gui.pointer_hidden != hide)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
1885 gui.pointer_hidden = hide;
1886#ifdef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001887 if (hide)
1888 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE,
1889 Ph_CURSOR_NONE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001891 mch_set_mouse_shape(last_shape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02001893 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_TYPE,
1894 (hide == MOUSE_SHOW) ? GUI_PH_MOUSE_TYPE : Ph_CURSOR_NONE,
1895 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896#endif
1897 }
1898}
1899
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001900 void
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001901gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
1903 PhCursorInfo_t info;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001904 short ix, iy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar734a8672019-12-02 22:49:38 +01001906 // FIXME: does this return the correct position,
1907 // with respect to the border?
Bram Moolenaar15d63192011-09-14 16:05:15 +02001908 PhQueryCursor(PhInputGroup(NULL), &info);
1909 PtGetAbsPosition(gui.vimTextArea , &ix, &iy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001911 *x = info.pos.x - ix;
1912 *y = info.pos.y - iy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913}
1914
1915 void
1916gui_mch_setmouse(int x, int y)
1917{
1918 short abs_x, abs_y;
1919
Bram Moolenaar15d63192011-09-14 16:05:15 +02001920 PtGetAbsPosition(gui.vimTextArea, &abs_x, &abs_y);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001921 // Add the border offset?
Bram Moolenaar15d63192011-09-14 16:05:15 +02001922 PhMoveCursorAbs(PhInputGroup(NULL), abs_x + x, abs_y + y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923}
1924
Bram Moolenaar734a8672019-12-02 22:49:38 +01001925////////////////////////////////////////////////////////////////////////////
1926// Colours
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928/*
1929 * Return the RGB value of a pixel as a long.
1930 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001931 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932gui_mch_get_rgb(guicolor_T pixel)
1933{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001934 return (guicolor_T)(PgRGB(PgRedValue(pixel),
1935 PgGreenValue(pixel), PgBlueValue(pixel)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936}
1937
1938 void
1939gui_mch_new_colors(void)
1940{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001941#if 0 // Don't bother changing the cursor colour
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 short color_diff;
1943
1944 /*
1945 * If there isn't enough difference between the background colour and
1946 * the mouse pointer colour then change the mouse pointer colour
1947 */
1948 color_diff = gui_get_lightness(gui_ph_mouse_color)
1949 - gui_get_lightness(gui.back_pixel);
1950
Bram Moolenaar15d63192011-09-14 16:05:15 +02001951 if (abs(color_diff) < 64)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 short r, g, b;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001954 // not a great algorithm...
Bram Moolenaar15d63192011-09-14 16:05:15 +02001955 r = PgRedValue(gui_ph_mouse_color) ^ 255;
1956 g = PgGreenValue(gui_ph_mouse_color) ^ 255;
1957 b = PgBlueValue(gui_ph_mouse_color) ^ 255;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959#ifndef FEAT_MOUSESHAPE
Bram Moolenaar15d63192011-09-14 16:05:15 +02001960 gui_ph_mouse_color = PgRGB(r, g, b);
1961 PtSetResource(gui.vimTextArea, Pt_ARG_CURSOR_COLOR,
1962 gui_ph_mouse_color, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963#endif
1964 }
1965#endif
1966
Bram Moolenaar15d63192011-09-14 16:05:15 +02001967 PtSetResource(gui.vimTextArea, Pt_ARG_FILL_COLOR, gui.back_pixel, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968}
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00001971 * This should be split out into a separate file,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 * every port does basically the same thing.
1973 *
1974 * This is the gui_w32.c version (i think..)
1975 * Return INVALCOLOR when failed.
1976 */
1977
1978 guicolor_T
1979gui_mch_get_color(char_u *name)
1980{
Bram Moolenaarab302212016-04-26 20:59:29 +02001981 return gui_get_color_cmn(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982}
1983
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001984 guicolor_T
1985gui_mch_get_rgb_color(int r, int g, int b)
1986{
1987 return gui_get_rgb_color_cmn(r, g, b);
1988}
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 void
1991gui_mch_set_fg_color(guicolor_T color)
1992{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001993 PgSetTextColor(color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994}
1995
1996 void
1997gui_mch_set_bg_color(guicolor_T color)
1998{
Bram Moolenaar15d63192011-09-14 16:05:15 +02001999 PgSetFillColor(color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000}
2001
2002 void
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002003gui_mch_set_sp_color(guicolor_T color)
2004{
2005}
2006
2007 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008gui_mch_invert_rectangle(int row, int col, int nr, int nc)
2009{
2010 PhRect_t rect;
2011
Bram Moolenaar15d63192011-09-14 16:05:15 +02002012 rect.ul.x = FILL_X(col);
2013 rect.ul.y = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaar734a8672019-12-02 22:49:38 +01002015 // FIXME: This has an off by one pixel problem
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 rect.lr.x = rect.ul.x + nc * gui.char_width;
2017 rect.lr.y = rect.ul.y + nr * gui.char_height;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002018 if (nc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 rect.lr.x -= 1;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002020 if (nr > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 rect.lr.y -= 1;
2022
2023 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002024 PgSetDrawMode(Pg_DrawModeDSTINVERT);
2025 PgDrawRect(&rect, Pg_DRAW_FILL);
2026 PgSetDrawMode(Pg_DrawModeSRCCOPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 DRAW_END;
2028}
2029
2030 void
2031gui_mch_clear_block(int row1, int col1, int row2, int col2)
2032{
2033 PhRect_t block = {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002034 { FILL_X(col1), FILL_Y(row1) },
2035 { FILL_X(col2 + 1) - 1, FILL_Y(row2 + 1) - 1}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 };
2037
2038 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002039 gui_mch_set_bg_color(gui.back_pixel);
2040 PgDrawRect(&block, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 DRAW_END;
2042}
2043
2044 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002045gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 PhRect_t text_rect = {
2048 { gui.border_width, gui.border_width },
2049 { Columns * gui.char_width + gui.border_width - 1 ,
2050 Rows * gui.char_height + gui.border_width - 1 }
2051 };
2052
Bram Moolenaar15d63192011-09-14 16:05:15 +02002053 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 return;
2055
2056 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002057 gui_mch_set_bg_color(gui.back_pixel);
2058 PgDrawRect(&text_rect, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 DRAW_END;
2060}
2061
2062 void
2063gui_mch_delete_lines(int row, int num_lines)
2064{
2065 PhRect_t rect;
2066 PhPoint_t delta;
2067
Bram Moolenaar15d63192011-09-14 16:05:15 +02002068 rect.ul.x = FILL_X(gui.scroll_region_left);
2069 rect.ul.y = FILL_Y(row + num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaar15d63192011-09-14 16:05:15 +02002071 rect.lr.x = FILL_X(gui.scroll_region_right + 1) - 1;
2072 rect.lr.y = FILL_Y(gui.scroll_region_bot + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
Bram Moolenaar15d63192011-09-14 16:05:15 +02002074 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
2075 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
2076 PhTranslateRect(&rect, &gui_ph_raw_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 delta.x = 0;
2079 delta.y = -num_lines * gui.char_height;
2080
2081 PgFlush();
2082
Bram Moolenaar15d63192011-09-14 16:05:15 +02002083 PhBlit(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)), &rect, &delta);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085 gui_clear_block(
2086 gui.scroll_region_bot - num_lines + 1,
2087 gui.scroll_region_left,
2088 gui.scroll_region_bot,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002089 gui.scroll_region_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090}
2091
2092 void
2093gui_mch_insert_lines(int row, int num_lines)
2094{
2095 PhRect_t rect;
2096 PhPoint_t delta;
2097
Bram Moolenaar15d63192011-09-14 16:05:15 +02002098 rect.ul.x = FILL_X(gui.scroll_region_left);
2099 rect.ul.y = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
Bram Moolenaar15d63192011-09-14 16:05:15 +02002101 rect.lr.x = FILL_X(gui.scroll_region_right + 1) - 1;
2102 rect.lr.y = FILL_Y(gui.scroll_region_bot - num_lines + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
Bram Moolenaar15d63192011-09-14 16:05:15 +02002104 PtWidgetOffset(gui.vimTextArea, &gui_ph_raw_offset);
2105 PhTranslatePoint(&gui_ph_raw_offset, PtWidgetPos(gui.vimTextArea, NULL));
2106 PhTranslateRect(&rect, &gui_ph_raw_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 delta.x = 0;
2109 delta.y = num_lines * gui.char_height;
2110
2111 PgFlush();
2112
Bram Moolenaar15d63192011-09-14 16:05:15 +02002113 PhBlit(PtWidgetRid(PtFindDisjoint(gui.vimTextArea)) , &rect, &delta);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar15d63192011-09-14 16:05:15 +02002115 gui_clear_block(row, gui.scroll_region_left,
2116 row + num_lines - 1, gui.scroll_region_right);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117}
2118
2119 void
2120gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
2121{
2122 static char *utf8_buffer = NULL;
2123 static int utf8_len = 0;
2124
Bram Moolenaar15d63192011-09-14 16:05:15 +02002125 PhPoint_t pos = { TEXT_X(col), TEXT_Y(row) };
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 PhRect_t rect;
2127
Bram Moolenaar15d63192011-09-14 16:05:15 +02002128 if (is_ignore_draw == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 return;
2130
2131 DRAW_START;
2132
Bram Moolenaar15d63192011-09-14 16:05:15 +02002133 if (!(flags & DRAW_TRANSP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
2135 PgDrawIRect(
Bram Moolenaar15d63192011-09-14 16:05:15 +02002136 FILL_X(col), FILL_Y(row),
2137 FILL_X(col + len) - 1, FILL_Y(row + 1) - 1,
2138 Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
2140
Bram Moolenaar15d63192011-09-14 16:05:15 +02002141 if (flags & DRAW_UNDERL)
2142 PgSetUnderline(gui.norm_pixel, Pg_TRANSPARENT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
Bram Moolenaar13505972019-01-24 15:04:48 +01002144 if (charset_translate != NULL && enc_utf8 == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 int src_taken, dst_made;
2147
Bram Moolenaar734a8672019-12-02 22:49:38 +01002148 // Use a static buffer to avoid large amounts of de/allocations
Bram Moolenaar15d63192011-09-14 16:05:15 +02002149 if (utf8_len < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002151 utf8_buffer = realloc(utf8_buffer, len * MB_LEN_MAX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 utf8_len = len;
2153 }
2154
2155 PxTranslateToUTF(
2156 charset_translate,
2157 s,
2158 len,
2159 &src_taken,
2160 utf8_buffer,
2161 utf8_len,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002162 &dst_made);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 s = utf8_buffer;
2164 len = dst_made;
2165 }
2166
Bram Moolenaar15d63192011-09-14 16:05:15 +02002167 PgDrawText(s, len, &pos, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
Bram Moolenaar15d63192011-09-14 16:05:15 +02002169 if (flags & DRAW_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002171 // FIXME: try and only calculate these values once...
Bram Moolenaar15d63192011-09-14 16:05:15 +02002172 rect.ul.x = FILL_X(col) + 1;
2173 rect.ul.y = FILL_Y(row);
2174 rect.lr.x = FILL_X(col + len) - 1;
2175 rect.lr.y = FILL_Y(row + 1) - 1;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002176 // PgSetUserClip(NULL) causes the scrollbar to not redraw...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#if 0
2178 pos.x++;
2179
Bram Moolenaar15d63192011-09-14 16:05:15 +02002180 PgSetUserClip(&rect);
2181 PgDrawText(s, len, &pos, 0);
2182 PgSetUserClip(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002184 rect.lr.y -= (p_linespace + 1) / 2;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002185 // XXX: DrawTextArea doesn't work with phditto
Bram Moolenaar15d63192011-09-14 16:05:15 +02002186 PgDrawTextArea(s, len, &rect, Pg_TEXT_BOTTOM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#endif
2188 }
2189
Bram Moolenaar15d63192011-09-14 16:05:15 +02002190 if (flags & DRAW_UNDERL)
2191 PgSetUnderline(Pg_TRANSPARENT, Pg_TRANSPARENT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 DRAW_END;
2194}
2195
Bram Moolenaar734a8672019-12-02 22:49:38 +01002196////////////////////////////////////////////////////////////////////////////
2197// Cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199 void
2200gui_mch_draw_hollow_cursor(guicolor_T color)
2201{
2202 PhRect_t r;
2203
Bram Moolenaar734a8672019-12-02 22:49:38 +01002204 // FIXME: Double width characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaar15d63192011-09-14 16:05:15 +02002206 r.ul.x = FILL_X(gui.col);
2207 r.ul.y = FILL_Y(gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 r.lr.x = r.ul.x + gui.char_width - 1;
2209 r.lr.y = r.ul.y + gui.char_height - 1;
2210
2211 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002212 PgSetStrokeColor(color);
2213 PgDrawRect(&r, Pg_DRAW_STROKE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 DRAW_END;
2215}
2216
2217 void
2218gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
2219{
2220 PhRect_t r;
2221
Bram Moolenaar15d63192011-09-14 16:05:15 +02002222 r.ul.x = FILL_X(gui.col);
2223 r.ul.y = FILL_Y(gui.row) + gui.char_height - h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 r.lr.x = r.ul.x + w - 1;
2225 r.lr.y = r.ul.y + h - 1;
2226
2227 DRAW_START;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002228 gui_mch_set_bg_color(color);
2229 PgDrawRect(&r, Pg_DRAW_FILL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 DRAW_END;
2231}
2232
Bram Moolenaar703a8042016-06-04 16:24:32 +02002233 int
2234gui_mch_is_blinking(void)
2235{
2236 return blink_state != BLINK_NONE;
2237}
2238
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02002239 int
2240gui_mch_is_blink_off(void)
2241{
2242 return blink_state == BLINK_OFF;
2243}
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 void
2246gui_mch_set_blinking(long wait, long on, long off)
2247{
2248 blink_waittime = wait;
2249 blink_ontime = on;
2250 blink_offtime = off;
2251}
2252
2253 void
2254gui_mch_start_blink(void)
2255{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002256 // Only turn on the timer on if none of the times are zero
Bram Moolenaar15d63192011-09-14 16:05:15 +02002257 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002259 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL,
2260 blink_waittime, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 blink_state = BLINK_ON;
2262 gui_update_cursor(TRUE, FALSE);
2263 }
2264}
2265
2266 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002267gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002269 PtSetResource(gui_ph_timer_cursor, Pt_ARG_TIMER_INITIAL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002271 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 gui_update_cursor(TRUE, FALSE);
2273
2274 blink_state = BLINK_NONE;
2275}
2276
Bram Moolenaar734a8672019-12-02 22:49:38 +01002277////////////////////////////////////////////////////////////////////////////
2278// miscellaneous functions
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280 void
2281gui_mch_beep(void)
2282{
2283 PtBeep();
2284}
2285
2286 void
2287gui_mch_flash(int msec)
2288{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002289 PgSetFillXORColor(Pg_BLACK, Pg_WHITE);
2290 PgSetDrawMode(Pg_DRAWMODE_XOR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 gui_mch_clear_all();
2292 gui_mch_flush();
2293
Bram Moolenaar15d63192011-09-14 16:05:15 +02002294 ui_delay((long) msec, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296 gui_mch_clear_all();
Bram Moolenaar15d63192011-09-14 16:05:15 +02002297 PgSetDrawMode(Pg_DRAWMODE_OPAQUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 gui_mch_flush();
2299}
2300
2301 void
2302gui_mch_flush(void)
2303{
2304 PgFlush();
2305}
2306
2307 void
2308gui_mch_set_text_area_pos(int x, int y, int w, int h)
2309{
2310 PhArea_t area = {{x, y}, {w, h}};
2311
Bram Moolenaar15d63192011-09-14 16:05:15 +02002312 PtSetResource(gui.vimTextArea, Pt_ARG_AREA, &area, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313}
2314
2315 int
2316gui_mch_haskey(char_u *name)
2317{
2318 int i;
2319
2320 for (i = 0; special_keys[i].key_sym != 0; i++)
2321 if (name[0] == special_keys[i].vim_code0 &&
2322 name[1] == special_keys[i].vim_code1)
Bram Moolenaard2221132011-07-27 14:09:09 +02002323 return OK;
2324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325}
2326
Bram Moolenaar734a8672019-12-02 22:49:38 +01002327////////////////////////////////////////////////////////////////////////////
2328// Menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330#ifdef FEAT_TOOLBAR
2331#include "toolbar.phi"
2332
2333static PhImage_t *gui_ph_toolbar_images[] = {
2334 &tb_new_phi,
2335 &tb_open_phi,
2336 &tb_save_phi,
2337 &tb_undo_phi,
2338 &tb_redo_phi,
2339 &tb_cut_phi,
2340 &tb_copy_phi,
2341 &tb_paste_phi,
2342 &tb_print_phi,
2343 &tb_help_phi,
2344 &tb_find_phi,
2345 &tb_save_all_phi,
2346 &tb_save_session_phi,
2347 &tb_new_session_phi,
2348 &tb_load_session_phi,
2349 &tb_macro_phi,
2350 &tb_replace_phi,
2351 &tb_close_phi,
2352 &tb_maximize_phi,
2353 &tb_minimize_phi,
2354 &tb_split_phi,
2355 &tb_shell_phi,
2356 &tb_find_prev_phi,
2357 &tb_find_next_phi,
2358 &tb_find_help_phi,
2359 &tb_make_phi,
2360 &tb_jump_phi,
2361 &tb_ctags_phi,
2362 &tb_vsplit_phi,
2363 &tb_maxwidth_phi,
2364 &tb_minwidth_phi
2365};
2366
2367static PhImage_t *
Bram Moolenaar15d63192011-09-14 16:05:15 +02002368gui_ph_toolbar_load_icon(char_u *iconfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 static PhImage_t external_icon;
2371 PhImage_t *temp_phi = NULL;
2372
Bram Moolenaar15d63192011-09-14 16:05:15 +02002373 temp_phi = PxLoadImage(iconfile, NULL);
2374 if (temp_phi != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002376 // The label widget will free the image/palette/etc. for us when
2377 // it's destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 temp_phi->flags |= Ph_RELEASE_IMAGE_ALL;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002379 memcpy(&external_icon, temp_phi, sizeof(external_icon));
2380 free(temp_phi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382 temp_phi = &external_icon;
2383 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002384 return temp_phi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385}
2386
2387/*
2388 * This returns either a builtin icon image, an external image or NULL
2389 * if it can't find either. The caller can't and doesn't need to try and
2390 * free() the returned image, and it can't store the image pointer.
2391 * (When setting the Pt_ARG_LABEL_IMAGE resource, the contents of the
2392 * PhImage_t are copied, and the original PhImage_t aren't needed anymore).
2393 */
2394static PhImage_t *
Bram Moolenaar15d63192011-09-14 16:05:15 +02002395gui_ph_toolbar_find_icon(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 char_u full_pathname[ MAXPATHL + 1 ];
2398 PhImage_t *icon = NULL;
2399
Bram Moolenaar15d63192011-09-14 16:05:15 +02002400 if (menu->icon_builtin == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002402 if (menu->iconfile != NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002403 // TODO: use gui_find_iconfile()
Bram Moolenaar15d63192011-09-14 16:05:15 +02002404 icon = gui_ph_toolbar_load_icon(menu->iconfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaar734a8672019-12-02 22:49:38 +01002406 // TODO: Restrict loading to just .png? Search for any format?
Bram Moolenaar15d63192011-09-14 16:05:15 +02002407 if ((icon == NULL) &&
2408 ((gui_find_bitmap(menu->name, full_pathname, "gif") == OK) ||
2409 (gui_find_bitmap(menu->name, full_pathname, "png") == OK)))
2410 icon = gui_ph_toolbar_load_icon(full_pathname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar15d63192011-09-14 16:05:15 +02002412 if (icon != NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002413 return icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415
Bram Moolenaar15d63192011-09-14 16:05:15 +02002416 if (menu->iconidx >= 0 &&
2417 (menu->iconidx < ARRAY_LENGTH(gui_ph_toolbar_images)))
Bram Moolenaard2221132011-07-27 14:09:09 +02002418 return gui_ph_toolbar_images[menu->iconidx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaard2221132011-07-27 14:09:09 +02002420 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421}
2422#endif
2423
Bram Moolenaar15d63192011-09-14 16:05:15 +02002424#if defined(FEAT_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 void
2426gui_mch_enable_menu(int flag)
2427{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002428 if (flag != 0)
2429 PtRealizeWidget(gui.vimMenuBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002431 PtUnrealizeWidget(gui.vimMenuBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432}
2433
2434 void
2435gui_mch_set_menu_pos(int x, int y, int w, int h)
2436{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002437 // Nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438}
2439
Bram Moolenaar734a8672019-12-02 22:49:38 +01002440/*
2441 * Change the position of a menu button in the parent
2442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 static void
Bram Moolenaar15d63192011-09-14 16:05:15 +02002444gui_ph_position_menu(PtWidget_t *widget, int priority)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446 PtWidget_t *traverse;
2447 vimmenu_T *menu;
2448
Bram Moolenaar15d63192011-09-14 16:05:15 +02002449 traverse = PtWidgetChildBack(PtWidgetParent(widget));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar734a8672019-12-02 22:49:38 +01002451 // Iterate through the list of widgets in traverse, until
2452 // we find the position we want to insert our widget into
2453 // TODO: traverse from front to back, possible speedup?
Bram Moolenaar15d63192011-09-14 16:05:15 +02002454 while (traverse != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002456 PtGetResource(traverse, Pt_ARG_POINTER, &menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaar15d63192011-09-14 16:05:15 +02002458 if (menu != NULL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 priority < menu->priority &&
Bram Moolenaar15d63192011-09-14 16:05:15 +02002460 widget != traverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002462 // Insert the widget before the current traverse widget
Bram Moolenaar15d63192011-09-14 16:05:15 +02002463 PtWidgetInsert(widget, traverse, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 return;
2465 }
2466
Bram Moolenaar15d63192011-09-14 16:05:15 +02002467 traverse = PtWidgetBrotherInFront(traverse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469}
2470
Bram Moolenaar734a8672019-12-02 22:49:38 +01002471/*
2472 * the index is ignored because it's not useful for our purposes
2473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 void
2475gui_mch_add_menu(vimmenu_T *menu, int index)
2476{
2477 vimmenu_T *parent = menu->parent;
2478 char_u *accel_key;
2479 char_u mnemonic_str[MB_LEN_MAX];
2480 int n;
2481 PtArg_t args[5];
2482
2483 menu->submenu_id = menu->id = NULL;
2484
Bram Moolenaar15d63192011-09-14 16:05:15 +02002485 if (menu_is_menubar(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487
Bram Moolenaar15d63192011-09-14 16:05:15 +02002488 accel_key = vim_strchr(menu->name, '&');
2489 if (accel_key != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 mnemonic_str[0] = accel_key[1];
2492 mnemonic_str[1] = NUL;
2493 }
2494
Bram Moolenaar734a8672019-12-02 22:49:38 +01002495 // Create the menu button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002497 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2498 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_TEXT, menu->actext, 0);
2499 if (accel_key != NULL)
2500 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_KEY, mnemonic_str, 0);
2501 PtSetArg(&args[ n++ ], Pt_ARG_POINTER, menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaar15d63192011-09-14 16:05:15 +02002503 if (parent != NULL)
2504 PtSetArg(&args[ n++ ], Pt_ARG_BUTTON_TYPE, Pt_MENU_RIGHT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
Bram Moolenaar15d63192011-09-14 16:05:15 +02002506 menu->id = PtCreateWidget(PtMenuButton,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 (parent == NULL) ? gui.vimMenuBar : parent->submenu_id,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002508 n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar15d63192011-09-14 16:05:15 +02002510 PtAddCallback(menu->id, Pt_CB_ARM, gui_ph_handle_pulldown_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
Bram Moolenaar734a8672019-12-02 22:49:38 +01002512 // Create the actual menu
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002514 if (parent != NULL)
2515 PtSetArg(&args[ n++ ], Pt_ARG_MENU_FLAGS, Pt_TRUE, Pt_MENU_CHILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaar15d63192011-09-14 16:05:15 +02002517 menu->submenu_id = PtCreateWidget(PtMenu, menu->id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
Bram Moolenaar15d63192011-09-14 16:05:15 +02002519 if (parent == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002521 PtAddCallback(menu->submenu_id, Pt_CB_UNREALIZED,
2522 gui_ph_handle_menu_unrealized, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar15d63192011-09-14 16:05:15 +02002524 if (menu->mnemonic != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002526 PtAddHotkeyHandler(gui.vimWindow, tolower(menu->mnemonic),
2527 Pk_KM_Alt, 0, menu, gui_ph_handle_pulldown_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529 }
2530
Bram Moolenaar15d63192011-09-14 16:05:15 +02002531 gui_ph_position_menu(menu->id, menu->priority);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaar734a8672019-12-02 22:49:38 +01002533 // Redraw menubar here instead of gui_mch_draw_menubar
Bram Moolenaar15d63192011-09-14 16:05:15 +02002534 if (gui.menu_is_active)
2535 PtRealizeWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002537 else if (menu_is_popup(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002539 menu->submenu_id = PtCreateWidget(PtMenu, gui.vimWindow, 0, NULL);
2540 PtAddCallback(menu->submenu_id, Pt_CB_UNREALIZED,
2541 gui_ph_handle_menu_unrealized, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543}
2544
2545 void
2546gui_mch_add_menu_item(vimmenu_T *menu, int index)
2547{
2548 vimmenu_T *parent = menu->parent;
2549 char_u *accel_key;
2550 char_u mnemonic_str[MB_LEN_MAX];
2551 int n;
2552 PtArg_t args[13];
2553
2554 n = 0;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002555 PtSetArg(&args[ n++ ], Pt_ARG_POINTER, menu, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
2557#ifdef FEAT_TOOLBAR
Bram Moolenaar15d63192011-09-14 16:05:15 +02002558 if (menu_is_toolbar(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002560 if (menu_is_separator(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002562 PtSetArg(&args[ n++ ], Pt_ARG_SEP_FLAGS,
2563 Pt_SEP_VERTICAL, Pt_SEP_ORIENTATION);
2564 PtSetArg(&args[ n++ ], Pt_ARG_SEP_TYPE, Pt_ETCHED_IN, 0);
2565 PtSetArg(&args[ n++ ], Pt_ARG_ANCHOR_FLAGS,
2566 Pt_TRUE, Pt_ANCHOR_TOP_BOTTOM);
2567 PtSetArg(&args[ n++ ], Pt_ARG_WIDTH, 2, 0);
2568 menu->id = PtCreateWidget(PtSeparator, gui.vimToolBar, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 else
2571 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002572 if (strstr((const char *) p_toolbar, "text") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002574 PtSetArg(&args[ n++ ], Pt_ARG_BALLOON_POSITION,
2575 Pt_BALLOON_BOTTOM, 0);
2576 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2577 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_FONT, "TextFont08", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002579 if ((strstr((const char *) p_toolbar, "icons") != NULL) &&
2580 (gui_ph_toolbar_images != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002582 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_IMAGE,
2583 gui_ph_toolbar_find_icon(menu), 0);
2584 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_TYPE, Pt_TEXT_IMAGE, 0);
2585 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_IMAGE_SPACING, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002587 if (strstr((const char *) p_toolbar, "tooltips") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002589 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_BALLOON,
2590 gui_ph_show_tooltip, 0);
2591 PtSetArg(&args[ n++ ], Pt_ARG_LABEL_FLAGS,
2592 Pt_TRUE, Pt_SHOW_BALLOON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002594 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_HEIGHT, 1, 0);
2595 PtSetArg(&args[ n++ ], Pt_ARG_MARGIN_WIDTH, 1, 0);
2596 PtSetArg(&args[ n++ ], Pt_ARG_FLAGS, Pt_FALSE,
2597 Pt_HIGHLIGHTED | Pt_GETS_FOCUS);
2598 PtSetArg(&args[ n++ ], Pt_ARG_FILL_COLOR, Pg_TRANSPARENT, 0);
2599 menu->id = PtCreateWidget(PtButton, gui.vimToolBar, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaar15d63192011-09-14 16:05:15 +02002601 PtAddCallback(menu->id, Pt_CB_ACTIVATE, gui_ph_handle_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002603 // Update toolbar if it's open
Bram Moolenaar15d63192011-09-14 16:05:15 +02002604 if (PtWidgetIsRealized(gui.vimToolBar))
2605 PtRealizeWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607 else
2608#endif
Bram Moolenaar15d63192011-09-14 16:05:15 +02002609 if (menu_is_separator(menu->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002611 menu->id = PtCreateWidget(PtSeparator, parent->submenu_id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613 else
2614 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002615 accel_key = vim_strchr(menu->name, '&');
2616 if (accel_key != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 mnemonic_str[0] = accel_key[1];
2619 mnemonic_str[1] = NUL;
2620 }
2621
Bram Moolenaar15d63192011-09-14 16:05:15 +02002622 PtSetArg(&args[ n++ ], Pt_ARG_TEXT_STRING, menu->dname, 0);
2623 if (accel_key != NULL)
2624 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_KEY, mnemonic_str,
2625 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
Bram Moolenaar15d63192011-09-14 16:05:15 +02002627 PtSetArg(&args[ n++ ], Pt_ARG_ACCEL_TEXT, menu->actext, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
Bram Moolenaar15d63192011-09-14 16:05:15 +02002629 menu->id = PtCreateWidget(PtMenuButton, parent->submenu_id, n, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
Bram Moolenaar15d63192011-09-14 16:05:15 +02002631 PtAddCallback(menu->id, Pt_CB_ACTIVATE, gui_ph_handle_menu, menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633#ifdef USE_PANEL_GROUP
Bram Moolenaar15d63192011-09-14 16:05:15 +02002634 if (gui_ph_is_buffer_item(menu, parent) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002636 PtAddCallback(menu->id, Pt_CB_DESTROYED,
2637 gui_ph_handle_buffer_remove, menu);
2638 gui_ph_pg_add_buffer(menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640#endif
2641 }
2642
Bram Moolenaar15d63192011-09-14 16:05:15 +02002643 gui_ph_position_menu(menu->id, menu->priority);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644}
2645
2646 void
2647gui_mch_destroy_menu(vimmenu_T *menu)
2648{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002649 if (menu->submenu_id != NULL)
2650 PtDestroyWidget(menu->submenu_id);
2651 if (menu->id != NULL)
2652 PtDestroyWidget(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 menu->submenu_id = NULL;
2655 menu->id = NULL;
2656}
2657
2658 void
2659gui_mch_menu_grey(vimmenu_T *menu, int grey)
2660{
2661 long flags, mask, fields;
2662
Bram Moolenaar15d63192011-09-14 16:05:15 +02002663 if (menu->id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 return;
2665
Bram Moolenaar15d63192011-09-14 16:05:15 +02002666 flags = PtWidgetFlags(menu->id);
2667 if (PtWidgetIsClass(menu->id, PtMenuButton) &&
2668 PtWidgetIsClass(PtWidgetParent(menu->id), PtMenu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 fields = Pt_FALSE;
2671 mask = Pt_SELECTABLE | Pt_HIGHLIGHTED;
2672 }
2673 else
2674 {
2675 fields = Pt_TRUE;
2676 mask = Pt_BLOCKED | Pt_GHOST;
2677 }
2678
Bram Moolenaar15d63192011-09-14 16:05:15 +02002679 if (! grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 fields = ~fields;
2681
Bram Moolenaar15d63192011-09-14 16:05:15 +02002682 PtSetResource(menu->id, Pt_ARG_FLAGS, fields,
2683 mask);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684}
2685
2686 void
2687gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
2688{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002689 // TODO: [un]realize the widget?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690}
2691
2692 void
2693gui_mch_draw_menubar(void)
2694{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002695 // The only time a redraw is needed is when a menu button
2696 // is added to the menubar, and that is detected and the bar
2697 // redrawn in gui_mch_add_menu_item
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698}
2699
2700 void
2701gui_mch_show_popupmenu(vimmenu_T *menu)
2702{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002703 PtSetResource(menu->submenu_id, Pt_ARG_POS, &abs_mouse, 0);
2704 PtRealizeWidget(menu->submenu_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705}
2706
2707 void
2708gui_mch_toggle_tearoffs(int enable)
2709{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002710 // No tearoffs yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711}
2712
2713#endif
2714
Bram Moolenaar15d63192011-09-14 16:05:15 +02002715#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 void
2717gui_mch_show_toolbar(int showit)
2718{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002719 if (showit)
2720 PtRealizeWidget(gui.vimToolBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002722 PtUnrealizeWidget(gui.vimToolBar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723}
2724#endif
2725
Bram Moolenaar734a8672019-12-02 22:49:38 +01002726////////////////////////////////////////////////////////////////////////////
2727// Fonts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 static GuiFont
2730gui_ph_get_font(
2731 char_u *font_name,
2732 int_u font_flags,
2733 int_u font_size,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002734 // Check whether the resulting font has the font flags and size that
2735 // was asked for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 int_u enforce
2737 )
2738{
2739 char_u *font_tag;
2740 FontQueryInfo info;
2741 int_u style;
2742
Bram Moolenaar15d63192011-09-14 16:05:15 +02002743 font_tag = alloc(MAX_FONT_TAG);
2744 if (font_tag != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002746 if (PfGenerateFontName(font_name, font_flags, font_size,
2747 font_tag) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002749 // Enforce some limits on the font used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 style = PHFONT_INFO_FIXED;
2751
Bram Moolenaar15d63192011-09-14 16:05:15 +02002752 if (enforce & PF_STYLE_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 style |= PHFONT_INFO_BOLD;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002754 if (enforce & PF_STYLE_ANTIALIAS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 style |= PHFONT_INFO_ALIAS;
Bram Moolenaar15d63192011-09-14 16:05:15 +02002756 if (enforce & PF_STYLE_ITALIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 style |= PHFONT_INFO_ITALIC;
2758
Bram Moolenaar15d63192011-09-14 16:05:15 +02002759 PfQueryFontInfo(font_tag, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar15d63192011-09-14 16:05:15 +02002761 if (info.size == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 font_size = 0;
2763
Bram Moolenaar734a8672019-12-02 22:49:38 +01002764 // Make sure font size matches, and that the font style
2765 // at least has the bits we're checking for
Bram Moolenaar15d63192011-09-14 16:05:15 +02002766 if (font_size == info.size &&
2767 style == (info.style & style))
Bram Moolenaard2221132011-07-27 14:09:09 +02002768 return (GuiFont)font_tag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
Bram Moolenaar15d63192011-09-14 16:05:15 +02002770 vim_free(font_tag);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002772 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773}
2774
2775/*
2776 * Split up the vim font name
2777 *
2778 * vim_font is in the form of
2779 * <name>:s<height>:a:b:i
2780 *
2781 * a = antialias
2782 * b = bold
2783 * i = italic
2784 *
2785 */
2786
2787 static int
2788gui_ph_parse_font_name(
2789 char_u *vim_font,
2790 char_u **font_name,
2791 int_u *font_flags,
Bram Moolenaar15d63192011-09-14 16:05:15 +02002792 int_u *font_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 char_u *mark;
2795 int_u name_len, size;
2796
Bram Moolenaar15d63192011-09-14 16:05:15 +02002797 mark = vim_strchr(vim_font, ':');
2798 if (mark == NULL)
2799 name_len = STRLEN(vim_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else
Bram Moolenaar15d63192011-09-14 16:05:15 +02002801 name_len = (int_u) (mark - vim_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaar15d63192011-09-14 16:05:15 +02002803 *font_name = vim_strnsave(vim_font, name_len);
Bram Moolenaard0988c52011-08-10 12:19:04 +02002804 if (*font_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002806 if (mark != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002808 while (*mark != NUL && *mark++ == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaard0988c52011-08-10 12:19:04 +02002810 switch (tolower(*mark++))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
2812 case 'a': *font_flags |= PF_STYLE_ANTIALIAS; break;
2813 case 'b': *font_flags |= PF_STYLE_BOLD; break;
2814 case 'i': *font_flags |= PF_STYLE_ITALIC; break;
2815
2816 case 's':
Bram Moolenaar15d63192011-09-14 16:05:15 +02002817 size = getdigits(&mark);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002818 // Restrict the size to some vague limits
Bram Moolenaard0988c52011-08-10 12:19:04 +02002819 if (size < 1 || size > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 size = 8;
2821
2822 *font_size = size;
2823 break;
2824
2825 default:
2826 break;
2827 }
2828 }
2829 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002830 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
Bram Moolenaard2221132011-07-27 14:09:09 +02002832 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833}
2834
2835 int
2836gui_mch_init_font(char_u *vim_font_name, int fontset)
2837{
2838 char_u *font_tag;
2839 char_u *font_name = NULL;
2840 int_u font_flags = 0;
2841 int_u font_size = 12;
2842
2843 FontQueryInfo info;
2844 PhRect_t extent;
2845
Bram Moolenaard0988c52011-08-10 12:19:04 +02002846 if (vim_font_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002848 // Default font
Bram Moolenaara0b19972009-07-01 14:13:18 +00002849 vim_font_name = "PC Terminal";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851
Bram Moolenaar15d63192011-09-14 16:05:15 +02002852 if (STRCMP(vim_font_name, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002854 font_tag = PtFontSelection(gui.vimWindow, NULL, NULL,
2855 "pcterm12", -1, PHFONT_FIXED, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaard0988c52011-08-10 12:19:04 +02002857 if (font_tag == NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar15d63192011-09-14 16:05:15 +02002860 gui_mch_free_font(gui.norm_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 gui.norm_font = font_tag;
2862
Bram Moolenaar15d63192011-09-14 16:05:15 +02002863 PfQueryFontInfo(font_tag, &info);
2864 font_name = vim_strsave(info.font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866 else
2867 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002868 if (gui_ph_parse_font_name(vim_font_name, &font_name, &font_flags,
2869 &font_size) == FALSE)
Bram Moolenaard2221132011-07-27 14:09:09 +02002870 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
Bram Moolenaar15d63192011-09-14 16:05:15 +02002872 font_tag = gui_ph_get_font(font_name, font_flags, font_size, 0);
Bram Moolenaard0988c52011-08-10 12:19:04 +02002873 if (font_tag == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002875 vim_free(font_name);
Bram Moolenaard2221132011-07-27 14:09:09 +02002876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002878
Bram Moolenaar15d63192011-09-14 16:05:15 +02002879 gui_mch_free_font(gui.norm_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 gui.norm_font = font_tag;
2881 }
2882
Bram Moolenaar15d63192011-09-14 16:05:15 +02002883 gui_mch_free_font(gui.bold_font);
2884 gui.bold_font = gui_ph_get_font(font_name, font_flags | PF_STYLE_BOLD,
2885 font_size, PF_STYLE_BOLD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
Bram Moolenaar15d63192011-09-14 16:05:15 +02002887 gui_mch_free_font(gui.ital_font);
2888 gui.ital_font = gui_ph_get_font(font_name, font_flags | PF_STYLE_ITALIC,
2889 font_size, PF_STYLE_ITALIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
Bram Moolenaar734a8672019-12-02 22:49:38 +01002891 // This extent was brought to you by the letter 'g'
Bram Moolenaar15d63192011-09-14 16:05:15 +02002892 PfExtentText(&extent, NULL, font_tag, "g", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 gui.char_width = extent.lr.x - extent.ul.x + 1;
2895 gui.char_height = (- extent.ul.y) + extent.lr.y + 1;
2896 gui.char_ascent = - extent.ul.y;
2897
Bram Moolenaar15d63192011-09-14 16:05:15 +02002898 vim_free(font_name);
Bram Moolenaard2221132011-07-27 14:09:09 +02002899 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900}
2901
Bram Moolenaar02743632005-07-25 20:42:36 +00002902/*
2903 * Adjust gui.char_height (after 'linespace' was changed).
2904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 int
Bram Moolenaar02743632005-07-25 20:42:36 +00002906gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
2908 FontQueryInfo info;
2909
Bram Moolenaar15d63192011-09-14 16:05:15 +02002910 PfQueryFontInfo(gui.norm_font, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 gui.char_height = - info.ascender + info.descender + p_linespace;
2913 gui.char_ascent = - info.ascender + p_linespace / 2;
2914
Bram Moolenaard2221132011-07-27 14:09:09 +02002915 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916}
2917
2918 GuiFont
2919gui_mch_get_font(char_u *vim_font_name, int report_error)
2920{
2921 char_u *font_name;
2922 char_u *font_tag;
2923 int_u font_size = 12;
2924 int_u font_flags = 0;
2925
Bram Moolenaar15d63192011-09-14 16:05:15 +02002926 if (gui_ph_parse_font_name(vim_font_name, &font_name, &font_flags,
2927 &font_size) != FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
Bram Moolenaar15d63192011-09-14 16:05:15 +02002929 font_tag = gui_ph_get_font(font_name, font_flags, font_size, -1);
2930 vim_free(font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaard0988c52011-08-10 12:19:04 +02002932 if (font_tag != NULL)
Bram Moolenaard2221132011-07-27 14:09:09 +02002933 return (GuiFont)font_tag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935
Bram Moolenaard0988c52011-08-10 12:19:04 +02002936 if (report_error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002937 semsg(e_font, vim_font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
Bram Moolenaard2221132011-07-27 14:09:09 +02002939 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002942#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002943/*
2944 * Return the name of font "font" in allocated memory.
2945 * Don't know how to get the actual name, thus use the provided name.
2946 */
2947 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002948gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002949{
2950 if (name == NULL)
2951 return NULL;
2952 return vim_strsave(name);
2953}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00002954#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002955
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 void
2957gui_mch_set_font(GuiFont font)
2958{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002959 PgSetFont(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960}
2961
2962 void
2963gui_mch_free_font(GuiFont font)
2964{
Bram Moolenaar15d63192011-09-14 16:05:15 +02002965 vim_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966}
2967