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