Home | History | Annotate | Download | only in table
      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_TABLE_TABLE_HEADER_H_
      6 #define UI_VIEWS_CONTROLS_TABLE_TABLE_HEADER_H_
      7 
      8 #include "ui/gfx/font_list.h"
      9 #include "ui/views/view.h"
     10 #include "ui/views/views_export.h"
     11 
     12 namespace views {
     13 
     14 class TableView;
     15 
     16 // Views used to render the header for the table.
     17 class VIEWS_EXPORT TableHeader : public views::View {
     18  public:
     19   // Amount the text is padded on the left/right side.
     20   static const int kHorizontalPadding;
     21 
     22   // Amount of space reserved for the indicator and padding.
     23   static const int kSortIndicatorWidth;
     24 
     25   explicit TableHeader(TableView* table);
     26   virtual ~TableHeader();
     27 
     28   const gfx::FontList& font_list() const { return font_list_; }
     29 
     30   // views::View overrides.
     31   virtual void Layout() OVERRIDE;
     32   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
     33   virtual gfx::Size GetPreferredSize() const OVERRIDE;
     34   virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE;
     35   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
     36   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
     37   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
     38   virtual void OnMouseCaptureLost() OVERRIDE;
     39   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
     40 
     41  private:
     42   // Used to track the column being resized.
     43   struct ColumnResizeDetails {
     44     ColumnResizeDetails() : column_index(0), initial_x(0), initial_width(0) {}
     45 
     46     // Index into table_->visible_columns() that is being resized.
     47     int column_index;
     48 
     49     // X-coordinate of the mouse at the time the resize started.
     50     int initial_x;
     51 
     52     // Width of the column when the drag started.
     53     int initial_width;
     54   };
     55 
     56   // If not already resizing and |event| is over a resizable column starts
     57   // resizing.
     58   bool StartResize(const ui::LocatedEvent& event);
     59 
     60   // Continues a resize operation. Does nothing if not in the process of
     61   // resizing.
     62   void ContinueResize(const ui::LocatedEvent& event);
     63 
     64   // Toggles the sort order of the column at the location in |event|.
     65   void ToggleSortOrder(const ui::LocatedEvent& event);
     66 
     67   // Returns the column to resize given the specified x-coordinate, or -1 if |x|
     68   // is not in the resize range of any columns.
     69   int GetResizeColumn(int x) const;
     70 
     71   bool is_resizing() const { return resize_details_.get() != NULL; }
     72 
     73   const gfx::FontList font_list_;
     74 
     75   TableView* table_;
     76 
     77   // If non-null a resize is in progress.
     78   scoped_ptr<ColumnResizeDetails> resize_details_;
     79 
     80   DISALLOW_COPY_AND_ASSIGN(TableHeader);
     81 };
     82 
     83 }  // namespace views
     84 
     85 #endif  // UI_VIEWS_CONTROLS_TABLE_TABLE_HEADER_H_
     86