summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc
blob: f4d9e9eb0586569c81d751f5ad19e956594018f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.

#include "chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h"

#include <objidl.h>
#include <commctrl.h>
#include <dwmapi.h>

#include "app/gfx/chrome_canvas.h"
#include "app/gfx/color_utils.h"
#include "app/gfx/insets.h"
#include "app/gfx/path.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "app/win_util.h"
#include "chrome/browser/autocomplete/autocomplete_edit_view_win.h"
#include "chrome/browser/autocomplete/autocomplete_popup_model.h"
#include "chrome/browser/views/autocomplete/autocomplete_popup_win.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "third_party/skia/include/core/SkShader.h"
#include "third_party/icu38/public/common/unicode/ubidi.h"
#include "views/widget/widget.h"

// Colors for various components of the view.
static const SkColor kBackgroundColor =
    color_utils::GetSysSkColor(COLOR_WINDOW);
static const SkColor kSelectedBackgroundColor =
    color_utils::GetSysSkColor(COLOR_HIGHLIGHT);
static const SkColor kHoverBackgroundColor =
    SkColorSetA(kSelectedBackgroundColor, 127);
static const SkColor kTextColor =
    color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
static const SkColor kSelectedTextColor =
    color_utils::GetSysSkColor(COLOR_HIGHLIGHTTEXT);
static const SkColor kDimTextColor =
    color_utils::GetSysSkColor(COLOR_GRAYTEXT);
static const SkColor kSelectedDimTextColor =
    color_utils::GetSysSkColor(COLOR_HIGHLIGHTTEXT);
static const SkColor kStandardURLColor = SkColorSetRGB(0, 0x80, 0);
static const SkColor kHighlightURLColor = SkColorSetRGB(0xD0, 0xFF, 0xD0);
static const int kPopupTransparency = 235;
static const int kHoverRowAlpha = 0x40;
// The minimum distance between the top and bottom of the icon and the top or
// bottom of the row. "Minimum" is used because the vertical padding may be
// larger, depending on the size of the text.
static const int kIconVerticalPadding = 2;
// The minimum distance between the top and bottom of the text and the top or
// bottom of the row. See comment about the use of "minimum" for
// kIconVerticalPadding.
static const int kTextVerticalPadding = 3;
// The padding at the left edge of the row, left of the icon.
static const int kRowLeftPadding = 6;
// The padding on the right edge of the row, right of the text.
static const int kRowRightPadding = 3;
// The horizontal distance between the right edge of the icon and the left edge
// of the text.
static const int kIconTextSpacing = 9;

class AutocompleteResultView : public views::View {
 public:
  AutocompleteResultView(AutocompleteResultViewModel* model,
                         int model_index,
                         const ChromeFont& font);
  virtual ~AutocompleteResultView();

  // Overridden from views::View:
  virtual void Paint(ChromeCanvas* canvas);
  virtual void Layout();
  virtual gfx::Size GetPreferredSize();
  virtual void OnMouseEntered(const views::MouseEvent& event);
  virtual void OnMouseMoved(const views::MouseEvent& event);
  virtual void OnMouseExited(const views::MouseEvent& event);
  virtual bool OnMousePressed(const views::MouseEvent& event);
  virtual void OnMouseReleased(const views::MouseEvent& event, bool canceled);
  virtual bool OnMouseDragged(const views::MouseEvent& event);

 private:
  // Get colors for row backgrounds and text for different row states.
  SkColor GetBackgroundColor() const;
  SkColor GetTextColor() const;

  SkBitmap* GetIcon() const;

  // Draws the specified |text| into the canvas, using highlighting provided by
  // |classifications|. If |force_dim| is true, ACMatchClassification::DIM is
  // added to all of the classifications. Returns the x position to the right
  // of the string.
  int DrawString(ChromeCanvas* canvas,
                  const std::wstring& text,
                  const ACMatchClassifications& classifications,
                  bool force_dim,
                  int x,
                  int y);

  // Draws an individual sub-fragment with the specified style. Returns the x
  // position to the right of the fragment.
  int DrawStringFragment(ChromeCanvas* canvas,
                         const std::wstring& text,
                         int style,
                         int x,
                         int y);

  // Gets the font and text color for a fragment with the specified style.
  ChromeFont GetFragmentFont(int style) const;
  SkColor GetFragmentTextColor(int style) const;

