-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
uicmd.cc
1611 lines (1405 loc) · 39.8 KB
/
uicmd.cc
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
1000
/*
* File: uicmd.cc
*
* Copyright (C) 2005-2011 Jorge Arellano Cid <[email protected]>
* Copyright (C) 2024 Rodrigo Arias Mallo <[email protected]>
*
* This program 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; either version 3 of the License, or
* (at your option) any later version.
*/
/**
* @file
* Functions/Methods for commands triggered from the UI
*/
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> /* for qsort */
#include <math.h> /* for rint */
#include <limits.h> /* for UINT_MAX */
#include <sys/stat.h>
#include <FL/Fl.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Wizard.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Pack.H>
#include <FL/Fl_Scroll.H>
#include <FL/names.h>
#include "paths.hh"
#include "keys.hh"
#include "ui.hh"
#include "uicmd.hh"
#include "timeout.hh"
#include "utf8.hh"
#include "menu.hh"
#include "dialog.hh"
#include "xembed.hh"
#include "bookmark.h"
#include "history.h"
#include "msg.h"
#include "prefs.h"
#include "misc.h"
#include "dlib/dlib.h"
#include "dw/fltkviewport.hh"
#include "nav.h"
//#define DEFAULT_TAB_LABEL "-.untitled.-"
#define DEFAULT_TAB_LABEL "-.new.-"
// Handy macro
#define BW2UI(bw) ((UI*)((bw)->ui))
// Platform idependent part
using namespace dw::core;
// FLTK related
using namespace dw::fltk;
/*
* Local data
*/
static const char *save_dir = "";
/*
* Forward declarations
*/
static BrowserWindow *UIcmd_tab_new(CustTabs *tabs, UI *old_ui, int focus);
static void close_tab_btn_cb (Fl_Widget *w, void *cb_data);
static char *UIcmd_make_search_str(const char *str);
static void UIcmd_set_window_labels(Fl_Window *win, const char *str);
//----------------------------------------------------------------------------
/*
* CustTabs ---------------------------------------------------------------
*/
/**
* stores the respective UI pointer
*/
class CustTabButton : public Fl_Button {
UI *ui_;
uint_t focus_num_; // Used to choose which tab to focus when closing the
// active one (the highest numbered gets focus).
public:
CustTabButton (int x,int y,int w,int h, const char* label = 0) :
Fl_Button (x,y,w,h,label) { ui_ = NULL; focus_num_ = 0; };
void ui(UI *pui) { ui_ = pui; }
UI *ui(void) { return ui_; }
void focus_num(uint_t fn) { focus_num_ = fn; }
uint_t focus_num(void) { return focus_num_; }
};
static int btn_cmp(const void *p1, const void *p2)
{
return ((*(CustTabButton * const *)p1)->focus_num() -
(*(CustTabButton * const *)p2)->focus_num() );
}
/**
* Allows fine control of the tabbed interface
*/
class CustTabs : public Fl_Group {
uint_t focus_counter; // An increasing counter
int tab_w, tab_h, ctab_h, btn_w, ctl_w;
Fl_Wizard *Wizard;
Fl_Scroll *Scroll;
Fl_Pack *Pack;
Fl_Group *Control;
CustButton *CloseBtn;
void update_pack_offset(void);
void resize(int x, int y, int w, int h)
{ Fl_Group::resize(x,y,w,h); update_pack_offset(); }
int get_btn_idx(UI *ui);
void increase_focus_counter() {
if (focus_counter < UINT_MAX) /* check for overflow */
++focus_counter;
else {
/* wrap & normalize old numbers here */
CustTabButton **btns = dNew(CustTabButton*, num_tabs());
for (int i = 0; i < num_tabs(); ++i)
btns[i] = (CustTabButton*)Pack->child(i);
qsort(btns, num_tabs(), sizeof(CustTabButton *), btn_cmp);
focus_counter = 0;
for (int i = 0; i < num_tabs(); ++i)
btns[i]->focus_num(focus_counter++);
dFree(btns);
}
}
public:
CustTabs (int ww, int wh, int th, const char *lbl=0) :
Fl_Group(0,0,ww,th,lbl) {
Pack = NULL;
focus_counter = 0;
tab_w = 50, tab_h = th, ctab_h = 1, btn_w = 20, ctl_w = 1*btn_w+2;
resize(0,0,ww,ctab_h);
/* tab buttons go inside a pack within a scroll */
Scroll = new Fl_Scroll(0,0,ww-ctl_w,ctab_h);
Scroll->type(0); /* no scrollbars */
Scroll->box(FL_NO_BOX);
Pack = new Fl_Pack(0,0,ww-ctl_w,tab_h);
Pack->type(Fl_Pack::HORIZONTAL);
Pack->box(FL_NO_BOX); //FL_THIN_DOWN_FRAME
Pack->end();
Scroll->end();
resizable(Scroll);
/* control buttons go inside a group */
Control = new Fl_Group(ww-ctl_w,0,ctl_w,ctab_h);
CloseBtn = new CustButton(ww-ctl_w+2,0,btn_w,ctab_h, "X");
CloseBtn->box(FL_THIN_UP_BOX);
CloseBtn->clear_visible_focus();
CloseBtn->set_tooltip(prefs.right_click_closes_tab ?
"Close current tab.\nor Right-click tab label to close." :
"Close current tab.\nor Middle-click tab label to close.");
CloseBtn->callback(close_tab_btn_cb, this);
CloseBtn->hide();
Control->end();
box(FL_FLAT_BOX);
end();
Wizard = new Fl_Wizard(0,ctab_h,ww,wh-ctab_h);
Wizard->box(FL_NO_BOX);
Wizard->end();
};
int handle(int e);
UI *add_new_tab(UI *old_ui, int focus);
void remove_tab(UI *ui);
Fl_Wizard *wizard(void) { return Wizard; }
int num_tabs() { return (Pack ? Pack->children() : 0); }
void switch_tab(CustTabButton *cbtn);
void prev_tab(void);
void next_tab(void);
void set_tab_label(UI *ui, const char *title);
};
/**
* Callback for mouse click
*/
static void tab_btn_cb (Fl_Widget *w, void *cb_data)
{
CustTabButton *btn = (CustTabButton*) w;
CustTabs *tabs = (CustTabs*) cb_data;
int b = Fl::event_button();
if (b == FL_LEFT_MOUSE) {
tabs->switch_tab(btn);
} else if ((b == FL_RIGHT_MOUSE && prefs.right_click_closes_tab) ||
(b == FL_MIDDLE_MOUSE && !prefs.right_click_closes_tab)) {
// TODO: just an example, not necessarily final
a_UIcmd_close_bw(a_UIcmd_get_bw_by_widget(btn->ui()));
}
}
/**
* Callback for the close-tab button
*/
static void close_tab_btn_cb (Fl_Widget *, void *cb_data)
{
CustTabs *tabs = (CustTabs*) cb_data;
int b = Fl::event_button();
if (b == FL_LEFT_MOUSE) {
a_UIcmd_close_bw(a_UIcmd_get_bw_by_widget(tabs->wizard()->value()));
}
}
int CustTabs::handle(int e)
{
int ret = 0;
_MSG("CustTabs::handle e=%s\n", fl_eventnames[e]);
if (e == FL_KEYBOARD) {
return 0; // Receive as shortcut
} else if (e == FL_SHORTCUT) {
UI *ui = (UI*)wizard()->value();
BrowserWindow *bw = a_UIcmd_get_bw_by_widget(ui);
KeysCommand_t cmd = Keys::getKeyCmd();
if (cmd == KEYS_NOP) {
// Do nothing
_MSG("CustTabs::handle KEYS_NOP\n");
} else if (cmd == KEYS_NEW_TAB) {
a_UIcmd_open_url_nt(bw, NULL, 1);
ret = 1;
} else if (cmd == KEYS_CLOSE_TAB) {
a_UIcmd_close_bw(bw);
ret = 1;
} else if (cmd == KEYS_LEFT_TAB) {
prev_tab();
ret = 1;
} else if (cmd == KEYS_RIGHT_TAB) {
next_tab();
ret = 1;
} else if (cmd == KEYS_NEW_WINDOW) {
a_UIcmd_open_url_nw(bw, NULL);
ret = 1;
} else if (cmd == KEYS_CLOSE_ALL) {
a_Timeout_add(0.0, a_UIcmd_close_all_bw, NULL);
ret = 1;
}
} else if (e == FL_MOUSEWHEEL && prefs.scroll_switches_tabs) {
/* Move to the next or previous tab using the mouse wheel */
int dx = Fl::event_dx();
int dy = Fl::event_dy();
int dir = dy ? dy : dx;
if (prefs.scroll_switches_tabs_reverse)
dir = -dir;
if (dir > 0)
next_tab();
else if (dir < 0)
prev_tab();
ret = 1;
}
return (ret) ? ret : Fl_Group::handle(e);
}
/**
* Create a new tab with its own UI
*/
UI *CustTabs::add_new_tab(UI *old_ui, int focus)
{
if (num_tabs() == 1) {
// Show tabbar
ctab_h = tab_h;
Wizard->resize(0,ctab_h,Wizard->w(),window()->h()-ctab_h);
resize(0,0,window()->w(),ctab_h); // tabbar
CloseBtn->show();
{int w = 0, h; Pack->child(0)->measure_label(w, h);
Pack->child(0)->size(w+14,ctab_h);}
Pack->child(0)->show(); // first tab button
window()->init_sizes();
}
/* The UI is constructed in a comfortable fitting size, and then resized
* so FLTK doesn't get confused later with even smaller dimensions! */
current(0);
UI *new_ui = new UI(0,0,UI_MIN_W,UI_MIN_H,"Dillo:",old_ui);
new_ui->resize(0,ctab_h,Wizard->w(),Wizard->h());
new_ui->tabs(this);
Wizard->add(new_ui);
new_ui->show();
CustTabButton *btn = new CustTabButton(num_tabs()*tab_w,0,tab_w,ctab_h);
btn->align(FL_ALIGN_INSIDE);
btn->labelsize(btn->labelsize()-2);
btn->copy_label(DEFAULT_TAB_LABEL);
btn->clear_visible_focus();
btn->box(FL_GTK_THIN_UP_BOX);
btn->color(focus ? PREFS_UI_TAB_ACTIVE_BG_COLOR : PREFS_UI_TAB_BG_COLOR);
btn->labelcolor(focus ? PREFS_UI_TAB_ACTIVE_FG_COLOR:PREFS_UI_TAB_FG_COLOR);
btn->ui(new_ui);
btn->callback(tab_btn_cb, this);
Pack->add(btn); // append
if (focus) {
switch_tab(btn);
} else { // no focus
// set focus counter
increase_focus_counter();
btn->focus_num(focus_counter);
if (num_tabs() == 2) {
// tabbar added: redraw current page
Wizard->redraw();
}
}
if (num_tabs() == 1)
btn->hide();
update_pack_offset();
return new_ui;
}
/**
* Remove tab by UI
*/
void CustTabs::remove_tab(UI *ui)
{
CustTabButton *btn;
// get active tab idx
int act_idx = get_btn_idx((UI*)Wizard->value());
// get to-be-removed tab idx
int rm_idx = get_btn_idx(ui);
btn = (CustTabButton*)Pack->child(rm_idx);
if (act_idx == rm_idx) {
// Active tab is being closed, switch to the "previous" one
CustTabButton *fbtn = NULL;
for (int i = 0; i < num_tabs(); ++i) {
if (i != rm_idx) {
if (!fbtn)
fbtn = (CustTabButton*)Pack->child(i);
CustTabButton *btn = (CustTabButton*)Pack->child(i);
if (btn->focus_num() > fbtn->focus_num())
fbtn = btn;
}
}
switch_tab(fbtn);
}
Pack->remove(rm_idx);
update_pack_offset();
delete btn;
Wizard->remove(ui);
delete(ui);
if (num_tabs() == 1) {
// hide tabbar
ctab_h = 1;
CloseBtn->hide();
Pack->child(0)->size(0,0);
Pack->child(0)->hide(); // first tab button
resize(0,0,window()->w(),ctab_h); // tabbar
Wizard->resize(0,ctab_h,Wizard->w(),window()->h()-ctab_h);
window()->init_sizes();
window()->redraw();
}
}
int CustTabs::get_btn_idx(UI *ui)
{
for (int i = 0; i < num_tabs(); ++i) {
CustTabButton *btn = (CustTabButton*)Pack->child(i);
if (btn->ui() == ui)
return i;
}
return -1;
}
/**
* Keep active tab visible.
* (Pack children have unusable x() coordinate)
*/
void CustTabs::update_pack_offset()
{
dReturn_if (num_tabs() == 0);
// get active tab button
int act_idx = get_btn_idx((UI*)Wizard->value());
CustTabButton *cbtn = (CustTabButton*)Pack->child(act_idx);
// calculate tab button's x() coordinates
int x_i = 0, x_f;
for (int j=0; j < act_idx; ++j)
x_i += Pack->child(j)->w();
x_f = x_i + cbtn->w();
int scr_x = Scroll->xposition(), scr_y = Scroll->yposition();
int px_i = x_i - scr_x;
int px_f = px_i + cbtn->w();
int pw = Scroll->window()->w() - ctl_w;
_MSG(" scr_x=%d btn_x=%d px_i=%d btn_w=%d px_f=%d pw=%d",
Scroll->xposition(),cbtn->x(),px_i,cbtn->w(),px_f,pw);
if (px_i < 0) {
Scroll->scroll_to(x_i, scr_y);
} else if (px_i > pw || (px_i > 0 && px_f > pw)) {
Scroll->scroll_to(MIN(x_i, x_f-pw), scr_y);
}
Scroll->redraw();
_MSG(" >>scr_x=%d btn0_x=%d\n", scr_x, Pack->child(0)->x());
}
/**
* Make cbtn's tab the active one.
*/
void CustTabs::switch_tab(CustTabButton *cbtn)
{
int idx;
CustTabButton *btn;
BrowserWindow *bw;
UI *old_ui = (UI*)Wizard->value();
if (cbtn && cbtn->ui() != old_ui) {
// Set old tab label to normal color
if ((idx = get_btn_idx(old_ui)) != -1) {
btn = (CustTabButton*)Pack->child(idx);
btn->color(PREFS_UI_TAB_BG_COLOR);
btn->labelcolor(PREFS_UI_TAB_FG_COLOR);
btn->redraw();
}
/* We make a point of calling show() before value() is changed because
* the wizard may hide the old one before showing the new one. In that
* case, the new UI gets focus with Fl::e_keysym set to whatever
* triggered the switch, and this is a problem when it's Tab/Left/Right/
* Up/Down because some widgets (notably Fl_Group and Fl_Input) exhibit
* unwelcome behaviour in that case. If the new widgets are already
* shown, fl_fix_focus will fix everything with Fl::e_keysym temporarily
* cleared.
*/
cbtn->ui()->show();
Wizard->value(cbtn->ui());
cbtn->color(PREFS_UI_TAB_ACTIVE_BG_COLOR);
cbtn->labelcolor(PREFS_UI_TAB_ACTIVE_FG_COLOR);
cbtn->redraw();
update_pack_offset();
// Update window title
if ((bw = a_UIcmd_get_bw_by_widget(cbtn->ui()))) {
const char *title = (cbtn->ui())->label();
UIcmd_set_window_labels(cbtn->window(), title ? title : "");
}
// Update focus priority
increase_focus_counter();
cbtn->focus_num(focus_counter);
}
}
void CustTabs::prev_tab()
{
int idx;
if ((idx = get_btn_idx((UI*)Wizard->value())) != -1)
switch_tab((CustTabButton*)Pack->child(idx>0 ? idx-1 : num_tabs()-1));
}
void CustTabs::next_tab()
{
int idx;
if ((idx = get_btn_idx((UI*)Wizard->value())) != -1)
switch_tab((CustTabButton*)Pack->child((idx+1<num_tabs()) ? idx+1 : 0));
}
/**
* Set this UI's tab button label.
*/
void CustTabs::set_tab_label(UI *ui, const char *label)
{
char title[128];
int idx = get_btn_idx(ui);
if (idx != -1) {
// Make a label for this tab
size_t tab_chars = 15, label_len = strlen(label);
if (label_len > tab_chars)
tab_chars = a_Utf8_end_of_char(label, tab_chars - 1) + 1;
snprintf(title, tab_chars + 1, "%s", label);
if (label_len > tab_chars)
snprintf(title + tab_chars, 4, "...");
// Avoid unnecessary redraws
if (strcmp(Pack->child(idx)->label(), title)) {
int w = 0, h;
Pack->child(idx)->copy_label(title);
Pack->child(idx)->measure_label(w, h);
Pack->child(idx)->size(w+14,ctab_h);
update_pack_offset();
}
}
}
//----------------------------------------------------------------------------
static void win_cb (Fl_Widget *w, void *cb_data) {
CustTabs *tabs = (CustTabs*) cb_data;
int choice = 1, ntabs = tabs->num_tabs();
if (Fl::event_key() == FL_Escape) {
// Don't let FLTK close a browser window due to unhandled Escape
// (most likely with modifiers).
return;
}
if (prefs.show_quit_dialog && ntabs > 1)
choice = a_Dialog_choice("Dillo: Close window?",
"Window contains more than one tab.",
"Close", "Cancel", NULL);
if (choice == 1)
while (ntabs-- > 0)
a_UIcmd_close_bw(a_UIcmd_get_bw_by_widget(tabs->wizard()->value()));
}
/*
* Given a UI or UI child widget, return its bw.
*/
BrowserWindow *a_UIcmd_get_bw_by_widget(void *v_wid)
{
BrowserWindow *bw;
for (int i = 0; i < a_Bw_num(); ++i) {
bw = a_Bw_get(i);
if (((UI*)bw->ui)->contains((Fl_Widget*)v_wid)) {
return bw;
}
}
return NULL;
}
/*
* Create a new UI and its associated BrowserWindow data structure.
* Use style from v_ui. If non-NULL it must be of type UI*.
*/
BrowserWindow *a_UIcmd_browser_window_new(int ww, int wh,
uint32_t xid, const void *vbw)
{
BrowserWindow *old_bw = (BrowserWindow*)vbw;
BrowserWindow *new_bw = NULL;
UI *old_ui = old_bw ? BW2UI(old_bw) : NULL;
Fl_Window *win;
if (ww <= 0 || wh <= 0) {
// Set default geometry from dillorc.
ww = prefs.width;
wh = prefs.height;
}
if (xid)
win = new Xembed(xid, ww, wh);
else if (prefs.buffered_drawing != 2)
win = new Fl_Window(ww, wh);
else
win = new Fl_Double_Window(ww, wh);
win->box(FL_NO_BOX);
CustTabs *DilloTabs = new CustTabs(ww, wh, prefs.ui_tab_height);
win->end();
win->resizable(DilloTabs->wizard());
int focus = 1;
new_bw = UIcmd_tab_new(DilloTabs, old_ui, focus);
win->show();
if (old_bw == NULL) {
new_bw->zoom = prefs.zoom_factor;
if (prefs.xpos >= 0 && prefs.ypos >= 0) {
// position the first window according to preferences
DilloTabs->window()->position(prefs.xpos, prefs.ypos);
}
}
win->callback(win_cb, DilloTabs);
return new_bw;
}
/*
* Set the window name and icon name.
*/
static void UIcmd_set_window_labels(Fl_Window *win, const char *str)
{
const char *copy;
win->Fl_Widget::copy_label(str);
copy = win->label();
win->label(copy, copy);
}
/*
* Create a new Tab button, UI and its associated BrowserWindow data
* structure.
*/
static BrowserWindow *UIcmd_tab_new(CustTabs *tabs, UI *old_ui, int focus)
{
_MSG(" UIcmd_tab_new\n");
// Create and set the UI
UI *new_ui = tabs->add_new_tab(old_ui, focus);
// Now create the Dw render layout and viewport
FltkPlatform *platform = new FltkPlatform ();
Layout *layout = new Layout (platform);
style::Color *bgColor = style::Color::create (layout, prefs.bg_color);
layout->setBgColor (bgColor);
layout->setBgImage (NULL, style::BACKGROUND_REPEAT,
style::BACKGROUND_ATTACHMENT_SCROLL,
style::createPerLength (0), style::createPerLength (0));
// set_render_layout() sets the proper viewport size
FltkViewport *viewport = new FltkViewport (0, 0, 0, 1);
viewport->box(FL_NO_BOX);
viewport->setBufferedDrawing (prefs.buffered_drawing ? true : false);
viewport->setDragScroll (prefs.middle_click_drags_page ? true : false);
viewport->setScrollbarOnLeft (prefs.scrollbar_on_left ? true : false);
viewport->setScrollbarPageMode (prefs.scrollbar_page_mode ? true : false);
layout->attachView (viewport);
new_ui->set_render_layout(viewport);
viewport->setScrollStep(prefs.scroll_step);
viewport->setPageOverlap(prefs.scroll_page_overlap);
// Now, create a new browser window structure
BrowserWindow *new_bw = a_Bw_new();
// Reference the UI from the bw
new_bw->ui = (void *)new_ui;
// Copy the layout pointer into the bw data
new_bw->render_layout = (void*)layout;
// Clear the window title
if (focus)
UIcmd_set_window_labels(new_ui->window(), new_ui->label());
// WORKAROUND: see findbar_toggle()
new_ui->findbar_toggle(0);
return new_bw;
}
/*
* Close one browser window
*/
void a_UIcmd_close_bw(void *vbw)
{
BrowserWindow *bw = (BrowserWindow *)vbw;
UI *ui = BW2UI(bw);
CustTabs *tabs = ui->tabs();
Layout *layout = (Layout*)bw->render_layout;
_MSG("a_UIcmd_close_bw\n");
a_Bw_stop_clients(bw, BW_Root + BW_Img + BW_Force);
delete(layout);
if (tabs) {
tabs->remove_tab(ui);
if (tabs->num_tabs() == 0)
delete tabs->window();
}
a_Bw_free(bw);
}
/*
* Close all the browser windows
*/
void a_UIcmd_close_all_bw(void *)
{
BrowserWindow *bw;
int choice = 1;
if (prefs.show_quit_dialog && a_Bw_num() > 1)
choice = a_Dialog_choice("Dillo: Quit?",
"More than one open tab or window.",
"Quit", "Cancel", NULL);
if (choice == 1)
while ((bw = a_Bw_get(0)))
a_UIcmd_close_bw((void*)bw);
}
/*
* Return a search string of the suffix if str starts with a
* prefix of a search engine name and a blank
*/
static char *UIcmd_find_search_str(const char *str)
{
int p;
char *url = NULL;
int len = strcspn(str, " ");
if (len > 0 && str[len] != '\0') {
/* we found a ' ' in str, check whether the first part of str
* is a prefix of a search_url label
*/
for (p = 0; p < dList_length(prefs.search_urls); p++) {
const char *search =
(const char *)dList_nth_data(prefs.search_urls, p);
if (search && dStrnAsciiCasecmp(str, search, len) == 0) {
prefs.search_url_idx = p;
url = UIcmd_make_search_str(str + len + 1);
break;
}
}
}
return url;
}
/*
* Open a new URL in the given browser window.
*
* our custom "file:" URIs are normalized here too.
*/
void a_UIcmd_open_urlstr(void *vbw, const char *urlstr)
{
char *new_urlstr;
char *search_urlstr = NULL;
DilloUrl *url;
int ch;
BrowserWindow *bw = (BrowserWindow*)vbw;
if ((search_urlstr = UIcmd_find_search_str(urlstr))) {
urlstr = search_urlstr;
}
if (urlstr && *urlstr) {
/* Filter URL string */
new_urlstr = a_Url_string_strip_delimiters(urlstr);
if (!dStrnAsciiCasecmp(new_urlstr, "file:", 5)) {
/* file URI */
ch = new_urlstr[5];
if (!ch || ch == '.') {
url = a_Url_new(Paths::getOldWorkingDir(), "file:");
} else {
url = a_Url_new(new_urlstr, "file:");
}
} else {
/* common case */
url = a_Url_new(new_urlstr, NULL);
}
dFree(new_urlstr);
if (url) {
a_UIcmd_open_url(bw, url);
a_Url_free(url);
}
}
dFree(search_urlstr);
}
/*
* Open a new URL in the given browser window
*/
void a_UIcmd_open_url(BrowserWindow *bw, const DilloUrl *url)
{
if (url) {
a_Nav_push(bw, url, NULL);
BW2UI(bw)->focus_main();
} else {
// Used to start a bw with a blank screen
BW2UI(bw)->focus_location();
a_UIcmd_set_buttons_sens(bw);
}
}
static void UIcmd_open_url_nbw(BrowserWindow *new_bw, const DilloUrl *url)
{
if (!url && prefs.new_tab_page) {
if (strcmp(URL_STR(prefs.new_tab_page), "about:blank") != 0)
url = prefs.new_tab_page;
}
/* When opening a new BrowserWindow (tab or real window) we focus
* Location if we don't yet have an URL, main otherwise.
*/
if (url) {
a_Nav_push(new_bw, url, NULL);
a_UIcmd_set_location_text(new_bw, URL_STR(url));
BW2UI(new_bw)->focus_main();
} else {
BW2UI(new_bw)->focus_location();
a_UIcmd_set_buttons_sens(new_bw);
}
}
/*
* Open a new URL in a new browser window
*/
void a_UIcmd_open_url_nw(BrowserWindow *bw, const DilloUrl *url)
{
int w, h;
BrowserWindow *new_bw;
a_UIcmd_get_wh(bw, &w, &h);
new_bw = a_UIcmd_browser_window_new(w, h, 0, bw);
/* Preserve same zoom factor in new window */
new_bw->zoom = bw->zoom;
UIcmd_open_url_nbw(new_bw, url);
}
/*
* Open a new URL in a new tab in the same browser window
*/
void a_UIcmd_open_url_nt(void *vbw, const DilloUrl *url, int focus)
{
BrowserWindow *bw = (BrowserWindow *)vbw;
BrowserWindow *new_bw = UIcmd_tab_new(BW2UI(bw)->tabs(),
bw ? BW2UI(bw) : NULL, focus);
/* Preserve same zoom factor in new tab */
new_bw->zoom = bw->zoom;
UIcmd_open_url_nbw(new_bw, url);
}
/*
* Send the browser back to previous page
*/
void a_UIcmd_back(void *vbw)
{
a_Nav_back((BrowserWindow*)vbw);
}
/*
* Popup the navigation menu of the Back button
*/
void a_UIcmd_back_popup(void *vbw, int x, int y)
{
a_Menu_history_popup((BrowserWindow*)vbw, x, y, -1);
}
/*
* Send the browser to next page in the history list
*/
void a_UIcmd_forw(void *vbw)
{
a_Nav_forw((BrowserWindow*)vbw);
}
/*
* Popup the navigation menu of the Forward button
*/
void a_UIcmd_forw_popup(void *vbw, int x, int y)
{
a_Menu_history_popup((BrowserWindow*)vbw, x, y, 1);
}
/*
* Send the browser to home URL
*/
void a_UIcmd_home(void *vbw)
{
a_UIcmd_open_url((BrowserWindow*)vbw, prefs.home);
}
/*
* Reload current URL
*/
void a_UIcmd_reload(void *vbw)
{
a_Nav_reload((BrowserWindow*)vbw);
}
/*
* Repush current URL
*/
void a_UIcmd_repush(void *vbw)
{
a_Nav_repush((BrowserWindow*)vbw);
}
/*
* Zero-delay URL redirection.
*/
void a_UIcmd_redirection0(void *vbw, const DilloUrl *url)
{
a_Nav_redirection0((BrowserWindow*)vbw, url);
}
/*
* Zoom in
*/
void a_UIcmd_zoom_in(void *vbw)
{
BrowserWindow *bw = (BrowserWindow*) vbw;
bw->zoom += 0.10;
if (bw->zoom > 10.0)
bw->zoom = 10.0;
a_UIcmd_set_msg(bw, "Zoom set to %.0f%%", bw->zoom * 100.0);
a_Nav_repush((BrowserWindow*)vbw);
}
/*
* Zoom out
*/
void a_UIcmd_zoom_out(void *vbw)
{
BrowserWindow *bw = (BrowserWindow*) vbw;
bw->zoom -= 0.10;
if (bw->zoom < 0.10)
bw->zoom = 0.10;
a_UIcmd_set_msg(bw, "Zoom set to %.0f%%", bw->zoom * 100.0);
a_Nav_repush((BrowserWindow*)vbw);
}
/*
* Zoom reset
*/
void a_UIcmd_zoom_reset(void *vbw)
{
BrowserWindow *bw = (BrowserWindow*) vbw;
bw->zoom = 1.0;
a_UIcmd_set_msg(bw, "Zoom set to %.0f%%", bw->zoom * 100.0);
a_Nav_repush((BrowserWindow*)vbw);
}
/*
* Return a suitable filename for a given URL path.
*/
static char *UIcmd_make_save_filename(const DilloUrl *url)
{
size_t MaxLen = 64;
const char *dir = save_dir, *path, *path2, *query;
char *name, *free1, *free2, *n1, *n2;
free1 = free2 = NULL;
/* get the last component of the path */
path = URL_PATH(url);
path2 = strrchr(path, '/');
path = path2 ? path2 + 1 : path;
/* truncate the path if necessary */
if (strlen(path) > MaxLen) {
path = free1 = dStrndup(path, MaxLen);
}
/* is there a query? */
query = URL_QUERY(url);
if (*query) {
/* truncate the query if necessary */
if (strlen(query) > MaxLen) {
query = free2 = dStrndup(query, MaxLen);
}
name = dStrconcat(dir, path, "?", query, NULL);
} else {
name = dStrconcat(dir, path, NULL);
}
dFree(free1);
dFree(free2);
/* Replace %20 and ' ' with '_' */
for (n1 = n2 = name; *n1; n1++, n2++) {
*n2 =
(n1[0] == ' ')
? '_' :
(n1[0] == '%' && n1[1] == '2' && n1[2] == '0')
? (n1 += 2, '_') :
n1[0];
}
*n2 = 0;
return name;
}
/*
* Set the default directory for saving files.
*/
void a_UIcmd_init(void)
{
const char *dir = prefs.save_dir;
if (dir && *dir) {
// assert a trailing '/'
save_dir =
(dir[strlen(dir)-1] == '/')
? dStrdup(dir)
: dStrconcat(dir, "/", NULL);
}