summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/toolbar_star_toggle_gtk.cc
blob: 77c5732c9895799c8be2c4bfb7021324630ec8c6 (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
// 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/toolbar_star_toggle_gtk.h"

#include "app/resource_bundle.h"
#include "base/gfx/rect.h"
#include "chrome/browser/gtk/bookmark_bubble_gtk.h"
#include "chrome/browser/gtk/browser_toolbar_gtk.h"
#include "grit/theme_resources.h"

ToolbarStarToggleGtk::ToolbarStarToggleGtk(BrowserToolbarGtk* host)
    : host_(host),
      widget_(gtk_button_new()),
      is_starred_(false),
      unstarred_(IDR_STAR, IDR_STAR_P, IDR_STAR_H, IDR_STAR_D),
      starred_(IDR_STARRED, IDR_STARRED_P, IDR_STARRED_H, 0) {
  gtk_widget_set_size_request(widget_.get(),
                             gdk_pixbuf_get_width(unstarred_.pixbufs(0)),
                             gdk_pixbuf_get_height(unstarred_.pixbufs(0)));

  gtk_widget_set_app_paintable(widget_.get(), TRUE);
  // We effectively double-buffer by virtue of having only one image...
  gtk_widget_set_double_buffered(widget_.get(), FALSE);
  g_signal_connect(G_OBJECT(widget_.get()), "expose-event",
                   G_CALLBACK(OnExpose), this);
  GTK_WIDGET_UNSET_FLAGS(widget_.get(), GTK_CAN_FOCUS);
}

ToolbarStarToggleGtk::~ToolbarStarToggleGtk() {
  widget_.Destroy();
}

void ToolbarStarToggleGtk::ShowStarBubble(const GURL& url,
                                          bool newly_bookmarked) {
  GtkWidget* widget = widget_.get();
  gint x, y;
  gdk_window_get_origin(widget->window, &x, &y);
  x += widget->allocation.x;
  y += widget->allocation.y;
  gint width = widget->allocation.width;
  gint height = widget->allocation.height;

  BookmarkBubbleGtk::Show(GTK_WINDOW(gtk_widget_get_toplevel(widget)),
                          gfx::Rect(x, y, width, height),
                          host_->profile(),
                          url,
                          newly_bookmarked);
}

void ToolbarStarToggleGtk::SetStarred(bool starred) {
  is_starred_ = starred;
  gtk_widget_queue_draw(widget_.get());
}

// static
gboolean ToolbarStarToggleGtk::OnExpose(GtkWidget* widget, GdkEventExpose* e,
                                        ToolbarStarToggleGtk* button) {
  if (button->is_starred_) {
    return button->starred_.OnExpose(widget, e);
  } else {
    return button->unstarred_.OnExpose(widget, e);
  }
}