diff options
author | erg@chromium.org <erg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-22 19:47:30 +0000 |
---|---|---|
committer | erg@chromium.org <erg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-22 19:47:30 +0000 |
commit | 735d947c3e1abc6787e159354dbba5d0472502fd (patch) | |
tree | 676dfdb7a3a0c217bc9ed51a338e92da1c259cdc /ui/base | |
parent | 3e7502c979ee67073edd878696275a84847ff85b (diff) | |
download | chromium_src-735d947c3e1abc6787e159354dbba5d0472502fd.zip chromium_src-735d947c3e1abc6787e159354dbba5d0472502fd.tar.gz chromium_src-735d947c3e1abc6787e159354dbba5d0472502fd.tar.bz2 |
GTK: Move code shared by future content/ classes and chrome/ to ui/.
BUG=93804
TEST=none
Review URL: http://codereview.chromium.org/8490029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111193 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base')
-rw-r--r-- | ui/base/gtk/focus_store_gtk.cc | 55 | ||||
-rw-r--r-- | ui/base/gtk/focus_store_gtk.h | 47 |
2 files changed, 102 insertions, 0 deletions
diff --git a/ui/base/gtk/focus_store_gtk.cc b/ui/base/gtk/focus_store_gtk.cc new file mode 100644 index 0000000..b98787c --- /dev/null +++ b/ui/base/gtk/focus_store_gtk.cc @@ -0,0 +1,55 @@ +// Copyright (c) 2011 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 "ui/base/gtk/focus_store_gtk.h" + +#include <gtk/gtk.h> + +namespace ui { + +FocusStoreGtk::FocusStoreGtk() + : widget_(NULL), + destroy_handler_id_(0) { +} + +FocusStoreGtk::~FocusStoreGtk() { + DisconnectDestroyHandler(); +} + +void FocusStoreGtk::Store(GtkWidget* widget) { + GtkWidget* focus_widget = NULL; + if (widget) { + // A detached widget won't have a toplevel window as an ancestor, so we + // can't assume that the query for toplevel will return a window. + GtkWidget* toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW); + GtkWindow* window = GTK_IS_WINDOW(toplevel) ? GTK_WINDOW(toplevel) : NULL; + if (window) + focus_widget = window->focus_widget; + } + + SetWidget(focus_widget); +} + +void FocusStoreGtk::SetWidget(GtkWidget* widget) { + DisconnectDestroyHandler(); + + // We don't add a ref. The signal handler below effectively gives us a weak + // reference. + widget_ = widget; + if (widget_) { + // When invoked, |gtk_widget_destroyed| will set |widget_| to NULL. + destroy_handler_id_ = g_signal_connect(widget_, "destroy", + G_CALLBACK(gtk_widget_destroyed), + &widget_); + } +} + +void FocusStoreGtk::DisconnectDestroyHandler() { + if (widget_) { + g_signal_handler_disconnect(widget_, destroy_handler_id_); + widget_ = NULL; + } +} + +} // namespace ui diff --git a/ui/base/gtk/focus_store_gtk.h b/ui/base/gtk/focus_store_gtk.h new file mode 100644 index 0000000..e237ee1 --- /dev/null +++ b/ui/base/gtk/focus_store_gtk.h @@ -0,0 +1,47 @@ +// Copyright (c) 2011 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. + +#ifndef UI_BASE_GTK_FOCUS_STORE_GTK_H_ +#define UI_BASE_GTK_FOCUS_STORE_GTK_H_ +#pragma once + +#include "base/basictypes.h" +#include "ui/base/ui_export.h" + +typedef struct _GtkWidget GtkWidget; + +namespace ui { + +class UI_EXPORT FocusStoreGtk { + public: + FocusStoreGtk(); + ~FocusStoreGtk(); + + GtkWidget* widget() const { return widget_; } + + // Save the widget that is currently focused for |widget|'s toplevel (NOT + // |widget|). + void Store(GtkWidget* widget); + + // Save |widget| as the focus widget. Call with NULL to clear |widget_|. + void SetWidget(GtkWidget* widget); + + private: + // Disconnect the previous destroy handler (if any). + void DisconnectDestroyHandler(); + + // The widget which last had focus. + GtkWidget* widget_; + + // The widget for which we've stored focus might be destroyed by the time we + // want to restore focus. Thus we connect to the "destroy" signal on that + // widget. This is the ID for the destroy handler. + unsigned int destroy_handler_id_; + + DISALLOW_COPY_AND_ASSIGN(FocusStoreGtk); +}; + +} // namespace ui + +#endif // UI_BASE_GTK_FOCUS_STORE_GTK_H_ |