File: NSForm.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (413 lines) | stat: -rw-r--r-- 11,622 bytes parent folder | download | duplicates (7)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/** <title>NSForm</title>

   <abstract>Form class, a matrix of text fields with labels</abstract>

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author: Ovidiu Predescu <ovidiu@net-community.com>
   Date: March 1997
   
   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/ 

#include "config.h"
#import <Foundation/NSNotification.h>
#import "AppKit/NSForm.h"
#import "AppKit/NSFormCell.h"

@implementation NSForm

/* Class variables */
static Class defaultCellClass = nil;

+ (void) initialize
{
  if (self == [NSForm class])
    {
      /* Set the initial version */
      [self setVersion: 1];

      /* Set the default cell class */
      defaultCellClass = [NSFormCell class];
    }
}

+ (Class) cellClass
{
  return defaultCellClass;
}

+ (void) setCellClass: (Class)classId
{
  defaultCellClass = classId;
}

- (id) initWithFrame: (NSRect)frameRect
                mode: (NSMatrixMode)aMode
           cellClass: (Class)class
        numberOfRows: (NSInteger)rowsHigh
     numberOfColumns: (NSInteger)colsWide
{
  self = [super initWithFrame: frameRect
                         mode: aMode
                    cellClass: class
                 numberOfRows: rowsHigh
              numberOfColumns: colsWide];
  if (nil == self)
    return nil;

  [self setIntercellSpacing: NSMakeSize (0, 4)];
  return self;
}

- (id) initWithFrame: (NSRect)frameRect
                mode: (NSMatrixMode)aMode
           prototype: (NSCell*)prototype
        numberOfRows: (NSInteger)rowsHigh
     numberOfColumns: (NSInteger)colsWide
{
  self = [super initWithFrame: frameRect
                         mode: aMode
                    prototype: prototype
                 numberOfRows: rowsHigh
              numberOfColumns: colsWide];
  if (nil == self)
    return nil;

  [self setIntercellSpacing: NSMakeSize (0, 4)];  
  return self;
}

/** <p>Adds a new entry with title as its title at the end of the NSForm
    and returns the NSFormCell.</p><p>See Also: -insertEntry:atIndex:
    -removeEntryAtIndex:</p>
 */
- (NSFormCell*) addEntry: (NSString*)title
{
  return [self insertEntry: title atIndex: [self numberOfRows]];
}

/** <p>Inserts a new entry with title as its title at the index index of
    the NSForm and returns the NSFormCell.</p>
    <p>See Also: -addEntry: -removeEntryAtIndex:</p>
 */
- (NSFormCell*) insertEntry: (NSString*)title
                    atIndex: (NSInteger)index
{
  NSFormCell *new_cell = [[[object_getClass(self) cellClass] alloc] initTextCell: title];

  [self insertRow: index];
  [self putCell: new_cell atRow: index column: 0];
  RELEASE (new_cell);
  
  return new_cell;
}

/** <p>Removes the entry at index index. </p>
    <p>See Also: -insertEntry:atIndex: -addEntry:</p>
 */
- (void) removeEntryAtIndex: (NSInteger)index
{
  [[NSNotificationCenter defaultCenter] 
    removeObserver: self 
    name: _NSFormCellDidChangeTitleWidthNotification
    object: [self cellAtRow: index column: 0]];
  
  [self removeRow: index];
}

/* Overriding this method allows decoding stuff to be inherited
   simpler by NSForm */
