window_switcher_button.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
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/chromeos/status/window_switcher_button.h"
6
7#include "chrome/browser/chromeos/status/status_area_host.h"
8#include "chrome/browser/chromeos/wm_ipc.h"
9#include "chrome/browser/ui/browser_window.h"
10#include "grit/generated_resources.h"
11#include "grit/theme_resources.h"
12#include "ui/base/l10n/l10n_util.h"
13#include "ui/base/resource/resource_bundle.h"
14#include "ui/gfx/canvas.h"
15
16namespace chromeos {
17
18namespace {
19int GetNormalBrowserCount() {
20  int count = 0;
21  BrowserList::const_iterator iter;
22  for (iter = BrowserList::begin(); iter != BrowserList::end(); ++iter) {
23    if ((*iter)->type() == Browser::TYPE_NORMAL)
24      count++;
25  }
26  return count;
27}
28}
29
30WindowSwitcherButton::WindowSwitcherButton(StatusAreaHost* host)
31    : StatusAreaButton(host, this) {
32  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
33  SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WINDOW_SWITCHER));
34  SetEnabled(true);
35
36  UpdateStatus();
37  BrowserList::AddObserver(this);
38}
39
40WindowSwitcherButton::~WindowSwitcherButton() {
41  BrowserList::RemoveObserver(this);
42}
43
44void WindowSwitcherButton::UpdateStatus() {
45  if (GetNormalBrowserCount() < 2) {
46    // There are less than two browsers.  This means we can't switch
47    // anywhere, so we disappear.
48    SetTooltipText(L"");
49    SetVisible(false);
50    PreferredSizeChanged();
51    return;
52  }
53
54  SetTooltipText(UTF16ToWide(l10n_util::GetStringUTF16(
55      IDS_STATUSBAR_WINDOW_SWITCHER_TOOLTIP)));
56
57  // There are at least two browsers, so we show ourselves.
58  SetVisible(true);
59  PreferredSizeChanged();
60}
61
62////////////////////////////////////////////////////////////////////////////////
63// WindowSwitcherButton, views::ViewMenuDelegate implementation:
64
65void WindowSwitcherButton::RunMenu(views::View* source, const gfx::Point& pt) {
66  // Don't do anything if there aren't at least two normal browsers to
67  // switch between.
68  if (GetNormalBrowserCount() < 2)
69    return;
70
71  // Send a message to the ChromeOS window manager to switch to the
72  // next top level browser window.  Only the window manager knows
73  // what order they are in, so we can't use Chrome's browser list
74  // to decide.
75  WmIpc::Message message(chromeos::WM_IPC_MESSAGE_WM_CYCLE_WINDOWS);
76  message.set_param(0, true);
77  WmIpc::instance()->SendMessage(message);
78}
79
80////////////////////////////////////////////////////////////////////////////////
81// BrowserList::Observer implementation:
82
83// Called immediately after a browser is added to the list
84void WindowSwitcherButton::OnBrowserAdded(const Browser* browser) {
85  UpdateStatus();
86}
87
88// Called immediately after a browser is removed from the list
89void WindowSwitcherButton::OnBrowserRemoved(const Browser* browser) {
90  UpdateStatus();
91}
92
93}  // namespace chromeos
94