1/******************************************************************************
2 * $Id: DragOverlayView.m 13251 2012-03-13 02:52:11Z livings124 $
3 *
4 * Copyright (c) 2007-2012 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "DragOverlayView.h"
26
27#define PADDING 10.0
28#define ICON_WIDTH 64.0
29
30@implementation DragOverlayView
31
32- (id) initWithFrame: (NSRect) frame
33{
34    if ((self = [super initWithFrame: frame]))
35    {
36        //create attributes
37        NSShadow * stringShadow = [[NSShadow alloc] init];
38        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
39        [stringShadow setShadowBlurRadius: 4.0];
40        
41        NSFont * bigFont = [NSFont boldSystemFontOfSize: 18.0],
42                * smallFont = [NSFont systemFontOfSize: 14.0];
43        
44        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
45        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingMiddle];
46        
47        fMainLineAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
48                                [NSColor whiteColor], NSForegroundColorAttributeName,
49                                bigFont, NSFontAttributeName, stringShadow, NSShadowAttributeName,
50                                paragraphStyle, NSParagraphStyleAttributeName, nil];
51        
52        fSubLineAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
53                                [NSColor whiteColor], NSForegroundColorAttributeName,
54                                smallFont, NSFontAttributeName, stringShadow, NSShadowAttributeName,
55                                paragraphStyle, NSParagraphStyleAttributeName, nil];
56        
57        [stringShadow release];
58        [paragraphStyle release];
59    }
60    return self;
61}
62
63- (void) dealloc
64{
65    [fBadge release];
66    
67    [fMainLineAttributes release];
68    [fSubLineAttributes release];
69    
70    [super dealloc];
71}
72
73- (void) setOverlay: (NSImage *) icon mainLine: (NSString *) mainLine subLine: (NSString *) subLine
74{
75    [fBadge release];
76    
77    //create badge
78    const NSRect badgeRect = NSMakeRect(0.0, 0.0, 325.0, 84.0);
79    
80    fBadge = [[NSImage alloc] initWithSize: badgeRect.size];
81    [fBadge lockFocus];
82    
83    NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: badgeRect xRadius: 15.0 yRadius: 15.0];
84    [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.75] set];
85    [bp fill];
86    
87    //place icon
88    [icon drawInRect: NSMakeRect(PADDING, (NSHeight(badgeRect) - ICON_WIDTH) * 0.5, ICON_WIDTH, ICON_WIDTH) fromRect: NSZeroRect
89            operation: NSCompositeSourceOver fraction: 1.0];
90    
91    //place main text
92    const NSSize mainLineSize = [mainLine sizeWithAttributes: fMainLineAttributes];
93    const NSSize subLineSize = [subLine sizeWithAttributes: fSubLineAttributes];
94    
95    NSRect lineRect = NSMakeRect(PADDING + ICON_WIDTH + 5.0,
96                        (NSHeight(badgeRect) + (subLineSize.height + 2.0 - mainLineSize.height)) * 0.5,
97                        NSWidth(badgeRect) - (PADDING + ICON_WIDTH + 2.0) - PADDING, mainLineSize.height);
98    [mainLine drawInRect: lineRect withAttributes: fMainLineAttributes];
99    
100    //place sub text
101    lineRect.origin.y -= subLineSize.height + 2.0;
102    lineRect.size.height = subLineSize.height;
103    [subLine drawInRect: lineRect withAttributes: fSubLineAttributes];
104    
105    [fBadge unlockFocus];
106    
107    [self setNeedsDisplay: YES];
108}
109
110-(void) drawRect: (NSRect) rect
111{
112    if (fBadge)
113    {
114        const NSRect frame = [self frame];
115        const NSSize imageSize = [fBadge size];
116        [fBadge compositeToPoint: NSMakePoint((NSWidth(frame) - imageSize.width) * 0.5,
117                    (NSHeight(frame) - imageSize.height) * 0.5) operation: NSCompositeSourceOver];
118    }
119}
120
121@end
122