summaryrefslogtreecommitdiff
path: root/frontends/atari/settings.c
blob: ac4afa315caf26236ae4e8307259d7805d038b5c (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
/*
 * Copyright 2013 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 <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <cflib.h>
#include <gem.h>

#include "utils/dirent.h"
#include "utils/nsoption.h"
#include "utils/log.h"
#include "utils/utils.h"
#include "netsurf/plot_style.h"

#include "atari/gui.h"
#include "atari/res/netsurf.rsh"
#include "atari/settings.h"
#include "atari/deskmenu.h"
#include "atari/misc.h"
#include "atari/plot/plot.h"
#include "atari/bitmap.h"
#include "atari/findfile.h"
#include "atari/gemtk/gemtk.h"

extern char options[PATH_MAX];
extern GRECT desk_area;

static float tmp_option_minimum_gif_delay;
static unsigned tmp_option_memory_cache_size;
static unsigned int tmp_option_disc_cache_size;
static unsigned int tmp_option_expire_url;
static unsigned int tmp_option_disc_cache_age;
static unsigned int tmp_option_font_min_size;
static unsigned int tmp_option_font_size;
static unsigned int tmp_option_min_reflow_period;
static unsigned int tmp_option_max_fetchers;
static unsigned int tmp_option_max_fetchers_per_host;
static unsigned int tmp_option_max_cached_fetch_handles;

static int num_locales = 0;
static char **locales = NULL;
static short h_aes_win = 0;
static GUIWIN * settings_guiwin = NULL;
static OBJECT * dlgtree;

/* Available font engines for the font engine selection popup: */
static const char *font_engines[]  = {

#ifdef WITH_FREETYPE_FONT_DRIVER
    "freetype",
#endif

#ifdef WITH_INTERNAL_FONT_DRIVER
    "internal",
#endif

#ifdef WITH_VDI_FONT_DRIVER
    "vdi",
#endif
};

/* Available GUI timeouts for the timeout selection popup: */
static const char *gui_timeouts[]  = {
    "0", "5", "10"
};

#define OBJ_SELECTED(idx) ((bool)((dlgtree[idx].ob_state & OS_SELECTED)!=0))

#define OBJ_CHECK(idx) (dlgtree[idx].ob_state |= (OS_SELECTED));

#define OBJ_UNCHECK(idx) (dlgtree[idx].ob_state &= ~(OS_SELECTED));

#define OBJ_REDRAW(idx) gemtk_wm_exec_redraw(settings_guiwin, \
										gemtk_obj_screen_rect(dlgtree, idx));

#define DISABLE_OBJ(idx) (dlgtree[idx].ob_state |= OS_DISABLED); \
						 gemtk_wm_exec_redraw(settings_guiwin, \
											gemtk_obj_screen_rect(dlgtree, idx));

#define ENABLE_OBJ(idx) (dlgtree[idx].ob_state &= ~(OS_DISABLED)); \
						 gemtk_wm_exec_redraw(settings_guiwin, \
											gemtk_obj_screen_rect(dlgtree, idx));

#define FORMEVENT(idx) form_event(idx, 0);

#define INPUT_HOMEPAGE_URL_MAX_LEN 44
#define INPUT_LOCALE_MAX_LEN 6
#define INPUT_PROXY_HOST_MAX_LEN 31
#define INPUT_PROXY_USERNAME_MAX_LEN 36
#define INPUT_PROXY_PASSWORD_MAX_LEN 36
#define INPUT_PROXY_PORT_MAX_LEN 5
#define INPUT_MIN_REFLOW_PERIOD_MAX_LEN 4
#define LABEL_FONT_RENDERER_MAX_LEN 8
#define LABEL_PATH_MAX_LEN 40
#define LABEL_ICONSET_MAX_LEN 8
#define INPUT_TOOLBAR_COLOR_MAX_LEN 6


static void display_settings(void);
static void form_event(int index, int external);
static void apply_settings(void);
static void save_settings(void);


static void set_text( short idx, char * text, int len )
{
    char spare[255];

    if( len > 254 )
        len = 254;
    if( text != NULL ) {
        strncpy(spare, text, 254);
    } else {
        strcpy(spare, "");
    }

    set_string( dlgtree, idx, spare);
}




/**
 * Toogle all objects which are directly influenced by other GUI elements
 * ( like checkbox )
 */
static void toggle_objects(void)
{
    /* enable / disable (refresh) objects depending on radio button values: */
    /* Simulate GUI events which trigger refresh of bound elements: */
    FORMEVENT(SETTINGS_CB_USE_PROXY);
    FORMEVENT(SETTINGS_CB_PROXY_AUTH);
    FORMEVENT(SETTINGS_BT_SEL_FONT_RENDERER);
}

static char **read_locales(void)
{
    char buf[PATH_MAX];
    char tmp_locale[16];
    char **locales = NULL;

    FILE * fp_locales = NULL;

    atari_find_resource(buf, "languages", "./res/languages");

    fp_locales = fopen(buf, "r");

    if (fp_locales == NULL) {
        atari_warn_user("Failed to load locales: %s",buf);
        return(NULL);
    } else {
        NSLOG(netsurf, INFO, "Reading locales from: %s...", buf);
    }

    /* Count items: */
    num_locales = 0;
    while (fgets(tmp_locale, 16, fp_locales) != NULL) {
            num_locales++;
    }

    locales = malloc(sizeof(char*)*num_locales);

    rewind(fp_locales);
    int i = 0;
    while (fgets(tmp_locale, 16, fp_locales) != NULL) {
        int len = strlen(tmp_locale);
        tmp_locale[len-1] = 0;
        len--;
        locales[i] = malloc(len+1);
        // do not copy the last \n
        snprintf(locales[i], 16, "%s", tmp_locale);
        i++;
    }

    fclose(fp_locales);

    return(locales);
}


static void save_settings(void)
{
    apply_settings();
    // Save settings
    nsoption_write( (const char*)&options, NULL, NULL);
    nsoption_read( (const char*)&options , NULL);
    close_settings();
    form_alert(1, "[1][Some options require an netsurf restart!][OK]");
    deskmenu_update();
}

