forked from adobe/brackets-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_window_gtk.cc
More file actions
964 lines (789 loc) · 31 KB
/
root_window_gtk.cc
File metadata and controls
964 lines (789 loc) · 31 KB
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
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "appshell/browser/root_window_gtk.h"
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#undef Success // Definition conflicts with cef_message_router.h
#undef RootWindow // Definition conflicts with root_window.h
#include "include/base/cef_bind.h"
#include "include/cef_app.h"
#include "appshell/browser/browser_window_std_gtk.h"
#include "appshell/browser/main_context.h"
#include "appshell/browser/main_message_loop.h"
#include "appshell/browser/resource.h"
#include "appshell/browser/temp_window.h"
#include "appshell/browser/window_test.h"
#include "appshell/common/client_switches.h"
// Brackets specific changes.
#include "appshell/native_menu_model.h"
#include "appshell/command_callbacks.h"
#include "appshell/appshell_helpers.h"
#define DEFAULT_WINDOW_WIDTH 800
#define DEFAULT_WINDOW_HEIGHT 600
// End of Brackets specific changes.
namespace client {
namespace {
const char kMenuIdKey[] = "menu_id";
// Brackets specific changes.
gboolean IsWindowMaximized(GtkWindow* window) {
if (window) {
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
if (gdk_window) {
gint state = gdk_window_get_state(gdk_window);
return (state & GDK_WINDOW_STATE_MAXIMIZED) ? TRUE : FALSE;
}
} else {
return FALSE;
}
}
void MinimizeWindow(GtkWindow* window) {
if (window) {
// Unmaximize the window before minimizing so restore behaves correctly.
if (IsWindowMaximized(window))
gtk_window_unmaximize(window);
gtk_window_iconify(window);
}
}
void MaximizeWindow(GtkWindow* window) {
if (window) {
gtk_window_maximize(window);
}
}
void SaveWindowState(GtkWindow* window) {
if (!window)
return;
gint left = 1;
gint top = 1;
gint width = DEFAULT_WINDOW_WIDTH;
gint height = DEFAULT_WINDOW_HEIGHT;
// Try to center the window.
GdkScreen* screen = gdk_screen_get_default();
if (screen) {
left = (gdk_screen_get_width(screen) - DEFAULT_WINDOW_WIDTH) / 2 ;
top = (gdk_screen_get_height(screen) - DEFAULT_WINDOW_HEIGHT) / 2 ;
}
GKeyFile* key_file = g_key_file_new();
GError* err = NULL;
gchar* filePath = NULL;
if (key_file) {
filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");
gboolean maximized = IsWindowMaximized(window);
// If window is not maximized, save current size and position
if (!maximized) {
gtk_window_get_position(window, &left, &top);
gtk_window_get_size(window, &width, &height);
} else if (g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {
// If maximized, load size and position from file
// to preserve last saved values
left = g_key_file_get_integer(key_file, "position", "left", &err);
if (!err)
top = g_key_file_get_integer(key_file, "position", "top", &err);
if (!err)
width = g_key_file_get_integer(key_file, "size", "width", &err);
if (!err)
height = g_key_file_get_integer(key_file, "size", "height", &err);
// If any value can not be read, restore defaults
if (err) {
left = 1;
top = 1;
width = DEFAULT_WINDOW_WIDTH;
height = DEFAULT_WINDOW_HEIGHT;
g_error_free(err);
}
}
// The values would always be written to file.
g_key_file_set_integer(key_file, "position", "left", left);
g_key_file_set_integer(key_file, "position", "top", top);
g_key_file_set_integer(key_file, "size", "width", width - 1); // DelayedResize() 1 pixel compensation
g_key_file_set_integer(key_file, "size", "height", height - 1); // DelayedResize() 1 pixel compensation
g_key_file_set_boolean(key_file, "state", "maximized", maximized);
err = NULL;
g_key_file_save_to_file(key_file, filePath, &err);
if (err) {
fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`. Error Description: %s\n", err->message);
}
} else {
fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`\n");
}
}
void LoadWindowState(GtkWindow* window) {
if (!window) {
return;
}
// Default values for the window state.
gint left = 1;
gint top = 1;
gint width = DEFAULT_WINDOW_WIDTH;
gint height = DEFAULT_WINDOW_HEIGHT;
// Try to center the window.
GdkScreen* screen = gdk_screen_get_default();
if (screen) {
left = (gdk_screen_get_width(screen) - DEFAULT_WINDOW_WIDTH) / 2 ;
top = (gdk_screen_get_height(screen) - DEFAULT_WINDOW_HEIGHT) / 2 ;
}
gboolean maximized = false;
GKeyFile* key_file = g_key_file_new();
bool any_error = false;
GError* err = NULL;
gchar* filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");
if (key_file && g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {
left = g_key_file_get_integer(key_file, "position", "left", &err);
if (!err)
top = g_key_file_get_integer(key_file, "position", "top", &err);
if (!err)
width = g_key_file_get_integer(key_file, "size", "width", &err);
if (!err)
height = g_key_file_get_integer(key_file, "size", "height", &err);
if (!err)
maximized = g_key_file_get_boolean(key_file, "state", "maximized", &err);
// If any value can not be readed, set defaults again
if (err) {
left = 1;
top = 1;
width = DEFAULT_WINDOW_WIDTH;
height = DEFAULT_WINDOW_HEIGHT;
maximized = TRUE;
}
} else {
any_error = true;
}
gtk_window_move(GTK_WINDOW(window), left, top);
gtk_window_set_default_size(GTK_WINDOW(window), width, height);
if (maximized)
MaximizeWindow(window);
if (err || any_error) {
// The failure could be because the file may not have been present,
// or the file read itself failed.
// In either of the cases default to maximizing the window.
MaximizeWindow(window);
if (err) {
if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND){
fprintf(stderr, "LoadWindowState(): Could not read %s. Error Description:%s.\n", filePath, err->message);
}
g_error_free(err);
} else {
fprintf(stderr, "LoadWindowState(): Could not read %s.\n", filePath);
}
}
}
// End of Brackets specific changes.
} // namespace
RootWindowGtk::RootWindowGtk()
: delegate_(NULL),
with_controls_(false),
with_osr_(false),
is_popup_(false),
initialized_(false),
window_(NULL),
back_button_(NULL),
forward_button_(NULL),
reload_button_(NULL),
stop_button_(NULL),
url_entry_(NULL),
toolbar_height_(0),
menubar_height_(0),
force_close_(false),
window_destroyed_(false),
browser_destroyed_(false) {
}
RootWindowGtk::~RootWindowGtk() {
REQUIRE_MAIN_THREAD();
// The window and browser should already have been destroyed.
DCHECK(window_destroyed_);
DCHECK(browser_destroyed_);
}
void RootWindowGtk::Init(RootWindow::Delegate* delegate,
bool with_controls,
bool with_osr,
const CefRect& bounds,
const CefBrowserSettings& settings,
const std::string& url) {
DCHECK(delegate);
DCHECK(!initialized_);
delegate_ = delegate;
with_controls_ = with_controls;
with_osr_ = with_osr;
start_rect_ = bounds;
CreateBrowserWindow(url);
initialized_ = true;
// Create the native root window on the main thread.
if (CURRENTLY_ON_MAIN_THREAD()) {
CreateRootWindow(settings);
} else {
MAIN_POST_CLOSURE(
base::Bind(&RootWindowGtk::CreateRootWindow, this, settings));
}
}
void RootWindowGtk::InitAsPopup(RootWindow::Delegate* delegate,
bool with_controls,
bool with_osr,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings) {
DCHECK(delegate);
DCHECK(!initialized_);
delegate_ = delegate;
with_controls_ = with_controls;
with_osr_ = with_osr;
is_popup_ = true;
if (popupFeatures.xSet)
start_rect_.x = popupFeatures.x;
if (popupFeatures.ySet)
start_rect_.y = popupFeatures.y;
if (popupFeatures.widthSet)
start_rect_.width = popupFeatures.width;
if (popupFeatures.heightSet)
start_rect_.height = popupFeatures.height;
CreateBrowserWindow(std::string());
initialized_ = true;
// The new popup is initially parented to a temporary window. The native root
// window will be created after the browser is created and the popup window
// will be re-parented to it at that time.
browser_window_->GetPopupConfig(TempWindow::GetWindowHandle(),
windowInfo, client, settings);
}
void RootWindowGtk::Show(ShowMode mode) {
REQUIRE_MAIN_THREAD();
if (!window_)
return;
// Show the GTK window.
gtk_widget_show_all(window_);
if (mode == ShowMinimized)
MinimizeWindow(GTK_WINDOW(window_));
else if (mode == ShowMaximized)
MaximizeWindow(GTK_WINDOW(window_));
// Flush the display to make sure the underlying X11 window gets created
// immediately.
GdkWindow* gdk_window = gtk_widget_get_window(window_);
GdkDisplay* display = gdk_window_get_display(gdk_window);
gdk_display_flush(display);
}
void RootWindowGtk::Hide() {
REQUIRE_MAIN_THREAD();
if (window_)
gtk_widget_hide(window_);
}
void RootWindowGtk::SetBounds(int x, int y, size_t width, size_t height) {
REQUIRE_MAIN_THREAD();
if (!window_)
return;
GtkWindow* window = GTK_WINDOW(window_);
GdkWindow* gdk_window = gtk_widget_get_window(window_);
// Make sure the window isn't minimized or maximized.
if (IsWindowMaximized(window))
gtk_window_unmaximize(window);
else
gtk_window_present(window);
// Retrieve information about the display that contains the window.
GdkScreen* screen = gdk_screen_get_default();
const gint monitor = gdk_screen_get_monitor_at_window(screen, gdk_window);
GdkRectangle rect;
gdk_screen_get_monitor_geometry(screen, monitor, &rect);
gdk_window_move_resize(gdk_window, rect.x, rect.y, rect.width, rect.height);
}
void RootWindowGtk::Close(bool force) {
REQUIRE_MAIN_THREAD();
if (window_) {
SaveWindowState(GTK_WINDOW(window_));
force_close_ = force;
gtk_widget_destroy(window_);
}
}
void RootWindowGtk::SetDeviceScaleFactor(float device_scale_factor) {
REQUIRE_MAIN_THREAD();
if (browser_window_)
browser_window_->SetDeviceScaleFactor(device_scale_factor);
}
float RootWindowGtk::GetDeviceScaleFactor() const {
REQUIRE_MAIN_THREAD();
if (browser_window_)
return browser_window_->GetDeviceScaleFactor();
return 1.0f;
}
CefRefPtr<CefBrowser> RootWindowGtk::GetBrowser() const {
REQUIRE_MAIN_THREAD();
if (browser_window_)
return browser_window_->GetBrowser();
return NULL;
}
ClientWindowHandle RootWindowGtk::GetWindowHandle() const {
REQUIRE_MAIN_THREAD();
return window_;
}
void RootWindowGtk::CreateBrowserWindow(const std::string& startup_url) {
browser_window_.reset(new BrowserWindowStdGtk(this, startup_url));
}
void RootWindowGtk::CreateRootWindow(const CefBrowserSettings& settings) {
REQUIRE_MAIN_THREAD();
DCHECK(!window_);
// TODO(port): If no x,y position is specified the window will always appear
// in the upper-left corner. Maybe there's a better default place to put it?
int x = start_rect_.x;
int y = start_rect_.y;
int width = start_rect_.width;
int height = start_rect_.height;
window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
// Brackets specific change.
if (start_rect_.IsEmpty()) {
LoadWindowState(GTK_WINDOW(window_));
} else {
gtk_window_move(GTK_WINDOW(window_), x, y);
gtk_window_set_default_size(GTK_WINDOW(window_), width, height);
}
// End of Brackets specific change.
g_signal_connect(G_OBJECT(window_), "focus-in-event",
G_CALLBACK(&RootWindowGtk::WindowFocusIn), this);
g_signal_connect(G_OBJECT(window_), "window-state-event",
G_CALLBACK(&RootWindowGtk::WindowState), this);
g_signal_connect(G_OBJECT(window_), "configure-event",
G_CALLBACK(&RootWindowGtk::WindowConfigure), this);
g_signal_connect(G_OBJECT(window_), "destroy",
G_CALLBACK(&RootWindowGtk::WindowDestroy), this);
g_signal_connect(G_OBJECT(window_), "delete_event",
G_CALLBACK(&RootWindowGtk::WindowDelete), this);
const cef_color_t background_color = MainContext::Get()->GetBackgroundColor();
GdkColor color = {0};
color.red = CefColorGetR(background_color) * 65535 / 255;
color.green = CefColorGetG(background_color) * 65535 / 255;
color.blue = CefColorGetB(background_color) * 65535 / 255;
gtk_widget_modify_bg(window_, GTK_STATE_NORMAL, &color);
GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
g_signal_connect(vbox, "size-allocate",
G_CALLBACK(&RootWindowGtk::VboxSizeAllocated), this);
gtk_container_add(GTK_CONTAINER(window_), vbox);
// Brackets specific change.
v_box_ = vbox;
if (with_controls_) {
GtkWidget* menu_bar = CreateMenuBar();
g_signal_connect(menu_bar, "size-allocate",
G_CALLBACK(&RootWindowGtk::MenubarSizeAllocated), this);
gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 0);
GtkWidget* toolbar = gtk_toolbar_new();
// Turn off the labels on the toolbar buttons.
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
g_signal_connect(toolbar, "size-allocate",
G_CALLBACK(&RootWindowGtk::ToolbarSizeAllocated), this);
back_button_ = gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK);
g_signal_connect(back_button_, "clicked",
G_CALLBACK(&RootWindowGtk::BackButtonClicked), this);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), back_button_, -1 /* append */);
forward_button_ = gtk_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD);
g_signal_connect(forward_button_, "clicked",
G_CALLBACK(&RootWindowGtk::ForwardButtonClicked), this);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), forward_button_, -1 /* append */);
reload_button_ = gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH);
g_signal_connect(reload_button_, "clicked",
G_CALLBACK(&RootWindowGtk::ReloadButtonClicked), this);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), reload_button_, -1 /* append */);
stop_button_ = gtk_tool_button_new_from_stock(GTK_STOCK_STOP);
g_signal_connect(stop_button_, "clicked",
G_CALLBACK(&RootWindowGtk::StopButtonClicked), this);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), stop_button_, -1 /* append */);
url_entry_ = gtk_entry_new();
g_signal_connect(url_entry_, "activate",
G_CALLBACK(&RootWindowGtk::URLEntryActivate), this);
g_signal_connect(url_entry_, "button-press-event",
G_CALLBACK(&RootWindowGtk::URLEntryButtonPress), this);
GtkToolItem* tool_item = gtk_tool_item_new();
gtk_container_add(GTK_CONTAINER(tool_item), url_entry_);
gtk_tool_item_set_expand(tool_item, TRUE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tool_item, -1); // append
gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
} else {
// Brackets specific change.
GtkWidget *menu_bar_ = gtk_menu_bar_new();
gtk_box_pack_start(GTK_BOX(vbox), menu_bar_, FALSE, FALSE, 0);
g_signal_connect(menu_bar_, "size-allocate",
G_CALLBACK(&RootWindowGtk::MenubarSizeAllocated), this);
}
// Realize (show) the GTK widget. This must be done before the browser is
// created because the underlying X11 Window is required. |browser_bounds_|
// will be set at this point based on the GTK *SizeAllocated signal callbacks.
Show(ShowNormal);
// Most window managers ignore requests for initial window positions (instead
// using a user-defined placement algorithm) and honor requests after the
// window has already been shown.
//gtk_window_move(GTK_WINDOW(window_), x, y);
// Windowed browsers are parented to the X11 Window underlying the GtkWindow*
// and must be sized manually. The OSR GTK widget, on the other hand, can be
// added to the Vbox container for automatic layout-based sizing.
GtkWidget* parent = with_osr_ ? vbox : window_;
if (!is_popup_) {
// Create the browser window.
browser_window_->CreateBrowser(parent, browser_bounds_, settings,
delegate_->GetRequestContext(this));
} else {
// With popups we already have a browser window. Parent the browser window
// to the root window and show it in the correct location.
browser_window_->ShowPopup(parent, browser_bounds_.x, browser_bounds_.y,
browser_bounds_.width, browser_bounds_.height);
}
}
// The following function makes sure we repaginate
// the content once the window is shown first. The
// problem was that the view port was not getting
// updated and a fake resize was required for the
// content to repaginate.
void DelayedResize(GtkWidget* window_) {
if(window_){
GtkWindow* window = GTK_WINDOW(window_);
GdkWindow* gdk_window = gtk_widget_get_window(window_);
gint x;
gint y;
gint width;
gint height;
gint depth;
gdk_window_get_geometry (gdk_window,
&x,
&y,
&width,
&height,
&depth);
// A hack to repaginate the content inside the window.
// Nudge the width and height by one pixel, so that the
// resize is not noticeable.
gdk_window_resize (gdk_window, width + 1, height + 1);
}
}
void RootWindowGtk::OnBrowserCreated(CefRefPtr<CefBrowser> browser) {
REQUIRE_MAIN_THREAD();
// For popup browsers create the root window once the browser has been
// created.
if (is_popup_)
CreateRootWindow(CefBrowserSettings());
// Post a message to CEF queue for delated resize.
CefPostTask(TID_UI, base::Bind(&DelayedResize, window_));
}
void RootWindowGtk::OnBrowserWindowDestroyed() {
REQUIRE_MAIN_THREAD();
browser_window_.reset();
if (!window_destroyed_) {
// The browser was destroyed first. This could be due to the use of
// off-screen rendering or execution of JavaScript window.close().
// Close the RootWindow.
Close(true);
}
browser_destroyed_ = true;
NotifyDestroyedIfDone();
}
void RootWindowGtk::OnSetAddress(const std::string& url) {
REQUIRE_MAIN_THREAD();
if (url_entry_) {
std::string urlStr(url);
gtk_entry_set_text(GTK_ENTRY(url_entry_), urlStr.c_str());
}
}
void RootWindowGtk::OnSetTitle(const std::string& title) {
REQUIRE_MAIN_THREAD();
if (window_) {
std::string titleStr(title);
gtk_window_set_title(GTK_WINDOW(window_), titleStr.c_str());
}
}
void RootWindowGtk::OnSetFullscreen(bool fullscreen) {
REQUIRE_MAIN_THREAD();
CefRefPtr<CefBrowser> browser = GetBrowser();
// Brackets specific change.
/*
if (browser) {
if (fullscreen)
window_test::Maximize(browser);
else
window_test::Restore(browser);
}
*/
}
void RootWindowGtk::OnSetLoadingState(bool isLoading,
bool canGoBack,
bool canGoForward) {
REQUIRE_MAIN_THREAD();
if (with_controls_) {
gtk_widget_set_sensitive(GTK_WIDGET(stop_button_), isLoading);
gtk_widget_set_sensitive(GTK_WIDGET(reload_button_), !isLoading);
gtk_widget_set_sensitive(GTK_WIDGET(back_button_), canGoBack);
gtk_widget_set_sensitive(GTK_WIDGET(forward_button_), canGoForward);
}
}
void RootWindowGtk::OnSetDraggableRegions(
const std::vector<CefDraggableRegion>& regions) {
REQUIRE_MAIN_THREAD();
// TODO(cef): Implement support for draggable regions on this platform.
}
void RootWindowGtk::NotifyDestroyedIfDone() {
// Notify once both the window and the browser have been destroyed.
if (window_destroyed_ && browser_destroyed_)
delegate_->OnRootWindowDestroyed(this);
}
// static
gboolean RootWindowGtk::WindowFocusIn(GtkWidget* widget,
GdkEventFocus* event,
RootWindowGtk* self) {
if (event->in && self->browser_window_.get()) {
self->browser_window_->SetFocus(true);
// Return true for a windowed browser so that focus is not passed to GTK.
return self->with_osr_ ? FALSE : TRUE;
}
return FALSE;
}
// static
gboolean RootWindowGtk::WindowState(GtkWidget* widget,
GdkEventWindowState* event,
RootWindowGtk* self) {
// Called when the root window is iconified or restored. Hide the browser
// window when the root window is iconified to reduce resource usage.
if ((event->changed_mask & GDK_WINDOW_STATE_ICONIFIED) &&
self->browser_window_.get()) {
if (event->new_window_state & GDK_WINDOW_STATE_ICONIFIED)
self->browser_window_->Hide();
else
self->browser_window_->Show();
}
return TRUE;
}
// static
gboolean RootWindowGtk::WindowConfigure(GtkWindow* window,
GdkEvent* event,
RootWindowGtk* self) {
// Called when size, position or stack order changes.
CefRefPtr<CefBrowser> browser = self->GetBrowser();
if (browser.get()) {
// Notify the browser of move/resize events so that:
// - Popup windows are displayed in the correct location and dismissed
// when the window moves.
// - Drag&drop areas are updated accordingly.
browser->GetHost()->NotifyMoveOrResizeStarted();
}
return FALSE; // Don't stop this message.
}
// static
void RootWindowGtk::WindowDestroy(GtkWidget* widget, RootWindowGtk* self) {
// Called when the root window is destroyed.
self->window_destroyed_ = true;
self->NotifyDestroyedIfDone();
}
// static
gboolean RootWindowGtk::WindowDelete(GtkWidget* widget,
GdkEvent* event,
RootWindowGtk* self) {
// Called to query whether the root window should be closed.
if (self->force_close_)
return FALSE; // Allow the close.
// Brackets specific change.
if (self->browser_window_.get() && !self->browser_window_->IsClosing()) {
CefRefPtr<CefBrowser> browser = self->GetBrowser();
if (browser) {
// Brackets specific change.
self->DispatchCloseToBrowser(browser);
// Cancel the close.
return TRUE;
}
}
// Allow the close.
return FALSE;
}
// Brackets specific change.
void RootWindowGtk::DispatchCloseToBrowser(CefRefPtr<CefBrowser> browser)
{
DCHECK(browser_window_);
if(browser_window_)
browser_window_->DispatchCloseToBrowser(browser);
}
ClientWindowHandle RootWindowGtk::GetContainerHandle() const
{
return v_box_;
}
void RootWindowGtk::InstallMenuHandler(GtkWidget* entry, int tag)
{
if(entry){
g_object_set_data(G_OBJECT(entry), kMenuIdKey, GINT_TO_POINTER(tag));
g_signal_connect(entry, "activate", G_CALLBACK(&RootWindowGtk::MenuItemActivated), this);
}
}
// End of Brackets specific changes.
// static
void RootWindowGtk::VboxSizeAllocated(GtkWidget* widget,
GtkAllocation* allocation,
RootWindowGtk* self) {
// Offset browser positioning by any controls that will appear in the client
// area.
const int ux_height = self->toolbar_height_ + self->menubar_height_ > 1 ? self->menubar_height_ : 0;
const int x = allocation->x;
const int y = allocation->y + ux_height;
const int width = allocation->width;
const int height = allocation->height - ux_height;
// Size the browser window to match the GTK widget.
self->browser_bounds_ = CefRect(x, y, width, height);
if (self->browser_window_.get())
self->browser_window_->SetBounds(x, y, width, height);
}
// static
void RootWindowGtk::MenubarSizeAllocated(GtkWidget* widget,
GtkAllocation* allocation,
RootWindowGtk* self) {
self->menubar_height_ = allocation->height;
}
// Brackets specific change.
// GTK is toggling the menu state just by
// clicking on the menu entry. So posting a task to undo that
// unwanted side effect. This is a @nethip hack.
void DelayedMenuMarkToggle(GtkWidget *menuItem){
if (GTK_IS_CHECK_MENU_ITEM(menuItem)) {
// It is important to make sure our MenuItemACtivated
// knows that we are only changing the state and not
// firing any command as such.
StMenuCommandSkipper skipCmd;
// Get the current state of the menu.
gboolean menuState = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuItem));
// Now go about toggling the state. If Brackets is triggering a menu state change,
// that will get executed later than DelayedMenuMarkToggle, as this task would have been
// posted prior to shell call to fire a command, so need not worry about this function
// getting executed after the shell call.
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuItem), !menuState);
}
}
// static
gboolean RootWindowGtk::MenuItemActivated(GtkWidget* widget,
RootWindowGtk* self) {
// Since we have migrated to checked menu items, setting the
// state of the menu is triggering a menu activation. So made
// sure we actually execute a command only when is menu is
// actually activated by the user and not when setting the menu
// state.
if(!StMenuCommandSkipper::GetMenuCmdSkipFlag()){
// Post a message to undo the undesired menu state change.
// See the comments above for explanation.
CefPostTask(TID_UI, base::Bind(&DelayedMenuMarkToggle, widget));
// Retrieve the menu ID set in AddMenuEntry.
int tag = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), kMenuIdKey));
if (self && self->browser_window_)
self->browser_window_->DispatchCommandToBrowser(self->GetBrowser(), tag);
}
}
// static
void RootWindowGtk::ToolbarSizeAllocated(GtkWidget* widget,
GtkAllocation* allocation,
RootWindowGtk* self) {
self->toolbar_height_ = allocation->height;
}
// static
void RootWindowGtk::BackButtonClicked(GtkButton* button,
RootWindowGtk* self) {
CefRefPtr<CefBrowser> browser = self->GetBrowser();
if (browser.get())
browser->GoBack();
}
// static
void RootWindowGtk::ForwardButtonClicked(GtkButton* button,
RootWindowGtk* self) {
CefRefPtr<CefBrowser> browser = self->GetBrowser();
if (browser.get())
browser->GoForward();
}
// static
void RootWindowGtk::StopButtonClicked(GtkButton* button,
RootWindowGtk* self) {
CefRefPtr<CefBrowser> browser = self->GetBrowser();
if (browser.get())
browser->StopLoad();
}
// static
void RootWindowGtk::ReloadButtonClicked(GtkButton* button,
RootWindowGtk* self) {
CefRefPtr<CefBrowser> browser = self->GetBrowser();
if (browser.get())
browser->Reload();
}
// static
void RootWindowGtk::URLEntryActivate(GtkEntry* entry,
RootWindowGtk* self) {
CefRefPtr<CefBrowser> browser = self->GetBrowser();
if (browser.get()) {
const gchar* url = gtk_entry_get_text(entry);
browser->GetMainFrame()->LoadURL(std::string(url).c_str());
}
}
// static
gboolean RootWindowGtk::URLEntryButtonPress(GtkWidget* widget,
GdkEventButton* event,
RootWindowGtk* self) {
// Give focus to the GTK window. This is a work-around for bad focus-related
// interaction between the root window managed by GTK and the browser managed
// by X11.
GtkWidget* window = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);
GdkWindow* gdk_window = gtk_widget_get_window(window);
::Display* xdisplay = GDK_WINDOW_XDISPLAY(gdk_window);
::Window xwindow = GDK_WINDOW_XID(gdk_window);
// Retrieve the atoms required by the below XSendEvent call.
const char* kAtoms[] = {
"WM_PROTOCOLS",
"WM_TAKE_FOCUS"
};
Atom atoms[2];
int result = XInternAtoms(xdisplay, const_cast<char**>(kAtoms), 2, false,
atoms);
if (!result)
NOTREACHED();
XEvent e;
e.type = ClientMessage;
e.xany.display = xdisplay;
e.xany.window = xwindow;
e.xclient.format = 32;
e.xclient.message_type = atoms[0];
e.xclient.data.l[0] = atoms[1];
e.xclient.data.l[1] = CurrentTime;
e.xclient.data.l[2] = 0;
e.xclient.data.l[3] = 0;
e.xclient.data.l[4] = 0;
XSendEvent(xdisplay, xwindow, false, 0, &e);
return FALSE;
}
GtkWidget* RootWindowGtk::CreateMenuBar() {
GtkWidget* menu_bar = gtk_menu_bar_new();
// Create the test menu.
GtkWidget* test_menu = CreateMenu(menu_bar, "Tests");
AddMenuEntry(test_menu, "Get Source", ID_TESTS_GETSOURCE);
AddMenuEntry(test_menu, "Get Text", ID_TESTS_GETTEXT);
AddMenuEntry(test_menu, "New Window", ID_TESTS_WINDOW_NEW);
AddMenuEntry(test_menu, "Popup Window", ID_TESTS_WINDOW_POPUP);
AddMenuEntry(test_menu, "Request", ID_TESTS_REQUEST);
AddMenuEntry(test_menu, "Plugin Info", ID_TESTS_PLUGIN_INFO);
AddMenuEntry(test_menu, "Zoom In", ID_TESTS_ZOOM_IN);
AddMenuEntry(test_menu, "Zoom Out", ID_TESTS_ZOOM_OUT);
AddMenuEntry(test_menu, "Zoom Reset", ID_TESTS_ZOOM_RESET);
if (with_osr_) {
AddMenuEntry(test_menu, "Set FPS", ID_TESTS_OSR_FPS);
AddMenuEntry(test_menu, "Set Scale Factor", ID_TESTS_OSR_DSF);
}
AddMenuEntry(test_menu, "Begin Tracing", ID_TESTS_TRACING_BEGIN);
AddMenuEntry(test_menu, "End Tracing", ID_TESTS_TRACING_END);
AddMenuEntry(test_menu, "Print", ID_TESTS_PRINT);
AddMenuEntry(test_menu, "Print to PDF", ID_TESTS_PRINT_TO_PDF);
AddMenuEntry(test_menu, "Other Tests", ID_TESTS_OTHER_TESTS);
return menu_bar;
}
GtkWidget* RootWindowGtk::CreateMenu(GtkWidget* menu_bar, const char* text) {
GtkWidget* menu_widget = gtk_menu_new();
GtkWidget* menu_header = gtk_menu_item_new_with_label(text);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_header), menu_widget);
gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), menu_header);
return menu_widget;
}
GtkWidget* RootWindowGtk::AddMenuEntry(GtkWidget* menu_widget,
const char* text,
int id) {
GtkWidget* entry = gtk_menu_item_new_with_label(text);
g_signal_connect(entry, "activate",
G_CALLBACK(&RootWindowGtk::MenuItemActivated), this);
// Set the menu ID that will be retrieved in MenuItemActivated.
g_object_set_data(G_OBJECT(entry), kMenuIdKey, GINT_TO_POINTER(id));
gtk_menu_shell_append(GTK_MENU_SHELL(menu_widget), entry);
return entry;
}
// static
scoped_refptr<RootWindow> RootWindow::Create() {
return new RootWindowGtk();
}
} // namespace client