summaryrefslogtreecommitdiffstats
path: root/ui/aura/window.cc
blob: a244ac44b90e1b4ceda46806724e44c791081f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/aura/window.h"

#include <algorithm>

#include "base/logging.h"
#include "base/stl_util.h"
#include "ui/aura/desktop.h"
#include "ui/aura/desktop_delegate.h"
#include "ui/aura/event.h"
#include "ui/aura/event_filter.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_types.h"
#include "ui/base/animation/multi_animation.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/compositor/compositor.h"
#include "ui/gfx/screen.h"

namespace aura {

Window::Window(WindowDelegate* delegate)
    : type_(WINDOW_TYPE_UNKNOWN),
      delegate_(delegate),
      show_state_(ui::SHOW_STATE_NORMAL),
      parent_(NULL),
      transient_parent_(NULL),
      id_(-1),
      user_data_(NULL),
      stops_event_propagation_(false) {
}

Window::~Window() {
  // Let the delegate know we're in the processing of destroying.
  if (delegate_)
    delegate_->OnWindowDestroying();

  // Let the root know so that it can remove any references to us.
  Desktop* desktop = GetDesktop();
  if (desktop)
    desktop->WindowDestroying(this);

  // Then destroy the children.
  while (!children_.empty()) {
    Window* child = children_[0];
    delete child;
    // Deleting the child so remove it from out children_ list.
    DCHECK(std::find(children_.begin(), children_.end(), child) ==
           children_.end());
  }

  // Removes ourselves from our transient parent.
  if (transient_parent_)
    transient_parent_->RemoveTransientChild(this);

  // Destroy transient children.
  Windows transient_children(transient_children_);
  STLDeleteElements(&transient_children);
  DCHECK(transient_children_.empty());

  // And let the delegate do any post cleanup.
  if (delegate_)
    delegate_->OnWindowDestroyed();
  if (parent_)
    parent_->RemoveChild(this);

  FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowDestroyed(this));
}

void Window::Init(ui::Layer::LayerType layer_type) {
  layer_.reset(new ui::Layer(Desktop::GetInstance()->compositor(), layer_type));
  layer_->SetVisible(false);
  layer_->set_delegate(this);
}

void Window::SetType(WindowType type) {
  // Cannot change type after the window is initialized.
  DCHECK(!layer());
  type_ = type;
}

void Window::Show() {
  SetVisible(true);
}

void Window::Hide() {
  SetVisible(false);
  ReleaseCapture();
  if (Desktop::GetInstance()->active_window() == this ||
      !Desktop::GetInstance()->active_window()) {
    Desktop::GetInstance()->ActivateTopmostWindow();
  }
}

bool Window::IsVisible() const {
  return layer_->IsDrawn();
}

void Window::Maximize() {
  // The desktop size may have changed, so make sure the window is maximized to
  // the correct size even if it's already maximized.
  gfx::Rect rect = gfx::Screen::GetMonitorWorkAreaNearestWindow(this);
  if (UpdateShowStateAndRestoreBounds(ui::SHOW_STATE_MAXIMIZED) ||
      rect != bounds())
    SetBoundsInternal(rect);
}

void Window::Fullscreen() {
  // The desktop size may have changed, so make sure the window is fullscreen to
  // the correct size even if it's already fullscreen.
  gfx::Rect rect = gfx::Screen::GetMonitorAreaNearestWindow(this);
  if (UpdateShowStateAndRestoreBounds(ui::SHOW_STATE_FULLSCREEN) ||
      rect != bounds())
    SetBoundsInternal(rect);
}

void Window::Restore() {
  if (show_state_ != ui::SHOW_STATE_NORMAL) {
    show_state_ = ui::SHOW_STATE_NORMAL;
    SetBoundsInternal(restore_bounds_);
    restore_bounds_.SetRect(0, 0, 0, 0);
  }
}

gfx::Rect Window::GetScreenBounds() const {
  const gfx::Rect local_bounds = bounds();
  gfx::Point origin = local_bounds.origin();
  Window::ConvertPointToWindow(parent_,
                               aura::Desktop::GetInstance(),
                               &origin);
  return gfx::Rect(origin, local_bounds.size());
}

