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
|
// Copyright (c) 2012 The Chromium 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 "chrome/browser/ui/panels/panel_browser_window_gtk.h"
#include "base/bind.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/gtk/browser_titlebar.h"
#include "chrome/browser/ui/panels/panel.h"
#include "chrome/browser/ui/panels/panel_bounds_animation.h"
#include "chrome/browser/ui/panels/panel_manager.h"
#include "chrome/browser/ui/panels/panel_strip.h"
#include "chrome/browser/ui/panels/panel_settings_menu_model.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_service.h"
#include "ui/base/dragdrop/gtk_dnd_util.h"
#include "ui/base/x/work_area_watcher_x.h"
using content::WebContents;
namespace {
// RGB values for titlebar in draw attention state. A shade of orange.
const int kDrawAttentionR = 0xfa;
const int kDrawAttentionG = 0x98;
const int kDrawAttentionB = 0x3a;
const float kDrawAttentionRFraction = kDrawAttentionR / 255.0;
const float kDrawAttentionGFraction = kDrawAttentionG / 255.0;
const float kDrawAttentionBFraction = kDrawAttentionB / 255.0;
// Delay before click on a titlebar is allowed to minimize the panel after
// the 'draw attention' mode has been cleared.
const int kSuspendMinimizeOnClickIntervalMs = 500;
// Markup for title text in draw attention state. Set to color white.
const char* const kDrawAttentionTitleMarkupPrefix =
"<span fgcolor='#ffffff'>";
const char* const kDrawAttentionTitleMarkupSuffix = "</span>";
// Set minimium width for window really small so it can be reduced to icon only
// size in overflow expansion state.
const int kMinWindowWidth = 2;
}
NativePanel* Panel::CreateNativePanel(Browser* browser, Panel* panel,
const gfx::Rect& bounds) {
PanelBrowserWindowGtk* panel_browser_window_gtk =
new PanelBrowserWindowGtk(browser, panel, bounds);
panel_browser_window_gtk->Init();
return panel_browser_window_gtk;
}
PanelBrowserWindowGtk::PanelBrowserWindowGtk(Browser* browser,
Panel* panel,
const gfx::Rect& bounds)
: BrowserWindowGtk(browser),
system_drag_disabled_for_testing_(false),
last_mouse_down_(NULL),
drag_widget_(NULL),
drag_end_factory_(this),
panel_(panel),
bounds_(bounds),
window_size_known_(false),
is_drawing_attention_(false),
window_has_mouse_(false),
show_close_button_(true) {
}
PanelBrowserWindowGtk::~PanelBrowserWindowGtk() {
}
void PanelBrowserWindowGtk::Init() {
BrowserWindowGtk::Init();
// Keep the window always on top.
gtk_window_set_keep_above(window(), TRUE);
// Show the window on all the virtual desktops.
gtk_window_stick(window());
// Do not show an icon in the task bar. Window operations such as close,
// minimize etc. can only be done from the panel UI.
gtk_window_set_skip_taskbar_hint(window(), TRUE);
g_signal_connect(titlebar_widget(), "button-press-event",
G_CALLBACK(OnTitlebarButtonPressEventThunk), this);
g_signal_connect(titlebar_widget(), "button-release-event",
G_CALLBACK(OnTitlebarButtonReleaseEventThunk), this);
g_signal_connect(window_, "enter-notify-event",
G_CALLBACK(OnEnterNotifyThunk), this);
g_signal_connect(window_, "leave-notify-event",
G_CALLBACK(OnLeaveNotifyThunk), this);
ui::WorkAreaWatcherX::AddObserver(this);
registrar_.Add(
this,
chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE,
content::Source<Panel>(panel_.get()));
registrar_.Add(
this,
chrome::NOTIFICATION_PANEL_CHANGED_LAYOUT_MODE,
content::Source<Panel>(panel_.get()));
registrar_.Add(
this,
chrome::NOTIFICATION_WINDOW_CLOSED,
content::Source<GtkWindow>(window()));
}
bool PanelBrowserWindowGtk::GetWindowEdge(int x, int y, GdkWindowEdge* edge) {
// Since panels are not resizable or movable by the user, we should not
// detect the window edge for behavioral purposes. The edge, if any,
// is present only for visual aspects.
return FALSE;
}
bool PanelBrowserWindowGtk::HandleTitleBarLeftMousePress(
GdkEventButton* event,
guint32 last_click_time,
gfx::Point last_click_position) {
// In theory we should never enter this function as we have a handler for
// button press on titlebar where we handle this. Not putting NOTREACHED()
// here because we don't want to crash if hit-testing for titlebar in window
// button press handler in BrowserWindowGtk is off by a pixel or two.
DLOG(WARNING) << "Hit-testing for titlebar off by a pixel or two?";
return TRUE;
}
void PanelBrowserWindowGtk::SaveWindowPosition() {
// We don't save window position for panels as it's controlled by
// PanelManager.
return;
}
void PanelBrowserWindowGtk::SetGeometryHints() {
// Set minimum height the window can be set to.
GdkGeometry hints;
hints.min_height = Panel::kMinimizedPanelHeight;
hints.min_width = kMinWindowWidth;
gtk_window_set_geometry_hints(
window(), GTK_WIDGET(window()), &hints, GDK_HINT_MIN_SIZE);
DCHECK(!window_size_known_);
}
void PanelBrowserWindowGtk::SetBounds(const gfx::Rect& bounds) {
// This should never be called.
DLOG(WARNING) << "Unexpected call to PanelBrowserWindowGtk::SetBounds()";
}
void PanelBrowserWindowGtk::OnSizeChanged(int width, int height) {
BrowserWindowGtk::OnSizeChanged(width, height);
if (window_size_known_)
return;
window_size_known_ = true;
int top = bounds_.bottom() - height;
int left = bounds_.right() - width;
gtk_window_move(window_, left, top);
StartBoundsAnimation(gfx::Rect(left, top, width, height), bounds_);
panel_->OnWindowSizeAvailable();
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PANEL_WINDOW_SIZE_KNOWN,
content::Source<Panel>(panel_.get()),
content::NotificationService::NoDetails());
}
bool PanelBrowserWindowGtk::UseCustomFrame() {
// We always use custom frame for panels.
return TRUE;
}
void PanelBrowserWindowGtk::ShowSettingsMenu(GtkWidget* widget,
GdkEventButton* event) {
if (!settings_menu_.get()) {
settings_menu_model_.reset(new PanelSettingsMenuModel(panel_.get()));
settings_menu_.reset(new MenuGtk(this, settings_menu_model_.get()));
}
settings_menu_->PopupForWidget(widget, event->button, event->time);
}
void PanelBrowserWindowGtk::DrawPopupFrame(cairo_t* cr,
GtkWidget* widget,
GdkEventExpose* event) {
BrowserWindowGtk::DrawPopupFrame(cr, widget, event);
if (is_drawing_attention_)
DrawAttentionFrame(cr, widget, event);
}
void PanelBrowserWindowGtk::DrawCustomFrame(cairo_t* cr,
GtkWidget* widget,
GdkEventExpose* event) {
BrowserWindowGtk::DrawCustomFrame(cr, widget, event);
if (is_drawing_attention_)
DrawAttentionFrame(cr, widget, event);
}
void PanelBrowserWindowGtk::DrawAttentionFrame(cairo_t* cr,
GtkWidget* widget,
GdkEventExpose* event) {
cairo_set_source_rgb(cr, kDrawAttentionRFraction,
kDrawAttentionGFraction,
kDrawAttentionBFraction);
GdkRectangle dest_rectangle = GetTitlebarRectForDrawAttention();
GdkRegion* dest_region = gdk_region_rectangle(&dest_rectangle);
gdk_region_intersect(dest_region, event->region);
gdk_cairo_region(cr, dest_region);
cairo_clip(cr);
cairo_paint(cr);
gdk_region_destroy(dest_region);
}
void PanelBrowserWindowGtk::ActiveWindowChanged(GdkWindow* active_window) {
bool was_active = IsActive();
BrowserWindowGtk::ActiveWindowChanged(active_window);
if (!window() || was_active == IsActive()) // State didn't change.
return;
if ((IsActive() || window_has_mouse_) &&
panel_->panel_strip()->type() != PanelStrip::IN_OVERFLOW) {
titlebar()->ShowPanelWrenchButton();
} else {
titlebar()->HidePanelWrenchButton();
}
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PANEL_CHANGED_ACTIVE_STATUS,
content::Source<Panel>(panel_.get()),
content::NotificationService::NoDetails());
}
BrowserWindowGtk::TitleDecoration PanelBrowserWindowGtk::GetWindowTitle(
std::string* title) const {
if (is_drawing_attention_) {
std::string title_original;
BrowserWindowGtk::TitleDecoration title_decoration =
BrowserWindowGtk::GetWindowTitle(&title_original);
DCHECK_EQ(BrowserWindowGtk::PLAIN_TEXT, title_decoration);
gchar* title_escaped = g_markup_escape_text(title_original.c_str(), -1);
gchar* title_with_markup = g_strconcat(kDrawAttentionTitleMarkupPrefix,
title_escaped,
kDrawAttentionTitleMarkupSuffix,
NULL);
*title = title_with_markup;
g_free(title_escaped);
g_free(title_with_markup);
return BrowserWindowGtk::PANGO_MARKUP;
} else {
return BrowserWindowGtk::GetWindowTitle(title);
}
}
bool PanelBrowserWindowGtk::ShouldShowCloseButton() const {
return show_close_button_;
}
void PanelBrowserWindowGtk::WorkAreaChanged() {
panel_->manager()->OnDisplayChanged();
}
void PanelBrowserWindowGtk::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_PANEL_CHANGED_LAYOUT_MODE:
titlebar()->UpdateCustomFrame(true);
// No break. Accept focus code should execute for both cases.
case chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE:
gtk_window_set_accept_focus(window(), !panel_->IsMinimized());
break;
case chrome::NOTIFICATION_WINDOW_CLOSED:
// Cleanup.
if (bounds_animator_.get())
bounds_animator_.reset();
CleanupDragDrop();
if (drag_widget_) {
// Terminate the grab if we have it. We could do this using any widget,
// |drag_widget_| is just convenient.
gtk_grab_add(drag_widget_);
gtk_grab_remove(drag_widget_);
DestroyDragWidget();
}
panel_->OnNativePanelClosed();
ui::WorkAreaWatcherX::RemoveObserver(this);
break;
}
BrowserWindowGtk::Observe(type, source, details);
}
void PanelBrowserWindowGtk::ShowPanel() {
Show();
}
void PanelBrowserWindowGtk::ShowPanelInactive() {
ShowInactive();
}
gfx::Rect PanelBrowserWindowGtk::GetPanelBounds() const {
return bounds_;
}
void PanelBrowserWindowGtk::SetPanelBounds(const gfx::Rect& bounds) {
SetBoundsInternal(bounds, true);
}
void PanelBrowserWindowGtk::SetPanelBoundsInstantly(const gfx::Rect& bounds) {
SetBoundsInternal(bounds, false);
}
void PanelBrowserWindowGtk::SetBoundsInternal(const gfx::Rect& bounds,
bool animate) {
if (bounds == bounds_)
return;
if (drag_widget_) {
DCHECK(!IsAnimatingBounds());
// If the current panel is being dragged, it should just move with the
// user drag, we should not animate.
gtk_window_move(window(), bounds.x(), bounds.y());
} else {
bool old_show_close_button = show_close_button_;
show_close_button_ =
bounds.width() > panel_->manager()->overflow_strip_width();
if (show_close_button_ != old_show_close_button)
titlebar()->UpdateCustomFrame(true);
if (!bounds_.width() || !bounds_.height()) {
// If the old bounds are 0 in either dimension, we need to show the
// window as it would now be hidden.
gtk_widget_show(GTK_WIDGET(window()));
gdk_window_move(gtk_widget_get_window(GTK_WIDGET(window())), bounds_.x(),
bounds_.y());
}
if (!animate) {
if (bounds_.origin() != bounds.origin())
gtk_window_move(window(), bounds.x(), bounds.y());
if (bounds_.size() != bounds.size())
ResizeWindow(bounds.width(), bounds.height());
} else if (window_size_known_) {
StartBoundsAnimation(bounds_, bounds);
}
}
// If window size is not known, wait till the size is known before starting
// the animation.
bounds_ = bounds;
}
void PanelBrowserWindowGtk::ResizeWindow(int width, int height) {
// Gtk does not allow window size to be set to 0, so make it 1 if it's 0.
gtk_window_resize(window(), std::max(width, 1), std::max(height, 1));
}
void PanelBrowserWindowGtk::ClosePanel() {
Close();
}
void PanelBrowserWindowGtk::ActivatePanel() {
Activate();
}
void PanelBrowserWindowGtk::DeactivatePanel() {
BrowserWindow* browser_window =
panel_->manager()->GetNextBrowserWindowToActivate(panel_.get());
if (browser_window) {
browser_window->Activate();
} else {
Deactivate();
}
}
bool PanelBrowserWindowGtk::IsPanelActive() const {
return IsActive();
}
gfx::NativeWindow PanelBrowserWindowGtk::GetNativePanelHandle() {
return GetNativeHandle();
}
void PanelBrowserWindowGtk::UpdatePanelTitleBar() {
UpdateTitleBar();
}
void PanelBrowserWindowGtk::UpdatePanelLoadingAnimations(bool should_animate) {
UpdateLoadingAnimations(should_animate);
}
void PanelBrowserWindowGtk::ShowTaskManagerForPanel() {
ShowTaskManager();
}
FindBar* PanelBrowserWindowGtk::CreatePanelFindBar() {
return CreateFindBar();
}
void PanelBrowserWindowGtk::NotifyPanelOnUserChangedTheme() {
UserChangedTheme();
}
void PanelBrowserWindowGtk::PanelWebContentsFocused(WebContents* contents) {
WebContentsFocused(contents);
}
void PanelBrowserWindowGtk::PanelCut() {
Cut();
}
void PanelBrowserWindowGtk::PanelCopy() {
Copy();
}
void PanelBrowserWindowGtk::PanelPaste() {
Paste();
}
void PanelBrowserWindowGtk::DrawAttention(bool draw_attention) {
if (is_drawing_attention_ == draw_attention)
return;
is_drawing_attention_ = draw_attention;
GdkRectangle rect = GetTitlebarRectForDrawAttention();
gdk_window_invalidate_rect(
gtk_widget_get_window(GTK_WIDGET(window())), &rect, TRUE);
UpdateTitleBar();
}
bool PanelBrowserWindowGtk::IsDrawingAttention() const {
return is_drawing_attention_;
}
bool PanelBrowserWindowGtk::PreHandlePanelKeyboardEvent(
const NativeWebKeyboardEvent& event,
bool* is_keyboard_shortcut) {
return PreHandleKeyboardEvent(event, is_keyboard_shortcut);
}
void PanelBrowserWindowGtk::FullScreenModeChanged(bool is_full_screen) {
// Nothing to do here as z-order rules for panels ensures that they're below
// any app running in full screen mode.
}
void PanelBrowserWindowGtk::HandlePanelKeyboardEvent(
const NativeWebKeyboardEvent& event) {
HandleKeyboardEvent(event);
}
Browser* PanelBrowserWindowGtk::GetPanelBrowser() const {
return browser();
}
void PanelBrowserWindowGtk::DestroyPanelBrowser() {
DestroyBrowser();
}
gfx::Size PanelBrowserWindowGtk::IconOnlySize() const {
GtkAllocation allocation;
gtk_widget_get_allocation(titlebar_widget(), &allocation);
return gfx::Size(titlebar()->IconOnlyWidth(), allocation.height);
}
void PanelBrowserWindowGtk::EnsurePanelFullyVisible() {
gtk_window_present(window());
}
void PanelBrowserWindowGtk::SetPanelAppIconVisibility(bool visible) {
return;
}
gfx::Size PanelBrowserWindowGtk::WindowSizeFromContentSize(
const gfx::Size& content_size) const {
gfx::Size frame = GetNonClientFrameSize();
return gfx::Size(content_size.width() + frame.width(),
content_size.height() + frame.height());
}
gfx::Size PanelBrowserWindowGtk::ContentSizeFromWindowSize(
const gfx::Size& window_size) const {
gfx::Size frame = GetNonClientFrameSize();
return gfx::Size(window_size.width() - frame.width(),
window_size.height() - frame.height());
}
int PanelBrowserWindowGtk::TitleOnlyHeight() const {
GtkAllocation allocation;
gtk_widget_get_allocation(titlebar_widget(), &allocation);
return allocation.height;
}
void PanelBrowserWindowGtk::StartBoundsAnimation(
const gfx::Rect& from_bounds, const gfx::Rect& to_bounds) {
animation_start_bounds_ = IsAnimatingBounds() ?
last_animation_progressed_bounds_ : from_bounds;
bounds_animator_.reset(new PanelBoundsAnimation(
this, panel_.get(), animation_start_bounds_, to_bounds));
bounds_animator_->Start();
last_animation_progressed_bounds_ = animation_start_bounds_;
}
bool PanelBrowserWindowGtk::IsAnimatingBounds() const {
return bounds_animator_.get() && bounds_animator_->is_animating();
}
void PanelBrowserWindowGtk::WillProcessEvent(GdkEvent* event) {
// Nothing to do.
}
void PanelBrowserWindowGtk::DidProcessEvent(GdkEvent* event) {
DCHECK(last_mouse_down_);
if (event->type != GDK_MOTION_NOTIFY || !panel_->draggable())
return;
gdouble new_x_double;
gdouble new_y_double;
gdouble old_x_double;
gdouble old_y_double;
gdk_event_get_root_coords(event, &new_x_double, &new_y_double);
gdk_event_get_root_coords(last_mouse_down_, &old_x_double, &old_y_double);
gint new_x = static_cast<gint>(new_x_double);
gint new_y = static_cast<gint>(new_y_double);
gint old_x = static_cast<gint>(old_x_double);
gint old_y = static_cast<gint>(old_y_double);
if (!drag_widget_ &&
!IsAnimatingBounds() &&
gtk_drag_check_threshold(titlebar_widget(), old_x,
old_y, new_x, new_y)) {
CreateDragWidget();
if (!system_drag_disabled_for_testing_) {
GtkTargetList* list = ui::GetTargetListFromCodeMask(ui::CHROME_TAB);
gtk_drag_begin(drag_widget_, list, GDK_ACTION_MOVE, 1, last_mouse_down_);
// gtk_drag_begin increments reference count for GtkTargetList. So unref
// it here to reduce the reference count.
gtk_target_list_unref(list);
}
panel_->manager()->StartDragging(panel_.get());
}
if (drag_widget_) {
panel_->manager()->Drag(new_x - old_x, new_y - old_y);
gdk_event_free(last_mouse_down_);
last_mouse_down_ = gdk_event_copy(event);
}
}
void PanelBrowserWindowGtk::AnimationEnded(const ui::Animation* animation) {
titlebar()->SendEnterNotifyToCloseButtonIfUnderMouse();
// If the final size is 0 in either dimension, hide the window as gtk does
// not allow the window size to be 0.
if (!bounds_.width() || !bounds_.height())
gtk_widget_hide(GTK_WIDGET(window()));
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PANEL_BOUNDS_ANIMATIONS_FINISHED,
content::Source<Panel>(panel_.get()),
content::NotificationService::NoDetails());
}
void PanelBrowserWindowGtk::AnimationProgressed(
const ui::Animation* animation) {
DCHECK(!drag_widget_);
DCHECK(window_size_known_);
gfx::Rect new_bounds = bounds_animator_->CurrentValueBetween(
animation_start_bounds_, bounds_);
gdk_window_move_resize(gtk_widget_get_window(GTK_WIDGET(window())),
new_bounds.x(), new_bounds.y(),
std::max(new_bounds.width(), 1), // 0 not allowed
std::max(new_bounds.height(), 1)); // 0 not allowed
last_animation_progressed_bounds_ = new_bounds;
}
void PanelBrowserWindowGtk::CreateDragWidget() {
DCHECK(!drag_widget_);
drag_widget_ = gtk_invisible_new();
g_signal_connect_after(drag_widget_, "drag-begin",
G_CALLBACK(OnDragBeginThunk), this);
g_signal_connect(drag_widget_, "drag-failed",
G_CALLBACK(OnDragFailedThunk), this);
g_signal_connect(drag_widget_, "button-release-event",
G_CALLBACK(OnDragButtonReleasedThunk), this);
}
void PanelBrowserWindowGtk::DestroyDragWidget() {
if (drag_widget_) {
gtk_widget_destroy(drag_widget_);
drag_widget_ = NULL;
}
}
void PanelBrowserWindowGtk::EndDrag(bool canceled) {
if (!system_drag_disabled_for_testing_)
DCHECK(drag_widget_);
// Make sure we only run EndDrag once by canceling any tasks that want
// to call EndDrag.
drag_end_factory_.InvalidateWeakPtrs();
CleanupDragDrop();
if (drag_widget_) {
// We must let gtk clean up after we handle the drag operation, otherwise
// there will be outstanding references to the drag widget when we try to
// destroy it.
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&PanelBrowserWindowGtk::DestroyDragWidget,
drag_end_factory_.GetWeakPtr()));
panel_->manager()->EndDragging(canceled);
}
}
void PanelBrowserWindowGtk::CleanupDragDrop() {
if (last_mouse_down_) {
MessageLoopForUI::current()->RemoveObserver(this);
gdk_event_free(last_mouse_down_);
last_mouse_down_ = NULL;
}
}
GdkRectangle PanelBrowserWindowGtk::GetTitlebarRectForDrawAttention() const {
GdkRectangle rect;
rect.x = 0;
rect.y = 0;
// We get the window width and not the titlebar_widget() width because we'd
// like for the window borders on either side of the title bar to be the same
// color.
GtkAllocation window_allocation;
gtk_widget_get_allocation(GTK_WIDGET(window()), &window_allocation);
rect.width = window_allocation.width;
GtkAllocation titlebar_allocation;
gtk_widget_get_allocation(titlebar_widget(), &titlebar_allocation);
rect.height = titlebar_allocation.height;
return rect;
}
gboolean PanelBrowserWindowGtk::OnTitlebarButtonPressEvent(
GtkWidget* widget, GdkEventButton* event) {
// Early return if animation in progress.
if (IsAnimatingBounds())
return TRUE;
// Every button press ensures either a button-release-event or a drag-fail
// signal for |widget|.
if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
// Store the button press event, used to initiate a drag.
DCHECK(!last_mouse_down_);
last_mouse_down_ = gdk_event_copy(reinterpret_cast<GdkEvent*>(event));
MessageLoopForUI::current()->AddObserver(this);
}
return TRUE;
}
gboolean PanelBrowserWindowGtk::OnTitlebarButtonReleaseEvent(
GtkWidget* widget, GdkEventButton* event) {
if (event->button != 1) {
DCHECK(!last_mouse_down_);
return TRUE;
}
CleanupDragDrop();
if (panel_->panel_strip()->type() == PanelStrip::DOCKED &&
panel_->expansion_state() == Panel::EXPANDED) {
if (base::Time::Now() < disableMinimizeUntilTime_)
return TRUE;
panel_->SetExpansionState(Panel::MINIMIZED);
} else {
panel_->Activate();
}
return TRUE;
}
void PanelBrowserWindowGtk::HandleFocusIn(GtkWidget* widget,
GdkEventFocus* event) {
BrowserWindowGtk::HandleFocusIn(widget, event);
if (!is_drawing_attention_)
return;
DrawAttention(false);
DCHECK(panel_->expansion_state() == Panel::EXPANDED);
disableMinimizeUntilTime_ = base::Time::Now() +
base::TimeDelta::FromMilliseconds(kSuspendMinimizeOnClickIntervalMs);
}
void PanelBrowserWindowGtk::OnDragBegin(GtkWidget* widget,
GdkDragContext* context) {
// Set drag icon to be a transparent pixbuf.
GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
gdk_pixbuf_fill(pixbuf, 0);
gtk_drag_set_icon_pixbuf(context, pixbuf, 0, 0);
g_object_unref(pixbuf);
}
gboolean PanelBrowserWindowGtk::OnDragFailed(
GtkWidget* widget, GdkDragContext* context, GtkDragResult result) {
bool canceled = (result != GTK_DRAG_RESULT_NO_TARGET);
EndDrag(canceled);
return TRUE;
}
gboolean PanelBrowserWindowGtk::OnDragButtonReleased(GtkWidget* widget,
GdkEventButton* button) {
// We always get this event when gtk is releasing the grab and ending the
// drag. This gets fired before drag-failed handler gets fired. However,
// if the user ended the drag with space or enter, we don't get a follow up
// event to tell us the drag has finished (either a drag-failed or a
// drag-end). We post a task, instead of calling EndDrag right here, to give
// GTK+ a chance to send the drag-failed event with the right status. If
// GTK+ does send the drag-failed event, we cancel the task.
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&PanelBrowserWindowGtk::EndDrag,
drag_end_factory_.GetWeakPtr(),
false));
return TRUE;
}
gboolean PanelBrowserWindowGtk::OnEnterNotify(GtkWidget* widget,
GdkEventCrossing* event) {
// Ignore if entered from a child widget.
if (event->detail == GDK_NOTIFY_INFERIOR)
return FALSE;
if (window() && panel_->panel_strip()->type() != PanelStrip::IN_OVERFLOW)
titlebar()->ShowPanelWrenchButton();
window_has_mouse_ = true;
return FALSE;
}
gboolean PanelBrowserWindowGtk::OnLeaveNotify(GtkWidget* widget,
GdkEventCrossing* event) {
// Ignore if left towards a child widget.
if (event->detail == GDK_NOTIFY_INFERIOR)
return FALSE;
if (window_ && !IsActive())
titlebar()->HidePanelWrenchButton();
window_has_mouse_ = false;
return FALSE;
}
// NativePanelTesting implementation.
class NativePanelTestingGtk : public NativePanelTesting {
public:
explicit NativePanelTestingGtk(
PanelBrowserWindowGtk* panel_browser_window_gtk);
private:
virtual void PressLeftMouseButtonTitlebar(
const gfx::Point& point) OVERRIDE;
virtual void ReleaseMouseButtonTitlebar() OVERRIDE;
virtual void DragTitlebar(int delta_x, int delta_y) OVERRIDE;
virtual void CancelDragTitlebar() OVERRIDE;
virtual void FinishDragTitlebar() OVERRIDE;
virtual bool VerifyDrawingAttention() const OVERRIDE;
virtual bool VerifyActiveState(bool is_active) OVERRIDE;
virtual void WaitForWindowCreationToComplete() const OVERRIDE;
virtual bool IsWindowSizeKnown() const OVERRIDE;
virtual bool IsAnimatingBounds() const OVERRIDE;
PanelBrowserWindowGtk* panel_browser_window_gtk_;
};
// static
NativePanelTesting* NativePanelTesting::Create(NativePanel* native_panel) {
return new NativePanelTestingGtk(static_cast<PanelBrowserWindowGtk*>(
native_panel));
}
NativePanelTestingGtk::NativePanelTestingGtk(
PanelBrowserWindowGtk* panel_browser_window_gtk) :
panel_browser_window_gtk_(panel_browser_window_gtk) {
}
void NativePanelTestingGtk::PressLeftMouseButtonTitlebar(
const gfx::Point& point) {
// If there is an animation, wait for it to finish as we don't handle button
// clicks while animation is in progress.
while (panel_browser_window_gtk_->IsAnimatingBounds())
MessageLoopForUI::current()->RunAllPending();
GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS);
event->button.button = 1;
event->button.x_root = point.x();
event->button.y_root = point.y();
panel_browser_window_gtk_->OnTitlebarButtonPressEvent(
panel_browser_window_gtk_->titlebar_widget(),
reinterpret_cast<GdkEventButton*>(event));
gdk_event_free(event);
MessageLoopForUI::current()->RunAllPending();
}
void NativePanelTestingGtk::ReleaseMouseButtonTitlebar() {
GdkEvent* event = gdk_event_new(GDK_BUTTON_RELEASE);
event->button.button = 1;
panel_browser_window_gtk_->OnTitlebarButtonReleaseEvent(
panel_browser_window_gtk_->titlebar_widget(),
reinterpret_cast<GdkEventButton*>(event));
MessageLoopForUI::current()->RunAllPending();
}
void NativePanelTestingGtk::DragTitlebar(int delta_x, int delta_y) {
// Prevent extra unwanted signals and focus grabs.
panel_browser_window_gtk_->system_drag_disabled_for_testing_ = true;
GdkEvent* event = gdk_event_new(GDK_MOTION_NOTIFY);
gdk_event_get_root_coords(panel_browser_window_gtk_->last_mouse_down_,
&event->motion.x_root, &event->motion.y_root);
event->motion.x_root += delta_x;
event->motion.y_root += delta_y;
panel_browser_window_gtk_->DidProcessEvent(event);
gdk_event_free(event);
MessageLoopForUI::current()->RunAllPending();
}
void NativePanelTestingGtk::CancelDragTitlebar() {
panel_browser_window_gtk_->OnDragFailed(
panel_browser_window_gtk_->drag_widget_, NULL,
GTK_DRAG_RESULT_USER_CANCELLED);
MessageLoopForUI::current()->RunAllPending();
}
void NativePanelTestingGtk::FinishDragTitlebar() {
panel_browser_window_gtk_->OnDragFailed(
panel_browser_window_gtk_->drag_widget_, NULL,
GTK_DRAG_RESULT_NO_TARGET);
MessageLoopForUI::current()->RunAllPending();
}
bool NativePanelTestingGtk::VerifyDrawingAttention() const {
std::string title;
BrowserWindowGtk::TitleDecoration decoration =
panel_browser_window_gtk_->GetWindowTitle(&title);
return panel_browser_window_gtk_->IsDrawingAttention() &&
decoration == BrowserWindowGtk::PANGO_MARKUP;
}
bool NativePanelTestingGtk::VerifyActiveState(bool is_active) {
// TODO(jianli): to be implemented. http://crbug.com/102737
return false;
}
void NativePanelTestingGtk::WaitForWindowCreationToComplete() const {
while (!panel_browser_window_gtk_->window_size_known_)
MessageLoopForUI::current()->RunAllPending();
while (panel_browser_window_gtk_->IsAnimatingBounds())
MessageLoopForUI::current()->RunAllPending();
}
bool NativePanelTestingGtk::IsWindowSizeKnown() const {
return panel_browser_window_gtk_->window_size_known_;
}
bool NativePanelTestingGtk::IsAnimatingBounds() const {
return panel_browser_window_gtk_->IsAnimatingBounds();
}
|