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.
5 #ifndef UI_VIEWS_WIDGET_WIDGET_H_
6 #define UI_VIEWS_WIDGET_WIDGET_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/scoped_observer.h"
16 #include "ui/aura/window_layer_type.h"
17 #include "ui/base/ui_base_types.h"
18 #include "ui/events/event_source.h"
19 #include "ui/gfx/native_widget_types.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/native_theme/native_theme_observer.h"
22 #include "ui/views/focus/focus_manager.h"
23 #include "ui/views/widget/native_widget_delegate.h"
24 #include "ui/views/window/client_view.h"
25 #include "ui/views/window/non_client_view.h"
28 // Windows headers define macros for these function names which screw with us.
29 #if defined(IsMaximized)
32 #if defined(IsMinimized)
35 #if defined(CreateWindow)
49 class DefaultThemeProvider;
59 class DesktopWindowTreeHost;
62 class NonClientFrameView;
67 class WidgetRemovalsObserver;
70 class NativeWidgetPrivate;
74 ////////////////////////////////////////////////////////////////////////////////
77 // Encapsulates the platform-specific rendering, event receiving and widget
78 // management aspects of the UI framework.
80 // Owns a RootView and thus a View hierarchy. Can contain child Widgets.
81 // Widget is a platform-independent type that communicates with a platform or
82 // context specific NativeWidget implementation.
84 // A special note on ownership:
86 // Depending on the value of the InitParams' ownership field, the Widget
87 // either owns or is owned by its NativeWidget:
89 // ownership = NATIVE_WIDGET_OWNS_WIDGET (default)
90 // The Widget instance is owned by its NativeWidget. When the NativeWidget
91 // is destroyed (in response to a native destruction message), it deletes
92 // the Widget from its destructor.
93 // ownership = WIDGET_OWNS_NATIVE_WIDGET (non-default)
94 // The Widget instance owns its NativeWidget. This state implies someone
95 // else wants to control the lifetime of this object. When they destroy
96 // the Widget it is responsible for destroying the NativeWidget (from its
99 class VIEWS_EXPORT Widget : public internal::NativeWidgetDelegate,
100 public ui::EventSource,
101 public FocusTraversable,
102 public ui::NativeThemeObserver {
104 typedef std::set<Widget*> Widgets;
107 FRAME_TYPE_DEFAULT, // Use whatever the default would be.
108 FRAME_TYPE_FORCE_CUSTOM, // Force the custom frame.
109 FRAME_TYPE_FORCE_NATIVE // Force the native frame.
112 // Result from RunMoveLoop().
113 enum MoveLoopResult {
114 // The move loop completed successfully.
115 MOVE_LOOP_SUCCESSFUL,
117 // The user canceled the move loop.
121 // Source that initiated the move loop.
122 enum MoveLoopSource {
123 MOVE_LOOP_SOURCE_MOUSE,
124 MOVE_LOOP_SOURCE_TOUCH,
127 // Behavior when escape is pressed during a move loop.
128 enum MoveLoopEscapeBehavior {
129 // Indicates the window should be hidden.
130 MOVE_LOOP_ESCAPE_BEHAVIOR_HIDE,
132 // Indicates the window should not be hidden.
133 MOVE_LOOP_ESCAPE_BEHAVIOR_DONT_HIDE,
136 struct VIEWS_EXPORT InitParams {
138 TYPE_WINDOW, // A decorated Window, like a frame window.
139 // Widgets of TYPE_WINDOW will have a NonClientView.
140 TYPE_PANEL, // Always on top window managed by PanelManager.
141 // Widgets of TYPE_PANEL will have a NonClientView.
142 TYPE_WINDOW_FRAMELESS,
143 // An undecorated Window.
144 TYPE_CONTROL, // A control, like a button.
145 TYPE_POPUP, // An undecorated Window, with transient properties.
146 TYPE_MENU, // An undecorated Window, with transient properties
147 // specialized to menus.
150 TYPE_DRAG, // An undecorated Window, used during a drag-and-drop to
151 // show the drag image.
155 // Infer fully opaque or not. For WinAura, top-level windows that are not
156 // of TYPE_WINDOW are translucent so that they can be made to fade in. In
157 // all other cases, windows are fully opaque.
161 // Possibly translucent/transparent.
166 // Infer whether the window should be activatable from the window type.
174 // Default. Creator is not responsible for managing the lifetime of the
175 // Widget, it is destroyed when the corresponding NativeWidget is
177 NATIVE_WIDGET_OWNS_WIDGET,
178 // Used when the Widget is owned by someone other than the NativeWidget,
179 // e.g. a scoped_ptr in tests.
180 WIDGET_OWNS_NATIVE_WIDGET
184 SHADOW_TYPE_DEFAULT, // Use default shadow setting. It will be one of
185 // the settings below depending on InitParams::type
186 // and the native widget's type.
187 SHADOW_TYPE_NONE, // Don't draw any shadow.
188 SHADOW_TYPE_DROP, // Draw a drop shadow that emphasizes Z-order
189 // relationship to other windows.
193 explicit InitParams(Type type);
197 // If NULL, a default implementation will be constructed.
198 WidgetDelegate* delegate;
200 // If TRANSLUCENT_WINDOW, the widget may be fully or partially transparent.
201 // Translucent windows may not always be supported. Use
202 // IsTranslucentWindowOpacitySupported to determine if translucent windows
204 // If OPAQUE_WINDOW, we can perform optimizations based on the widget being
205 // fully opaque. Defaults to TRANSLUCENT_WINDOW if
206 // ViewsDelegate::UseTransparentWindows(). Defaults to OPAQUE_WINDOW for
207 // non-window widgets.
208 WindowOpacity opacity;
210 Activatable activatable;
212 bool visible_on_all_workspaces;
214 bool mirror_origin_in_rtl;
215 ShadowType shadow_type;
216 // Specifies that the system default caption and icon should not be
217 // rendered, and that the client area should be equivalent to the window
218 // area. Only used on some platforms (Windows and Linux).
219 bool remove_standard_frame;
220 // Only used by ShellWindow on Windows. Specifies that the default icon of
221 // packaged app should be the system default icon.
222 bool use_system_default_icon;
223 // Whether the widget should be maximized or minimized.
224 ui::WindowShowState show_state;
225 // Should the widget be double buffered? Default is false.
227 gfx::NativeView parent;
228 // Specifies the initial bounds of the Widget. Default is empty, which means
229 // the NativeWidget may specify a default size. If the parent is specified,
230 // |bounds| is in the parent's coordinate system. If the parent is not
231 // specified, it's in screen's global coordinate system.
233 // When set, this value is used as the Widget's NativeWidget implementation.
234 // The Widget will not construct a default one. Default is NULL.
235 NativeWidget* native_widget;
236 // Aura-only. Provides a DesktopWindowTreeHost implementation to use instead
237 // of the default one.
238 // TODO(beng): Figure out if there's a better way to expose this, e.g. get
239 // rid of NW subclasses and do this all via message handling.
240 DesktopWindowTreeHost* desktop_window_tree_host;
241 // Only used by NativeWidgetAura. Specifies the type of layer for the
242 // aura::Window. Default is WINDOW_LAYER_TEXTURED.
243 aura::WindowLayerType layer_type;
244 // Only used by Aura. Provides a context window whose RootWindow is
245 // consulted during widget creation to determine where in the Window
246 // hierarchy this widget should be placed. (This is separate from |parent|;
247 // if you pass a RootWindow to |parent|, your window will be parented to
248 // |parent|. If you pass a RootWindow to |context|, we ask that RootWindow
249 // where it wants your window placed.) NULL is not allowed if you are using
251 gfx::NativeWindow context;
252 // If true, forces the window to be shown in the taskbar, even for window
253 // types that do not appear in the taskbar by default (popup and bubble).
254 bool force_show_in_taskbar;
255 // Only used by X11, for root level windows. Specifies the res_name and
256 // res_class fields, respectively, of the WM_CLASS window property. Controls
257 // window grouping and desktop file matching in Linux window managers.
258 std::string wm_role_name;
259 std::string wm_class_name;
260 std::string wm_class_class;
261 // Only used by X11, for root level windows. Specifies the PID set in
262 // _NET_WM_PID window property.
269 // Creates a toplevel window with no context. These methods should only be
270 // used in cases where there is no contextual information because we're
271 // creating a toplevel window connected to no other event.
273 // If you have any parenting or context information, or can pass that
274 // information, prefer the WithParent or WithContext versions of these
276 static Widget* CreateWindow(WidgetDelegate* delegate);
277 static Widget* CreateWindowWithBounds(WidgetDelegate* delegate,
278 const gfx::Rect& bounds);
280 // Creates a decorated window Widget with the specified properties.
281 static Widget* CreateWindowWithParent(WidgetDelegate* delegate,
282 gfx::NativeView parent);
283 static Widget* CreateWindowWithParentAndBounds(WidgetDelegate* delegate,
284 gfx::NativeView parent,
285 const gfx::Rect& bounds);
287 // Creates a decorated window Widget in the same desktop context as |context|.
288 static Widget* CreateWindowWithContext(WidgetDelegate* delegate,
289 gfx::NativeWindow context);
290 static Widget* CreateWindowWithContextAndBounds(WidgetDelegate* delegate,
291 gfx::NativeWindow context,
292 const gfx::Rect& bounds);
294 // Closes all Widgets that aren't identified as "secondary widgets". Called
295 // during application shutdown when the last non-secondary widget is closed.
296 static void CloseAllSecondaryWidgets();
298 // Converts a rectangle from one Widget's coordinate system to another's.
299 // Returns false if the conversion couldn't be made, because either these two
300 // Widgets do not have a common ancestor or they are not on the screen yet.
301 // The value of |*rect| won't be changed when false is returned.
302 static bool ConvertRect(const Widget* source,
303 const Widget* target,
306 // Retrieves the Widget implementation associated with the given
307 // NativeView or Window, or NULL if the supplied handle has no associated
309 static Widget* GetWidgetForNativeView(gfx::NativeView native_view);
310 static Widget* GetWidgetForNativeWindow(gfx::NativeWindow native_window);
312 // Retrieves the top level widget in a native view hierarchy
313 // starting at |native_view|. Top level widget is a widget with TYPE_WINDOW,
314 // TYPE_PANEL, TYPE_WINDOW_FRAMELESS, POPUP or MENU and has its own
315 // focus manager. This may be itself if the |native_view| is top level,
316 // or NULL if there is no toplevel in a native view hierarchy.
317 static Widget* GetTopLevelWidgetForNativeView(gfx::NativeView native_view);
319 // Returns all Widgets in |native_view|'s hierarchy, including itself if
321 static void GetAllChildWidgets(gfx::NativeView native_view,
324 // Returns all non-child Widgets owned by |native_view|.
325 static void GetAllOwnedWidgets(gfx::NativeView native_view,
328 // Re-parent a NativeView and notify all Widgets in |native_view|'s hierarchy
330 static void ReparentNativeView(gfx::NativeView native_view,
331 gfx::NativeView new_parent);
333 // Returns the preferred size of the contents view of this window based on
334 // its localized size data. The width in cols is held in a localized string
335 // resource identified by |col_resource_id|, the height in the same fashion.
336 // TODO(beng): This should eventually live somewhere else, probably closer to
338 static int GetLocalizedContentsWidth(int col_resource_id);
339 static int GetLocalizedContentsHeight(int row_resource_id);
340 static gfx::Size GetLocalizedContentsSize(int col_resource_id,
341 int row_resource_id);
343 // Returns true if the specified type requires a NonClientView.
344 static bool RequiresNonClientView(InitParams::Type type);
346 void Init(const InitParams& params);
348 // Returns the gfx::NativeView associated with this Widget.
349 gfx::NativeView GetNativeView() const;
351 // Returns the gfx::NativeWindow associated with this Widget. This may return
352 // NULL on some platforms if the widget was created with a type other than
353 // TYPE_WINDOW or TYPE_PANEL.
354 gfx::NativeWindow GetNativeWindow() const;
356 // Add/remove observer.
357 void AddObserver(WidgetObserver* observer);
358 void RemoveObserver(WidgetObserver* observer);
359 bool HasObserver(WidgetObserver* observer);
361 // Add/remove removals observer.
362 void AddRemovalsObserver(WidgetRemovalsObserver* observer);
363 void RemoveRemovalsObserver(WidgetRemovalsObserver* observer);
364 bool HasRemovalsObserver(WidgetRemovalsObserver* observer);
366 // Returns the accelerator given a command id. Returns false if there is
367 // no accelerator associated with a given id, which is a common condition.
368 virtual bool GetAccelerator(int cmd_id, ui::Accelerator* accelerator) const;
370 // Forwarded from the RootView so that the widget can do any cleanup.
371 void ViewHierarchyChanged(const View::ViewHierarchyChangedDetails& details);
373 // Called right before changing the widget's parent NativeView to do any
375 void NotifyNativeViewHierarchyWillChange();
377 // Called after changing the widget's parent NativeView. Notifies the RootView
379 void NotifyNativeViewHierarchyChanged();
381 // Called immediately before removing |view| from this widget.
382 void NotifyWillRemoveView(View* view);
384 // Returns the top level widget in a hierarchy (see is_top_level() for
385 // the definition of top level widget.) Will return NULL if called
386 // before the widget is attached to the top level widget's hierarchy.
387 Widget* GetTopLevelWidget();
388 const Widget* GetTopLevelWidget() const;
390 // Gets/Sets the WidgetDelegate.
391 WidgetDelegate* widget_delegate() const { return widget_delegate_; }
393 // Sets the specified view as the contents of this Widget. There can only
394 // be one contents view child of this Widget's RootView. This view is sized to
395 // fit the entire size of the RootView. The RootView takes ownership of this
396 // View, unless it is set as not being parent-owned.
397 void SetContentsView(View* view);
398 View* GetContentsView();
400 // Returns the bounds of the Widget in screen coordinates.
401 gfx::Rect GetWindowBoundsInScreen() const;
403 // Returns the bounds of the Widget's client area in screen coordinates.
404 gfx::Rect GetClientAreaBoundsInScreen() const;
406 // Retrieves the restored bounds for the window.
407 gfx::Rect GetRestoredBounds() const;
409 // Sizes and/or places the widget to the specified bounds, size or position.
410 void SetBounds(const gfx::Rect& bounds);
411 void SetSize(const gfx::Size& size);
413 // Sizes the window to the specified size and centerizes it.
414 void CenterWindow(const gfx::Size& size);
416 // Like SetBounds(), but ensures the Widget is fully visible on screen,
417 // resizing and/or repositioning as necessary. This is only useful for
418 // non-child widgets.
419 void SetBoundsConstrained(const gfx::Rect& bounds);
421 // Sets whether animations that occur when visibility is changed are enabled.
423 void SetVisibilityChangedAnimationsEnabled(bool value);
425 // Starts a nested message loop that moves the window. This can be used to
426 // start a window move operation from a mouse or touch event. This returns
427 // when the move completes. |drag_offset| is the offset from the top left
428 // corner of the window to the point where the cursor is dragging, and is used
429 // to offset the bounds of the window from the cursor.
430 MoveLoopResult RunMoveLoop(const gfx::Vector2d& drag_offset,
431 MoveLoopSource source,
432 MoveLoopEscapeBehavior escape_behavior);
434 // Stops a previously started move loop. This is not immediate.
437 // Places the widget in front of the specified widget in z-order.
438 void StackAboveWidget(Widget* widget);
439 void StackAbove(gfx::NativeView native_view);
442 // Places the widget below the specified NativeView.
443 void StackBelow(gfx::NativeView native_view);
445 // Sets a shape on the widget. Passing a NULL |shape| reverts the widget to
446 // be rectangular. Takes ownership of |shape|.
447 void SetShape(gfx::NativeRegion shape);
449 // Hides the widget then closes it after a return to the message loop.
450 virtual void Close();
452 // TODO(beng): Move off public API.
453 // Closes the widget immediately. Compare to |Close|. This will destroy the
454 // window handle associated with this Widget, so should not be called from
455 // any code that expects it to be valid beyond this call.
458 // Whether the widget has been asked to close itself. In particular this is
459 // set to true after Close() has been invoked on the NativeWidget.
460 bool IsClosed() const;
462 // Shows the widget. The widget is activated if during initialization the
463 // can_activate flag in the InitParams structure is set to true.
468 // Like Show(), but does not activate the window.
471 // Activates the widget, assuming it already exists and is visible.
474 // Deactivates the widget, making the next window in the Z order the active
478 // Returns whether the Widget is the currently active window.
479 virtual bool IsActive() const;
481 // Prevents the window from being rendered as deactivated. This state is
482 // reset automatically as soon as the window becomes activated again. There is
483 // no ability to control the state through this API as this leads to sync
485 void DisableInactiveRendering();
487 // Sets the widget to be on top of all other widgets in the windowing system.
488 void SetAlwaysOnTop(bool on_top);
490 // Returns whether the widget has been set to be on top of most other widgets
491 // in the windowing system.
492 bool IsAlwaysOnTop() const;
494 // Sets the widget to be visible on all work spaces.
495 void SetVisibleOnAllWorkspaces(bool always_visible);
497 // Maximizes/minimizes/restores the window.
502 // Whether or not the window is maximized or minimized.
503 virtual bool IsMaximized() const;
504 bool IsMinimized() const;
506 // Accessors for fullscreen state.
507 void SetFullscreen(bool fullscreen);
508 bool IsFullscreen() const;
510 // Sets the opacity of the widget. This may allow widgets behind the widget
511 // in the Z-order to become visible, depending on the capabilities of the
512 // underlying windowing system.
513 void SetOpacity(unsigned char opacity);
515 // Sets whether or not the window should show its frame as a "transient drag
516 // frame" - slightly transparent and without the standard window controls.
517 void SetUseDragFrame(bool use_drag_frame);
519 // Flashes the frame of the window to draw attention to it. Currently only
520 // implemented on Windows for non-Aura.
521 void FlashFrame(bool flash);
523 // Returns the View at the root of the View hierarchy contained by this
526 const View* GetRootView() const;
528 // A secondary widget is one that is automatically closed (via Close()) when
529 // all non-secondary widgets are closed.
531 // TODO(beng): This is an ugly API, should be handled implicitly via
533 void set_is_secondary_widget(bool is_secondary_widget) {
534 is_secondary_widget_ = is_secondary_widget;
536 bool is_secondary_widget() const { return is_secondary_widget_; }
538 // Returns whether the Widget is visible to the user.
539 virtual bool IsVisible() const;
541 // Returns the ThemeProvider that provides theme resources for this Widget.
542 virtual ui::ThemeProvider* GetThemeProvider() const;
544 ui::NativeTheme* GetNativeTheme() {
545 return const_cast<ui::NativeTheme*>(
546 const_cast<const Widget*>(this)->GetNativeTheme());
548 const ui::NativeTheme* GetNativeTheme() const;
550 // Returns the FocusManager for this widget.
551 // Note that all widgets in a widget hierarchy share the same focus manager.
552 FocusManager* GetFocusManager();
553 const FocusManager* GetFocusManager() const;
555 // Returns the InputMethod for this widget.
556 // Note that all widgets in a widget hierarchy share the same input method.
557 InputMethod* GetInputMethod();
558 const InputMethod* GetInputMethod() const;
560 // Returns the ui::InputMethod for this widget.
561 // TODO(yukishiino): Rename this method to GetInputMethod once we remove
562 // views::InputMethod.
563 ui::InputMethod* GetHostInputMethod();
565 // Starts a drag operation for the specified view. This blocks until the drag
566 // operation completes. |view| can be NULL.
567 // If the view is non-NULL it can be accessed during the drag by calling
568 // dragged_view(). If the view has not been deleted during the drag,
569 // OnDragDone() is called on it. |location| is in the widget's coordinate
571 void RunShellDrag(View* view,
572 const ui::OSExchangeData& data,
573 const gfx::Point& location,
575 ui::DragDropTypes::DragEventSource source);
577 // Returns the view that requested the current drag operation via
578 // RunShellDrag(), or NULL if there is no such view or drag operation.
579 View* dragged_view() { return dragged_view_; }
581 // Adds the specified |rect| in client area coordinates to the rectangle to be
583 virtual void SchedulePaintInRect(const gfx::Rect& rect);
585 // Sets the currently visible cursor. If |cursor| is NULL, the cursor used
586 // before the current is restored.
587 void SetCursor(gfx::NativeCursor cursor);
589 // Returns true if and only if mouse events are enabled.
590 bool IsMouseEventsEnabled() const;
592 // Sets/Gets a native window property on the underlying native window object.
593 // Returns NULL if the property does not exist. Setting the property value to
594 // NULL removes the property.
595 void SetNativeWindowProperty(const char* name, void* value);
596 void* GetNativeWindowProperty(const char* name) const;
598 // Tell the window to update its title from the delegate.
599 void UpdateWindowTitle();
601 // Tell the window to update its icon from the delegate.
602 void UpdateWindowIcon();
604 // Retrieves the focus traversable for this widget.
605 FocusTraversable* GetFocusTraversable();
607 // Notifies the view hierarchy contained in this widget that theme resources
611 // Notifies the view hierarchy contained in this widget that locale resources
613 void LocaleChanged();
615 void SetFocusTraversableParent(FocusTraversable* parent);
616 void SetFocusTraversableParentView(View* parent_view);
618 // Clear native focus set to the Widget's NativeWidget.
619 void ClearNativeFocus();
621 void set_frame_type(FrameType frame_type) { frame_type_ = frame_type; }
622 FrameType frame_type() const { return frame_type_; }
624 // Creates an appropriate NonClientFrameView for this widget. The
625 // WidgetDelegate is given the first opportunity to create one, followed by
626 // the NativeWidget implementation. If both return NULL, a default one is
628 virtual NonClientFrameView* CreateNonClientFrameView();
630 // Whether we should be using a native frame.
631 bool ShouldUseNativeFrame() const;
633 // Determines whether the window contents should be rendered transparently
634 // (for example, so that they can overhang onto the window title bar).
635 bool ShouldWindowContentsBeTransparent() const;
637 // Forces the frame into the alternate frame type (custom or native) depending
638 // on its current state.
639 void DebugToggleFrameType();
641 // Tell the window that something caused the frame type to change.
642 void FrameTypeChanged();
644 NonClientView* non_client_view() {
645 return const_cast<NonClientView*>(
646 const_cast<const Widget*>(this)->non_client_view());
648 const NonClientView* non_client_view() const {
649 return non_client_view_;
652 ClientView* client_view() {
653 return const_cast<ClientView*>(
654 const_cast<const Widget*>(this)->client_view());
656 const ClientView* client_view() const {
657 // non_client_view_ may be NULL, especially during creation.
658 return non_client_view_ ? non_client_view_->client_view() : NULL;
661 const ui::Compositor* GetCompositor() const;
662 ui::Compositor* GetCompositor();
664 // Returns the widget's layer, if any.
665 ui::Layer* GetLayer();
667 // Reorders the widget's child NativeViews which are associated to the view
668 // tree (eg via a NativeViewHost) to match the z-order of the views in the
669 // view tree. The z-order of views with layers relative to views with
670 // associated NativeViews is used to reorder the NativeView layers. This
671 // method assumes that the widget's child layers which are owned by a view are
672 // already in the correct z-order relative to each other and does no
673 // reordering if there are no views with an associated NativeView.
674 void ReorderNativeViews();
676 // Schedules an update to the root layers. The actual processing occurs when
677 // GetRootLayers() is invoked.
678 void UpdateRootLayers();
680 const NativeWidget* native_widget() const;
681 NativeWidget* native_widget();
683 internal::NativeWidgetPrivate* native_widget_private() {
684 return native_widget_;
686 const internal::NativeWidgetPrivate* native_widget_private() const {
687 return native_widget_;
690 // Sets capture to the specified view. This makes it so that all mouse, touch
691 // and gesture events go to |view|. If |view| is NULL, the widget still
692 // obtains event capture, but the events will go to the view they'd normally
694 void SetCapture(View* view);
697 void ReleaseCapture();
699 // Returns true if the widget has capture.
702 void set_auto_release_capture(bool auto_release_capture) {
703 auto_release_capture_ = auto_release_capture;
706 // Returns the font used for tooltips.
707 TooltipManager* GetTooltipManager();
708 const TooltipManager* GetTooltipManager() const;
710 void set_focus_on_creation(bool focus_on_creation) {
711 focus_on_creation_ = focus_on_creation;
714 // True if the widget is considered top level widget. Top level widget
715 // is a widget of TYPE_WINDOW, TYPE_PANEL, TYPE_WINDOW_FRAMELESS, BUBBLE,
716 // POPUP or MENU, and has a focus manager and input method object associated
717 // with it. TYPE_CONTROL and TYPE_TOOLTIP is not considered top level.
718 bool is_top_level() const { return is_top_level_; }
720 // True when window movement via mouse interaction with the frame is disabled.
721 bool movement_disabled() const { return movement_disabled_; }
722 void set_movement_disabled(bool disabled) { movement_disabled_ = disabled; }
724 // Returns the work area bounds of the screen the Widget belongs to.
725 gfx::Rect GetWorkAreaBoundsInScreen() const;
727 // Creates and dispatches synthesized mouse move event using the current
728 // mouse location to refresh hovering status in the widget.
729 void SynthesizeMouseMoveEvent();
731 // Called by our RootView after it has performed a Layout. Used to forward
732 // window sizing information to the window server on some platforms.
733 void OnRootViewLayout();
735 // Whether the widget supports translucency.
736 bool IsTranslucentWindowOpacitySupported() const;
738 // Called when the delegate's CanResize or CanMaximize changes.
739 void OnSizeConstraintsChanged();
741 // Notification that our owner is closing.
742 // NOTE: this is not invoked for aura as it's currently not needed there.
743 // Under aura menus close by way of activation getting reset when the owner
745 virtual void OnOwnerClosing();
747 // Overridden from NativeWidgetDelegate:
748 virtual bool IsModal() const OVERRIDE;
749 virtual bool IsDialogBox() const OVERRIDE;
750 virtual bool CanActivate() const OVERRIDE;
751 virtual bool IsInactiveRenderingDisabled() const OVERRIDE;
752 virtual void EnableInactiveRendering() OVERRIDE;
753 virtual void OnNativeWidgetActivationChanged(bool active) OVERRIDE;
754 virtual void OnNativeFocus(gfx::NativeView old_focused_view) OVERRIDE;
755 virtual void OnNativeBlur(gfx::NativeView new_focused_view) OVERRIDE;
756 virtual void OnNativeWidgetVisibilityChanging(bool visible) OVERRIDE;
757 virtual void OnNativeWidgetVisibilityChanged(bool visible) OVERRIDE;
758 virtual void OnNativeWidgetCreated(bool desktop_widget) OVERRIDE;
759 virtual void OnNativeWidgetDestroying() OVERRIDE;
760 virtual void OnNativeWidgetDestroyed() OVERRIDE;
761 virtual gfx::Size GetMinimumSize() const OVERRIDE;
762 virtual gfx::Size GetMaximumSize() const OVERRIDE;
763 virtual void OnNativeWidgetMove() OVERRIDE;
764 virtual void OnNativeWidgetSizeChanged(const gfx::Size& new_size) OVERRIDE;
765 virtual void OnNativeWidgetWindowShowStateChanged() OVERRIDE;
766 virtual void OnNativeWidgetBeginUserBoundsChange() OVERRIDE;
767 virtual void OnNativeWidgetEndUserBoundsChange() OVERRIDE;
768 virtual bool HasFocusManager() const OVERRIDE;
769 virtual bool OnNativeWidgetPaintAccelerated(
770 const gfx::Rect& dirty_region) OVERRIDE;
771 virtual void OnNativeWidgetPaint(gfx::Canvas* canvas) OVERRIDE;
772 virtual int GetNonClientComponent(const gfx::Point& point) OVERRIDE;
773 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
774 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
775 virtual void OnMouseCaptureLost() OVERRIDE;
776 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
777 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
778 virtual bool ExecuteCommand(int command_id) OVERRIDE;
779 virtual InputMethod* GetInputMethodDirect() OVERRIDE;
780 virtual const std::vector<ui::Layer*>& GetRootLayers() OVERRIDE;
781 virtual bool HasHitTestMask() const OVERRIDE;
782 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE;
783 virtual Widget* AsWidget() OVERRIDE;
784 virtual const Widget* AsWidget() const OVERRIDE;
785 virtual bool SetInitialFocus(ui::WindowShowState show_state) OVERRIDE;
787 // Overridden from ui::EventSource:
788 virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
790 // Overridden from FocusTraversable:
791 virtual FocusSearch* GetFocusSearch() OVERRIDE;
792 virtual FocusTraversable* GetFocusTraversableParent() OVERRIDE;
793 virtual View* GetFocusTraversableParentView() OVERRIDE;
795 // Overridden from ui::NativeThemeObserver:
796 virtual void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) OVERRIDE;
799 // Creates the RootView to be used within this Widget. Subclasses may override
800 // to create custom RootViews that do specialized event processing.
801 // TODO(beng): Investigate whether or not this is needed.
802 virtual internal::RootView* CreateRootView();
804 // Provided to allow the NativeWidget implementations to destroy the RootView
805 // _before_ the focus manager/tooltip manager.
806 // TODO(beng): remove once we fold those objects onto this one.
807 void DestroyRootView();
809 // Notification that a drag will start. Default implementation does nothing.
810 virtual void OnDragWillStart();
812 // Notification that the drag performed by RunShellDrag() has completed.
813 virtual void OnDragComplete();
816 friend class ComboboxTest;
817 friend class TextfieldTest;
819 // Sets the value of |disable_inactive_rendering_|. If the value changes,
820 // both the NonClientView and WidgetDelegate are notified.
821 void SetInactiveRenderingDisabled(bool value);
823 // Persists the window's restored position and "show" state using the
825 void SaveWindowPlacement();
827 // Invokes SaveWindowPlacement() if the native widget has been initialized.
828 // This is called at times when the native widget may not have been
830 void SaveWindowPlacementIfInitialized();
832 // Sizes and positions the window just after it is created.
833 void SetInitialBounds(const gfx::Rect& bounds);
835 // Sizes and positions the frameless window just after it is created.
836 void SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds);
838 // Returns the bounds and "show" state from the delegate. Returns true if
839 // the delegate wants to use a specified bounds.
840 bool GetSavedWindowPlacement(gfx::Rect* bounds,
841 ui::WindowShowState* show_state);
843 // Creates and initializes a new InputMethod and returns it, otherwise null.
844 scoped_ptr<InputMethod> CreateInputMethod();
846 // Sets a different InputMethod instance to this widget. The instance
847 // must not be initialized, the ownership will be assumed by the widget.
848 // It's only for testing purpose.
849 void ReplaceInputMethod(InputMethod* input_method);
851 internal::NativeWidgetPrivate* native_widget_;
853 ObserverList<WidgetObserver> observers_;
855 ObserverList<WidgetRemovalsObserver> removals_observers_;
857 // Non-owned pointer to the Widget's delegate. If a NULL delegate is supplied
858 // to Init() a default WidgetDelegate is created.
859 WidgetDelegate* widget_delegate_;
861 // The root of the View hierarchy attached to this window.
862 // WARNING: see warning in tooltip_manager_ for ordering dependencies with
863 // this and tooltip_manager_.
864 scoped_ptr<internal::RootView> root_view_;
866 // The View that provides the non-client area of the window (title bar,
867 // window controls, sizing borders etc). To use an implementation other than
868 // the default, this class must be sub-classed and this value set to the
869 // desired implementation before calling |InitWindow()|.
870 NonClientView* non_client_view_;
872 // The focus manager keeping track of focus for this Widget and any of its
873 // children. NULL for non top-level widgets.
874 // WARNING: RootView's destructor calls into the FocusManager. As such, this
875 // must be destroyed AFTER root_view_. This is enforced in DestroyRootView().
876 scoped_ptr<FocusManager> focus_manager_;
878 // A theme provider to use when no other theme provider is specified.
879 scoped_ptr<ui::DefaultThemeProvider> default_theme_provider_;
881 // Valid for the lifetime of RunShellDrag(), indicates the view the drag
885 // See class documentation for Widget above for a note about ownership.
886 InitParams::Ownership ownership_;
888 // See set_is_secondary_widget().
889 bool is_secondary_widget_;
891 // The current frame type in use by this window. Defaults to
892 // FRAME_TYPE_DEFAULT.
893 FrameType frame_type_;
895 // True when the window should be rendered as active, regardless of whether
896 // or not it actually is.
897 bool disable_inactive_rendering_;
899 // Set to true if the widget is in the process of closing.
902 // The saved "show" state for this window. See note in SetInitialBounds
903 // that explains why we save this.
904 ui::WindowShowState saved_show_state_;
906 // The restored bounds used for the initial show. This is only used if
907 // |saved_show_state_| is maximized.
908 gfx::Rect initial_restored_bounds_;
910 // Focus is automatically set to the view provided by the delegate
911 // when the widget is shown. Set this value to false to override
912 // initial focus for the widget.
913 bool focus_on_creation_;
915 mutable scoped_ptr<InputMethod> input_method_;
917 // See |is_top_level()| accessor.
920 // Tracks whether native widget has been initialized.
921 bool native_widget_initialized_;
923 // Whether native widget has been destroyed.
924 bool native_widget_destroyed_;
926 // TODO(beng): Remove NativeWidgetGtk's dependence on these:
927 // If true, the mouse is currently down.
928 bool is_mouse_button_pressed_;
930 // True if capture losses should be ignored.
931 bool ignore_capture_loss_;
933 // TODO(beng): Remove NativeWidgetGtk's dependence on these:
934 // The following are used to detect duplicate mouse move events and not
935 // deliver them. Displaying a window may result in the system generating
936 // duplicate move events even though the mouse hasn't moved.
937 bool last_mouse_event_was_move_;
938 gfx::Point last_mouse_event_position_;
940 // True if event capture should be released on a mouse up event. Default is
942 bool auto_release_capture_;
944 // See description in GetRootLayers().
945 std::vector<ui::Layer*> root_layers_;
947 // Is |root_layers_| out of date?
948 bool root_layers_dirty_;
950 // True when window movement via mouse interaction with the frame should be
952 bool movement_disabled_;
954 ScopedObserver<ui::NativeTheme, ui::NativeThemeObserver> observer_manager_;
956 DISALLOW_COPY_AND_ASSIGN(Widget);
961 #endif // UI_VIEWS_WIDGET_WIDGET_H_