1 | /****************************************************************************** |
---|
2 | * $Id: DragOverlayView.m 9844 2010-01-01 21:12:04Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007-2010 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.0f |
---|
28 | #define ICON_WIDTH 64.0f |
---|
29 | |
---|
30 | @implementation DragOverlayView |
---|
31 | |
---|
32 | - (id) initWithFrame: (NSRect) frame |
---|
33 | { |
---|
34 | if ((self = [super initWithFrame: frame])) |
---|
35 | { |
---|
36 | //create badge |
---|
37 | NSRect badgeRect = NSMakeRect(0.0f, 0.0f, 325.0f, 84.0f); |
---|
38 | NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: badgeRect xRadius: 15.0f yRadius: 15.0f]; |
---|
39 | |
---|
40 | fBackBadge = [[NSImage alloc] initWithSize: badgeRect.size]; |
---|
41 | [fBackBadge lockFocus]; |
---|
42 | |
---|
43 | [[NSColor colorWithCalibratedWhite: 0.0f alpha: 0.75f] set]; |
---|
44 | [bp fill]; |
---|
45 | |
---|
46 | [fBackBadge unlockFocus]; |
---|
47 | |
---|
48 | //create attributes |
---|
49 | NSShadow * stringShadow = [[NSShadow alloc] init]; |
---|
50 | [stringShadow setShadowOffset: NSMakeSize(2.0f, -2.0f)]; |
---|
51 | [stringShadow setShadowBlurRadius: 4.0f]; |
---|
52 | |
---|
53 | NSFont * bigFont = [[NSFontManager sharedFontManager] convertFont: |
---|
54 | [NSFont fontWithName: @"Lucida Grande" size: 18.0f] toHaveTrait: NSBoldFontMask], |
---|
55 | * smallFont = [NSFont fontWithName: @"Lucida Grande" size: 14.0f]; |
---|
56 | |
---|
57 | NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
---|
58 | [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail]; |
---|
59 | |
---|
60 | fMainLineAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
61 | [NSColor whiteColor], NSForegroundColorAttributeName, |
---|
62 | bigFont, NSFontAttributeName, stringShadow, NSShadowAttributeName, |
---|
63 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
64 | |
---|
65 | fSubLineAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
66 | [NSColor whiteColor], NSForegroundColorAttributeName, |
---|
67 | smallFont, NSFontAttributeName, stringShadow, NSShadowAttributeName, |
---|
68 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
69 | |
---|
70 | [stringShadow release]; |
---|
71 | [paragraphStyle release]; |
---|
72 | } |
---|
73 | return self; |
---|
74 | } |
---|
75 | |
---|
76 | - (void) dealloc |
---|
77 | { |
---|
78 | [fBackBadge release]; |
---|
79 | [fBadge release]; |
---|
80 | |
---|
81 | [fMainLineAttributes release]; |
---|
82 | [fSubLineAttributes release]; |
---|
83 | |
---|
84 | [super dealloc]; |
---|
85 | } |
---|
86 | |
---|
87 | - (void) setOverlay: (NSImage *) icon mainLine: (NSString *) mainLine subLine: (NSString *) subLine |
---|
88 | { |
---|
89 | [fBadge release]; |
---|
90 | fBadge = [fBackBadge copy]; |
---|
91 | NSSize badgeSize = [fBadge size]; |
---|
92 | |
---|
93 | [fBadge lockFocus]; |
---|
94 | |
---|
95 | //place icon |
---|
96 | [icon drawInRect: NSMakeRect(PADDING, (badgeSize.height - ICON_WIDTH) * 0.5f, ICON_WIDTH, ICON_WIDTH) fromRect: NSZeroRect |
---|
97 | operation: NSCompositeSourceOver fraction: 1.0f]; |
---|
98 | |
---|
99 | //place main text |
---|
100 | NSSize mainLineSize = [mainLine sizeWithAttributes: fMainLineAttributes]; |
---|
101 | NSSize subLineSize = [subLine sizeWithAttributes: fSubLineAttributes]; |
---|
102 | |
---|
103 | NSRect lineRect = NSMakeRect(PADDING + ICON_WIDTH + 5.0f, |
---|
104 | (badgeSize.height + (subLineSize.height + 2.0f - mainLineSize.height)) * 0.5f, |
---|
105 | badgeSize.width - (PADDING + ICON_WIDTH + 2.0f) - PADDING, mainLineSize.height); |
---|
106 | [mainLine drawInRect: lineRect withAttributes: fMainLineAttributes]; |
---|
107 | |
---|
108 | //place sub text |
---|
109 | lineRect.origin.y -= subLineSize.height + 2.0f; |
---|
110 | lineRect.size.height = subLineSize.height; |
---|
111 | [subLine drawInRect: lineRect withAttributes: fSubLineAttributes]; |
---|
112 | |
---|
113 | [fBadge unlockFocus]; |
---|
114 | |
---|
115 | [self setNeedsDisplay: YES]; |
---|
116 | } |
---|
117 | |
---|
118 | -(void) drawRect: (NSRect) rect |
---|
119 | { |
---|
120 | if (fBadge) |
---|
121 | { |
---|
122 | NSRect frame = [self frame]; |
---|
123 | NSSize imageSize = [fBadge size]; |
---|
124 | [fBadge compositeToPoint: NSMakePoint((frame.size.width - imageSize.width) * 0.5f, |
---|
125 | (frame.size.height - imageSize.height) * 0.5f) operation: NSCompositeSourceOver]; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | @end |
---|