void Window::Activate() {
  // If we support minimization need to ensure this restores the window first.
  aura::Desktop::GetInstance()->SetActiveWindow(this, this);
}

void Window::Deactivate() {
  aura::Desktop::GetInstance()->Deactivate(this);
}

bool Window::IsActive() const {
  return aura::Desktop::GetInstance()->active_window() == this;
}

ToplevelWindowContainer* Window::AsToplevelWindowContainer() {
  return NULL;
}

const ToplevelWindowContainer* Window::AsToplevelWindowContainer() const {
  return NULL;
}

void Window::SetTransform(const ui::Transform& transform) {
  layer()->SetTransform(transform);
}

void Window::SetLayoutManager(LayoutManager* layout_manager) {
  layout_manager_.reset(layout_manager);
}

void Window::SetBounds(const gfx::Rect& new_bounds) {
  gfx::Rect adjusted_bounds = new_bounds;
  if (parent_ && parent_->layout_manager())
    parent_->layout_manager()->CalculateBoundsForChild(this, &adjusted_bounds);

  if (show_state_ == ui::SHOW_STATE_MAXIMIZED ||
      show_state_ == ui::SHOW_STATE_FULLSCREEN) {
    restore_bounds_ = adjusted_bounds;
    return;
  }
  SetBoundsInternal(adjusted_bounds);
}

gfx::Rect Window::GetTargetBounds() const {
  return layer_->GetTargetBounds();
}

const gfx::Rect& Window::bounds() const {
  return layer_->bounds();
}

void Window::SchedulePaintInRect(const gfx::Rect& rect) {
  layer_->SchedulePaint(rect);
}

void Window::SetCanvas(const SkCanvas& canvas, const gfx::Point& origin) {
  // TODO: figure out how this is going to work when animating the layer. In
  // particular if we're animating the size then the underlying Texture is going
  // to be unhappy if we try to set a texture on a size bigger than the size of
  // the texture.
  layer_->SetCanvas(canvas, origin);
}

void Window::SetParent(Window* parent) {
  if (parent)
    parent->AddChild(this);
  else if (Desktop::GetInstance()->delegate())
    Desktop::GetInstance()->delegate()->AddChildToDefaultParent(this);
  else
    NOTREACHED();
}

void Window::MoveChildToFront(Window* child) {
  DCHECK_EQ(child->parent(), this);
  const Windows::iterator i(std::find(children_.begin(), children_.end(),
                                      child));
  DCHECK(i != children_.end());
  children_.erase(i);

  children_.insert(children_.begin() + children_.size(), child);
  child->layer()->parent()->MoveToFront(child->layer());
  // Repaint the window as active status may have changed.
  SchedulePaintInRect(gfx::Rect());

  // Move any transient children that share the same parent to be in front of
  // us.
  for (Windows::iterator i = child->transient_children_.begin();
       i != child->transient_children_.end(); ++i) {
    Window* transient_child = *i;
    if (transient_child->parent_ == this)
      MoveChildToFront(transient_child);
  }
}

bool Window::CanActivate() const {
  return IsVisible() && delegate_ && delegate_->ShouldActivate(NULL);
}

void Window::AddChild(Window* child) {
  DCHECK(std::find(children_.begin(), children_.end(), child) ==
      children_.end());
  if (child->parent())
    child->parent()->RemoveChild(child);
  child->parent_ = this;
  layer_->Add(child->layer_.get());
  children_.push_back(child);
  if (layout_manager_.get())
    layout_manager_->OnWindowAdded(child);
  FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowAdded(child));
}

void Window::AddTransientChild(Window* child) {
  if (child->transient_parent_)
    child->transient_parent_->RemoveTransientChild(child);
  DCHECK(std::find(transient_children_.begin(), transient_children_.end(),
                   child) == transient_children_.end());
  transient_children_.push_back(child);
  child->transient_parent_ = this;
}

void Window::RemoveTransientChild(Window* child) {
  Windows::iterator i =
      std::find(transient_children_.begin(), transient_children_.end(), child);
  DCHECK(i != transient_children_.end());
  transient_children_.erase(i);
  if (child->transient_parent_ == this)
    child->transient_parent_ = NULL;
}

