Home | History | Annotate | Download | only in shell
      1 // Copyright (c) 2012 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 "ash/shell/toplevel_window.h"
      6 
      7 #include "ash/display/display_controller.h"
      8 #include "ash/shell.h"
      9 #include "ash/wm/window_positioner.h"
     10 #include "ash/wm/window_state.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "ui/aura/window.h"
     13 #include "ui/aura/window_event_dispatcher.h"
     14 #include "ui/gfx/canvas.h"
     15 #include "ui/views/widget/widget.h"
     16 
     17 namespace ash {
     18 namespace shell {
     19 namespace {
     20 
     21 struct SavedState {
     22   gfx::Rect bounds;
     23   ui::WindowShowState show_state;
     24 };
     25 
     26 // The last window state in ash_shell. We don't bother deleting
     27 // this on shutdown.
     28 SavedState* saved_state = NULL;
     29 
     30 }  // namespace
     31 
     32 ToplevelWindow::CreateParams::CreateParams()
     33     : can_resize(false),
     34       can_maximize(false) {
     35 }
     36 
     37 // static
     38 views::Widget* ToplevelWindow::CreateToplevelWindow(
     39     const CreateParams& params) {
     40   views::Widget* widget = views::Widget::CreateWindowWithContext(
     41       new ToplevelWindow(params), Shell::GetPrimaryRootWindow());
     42   widget->GetNativeView()->SetName("Examples:ToplevelWindow");
     43   wm::WindowState* window_state = wm::GetWindowState(widget->GetNativeView());
     44   window_state->set_window_position_managed(true);
     45   widget->Show();
     46   return widget;
     47 }
     48 
     49 // static
     50 void ToplevelWindow::ClearSavedStateForTest() {
     51   delete saved_state;
     52   saved_state = NULL;
     53 }
     54 
     55 ToplevelWindow::ToplevelWindow(const CreateParams& params) : params_(params) {
     56 }
     57 
     58 ToplevelWindow::~ToplevelWindow() {
     59 }
     60 
     61 void ToplevelWindow::OnPaint(gfx::Canvas* canvas) {
     62   canvas->FillRect(GetLocalBounds(), SK_ColorDKGRAY);
     63 }
     64 
     65 base::string16 ToplevelWindow::GetWindowTitle() const {
     66   return base::ASCIIToUTF16("Examples: Toplevel Window");
     67 }
     68 
     69 void ToplevelWindow::SaveWindowPlacement(const gfx::Rect& bounds,
     70                                          ui::WindowShowState show_state) {
     71   if (!saved_state)
     72     saved_state = new SavedState;
     73   saved_state->bounds = bounds;
     74   saved_state->show_state = show_state;
     75 }
     76 
     77 bool ToplevelWindow::GetSavedWindowPlacement(
     78     const views::Widget* widget,
     79     gfx::Rect* bounds,
     80     ui::WindowShowState* show_state) const {
     81   bool is_saved_bounds = !!saved_state;
     82   if (saved_state) {
     83     *bounds = saved_state->bounds;
     84     *show_state = saved_state->show_state;
     85   } else {
     86     // Initial default bounds.
     87     bounds->SetRect(10, 150, 300, 300);
     88   }
     89   ash::WindowPositioner::GetBoundsAndShowStateForNewWindow(
     90       ash::Shell::GetScreen(),
     91       NULL,
     92       is_saved_bounds,
     93       *show_state,
     94       bounds,
     95       show_state);
     96   return true;
     97 }
     98 
     99 views::View* ToplevelWindow::GetContentsView() {
    100   return this;
    101 }
    102 
    103 bool ToplevelWindow::CanResize() const {
    104   return params_.can_resize;
    105 }
    106 
    107 bool ToplevelWindow::CanMaximize() const {
    108   return params_.can_maximize;
    109 }
    110 
    111 bool ToplevelWindow::CanMinimize() const {
    112   return params_.can_maximize;
    113 }
    114 
    115 }  // namespace shell
    116 }  // namespace ash
    117