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