| 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/compact_navigation_bar.h" |
| 6 |
| 7 #include "app/gfx/canvas.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #include "app/theme_provider.h" |
| 10 #include "base/logging.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" |
| 12 #include "chrome/browser/browser.h" |
| 13 #include "chrome/browser/profile.h" |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" |
| 15 #include "grit/theme_resources.h" |
| 16 #include "views/controls/button/image_button.h" |
| 17 #include "views/controls/image_view.h" |
| 18 #include "views/controls/native/native_view_host.h" |
| 19 |
| 20 // Padding inside each button around the image. |
| 21 static const int kInnerPadding = 1; |
| 22 |
| 23 // Spacing between buttons. |
| 24 static const int kHorizPadding = 3; |
| 25 |
| 26 static const int kURLWidth = 150; |
| 27 |
| 28 static const int kChromeButtonSize = 25; |
| 29 |
| 30 CompactNavigationBar::CompactNavigationBar(Browser* browser) |
| 31 : browser_(browser), |
| 32 initialized_(false) { |
| 33 } |
| 34 |
| 35 CompactNavigationBar::~CompactNavigationBar() { |
| 36 } |
| 37 |
| 38 void CompactNavigationBar::Init() { |
| 39 DCHECK(!initialized_); |
| 40 initialized_ = true; |
| 41 |
| 42 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); |
| 43 |
| 44 chrome_button_ = new views::ImageButton(this); |
| 45 chrome_button_->SetImage(views::CustomButton::BS_NORMAL, |
| 46 resource_bundle.GetBitmapNamed(IDR_COMPACTNAV_CHROME)); |
| 47 chrome_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
| 48 views::ImageButton::ALIGN_MIDDLE); |
| 49 AddChildView(chrome_button_); |
| 50 |
| 51 back_button_ = new views::ImageButton(this); |
| 52 back_button_->SetImage(views::CustomButton::BS_NORMAL, |
| 53 resource_bundle.GetBitmapNamed(IDR_COMPACTNAV_BACK)); |
| 54 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
| 55 views::ImageButton::ALIGN_MIDDLE); |
| 56 AddChildView(back_button_); |
| 57 |
| 58 bf_separator_ = new views::ImageView; |
| 59 bf_separator_->SetImage( |
| 60 resource_bundle.GetBitmapNamed(IDR_COMPACTNAV_SEPARATOR)); |
| 61 AddChildView(bf_separator_); |
| 62 |
| 63 forward_button_ = new views::ImageButton(this); |
| 64 forward_button_->SetImage(views::CustomButton::BS_NORMAL, |
| 65 resource_bundle.GetBitmapNamed(IDR_COMPACTNAV_FORWARD)); |
| 66 forward_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
| 67 views::ImageButton::ALIGN_MIDDLE); |
| 68 AddChildView(forward_button_); |
| 69 |
| 70 // URL bar construction. |
| 71 location_entry_.reset(new AutocompleteEditViewGtk( |
| 72 this, browser_->toolbar_model(), browser_->profile(), |
| 73 browser_->command_updater(), false, this)); |
| 74 location_entry_->Init(); |
| 75 gtk_widget_show_all(location_entry_->widget()); |
| 76 gtk_widget_hide(location_entry_->widget()); |
| 77 |
| 78 location_entry_view_ = new views::NativeViewHost; |
| 79 AddChildView(location_entry_view_); |
| 80 location_entry_view_->set_focus_view(this); |
| 81 location_entry_view_->Attach(location_entry_->widget()); |
| 82 } |
| 83 |
| 84 gfx::Size CompactNavigationBar::GetPreferredSize() { |
| 85 int width = 0; |
| 86 |
| 87 width += kChromeButtonSize + kHorizPadding; // Chrome button. |
| 88 width += kURLWidth + kHorizPadding; // URL bar. |
| 89 width += back_button_->GetPreferredSize().width() + kHorizPadding + |
| 90 kInnerPadding * 2; |
| 91 width += bf_separator_->GetPreferredSize().width() + kHorizPadding; |
| 92 width += forward_button_->GetPreferredSize().width() + kHorizPadding + |
| 93 kInnerPadding * 2; |
| 94 |
| 95 return gfx::Size(width, kChromeButtonSize); |
| 96 } |
| 97 |
| 98 void CompactNavigationBar::Layout() { |
| 99 if (!initialized_) |
| 100 return; |
| 101 |
| 102 int curx = 0; |
| 103 |
| 104 // Make the chrome button square since it looks better that way. |
| 105 chrome_button_->SetBounds(curx, 0, kChromeButtonSize, kChromeButtonSize); |
| 106 curx += kChromeButtonSize + kHorizPadding; |
| 107 |
| 108 // URL bar. |
| 109 location_entry_view_->SetBounds(curx, 0, kURLWidth, height()); |
| 110 curx += kURLWidth + kHorizPadding; |
| 111 |
| 112 // "Back | Forward" section. |
| 113 gfx::Size button_size = back_button_->GetPreferredSize(); |
| 114 button_size.set_width(button_size.width() + kInnerPadding * 2); |
| 115 back_button_->SetBounds(curx, 1, button_size.width(), height() - 1); |
| 116 curx += button_size.width() + kHorizPadding; |
| 117 |
| 118 button_size = bf_separator_->GetPreferredSize(); |
| 119 bf_separator_->SetBounds(curx, 1, button_size.width(), height() - 1); |
| 120 curx += button_size.width() + kHorizPadding; |
| 121 |
| 122 button_size = forward_button_->GetPreferredSize(); |
| 123 button_size.set_width(button_size.width() + kInnerPadding * 2); |
| 124 forward_button_->SetBounds(curx, 1, button_size.width(), height() - 1); |
| 125 curx += button_size.width() + kHorizPadding; |
| 126 } |
| 127 |
| 128 void CompactNavigationBar::Paint(gfx::Canvas* canvas) { |
| 129 ThemeProvider* theme = browser_->profile()->GetThemeProvider(); |
| 130 |
| 131 // Fill the background. |
| 132 SkBitmap* background = theme->GetBitmapNamed(IDR_THEME_FRAME); |
| 133 canvas->TileImageInt(*background, 0, 0, width(), height()); |
| 134 } |
| 135 |
| 136 void CompactNavigationBar::ButtonPressed(views::Button* sender) { |
| 137 TabContents* tab_contents = browser_->GetSelectedTabContents(); |
| 138 if (!tab_contents) |
| 139 return; |
| 140 |
| 141 if (sender == chrome_button_) { |
| 142 NOTIMPLEMENTED(); // TODO(brettw) hook this up to something. |
| 143 } else if (sender == back_button_) { |
| 144 if (tab_contents->controller().CanGoBack()) |
| 145 tab_contents->controller().GoBack(); |
| 146 } else if (sender == forward_button_) { |
| 147 if (tab_contents->controller().CanGoForward()) |
| 148 tab_contents->controller().GoForward(); |
| 149 } else { |
| 150 NOTREACHED(); |
| 151 } |
| 152 } |
| 153 |
| 154 void CompactNavigationBar::OnAutocompleteAccept( |
| 155 const GURL& url, |
| 156 WindowOpenDisposition disposition, |
| 157 PageTransition::Type transition, |
| 158 const GURL& alternate_nav_url) { |
| 159 // Add the new tab at the first non-pinned location. |
| 160 int index = browser_->tabstrip_model()->IndexOfFirstNonPinnedTab(); |
| 161 browser_->AddTabWithURL(url, GURL(), PageTransition::TYPED, |
| 162 true, index, true, NULL); |
| 163 } |
| 164 |
| 165 void CompactNavigationBar::OnChanged() { |
| 166 // Other one does "DoLayout" here. |
| 167 } |
| 168 |
| 169 void CompactNavigationBar::OnInputInProgress(bool in_progress) { |
| 170 } |
| 171 |
| 172 SkBitmap CompactNavigationBar::GetFavIcon() const { |
| 173 return SkBitmap(); |
| 174 } |
| 175 |
| 176 std::wstring CompactNavigationBar::GetTitle() const { |
| 177 return std::wstring(); |
| 178 } |
| 179 |
| 180 gfx::Rect CompactNavigationBar::GetPopupBounds() const { |
| 181 gfx::Point upper_left(0, height()); |
| 182 ConvertPointToScreen(this, &upper_left); |
| 183 return gfx::Rect(upper_left.x(), upper_left.y(), 700, 100); |
| 184 } |
| OLD | NEW |