summaryrefslogtreecommitdiff
path: root/frontends/riscos/corewindow.c
blob: b57e4e0eb5809be1180242077baecbce89e4fe82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
 * Copyright 2016 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * RISC OS generic core window implementation.
 *
 * Provides interface for core renderers to a risc os drawing area.
 *
 * This module is an object that must be encapsulated. Client users
 * should embed a struct ro_corewindow at the beginning of their
 * context for this display surface, fill in relevant data and then
 * call ro_corewindow_init()
 *
 * The ro core window structure requires the callback for draw, key
 * and mouse operations.
 */

#include <stdint.h>
#include <oslib/wimp.h>

#include "utils/log.h"
#include "netsurf/types.h"
#include "netsurf/mouse.h"
#include "netsurf/keypress.h"

#include "riscos/wimp_event.h"
#include "riscos/dialog.h"
#include "riscos/gui.h"
#include "riscos/toolbar.h"
#include "riscos/mouse.h"
#include "riscos/corewindow.h"

#ifndef wimp_KEY_END
#define wimp_KEY_END wimp_KEY_COPY
#endif

/**
 * Update a windows scrollbars.
 *
 * in the wimp this is done by setting the extent and calling window open
 */
static void update_scrollbars(struct ro_corewindow *ro_cw, wimp_open *open)
{
	os_error *error;
	int extent_width;
	int extent_height;
	os_box extent;

	LOG("RO corewindow context %p", ro_cw);

	/* extent of content in not smaller than window so start there */
	extent_width = open->visible.x1 - open->visible.x0;
	extent_height = open->visible.y0 - open->visible.y1;
	LOG("extent w:%d h:%d content w:%d h:%d origin h:%d",
	    extent_width, extent_height,
	    ro_cw->content_width, ro_cw->content_height, ro_cw->origin_y);
	if (ro_cw->content_width > extent_width) {
		extent_width = ro_cw->content_width;
	}
	if (extent_height > (ro_cw->origin_y + ro_cw->content_height)) {
		extent_height = ro_cw->origin_y + ro_cw->content_height;
	}
	LOG("extent w:%d h:%d",
	    extent_width, extent_height);
	extent.x0 = 0;
	extent.y0 = extent_height;
	extent.x1 = extent_width;
	extent.y1 = 0;

	error = xwimp_set_extent(ro_cw->wh, &extent);
	if (error) {
		LOG("xwimp_set_extent: 0x%x: %s",
		    error->errnum, error->errmess);
		return;
	}

	error = xwimp_open_window(open);
	if (error) {
		LOG("xwimp_open_window: 0x%x: %s",
		    error->errnum, error->errmess);
	}
}


/**
 * wimp callback on redraw event
 */
static void ro_cw_redraw(wimp_draw *redraw)
{
	struct ro_corewindow *ro_cw;
	osbool more;
	os_error *error;
	struct rect r;
	int origin_x;
	int origin_y;

	ro_cw = (struct ro_corewindow *)ro_gui_wimp_event_get_user_data(redraw->w);

	error = xwimp_redraw_window(redraw, &more);
	while ((error == NULL) && (more)) {
		/* compute rectangle to redraw */
		origin_x = redraw->box.x0 - redraw->xscroll;
		origin_y = redraw->box.y1 + ro_cw->origin_y - redraw->yscroll;

		r.x0 = (redraw->clip.x0 - origin_x) / 2;
		r.y0 = (origin_y - redraw->clip.y1) / 2;
		r.x1 = r.x0 + ((redraw->clip.x1 - redraw->clip.x0) / 2);
		r.y1 = r.y0 + ((redraw->clip.y1 - redraw->clip.y0) / 2);

		/* call the draw callback */
		ro_cw->draw(ro_cw, origin_x, origin_y, &r);

		error = xwimp_get_rectangle(redraw, &more);
	}
	if (error != NULL) {
		LOG("xwimp_redraw_window: 0x%x: %s",
		    error->errnum, error->errmess);
	}

}

