- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / widget / widget.h
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 #ifndef UI_VIEWS_WIDGET_WIDGET_H_
6 #define UI_VIEWS_WIDGET_WIDGET_H_
7
8 #include <set>
9 #include <stack>
10 #include <vector>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "ui/base/ui_base_types.h"
16 #include "ui/compositor/layer_type.h"
17 #include "ui/gfx/native_widget_types.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/views/focus/focus_manager.h"
20 #include "ui/views/widget/native_widget_delegate.h"
21 #include "ui/views/window/client_view.h"
22 #include "ui/views/window/non_client_view.h"
23
24 #if defined(OS_WIN)
25 // Windows headers define macros for these function names which screw with us.
26 #if defined(IsMaximized)
27 #undef IsMaximized
28 #endif
29 #if defined(IsMinimized)
30 #undef IsMinimized
31 #endif
32 #if defined(CreateWindow)
33 #undef CreateWindow
34 #endif
35 #endif
36
37 namespace gfx {
38 class Canvas;
39 class Point;
40 class Rect;
41 }
42
43 namespace ui {
44 class Accelerator;
45 class Compositor;
46 class DefaultThemeProvider;
47 class Layer;
48 class NativeTheme;
49 class OSExchangeData;
50 class ThemeProvider;
51 }
52
53 namespace views {
54
55 class DesktopRootWindowHost;
56 class InputMethod;
57 class NativeWidget;
58 class NonClientFrameView;
59 class TooltipManager;
60 class View;
61 class WidgetDelegate;
62 class WidgetObserver;
63
64 namespace internal {
65 class NativeWidgetPrivate;
66 class RootView;
67 }
68
69 ////////////////////////////////////////////////////////////////////////////////
70 // Widget class
71 //
72 //  Encapsulates the platform-specific rendering, event receiving and widget
73 //  management aspects of the UI framework.
74 //
75 //  Owns a RootView and thus a View hierarchy. Can contain child Widgets.
76 //  Widget is a platform-independent type that communicates with a platform or
77 //  context specific NativeWidget implementation.
78 //
79 //  A special note on ownership:
80 //
81 //    Depending on the value of the InitParams' ownership field, the Widget
82 //    either owns or is owned by its NativeWidget:
83 //
84 //    ownership = NATIVE_WIDGET_OWNS_WIDGET (default)
85 //      The Widget instance is owned by its NativeWidget. When the NativeWidget
86 //      is destroyed (in response to a native destruction message), it deletes
87 //      the Widget from its destructor.
88 //    ownership = WIDGET_OWNS_NATIVE_WIDGET (non-default)
89 //      The Widget instance owns its NativeWidget. This state implies someone
90 //      else wants to control the lifetime of this object. When they destroy
91 //      the Widget it is responsible for destroying the NativeWidget (from its
92 //      destructor).
93 //
94 class VIEWS_EXPORT Widget : public internal::NativeWidgetDelegate,
95                             public FocusTraversable {
96  public:
97   typedef std::set<Widget*> Widgets;
98
99   enum FrameType {
100     FRAME_TYPE_DEFAULT,         // Use whatever the default would be.
101     FRAME_TYPE_FORCE_CUSTOM,    // Force the custom frame.
102     FRAME_TYPE_FORCE_NATIVE     // Force the native frame.
103   };
104
105   // Result from RunMoveLoop().
106   enum MoveLoopResult {
107     // The move loop completed successfully.
108     MOVE_LOOP_SUCCESSFUL,
109
110     // The user canceled the move loop.
111     MOVE_LOOP_CANCELED
112   };
113
114   // Source that initiated the move loop.
115   enum MoveLoopSource {
116     MOVE_LOOP_SOURCE_MOUSE,
117     MOVE_LOOP_SOURCE_TOUCH,
118   };
119
120   // Behavior when escape is pressed during a move loop.
121   enum MoveLoopEscapeBehavior {
122     // Indicates the window should be hidden.
123     MOVE_LOOP_ESCAPE_BEHAVIOR_HIDE,
124
125     // Indicates the window should not be hidden.
126     MOVE_LOOP_ESCAPE_BEHAVIOR_DONT_HIDE,
127   };
128
129   struct VIEWS_EXPORT InitParams {
130     enum Type {
131       TYPE_WINDOW,      // A decorated Window, like a frame window.
132                         // Widgets of TYPE_WINDOW will have a NonClientView.
133       TYPE_PANEL,       // Always on top window managed by PanelManager.
134                         // Widgets of TYPE_PANEL will have a NonClientView.
135       TYPE_WINDOW_FRAMELESS,
136                         // An undecorated Window.
137       TYPE_CONTROL,     // A control, like a button.
138       TYPE_POPUP,       // An undecorated Window, with transient properties.
139       TYPE_MENU,        // An undecorated Window, with transient properties
140                         // specialized to menus.
141       TYPE_TOOLTIP,
142       TYPE_BUBBLE,
143     };
144
145     enum WindowOpacity {
146       // Infer fully opaque or not. For WinAura, top-level windows that are not
147       // of TYPE_WINDOW are translucent so that they can be made to fade in. In
148       // all other cases, windows are fully opaque.
149       INFER_OPACITY,
150       // Fully opaque.
151       OPAQUE_WINDOW,
152       // Possibly translucent/transparent.
153       TRANSLUCENT_WINDOW,
154     };
155
156     enum Ownership {
157       // Default. Creator is not responsible for managing the lifetime of the
158       // Widget, it is destroyed when the corresponding NativeWidget is
159       // destroyed.
160       NATIVE_WIDGET_OWNS_WIDGET,
161       // Used when the Widget is owned by someone other than the NativeWidget,
162       // e.g. a scoped_ptr in tests.
163       WIDGET_OWNS_NATIVE_WIDGET
164     };
165
166     InitParams();
167     explicit InitParams(Type type);
168
169     // Will return the first of the following that isn't NULL: the native view,
170     // |parent|, |context|.
171     gfx::NativeView GetContext() const;
172
173     Type type;
174     // If NULL, a default implementation will be constructed.
175     WidgetDelegate* delegate;
176     bool child;
177     // If TRANSLUCENT_WINDOW, the widget may be fully or partially transparent.
178     // If OPAQUE_WINDOW, we can perform optimizations based on the widget being
179     // fully opaque.  Defaults to TRANSLUCENT_WINDOW if
180     // ViewsDelegate::UseTransparentWindows().  Defaults to OPAQUE_WINDOW for
181     // non-window widgets.
182     WindowOpacity opacity;
183     bool accept_events;
184     bool can_activate;
185     bool keep_on_top;
186     Ownership ownership;
187     bool mirror_origin_in_rtl;
188     bool has_dropshadow;
189     // Only used by Windows. Specifies that the system default caption and icon
190     // should not be rendered, and that the client area should be equivalent to
191     // the window area.
192     bool remove_standard_frame;
193     // Only used by ShellWindow on Windows. Specifies that the default icon of
194     // packaged app should be the system default icon.
195     bool use_system_default_icon;
196     // Whether the widget should be maximized or minimized.
197     ui::WindowShowState show_state;
198     // Should the widget be double buffered? Default is false.
199     bool double_buffer;
200     gfx::NativeView parent;
201     // Specifies the initial bounds of the Widget. Default is empty, which means
202     // the NativeWidget may specify a default size. If the parent is specified,
203     // |bounds| is in the parent's coordinate system. If the parent is not
204     // specified, it's in screen's global coordinate system.
205     gfx::Rect bounds;
206     // When set, this value is used as the Widget's NativeWidget implementation.
207     // The Widget will not construct a default one. Default is NULL.
208     NativeWidget* native_widget;
209     // Aura-only. Provides a DesktopRootWindowHost implementation to use instead
210     // of the default one.
211     // TODO(beng): Figure out if there's a better way to expose this, e.g. get
212     // rid of NW subclasses and do this all via message handling.
213     DesktopRootWindowHost* desktop_root_window_host;
214     // Whether this window is intended to be a toplevel window with no
215     // attachment to any other window. (This may be a transient window if
216     // |parent| is set.)
217     bool top_level;
218     // Only used by NativeWidgetAura. Specifies the type of layer for the
219     // aura::Window. Default is LAYER_TEXTURED.
220     ui::LayerType layer_type;
221     // Only used by Aura. Provides a context window whose RootWindow is
222     // consulted during widget creation to determine where in the Window
223     // hierarchy this widget should be placed. (This is separate from |parent|;
224     // if you pass a RootWindow to |parent|, your window will be parented to
225     // |parent|. If you pass a RootWindow to |context|, we ask that RootWindow
226     // where it wants your window placed.) NULL is not allowed if you are using
227     // aura.
228     gfx::NativeView context;
229     // Only used by X11, for root level windows. Specifies the res_name and
230     // res_class fields, respectively, of the WM_CLASS window property. Controls
231     // window grouping and desktop file matching in Linux window managers.
232     std::string wm_class_name;
233     std::string wm_class_class;
234     // Only used by X11, for root level windows. Specifies the PID set in
235     // _NET_WM_PID window property.
236     long net_wm_pid;
237   };
238
239   Widget();
240   virtual ~Widget();
241
242   // Creates a toplevel window with no context. These methods should only be
243   // used in cases where there is no contextual information because we're
244   // creating a toplevel window connected to no other event.
245   //
246   // If you have any parenting or context information, or can pass that
247   // information, prefer the WithParent or WithContext versions of these
248   // methods.
249   static Widget* CreateWindow(WidgetDelegate* delegate);
250   static Widget* CreateWindowWithBounds(WidgetDelegate* delegate,
251                                         const gfx::Rect& bounds);
252
253   // Creates a decorated window Widget with the specified properties.
254   static Widget* CreateWindowWithParent(WidgetDelegate* delegate,
255                                         gfx::NativeWindow parent);
256   static Widget* CreateWindowWithParentAndBounds(WidgetDelegate* delegate,
257                                                  gfx::NativeWindow parent,
258                                                  const gfx::Rect& bounds);
259
260   // Creates a decorated window Widget in the same desktop context as
261   // |context|.
262   static Widget* CreateWindowWithContext(WidgetDelegate* delegate,
263                                          gfx::NativeView context);
264   static Widget* CreateWindowWithContextAndBounds(WidgetDelegate* delegate,
265                                                   gfx::NativeView context,
266                                                   const gfx::Rect& bounds);
267
268   // Creates an undecorated child window Widget. |new_style_parent| is the
269   // parent to use for new style dialogs, |parent| for currently-styled dialogs.
270   //
271   // TODO(wittman): use a single parent parameter once we move fully to
272   // new-style dialogs.
273   static Widget* CreateWindowAsFramelessChild(WidgetDelegate* widget_delegate,
274                                               gfx::NativeView parent,
275                                               gfx::NativeView new_style_parent);
276
277   // Enumerates all windows pertaining to us and notifies their
278   // view hierarchies that the locale has changed.
279   // TODO(beng): remove post-Aurafication of ChromeOS.
280   static void NotifyLocaleChanged();
281
282   // Closes all Widgets that aren't identified as "secondary widgets". Called
283   // during application shutdown when the last non-secondary widget is closed.
284   static void CloseAllSecondaryWidgets();
285
286   // Converts a rectangle from one Widget's coordinate system to another's.
287   // Returns false if the conversion couldn't be made, because either these two
288   // Widgets do not have a common ancestor or they are not on the screen yet.
289   // The value of |*rect| won't be changed when false is returned.
290   static bool ConvertRect(const Widget* source,
291                           const Widget* target,
292                           gfx::Rect* rect);
293
294   // Retrieves the Widget implementation associated with the given
295   // NativeView or Window, or NULL if the supplied handle has no associated
296   // Widget.
297   static Widget* GetWidgetForNativeView(gfx::NativeView native_view);
298   static Widget* GetWidgetForNativeWindow(gfx::NativeWindow native_window);
299
300   // Retrieves the top level widget in a native view hierarchy
301   // starting at |native_view|. Top level widget is a widget with TYPE_WINDOW,
302   // TYPE_PANEL, TYPE_WINDOW_FRAMELESS, POPUP or MENU and has its own
303   // focus manager. This may be itself if the |native_view| is top level,
304   // or NULL if there is no toplevel in a native view hierarchy.
305   static Widget* GetTopLevelWidgetForNativeView(gfx::NativeView native_view);
306
307   // Returns all Widgets in |native_view|'s hierarchy, including itself if
308   // it is one.
309   static void GetAllChildWidgets(gfx::NativeView native_view,
310                                  Widgets* children);
311
312   // Returns all non-child Widgets owned by |native_view|.
313   static void GetAllOwnedWidgets(gfx::NativeView native_view,
314                                  Widgets* owned);
315
316   // Re-parent a NativeView and notify all Widgets in |native_view|'s hierarchy
317   // of the change.
318   static void ReparentNativeView(gfx::NativeView native_view,
319                                  gfx::NativeView new_parent);
320
321   // Returns the preferred size of the contents view of this window based on
322   // its localized size data. The width in cols is held in a localized string
323   // resource identified by |col_resource_id|, the height in the same fashion.
324   // TODO(beng): This should eventually live somewhere else, probably closer to
325   //             ClientView.
326   static int GetLocalizedContentsWidth(int col_resource_id);
327   static int GetLocalizedContentsHeight(int row_resource_id);
328   static gfx::Size GetLocalizedContentsSize(int col_resource_id,
329                                             int row_resource_id);
330
331   // Returns true if the specified type requires a NonClientView.
332   static bool RequiresNonClientView(InitParams::Type type);
333
334   void Init(const InitParams& params);
335
336   // Returns the gfx::NativeView associated with this Widget.
337   gfx::NativeView GetNativeView() const;
338
339   // Returns the gfx::NativeWindow associated with this Widget. This may return
340   // NULL on some platforms if the widget was created with a type other than
341   // TYPE_WINDOW or TYPE_PANEL.
342   gfx::NativeWindow GetNativeWindow() const;
343
344   // Add/remove observer.
345   void AddObserver(WidgetObserver* observer);
346   void RemoveObserver(WidgetObserver* observer);
347   bool HasObserver(WidgetObserver* observer);
348
349   // Returns the accelerator given a command id. Returns false if there is
350   // no accelerator associated with a given id, which is a common condition.
351   virtual bool GetAccelerator(int cmd_id, ui::Accelerator* accelerator);
352
353   // Forwarded from the RootView so that the widget can do any cleanup.
354   void ViewHierarchyChanged(const View::ViewHierarchyChangedDetails& details);
355
356   // Called right before changing the widget's parent NativeView to do any
357   // cleanup.
358   void NotifyNativeViewHierarchyWillChange();
359
360   // Called after changing the widget's parent NativeView. Notifies the RootView
361   // about the change.
362   void NotifyNativeViewHierarchyChanged();
363
364   // Returns the top level widget in a hierarchy (see is_top_level() for
365   // the definition of top level widget.) Will return NULL if called
366   // before the widget is attached to the top level widget's hierarchy.
367   Widget* GetTopLevelWidget();
368   const Widget* GetTopLevelWidget() const;
369
370   // Gets/Sets the WidgetDelegate.
371   WidgetDelegate* widget_delegate() const { return widget_delegate_; }
372
373   // Sets the specified view as the contents of this Widget. There can only
374   // be one contents view child of this Widget's RootView. This view is sized to
375   // fit the entire size of the RootView. The RootView takes ownership of this
376   // View, unless it is set as not being parent-owned.
377   void SetContentsView(View* view);
378   View* GetContentsView();
379
380   // Returns the bounds of the Widget in screen coordinates.
381   gfx::Rect GetWindowBoundsInScreen() const;
382
383   // Returns the bounds of the Widget's client area in screen coordinates.
384   gfx::Rect GetClientAreaBoundsInScreen() const;
385
386   // Retrieves the restored bounds for the window.
387   gfx::Rect GetRestoredBounds() const;
388
389   // Sizes and/or places the widget to the specified bounds, size or position.
390   void SetBounds(const gfx::Rect& bounds);
391   void SetSize(const gfx::Size& size);
392
393   // Sizes the window to the specified size and centerizes it.
394   void CenterWindow(const gfx::Size& size);
395
396   // Like SetBounds(), but ensures the Widget is fully visible on screen,
397   // resizing and/or repositioning as necessary. This is only useful for
398   // non-child widgets.
399   void SetBoundsConstrained(const gfx::Rect& bounds);
400
401   // Sets whether animations that occur when visibility is changed are enabled.
402   // Default is true.
403   void SetVisibilityChangedAnimationsEnabled(bool value);
404
405   // Starts a nested message loop that moves the window. This can be used to
406   // start a window move operation from a mouse or touch event. This returns
407   // when the move completes. |drag_offset| is the offset from the top left
408   // corner of the window to the point where the cursor is dragging, and is used
409   // to offset the bounds of the window from the cursor.
410   MoveLoopResult RunMoveLoop(const gfx::Vector2d& drag_offset,
411                              MoveLoopSource source,
412                              MoveLoopEscapeBehavior escape_behavior);
413
414   // Stops a previously started move loop. This is not immediate.
415   void EndMoveLoop();
416
417   // Places the widget in front of the specified widget in z-order.
418   void StackAboveWidget(Widget* widget);
419   void StackAbove(gfx::NativeView native_view);
420   void StackAtTop();
421
422   // Places the widget below the specified NativeView.
423   void StackBelow(gfx::NativeView native_view);
424
425   // Sets a shape on the widget. Passing a NULL |shape| reverts the widget to
426   // be rectangular. Takes ownership of |shape|.
427   void SetShape(gfx::NativeRegion shape);
428
429   // Hides the widget then closes it after a return to the message loop.
430   virtual void Close();
431
432   // TODO(beng): Move off public API.
433   // Closes the widget immediately. Compare to |Close|. This will destroy the
434   // window handle associated with this Widget, so should not be called from
435   // any code that expects it to be valid beyond this call.
436   void CloseNow();
437
438   // Whether the widget has been asked to close itself. In particular this is
439   // set to true after Close() has been invoked on the NativeWidget.
440   bool IsClosed() const;
441
442   // Shows or hides the widget, without changing activation state.
443   virtual void Show();
444   void Hide();
445
446   // Like Show(), but does not activate the window.
447   void ShowInactive();
448
449   // Activates the widget, assuming it already exists and is visible.
450   void Activate();
451
452   // Deactivates the widget, making the next window in the Z order the active
453   // window.
454   void Deactivate();
455
456   // Returns whether the Widget is the currently active window.
457   virtual bool IsActive() const;
458
459   // Prevents the window from being rendered as deactivated. This state is
460   // reset automatically as soon as the window becomes activated again. There is
461   // no ability to control the state through this API as this leads to sync
462   // problems.
463   void DisableInactiveRendering();
464
465   // Sets the widget to be on top of all other widgets in the windowing system.
466   void SetAlwaysOnTop(bool on_top);
467
468   // Returns whether the widget has been set to be on top of most other widgets
469   // in the windowing system.
470   bool IsAlwaysOnTop() const;
471
472   // Maximizes/minimizes/restores the window.
473   void Maximize();
474   void Minimize();
475   void Restore();
476
477   // Whether or not the window is maximized or minimized.
478   virtual bool IsMaximized() const;
479   bool IsMinimized() const;
480
481   // Accessors for fullscreen state.
482   void SetFullscreen(bool fullscreen);
483   bool IsFullscreen() const;
484
485   // Sets the opacity of the widget. This may allow widgets behind the widget
486   // in the Z-order to become visible, depending on the capabilities of the
487   // underlying windowing system.
488   void SetOpacity(unsigned char opacity);
489
490   // Sets whether or not the window should show its frame as a "transient drag
491   // frame" - slightly transparent and without the standard window controls.
492   void SetUseDragFrame(bool use_drag_frame);
493
494   // Flashes the frame of the window to draw attention to it. Currently only
495   // implemented on Windows for non-Aura.
496   void FlashFrame(bool flash);
497
498   // Returns the View at the root of the View hierarchy contained by this
499   // Widget.
500   View* GetRootView();
501   const View* GetRootView() const;
502
503   // A secondary widget is one that is automatically closed (via Close()) when
504   // all non-secondary widgets are closed.
505   // Default is true.
506   // TODO(beng): This is an ugly API, should be handled implicitly via
507   //             transience.
508   void set_is_secondary_widget(bool is_secondary_widget) {
509     is_secondary_widget_ = is_secondary_widget;
510   }
511   bool is_secondary_widget() const { return is_secondary_widget_; }
512
513   // Returns whether the Widget is visible to the user.
514   virtual bool IsVisible() const;
515
516   // Returns the ThemeProvider that provides theme resources for this Widget.
517   virtual ui::ThemeProvider* GetThemeProvider() const;
518
519   ui::NativeTheme* GetNativeTheme() {
520     return const_cast<ui::NativeTheme*>(
521         const_cast<const Widget*>(this)->GetNativeTheme());
522   }
523   const ui::NativeTheme* GetNativeTheme() const;
524
525   // Returns the FocusManager for this widget.
526   // Note that all widgets in a widget hierarchy share the same focus manager.
527   FocusManager* GetFocusManager();
528   const FocusManager* GetFocusManager() const;
529
530   // Returns the InputMethod for this widget.
531   // Note that all widgets in a widget hierarchy share the same input method.
532   InputMethod* GetInputMethod();
533   const InputMethod* GetInputMethod() const;
534
535   // Starts a drag operation for the specified view. This blocks until the drag
536   // operation completes. |view| can be NULL.
537   // If the view is non-NULL it can be accessed during the drag by calling
538   // dragged_view(). If the view has not been deleted during the drag,
539   // OnDragDone() is called on it. |location| is in the widget's coordinate
540   // system.
541   void RunShellDrag(View* view,
542                     const ui::OSExchangeData& data,
543                     const gfx::Point& location,
544                     int operation,
545                     ui::DragDropTypes::DragEventSource source);
546
547   // Returns the view that requested the current drag operation via
548   // RunShellDrag(), or NULL if there is no such view or drag operation.
549   View* dragged_view() { return dragged_view_; }
550
551   // Adds the specified |rect| in client area coordinates to the rectangle to be
552   // redrawn.
553   virtual void SchedulePaintInRect(const gfx::Rect& rect);
554
555   // Sets the currently visible cursor. If |cursor| is NULL, the cursor used
556   // before the current is restored.
557   void SetCursor(gfx::NativeCursor cursor);
558
559   // Returns true if and only if mouse events are enabled.
560   bool IsMouseEventsEnabled() const;
561
562   // Sets/Gets a native window property on the underlying native window object.
563   // Returns NULL if the property does not exist. Setting the property value to
564   // NULL removes the property.
565   void SetNativeWindowProperty(const char* name, void* value);
566   void* GetNativeWindowProperty(const char* name) const;
567
568   // Tell the window to update its title from the delegate.
569   void UpdateWindowTitle();
570
571   // Tell the window to update its icon from the delegate.
572   void UpdateWindowIcon();
573
574   // Retrieves the focus traversable for this widget.
575   FocusTraversable* GetFocusTraversable();
576
577   // Notifies the view hierarchy contained in this widget that theme resources
578   // changed.
579   void ThemeChanged();
580
581   // Notifies the view hierarchy contained in this widget that locale resources
582   // changed.
583   void LocaleChanged();
584
585   void SetFocusTraversableParent(FocusTraversable* parent);
586   void SetFocusTraversableParentView(View* parent_view);
587
588   // Clear native focus set to the Widget's NativeWidget.
589   void ClearNativeFocus();
590
591   void set_frame_type(FrameType frame_type) { frame_type_ = frame_type; }
592   FrameType frame_type() const { return frame_type_; }
593
594   // Creates an appropriate NonClientFrameView for this widget. The
595   // WidgetDelegate is given the first opportunity to create one, followed by
596   // the NativeWidget implementation. If both return NULL, a default one is
597   // created.
598   virtual NonClientFrameView* CreateNonClientFrameView();
599
600   // Whether we should be using a native frame.
601   bool ShouldUseNativeFrame() const;
602
603   // Forces the frame into the alternate frame type (custom or native) depending
604   // on its current state.
605   void DebugToggleFrameType();
606
607   // Tell the window that something caused the frame type to change.
608   void FrameTypeChanged();
609
610   NonClientView* non_client_view() {
611     return const_cast<NonClientView*>(
612         const_cast<const Widget*>(this)->non_client_view());
613   }
614   const NonClientView* non_client_view() const {
615     return non_client_view_;
616   }
617
618   ClientView* client_view() {
619     return const_cast<ClientView*>(
620         const_cast<const Widget*>(this)->client_view());
621   }
622   const ClientView* client_view() const {
623     // non_client_view_ may be NULL, especially during creation.
624     return non_client_view_ ? non_client_view_->client_view() : NULL;
625   }
626
627   const ui::Compositor* GetCompositor() const;
628   ui::Compositor* GetCompositor();
629
630   // Returns the widget's layer, if any.
631   ui::Layer* GetLayer();
632
633   // Reorders the widget's child NativeViews which are associated to the view
634   // tree (eg via a NativeViewHost) to match the z-order of the views in the
635   // view tree. The z-order of views with layers relative to views with
636   // associated NativeViews is used to reorder the NativeView layers. This
637   // method assumes that the widget's child layers which are owned by a view are
638   // already in the correct z-order relative to each other and does no
639   // reordering if there are no views with an associated NativeView.
640   void ReorderNativeViews();
641
642   // Schedules an update to the root layers. The actual processing occurs when
643   // GetRootLayers() is invoked.
644   void UpdateRootLayers();
645
646   const NativeWidget* native_widget() const;
647   NativeWidget* native_widget();
648
649   internal::NativeWidgetPrivate* native_widget_private() {
650     return native_widget_;
651   }
652   const internal::NativeWidgetPrivate* native_widget_private() const {
653     return native_widget_;
654   }
655
656   // Sets capture to the specified view. This makes it so that all mouse, touch
657   // and gesture events go to |view|. If |view| is NULL, the widget still
658   // obtains event capture, but the events will go to the view they'd normally
659   // go to.
660   void SetCapture(View* view);
661
662   // Releases capture.
663   void ReleaseCapture();
664
665   // Returns true if the widget has capture.
666   bool HasCapture();
667
668   void set_auto_release_capture(bool auto_release_capture) {
669     auto_release_capture_ = auto_release_capture;
670   }
671
672   // Returns the font used for tooltips.
673   TooltipManager* GetTooltipManager();
674   const TooltipManager* GetTooltipManager() const;
675
676   // Sets-up the focus manager with the view that should have focus when the
677   // window is shown the first time.  Returns true if the initial focus has been
678   // set or the widget should not set the initial focus, or false if the caller
679   // should set the initial focus (if any).
680   bool SetInitialFocus();
681
682   void set_focus_on_creation(bool focus_on_creation) {
683     focus_on_creation_ = focus_on_creation;
684   }
685
686   // True if the widget is considered top level widget. Top level widget
687   // is a widget of TYPE_WINDOW, TYPE_PANEL, TYPE_WINDOW_FRAMELESS, BUBBLE,
688   // POPUP or MENU, and has a focus manager and input method object associated
689   // with it. TYPE_CONTROL and TYPE_TOOLTIP is not considered top level.
690   bool is_top_level() const { return is_top_level_; }
691
692   // True when window movement via mouse interaction with the frame is disabled.
693   bool movement_disabled() const { return movement_disabled_; }
694   void set_movement_disabled(bool disabled) { movement_disabled_ = disabled; }
695
696   // Returns the work area bounds of the screen the Widget belongs to.
697   gfx::Rect GetWorkAreaBoundsInScreen() const;
698
699   // Creates and dispatches synthesized mouse move event using the current
700   // mouse location to refresh hovering status in the widget.
701   void SynthesizeMouseMoveEvent();
702
703   // Called by our RootView after it has performed a Layout. Used to forward
704   // window sizing information to the window server on some platforms.
705   void OnRootViewLayout();
706
707   // Notification that our owner is closing.
708   // NOTE: this is not invoked for aura as it's currently not needed there.
709   // Under aura menus close by way of activation getting reset when the owner
710   // closes.
711   virtual void OnOwnerClosing();
712
713   // Overridden from NativeWidgetDelegate:
714   virtual bool IsModal() const OVERRIDE;
715   virtual bool IsDialogBox() const OVERRIDE;
716   virtual bool CanActivate() const OVERRIDE;
717   virtual bool IsInactiveRenderingDisabled() const OVERRIDE;
718   virtual void EnableInactiveRendering() OVERRIDE;
719   virtual void OnNativeWidgetActivationChanged(bool active) OVERRIDE;
720   virtual void OnNativeFocus(gfx::NativeView old_focused_view) OVERRIDE;
721   virtual void OnNativeBlur(gfx::NativeView new_focused_view) OVERRIDE;
722   virtual void OnNativeWidgetVisibilityChanging(bool visible) OVERRIDE;
723   virtual void OnNativeWidgetVisibilityChanged(bool visible) OVERRIDE;
724   virtual void OnNativeWidgetCreated(bool desktop_widget) OVERRIDE;
725   virtual void OnNativeWidgetDestroying() OVERRIDE;
726   virtual void OnNativeWidgetDestroyed() OVERRIDE;
727   virtual gfx::Size GetMinimumSize() OVERRIDE;
728   virtual gfx::Size GetMaximumSize() OVERRIDE;
729   virtual void OnNativeWidgetMove() OVERRIDE;
730   virtual void OnNativeWidgetSizeChanged(const gfx::Size& new_size) OVERRIDE;
731   virtual void OnNativeWidgetBeginUserBoundsChange() OVERRIDE;
732   virtual void OnNativeWidgetEndUserBoundsChange() OVERRIDE;
733   virtual bool HasFocusManager() const OVERRIDE;
734   virtual bool OnNativeWidgetPaintAccelerated(
735       const gfx::Rect& dirty_region) OVERRIDE;
736   virtual void OnNativeWidgetPaint(gfx::Canvas* canvas) OVERRIDE;
737   virtual int GetNonClientComponent(const gfx::Point& point) OVERRIDE;
738   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
739   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
740   virtual void OnMouseCaptureLost() OVERRIDE;
741   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
742   virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
743   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
744   virtual bool ExecuteCommand(int command_id) OVERRIDE;
745   virtual InputMethod* GetInputMethodDirect() OVERRIDE;
746   virtual const std::vector<ui::Layer*>& GetRootLayers() OVERRIDE;
747   virtual bool HasHitTestMask() const OVERRIDE;
748   virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE;
749   virtual Widget* AsWidget() OVERRIDE;
750   virtual const Widget* AsWidget() const OVERRIDE;
751
752   // Overridden from FocusTraversable:
753   virtual FocusSearch* GetFocusSearch() OVERRIDE;
754   virtual FocusTraversable* GetFocusTraversableParent() OVERRIDE;
755   virtual View* GetFocusTraversableParentView() OVERRIDE;
756
757  protected:
758   // Creates the RootView to be used within this Widget. Subclasses may override
759   // to create custom RootViews that do specialized event processing.
760   // TODO(beng): Investigate whether or not this is needed.
761   virtual internal::RootView* CreateRootView();
762
763   // Provided to allow the NativeWidget implementations to destroy the RootView
764   // _before_ the focus manager/tooltip manager.
765   // TODO(beng): remove once we fold those objects onto this one.
766   void DestroyRootView();
767
768  private:
769   friend class ComboboxTest;
770   friend class NativeTextfieldViewsTest;
771
772   // Sets the value of |disable_inactive_rendering_|. If the value changes,
773   // both the NonClientView and WidgetDelegate are notified.
774   void SetInactiveRenderingDisabled(bool value);
775
776   // Persists the window's restored position and "show" state using the
777   // window delegate.
778   void SaveWindowPlacement();
779
780   // Sizes and positions the window just after it is created.
781   void SetInitialBounds(const gfx::Rect& bounds);
782
783   // Sizes and positions the frameless window just after it is created.
784   void SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds);
785
786   // Returns the bounds and "show" state from the delegate. Returns true if
787   // the delegate wants to use a specified bounds.
788   bool GetSavedWindowPlacement(gfx::Rect* bounds,
789                                ui::WindowShowState* show_state);
790
791   // Creates and initializes a new InputMethod and returns it, otherwise null.
792   scoped_ptr<InputMethod> CreateInputMethod();
793
794   // Sets a different InputMethod instance to this widget. The instance
795   // must not be initialized, the ownership will be assumed by the widget.
796   // It's only for testing purpose.
797   void ReplaceInputMethod(InputMethod* input_method);
798
799   internal::NativeWidgetPrivate* native_widget_;
800
801   ObserverList<WidgetObserver> observers_;
802
803   // Non-owned pointer to the Widget's delegate. If a NULL delegate is supplied
804   // to Init() a default WidgetDelegate is created.
805   WidgetDelegate* widget_delegate_;
806
807   // The root of the View hierarchy attached to this window.
808   // WARNING: see warning in tooltip_manager_ for ordering dependencies with
809   // this and tooltip_manager_.
810   scoped_ptr<internal::RootView> root_view_;
811
812   // The View that provides the non-client area of the window (title bar,
813   // window controls, sizing borders etc). To use an implementation other than
814   // the default, this class must be sub-classed and this value set to the
815   // desired implementation before calling |InitWindow()|.
816   NonClientView* non_client_view_;
817
818   // The focus manager keeping track of focus for this Widget and any of its
819   // children.  NULL for non top-level widgets.
820   // WARNING: RootView's destructor calls into the FocusManager. As such, this
821   // must be destroyed AFTER root_view_. This is enforced in DestroyRootView().
822   scoped_ptr<FocusManager> focus_manager_;
823
824   // A theme provider to use when no other theme provider is specified.
825   scoped_ptr<ui::DefaultThemeProvider> default_theme_provider_;
826
827   // Valid for the lifetime of RunShellDrag(), indicates the view the drag
828   // started from.
829   View* dragged_view_;
830
831   // See class documentation for Widget above for a note about ownership.
832   InitParams::Ownership ownership_;
833
834   // See set_is_secondary_widget().
835   bool is_secondary_widget_;
836
837   // The current frame type in use by this window. Defaults to
838   // FRAME_TYPE_DEFAULT.
839   FrameType frame_type_;
840
841   // True when the window should be rendered as active, regardless of whether
842   // or not it actually is.
843   bool disable_inactive_rendering_;
844
845   // Set to true if the widget is in the process of closing.
846   bool widget_closed_;
847
848   // The saved "show" state for this window. See note in SetInitialBounds
849   // that explains why we save this.
850   ui::WindowShowState saved_show_state_;
851
852   // The restored bounds used for the initial show. This is only used if
853   // |saved_show_state_| is maximized.
854   gfx::Rect initial_restored_bounds_;
855
856   // Focus is automatically set to the view provided by the delegate
857   // when the widget is shown. Set this value to false to override
858   // initial focus for the widget.
859   bool focus_on_creation_;
860
861   mutable scoped_ptr<InputMethod> input_method_;
862
863   // See |is_top_level()| accessor.
864   bool is_top_level_;
865
866   // Tracks whether native widget has been initialized.
867   bool native_widget_initialized_;
868
869   // Whether native widget has been destroyed.
870   bool native_widget_destroyed_;
871
872   // TODO(beng): Remove NativeWidgetGtk's dependence on these:
873   // If true, the mouse is currently down.
874   bool is_mouse_button_pressed_;
875
876   // If true, a touch device is currently down.
877   bool is_touch_down_;
878
879   // TODO(beng): Remove NativeWidgetGtk's dependence on these:
880   // The following are used to detect duplicate mouse move events and not
881   // deliver them. Displaying a window may result in the system generating
882   // duplicate move events even though the mouse hasn't moved.
883   bool last_mouse_event_was_move_;
884   gfx::Point last_mouse_event_position_;
885
886   // True if event capture should be released on a mouse up event. Default is
887   // true.
888   bool auto_release_capture_;
889
890   // See description in GetRootLayers().
891   std::vector<ui::Layer*> root_layers_;
892
893   // Is |root_layers_| out of date?
894   bool root_layers_dirty_;
895
896   // True when window movement via mouse interaction with the frame should be
897   // disabled.
898   bool movement_disabled_;
899
900   DISALLOW_COPY_AND_ASSIGN(Widget);
901 };
902
903 }  // namespace views
904
905 #endif  // UI_VIEWS_WIDGET_WIDGET_H_