File: Controller.m

package info (click to toggle)
gnustep-examples 1%3A1.2.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,952 kB
  • ctags: 270
  • sloc: objc: 14,381; makefile: 65
file content (236 lines) | stat: -rw-r--r-- 8,170 bytes parent folder | download | duplicates (10)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
        Controller.m
	Copyright (c) 1995-1996, NeXT Software, Inc.
        All rights reserved.
        Author: Ali Ozer

	You may freely copy, distribute and reuse the code in this example.
	NeXT disclaims any warranty of any kind, expressed or implied,
	as to its fitness for any particular use.

   	Central controller object for Edit...

   GNUstep changes are:
   Copyright (C) 1996 Free Software Foundation, Inc.

   Author:  Felipe A. Rodriguez <far@ix.netcom.com>
   Date: November 1998

   This file is part of the GNUstep GUI X/RAW Library.
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library 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
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02111, USA.
*/

#import <AppKit/AppKit.h>
#import "Controller.h"
#import "Document.h"


@implementation Controller

- (void)method:menuCell								// temp for sake of menu
{
  	NSLog (@"method invoked from cell with title '%@'", [menuCell title]);
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSWindow *window;
NSScrollView* scrollView;
NSTextView* textView;
Document *document;
NSString *string;
NSRect scrollViewRect = {{0, 0}, {470, 400}};
NSRect winRect = {{100, 100}, {470, 400}};
id openPanel;
NSColor* backColor;
unsigned int style = NSTitledWindowMask | NSClosableWindowMask				
					| NSMiniaturizableWindowMask | NSResizableWindowMask;

	window = [[NSWindow alloc] initWithContentRect:winRect
								styleMask:style
								backing:NSBackingStoreRetained
								defer:NO];

        [window setRepresentedFilename: @"Edit"];
        [window setFrameUsingName: @"Edit"];
        [window setFrameAutosaveName: @"Edit"];
        [window setDocumentEdited: NO];

	document = [[Document new] initWithPath:nil encoding:UnknownStringEncoding 
										  		uniqueZone:NO];
	scrollView = [[NSScrollView alloc] initWithFrame:scrollViewRect];
	[scrollView setHasHorizontalScroller:NO];
	[scrollView setHasVerticalScroller:YES]; 
	[scrollView setAutoresizingMask: NSViewHeightSizable];

	textView = [document firstTextView];
	[textView setFrame:[[scrollView contentView] frame]];
	backColor = [NSColor colorWithCalibratedWhite:0.85 alpha:1.0]; // off white
	[textView setBackgroundColor:backColor];					
//	[textView setBackgroundColor:[NSColor whiteColor]];					
/*
	string = [NSString stringWithContentsOfFile:[[[NSBundle mainBundle] 
			  bundlePath] stringByAppendingString:@"/Resources/Readme.txt"]];
	[textView setString:string];
*/
	string = [[[NSBundle mainBundle] 
		      bundlePath] stringByAppendingString:@"/Resources/Readme.rtf"];

	[textView readRTFDFromFile:string];
	[scrollView setDocumentView:textView];
	[[window contentView] addSubview: scrollView];
	[window setDelegate: document];

	[window setTitle:@"Edit"];
	[window display];
	[window orderFront:nil];

//	openPanel = [NSOpenPanel openPanel];
//	[openPanel display];
//	[openPanel runModalForDirectory:@"/" file:@""];
}