static void ro_cw_scroll(wimp_scroll *scroll)
{
	os_error *error;
	int page_x;
	int page_y;
	struct ro_corewindow *ro_cw;
	wimp_open open;

	ro_cw = (struct ro_corewindow *)ro_gui_wimp_event_get_user_data(scroll->w);
	LOG("RO corewindow context %p", ro_cw);

	page_x = scroll->visible.x1 - scroll->visible.x0 - 32;
	page_y = scroll->visible.y1 - scroll->visible.y0 - 32;

	page_y += ro_cw->origin_y;

	open.w = scroll->w;
	open.visible = scroll->visible;
	open.next = scroll->next;

	switch (scroll->xmin) {
	case wimp_SCROLL_PAGE_LEFT:
		open.xscroll = scroll->xscroll - page_x;
		break;

	case wimp_SCROLL_COLUMN_LEFT:
		open.xscroll = scroll->xscroll - 32;
		break;

	case wimp_SCROLL_COLUMN_RIGHT:
		open.xscroll = scroll->xscroll + 32;
		break;

	case wimp_SCROLL_PAGE_RIGHT:
		open.xscroll = scroll->xscroll + page_x;
		break;

	default:
		open.xscroll = scroll->xscroll + ((page_x * (scroll->xmin>>2)) >> 2);
		break;
	}

	switch (scroll->ymin) {
	case wimp_SCROLL_PAGE_UP:
		open.yscroll = scroll->yscroll + page_y;
		break;

	case wimp_SCROLL_LINE_UP:
		open.yscroll = scroll->yscroll + 32;
		break;

	case wimp_SCROLL_LINE_DOWN:
		open.yscroll = scroll->yscroll - 32;
		break;

	case wimp_SCROLL_PAGE_DOWN:
		open.yscroll = scroll->yscroll - page_y;
		break;

	default:
		open.yscroll = scroll->yscroll + ((page_y * (scroll->ymin>>2)) >> 2);
		break;
	}

	error = xwimp_open_window(&open);
	if (error) {
		LOG("xwimp_open_window: 0x%x: %s",
		    error->errnum, error->errmess);
	}

}



/**
 * Track the mouse under Null Polls from the wimp, to support dragging.
 *
 * \param pointer Pointer to a Wimp Pointer block.
 * \param data NULL to allow use as a ro_mouse callback.
 */
static void ro_cw_mouse_at(wimp_pointer *pointer, void *data)
{
	os_error *error;
	struct ro_corewindow *ro_cw;
	wimp_window_state state;
	int xpos, ypos;
	browser_mouse_state mouse;

	/* ignore menu clicks */
	if (pointer->buttons & (wimp_CLICK_MENU)) {
		return;
	}

	ro_cw = (struct ro_corewindow *)ro_gui_wimp_event_get_user_data(pointer->w);
	if (ro_cw == NULL) {
		LOG("no corewindow conext for window: 0x%x",
		    (unsigned int)pointer->w);
		return;
	}
	LOG("RO corewindow context %p", ro_cw);

	/* no futher processing required if no drag in progress */
	if (ro_cw->drag_status == CORE_WINDOW_DRAG_NONE) {
		return;
	}

	/* Not a Menu click and a drag is in progress. */
	state.w = pointer->w;
	error = xwimp_get_window_state(&state);
	if (error) {
		LOG("xwimp_get_window_state: 0x%x: %s",
		    error->errnum, error->errmess);
		return;
	}

	/* Convert the returned mouse coordinates into NetSurf's internal
	 * units.
	 */
	xpos = ((pointer->pos.x - state.visible.x0) + state.xscroll) / 2;
	ypos = ((state.visible.y1 - pointer->pos.y) -
		state.yscroll + ro_cw->origin_y) / 2;

	/* Start to process the mouse click. */
	mouse = ro_gui_mouse_drag_state(pointer->buttons,
					wimp_BUTTON_DOUBLE_CLICK_DRAG);

	ro_cw->mouse(ro_cw, mouse, xpos, ypos);

	if (!(mouse & BROWSER_MOUSE_DRAG_ON)) {
		ro_cw->mouse(ro_cw, BROWSER_MOUSE_HOVER, xpos, ypos);
		ro_cw->drag_status = CORE_WINDOW_DRAG_NONE;
	}

	ro_cw->toolbar_update(ro_cw);
}

/**
 * Process RISC OS User Drag Box events which relate to us: in effect, drags
 * started by ro_cw_drag_start().
 *
 * \param drag Pointer to the User Drag Box Event block.
 * \param data NULL to allow use as a ro_mouse callback.
 */
