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