/* this gets called each time the settings dialog is opened: */
static void display_settings(void)
{
    char spare[255];
    // read current settings and display them


    /* "Browser" tab: */
    set_text( SETTINGS_EDIT_HOMEPAGE, nsoption_charp(homepage_url),
              INPUT_HOMEPAGE_URL_MAX_LEN );

    if( nsoption_bool(block_advertisements) ) {
        OBJ_CHECK( SETTINGS_CB_HIDE_ADVERTISEMENT );
    } else {
        OBJ_UNCHECK( SETTINGS_CB_HIDE_ADVERTISEMENT );
    }
    if( nsoption_bool(target_blank) ) {
        OBJ_UNCHECK( SETTINGS_CB_DISABLE_POPUP_WINDOWS );
    } else {
        OBJ_CHECK( SETTINGS_CB_DISABLE_POPUP_WINDOWS );
    }
    if( nsoption_bool(send_referer) ) {
        OBJ_CHECK( SETTINGS_CB_SEND_HTTP_REFERRER );
    } else {
        OBJ_UNCHECK( SETTINGS_CB_SEND_HTTP_REFERRER );
    }
    if( nsoption_bool(do_not_track) ) {
        OBJ_CHECK( SETTINGS_CB_SEND_DO_NOT_TRACK );
    } else {
        OBJ_UNCHECK( SETTINGS_CB_SEND_DO_NOT_TRACK );
    }

    set_text( SETTINGS_BT_SEL_LOCALE,
              nsoption_charp(accept_language) ? nsoption_charp(accept_language) : (char*)"en",
              INPUT_LOCALE_MAX_LEN );

    sprintf(spare, "%d", nsoption_int(atari_gui_poll_timeout));
    set_text(SETTINGS_BT_GUI_TOUT, spare, 2);

    tmp_option_expire_url = nsoption_int(expire_url);
    snprintf( spare, 255, "%02d", nsoption_int(expire_url) );
    set_text( SETTINGS_EDIT_HISTORY_AGE, spare,  3);

    /* "Cache" tab: */
    tmp_option_memory_cache_size = nsoption_int(memory_cache_size) / (1024*1024);
    snprintf( spare, 255, "%d", tmp_option_memory_cache_size );
    set_text( SETTINGS_STR_MAX_MEM_CACHE, spare, 4 );

    tmp_option_disc_cache_size = nsoption_int(disc_cache_size) / (1024*1024);
    snprintf( spare, 255, "%d", tmp_option_disc_cache_size );
    set_text( SETTINGS_STR_MAX_DISC_CACHE, spare, 4 );

    tmp_option_disc_cache_age = nsoption_int(disc_cache_age);
    snprintf( spare, 255, "%02d", tmp_option_disc_cache_age );
    set_text( SETTINGS_EDIT_CACHE_AGE, spare, 3 );

    /* "Paths" tab: */
    set_text( SETTINGS_EDIT_DOWNLOAD_PATH, nsoption_charp(downloads_path),
              LABEL_PATH_MAX_LEN );
    set_text( SETTINGS_EDIT_HOTLIST_FILE, nsoption_charp(hotlist_file),
              LABEL_PATH_MAX_LEN );
    set_text( SETTINGS_EDIT_CA_BUNDLE, nsoption_charp(ca_bundle),
              LABEL_PATH_MAX_LEN );
    set_text( SETTINGS_EDIT_CA_CERTS_PATH, nsoption_charp(ca_path),
              LABEL_PATH_MAX_LEN );
    set_text( SETTINGS_EDIT_EDITOR, nsoption_charp(atari_editor),
              LABEL_PATH_MAX_LEN );

    /* "Rendering" tab: */
    set_text( SETTINGS_BT_SEL_FONT_RENDERER, nsoption_charp(atari_font_driver),
              LABEL_FONT_RENDERER_MAX_LEN );
    SET_BIT(dlgtree[SETTINGS_CB_TRANSPARENCY].ob_state,
            OS_SELECTED, nsoption_int(atari_transparency) ? 1 : 0 );
    SET_BIT(dlgtree[SETTINGS_CB_ENABLE_ANIMATION].ob_state,
            OS_SELECTED, nsoption_bool(animate_images) ? 1 : 0 );
    SET_BIT(dlgtree[SETTINGS_CB_FG_IMAGES].ob_state,
            OS_SELECTED, nsoption_bool(foreground_images) ? 1 : 0 );
    SET_BIT(dlgtree[SETTINGS_CB_BG_IMAGES].ob_state,
            OS_SELECTED, nsoption_bool(background_images) ? 1 : 0 );


    // TODO: enable this option?
    /*	SET_BIT(dlgtree[SETTINGS_CB_INCREMENTAL_REFLOW].ob_state,
    			OS_SELECTED, nsoption_bool(incremental_reflow) ? 1 : 0 );*/

    SET_BIT(dlgtree[SETTINGS_CB_ANTI_ALIASING].ob_state,
            OS_SELECTED, nsoption_int(atari_font_monochrom) ? 0 : 1 );


    // TODO: activate this option?
    tmp_option_min_reflow_period = nsoption_int(min_reflow_period);
    snprintf( spare, 255, "%04d", tmp_option_min_reflow_period );
    set_text( SETTINGS_EDIT_MIN_REFLOW_PERIOD, spare,
              INPUT_MIN_REFLOW_PERIOD_MAX_LEN );


    tmp_option_minimum_gif_delay = (float)nsoption_int(minimum_gif_delay) / (float)100;
    snprintf( spare, 255, "%01.1f", tmp_option_minimum_gif_delay );
    set_text( SETTINGS_EDIT_MIN_GIF_DELAY, spare, 3 );

    /* "Network" tab: */
    set_text( SETTINGS_EDIT_PROXY_HOST, nsoption_charp(http_proxy_host),
              INPUT_PROXY_HOST_MAX_LEN );
    snprintf( spare, 255, "%5d", nsoption_int(http_proxy_port) );
    set_text( SETTINGS_EDIT_PROXY_PORT, spare,
              INPUT_PROXY_PORT_MAX_LEN );

    set_text( SETTINGS_EDIT_PROXY_USERNAME, nsoption_charp(http_proxy_auth_user),
              INPUT_PROXY_USERNAME_MAX_LEN );
    set_text( SETTINGS_EDIT_PROXY_PASSWORD, nsoption_charp(http_proxy_auth_pass),
              INPUT_PROXY_PASSWORD_MAX_LEN );
    SET_BIT(dlgtree[SETTINGS_CB_USE_PROXY].ob_state,
            OS_SELECTED, nsoption_bool(http_proxy) ? 1 : 0 );
    SET_BIT(dlgtree[SETTINGS_CB_PROXY_AUTH].ob_state,
            OS_SELECTED, nsoption_int(http_proxy_auth) ? 1 : 0 );

    tmp_option_max_cached_fetch_handles = nsoption_int(max_cached_fetch_handles);
    snprintf( spare, 255, "%2d", nsoption_int(max_cached_fetch_handles) );
    set_text( SETTINGS_EDIT_MAX_CACHED_CONNECTIONS, spare , 2 );

    tmp_option_max_fetchers = nsoption_int(max_fetchers);
    snprintf( spare, 255, "%2d", nsoption_int(max_fetchers) );
    set_text( SETTINGS_EDIT_MAX_FETCHERS, spare , 2 );

    tmp_option_max_fetchers_per_host = nsoption_int(max_fetchers_per_host);
    snprintf( spare, 255, "%2d", nsoption_int(max_fetchers_per_host) );
    set_text( SETTINGS_EDIT_MAX_FETCHERS_PER_HOST, spare , 2 );


    /* "Style" tab: */
    tmp_option_font_min_size = nsoption_int(font_min_size);
    snprintf( spare, 255, "%3d", nsoption_int(font_min_size) );
    set_text( SETTINGS_EDIT_MIN_FONT_SIZE, spare , 3 );

    tmp_option_font_size = nsoption_int(font_size);
    snprintf( spare, 255, "%3d", nsoption_int(font_size) );
    set_text( SETTINGS_EDIT_DEF_FONT_SIZE, spare , 3 );

    toggle_objects();
}