static void ro_cw_drag_end(wimp_dragged *drag, void *data)
{
	os_error *error;

	error = xwimp_drag_box((wimp_drag *) -1);
	if (error) {
		LOG("xwimp_drag_box: 0x%x: %s", error->errnum, error->errmess);
		ro_warn_user("WimpError", error->errmess);
	}

	error = xwimp_auto_scroll(0, NULL, NULL);
	if (error) {
		LOG("xwimp_auto_scroll: 0x%x: %s",
		    error->errnum, error->errmess);
		ro_warn_user("WimpError", error->errmess);
	}
}


/**
 * Start a RISC OS drag event to reflect on screen what is happening
 * during the core tree drag.
 *
 * \param ro_cw The RO corewindow to which the drag is attached.
 * \param pointer The RO pointer event data block starting the drag.
 * \param state The RO window state block for the treeview window.
 */
static void
ro_cw_drag_start(struct ro_corewindow *ro_cw,
		 wimp_pointer *pointer,
		 wimp_window_state *state)
{
	os_error *error;
	wimp_drag drag;
	wimp_auto_scroll_info auto_scroll;

	drag.w = ro_cw->wh;
	drag.bbox.x0 = state->visible.x0;
	drag.bbox.y0 = state->visible.y0;
	drag.bbox.x1 = state->visible.x1;
	drag.bbox.y1 = state->visible.y1 - ro_toolbar_height(ro_cw->toolbar) - 2;

	switch (ro_cw->drag_status) {
	case CORE_WINDOW_DRAG_SELECTION:
		drag.type = wimp_DRAG_USER_RUBBER;

		drag.initial.x0 = pointer->pos.x;
		drag.initial.y0 = pointer->pos.y;
		drag.initial.x1 = pointer->pos.x;
		drag.initial.y1 = pointer->pos.y;
		break;

	case CORE_WINDOW_DRAG_MOVE:
		drag.type = wimp_DRAG_USER_POINT;

		drag.initial.x0 = pointer->pos.x - 4;
		drag.initial.y0 = pointer->pos.y - 48;
		drag.initial.x1 = pointer->pos.x + 48;
		drag.initial.y1 = pointer->pos.y + 4;
		break;

	default:
		/* No other drag types are supported. */
		break;
	}

	LOG("Drag start...");

	error = xwimp_drag_box_with_flags(&drag,
			wimp_DRAG_BOX_KEEP_IN_LINE | wimp_DRAG_BOX_CLIP);
	if (error) {
		LOG("xwimp_drag_box: 0x%x: %s", error->errnum, error->errmess);
		ro_warn_user("WimpError", error->errmess);
	} else {
		auto_scroll.w = ro_cw->wh;
		auto_scroll.pause_zone_sizes.x0 = 80;
		auto_scroll.pause_zone_sizes.y0 = 80;
		auto_scroll.pause_zone_sizes.x1 = 80;
		auto_scroll.pause_zone_sizes.y1 = 80 + ro_toolbar_height(ro_cw->toolbar);
		auto_scroll.pause_duration = 0;
		auto_scroll.state_change = (void *) 1;

		error = xwimp_auto_scroll(wimp_AUTO_SCROLL_ENABLE_VERTICAL,
				&auto_scroll, NULL);
		if (error) {
			LOG("xwimp_auto_scroll: 0x%x: %s", error->errnum, error->errmess);
			ro_warn_user("WimpError", error->errmess);
		}

		ro_mouse_drag_start(ro_cw_drag_end, ro_cw_mouse_at, NULL, NULL);
	}
}


/**
 * Wimp callback on pointer entering window.
 *
 * The wimp has issued an event to the window because the pointer has
 * entered it.
 *
 * \param entering The entering event to be processed
 */
static void ro_cw_pointer_entering(wimp_entering *entering)
{
	ro_mouse_track_start(NULL, ro_cw_mouse_at, NULL);
}


/**
 * Wimp callback on window open event.
 *
 * The wimp has issued an event to the window because an attempt has
 * been made to open or resize it. This requires the new dimensions to
 * be calculated and set within the wimp.
 *
 * \param open The open event to be processed
 */
static void ro_cw_open(wimp_open *open)
{
	struct ro_corewindow *ro_cw;

	ro_cw = (struct ro_corewindow *)ro_gui_wimp_event_get_user_data(open->w);

	update_scrollbars(ro_cw, open);
}

