1/* Copyright (C) 2003 Raffael Herzog
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 *
17 * $Id: WrapperDocumentController.m 103 2004-08-09 16:30:51Z rherzog $
18 * $HeadURL: file:///home/rherzog/Subversion/GNUstep/GSWrapper/tags/release-0.1.0/WrapperFactory/WrapperDocumentController.m $
19 */
20
21#include <AppKit/AppKit.h>
22
23#include "WrapperDocumentController.h"
24#include "WrapperDocument.h"
25#include "Icon.h"
26
27
28static NSString *emptyString = @"";
29
30
31@interface WrapperDocumentController (Notifications)
32
33- (void)wrapperDocumentChangedNotification: (NSNotification *)not;
34
35@end
36
37
38@implementation WrapperDocumentController
39
40- (id)init
41{
42    self = [super init];
43    if ( self ) {
44//         textCursor = [NSCursor IBeamCursor];
45//         defaultCursor = [NSCursor arrowCursor];
46//         [textCursor setOnMouseEntered: YES];
47//         [defaultCursor setOnMouseExited: YES];
48        settingValue = NO;
49    }
50    return self;
51}
52
53- (void)awakeFromNib
54{
55//     if ( currentScript ) {
56//         NSLog(@"Setting cursor");
57//         [currentScript addCursorRect: [currentScript bounds]
58//                        cursor: (textCursor)];
59//         [currentScript addCursorRect: [currentScript bounds]
60//                        cursor: (defaultCursor)];
61//     }
62//    [self setCurrentScriptId: StartScript];
63}
64
65
66
67/*
68 * delegate methods
69 */
70
71- (void)controlTextDidChange: (NSNotification *)not
72{
73    [self textDidChange: not];
74}
75
76- (void)textDidChange: (NSNotification *)not
77{
78    if ( settingValue ) {
79        return;
80    }
81    id src = [not object];
82    settingValue = YES;
83    if ( src == name ) {
84        [document setName: AUTORELEASE([[name stringValue] copyWithZone: NSDefaultMallocZone()])];
85    }
86    else if ( src == version ) {
87        [document setVersion: AUTORELEASE([[version stringValue] copyWithZone: NSDefaultMallocZone()])];
88    }
89    else if ( src == fullVersion ) {
90        [document setFullVersion: AUTORELEASE([[fullVersion stringValue] copyWithZone: NSDefaultMallocZone()])];
91    }
92    else if ( src == description ) {
93        [document setDescription: AUTORELEASE([[description stringValue] copyWithZone: NSDefaultMallocZone()])];
94    }
95    else if ( src == url ) {
96        [document setUrl: AUTORELEASE([[url stringValue] copyWithZone: NSDefaultMallocZone()])];
97    }
98    else if ( src == authors ) {
99        [document setAuthors: AUTORELEASE([[authors stringValue] copyWithZone: NSDefaultMallocZone()])];
100    }
101    else if ( src == currentScriptShell ) {
102        NSString *shell = AUTORELEASE([[currentScriptShell stringValue] copyWithZone: NSDefaultMallocZone()]);
103        switch ( currentScriptId ) {
104        case StartScript:
105            [document setStartScriptShell: shell];
106            break;
107        case StartOpenScript:
108            [document setStartOpenScriptShell: shell];
109            break;
110        case OpenScript:
111            [document setOpenScriptShell: shell];
112            break;
113        default:
114            NSLog(@"Unknown script ID: %d", currentScriptId);
115        }
116    }
117    else if ( src == currentScript ) {
118        NSString *script = AUTORELEASE([[currentScript string] copyWithZone: NSDefaultMallocZone()]);
119        switch ( currentScriptId ) {
120        case StartScript:
121            [document setStartScript: script];
122            break;
123        case StartOpenScript:
124            [document setStartOpenScript: script];
125            break;
126        case OpenScript:
127            [document setOpenScript: script];
128            break;
129        default:
130            NSLog(@"Unknown script ID: %d", currentScriptId);
131        }
132    }
133    else {
134        NSLog(@"Received textDidChange notification from unknown control: %@", src);
135    }
136    settingValue = NO;
137}
138
139- (void)iconViewDidChangeIcon: (NSNotification *)not
140{
141    if ( settingValue ) {
142        return;
143    }
144    settingValue = YES;
145    if ( [not object] == appIcon ) {
146        [document setAppIcon: [(IconView *)[not object] icon]];
147    }
148    else {
149        NSLog(@"Received iconViewImageChanged notification from unknown ImageView: %@", [not object]);
150    }
151    settingValue = NO;
152}
153
154
155/*
156 * actions
157 */
158
159- (IBAction)setCurrentScriptToStart: (id)sender
160{
161    [self setCurrentScriptId: StartScript];
162}
163
164- (IBAction)setCurrentScriptToStartOpen: (id)sender
165{
166    [self setCurrentScriptId: StartOpenScript];
167}
168
169- (IBAction)setCurrentScriptToOpen: (id)sender
170{
171    [self setCurrentScriptId: OpenScript];
172}
173
174- (IBAction)setRole: (id)sender
175{
176    int tag = [[sender selectedItem] tag];
177    settingValue = YES;
178    switch ( tag ) {
179    case NoneRole:
180        [document setRole: NoneRole];
181        break;
182    case ViewerRole:
183        [document setRole: ViewerRole];
184        break;
185    case EditorRole:
186        [document setRole: EditorRole];
187        break;
188    default:
189        NSLog(@"Unknown role: %d", tag);
190    }
191    settingValue = NO;
192}
193
194- (IBAction)setCurrentScriptAction: (id)sender
195{
196    int tag = [[sender selectedItem] tag];
197    settingValue = YES;
198    switch ( currentScriptId ) {
199    case StartScript:
200        [document setStartScriptAction: tag];
201        break;
202    case StartOpenScript:
203        [document setStartOpenScriptAction: tag];
204        break;
205    case OpenScript:
206        [document setOpenScriptAction: tag];
207        break;
208    }
209}
210
211
212/*
213 * outlets
214 */
215
216- (void)setWindowController: (NSWindowController *)controller
217{
218    windowController = controller;
219    [self setDocument: [controller document]];
220}
221
222- (void)setDocument: (WrapperDocument *)d
223{
224    if ( document ) {
225        [[NSNotificationCenter defaultCenter] removeObserver: self
226                                              name: (WrapperChangedNotification)
227                                              object: (document)];
228    }
229
230    document = d;
231
232    settingValue = YES;
233    if ( ! d ) {
234        NSLog(@"Document set to null");
235    }
236    if ( appIcon ) {
237        [appIcon setIcon: [d appIcon]];
238    }
239    else {
240        NSLog(@"No appIcon image view");
241    }
242    if ( name ) {
243        [name setStringValue: d ? [d name] : emptyString];
244    }
245    else {
246        NSLog(@"No name text field");
247    }
248    if ( version ) {
249        [version setStringValue: d ? [d version] : emptyString];
250    }
251    else {
252        NSLog(@"No version text field");
253    }
254    if ( fullVersion ) {
255        [fullVersion setStringValue: d ? [d fullVersion] : emptyString];
256    }
257    else {
258        NSLog(@"No fullVersion text field");
259    }
260    if ( description ) {
261        [description setStringValue: d ? [d description] : emptyString];
262    }
263    else {
264        NSLog(@"No description text field");
265    }
266    if ( url ) {
267        [url setStringValue: d ? [d url] : emptyString];
268    }
269    else {
270        NSLog(@"No url text field");
271    }
272    if ( authors ) {
273        [authors setStringValue: d ? [d authors] : emptyString];
274    }
275    else {
276        NSLog(@"No authors text field");
277    }
278    [self setCurrentScriptId: currentScriptId];
279    settingValue = NO;
280
281    if ( document ) {
282        [[NSNotificationCenter defaultCenter] addObserver: self
283                                              selector: @selector(wrapperDocumentChangedNotification:)
284                                              name: (WrapperChangedNotification)
285                                              object: (document)];
286    }
287}
288
289- (void)setAppIcon: (IconView *)i
290{
291    if ( appIcon) {
292        [[NSNotificationCenter defaultCenter] removeObserver: self
293                                              name: (IconViewDidChangeIconNotification)
294                                              object: (self)];
295    }
296    appIcon = i;
297    if ( appIcon ) {
298        [[NSNotificationCenter defaultCenter] addObserver: self
299                                              selector: @selector(iconViewDidChangeIcon:)
300                                              name: (IconViewDidChangeIconNotification)
301                                              object: (appIcon)];
302    }
303}
304- (IconView *)appIcon
305{
306    return appIcon;
307}
308
309- (void)setName: (NSTextField *)n
310{
311    name = n;
312}
313- (NSTextField *)name
314{
315    return name;
316}
317
318- (void)setVersion: (NSTextField *)v
319{
320    version = v;
321}
322- (NSTextField *)version
323{
324    return version;
325}
326
327- (void)setFullVersion: (NSTextField *)v
328{
329    fullVersion = v;
330}
331- (NSTextField *)fullVersion
332{
333    return fullVersion;
334}
335
336- (void)setDescription: (NSTextField *)d
337{
338    description = d;
339}
340- (NSTextField *)description
341{
342    return description;
343}
344
345- (void)setUrl: (NSTextField *)u
346{
347    url = u;
348}
349- (NSTextField *)url
350{
351    return url;
352}
353
354- (void)setAuthors: (NSTextField *)a
355{
356    authors = a;
357}
358- (NSTextField *)authors
359{
360    return authors;
361}
362
363- (NSPopUpButton *)rolePopUp
364{
365    return rolePopUp;
366}
367- (void)setRolePopUp: (NSPopUpButton *)role
368{
369    ASSIGN(rolePopUp, role);
370}
371
372- (NSPopUpButton *)currentScriptActionPopUp
373{
374    return currentScriptActionPopUp;
375}
376- (void)setCurrentScriptActionPopUp: (NSPopUpButton *)actionPopUp;
377{
378    ASSIGN(currentScriptActionPopUp, actionPopUp);
379}
380
381- (void)setCurrentScriptId: (int)i
382{
383    NSString *shell;
384    NSString *script;
385    int action;
386    switch ( i ) {
387    case StartScript:
388        shell = [document startScriptShell];
389        script = [document startScript];
390        action = [document startScriptAction];
391        break;
392    case StartOpenScript:
393        shell = [document startOpenScriptShell];
394        script = [document startOpenScript];
395        action = [document startOpenScriptAction];
396        break;
397    case OpenScript:
398        shell = [document openScriptShell];
399        script = [document openScript];
400        action = [document openScriptAction];
401        break;
402    default:
403        NSLog(@"Unknown script ID: %d", currentScriptId);
404        return;
405    }
406    [currentScriptActionPopUp selectItemAtIndex: [currentScriptActionPopUp indexOfItemWithTag: action]];
407    currentScriptId = i;
408    if ( !script ) {
409        script = @"";
410    }
411    if ( !shell ) {
412        shell = @"/bin/sh";
413    }
414    if ( currentScriptShell ) {
415        [currentScriptShell setStringValue: shell];
416    }
417    if ( currentScript ) {
418        [currentScript setString: script];
419    }
420}
421- (int)currentScriptId
422{
423    return currentScriptId;
424}
425
426- (void)setCurrentScriptShell: (NSTextField *)s
427{
428    currentScriptShell = s;
429}
430- (NSTextField *)currentScriptShell
431{
432    return currentScriptShell;
433}
434
435- (void)setCurrentScript: (NSTextView *)s
436{
437    currentScript = s;
438}
439- (NSTextView *)currentScript
440{
441    return currentScript;
442}
443
444@end
445
446
447
448@implementation WrapperDocumentController (Notifications)
449
450- (void)wrapperDocumentChangedNotification: (NSNotification *)not
451{
452    if ( settingValue ) {
453        return;
454    }
455    settingValue = YES;
456    NSString *attr = [[not userInfo] objectForKey: WrapperChangedAttributeName];
457    id val = [[not userInfo] objectForKey: WrapperChangedAttributeValue];
458    if ( [attr isEqualToString: @"appIcon"] && appIcon ) {
459        [appIcon setIcon: [Icon iconWithImage: val]];
460    }
461    else if ( [attr isEqualToString: @"name"] && name ) {
462        [name setStringValue: val];
463    }
464    else if ( [attr isEqualToString: @"version"] && version ) {
465        [version setStringValue: val];
466    }
467    else if ( [attr isEqualToString: @"fullVersion"] && fullVersion ) {
468        [fullVersion setStringValue: val];
469    }
470    else if ( [attr isEqualToString: @"description"] && description ) {
471        [description setStringValue: val];
472    }
473    else if ( [attr isEqualToString: @"url"] && url ) {
474        [url setStringValue: val];
475    }
476    else if ( [attr isEqualToString: @"authors"] && authors ) {
477        [authors setStringValue: val];
478    }
479    else if ( [attr isEqualToString: @"role"] && rolePopUp ) {
480        [rolePopUp selectItemAtIndex: [rolePopUp indexOfItemWithTag: [val intValue]]];
481    }
482    else if ( [attr isEqualToString: @"startScript"] && currentScript) {
483        if ( currentScriptId == StartScript ) {
484            [currentScript setString: val];
485        }
486    }
487    else if ( [attr isEqualToString: @"startScriptShell"] && currentScriptShell ) {
488        if ( currentScriptId == StartScript ) {
489            [currentScriptShell setStringValue: val];
490        }
491    }
492    else if ( [attr isEqualToString: @"startScriptAction"] && currentScriptActionPopUp ) {
493        if ( currentScriptId == StartScript ) {
494            [currentScriptActionPopUp selectItemAtIndex: [currentScriptActionPopUp indexOfItemWithTag: [val intValue]]];
495        }
496    }
497    else if ( [attr isEqualToString: @"startOpenScript"] && currentScriptActionPopUp ) {
498        if ( currentScriptId == StartOpenScript ) {
499            [currentScriptActionPopUp selectItemAtIndex: [currentScriptActionPopUp indexOfItemWithTag: [val intValue]]];
500        }
501    }
502    else if ( [attr isEqualToString: @"startOpenScriptShell"] && currentScriptShell ) {
503        if ( currentScriptId == StartOpenScript ) {
504            [currentScriptShell setStringValue: val];
505        }
506    }
507    else if ( [attr isEqualToString: @"startOpenScriptAction"] && currentScriptActionPopUp ) {
508        if ( currentScriptId == StartOpenScript ) {
509            [currentScriptActionPopUp selectItemAtIndex: [currentScriptActionPopUp indexOfItemWithTag: [val intValue]]];
510        }
511    }
512    else if ( [attr isEqualToString: @"openScript"] && currentScript ) {
513        if ( currentScriptId == OpenScript ) {
514            [currentScript setString: val];
515        }
516    }
517    else if ( [attr isEqualToString: @"openScriptAction"] && currentScriptActionPopUp ) {
518        if ( currentScriptId == OpenScript ) {
519            [currentScriptActionPopUp selectItemAtIndex: [currentScriptActionPopUp indexOfItemWithTag: [val intValue]]];
520        }
521    }
522    else if ( [attr isEqualToString: @"openScriptShell"] && currentScriptShell ) {
523        if ( currentScriptId == OpenScript ) {
524            [currentScriptShell setStringValue: val];
525        }
526    }
527    else {
528        NSLog(@"Received WrapperChangedNotification for unknown attribute %@", attr);
529    }
530    settingValue = NO;
531}
532
533@end
534