  // This row's model and model index.
  AutocompleteResultViewModel* model_;
  size_t model_index_;

  // True if the mouse is over this row.
  bool hot_;

  // The font used to derive fonts for rendering the text in this row.
  ChromeFont font_;

  // A context used for mirroring regions.
  class MirroringContext;
  scoped_ptr<MirroringContext> mirroring_context_;

  // Layout rects for various sub-components of the view.
  gfx::Rect icon_bounds_;
  gfx::Rect text_bounds_;

  // Icons for rows.
  static SkBitmap* icon_url_;
  static SkBitmap* icon_url_selected_;
  static SkBitmap* icon_history_;
  static SkBitmap* icon_history_selected_;
  static SkBitmap* icon_search_;
  static SkBitmap* icon_search_selected_;
  static SkBitmap* icon_more_;
  static SkBitmap* icon_more_selected_;
  static SkBitmap* icon_star_;
  static SkBitmap* icon_star_selected_;
  static int icon_size_;

  static bool initialized_;
  static void InitClass();

  DISALLOW_COPY_AND_ASSIGN(AutocompleteResultView);
};

// static
SkBitmap* AutocompleteResultView::icon_url_ = NULL;
SkBitmap* AutocompleteResultView::icon_url_selected_ = NULL;
SkBitmap* AutocompleteResultView::icon_history_ = NULL;
SkBitmap* AutocompleteResultView::icon_history_selected_ = NULL;
SkBitmap* AutocompleteResultView::icon_search_ = NULL;
SkBitmap* AutocompleteResultView::icon_search_selected_ = NULL;
SkBitmap* AutocompleteResultView::icon_star_ = NULL;
SkBitmap* AutocompleteResultView::icon_star_selected_ = NULL;
SkBitmap* AutocompleteResultView::icon_more_ = NULL;
SkBitmap* AutocompleteResultView::icon_more_selected_ = NULL;
int AutocompleteResultView::icon_size_ = 0;
bool AutocompleteResultView::initialized_ = false;

// This class is a utility class which mirrors an x position, calculates the
// index of the i-th run of a text, and calculates the index of the i-th
// fragment of a run.
// To render a styled text, we split a text into fragments and draw each
// fragment with the specified style. Unfortunately, it is not trivial to
// implement the above steps in a mirrored window.
// When we split a URL "www.google.com" into three fragments ('www.', 'google',
// and '.com') and draw them in a mirrored window as shown in the following
// steps, the output text becomes ".comgooglewww.".
// 1. Draw 'www.'
//      +-----------------+ +-----------------+
//      |LTR window       | |       RTL window|
//      +-----------------+ +-----------------+
//      |www.             | |             www.|
//      +-----------------+ +-----------------+
// 2. Draw 'google'
//      +-----------------+ +-----------------+
//      |LTR window       | |       RTL window|
//      +-----------------+ +-----------------+
//      |www.google       | |       googlewww.|
//      +-----------------+ +-----------------+
// 3. Draw '.com'
//      +-----------------+ +-----------------+
//      |LTR window       | |       RTL window|
//      +-----------------+ +-----------------+
//      |www.google.com   | |   .comgooglewww.|
//      +-----------------+ +-----------------+
// To fix this fragment-ordering problem, we should swap the run indices and
// fragment indices when rendering in a mirrred coordinate as listed below.
// 1. Draw 'www.' for LTR (or ".com" for RTL)
//      +-----------------+ +-----------------+
//      |LTR window       | |       RTL window|
//      +-----------------+ +-----------------+
//      |www.             | |             .com|
//      +-----------------+ +-----------------+
// 2. Draw 'google'
//      +-----------------+ +-----------------+
//      |LTR window       | |       RTL window|
//      +-----------------+ +-----------------+
//      |www.google       | |       google.com|
//      +-----------------+ +-----------------+
// 3. Draw '.com' for LTR (or "www." for RTL)
//      +-----------------+ +-----------------+
//      |LTR window       | |       RTL window|
//      +-----------------+ +-----------------+
//      |www.google.com   | |   www.google.com|
//      +-----------------+ +-----------------+
// This class encapsulates the above steps for AutocompleteResultView.
class AutocompleteResultView::MirroringContext {
 public:
  MirroringContext() : min_x_(0), center_x_(0), max_x_(0), mirrored_(false) { }