static bool ro_cw_mouse_click(wimp_pointer *pointer)
{
	os_error *error;
	wimp_window_state state;
	int xpos, ypos;
	browser_mouse_state mouse = 0;
	bool handled = false;
	struct ro_corewindow *ro_cw;

	ro_cw = (struct ro_corewindow *)ro_gui_wimp_event_get_user_data(pointer->w);
	LOG("RO corewindow context %p", ro_cw);


	state.w = ro_cw->wh;
	error = xwimp_get_window_state(&state);
	if (error) {
		LOG("xwimp_get_window_state: 0x%x: %s",
		    error->errnum, error->errmess);
		return false;
	}

	/* Convert the returned mouse coordinates into NetSurf's internal
	 * units.
	 */
	xpos = ((pointer->pos.x - state.visible.x0) + state.xscroll) / 2;
	ypos = ((state.visible.y1 - pointer->pos.y) -
		state.yscroll + ro_cw->origin_y) / 2;

	if (pointer->buttons != wimp_CLICK_MENU) {
		mouse = ro_gui_mouse_click_state(pointer->buttons,
						 wimp_BUTTON_DOUBLE_CLICK_DRAG);

		/* Give the window input focus on Select-clicks.  This wouldn't
		 * be necessary if the core used the RISC OS caret.
		 */
		if (mouse & BROWSER_MOUSE_CLICK_1) {
			xwimp_set_caret_position(ro_cw->wh,
						 -1, -100, -100, 32, -1);
		}
	}

	/* call the mouse callback */
	if (mouse != 0) {
		ro_cw->mouse(ro_cw, mouse, xpos, ypos);

		/* If it's a visible drag, start the RO side of the visible
		 * effects.
		 */
		if ((ro_cw->drag_status == CORE_WINDOW_DRAG_SELECTION) ||
		    (ro_cw->drag_status == CORE_WINDOW_DRAG_MOVE)) {
			ro_cw_drag_start(ro_cw, pointer, &state);
		}

		ro_cw->toolbar_update(ro_cw);
	}

	/* Special actions for some mouse buttons.  Adjust closes the dialog;
	 * Menu opens a menu.  For the latter, we assume that the owning module
	 * will have attached a window menu to our parent window with the auto
	 * flag unset (so that we can fudge the selection above).  If it hasn't,
	 * the call will quietly fail.
	 *
	 * \TODO -- Adjust-click close isn't a perfect copy of what the RO
	 *          version did: adjust clicks anywhere close the tree, and
	 *          selections persist.
	 */
	switch(pointer->buttons) {
	case wimp_CLICK_ADJUST:
		if (handled) {
			ro_gui_dialog_close(ro_cw->wh);
		}
		break;

	case wimp_CLICK_MENU:
		ro_gui_wimp_event_process_window_menu_click(pointer);
		break;
	}

	return true;
}

