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_WINDOW_DIALOG_DELEGATE_H_ 6 #define UI_VIEWS_WINDOW_DIALOG_DELEGATE_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/strings/string16.h" 10 #include "ui/accessibility/ax_enums.h" 11 #include "ui/base/models/dialog_model.h" 12 #include "ui/base/ui_base_types.h" 13 #include "ui/views/widget/widget_delegate.h" 14 15 namespace views { 16 17 class DialogClientView; 18 19 /////////////////////////////////////////////////////////////////////////////// 20 // 21 // DialogDelegate 22 // 23 // DialogDelegate is an interface implemented by objects that wish to show a 24 // dialog box Window. The window that is displayed uses this interface to 25 // determine how it should be displayed and notify the delegate object of 26 // certain events. 27 // 28 /////////////////////////////////////////////////////////////////////////////// 29 class VIEWS_EXPORT DialogDelegate : public ui::DialogModel, 30 public WidgetDelegate { 31 public: 32 DialogDelegate(); 33 virtual ~DialogDelegate(); 34 35 // Same as CreateDialogWidgetWithBounds() with an empty |bounds|. 36 static Widget* CreateDialogWidget(WidgetDelegate* delegate, 37 gfx::NativeWindow context, 38 gfx::NativeView parent); 39 40 // Create a dialog widget with the specified |context| or |parent|. 41 // If |bounds| is not empty, used to initially place the dialog, otherwise 42 // a default location is used. 43 static Widget* CreateDialogWidgetWithBounds(WidgetDelegate* delegate, 44 gfx::NativeWindow context, 45 gfx::NativeView parent, 46 const gfx::Rect& bounds); 47 48 // Override this function to display an extra view adjacent to the buttons. 49 // Overrides may construct the view; this will only be called once per dialog. 50 virtual View* CreateExtraView(); 51 52 // Override this function to display an extra view in the titlebar. 53 // Overrides may construct the view; this will only be called once per dialog. 54 // Note: this only works for new style dialogs. 55 virtual View* CreateTitlebarExtraView(); 56 57 // Override this function to display a footnote view below the buttons. 58 // Overrides may construct the view; this will only be called once per dialog. 59 virtual View* CreateFootnoteView(); 60 61 // For Dialog boxes, if there is a "Cancel" button or no dialog button at all, 62 // this is called when the user presses the "Cancel" button or the Esc key. 63 // It can also be called on a close action if |Close| has not been 64 // overridden. This function should return true if the window can be closed 65 // after it returns, or false if it must remain open. 66 virtual bool Cancel(); 67 68 // For Dialog boxes, this is called when the user presses the "OK" button, 69 // or the Enter key. It can also be called on a close action if |Close| 70 // has not been overridden. This function should return true if the window 71 // can be closed after it returns, or false if it must remain open. 72 // If |window_closing| is true, it means that this handler is 73 // being called because the window is being closed (e.g. by Window::Close) 74 // and there is no Cancel handler, so Accept is being called instead. 75 virtual bool Accept(bool window_closing); 76 virtual bool Accept(); 77 78 // Called when the user closes the window without selecting an option, 79 // e.g. by pressing the close button on the window or using a window manager 80 // gesture. By default, this calls Accept() if the only button in the dialog 81 // is Accept, Cancel() otherwise. This function should return true if the 82 // window can be closed after it returns, or false if it must remain open. 83 virtual bool Close(); 84 85 // Overridden from ui::DialogModel: 86 virtual base::string16 GetDialogLabel() const OVERRIDE; 87 virtual base::string16 GetDialogTitle() const OVERRIDE; 88 virtual int GetDialogButtons() const OVERRIDE; 89 virtual int GetDefaultDialogButton() const OVERRIDE; 90 virtual bool ShouldDefaultButtonBeBlue() const OVERRIDE; 91 virtual base::string16 GetDialogButtonLabel( 92 ui::DialogButton button) const OVERRIDE; 93 virtual bool IsDialogButtonEnabled(ui::DialogButton button) const OVERRIDE; 94 95 // Overridden from WidgetDelegate: 96 virtual View* GetInitiallyFocusedView() OVERRIDE; 97 virtual DialogDelegate* AsDialogDelegate() OVERRIDE; 98 virtual ClientView* CreateClientView(Widget* widget) OVERRIDE; 99 virtual NonClientFrameView* CreateNonClientFrameView(Widget* widget) OVERRIDE; 100 101 // Create a frame view using the new dialog style. 102 static NonClientFrameView* CreateDialogFrameView(Widget* widget); 103 104 // Returns whether this particular dialog should use the new dialog style. 105 virtual bool UseNewStyleForThisDialog() const; 106 107 // Called when the window has been closed. OnClosed()108 virtual void OnClosed() {} 109 110 // A helper for accessing the DialogClientView object contained by this 111 // delegate's Window. 112 const DialogClientView* GetDialogClientView() const; 113 DialogClientView* GetDialogClientView(); 114 115 protected: 116 // Overridden from WidgetDelegate: 117 virtual ui::AXRole GetAccessibleWindowRole() const OVERRIDE; 118 119 private: 120 // A flag indicating whether this dialog supports the new style. 121 bool supports_new_style_; 122 }; 123 124 // A DialogDelegate implementation that is-a View. Used to override GetWidget() 125 // to call View's GetWidget() for the common case where a DialogDelegate 126 // implementation is-a View. Note that DialogDelegateView is not owned by 127 // view's hierarchy and is expected to be deleted on DeleteDelegate call. 128 class VIEWS_EXPORT DialogDelegateView : public DialogDelegate, 129 public View { 130 public: 131 DialogDelegateView(); 132 virtual ~DialogDelegateView(); 133 134 // Overridden from DialogDelegate: 135 virtual void DeleteDelegate() OVERRIDE; 136 virtual Widget* GetWidget() OVERRIDE; 137 virtual const Widget* GetWidget() const OVERRIDE; 138 virtual View* GetContentsView() OVERRIDE; 139 140 private: 141 DISALLOW_COPY_AND_ASSIGN(DialogDelegateView); 142 }; 143 144 } // namespace views 145 146 #endif // UI_VIEWS_WINDOW_DIALOG_DELEGATE_H_ 147