  // Initializes a mirroring context with the bounding region of a text.
  // This class uses the center of this region as an axis for calculating
  // mirrored coordinates.
  int Initialize(int x1, int x2, bool enabled);

  // Returns the "left" side of the specified region.
  // When the application language is a right-to-left one, this function
  // calculates the mirrored coordinates of the input region and returns the
  // left side of the mirrored region.
  // The input region must be in the bounding region specified in the
  // Initialize() function.
  int GetLeft(int x1, int x2) const;

  // Returns the index of the i-th run of a text.
  // When we split a text into runs, we need to write each run in the LTR
  // (or RTL) order if UI language is LTR (or RTL), respectively.
  int GetRun(int i, int size) const {
    return mirrored_ ? (size - i - 1) : i;
  }

  // Returns the index of the i-th text fragment of a run.
  // When we split a run into fragments, we need to write each fragment in the
  // LTR (or RTL) order if UI language is LTR (or RTL), respectively.
  size_t GetClassification(size_t i, size_t size, bool run_rtl) const {
    return (mirrored_ != run_rtl) ? (size - i - 1) : i;
  }

  // Returns whether or not the x coordinate is mirrored.
  bool mirrored() const {
    return mirrored_;
  }

 private:
  int min_x_;
  int center_x_;
  int max_x_;
  bool mirrored_;

  DISALLOW_COPY_AND_ASSIGN(MirroringContext);
};

int AutocompleteResultView::MirroringContext::Initialize(int x1, int x2,
                                                         bool mirrored) {
  min_x_ = std::min(x1, x2);
  max_x_ = std::max(x1, x2);
  center_x_ = min_x_ + (max_x_ - min_x_) / 2;
  mirrored_ = mirrored;
  return x1;
}

int AutocompleteResultView::MirroringContext::GetLeft(int x1, int x2) const {
  return mirrored_ ?
      (center_x_ + (center_x_ - std::max(x1, x2))) : std::min(x1, x2);
}

AutocompleteResultView::AutocompleteResultView(
    AutocompleteResultViewModel* model,
    int model_index,
    const ChromeFont& font)
    : model_(model),
      model_index_(model_index),
      hot_(false),
      font_(font),
      mirroring_context_(new MirroringContext()) {
  InitClass();
}

AutocompleteResultView::~AutocompleteResultView() {
}

void AutocompleteResultView::Paint(ChromeCanvas* canvas) {
  canvas->FillRectInt(GetBackgroundColor(), 0, 0, width(), height());

  int x = MirroredLeftPointForRect(icon_bounds_);

  // Paint the icon.
  canvas->DrawBitmapInt(*GetIcon(), x, icon_bounds_.y());

  const AutocompleteMatch& match = model_->GetMatchAtIndex(model_index_);

  // Paint the text.
  // Initialize the |mirroring_context_| with the left and right positions.
  // The DrawString() function uses this |mirroring_context_| to calculate the
  // position of an input text.
  bool text_mirroring = View::UILayoutIsRightToLeft();
  int text_left = MirroredLeftPointForRect(text_bounds_);
  int text_right = text_mirroring ? x - kIconTextSpacing : text_bounds_.right();
  x = mirroring_context_->Initialize(text_left, text_right, text_mirroring);
  x = DrawString(canvas, match.contents, match.contents_class, false, x,
                 text_bounds_.y());

  // Paint the description.
  if (!match.description.empty()) {
    std::wstring separator =
        l10n_util::GetString(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR);
    ACMatchClassifications classifications;
    classifications.push_back(
        ACMatchClassification(0, ACMatchClassification::NONE));
    x = DrawString(canvas, separator, classifications, true, x,
                   text_bounds_.y());

    DrawString(canvas, match.description, match.description_class, true, x,
               text_bounds_.y());
  }
}

void AutocompleteResultView::Layout() {
  icon_bounds_.SetRect(kRowLeftPadding, (height() - icon_size_) / 2,
                       icon_size_, icon_size_);
  int text_x = icon_bounds_.right() + kIconTextSpacing;
  text_bounds_.SetRect(
      text_x,
      std::max(0, (height() - font_.height()) / 2),
      std::max(0, bounds().right() - text_x - kRowRightPadding),
      font_.height());
}

gfx::Size AutocompleteResultView::GetPreferredSize() {
  int text_height = font_.height() + 2 * kTextVerticalPadding;
  int icon_height = icon_size_ + 2 * kIconVerticalPadding;
  return gfx::Size(0, std::max(icon_height, text_height));
}

