blob: 59d2272e8a2828e6574bb2b71a813302456f0eed [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11#include <string.h>
12
13/*
14 * gui_riscos.c
15 *
16 * Thomas Leonard <tal197@ecs.soton.ac.uk>
17 * Updated by Andy Wingate <andy@sparse.net>
18 */
19
20extern int time_of_last_poll;
21
22int task_handle = 0; /* Zero means we are not yet a Wimp task */
23int child_handle = 0; /* Task handle of our child process (zero if none). */
24int *wimp_menu = (int *) -1; /* Pointer to a Wimp menu structure (or -1) */
25int save_window = -1; /* Save As window handle */
26
27int *redraw_block = NULL; /* NULL means not in a redraw loop. */
28int ro_return_early = FALSE; /* Break out of gui_mch_wait_for_chars() */
29
30int leaf_ref = 0; /* Wimp message number - send via Wimp$Scrap */
31char_u *leaf_name = NULL; /* Leaf name from DataSave */
32
33int default_columns = 120; /* These values are used if the --rows and --columns */
34int default_rows = 32; /* options aren't used on startup. */
35
36#define DRAG_FALSE 0
37#define DRAG_SELECTION 1
38#define DRAG_RESIZE_WINDOW 2
39int ro_dragging = DRAG_FALSE;
40int drag_button;
41int drag_modifiers;
42int drag_x_offset;
43int drag_y_offset;
44
45int nested_wimp = FALSE; /* Bool - can we use the new wimp? */
46
47int changed_mode = FALSE;
48int x_eigen_factor;
49int y_eigen_factor;
50
51/* If ro_current_font is non-zero then use the outline font with that handle,
52 * otherwise, if zap_redraw is TRUE then use ZapRedraw, otherwise use the
53 * system font.
54 *
55 * If zap_redraw is TRUE then zap_file[] contains valid Zap font file
56 * pointers (or NULLs).
57 */
58int ro_current_font = 0; /* 0 is system font, or ZapRedraw */
59int font_x_offset = 0; /* Where to position each char in its box */
60int font_y_offset = 0;
61
62int zap_redraw = FALSE;
63int double_height = FALSE; /* Plot each line twice? */
64
65#define grgb(r,g,b) ((b<<16) + (g<<8) + (r))
66#define UNUSED_COLOUR (gui.back_pixel)
67
68#define RO_LOAD_CLIPBOARD -2 /* Internal handle for DataSave message. */
69
70/* Changes by John Kortink, 22-23 July 1998
71 *
72 * Stuff to make redraw a lot faster. Almost all of it is right here below,
73 * elsewhere changes are marked with 'JK230798'. Apart from a small change in
74 * 'gui.c' all changes are limited to this file, 'gui_riscos.c'. The change in
75 * 'gui.c' is to make Vim stop being 'smart' not redrawing characters that are
76 * 'already there' (i.e. from the previous line, by coincidence). This caused a
77 * lot more calls to the redraw code, which we want to avoid because a few nice
78 * big strings at a time is a lot faster than a truckload of small ones. ('Dear
79 * Bram ...').
80 */
81
82/* The ZapRedraw structure */
83
84static struct
85{
86 int r_flags;
87 int r_minx;
88 int r_miny;
89 int r_maxx;
90 int r_maxy;
91 int r_screen;
92 int r_bpl;
93 int r_bpp;
94 int r_charw;
95 int r_charh;
96 char *r_caddr;
97 int r_cbpl;
98 int r_cbpc;
99 int r_linesp;
100 int r_data;
101 int r_scrollx;
102 int r_scrolly;
103 int *r_palette;
104 int r_for;
105 int r_bac;
106 char *r_workarea;
107 int r_magx;
108 int r_magy;
109 int r_xsize;
110 int r_ysize;
111 int r_mode;
112}
113zap_redraw_block;
114
115/* Other globals */
116
117static int zap_redraw_initialised = FALSE;
118static int zap_redraw_update_colours;
119static int zap_redraw_colours[2];
120static int zap_redraw_palette[16];
121
122/* Holds the current Zap font file(s).
123 * The font is recreated from this block on a mode change.
124 * When using zap, element ZAP_NORMAL is always valid, but
125 * the others can be NULL.
126 */
127
128#define ZAP_NORMAL 0
129#define ZAP_BOLD 1
130#define ZAP_ITALIC 2
131#define ZAP_BITALIC 3
132#define ZAP_STYLES 4
133
134/* Zap font file format data */
135static char *zap_file[ZAP_STYLES] = {NULL, NULL, NULL, NULL};
136
137/* r_caddr format for current mode */
138static char *zap_caddr[ZAP_STYLES] = {NULL, NULL, NULL, NULL};
139
140static void ro_remove_menu(int *menu);
141
142/*
143 * Initialise all the ZapRedraw stuff.
144 * Call this when changing font and after each mode change.
145 * zap_redraw_bitmap must contain a valid Zap font file (possibly
146 * created from the system font).
147 *
148 * Return FAIL to revert to system font (if we can't use ZapRedraw).
149 */
150 int
151ro_zap_redraw_initialise()
152{
153 int bytes_per_bitmap_char;
154 int first, last;
155 int i;
156
157 /* Can't have initialisers for struct members :-(, ok, this way then ... */
158 if (!zap_redraw_initialised)
159 {
160 zap_redraw_block.r_workarea = NULL;
161 zap_redraw_initialised = TRUE;
162 }
163
164 /* We redraw in DSA mode */
165 zap_redraw_block.r_flags = 0x0;
166
167 /* Let ZapRedraw get the screen address for us */
168 zap_redraw_block.r_screen = 0;
169
170 /* Read the font width and height from the font file header.
171 * Assume that all styles are the same size.
172 * ZAP_NORMAL is always present.
173 */
174 zap_redraw_block.r_charw = ((int *) zap_file[ZAP_NORMAL])[2];
175 zap_redraw_block.r_charh = ((int *) zap_file[ZAP_NORMAL])[3];
176
177 /* We have no linespacing */
178 zap_redraw_block.r_linesp = 0;
179
180 /* Fix foreground = colour 1 */
181 zap_redraw_block.r_for = 1;
182
183 /* Fix background = colour 0 */
184 zap_redraw_block.r_bac = 0;
185
186 /* Colour mask buffer */
187 zap_redraw_block.r_palette = zap_redraw_palette;
188
189 /* Allocate local workspace (for the few calls following here) */
190 if (zap_redraw_block.r_workarea != NULL)
191 free(zap_redraw_block.r_workarea);
192 zap_redraw_block.r_workarea = (char*) malloc(128);
193 if (!zap_redraw_block.r_workarea)
194 return FAIL; /* Out of memory */
195
196 /* Fill in VDU variables */
197 if (xswi(ZapRedraw_ReadVduVars, 0, &zap_redraw_block) & v_flag)
198 return FAIL; /* Can't find ZapRedraw module - use VDU instead */
199
200 /* Determine cbpl and cbpc */
201 swi(ZapRedraw_CachedCharSize, zap_redraw_block.r_bpp, 0,
202 zap_redraw_block.r_charw, zap_redraw_block.r_charh);
203 zap_redraw_block.r_cbpl = r2;
204 zap_redraw_block.r_cbpc = r3;
205
206 /* Allocate general workspace (for the calls outside) */
207 if (zap_redraw_block.r_workarea != NULL)
208 free(zap_redraw_block.r_workarea);
209 zap_redraw_block.r_workarea = (char*) malloc(128 + zap_redraw_block.r_cbpl);
210 if (!zap_redraw_block.r_workarea)
211 return FAIL; /* Out of memory */
212
213 /* Now convert the 1 bpp character data ready for the current mode */
214
215 bytes_per_bitmap_char = (zap_redraw_block.r_charw * zap_redraw_block.r_charh + 7) / 8;
216
217 /* Convert the fonts from 1bpp to a format suitable for the
218 * current mode.
219 */
220 for (i = 0; i < ZAP_STYLES; i++)
221 {
222 first = ((int *) zap_file[i])[4];
223 last = ((int *) zap_file[i])[5];
224
225 if (last > 255)
226 last = 255; /* Don't convert cursors (overwrites memory!) */
227
228 /* Allocate the font cache */
229 vim_free(zap_caddr[i]);
230 if (zap_file[i])
231 zap_caddr[i] = (char*) malloc(zap_redraw_block.r_cbpc * 256);
232 else
233 zap_caddr[i] = NULL; /* No file for this style */
234
235 if (zap_caddr[i])
236 {
237 zap_redraw_block.r_caddr = zap_caddr[i];
238
239 swi(ZapRedraw_ConvertBitmap, 0, &zap_redraw_block,
240 first, last, /* Range of characters to convert */
241 zap_file[i] + 0x20 /* Addr of first char provided by font */
242 - first * bytes_per_bitmap_char);
243 }
244 }
245
246 if (!zap_caddr[ZAP_NORMAL])
247 {
248 zap_redraw = FALSE; /* Out of memory */
249 return FAIL;
250 }
251
252 /* Next time we need them, we have to update the colour masks */
253 zap_redraw_update_colours = TRUE;
254
255 return OK;
256}
257
258/*
259 * Redraw a string at OS coordinates <x,y> (top-left, x inclusive, y exclusive).
260 * Graphics clip window is window[0..3] as in R1+28..40 of Wimp_RedrawWindow.
261 * Returns (possibly modified) flags.
262 */
263 int
264ro_zap_redraw_draw_string(x, y, string, length, flags, clip)
265 int x;
266 int y;
267 char *string;
268 int length;
269 int flags; /* DRAW_TRANSP, DRAW_BOLD, DRAW_UNDERL, DRAW_ITALIC */
270 int *clip;
271{
272 char redraw_data[1024];
273 int clip_minx;
274 int clip_miny;
275 int clip_maxx;
276 int clip_maxy;
277 int os_xshift = zap_redraw_block.r_magx;
278 int os_yshift = zap_redraw_block.r_magy;
279
280 if (flags & DRAW_TRANSP)
281 return flags; /* We don't do transparent plotting yet. */
282
283 if (flags & DRAW_BOLD)
284 {
285 if (flags & DRAW_ITALIC && zap_caddr[ZAP_BITALIC])
286 zap_redraw_block.r_caddr = zap_caddr[ZAP_BITALIC];
287 else
288 zap_redraw_block.r_caddr = zap_caddr[ZAP_BOLD];
289 }
290 else
291 {
292 if (flags & DRAW_ITALIC)
293 zap_redraw_block.r_caddr = zap_caddr[ZAP_ITALIC];
294 else
295 zap_redraw_block.r_caddr = zap_caddr[ZAP_NORMAL];
296 }
297 if (!zap_redraw_block.r_caddr)
298 {
299 zap_redraw_block.r_caddr = zap_caddr[ZAP_NORMAL];
300 flags |= DRAW_UNDERL; /* Style missing - we can always underline */
301 }
302
303 /* Set the vertical scaling flag */
304 if (double_height)
305 zap_redraw_block.r_flags = 1 << 1;
306 else
307 zap_redraw_block.r_flags = 0;
308
309 /* Update the colour masks (if needed) */
310 if (zap_redraw_update_colours)
311 {
312 swi(ZapRedraw_CreatePalette, 2,
313 &zap_redraw_block,
314 zap_redraw_colours,
315 zap_redraw_block.r_palette, 2);
316 zap_redraw_update_colours = FALSE;
317 }
318
319 /* Target rectangle in ZapRedraw rectangle coordinates (pixels, Y-min/max reversed !!!) */
320 zap_redraw_block.r_minx = x >> os_xshift; /* inclusive */
321 zap_redraw_block.r_miny = zap_redraw_block.r_ysize - (y >> os_yshift); /* inclusive */
322 zap_redraw_block.r_maxx = (x + length * gui.char_width) >> os_xshift; /* exclusive */
323 zap_redraw_block.r_maxy = zap_redraw_block.r_ysize - ((y - gui.char_height) >> os_yshift);
324 /* exclusive */
325
326 /* Clip rectangle in ZapRedraw rectangle coordinates (pixels, Y-min/max reversed !!!) */
327 clip_minx = clip[0] >> os_xshift; /* inclusive */
328 clip_miny = zap_redraw_block.r_ysize - (clip[3] >> os_yshift); /* inclusive */
329 clip_maxx = clip[2] >> os_xshift; /* exclusive */
330 clip_maxy = zap_redraw_block.r_ysize - (clip[1] >> os_yshift); /* exclusive */
331
332 /* Clip target rectangle against the current graphics window */
333 if (zap_redraw_block.r_minx < clip_minx)
334 {
335 zap_redraw_block.r_scrollx = clip_minx - zap_redraw_block.r_minx;
336 zap_redraw_block.r_minx = clip_minx;
337 }
338 else
339 zap_redraw_block.r_scrollx = 0;
340 if (zap_redraw_block.r_miny < clip_miny)
341 {
342 zap_redraw_block.r_scrolly = clip_miny - zap_redraw_block.r_miny;
343 zap_redraw_block.r_miny = clip_miny;
344 }
345 else
346 zap_redraw_block.r_scrolly = 0;
347 if (zap_redraw_block.r_maxx > clip_maxx)
348 zap_redraw_block.r_maxx = clip_maxx;
349 if (zap_redraw_block.r_maxy > clip_maxy)
350 zap_redraw_block.r_maxy = clip_maxy;
351
352 /* Fill in the character data structure */
353 if (length > (sizeof(redraw_data) - 2 * 4 - 2))
354 length = sizeof(redraw_data) - 2 * 4 - 2;
355 ((int*) redraw_data)[0] = 2 * 4;
356 ((int*) redraw_data)[1] = 0;
357 strncpy(redraw_data + 2 * 4, string, length);
358 redraw_data[2 * 4 + length + 0] = '\0';
359 redraw_data[2 * 4 + length + 1] = '\x2';
360 zap_redraw_block.r_data = (int) redraw_data;
361
362 /* Perform the draw */
363 swi(ZapRedraw_RedrawArea, 0, &zap_redraw_block);
364
365 return flags;
366}
367
368/*
369 * Okay that was it from me, back to Thomas ...
370 */
371
372/*
373 * Parse the GUI related command-line arguments. Any arguments used are
374 * deleted from argv, and *argc is decremented accordingly. This is called
375 * when vim is started, whether or not the GUI has been started.
376 */
377 void
378gui_mch_prepare(int *argc, char **argv)
379{
380 int arg = 1;
381
382 while (arg < *argc - 1)
383 {
384 if (strcmp(argv[arg], "--rows") == 0 || strcmp(argv[arg], "--columns") == 0)
385 {
386 int value;
387
388 value = atoi(argv[arg + 1]);
389
390 if (argv[arg][2] == 'r')
391 default_rows = value;
392 else
393 default_columns = value;
394
395 /* Delete argument from argv[]. (hope this is read/write!) */
396
397 *argc -= 2;
398 if (*argc > arg)
399 mch_memmove(&argv[arg], &argv[arg + 2], (*argc - arg)
400 * sizeof(char *));
401 }
402 else
403 arg++;
404 }
405}
406
407/* Fatal error on initialisation - report it and die. */
408 void
409ro_die(error)
410 char_u *error; /* RISC OS error block */
411{
412 swi(Wimp_ReportError, error, 5, "GVim");
413 exit(EXIT_FAILURE);
414}
415
416/* Find the sizes of the window tools:
417 *
418 * Create a test window.
419 * Find inner and outer sizes.
420 * Find the difference.
421 * Delete window.
422 *
423 * While we're here, find the eigen values too.
424 */
425 void
426ro_measure_tools()
427{
428 int block[10];
429 int vdu[] = { 4, 5, -1};
430 int test_window[] =
431 {
432 -100, -100, /* Visible area : min X,Y */
433 -50, -50, /* max X,Y */
434 0, 0, /* Scroll offsets */
435 -1, /* Window in front */
436 0xd0800150, /* Window flags */
437 0xff070207, /* Colours */
438 0x000c0103, /* More colours */
439 0, -0x4000, /* Workarea extent */
440 0x4000, 0, /* max X,Y */
441 0x00000000, /* No title */
442 0 << 12, /* No workarea button type */
443 1, /* Wimp sprite area */
444 0x00010001, /* Minimum width, height */
445 0, 0, 0, /* Title data (none) */
446 0 /* No icons */
447 };
448 int inner_max_x, inner_min_y;
449
450 swi(Wimp_CreateWindow, 0, test_window);
451
452 block[0] = r0;
453 /* Open the window (and read state).
454 * GetWindowOutline needs it too if the wimp isn't nested.
455 */
456 swi(Wimp_OpenWindow, 0, block);
457 inner_max_x = block[3];
458 inner_min_y = block[2];
459
460 swi(Wimp_GetWindowOutline, 0, block);
461
462 gui.scrollbar_width = block[3] - inner_max_x;
463 gui.scrollbar_height = inner_min_y - block[2];
464
465 swi(Wimp_DeleteWindow, 0, block);
466
467 /* Read the size of one pixel. */
468 swi(OS_ReadVduVariables, vdu, vdu);
469 x_eigen_factor = vdu[0];
470 y_eigen_factor = vdu[1];
471}
472
473/* Load a template from the current templates file.
474 * Create the window and return its handle.
475 */
476 int
477ro_load_template(str_name, title, title_size)
478 char_u *str_name; /* Identifier of window in file (max 12 chars) */
479 char_u **title; /* If not NULL then return pointer to title here */
480 int *title_size; /* If not NULL then return the title length here */
481{
482 int *window;
483 char *data;
484 int name[4];
485
486 strcpy( (char *) name, str_name);
487
488 /* Find how big we must make the buffers */
489
490 if (xswi(Wimp_LoadTemplate, 0, 0, 0, 0, -1, name, 0) & v_flag)
491 ro_die( (char *) r0);
492
493 window = malloc(r1); /* Don't print text messages from alloc() */
494 data = malloc(r2);
495 if (window == NULL || data == NULL)
496 ro_die("\0\0\0\0Out of memory - Can't load templates");
497
498 /* Load the template into the buffers */
499
500 swi(Wimp_LoadTemplate, 0,
501 window, /* Temp block */
502 data, /* Icon data */
503 data + r2 + 1, /* End of icon data */
504 -1, /* No fonts */
505 name, 0); /* First match */
506 if (r6 == 0)
507 ro_die("\0\0\0\0Can't find window in Templates file");
508
509 /* Create the window */
510
511 if (xswi(Wimp_CreateWindow, 0, window) & v_flag)
512 ro_die( (char *) r0);
513
514 if (title)
515 *title = (char_u *) window[18];
516 if (title_size)
517 *title_size = window[20];
518
519 free(window); /* Free temp block */
520 return r0; /* Return the window handle */
521}
522
523/*
524 * Check if the GUI can be started. Called before gvimrc is sourced.
525 * Return OK or FAIL.
526 */
527 int
528gui_mch_init_check()
529{
530 return OK; /* TODO: GUI can always be started? */
531}
532
533/*
534 * Initialise the RISC OS GUI.
535 * Create all the windows.
536 * Returns OK for success, FAIL when the GUI can't be started.
537 */
538 int
539gui_mch_init()
540{
541 int messages[] = {
542 1, 2, 3, 4, /* DataSave, DataSaveAck, DataLoad, DataLoadAck */
543 8, /* PreQuit */
544 0xf, /* ClaimEntity (for clipboard) */
545 0x10, /* DataRequest (for clipboard) */
546 0x400c1, /* Mode change */
547 0x400c3, /* TaskCloseDown */
548 0x400c9, /* MenusDeleted */
549 0x808c1, /* TW_Output */
550 0x808c2, /* TW_Ego */
551 0x808c3, /* TW_Morio */
552 0x808c4, /* TW_Morite */
553 0}; /* End-of-list. */
554
555
556 /* There may have been some errors reported in the
557 * command window before we get here. Wait if so.
558 */
559 swi(Wimp_ReadSysInfo, 3);
560 if (r0 == 0)
561 swi(Wimp_CommandWindow, 0); /* Window opened - close with prompt */
562
563 if (xswi(Wimp_Initialise, 310, 0x4b534154, "GVim", messages) & v_flag)
564 return FAIL;
565 nested_wimp = r0 >= 397;
566 task_handle = r1;
567
568 /* Load the templates. */
569
570 if (xswi(Wimp_OpenTemplate, 0, "Vim:Templates") & v_flag)
571 ro_die( (char *) r0);
572
573 gui.window_handle = ro_load_template("editor",
574 &gui.window_title,
575 &gui.window_title_size);
576
577 save_window = ro_load_template("save", NULL, NULL);
578
579 swi(Wimp_CloseTemplate);
580
581 /* Set default foreground and background colours. */
582
583 gui.norm_pixel = gui.def_norm_pixel;
584 gui.back_pixel = gui.def_back_pixel;
585
586 /* Get the colours from the "Normal" and "Menu" group (set in syntax.c or
587 * in a vimrc file) */
588
589 set_normal_colors();
590
591 /*
592 * Check that none of the colors are the same as the background color
593 */
594
595 gui_check_colors();
596
597 /* Get the colours for the highlight groups (gui_check_colors() might have
598 * changed them) */
599
600 highlight_gui_started(); /* re-init colours and fonts */
601
602 /* Set geometry based on values read on initialisation. */
603
604 gui.num_cols = Columns = default_columns;
605 gui.num_rows = Rows = default_rows;
606
607 /* Get some information about our environment. */
608
609 ro_measure_tools();
610
611 return OK;
612}
613
614/*
615 * Called when the foreground or background colour has been changed.
616 */
617 void
618gui_mch_new_colors()
619{
620}
621
622/*
623 * Open the GUI window which was created by a call to gui_mch_init().
624 */
625 int
626gui_mch_open(void)
627{
628 int block[10];
629
630 block[0] = gui.window_handle;
631 swi(Wimp_GetWindowState, 0, block);
632 block[7] = -1; /* Open at the top of the stack */
633 swi(Wimp_OpenWindow, 0, block);
634
635 /* Give the new window the input focus */
636 swi(Wimp_SetCaretPosition, gui.window_handle, -1, 0, 0, -1, -1);
637
638 if (gui_win_x != -1 && gui_win_y != -1)
639 gui_mch_set_winpos(gui_win_x, gui_win_y);
640
641 return OK;
642}
643
644 void
645gui_mch_exit(int rc)
646{
647 int block[64];
648
649 /* Close window. Stops us from getting troublesome events
650 * if we take a while to die.
651 */
652 block[0] = gui.window_handle;
653 swi(Wimp_CloseWindow, 0, block);
654
655 if (child_handle)
656 {
657 /* We still have a sub-task running - kill it */
658 block[0] = 20;
659 block[3] = 0;
660 block[4] = 0; /* Quit */
661 if ((xswi(Wimp_SendMessage, 17, block, child_handle) & v_flag) == 0)
662 {
663 /* Idle until child dies. */
664 while (child_handle)
665 {
666 process_event(wimp_poll(1, block), block);
667 }
668 }
669 }
670
671 exit(rc);
672}
673
674/*
675 * Get the position of the top left corner of the window.
676 */
677 int
678gui_mch_get_winpos(int *x, int *y)
679{
680 /* TODO */
681 return FAIL;
682}
683
684/*
685 * Set the position of the top left corner of the window to the given
686 * coordinates.
687 */
688 void
689gui_mch_set_winpos(int x, int y)
690{
691 /* TODO */
692}
693
694 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000695gui_mch_set_shellsize(width, height, min_width, min_height, base_width, base_height, direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 int width; /* In OS units */
697 int height;
698 int min_width; /* Smallest permissable window size (ignored) */
699 int min_height;
700 int base_width; /* Space for scroll bars, etc */
701 int base_height;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000702 int direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703{
704 int s_width, s_height;
705 int block[] = {
706 gui.window_handle,
707 0,
708 -height + 1,
709 width,
710 1};
711
712 gui_mch_get_screen_dimensions(&s_width, &s_height);
713 s_width -= base_width;
714 s_height -= base_height; /* Underestimate - ignores titlebar */
715
716 swi(Wimp_GetWindowState, 0, block);
717 block[3] = block[1] + width;
718 block[2] = block[4] - height;
719 if (block[3] > s_width)
720 {
721 block[3] = s_width;
722 block[1] = block[3] - width;
723 }
724 if (block[2] < gui.scrollbar_height)
725 {
726 block[2] = gui.scrollbar_height;
727 block[4] = block[2] + height;
728 }
729 swi(Wimp_OpenWindow, 0, block);
730 swi(Wimp_ForceRedraw, gui.window_handle, 0, -height, width, 0);
731}
732
733 void
734gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
735{
736 int block[] = {4, 5, 11, 12, -1};
737
738 swi(OS_ReadVduVariables, block, block);
739 *screen_w = (block[2] + 1) << block[0];
740 *screen_h = (block[3] + 1) << block[1];
741}
742
743/* Take a font name with options and return a font handle, or
744 * zero for failure.
745 * Replace extension with 'Bold' or 'Italic' depending on modifiers.
746 */
747 int
748ro_get_font(fullname, weight)
749 char_u *fullname;
750 int weight; /* Initial weights:
751 * BIT MEANING
752 * 0 bold
753 * 1 italic
754 */
755{
756 char_u *arg;
757 char_u font[41];
758 int width = -1;
759 int height = -1;
760 int name_len;
761 int i;
762 char_u c;
763
764 for (i = 0; i < 39;)
765 {
766 c = fullname[i];
767 if (c == ':' || c == NUL || c == '.')
768 break;
769 font[i++] = c;
770 }
771
772 /* find the first modifier, NULL if none */
773 arg = strchr(fullname + i, ':');
774
775 while (arg)
776 {
777 switch (*++arg)
778 {
779 case 'h':
780 height = strtol(arg + 1, (char **) &arg, 10);
781 break;
782 case 'w':
783 width = strtol(arg + 1, (char **) &arg, 10);
784 break;
785 case 'b':
786 weight |= 1;
787 break;
788 case 'i':
789 weight |= 2;
790 break;
791 default:
792 return 0;
793 }
794 arg = strchr(arg, ':');
795 }
796
797 if ((weight & 1) && i < 35)
798 {
799 /* Bold goes instead of given suffix */
800 strncpy(font + i, ".Bold", 5);
801 i += 5;
802 }
803 else
804 {
805 /* Copy rest of name unless we are using Bold */
806 while (i < 39)
807 {
808 c = fullname[i];
809 if (c == ':' || c == NUL)
810 break;
811 font[i++] = c;
812 }
813 }
814 if ((weight & 2) && i < 32)
815 {
816 strncpy(font + i, ".Oblique", 8);
817 i += 8;
818 }
819
820 font[i] = 0;
821
822 if (height < 1 && width < 1)
823 height = width = 10; /* Default to 10pt */
824 else if (height < 1)
825 height = width;
826 else if (width < 1)
827 width = height;
828
829 if (xswi(Font_FindFont, 0, font, width << 4, height << 4, 0, 0) & v_flag)
830 return NOFONT; /* Can't find font */
831
832 return r0;
833}
834
835/* Load a file into allocated memory and check it is valid.
836 * Return a pointer to the allocated block on success.
837 */
838 char *
839zap_load_file(name, style)
840 char_u *name; /* Name of directory containing styles */
841 char_u *style; /* Name of style within directory */
842{
843 char_u fname[256];
844 char_u *file;
845
846 if (strlen(name) + strlen(style) > 254)
847 return NULL; /* Names too long */
848
849 sprintf(fname, "%s.%s", name, style);
850
851 /* Load the named font in 1bpp format. */
852 if (xswi(OS_File, 13, fname, 0, 0, "VimFonts:") & v_flag || r0 != 1)
853 return NULL; /* Error reading file info, or not a file */
854
855 /* Allocate enough memory to load the whole file */
856 file = (char *) alloc(r4);
857 if (!file)
858 return NULL; /* Out of memory */
859
860 if (xswi(OS_File, 12, fname, file, 0, "VimFonts:") & v_flag)
861 return NULL; /* Unable to load file */
862
863 if (strncmp(file, "ZapFont\015", 8) == 0)
864 return file; /* Loaded OK! */
865
866 free(file);
867 return NULL; /* Not a valid font file */
868}
869
870/* Load and convert the named font.
871 * If name is NULL or a null string then convert the system font.
872 * Return OK on success; FAIL and we revert to using the VDU drivers.
873 *
874 * 'name' is the name of a directory.
875 * Tries to load 'name.0', 'name.B', 'name.I' and 'name.IB'.
876 */
877 int
878zap_load_font(name)
879 char_u *name;
880{
881 int i;
882
883 /* Free the existing font files, if any */
884 for (i = 0; i < ZAP_STYLES; i++)
885 {
886 vim_free(zap_file[i]);
887 zap_file[i] = NULL;
888 }
889
890 if (name && *name == '!')
891 {
892 name++;
893 double_height = TRUE;
894 }
895 else
896 double_height = FALSE;
897
898 if (name && *name)
899 {
900 zap_file[ZAP_NORMAL] = zap_load_file(name, "0");
901 if (!zap_file[ZAP_NORMAL])
902 return FAIL; /* Can't load the 'normal' style - error */
903
904 zap_file[ZAP_BOLD] = zap_load_file(name, "B");
905 zap_file[ZAP_ITALIC] = zap_load_file(name, "I");
906 zap_file[ZAP_BITALIC] = zap_load_file(name, "IB");
907 }
908 else
909 {
910 int *header;
911 char workarea[16];
912 char *old_wa;
913
914 /* Allocate memory for system font (8 x 8 x 256 bits, plus header) */
915 header = (int *) alloc(0x20 + 8 * 256);
916 if (header == NULL)
917 return FAIL;
918 zap_file[ZAP_NORMAL] = (char *) header;
919
920 /* Store details about the system font */
921 header[2] = 8; /* Width */
922 header[3] = 8; /* Height */
923 header[4] = 0; /* First char */
924 header[5] = 255; /* Last char */
925 header[6] = header[7] = 0; /* Reserved */
926
927 /* Get system font bitmap */
928 old_wa = zap_redraw_block.r_workarea;
929 zap_redraw_block.r_workarea = workarea;
930 swi(ZapRedraw_ReadSystemChars, zap_file[ZAP_NORMAL] + 0x20, &zap_redraw_block);
931 zap_redraw_block.r_workarea = old_wa;
932 }
933
934 return ro_zap_redraw_initialise();
935}
936
937/*
938 * Initialise vim to use the font with the given name.
939 * Return FAIL if the font could not be loaded, OK otherwise.
940 */
941 int
942gui_mch_init_font(char_u *font_name, int fontset)
943{
944 int new_handle = 0; /* Use the system font by default */
945
946 if (font_name[0] == '!')
947 {
948 /* Select a ZapRedraw font */
949 if (zap_load_font(font_name + 1))
950 zap_redraw = TRUE;
951 else
952 {
953 EMSG2(_("E610: Can't load Zap font '%s'"), font_name);
954 font_name = "System"; /* Error - use system font */
955 zap_redraw = FALSE;
956 }
957 }
958 else
959 {
960 zap_redraw = FALSE;
961
962 if (font_name)
963 {
964 /* Extract any extra details about the font */
965 new_handle = ro_get_font(font_name, 0);
966 if (!new_handle)
967 return FAIL;
968 }
969 else
970 font_name = "System";
971 }
972
973 /* Free the previous font, if any */
974 gui_mch_free_font(gui.norm_font);
975 gui.norm_font = new_handle;
976 gui.char_ascent = 0;
977
978 if (new_handle)
979 {
980 /* Read details about the chosen font */
981 swi(Font_ReadInfo, new_handle);
982
983 gui.char_width = r3 - r1;
984 gui.char_height = r4 - r2;
985
986 font_x_offset = -r1; /* Where to position each char in its box */
987 font_y_offset = -r4;
988
989 /* Try to load other fonts for bold, italic, and bold-italic */
990 gui_mch_free_font(gui.bold_font);
991 gui.bold_font = ro_get_font(font_name, 1);
992 gui_mch_free_font(gui.ital_font);
993 gui.ital_font = ro_get_font(font_name, 2);
994 gui_mch_free_font(gui.boldital_font);
995 gui.boldital_font = ro_get_font(font_name, 3);
996 }
997 else
998 {
999 /* Use the system font or ZapRedraw. */
1000 if (zap_redraw)
1001 {
1002 gui.char_width = zap_redraw_block.r_charw << zap_redraw_block.r_magx;
1003 gui.char_height = zap_redraw_block.r_charh << zap_redraw_block.r_magy;
1004 if (double_height)
1005 gui.char_height <<= 1;
1006 }
1007 else
1008 {
1009 gui.char_width = 16;
1010 gui.char_height = 32;
1011 }
1012
1013 gui_mch_free_font(gui.bold_font);
1014 gui.bold_font = 0;
1015 gui_mch_free_font(gui.ital_font);
1016 gui.ital_font = 0;
1017 gui_mch_free_font(gui.boldital_font);
1018 gui.boldital_font = 0;
1019 }
1020 hl_set_font_name(font_name);
1021
1022 must_redraw = CLEAR;
1023 return OK;
1024}
1025
Bram Moolenaar02743632005-07-25 20:42:36 +00001026/*
1027 * Adjust gui.char_height (after 'linespace' was changed).
1028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 int
Bram Moolenaar02743632005-07-25 20:42:36 +00001030gui_mch_adjust_charheight()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
1032 return FAIL;
1033}
1034
1035/*
1036 * Get a font structure for highlighting.
1037 */
1038 GuiFont
1039gui_mch_get_font(name, giveErrorIfMissing)
1040 char_u *name;
1041 int giveErrorIfMissing;
1042{
1043 int handle;
1044
1045 if (!name)
1046 return NOFONT; /* System font if no name */
1047
1048 handle = ro_get_font(name, 0);
1049 if (!handle)
1050 {
1051 if (giveErrorIfMissing)
1052 EMSG2(_("E611: Can't use font %s"), name);
1053 return NOFONT;
1054 }
1055
1056 return handle;
1057}
1058
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001059#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001061 * Return the name of font "font" in allocated memory.
1062 * Don't know how to get the actual name, thus use the provided name.
1063 */
1064 char_u *
1065gui_mch_get_fontname(font, name)
1066 GuiFont font;
1067 char_u *name;
1068{
1069 if (name == NULL)
1070 return NULL;
1071 return vim_strsave(name);
1072}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001073#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001074
1075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 * Set the current text font.
1077 */
1078 void
1079gui_mch_set_font(GuiFont font)
1080{
1081 ro_current_font = font;
1082
1083 if (font)
1084 {
1085 /* Not the system font or ZapRedraw font - select it */
1086 swi(Font_SetFont, font);
1087 }
1088}
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/*
1091 * If a font is not going to be used, free its structure.
1092 */
1093 void
1094gui_mch_free_font(GuiFont font)
1095{
1096 if (font)
1097 swi(Font_LoseFont, font);
1098}
1099
1100/*
1101 * Return the Pixel value (colour) for the given colour name.
1102 * Return INVALCOLOR for error.
1103 * NB: I've changed Green for now, since it looked really sick
1104 */
1105 guicolor_T
1106gui_mch_get_color(char_u *name)
1107{
1108 int i;
1109 struct colour
1110 {
1111 char_u *name;
1112 guicolor_T value;
1113 } colours[] =
1114 {
1115 { "Red", grgb(255, 0, 0) },
1116 { "LightRed", grgb(255, 0, 0) },
1117 { "DarkRed", grgb(139, 0, 0) },
1118
1119 { "Green", grgb(50, 200, 50) },
1120 { "LightGreen", grgb(144, 238, 144) },
1121 { "DarkGreen", grgb(0, 100, 0) },
1122 { "SeaGreen", grgb(46, 139, 87) },
1123
1124 { "Blue", grgb(0, 0, 255) },
1125 { "LightBlue", grgb(173, 216, 230) },
1126 { "DarkBlue", grgb(0, 0, 139) },
1127 { "SlateBlue", grgb(160, 90, 205) },
1128
1129 { "Cyan", grgb(0, 255, 255) },
1130 { "LightCyan", grgb(224, 255, 255) },
1131 { "DarkCyan", grgb(0, 139, 139) },
1132
1133 { "Magenta", grgb(255, 0, 255) },
1134 { "LightMagenta", grgb(255, 224, 255) },
1135 { "DarkMagenta", grgb(139, 0, 139) },
1136
1137 { "Yellow", grgb(255, 255, 0) },
1138 { "LightYellow", grgb(255, 255, 224) },
1139 { "DarkYellow", grgb(139, 139, 0) },
1140 { "Brown", grgb(165, 42, 42) },
1141
1142 { "Gray", grgb(190, 190, 190) },
1143 { "Grey", grgb(190, 190, 190) },
1144 { "LightGray", grgb(211, 211, 211) },
1145 { "LightGrey", grgb(211, 211, 211) },
1146 { "DarkGray", grgb(169, 169, 169) },
1147 { "DarkGrey", grgb(169, 169, 169) },
1148
1149 { "Black", grgb(0, 0, 0) },
1150 { "White", grgb(255, 255, 255) },
1151
1152 { "Orange", grgb(255, 165, 0) },
1153 { "Purple", grgb(160, 32, 240) },
1154 { "Violet", grgb(238, 130, 238) },
1155 {NULL, 0}
1156 };
1157
1158 if (name[0] == '#')
1159 {
1160 char *end;
1161 int c;
1162
1163 c = strtol(name + 1, &end, 16);
1164 return (guicolor_T) ((c >> 16) & 0xff) | (c & 0xff00) | ((c & 0xff) << 16);
1165 }
1166
1167 for (i = 0; colours[i].name != NULL; i++)
1168 {
1169 if (STRICMP(name, colours[i].name) == 0)
1170 return colours[i].value;
1171 }
1172 if (strnicmp(name, "grey", 4) == 0 || strnicmp(name, "gray", 4) == 0)
1173 {
1174 int level = (255 * atoi(name + 4)) / 100;
1175 return (guicolor_T) grgb(level, level, level);
1176 }
1177 return INVALCOLOR;
1178}
1179
1180/*
1181 * Set the current text colours.
1182 * If we are using fonts then set the antialiasing colours too.
1183 */
1184 void
1185gui_mch_set_colors(guicolor_T fg, guicolor_T bg)
1186{
1187 zap_redraw_colours[0] = bg << 8; /* JK230798, register new background colour */
1188 zap_redraw_colours[1] = fg << 8; /* JK230798, register new foreground colour */
1189 zap_redraw_update_colours = TRUE; /* JK230798, need update of colour masks */
1190
1191 swi(ColourTrans_ReturnGCOL, fg << 8);
1192 gui.fg_colour = r0;
1193 swi(ColourTrans_ReturnGCOL, bg << 8);
1194 gui.bg_colour = r0;
1195
1196 if (ro_current_font)
1197 swi(ColourTrans_SetFontColours, 0, bg << 8, fg << 8, 14);
1198}
1199
1200 void
1201ro_draw_string(x, y, s, len, flags, clip)
1202 int x; /* Top-left coord to plot at (x incl, y excl) */
1203 int y; /* (screen coords) */
1204 char_u *s; /* String to plot */
1205 int len; /* Length of string */
1206 int flags; /* DRAW_TRANSP, DRAW_BOLD, DRAW_UNDERL */
1207 int* clip; /* JK230798, added clip window */
1208{
1209 if (ro_current_font)
1210 {
1211 int fx;
1212 int flen = len; /* Preserve for underline */
1213
1214 /* Use the Font manager to paint the string.
1215 * Must do one char at a time to get monospacing.
1216 */
1217
1218 if (flags & DRAW_ITALIC && !gui.ital_font)
1219 flags |= DRAW_UNDERL; /* No italic - underline instead */
1220
1221 if ((flags & DRAW_TRANSP) == 0)
1222 {
1223 swi(ColourTrans_SetColour, gui.bg_colour, 0, 0, 0, 0);
1224 swi(OS_Plot, 4, x, y - gui.char_height);
1225 swi(OS_Plot, 96 + 5, x + len * gui.char_width - 1, y - 1);
1226 }
1227
1228 fx = x + font_x_offset;
1229 while (flen--)
1230 {
1231 swi(Font_Paint, 0, s++, 0x90, fx, y + font_y_offset, 0, 0, 1);
1232 fx += gui.char_width;
1233 }
1234 }
1235 else
1236 {
1237 if (zap_redraw)
1238 {
1239 /* Using fast Zap redraw. */
1240 flags = ro_zap_redraw_draw_string(x, y, s, len, flags, clip);
1241 }
1242 else
1243 {
1244 /* Using the system font */
1245 if (flags & DRAW_ITALIC)
1246 flags |= DRAW_UNDERL;
1247
1248 if ((flags & DRAW_TRANSP) == 0)
1249 {
1250 swi(ColourTrans_SetColour, gui.bg_colour, 0, 0, 0, 0);
1251 swi(OS_Plot, 4, x, y - gui.char_height);
1252 swi(OS_Plot, 96 + 5, x + len * gui.char_width - 1, y - 1);
1253 }
1254 swi(OS_Plot, 4, /* Move the drawing cursor */
1255 x,
1256 y - 1);
1257 swi(ColourTrans_SetColour, gui.fg_colour, 0, 0, 0, 0);
1258 swi(OS_WriteN, s, len);
1259
1260 if (flags & DRAW_BOLD)
1261 {
1262 swi(OS_Plot, 4, x + (1 << x_eigen_factor), y - 1);
1263 swi(OS_WriteN, s, len);
1264 }
1265 }
1266 }
1267
1268 if (flags & DRAW_UNDERL)
1269 {
1270 if (ro_current_font || zap_redraw)
1271 swi(ColourTrans_SetColour, gui.fg_colour, 0, 0, 0, 0);
1272 /* Underlined is the same with all plotting methods */
1273 swi(OS_Plot, 4, x, y - gui.char_height);
1274 swi(OS_Plot, 1, gui.char_width * len, 0);
1275 }
1276}
1277
1278 void
1279gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
1280{
1281 int x, y; /* Workarea x,y */
1282 x = col * gui.char_width;
1283 y = -row * gui.char_height;
1284
1285 if (redraw_block)
1286 {
1287 ro_draw_string(x + redraw_block[1], y + redraw_block[4],
1288 s, len, flags, &redraw_block[7]); /* JK230798, added clip window */
1289 }
1290 else
1291 {
1292 int block[44];
1293 block[0] = gui.window_handle;
1294 block[1] = x;
1295 block[2] = y - gui.char_height;
1296 block[3] = (col + len) * gui.char_width;
1297 block[4] = y;
1298 swi(Wimp_UpdateWindow, 0, block);
1299 while (r0)
1300 {
1301 ro_draw_string(x + block[1], y + block[4],
1302 s, len, flags, &block[7]); /* JK230798, added clip window */
1303 swi(Wimp_GetRectangle, 0, block);
1304 }
1305 }
1306}
1307
1308/*
1309 * Return OK if the key with the termcap name "name" is supported.
1310 */
1311 int
1312gui_mch_haskey(char_u *name)
1313{
1314 return FAIL;
1315}
1316
1317 void
1318gui_mch_beep(void)
1319{
1320 swi(OS_WriteI + 7);
1321}
1322
1323/*
1324 * Visual bell.
1325 */
1326 void
1327gui_mch_flash(int msec)
1328{
1329 /* TODO */
1330}
1331
1332
1333/*
1334 * Plot a solid rectangle using the given plot action and colour.
1335 * Coordinates are inclusive and window-relative.
1336 */
1337 void
1338plot_rectangle(plot, colour, minx, miny, maxx, maxy)
1339 int plot; /* OS_Plot action */
1340 int colour;
1341 int minx;
1342 int miny;
1343 int maxx;
1344 int maxy;
1345{
1346 if (redraw_block)
1347 {
1348 swi(ColourTrans_SetColour, colour, 0, 0, 0, 0);
1349 swi(OS_Plot, 4, minx + redraw_block[1], miny + redraw_block[4]);
1350 swi(OS_Plot, plot, maxx + redraw_block[1], maxy + redraw_block[4]);
1351 }
1352 else
1353 {
1354 int block[44];
1355 block[0] = gui.window_handle;
1356 block[1] = minx;
1357 block[2] = miny;
1358 block[3] = maxx + 1;
1359 block[4] = maxy + 1;
1360 swi(Wimp_UpdateWindow, 0, block);
1361 while (r0)
1362 {
1363 swi(ColourTrans_SetColour, colour, 0, 0, 0, 0);
1364 swi(OS_Plot, 4, minx + block[1], miny + block[4]);
1365 swi(OS_Plot, plot, maxx + block[1], maxy + block[4]);
1366 swi(Wimp_GetRectangle, 0, block);
1367 }
1368 }
1369}
1370
1371/*
1372 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1373 */
1374 void
1375gui_mch_invert_rectangle(int r, int c, int nr, int nc)
1376{
1377 plot_rectangle(96 + 6, 0, FILL_X(c), -FILL_Y(r + nr), FILL_X(c + nc), -FILL_Y(r));
1378}
1379
1380/*
1381 * Iconify the GUI window.
1382 */
1383 void
1384gui_mch_iconify(void)
1385{
1386}
1387
1388#if defined(FEAT_EVAL) || defined(PROTO)
1389/*
1390 * Bring the Vim window to the foreground.
1391 */
1392 void
1393gui_mch_set_foreground()
1394{
1395 /* TODO */
1396}
1397#endif
1398
1399/* Draw a hollow rectangle relative to the current
1400 * graphics cursor position, with the given width
1401 * and height. Start position is top-left.
1402 */
1403 void
1404draw_hollow(w, h)
1405 int w;
1406 int h;
1407{
1408 swi(OS_Plot, 1, w - 1, 0);
1409 swi(OS_Plot, 1, 0, 1 - h);
1410 swi(OS_Plot, 1, 1 - w, 0);
1411 swi(OS_Plot, 1, 0, h - 1);
1412}
1413
1414/*
1415 * Draw a cursor without focus.
1416 */
1417 void
1418gui_mch_draw_hollow_cursor(guicolor_T colour)
1419{
1420 int x = FILL_X(gui.cursor_col); /* Window relative, top-left */
1421 int y = -FILL_Y(gui.cursor_row);
1422 if (redraw_block == NULL)
1423 {
1424 int block[11];
1425
1426 block[0] = gui.window_handle;
1427 block[1] = x;
1428 block[2] = y - gui.char_height;
1429 block[3] = x + gui.char_width;
1430 block[4] = y;
1431 swi(Wimp_UpdateWindow, 0, block);
1432 while (r0)
1433 {
1434 swi(ColourTrans_SetGCOL, colour << 8, 0, 0, 0, 0);
1435
1436 swi(OS_Plot, 4, x + block[1], y + block[4] - 1);
1437 draw_hollow(gui.char_width, gui.char_height);
1438
1439 swi(Wimp_GetRectangle, 0, block);
1440 }
1441 }
1442 else
1443 {
1444 swi(ColourTrans_SetGCOL, colour << 8, 0, 0, 0, 0);
1445
1446 swi(OS_Plot, 4, x + redraw_block[1], y + redraw_block[4] - 1);
1447 draw_hollow(gui.char_width, gui.char_height);
1448 }
1449}
1450
1451/*
1452 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1453 * color "color".
1454 */
1455 void
1456gui_mch_draw_part_cursor(w, h, colour)
1457 int w;
1458 int h;
1459 guicolor_T colour;
1460{
1461 int x = FILL_X(gui.cursor_col);
1462 int y = -FILL_Y(gui.cursor_row);
1463 swi(ColourTrans_ReturnGCOL, colour << 8);
1464 plot_rectangle(96 + 5, r0, x, y - h, x + w - 1, y - 1);
1465}
1466
1467/*
1468 * Catch up with any queued events. This may put keyboard input into the
1469 * input buffer, call resize call-backs, trigger timers etc.
1470 * If there is nothing in the event queue(& no timers pending), then we return
1471 * immediately (well, after a Wimp_Poll).
1472 */
1473 void
1474gui_mch_update(void)
1475{
1476 int block[64];
1477 int reason;
1478
1479 swi(OS_ReadMonotonicTime);
1480 if ((r0 - time_of_last_poll) < 50)
1481 return; /* Don't return too often */
1482
1483 reason = wimp_poll(0, block);
1484 if (reason)
1485 process_event(reason, block);
1486 ro_return_early = FALSE; /* We're returning anyway. */
1487}
1488
1489 void
1490redraw_window(block)
1491 int *block;
1492{
1493 int x, y; /* Vim workarea coords */
1494 int width, height;
1495 int blank_col;
1496
1497 swi(ColourTrans_ReturnGCOL, UNUSED_COLOUR << 8, 0, 0, 1<<7, 0);
1498 blank_col = r0;
1499
1500 swi(Wimp_RedrawWindow, 0, block);
1501 redraw_block = block;
1502 while (r0)
1503 {
1504 x = block[7] - block[1];
1505 y = block[4] - block[10];
1506 width = block[9] - block[7];
1507 height = block[10] - block[8];
1508
1509 if (height + y > Rows * gui.char_height)
1510 {
1511 /* Blank everything off the bottom. */
1512 plot_rectangle(96 + 5, blank_col,
1513 0, block[8] - block[4],
1514 block[9] - block[1], -FILL_Y(Rows) - 1);
1515 height = Rows * gui.char_height;
1516 }
1517 if (width + x> Columns * gui.char_width)
1518 {
1519 /* Blank everything off to the right. */
1520 plot_rectangle(96 + 5, blank_col,
1521 FILL_X(Columns), block[8] - block[4],
1522 block[9] - block[1], 0);
1523 width = Columns * gui.char_width;
1524 }
1525 gui_redraw(x , y, width, height);
1526 swi(Wimp_GetRectangle, 0, block);
1527 }
1528 redraw_block = NULL;
1529}
1530
1531/* Check if we have modified data.
1532 * If we do then ack the message to stop the shutdown.
1533 * Otherwise, ignore the message.
1534 */
1535 void
1536ro_prequit(block)
1537 int *block;
1538{
1539 if (!ro_ok_to_quit())
1540 {
1541 /* Not OK to quit - stop shutdown */
1542 block[3] = block[2];
1543 swi(Wimp_SendMessage, 19, block, block[1]);
1544 }
1545 /* Do nothing. We may get a Message_Quit later. */
1546}
1547
1548/* If there is unsaved data then ask the user if they mind losing it.
1549 * Return TRUE if we can quit without saving, FALSE to halt the
1550 * shutdown.
1551 */
1552 int
1553ro_ok_to_quit()
1554{
1555 int old_confirm = cmdmod.confirm;
1556
1557 cmdmod.confirm = FALSE; /* Use our own, single tasking, box */
1558
1559 if (check_changed_any(FALSE))
1560 {
1561 swi(Wimp_ReportError,
1562 "\0\0\0\0Vim contains unsaved data - quit anyway?",
1563 0x17,
1564 "Vim");
1565 cmdmod.confirm = old_confirm;
1566 if (r1 != 1)
1567 return FALSE;
1568 }
1569 cmdmod.confirm = old_confirm;
1570 return TRUE;
1571}
1572
1573/* Quit without checking for unsaved data. */
1574 void
1575ro_quit()
1576{
1577 exiting = TRUE;
1578 getout(0);
1579
1580 exiting = FALSE; /* probably can't get here */
1581 setcursor(); /* position cursor */
1582 out_flush();
1583}
1584
1585/* Insent the given vim special code into the input buffer */
1586 void
1587ro_press(a, b, modifier)
1588 char a;
1589 char b;
1590 int modifier; /* %<Ctrl><Shift> 0000 0000 */
1591{
1592 char_u buf[6];
1593 int vim_mod;
1594 int key;
1595
1596
1597 /* Convert RISC OS modifier to Vim modifier. */
1598 vim_mod = ((modifier & 0x10) ? MOD_MASK_SHIFT : 0)
1599 | ((modifier & 0x20) ? MOD_MASK_CTRL : 0);
1600 key = simplify_key(TERMCAP2KEY(a, b), &vim_mod);
1601
1602 buf[3] = CSI;
1603 buf[4] = KEY2TERMCAP0(key);
1604 buf[5] = KEY2TERMCAP1(key);
1605 if (vim_mod)
1606 {
1607 buf[0] = CSI;
1608 buf[1] = KS_MODIFIER;
1609 buf[2] = vim_mod;
1610 add_to_input_buf(buf, 6);
1611 }
1612 else
1613 add_to_input_buf(buf + 3, 3);
1614}
1615
1616/* Take a wimp key code and insert the vim equivalent
1617 * into vim's input buffer.
1618 * CTRL-C also sets got_int.
1619 */
1620 void
1621ro_insert_key(code)
1622 char_u *code; /* Wimp_ProcessKey code (4 bytes) */
1623{
1624 char a = code[0];
1625 char b = code[1];
1626 int base, modifier;
1627
1628 if (a == 3 && ctrl_c_interrupts)
1629 got_int = TRUE;
1630
1631 /* Is it a normal key? */
1632 if (a > 31 && a < 127)
1633 {
1634 add_to_input_buf(code, 1);
1635 return;
1636 }
1637
1638 /* We should pass any unrecognised keys on, but
1639 * for now just pass on F12 combinations.
1640 */
1641 switch (b)
1642 {
1643 case 0:
1644 /* Home and Delete are the only special cases */
1645 switch (a)
1646 {
1647 case 0x1e:
1648 ro_press('k','h', 0); /* Home */
1649 return;
1650 case 0x7f:
1651 ro_press('k','D', 0); /* Delete */
1652 return;
1653 case CSI:
1654 {
1655 /* Turn CSI into K_CSI. Untested! */
1656 char_u string[3] = {CSI, KS_EXTRA, KE_CSI};
1657
1658 add_to_input_buf(string, 3);
1659 return;
1660 }
1661 default:
1662 add_to_input_buf(code, 1);
1663 return;
1664 }
1665 case 1:
1666 if ((a & 0xcf) == 0xcc)
1667 {
1668 /* F12 pressed - pass it on (quick hack) */
1669 swi(Wimp_ProcessKey, a | 0x100);
1670 return;
1671 }
1672 base = a & 0xcf;
1673 modifier = a & 0x30;
1674 switch (base)
1675 {
1676 case 0x8a: /* Tab */
1677 add_to_input_buf("\011", 1);
1678 return;
1679 case 0x8b: /* Copy (End) */
1680 return ro_press('@', '7', modifier);
1681 case 0x8c: /* Left */
1682 return ro_press('k', 'l', modifier);
1683 case 0x8d: /* Right */
1684 return ro_press('k', 'r', modifier);
1685 case 0x8e: /* Down */
1686 if (modifier & 0x10)
1687 return ro_press('k', 'N', modifier ^ 0x10);
1688 else
1689 return ro_press('k', 'd', modifier);
1690 case 0x8f: /* Up */
1691 if (modifier & 0x10)
1692 return ro_press('k', 'P', modifier ^ 0x10);
1693 else
1694 return ro_press('k', 'u', modifier);
1695 case 0xca: /* F10 */
1696 return ro_press('k', ';', modifier);
1697 case 0xcb: /* F11 */
1698 return ro_press('F', '1', modifier);
1699 case 0xcd: /* Insert */
1700 return ro_press('k', 'I', modifier);
1701 default:
1702 if (base > 0x80 && base < 0x18a)
1703 {
1704 /* One of the other function keys */
1705 return ro_press('k', '0' + (base & 15), modifier);
1706 }
1707 }
1708 }
1709}
1710
1711/* Process a mouse event. */
1712 void
1713ro_mouse(block)
1714 int *block;
1715{
1716 int x, y, button, vim_button;
1717 int modifiers = 0;
1718 int min_x, min_y; /* Visible area of editor window */
1719 int max_x, max_y;
1720
1721 if (block[3] != gui.window_handle || ro_dragging)
1722 return; /* Not our window or ignoring clicks*/
1723
1724 x = block[0]; /* Click position - screen coords */
1725 y = block[1];
1726 button = block[2];
1727
1728 block[0] = gui.window_handle;
1729 swi(Wimp_GetWindowState, 0, block);
1730 min_x = block[1];
1731 min_y = block[2];
1732 max_x = block[3];
1733 max_y = block[4];
1734
1735 if (block[3] - x < gui.scrollbar_width)
1736 {
1737 /* Click in that blank area under the scrollbars */
1738
1739 if (button & 0x444)
1740 {
1741 int front_block[10];
1742 /* Dragging with Select - bring window to front first */
1743 front_block[0] = gui.window_handle;
1744 swi(Wimp_GetWindowState, 0, front_block);
1745 front_block[7] = -1;
1746 ro_open_main(front_block);
1747 }
1748
1749 block[0] = gui.window_handle;
1750 block[1] = 7; /* Drag point */
1751 block[2] = block[4] = 0; /* Coords of point. */
1752 block[3] = block[5] = 0;
1753 drag_x_offset = max_x - x;
1754 drag_y_offset = min_y - y;
1755
1756 /* Parent box. */
1757 block[6] = min_x +
1758 gui.scrollbar_width * 2 +
1759 MIN_COLUMNS * gui.char_width;
1760 block[7] = 0;
1761 gui_mch_get_screen_dimensions(&block[8], &block[9]);
1762 block[9] = max_y -
1763 4 * gui.char_height -
1764 gui.scrollbar_height;
1765
1766 swi(Wimp_DragBox, 0, block);
1767 ro_dragging = DRAG_RESIZE_WINDOW;
1768 drag_button = vim_button;
1769 drag_modifiers = modifiers;
1770 return;
1771 }
1772
1773 if (button & 0x111)
1774 vim_button = MOUSE_RIGHT;
1775 else if (button & 0x222)
1776 vim_button = MOUSE_MIDDLE;
1777 else
1778 vim_button = MOUSE_LEFT;
1779
1780 swi(OS_Byte, 121, 0x80);
1781 if (r1 == 0xff)
1782 modifiers |= MOUSE_SHIFT;
1783 swi(OS_Byte, 121, 0x81);
1784 if (r1 == 0xff)
1785 modifiers |= MOUSE_CTRL;
1786 swi(OS_Byte, 121, 0x82);
1787 if (r1 == 0xff)
1788 modifiers |= MOUSE_ALT;
1789
1790 if (button == 2)
1791 {
1792 /* Menu click:
1793 * If shift was pressed then do the paste action.
1794 * If not, then open the pop-up menu.
1795 */
1796 modifiers ^= MOUSE_SHIFT;
1797 if (modifiers && MOUSE_SHIFT)
1798 {
1799 vimmenu_T main;
1800 /* Shift was NOT pressed - show menu */
1801 main.dname = (char_u *) "Vim";
1802 main.children = root_menu;
1803 gui_mch_show_popupmenu(&main);
1804 return;
1805 }
1806 }
1807
1808 /* Gain the input focus */
1809 swi(Wimp_SetCaretPosition, gui.window_handle, -1, 0, 0, -1, -1);
1810
1811 if (button & 0xf0)
1812 {
1813 /* Drag operation:
1814 *
1815 * Tell the Wimp to start a drag.
1816 * Monitor null events.
1817 */
1818 block[1] = 7; /* Drag a point. */
1819 block[2] = block[4] = x; /* Coords of point. */
1820 block[3] = block[5] = y;
1821 block[6] = 0; /* Coords of bounding box. */
1822 block[7] = 0;
1823 gui_mch_get_screen_dimensions(&block[8], &block[9]);
1824
1825 drag_x_offset = drag_y_offset = 0;
1826
1827 swi(Wimp_DragBox, 0, block);
1828 ro_dragging = DRAG_SELECTION;
1829 drag_button = vim_button;
1830 drag_modifiers = modifiers;
1831
1832 vim_button |= MOUSE_DRAG;
1833 }
1834
1835 gui_send_mouse_event(
1836 vim_button,
1837 x - min_x,
1838 max_y - y,
1839 button & 0xf ? TRUE : FALSE, /* dclick */
1840 modifiers);
1841}
1842
1843 void
1844ro_continue_drag(block)
1845 int *block; /* Just used as scrap. */
1846{
1847 int x, y;
1848
1849 /* Get screen coords of pointer. */
1850 swi(Wimp_GetPointerInfo, 0, block);
1851 x = block[0] + drag_x_offset;
1852 y = block[1] + drag_y_offset;
1853
1854 block[0] = gui.window_handle;
1855 swi(Wimp_GetWindowState, 0, block);
1856
1857 if (ro_dragging == DRAG_RESIZE_WINDOW)
1858 {
1859 /* Resizeing the main window. */
1860 block[2] = y;
1861 block[3] = x;
1862 ro_open_main(block);
1863 }
1864 else
1865 {
1866 /* Selecting some text. */
1867 gui_send_mouse_event(
1868 drag_button | MOUSE_DRAG, /* Always report the same button */
1869 x - block[1],
1870 block[4] - y,
1871 FALSE, /* Not a double click. */
1872 drag_modifiers);
1873 }
1874}
1875
1876/* User has released all mouse buttons, marking the end of a drag. */
1877 void
1878ro_drag_finished(block)
1879 int *block;
1880{
1881 int x;
1882 int y;
1883 int width, height;
1884
1885 /* I don't trust the box returned by Wimp_Poll; look at the pointer
1886 * ourselves.
1887 */
1888 swi(Wimp_GetPointerInfo, 0, block);
1889 x = block[0] + drag_x_offset;
1890 y = block[1] + drag_y_offset;
1891
1892 if (ro_dragging == DRAG_RESIZE_WINDOW)
1893 {
1894 block[0] = gui.window_handle;
1895 swi(Wimp_GetWindowState, 0, block);
1896 block[2] = y;
1897 block[3] = x;
1898 ro_open_main(block);
1899
1900 width = (block[3] - block[1]);
1901 height = (block[4] - block[2]);
1902
1903 swi(Wimp_ForceRedraw, gui.window_handle, 0, -height, width, 0);
1904 gui_resize_shell(width, height);
1905 }
1906 else
1907 {
1908 block[0] = gui.window_handle;
1909 swi(Wimp_GetWindowState, 0, block);
1910 gui_send_mouse_event(
1911 MOUSE_RELEASE,
1912 x - block[1],
1913 block[4] - y,
1914 FALSE, /* not a double click */
1915 drag_modifiers);
1916 }
1917 ro_dragging = DRAG_FALSE;
1918}
1919
1920/* Load the file/pathname given in block into a [new] buffer.
1921 *
1922 * Modifier Action
1923 *
1924 * None :confirm e <file>
1925 * Ctrl :sp <file>
1926 * Shift <file>
1927 *
1928 * Insert into typebuf, at the start.
1929 * If loading from !Scrap then use saved leafname instead, and
1930 * delete the scrap file. Also, ignore shift key.
1931 *
1932 * NB: Doesn't send DataLoadAck (other app might delete temp file?).
1933 */
1934 void
1935ro_dataload(block)
1936 int *block;
1937{
1938 char_u new_path[MAXPATHL];
1939 char_u *path = ((char_u *) block) + 44;
1940 int scrap = FALSE;
1941
1942 if (block[3] == leaf_ref && leaf_name)
1943 scrap = TRUE;
1944
1945 switch (get_real_state() & 0xff)
1946 {
1947 case INSERT:
1948 case CMDLINE:
1949 case CMDLINE+LANGMAP:
1950 /* For insert mode we can only insert the pathname (currently)
1951 * Make sure Shift is pressed.
1952 */
1953 swi(OS_Byte, 121, 0x80); /* Is Shift pressed? */
1954 if (r1 == 0xff)
1955 {
1956 ins_typebuf(" ", REMAP_NONE, 0, TRUE, FALSE);
1957 ins_typebuf(path, REMAP_NONE, 0, TRUE, FALSE);
1958 ro_return_early = TRUE; /* Return even though nothing was typed. */
1959 }
1960 else
1961 swi(Wimp_ReportError,
1962 "\0\0\0\0Sorry, you can only load text in normal mode", 5, "Vim");
1963 break;
1964
1965 case NORMAL:
1966 ro_return_early = TRUE; /* Return even though nothing was typed. */
1967
1968 if (scrap) /* Remove <Wimp$Scrap>. Later. */
1969 ins_typebuf(":!~remove <Wimp$Scrap>\r", REMAP_NONE, 0, TRUE, FALSE);
1970
1971 /* Insert {:sp ,:confirm e }[+f\ <leaf> ]<file><CR> */
1972 ins_typebuf("\r", REMAP_NONE, 0, TRUE, FALSE);
1973 ins_typebuf(path, REMAP_NONE, 0, TRUE, FALSE);
1974 ins_typebuf(" ", REMAP_NONE, 0, TRUE, FALSE);
1975
1976 if (scrap)
1977 {
1978 /* Loading via !Scrap - change pathname to stored leafname */
1979 ins_typebuf(leaf_name, REMAP_NONE, 0, TRUE, FALSE);
1980 ins_typebuf(" +f\\ ", REMAP_NONE, 0, TRUE, FALSE);
1981 leaf_ref = 0;
1982 vim_free(leaf_name);
1983 leaf_name = NULL;
1984 }
1985
1986 swi(OS_Byte, 121, 0x81); /* Is Ctrl pressed? */
1987 if (r1 == 0xff)
1988 /* Yes, split window */
1989 ins_typebuf(":sp", REMAP_NONE, 0, TRUE, FALSE);
1990 else
1991 ins_typebuf(":confirm e", REMAP_NONE, 0, TRUE, FALSE);
1992 break;
1993
1994 default:
1995 swi(Wimp_ReportError, "\0\0\0\0You can only load text in normal mode.", 5, "Vim");
1996 }
1997 /* Send DataSaveAck so other program doesn't think we died
1998 * and delete <Wimp$Scrap>.
1999 */
2000 block[3] = block[2];
2001 block[4] = 4;
2002 swi(Wimp_SendMessage, 17, block, block[1]);
2003}
2004
2005 void
2006ro_datasave(block)
2007 int *block;
2008{
2009 char_u *path = ((char_u *) block) + 44;
2010
2011 /* Preserve the name given so we can use it, not <Wimp$Scrap> */
2012 if (leaf_name)
2013 vim_free(leaf_name);
2014 leaf_name = vim_strsave(path);
2015
2016 block[9] = -1; /* File is unsafe. */
2017 strcpy(path, "<Wimp$Scrap>");
2018 block[0] = 60;
2019 block[3] = block[2];
2020 block[4] = 2;
2021 swi(Wimp_SendMessage, 17, block, block[1]);
2022
2023 leaf_ref = block[2];
2024}
2025
2026 void
2027ro_message(block)
2028 int *block;
2029{
2030 char_u *buffer;
2031 long_u len;
2032
2033 if (block[1] == task_handle)
2034 return; /* Don't talk to ourself! */
2035 switch (block[4])
2036 {
2037 case 0: /* Quit. */
2038 if (block[4] == 0)
2039 ro_quit();
2040 break;
2041 case 1: /* DataSave */
2042 ro_datasave(block);
2043 break;
2044 case 2: /* DataSaveAck. */
2045 if (clip_convert_selection(&buffer, &len, &clip_star) == -1)
2046 return;
2047
2048 /* Save the clipboard contents to a file. */
2049 swi(OS_File, 10, ((char_u *) block) + 44, 0xfff, 0, buffer, buffer + len);
2050
2051 /* Ack with DataLoad message. */
2052 block[3] = block[2];
2053 block[4] = 3;
2054 block[9] = len;
2055 swi(Wimp_SendMessage, 17, block, block[1]);
2056
2057 vim_free(buffer);
2058 break;
2059 case 3: /* DataLoad */
2060 ro_dataload(block);
2061 break;
2062 case 8: /* PreQuit */
2063 ro_prequit(block);
2064 break;
2065 case 0xf: /* Lose clipboard. */
2066 if (block[5] & 4)
2067 {
2068 clip_free_selection(&clip_star);
2069 clip_star.owned = FALSE;
2070 }
2071 break;
2072 case 0x10: /* DataRequest (clip_star) */
2073 if (clip_star.owned)
2074 {
2075 int rows;
2076
2077 /* Tell other program that we have the clipboard. */
2078 block[0] = 52;
2079 block[3] = block[2]; /* Copy myref to yourref. */
2080 block[4] = 1; /* DataSave message. */
2081 /* Create an estimate for the size (larger or same as true
2082 * value) */
2083 rows = clip_star.end.lnum - clip_star.start.lnum;
2084 if (rows < 0)
2085 rows = -rows;
2086 block[9] = (rows + 1) * Columns + 1; /* Add one for possible
2087 final newline. */
2088 block[10] = 0xfff; /* Clipboard is text. */
2089 strcpy( ((char_u *) block) + 44, "VimClip");
2090 swi(Wimp_SendMessage, 17, block, block[1]);
2091 }
2092 break;
2093 case 0x400c1: /* Mode change */
2094 changed_mode = TRUE; /* Flag - update on next OpenWindow */
2095 if (zap_redraw)
2096 {
2097 /* JK230798, re-initialise ZapRedraw stuff */
2098 if (ro_zap_redraw_initialise() == FAIL)
2099 zap_redraw = FALSE;
2100 }
2101 break;
2102 case 0x400c3: /* TaskCloseDown */
2103 if (block[1] == child_handle)
2104 child_handle = 0;
2105 break;
2106 }
2107}
2108
2109/*
2110 * Converts a scrollbar's window handle into a scrollbar pointer.
2111 * NULL on failure.
2112 */
2113 scrollbar_T *
2114ro_find_sbar(id)
2115 int id;
2116{
2117 win_T *wp;
2118
2119 if (gui.bottom_sbar.id == id)
2120 return &gui.bottom_sbar;
2121 FOR_ALL_WINDOWS(wp)
2122 {
2123 if (wp->w_scrollbars[SBAR_LEFT].id == id)
2124 return &wp->w_scrollbars[SBAR_LEFT];
2125 if (wp->w_scrollbars[SBAR_RIGHT].id == id)
2126 return &wp->w_scrollbars[SBAR_RIGHT];
2127 }
2128 return NULL;
2129}
2130
2131 void
2132scroll_to(line, sb)
2133 int sb; /* Scrollbar number */
2134 int line;
2135{
2136 char_u code[8];
2137
2138 /* Don't put events in the input queue now. */
2139 if (hold_gui_events)
2140 return;
2141
2142 /* Send a scroll event:
2143 *
2144 * A scrollbar event is CSI (NOT K_SPECIAL), KS_VER_SCROLLBAR,
2145 * KE_FILLER followed by:
2146 * one byte representing the scrollbar number, and then four bytes
2147 * representing a long_u which is the new value of the scrollbar.
2148 */
2149 code[0] = CSI;
2150 code[1] = KS_VER_SCROLLBAR;
2151 code[2] = KE_FILLER;
2152 code[3] = sb;
2153 code[4] = line >> 24;
2154 code[5] = line >> 16;
2155 code[6] = line >> 8;
2156 code[7] = line;
2157 add_to_input_buf(code, 8);
2158}
2159
2160 void
2161h_scroll_to(col)
2162 int col;
2163{
2164 char_u code[8];
2165
2166 /* Don't put events in the input queue now. */
2167 if (hold_gui_events)
2168 return;
2169
2170 /* Send a scroll event:
2171 *
2172 * A scrollbar event is CSI (NOT K_SPECIAL)
2173 *
2174 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
2175 * KE_FILLER followed by four bytes representing a long_u which is the
2176 * new value of the scrollbar.
2177 */
2178 code[0] = CSI;
2179 code[1] = KS_HOR_SCROLLBAR;
2180 code[2] = KE_FILLER;
2181 code[4] = col >> 24;
2182 code[5] = col >> 16;
2183 code[6] = col >> 8;
2184 code[7] = col;
2185 add_to_input_buf(code, 8);
2186}
2187
2188 void
2189ro_scroll(block)
2190 int *block;
2191{
2192 scrollbar_T *sb;
2193 int offset;
2194 win_T *wp;
2195
2196 /* Block is ready for Wimp_OpenWindow, and also contains:
2197 *
2198 * +32 = scroll X direction (-2 .. +2)
2199 * +36 = scroll Y direction (-2 .. +2)
2200 */
2201
2202 sb = ro_find_sbar(block[0]);
2203 if (!sb)
2204 return; /* Window not found (error). */
2205
2206 wp = sb-> wp;
2207
2208 if (wp == NULL)
2209 {
2210 /* Horizontal bar. */
2211 offset = block[8];
2212 if (offset == -2)
2213 offset = (block[1] - block[3]) / gui.char_width;
2214 else if (offset == 2)
2215 offset = (block[3] - block[1]) / gui.char_width;
2216
2217 block[5] += offset * gui.char_width;
2218
2219 gui_drag_scrollbar(sb, block[5] / gui.char_width, FALSE);
2220
2221 swi(Wimp_OpenWindow, 0, block);
2222 }
2223 else
2224 {
2225 offset = -block[9];
2226 if (offset == -2)
2227 offset = -(wp -> w_height - 1);
2228 else if (offset == 2)
2229 offset = wp -> w_height - 1;
2230
2231 /* Possibly we should reposition the scrollbar?
2232 * Vim seems to update the bar anyway...
2233 */
2234 gui_drag_scrollbar(sb, offset - (block[6] / gui.char_height), FALSE);
2235 }
2236}
2237
2238/* Move a window by a given offset. Used to simulate the function of the
2239 * nested wimp.
2240 */
2241 void
2242ro_move_child(window, x, y, pos_wanted, pos_got)
2243 int window;
2244 int x,y; /* offset to move by */
2245 int pos_wanted, pos_got;
2246{
2247 int block[10];
2248
2249 block[0] = window;
2250 swi(Wimp_GetWindowState, 0, block);
2251 block[1] += x;
2252 block[2] += y;
2253 block[3] += x;
2254 block[4] += y;
2255 if (pos_wanted == -1)
2256 block[7] = -1;
2257 else if (pos_wanted == -2)
2258 block[7] = pos_got;
2259 swi(Wimp_OpenWindow, 0, block);
2260}
2261
2262/* Open the main window. Also updates scrollbars if we are not
2263 * using the nested Wimp.
2264 * If we have just changed mode then re-read all values.
2265 */
2266 void
2267ro_open_main(block)
2268 int *block;
2269{
2270 int toggle_size;
2271
2272 /* Find out if the user clicked on the toggle size icon. */
2273 block[20] = block[0];
2274 swi(Wimp_GetWindowState, 0, block + 20);
2275 toggle_size = block[28] & (1 << 19);
2276
2277 if (nested_wimp)
2278 {
2279 swi(Wimp_OpenWindow, 0, block);
2280 }
2281 else
2282 {
2283 int old[10];
2284 int x_offset, y_offset; /* Move children same as parent. */
2285 int pos_wanted, pos_got;
2286 int left_bar = gui.which_scrollbars[SBAR_LEFT];
2287 int right_bar = gui.which_scrollbars[SBAR_RIGHT];
2288 win_T *wp;
2289
2290 /* Three cases to think about:
2291 * 1) Move to top. Open each window at the top.
2292 * 2) Same stack position. Open each with same position.
2293 * 3) Open at bottom. Open children with parent's new position.
2294 */
2295
2296 old[0] = block[0];
2297 swi(Wimp_GetWindowState, 0, old);
2298 pos_wanted = block[7];
2299 swi(Wimp_OpenWindow, 0, block);
2300 /* Block updated by OpenWindow? I don't think so! */
2301 swi(Wimp_GetWindowState, 0, block);
2302 pos_got = block[7];
2303
2304 x_offset = block[1] - old[1];
2305 y_offset = block[4] - old[4];
2306 if (x_offset || y_offset || pos_wanted == -1 || pos_wanted == -2)
2307 {
2308 /* If parent has moved, re-open all the child windows. */
2309 FOR_ALL_WINDOWS(wp)
2310 {
2311 /* Reopen scrollbars for this window. */
2312 if (left_bar)
2313 ro_move_child(wp -> w_scrollbars[SBAR_LEFT].id,
2314 x_offset, y_offset,
2315 pos_wanted, pos_got);
2316 if (right_bar)
2317 ro_move_child(wp -> w_scrollbars[SBAR_RIGHT].id,
2318 x_offset, y_offset,
2319 pos_wanted, pos_got);
2320 }
2321 }
2322 }
2323 if (changed_mode || toggle_size)
2324 {
2325 int width, height;
2326
2327 if (changed_mode)
2328 ro_measure_tools();
2329 block[0] = gui.window_handle;
2330 swi(Wimp_GetWindowState, 0, block);
2331
2332 width = block[3] - block[1];
2333 height = block[4] - block[2];
2334 swi(Wimp_ForceRedraw, gui.window_handle, 0, -height, width, 0);
2335 gui_resize_shell(width, height);
2336 changed_mode = FALSE;
2337 }
2338}
2339
2340 void
2341ro_open_window(block)
2342 int *block;
2343{
2344 int pos;
2345 scrollbar_T *sb;
2346
2347 if (block[0] == gui.window_handle)
2348 ro_open_main(block);
2349 else
2350 {
2351 swi(Wimp_OpenWindow, 0, block);
2352 if (block[0] != gui.window_handle)
2353 {
2354 sb = ro_find_sbar(block[0]);
2355 if (sb)
2356 {
2357 if (sb-> wp != NULL)
2358 gui_drag_scrollbar(sb, -block[6] / gui.char_height, FALSE);
2359 else
2360 gui_drag_scrollbar(sb, block[5] / gui.char_width, FALSE);
2361 }
2362 }
2363 }
2364}
2365
2366 void
2367ro_menu_selection(block)
2368 int *block;
2369{
2370 int *item = wimp_menu + 7;
2371 vimmenu_T *menu;
2372 /* wimp_menu points to a wimp menu structure */
2373
2374 for (;;)
2375 {
2376 while (block[0]--)
2377 item += 6;
2378 if (block[1] == -1)
2379 break;
2380 item = ((int *) item[1]) + 7;
2381 block++;
2382 }
2383 /* item points to the wimp menu item structure chosen */
2384 menu = (vimmenu_T *) item[5];
2385
2386 swi(Wimp_GetPointerInfo, 0, block);
2387 if (block[2] == 1)
2388 /* Adjust used - keep menu open */
2389 swi(Wimp_CreateMenu, 0, wimp_menu);
2390
2391 if (menu-> cb)
2392 menu-> cb(menu);
2393}
2394
2395 void
2396ro_open_parent()
2397{
2398 int head;
2399 char_u *i = curbuf-> b_ffname;
2400 char_u buffer[256];
2401
2402 head = 0;
2403 for (; *i; i++)
2404 {
2405 if (*i == '.')
2406 head = i - curbuf-> b_ffname;
2407 }
2408
2409 /* Append head chars to buffer */
2410 if (head < 240 && curbuf-> b_ffname && head)
2411 {
2412 strcpy(buffer, "%filer_opendir ");
2413 strncpy(buffer + 15, curbuf-> b_ffname, head);
2414 buffer[15 + head] = '\0';
2415 swi(OS_CLI, buffer);
2416 }
2417}
2418
2419 void
2420process_event(event, block)
2421 int event;
2422 int *block;
2423{
2424 switch (event)
2425 {
2426 case 0: /* Nothing - update drag state. */
2427 if (ro_dragging)
2428 ro_continue_drag(block);
2429 break;
2430 case 1: /* Redraw window. */
2431 redraw_window(block);
2432 break;
2433 case 2: /* Open window. */
2434 ro_open_window(block);
2435 break;
2436 case 3: /* Close window. */
2437 swi(Wimp_GetPointerInfo, 0, block + 1);
2438 if (block[3] == 1)
2439 ro_open_parent();
2440 else
2441 if (ro_ok_to_quit())
2442 ro_quit();
2443 break;
2444 case 6: /* Mouse click. */
2445 ro_mouse(block);
2446 break;
2447 case 7: /* Finished drag. */
2448 ro_drag_finished(block);
2449 break;
2450 case 8: /* Key pressed. */
2451 ro_insert_key((char_u *) &block[6]);
2452 break;
2453 case 9:
2454 ro_menu_selection(block);
2455 break;
2456 case 10: /* Scroll request. */
2457 ro_scroll(block);
2458 break;
2459 case 11: /* Lose caret. */
2460 if (block[0] == gui.window_handle)
2461 gui_focus_change(FALSE);
2462 break;
2463 case 12: /* Gain caret. */
2464 if (block[0] == gui.window_handle)
2465 gui_focus_change(TRUE);
2466 break;
2467 case 17: /* User message. */
2468 case 18: /* User message recorded. */
2469 ro_message(block);
2470 break;
2471 }
2472}
2473
2474/*
2475 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2476 * from the keyboard.
2477 * wtime == -1 Wait forever.
2478 * wtime == 0 This should never happen.
2479 * wtime > 0 Wait wtime milliseconds for a character.
2480 * Returns OK if a character was found to be available within the given time,
2481 * or FAIL otherwise.
2482 */
2483 int
2484gui_mch_wait_for_chars(long wtime)
2485{
2486 int block[64];
2487 int reason;
2488 int start_time = -1;
2489 int ctime = wtime / 10; /* delay in cs */
2490
2491 if (wtime != -1)
2492 {
2493 swi(OS_ReadMonotonicTime);
2494 start_time = r0;
2495 }
2496
2497 for (;;)
2498 {
2499 if (ro_dragging)
2500 reason = wimp_poll(0, block); /* Always return immediately */
2501 else if (wtime == -1)
2502 reason = wimp_poll(1, block);
2503 else
2504 reason = wimp_pollidle(0, block, start_time + ctime);
2505
2506 process_event(reason, block);
2507
2508 if (input_available() || ro_return_early)
2509 {
2510 ro_return_early = FALSE;
2511 return OK; /* There is something to process (key / menu event) */
2512 }
2513
2514 if (wtime != -1)
2515 {
2516 swi(OS_ReadMonotonicTime);
2517 if (r0 - start_time > ctime)
2518 return FAIL; /* We've been waiting too long - return failure */
2519 }
2520 }
2521}
2522
2523/* Flush any output to the screen */
2524 void
2525gui_mch_flush(void)
2526{
2527}
2528
2529/*
2530 * Clear a rectangular region of the screen from text pos(row1, col1) to
2531 * (row2, col2) inclusive.
2532 */
2533 void
2534gui_mch_clear_block(int row1, int col1, int row2, int col2)
2535{
2536 swi(ColourTrans_ReturnGCOL, gui.back_pixel << 8, 0, 0, 1<<7, 0);
2537 plot_rectangle(96 + 5, r0,
2538 FILL_X(col1), -FILL_Y(row2 + 1),
2539 FILL_X(col2 + 1), -FILL_Y(row1));
2540}
2541
2542 void
2543gui_mch_clear_all(void)
2544{
2545 if (redraw_block)
2546 {
2547 swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 1<<7, 0);
2548 swi(OS_WriteI + 16);
2549 }
2550 else
2551 {
2552 int block[44];
2553 block[0] = gui.window_handle;
2554 block[1] = 0;
2555 block[2] = -gui.num_rows * gui.char_height;
2556 block[3] = gui.num_cols * gui.char_width;
2557 block[4] = 0;
2558 swi(Wimp_UpdateWindow, 0, block);
2559 while (r0)
2560 {
2561 swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 1<<7, 0);
2562 swi(OS_WriteI + 16);
2563 swi(Wimp_GetRectangle, 0, block);
2564 }
2565 }
2566}
2567
2568/*
2569 * Delete the given number of lines from the given row, scrolling up any
2570 * text further down within the scroll region.
2571 */
2572 void
2573gui_mch_delete_lines(int row, int num_lines)
2574{
2575 int top_from = -row - num_lines;
2576 int bot_from = -gui.scroll_region_bot - 1;
2577 int bot_to = bot_from + num_lines;
2578
2579 swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 0x80, 0);
2580
2581 /* Changed without checking! */
2582 swi(Wimp_BlockCopy, gui.window_handle,
2583 gui.scroll_region_left * gui.char_width,
2584 bot_from * gui.char_height,
2585 (gui.scroll_region_right - gui.scroll_region_left
2586 + 1) * gui.char_width,
2587 top_from * gui.char_height,
2588
2589 gui.scroll_region_left * gui.char_width,
2590 bot_to * gui.char_height);
2591
2592 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2593 gui.scroll_region_left,
2594 gui.scroll_region_bot, gui.scroll_region_right);
2595}
2596
2597/*
2598 * Insert the given number of lines before the given row, scrolling down any
2599 * following text within the scroll region.
2600 */
2601 void
2602gui_mch_insert_lines(int row, int num_lines)
2603{
2604 int top_from = -row;
2605 int bot_to = -gui.scroll_region_bot - 1;
2606 int bot_from = bot_to + num_lines;
2607
2608 swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 0x80, 0);
2609
2610 swi(Wimp_BlockCopy, gui.window_handle,
2611 gui.scroll_region_left * gui.char_width,
2612 bot_from * gui.char_height,
2613 (gui.scroll_region_right - gui.scroll_region_left
2614 + 1) * gui.char_width,
2615 top_from * gui.char_height,
2616
2617 gui.scroll_region_left * gui.char_width,
2618 bot_to * gui.char_height);
2619
2620 gui_clear_block(row, gui.scroll_region_left,
2621 row + num_lines - 1, gui.scroll_region_right);
2622}
2623
2624/* Put selection in clipboard buffer.
2625 * Should we become the new owner?
2626 */
2627 void
2628clip_mch_request_selection(VimClipboard *cbd)
2629{
2630 int block[64]; /* Will be used in Wimp_Poll. */
2631 int reason;
2632 char_u *buffer;
2633 long_u length;
2634
2635 block[0] = 48; /* Size of block. */
2636 block[3] = 0; /* Orinial message. */
2637 block[4] = 0x10; /* Data request. */
2638 block[5] = gui.window_handle;
2639 block[6] = RO_LOAD_CLIPBOARD; /* Internal handle. */
2640 block[7] = block[8] = 0; /* (x,y) not used. */
2641 block[9] = 4;
2642 block[10] = 0xfff; /* We want text files if possible, I think. */
2643 block[11] = -1; /* End of list. */
2644 swi(Wimp_SendMessage, 17, block, 0); /* Broadcast request. */
2645
2646 /* OK, we've sent the request. Poll until we get a null poll (failure) or
2647 * we load the clipboard.
2648 * If we receive a DataSave event with icon handle = -2 then put it on the
2649 * clipboard. RISC OS should ensure that key events will not be delivered
2650 * until the clipboard operation completes (unless the owner starts idling
2651 * - we can't wait forever!).
2652 */
2653 for (;;)
2654 {
2655 reason = wimp_poll(0, block);
2656 if (reason == 0)
2657 return; /* Failed to get clipboard. */
2658 if ((reason == 17 || reason == 18) &&
2659 block[4] == 1 && block[6] == RO_LOAD_CLIPBOARD)
2660 break; /* Got it - stop waiting. */
2661 process_event(reason, block);
2662 if (ro_return_early)
2663 return;
2664 }
2665 /* Tell owner to save data in <Wimp$Scrap>. */
2666 block[0] = 60;
2667 block[3] = block[2]; /* Copy myref -> yourref */
2668 block[4] = 2; /* DataSaveAck. */
2669 block[9] = -1; /* Data is unsafe. */
2670 strcpy( ((char_u *) block) + 44, "<Wimp$Scrap>");
2671 swi(Wimp_SendMessage, 17, block, block[1]);
2672
2673 /* Wait again for reply. */
2674 for (;;)
2675 {
2676 reason = wimp_poll(0, block);
2677 if (reason == 0)
2678 return; /* Other program has given up! */
2679 if ((reason == 17 || reason == 18) && block[4] == 3 && block[6] == RO_LOAD_CLIPBOARD)
2680 break; /* Clipboard data saved to <Wimp$Scrap> */
2681 process_event(reason, block);
2682 if (ro_return_early)
2683 return;
2684 }
2685
2686 /* <Wimp$Scrap> contains clipboard - load it. */
2687 if (xswi(OS_File, 17, "<Wimp$Scrap>") & v_flag)
2688 return; /* Error! */
2689 if (r0 != 1 && r0 != 3)
2690 return;
2691 length = r4;
2692
2693 buffer = lalloc(length, TRUE); /* Claim memory (and report errors). */
2694 if (buffer == NULL)
2695 return;
2696
2697 if (xswi(OS_File, 16, "<Wimp$Scrap>", buffer, 0) & v_flag)
2698 return;
2699
2700 clip_yank_selection(MCHAR, buffer, length, cbd);
2701
2702 vim_free(buffer);
2703
2704 swi(OS_FSControl, 27, "<Wimp$Scrap>", 0, 0); /* Delete temp file. */
2705
2706 block[4] = 4; /* Send DataLoadAck. */
2707 block[3] = block[2]; /* Copy myref -> yourref. */
2708 swi(Wimp_SendMessage, 17, block, block[1]);
2709}
2710
2711/* Not sure what this means under RISC OS. */
2712 void
2713clip_mch_lose_selection(VimClipboard *cbd)
2714{
2715}
2716
2717/* Tell everyone that we now own the clipboard.
2718 * Return OK if our claim is accepted (always, under RISC OS)
2719 */
2720 int
2721clip_mch_own_selection(VimClipboard *cbd)
2722{
2723 int block[6];
2724 block[0] = 24; /* Length of block. */
2725 block[3] = 0; /* Original message. */
2726 block[4] = 0xf; /* ClaimEntity. */
2727 block[5] = 0x4; /* Claim clipboard only. */
2728 swi(Wimp_SendMessage, 17, block, 0);
2729 return OK;
2730}
2731
2732/*
2733 * Send the current selection to the clipboard. Do nothing for X because we
2734 * will fill in the selection only when requested by another app. Sounds good
2735 * for RISC OS too.
2736 */
2737 void
2738clip_mch_set_selection(VimClipboard *cbd)
2739{
2740 clip_get_selection(cbd);
2741}
2742
2743/*
2744 * Make a menu either grey or not grey.
2745 */
2746 void
2747gui_mch_menu_grey(vimmenu_T *menu, int grey)
2748{
2749 menu-> greyed_out = grey;
2750}
2751
2752/*
2753 * Make menu item hidden or not hidden
2754 */
2755 void
2756gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
2757{
2758 menu-> hidden = hidden;
2759}
2760
2761/*
2762 * This is called after setting all the menus to grey/hidden or not.
2763 */
2764 void
2765gui_mch_draw_menubar(void)
2766{
2767 swi(Wimp_CreateMenu, 0, -1);
2768 if (wimp_menu != (int *) -1)
2769 {
2770 ro_remove_menu(wimp_menu);
2771 wimp_menu = (int *) -1;
2772 }
2773}
2774
2775/* Add or remove a scrollbar. Note that this is only called when
2776 * the scrollbar state is changing.
2777 * The scroll bar window has already been created.
2778 * We can't do anything except remove the scroll bar
2779 * until we know what size to use.
2780 */
2781 void
2782gui_mch_enable_scrollbar(sb, flag)
2783 scrollbar_T *sb;
2784 int flag;
2785{
2786 if (!flag)
2787 swi(Wimp_CloseWindow, 0, & (sb->id) );
2788 return;
2789}
2790
2791 void
2792gui_mch_set_blinking(long waittime, long on, long off)
2793{
2794}
2795
2796/*
2797 * Stop the cursor blinking. Show the cursor if it wasn't shown.
2798 */
2799 void
2800gui_mch_stop_blink(void)
2801{
2802}
2803
2804/*
2805 * Start the cursor blinking. If it was already blinking, this restarts the
2806 * waiting time and shows the cursor.
2807 */
2808 void
2809gui_mch_start_blink(void)
2810{
2811}
2812
2813/*
2814 * Return the RGB value of a pixel as a long.
2815 */
2816 long_u
2817gui_mch_get_rgb(guicolor_T pixel)
2818{
2819 return (long_u)pixel;
2820}
2821
2822 void
2823gui_mch_set_text_area_pos(int x, int y, int w, int h)
2824{
2825}
2826
2827 void
2828gui_mch_enable_menu(int flag)
2829{
2830}
2831
2832 void
2833gui_mch_set_menu_pos(int x, int y, int w, int h)
2834{
2835}
2836
2837 void
2838gui_mch_add_menu(vimmenu_T *menu, int idx)
2839{
2840}
2841
2842 void
2843gui_mch_add_menu_item(vimmenu_T *menu, int idx)
2844{
2845}
2846
2847 void
2848gui_mch_new_menu_colors(void)
2849{
2850}
2851
2852 void
2853gui_mch_destroy_menu(vimmenu_T *menu)
2854{
2855}
2856
2857/* Size of buffer has changed.
2858 * Add one to max since gui.c substracts one more than it should!
2859 */
2860 void
2861gui_mch_set_scrollbar_thumb(sb, val, size, max)
2862 scrollbar_T *sb;
2863 long val;
2864 long size;
2865 long max;
2866{
2867 int block[10], width, height;
2868
2869 width = (max + 1) * gui.char_width;
2870 height = (max + 1 + W_STATUS_HEIGHT(sb->wp)) * gui.char_height;
2871
2872 block[0] = block[3] = 0;
2873 block[1] = -height + (1 << y_eigen_factor);
2874 block[2] = width;
2875
2876 swi(Wimp_SetExtent, sb -> id, block);
2877
2878 block[0] = sb -> id;
2879 swi(Wimp_GetWindowState, 0, block);
2880 block[5] = val * gui.char_width;
2881 block[6] = -val * gui.char_height;
2882 swi(Wimp_OpenWindow, 0, block, 0x4b534154,
2883 gui.window_handle, /* Parent window handle. */
2884 (CHILD_FIX_TO_RIGHT << CHILD_LEFT ) |
2885 (CHILD_FIX_TO_RIGHT << CHILD_RIGHT ) |
2886 (CHILD_FIX_TO_BOTTOM << CHILD_TOP ) |
2887 (CHILD_FIX_TO_BOTTOM << CHILD_BOTTOM) |
2888 (CHILD_SELF_SCROLL << CHILD_SCROLL_X) |
2889 (CHILD_SELF_SCROLL << CHILD_SCROLL_Y)
2890 );
2891}
2892
2893/* Set the position of the scrollbar within the editor
2894 * window. Note that, for vertical scrollbars, x and w
2895 * are ignored. For horizontal bars y and h are ignored.
2896 */
2897 void
2898gui_mch_set_scrollbar_pos(sb, x, y, w, h)
2899 scrollbar_T *sb;
2900 int x; /* Horizontal sb position */
2901 int y; /* Top of scroll bar */
2902 int w; /* Width */
2903 int h; /* Height */
2904{
2905 int block[24];
2906 int px1, py1; /* Parent window min coords */
2907 int px2, py2; /* Parent window max coords */
2908
2909 /* Find where the parent window is. */
2910 block[0] = gui.window_handle;
2911 swi(Wimp_GetWindowState, 0, block);
2912 px1 = block[1];
2913 py1 = block[2];
2914 px2 = block[3];
2915 py2 = block[4];
2916
2917 block[0] = sb -> id;
2918
2919 /* Find out how big the scroll window is at the moment. */
2920 swi(Wimp_GetWindowInfo, 0, ((char_u *)block) + 1);
2921
2922 if (block[13] < w || block[12] > -h)
2923 {
2924 /* Current window is too small! */
2925 if (block[12] > -h)
2926 block[12] = -h;
2927 if (block[13] < w)
2928 block[13] = w;
2929 swi(Wimp_SetExtent, block[0], block + 11);
2930 }
2931
2932 /* This works better on the nested_wimp. */
2933 if (sb-> wp)
2934 {
2935 /* This is a vertical scrollbar. */
2936 block[1] = block[3] = px2 - gui.scrollbar_width + (1 << x_eigen_factor);
2937 block[2] = 1 + py2 - (y + h) + (1 << y_eigen_factor);
2938 block[4] = 1 + py2 - y;
2939 }
2940 else
2941 {
2942 /* This is a horizontal scrollbar. */
2943 block[2] = block[4] = py1 + gui.scrollbar_height;
2944 block[1] = px1;
2945 block[3] = px2 - gui.scrollbar_width;
2946 }
2947
2948 block[5] = 0;
2949 block[6] = 0;
2950 block[7] = -1;
2951
2952 swi(Wimp_OpenWindow, 0, block, 0x4b534154,
2953 gui.window_handle, /* Parent window handle. */
2954 (CHILD_FIX_TO_RIGHT << CHILD_LEFT ) |
2955 (CHILD_FIX_TO_RIGHT << CHILD_RIGHT ) |
2956 (CHILD_FIX_TO_BOTTOM << CHILD_TOP ) |
2957 (CHILD_FIX_TO_BOTTOM << CHILD_BOTTOM) |
2958 (CHILD_SELF_SCROLL << CHILD_SCROLL_X) |
2959 (CHILD_SELF_SCROLL << CHILD_SCROLL_Y)
2960 );
2961}
2962
2963/* Create a window with no workarea to place inside editor window.
2964 * (what happens without the nested wimp?)
2965 * Data for scrollbar is invalid.
2966 */
2967 void
2968gui_mch_create_scrollbar(sb, orient)
2969 scrollbar_T *sb;
2970 int orient; /* orient is SBAR_HORIZ or SBAR_VERT */
2971{
2972 int bar[] =
2973 {
2974 0, 0, /* Visible area : min X,Y */
2975 100, 100, /* max X,Y */
2976 0, 0, /* Scroll offsets */
2977 -1, /* Window in front */
2978 0x80800150 | (orient == SBAR_HORIZ ? (1 << 30) : (1 << 28)),
2979 0xff070207, /* Colours */
2980 0x000c0103, /* More colours */
2981 0, -0x4000, /* Workarea extent */
2982 0x4000, 0, /* max X,Y */
2983 0x00000000, /* No title */
2984 0 << 12, /* No workarea button type */
2985 1, /* Wimp sprite area */
2986 0x00010001, /* Minimum width, height */
2987 0, 0, 0, /* Title data (none) */
2988 0 /* No icons */
2989 };
2990 swi(Wimp_CreateWindow, 0, bar);
2991 sb -> id = r0;
2992}
2993
2994#if defined(FEAT_WINDOWS) || defined(PROTO)
2995 void
2996gui_mch_destroy_scrollbar(scrollbar_T *sb)
2997{
2998 swi(Wimp_DeleteWindow, 0, & (sb->id));
2999 sb -> id = -1;
3000}
3001#endif
3002
3003 void
3004gui_mch_set_scrollbar_colors(scrollbar_T *sb)
3005{
3006 /* Always use default RO colour scheme. */
3007}
3008
3009/*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003010 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 * Note: (0,0) is the bottom left corner, positive y is UP.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 */
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003013 void
3014gui_mch_getmouse(x, y)
3015 int *x;
3016 int *y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 int left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 int top;
3020 int block[10];
3021
3022 block[0] = gui.window_handle;
3023 swi(Wimp_GetWindowState, 0, block);
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003024 left = block[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 top = block[4];
3026
3027 swi(Wimp_GetPointerInfo, 0, block);
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003028 *x = block[0] - left;
3029 *y = top - block[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030}
3031
3032/* MouseTo(x, y) */
3033 void
3034gui_mch_setmouse(x, y)
3035 int x;
3036 int y;
3037{
3038}
3039
3040 void
3041gui_mch_toggle_tearoffs(enable)
3042 int enable;
3043{
3044 /* no tearoff menus */
3045}
3046
3047/* Redraw a window's title.
3048 * For the nested wimp we use the new 'redraw-title-bar' reason code.
3049 * For older wimps we mark the area of the screen where the title bar
3050 * is as invalid.
3051 */
3052 void
3053ro_redraw_title(window)
3054 int window;
3055{
3056 if (nested_wimp)
3057 {
3058 swi(Wimp_ForceRedraw, window, 0x4b534154, 3);
3059 }
3060 else
3061 {
3062 int block[10];
3063 int miny;
3064
3065 block[0] = window;
3066 swi(Wimp_GetWindowState, 0, block);
3067 miny = block[4];
3068 swi(Wimp_GetWindowOutline, 0, block);
3069 swi(Wimp_ForceRedraw, -1,
3070 block[1], miny,
3071 block[3], block[4]);
3072 }
3073}
3074
3075/* Turn a vimmenu_T structure into a wimp menu structure.
3076 * -1 if resulting menu is empty.
3077 * Only the children and dname items in the root menu are used.
3078 */
3079 int *
3080ro_build_menu(menu)
3081 vimmenu_T *menu;
3082{
3083 int *wimp_menu;
3084 int width = 4;
3085 int w;
3086 int size = 28;
3087 vimmenu_T *item;
3088 int *wimp_item;
3089
3090 /* Find out how big the menu is so we can allocate memory for it */
3091 for (item = menu-> children; item; item = item-> next)
3092 {
3093 if (item-> hidden == FALSE && !menu_is_separator(item->name))
3094 size += 24;
3095 }
3096
3097 if (size <= 28)
3098 return (int *) -1; /* No children - shouldn't happen */
3099
3100 wimp_menu = (int *) alloc(size);
3101
3102 wimp_menu[0] = (int) menu-> dname;
3103 wimp_menu[1] = -1;
3104 wimp_menu[2] = 0;
3105 wimp_menu[3] = 0x00070207;
3106 wimp_menu[5] = 44;
3107 wimp_menu[6] = 0;
3108
3109 wimp_item = wimp_menu + 7;
3110
3111 for (item = menu-> children; item; item = item-> next)
3112 {
3113 if (menu_is_separator(item-> name))
3114 {
3115 /* This menu entry is actually a separator. If it is not the first
3116 * menu entry then mark the previous menu item as needing a dotted
3117 * line after it.
3118 */
3119 if (wimp_item > wimp_menu + 7)
3120 wimp_item[-6] |= 0x2;
3121 }
3122 else if (item-> hidden == FALSE)
3123 {
3124 wimp_item[0] = 0;
3125 wimp_item[1] = item-> children ? (int) ro_build_menu(item) : -1;
3126 wimp_item[2] = 0x07009131 | (item-> greyed_out << 22);
3127 wimp_item[3] = (int) item-> dname;
3128 wimp_item[4] = -1;
3129 wimp_item[5] = (int) item; /* Stuff the menu address in this unused space */
3130
3131 w = strlen(item-> dname) + 1;
3132 if (w > width)
3133 width = w;
3134 wimp_item += 6;
3135 }
3136 }
3137
3138 wimp_menu[4] = (width + 2) * 16;
3139 wimp_menu[7] |= 0x100; /* Menu title is indirected */
3140 wimp_item[-6] |= 0x080; /* Last entry in menu */
3141 return wimp_menu;
3142}
3143
3144 static void
3145ro_remove_menu(menu)
3146 int *menu;
3147{
3148 int *item = menu + 7;
3149
3150 if (menu == NULL || menu == (int *) -1)
3151 return;
3152
3153 for (;;)
3154 {
3155 if (item[1] != -1)
3156 ro_remove_menu((int *) item[1]); /* Remove sub-menu */
3157 if (item[0] & 0x80)
3158 break; /* This was the last entry */
3159 item += 6;
3160 }
3161 vim_free(menu);
3162}
3163
3164 void
3165gui_mch_show_popupmenu(menu)
3166 vimmenu_T *menu;
3167{
3168 int block[10];
3169
3170 /* Remove the existing menu, if any */
3171 if (wimp_menu != (int *) -1)
3172 {
3173 swi(Wimp_CreateMenu, 0, -1);
3174 ro_remove_menu(wimp_menu);
3175 wimp_menu = (int *) -1;
3176 }
3177
3178 wimp_menu = ro_build_menu(menu);
3179 if (wimp_menu != (int *) -1)
3180 {
3181 swi(Wimp_GetPointerInfo, 0, block);
3182 swi(Wimp_CreateMenu, 0, wimp_menu, block[0] - 64, block[1] + 64);
3183 }
3184}
3185
3186/* Run a command using the TaskWindow module.
3187 * If SHELL_FILTER is set then output is not echoed to the screen,
3188 * If it is not set, then \r is not sent to the output file.
3189 */
3190 int
3191gui_mch_call_shell(cmd, options)
3192 char_u *cmd;
3193 int options; /* SHELL_FILTER if called by do_filter() */
3194 /* SHELL_COOKED if term needs cooked mode */
3195{
3196 char_u task_cmd[256]; /* Contains *TaskWindow command. */
3197 int block[64];
3198 int reason;
3199 char_u *out;
3200 char_u c;
3201 int old_msg_col;
3202 char_u *out_redir;
3203 int length;
3204 FILE *out_file = NULL;
3205
3206 out_redir = strstr(cmd, " > ");
3207 if (out_redir == NULL)
3208 length = strlen(cmd); /* No redirection. */
3209 else
3210 {
3211 length = out_redir - cmd;
3212 out_file = fopen(out_redir + 3, "wb");
3213 if (out_file == NULL)
Bram Moolenaar051b7822005-05-19 21:00:46 +00003214 smsg("WARNING : Can't open file %s for writing\n", out_redir + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216
3217 if (length > 180)
3218 {
3219 if (out_file)
3220 fclose(out_file);
3221 return FAIL; /* Command too long. */
3222 }
3223
3224 strcpy(task_cmd, "TaskWindow \"");
3225 strncpy(task_cmd + 12, cmd, length);
3226 sprintf(task_cmd + 12 + length,
3227 "\" -task &%08x -ctrl -quit -name \"Vim command\"",
3228 task_handle);
3229
3230 if (options & SHELL_COOKED)
3231 settmode(TMODE_COOK);
3232
3233 if (xswi(Wimp_StartTask, task_cmd) & v_flag)
3234 {
3235 /* Failed to even start a new task (out of memory?) */
3236 settmode(TMODE_RAW);
3237 if (out_file)
3238 fclose(out_file);
3239 return FAIL;
3240 }
3241
3242 /* Wait for the child process to initialise. */
3243 child_handle = 0;
3244 while (!child_handle)
3245 {
3246 reason = wimp_poll(0, block);
3247 if ((reason == 17 || reason == 18) && block[4] == 0x808c2)
3248 child_handle = block[1];
3249 else
3250 process_event(reason, block);
3251 }
3252
3253 /* Block until finished */
3254 while (child_handle)
3255 {
3256 reason = wimp_poll(1, block);
3257 if (reason == 3 || (reason == 8 && block[6] == 3))
3258 {
3259 /* Close window request or CTRL-C - kill child task. */
3260 block[0] = 20;
3261 block[3] = 0;
3262 block[4] = 0x808c4; /* Morite */
3263 swi(Wimp_SendMessage, 17, block, child_handle);
3264 MSG_PUTS(_("\nSending message to terminate child process.\n"));
3265 continue;
3266 }
3267 else if (reason == 8)
3268 {
3269 block[0] = 28;
3270 block[3] = 0;
3271 block[4] = 0x808c0; /* Input */
3272 block[5] = 1;
3273 /* Block[6] is OK as it is! */
3274 swi(Wimp_SendMessage, 17, block, child_handle);
3275 continue;
3276 }
3277 else if (reason == 17 || reason == 18)
3278 {
3279 if (block[4] == 0x808c1)
3280 {
3281 /* Ack message. */
3282 block[3] = block[2];
3283 swi(Wimp_SendMessage, 19, block, block[1]);
3284 out = (char_u *)block + 24;
3285 old_msg_col = msg_col;
3286 while (block[5]--)
3287 {
3288 c = *out++;
3289 if (out_file && (c != '\r' || (options & SHELL_FILTER)))
3290 fputc(c, out_file);
3291 if ((options & SHELL_FILTER) == 0)
3292 {
3293 if (c == 127)
3294 msg_puts("\b \b");
3295 else if (c > 31)
3296 msg_putchar(c);
3297 else if (c == 10)
3298 {
3299 lines_left = 8; /* Don't do More prompt! */
3300 msg_putchar(10);
3301 }
3302 }
3303 }
3304 /* Flush output to the screen. */
3305 windgoto(msg_row, msg_col);
3306 out_flush();
3307 continue;
3308 }
3309 }
3310 process_event(reason, block);
3311 }
3312 msg_putchar('\n');
3313 settmode(TMODE_RAW);
3314 if (out_file)
3315 fclose(out_file);
3316 return OK;
3317}
3318
3319/* Like strsave(), but stops at any control char */
3320 char_u *
3321wimp_strsave(str)
3322 char *str;
3323{
3324 int strlen = 0;
3325 char_u *retval;
3326 while (str[strlen] > 31)
3327 strlen++;
3328 retval = alloc(strlen + 1);
3329 if (retval)
3330 {
3331 memcpy(retval, str, strlen);
3332 retval[strlen] = '\0';
3333 }
3334 return retval;
3335}
3336
3337/* If we are saving then pop up a standard RISC OS save box.
3338 * Otherwise, open a directory viewer on the given directory (and return NULL)
3339 * The string we return will be freed later.
3340 */
3341 char_u *
3342gui_mch_browse(saving, title, dflt, ext, initdir, filter)
3343 int saving; /* write action */
3344 char_u *title; /* title for the window */
3345 char_u *dflt; /* default file name */
3346 char_u *ext; /* extension added */
3347 char_u *initdir; /* initial directory, NULL for current dir */
3348 char_u *filter; /* file name filter */
3349{
3350 char command[256];
3351 int length;
3352
3353 if (saving)
3354 {
3355 int block[64];
3356 int reason;
3357 int done_save = FALSE;
3358 char_u *retval = NULL;
3359 char_u *sprname;
3360 char_u *fname;
3361 int dragging_icon = FALSE;
3362 int filetype;
3363
3364 if (!dflt)
3365 dflt = "TextFile";
3366
3367 block[0] = save_window;
3368 block[1] = 0;
3369 swi(Wimp_GetIconState, 0, block);
3370 sprname = ((char_u *) block[7]);
3371 block[1] = 1;
3372 swi(Wimp_GetIconState, 0, block);
3373 fname = ((char *) block[7]);
3374 strncpy(fname, dflt, 255);
3375
3376 if (xswi(OS_FSControl, 31, curbuf->b_p_oft) & v_flag)
3377 {
3378 filetype = 0xfff;
3379 strcpy(sprname + 5, "xxx");
3380 }
3381 else
3382 {
3383 filetype = r2;
3384 sprintf(sprname + 5, "%03x", filetype);
3385 }
3386
3387 /* Open the save box */
3388
3389 swi(Wimp_GetPointerInfo, 0, block);
3390 swi(Wimp_CreateMenu, 0, save_window, block[0] - 64, block[1] + 64);
3391 swi(Wimp_SetCaretPosition, save_window, 1, 0, 0, -1, -1);
3392
3393 while (!done_save)
3394 {
3395 reason = wimp_poll(1, block);
3396 switch (reason)
3397 {
3398 case 1:
3399 redraw_window(block);
3400 break;
3401 case 2:
3402 if (block[0] == save_window)
3403 swi(Wimp_OpenWindow, 0, block);
3404 else
3405 ro_open_window(block);
3406 break;
3407 case 3:
3408 done_save = TRUE;
3409 break;
3410 case 6:
3411 if (block[3] != save_window)
3412 done_save = TRUE;
3413 else
3414 {
3415 int drag_box[4];
3416 int min_x, max_y;
3417
3418 switch (block[4])
3419 {
3420 case 0: /* Start drag */
3421 block[0] = save_window;
3422 swi(Wimp_GetWindowState, 0, block);
3423 min_x = block[1];
3424 max_y = block[4];
3425 block[1] = 0;
3426 swi(Wimp_GetIconState, 0, block);
3427 drag_box[0] = block[2] + min_x;
3428 drag_box[1] = block[3] + max_y;
3429 drag_box[2] = block[4] + min_x;
3430 drag_box[3] = block[5] + max_y;
3431
3432 swi(DragASprite_Start,
3433 0x45,
3434 1,
3435 sprname,
3436 drag_box);
3437 dragging_icon = TRUE;
3438 break;
3439 case 2: /* OK */
3440 retval = wimp_strsave(fname);
3441 done_save = TRUE;
3442 break;
3443 case 3: /* Cancel */
3444 done_save = TRUE;
3445 break;
3446 }
3447 }
3448 break;
3449 case 7:
3450 if (dragging_icon)
3451 {
3452 int len = 0;
3453
3454 dragging_icon = FALSE;
3455 swi(Wimp_GetPointerInfo, 0, block);
3456 block[5] = block[3];
3457 block[6] = block[4];
3458 block[7] = block[0];
3459 block[8] = block[1];
3460 block[9] = 0; /* Don't know the size */
3461 block[10] = filetype;
3462
3463 while (fname[len] > 31)
3464 {
3465 if (fname[len] == '.')
3466 {
3467 fname += len + 1;
3468 len = 0;
3469 }
3470 else
3471 len++;
3472 }
3473 if (len > 211)
3474 len = 211;
3475
3476 memcpy(((char_u *) block) + 44, fname, len);
3477 ((char_u *)block)[44 + len] = '\0';
3478
3479 block[0] = (len + 48) & 0xfc;
3480 block[3] = 0;
3481 block[4] = 1; /* DataSave */
3482
3483 swi(Wimp_SendMessage, 17, block, block[5], block[6]);
3484 }
3485 else
3486 ro_drag_finished(block);
3487 break;
3488 case 8:
3489 if (block[6] == 13)
3490 {
3491 retval = wimp_strsave(fname);
3492 done_save = TRUE;
3493 }
3494 else if (block[6] == 0x1b)
3495 done_save = TRUE;
3496 else
3497 swi(Wimp_ProcessKey, block[6]);
3498 break;
3499 case 17:
3500 case 18:
3501 if (block[4] == 2 && block[9] != -1)
3502 {
3503 /* DataSaveAck from dragging icon. */
3504 retval = wimp_strsave(((char_u *) block) + 44);
3505 done_save = TRUE;
3506 }
3507 else if (block[4] == 0x400c9)
3508 {
3509 /* MenusDeleted */
3510 done_save = TRUE;
3511 }
3512 else
3513 ro_message(block);
3514 break;
3515 }
3516 }
3517 block[0] = save_window;
3518 swi(Wimp_CloseWindow, 0, block);
3519 swi(Wimp_GetCaretPosition, 0, block);
3520 if (block[0] == -1)
3521 swi(Wimp_SetCaretPosition, gui.window_handle, -1, 0, 0, -1, -1);
3522
3523 return retval;
3524 }
3525 else if (initdir)
3526 {
3527 /* Open a directory viewer */
3528 length = strlen(initdir);
3529
3530 if (length > 240)
3531 return NULL; /* Path too long! */
3532
3533 length = sprintf(command, "Filer_OpenDir %s", initdir);
3534 while (command[length - 1] == '.')
3535 length--;
3536 command[length] = '\0';
3537 swi(OS_CLI, command);
3538 }
3539 return NULL;
3540}