Home | History | Annotate | Download | only in controls
      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_CONTROLS_MESSAGE_BOX_VIEW_H_
      6 #define UI_VIEWS_CONTROLS_MESSAGE_BOX_VIEW_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/strings/string16.h"
     11 #include "ui/views/view.h"
     12 
     13 namespace gfx {
     14 class ImageSkia;
     15 }
     16 
     17 namespace views {
     18 
     19 class Checkbox;
     20 class ImageView;
     21 class Label;
     22 class Link;
     23 class LinkListener;
     24 class Textfield;
     25 
     26 // This class displays the contents of a message box. It is intended for use
     27 // within a constrained window, and has options for a message, prompt, OK
     28 // and Cancel buttons.
     29 class VIEWS_EXPORT MessageBoxView : public View {
     30  public:
     31   enum Options {
     32     NO_OPTIONS = 0,
     33     // For a message from a web page (not from Chrome's UI), such as script
     34     // dialog text, each paragraph's directionality is auto-detected using the
     35     // directionality of the paragraph's first strong character's. Please refer
     36     // to HTML5 spec for details.
     37     // http://dev.w3.org/html5/spec/Overview.html#text-rendered-in-native-user-interfaces:
     38     // The spec does not say anything about alignment. And we choose to
     39     // align all paragraphs according to the direction of the first paragraph.
     40     DETECT_DIRECTIONALITY = 1 << 0,
     41     HAS_PROMPT_FIELD = 1 << 1,
     42   };
     43 
     44   struct VIEWS_EXPORT InitParams {
     45     explicit InitParams(const string16& message);
     46     ~InitParams();
     47 
     48     uint16 options;
     49     string16 message;
     50     string16 default_prompt;
     51     int message_width;
     52     int inter_row_vertical_spacing;
     53   };
     54 
     55   explicit MessageBoxView(const InitParams& params);
     56 
     57   virtual ~MessageBoxView();
     58 
     59   // Returns the text box.
     60   views::Textfield* text_box() { return prompt_field_; }
     61 
     62   // Returns user entered data in the prompt field.
     63   string16 GetInputText();
     64 
     65   // Returns true if a checkbox is selected, false otherwise. (And false if
     66   // the message box has no checkbox.)
     67   bool IsCheckBoxSelected();
     68 
     69   // Adds |icon| to the upper left of the message box or replaces the current
     70   // icon. To start out, the message box has no icon.
     71   void SetIcon(const gfx::ImageSkia& icon);
     72 
     73   // Adds a checkbox with the specified label to the message box if this is the
     74   // first call. Otherwise, it changes the label of the current checkbox. To
     75   // start, the message box has no checkbox until this function is called.
     76   void SetCheckBoxLabel(const string16& label);
     77 
     78   // Sets the state of the check-box.
     79   void SetCheckBoxSelected(bool selected);
     80 
     81   // Sets the text and the listener of the link. If |text| is empty, the link
     82   // is removed.
     83   void SetLink(const string16& text, LinkListener* listener);
     84 
     85   // View:
     86   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
     87 
     88  protected:
     89   // View:
     90   virtual void ViewHierarchyChanged(
     91       const ViewHierarchyChangedDetails& details) OVERRIDE;
     92   // Handles Ctrl-C and writes the message in the system clipboard.
     93   virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
     94 
     95  private:
     96   // Sets up the layout manager and initializes the message labels and prompt
     97   // field. This should only be called once, from the constructor.
     98   void Init(const InitParams& params);
     99 
    100   // Sets up the layout manager based on currently initialized views. Should be
    101   // called when a view is initialized or changed.
    102   void ResetLayoutManager();
    103 
    104   // Message for the message box.
    105   std::vector<Label*> message_labels_;
    106 
    107   // Input text field for the message box.
    108   Textfield* prompt_field_;
    109 
    110   // Icon displayed in the upper left corner of the message box.
    111   ImageView* icon_;
    112 
    113   // Checkbox for the message box.
    114   Checkbox* checkbox_;
    115 
    116   // Link displayed at the bottom of the view.
    117   Link* link_;
    118 
    119   // Maximum width of the message label.
    120   int message_width_;
    121 
    122   // Spacing between rows in the grid layout.
    123   int inter_row_vertical_spacing_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(MessageBoxView);
    126 };
    127 
    128 }  // namespace views
    129 
    130 #endif  // UI_VIEWS_CONTROLS_MESSAGE_BOX_VIEW_H_
    131