void AutocompleteResultView::OnMouseEntered(const views::MouseEvent& event) {
  hot_ = true;
  SchedulePaint();
}

void AutocompleteResultView::OnMouseMoved(const views::MouseEvent& event) {
  if (!hot_) {
    hot_ = true;
    SchedulePaint();
  }
}

void AutocompleteResultView::OnMouseExited(const views::MouseEvent& event) {
  hot_ = false;
  SchedulePaint();
}

bool AutocompleteResultView::OnMousePressed(const views::MouseEvent& event) {
  if (event.IsOnlyLeftMouseButton()) {
    model_->SetHoveredLine(model_index_);
    model_->SetSelectedLine(model_index_, false);
  } else if (event.IsOnlyMiddleMouseButton()) {
    model_->SetHoveredLine(model_index_);
  }
  return true;
}

void AutocompleteResultView::OnMouseReleased(const views::MouseEvent& event,
                                             bool canceled) {
  if (canceled)
    return;
  if (event.IsOnlyMiddleMouseButton())
    model_->OpenIndex(model_index_, NEW_BACKGROUND_TAB);
  else if (event.IsOnlyLeftMouseButton())
    model_->OpenIndex(model_index_, CURRENT_TAB);
}

bool AutocompleteResultView::OnMouseDragged(const views::MouseEvent& event) {
  // TODO(beng): move all message handling into the contents view and override
  //             GetViewForPoint.
  return false;
}

SkColor AutocompleteResultView::GetBackgroundColor() const {
  if (model_->IsSelectedIndex(model_index_))
    return kSelectedBackgroundColor;
  return hot_ ? kHoverBackgroundColor : kBackgroundColor;
}

SkColor AutocompleteResultView::GetTextColor() const {
  return model_->IsSelectedIndex(model_index_) ? kSelectedTextColor
                                               : kTextColor;
}

SkBitmap* AutocompleteResultView::GetIcon() const {
  bool selected = model_->IsSelectedIndex(model_index_);
  switch (model_->GetMatchAtIndex(model_index_).type) {
    case AutocompleteMatch::URL_WHAT_YOU_TYPED:
    case AutocompleteMatch::HISTORY_URL:
    case AutocompleteMatch::NAVSUGGEST:
      return selected ? icon_url_selected_ : icon_url_;
    case AutocompleteMatch::HISTORY_TITLE:
    case AutocompleteMatch::HISTORY_BODY:
    case AutocompleteMatch::HISTORY_KEYWORD:
      return selected ? icon_history_selected_ : icon_history_;
    case AutocompleteMatch::SEARCH_WHAT_YOU_TYPED:
    case AutocompleteMatch::SEARCH_HISTORY:
    case AutocompleteMatch::SEARCH_SUGGEST:
    case AutocompleteMatch::SEARCH_OTHER_ENGINE:
      return selected ? icon_search_selected_ : icon_search_;
    case AutocompleteMatch::OPEN_HISTORY_PAGE:
      return selected ? icon_more_selected_ : icon_more_;
    default:
      NOTREACHED();
      break;
  }
  return NULL;
}

