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