- (void) putCell: (NSCell*)newCell  atRow: (NSInteger)row  column: (NSInteger)column 
{
  if (column > 0)
    {
      NSLog (@"Warning: NSForm: tried to add a cell in a column > 0");
      return;
    }
  [super putCell: newCell  atRow: row  column: column];
  
  [self setValidateSize: YES];
  
  [[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(_setTitleWidthNeedsUpdate:)
    name: _NSFormCellDidChangeTitleWidthNotification
    object: newCell];
}

- (void) dealloc
{
  [[NSNotificationCenter defaultCenter] 
    removeObserver: self 
    name: _NSFormCellDidChangeTitleWidthNotification
    object: nil];  

  [super dealloc];
}

/** <p>Sets whether then NSForm's entries have bezeled border.</p>
    <p>See Also: [NSCell-setBezeled:]</p>
 */
- (void) setBezeled: (BOOL)flag
{
  NSInteger i, count = [self numberOfRows];

  /* Set the bezeled attribute to the cell prototype */
  [[self prototype] setBezeled: flag];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setBezeled: flag];
}

/** <p>Sets whether then NSForm's entries have border</p>
    <p>See Also: [NSCell-setBordered:]</p>
 */
- (void) setBordered: (BOOL)flag
{
  NSInteger i, count = [self numberOfRows];

  /* Set the bordered attribute to the cell prototype */
  [[self prototype] setBordered: flag];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setBordered: flag];
}

/**<p>Sets the width of all entries to width</p> 
   <p>See Also: [NSMatrix-setCellSize:]</p>
 */
- (void) setEntryWidth: (float)width
{
  NSSize size = [self cellSize];

  size.width = width;
  [self setCellSize: size];
}

/**<p>Sets the size of the frame to aSize</p> 
   <p>See Also: [NSView-setFrameSize:]</p>
 */
- (void) setFrameSize: (NSSize)aSize
{
  [super setFrameSize: aSize];
  // Set the width of the entries independent of autosizesCells
  _cellSize.width = _bounds.size.width;
}

/** <p>Sets the spacing between all entries to spacing. By default
    the spacing is 4.</p><p>See Also: [NSMatrix-setIntercellSpacing:]</p>
 */
- (void) setInterlineSpacing: (CGFloat)spacing
{
  [self setIntercellSpacing: NSMakeSize(0, spacing)];
}

/* For the title attributes we use the corresponding attributes from the cell.
   For the text attributes we use instead the attributes inherited from the
   NSCell class. */

/** <p>Sets the text alignment of the title to aMode for all entries.
    See <ref type="type" id="NSTextAlignment">NSTextAlignment</ref> for more
    informations. The default title alignment is NSLeftTextAlignment</p> 
    <p>See Also:  [NSFormCell-setTitleAlignment:] -setTextAlignment: </p>
 */
- (void) setTitleAlignment: (NSTextAlignment)aMode
{
  NSInteger i, count = [self numberOfRows];

  /* Set the title alignment attribute to the cell prototype */
  [[self prototype] setTitleAlignment: aMode];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setTitleAlignment: aMode];
}

/** <p>Sets the text alignment to aMode for all entries. See
    <ref type="type" id="NSTextAlignment">NSTextAlignment</ref> for more
    informations. The default text alignment is NSRightTextAlignment</p> 
    <p>See Also: -setTitleAlignment: [NSCell-setAlignment:]</p>
 */
- (void) setTextAlignment: (NSTextAlignment)aMode
{
  NSInteger i, count = [self numberOfRows];

  /* Set the text alignment attribute to the cell prototype */
  [[self prototype] setAlignment: aMode];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setAlignment: aMode];
}

/** <p>Sets the text font of the title to fontObject for all entries</p>
    <p>See Also: [NSFormCell-setTitleFont:] -setTextFont:</p>
 */
- (void) setTitleFont: (NSFont*)fontObject
{
  NSInteger i, count = [self numberOfRows];

  /* Set the title font attribute to the cell prototype */
  [[self prototype] setTitleFont: fontObject];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setTitleFont: fontObject];
}

/** <p>Sets the text font to fontObject for all entries</p>
    <p>See Also: [NSCell-setFont:] -setTitleFont:</p>
 */
- (void) setTextFont: (NSFont*)fontObject
{
  NSInteger i, count = [self numberOfRows];

  /* Set the text font attribute to the cell prototype */
  [[self prototype] setFont: fontObject];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setFont: fontObject];
}

