/
BrowserWindow.cpp
2416 lines (2084 loc) · 64.8 KB
/
BrowserWindow.cpp
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
/*
* Copyright (C) 2007 Andrea Anzani <andrea.anzani@gmail.com>
* Copyright (C) 2007, 2010 Ryan Leavengood <leavengood@gmail.com>
* Copyright (C) 2009 Maxime Simon <simon.maxime@gmail.com>
* Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
* Copyright (C) 2010 Michael Lotz <mmlr@mlotz.ch>
* Copyright (C) 2010 Rene Gollent <rene@gollent.com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BrowserWindow.h"
#include <Alert.h>
#include <Application.h>
#include <Bitmap.h>
#include <Button.h>
#include <Catalog.h>
#include <CheckBox.h>
#include <Clipboard.h>
#include <ControlLook.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <GridLayoutBuilder.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <Locale.h>
#include <MenuBar.h>
#include <MenuItem.h>
#include <MessageRunner.h>
#include <NodeInfo.h>
#include <Path.h>
#include <Roster.h>
#include <Screen.h>
#include <SeparatorView.h>
#include <Size.h>
#include <SpaceLayoutItem.h>
#include <StatusBar.h>
#include <StringView.h>
#include <TextControl.h>
#include <stdio.h>
#include "AuthenticationPanel.h"
#include "BaseURL.h"
#include "BitmapButton.h"
#include "BrowserApp.h"
#include "BrowsingHistory.h"
#include "CredentialsStorage.h"
#include "IconButton.h"
#include "NavMenu.h"
#include "SettingsKeys.h"
#include "SettingsMessage.h"
#include "TabManager.h"
#include "URLInputGroup.h"
#include "WebPage.h"
#include "WebView.h"
#include "WebViewConstants.h"
#include "WindowIcon.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "WebPositive Window"
enum {
OPEN_LOCATION = 'open',
GO_BACK = 'goba',
GO_FORWARD = 'gofo',
STOP = 'stop',
HOME = 'home',
GOTO_URL = 'goul',
RELOAD = 'reld',
CLEAR_HISTORY = 'clhs',
CREATE_BOOKMARK = 'crbm',
SHOW_BOOKMARKS = 'shbm',
ZOOM_FACTOR_INCREASE = 'zfin',
ZOOM_FACTOR_DECREASE = 'zfdc',
ZOOM_FACTOR_RESET = 'zfrs',
ZOOM_TEXT_ONLY = 'zfto',
TOGGLE_FULLSCREEN = 'tgfs',
TOGGLE_AUTO_HIDE_INTERFACE_IN_FULLSCREEN = 'tgah',
CHECK_AUTO_HIDE_INTERFACE = 'cahi',
SHOW_PAGE_SOURCE = 'spgs',
EDIT_SHOW_FIND_GROUP = 'sfnd',
EDIT_HIDE_FIND_GROUP = 'hfnd',
EDIT_FIND_NEXT = 'fndn',
EDIT_FIND_PREVIOUS = 'fndp',
FIND_TEXT_CHANGED = 'ftxt',
SELECT_TAB = 'sltb',
};
static BLayoutItem*
layoutItemFor(BView* view)
{
BLayout* layout = view->Parent()->GetLayout();
int32 index = layout->IndexOfView(view);
return layout->ItemAt(index);
}
class BookmarkMenu : public BNavMenu {
public:
BookmarkMenu(const char* title, BHandler* target, const entry_ref* navDir)
:
BNavMenu(title, B_REFS_RECEIVED, target)
{
// Add these items here already, so the shortcuts work even when
// the menu has never been opened yet.
_AddStaticItems();
SetNavDir(navDir);
}
virtual void AttachedToWindow()
{
RemoveItems(0, CountItems(), true);
ForceRebuild();
BNavMenu::AttachedToWindow();
if (CountItems() > 0)
AddItem(new BSeparatorItem(), 0);
_AddStaticItems();
DoLayout();
}
private:
void _AddStaticItems()
{
AddItem(new BMenuItem(B_TRANSLATE("Manage bookmarks"),
new BMessage(SHOW_BOOKMARKS), 'M'), 0);
AddItem(new BMenuItem(B_TRANSLATE("Bookmark this page"),
new BMessage(CREATE_BOOKMARK), 'B'), 0);
}
};
class PageUserData : public BWebView::UserData {
public:
PageUserData(BView* focusedView)
:
fFocusedView(focusedView),
fPageIcon(NULL),
fURLInputSelectionStart(-1),
fURLInputSelectionEnd(-1)
{
}
~PageUserData()
{
delete fPageIcon;
}
void SetFocusedView(BView* focusedView)
{
fFocusedView = focusedView;
}
BView* FocusedView() const
{
return fFocusedView;
}
void SetPageIcon(const BBitmap* icon)
{
delete fPageIcon;
if (icon)
fPageIcon = new BBitmap(icon);
else
fPageIcon = NULL;
}
const BBitmap* PageIcon() const
{
return fPageIcon;
}
void SetURLInputContents(const char* text)
{
fURLInputContents = text;
}
const BString& URLInputContents() const
{
return fURLInputContents;
}
void SetURLInputSelection(int32 selectionStart, int32 selectionEnd)
{
fURLInputSelectionStart = selectionStart;
fURLInputSelectionEnd = selectionEnd;
}
int32 URLInputSelectionStart() const
{
return fURLInputSelectionStart;
}
int32 URLInputSelectionEnd() const
{
return fURLInputSelectionEnd;
}
private:
BView* fFocusedView;
BBitmap* fPageIcon;
BString fURLInputContents;
int32 fURLInputSelectionStart;
int32 fURLInputSelectionEnd;
};
class CloseButton : public BButton {
public:
CloseButton(BMessage* message)
:
BButton("close button", NULL, message),
fOverCloseRect(false)
{
// Button is 16x16 regardless of font size
SetExplicitMinSize(BSize(15, 15));
SetExplicitMaxSize(BSize(15, 15));
}
virtual void Draw(BRect updateRect)
{
BRect frame = Bounds();
BRect closeRect(frame.InsetByCopy(4, 4));
rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
float tint = B_DARKEN_1_TINT;
if (fOverCloseRect)
tint *= 1.4;
else
tint *= 1.2;
if (Value() == B_CONTROL_ON && fOverCloseRect) {
// Draw the button frame
be_control_look->DrawButtonFrame(this, frame, updateRect,
base, base, BControlLook::B_ACTIVATED
| BControlLook::B_BLEND_FRAME);
be_control_look->DrawButtonBackground(this, frame,
updateRect, base, BControlLook::B_ACTIVATED);
closeRect.OffsetBy(1, 1);
tint *= 1.2;
} else {
SetHighColor(base);
FillRect(updateRect);
}
// Draw the ×
base = tint_color(base, tint);
SetHighColor(base);
SetPenSize(2);
StrokeLine(closeRect.LeftTop(), closeRect.RightBottom());
StrokeLine(closeRect.LeftBottom(), closeRect.RightTop());
SetPenSize(1);
}
virtual void MouseMoved(BPoint where, uint32 transit,
const BMessage* dragMessage)
{
switch (transit) {
case B_ENTERED_VIEW:
fOverCloseRect = true;
Invalidate();
break;
case B_EXITED_VIEW:
fOverCloseRect = false;
Invalidate();
break;
case B_INSIDE_VIEW:
fOverCloseRect = true;
break;
case B_OUTSIDE_VIEW:
fOverCloseRect = false;
break;
}
BButton::MouseMoved(where, transit, dragMessage);
}
private:
bool fOverCloseRect;
};
// #pragma mark - BrowserWindow
BrowserWindow::BrowserWindow(BRect frame, SettingsMessage* appSettings,
const BString& url, uint32 interfaceElements, BWebView* webView)
:
BWebWindow(frame, kApplicationName,
B_DOCUMENT_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
B_AUTO_UPDATE_SIZE_LIMITS | B_ASYNCHRONOUS_CONTROLS),
fIsFullscreen(false),
fInterfaceVisible(false),
fPulseRunner(NULL),
fVisibleInterfaceElements(interfaceElements),
fAppSettings(appSettings),
fZoomTextOnly(true),
fShowTabsIfSinglePageOpen(true),
fAutoHideInterfaceInFullscreenMode(false),
fAutoHidePointer(false)
{
// Begin listening to settings changes and read some current values.
fAppSettings->AddListener(BMessenger(this));
// fZoomTextOnly = fAppSettings->GetValue("zoom text only", fZoomTextOnly);
fShowTabsIfSinglePageOpen = fAppSettings->GetValue(
kSettingsKeyShowTabsIfSinglePageOpen, fShowTabsIfSinglePageOpen);
fAutoHidePointer = fAppSettings->GetValue(kSettingsKeyAutoHidePointer,
fAutoHidePointer);
fNewWindowPolicy = fAppSettings->GetValue(kSettingsKeyNewWindowPolicy,
(uint32)OpenStartPage);
fNewTabPolicy = fAppSettings->GetValue(kSettingsKeyNewTabPolicy,
(uint32)OpenBlankPage);
fStartPageURL = fAppSettings->GetValue(kSettingsKeyStartPageURL,
kDefaultStartPageURL);
fSearchPageURL = fAppSettings->GetValue(kSettingsKeySearchPageURL,
kDefaultSearchPageURL);
// Create the interface elements
BMessage* newTabMessage = new BMessage(NEW_TAB);
newTabMessage->AddString("url", "");
newTabMessage->AddPointer("window", this);
newTabMessage->AddBool("select", true);
fTabManager = new TabManager(BMessenger(this), newTabMessage);
// Menu
#if INTEGRATE_MENU_INTO_TAB_BAR
BMenu* mainMenu = fTabManager->Menu();
#else
BMenu* mainMenu = new BMenuBar("Main menu");
#endif
BMenu* menu = new BMenu(B_TRANSLATE("Window"));
BMessage* newWindowMessage = new BMessage(NEW_WINDOW);
newWindowMessage->AddString("url", "");
BMenuItem* newItem = new BMenuItem(B_TRANSLATE("New window"),
newWindowMessage, 'N');
menu->AddItem(newItem);
newItem->SetTarget(be_app);
newItem = new BMenuItem(B_TRANSLATE("New tab"),
new BMessage(*newTabMessage), 'T');
menu->AddItem(newItem);
newItem->SetTarget(be_app);
menu->AddItem(new BMenuItem(B_TRANSLATE("Open location"),
new BMessage(OPEN_LOCATION), 'L'));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem(B_TRANSLATE("Close window"),
new BMessage(B_QUIT_REQUESTED), 'W', B_SHIFT_KEY));
menu->AddItem(new BMenuItem(B_TRANSLATE("Close tab"),
new BMessage(CLOSE_TAB), 'W'));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem(B_TRANSLATE("Downloads"),
new BMessage(SHOW_DOWNLOAD_WINDOW), 'D'));
menu->AddItem(new BMenuItem(B_TRANSLATE("Settings"),
new BMessage(SHOW_SETTINGS_WINDOW)));
BMenuItem* aboutItem = new BMenuItem(B_TRANSLATE("About"),
new BMessage(B_ABOUT_REQUESTED));
menu->AddItem(aboutItem);
aboutItem->SetTarget(be_app);
menu->AddSeparatorItem();
BMenuItem* quitItem = new BMenuItem(B_TRANSLATE("Quit"),
new BMessage(B_QUIT_REQUESTED), 'Q');
menu->AddItem(quitItem);
quitItem->SetTarget(be_app);
mainMenu->AddItem(menu);
menu = new BMenu(B_TRANSLATE("Edit"));
menu->AddItem(fCutMenuItem = new BMenuItem(B_TRANSLATE("Cut"),
new BMessage(B_CUT), 'X'));
menu->AddItem(fCopyMenuItem = new BMenuItem(B_TRANSLATE("Copy"),
new BMessage(B_COPY), 'C'));
menu->AddItem(fPasteMenuItem = new BMenuItem(B_TRANSLATE("Paste"),
new BMessage(B_PASTE), 'V'));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem(B_TRANSLATE("Find"),
new BMessage(EDIT_SHOW_FIND_GROUP), 'F'));
menu->AddItem(fFindPreviousMenuItem
= new BMenuItem(B_TRANSLATE("Find previous"),
new BMessage(EDIT_FIND_PREVIOUS), 'G', B_SHIFT_KEY));
menu->AddItem(fFindNextMenuItem = new BMenuItem(B_TRANSLATE("Find next"),
new BMessage(EDIT_FIND_NEXT), 'G'));
mainMenu->AddItem(menu);
fFindPreviousMenuItem->SetEnabled(false);
fFindNextMenuItem->SetEnabled(false);
menu = new BMenu(B_TRANSLATE("View"));
menu->AddItem(new BMenuItem(B_TRANSLATE("Reload"), new BMessage(RELOAD),
'R'));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem(B_TRANSLATE("Increase size"),
new BMessage(ZOOM_FACTOR_INCREASE), '+'));
menu->AddItem(new BMenuItem(B_TRANSLATE("Decrease size"),
new BMessage(ZOOM_FACTOR_DECREASE), '-'));
menu->AddItem(new BMenuItem(B_TRANSLATE("Reset size"),
new BMessage(ZOOM_FACTOR_RESET), '0'));
fZoomTextOnlyMenuItem = new BMenuItem(B_TRANSLATE("Zoom text only"),
new BMessage(ZOOM_TEXT_ONLY));
fZoomTextOnlyMenuItem->SetMarked(fZoomTextOnly);
menu->AddItem(fZoomTextOnlyMenuItem);
menu->AddSeparatorItem();
fFullscreenItem = new BMenuItem(B_TRANSLATE("Full screen"),
new BMessage(TOGGLE_FULLSCREEN), B_RETURN);
menu->AddItem(fFullscreenItem);
menu->AddItem(new BMenuItem(B_TRANSLATE("Page source"),
new BMessage(SHOW_PAGE_SOURCE), 'U'));
mainMenu->AddItem(menu);
fHistoryMenu = new BMenu(B_TRANSLATE("History"));
fHistoryMenu->AddItem(fBackMenuItem = new BMenuItem(B_TRANSLATE("Back"),
new BMessage(GO_BACK), B_LEFT_ARROW));
fHistoryMenu->AddItem(fForwardMenuItem
= new BMenuItem(B_TRANSLATE("Forward"), new BMessage(GO_FORWARD),
B_RIGHT_ARROW));
fHistoryMenu->AddSeparatorItem();
fHistoryMenuFixedItemCount = fHistoryMenu->CountItems();
mainMenu->AddItem(fHistoryMenu);
BPath bookmarkPath;
entry_ref bookmarkRef;
if (_BookmarkPath(bookmarkPath) == B_OK
&& get_ref_for_path(bookmarkPath.Path(), &bookmarkRef) == B_OK) {
BMenu* bookmarkMenu
= new BookmarkMenu(B_TRANSLATE("Bookmarks"), this, &bookmarkRef);
mainMenu->AddItem(bookmarkMenu);
}
// Back, Forward, Stop & Home buttons
fBackButton = new IconButton("Back", 0, NULL, new BMessage(GO_BACK));
fBackButton->SetIcon(201);
fBackButton->TrimIcon();
fForwardButton = new IconButton("Forward", 0, NULL, new BMessage(GO_FORWARD));
fForwardButton->SetIcon(202);
fForwardButton->TrimIcon();
fStopButton = new IconButton("Stop", 0, NULL, new BMessage(STOP));
fStopButton->SetIcon(204);
fStopButton->TrimIcon();
fHomeButton = new IconButton("Home", 0, NULL, new BMessage(HOME));
fHomeButton->SetIcon(206);
fHomeButton->TrimIcon();
if (!fAppSettings->GetValue(kSettingsKeyShowHomeButton, true))
fHomeButton->Hide();
// URL input group
fURLInputGroup = new URLInputGroup(new BMessage(GOTO_URL));
// Status Bar
fStatusText = new BStringView("status", "");
fStatusText->SetAlignment(B_ALIGN_LEFT);
fStatusText->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
fStatusText->SetExplicitMinSize(BSize(150, 12));
// Prevent the window from growing to fit a long status message...
BFont font(be_plain_font);
font.SetSize(ceilf(font.Size() * 0.8));
fStatusText->SetFont(&font, B_FONT_SIZE);
// Loading progress bar
fLoadingProgressBar = new BStatusBar("progress");
fLoadingProgressBar->SetMaxValue(100);
fLoadingProgressBar->Hide();
fLoadingProgressBar->SetBarHeight(12);
const float kInsetSpacing = 3;
const float kElementSpacing = 5;
// Find group
fFindCloseButton = new CloseButton(new BMessage(EDIT_HIDE_FIND_GROUP));
fFindTextControl = new BTextControl("find", B_TRANSLATE("Find:"), "",
new BMessage(EDIT_FIND_NEXT));
fFindTextControl->SetModificationMessage(new BMessage(FIND_TEXT_CHANGED));
fFindPreviousButton = new BButton(B_TRANSLATE("Previous"),
new BMessage(EDIT_FIND_PREVIOUS));
fFindPreviousButton->SetToolTip(
B_TRANSLATE_COMMENT("Find previous occurrence of search terms",
"find bar previous button tooltip"));
fFindNextButton = new BButton(B_TRANSLATE("Next"),
new BMessage(EDIT_FIND_NEXT));
fFindNextButton->SetToolTip(
B_TRANSLATE_COMMENT("Find next occurrence of search terms",
"find bar next button tooltip"));
fFindCaseSensitiveCheckBox = new BCheckBox(B_TRANSLATE("Match case"));
BGroupLayout* findGroup = BLayoutBuilder::Group<>(B_VERTICAL, 0.0)
.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
.Add(BGroupLayoutBuilder(B_HORIZONTAL, B_USE_SMALL_SPACING)
.Add(fFindCloseButton)
.Add(fFindTextControl)
.Add(fFindPreviousButton)
.Add(fFindNextButton)
.Add(fFindCaseSensitiveCheckBox)
.SetInsets(kInsetSpacing, kInsetSpacing,
kInsetSpacing, kInsetSpacing)
)
;
// Navigation group
BGroupLayout* navigationGroup = BLayoutBuilder::Group<>(B_VERTICAL, 0.0)
.Add(BLayoutBuilder::Group<>(B_HORIZONTAL, kElementSpacing)
.Add(fBackButton)
.Add(fForwardButton)
.Add(fStopButton)
.Add(fHomeButton)
.Add(fURLInputGroup)
.SetInsets(kInsetSpacing, kInsetSpacing, kInsetSpacing,
kInsetSpacing)
)
.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
;
// Status bar group
BGroupLayout* statusGroup = BLayoutBuilder::Group<>(B_VERTICAL, 0.0)
.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
.Add(BLayoutBuilder::Group<>(B_HORIZONTAL, kElementSpacing)
.Add(fStatusText)
.Add(fLoadingProgressBar, 0.2)
.AddStrut(12 - kElementSpacing)
.SetInsets(kInsetSpacing, 0, kInsetSpacing, 0)
)
;
BitmapButton* toggleFullscreenButton = new BitmapButton(kWindowIconBits,
kWindowIconWidth, kWindowIconHeight, kWindowIconFormat,
new BMessage(TOGGLE_FULLSCREEN));
toggleFullscreenButton->SetBackgroundMode(BitmapButton::MENUBAR_BACKGROUND);
BGroupLayout* menuBarGroup = BLayoutBuilder::Group<>(B_HORIZONTAL, 0.0)
.Add(mainMenu)
.Add(toggleFullscreenButton, 0.0f)
;
// Layout
AddChild(BLayoutBuilder::Group<>(B_VERTICAL, 0.0)
#if !INTEGRATE_MENU_INTO_TAB_BAR
.Add(menuBarGroup)
#endif
.Add(fTabManager->TabGroup())
.Add(navigationGroup)
.Add(fTabManager->ContainerView())
.Add(findGroup)
.Add(statusGroup)
);
fURLInputGroup->MakeFocus(true);
fMenuGroup = menuBarGroup;
fTabGroup = fTabManager->TabGroup()->GetLayout();
fNavigationGroup = navigationGroup;
fFindGroup = findGroup;
fStatusGroup = statusGroup;
fToggleFullscreenButton = layoutItemFor(toggleFullscreenButton);
fFindGroup->SetVisible(false);
fToggleFullscreenButton->SetVisible(false);
CreateNewTab(url, true, webView);
_ShowInterface(true);
_SetAutoHideInterfaceInFullscreen(fAppSettings->GetValue(
kSettingsKeyAutoHideInterfaceInFullscreenMode,
fAutoHideInterfaceInFullscreenMode));
AddShortcut('F', B_COMMAND_KEY | B_SHIFT_KEY,
new BMessage(EDIT_HIDE_FIND_GROUP));
// TODO: Should be a different shortcut, H is usually for Find selection.
AddShortcut('H', B_COMMAND_KEY, new BMessage(HOME));
// Add shortcuts to select a particular tab
for (int32 i = 1; i <= 9; i++) {
BMessage* selectTab = new BMessage(SELECT_TAB);
selectTab->AddInt32("tab index", i - 1);
char numStr[2];
snprintf(numStr, sizeof(numStr), "%d", (int) i);
AddShortcut(numStr[0], B_COMMAND_KEY, selectTab);
}
be_app->PostMessage(WINDOW_OPENED);
}
BrowserWindow::~BrowserWindow()
{
fAppSettings->RemoveListener(BMessenger(this));
delete fTabManager;
delete fPulseRunner;
}
void
BrowserWindow::DispatchMessage(BMessage* message, BHandler* target)
{
const char* bytes;
uint32 modifierKeys;
if ((message->what == B_KEY_DOWN || message->what == B_UNMAPPED_KEY_DOWN)
&& message->FindString("bytes", &bytes) == B_OK
&& message->FindInt32("modifiers", (int32*)&modifierKeys) == B_OK) {
modifierKeys = modifierKeys & 0x000000ff;
if (bytes[0] == B_LEFT_ARROW && modifierKeys == B_COMMAND_KEY) {
PostMessage(GO_BACK);
return;
} else if (bytes[0] == B_RIGHT_ARROW && modifierKeys == B_COMMAND_KEY) {
PostMessage(GO_FORWARD);
return;
} else if (bytes[0] == B_FUNCTION_KEY) {
// Some function key Firefox compatibility
int32 key;
if (message->FindInt32("key", &key) == B_OK) {
switch (key) {
case B_F5_KEY:
PostMessage(RELOAD);
break;
case B_F11_KEY:
PostMessage(TOGGLE_FULLSCREEN);
break;
default:
break;
}
}
} else if (target == fURLInputGroup->TextView()) {
// Handle B_RETURN in the URL text control. This is the easiest
// way to react *only* when the user presses the return key in the
// address bar, as opposed to trying to load whatever is in there
// when the text control just goes out of focus.
if (bytes[0] == B_RETURN) {
// Do it in such a way that the user sees the Go-button go down.
_InvokeButtonVisibly(fURLInputGroup->GoButton());
return;
}
} else if (target == fFindTextControl->TextView()) {
// Handle B_RETURN when the find text control has focus.
if (bytes[0] == B_RETURN) {
if ((modifierKeys & B_SHIFT_KEY) != 0)
_InvokeButtonVisibly(fFindPreviousButton);
else
_InvokeButtonVisibly(fFindNextButton);
return;
} else if (bytes[0] == B_ESCAPE) {
_InvokeButtonVisibly(fFindCloseButton);
return;
}
} else if (bytes[0] == B_ESCAPE) {
// Default escape key behavior:
PostMessage(STOP);
return;
}
}
if (message->what == B_MOUSE_MOVED || message->what == B_MOUSE_DOWN
|| message->what == B_MOUSE_UP) {
message->FindPoint("where", &fLastMousePos);
if (message->FindInt64("when", &fLastMouseMovedTime) != B_OK)
fLastMouseMovedTime = system_time();
_CheckAutoHideInterface();
}
if (message->what == B_MOUSE_WHEEL_CHANGED) {
BPoint where;
uint32 buttons;
CurrentWebView()->GetMouse(&where, &buttons, false);
// Only do this when the mouse is over the web view
if (CurrentWebView()->Bounds().Contains(where)) {
// Zoom and unzoom text on Command + mouse wheel.
// This could of course (and maybe should be) implemented in the
// WebView, but there would need to be a way for the WebView to
// know the setting of the fZoomTextOnly member here. Plus other
// clients of the API may not want this feature.
if ((modifiers() & B_COMMAND_KEY) != 0) {
float dy;
if (message->FindFloat("be:wheel_delta_y", &dy) == B_OK) {
if (dy < 0)
CurrentWebView()->IncreaseZoomFactor(fZoomTextOnly);
else
CurrentWebView()->DecreaseZoomFactor(fZoomTextOnly);
return;
}
}
} else // Also don't scroll up and down if the mouse is not over the web view
return;
}
BWebWindow::DispatchMessage(message, target);
}
void
BrowserWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case OPEN_LOCATION:
_ShowInterface(true);
if (fURLInputGroup->TextView()->IsFocus())
fURLInputGroup->TextView()->SelectAll();
else
fURLInputGroup->MakeFocus(true);
break;
case RELOAD:
CurrentWebView()->Reload();
break;
case GOTO_URL:
{
BString url;
if (message->FindString("url", &url) != B_OK)
url = fURLInputGroup->Text();
_SetPageIcon(CurrentWebView(), NULL);
_SmartURLHandler(url);
break;
}
case GO_BACK:
CurrentWebView()->GoBack();
break;
case GO_FORWARD:
CurrentWebView()->GoForward();
break;
case STOP:
CurrentWebView()->StopLoading();
break;
case HOME:
CurrentWebView()->LoadURL(fStartPageURL);
break;
case CLEAR_HISTORY: {
BrowsingHistory* history = BrowsingHistory::DefaultInstance();
if (history->CountItems() == 0)
break;
BAlert* alert = new BAlert(B_TRANSLATE("Confirmation"),
B_TRANSLATE("Do you really want to "
"clear the browsing history?"), B_TRANSLATE("Clear"),
B_TRANSLATE("Cancel"));
alert->SetShortcut(1, B_ESCAPE);
if (alert->Go() == 0)
history->Clear();
break;
}
case CREATE_BOOKMARK:
_CreateBookmark();
break;
case SHOW_BOOKMARKS:
_ShowBookmarks();
break;
case B_REFS_RECEIVED:
{
// Currently the only source of these messages is the bookmarks menu.
// Filter refs into URLs, this also gets rid of refs for folders.
// For clicks on sub-folders in the bookmarks menu, we have Tracker
// open the corresponding folder.
entry_ref ref;
uint32 addedCount = 0;
for (int32 i = 0; message->FindRef("refs", i, &ref) == B_OK; i++) {
BEntry entry(&ref);
uint32 addedSubCount = 0;
if (entry.IsDirectory()) {
BDirectory directory(&entry);
_AddBookmarkURLsRecursively(directory, message,
addedSubCount);
} else {
BFile file(&ref, B_READ_ONLY);
BString url;
if (_ReadURLAttr(file, url)) {
message->AddString("url", url.String());
addedSubCount++;
}
}
if (addedSubCount == 0) {
// Don't know what to do with this entry, just pass it
// on to the system to handle. Note that this may result
// in us opening other supported files via the application
// mechanism.
be_roster->Launch(&ref);
}
addedCount += addedSubCount;
}
message->RemoveName("refs");
if (addedCount > 10) {
BString string(B_TRANSLATE_COMMENT("Do you want to open %addedCount "
"bookmarks all at once?", "Don't translate variable %addedCount."));
string.ReplaceFirst("%addedCount", BString() << addedCount);
BAlert* alert = new BAlert(B_TRANSLATE("Open bookmarks confirmation"),
string.String(), B_TRANSLATE("Cancel"), B_TRANSLATE("Open all"));
alert->SetShortcut(0, B_ESCAPE);
if (alert->Go() == 0)
break;
}
be_app->PostMessage(message);
break;
}
case B_SIMPLE_DATA:
{
// User possibly dropped files on this window.
// If there is more than one entry_ref, let the app handle it
// (open one new page per ref). If there is one ref, open it in
// this window.
type_code type;
int32 countFound;
if (message->GetInfo("refs", &type, &countFound) != B_OK
|| type != B_REF_TYPE) {
break;
}
if (countFound > 1) {
message->what = B_REFS_RECEIVED;
be_app->PostMessage(message);
break;
}
entry_ref ref;
if (message->FindRef("refs", &ref) != B_OK)
break;
BEntry entry(&ref, true);
BPath path;
if (!entry.Exists() || entry.GetPath(&path) != B_OK)
break;
CurrentWebView()->LoadURL(path.Path());
break;
}
case ZOOM_FACTOR_INCREASE:
CurrentWebView()->IncreaseZoomFactor(fZoomTextOnly);
break;
case ZOOM_FACTOR_DECREASE:
CurrentWebView()->DecreaseZoomFactor(fZoomTextOnly);
break;
case ZOOM_FACTOR_RESET:
CurrentWebView()->ResetZoomFactor();
break;
case ZOOM_TEXT_ONLY:
fZoomTextOnly = !fZoomTextOnly;
fZoomTextOnlyMenuItem->SetMarked(fZoomTextOnly);
// TODO: Would be nice to have an instant update if the page is
// already zoomed.
break;
case TOGGLE_FULLSCREEN:
ToggleFullscreen();
break;
case TOGGLE_AUTO_HIDE_INTERFACE_IN_FULLSCREEN:
_SetAutoHideInterfaceInFullscreen(
!fAutoHideInterfaceInFullscreenMode);
break;
case CHECK_AUTO_HIDE_INTERFACE:
_CheckAutoHideInterface();
break;
case SHOW_PAGE_SOURCE:
CurrentWebView()->WebPage()->SendPageSource();
break;
case B_PAGE_SOURCE_RESULT:
_HandlePageSourceResult(message);
break;
case EDIT_FIND_NEXT:
CurrentWebView()->FindString(fFindTextControl->Text(), true,
fFindCaseSensitiveCheckBox->Value());
break;
case FIND_TEXT_CHANGED:
{
bool findTextAvailable = strlen(fFindTextControl->Text()) > 0;
fFindPreviousMenuItem->SetEnabled(findTextAvailable);
fFindNextMenuItem->SetEnabled(findTextAvailable);
break;
}
case EDIT_FIND_PREVIOUS:
CurrentWebView()->FindString(fFindTextControl->Text(), false,
fFindCaseSensitiveCheckBox->Value());
break;
case EDIT_SHOW_FIND_GROUP:
if (!fFindGroup->IsVisible())
fFindGroup->SetVisible(true);
fFindTextControl->MakeFocus(true);
break;
case EDIT_HIDE_FIND_GROUP:
if (fFindGroup->IsVisible()) {
fFindGroup->SetVisible(false);
if (CurrentWebView() != NULL)
CurrentWebView()->MakeFocus(true);
}
break;
case B_CUT:
case B_COPY:
case B_PASTE:
{
BTextView* textView = dynamic_cast<BTextView*>(CurrentFocus());
if (textView != NULL)
textView->MessageReceived(message);
else if (CurrentWebView() != NULL)
CurrentWebView()->MessageReceived(message);
break;
}
case B_EDITING_CAPABILITIES_RESULT:
{
BWebView* webView;
if (message->FindPointer("view",
reinterpret_cast<void**>(&webView)) != B_OK
|| webView != CurrentWebView()) {
break;
}
bool canCut;
bool canCopy;
bool canPaste;
if (message->FindBool("can cut", &canCut) != B_OK)
canCut = false;
if (message->FindBool("can copy", &canCopy) != B_OK)
canCopy = false;
if (message->FindBool("can paste", &canPaste) != B_OK)
canPaste = false;
fCutMenuItem->SetEnabled(canCut);
fCopyMenuItem->SetEnabled(canCopy);
fPasteMenuItem->SetEnabled(canPaste);
break;
}
case SHOW_DOWNLOAD_WINDOW:
case SHOW_SETTINGS_WINDOW:
message->AddUInt32("workspaces", Workspaces());
be_app->PostMessage(message);
break;
case CLOSE_TAB:
if (fTabManager->CountTabs() > 1) {
int32 index;
if (message->FindInt32("tab index", &index) != B_OK)
index = fTabManager->SelectedTabIndex();
_ShutdownTab(index);
_UpdateTabGroupVisibility();
} else
PostMessage(B_QUIT_REQUESTED);
break;
case SELECT_TAB:
{
int32 index;
if (message->FindInt32("tab index", &index) == B_OK
&& fTabManager->SelectedTabIndex() != index
&& fTabManager->CountTabs() > index) {
fTabManager->SelectTab(index);
}
break;
}
case TAB_CHANGED:
{
// This message may be received also when the last tab closed,
// i.e. with index == -1.
int32 index;
if (message->FindInt32("tab index", &index) != B_OK)
index = -1;
_TabChanged(index);
break;
}
case SETTINGS_VALUE_CHANGED:
{
BString name;
if (message->FindString("name", &name) != B_OK)
break;
bool flag;
BString string;
uint32 value;
if (name == kSettingsKeyShowTabsIfSinglePageOpen