int AutocompleteResultView::DrawString(
    ChromeCanvas* canvas,
    const std::wstring& text,
    const ACMatchClassifications& classifications,
    bool force_dim,
    int x,
    int y) {
  if (!text.length())
    return x;

  // Initialize a bidirectional line iterator of ICU and split the text into
  // visual runs. (A visual run is consecutive characters which have the same
  // display direction and should be displayed at once.)
  l10n_util::BiDiLineIterator bidi_line;
  if (!bidi_line.Open(text, mirroring_context_->mirrored(), false))
    return x;
  const int runs = bidi_line.CountRuns();

  // Draw the visual runs.
  // This loop splits each run into text fragments with the given
  // classifications and draws the text fragments.
  // When the direction of a run is right-to-left, we have to mirror the
  // x-coordinate of this run and render the fragments in the right-to-left
  // reading order. To handle this display order independently from the one of
  // this popup window, this loop renders a run with the steps below:
  // 1. Create a local display context for each run;
  // 2. Render the run into the local display context, and;
  // 3. Copy the local display context to the one of the popup window.
  for (int run = 0; run < runs; ++run) {
    int run_start = 0;
    int run_length = 0;

    // The index we pass to GetVisualRun corresponds to the position of the run
    // in the displayed text. For example, the string "Google in HEBREW" (where
    // HEBREW is text in the Hebrew language) has two runs: "Google in " which
    // is an LTR run, and "HEBREW" which is an RTL run. In an LTR context, the
    // run "Google in " has the index 0 (since it is the leftmost run
    // displayed). In an RTL context, the same run has the index 1 because it
    // is the rightmost run. This is why the order in which we traverse the
    // runs is different depending on the locale direction.
    //
    // Note that for URLs we always traverse the runs from lower to higher
    // indexes because the return order of runs for a URL always matches the
    // physical order of the context.
    int current_run = mirroring_context_->GetRun(run, runs);
    const UBiDiDirection run_direction = bidi_line.GetVisualRun(current_run,
                                                                &run_start,
                                                                &run_length);
    const int run_end = run_start + run_length;

    // Split this run with the given classifications and draw the fragments.
    for (size_t classification = 0; classification < classifications.size();
         ++classification) {
      size_t i = mirroring_context_->GetClassification(
          classification, classifications.size(), run_direction == UBIDI_RTL);
      size_t text_start = std::max(static_cast<size_t>(run_start),
                                   classifications[i].offset);
      size_t text_end = std::min(static_cast<size_t>(run_end),
          i < classifications.size() - 1 ?
          classifications[i + 1].offset : run_end);
      int style = classifications[i].style;
      if (force_dim)
        style |= ACMatchClassification::DIM;
      if (text_start < text_end) {
        x += DrawStringFragment(canvas,
                                text.substr(text_start, text_end - text_start),
                                style, x, y);
      }
    }
  }
  return x;
}

int AutocompleteResultView::DrawStringFragment(
    ChromeCanvas* canvas,
    const std::wstring& text,
    int style,
    int x,
    int y) {
  ChromeFont display_font = GetFragmentFont(style);
  // Clamp text width to the available width within the popup so we elide if
  // necessary.
  int string_width = std::min(display_font.GetStringWidth(text),
                              width() - kRowRightPadding - x);
  int string_left = mirroring_context_->GetLeft(x, x + string_width);
  canvas->DrawStringInt(text, GetFragmentFont(style),
                        GetFragmentTextColor(style), string_left, y,
                        string_width, display_font.height());
  return string_width;
}

ChromeFont AutocompleteResultView::GetFragmentFont(int style) const {
  if (style & ACMatchClassification::MATCH)
    return font_.DeriveFont(0, ChromeFont::BOLD);
  return font_;
}

SkColor AutocompleteResultView::GetFragmentTextColor(int style) const {
  bool selected = model_->IsSelectedIndex(model_index_);
  if (style & ACMatchClassification::URL) {
    // TODO(beng): bring over the contrast logic from the old popup and massage
    //             these values. See autocomplete_popup_view_win.cc and
    //             LuminosityContrast etc.
    return selected ? kHighlightURLColor : kStandardURLColor;
  }
  if (style & ACMatchClassification::DIM)
    return selected ? kSelectedDimTextColor : kDimTextColor;
  return GetTextColor();
}

void AutocompleteResultView::InitClass() {
  if (!initialized_) {
    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
    icon_url_ = rb.GetBitmapNamed(IDR_O2_GLOBE);
    icon_url_selected_ = rb.GetBitmapNamed(IDR_O2_GLOBE_SELECTED);
    icon_history_ = rb.GetBitmapNamed(IDR_O2_HISTORY);
    icon_history_selected_ = rb.GetBitmapNamed(IDR_O2_HISTORY_SELECTED);
    icon_search_ = rb.GetBitmapNamed(IDR_O2_SEARCH);
    icon_search_selected_ = rb.GetBitmapNamed(IDR_O2_SEARCH_SELECTED);
    icon_star_ = rb.GetBitmapNamed(IDR_O2_STAR);
    icon_star_selected_ = rb.GetBitmapNamed(IDR_O2_STAR_SELECTED);
    icon_more_ = rb.GetBitmapNamed(IDR_O2_MORE);
    icon_more_selected_ = rb.GetBitmapNamed(IDR_O2_MORE_SELECTED);
    // All icons are assumed to be square, and the same size.
    icon_size_ = icon_url_->width();
    initialized_ = true;
  }
}