static void handle_filesystem_select_button(short rsc_bt)
{
    bool require_path = false;
    bool is_folder = false;
    short rsc_te = 0;            // The textarea that is bound to the button
    const char * title = "";
    const char * path = NULL;

    // TODO: localize String:
    switch (rsc_bt) {


        case SETTINGS_BT_SEL_DOWNLOAD_DIR:
            title = "Select Download Directory:";
            rsc_te = SETTINGS_EDIT_DOWNLOAD_PATH;
            require_path = true;
        break;

        case SETTINGS_BT_SEL_HOTLIST:
            title = "Select Hotlist File:";
            rsc_te = SETTINGS_EDIT_HOTLIST_FILE;
        break;

        case SETTINGS_BT_SEL_CA_BUNDLE:
            title = "Select CA Bundle File:";
            rsc_te = SETTINGS_EDIT_CA_BUNDLE;
        break;

        case SETTINGS_BT_SEL_CA_CERTS:
            title = "Select Certs Directory:";
            rsc_te = SETTINGS_EDIT_CA_CERTS_PATH;
            require_path = true;
        break;

        case SETTINGS_BT_SEL_EDITOR:
            title = "Select Editor Application:";
            rsc_te = SETTINGS_EDIT_EDITOR;
        break;

        default:
            break;
    };

    assert(rsc_te != 0);

    if (require_path == false) {
        path = file_select(title, "");
        if (path != NULL) {
            gemtk_obj_set_str_safe(dlgtree, rsc_te, path);
        }
    }
    else {
        do {
            /* display file selector: */
            path = file_select(title, "");
            if (path) {
                is_folder = is_dir(path);
            }
            if ((is_folder == false) && (path != NULL)) {
                gemtk_msg_box_show(GEMTK_MSG_BOX_ALERT, "Folder Required!");
            }
        } while ((is_folder == false) && (path != NULL));

        if ((is_folder == true) && (path != NULL)) {
            gemtk_obj_set_str_safe(dlgtree, rsc_te, path);
        }
    }

    OBJ_UNCHECK(rsc_bt);
    OBJ_REDRAW(rsc_bt);
    OBJ_REDRAW(rsc_te);
}

