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