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

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

Issue 125133: Make tooltips work correctly, allowing for multiple tooltips w/out the mouse ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 years, 6 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
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 [[NSNotificationCenter defaultCenter] removeObserver:self];
95
96 [mTooltipWindow close]; // we set the window not to release on -close
97 [mTooltipWindow release];
98
99 [super dealloc];
100 }
101
102 - (void)showToolTipAtPoint:(NSPoint)windowPoint
103 withString:(NSString*)string
104 overWindow:(NSWindow*)inWindow
105 {
106 if ([string length] == 0)
107 return;
108
109 NSPoint point = [inWindow convertBaseToScreen:windowPoint];
110 NSScreen* screen = [NSScreen screenForPoint:point];
111 if (!screen)
112 screen = [NSScreen mainScreen];
113
114 if (!screen)
115 return;
116
117 // register for window losing key status notifications, so we can hide the too ltip
118 // on window deactivation
119 [[NSNotificationCenter defaultCenter] addObserver:self
120 selector:@selector(parentWindowDidRes ignKey:)
121 name:NSWindowDidResignKeyNotifica tion
122 object:inWindow];
123
124 NSRect screenFrame = [screen visibleFrame];
125 NSSize screenSize = screenFrame.size;
126
127 // for some reason, text views suffer from hysteresis; the answer you get this time
128 // depends on what you had in there before. so clear state first.
129 [mTextView setString:@""];
130 [mTextView setFrame:NSMakeRect(0, kBorderPadding, 0, 0)];
131
132 // -sizeToFit sucks. For some reason it likes to wrap short words, so
133 // we measure the text by hand and set that as the min width.
134 NSSize stringSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObj ect:[NSFont toolTipsFontOfSize:[NSFont smallSystemFontSize]] forKey:NSFontAttrib uteName]];
135 float textViewWidth = ceil(stringSize.width);
136 if (textViewWidth > kMaxTextFieldWidth)
137 textViewWidth = kMaxTextFieldWidth;
138
139 textViewWidth += 2.0 * 5.0; // magic numbers required to make the text not w rap. No, this isn't -textContainerInset.
140
141 // set up the text view
142 [mTextView setMaxSize:NSMakeSize(kMaxTextFieldWidth, screenSize.height - 2 * k BorderPadding)]; // do this here since we know screen size
143 [mTextView setString:string]; // do this after setting max size, before settin g constrained frame size,
144 // reset to max width - it will not grow horizon tally when resizing, only vertically
145 [mTextView setConstrainedFrameSize:NSMakeSize(kMaxTextFieldWidth, 0.0)];
146 // to avoid wrapping when we don't want it, set the min width
147 [mTextView setMinSize:NSMakeSize(textViewWidth, 0.0)];
148
149 // finally, do the buggy sizeToFit
150 [mTextView sizeToFit];
151 // The first time we sizeToFit a text field on Leopard, it decides that
152 // 0 would be a good height. We disagree, so make it try again.
153 NSRect textViewFrame = [mTextView frame];
154 if (textViewFrame.size.height < 1.0) {
155 [mTextView sizeToFit];
156 textViewFrame = [mTextView frame];
157 }
158
159 // set the origin back where its supposed to be
160 [mTextView setFrameOrigin:NSMakePoint(0, kBorderPadding)];
161
162 // size the panel correctly, taking border into account
163 NSSize textSize = textViewFrame.size;
164 textSize.height += kBorderPadding + kBorderPadding;
165 [mTooltipWindow setContentSize:textSize];
166
167 // We try to put the top left point right below the cursor. If that doesn't fi t
168 // on screen, put the bottom left point above the cursor.
169 if (point.y - kVOffset - textSize.height > NSMinY(screenFrame)) {
170 point.y -= kVOffset;
171 [mTooltipWindow setFrameTopLeftPoint:point];
172 }
173 else {
174 point.y += kVOffset / 2.5;
175 [mTooltipWindow setFrameOrigin:point];
176 }
177
178 // if it doesn't fit on screen horizontally, adjust so that it does
179 float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mTooltipWindow frame]);
180 if (amountOffScreenX < 0) {
181 NSRect movedFrame = [mTooltipWindow frame];
182 movedFrame.origin.x += amountOffScreenX;
183 [mTooltipWindow setFrame:movedFrame display:NO];
184 }
185
186 // add as a child window
187 [inWindow addChildWindow:mTooltipWindow ordered:NSWindowAbove];
188 // show the panel
189 [mTooltipWindow orderFront:nil];
190 }
191
192 - (void)closeToolTip
193 {
194 // we can get -closeToolTip even if we didn't show it
195 if ([mTooltipWindow isVisible])
196 {
197 [[mTooltipWindow parentWindow] removeChildWindow:mTooltipWindow];
198 [mTooltipWindow orderOut:nil];
199 }
200
201 [[NSNotificationCenter defaultCenter] removeObserver:self];
202 }
203
204 - (void)parentWindowDidResignKey:(NSNotification*)inNotification
205 {
206 [self closeToolTip];
207 }
208
209 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698