class PopupBorder : public views::Border {
 public:
  PopupBorder() {
    InitClass();
  }
  virtual ~PopupBorder() {}

  // Returns the border radius of the edge of the popup.
  static int GetBorderRadius() {
    InitClass();
    return dropshadow_topleft_->width() - dropshadow_left_->width() - 1;
  }

  // Overridden from views::Border:
  virtual void Paint(const views::View& view, ChromeCanvas* canvas) const;
  virtual void GetInsets(gfx::Insets* insets) const;

 private:
  // Border graphics.
  static SkBitmap* dropshadow_left_;
  static SkBitmap* dropshadow_topleft_;
  static SkBitmap* dropshadow_top_;
  static SkBitmap* dropshadow_topright_;
  static SkBitmap* dropshadow_right_;
  static SkBitmap* dropshadow_bottomright_;
  static SkBitmap* dropshadow_bottom_;
  static SkBitmap* dropshadow_bottomleft_;

  static void InitClass();

  DISALLOW_COPY_AND_ASSIGN(PopupBorder);
};

// static
SkBitmap* PopupBorder::dropshadow_left_ = NULL;
SkBitmap* PopupBorder::dropshadow_topleft_ = NULL;
SkBitmap* PopupBorder::dropshadow_top_ = NULL;
SkBitmap* PopupBorder::dropshadow_topright_ = NULL;
SkBitmap* PopupBorder::dropshadow_right_ = NULL;
SkBitmap* PopupBorder::dropshadow_bottomright_ = NULL;
SkBitmap* PopupBorder::dropshadow_bottom_ = NULL;
SkBitmap* PopupBorder::dropshadow_bottomleft_ = NULL;

void PopupBorder::Paint(const views::View& view, ChromeCanvas* canvas) const {
  int ds_tl_width = dropshadow_topleft_->width();
  int ds_tl_height = dropshadow_topleft_->height();
  int ds_tr_width = dropshadow_topright_->width();
  int ds_tr_height = dropshadow_topright_->height();
  int ds_br_width = dropshadow_bottomright_->width();
  int ds_br_height = dropshadow_bottomright_->height();
  int ds_bl_width = dropshadow_bottomleft_->width();
  int ds_bl_height = dropshadow_bottomleft_->height();

  canvas->DrawBitmapInt(*dropshadow_topleft_, 0, 0);
  canvas->TileImageInt(*dropshadow_top_, ds_tl_width, 0,
                       view.width() - ds_tr_width - ds_tl_width,
                       dropshadow_top_->height());
  canvas->DrawBitmapInt(*dropshadow_topright_, view.width() - ds_tr_width, 0);
  canvas->TileImageInt(*dropshadow_right_,
                       view.width() - dropshadow_right_->width(),
                       ds_tr_height, dropshadow_right_->width(),
                       view.height() - ds_tr_height - ds_br_height);
  canvas->DrawBitmapInt(*dropshadow_bottomright_, view.width() - ds_br_width,
                        view.height() - ds_br_height);
  canvas->TileImageInt(*dropshadow_bottom_, ds_bl_width,
                       view.height() - dropshadow_bottom_->height(),
                       view.width() - ds_bl_width - ds_br_width,
                       dropshadow_bottom_->height());
  canvas->DrawBitmapInt(*dropshadow_bottomleft_, 0,
                        view.height() - dropshadow_bottomleft_->height());
  canvas->TileImageInt(*dropshadow_left_, 0, ds_tl_height,
                       dropshadow_left_->width(),
                       view.height() - ds_tl_height - ds_bl_height);
}

void PopupBorder::GetInsets(gfx::Insets* insets) const {
  // The left, right and bottom edge image sizes define our insets. The corner
  // images don't determine this because they can extend in both directions.
  insets->Set(dropshadow_top_->height(), dropshadow_left_->width(),
              dropshadow_bottom_->height(), dropshadow_right_->width());
}

void PopupBorder::InitClass() {
  static bool initialized = false;
  if (!initialized) {
    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
    dropshadow_left_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_L);
    dropshadow_topleft_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_TL);
    dropshadow_top_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_T);
    dropshadow_topright_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_TR);
    dropshadow_right_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_R);
    dropshadow_bottomright_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_BR);
    dropshadow_bottom_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_B);
    dropshadow_bottomleft_ = rb.GetBitmapNamed(IDR_OMNIBOX_POPUP_DS_BL);
    initialized = true;
  }
}