static bool ro_cw_keypress(wimp_key *key)
{
	uint32_t c;
	struct ro_corewindow *ro_cw;
	nserror res;

	ro_cw = (struct ro_corewindow *)ro_gui_wimp_event_get_user_data(key->w);
	LOG("RO corewindow context %p", ro_cw);

	c = (uint32_t) key->c;

	if ((unsigned)c < 0x20 ||
	    (0x7f <= c && c <= 0x9f) ||
	    (c & IS_WIMP_KEY)) {
		/* Munge control keys into unused control chars */
		/* We can't map onto 1->26 (reserved for ctrl+<qwerty>
		   That leaves 27->31 and 128->159 */
		switch (c & ~IS_WIMP_KEY) {
		case wimp_KEY_TAB:
			c = 9;
			break;

		case wimp_KEY_SHIFT | wimp_KEY_TAB:
			c = 11;
			break;

		/* cursor movement keys */
		case wimp_KEY_HOME:
		case wimp_KEY_CONTROL | wimp_KEY_LEFT:
			c = NS_KEY_LINE_START;
			break;

		case wimp_KEY_END:
			if (os_version >= RISCOS5)
				c = NS_KEY_LINE_END;
			else
				c = NS_KEY_DELETE_RIGHT;
			break;

		case wimp_KEY_CONTROL | wimp_KEY_RIGHT:
			c = NS_KEY_LINE_END;
			break;

		case wimp_KEY_CONTROL | wimp_KEY_UP:
			c = NS_KEY_TEXT_START;
			break;

		case wimp_KEY_CONTROL | wimp_KEY_DOWN:
			c = NS_KEY_TEXT_END;
			break;

		case wimp_KEY_SHIFT | wimp_KEY_LEFT:
			c = NS_KEY_WORD_LEFT;
			break;

		case wimp_KEY_SHIFT | wimp_KEY_RIGHT:
			c = NS_KEY_WORD_RIGHT;
			break;

		case wimp_KEY_SHIFT | wimp_KEY_UP:
			c = NS_KEY_PAGE_UP;
			break;

		case wimp_KEY_SHIFT | wimp_KEY_DOWN:
			c = NS_KEY_PAGE_DOWN;
			break;

		case wimp_KEY_LEFT:
			c = NS_KEY_LEFT;
			break;

		case wimp_KEY_RIGHT:
			c = NS_KEY_RIGHT;
			break;

		case wimp_KEY_UP:
			c = NS_KEY_UP;
			break;

		case wimp_KEY_DOWN:
			c = NS_KEY_DOWN;
			break;

		/* editing */
		case wimp_KEY_CONTROL | wimp_KEY_END:
			c = NS_KEY_DELETE_LINE_END;
			break;

		case wimp_KEY_DELETE:
			if (ro_gui_ctrl_pressed())
				c = NS_KEY_DELETE_LINE_START;
			else if (os_version < RISCOS5)
				c = NS_KEY_DELETE_LEFT;
			break;

		default:
			break;
		}
	}

	if (!(c & IS_WIMP_KEY)) {
		res = ro_cw->key(ro_cw, c);
		if (res == NSERROR_OK) {
			ro_cw->toolbar_update(ro_cw);
			return true;
		}
	}

	return false;
}


/**
 * Update a corewindow toolbar to a new size.
 *
 * \param ctx Context as passed to toolbar creation.
 */
static void cw_tb_size(void *ctx)
{
	struct ro_corewindow *ro_cw;
	wimp_window_state state;
	os_error *error;

	ro_cw = (struct ro_corewindow *)ctx;

	ro_cw->origin_y = -(ro_toolbar_height(ro_cw->toolbar));

	state.w = ro_cw->wh;
	error = xwimp_get_window_state(&state);
	if (error) {
		LOG("xwimp_get_window_state: 0x%x: %s",
		    error->errnum, error->errmess);
		return;
	}

	error = xwimp_force_redraw(ro_cw->wh,
				   0, state.visible.y0 - state.visible.y1,
				   state.visible.x1 - state.visible.x0, 0);
	if (error) {
		LOG("xwimp_force_redraw: 0x%x: %s",
		    error->errnum, error->errmess);
		return;
	}
}


/**
 * Update a corewindow toolbar to use a new theme.
 *
 * \param ctx Context as passed to toolbar creation.
 * \param exists true if the bar still exists; else false.
 */
static void cw_tb_theme(void *ctx, bool exists)
{
	if (exists) {
		cw_tb_size(ctx);
	}
}


/**
 * Allow a corewindow toolbar button state to be updated.
 *
 * \param ctx Context as passed to toolbar creation.
 */
static void cw_tb_update(void *ctx)
{
	struct ro_corewindow *ro_cw;

	ro_cw = (struct ro_corewindow *)ctx;

	ro_cw->toolbar_update(ro_cw);
}


/**
 * Respond to user actions (click) in a corewindow.
 *
 * \param ctx Context as passed to toolbar creation.
 * \param action_type type of action on toolbar
 * \param action data for action.
 */
static void
cw_tb_click(void *ctx,
	    toolbar_action_type action_type,
	    union toolbar_action action)
{
	struct ro_corewindow *ro_cw;

	ro_cw = (struct ro_corewindow *)ctx;

	if (action_type == TOOLBAR_ACTION_BUTTON) {
		ro_cw->toolbar_click(ro_cw, action.button);
		ro_cw->toolbar_update(ro_cw);
	}
}


/**
 * Save positions of core window toolbar buttons.
 *
 * \param ctx Context as passed to toolbar creation.
 * \param config The new button config string.
 */
static void cw_tb_save(void *ctx, char *config)
{
	struct ro_corewindow *ro_cw;

	ro_cw = (struct ro_corewindow *)ctx;

	ro_cw->toolbar_save(ro_cw, config);
}



