OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 CHROME_BROWSER_UI_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_RESULT_VIEW_H_ |
| 6 #define CHROME_BROWSER_UI_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_RESULT_VIEW_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 10 #include "third_party/skia/include/core/SkColor.h" |
| 11 #include "ui/gfx/font.h" |
| 12 #include "ui/gfx/rect.h" |
| 13 #include "views/view.h" |
| 14 |
| 15 namespace gfx { |
| 16 class Canvas; |
| 17 } |
| 18 class SkBitmap; |
| 19 |
| 20 // An interface implemented by an object that provides data to populate |
| 21 // individual result views. |
| 22 class AutocompleteResultViewModel { |
| 23 public: |
| 24 // Returns true if the index is selected. |
| 25 virtual bool IsSelectedIndex(size_t index) const = 0; |
| 26 |
| 27 // Returns true if the index is hovered. |
| 28 virtual bool IsHoveredIndex(size_t index) const = 0; |
| 29 |
| 30 // Returns the special-case icon we should use for the given index, or NULL |
| 31 // if we should use the default icon. |
| 32 virtual const SkBitmap* GetSpecialIcon(size_t index) const = 0; |
| 33 }; |
| 34 |
| 35 class AutocompleteResultView : public views::View { |
| 36 public: |
| 37 enum ResultViewState { |
| 38 NORMAL = 0, |
| 39 SELECTED, |
| 40 HOVERED, |
| 41 NUM_STATES |
| 42 }; |
| 43 |
| 44 enum ColorKind { |
| 45 BACKGROUND = 0, |
| 46 TEXT, |
| 47 DIMMED_TEXT, |
| 48 URL, |
| 49 NUM_KINDS |
| 50 }; |
| 51 |
| 52 AutocompleteResultView(AutocompleteResultViewModel* model, |
| 53 int model_index, |
| 54 const gfx::Font& font, |
| 55 const gfx::Font& bold_font); |
| 56 virtual ~AutocompleteResultView(); |
| 57 |
| 58 // Updates the match used to paint the contents of this result view. We copy |
| 59 // the match so that we can continue to paint the last result even after the |
| 60 // model has changed. |
| 61 void set_match(const AutocompleteMatch& match) { match_ = match; } |
| 62 gfx::Font normal_font() const { return normal_font_; } |
| 63 gfx::Font bold_font() const { return bold_font_; } |
| 64 gfx::Rect text_bounds() const { return text_bounds_; } |
| 65 void set_text_bounds(const gfx::Rect& tb) { text_bounds_ = tb; } |
| 66 |
| 67 // Overridden from views::View: |
| 68 virtual void Paint(gfx::Canvas* canvas); |
| 69 virtual void Layout(); |
| 70 virtual gfx::Size GetPreferredSize(); |
| 71 |
| 72 // Returns the preferred height for a single row. |
| 73 int GetPreferredHeight(const gfx::Font& font, |
| 74 const gfx::Font& bold_font); |
| 75 static SkColor GetColor(ResultViewState state, ColorKind kind); |
| 76 static int icon_size_; |
| 77 |
| 78 protected: |
| 79 virtual void PaintMatch(gfx::Canvas* canvas, |
| 80 const AutocompleteMatch& match, |
| 81 int x); |
| 82 |
| 83 // Draws the specified |text| into the canvas, using highlighting provided by |
| 84 // |classifications|. If |force_dim| is true, ACMatchClassification::DIM is |
| 85 // added to all of the classifications. Returns the x position to the right |
| 86 // of the string. |
| 87 int DrawString(gfx::Canvas* canvas, |
| 88 const string16& text, |
| 89 const ACMatchClassifications& classifications, |
| 90 bool force_dim, |
| 91 int x, |
| 92 int y); |
| 93 |
| 94 int icon_vertical_padding_; |
| 95 int text_vertical_padding_; |
| 96 |
| 97 private: |
| 98 // Precalculated data used to draw the portion of a match classification that |
| 99 // fits entirely within one run. |
| 100 struct ClassificationData { |
| 101 string16 text; |
| 102 const gfx::Font* font; |
| 103 SkColor color; |
| 104 int pixel_width; |
| 105 }; |
| 106 typedef std::vector<ClassificationData> Classifications; |
| 107 |
| 108 // Precalculated data used to draw a complete visual run within the match. |
| 109 // This will include all or part of at leasdt one, and possibly several, |
| 110 // classifications. |
| 111 struct RunData { |
| 112 size_t run_start; // Offset within the match text where this run begins. |
| 113 int visual_order; // Where this run occurs in visual order. The earliest |
| 114 // run drawn is run 0. |
| 115 bool is_rtl; |
| 116 int pixel_width; |
| 117 Classifications classifications; // Classification pieces within this run, |
| 118 // in logical order. |
| 119 }; |
| 120 typedef std::vector<RunData> Runs; |
| 121 |
| 122 // Predicate functions for use when sorting the runs. |
| 123 static bool SortRunsLogically(const RunData& lhs, const RunData& rhs); |
| 124 static bool SortRunsVisually(const RunData& lhs, const RunData& rhs); |
| 125 |
| 126 ResultViewState GetState() const; |
| 127 |
| 128 const SkBitmap* GetIcon() const; |
| 129 |
| 130 // Elides |runs| to fit in |remaining_width|. The runs in |runs| should be in |
| 131 // logical order. |
| 132 // |
| 133 // When we need to elide a run, the ellipsis will be placed at the end of that |
| 134 // run. This means that if we elide a run whose visual direction is opposite |
| 135 // that of the drawing context, the ellipsis will not be at the "end" of the |
| 136 // drawn string. For example, if in an LTR context we have the LTR run |
| 137 // "LTR_STRING" and the RTL run "RTL_STRING", the unelided text would be drawn |
| 138 // like: |
| 139 // LTR_STRING GNIRTS_LTR |
| 140 // If we need to elide the RTL run, then it will be drawn like: |
| 141 // LTR_STRING ...RTS_LTR |
| 142 // Instead of: |
| 143 // LTR_STRING RTS_LTR... |
| 144 void Elide(Runs* runs, int remaining_width) const; |
| 145 |
| 146 // This row's model and model index. |
| 147 AutocompleteResultViewModel* model_; |
| 148 size_t model_index_; |
| 149 |
| 150 const gfx::Font normal_font_; |
| 151 const gfx::Font bold_font_; |
| 152 |
| 153 // Width of the ellipsis in the normal font. |
| 154 int ellipsis_width_; |
| 155 |
| 156 // A context used for mirroring regions. |
| 157 class MirroringContext; |
| 158 scoped_ptr<MirroringContext> mirroring_context_; |
| 159 |
| 160 // Layout rects for various sub-components of the view. |
| 161 gfx::Rect icon_bounds_; |
| 162 gfx::Rect text_bounds_; |
| 163 |
| 164 AutocompleteMatch match_; |
| 165 |
| 166 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultView); |
| 167 }; |
| 168 |
| 169 #endif // CHROME_BROWSER_UI_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_RESULT_VIEW_H_ |
OLD | NEW |