File: nsbrowser.m

package info (click to toggle)
gnustep-examples 1%3A1.4.0%2Bgit20210703-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,164 kB
  • sloc: objc: 17,202; makefile: 56
file content (393 lines) | stat: -rw-r--r-- 10,228 bytes parent folder | download | duplicates (9)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
   nsbrowser.m
   
   Copyright (C) 1996 Free Software Foundation, Inc.
   
   Author: Scott Christley <scottc@net-community.com>
   Date: October 1997
   
   Author: Franck Wolff <wolff@cybercable.fr>
   Date: November 1999
   
   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.
*/

#include <Foundation/NSAutoreleasePool.h>
#include <AppKit/AppKit.h>


#if 0

//
// a NSBrowser delegate which passively creates the rows
//
@interface PassiveBrowserDelegate : NSObject
{
}

- (int)browser:(NSBrowser *)sender numberOfRowsInColumn:(int)column;
- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell
	  atRow:(int)row
	 column:(int)column;
- (NSString *)browser:(NSBrowser *)sender
	titleOfColumn:(int)column;

@end

@implementation PassiveBrowserDelegate

- (int)browser:(NSBrowser *)sender numberOfRowsInColumn:(int)column
{
  NSFileManager *fm = [NSFileManager defaultManager];
  NSArray *files = [fm directoryContentsAtPath: @"/"];

  return [files count];
}

- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell
	  atRow:(int)row
	 column:(int)column
{
  NSFileManager *fm = [NSFileManager defaultManager];
  NSArray *files = [fm directoryContentsAtPath: @"/"];
  int count = [files count];
  BOOL exists = NO, is_dir = NO;
  NSMutableString *s = [[[NSMutableString alloc] initWithCString: "/"]
			 autorelease];

  if (row >= count)
    return;

  [s appendString: [files objectAtIndex: row]];
  exists = [fm fileExistsAtPath: s isDirectory: &is_dir];

  if ((exists) && (is_dir))
    [cell setLeaf: NO];
  else
    [cell setLeaf: YES];

  [cell setStringValue: [files objectAtIndex: row]];
}

- (NSString *)browser:(NSBrowser *)sender
	titleOfColumn:(int)column
{
  if (column == 0)
    return @"Column 0";
  else if (column == 1)
    return @"Column 1";
  else if (column == 2)
    return @"Column 2";
  else
    return @"";
}

@end

#endif

//
// a NSWindow delegate which resize the browser
//
@interface WindowDelegate : NSObject
{
  NSWindow *_w;
  NSBrowser *_b;
}
- (void)initMembersWith: (NSWindow *)w and: (NSBrowser *)b;
- (void)windowDidResize:(NSNotification *)aNotification;
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)proposedFrameSize;
@end

@implementation WindowDelegate
- (void)initMembersWith: (NSWindow *)w and: (NSBrowser *)b
{
  _w = w;
  _b = b;
}
- (void)windowDidResize:(NSNotification *)aNotification
{
  NSRect rect = [_w frame];

  rect.origin.x = rect.origin.y = 10;
  rect.size.width -= 20;
  rect.size.height -= 150;

  [_b setFrame: rect];
  NSLog(@"----------------sizeToFit");
  [_b sizeToFit];
  NSLog(@"----------------Done sizeToFit");
  [[_w contentView] setNeedsDisplay: YES];
}
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)proposedFrameSize
{
  // Doesn't work. Why ????
  printf("windowWillResize: %s\n", [NSStringFromSize(proposedFrameSize) cString]);
  return proposedFrameSize;
}
@end

//
// an NSBrowser delegate which actively creates the rows
//
@interface ActiveBrowserDelegate : NSObject
{
}

- (void)browser:(NSBrowser *)sender createRowsForColumn:(int)column
      inMatrix:(NSMatrix *)matrix;
- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell
	  atRow:(int)row
	 column:(int)column;

@end

@implementation ActiveBrowserDelegate

- (void)browser:(NSBrowser *)sender createRowsForColumn:(int)column
      					inMatrix:(NSMatrix *)matrix
{
  NSFileManager *fm = [NSFileManager defaultManager];
  NSString *ptc = [sender pathToColumn: column];
  NSArray *files = [fm directoryContentsAtPath: ptc];
  int i, count = [files count];

  if (count == 0)
    return;

  [matrix addColumn];

  for (i = 0; i < count; ++i)
    {
      id cell;
      BOOL exists = NO, is_dir = NO;
      NSMutableString *s = [[[NSMutableString alloc] initWithString: ptc]
			     autorelease];

      // First row is created when column is added
      if (i != 0)
		[matrix insertRow: i];

      cell = [matrix cellAtRow: i column: 0];

      [cell setStringValue: [files objectAtIndex: i]];

      [s appendString: @"/"];
      [s appendString: [files objectAtIndex: i]];
      exists = [fm fileExistsAtPath: s isDirectory: &is_dir];

      if ((exists) && (is_dir))
		[cell setLeaf: NO];
      else
		[cell setLeaf: YES];
    }
}

- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell
	                            atRow:(int)row
	                            column:(int)column;
{
}

- (BOOL)browser:(NSBrowser *)sender selectCellWithString:(NSString *)title
       											inColumn:(int)column;
{
/*
  //NSFileManager *fm = [NSFileManager defaultManager];
  NSString *ptc = [sender pathToColumn: column];
  NSMutableString *s = [[[NSMutableString alloc] initWithString:ptc]autorelease];

  fprintf(stderr, " browser:sender selectCellWithString: %s ", [title cString]);

  if(column > 0)
    [s appendString: @"/"];
  [s appendString:title];

  fprintf(stderr, " source Path: %s ", [s cString]);

  fprintf(stderr, " destination Path: %s ", [d cString]);

  //if([fm movePath:s toPath:d handler:self])
*/
  return YES;
}

