File: GormFunctions.m

package info (click to toggle)
gorm 0.9.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,468 kB
  • ctags: 268
  • sloc: objc: 30,080; makefile: 68
file content (341 lines) | stat: -rw-r--r-- 8,941 bytes parent folder | download
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* GormFunctions.m
 *
 * Copyright (C) 2004 Free Software Foundation, Inc.
 *
 * Author:	Gregory John Casamento <greg_casamento@yahoo.com>
 * Date:	2004
 * 
 * This file is part of GNUstep.
 * 
 * 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.
 */

#include "GormFunctions.h"
#include "GormViewEditor.h"
#include <AppKit/AppKit.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSUserDefaults.h>
#include <Foundation/NSFileManager.h>
#include <Foundation/NSPathUtilities.h>
#include <Foundation/NSString.h>
#include "GormClassPanelController.h"

// find all subitems for the given items...
void findAllWithArray(id item, NSMutableArray *array)
{
  [array addObject: item];
  if([item isKindOfClass: [NSMenuItem class]])
    {
      if([item hasSubmenu])
	{
	  NSMenu *submenu = [item submenu];
	  NSArray *items = [submenu itemArray];
	  NSEnumerator *e = [items objectEnumerator];
	  id i = nil;

	  [array addObject: submenu];
	  while((i = [e nextObject]) != nil)
	    {
	      findAllWithArray(i, array);
	    }
	}
    } 
}

// find all sub items for the selections...
NSArray* findAllSubmenus(NSArray *array)
{
  NSEnumerator *e = [array objectEnumerator];
  id i = nil;
  NSMutableArray *results = [[NSMutableArray alloc] init];

  while((i = [e nextObject]) != nil)
    {
      findAllWithArray(i, results);
    }

  return results;
}

NSArray* findAll(NSMenu *menu)
{
  NSArray *items = [menu itemArray];
  return findAllSubmenus(items);
}

void subviewsForView(NSView *view, NSMutableArray *array)
{
  if(view != nil)
    {
      NSArray *subviews = [view subviews];
      NSEnumerator *en = [subviews objectEnumerator];
      NSView *aView = nil;

      // if it's not me and it's not and editor, include it in the list of
      // things to be deleted from the document.
      if(![view isKindOfClass: [GormViewEditor class]]) 
	{
	  [array addObject: view];
	}

      while((aView = [en nextObject]) != nil)
	{
	  subviewsForView( aView, array );
	}
    }
}

NSArray *allSubviews(NSView *view)
{
  NSMutableArray *views = [NSMutableArray array];
  subviewsForView( view, views );
  return views;
}

// cut the text...  code taken from GWorkspace, by Enrico Sersale
static inline NSString *cutText(NSString *filename, id label, int lenght)
{
  NSString *cutname = nil;
  NSString *reststr = nil;
  NSString *dots;
  NSFont *labfont;
  NSDictionary *attr;
  float w, cw, dotslenght;
  int i;
  
  cw = 0;
  labfont = [label font];
  
  attr = [NSDictionary dictionaryWithObjectsAndKeys: 
			 labfont, NSFontAttributeName, nil];  
  
  dots = @"...";  
  dotslenght = [dots sizeWithAttributes: attr].width;  
  w = [filename sizeWithAttributes: attr].width;
  
  if (w > lenght) 
    {
      i = 0;
      while (cw <= (lenght - dotslenght)) 
	{
	  if (i == [filename cStringLength]) 
	    {
	      break;
	    }
	  cutname = [filename substringToIndex: i];
	  reststr = [filename substringFromIndex: i];
	  cw = [cutname sizeWithAttributes: attr].width;
	  i++;
	}	
      if ([cutname isEqual: filename] == NO) 
	{      
	  if ([reststr cStringLength] <= 3) 
	    { 
	      return filename;
	    } 
	  else 
	    {
	      cutname = [cutname stringByAppendingString: dots];
	    }
	} 
      else 
	{
	  return filename;
	}	
    } 
  else 
    {
      return filename;
    }
  
  return cutname;
}

NSString *cutFileLabelText(NSString *filename, id label, int length)
{
  if (length > 0) 
    {
      return cutText(filename, label, length);
    }
  return filename;
}

NSSize defaultCellSize()
{
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
  int width = [defaults integerForKey: @"CellSizeWidth"];
  NSSize size = NSMakeSize(width, 72);
  return size;
}

NSColor *colorFromDict(NSDictionary *dict)
{
  if(dict != nil)
    {
      return [NSColor colorWithCalibratedRed: [[dict objectForKey: @"red"] floatValue]
		      green: [[dict objectForKey: @"green"] floatValue]
		      blue: [[dict objectForKey: @"blue"] floatValue]
		      alpha: [[dict objectForKey: @"alpha"] floatValue]];
    }
  return nil;
}

