Home | History | Annotate | Download | only in autocomplete
      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 class AutocompleteResultViewModel;
     16 namespace gfx {
     17 class Canvas;
     18 }
     19 
     20 class AutocompleteResultView : public views::View {
     21  public:
     22   enum ResultViewState {
     23     NORMAL = 0,
     24     SELECTED,
     25     HOVERED,
     26     NUM_STATES
     27   };
     28 
     29   enum ColorKind {
     30     BACKGROUND = 0,
     31     TEXT,
     32     DIMMED_TEXT,
     33     URL,
     34     NUM_KINDS
     35   };
     36 
     37   AutocompleteResultView(AutocompleteResultViewModel* model,
     38                          int model_index,
     39                          const gfx::Font& font,
     40                          const gfx::Font& bold_font);
     41   virtual ~AutocompleteResultView();
     42 
     43   static SkColor GetColor(ResultViewState state, ColorKind kind);
     44 
     45   // Updates the match used to paint the contents of this result view. We copy
     46   // the match so that we can continue to paint the last result even after the
     47   // model has changed.
     48   void SetMatch(const AutocompleteMatch& match);
     49 
     50  protected:
     51   virtual void PaintMatch(gfx::Canvas* canvas,
     52                           const AutocompleteMatch& match,
     53                           int x);
     54   virtual int GetFontHeight() const;
     55 
     56   // Draws the specified |text| into the canvas, using highlighting provided by
     57   // |classifications|. If |force_dim| is true, ACMatchClassification::DIM is
     58   // added to all of the classifications. Returns the x position to the right
     59   // of the string.
     60   int DrawString(gfx::Canvas* canvas,
     61                  const string16& text,
     62                  const ACMatchClassifications& classifications,
     63                  bool force_dim,
     64                  int x,
     65                  int y);
     66 
     67   const gfx::Rect& text_bounds() const { return text_bounds_; }
     68 
     69  private:
     70   struct ClassificationData;
     71   typedef std::vector<ClassificationData> Classifications;
     72 
     73   struct RunData;
     74   typedef std::vector<RunData> Runs;
     75 
     76   // Predicate functions for use when sorting the runs.
     77   static bool SortRunsLogically(const RunData& lhs, const RunData& rhs);
     78   static bool SortRunsVisually(const RunData& lhs, const RunData& rhs);
     79 
     80   ResultViewState GetState() const;
     81   const SkBitmap* GetIcon() const;
     82 
     83   // Elides |runs| to fit in |remaining_width|.  The runs in |runs| should be in
     84   // logical order.
     85   //
     86   // When we need to elide a run, the ellipsis will be placed at the end of that
     87   // run.  This means that if we elide a run whose visual direction is opposite
     88   // that of the drawing context, the ellipsis will not be at the "end" of the
     89   // drawn string.  For example, if in an LTR context we have the LTR run
     90   // "LTR_STRING" and the RTL run "RTL_STRING", the unelided text would be drawn
     91   // like:
     92   //     LTR_STRING GNIRTS_LTR
     93   // If we need to elide the RTL run, then it will be drawn like:
     94   //     LTR_STRING ...RTS_LTR
     95   // Instead of:
     96   //     LTR_STRING RTS_LTR...
     97   void Elide(Runs* runs, int remaining_width) const;
     98 
     99   // views::View:
    100   virtual gfx::Size GetPreferredSize() OVERRIDE;
    101   virtual void Layout() OVERRIDE;
    102   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
    103 
    104   static int default_icon_size_;
    105 
    106   // This row's model and model index.
    107   AutocompleteResultViewModel* model_;
    108   size_t model_index_;
    109 
    110   const gfx::Font normal_font_;
    111   const gfx::Font bold_font_;
    112 
    113   // Width of the ellipsis in the normal font.
    114   int ellipsis_width_;
    115 
    116   // A context used for mirroring regions.
    117   class MirroringContext;
    118   scoped_ptr<MirroringContext> mirroring_context_;
    119 
    120   AutocompleteMatch match_;
    121 
    122   gfx::Rect text_bounds_;
    123   gfx::Rect icon_bounds_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(AutocompleteResultView);
    126 };
    127 
    128 #endif  // CHROME_BROWSER_UI_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_RESULT_VIEW_H_
    129