- (BOOL)applicationShouldTerminate:(NSApplication *)app {
    NSArray *windows = [NSApp windows];
    unsigned count = [windows count];
    BOOL needsSaving = NO;

    // Determine if there are any unsaved documents...

    while (!needsSaving && count--) {
        NSWindow *window = [windows objectAtIndex:count];
        Document *document = [Document documentForWindow:window];
        if (document && [document isDocumentEdited]) needsSaving = YES;
    }

    if (needsSaving) {
        int choice = NSRunAlertPanel(NSLocalizedString(@"Quit", @"Title of alert panel which comes up when user chooses Quit and there are unsaved documents."), 
			NSLocalizedString(@"You have unsaved documents.", @"Message in the alert panel which comes up when user chooses Quit and there are unsaved documents."), 
			NSLocalizedString(@"Review Unsaved", @"Choice (on a button) given to user which allows him/her to review all unsaved documents if he/she quits the application without saving them all first."), 
			NSLocalizedString(@"Quit Anyway", @"Choice (on a button) given to user which allows him/her to quit the application even though there are unsaved documents."), 
			NSLocalizedString(@"Cancel", @"Button choice allowing user to cancel."));
        if (choice == NSAlertOtherReturn)  {		/* Cancel */
            return NO;
        } else if (choice != NSAlertAlternateReturn) {	/* Review unsaved; Quit Anyway falls through */
            count = [windows count];
            while (count--) {
                NSWindow *window = [windows objectAtIndex:count];
                Document *document = [Document documentForWindow:window];
                if (document) {
                    [window makeKeyAndOrderFront:nil];
                    if (![document canCloseDocument]) {
                        return NO;
                    }			
                }
            }
        }
    }
//    [Preferences saveDefaults];
    return YES;
}

- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
#ifdef WIN32
    /* If the document is in a .rtfd and it's name is TXT.rtf or index.rtf, open the parent dir... This is because on windows it doesn't seem trivial to double-click to open folders as documents.
    */
    NSString *parentDir = [filename stringByDeletingLastPathComponent];
    if ([[[parentDir pathExtension] lowercaseString] isEqualToString:@"rtfd"]) {
        NSString *lastPathComponent = [[filename lastPathComponent] lowercaseString];
        if ([lastPathComponent isEqualToString:@"txt.rtf"] || [lastPathComponent isEqualToString:@"index.rtf"]) {
	    filename = parentDir;
        }
    }
#endif
    return [Document openDocumentWithPath:filename encoding:UnknownStringEncoding] ? YES : NO;
}

- (BOOL)application:(NSApplication *)sender openTempFile:(NSString *)filename {	/* ??? Why? */
    return [Document openDocumentWithPath:filename encoding:UnknownStringEncoding] ? YES : NO;
}

- (BOOL)applicationOpenUntitledFile:(NSApplication *)sender {
    return [Document openUntitled];
}

- (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename {
    BOOL retval = NO;
    BOOL releaseDoc = NO;
    Document *document = [Document documentForPath:filename];
    
    if (!document) {
        document =  [[Document alloc] initWithPath:filename encoding:UnknownStringEncoding uniqueZone:NO];
        releaseDoc = YES;
    }
    if (document) {
        BOOL useUI = [NSPrintInfo defaultPrinter] ? NO : YES;

        [document printDocumentUsingPrintPanel:useUI];
        retval = YES;

        if (releaseDoc) {
            // If we created it, we get rid of it.
            [document release];
        }
    }
    return retval;
}

- (void)createNew:(id)sender {
    (void)[Document openUntitled];
}

- (void)open:(id)sender {
    (void)[Document open:sender];
}

- (void)saveAll:(id)sender {
    NSArray *windows = [[NSApplication sharedApplication] windows];
    unsigned count = [windows count];
    while (count--) {
        NSWindow *window = [windows objectAtIndex:count];
        Document *document = [Document documentForWindow:window];
        if (document) {
            if ([document isDocumentEdited]) {
                if (![document saveDocument:NO]) return;
            }
        }
    }
}

/*** Info Panel related stuff ***/

- (void)showInfoPanel:(id)sender {
    if (!infoPanel) {
        if (![NSBundle loadNibNamed:@"Info" owner:self])  {
            NSLog(@"Failed to load Info.nib");
            NSBeep();
            return;
        }
	[infoPanel center];
    }
    [infoPanel makeKeyAndOrderFront:nil];
}


@end

/*

 1/28/95 aozer	Created for Edit II.
 7/21/95 aozer	Command line file names
 
*/