Home | History | Annotate | Download | only in chromeos
      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/wm_overview_title.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/string16.h"
     10 #include "base/utf_string_conversions.h"
     11 #include "chrome/browser/chromeos/drop_shadow_label.h"
     12 #include "chrome/browser/chromeos/wm_ipc.h"
     13 #include "chrome/browser/chromeos/wm_overview_snapshot.h"
     14 #include "chrome/browser/ui/browser_window.h"
     15 #include "third_party/cros/chromeos_wm_ipc_enums.h"
     16 #include "third_party/skia/include/core/SkBitmap.h"
     17 #include "ui/base/x/x11_util.h"
     18 #include "views/border.h"
     19 #include "views/layout/grid_layout.h"
     20 #include "views/view.h"
     21 
     22 using std::vector;
     23 using views::ColumnSet;
     24 using views::GridLayout;
     25 using views::View;
     26 using gfx::Font;
     27 
     28 #if !defined(OS_CHROMEOS)
     29 #error This file is only meant to be compiled for ChromeOS
     30 #endif
     31 
     32 namespace chromeos {
     33 
     34 // The padding between the title and the URL, in pixels.
     35 static const int kVerticalPadding = 2;
     36 
     37 // This is the size (in pixels) of the drop shadow on the title and url text.
     38 static const int kDropShadowSize = 2;
     39 
     40 namespace {
     41 // Finds a font based on the base font that is no taller than the
     42 // given value (well, unless the font at size 1 is taller than the
     43 // given value).
     44 Font FindFontThisHigh(int pixels, Font base) {
     45   Font font(base.GetFontName(), 1);
     46   Font last_font = font;
     47   while (font.GetHeight() < pixels) {
     48     last_font = font;
     49     font = font.DeriveFont(1, Font::BOLD);
     50   }
     51   return last_font;
     52 }
     53 }  // Anonymous namespace
     54 
     55 WmOverviewTitle::WmOverviewTitle()
     56   : WidgetGtk(TYPE_WINDOW),
     57     title_label_(NULL),
     58     url_label_(NULL) {
     59 }
     60 
     61 void WmOverviewTitle::Init(const gfx::Size& size,
     62                            WmOverviewSnapshot* snapshot) {
     63   MakeTransparent();
     64 
     65   title_label_ = new DropShadowLabel();
     66   title_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
     67   title_label_->SetColor(SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF));
     68   title_label_->SetDropShadowSize(kDropShadowSize);
     69   title_label_->SetFont(FindFontThisHigh(16, title_label_->font()));
     70 
     71   url_label_ = new DropShadowLabel();
     72   url_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
     73   url_label_->SetColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
     74   url_label_->SetDropShadowSize(kDropShadowSize);
     75   url_label_->SetFont(FindFontThisHigh(14, title_label_->font()));
     76 
     77   // Create a new view to be the host for the grid.
     78   View* view = new View();
     79   GridLayout* layout = new GridLayout(view);
     80   view->SetLayoutManager(layout);
     81 
     82   int title_cs_id = 0;
     83   ColumnSet* columns = layout->AddColumnSet(title_cs_id);
     84   columns->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
     85                      GridLayout::FIXED, size.width(), 0);
     86 
     87   layout->StartRow(0, title_cs_id);
     88   layout->AddView(title_label_);
     89   layout->StartRowWithPadding(1, title_cs_id, 0, kVerticalPadding);
     90   layout->AddView(url_label_);
     91 
     92   // Realize the widget.
     93   WidgetGtk::Init(NULL, gfx::Rect(size));
     94 
     95   // Make the view the contents view for this widget.
     96   SetContentsView(view);
     97 
     98   // Set the window type
     99   vector<int> params;
    100   params.push_back(ui::GetX11WindowFromGtkWidget(
    101       GTK_WIDGET(snapshot->GetNativeView())));
    102   WmIpc::instance()->SetWindowType(
    103       GetNativeView(),
    104       WM_IPC_WINDOW_CHROME_TAB_TITLE,
    105       &params);
    106 }
    107 
    108 void WmOverviewTitle::SetTitle(const string16& title) {
    109   title_label_->SetText(UTF16ToWide(title));
    110 }
    111 
    112 void WmOverviewTitle::SetUrl(const GURL& url) {
    113   url_label_->SetURL(url);
    114 }
    115 }  // namespace chromeos
    116