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