- (BOOL)fileManager:(NSFileManager*)fileManager
				shouldProceedAfterError:(NSDictionary*)errorDictionary
{
  return YES;
}

- (void)fileManager:(NSFileManager*)fileManager
				willProcessPath:(NSString*)path
{
}

@end

typedef enum {
  Tag_nothing,
  Tag_moreColumns,
  Tag_lessColumns,
  Tag_setTitled,
  Tag_setHasHScroller
} TMenuItemTag;

@interface browserController : NSObject
{
  NSBrowser *browser;
}
@end

@implementation browserController

- (void)menuAction:menuItem
{
  //NSLog (@"method \"menuAction\" invoked with tag: %d\n", [menuItem tag]);
  switch ([menuItem tag])
    {
      case Tag_nothing:
      	break;
      case Tag_moreColumns:
      	[browser setMaxVisibleColumns: [browser maxVisibleColumns] + 1];
      	break;
      case Tag_lessColumns:
      	[browser setMaxVisibleColumns: [browser maxVisibleColumns] - 1];
      	break;
      case Tag_setTitled:
      	[menuItem setState: [menuItem state] ? NSOffState : NSOnState];
      	[browser setTitled: ([menuItem state] == NSOnState)];
      	break;
      case Tag_setHasHScroller:
      	[menuItem setState: [menuItem state] ? NSOffState : NSOnState];
      	[browser setHasHorizontalScroller: ([menuItem state] == NSOnState)];
      	break;
      default:
      	break;
    }
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  NSWindow *win;
  ActiveBrowserDelegate * abd;
  WindowDelegate *wd;
  NSRect wf = {{100, 100}, {600, 500}};
  NSRect bf = {{10, 10}, {580, 350}};
  unsigned int style = NSTitledWindowMask | NSClosableWindowMask
		      | NSMiniaturizableWindowMask | NSResizableWindowMask;

  //NSLog(@"Starting the application\n");
  win = [[NSWindow alloc] initWithContentRect:wf
				    styleMask:style
				      backing:NSBackingStoreRetained
					defer:NO];

  //NSLog(@"Making the active browser delegate\n");
  abd = [ActiveBrowserDelegate new];

  //NSLog(@"Making the browser\n");
  browser = [[NSBrowser alloc] initWithFrame: bf];
  [browser setDelegate: abd];
  [browser setMaxVisibleColumns: 3];
  [browser setAllowsMultipleSelection:NO];

  //NSLog(@"Making the window delegate\n");
  wd = [WindowDelegate new];
  [wd initMembersWith: win and: browser];

  //NSLog(@"Setting the window subviews\n");
  
  [win setDelegate: wd];
  [[win contentView] addSubview: browser];

  //NSLog(@"Making the application menu\n");
  {
    NSMenu *menu, *columns, *options;
    NSMenuItem *menuItem;
    SEL ma = @selector(menuAction:);

    //NSLog(@"Making the main menu\n");
    menu = [NSMenu new];
    {
      [[menu addItemWithTitle: @"Columns" action: ma keyEquivalent: @""]
	setTag: Tag_nothing];
      [[menu addItemWithTitle: @"Options" action: ma keyEquivalent: @""]
	setTag: Tag_nothing];
      [[menu addItemWithTitle: @"Quit" action: @selector(terminate:)
                                       keyEquivalent: @"q"]
	setTag: Tag_nothing];
    }

    //NSLog(@"Making the sub menu \"Columns\"\n");
    columns = [NSMenu new];
    [menu setSubmenu: columns forItem: [menu itemWithTitle: @"Columns"]];
    {
      [[columns addItemWithTitle: @"More" action: ma keyEquivalent: @"+"]
	setTag: Tag_moreColumns];
      [[columns addItemWithTitle: @"Less" action: ma keyEquivalent: @"-"]
	setTag: Tag_lessColumns];
    }
    
    //NSLog(@"Making the sub menu \"Options\"\n");
    options = [NSMenu new];
    [menu setSubmenu: options forItem: [menu itemWithTitle:@"Options"]];
    {
      menuItem = [[NSMenuItem alloc] initWithTitle: @"Titled" action: ma
				     keyEquivalent: @""];
      [menuItem setState: NSOnState];
      [menuItem setTag: Tag_setTitled];
      [options addItem: menuItem];

      menuItem = [[NSMenuItem alloc] initWithTitle: @"Horizontal scroller"
                                     action: ma keyEquivalent: @""];
      [menuItem setState: NSOnState];
      [menuItem setTag: Tag_setHasHScroller];
      [options addItem: menuItem];
    }
    
    [menu update];

    [NSApp setMainMenu: menu];    
  }

  //NSLog(@"Displaying\n");
  [win setTitle:@"NSBrowser Test (try to resize window !)"];
  [[win contentView] display];
  [win orderFront:nil];
}

@end

int
main(int argc, char **argv, char** env)
{
  id pool = [NSAutoreleasePool new];
  NSApplication *theApp;

#if LIB_FOUNDATION_LIBRARY
  [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
#endif

  theApp = [NSApplication sharedApplication];
  [theApp setDelegate: [browserController new]];
  [theApp run];

  [pool release];

  return 0;
}