static void form_event(int index, int external)
{
    char spare[255];
    bool is_button = false;
    bool checked = OBJ_SELECTED(index);
    char * tmp;
    MENU pop_menu, me_data;

    short x, y;
    int choice;

    switch(index) {

    case SETTINGS_CB_USE_PROXY:
        if( checked ) {
            ENABLE_OBJ(SETTINGS_EDIT_PROXY_HOST);
            ENABLE_OBJ(SETTINGS_EDIT_PROXY_PORT);
            ENABLE_OBJ(SETTINGS_CB_PROXY_AUTH);
        } else {
            DISABLE_OBJ(SETTINGS_EDIT_PROXY_HOST);
            DISABLE_OBJ(SETTINGS_EDIT_PROXY_PORT);
            DISABLE_OBJ(SETTINGS_CB_PROXY_AUTH);
        }
        FORMEVENT(SETTINGS_CB_PROXY_AUTH);
        OBJ_REDRAW(SETTINGS_CB_USE_PROXY);
        break;

    case SETTINGS_CB_PROXY_AUTH:
        if( checked && OBJ_SELECTED( SETTINGS_CB_USE_PROXY ) ) {
            ENABLE_OBJ(SETTINGS_EDIT_PROXY_USERNAME);
            ENABLE_OBJ(SETTINGS_EDIT_PROXY_PASSWORD);
        } else {
            DISABLE_OBJ(SETTINGS_EDIT_PROXY_USERNAME);
            DISABLE_OBJ(SETTINGS_EDIT_PROXY_PASSWORD);
        }
        break;

    case SETTINGS_CB_ENABLE_ANIMATION:
        if( checked ) {
            ENABLE_OBJ( SETTINGS_EDIT_MIN_GIF_DELAY );
        } else {
            DISABLE_OBJ( SETTINGS_EDIT_MIN_GIF_DELAY );
        }
        break;

    case SETTINGS_BT_SEL_FONT_RENDERER:
        if( external ) {
            objc_offset(dlgtree, SETTINGS_BT_SEL_FONT_RENDERER, &x, &y);
            tmp = gemtk_obj_get_text(dlgtree, SETTINGS_BT_SEL_FONT_RENDERER);
            // point mn_tree tree to font renderer popup:
            pop_menu.mn_tree = gemtk_obj_create_popup_tree(font_engines,
                                    NOF_ELEMENTS(font_engines), tmp, false,
                                    -1, -1);

            assert(pop_menu.mn_tree != NULL);

            pop_menu.mn_menu = 0;
            pop_menu.mn_item = 1;
            pop_menu.mn_scroll = SCROLL_NO;
            pop_menu.mn_keystate = 0;

            /* Show the popup: */
            menu_popup(&pop_menu, x, y, &me_data);
            choice = me_data.mn_item;

             /* Process selection: */
            if (choice > 0 && choice <= (short)NOF_ELEMENTS(font_engines)) {
                get_string(pop_menu.mn_tree, choice, spare);
                set_text(SETTINGS_BT_SEL_FONT_RENDERER,
                         (char*)&spare[2],
                         LABEL_FONT_RENDERER_MAX_LEN);
                OBJ_REDRAW(SETTINGS_BT_SEL_FONT_RENDERER);
            }

            gemtk_obj_destroy_popup_tree(pop_menu.mn_tree);
        }
        tmp = gemtk_obj_get_text(dlgtree, SETTINGS_BT_SEL_FONT_RENDERER);
        if (strcasecmp(tmp, "freetype") == 0) {
            ENABLE_OBJ(SETTINGS_CB_ANTI_ALIASING);
        } else {
            DISABLE_OBJ(SETTINGS_CB_ANTI_ALIASING);
        }
        break;

    case SETTINGS_BT_SEL_LOCALE:
        objc_offset(dlgtree, SETTINGS_BT_SEL_LOCALE, &x, &y);

        if(num_locales < 1 || locales == NULL){
            locales = read_locales();
        }

        if (num_locales < 1 || locales == NULL) {
            // point mn_tree tree to states popup:
            num_locales = 15;
            pop_menu.mn_tree = gemtk_obj_get_tree(POP_LANGUAGE);
            pop_menu.mn_item = POP_LANGUAGE_CS;
        } else {
            // point mn_tree tree to dynamic list:
            tmp = gemtk_obj_get_text(dlgtree, SETTINGS_BT_SEL_LOCALE);
            pop_menu.mn_tree = gemtk_obj_create_popup_tree((const char**)locales,
                                    num_locales, tmp, false,
                                    -1, 100);

            pop_menu.mn_item = 0;
        }

        pop_menu.mn_menu = 0;
        pop_menu.mn_scroll = SCROLL_YES;
        pop_menu.mn_keystate = 0;

        /* display popup: */
        menu_popup(&pop_menu, x, y, &me_data);

        /* Process user selection: */
        choice = me_data.mn_item;
        if( choice > 0 && choice <= num_locales ) {
            get_string(pop_menu.mn_tree, choice, spare);
            set_text(SETTINGS_BT_SEL_LOCALE, (char*)&spare[2], 5);
        }

        gemtk_obj_destroy_popup_tree(pop_menu.mn_tree);

        OBJ_REDRAW(SETTINGS_BT_SEL_LOCALE);
        break;

    case SETTINGS_BT_GUI_TOUT:
        objc_offset(dlgtree, SETTINGS_BT_GUI_TOUT, &x, &y);
        tmp = gemtk_obj_get_text(dlgtree, SETTINGS_BT_GUI_TOUT);
        pop_menu.mn_tree = gemtk_obj_create_popup_tree(gui_timeouts,
                                NOF_ELEMENTS(gui_timeouts), tmp, false, -1,
                                100);

        pop_menu.mn_item = 0;
        pop_menu.mn_menu = 0;
        pop_menu.mn_scroll = SCROLL_NO;
        pop_menu.mn_keystate = 0;

        /* Display popup: */
        menu_popup(&pop_menu, x, y, &me_data);

        /* Process user selection: */
        choice = me_data.mn_item;
        if( choice > 0 && choice <= (int)NOF_ELEMENTS(gui_timeouts)) {
            get_string(pop_menu.mn_tree, choice, spare);
            set_text(SETTINGS_BT_GUI_TOUT, (char*)&spare[2], 5);
        }

        gemtk_obj_destroy_popup_tree(pop_menu.mn_tree);

        OBJ_REDRAW(SETTINGS_BT_GUI_TOUT);
        break;

    case SETTINGS_BT_SEL_DOWNLOAD_DIR:
    case SETTINGS_BT_SEL_HOTLIST:
    case SETTINGS_BT_SEL_CA_BUNDLE:
    case SETTINGS_BT_SEL_CA_CERTS:
    case SETTINGS_BT_SEL_EDITOR:
        handle_filesystem_select_button(index);
        break;

    case SETTINGS_INC_MEM_CACHE:
    case SETTINGS_DEC_MEM_CACHE:
        if( index == SETTINGS_DEC_MEM_CACHE )
            tmp_option_memory_cache_size -= 1;
        else
            tmp_option_memory_cache_size += 1;

        if( tmp_option_memory_cache_size == 0)
            tmp_option_memory_cache_size = 1;
        if( tmp_option_memory_cache_size > 999 )
            tmp_option_memory_cache_size = 999;
        snprintf( spare, 255, "%02d", tmp_option_memory_cache_size );
        set_text( SETTINGS_STR_MAX_MEM_CACHE, spare, 5 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_STR_MAX_MEM_CACHE);
        break;

    case SETTINGS_INC_DISC_CACHE:
    case SETTINGS_DEC_DISC_CACHE:
        if( index == SETTINGS_DEC_DISC_CACHE )
            tmp_option_disc_cache_size -= 1;
        else
            tmp_option_disc_cache_size += 1;

        if( tmp_option_disc_cache_size == 0 )
            tmp_option_disc_cache_size = 1;
        if( tmp_option_disc_cache_size > 9999 )
            tmp_option_disc_cache_size = 9999;
        snprintf( spare, 255, "%02d", tmp_option_disc_cache_size );
        set_text( SETTINGS_STR_MAX_DISC_CACHE, spare, 5 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_STR_MAX_DISC_CACHE);
        break;

    case SETTINGS_INC_CACHE_AGE:
    case SETTINGS_DEC_CACHE_AGE:
        if( index == SETTINGS_INC_CACHE_AGE )
            tmp_option_disc_cache_age += 1;
        else
            tmp_option_disc_cache_age -= 1;

        if( tmp_option_disc_cache_age > 99 )
            tmp_option_disc_cache_age =  0;

        snprintf( spare, 255, "%02d", tmp_option_disc_cache_age );
        set_text( SETTINGS_EDIT_CACHE_AGE, spare, 2 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_CACHE_AGE);
        break;

    case SETTINGS_INC_CACHED_CONNECTIONS:
    case SETTINGS_DEC_CACHED_CONNECTIONS:
        if( index == SETTINGS_INC_CACHED_CONNECTIONS )
            tmp_option_max_cached_fetch_handles += 1;
        else
            tmp_option_max_cached_fetch_handles -= 1;
        if( tmp_option_max_cached_fetch_handles > 31 )
            tmp_option_max_cached_fetch_handles = 31;

        snprintf( spare, 255, "%02d", tmp_option_max_cached_fetch_handles );
        set_text( SETTINGS_EDIT_MAX_CACHED_CONNECTIONS, spare, 2 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_MAX_CACHED_CONNECTIONS);
        break;

    case SETTINGS_INC_MAX_FETCHERS:
    case SETTINGS_DEC_MAX_FETCHERS:
        if( index == SETTINGS_INC_MAX_FETCHERS )
            tmp_option_max_fetchers += 1;
        else
            tmp_option_max_fetchers -= 1;
        if( tmp_option_max_fetchers > 31 )
            tmp_option_max_fetchers = 31;

        snprintf( spare, 255, "%02d", tmp_option_max_fetchers );
        set_text( SETTINGS_EDIT_MAX_FETCHERS, spare, 2 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_MAX_FETCHERS);
        break;

    case SETTINGS_INC_MAX_FETCHERS_PER_HOST:
    case SETTINGS_DEC_MAX_FETCHERS_PER_HOST:
        if( index == SETTINGS_INC_MAX_FETCHERS_PER_HOST )
            tmp_option_max_fetchers_per_host += 1;
        else
            tmp_option_max_fetchers_per_host -= 1;
        if( tmp_option_max_fetchers_per_host > 31 )
            tmp_option_max_fetchers_per_host = 31;

        snprintf( spare, 255, "%02d", tmp_option_max_fetchers_per_host );
        set_text( SETTINGS_EDIT_MAX_FETCHERS_PER_HOST, spare, 2 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_MAX_FETCHERS_PER_HOST);
        break;

    case SETTINGS_INC_HISTORY_AGE:
    case SETTINGS_DEC_HISTORY_AGE:
        if( index == SETTINGS_INC_HISTORY_AGE )
            tmp_option_expire_url += 1;
        else
            tmp_option_expire_url -= 1;

        if( tmp_option_expire_url > 99 )
            tmp_option_expire_url =  0;

        snprintf( spare, 255, "%02d", tmp_option_expire_url );
        set_text( SETTINGS_EDIT_HISTORY_AGE, spare, 2 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_HISTORY_AGE);
        break;

    case SETTINGS_INC_GIF_DELAY:
    case SETTINGS_DEC_GIF_DELAY:
        if( index == SETTINGS_INC_GIF_DELAY )
            tmp_option_minimum_gif_delay += 0.1;
        else
            tmp_option_minimum_gif_delay -= 0.1;

        if( tmp_option_minimum_gif_delay < 0.1 )
            tmp_option_minimum_gif_delay = 0.1;
        if( tmp_option_minimum_gif_delay > 9.0 )
            tmp_option_minimum_gif_delay = 9.0;
        snprintf( spare, 255, "%01.1f", tmp_option_minimum_gif_delay );
        set_text( SETTINGS_EDIT_MIN_GIF_DELAY, spare, 3 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_MIN_GIF_DELAY);
        break;

    case SETTINGS_INC_MIN_FONT_SIZE:
    case SETTINGS_DEC_MIN_FONT_SIZE:
        if( index == SETTINGS_INC_MIN_FONT_SIZE )
            tmp_option_font_min_size += 1;
        else
            tmp_option_font_min_size -= 1;

        if( tmp_option_font_min_size > 500 )
            tmp_option_font_min_size = 500;
        if( tmp_option_font_min_size < 10 )
            tmp_option_font_min_size = 10;

        snprintf( spare, 255, "%03d", tmp_option_font_min_size );
        set_text( SETTINGS_EDIT_MIN_FONT_SIZE, spare, 3 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_MIN_FONT_SIZE);
        break;

    case SETTINGS_INC_DEF_FONT_SIZE:
    case SETTINGS_DEC_DEF_FONT_SIZE:
        if( index == SETTINGS_INC_DEF_FONT_SIZE )
            tmp_option_font_size += 1;
        else
            tmp_option_font_size -= 1;

        if( tmp_option_font_size > 999 )
            tmp_option_font_size = 999;
        if( tmp_option_font_size < 50 )
            tmp_option_font_size = 50;

        snprintf( spare, 255, "%03d", tmp_option_font_size );
        set_text( SETTINGS_EDIT_DEF_FONT_SIZE, spare, 3 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_DEF_FONT_SIZE);
        break;

    case SETTINGS_INC_INCREMENTAL_REFLOW:
    case SETTINGS_DEC_INCREMENTAL_REFLOW:
        if( index == SETTINGS_INC_INCREMENTAL_REFLOW )
            tmp_option_min_reflow_period += 1;
        else
            tmp_option_min_reflow_period -= 1;

        if( tmp_option_min_reflow_period > 9999 )
            tmp_option_min_reflow_period = 10;

        snprintf( spare, 255, "%04d", tmp_option_min_reflow_period );
        set_text( SETTINGS_EDIT_MIN_REFLOW_PERIOD, spare, 4 );
        is_button = true;
        OBJ_REDRAW(SETTINGS_EDIT_MIN_REFLOW_PERIOD);
        break;

    default:
        break;
    }

    if( is_button ) {
        // remove selection indicator from button element:
        OBJ_UNCHECK(index);
        OBJ_REDRAW(index);
    }
}


