1/*
2   GNUstep ProjectCenter - http://www.gnustep.org/experience/ProjectCenter.html
3
4   Copyright (C) 2001 Free Software Foundation
5
6   This file is part of GNUstep.
7
8   This application is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public
10   License as published by the Free Software Foundation; either
11   version 2 of the License, or (at your option) any later version.
12
13   This application is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Library General Public License for more details.
17
18   You should have received a copy of the GNU General Public
19   License along with this library; if not, write to the Free
20   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21*/
22#import <ProjectCenter/PCLogController.h>
23#import <ProjectCenter/ProjectCenter.h>
24
25#import <Protocols/CodeEditor.h>
26
27#import "PCAppController.h"
28#import "PCMenuController.h"
29#import "PCInfoController.h"
30#import "PCPrefController.h"
31
32@implementation PCMenuController
33
34- (id)init
35{
36  if ((self = [super init]))
37    {
38      [[NSNotificationCenter defaultCenter]
39	addObserver:self
40	   selector:@selector(editorDidBecomeActive:)
41	       name:PCEditorDidBecomeActiveNotification
42	     object:nil];
43
44      [[NSNotificationCenter defaultCenter]
45	addObserver:self
46	   selector:@selector(editorDidResignActive:)
47	       name:PCEditorDidResignActiveNotification
48	     object:nil];
49
50      editorIsActive = NO;
51    }
52
53  return self;
54}
55
56- (void)dealloc
57{
58#ifdef DEVELOPMENT
59  NSLog(@"PCMenuController: dealloc");
60#endif
61  [[NSNotificationCenter defaultCenter] removeObserver:self];
62
63  [super dealloc];
64}
65
66- (void)setAppController:(id)anObject
67{
68  appController = anObject;
69}
70
71- (void)setProjectManager:(id)anObject
72{
73  projectManager = anObject;
74}
75
76//============================================================================
77//==== Menu stuff
78//============================================================================
79
80// Info
81- (void)showPrefWindow:(id)sender
82{
83  [[appController prefController] showPanel:sender];
84}
85
86- (void)showInfoPanel:(id)sender
87{
88  [[appController infoController] showInfoWindow:sender];
89}
90
91- (void)showEditorPanel:(id)sender
92{
93  [[[projectManager rootActiveProject] projectWindow] showProjectEditor:self];
94}
95
96- (void)showLogPanel:(id)sender
97{
98  [[appController logController] showPanel];
99}
100
101// Project
102- (void)projectOpen:(id)sender
103{
104  [projectManager openProject];
105}
106
107- (void)projectNew:(id)sender
108{
109  [projectManager newProject: sender];
110}
111
112- (void)projectSave:(id)sender
113{
114  [projectManager saveProject];
115}
116
117- (void)projectAddFiles:(id)sender
118{
119  [projectManager addProjectFiles];
120}
121
122- (void)projectSaveFiles:(id)sender
123{
124  [projectManager saveProjectFiles];
125}
126
127- (void)projectRemoveFiles:(id)sender
128{
129  [projectManager removeProjectFiles];
130}
131
132- (void)projectClose:(id)sender
133{
134  [projectManager closeProject];
135}
136
137// Subproject
138- (void)subprojectNew:(id)sender
139{
140  [projectManager openNewSubprojectPanel];
141}
142
143- (void)subprojectAdd:(id)sender
144{
145  [projectManager addSubproject];
146}
147
148// File
149- (void)fileOpen:(id)sender
150{
151  [projectManager openFile];
152}
153
154- (void)fileNew:(id)sender
155{
156  [projectManager newFile];
157}
158
159- (void)fileSave:(id)sender
160{
161  [projectManager saveFile];
162}
163
164- (void)fileSaveAs:(id)sender
165{
166  [projectManager saveFileAs];
167}
168
169- (void)fileSaveTo:(id)sender
170{
171  [projectManager saveFileTo];
172}
173
174- (void)fileRevertToSaved:(id)sender
175{
176  [projectManager revertFileToSaved];
177}
178
179- (void)fileClose:(id)sender
180{
181  [projectManager closeFile];
182}
183
184- (void)fileOpenQuickly:(id)sender
185{
186  NSRunAlertPanel(@"PCMenuController: Sorry!",
187		  @"This feature is not finished yet",
188		  @"OK",nil,nil);
189}
190
191- (void)fileRename:(id)sender
192{
193  // Show Inspector panel with "File Attributes" section
194  [projectManager renameFile];
195
196/*  NSRunAlertPanel(@"PCMenuController: Sorry!",
197		  @"This feature is not finished yet",
198		  @"OK",nil,nil);*/
199}
200
201- (void)fileNewUntitled:(id)sender
202{
203  NSRunAlertPanel(@"PCMenuController: Sorry!",
204		  @"This feature is not finished yet",
205		  @"OK",nil,nil);
206}
207
208// Tools
209- (void)toggleToolbar:(id)sender
210{
211  [[[projectManager rootActiveProject] projectWindow] toggleToolbar];
212
213  if ([[sender title] isEqualToString:@"Hide Tool Bar"])
214    {
215      [sender setTitle:@"Show Tool Bar"];
216    }
217  else
218    {
219      [sender setTitle:@"Hide Tool Bar"];
220    }
221}
222
223- (void)showInspector:(id)sender
224{
225  [projectManager showProjectInspector:self];
226}
227
228// Build Panel
229- (void)showBuildPanel:(id)sender
230{
231  [[[projectManager rootActiveProject] projectWindow] showProjectBuild:self];
232}
233
234- (void)executeBuild:(id)sender
235{
236  [self showBuildPanel:self];
237  [[[projectManager rootActiveProject] projectBuilder] performStartBuild];
238}
239
240- (void)stopBuild:(id)sender
241{
242  [[[projectManager rootActiveProject] projectBuilder] performStopBuild];
243}
244
245- (void)startClean:(id)sender
246{
247  [self showBuildPanel:self];
248  [[[projectManager rootActiveProject] projectBuilder] performStartClean];
249}
250
251// Loaded Files
252- (void)showLoadedFilesPanel:(id)sender
253{
254  [projectManager showProjectLoadedFiles:self];
255}
256
257- (void)loadedFilesSortByTime:(id)sender
258{
259  [[[projectManager rootActiveProject] projectLoadedFiles] setSortByTime];
260}
261
262- (void)loadedFilesSortByName:(id)sender
263{
264  [[[projectManager rootActiveProject] projectLoadedFiles] setSortByName];
265}
266
267- (void)loadedFilesNextFile:(id)sender
268{
269  [[[projectManager rootActiveProject] projectLoadedFiles] selectNextFile];
270}
271
272- (void)loadedFilesPreviousFile:(id)sender
273{
274  [[[projectManager rootActiveProject] projectLoadedFiles] selectPreviousFile];
275}
276
277// Launch Panel
278- (void)showLaunchPanel:(id)sender
279{
280  [[[projectManager rootActiveProject] projectWindow] showProjectLaunch:self];
281}
282
283- (void)runTarget:(id)sender
284{
285  [self showLaunchPanel:self];
286  [[[projectManager rootActiveProject] projectLauncher] performRun];
287}
288
289- (void)debugTarget:(id)sender
290{
291  [self showLaunchPanel:self];
292  [[[projectManager rootActiveProject] projectLauncher] performDebug];
293}
294
295//============================================================================
296//==== Delegate stuff
297//============================================================================
298
299- (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
300{
301  NSString         *menuTitle = [[menuItem menu] title];
302  PCProject        *aProject = [projectManager activeProject];
303  PCProjectEditor  *projectEditor = [aProject projectEditor];
304  PCProjectBrowser *projectBrowser = [aProject projectBrowser];
305
306  if ([[projectManager loadedProjects] count] == 0)
307    {
308      // Project related menu items
309      if ([menuTitle isEqualToString: @"Project"])
310	{
311	  if ([[menuItem title] isEqualToString:@"Save"]) return NO;
312	  if ([[menuItem title] isEqualToString:@"Add Files..."]) return NO;
313	  if ([[menuItem title] isEqualToString:@"Save Files..."]) return NO;
314	  if ([[menuItem title] isEqualToString:@"Remove Files..."]) return NO;
315	  if ([[menuItem title] isEqualToString:@"New Subproject..."])
316	    return NO;
317	  if ([[menuItem title] isEqualToString:@"Add Subproject..."])
318	    return NO;
319	  if ([[menuItem title] isEqualToString:@"Close"]) return NO;
320	}
321
322      // File related menu items
323      if ([menuTitle isEqualToString: @"File"] && !editorIsActive)
324	{
325	  if ([[menuItem title] isEqualToString:@"New in Project"]) return NO;
326	  if ([[menuItem title] isEqualToString:@"Save"]) return NO;
327	  if ([[menuItem title] isEqualToString:@"Save As..."]) return NO;
328	  if ([[menuItem title] isEqualToString:@"Save To..."]) return NO;
329	  if ([[menuItem title] isEqualToString:@"Revert to Saved"]) return NO;
330	  if ([[menuItem title] isEqualToString:@"Close"]) return NO;
331	  if ([[menuItem title] isEqualToString:@"Open Quickly..."]) return NO;
332	  if ([[menuItem title] isEqualToString:@"Rename"]) return NO;
333	  if ([[menuItem title] isEqualToString:@"New Untitled"]) return NO;
334	}
335
336      // Tools menu items
337      if ([menuTitle isEqualToString: @"Tools"])
338	{
339	  if ([[menuItem title] isEqualToString:@"Inspector..."]) return NO;
340	  if ([[menuItem title] isEqualToString:@"Hide Tool Bar"]) return NO;
341	  if ([[menuItem title] isEqualToString:@"Show Tool Bar"]) return NO;
342	}
343      if ([menuTitle isEqualToString: @"Project Build"])
344	{
345	  if ([[menuItem title] isEqualToString:@"Show Panel..."]) return NO;
346	  if ([[menuItem title] isEqualToString:@"Build"]) return NO;
347	  if ([[menuItem title] isEqualToString:@"Stop Build"]) return NO;
348	  if ([[menuItem title] isEqualToString:@"Clean"]) return NO;
349	  if ([[menuItem title] isEqualToString:@"Next Error"]) return NO;
350	  if ([[menuItem title] isEqualToString:@"Previous Error"]) return NO;
351	}
352      if ([menuTitle isEqualToString: @"Project Find"])
353	{
354	  if ([[menuItem title] isEqualToString:@"Show Panel..."]) return NO;
355	  if ([[menuItem title] isEqualToString:@"Find Preferences"]) return NO;
356	  if ([[menuItem title] isEqualToString:@"Find Definitions"]) return NO;
357	  if ([[menuItem title] isEqualToString:@"Find Text"]) return NO;
358	  if ([[menuItem title] isEqualToString:@"Find Regular Expr"]) return NO;
359	  if ([[menuItem title] isEqualToString:@"Next match"]) return NO;
360	  if ([[menuItem title] isEqualToString:@"Previous match"]) return NO;
361	}
362      if ([menuTitle isEqualToString: @"Loaded Files"])
363	{
364	  if ([[menuItem title] isEqualToString:@"Show Panel..."]) return NO;
365	  if ([[menuItem title] isEqualToString:@"Sort by Time Viewed"]) return NO;
366	  if ([[menuItem title] isEqualToString:@"Sort by Name"]) return NO;
367	  if ([[menuItem title] isEqualToString:@"Next File"]) return NO;
368	  if ([[menuItem title] isEqualToString:@"Previous File"]) return NO;
369	}
370      if ([menuTitle isEqualToString: @"Launcher"])
371	{
372	  if ([[menuItem title] isEqualToString:@"Show Panel..."]) return NO;
373	  if ([[menuItem title] isEqualToString:@"Run"]) return NO;
374	  if ([[menuItem title] isEqualToString:@"Debug"]) return NO;
375	}
376      if ([menuTitle isEqualToString: @"Indexer"])
377	{
378	  if ([[menuItem title] isEqualToString:@"Show Panel..."]) return NO;
379	  if ([[menuItem title] isEqualToString:@"Purge Indices"]) return NO;
380	  if ([[menuItem title] isEqualToString:@"Index Subproject"]) return NO;
381	  if ([[menuItem title] isEqualToString:@"Index File"]) return NO;
382	}
383      return YES;
384    }
385
386  // Project related menu items
387  if ([menuTitle isEqualToString: @"Project"]
388      && [projectBrowser nameOfSelectedFile] == nil
389      && [projectBrowser selectedFiles] == nil)
390    {
391      if ([[menuItem title] isEqualToString:@"Remove Files..."]) return NO;
392    }
393  if ([menuTitle isEqualToString: @"Project"]
394      && [[projectEditor allEditors] count] == 0)
395    {
396      if ([[menuItem title] isEqualToString:@"Save Files..."]) return NO;
397    }
398  if ([menuTitle isEqualToString: @"Project"]
399      && [projectBrowser nameOfSelectedCategory] == nil)
400    {
401      if ([[menuItem title] isEqualToString:@"Add Subproject..."]) return NO;
402      if ([[menuItem title] isEqualToString:@"Add Files..."]) return NO;
403    }
404  if ([menuTitle isEqualToString: @"Project"]
405      && [[projectBrowser nameOfSelectedCategory]
406         isEqualToString:@"Subprojects"])
407    {
408      if ([[menuItem title] isEqualToString:@"Add Files..."]) return NO;
409    }
410  if ([menuTitle isEqualToString: @"Project"]
411      && ![[projectBrowser nameOfSelectedRootCategory] isEqualToString:@"Subprojects"])
412    {
413      if ([[menuItem title] isEqualToString:@"Add Subproject..."]) return NO;
414    }
415
416  // File related menu items
417  if (([menuTitle isEqualToString: @"File"]))
418    {
419      if (!editorIsActive)
420	{
421	  if ([[menuItem title] isEqualToString:@"Save"]) return NO;
422	  if ([[menuItem title] isEqualToString:@"Save As..."]) return NO;
423	  if ([[menuItem title] isEqualToString:@"Save To..."]) return NO;
424	  if ([[menuItem title] isEqualToString:@"Revert to Saved"]) return NO;
425	  if ([[menuItem title] isEqualToString:@"Close"]) return NO;
426	}
427    }
428  if ([projectBrowser nameOfSelectedFile] == nil)
429    {
430      if ([[menuItem title] isEqualToString:@"Rename"]) return NO;
431    }
432
433  // Toolbar
434  if ([[menuItem title] isEqualToString:@"Hide Tool Bar"]
435      && ![[[projectManager activeProject] projectWindow] isToolbarVisible])
436    {
437      [menuItem setTitle:@"Show Tool Bar"];
438    }
439  if ([[menuItem title] isEqualToString:@"Show Tool Bar"]
440      && [[[projectManager activeProject] projectWindow] isToolbarVisible])
441    {
442      [menuItem setTitle:@"Hide Tool Bar"];
443    }
444
445  // Project Build related
446  if (([menuTitle isEqualToString: @"Project Build"]))
447    {
448      if ([[[projectManager activeProject] projectBuilder] isBuilding]
449	  || [[[projectManager activeProject] projectBuilder] isCleaning])
450	{
451	  if ([[menuItem title] isEqualToString:@"Build"]) return NO;
452	  if ([[menuItem title] isEqualToString:@"Clean"]) return NO;
453	  if ([[menuItem title] isEqualToString:@"Next error"]) return NO;
454	  if ([[menuItem title] isEqualToString:@"Previous error"]) return NO;
455	}
456      else
457	{
458	  if ([[menuItem title] isEqualToString:@"Stop Build"]) return NO;
459	}
460    }
461
462  // Project Launcher related
463  if (([menuTitle isEqualToString: @"Launcher"]))
464    {
465      if ([[[projectManager activeProject] projectLauncher] isRunning]
466	  || [[[projectManager activeProject] projectLauncher] isDebugging])
467	{
468	  if ([[menuItem title] isEqualToString:@"Run"]) return NO;
469	  if ([[menuItem title] isEqualToString:@"Debug"]) return NO;
470	}
471    }
472
473  // Loaded Files related
474  if (([menuTitle isEqualToString: @"Loaded Files"]))
475    {
476      if ([[[aProject projectLoadedFiles] editedFilesRep] count] <= 0)
477	{
478	  if ([[menuItem title] isEqualToString:@"Sort by Time Viewed"])
479	    return NO;
480	  if ([[menuItem title] isEqualToString:@"Sort by Name"]) return NO;
481	  if ([[menuItem title] isEqualToString:@"Next File"]) return NO;
482	  if ([[menuItem title] isEqualToString:@"Previous File"]) return NO;
483	}
484    }
485
486  return YES;
487}
488
489- (void)editorDidResignActive:(NSNotification *)aNotif
490{
491  editorIsActive = NO;
492}
493
494- (void)editorDidBecomeActive:(NSNotification *)aNotif
495{
496  editorIsActive = YES;
497}
498
499@end
500
501