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/status_area_view.h"
6
7 #include <algorithm>
8
9 #include "chrome/browser/chromeos/status/clock_menu_button.h"
10 #include "chrome/browser/chromeos/status/input_method_menu_button.h"
11 #include "chrome/browser/chromeos/status/network_menu_button.h"
12 #include "chrome/browser/chromeos/status/power_menu_button.h"
13 #include "chrome/browser/chromeos/status/status_area_host.h"
14 #include "chrome/browser/chromeos/status/window_switcher_button.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/canvas.h"
18 #include "views/border.h"
19 #include "views/controls/image_view.h"
20
21 namespace chromeos {
22
23 // Number of pixels to separate each icon.
24 const int kSeparation = 5;
25
StatusAreaView(StatusAreaHost * host)26 StatusAreaView::StatusAreaView(StatusAreaHost* host)
27 : host_(host),
28 clock_view_(NULL),
29 input_method_view_(NULL),
30 network_view_(NULL),
31 power_view_(NULL),
32 window_switcher_view_(NULL) {
33 }
34
Init()35 void StatusAreaView::Init() {
36 // Clock.
37 clock_view_ = new ClockMenuButton(host_);
38 clock_view_->set_border(views::Border::CreateEmptyBorder(0, 1, 0, 0));
39 AddChildView(clock_view_);
40
41 // InputMethod.
42 input_method_view_ = new InputMethodMenuButton(host_);
43 AddChildView(input_method_view_);
44
45 // Network.
46 network_view_ = new NetworkMenuButton(host_);
47 AddChildView(network_view_);
48
49 // Power.
50 power_view_ = new PowerMenuButton(host_);
51 AddChildView(power_view_);
52
53 // Window Switcher.
54 window_switcher_view_ = new WindowSwitcherButton(host_);
55 AddChildView(window_switcher_view_);
56 }
57
GetPreferredSize()58 gfx::Size StatusAreaView::GetPreferredSize() {
59 int result_w = kSeparation;
60 int result_h = 0;
61 int visible_count = 0;
62 for (int i = 0; i < child_count(); i++) {
63 views::View* cur = GetChildViewAt(i);
64 if (cur->IsVisible()) {
65 visible_count++;
66 gfx::Size cur_size = cur->GetPreferredSize();
67 // Add each width.
68 result_w += cur_size.width() + kSeparation;
69 // Use max height.
70 result_h = std::max(result_h, cur_size.height());
71 }
72 }
73 if (visible_count > 0)
74 result_w -= kSeparation;
75 return gfx::Size(result_w, result_h);
76 }
77
Layout()78 void StatusAreaView::Layout() {
79 int cur_x = kSeparation;
80 for (int i = 0; i < child_count(); i++) {
81 views::View* cur = GetChildViewAt(i);
82 if (cur->IsVisible()) {
83 gfx::Size cur_size = cur->GetPreferredSize();
84 int cur_y = (height() - cur_size.height()) / 2;
85
86 // Handle odd number of pixels.
87 cur_y += (height() - cur_size.height()) % 2;
88
89 // Put next in row horizontally, and center vertically.
90 cur->SetBounds(cur_x, cur_y, cur_size.width(), cur_size.height());
91
92 if (cur_size.width() > 0)
93 cur_x += cur_size.width() + kSeparation;
94 }
95 }
96 }
97
ChildPreferredSizeChanged(View * child)98 void StatusAreaView::ChildPreferredSizeChanged(View* child) {
99 // When something like the clock menu button's size changes, we need to
100 // relayout. Also mark that this view's size has changed. This will let
101 // BrowserView know to relayout, which will reset the bounds of this view.
102 Layout();
103 PreferredSizeChanged();
104 }
105
MakeButtonsActive(bool active)106 void StatusAreaView::MakeButtonsActive(bool active) {
107 clock_view()->set_active(active);
108 input_method_view()->set_active(active);
109 network_view()->set_active(active);
110 power_view()->set_active(active);
111 }
112
113 } // namespace chromeos
114