Commit 150f4bbc authored by rjw's avatar rjw
Browse files

i * WebView.subproj/WebHTMLView.m:

        (-[WebHTMLView mouseDragged:]):

        More fun and games with dragged links.
        Draw both a label and url (in a smaller font)
        into the dragged image.

        * Misc.subproj/WebStringTruncator.h:
        * Misc.subproj/WebStringTruncator.m:
        (+[WebStringTruncator rightTruncateString:toWidth:withFont:]):

        Added right string truncator.  Not optimized, but not used
        very often.  Currently just for dragged link experiment.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@1984 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent e9989357
2002-09-06 Richard Williamson (Home) <rjw@apple.com>
* WebView.subproj/WebHTMLView.m:
(-[WebHTMLView mouseDragged:]):
More fun and games with dragged links.
Draw both a label and url (in a smaller font)
into the dragged image.
* Misc.subproj/WebStringTruncator.h:
* Misc.subproj/WebStringTruncator.m:
(+[WebStringTruncator rightTruncateString:toWidth:withFont:]):
Added right string truncator. Not optimized, but not used
very often. Currently just for dragged link experiment.
=== Alexander-22 ===
2002-09-06 Ken Kocienda <kocienda@apple.com>
......
2002-09-06 Richard Williamson (Home) <rjw@apple.com>
* WebView.subproj/WebHTMLView.m:
(-[WebHTMLView mouseDragged:]):
More fun and games with dragged links.
Draw both a label and url (in a smaller font)
into the dragged image.
* Misc.subproj/WebStringTruncator.h:
* Misc.subproj/WebStringTruncator.m:
(+[WebStringTruncator rightTruncateString:toWidth:withFont:]):
Added right string truncator. Not optimized, but not used
very often. Currently just for dragged link experiment.
=== Alexander-22 ===
2002-09-06 Ken Kocienda <kocienda@apple.com>
......
......@@ -19,4 +19,6 @@
// Default font is [NSFont menuFontOfSize:0].
+ (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth;
+ (NSString *)rightTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font;
@end
......@@ -55,6 +55,40 @@ + (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth
return [self centerTruncateString:string toWidth:maxWidth withFont:[NSFont menuFontOfSize:0]];
}
+ (NSString *)rightTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font
{
unichar stringBuffer[STRING_BUFFER_SIZE];
WebTextRenderer *renderer;
unichar ellipsis;
float ellipsisWidth;
float width;
unsigned truncatedLength = [string length];
ASSERT (truncatedLength+1 < STRING_BUFFER_SIZE);
// FIXME: Allocate buffer is string doesn't fit in local buffer.
[string getCharacters:stringBuffer];
renderer = [[WebTextRendererFactory sharedFactory] rendererWithFont:font];
width = [renderer floatWidthForCharacters:stringBuffer
stringLength:truncatedLength fromCharacterPosition: 0 numberOfCharacters: truncatedLength applyRounding: NO attemptFontSubstitution: YES];
if (width <= maxWidth)
return string;
ellipsis = ELLIPSIS_CHARACTER;
ellipsisWidth = [renderer floatWidthForCharacters:&ellipsis stringLength:1 fromCharacterPosition: 0 numberOfCharacters: 1 applyRounding: NO attemptFontSubstitution: YES];
maxWidth -= ellipsisWidth;
while (width > maxWidth && truncatedLength){
truncatedLength--;
width = [renderer floatWidthForCharacters:stringBuffer
stringLength:truncatedLength fromCharacterPosition: 0 numberOfCharacters: truncatedLength applyRounding: NO attemptFontSubstitution: YES];
}
stringBuffer[truncatedLength++] = ELLIPSIS_CHARACTER;
return [NSString stringWithCharacters:stringBuffer length:truncatedLength];
}
+ (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font
{
unichar stringBuffer[STRING_BUFFER_SIZE];
......
......@@ -17,6 +17,7 @@
#import <WebKit/WebIconLoader.h>
#import <WebKit/WebKitDebug.h>
#import <WebKit/WebNSViewExtras.h>
#import <WebKit/WebStringTruncator.h>
#import <WebKit/WebTextRenderer.h>
#import <WebKit/WebTextRendererFactory.h>
#import <WebKit/WebViewPrivate.h>
......@@ -416,6 +417,8 @@ - (void)mouseDown: (NSEvent *)event
#define DragStartXHysteresis 2.0
#define DragStartYHysteresis 1.0
#define DRAG_LABEL_BORDER 2.0
- (void)mouseDragged:(NSEvent *)event
{
// Ensure that we're visible wrt the event location.
......@@ -442,23 +445,47 @@ - (void)mouseDragged:(NSEvent *)event
[self dragPromisedFilesOfTypes: fileType fromRect: rect source: self slideBack: YES event: event];
}
else if (linkURL) {
BOOL drawURLString = YES;
BOOL clipURLString = NO;
_private->draggedURL = linkURL;
NSString *label = [element objectForKey: WebContextMenuElementLinkLabelKey];
NSString *urlString = [linkURL absoluteString];
if (!label)
label = [linkURL absoluteString];
if (!label){
drawURLString = NO;
label = urlString;
}
NSFont *dragImageFont = [NSFont systemFontOfSize: 12.0];
NSDictionary *dragImageAttributes = [NSDictionary dictionaryWithObject:dragImageFont forKey: NSFontAttributeName];
NSSize imageSize = [label sizeWithAttributes: dragImageAttributes];
imageSize.width += 4.0;
imageSize.height += 4.0;
NSFont *labelFont = [NSFont systemFontOfSize: 12.0];
NSFont *urlFont = [NSFont systemFontOfSize: 8.0];
NSDictionary *labelAttributes = [NSDictionary dictionaryWithObject:labelFont forKey: NSFontAttributeName];
NSDictionary *urlAttributes = [NSDictionary dictionaryWithObject:urlFont forKey: NSFontAttributeName];
NSSize labelSize = [label sizeWithAttributes: labelAttributes];
NSSize imageSize, urlStringSize;
imageSize.width += labelSize.width + DRAG_LABEL_BORDER*2;
imageSize.height += labelSize.height + DRAG_LABEL_BORDER*2;
if (drawURLString){
urlStringSize = [urlString sizeWithAttributes: urlAttributes];
imageSize.height += urlStringSize.height;
// Clip the url string to 2.5 times the width of the label.
if (urlStringSize.width > 2.5 * labelSize.width){
imageSize.width = (labelSize.width*2.5)+DRAG_LABEL_BORDER*2;
clipURLString = YES;
}
else
imageSize.width = MAX(labelSize.width+DRAG_LABEL_BORDER*2,urlStringSize.width+DRAG_LABEL_BORDER*2);
}
NSImage *dragImage = [[[NSImage alloc] initWithSize: imageSize] autorelease];
[dragImage lockFocus];
[[NSColor colorWithCalibratedRed: 0.25 green: 0.25 blue: 0.75 alpha: 0.5] set];
[[NSColor colorWithCalibratedRed: 0.75 green: 0.75 blue: 1.0 alpha: 0.75] set];
[NSBezierPath fillRect:NSMakeRect(0, 0, imageSize.width, imageSize.height)];
[label drawAtPoint: NSMakePoint (2.0,2.0) withAttributes: dragImageAttributes];
if (drawURLString){
urlString = [WebStringTruncator rightTruncateString: urlString toWidth: imageSize.width-2.0 withFont: urlFont];
[urlString drawAtPoint: NSMakePoint (DRAG_LABEL_BORDER,DRAG_LABEL_BORDER) withAttributes: urlAttributes];
}
[label drawAtPoint: NSMakePoint (DRAG_LABEL_BORDER,DRAG_LABEL_BORDER+urlStringSize.height) withAttributes: labelAttributes];
[dragImage unlockFocus];
NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment