| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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/views/frame/status_area_view.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "app/gfx/canvas.h" |
| 10 #include "app/gfx/font.h" |
| 11 #include "app/l10n_util.h" |
| 12 #include "app/resource_bundle.h" |
| 13 #include "app/theme_provider.h" |
| 14 #include "base/string_util.h" |
| 15 #include "base/time.h" |
| 16 #include "base/timer.h" |
| 17 #include "chrome/app/chrome_dll_resource.h" |
| 18 #include "chrome/browser/browser.h" |
| 19 #include "chrome/browser/profile.h" |
| 20 #include "grit/chromium_strings.h" |
| 21 #include "grit/generated_resources.h" |
| 22 #include "grit/theme_resources.h" |
| 23 #include "views/controls/button/menu_button.h" |
| 24 #include "views/controls/image_view.h" |
| 25 #include "views/controls/menu/menu.h" |
| 26 #include "views/controls/menu/simple_menu_model.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Number of pixels to separate adjacent status items. |
| 31 const int kStatusItemSeparation = 1; |
| 32 |
| 33 class ClockView : public views::View { |
| 34 public: |
| 35 ClockView(); |
| 36 virtual ~ClockView(); |
| 37 |
| 38 // views::View* overrides. |
| 39 virtual gfx::Size GetPreferredSize(); |
| 40 virtual void Paint(gfx::Canvas* canvas); |
| 41 |
| 42 private: |
| 43 // Schedules the timer to fire at the next minute interval. |
| 44 void SetNextTimer(); |
| 45 |
| 46 // Schedules a paint when the timer goes off. |
| 47 void OnTimer(); |
| 48 |
| 49 gfx::Font font_; |
| 50 |
| 51 base::OneShotTimer<ClockView> timer_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(ClockView); |
| 54 }; |
| 55 |
| 56 // Amount of slop to add into the timer to make sure we're into the next minute |
| 57 // when the timer goes off. |
| 58 const int kTimerSlopSeconds = 1; |
| 59 |
| 60 ClockView::ClockView() |
| 61 : font_(ResourceBundle::GetSharedInstance().GetFont( |
| 62 ResourceBundle::BaseFont)) { |
| 63 SetNextTimer(); |
| 64 } |
| 65 |
| 66 ClockView::~ClockView() { |
| 67 } |
| 68 |
| 69 gfx::Size ClockView::GetPreferredSize() { |
| 70 return gfx::Size(40, 10); |
| 71 } |
| 72 |
| 73 void ClockView::Paint(gfx::Canvas* canvas) { |
| 74 base::Time now = base::Time::Now(); |
| 75 base::Time::Exploded now_exploded; |
| 76 now.LocalExplode(&now_exploded); |
| 77 |
| 78 std::wstring time_string = StringPrintf(L"%d:%d", |
| 79 now_exploded.hour, |
| 80 now_exploded.minute); |
| 81 canvas->DrawStringInt(time_string, font_, SK_ColorWHITE, 0, 0, |
| 82 width(), height(), gfx::Canvas::TEXT_ALIGN_CENTER); |
| 83 } |
| 84 |
| 85 void ClockView::SetNextTimer() { |
| 86 // Try to set the timer to go off at the next change of the minute. We don't |
| 87 // want to have the timer go off more than necessary since that will cause |
| 88 // the CPU to wake up and consume power. |
| 89 base::Time now = base::Time::Now(); |
| 90 base::Time::Exploded exploded; |
| 91 now.LocalExplode(&exploded); |
| 92 |
| 93 // Often this will be called at minute boundaries, and we'll actually want |
| 94 // 60 seconds from now. |
| 95 int seconds_left = 60 - exploded.second; |
| 96 if (seconds_left == 0) |
| 97 seconds_left = 60; |
| 98 |
| 99 // Make sure that the timer fires on the next minute. Without this, if it is |
| 100 // called just a teeny bit early, then it will skip the next minute. |
| 101 seconds_left += kTimerSlopSeconds; |
| 102 |
| 103 timer_.Start(base::TimeDelta::FromSeconds(seconds_left), |
| 104 this, &ClockView::OnTimer); |
| 105 } |
| 106 |
| 107 void ClockView::OnTimer() { |
| 108 SchedulePaint(); |
| 109 SetNextTimer(); |
| 110 } |
| 111 |
| 112 } // namespace |
| 113 |
| 114 StatusAreaView::StatusAreaView(Browser* browser) |
| 115 : browser_(browser), |
| 116 battery_view_(NULL), |
| 117 menu_view_(NULL) { |
| 118 } |
| 119 |
| 120 StatusAreaView::~StatusAreaView() { |
| 121 } |
| 122 |
| 123 void StatusAreaView::Init() { |
| 124 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); |
| 125 |
| 126 // Battery. |
| 127 battery_view_ = new views::ImageView; |
| 128 battery_view_->SetImage( |
| 129 resource_bundle.GetBitmapNamed(IDR_STATUSBAR_BATTERY)); |
| 130 AddChildView(battery_view_); |
| 131 |
| 132 // Clock. |
| 133 AddChildView(new ClockView); |
| 134 |
| 135 // Menu. |
| 136 menu_view_ = new views::MenuButton(NULL, std::wstring(), this, false); |
| 137 menu_view_->SetIcon(*resource_bundle.GetBitmapNamed(IDR_STATUSBAR_MENU)); |
| 138 AddChildView(menu_view_); |
| 139 } |
| 140 |
| 141 gfx::Size StatusAreaView::GetPreferredSize() { |
| 142 int result_w = kStatusItemSeparation; // Left border. |
| 143 int result_h = 0; |
| 144 for (int i = 0; i < GetChildViewCount(); i++) { |
| 145 gfx::Size cur_size = GetChildViewAt(i)->GetPreferredSize(); |
| 146 result_w += cur_size.width() + kStatusItemSeparation; |
| 147 result_h = std::max(result_h, cur_size.height()); |
| 148 } |
| 149 result_w -= kStatusItemSeparation; // Don't have space after the last one. |
| 150 |
| 151 // TODO(brettw) do we need to use result_h? This is currently hardcoded |
| 152 // becuase the menu button really wants to be larger, but we don't want |
| 153 // the status bar to force the whole tab strip to be larger. Making it |
| 154 // "small" just means that we'll expand to the height, which we want. |
| 155 return gfx::Size(result_w - kStatusItemSeparation, 10); |
| 156 } |
| 157 |
| 158 void StatusAreaView::Layout() { |
| 159 int cur_x = 0; |
| 160 for (int i = 0; i < GetChildViewCount(); i++) { |
| 161 views::View* cur = GetChildViewAt(i); |
| 162 gfx::Size cur_size = cur->GetPreferredSize(); |
| 163 |
| 164 // Put next in row horizontally, and center vertically. |
| 165 cur->SetBounds(cur_x, (height() - cur_size.height()) / 2, |
| 166 cur_size.width(), cur_size.height()); |
| 167 cur_x += cur_size.width() + kStatusItemSeparation; |
| 168 } |
| 169 } |
| 170 |
| 171 void StatusAreaView::Paint(gfx::Canvas* canvas) { |
| 172 ThemeProvider* theme = browser_->profile()->GetThemeProvider(); |
| 173 |
| 174 // Fill the background. |
| 175 SkBitmap* background = theme->GetBitmapNamed(IDR_THEME_FRAME); |
| 176 canvas->TileImageInt(*background, 0, 0, width(), height()); |
| 177 } |
| 178 |
| 179 void StatusAreaView::CreateAppMenu() { |
| 180 if (app_menu_contents_.get()) |
| 181 return; |
| 182 |
| 183 app_menu_contents_.reset(new views::SimpleMenuModel(this)); |
| 184 app_menu_contents_->AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB); |
| 185 app_menu_contents_->AddItemWithStringId(IDC_NEW_WINDOW, IDS_NEW_WINDOW); |
| 186 app_menu_contents_->AddItemWithStringId(IDC_NEW_INCOGNITO_WINDOW, |
| 187 IDS_NEW_INCOGNITO_WINDOW); |
| 188 app_menu_contents_->AddSeparator(); |
| 189 app_menu_contents_->AddCheckItemWithStringId(IDC_SHOW_BOOKMARK_BAR, |
| 190 IDS_SHOW_BOOKMARK_BAR); |
| 191 app_menu_contents_->AddItemWithStringId(IDC_FULLSCREEN, IDS_FULLSCREEN); |
| 192 app_menu_contents_->AddSeparator(); |
| 193 app_menu_contents_->AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); |
| 194 app_menu_contents_->AddItemWithStringId(IDC_SHOW_BOOKMARK_MANAGER, |
| 195 IDS_BOOKMARK_MANAGER); |
| 196 app_menu_contents_->AddItemWithStringId(IDC_SHOW_DOWNLOADS, |
| 197 IDS_SHOW_DOWNLOADS); |
| 198 app_menu_contents_->AddSeparator(); |
| 199 app_menu_contents_->AddItemWithStringId(IDC_CLEAR_BROWSING_DATA, |
| 200 IDS_CLEAR_BROWSING_DATA); |
| 201 app_menu_contents_->AddItemWithStringId(IDC_IMPORT_SETTINGS, |
| 202 IDS_IMPORT_SETTINGS); |
| 203 app_menu_contents_->AddSeparator(); |
| 204 app_menu_contents_->AddItem(IDC_OPTIONS, |
| 205 l10n_util::GetStringFUTF16( |
| 206 IDS_OPTIONS, |
| 207 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); |
| 208 app_menu_contents_->AddItem(IDC_ABOUT, |
| 209 l10n_util::GetStringFUTF16( |
| 210 IDS_ABOUT, |
| 211 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); |
| 212 app_menu_contents_->AddItemWithStringId(IDC_HELP_PAGE, IDS_HELP_PAGE); |
| 213 app_menu_contents_->AddSeparator(); |
| 214 app_menu_contents_->AddItemWithStringId(IDC_EXIT, IDS_EXIT); |
| 215 |
| 216 app_menu_menu_.reset(new views::Menu2(app_menu_contents_.get())); |
| 217 } |
| 218 |
| 219 bool StatusAreaView::IsCommandIdChecked(int command_id) const { |
| 220 return false; |
| 221 } |
| 222 |
| 223 bool StatusAreaView::IsCommandIdEnabled(int command_id) const { |
| 224 if (command_id == IDC_RESTORE_TAB) |
| 225 return browser_->CanRestoreTab(); |
| 226 return browser_->command_updater()->IsCommandEnabled(command_id); |
| 227 } |
| 228 |
| 229 bool StatusAreaView::GetAcceleratorForCommandId( |
| 230 int command_id, |
| 231 views::Accelerator* accelerator) { |
| 232 return false; |
| 233 } |
| 234 |
| 235 void StatusAreaView::ExecuteCommand(int command_id) { |
| 236 browser_->ExecuteCommand(command_id); |
| 237 } |
| 238 |
| 239 void StatusAreaView::RunMenu(views::View* source, const gfx::Point& pt, |
| 240 gfx::NativeView hwnd) { |
| 241 CreateAppMenu(); |
| 242 app_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
| 243 } |
| OLD | NEW |