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