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