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