Home | History | Annotate | Download | only in combobox
      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_COMBOBOX_NATIVE_COMBOBOX_VIEWS_H_
      6 #define UI_VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_VIEWS_H_
      7 
      8 #include "base/time/time.h"
      9 #include "ui/views/controls/combobox/native_combobox_wrapper.h"
     10 #include "ui/views/controls/menu/menu_delegate.h"
     11 #include "ui/views/view.h"
     12 
     13 namespace gfx {
     14 class Canvas;
     15 class Font;
     16 }
     17 
     18 namespace views {
     19 
     20 class FocusableBorder;
     21 class MenuRunner;
     22 
     23 // A views/skia only implementation of NativeComboboxWrapper.
     24 // No platform specific code is used.
     25 class NativeComboboxViews : public views::View,
     26                             public NativeComboboxWrapper,
     27                             public views::MenuDelegate {
     28  public:
     29   static const char kViewClassName[];
     30 
     31   explicit NativeComboboxViews(Combobox* combobox);
     32   virtual ~NativeComboboxViews();
     33 
     34   // views::View overrides:
     35   virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE;
     36   virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE;
     37   virtual bool OnKeyPressed(const ui::KeyEvent& key_event) OVERRIDE;
     38   virtual bool OnKeyReleased(const ui::KeyEvent& key_event) OVERRIDE;
     39   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
     40   virtual void OnFocus() OVERRIDE;
     41   virtual void OnBlur() OVERRIDE;
     42 
     43   // ui::EventHandler overrides:
     44   virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE;
     45 
     46   // NativeComboboxWrapper overrides:
     47   virtual void UpdateFromModel() OVERRIDE;
     48   virtual void UpdateSelectedIndex() OVERRIDE;
     49   virtual void UpdateEnabled() OVERRIDE;
     50   virtual int GetSelectedIndex() const OVERRIDE;
     51   virtual bool IsDropdownOpen() const OVERRIDE;
     52   virtual gfx::Size GetPreferredSize() OVERRIDE;
     53   virtual View* GetView() OVERRIDE;
     54   virtual void SetFocus() OVERRIDE;
     55   virtual void ValidityStateChanged() OVERRIDE;
     56   virtual bool HandleKeyPressed(const ui::KeyEvent& event) OVERRIDE;
     57   virtual bool HandleKeyReleased(const ui::KeyEvent& event) OVERRIDE;
     58   virtual void HandleFocus() OVERRIDE;
     59   virtual void HandleBlur() OVERRIDE;
     60   virtual gfx::NativeView GetTestingHandle() const OVERRIDE;
     61 
     62   // MenuDelegate overrides:
     63   virtual bool IsItemChecked(int id) const OVERRIDE;
     64   virtual bool IsCommandEnabled(int id) const OVERRIDE;
     65   virtual void ExecuteCommand(int id) OVERRIDE;
     66   virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE;
     67 
     68  private:
     69   // Given bounds within our View, this helper routine mirrors the bounds if
     70   // necessary.
     71   void AdjustBoundsForRTLUI(gfx::Rect* rect) const;
     72 
     73   // Draw the selected value of the drop down list
     74   void PaintText(gfx::Canvas* canvas);
     75 
     76   // Show the drop down list
     77   void ShowDropDownMenu(ui::MenuSourceType source_type);
     78 
     79   // The parent combobox, the owner of this object.
     80   Combobox* combobox_;
     81 
     82   // The reference to the border class. The object is owned by View::border_.
     83   FocusableBorder* text_border_;
     84 
     85   // The disclosure arrow next to the currently selected item from the list.
     86   const gfx::ImageSkia* disclosure_arrow_;
     87 
     88   // Responsible for showing the context menu.
     89   scoped_ptr<MenuRunner> dropdown_list_menu_runner_;
     90 
     91   // Is the drop down list showing
     92   bool dropdown_open_;
     93 
     94   // Like MenuButton, we use a time object in order to keep track of when the
     95   // combobox was closed. The time is used for simulating menu behavior; that
     96   // is, if the menu is shown and the button is pressed, we need to close the
     97   // menu. There is no clean way to get the second click event because the
     98   // menu is displayed using a modal loop and, unlike regular menus in Windows,
     99   // the button is not part of the displayed menu.
    100   base::Time closed_time_;
    101 
    102   // The selected index in the model. The default value is -1, which means no
    103   // selection.
    104   int selected_index_;
    105 
    106   // The maximum dimensions of the content in the dropdown
    107   int content_width_;
    108   int content_height_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(NativeComboboxViews);
    111 };
    112 
    113 }  // namespace views
    114 
    115 #endif  // UI_VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_VIEWS_H_
    116