/**
 * riscos core window toolbar callbacks
 */
static const struct toolbar_callbacks corewindow_toolbar_callbacks = {
	.theme_update = cw_tb_theme,
	.change_size = cw_tb_size,
	.update_buttons = cw_tb_update,
	.user_action = cw_tb_click,
	.save_buttons = cw_tb_save,
};


/**
 * callback from core to request an invalidation of a window area.
 *
 * The specified area of the window should now be considered
 *  out of date. If the area is NULL the entire window must be
 *  invalidated.
 *
 * \param[in] cw The core window to invalidate.
 * \param[in] r area to redraw or NULL for the entire window area.
 * \return NSERROR_OK on success or appropriate error code.
 */
static nserror
ro_cw_invalidate(struct core_window *cw, const struct rect *r)
{
	struct ro_corewindow *ro_cw = (struct ro_corewindow *)cw;
	os_error *error;
	wimp_window_info info;

	if (r == NULL) {
		info.w = ro_cw->wh;
		error = xwimp_get_window_info_header_only(&info);
		if (error) {
			LOG("xwimp_get_window_info_header_only: 0x%x: %s",
			    error->errnum, error->errmess);
			return NSERROR_INVALID;
		}
	} else {
		/* convert the passed rectangle into RO window dimensions */
		info.extent.x0 = 2 * r->x0;
		info.extent.y0 = (-2 * (r->y0 + (r->y1 - r->y0))) + ro_cw->origin_y;
		info.extent.x1 = 2 * (r->x0 + (r->x1 - r->x0));
		info.extent.y1 = (-2 * r->y0) + ro_cw->origin_y;
	}

	error = xwimp_force_redraw(ro_cw->wh,
				   info.extent.x0, info.extent.y0,
				   info.extent.x1, info.extent.y1);
	if (error) {
		LOG("xwimp_force_redraw: 0x%x: %s",
		    error->errnum, error->errmess);
		return NSERROR_INVALID;
	}
	return NSERROR_OK;
}


/**
 * Callback from the core to update the content area size.
 */
static void
ro_cw_update_size(struct core_window *cw, int width, int height)
{
	struct ro_corewindow *ro_cw = (struct ro_corewindow *)cw;
	wimp_open open;
	wimp_window_state state;
	os_error *error;

	LOG("content resize from w:%d h:%d to w:%d h:%d",
	    ro_cw->content_width, ro_cw->content_height, width, height);

	ro_cw->content_width = width * 2;
	ro_cw->content_height = -(2 * height);

	state.w = ro_cw->wh;
	error = xwimp_get_window_state(&state);
	if (error) {
		LOG("xwimp_get_window_state: 0x%x: %s",
		    error->errnum, error->errmess);
		return;
	}

	/* only update the window if it is open */
	if (state.flags & wimp_WINDOW_OPEN) {
		open.w = ro_cw->wh;
		open.visible = state.visible;
		open.xscroll = state.xscroll;
		open.yscroll = state.yscroll;
		open.next = state.next;

		update_scrollbars(ro_cw, &open);
	}
}


/**
 * Callback from the core to scroll the visible content.
 */
static void
ro_cw_scroll_visible(struct core_window *cw, const struct rect *r)
{
	//struct ro_corewindow *ro_cw = (struct ro_corewindow *)cw;
}


/**
 * Callback from the core to obtain the window viewport dimensions
 *
 * \param[in] cw the core window object
 * \param[out] width to be set to viewport width in px
 * \param[out] height to be set to viewport height in px
 */
static void
ro_cw_get_window_dimensions(struct core_window *cw, int *width, int *height)
{
	struct ro_corewindow *ro_cw = (struct ro_corewindow *)cw;
	os_error *error;
	wimp_window_state state;

	state.w = ro_cw->wh;
	error = xwimp_get_window_state(&state);
	if (error) {
		LOG("xwimp_get_window_state: 0x%x: %s",
		    error->errnum, error->errmess);
		return;
	}

	*width = (state.visible.x1 - state.visible.x0) / 2;
	*height = (state.visible.y1 - state.visible.y0) / 2;
}


/**
 * Callback from the core to update the drag status.
 */