void Window::RemoveChild(Window* child) {
  Windows::iterator i = std::find(children_.begin(), children_.end(), child);
  DCHECK(i != children_.end());
  if (layout_manager_.get())
    layout_manager_->OnWillRemoveWindow(child);
  FOR_EACH_OBSERVER(WindowObserver, observers_, OnWillRemoveWindow(child));
  child->parent_ = NULL;
  layer_->Remove(child->layer_.get());
  children_.erase(i);
}

Window* Window::GetChildById(int id) {
  return const_cast<Window*>(const_cast<const Window*>(this)->GetChildById(id));
}

const Window* Window::GetChildById(int id) const {
  Windows::const_iterator i;
  for (i = children_.begin(); i != children_.end(); ++i) {
    if ((*i)->id() == id)
      return *i;
    const Window* result = (*i)->GetChildById(id);
    if (result)
      return result;
  }
  return NULL;
}

// static
void Window::ConvertPointToWindow(const Window* source,
                                  const Window* target,
                                  gfx::Point* point) {
  ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point);
}

gfx::NativeCursor Window::GetCursor(const gfx::Point& point) const {
  return delegate_ ? delegate_->GetCursor(point) : gfx::kNullCursor;
}

void Window::SetEventFilter(EventFilter* event_filter) {
  event_filter_.reset(event_filter);
}

void Window::AddObserver(WindowObserver* observer) {
  observers_.AddObserver(observer);
}

void Window::RemoveObserver(WindowObserver* observer) {
  observers_.RemoveObserver(observer);
}

bool Window::ContainsPoint(const gfx::Point& local_point) {
  gfx::Rect local_bounds(gfx::Point(), bounds().size());
  return local_bounds.Contains(local_point);
}

bool Window::HitTest(const gfx::Point& local_point) {
  // TODO(beng): hittest masks.
  return ContainsPoint(local_point);
}

Window* Window::GetEventHandlerForPoint(const gfx::Point& local_point) {
  return GetWindowForPoint(local_point, true, true);
}

Window* Window::GetTopWindowContainingPoint(const gfx::Point& local_point) {
  return GetWindowForPoint(local_point, false, false);
}

void Window::Focus() {
  DCHECK(GetFocusManager());
  GetFocusManager()->SetFocusedWindow(this);
}

void Window::Blur() {
  DCHECK(GetFocusManager());
  GetFocusManager()->SetFocusedWindow(NULL);
}

bool Window::HasFocus() const {
  const internal::FocusManager* focus_manager = GetFocusManager();
  return focus_manager ? focus_manager->IsFocusedWindow(this) : false;
}

// For a given window, we determine its focusability by inspecting each sibling
// after it (i.e. drawn in front of it in the z-order) to see if it stops
// propagation of events that would otherwise be targeted at windows behind it.
// We then perform this same check on every window up to the root.
bool Window::CanFocus() const {
  // TODO(beng): Figure out how to consult the delegate wrt. focusability also.
  if (!IsVisible() || !parent_)
    return false;

  Windows::const_iterator i = std::find(parent_->children().begin(),
                                        parent_->children().end(),
                                        this);
  for (++i; i != parent_->children().end(); ++i) {
    if ((*i)->StopsEventPropagation())
      return false;
  }
  return parent_->CanFocus();
}

internal::FocusManager* Window::GetFocusManager() {
  return const_cast<internal::FocusManager*>(
      static_cast<const Window*>(this)->GetFocusManager());
}

const internal::FocusManager* Window::GetFocusManager() const {
  return parent_ ? parent_->GetFocusManager() : NULL;
}

void Window::SetCapture() {
  if (!IsVisible())
    return;

  Desktop* desktop = GetDesktop();
  if (!desktop)
    return;

  desktop->SetCapture(this);
}

void Window::ReleaseCapture() {
  Desktop* desktop = GetDesktop();
  if (!desktop)
    return;

  desktop->ReleaseCapture(this);
}