////////////////////////////////////////////////////////////////////////////////
// AutocompletePopupContentsView, public:

AutocompletePopupContentsView::AutocompletePopupContentsView(
    const ChromeFont& font,
    AutocompleteEditViewWin* edit_view,
    AutocompleteEditModel* edit_model,
    Profile* profile,
    AutocompletePopupPositioner* popup_positioner)
    : popup_(new AutocompletePopupWin(this)),
      model_(new AutocompletePopupModel(this, edit_model, profile)),
      edit_view_(edit_view),
      popup_positioner_(popup_positioner),
      edit_font_(font) {
  set_border(new PopupBorder);
}

void AutocompletePopupContentsView::UpdateResultViewsFromResult(
    const AutocompleteResult& result) {
  RemoveAllChildViews(true);
  for (size_t i = 0; i < result.size(); ++i)
    AddChildView(new AutocompleteResultView(this, i, edit_font_));
  Layout();
}

gfx::Rect AutocompletePopupContentsView::GetPopupBounds() const {
  gfx::Insets insets;
  border()->GetInsets(&insets);
  gfx::Rect contents_bounds = popup_positioner_->GetPopupBounds();
  int child_count = GetChildViewCount();
  int height = 0;
  for (int i = 0; i < child_count; ++i)
    height += GetChildViewAt(i)->GetPreferredSize().height();
  contents_bounds.set_height(height);
  contents_bounds.Inset(-insets.left(), -insets.top(), -insets.right(),
                        -insets.bottom());
  return contents_bounds;
}

////////////////////////////////////////////////////////////////////////////////
// AutocompletePopupContentsView, AutocompletePopupView overrides:

bool AutocompletePopupContentsView::IsOpen() const {
  return popup_->IsWindow() && popup_->IsVisible();
}

void AutocompletePopupContentsView::InvalidateLine(size_t line) {
  GetChildViewAt(static_cast<int>(line))->SchedulePaint();
}

void AutocompletePopupContentsView::UpdatePopupAppearance() {
  const AutocompleteResult& result = model_->result();
  UpdateResultViewsFromResult(result);
  if (result.empty()) {
    // No matches, close any existing popup.
    if (popup_->IsWindow())
      popup_->Hide();
    return;
  }

  if (popup_->IsWindow())
    popup_->Show();
  else
    popup_->Init(edit_view_, this);
}

void AutocompletePopupContentsView::OnHoverEnabledOrDisabled(bool disabled) {
  // TODO(beng): remove this from the interface.
}

void AutocompletePopupContentsView::PaintUpdatesNow() {
  // TODO(beng): remove this from the interface.
}

AutocompletePopupModel* AutocompletePopupContentsView::GetModel() {
  return model_.get();
}

////////////////////////////////////////////////////////////////////////////////
// AutocompletePopupContentsView, AutocompleteResultViewModel implementation:

bool AutocompletePopupContentsView::IsSelectedIndex(size_t index) {
  return index == model_->selected_line();
}

const AutocompleteMatch& AutocompletePopupContentsView::GetMatchAtIndex(
    size_t index) {
  return model_->result().match_at(index);
}

void AutocompletePopupContentsView::OpenIndex(
    size_t index,
    WindowOpenDisposition disposition) {
  const AutocompleteMatch& match = model_->result().match_at(index);
  // OpenURL() may close the popup, which will clear the result set and, by
  // extension, |match| and its contents.  So copy the relevant strings out to
  // make sure they stay alive until the call completes.
  const GURL url(match.destination_url);
  std::wstring keyword;
  const bool is_keyword_hint = model_->GetKeywordForMatch(match, &keyword);
  edit_view_->OpenURL(url, disposition, match.transition, GURL(), index,
                      is_keyword_hint ? std::wstring() : keyword);
}

void AutocompletePopupContentsView::SetHoveredLine(size_t index) {
  model_->SetHoveredLine(index);
}

void AutocompletePopupContentsView::SetSelectedLine(size_t index,
                                                    bool revert_to_default) {
  model_->SetSelectedLine(index, revert_to_default);
}

////////////////////////////////////////////////////////////////////////////////
// AutocompletePopupContentsView, views::View overrides:

