summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/extension_shelf_gtk.cc
blob: 437f91f838246c7293200d01047ed372d59a6a54 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2009 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/extension_shelf_gtk.h"

#include "base/gfx/gtk_util.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/gtk/browser_window_gtk.h"
#include "chrome/browser/gtk/gtk_theme_provider.h"
#include "chrome/browser/gtk/view_id_util.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_service.h"
#include "grit/app_resources.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"

// Preferred height of the ExtensionShelfGtk.
static const int kShelfHeight = 29;

static const int kToolstripPadding = 2;

class ExtensionShelfGtk::Toolstrip {
 public:
  explicit Toolstrip(ExtensionHost* host)
      : host_(host),
        extension_name_(host_->extension()->name()) {
    DCHECK(host_->view());
    Init();
  }

  void AddToolstripToBox(GtkWidget* box);
  void RemoveToolstripFromBox(GtkWidget* box);

  void SetBackground(const SkBitmap& background) {
    host_->view()->SetBackground(background);
  }

 private:
  void Init();

  gfx::NativeView native_view() {
    return host_->view()->native_view();
  }

  ExtensionHost* host_;

  const std::string extension_name_;

 private:
  DISALLOW_COPY_AND_ASSIGN(Toolstrip);
};

void ExtensionShelfGtk::Toolstrip::AddToolstripToBox(GtkWidget* box) {
  gtk_box_pack_start(GTK_BOX(box), native_view(), FALSE, FALSE,
                     kToolstripPadding);
}

void ExtensionShelfGtk::Toolstrip::RemoveToolstripFromBox(GtkWidget* box) {
  gtk_container_remove(GTK_CONTAINER(box), native_view());
}

void ExtensionShelfGtk::Toolstrip::Init() {
  host_->view()->set_is_toolstrip(true);
  gtk_widget_show_all(native_view());
}

ExtensionShelfGtk::ExtensionShelfGtk(Profile* profile, Browser* browser)
    : browser_(browser),
      theme_provider_(GtkThemeProvider::GetFrom(profile)),
      model_(browser->extension_shelf_model()) {
  Init(profile);
}

ExtensionShelfGtk::~ExtensionShelfGtk() {
  if (model_)
    model_->RemoveObserver(this);
  event_box_.Destroy();
}

void ExtensionShelfGtk::Show() {
  gtk_widget_show_all(event_box_.get());
}

void ExtensionShelfGtk::Hide() {
  gtk_widget_hide(event_box_.get());
}

void ExtensionShelfGtk::ToolstripInsertedAt(ExtensionHost* host,
                                            int index) {
  InitBackground();
  Toolstrip* toolstrip = new Toolstrip(host);
  toolstrip->SetBackground(*background_.get());
  toolstrip->AddToolstripToBox(shelf_hbox_);
  toolstrips_.insert(toolstrip);
  model_->SetToolstripDataAt(index, toolstrip);

  AdjustHeight();
}

void ExtensionShelfGtk::ToolstripRemovingAt(ExtensionHost* host,
                                            int index) {
  Toolstrip* toolstrip = ToolstripAtIndex(index);
  toolstrip->RemoveToolstripFromBox(shelf_hbox_);
  toolstrips_.erase(toolstrip);
  model_->SetToolstripDataAt(index, NULL);
  delete toolstrip;

  AdjustHeight();
}

void ExtensionShelfGtk::ToolstripMoved(ExtensionHost* host,
                                       int from_index,
                                       int to_index) {
  // TODO(phajdan.jr): Implement reordering toolstrips.
  AdjustHeight();
}

void ExtensionShelfGtk::ToolstripChanged(
    ExtensionShelfModel::iterator toolstrip) {
  // TODO(phajdan.jr): Implement changing toolstrips.
  AdjustHeight();
}

void ExtensionShelfGtk::ExtensionShelfEmpty() {
  AdjustHeight();
}

void ExtensionShelfGtk::ShelfModelReloaded() {
  for (std::set<Toolstrip*>::iterator iter = toolstrips_.begin();
       iter != toolstrips_.end(); ++iter) {
    (*iter)->RemoveToolstripFromBox(shelf_hbox_);
    delete *iter;
  }
  toolstrips_.clear();
  LoadFromModel();
}

void ExtensionShelfGtk::ShelfModelDeleting() {
  for (std::set<Toolstrip*>::iterator iter = toolstrips_.begin();
       iter != toolstrips_.end(); ++iter) {
    (*iter)->RemoveToolstripFromBox(shelf_hbox_);
    delete *iter;
  }
  toolstrips_.clear();

  model_->RemoveObserver(this);
  model_ = NULL;
}

void ExtensionShelfGtk::Observe(NotificationType type,
                                const NotificationSource& source,
                                const NotificationDetails& details) {
  DCHECK(type == NotificationType::BROWSER_THEME_CHANGED);
  GdkColor color = theme_provider_->GetBorderColor();
  gtk_widget_modify_bg(top_border_, GTK_STATE_NORMAL, &color);

  color = theme_provider_->GetGdkColor(BrowserThemeProvider::COLOR_TOOLBAR);
  gtk_widget_modify_bg(event_box_.get(), GTK_STATE_NORMAL, &color);

  // Reset the background images on all the individual toolstrips
  background_.reset();
  InitBackground();
  for (std::set<Toolstrip*>::iterator it = toolstrips_.begin();
       it != toolstrips_.end(); ++it) {
    (*it)->SetBackground(*background_.get());
  }
}

void ExtensionShelfGtk::Init(Profile* profile) {
  top_border_ = gtk_event_box_new();
  gtk_widget_set_size_request(GTK_WIDGET(top_border_), 0, 1);

  // The event box provides a background for the shelf and is its top-level
  // widget.
  event_box_.Own(gtk_event_box_new());
  ViewIDUtil::SetID(event_box_.get(), VIEW_ID_DEV_EXTENSION_SHELF);

  shelf_hbox_ = gtk_hbox_new(FALSE, 0);

  GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), top_border_, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), shelf_hbox_, FALSE, FALSE, 0);

  gtk_container_add(GTK_CONTAINER(event_box_.get()), vbox);

  theme_provider_->InitThemesFor(this);
  registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
                 NotificationService::AllSources());

  LoadFromModel();
  model_->AddObserver(this);
}

void ExtensionShelfGtk::InitBackground() {
  if (background_.get())
    return;

  GdkColor color =
      theme_provider_->GetGdkColor(BrowserThemeProvider::COLOR_TOOLBAR);

  background_.reset(new SkBitmap);
  background_->setConfig(SkBitmap::kARGB_8888_Config, 3, 3);
  background_->allocPixels();
  background_->eraseRGB(color.red >> 8,
                        color.green >> 8,
                        color.blue >> 8);
}

void ExtensionShelfGtk::AdjustHeight() {
  if (model_->empty() || toolstrips_.empty()) {
    // It's possible that |model_| is not empty, but |toolstrips_| are empty
    // when removing the last toolstrip.
    DCHECK(toolstrips_.empty());
    Hide();
  } else {
    gtk_widget_set_size_request(shelf_hbox_, -1, kShelfHeight);
    Show();
  }
}

void ExtensionShelfGtk::LoadFromModel() {
  DCHECK(toolstrips_.empty());
  int count = model_->count();
  for (int i = 0; i < count; ++i)
    ToolstripInsertedAt(model_->ToolstripAt(i).host, i);
  AdjustHeight();
}

ExtensionShelfGtk::Toolstrip* ExtensionShelfGtk::ToolstripAtIndex(int index) {
  return static_cast<Toolstrip*>(model_->ToolstripAt(index).data);
}