/** <p>Sets the title writing direction to direction for all entries</p>
    <p>See Also: [NSFormCell-setTitleBaseWritingDirection:] -setTextBaseWritingDirection:</p>
 */
- (void) setTitleBaseWritingDirection: (NSWritingDirection)direction
{
  NSInteger i, count = [self numberOfRows];

  /* Set the writing direction attribute to the cell prototype */
  [[self prototype] setTitleBaseWritingDirection: direction];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setTitleBaseWritingDirection: direction];
}

/** <p>Sets the contents writing direction to direction for all entries</p>
    <p>See Also: [NSCell-setBaseWritingDirection:] -setTitleBaseWritingDirection:</p>
 */
- (void) setTextBaseWritingDirection: (NSWritingDirection)direction
{
  NSInteger i, count = [self numberOfRows];

  /* Set the writing direction attribute to the cell prototype */
  [[self prototype] setBaseWritingDirection: direction];

  for (i = 0; i < count; i++)
    [[self cellAtRow: i column: 0] setBaseWritingDirection: direction];
}

/**<p>Returns the index of the entry specified by aTag or -1 if aTag is not 
   found in entries.</p><p>See Also: [NSMatrix-cellAtRow:column:]</p>
 */
- (NSInteger) indexOfCellWithTag: (NSInteger)aTag
{
  NSInteger i, count = [self numberOfRows];

  for (i = 0; i < count; i++)
    if ([[self cellAtRow: i column: 0] tag] == aTag)
      return i;
  return -1;
}

/**<p>Returns the index of the current selected entry.</p>
   <p>[NSMatrix-selectedRow]</p>
 */
- (NSInteger) indexOfSelectedItem
{
  return [self selectedRow];
}

/**<p>Returns the NSFormCell at index <var>index</var></p>
   <p>See Also: [NSMatrix-cellAtRow:column:]</p>
 */
- (id) cellAtIndex: (NSInteger)index
{
  return [self cellAtRow: index column: 0];
}

-(void) _setTitleWidthNeedsUpdate: (NSNotification*)notification
{
  [self setValidateSize: YES];
}

- (void) setValidateSize: (BOOL)flag
{
  _title_width_needs_update = flag;
  // TODO: Think about reducing redisplaying
  if (flag)
    [self setNeedsDisplay];
}

- (void) calcSize
{
  NSInteger i, count = [self numberOfRows];
  CGFloat new_title_width = 0;
  CGFloat candidate_title_width = 0;
  NSRect rect;

  // Compute max of title width in the cells
  for (i = 0; i < count; i++)
    {
      candidate_title_width = [_cells[i][0] titleWidth];
      if (candidate_title_width > new_title_width)  
        new_title_width = candidate_title_width;
    }

  // Suggest this max as title width to all cells 
  rect = NSMakeRect (0, 0, new_title_width, 0);
  for (i = 0; i < count; i++)
    {
      [_cells[i][0] calcDrawInfo: rect];
    }
  _title_width_needs_update = NO;
}

- (void) drawRect: (NSRect)rect
{
  if (_title_width_needs_update)
    [self calcSize];

  [super drawRect: rect];
}

/** <p>Draws the NSFormCell at the specified index</p>
    <p>See Also: -cellAtIndex: [NSCell-drawWithFrame:inView:]
    [NSMatrix-cellFrameAtRow:column:]</p>
 */
- (void) drawCellAtIndex: (NSInteger)index
{
  id theCell = [self cellAtIndex: index];

  [theCell drawWithFrame: [self cellFrameAtRow: index column: 0]
                  inView: self];
}

- (void) drawCellAtRow: (NSInteger)row column: (NSInteger)column
{
  [self drawCellAtIndex: row];
}

/** <p>Selects the text in the entry specified by index.</p>
    <p>[NSMatrix-selectTextAtRow:column:]</p>
 */
- (void) selectTextAtIndex: (NSInteger)index
{
  [self selectTextAtRow: index column: 0];
}

@end