summaryrefslogtreecommitdiffstats
path: root/ui/base/gtk/focus_store_gtk.cc
blob: b98787c30b0df0fed6ea3e8a3e58d41b4282c612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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