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 #include "ui/views/controls/resize_area.h"
      6 
      7 #include "base/logging.h"
      8 #include "ui/base/accessibility/accessible_view_state.h"
      9 #include "ui/base/resource/resource_bundle.h"
     10 #include "ui/views/controls/resize_area_delegate.h"
     11 
     12 #if defined(USE_AURA)
     13 #include "ui/base/cursor/cursor.h"
     14 #endif
     15 
     16 namespace views {
     17 
     18 const char ResizeArea::kViewClassName[] = "ResizeArea";
     19 
     20 ////////////////////////////////////////////////////////////////////////////////
     21 // ResizeArea
     22 
     23 ResizeArea::ResizeArea(ResizeAreaDelegate* delegate)
     24     : delegate_(delegate),
     25       initial_position_(0) {
     26 }
     27 
     28 ResizeArea::~ResizeArea() {
     29 }
     30 
     31 const char* ResizeArea::GetClassName() const {
     32   return kViewClassName;
     33 }
     34 
     35 gfx::NativeCursor ResizeArea::GetCursor(const ui::MouseEvent& event) {
     36   if (!enabled())
     37     return gfx::kNullCursor;
     38 #if defined(USE_AURA)
     39   return ui::kCursorEastWestResize;
     40 #elif defined(OS_WIN)
     41   static HCURSOR g_resize_cursor = LoadCursor(NULL, IDC_SIZEWE);
     42   return g_resize_cursor;
     43 #endif
     44 }
     45 
     46 bool ResizeArea::OnMousePressed(const ui::MouseEvent& event) {
     47   if (!event.IsOnlyLeftMouseButton())
     48     return false;
     49 
     50   // The resize area obviously will move once you start dragging so we need to
     51   // convert coordinates to screen coordinates so that we don't lose our
     52   // bearings.
     53   gfx::Point point(event.x(), 0);
     54   View::ConvertPointToScreen(this, &point);
     55   initial_position_ = point.x();
     56 
     57   return true;
     58 }
     59 
     60 bool ResizeArea::OnMouseDragged(const ui::MouseEvent& event) {
     61   if (!event.IsLeftMouseButton())
     62     return false;
     63 
     64   ReportResizeAmount(event.x(), false);
     65   return true;
     66 }
     67 
     68 void ResizeArea::OnMouseReleased(const ui::MouseEvent& event) {
     69   ReportResizeAmount(event.x(), true);
     70 }
     71 
     72 void ResizeArea::OnMouseCaptureLost() {
     73   ReportResizeAmount(initial_position_, true);
     74 }
     75 
     76 void ResizeArea::GetAccessibleState(ui::AccessibleViewState* state) {
     77   state->role = ui::AccessibilityTypes::ROLE_SEPARATOR;
     78 }
     79 
     80 void ResizeArea::ReportResizeAmount(int resize_amount, bool last_update) {
     81   gfx::Point point(resize_amount, 0);
     82   View::ConvertPointToScreen(this, &point);
     83   resize_amount = point.x() - initial_position_;
     84   delegate_->OnResize(base::i18n::IsRTL() ? -resize_amount : resize_amount,
     85                       last_update);
     86 }
     87 
     88 }  // namespace views
     89