summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/view_id_util.cc
blob: 2b8e39601909ca38287639e3b009b6e21259be8c (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2010 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 "chrome/browser/gtk/view_id_util.h"

#include <stdint.h>
#include <string>

#include <gtk/gtk.h>

#include "base/logging.h"

namespace {

const char kViewIDString[] = "__VIEW_ID__";
const char kViewIDOverrideString[] = "__VIEW_ID_OVERRIDE__";

struct ViewIDSearchStruct {
  ViewID id;  // Input: the ID we are searching for.
  GtkWidget* widget;  // Output: the found widget, or NULL.
};

// Recursively search for the given view ID among the children of |widget|.
void SearchForWidgetWithViewID(GtkWidget* widget, gpointer data) {
  ViewIDSearchStruct* search_struct =
      reinterpret_cast<ViewIDSearchStruct*>(data);

  // The widget has already been found; abort the search.
  if (search_struct->widget)
    return;

  // Check if the widget defines its own ID function.
  ViewIDUtil::Delegate* delegate = reinterpret_cast<ViewIDUtil::Delegate*>(
      g_object_get_data(G_OBJECT(widget), kViewIDOverrideString));
  if (delegate) {
    search_struct->widget = delegate->GetWidgetForViewID(search_struct->id);
    // If there was success, return.
    if (search_struct->widget)
      return;
  }

  // Otherwise check the g_object data.
  int widget_id =
      reinterpret_cast<intptr_t>(g_object_get_data(G_OBJECT(widget),
                                 kViewIDString));
  if (search_struct->id == widget_id) {
    // Success; set |widget| and return.
    search_struct->widget = widget;
    return;
  }

  // Recurse.
  if (GTK_IS_CONTAINER(widget)) {
    gtk_container_foreach(GTK_CONTAINER(widget),
                          SearchForWidgetWithViewID, data);
  }
}

const char* GetNameFromID(ViewID id) {
  switch (id) {
    case VIEW_ID_TAB_STRIP:
      return "chrome-tab-strip";

    case VIEW_ID_TOOLBAR:
      return "chrome-toolbar";

    case VIEW_ID_BACK_BUTTON:
      return "chrome-toolbar-back-button";

    case VIEW_ID_FORWARD_BUTTON:
      return "chrome-toolbar-forward-button";

    case VIEW_ID_RELOAD_BUTTON:
      return "chrome-toolbar-reload-button";

    case VIEW_ID_HOME_BUTTON:
      return "chrome-toolbar-home-button";

    case VIEW_ID_STAR_BUTTON:
      return "chrome-toolbar-star-button";

    case VIEW_ID_LOCATION_BAR:
      return "chrome-location-bar";

    case VIEW_ID_BROWSER_ACTION_TOOLBAR:
      return "chrome-toolbar-browser-actions-container";

    case VIEW_ID_APP_MENU:
      return "chrome-app-menu";

    case VIEW_ID_AUTOCOMPLETE:
      return "chrome-autocomplete-edit";

    case VIEW_ID_BOOKMARK_BAR:
      return "chrome-bookmark-bar";

    case VIEW_ID_OTHER_BOOKMARKS:
      return "chrome-bookmark-bar-other-bookmarks";

    case VIEW_ID_FIND_IN_PAGE_TEXT_FIELD:
      return "chrome-find-in-page-entry";

    case VIEW_ID_FIND_IN_PAGE:
      return "chrome-find-in-page";

    case VIEW_ID_DEV_TOOLS_DOCKED:
      return "chrome-dev-tools-docked";

    // These are never hit because the tab container uses the delegate to
    // set its ID.
    case VIEW_ID_TAB_CONTAINER:
    case VIEW_ID_TAB_CONTAINER_FOCUS_VIEW:
    default:
      NOTREACHED() << "If you added a new VIEW_ID, please provide "
                      "a name for the widget.";
      return NULL;
  }
}

}  // namespace

void ViewIDUtil::SetID(GtkWidget* widget, ViewID id) {
  const char* name = GetNameFromID(id);
  if (name)
    gtk_widget_set_name(widget, name);
  g_object_set_data(G_OBJECT(widget), kViewIDString,
                    reinterpret_cast<void*>(id));
}

GtkWidget* ViewIDUtil::GetWidget(GtkWidget* root, ViewID id) {
  ViewIDSearchStruct search_struct = { id, NULL };
  SearchForWidgetWithViewID(root, &search_struct);
  return search_struct.widget;
}

void ViewIDUtil::SetDelegateForWidget(GtkWidget* widget, Delegate* delegate) {
  g_object_set_data(G_OBJECT(widget), kViewIDOverrideString, delegate);
}