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