OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/focus_store_gtk.h" | 5 #include "chrome/browser/gtk/focus_store_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "chrome/browser/platform_util.h" | 10 #include "chrome/browser/platform_util.h" |
11 | 11 |
12 FocusStoreGtk::FocusStoreGtk() | 12 FocusStoreGtk::FocusStoreGtk() |
13 : widget_(NULL), | 13 : widget_(NULL), |
14 destroy_handler_id_(0) { | 14 destroy_handler_id_(0) { |
15 } | 15 } |
16 | 16 |
17 FocusStoreGtk::~FocusStoreGtk() { | 17 FocusStoreGtk::~FocusStoreGtk() { |
18 DisconnectDestroyHandler(); | 18 DisconnectDestroyHandler(); |
19 } | 19 } |
20 | 20 |
21 void FocusStoreGtk::Store(GtkWidget* widget) { | 21 void FocusStoreGtk::Store(GtkWidget* widget) { |
22 DisconnectDestroyHandler(); | 22 GtkWidget* focus_widget = NULL; |
23 if (!widget) { | 23 if (widget) { |
24 widget_ = NULL; | 24 GtkWindow* window = platform_util::GetTopLevel(widget); |
25 return; | 25 if (window) |
| 26 focus_widget = window->focus_widget; |
26 } | 27 } |
27 | 28 |
28 GtkWindow* window = platform_util::GetTopLevel(widget); | 29 SetWidget(focus_widget); |
29 if (!window) { | 30 } |
30 widget_ = NULL; | |
31 return; | |
32 } | |
33 | 31 |
34 widget_ = window->focus_widget; | 32 void FocusStoreGtk::SetWidget(GtkWidget* widget) { |
| 33 DisconnectDestroyHandler(); |
| 34 |
| 35 // We don't add a ref. The signal handler below effectively gives us a weak |
| 36 // reference. |
| 37 widget_ = widget; |
35 if (widget_) { | 38 if (widget_) { |
36 // When invoked, |gtk_widget_destroyed| will set |widget_| to NULL. | 39 // When invoked, |gtk_widget_destroyed| will set |widget_| to NULL. |
37 destroy_handler_id_ = g_signal_connect(widget_, "destroy", | 40 destroy_handler_id_ = g_signal_connect(widget_, "destroy", |
38 G_CALLBACK(gtk_widget_destroyed), | 41 G_CALLBACK(gtk_widget_destroyed), |
39 &widget_); | 42 &widget_); |
40 } | 43 } |
41 } | 44 } |
42 | 45 |
43 void FocusStoreGtk::DisconnectDestroyHandler() { | 46 void FocusStoreGtk::DisconnectDestroyHandler() { |
44 if (widget_) | 47 if (widget_) { |
45 g_signal_handler_disconnect(widget_, destroy_handler_id_); | 48 g_signal_handler_disconnect(widget_, destroy_handler_id_); |
| 49 widget_ = NULL; |
| 50 } |
46 } | 51 } |
OLD | NEW |