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