NSDictionary *colorToDict(NSColor *color)
{
  if(color != nil)
    {
      NSMutableDictionary *dict = [NSMutableDictionary dictionary];
      float red, green, blue, alpha;
      NSNumber *fred = nil;
      NSNumber *fgreen = nil;
      NSNumber *fblue = nil;
      NSNumber *falpha = nil;
      
      [color getRed: &red
	     green: &green
	     blue: &blue
	     alpha: &alpha];
      
      fred   = [NSNumber numberWithFloat: red];
      fgreen = [NSNumber numberWithFloat: green];
      fblue  = [NSNumber numberWithFloat: blue];
      falpha = [NSNumber numberWithFloat: alpha];
      
      [dict setObject: fred   forKey: @"red"];
      [dict setObject: fgreen forKey: @"green"];
      [dict setObject: fblue  forKey: @"blue"];
      [dict setObject: falpha forKey: @"alpha"];
      
      return dict;
    }
  return nil;
}

NSArray *systemImagesList()
{
  NSString *system = [NSOpenStepRootDirectory() stringByAppendingPathComponent: @"System"];
  NSString *lib = [system stringByAppendingPathComponent: @"Library"];
  NSString *path = [lib stringByAppendingPathComponent: @"Images"];
  NSArray *contents = [[NSFileManager defaultManager] directoryContentsAtPath: path];
  NSEnumerator *en = [contents objectEnumerator];
  NSMutableArray *result = [NSMutableArray array];
  id obj;
  NSArray *fileTypes = [NSImage imageFileTypes];

  while((obj = [en nextObject]) != nil)
    {
      if([fileTypes containsObject: [obj pathExtension]])
	{
	  NSString *pathString = [path stringByAppendingPathComponent: obj];
	  [result addObject: pathString];
	}
    }

  return result;
}

NSArray *systemSoundsList()
{
  NSString *system = [NSOpenStepRootDirectory() stringByAppendingPathComponent: @"System"];
  NSString *lib = [system stringByAppendingPathComponent: @"Library"];
  NSString *path = [lib stringByAppendingPathComponent: @"Sounds"];
  NSArray *contents = [[NSFileManager defaultManager] directoryContentsAtPath: path];
  NSEnumerator *en = [contents objectEnumerator];
  NSMutableArray *result = [NSMutableArray array];
  id obj;
  NSArray *fileTypes = [NSSound soundUnfilteredFileTypes];

  while((obj = [en nextObject]) != nil)
    {
      if([fileTypes containsObject: [obj pathExtension]])
	{
	  NSString *pathString = [path stringByAppendingPathComponent: obj];
	  [result addObject: pathString];
	}
    }

  return result;
}

int appVersion(long a, long b, long c)
{
  return (((a) << 16)+((b) << 8) + (c));
}

NSString *promptForClassName(NSString *title, NSArray *classes)
{
  GormClassPanelController *cpc = AUTORELEASE([[GormClassPanelController alloc] initWithTitle: title classList: classes]);
  return [cpc runModal];
}

NSString *identifierString(NSString *str)
{
  NSCharacterSet  *illegal = [[NSCharacterSet 
				characterSetWithCharactersInString: 
				  @"_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"] 
			       invertedSet];
  NSCharacterSet  *numeric = [NSCharacterSet 
			       characterSetWithCharactersInString:
				 @"0123456789"];
  NSCharacterSet  *white = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  NSRange	  r;
  NSMutableString *result;
  
  if (str == nil)
    {
      return nil;
    }

  result = [NSMutableString stringWithString: str];
  r = [result rangeOfCharacterFromSet: illegal];
  while (r.length > 0)
    {
      [result deleteCharactersInRange: r];
      r = [result rangeOfCharacterFromSet: illegal];
    }
  r = [result rangeOfCharacterFromSet: numeric];
  while (r.length > 0 && r.location == 0)
    {
      [result deleteCharactersInRange: r];
      r = [result rangeOfCharacterFromSet: numeric];
    }
  r = [result rangeOfCharacterFromSet: white];
  while (r.length > 0 && r.location == 0)
    {
      [result deleteCharactersInRange: r];
      r = [result rangeOfCharacterFromSet: white];
    }

  // check the result's length.
  if([result length] == 0)
    {
      result = [NSString stringWithString: @"dummyIdentifier"];
    }

  return result;
}

NSString *formatAction(NSString *action)
{
  NSString *temp = identifierString(action);
  NSString *identifier = [temp stringByAppendingString: @":"];
  return identifier;
}

NSString *formatOutlet(NSString *outlet)
{
  NSString *identifier = identifierString(outlet);
  return identifier;
}