OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/gtk/constrained_window_gtk.h" | 5 #include "chrome/browser/gtk/constrained_window_gtk.h" |
6 | 6 |
7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
8 | 8 |
9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
10 #include "chrome/browser/gtk/gtk_util.h" | 10 #include "chrome/browser/gtk/gtk_util.h" |
11 #include "chrome/browser/tab_contents/tab_contents.h" | 11 #include "chrome/browser/tab_contents/tab_contents.h" |
12 #include "chrome/browser/tab_contents/tab_contents_view_gtk.h" | 12 #include "chrome/browser/tab_contents/tab_contents_view_gtk.h" |
13 | 13 |
14 ConstrainedWindowGtk::ConstrainedWindowGtk( | 14 ConstrainedWindowGtk::ConstrainedWindowGtk( |
15 TabContents* owner, ConstrainedWindowGtkDelegate* delegate) | 15 TabContents* owner, ConstrainedWindowGtkDelegate* delegate) |
16 : owner_(owner), | 16 : owner_(owner), |
17 delegate_(delegate), | 17 delegate_(delegate), |
18 visible_(false), | 18 visible_(false) { |
19 accel_group_(gtk_accel_group_new()) { | |
20 DCHECK(owner); | 19 DCHECK(owner); |
21 DCHECK(delegate); | 20 DCHECK(delegate); |
22 GtkWidget* dialog = delegate->GetWidgetRoot(); | 21 GtkWidget* dialog = delegate->GetWidgetRoot(); |
23 | 22 |
24 // Unlike other users of CreateBorderBin, we need a dedicated frame around | 23 // Unlike other users of CreateBorderBin, we need a dedicated frame around |
25 // our "window". | 24 // our "window". |
26 GtkWidget* ebox = gtk_event_box_new(); | 25 GtkWidget* ebox = gtk_event_box_new(); |
27 GtkWidget* frame = gtk_frame_new(NULL); | 26 GtkWidget* frame = gtk_frame_new(NULL); |
28 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); | 27 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); |
29 GtkWidget* alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0); | 28 GtkWidget* alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0); |
30 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), | 29 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), |
31 gtk_util::kContentAreaBorder, gtk_util::kContentAreaBorder, | 30 gtk_util::kContentAreaBorder, gtk_util::kContentAreaBorder, |
32 gtk_util::kContentAreaBorder, gtk_util::kContentAreaBorder); | 31 gtk_util::kContentAreaBorder, gtk_util::kContentAreaBorder); |
33 gtk_container_add(GTK_CONTAINER(alignment), dialog); | 32 gtk_container_add(GTK_CONTAINER(alignment), dialog); |
34 gtk_container_add(GTK_CONTAINER(frame), alignment); | 33 gtk_container_add(GTK_CONTAINER(frame), alignment); |
35 gtk_container_add(GTK_CONTAINER(ebox), frame); | 34 gtk_container_add(GTK_CONTAINER(ebox), frame); |
36 border_.Own(ebox); | 35 border_.Own(ebox); |
37 ConnectAccelerators(); | 36 |
| 37 gtk_widget_add_events(widget(), GDK_KEY_PRESS_MASK); |
| 38 g_signal_connect(widget(), "key-press-event", G_CALLBACK(OnKeyPressThunk), |
| 39 this); |
38 } | 40 } |
39 | 41 |
40 ConstrainedWindowGtk::~ConstrainedWindowGtk() { | 42 ConstrainedWindowGtk::~ConstrainedWindowGtk() { |
41 border_.Destroy(); | 43 border_.Destroy(); |
42 | |
43 gtk_accel_group_disconnect_key(accel_group_, GDK_Escape, | |
44 static_cast<GdkModifierType>(0)); | |
45 if (ContainingView() && ContainingView()->GetTopLevelNativeWindow()) { | |
46 gtk_window_remove_accel_group( | |
47 GTK_WINDOW(ContainingView()->GetTopLevelNativeWindow()), | |
48 accel_group_); | |
49 } | |
50 g_object_unref(accel_group_); | |
51 } | 44 } |
52 | 45 |
53 void ConstrainedWindowGtk::ShowConstrainedWindow() { | 46 void ConstrainedWindowGtk::ShowConstrainedWindow() { |
54 gtk_widget_show_all(border_.get()); | 47 gtk_widget_show_all(border_.get()); |
55 | 48 |
56 // We collaborate with TabContentsViewGtk and stick ourselves in the | 49 // We collaborate with TabContentsViewGtk and stick ourselves in the |
57 // TabContentsViewGtk's floating container. | 50 // TabContentsViewGtk's floating container. |
58 ContainingView()->AttachConstrainedWindow(this); | 51 ContainingView()->AttachConstrainedWindow(this); |
59 | 52 |
60 visible_ = true; | 53 visible_ = true; |
61 } | 54 } |
62 | 55 |
63 void ConstrainedWindowGtk::CloseConstrainedWindow() { | 56 void ConstrainedWindowGtk::CloseConstrainedWindow() { |
64 if (visible_) | 57 if (visible_) |
65 ContainingView()->RemoveConstrainedWindow(this); | 58 ContainingView()->RemoveConstrainedWindow(this); |
66 delegate_->DeleteDelegate(); | 59 delegate_->DeleteDelegate(); |
67 owner_->WillClose(this); | 60 owner_->WillClose(this); |
68 | 61 |
69 delete this; | 62 // Let the stack unwind so any relevant event handler can release its ref |
| 63 // on widget(). |
| 64 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
70 } | 65 } |
71 | 66 |
72 TabContentsViewGtk* ConstrainedWindowGtk::ContainingView() { | 67 TabContentsViewGtk* ConstrainedWindowGtk::ContainingView() { |
73 return static_cast<TabContentsViewGtk*>(owner_->view()); | 68 return static_cast<TabContentsViewGtk*>(owner_->view()); |
74 } | 69 } |
75 | 70 |
76 void ConstrainedWindowGtk::ConnectAccelerators() { | 71 gboolean ConstrainedWindowGtk::OnKeyPress(GtkWidget* sender, |
77 gtk_accel_group_connect(accel_group_, | 72 GdkEventKey* key) { |
78 GDK_Escape, static_cast<GdkModifierType>(0), | 73 if (key->keyval == GDK_Escape) { |
79 static_cast<GtkAccelFlags>(0), | 74 CloseConstrainedWindow(); |
80 g_cclosure_new(G_CALLBACK(OnEscapeThunk), | 75 return TRUE; |
81 this, NULL)); | 76 } |
82 gtk_window_add_accel_group( | |
83 GTK_WINDOW(ContainingView()->GetTopLevelNativeWindow()), | |
84 accel_group_); | |
85 } | |
86 | 77 |
87 | 78 return FALSE; |
88 gboolean ConstrainedWindowGtk::OnEscape(GtkAccelGroup* group, | |
89 GObject* acceleratable, | |
90 guint keyval, | |
91 GdkModifierType modifier) { | |
92 // Handle this accelerator only if this is on the currently selected tab. | |
93 Browser* browser = BrowserList::GetLastActive(); | |
94 if (!browser || browser->GetSelectedTabContents() != owner_) | |
95 return FALSE; | |
96 | |
97 CloseConstrainedWindow(); | |
98 return TRUE; | |
99 } | 79 } |
100 | 80 |
101 // static | 81 // static |
102 ConstrainedWindow* ConstrainedWindow::CreateConstrainedDialog( | 82 ConstrainedWindow* ConstrainedWindow::CreateConstrainedDialog( |
103 TabContents* parent, | 83 TabContents* parent, |
104 ConstrainedWindowGtkDelegate* delegate) { | 84 ConstrainedWindowGtkDelegate* delegate) { |
105 return new ConstrainedWindowGtk(parent, delegate); | 85 return new ConstrainedWindowGtk(parent, delegate); |
106 } | 86 } |
107 | |
OLD | NEW |