Documentation of `SecCertificateGetData()` says that we should malloc space for its...
[adiumx.git] / Plugins / Emoticons / AIEmoticonPackPreviewView.m
blob917aa9111809f467017e69e6578300c40eb855cd
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "AIEmoticonPack.h"
18 #import "AIEmoticonPackPreviewView.h"
19 #import <AIUtilities/AIParagraphStyleAdditions.h>
20 #import <Adium/AIEmoticon.h>
22 //Max size + bottom margin should equal previewView's height
23 #define EMOTICON_MAX_SIZE           20
24 #define EMOTICON_SPACING            4
26 #define EMOTICON_LEFT_MARGIN        2           //Left padding of cell
27 #define EMOTICON_BOTTOM_MARGIN      2
29 static  float   distanceBetweenEmoticons = 0;
31 @implementation AIEmoticonPackPreviewView
33 - (void)setEmoticonPack:(AIEmoticonPack *)inEmoticonPack
35         emoticonPack = [inEmoticonPack retain];
38 - (void)dealloc
40         [emoticonPack release];
41         
42         [super dealloc];
45 - (void)drawRect:(NSRect)rect
47         NSRect  cellFrame = [view_preview frame];
48         NSRect  nameFrame = [view_name frame];
49         
50         [super drawRect:rect];
51         
52         if (NSIntersectsRect(rect,nameFrame)) {
53                 //Display the title, truncating as necessary
54                 NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle styleWithAlignment:NSLeftTextAlignment
55                                                                                                                                                                 lineBreakMode:NSLineBreakByTruncatingTail];
56                 [paragraphStyle setMaximumLineHeight:nameFrame.size.height];
58                 [[emoticonPack name] drawInRect:nameFrame
59                                                  withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
60                                                          paragraphStyle, NSParagraphStyleAttributeName,
61                                                          [NSFont systemFontOfSize:12], NSFontAttributeName/*, 
62                                                          SELECTED_TEXT_COLOR, NSForegroundColorAttributeName*/, nil]];
63         }
65         if (NSIntersectsRect(rect,cellFrame)) {         
66                 NSEnumerator    *enumerator;
67                 AIEmoticon      *emoticon;
68                 float                   x = 0;
70                 //Display a few preview emoticons
71                 enumerator = [[emoticonPack emoticons] objectEnumerator];
72                 while ((x < cellFrame.size.width) && (emoticon = [enumerator nextObject])) {
73                         NSImage *image = [emoticon image];
74                         NSSize  imageSize = [image size];
75                         NSRect  destRect;
76                         
77                         //Scale the emoticon, preserving its proportions.
78                         if (imageSize.width > EMOTICON_MAX_SIZE) {
79                                 destRect.size.width = EMOTICON_MAX_SIZE;
80                                 destRect.size.height = imageSize.height * (EMOTICON_MAX_SIZE / imageSize.width);
81                         } else if (imageSize.height > EMOTICON_MAX_SIZE) {
82                                 destRect.size.width = imageSize.width * (EMOTICON_MAX_SIZE / imageSize.height);
83                                 destRect.size.height = EMOTICON_MAX_SIZE;
84                         } else {
85                                 destRect.size.width = imageSize.width;
86                                 destRect.size.height = imageSize.height;            
87                         }
88                         
89                         //Position it
90                         destRect.origin.x = cellFrame.origin.x + x;
91                         destRect.origin.y = cellFrame.origin.y + EMOTICON_BOTTOM_MARGIN;
93                         //If there is enough room, draw the image
94                         if ((destRect.origin.x + destRect.size.width) < (cellFrame.origin.x + cellFrame.size.width)) {
95                                 [image drawInRect:destRect
96                                                  fromRect:NSMakeRect(0, 0, imageSize.width, imageSize.height)
97                                                 operation:NSCompositeSourceOver
98                                                  fraction:1.0];
99                         }
100                         
101                         //Move over for the next emoticon, leaving some space
102                         float desiredIncrease = destRect.size.width + EMOTICON_SPACING;
103                         if (distanceBetweenEmoticons < desiredIncrease)
104                                 distanceBetweenEmoticons = desiredIncrease;
105                         x += distanceBetweenEmoticons;
106                 }
107         }
110 @end