Upstream version 6.34.113.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / ui / native_app_window_views.cc
1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/browser/ui/native_app_window_views.h"
6
7 #include "content/public/browser/notification_service.h"
8 #include "content/public/browser/web_contents.h"
9 #include "ui/gfx/screen.h"
10 #include "ui/views/controls/webview/webview.h"
11 #include "ui/views/widget/desktop_aura/desktop_screen.h"
12 #include "ui/views/widget/widget.h"
13 #include "xwalk/runtime/browser/ui/top_view_layout_views.h"
14 #include "xwalk/runtime/browser/ui/xwalk_views_delegate.h"
15 #include "xwalk/runtime/common/xwalk_notification_types.h"
16
17 #if defined(OS_WIN)
18 #include "ui/views/window/native_frame_view.h"
19 #endif
20
21 #if defined(OS_TIZEN)
22 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
23 #endif
24
25 namespace xwalk {
26
27 NativeAppWindowViews::NativeAppWindowViews(
28     const NativeAppWindow::CreateParams& create_params)
29   : create_params_(create_params),
30     delegate_(create_params.delegate),
31     web_contents_(create_params.web_contents),
32     web_view_(NULL),
33     window_(NULL),
34     is_fullscreen_(false),
35     minimum_size_(create_params.minimum_size),
36     maximum_size_(create_params.maximum_size),
37     resizable_(create_params.resizable) {}
38
39 NativeAppWindowViews::~NativeAppWindowViews() {}
40
41 // Two-step initialization is needed here so that calls done by views::Widget to
42 // its delegate during views::Widget::Init() reach the correct implementation --
43 // e.g. ViewHierarchyChanged().
44 void NativeAppWindowViews::Initialize() {
45   CHECK(!window_);
46   window_ = new views::Widget;
47
48   views::Widget::InitParams params;
49   params.delegate = this;
50   params.remove_standard_frame = false;
51   params.use_system_default_icon = true;
52   params.top_level = true;
53   params.show_state = create_params_.state;
54   params.parent = create_params_.parent;
55 #if defined(OS_TIZEN_MOBILE)
56   params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
57   // On Tizen apps are sized to the work area.
58   gfx::Rect bounds =
59       gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area();
60   params.bounds = bounds;
61 #else
62   params.type = views::Widget::InitParams::TYPE_WINDOW;
63   params.bounds = create_params_.bounds;
64 #endif
65   params.net_wm_pid = create_params_.net_wm_pid;
66
67   window_->Init(params);
68
69 #if defined(OS_TIZEN_MOBILE)
70   // Set the bounds manually to avoid inset.
71   window_->SetBounds(bounds);
72 #elif !defined(USE_OZONE)
73   window_->CenterWindow(create_params_.bounds.size());
74 #endif
75
76   if (create_params_.state == ui::SHOW_STATE_FULLSCREEN)
77     SetFullscreen(true);
78
79   // TODO(hmin): Need to configure the maximum and minimum size of this window.
80   window_->AddObserver(this);
81 }
82
83 gfx::NativeWindow NativeAppWindowViews::GetNativeWindow() const {
84   return window_->GetNativeWindow();
85 }
86
87 void NativeAppWindowViews::UpdateIcon(const gfx::Image& icon) {
88   icon_ = icon;
89   window_->UpdateWindowIcon();
90 }
91
92 void NativeAppWindowViews::UpdateTitle(const base::string16& title) {
93   title_ = title;
94   window_->UpdateWindowTitle();
95 }
96
97 gfx::Rect NativeAppWindowViews::GetRestoredBounds() const {
98   return window_->GetRestoredBounds();
99 }
100
101 gfx::Rect NativeAppWindowViews::GetBounds() const {
102   return window_->GetWindowBoundsInScreen();
103 }
104
105 void NativeAppWindowViews::SetBounds(const gfx::Rect& bounds) {
106   window_->SetBounds(bounds);
107 }
108
109 void NativeAppWindowViews::Focus() {
110   // window_->Focus();
111 }
112
113 void NativeAppWindowViews::Show() {
114   window_->Show();
115 }
116
117 void NativeAppWindowViews::Hide() {
118   window_->Hide();
119 }
120
121 void NativeAppWindowViews::Maximize() {
122   window_->Maximize();
123 }
124
125 void NativeAppWindowViews::Minimize() {
126   window_->Minimize();
127 }
128
129 void NativeAppWindowViews::SetFullscreen(bool fullscreen) {
130   if (is_fullscreen_ == fullscreen)
131     return;
132   is_fullscreen_ = fullscreen;
133   window_->SetFullscreen(is_fullscreen_);
134
135   content::NotificationService::current()->Notify(
136       xwalk::NOTIFICATION_FULLSCREEN_CHANGED,
137       content::Source<NativeAppWindow>(this),
138       content::NotificationService::NoDetails());
139 }
140
141 void NativeAppWindowViews::Restore() {
142   window_->Restore();
143 }
144
145 void NativeAppWindowViews::FlashFrame(bool flash) {
146   window_->FlashFrame(flash);
147 }
148
149 void NativeAppWindowViews::Close() {
150   window_->Close();
151 }
152
153 bool NativeAppWindowViews::IsActive() const {
154   return window_->IsActive();
155 }
156
157 bool NativeAppWindowViews::IsMaximized() const {
158   return window_->IsMaximized();
159 }
160
161 bool NativeAppWindowViews::IsMinimized() const {
162   return window_->IsMinimized();
163 }
164
165 bool NativeAppWindowViews::IsFullscreen() const {
166   return is_fullscreen_;
167 }
168
169 TopViewLayout* NativeAppWindowViews::top_view_layout() {
170   return static_cast<TopViewLayout*>(GetLayoutManager());
171 }
172
173 ////////////////////////////////////////////////////////////
174 // WidgetDelegate implementation
175 ////////////////////////////////////////////////////////////
176 views::View* NativeAppWindowViews::GetInitiallyFocusedView() {
177   return web_view_;
178 }
179
180 views::View* NativeAppWindowViews::GetContentsView() {
181   return this;
182 }
183
184 views::Widget* NativeAppWindowViews::GetWidget() {
185   return window_;
186 }
187
188 const views::Widget* NativeAppWindowViews::GetWidget() const {
189   return window_;
190 }
191
192 base::string16 NativeAppWindowViews::GetWindowTitle() const {
193   return title_;
194 }
195
196 void NativeAppWindowViews::DeleteDelegate() {
197   window_->RemoveObserver(this);
198   delegate_->OnWindowDestroyed();
199   delete this;
200 }
201 gfx::ImageSkia NativeAppWindowViews::GetWindowAppIcon() {
202   return GetWindowIcon();
203 }
204
205 gfx::ImageSkia NativeAppWindowViews::GetWindowIcon() {
206   return *icon_.ToImageSkia();
207 }
208
209 bool NativeAppWindowViews::ShouldShowWindowTitle() const {
210   return true;
211 }
212
213 void NativeAppWindowViews::SaveWindowPlacement(const gfx::Rect& bounds,
214                                                ui::WindowShowState show_state) {
215   // TODO(hmin): views::WidgetDelegate::SaveWindowPlacement(bounds, show_state);
216 }
217
218 bool NativeAppWindowViews::GetSavedWindowPlacement(const views::Widget* widget,
219     gfx::Rect* bounds, ui::WindowShowState* show_state) const {
220   // TODO(hmin): Get the saved window placement.
221   return false;
222 }
223
224 bool NativeAppWindowViews::CanResize() const {
225   return resizable_ &&
226       (maximum_size_.IsEmpty() || minimum_size_ != maximum_size_);
227 }
228
229 bool NativeAppWindowViews::CanMaximize() const {
230   return resizable_ && maximum_size_.IsEmpty();
231 }
232
233 #if defined(OS_WIN)
234 views::NonClientFrameView* NativeAppWindowViews::CreateNonClientFrameView(
235     views::Widget* widget) {
236   return new views::NativeFrameView(widget);
237 }
238 #endif
239
240 ////////////////////////////////////////////////////////////
241 // views::View implementation
242 ////////////////////////////////////////////////////////////
243
244 void NativeAppWindowViews::ChildPreferredSizeChanged(views::View* child) {
245   // We only re-layout when the top view changes its preferred size (and notify
246   // us by calling its own function PreferredSizeChanged()). We don't react to
247   // changes in preferred size for the content view since we are currently
248   // setting its bounds to the maximum size available.
249   if (child == top_view_layout()->top_view())
250     Layout();
251 }
252
253 void NativeAppWindowViews::ViewHierarchyChanged(
254     const ViewHierarchyChangedDetails& details) {
255   if (details.is_add && details.child == this) {
256     TopViewLayout* layout = new TopViewLayout();
257     SetLayoutManager(layout);
258
259     web_view_ = new views::WebView(NULL);
260     web_view_->SetWebContents(web_contents_);
261     AddChildView(web_view_);
262     layout->set_content_view(web_view_);
263   }
264 }
265
266 void NativeAppWindowViews::OnFocus() {
267   web_view_->RequestFocus();
268 }
269
270 gfx::Size NativeAppWindowViews::GetMaximumSize() {
271   return maximum_size_;
272 }
273
274 gfx::Size NativeAppWindowViews::GetMinimumSize() {
275   return minimum_size_;
276 }
277
278 ////////////////////////////////////////////////////////////
279 // views::WidgetObserver implementation
280 ////////////////////////////////////////////////////////////
281 void NativeAppWindowViews::OnWidgetClosing(views::Widget* widget) {
282 }
283 void NativeAppWindowViews::OnWidgetCreated(views::Widget* widget) {
284 }
285 void NativeAppWindowViews::OnWidgetDestroying(views::Widget* widget) {
286 }
287 void NativeAppWindowViews::OnWidgetDestroyed(views::Widget* widget) {
288 }
289 void NativeAppWindowViews::OnWidgetBoundsChanged(views::Widget* widget,
290     const gfx::Rect& new_bounds) {
291 }
292
293 // static
294 NativeAppWindow* NativeAppWindow::Create(
295     const NativeAppWindow::CreateParams& create_params) {
296   NativeAppWindowViews* window;
297 #if defined(OS_TIZEN)
298   window = new NativeAppWindowTizen(create_params);
299 #else
300   window = new NativeAppWindowViews(create_params);
301 #endif
302   window->Initialize();
303   return window;
304 }
305
306 // static
307 void NativeAppWindow::Initialize() {
308   CHECK(!views::ViewsDelegate::views_delegate);
309   gfx::Screen::SetScreenInstance(
310       gfx::SCREEN_TYPE_NATIVE, views::CreateDesktopScreen());
311   views::ViewsDelegate::views_delegate = new XWalkViewsDelegate();
312 }
313
314 }  // namespace xwalk