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