File: NoteWindowController.m

package info (click to toggle)
affiche 0.6.0-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 620 kB
  • sloc: objc: 2,402; makefile: 21
file content (257 lines) | stat: -rw-r--r-- 5,530 bytes parent folder | download | duplicates (6)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
**  NoteWindowController.m
**
**  Copyright (c) 2001
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**
**  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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#import "NoteWindowController.h"

#import "Affiche.h"
#import "Constants.h"
#import "Note.h"
#import "NoteInfoWindowController.h"
#import "NoteView.h"
#import "NoteWindow.h"

static id lastNoteWindowOnTop = nil;

@implementation NoteWindowController

- (id) initWithWindowNibName: (NSString *) theNibName
{
  NoteWindow *noteWindow;
  
  noteWindow = [[NoteWindow alloc] initWithContentRect:NSMakeRect(10,10,DEFAULT_NOTE_WIDTH,DEFAULT_NOTE_HEIGHT)
                                   //styleMask: NSTitledWindowMask
				   styleMask: NSBorderlessWindowMask
                                   backing: NSBackingStoreBuffered
                                   defer: NO];

  self = [super initWithWindow: noteWindow];
  
  [noteWindow layoutWindow];
  [noteWindow setDelegate: self];

  // We link our outlets
  textView = [noteWindow textView];
  
  // We set the title to an empty string
  [[self window] setTitle: @""];
  
  RELEASE(noteWindow);

  return self;
}

- (void) dealloc
{
  //NSLog(@"NoteWindowController: -dealloc");

  lastNoteWindowOnTop = nil;
  
  RELEASE(note);
  
  [super dealloc];
}

- (void) updateWindowTitle
{
  if ( [[self note] title] == NO_TITLE )
    {
      [[self window] setTitle: @""];
    }
  else if ( [[self note] title] == FIRST_LINE_OF_NOTE )
    {
      NSArray *allLines;
      
      allLines = [[textView string] componentsSeparatedByString: @"\n"];
      
      if ( [allLines count] > 0 )
	{
	  [[self window] setTitle: [allLines objectAtIndex: 0]];
	}
    }
  else
    {
      [[self window] setTitle: [[self note] titleValue]];
    }
}


//
// delegate methods
//

- (void) textDidChange: (NSNotification *) aNotification
{
  NSAttributedString *anAttributedString;
  
  anAttributedString = [[NSAttributedString alloc] initWithAttributedString: [textView textStorage]];
  [[self note] setValue: anAttributedString];

  [self updateWindowTitle];
  [[self noteView] setNeedsDisplay: YES];

  // We update the modification date of our note
  [[self note] setModificationDate: [NSDate date]];

  RELEASE(anAttributedString);
}

- (void) windowDidBecomeKey: (NSNotification *) aNotification
{
  lastNoteWindowOnTop = [self window];
  [[NoteInfoWindowController singleInstance] setNote: [self note]];
}

- (void) windowDidLoad
{
  lastNoteWindowOnTop = [self window];
  
  [super windowDidLoad];
}


- (void) windowDidMove: (NSNotification *) aNotification
{
  [self _updateWindowFrame];
}


- (void) windowDidResize: (NSNotification *) aNotification
{
  [self _updateWindowFrame];
}

- (BOOL) windowShouldClose: (id) sender
{
  int choice;

  choice = NSRunAlertPanel(_(@"Closing a note..."),
			   _(@"Would you like to save this note (%@...)\nto a file?"),
			   _(@"Cancel"), // default
			   _(@"Yes"),    // alternate
			   _(@"No"),     // other return
			   [[self window] title]);    
  
  if (choice == NSAlertDefaultReturn)
    {
      return NO;
    }
  else if ( choice == NSAlertAlternateReturn )
    {
      NSSavePanel *aSavePanel;
      int aChoice;
      
      aSavePanel = [NSSavePanel savePanel];
      [aSavePanel setAccessoryView: nil];
      [aSavePanel setRequiredFileType: @""];
      
      aChoice = [aSavePanel runModalForDirectory:NSHomeDirectory() file: @"note.txt"];
      
      /* if successful, save file under designated name */
      if (aChoice == NSOKButton)
	{
	  if (! [[textView string] writeToFile: [aSavePanel filename]
				   atomically: YES] )
	    {
	      NSBeep();
	    }
	}
    }
  
  [(Affiche *)[NSApp delegate] deleteNote: [self note]];
  [(Affiche *)[NSApp delegate] synchronize];

  return YES;
}

- (void) windowWillClose: (NSNotification *) theNotification
{
  AUTORELEASE(self);
}


//
// access / mutation
//

+ (id) lastNoteWindowOnTop
{
  return lastNoteWindowOnTop;
}

- (Note *) note
{
  return note;
}

- (void) setNote: (Note *) theNote
{
  if ( theNote )
    {
      RETAIN(theNote);
      RELEASE(note);
      note = theNote;
      
      [[textView textStorage] setAttributedString: [note value]];
      [textView setBackgroundColor: [note backgroundColor]];
      [[self noteView] setColorCode: [note colorCode]];
      [[self window] setFrame: [note frame]
		     display: NO];

      [self updateWindowTitle];
      [textView setNeedsDisplay: YES];
      [[self noteView] setNeedsDisplay: YES];
    }
  else
    {
      RELEASE(note);
      note = nil;
    }
}

- (NSTextView *) textView
{
  return textView;
}

- (NoteView *) noteView
{
  return [(NoteWindow *)[self window] noteView];
}

@end


//
// private methods
//
@implementation NoteWindowController (Private)

- (void) _updateWindowFrame
{
  if ( [self note] )
    {
      [[self note] setFrame: [[self window] frame]];
    }
}


@end