static void apply_settings(void)
{
    /* "Network" tab: */
    nsoption_set_bool(http_proxy, OBJ_SELECTED(SETTINGS_CB_USE_PROXY));
    if ( OBJ_SELECTED(SETTINGS_CB_PROXY_AUTH) ) {
        nsoption_set_int(http_proxy_auth, OPTION_HTTP_PROXY_AUTH_BASIC);
    } else {
        nsoption_set_int(http_proxy_auth, OPTION_HTTP_PROXY_AUTH_NONE);
    }
    nsoption_set_charp(http_proxy_auth_pass,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_PROXY_PASSWORD));
    nsoption_set_charp(http_proxy_auth_user,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_PROXY_USERNAME));
    nsoption_set_charp(http_proxy_host,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_PROXY_HOST));
    nsoption_set_int(http_proxy_port,
                     atoi( gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_PROXY_PORT) ));
    nsoption_set_int(max_fetchers_per_host,
                     atoi( gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_MAX_FETCHERS_PER_HOST)));
    nsoption_set_int(max_cached_fetch_handles,
                     atoi( gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_MAX_CACHED_CONNECTIONS)));
    nsoption_set_int(max_fetchers,
                     atoi( gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_MAX_FETCHERS) ));
    nsoption_set_bool(foreground_images,
                      OBJ_SELECTED( SETTINGS_CB_FG_IMAGES ));
    nsoption_set_bool(background_images,
                      OBJ_SELECTED( SETTINGS_CB_BG_IMAGES ));

    /* "Style" tab: */
    nsoption_set_int(font_min_size, tmp_option_font_min_size);
    nsoption_set_int(font_size, tmp_option_font_size);

    /* "Rendering" tab: */
    nsoption_set_charp(atari_font_driver,
                       gemtk_obj_get_text(dlgtree, SETTINGS_BT_SEL_FONT_RENDERER));
    nsoption_set_int(atari_transparency,
                      OBJ_SELECTED(SETTINGS_CB_TRANSPARENCY));
    nsoption_set_bool(animate_images,
                      OBJ_SELECTED(SETTINGS_CB_ENABLE_ANIMATION));
    nsoption_set_int(minimum_gif_delay,
                     (int)(tmp_option_minimum_gif_delay*100+0.5));
    /*	nsoption_set_bool(incremental_reflow,
    			  OBJ_SELECTED(SETTINGS_CB_INCREMENTAL_REFLOW));*/
    nsoption_set_int(min_reflow_period, tmp_option_min_reflow_period);
    nsoption_set_int(atari_font_monochrom,
                     !OBJ_SELECTED( SETTINGS_CB_ANTI_ALIASING ));

    /* "Paths" tabs: */
    nsoption_set_charp(ca_bundle,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_CA_BUNDLE));
    nsoption_set_charp(ca_path,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_CA_CERTS_PATH));
    nsoption_set_charp(homepage_url,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_CA_CERTS_PATH));
    nsoption_set_charp(hotlist_file,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_HOTLIST_FILE));
    nsoption_set_charp(atari_editor,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_EDITOR));
    nsoption_set_charp(downloads_path,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_DOWNLOAD_PATH));

    /* "Cache" tab: */
    tmp_option_memory_cache_size = atoi(gemtk_obj_get_text(dlgtree,
                                                    SETTINGS_STR_MAX_MEM_CACHE));
    tmp_option_expire_url = atoi(gemtk_obj_get_text(dlgtree,
                                                    SETTINGS_EDIT_HISTORY_AGE));
    tmp_option_disc_cache_size = atoi(gemtk_obj_get_text(dlgtree,
                                                    SETTINGS_STR_MAX_DISC_CACHE));
    tmp_option_disc_cache_age = atoi(gemtk_obj_get_text(dlgtree,
                                                    SETTINGS_EDIT_CACHE_AGE));
    nsoption_set_int(memory_cache_size,
                     tmp_option_memory_cache_size * (1024*1024));

    nsoption_set_int(expire_url, tmp_option_expire_url);

    nsoption_set_int(disc_cache_size, tmp_option_disc_cache_size * (1024*1024));

    nsoption_set_int(disc_cache_age, tmp_option_disc_cache_age);


    /* "Browser" tab: */
    nsoption_set_bool(target_blank,
                      !OBJ_SELECTED(SETTINGS_CB_DISABLE_POPUP_WINDOWS));
    nsoption_set_bool(block_advertisements,
                      OBJ_SELECTED(SETTINGS_CB_HIDE_ADVERTISEMENT));
    nsoption_set_charp(accept_language,
                       gemtk_obj_get_text(dlgtree, SETTINGS_BT_SEL_LOCALE));
    nsoption_set_int(atari_gui_poll_timeout,
                     atoi(gemtk_obj_get_text(dlgtree, SETTINGS_BT_GUI_TOUT)));
    nsoption_set_bool(send_referer,
                      OBJ_SELECTED(SETTINGS_CB_SEND_HTTP_REFERRER));
    nsoption_set_bool(do_not_track,
                      OBJ_SELECTED(SETTINGS_CB_SEND_DO_NOT_TRACK));
    nsoption_set_charp(homepage_url,
                       gemtk_obj_get_text(dlgtree, SETTINGS_EDIT_HOMEPAGE));
}

