File: scrollview2.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 (251 lines) | stat: -rw-r--r-- 7,115 bytes parent folder | download | duplicates (10)
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
/*
   scrollview2.m

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author: Ovidiu Predescu <ovidiu@net-community.com>
   Date: August 1997

   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.
*/

#import <Foundation/NSAutoreleasePool.h>
#import <AppKit/AppKit.h>
#include "TestView.h"



@interface Controller : NSObject
{
  NSMatrix* matrix;
  NSScrollView* scrollView;
}

@end

@implementation Controller

- (void)setButtonTitles
{
int i, j, index = 0;
int numRows = [matrix numberOfRows];
int numCols = [matrix numberOfColumns];
id cell;

  for (i = 0; i < numRows; i++)
    for (j = 0; j < numCols; j++) {
      cell = [matrix cellAtRow: i column: j];
      [cell setTag: index];
      [cell setTitle: [NSString stringWithFormat: @"button %d, %d (%d)", i, j, index]];
      [cell setTarget: self];
      [cell setAction: @selector(handleCellAction: )];
      index++;
    }
}

- (void) handleCellAction: sender
{
  NSLog (@"handleCellAction: sender = %@", [[sender selectedCell] title]);
}

- (void) handleDoubleAction: sender
{
  NSLog (@"handleDoubleAction");
}

- (void) addRow: sender
{
  [matrix addRow];
  [self setButtonTitles];
  [matrix sizeToCells];
  [scrollView setNeedsDisplay: YES];
}

- (void) addColumn: sender
{
  [matrix addColumn];
  [self setButtonTitles];
  [matrix sizeToCells];
  [scrollView setNeedsDisplay: YES];
}

- (void) removeRow: sender
{
  if ([matrix selectedRow] >= 0)
    {
      [scrollView setNeedsDisplay: YES];
      [matrix removeRow: [matrix selectedRow]];
      [self setButtonTitles];
      [matrix sizeToCells];
    }
}

- (void) removeColumn: sender
{
  if ([matrix selectedColumn] >= 0)
    {
      [matrix removeColumn: [matrix selectedColumn]];
      [scrollView setNeedsDisplay: YES];
      [self setButtonTitles];
      [matrix sizeToCells];
    }

}

- (void) setMatrixMode: sender
{
  NSLog (@"setMatrixMode: %d", [[sender selectedCell] tag]);
  [matrix setMode: [[sender selectedCell] tag]];
}

- (void) setMatrix: (NSMatrix*)aMatrix
{
  [aMatrix retain];
  [matrix release];
  matrix = aMatrix;
  [matrix setDoubleAction: @selector(handleDoubleAction:)];
  [matrix setTarget: self];
}

- (void) applicationDidFinishLaunching: (NSNotification *)aNotification
{
  NSWindow* window;
  Controller* controller = self;
  NSMatrix* newMatrix;
  NSMatrix* selectionMatrix;
  NSButtonCell* buttonCell;
  NSButton *addRowButton, *removeRowButton, *addColButton, *removeColButton;
  NSRect matrixRect = NSZeroRect;
  NSRect scrollViewRect = {{20, 115}, {350, 235}};
  NSRect winRect = {{100, 100}, {400, 450}};
  NSRect selectionMatrixRect = {{30, 15}, {85, 95}};
  NSRect addRowRect = {{160, 70}, {95, 24}};
  NSRect removeRowRect = {{160, 32}, {95, 24}};
  NSRect addColRect = {{272, 70}, {95, 24}};
  NSRect removeColRect = {{272, 32}, {95, 24}};

  window = [[NSWindow alloc] init];

  /* Setup the matrix */
  buttonCell = [[NSButtonCell new] autorelease];
  [buttonCell setButtonType: NSPushOnPushOffButton];
  newMatrix = [[[NSMatrix alloc] initWithFrame: matrixRect
				       mode: NSRadioModeMatrix
				  prototype: buttonCell
			       numberOfRows: 0
			    numberOfColumns: 0] autorelease];

  [controller setMatrix: newMatrix];

  scrollView = [[NSScrollView alloc] initWithFrame: scrollViewRect];
  [scrollView setHasHorizontalScroller: YES];
  [scrollView setHasVerticalScroller: YES];
  [scrollView setBorderType: NSBezelBorder];		// for NS compatibility
  [scrollView setDocumentView: newMatrix];
  [[window contentView] addSubview: scrollView];

  /* Setup the matrix for different selection types */
  buttonCell = [[NSButtonCell new] autorelease];
  [buttonCell setButtonType: NSRadioButton];
  [buttonCell setBordered: NO];
  [buttonCell setImagePosition: NSImageLeft];		// for NS compatibility

  selectionMatrix = [[[NSMatrix alloc] initWithFrame: selectionMatrixRect
						mode: NSRadioModeMatrix
					   prototype: buttonCell
					numberOfRows: 4
				     numberOfColumns: 1] autorelease];
  [selectionMatrix setTarget: controller];
  [selectionMatrix setAutosizesCells: YES];		// for NS compatibility
  [selectionMatrix setAction: @selector(setMatrixMode: )];

  buttonCell = [selectionMatrix cellAtRow: 0 column: 0];
  [buttonCell setTitle: @"Radio"];
  [buttonCell setTag: NSRadioModeMatrix];

  buttonCell = [selectionMatrix cellAtRow: 1 column: 0];
  [buttonCell setTitle: @"Highlight"];
  [buttonCell setTag: NSHighlightModeMatrix];

  buttonCell = [selectionMatrix cellAtRow: 2 column: 0];
  [buttonCell setTitle: @"List"];
  [buttonCell setTag: NSListModeMatrix];

  buttonCell = [selectionMatrix cellAtRow: 3 column: 0];
  [buttonCell setTitle: @"Track"];
  [buttonCell setTag: NSTrackModeMatrix];

  [[window contentView] addSubview: selectionMatrix];

  addRowButton = [[NSButton alloc] initWithFrame: addRowRect];
  [addRowButton setTitle: @"Add row"];
  [addRowButton setTarget: controller];
  [addRowButton setAction: @selector(addRow: )];
  [[window contentView] addSubview: addRowButton];

  removeRowButton = [[NSButton alloc] initWithFrame: removeRowRect];
  [removeRowButton setTitle: @"Remove row"];
  [removeRowButton setTarget: controller];
  [removeRowButton setAction: @selector(removeRow: )];
  [[window contentView] addSubview: removeRowButton];

  addColButton = [[NSButton alloc] initWithFrame: addColRect];
  [addColButton setTitle: @"Add column"];
  [addColButton setTarget: controller];
  [addColButton setAction: @selector(addColumn: )];
  [[window contentView] addSubview: addColButton];

  removeColButton = [[NSButton alloc] initWithFrame: removeColRect];
  [removeColButton setTitle: @"Remove column"];
  [removeColButton setTarget: controller];
  [removeColButton setAction: @selector(removeColumn: )];
  [[window contentView] addSubview: removeColButton];

  [window setFrame: winRect display: YES];
  [window 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: [Controller new]];
  {
    NSMenu	*menu = [NSMenu new];

    [menu addItemWithTitle: @"Quit"
		    action: @selector(terminate:)
	     keyEquivalent: @"q"];
    [NSApp setMainMenu: menu];
  }

  [theApp run];

  [pool release];

  return 0;
}