From e37cd5e245e09ae323894582503fdc4f3b0cd3d0 Mon Sep 17 00:00:00 2001 From: "pinkerton%netscape.com" Date: Wed, 14 Aug 2002 19:37:40 +0000 Subject: [PATCH] Fix padding issues and a memory leak (bug 162205) --- camino/ToolTip.mm | 137 ------------------------------ camino/src/browser/ToolTip.mm | 147 ++++++++++++++++++--------------- chimera/ToolTip.mm | 137 ------------------------------ chimera/src/browser/ToolTip.mm | 137 ------------------------------ 4 files changed, 80 insertions(+), 478 deletions(-) diff --git a/camino/ToolTip.mm b/camino/ToolTip.mm index e435736c4dc..e69de29bb2d 100644 --- a/camino/ToolTip.mm +++ b/camino/ToolTip.mm @@ -1,137 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Netscape Public License - * Version 1.1 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is mozilla.org code. - * - * The Initial Developer of the Original Code is - * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 2002 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): Richard Schreyer - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#import "ToolTip.h" -#import "NSScreen+Utils.h" - -@interface ToolTip (ToolTipPrivateMethods) - -@end - - -@implementation ToolTip - -- (id)init -{ - self = [super init]; - if (self) { - mPanel = [[NSPanel alloc] initWithContentRect: NSMakeRect(0.0, 0.0, 200.0, 200.0) styleMask: NSBorderlessWindowMask backing: NSBackingStoreBuffered defer: YES]; - mTextField = [[[NSTextField alloc] initWithFrame: NSMakeRect(0.0, 0.0, 200.0, 200.0)] autorelease]; - - [[mPanel contentView] addSubview: mTextField]; - [mPanel setHasShadow: YES]; - - [mTextField setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; - [mTextField setBackgroundColor: [NSColor colorWithCalibratedRed: 1.0 green: 1.0 blue: .81 alpha: 1.0]]; - [mTextField setDrawsBackground: YES]; - [mTextField setEditable: NO]; - [mTextField setSelectable: NO]; - [mTextField setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [mTextField setBezeled: NO]; - [mTextField setBordered: NO]; - } - return self; -} - -- (void)dealloc -{ - [mPanel close]; - [mPanel release]; - [super dealloc]; -} - -- (void)showToolTipAtPoint:(NSPoint)point withString:(NSString*)string -{ - if ([string length] == 0) - return; - - NSScreen* screen = [NSScreen screenForPoint: point]; - if (!screen) - screen = [NSScreen mainScreen]; - - if (screen) { - NSRect screenFrame = [screen visibleFrame]; - NSSize screenSize = screenFrame.size; - NSTextView* textView = [[NSTextView alloc] initWithFrame: screenFrame]; - - // find out how large the tooltip needs to be - - [textView setString: string]; - [textView setMinSize: NSMakeSize(0.0, 0.0)]; - [textView setMaxSize: screenSize]; - [textView setVerticallyResizable: YES]; - [textView setHorizontallyResizable: YES]; - [textView setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [textView sizeToFit]; - NSSize textSize = NSMakeSize( ceil( NSWidth([textView frame]) ), ceil( NSHeight([textView frame]) ) ); - - // set up the panel - - [mTextField setStringValue: string]; - [mPanel setContentSize: textSize]; - - // the given point is right where the mouse pointer is. We want the tooltip's - // top left corner somewhere below that, but not if that'll put it off the monitor. There - // is no way that we can go off the top of the monitor because cocoa won't let us position a window - // that way - const int kVOffset = 20; - if ( point.y - kVOffset - textSize.height > NSMinY(screenFrame) ) - point.y -= kVOffset; - else - point.y += kVOffset; - [mPanel setFrameTopLeftPoint: point]; - - // if it goes off the edge of the screen, shift around to put it all on the screen - - float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mPanel frame]); - if (amountOffScreenX < 0) { - NSRect movedFrame = [mPanel frame]; - movedFrame.origin.x += amountOffScreenX; - [mPanel setFrame: movedFrame display: NO]; - } - - // show the panel - [mPanel orderFront: nil]; - } -} - -- (void)closeToolTip -{ - [mPanel close]; -} - -@end diff --git a/camino/src/browser/ToolTip.mm b/camino/src/browser/ToolTip.mm index e435736c4dc..7d46337df28 100644 --- a/camino/src/browser/ToolTip.mm +++ b/camino/src/browser/ToolTip.mm @@ -42,96 +42,109 @@ @end +const float kBorderPadding = 2.0; @implementation ToolTip - (id)init { - self = [super init]; - if (self) { - mPanel = [[NSPanel alloc] initWithContentRect: NSMakeRect(0.0, 0.0, 200.0, 200.0) styleMask: NSBorderlessWindowMask backing: NSBackingStoreBuffered defer: YES]; - mTextField = [[[NSTextField alloc] initWithFrame: NSMakeRect(0.0, 0.0, 200.0, 200.0)] autorelease]; - - [[mPanel contentView] addSubview: mTextField]; - [mPanel setHasShadow: YES]; - - [mTextField setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; - [mTextField setBackgroundColor: [NSColor colorWithCalibratedRed: 1.0 green: 1.0 blue: .81 alpha: 1.0]]; - [mTextField setDrawsBackground: YES]; - [mTextField setEditable: NO]; - [mTextField setSelectable: NO]; - [mTextField setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [mTextField setBezeled: NO]; - [mTextField setBordered: NO]; - } - return self; + + self = [super init]; + if (self) { + mPanel = [[NSPanel alloc] initWithContentRect: NSMakeRect(0.0, 0.0, 200.0, 200.0) styleMask: NSBorderlessWindowMask backing: NSBackingStoreBuffered defer: YES]; + + // Create a textfield as the content of our new window. Autorelease it because the window will + // hold a ref when we add it as a subview. Field occupies all but the top 2 and bottom + // 2 pixels of the panel (bug 149635) + mTextField = [[[NSTextField alloc] initWithFrame: NSMakeRect(0.0, kBorderPadding, 200.0, 200 - 2*kBorderPadding)] autorelease]; + + [[mPanel contentView] addSubview: mTextField]; + [mPanel setHasShadow: YES]; + [mPanel setBackgroundColor: [NSColor colorWithCalibratedRed: 1.0 green: 1.0 blue: .81 alpha: 1.0]]; + + [mTextField setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; + [mTextField setDrawsBackground: NO]; + [mTextField setEditable: NO]; + [mTextField setSelectable: NO]; + [mTextField setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; + [mTextField setBezeled: NO]; + [mTextField setBordered: NO]; + } + return self; } - (void)dealloc { - [mPanel close]; - [mPanel release]; - [super dealloc]; + [mPanel close]; + [mPanel release]; + [super dealloc]; } - (void)showToolTipAtPoint:(NSPoint)point withString:(NSString*)string -{ - if ([string length] == 0) - return; - - NSScreen* screen = [NSScreen screenForPoint: point]; - if (!screen) - screen = [NSScreen mainScreen]; - - if (screen) { - NSRect screenFrame = [screen visibleFrame]; - NSSize screenSize = screenFrame.size; - NSTextView* textView = [[NSTextView alloc] initWithFrame: screenFrame]; - - // find out how large the tooltip needs to be - - [textView setString: string]; - [textView setMinSize: NSMakeSize(0.0, 0.0)]; - [textView setMaxSize: screenSize]; - [textView setVerticallyResizable: YES]; - [textView setHorizontallyResizable: YES]; - [textView setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [textView sizeToFit]; - NSSize textSize = NSMakeSize( ceil( NSWidth([textView frame]) ), ceil( NSHeight([textView frame]) ) ); - - // set up the panel - - [mTextField setStringValue: string]; - [mPanel setContentSize: textSize]; +{ + if ([string length] == 0) + return; - // the given point is right where the mouse pointer is. We want the tooltip's - // top left corner somewhere below that, but not if that'll put it off the monitor. There + NSScreen* screen = [NSScreen screenForPoint: point]; + if (!screen) + screen = [NSScreen mainScreen]; + + if (screen) { + NSRect screenFrame = [screen visibleFrame]; + NSSize screenSize = screenFrame.size; + NSTextView* textView = [[[NSTextView alloc] initWithFrame: screenFrame] autorelease]; + + // find out how large the tooltip needs to be + + [textView setString: string]; + [textView setMinSize: NSMakeSize(0.0, 0.0)]; + [textView setMaxSize: screenSize]; + [textView setVerticallyResizable: YES]; + [textView setHorizontallyResizable: YES]; + [textView setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; + [textView sizeToFit]; + NSSize textSize = NSMakeSize( ceil( NSWidth([textView frame]) ), ceil( NSHeight([textView frame]) ) ); + + // adjust the size of the tooltip by a constant amount, as illustrated in bug 149635 + // specifically, add padding above and below the text, and remove 6 from the right side + // (six!? we're still not sure why it's six pixels too big. ideas are welcome). + + textSize.width -= 6; + textSize.height += kBorderPadding + kBorderPadding; + + // set up the panel + + [mTextField setStringValue: string]; + [mPanel setContentSize: textSize]; + + // the given point is right where the mouse pointer is. We want the tooltip's + // top left corner somewhere below that, but not if that'll put it off the monitor. There // is no way that we can go off the top of the monitor because cocoa won't let us position a window // that way const int kVOffset = 20; - if ( point.y - kVOffset - textSize.height > NSMinY(screenFrame) ) + if ( point.y - kVOffset - textSize.height > NSMinY(screenFrame) ) point.y -= kVOffset; else point.y += kVOffset; - [mPanel setFrameTopLeftPoint: point]; - - // if it goes off the edge of the screen, shift around to put it all on the screen - - float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mPanel frame]); - if (amountOffScreenX < 0) { - NSRect movedFrame = [mPanel frame]; - movedFrame.origin.x += amountOffScreenX; - [mPanel setFrame: movedFrame display: NO]; - } - - // show the panel - [mPanel orderFront: nil]; - } + [mPanel setFrameTopLeftPoint: point]; + + // if it goes off the edge of the screen, shift around to put it all on the screen + + float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mPanel frame]); + if (amountOffScreenX < 0) { + NSRect movedFrame = [mPanel frame]; + movedFrame.origin.x += amountOffScreenX; + [mPanel setFrame: movedFrame display: NO]; + } + + // show the panel + [mPanel orderFront: nil]; + } } - (void)closeToolTip { - [mPanel close]; + [mPanel close]; } @end diff --git a/chimera/ToolTip.mm b/chimera/ToolTip.mm index e435736c4dc..e69de29bb2d 100644 --- a/chimera/ToolTip.mm +++ b/chimera/ToolTip.mm @@ -1,137 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Netscape Public License - * Version 1.1 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is mozilla.org code. - * - * The Initial Developer of the Original Code is - * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 2002 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): Richard Schreyer - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#import "ToolTip.h" -#import "NSScreen+Utils.h" - -@interface ToolTip (ToolTipPrivateMethods) - -@end - - -@implementation ToolTip - -- (id)init -{ - self = [super init]; - if (self) { - mPanel = [[NSPanel alloc] initWithContentRect: NSMakeRect(0.0, 0.0, 200.0, 200.0) styleMask: NSBorderlessWindowMask backing: NSBackingStoreBuffered defer: YES]; - mTextField = [[[NSTextField alloc] initWithFrame: NSMakeRect(0.0, 0.0, 200.0, 200.0)] autorelease]; - - [[mPanel contentView] addSubview: mTextField]; - [mPanel setHasShadow: YES]; - - [mTextField setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; - [mTextField setBackgroundColor: [NSColor colorWithCalibratedRed: 1.0 green: 1.0 blue: .81 alpha: 1.0]]; - [mTextField setDrawsBackground: YES]; - [mTextField setEditable: NO]; - [mTextField setSelectable: NO]; - [mTextField setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [mTextField setBezeled: NO]; - [mTextField setBordered: NO]; - } - return self; -} - -- (void)dealloc -{ - [mPanel close]; - [mPanel release]; - [super dealloc]; -} - -- (void)showToolTipAtPoint:(NSPoint)point withString:(NSString*)string -{ - if ([string length] == 0) - return; - - NSScreen* screen = [NSScreen screenForPoint: point]; - if (!screen) - screen = [NSScreen mainScreen]; - - if (screen) { - NSRect screenFrame = [screen visibleFrame]; - NSSize screenSize = screenFrame.size; - NSTextView* textView = [[NSTextView alloc] initWithFrame: screenFrame]; - - // find out how large the tooltip needs to be - - [textView setString: string]; - [textView setMinSize: NSMakeSize(0.0, 0.0)]; - [textView setMaxSize: screenSize]; - [textView setVerticallyResizable: YES]; - [textView setHorizontallyResizable: YES]; - [textView setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [textView sizeToFit]; - NSSize textSize = NSMakeSize( ceil( NSWidth([textView frame]) ), ceil( NSHeight([textView frame]) ) ); - - // set up the panel - - [mTextField setStringValue: string]; - [mPanel setContentSize: textSize]; - - // the given point is right where the mouse pointer is. We want the tooltip's - // top left corner somewhere below that, but not if that'll put it off the monitor. There - // is no way that we can go off the top of the monitor because cocoa won't let us position a window - // that way - const int kVOffset = 20; - if ( point.y - kVOffset - textSize.height > NSMinY(screenFrame) ) - point.y -= kVOffset; - else - point.y += kVOffset; - [mPanel setFrameTopLeftPoint: point]; - - // if it goes off the edge of the screen, shift around to put it all on the screen - - float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mPanel frame]); - if (amountOffScreenX < 0) { - NSRect movedFrame = [mPanel frame]; - movedFrame.origin.x += amountOffScreenX; - [mPanel setFrame: movedFrame display: NO]; - } - - // show the panel - [mPanel orderFront: nil]; - } -} - -- (void)closeToolTip -{ - [mPanel close]; -} - -@end diff --git a/chimera/src/browser/ToolTip.mm b/chimera/src/browser/ToolTip.mm index e435736c4dc..e69de29bb2d 100644 --- a/chimera/src/browser/ToolTip.mm +++ b/chimera/src/browser/ToolTip.mm @@ -1,137 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Netscape Public License - * Version 1.1 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is mozilla.org code. - * - * The Initial Developer of the Original Code is - * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 2002 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): Richard Schreyer - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#import "ToolTip.h" -#import "NSScreen+Utils.h" - -@interface ToolTip (ToolTipPrivateMethods) - -@end - - -@implementation ToolTip - -- (id)init -{ - self = [super init]; - if (self) { - mPanel = [[NSPanel alloc] initWithContentRect: NSMakeRect(0.0, 0.0, 200.0, 200.0) styleMask: NSBorderlessWindowMask backing: NSBackingStoreBuffered defer: YES]; - mTextField = [[[NSTextField alloc] initWithFrame: NSMakeRect(0.0, 0.0, 200.0, 200.0)] autorelease]; - - [[mPanel contentView] addSubview: mTextField]; - [mPanel setHasShadow: YES]; - - [mTextField setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; - [mTextField setBackgroundColor: [NSColor colorWithCalibratedRed: 1.0 green: 1.0 blue: .81 alpha: 1.0]]; - [mTextField setDrawsBackground: YES]; - [mTextField setEditable: NO]; - [mTextField setSelectable: NO]; - [mTextField setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [mTextField setBezeled: NO]; - [mTextField setBordered: NO]; - } - return self; -} - -- (void)dealloc -{ - [mPanel close]; - [mPanel release]; - [super dealloc]; -} - -- (void)showToolTipAtPoint:(NSPoint)point withString:(NSString*)string -{ - if ([string length] == 0) - return; - - NSScreen* screen = [NSScreen screenForPoint: point]; - if (!screen) - screen = [NSScreen mainScreen]; - - if (screen) { - NSRect screenFrame = [screen visibleFrame]; - NSSize screenSize = screenFrame.size; - NSTextView* textView = [[NSTextView alloc] initWithFrame: screenFrame]; - - // find out how large the tooltip needs to be - - [textView setString: string]; - [textView setMinSize: NSMakeSize(0.0, 0.0)]; - [textView setMaxSize: screenSize]; - [textView setVerticallyResizable: YES]; - [textView setHorizontallyResizable: YES]; - [textView setFont: [NSFont toolTipsFontOfSize: [NSFont smallSystemFontSize]]]; - [textView sizeToFit]; - NSSize textSize = NSMakeSize( ceil( NSWidth([textView frame]) ), ceil( NSHeight([textView frame]) ) ); - - // set up the panel - - [mTextField setStringValue: string]; - [mPanel setContentSize: textSize]; - - // the given point is right where the mouse pointer is. We want the tooltip's - // top left corner somewhere below that, but not if that'll put it off the monitor. There - // is no way that we can go off the top of the monitor because cocoa won't let us position a window - // that way - const int kVOffset = 20; - if ( point.y - kVOffset - textSize.height > NSMinY(screenFrame) ) - point.y -= kVOffset; - else - point.y += kVOffset; - [mPanel setFrameTopLeftPoint: point]; - - // if it goes off the edge of the screen, shift around to put it all on the screen - - float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mPanel frame]); - if (amountOffScreenX < 0) { - NSRect movedFrame = [mPanel frame]; - movedFrame.origin.x += amountOffScreenX; - [mPanel setFrame: movedFrame display: NO]; - } - - // show the panel - [mPanel orderFront: nil]; - } -} - -- (void)closeToolTip -{ - [mPanel close]; -} - -@end