static short on_aes_event(GUIWIN *win, EVMULT_OUT *ev_out, short msg[8])
{
    short retval = 0;

    if ((ev_out->emo_events & MU_MESAG) != 0) {
        // handle message
        // printf("settings win msg: %d\n", msg[0]);
        switch (msg[0]) {


        case WM_CLOSED:
            // TODO: this needs to iterate through all gui windows and
            // check if the rootwin is this window...
            close_settings();
            break;

        case WM_SIZED:
            gemtk_wm_update_slider(win, GEMTK_WM_VH_SLIDER);
            break;

        case WM_MOVED:
            break;

        case WM_TOOLBAR:
            switch(msg[4]) {
                case TOOLBAR_SETTINGS_SAVE:
                    save_settings();
                break;

                case TOOLBAR_SETTINGS_ABORT:
                    close_settings();
                    break;
            default:
                break;
            }
            break;

        case GEMTK_WM_WM_FORM_CLICK:
            form_event(msg[4], 1);
            break;

        default:
            break;
        }
    }

    if ((ev_out->emo_events & MU_KEYBD) != 0) {
    }

    if ((ev_out->emo_events & MU_BUTTON) != 0) {
    }

    return(retval);
}

void open_settings(void)
{
    if (h_aes_win == 0) {

        GRECT curr, area;
        OBJECT * toolbartree;
        struct gemtk_wm_scroll_info_s *slid;
        uint32_t kind = CLOSER | NAME | MOVER | VSLIDE | HSLIDE | UPARROW
                        | DNARROW | LFARROW | RTARROW | SIZER;

        dlgtree = gemtk_obj_get_tree(SETTINGS);
        toolbartree = gemtk_obj_get_tree(TOOLBAR_SETTINGS);
        area.g_x = area.g_y = 0;
        area.g_w = MIN(dlgtree->ob_width, desk_area.g_w);
        area.g_h = MIN(dlgtree->ob_height, desk_area.g_h);
        wind_calc_grect(WC_BORDER, kind, &area, &area);
        h_aes_win = wind_create_grect(kind, &area);
        wind_set_str(h_aes_win, WF_NAME, "Settings");
        settings_guiwin = gemtk_wm_add(h_aes_win, GEMTK_WM_FLAG_DEFAULTS,
                                     on_aes_event);
        curr.g_w = MIN(dlgtree->ob_width, desk_area.g_w);
        curr.g_h = MIN(dlgtree->ob_height, desk_area.g_h-64);
        curr.g_x = 1;
        curr.g_y = (desk_area.g_h / 2) - (curr.g_h / 2);

        wind_calc_grect(WC_BORDER, kind, &curr, &curr);

        dlgtree->ob_x = curr.g_x;
        dlgtree->ob_y = curr.g_y;

        /* set current config values: */
        display_settings();

        wind_open_grect(h_aes_win, &curr);

        gemtk_wm_set_toolbar(settings_guiwin, toolbartree, 0, 0);
        gemtk_wm_set_form(settings_guiwin, dlgtree, 0);
        gemtk_wm_set_scroll_grid(settings_guiwin, 32, 32);
        gemtk_wm_get_grect(settings_guiwin, GEMTK_WM_AREA_CONTENT, &area);

        slid = gemtk_wm_get_scroll_info(settings_guiwin);
        gemtk_wm_set_content_units(settings_guiwin,
                                 (dlgtree->ob_width/slid->x_unit_px),
                                 (dlgtree->ob_height/slid->y_unit_px));
        gemtk_wm_update_slider(settings_guiwin, GEMTK_WM_VH_SLIDER);
    }
}

void close_settings(void)
{
    NSLOG(netsurf, INFO, "closing");
    gemtk_wm_remove(settings_guiwin);
    settings_guiwin = NULL;
    wind_close(h_aes_win);
    wind_delete(h_aes_win);
    h_aes_win = 0;
    NSLOG(netsurf, INFO, "Done");
}