void AutocompletePopupContentsView::PaintChildren(ChromeCanvas* canvas) {
  // We paint our children in an unconventional way.
  //
  // Because the border of this view creates an anti-aliased round-rect region
  // for the contents, we need to render our rectangular result child views into
  // this round rect region. We can't use a simple clip because clipping is
  // 1-bit and we get nasty jagged edges.
  //
  // Instead, we paint all our children into a second canvas and use that as a
  // shader to fill a path representing the round-rect clipping region. This
  // yields a nice anti-aliased edge.
  ChromeCanvas contents_canvas(width(), height(), true);
  contents_canvas.FillRectInt(kBackgroundColor, 0, 0, width(), height());
  View::PaintChildren(&contents_canvas);
  // We want the contents background to be slightly transparent so we can see
  // the blurry glass effect on DWM systems behind. We do this _after_ we paint
  // the children since they paint text, and GDI will reset this alpha data if
  // we paint text after this call.
  MakeCanvasTransparent(&contents_canvas);

  // Now paint the contents of the contents canvas into the actual canvas.
  SkPaint paint;
  paint.setAntiAlias(true);

  SkShader* shader = SkShader::CreateBitmapShader(
      contents_canvas.getDevice()->accessBitmap(false),
      SkShader::kRepeat_TileMode,
      SkShader::kRepeat_TileMode);
  paint.setShader(shader);
  shader->unref();

  gfx::Path path;
  MakeContentsPath(&path, GetLocalBounds(false));
  canvas->drawPath(path, paint);
}

void AutocompletePopupContentsView::Layout() {
  UpdateBlurRegion();

  // Size our children to the available content area.
  gfx::Rect contents_rect = GetLocalBounds(false);
  int child_count = GetChildViewCount();
  int top = contents_rect.y();
  for (int i = 0; i < child_count; ++i) {
    View* v = GetChildViewAt(i);
    v->SetBounds(contents_rect.x(), top, contents_rect.width(),
                 v->GetPreferredSize().height());
    top = v->bounds().bottom();
  }

  // We need to manually schedule a paint here since we are a layered window and
  // won't implicitly require painting until we ask for one.
  SchedulePaint();
}

////////////////////////////////////////////////////////////////////////////////
// AutocompletePopupContentsView, private:

void AutocompletePopupContentsView::MakeContentsPath(
    gfx::Path* path,
    const gfx::Rect& bounding_rect) {
  SkRect rect;
  rect.set(SkIntToScalar(bounding_rect.x()),
           SkIntToScalar(bounding_rect.y()),
           SkIntToScalar(bounding_rect.right()),
           SkIntToScalar(bounding_rect.bottom()));

  SkScalar radius = SkIntToScalar(PopupBorder::GetBorderRadius());
  path->addRoundRect(rect, radius, radius);
}

void AutocompletePopupContentsView::UpdateBlurRegion() {
  // We only support background blurring on Vista with Aero-Glass enabled.
  if (!win_util::ShouldUseVistaFrame() || !GetWidget())
    return;

  // Provide a blurred background effect within the contents region of the
  // popup.
  DWM_BLURBEHIND bb = {0};
  bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
  bb.fEnable = true;

  // Translate the contents rect into widget coordinates, since that's what
  // DwmEnableBlurBehindWindow expects a region in.
  gfx::Rect contents_rect = GetLocalBounds(false);
  gfx::Point origin(contents_rect.origin());
  views::View::ConvertPointToWidget(this, &origin);
  contents_rect.set_origin(origin);

  gfx::Path contents_path;
  MakeContentsPath(&contents_path, contents_rect);
  ScopedGDIObject<HRGN> popup_region;
  popup_region.Set(contents_path.CreateHRGN());
  bb.hRgnBlur = popup_region.Get();
  DwmEnableBlurBehindWindow(GetWidget()->GetNativeView(), &bb);
}

void AutocompletePopupContentsView::MakeCanvasTransparent(
    ChromeCanvas* canvas) {
  // Allow the window blur effect to show through the popup background.
  SkPaint paint;
  paint.setColor(SkColorSetARGB(kPopupTransparency, 255, 255, 255));
  paint.setPorterDuffXfermode(SkPorterDuff::kDstIn_Mode);
  paint.setStyle(SkPaint::kFill_Style);
  canvas->FillRectInt(0, 0, canvas->getDevice()->width(),
                      canvas->getDevice()->height(), paint);
}