1/* DefEditorPref.m
2 *
3 * Copyright (C) 2003-2010 Free Software Foundation, Inc.
4 *
5 * Author: Enrico Sersale <enrico@imago.ro>
6 * Date: August 2001
7 *
8 * This file is part of the GNUstep GWorkspace application
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
23 */
24
25#include <math.h>
26
27#import <Foundation/Foundation.h>
28#import <AppKit/AppKit.h>
29#import <GNUstepBase/GNUstep.h>
30
31#import "FSNodeRep.h"
32#import "DefEditorPref.h"
33#import "GWorkspace.h"
34
35
36
37#define LABEL_MARGIN 8
38#define ICON_SIZE 48
39
40static NSString *nibName = @"DefEditorPref";
41
42@implementation DefEditorPref
43
44- (void)dealloc
45{
46  RELEASE (prefbox);
47  RELEASE (ednode);
48  RELEASE (noEditorStr);
49  RELEASE (font);
50  [super dealloc];
51}
52
53- (id)init
54{
55  self = [super init];
56
57  if (self) {
58    ASSIGN (font, [NSFont systemFontOfSize: 12]);
59    ASSIGN (noEditorStr, NSLocalizedString(@"No Default Editor", @""));
60
61		if ([NSBundle loadNibNamed: nibName owner: self] == NO) {
62      NSLog(@"failed to load %@!", nibName);
63    } else {
64	    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
65      NSString *editor = [defaults stringForKey: @"defaulteditor"];
66
67      RETAIN (prefbox);
68      iconBoxWidth = [iconbox bounds].size.width;
69      labelHeight = [nameLabel frame].size.height;
70      labelOrigin = [nameLabel frame].origin;
71      RELEASE (win);
72
73      fsnodeRep = [FSNodeRep sharedInstance];
74		  ws = [NSWorkspace sharedWorkspace];
75
76      [imView setImageScaling: NSScaleProportionally];
77
78      if (editor) {
79        NSString *path = [ws fullPathForApplication: editor];
80
81        if (path) {
82          NSImage *image;
83
84		      ASSIGN (ednode, [FSNode nodeWithPath: path]);
85          image = [fsnodeRep iconOfSize: ICON_SIZE forNode: ednode];
86          [imView setImage: image];
87
88	        [nameLabel setStringValue: [ednode name]];
89          [self tile];
90        } else {
91	        [nameLabel setStringValue: noEditorStr];
92          [self tile];
93        }
94      } else {
95	      [nameLabel setStringValue: noEditorStr];
96        [self tile];
97      }
98
99      /* Internationalization */
100      [chooseButt setTitle: NSLocalizedString(@"Choose", @"")];
101      [iconbox setTitle: NSLocalizedString(@"Default Editor", @"")];
102    }
103  }
104
105  return self;
106}
107
108- (NSView *)prefView
109{
110  return prefbox;
111}
112
113- (NSString *)prefName
114{
115  return NSLocalizedString(@"Editor", @"");
116}
117
118- (IBAction)chooseEditor:(id)sender
119{
120  NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSSystemDomainMask, YES) lastObject];
121	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
122	NSArray *fileTypes = [NSArray arrayWithObjects: @"app", @"debug", @"profile", nil];
123	FSNode *node;
124	int result;
125
126	[openPanel setTitle: @"open"];
127  [openPanel setAllowsMultipleSelection: NO];
128  [openPanel setCanChooseFiles: YES];
129  [openPanel setCanChooseDirectories: NO];
130
131  result = [openPanel runModalForDirectory: path file: nil types: fileTypes];
132	if(result != NSOKButton) {
133		return;
134  }
135
136	node = [FSNode nodeWithPath: [openPanel filename]];
137
138  if (([node isValid] == NO) || ([node isApplication] == NO)) {
139    NSRunAlertPanel(nil,
140        [NSString stringWithFormat: @"%@ %@",
141                [node name], NSLocalizedString(@"is not a valid application!", @"")],
142                            @"Continue",
143                            nil,
144                            nil);
145    return;
146  }
147
148  [self setEditor: [node name]];
149}
150
151- (void)setEditor:(NSString *)editor
152{
153  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
154  NSString *path;
155  NSImage *image;
156
157  if ([editor isEqual: [ednode name]]) {
158    return;
159  }
160
161  path = [ws fullPathForApplication: editor];
162
163  if (path) {
164    ASSIGN (ednode, [FSNode nodeWithPath: path]);
165    image = [fsnodeRep iconOfSize: ICON_SIZE forNode: ednode];
166    [imView setImage: image];
167
168    [nameLabel setStringValue: [ednode name]];
169    [self tile];
170
171	  [defaults setObject: [ednode name] forKey: @"defaulteditor"];
172	  [defaults synchronize];
173
174	  [[NSDistributedNotificationCenter defaultCenter]
175 				  postNotificationName: @"GWDefaultEditorChangedNotification"
176	 								      object: [ednode name]
177                      userInfo: nil];
178  } else {
179    NSRunAlertPanel(nil,
180        [NSString stringWithFormat: @"%@ %@",
181                editor, NSLocalizedString(@"seems not a valid application!", @"")],
182                            @"Continue",
183                            nil,
184                            nil);
185  }
186}
187
188- (void)tile
189{
190  NSRect r = [nameLabel frame];
191  int labw = (int)[font widthOfString: [nameLabel stringValue]] + LABEL_MARGIN;
192  NSPoint p = NSMakePoint(0, labelOrigin.y);
193
194  r.size.width = labw;
195  [nameLabel setFrame: r];
196
197  p.x = ((iconBoxWidth - [nameLabel frame].size.width) / 2);
198  [nameLabel setFrameOrigin: p];
199
200  [iconbox setNeedsDisplay: YES];
201}
202
203@end
204
205
206
207
208
209
210
211
212