summaryrefslogtreecommitdiff
path: root/atari/toolbar.c
blob: 6c08428a124a214792cc08f5cf113476da44490f (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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
/*
 * Copyright 2012 Ole Loots <ole@monochrom.net>
 *
 * 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/>.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>

#include "utils/log.h"
#include "desktop/gui.h"
#include "desktop/local_history.h"
#include "desktop/netsurf.h"
#include "desktop/browser.h"
#include "desktop/browser_private.h"
#include "desktop/mouse.h"
#include "desktop/plot_style.h"
#include "desktop/plotters.h"
#include "desktop/tree.h"
#include "utils/nsoption.h"
#include "utils/utf8.h"
#include "atari/clipboard.h"
#include "atari/gui.h"
#include "atari/toolbar.h"
#include "atari/rootwin.h"

#include "atari/clipboard.h"
#include "atari/misc.h"
#include "atari/plot/plot.h"
#include "cflib.h"
#include "atari/res/netsurf.rsh"

#include "desktop/textarea.h"
#include "desktop/textinput.h"
#include "content/hlcache.h"


#define TB_BUTTON_WIDTH 32
#define THROBBER_WIDTH 32
#define THROBBER_MIN_INDEX 1
#define THROBBER_MAX_INDEX 12
#define THROBBER_INACTIVE_INDEX 13

enum e_toolbar_button_states {
        button_on = 0,
        button_off = 1
};
#define TOOLBAR_BUTTON_NUM_STATES   2

struct s_toolbar;

struct s_tb_button
{
	short rsc_id;
	void (*cb_click)(struct s_toolbar *tb);
	hlcache_handle *icon[TOOLBAR_BUTTON_NUM_STATES];
	struct s_toolbar *owner;
    enum e_toolbar_button_states state;
    short index;
    GRECT area;
};


extern char * option_homepage_url;
extern void * h_gem_rsrc;
extern struct gui_window * input_window;
extern long atari_plot_flags;
extern int atari_plot_vdi_handle;
extern EVMULT_OUT aes_event_out;

static OBJECT * aes_toolbar = NULL;
static OBJECT * throbber_form = NULL;
static bool init = false;
static int area_navigation_height = 0;
static int area_search_height = 0;
static int area_full_height = 0;

static plot_font_style_t font_style_url = {
    .family = PLOT_FONT_FAMILY_SANS_SERIF,
    .size = 14*FONT_SIZE_SCALE,
    .weight = 400,
    .flags = FONTF_NONE,
    .background = 0xffffff,
    .foreground = 0x0
 };


/* prototypes & order for button widgets: */


static struct s_tb_button tb_buttons[] =
{
	{
        TOOLBAR_BT_BACK,
        toolbar_back_click,
        {0,0},
        0, 0, 0, {0,0,0,0}
    },
	{
        TOOLBAR_BT_HOME,
        toolbar_home_click,
        {0,0},
        0, 0, 0, {0,0,0,0}
    },
	{
        TOOLBAR_BT_FORWARD,
        toolbar_forward_click,
        {0,0},
        0, 0, 0, {0,0,0,0}
    },
	{
        TOOLBAR_BT_STOP,
        toolbar_stop_click,
        {0,0},
        0, 0, 0, {0,0,0,0}
    },
	{
        TOOLBAR_BT_RELOAD,
        toolbar_reload_click,
        {0,0},
        0, 0, 0, {0,0,0,0}
    },
	{ 0, 0, {0,0}, 0, -1, 0, {0,0,0,0}}
};

struct s_toolbar_style {
	int font_height_pt;
};

static struct s_toolbar_style toolbar_styles[] =
{
	/* small (18 px height) */
	{9},
	/* medium (default - 26 px height) */
	{14},
	/* large ( 49 px height ) */
	{18},
	/* custom style: */
	{18}
};

static const struct redraw_context toolbar_rdrw_ctx = {
				.interactive = true,
				.background_images = true,
				.plot = &atari_plotters
			};

static void tb_txt_request_redraw(void *data, int x, int y, int w, int h );
static nserror toolbar_icon_callback( hlcache_handle *handle,
		const hlcache_event *event, void *pw );

/**
*   Find a button for a specific resource ID
*/
static struct s_tb_button *find_button(struct s_toolbar *tb, int rsc_id)
{
	int i = 0;
	while (i < tb->btcnt) {
		if (tb->buttons[i].rsc_id == rsc_id) {
			return(&tb->buttons[i]);
		}
		i++;
	}
	return(NULL);
}

/**
*   Callback for textarea redraw
*/
static void tb_txt_request_redraw(void *data, int x, int y, int w, int h)
{

    GRECT area;
	struct s_toolbar * tb = (struct s_toolbar *)data;

	if (tb->attached == false) {
        return;
    }

	toolbar_get_grect(tb, TOOLBAR_AREA_URL, &area);
	area.g_x += x;
	area.g_y += y;
	area.g_w = w;
	area.g_h = h;
	//dbg_grect("tb_txt_request_redraw", &area);
	window_schedule_redraw_grect(tb->owner, &area);
    return;
}

static void tb_txt_callback(void *data, struct textarea_msg *msg)
{
	switch (msg->type) {

		case TEXTAREA_MSG_DRAG_REPORT:
		break;

		case TEXTAREA_MSG_REDRAW_REQUEST:
			tb_txt_request_redraw(data,
				msg->data.redraw.x0, msg->data.redraw.y0,
				msg->data.redraw.x1 - msg->data.redraw.x0,
				msg->data.redraw.y1 - msg->data.redraw.y0);
		break;

		default:
		break;
	}
}

static struct s_tb_button *button_init(struct s_toolbar *tb, OBJECT * tree, int index,
							struct s_tb_button * instance)
{
	*instance = tb_buttons[index];
	instance->owner = tb;

    return(instance);
}


static short __CDECL toolbar_url_userdraw(PARMBLK *parmblock)
{
	return(0);
}

void toolbar_init( void )
{
	static USERBLK userblk;

    aes_toolbar = gemtk_obj_get_tree(TOOLBAR);
    throbber_form = gemtk_obj_get_tree(THROBBER);

	userblk.ub_code = toolbar_url_userdraw;
    userblk.ub_parm = (long) aes_toolbar[TOOLBAR_AREA_URL].ob_spec.userblk;
    aes_toolbar[TOOLBAR_AREA_URL].ob_spec.userblk = &userblk;

	aes_toolbar[TOOLBAR_CB_SHOWALL].ob_state &= ~OS_SELECTED;
	aes_toolbar[TOOLBAR_CB_CASESENSE].ob_state &= ~OS_SELECTED;

	/* init default values: */
	gemtk_obj_set_str_safe(aes_toolbar, TOOLBAR_TB_SRCH, (char*)"");

    area_full_height = aes_toolbar->ob_height;
	area_search_height = aes_toolbar[TOOLBAR_AREA_SEARCH].ob_height;
	area_navigation_height = aes_toolbar[TOOLBAR_AREA_NAVIGATION].ob_height;

    init = true;
}


void toolbar_exit(void)
{

}


struct s_toolbar *toolbar_create(struct s_gui_win_root *owner)
{
	int i;
	struct s_toolbar *t;

	LOG((""));

	assert(init == true);

	t = calloc(sizeof(struct s_toolbar), 1);

	assert(t);

	/* initialize the toolbar values: */
	t->owner = owner;
	t->style = 1;
	t->search_visible = false;
	t->visible = true;
	t->reflow = true;

	/* dublicate the form template: */
	t->form = gemtk_obj_tree_copy(aes_toolbar);


	/* count buttons and add them as components: */
	i = 0;
	while(tb_buttons[i].rsc_id > 0) {
		i++;
	}
	t->btcnt = i;
	t->buttons = malloc(t->btcnt * sizeof(struct s_tb_button));
	memset(t->buttons, 0, t->btcnt * sizeof(struct s_tb_button));
	for (i=0; i < t->btcnt; i++) {
		button_init(t, aes_toolbar, i, &t->buttons[i]);
	}

	/* create the url widget: */
	font_style_url.size =
		toolbar_styles[t->style].font_height_pt * FONT_SIZE_SCALE;

	textarea_flags ta_flags = TEXTAREA_INTERNAL_CARET;
	textarea_setup ta_setup;
	ta_setup.width = 300;
	ta_setup.height = t->form[TOOLBAR_AREA_URL].ob_height;
	ta_setup.pad_top = 0;
	ta_setup.pad_right = 4;
	ta_setup.pad_bottom = 0;
	ta_setup.pad_left = 4;
	ta_setup.border_width = 1;
	ta_setup.border_col = 0x000000;
	ta_setup.selected_text = 0xffffff;
	ta_setup.selected_bg = 0x000000;
	ta_setup.text = font_style_url;
	ta_setup.text.foreground = 0x000000;
	ta_setup.text.background = 0xffffff;
	t->url.textarea = textarea_create(ta_flags, &ta_setup,
			tb_txt_callback, t);

	/* create the throbber widget: */
	t->throbber.index = THROBBER_INACTIVE_INDEX;
	t->throbber.max_index = THROBBER_MAX_INDEX;
	t->throbber.running = false;

	LOG(("created toolbar: %p, root: %p, textarea: %p, throbber: %p", t,
        owner, t->url.textarea, t->throbber));
	return( t );
}


void toolbar_destroy(struct s_toolbar *tb)
{
    free(tb->buttons);
	free(tb->form);

	textarea_destroy(tb->url.textarea);

	free(tb);
}

static int toolbar_calculate_height(struct s_toolbar *tb)
{
	int r = 0;

	if (tb->visible == false) {
		return(0);
	}

	r += area_navigation_height;

	if (tb->search_visible) {
		r += area_search_height;
	}

	return(r);
}

static void toolbar_reflow(struct s_toolbar *tb)
{
    int i;

    // position toolbar areas:
    tb->form->ob_x = tb->area.g_x;
    tb->form->ob_y = tb->area.g_y;
    tb->form->ob_width = tb->area.g_w;
    tb->form->ob_height = toolbar_calculate_height(tb);

	// expand the "main" areas to the current width:
    tb->form[TOOLBAR_AREA_NAVIGATION].ob_width = tb->area.g_w;
    tb->form[TOOLBAR_AREA_SEARCH].ob_width = tb->area.g_w;

    if (tb->search_visible) {
		tb->form[TOOLBAR_AREA_SEARCH].ob_state &= ~OF_HIDETREE;
    } else {
    	tb->form[TOOLBAR_AREA_SEARCH].ob_state |= OF_HIDETREE;

    }

	// align the throbber area at right edge:
    tb->form[TOOLBAR_THROBBER_AREA].ob_x = tb->area.g_w
        - tb->form[TOOLBAR_THROBBER_AREA].ob_width;

	// align the search button:
	tb->form[TOOLBAR_SEARCH_ALIGN_RIGHT].ob_x = tb->area.g_w
        - tb->form[TOOLBAR_SEARCH_ALIGN_RIGHT].ob_width;

	// center the URL area:
    tb->form[TOOLBAR_AREA_URL].ob_width = tb->area.g_w
       - (tb->form[TOOLBAR_AREA_BUTTONS].ob_width
       + tb->form[TOOLBAR_THROBBER_AREA].ob_width + 1);


    // position throbber image:
    throbber_form[tb->throbber.index].ob_x = tb->area.g_x +
        tb->form[TOOLBAR_THROBBER_AREA].ob_x;

    throbber_form[tb->throbber.index].ob_x = tb->area.g_x
        + tb->form[TOOLBAR_THROBBER_AREA].ob_x +
        ((tb->form[TOOLBAR_THROBBER_AREA].ob_width
        - throbber_form[tb->throbber.index].ob_width) >> 1);

    throbber_form[tb->throbber.index].ob_y = tb->area.g_y +
        ((tb->form[TOOLBAR_THROBBER_AREA].ob_height
        - throbber_form[tb->throbber.index].ob_height) >> 1);

    // set button states:
    for (i=0; i < tb->btcnt; i++ ) {
        if (tb->buttons[i].state == button_off) {
            tb->form[tb->buttons[i].rsc_id].ob_state |= OS_DISABLED;
        }
        else if (tb->buttons[i].state == button_on) {
            tb->form[tb->buttons[i].rsc_id].ob_state &= ~OS_DISABLED;
        }
	}
    tb->reflow = false;
    // TODO: iterate through all other toolbars and set reflow = true
}

void toolbar_redraw(struct s_toolbar *tb, GRECT *clip)
{
    GRECT area, area_ro;

    if (tb->attached == false) {
        return;
    }

    if(tb->reflow == true)
        toolbar_reflow(tb);

	//TODO: fix redraw under popup menu ... that not handled correctly somehow.

	//dbg_grect("toolbar redraw clip", clip);

	/* Redraw the AES objects: */
    objc_draw_grect(tb->form,0,8,clip);
    objc_draw_grect(&throbber_form[tb->throbber.index], 0, 1, clip);

    toolbar_get_grect(tb, TOOLBAR_AREA_URL, &area_ro);
    area = area_ro;

    if (rc_intersect(clip, &area)) {

        plot_set_dimensions(area_ro.g_x, area_ro.g_y, area_ro.g_w, area_ro.g_h);
        struct rect r = {
					.x0 = MAX(0,area.g_x - area_ro.g_x),
					.y0 = MAX(0,area.g_y - area_ro.g_y),
					.x1 = MAX(0,area.g_x - area_ro.g_x) + area.g_w,
					.y1 = MAX(0,area.g_y - area_ro.g_y) + area.g_h
		};
		//dbg_rect("tb textarea clip: ", &r);
		// TODO: let this be handled by an userdef object redraw function:
		/* Redraw the url input: */
        textarea_redraw(tb->url.textarea, 0, 0, 0xffffff, 1.0, &r, &toolbar_rdrw_ctx);
    }
}


void toolbar_update_buttons(struct s_toolbar *tb, struct browser_window *bw,
                       short button)
{
    LOG((""));

	struct s_tb_button * bt;
	bool enable = false;
	GRECT area;

	assert(bw != NULL);

	if (button == TOOLBAR_BT_BACK || button <= 0 ) {
	    bt = find_button(tb, TOOLBAR_BT_BACK);
		enable = browser_window_back_available(bw);
        if (enable) {
            bt->state = button_on;
        } else {
            bt->state = button_off;
        }
	}

	if (button == TOOLBAR_BT_HOME || button <= 0 ) {

	}

	if (button == TOOLBAR_BT_FORWARD || button <= 0 ) {
		bt = find_button(tb, TOOLBAR_BT_FORWARD);
		enable = browser_window_forward_available(bw);
        if (enable) {
            bt->state = button_on;
        } else {
            bt->state = button_off;
        }
	}

	if (button == TOOLBAR_BT_RELOAD || button <= 0 ) {
	    bt = find_button(tb, TOOLBAR_BT_RELOAD);
		enable = browser_window_reload_available(bw);
        if (enable) {
            bt->state = button_on;
        } else {
            bt->state = button_off;
        }
	}

	if (button == TOOLBAR_BT_STOP || button <= 0) {
	    bt = find_button(tb, TOOLBAR_BT_STOP);
		enable = browser_window_stop_available(bw);
        if (enable) {
            bt->state = button_on;
        } else {
            bt->state = button_off;
        }
	}

    if (tb->attached) {
        if (button > 0) {
            toolbar_get_grect(tb, button, &area);
            window_schedule_redraw_grect(tb->owner, &area);
        }
        else {
            toolbar_get_grect(tb, TOOLBAR_AREA_BUTTONS, &area);
            window_schedule_redraw_grect(tb->owner, &area);
        }
    }
}

void toolbar_set_width(struct s_toolbar *tb, short w)
{
	GRECT cur;

	toolbar_get_grect(tb, 0, &cur);

	if (w != cur.g_w) {

		tb->area.g_w = w;

        /* reflow now, just for url input calucation: */
        toolbar_reflow(tb);
        /* this will request an textarea redraw: */
        textarea_set_dimensions(tb->url.textarea,
                                tb->form[TOOLBAR_AREA_URL].ob_width,
                                tb->form[TOOLBAR_AREA_URL].ob_height);
		tb->reflow = true;
	}
}

void toolbar_set_origin(struct s_toolbar *tb, short x, short y)
{
	GRECT cur;

	toolbar_get_grect(tb, 0, &cur);

	if (x != cur.g_x || y != cur.g_y) {
		tb->area.g_x = x;
		tb->area.g_y = y;
		tb->reflow = true;
	}
}

void toolbar_set_dimensions(struct s_toolbar *tb, GRECT *area)
{
    if (area->g_w != tb->area.g_w)  {

        tb->area = *area;

        /* reflow now, just for url input calucation: */
        toolbar_reflow(tb);
        /* this will request an textarea redraw: */
        textarea_set_dimensions(tb->url.textarea,
                                tb->form[TOOLBAR_AREA_URL].ob_width,
                                tb->form[TOOLBAR_AREA_URL].ob_height-1);
    }
    else {
        tb->area = *area;
    }
	/* reflow for next redraw: */
    /* TODO: that's only required because we do not reset others toolbars reflow
             state on reflow */
    tb->reflow = true;
}


void toolbar_set_url(struct s_toolbar *tb, const char * text)
{
    LOG((""));
    textarea_set_text(tb->url.textarea, text);

    if (tb->attached && tb->visible) {
        GRECT area;
        toolbar_get_grect(tb, TOOLBAR_AREA_URL, &area);
        window_schedule_redraw_grect(tb->owner, &area);
        struct gui_window * gw = window_get_active_gui_window(tb->owner);
        assert(gw != NULL);
        toolbar_update_buttons(tb, gw->browser->bw , 0);
	}
}

void toolbar_set_throbber_state(struct s_toolbar *tb, bool active)
{
    GRECT throbber_area;

    tb->throbber.running = active;
    if (active) {
        tb->throbber.index = THROBBER_MIN_INDEX;
    } else {
        tb->throbber.index = THROBBER_INACTIVE_INDEX;
    }

    tb->reflow = true;
    toolbar_get_grect(tb, TOOLBAR_THROBBER_AREA, &throbber_area);
    window_schedule_redraw_grect(tb->owner, &throbber_area);
}

void toolbar_set_visible(struct s_toolbar *tb, short area, bool visible)
{
	if (area == 0) {
		if ((visible == false) && (tb->visible == true)) {
			tb->visible = false;
			tb->reflow = true;
		} else if((visible == true) && (tb->visible == false)) {
			tb->visible = false;
			tb->reflow = true;
		}
	}
	else if (area == TOOLBAR_AREA_SEARCH) {
		tb->search_visible = visible;
		tb->reflow = true;
		OBJECT *frm = toolbar_get_form(tb);
		if(visible == false){
			frm[TOOLBAR_AREA_SEARCH].ob_flags |= OF_HIDETREE;
		} else {
			frm[TOOLBAR_AREA_SEARCH].ob_flags &= ~OF_HIDETREE;
		}
	}
}

void toolbar_set_reflow(struct s_toolbar *tb, bool do_reflow)
{
	tb->reflow = do_reflow;
}

void toolbar_set_attached(struct s_toolbar *tb, bool attached)
{
    tb->attached = attached;

}

void toolbar_throbber_progress(struct s_toolbar *tb)
{
    GRECT throbber_area;

    assert(tb->throbber.running == true);

    if(tb->throbber.running == false)
        return;

    tb->throbber.index++;
    if(tb->throbber.index > THROBBER_MAX_INDEX)
        tb->throbber.index = THROBBER_MIN_INDEX;

    tb->reflow = true;
    toolbar_get_grect(tb, TOOLBAR_THROBBER_AREA, &throbber_area);
    window_schedule_redraw_grect(tb->owner, &throbber_area);
}

bool toolbar_text_input(struct s_toolbar *tb, char *text)
{
    bool handled = true;

    LOG((""));

    return(handled);
}

bool toolbar_key_input(struct s_toolbar *tb, short nkc)
{

	assert(tb!=NULL);

	GRECT work;
	bool ret = false;
	struct gui_window *gw = window_get_active_gui_window(tb->owner);

	assert( gw != NULL );

	long ucs4;
	long ik = nkc_to_input_key(nkc, &ucs4);

	if (ik == 0) {
		if ((nkc&0xFF) >= 9) {
			ret = textarea_keypress(tb->url.textarea, ucs4);
		}
	}
	else if (ik == KEY_CR || ik == KEY_NL) {
		nsurl *url;
		char tmp_url[PATH_MAX];
		if ( textarea_get_text( tb->url.textarea, tmp_url, PATH_MAX) > 0 ) {
			window_set_focus(tb->owner, BROWSER, gw->browser);

			if (nsurl_create((const char*)&tmp_url, &url) != NSERROR_OK) {
				warn_user("NoMemory", 0);
			} else {
				browser_window_navigate(gw->browser->bw,
					url,
					NULL,
					BROWSER_WINDOW_HISTORY |
					BROWSER_WINDOW_VERIFIABLE,
					NULL,
					NULL,
					NULL);
				nsurl_unref(url);
			}

			ret = true;
		}
	}
	else if (ik == KEY_COPY_SELECTION) {
		// copy whole text
		char * text;
		int len;
		len = textarea_get_text( tb->url.textarea, NULL, 0 );
		text = malloc( len+1 );
		if (text){
			textarea_get_text( tb->url.textarea, text, len+1 );
			scrap_txt_write(text);
			free( text );
		}
	}
	else if ( ik == KEY_PASTE) {
		char * clip = scrap_txt_read();
		if ( clip != NULL ){
			int clip_length = strlen( clip );
			if ( clip_length > 0 ) {
				char *utf8;
				utf8_convert_ret res;
				/* Clipboard is in local encoding so
				 * convert to UTF8 */
				res = utf8_from_local_encoding( clip, clip_length, &utf8 );
				if ( res == UTF8_CONVERT_OK ) {
					toolbar_set_url(tb, utf8);
					free(utf8);
					ret = true;
				}
			}
			free( clip );
		}
	}
	else if (ik == KEY_ESCAPE) {
		textarea_keypress( tb->url.textarea, KEY_SELECT_ALL );
		textarea_keypress( tb->url.textarea, KEY_DELETE_LEFT );
	}
	else {
		ret = textarea_keypress( tb->url.textarea, ik );
	}

	return( ret );
}


void toolbar_mouse_input(struct s_toolbar *tb, short obj, short button)
{
    LOG((""));
    GRECT work;
	short mx, my, mb, kstat;
	int old;
	OBJECT * toolbar_tree;
	struct gui_window * gw;


    if (obj==TOOLBAR_AREA_URL){

        graf_mkstate(&mx, &my, &mb,  &kstat);
        toolbar_get_grect(tb, TOOLBAR_AREA_URL, &work);
        mx -= work.g_x;
        my -= work.g_y;

	    /* TODO: reset mouse state of browser window? */
	    /* select whole text when newly focused, otherwise set caret to
	        end of text */
        if (!window_url_widget_has_focus(tb->owner)) {
            window_set_focus(tb->owner, URL_WIDGET, (void*)&tb->url);
        }
        /* url widget has focus and mouse button is still pressed... */
        else if (mb & 1) {

            textarea_mouse_action(tb->url.textarea, BROWSER_MOUSE_DRAG_1,
									mx, my );
			short prev_x = mx;
			short prev_y = my;
			do {
				if (abs(prev_x-mx) > 5 || abs(prev_y-my) > 5) {
					textarea_mouse_action( tb->url.textarea,
										BROWSER_MOUSE_HOLDING_1, mx, my );
					prev_x = mx;
					prev_y = my;
					window_schedule_redraw_grect(tb->owner, &work);
					window_process_redraws(tb->owner);
				}
				graf_mkstate( &mx, &my, &mb,  &kstat );
				mx = mx - (work.g_x);
				my = my - (work.g_y);
            } while (mb & 1);

			textarea_mouse_action( tb->url.textarea, BROWSER_MOUSE_HOVER, mx,
									my);
        }
        else if (button & 2) {
			// TODO: open a context popup
        }
        else {
            /* when execution reaches here, mouse input is a click or dclick */
            /* TODO: recognize click + shitoolbar_update_buttonsft key */
			int mstate = BROWSER_MOUSE_PRESS_1;
			if ((kstat & (K_LSHIFT|K_RSHIFT)) != 0) {
				mstate = BROWSER_MOUSE_MOD_1;
			}
            if (aes_event_out.emo_mclicks == 2 ) {
				textarea_mouse_action( tb->url.textarea,
						BROWSER_MOUSE_DOUBLE_CLICK | BROWSER_MOUSE_CLICK_1, mx,
						my);
                toolbar_get_grect(tb, TOOLBAR_AREA_URL, &work);
                window_schedule_redraw_grect(tb->owner, &work);
			} else {
				textarea_mouse_action(tb->url.textarea,
						BROWSER_MOUSE_PRESS_1, mx, my );
			}
        }
    }
    else if(obj==TOOLBAR_TB_SRCH) {
		window_set_focus(tb->owner, SEARCH_INPUT, NULL);
    }
	else if (obj==TOOLBAR_BT_SEARCH_FWD) {
		gw = tb->owner->active_gui_window;
		assert(gw->search);
		nsatari_search_perform(gw->search, tb->form, SEARCH_FLAG_FORWARDS);
	}
	else if (obj==TOOLBAR_BT_SEARCH_BACK) {
		gw = tb->owner->active_gui_window;
		assert(gw->search);
		nsatari_search_perform(gw->search, tb->form, 0);
	}
	else if (obj==TOOLBAR_BT_CLOSE_SEARCH) {
		tb->form[TOOLBAR_BT_CLOSE_SEARCH].ob_state &= ~OS_SELECTED;
		window_close_search(tb->owner);
	}
    else {
        struct s_tb_button *bt = find_button(tb, obj);
        if (bt != NULL && bt->state != button_off) {
            bt->cb_click(tb);
            struct gui_window * gw = window_get_active_gui_window(tb->owner);
            toolbar_update_buttons(tb, gw->browser->bw, 0);
        }

    }
}


/**
* Receive a specific region of the toolbar.
* @param tb - the toolbar pointer
* @param which - the area to retrieve:  0 to receive the workarea,
										all other values must be
										an resource ID of the TOOLBAR tree.
* @param dst - GRECT pointer receiving the area.
*/

void toolbar_get_grect(struct s_toolbar *tb, short which, GRECT *dst)
{
	#define LAST_TOOLBAR_AREA TOOLBAR_AREA_SEARCH

    if (tb->reflow == true) {
        toolbar_reflow(tb);
    }

    objc_offset(tb->form, which, &dst->g_x, &dst->g_y);

    dst->g_w = tb->form[which].ob_width;
    dst->g_h = tb->form[which].ob_height;
    //tb->form[which].ob_height;

    //printf("Toolbar get grect (%d): ", which);
    //dbg_grect("", dst);

    #undef LAST_TOOLBAR_AREA
}


struct textarea *toolbar_get_textarea(struct s_toolbar *tb,
                                       enum toolbar_textarea which)
{
    return(tb->url.textarea);
}

OBJECT *toolbar_get_form(struct s_toolbar *tb)
{
	return(tb->form);
}


/* public event handler */
void toolbar_back_click(struct s_toolbar *tb)
{
    struct browser_window * bw;
    struct gui_window * gw;

    gw = window_get_active_gui_window(tb->owner);
    assert(gw != NULL);
    bw = gw->browser->bw;
    assert(bw != NULL);

    if( history_back_available(bw->history) )
		history_back(bw, bw->history);
}

void toolbar_reload_click(struct s_toolbar *tb)
{
    struct browser_window * bw;
    struct gui_window * gw;

    gw = window_get_active_gui_window(tb->owner);
    assert(gw != NULL);
    bw = gw->browser->bw;
    assert(bw != NULL);

	browser_window_reload(bw, true);
}

void toolbar_forward_click(struct s_toolbar *tb)
{
    struct browser_window * bw;
    struct gui_window * gw;

    gw = window_get_active_gui_window(tb->owner);
    assert(gw != NULL);
    bw = gw->browser->bw;
    assert(bw != NULL);

	if (history_forward_available(bw->history))
		history_forward(bw, bw->history);
}

void toolbar_home_click(struct s_toolbar *tb)
{
	struct browser_window * bw;
	struct gui_window * gw;
	nsurl *url;

	gw = window_get_active_gui_window(tb->owner);
	assert(gw != NULL);
	bw = gw->browser->bw;
	assert(bw != NULL);

	if(nsoption_charp(homepage_url) == NULL){
		return;
	}

	if (nsurl_create(nsoption_charp(homepage_url), &url) != NSERROR_OK) {
		warn_user("NoMemory", 0);
	} else {
		browser_window_navigate(bw,
					url,
					NULL,
					BROWSER_WINDOW_HISTORY |
					BROWSER_WINDOW_VERIFIABLE,
					NULL,
					NULL,
					NULL);
			nsurl_unref(url);
	}
}


void toolbar_stop_click(struct s_toolbar *tb)
{
    struct browser_window * bw;
    struct gui_window * gw;

    gw = window_get_active_gui_window(tb->owner);

    assert(gw != NULL);

    bw = gw->browser->bw;

    assert(bw != NULL);

	browser_window_stop(bw);
}