Home | History | Annotate | Download | only in touchui
      1 // Copyright (c) 2013 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_UI_VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_IMPL_H_
      6 #define UI_UI_VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_IMPL_H_
      7 
      8 #include "base/timer/timer.h"
      9 #include "ui/base/touch/touch_editing_controller.h"
     10 #include "ui/gfx/point.h"
     11 #include "ui/views/touchui/touch_editing_menu.h"
     12 #include "ui/views/view.h"
     13 #include "ui/views/views_export.h"
     14 
     15 namespace views {
     16 
     17 // Touch specific implementation of TouchSelectionController. Responsible for
     18 // displaying selection handles and menu elements relevant in a touch interface.
     19 class VIEWS_EXPORT TouchSelectionControllerImpl
     20     : public ui::TouchSelectionController,
     21       public TouchEditingMenuController,
     22       public WidgetObserver {
     23  public:
     24   // Use TextSelectionController::create().
     25   explicit TouchSelectionControllerImpl(
     26       ui::TouchEditable* client_view);
     27 
     28   virtual ~TouchSelectionControllerImpl();
     29 
     30   // TextSelectionController.
     31   virtual void SelectionChanged() OVERRIDE;
     32   virtual bool IsHandleDragInProgress() OVERRIDE;
     33 
     34  private:
     35   friend class TouchSelectionControllerImplTest;
     36   class EditingHandleView;
     37 
     38   void SetDraggingHandle(EditingHandleView* handle);
     39 
     40   // Callback to inform the client view that the selection handle has been
     41   // dragged, hence selection may need to be updated.
     42   void SelectionHandleDragged(const gfx::Point& drag_pos);
     43 
     44   // Convenience method to convert a point from a selection handle's coordinate
     45   // system to that of the client view.
     46   void ConvertPointToClientView(EditingHandleView* source, gfx::Point* point);
     47 
     48   // Overridden from TouchEditingMenuController.
     49   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
     50   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
     51   virtual void OpenContextMenu() OVERRIDE;
     52   virtual void OnMenuClosed(TouchEditingMenuView* menu) OVERRIDE;
     53 
     54   // Overridden from WidgetObserver. We will observe the widget backing the
     55   // |client_view_| so that when its moved/resized, we can update the selection
     56   // handles appropriately.
     57   virtual void OnWidgetClosing(Widget* widget) OVERRIDE;
     58   virtual void OnWidgetBoundsChanged(Widget* widget,
     59                                      const gfx::Rect& new_bounds) OVERRIDE;
     60 
     61   // Time to show context menu.
     62   void ContextMenuTimerFired();
     63 
     64   void StartContextMenuTimer();
     65 
     66   // Convenience method to update the position/visibility of the context menu.
     67   void UpdateContextMenu(const gfx::Point& p1, const gfx::Point& p2);
     68 
     69   // Convenience method for hiding context menu.
     70   void HideContextMenu();
     71 
     72   // Convenience methods for testing.
     73   gfx::Point GetSelectionHandle1Position();
     74   gfx::Point GetSelectionHandle2Position();
     75   gfx::Point GetCursorHandlePosition();
     76   bool IsSelectionHandle1Visible();
     77   bool IsSelectionHandle2Visible();
     78   bool IsCursorHandleVisible();
     79 
     80   ui::TouchEditable* client_view_;
     81   Widget* client_widget_;
     82   scoped_ptr<EditingHandleView> selection_handle_1_;
     83   scoped_ptr<EditingHandleView> selection_handle_2_;
     84   scoped_ptr<EditingHandleView> cursor_handle_;
     85   TouchEditingMenuView* context_menu_;
     86 
     87   // Timer to trigger |context_menu| (|context_menu| is not shown if the
     88   // selection handles are being updated. It appears only when the handles are
     89   // stationary for a certain amount of time).
     90   base::OneShotTimer<TouchSelectionControllerImpl> context_menu_timer_;
     91 
     92   // Pointer to the SelectionHandleView being dragged during a drag session.
     93   EditingHandleView* dragging_handle_;
     94 
     95   gfx::Rect selection_end_point_1;
     96   gfx::Rect selection_end_point_2;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImpl);
     99 };
    100 
    101 class VIEWS_EXPORT ViewsTouchSelectionControllerFactory
    102     : public ui::TouchSelectionControllerFactory {
    103  public:
    104   ViewsTouchSelectionControllerFactory();
    105 
    106   // Overridden from ui::TouchSelectionControllerFactory.
    107   virtual ui::TouchSelectionController* create(
    108       ui::TouchEditable* client_view) OVERRIDE;
    109 };
    110 
    111 }  // namespace views
    112 
    113 #endif  // UI_UI_VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_IMPL_H_
    114