autocomplete_popup_view_mac.h revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1// Copyright (c) 2010 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_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_VIEW_MAC_H_
6#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_VIEW_MAC_H_
7#pragma once
8
9#import <Cocoa/Cocoa.h>
10
11#include <string>
12
13#include "base/basictypes.h"
14#include "base/scoped_ptr.h"
15#include "base/scoped_nsobject.h"
16#include "chrome/browser/autocomplete/autocomplete.h"
17#include "chrome/browser/autocomplete/autocomplete_match.h"
18#include "chrome/browser/autocomplete/autocomplete_popup_view.h"
19#import "chrome/browser/cocoa/location_bar/instant_opt_in_controller.h"
20#include "gfx/font.h"
21#include "webkit/glue/window_open_disposition.h"
22
23
24class AutocompleteEditModel;
25class AutocompleteEditViewMac;
26@class AutocompleteMatrix;
27class AutocompletePopupModel;
28@class InstantOptInController;
29@class NSImage;
30class Profile;
31
32// Implements AutocompletePopupView using a raw NSWindow containing an
33// NSTableView.
34//
35// TODO(rohitrao): This class is set up in a way that makes testing hard.
36// Refactor and write unittests.  http://crbug.com/9977
37
38class AutocompletePopupViewMac : public AutocompletePopupView,
39                                 public InstantOptInControllerDelegate {
40 public:
41  AutocompletePopupViewMac(AutocompleteEditViewMac* edit_view,
42                           AutocompleteEditModel* edit_model,
43                           Profile* profile,
44                           NSTextField* field);
45  virtual ~AutocompletePopupViewMac();
46
47  // Implement the InstantOptInControllerDelegate interface.
48  virtual void UserPressedOptIn(bool opt_in);
49
50  // Implement the AutocompletePopupView interface.
51  virtual bool IsOpen() const;
52  virtual void InvalidateLine(size_t line) {
53    // TODO(shess): Verify that there is no need to implement this.
54    // This is currently used in two places in the model:
55    //
56    // When setting the selected line, the selected line is
57    // invalidated, then the selected line is changed, then the new
58    // selected line is invalidated, then PaintUpdatesNow() is called.
59    // For us PaintUpdatesNow() should be sufficient.
60    //
61    // Same thing happens when changing the hovered line, except with
62    // no call to PaintUpdatesNow().  Since this code does not
63    // currently support special display of the hovered line, there's
64    // nothing to do here.
65    //
66    // deanm indicates that this is an anti-flicker optimization,
67    // which we probably cannot utilize (and may not need) so long as
68    // we're using NSTableView to implement the popup contents.  We
69    // may need to move away from NSTableView to implement hover,
70    // though.
71  }
72  virtual void UpdatePopupAppearance();
73
74  virtual gfx::Rect GetTargetBounds();
75
76  // Set |line| to be selected.
77  void SetSelectedLine(size_t line);
78
79  // This is only called by model in SetSelectedLine() after updating
80  // everything.  Popup should already be visible.
81  virtual void PaintUpdatesNow();
82
83  virtual void OnDragCanceled() {}
84
85  // Returns the popup's model.
86  virtual AutocompletePopupModel* GetModel();
87
88  // Opens the URL corresponding to the given |row|.  If |force_background| is
89  // true, forces the URL to open in a background tab.  Otherwise, determines
90  // the proper window open disposition from the modifier flags on |[NSApp
91  // currentEvent]|.
92  void OpenURLForRow(int row, bool force_background);
93
94  // Return the text to show for the match, based on the match's
95  // contents and description.  Result will be in |font|, with the
96  // boldfaced version used for matches.
97  static NSAttributedString* MatchText(const AutocompleteMatch& match,
98                                gfx::Font& font,
99                                float cellWidth);
100
101  // Helper for MatchText() to allow sharing code between the contents
102  // and description cases.  Returns NSMutableAttributedString as a
103  // convenience for MatchText().
104  static NSMutableAttributedString* DecorateMatchedString(
105      const std::wstring &matchString,
106      const AutocompleteMatch::ACMatchClassifications &classifications,
107      NSColor* textColor, NSColor* dimTextColor, gfx::Font& font);
108
109  // Helper for MatchText() to elide a marked-up string using
110  // gfx::ElideText() as a model.  Modifies |aString| in place.
111  // TODO(shess): Consider breaking AutocompleteButtonCell out of this
112  // code, and modifying it to have something like -setMatch:, so that
113  // these convolutions to expose internals for testing can be
114  // cleaner.
115  static NSMutableAttributedString* ElideString(
116      NSMutableAttributedString* aString,
117      const std::wstring originalString,
118      const gfx::Font& font,
119      const float cellWidth);
120
121 private:
122  // Returns the AutocompleteMatrix for this popup view.
123  AutocompleteMatrix* GetAutocompleteMatrix();
124
125  // Create the popup_ instance if needed.
126  void CreatePopupIfNeeded();
127
128  // Calculate the appropriate position for the popup based on the
129  // field's screen position and the given target for the matrix
130  // height, and makes the popup visible.  Animates to the new frame
131  // if the popup shrinks, snaps to the new frame if the popup grows,
132  // allows existing animations to continue if the size doesn't
133  // change.
134  void PositionPopup(const CGFloat matrixHeight);
135
136  // Returns the NSImage that should be used as an icon for the given match.
137  NSImage* ImageForMatch(const AutocompleteMatch& match);
138
139  // Returns whether or not to show the instant opt-in prompt.
140  bool ShouldShowInstantOptIn();
141
142  scoped_ptr<AutocompletePopupModel> model_;
143  AutocompleteEditViewMac* edit_view_;
144  NSTextField* field_;  // owned by tab controller
145
146  // Child window containing a matrix which implements the popup.
147  scoped_nsobject<NSWindow> popup_;
148  scoped_nsobject<InstantOptInController> opt_in_controller_;
149  NSRect targetPopupFrame_;
150
151  DISALLOW_COPY_AND_ASSIGN(AutocompletePopupViewMac);
152};
153
154#endif  // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_VIEW_MAC_H_
155