Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: third_party/mozilla/include/ToolTip.mm

Issue 150188: Use native Cocoa tooltips instead of Mozilla's ToolTip class. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/mozilla/include/ToolTip.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Richard Schreyer
24 * Josh Aas <josh@mozilla.com>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 #import "ToolTip.h"
41 #import "NSScreen+Utils.h"
42
43 @interface ToolTip (ToolTipPrivateMethods)
44
45 - (void)parentWindowDidResignKey:(NSNotification*)inNotification;
46
47 @end
48
49 const float kBorderPadding = 2.0;
50 const float kMaxTextFieldWidth = 250.0;
51 const float kVOffset = 20.0;
52
53 @implementation ToolTip
54
55 - (id)init
56 {
57 if ((self = [super init]))
58 {
59 // the ref from -alloc is balanced by the -release in dealloc
60 mTooltipWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0.0, 0.0, kMaxTextFieldWidth, 0.0)
61 styleMask:NSBorderlessWindowMask
62 backing:NSBackingStoreBuffered
63 defer:YES];
64
65 // we don't want closing the window to release it, because we aren't always in control
66 // of the close (AppKit may do it on quit).
67 [mTooltipWindow setReleasedWhenClosed:NO];
68
69 // Create a textfield as the content of our new window.
70 // Field occupies all but the top 2 and bottom 2 pixels of the panel (bug 14 9635)
71 mTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(0.0, kBorderPadding , kMaxTextFieldWidth, 0.0)];
72 [[mTooltipWindow contentView] addSubview:mTextView];
73 [mTextView release]; // window holds ref
74
75 // set up the panel
76 [mTooltipWindow setHasShadow:YES];
77 [mTooltipWindow setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green :1.0 blue:0.81 alpha:1.0]];
78
79
80 // set up the text view
81 [mTextView setDrawsBackground:NO];
82 [mTextView setEditable:NO];
83 [mTextView setSelectable:NO];
84 [mTextView setFont:[NSFont toolTipsFontOfSize:[NSFont smallSystemFontSize]]] ;
85 [mTextView setMinSize:NSMakeSize(0.0, 0.0)];
86 [mTextView setHorizontallyResizable:YES];
87 [mTextView setVerticallyResizable:YES];
88 }
89 return self;
90 }
91
92 - (void)dealloc
93 {
94 [self closeToolTip];
95
96 [[NSNotificationCenter defaultCenter] removeObserver:self];
97
98 [mTooltipWindow close]; // we set the window not to release on -close
99 [mTooltipWindow release];
100
101 [super dealloc];
102 }
103
104 - (void)showToolTipAtPoint:(NSPoint)windowPoint
105 withString:(NSString*)string
106 overWindow:(NSWindow*)inWindow
107 {
108 if ([string length] == 0)
109 return;
110
111 NSPoint point = [inWindow convertBaseToScreen:windowPoint];
112 NSScreen* screen = [NSScreen screenForPoint:point];
113 if (!screen)
114 screen = [NSScreen mainScreen];
115
116 if (!screen)
117 return;
118
119 // register for window losing key status notifications, so we can hide the too ltip
120 // on window deactivation
121 [[NSNotificationCenter defaultCenter] addObserver:self
122 selector:@selector(parentWindowDidRes ignKey:)
123 name:NSWindowDidResignKeyNotifica tion
124 object:inWindow];
125
126 NSRect screenFrame = [screen visibleFrame];
127 NSSize screenSize = screenFrame.size;
128
129 // for some reason, text views suffer from hysteresis; the answer you get this time
130 // depends on what you had in there before. so clear state first.
131 [mTextView setString:@""];
132 [mTextView setFrame:NSMakeRect(0, kBorderPadding, 0, 0)];
133
134 // -sizeToFit sucks. For some reason it likes to wrap short words, so
135 // we measure the text by hand and set that as the min width.
136 NSSize stringSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObj ect:[NSFont toolTipsFontOfSize:[NSFont smallSystemFontSize]] forKey:NSFontAttrib uteName]];
137 float textViewWidth = ceil(stringSize.width);
138 if (textViewWidth > kMaxTextFieldWidth)
139 textViewWidth = kMaxTextFieldWidth;
140
141 textViewWidth += 2.0 * 5.0; // magic numbers required to make the text not w rap. No, this isn't -textContainerInset.
142
143 // set up the text view
144 [mTextView setMaxSize:NSMakeSize(kMaxTextFieldWidth, screenSize.height - 2 * k BorderPadding)]; // do this here since we know screen size
145 [mTextView setString:string]; // do this after setting max size, before settin g constrained frame size,
146 // reset to max width - it will not grow horizon tally when resizing, only vertically
147 [mTextView setConstrainedFrameSize:NSMakeSize(kMaxTextFieldWidth, 0.0)];
148 // to avoid wrapping when we don't want it, set the min width
149 [mTextView setMinSize:NSMakeSize(textViewWidth, 0.0)];
150
151 // finally, do the buggy sizeToFit
152 [mTextView sizeToFit];
153 // The first time we sizeToFit a text field on Leopard, it decides that
154 // 0 would be a good height. We disagree, so make it try again.
155 NSRect textViewFrame = [mTextView frame];
156 if (textViewFrame.size.height < 1.0) {
157 [mTextView sizeToFit];
158 textViewFrame = [mTextView frame];
159 }
160
161 // set the origin back where its supposed to be
162 [mTextView setFrameOrigin:NSMakePoint(0, kBorderPadding)];
163
164 // size the panel correctly, taking border into account
165 NSSize textSize = textViewFrame.size;
166 textSize.height += kBorderPadding + kBorderPadding;
167 [mTooltipWindow setContentSize:textSize];
168
169 // We try to put the top left point right below the cursor. If that doesn't fi t
170 // on screen, put the bottom left point above the cursor.
171 if (point.y - kVOffset - textSize.height > NSMinY(screenFrame)) {
172 point.y -= kVOffset;
173 [mTooltipWindow setFrameTopLeftPoint:point];
174 }
175 else {
176 point.y += kVOffset / 2.5;
177 [mTooltipWindow setFrameOrigin:point];
178 }
179
180 // if it doesn't fit on screen horizontally, adjust so that it does
181 float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mTooltipWindow frame]);
182 if (amountOffScreenX < 0) {
183 NSRect movedFrame = [mTooltipWindow frame];
184 movedFrame.origin.x += amountOffScreenX;
185 [mTooltipWindow setFrame:movedFrame display:NO];
186 }
187
188 // add as a child window
189 [inWindow addChildWindow:mTooltipWindow ordered:NSWindowAbove];
190 // show the panel
191 [mTooltipWindow orderFront:nil];
192 }
193
194 - (void)closeToolTip
195 {
196 // we can get -closeToolTip even if we didn't show it
197 if ([mTooltipWindow isVisible])
198 {
199 [[mTooltipWindow parentWindow] removeChildWindow:mTooltipWindow];
200 [mTooltipWindow orderOut:nil];
201 }
202
203 [[NSNotificationCenter defaultCenter] removeObserver:self];
204 }
205
206 - (void)parentWindowDidResignKey:(NSNotification*)inNotification
207 {
208 [self closeToolTip];
209 }
210
211 @end
OLDNEW
« no previous file with comments | « third_party/mozilla/include/ToolTip.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698