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
3 // found in the LICENSE file.
5 #include "ui/base/gtk/focus_store_gtk.h"
11 FocusStoreGtk::FocusStoreGtk()
13 destroy_handler_id_(0) {
16 FocusStoreGtk::~FocusStoreGtk() {
17 DisconnectDestroyHandler();
20 void FocusStoreGtk::Store(GtkWidget
* widget
) {
21 GtkWidget
* focus_widget
= NULL
;
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.
25 GtkWidget
* toplevel
= gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
);
26 GtkWindow
* window
= GTK_IS_WINDOW(toplevel
) ? GTK_WINDOW(toplevel
) : NULL
;
28 focus_widget
= gtk_window_get_focus(window
);
31 SetWidget(focus_widget
);
34 void FocusStoreGtk::SetWidget(GtkWidget
* widget
) {
35 DisconnectDestroyHandler();
37 // We don't add a ref. The signal handler below effectively gives us a weak
41 // When invoked, |gtk_widget_destroyed| will set |widget_| to NULL.
42 destroy_handler_id_
= g_signal_connect(widget_
, "destroy",
43 G_CALLBACK(gtk_widget_destroyed
),
48 void FocusStoreGtk::DisconnectDestroyHandler() {
50 g_signal_handler_disconnect(widget_
, destroy_handler_id_
);