-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPreviewElement.m
146 lines (131 loc) · 3.6 KB
/
PreviewElement.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* PreviewElement.m
*
* Copyright (C) 2006-2010 Free Software Foundation, Inc.
*
* Author: Richard Frith-Macdonald <[email protected]>
* Date: 2006
*
* This file is part of GNUstep.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <GNUstepGUI/GSTheme.h>
#import "AppController.h"
#import "ThemeDocument.h"
#import "PreviewElement.h"
@implementation PreviewElement
- (NSImage*) image
{
NSString *path;
NSImage *image;
image = nil;
path = [[[GSTheme theme] infoDictionary] objectForKey: @"GSThemePreview"];
if (nil != path)
{
path = [[[GSTheme theme] bundle] pathForResource: path ofType: nil];
if (nil != path)
{
image = [[NSImage alloc] initWithContentsOfFile: path];
}
}
return [image autorelease];
}
- (void) selectAt: (NSPoint)mouse
{
[previewImage setImage: [self image]];
[previewImage setNeedsDisplay];
[super selectAt: mouse];
}
- (void) setPreview: (id)sender
{
ThemeDocument *doc = [[AppController sharedController] selectedDocument];
NSImage *image = [sender image];
if (image != nil)
{
NSSize s = [image size];
float scale = 1.0;
NSString *path = nil;
if (s.height > 128.0)
scale = 128.0 / s.height;
if (128.0 / s.width < scale)
scale = 128.0 / s.width;
if (scale != 1.0)
{
[image setScalesWhenResized: YES];
s.height *= scale;
s.width *= scale;
[image setSize: s];
}
// FIXME ... store image to a temporary file to copy into theme
[doc setResource: path forKey: @"GSThemePreview"];
RELEASE(image);
[previewImage setImage: [self image]];
}
}
- (void) takePreview: (id)sender
{
ThemeDocument *doc = [[AppController sharedController] selectedDocument];
NSArray *fileTypes = [NSImage imageFileTypes];
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
int result;
[oPanel setAllowsMultipleSelection: NO];
[oPanel setCanChooseFiles: YES];
[oPanel setCanChooseDirectories: NO];
result = [oPanel runModalForDirectory: NSHomeDirectory()
file: nil
types: fileTypes];
if (result == NSOKButton)
{
NSString *path = [oPanel filename];
NSImage *image = nil;
NS_DURING
{
image = [[NSImage alloc] initWithContentsOfFile: path];
}
NS_HANDLER
{
NSString *message = [localException reason];
NSRunAlertPanel(_(@"Problem loading theme preview image"),
message, nil, nil, nil);
}
NS_ENDHANDLER
if (image != nil)
{
NSSize s = [image size];
float scale = 1.0;
if (s.height > 128.0)
scale = 128.0 / s.height;
if (128.0 / s.width < scale)
scale = 128.0 / s.width;
if (scale != 1.0)
{
[image setScalesWhenResized: YES];
s.height *= scale;
s.width *= scale;
[image setSize: s];
}
[doc setResource: path forKey: @"GSThemePreview"];
RELEASE(image);
[previewImage setImage: [self image]];
}
}
}
- (NSString*) title
{
return @"Preview";
}
@end