bool Window::HasCapture() {
  Desktop* desktop = GetDesktop();
  return desktop && desktop->capture_window() == this;
}

Window* Window::GetToplevelWindow() {
  Window* window = this;
  while (window && window->parent() &&
         !window->parent()->AsToplevelWindowContainer())
    window = window->parent();
  return window && window->parent() ? window : NULL;
}

bool Window::IsOrContainsFullscreenWindow() const {
  if (delegate_)
    return IsVisible() && show_state_ == ui::SHOW_STATE_FULLSCREEN;

  for (Windows::const_iterator it = children_.begin();
       it != children_.end(); ++it) {
    if ((*it)->IsOrContainsFullscreenWindow())
      return true;
  }
  return false;
}

void Window::SetProperty(const char* name, void* value) {
  if (value)
    prop_map_[name] = value;
  else
    prop_map_.erase(name);
}

void* Window::GetProperty(const char* name) const {
  std::map<const char*, void*>::const_iterator iter = prop_map_.find(name);
  if (iter == prop_map_.end())
    return NULL;
  return iter->second;
}

Desktop* Window::GetDesktop() {
  return parent_ ? parent_->GetDesktop() : NULL;
}

void Window::SetBoundsInternal(const gfx::Rect& new_bounds) {
  const gfx::Rect old_bounds = bounds();
  if (old_bounds == new_bounds)
    return;

  bool was_move = old_bounds.size() == new_bounds.size();
  layer_->SetBounds(new_bounds);

  if (layout_manager_.get())
    layout_manager_->OnWindowResized();
  if (delegate_)
    delegate_->OnBoundsChanged(old_bounds, new_bounds);
  if (IsVisible()) {
    if (was_move)
      layer()->ScheduleDraw();
    else
      SchedulePaint();
  }
}

void Window::SetVisible(bool visible) {
  if (visible == layer_->visible())
    return;  // No change.

  bool was_visible = IsVisible();
  layer_->SetVisible(visible);
  bool is_visible = IsVisible();
  if (was_visible != is_visible) {
    SchedulePaint();
    if (delegate_)
      delegate_->OnWindowVisibilityChanged(is_visible);
  }
  if (parent_ && parent_->layout_manager_.get())
    parent_->layout_manager_->OnChildWindowVisibilityChanged(this, visible);
  FOR_EACH_OBSERVER(WindowObserver, observers_,
                    OnWindowVisibilityChanged(this, visible));
}

void Window::SchedulePaint() {
  SchedulePaintInRect(gfx::Rect(0, 0, bounds().width(), bounds().height()));
}

bool Window::StopsEventPropagation() const {
  return stops_event_propagation_ && !children_.empty();
}

bool Window::UpdateShowStateAndRestoreBounds(
    ui::WindowShowState new_show_state) {
  if (show_state_ == new_show_state)
    return false;
  show_state_ = new_show_state;
  if (restore_bounds_.IsEmpty())
    restore_bounds_ = bounds();
  return true;
}

Window* Window::GetWindowForPoint(const gfx::Point& local_point,
                                  bool return_tightest,
                                  bool for_event_handling) {
  if (!IsVisible())
    return NULL;

  if ((for_event_handling && !HitTest(local_point)) ||
      (!for_event_handling && !ContainsPoint(local_point)))
    return NULL;

  if (!return_tightest && delegate_)
    return this;

  for (Windows::const_reverse_iterator it = children_.rbegin();
       it != children_.rend(); ++it) {
    Window* child = *it;
    if (!child->IsVisible())
      continue;

    gfx::Point point_in_child_coords(local_point);
    Window::ConvertPointToWindow(this, child, &point_in_child_coords);
    Window* match = child->GetWindowForPoint(point_in_child_coords,
                                             return_tightest,
                                             for_event_handling);
    if (match)
      return match;

    if (for_event_handling && child->StopsEventPropagation())
      break;
  }

  return delegate_ ? this : NULL;
}

void Window::OnPaintLayer(gfx::Canvas* canvas) {
  delegate_->OnPaint(canvas);
}

void Window::OnLayerAnimationEnded(
    const ui::LayerAnimationSequence* animation) {
}

}  // namespace aura