static void
ro_cw_drag_status(struct core_window *cw, core_window_drag_status ds)
{
	struct ro_corewindow *ro_cw = (struct ro_corewindow *)cw;
	ro_cw->drag_status = ds;
}


struct core_window_callback_table ro_cw_cb_table = {
	.invalidate = ro_cw_invalidate,
	.update_size = ro_cw_update_size,
	.scroll_visible = ro_cw_scroll_visible,
	.get_window_dimensions = ro_cw_get_window_dimensions,
	.drag_status = ro_cw_drag_status
};

/**
 * dummy toolbar click callback
 *
 */
static nserror dummy_toolbar_click(struct ro_corewindow *ro_cw, button_bar_action action)
{
	return NSERROR_OK;
}

/**
 * dummy toolbar update callback
 */
static nserror dummy_toolbar_update(struct ro_corewindow *ro_cw)
{
	return NSERROR_OK;
}

/**
 * dummy toolbar save callback
 */
static nserror dummy_toolbar_save(struct ro_corewindow *ro_cw, char *config)
{
	return NSERROR_OK;
}

/* exported function documented ro/corewindow.h */
nserror
ro_corewindow_init(struct ro_corewindow *ro_cw,
		   const struct button_bar_buttons *tb_buttons,
		   char *tb_order,
		   theme_style tb_style,
		   const char *tb_help)
{
	/* setup the core window callback table */
	ro_cw->cb_table = &ro_cw_cb_table;

	/* start with the content area being as small as possible */
	ro_cw->content_width = -1;
	ro_cw->content_height = -1;
	ro_cw->origin_y = 0; /* no offset */
	ro_cw->drag_status = CORE_WINDOW_DRAG_NONE; /* no drag */

	/* Create toolbar. */
	if (tb_buttons != NULL) {
		/* ensure toolbar callbacks are always valid so calls
		 * do not have to be conditional
		 */
		if (ro_cw->toolbar_click == NULL) {
			ro_cw->toolbar_click = dummy_toolbar_click;
		}
		if (ro_cw->toolbar_save == NULL) {
			ro_cw->toolbar_save = dummy_toolbar_save;
		}
		if (ro_cw->toolbar_update == NULL) {
			ro_cw->toolbar_update = dummy_toolbar_update;
		}

		ro_cw->toolbar = ro_toolbar_create(NULL,
						   ro_cw->wh,
						   tb_style,
						   TOOLBAR_FLAGS_NONE,
						   &corewindow_toolbar_callbacks,
						   ro_cw,
						   tb_help);
		if (ro_cw->toolbar == NULL) {
			return NSERROR_INIT_FAILED;
		}

		ro_toolbar_add_buttons(ro_cw->toolbar, tb_buttons, tb_order);
		ro_toolbar_rebuild(ro_cw->toolbar);
		ro_cw->origin_y = -(ro_toolbar_height(ro_cw->toolbar));
	} else {
		ro_cw->toolbar = NULL; /* no toolbar */

		/* ensure callback functions are set to defaults when
		 * no toolbar
		 */
		ro_cw->toolbar_click = dummy_toolbar_click;
		ro_cw->toolbar_save = dummy_toolbar_save;
		ro_cw->toolbar_update = dummy_toolbar_update;
	}

	/* setup context for event handlers */
	ro_gui_wimp_event_set_user_data(ro_cw->wh, ro_cw);

	/* register wimp events. */
	ro_gui_wimp_event_register_redraw_window(ro_cw->wh,
			ro_cw_redraw);
	ro_gui_wimp_event_register_scroll_window(ro_cw->wh,
			ro_cw_scroll);
	ro_gui_wimp_event_register_pointer_entering_window(ro_cw->wh,
			ro_cw_pointer_entering);
	ro_gui_wimp_event_register_open_window(ro_cw->wh,
			ro_cw_open);
	ro_gui_wimp_event_register_mouse_click(ro_cw->wh,
			ro_cw_mouse_click);
	ro_gui_wimp_event_register_keypress(ro_cw->wh,
			ro_cw_keypress);

	return NSERROR_OK;
}

/* exported interface documented in ro/corewindow.h */
nserror ro_corewindow_fini(struct ro_corewindow *ro_cw)
{
	ro_gui_wimp_event_finalise(ro_cw->wh);

	/** \todo need to consider freeing of toolbar */

	return NSERROR_OK;
}