OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/base/gtk/focus_store_gtk.h" | 5 #include "ui/base/gtk/focus_store_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 namespace ui { | 9 namespace ui { |
10 | 10 |
11 FocusStoreGtk::FocusStoreGtk() | 11 FocusStoreGtk::FocusStoreGtk() |
12 : widget_(NULL), | 12 : widget_(NULL), |
13 destroy_handler_id_(0) { | 13 destroy_handler_id_(0) { |
14 } | 14 } |
15 | 15 |
16 FocusStoreGtk::~FocusStoreGtk() { | 16 FocusStoreGtk::~FocusStoreGtk() { |
17 DisconnectDestroyHandler(); | 17 DisconnectDestroyHandler(); |
18 } | 18 } |
19 | 19 |
20 void FocusStoreGtk::Store(GtkWidget* widget) { | 20 void FocusStoreGtk::Store(GtkWidget* widget) { |
21 GtkWidget* focus_widget = NULL; | 21 GtkWidget* focus_widget = NULL; |
22 if (widget) { | 22 if (widget) { |
23 // A detached widget won't have a toplevel window as an ancestor, so we | 23 // A detached widget won't have a toplevel window as an ancestor, so we |
24 // can't assume that the query for toplevel will return a window. | 24 // can't assume that the query for toplevel will return a window. |
25 GtkWidget* toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW); | 25 GtkWidget* toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW); |
26 GtkWindow* window = GTK_IS_WINDOW(toplevel) ? GTK_WINDOW(toplevel) : NULL; | 26 GtkWindow* window = GTK_IS_WINDOW(toplevel) ? GTK_WINDOW(toplevel) : NULL; |
27 if (window) | 27 if (window) |
28 focus_widget = window->focus_widget; | 28 focus_widget = gtk_window_get_focus(window); |
29 } | 29 } |
30 | 30 |
31 SetWidget(focus_widget); | 31 SetWidget(focus_widget); |
32 } | 32 } |
33 | 33 |
34 void FocusStoreGtk::SetWidget(GtkWidget* widget) { | 34 void FocusStoreGtk::SetWidget(GtkWidget* widget) { |
35 DisconnectDestroyHandler(); | 35 DisconnectDestroyHandler(); |
36 | 36 |
37 // We don't add a ref. The signal handler below effectively gives us a weak | 37 // We don't add a ref. The signal handler below effectively gives us a weak |
38 // reference. | 38 // reference. |
39 widget_ = widget; | 39 widget_ = widget; |
40 if (widget_) { | 40 if (widget_) { |
41 // When invoked, |gtk_widget_destroyed| will set |widget_| to NULL. | 41 // When invoked, |gtk_widget_destroyed| will set |widget_| to NULL. |
42 destroy_handler_id_ = g_signal_connect(widget_, "destroy", | 42 destroy_handler_id_ = g_signal_connect(widget_, "destroy", |
43 G_CALLBACK(gtk_widget_destroyed), | 43 G_CALLBACK(gtk_widget_destroyed), |
44 &widget_); | 44 &widget_); |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 void FocusStoreGtk::DisconnectDestroyHandler() { | 48 void FocusStoreGtk::DisconnectDestroyHandler() { |
49 if (widget_) { | 49 if (widget_) { |
50 g_signal_handler_disconnect(widget_, destroy_handler_id_); | 50 g_signal_handler_disconnect(widget_, destroy_handler_id_); |
51 widget_ = NULL; | 51 widget_ = NULL; |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 } // namespace ui | 55 } // namespace ui |
OLD | NEW |