File: nstabview.m

package info (click to toggle)
gnustep-examples 1%3A1.2.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,952 kB
  • ctags: 270
  • sloc: objc: 14,381; makefile: 65
file content (266 lines) | stat: -rw-r--r-- 7,126 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
#include <Foundation/NSAutoreleasePool.h>
#include <AppKit/AppKit.h>
#include <AppKit/NSTabView.h>
#include "TestView.h"
#include "GSImageTabViewItem.h"

@interface tabPlayground : NSView
{
}
@end

@interface appController : NSObject
{
  int tabNum;
  NSTabView *tabView;
}
- (void) addTab: (id)sender;
- (void) removeTab: (id)sender;
@end

@interface myTabViewDelegate : NSObject
{
}
- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)TabView;
@end

@implementation tabPlayground
- (void) drawRect:(NSRect)rect
{
  NSGraphicsContext     *ctxt = GSCurrentContext();

  DPSgsave(ctxt);
/*
  DPSsetlinewidth(ctxt,1);
  DPSsetgray(ctxt,0.8);
  DPSmoveto(ctxt, rect.origin.x+20, rect.origin.y+20);  
//  DPSrlineto(ctxt, 5, 17);
  DPScurveto(ctxt, rect.origin.x+20, rect.origin.y+20, 
                   rect.origin.x+20+4, rect.origin.y+20+15, 
		   rect.origin.x+20+4+5, rect.origin.y+20+15+5);

  DPSstroke(ctxt);

  DPSsetgray(ctxt,1);
  DPSmoveto(ctxt, rect.origin.x+21, rect.origin.y+20);  
//  DPSrlineto(ctxt, 5, 17);
  DPScurveto(ctxt, rect.origin.x+21, rect.origin.y+20, 
                   rect.origin.x+21+4, rect.origin.y+20+15, 
		   rect.origin.x+21+4+5, rect.origin.y+20+15+5);

  DPSstroke(ctxt);
*/

  DPSsetlinewidth(ctxt, 0.5);
  DPSsetgray(ctxt, 0.8);
  DPSmoveto(ctxt, rect.origin.x+20, rect.origin.y+20);  
  DPSrlineto(ctxt, 5, 15);
  DPSstroke(ctxt);

  DPSgrestore(ctxt);
}
@end

@implementation appController
- (BOOL)validateMenuItem:(NSMenuItem*)aMenuItem;
{
  NSString      *title = [aMenuItem title];

  if ([title isEqual: @"Remove"])
    {
      if (tabNum == 0)
        return NO;
    }

  return YES;
}

- (void) addTab: (id)sender
{
  NSView *view;
  NSTextField *label;
  NSTabViewItem *item;

  tabNum++;

  view = [[NSView alloc] initWithFrame:[tabView contentRect]];
  [view setAutoresizingMask: (NSViewWidthSizable
                               | NSViewHeightSizable)];

  label = [[NSTextField alloc] initWithFrame:[view frame]];
  [label setEditable: NO];
  [label setSelectable: NO];
  [label setBezeled: NO];
  [label setBordered: NO];
  [label setBackgroundColor: [NSColor lightGrayColor]];  
  [label setAlignment: NSCenterTextAlignment];
  [label setStringValue: [NSString stringWithFormat:@"Test View #%d", tabNum]];

  [view addSubview: label];

  item = [[NSTabViewItem alloc] initWithIdentifier: @"Urph"];
  [item setLabel: [NSString stringWithFormat:@"Test #%d", tabNum]];
  [item setView:view];
  
  [tabView addTabViewItem: item];  

  [tabView selectTabViewItem: item];
//  [tabView setNeedsDisplay: YES];
}

- (void) removeTab: (id)sender
{
  int index = [tabView numberOfTabViewItems] - 1;
  [tabView removeTabViewItem: [tabView tabViewItemAtIndex: index]];

  [tabView selectTabViewItemAtIndex: index - 1];

//  [tabView setNeedsDisplay: YES];

  tabNum--;
}

- (void) changeTabFont: (id)sender
{
  [[NSFontManager sharedFontManager] setSelectedFont:[tabView font] isMultiple:NO];
  [[NSFontManager sharedFontManager] orderFrontFontPanel:self];
}

- (void)changeFont:(id)fontManager
{
  [tabView setFont: [fontManager convertFont: [tabView font]]];
  [tabView setNeedsDisplay: YES];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  NSWindow *window;
  GSImageTabViewItem *item;
  NSRect winRect = {{100, 100}, {300, 300}};
  NSRect tabViewRect = {{10, 10}, {280, 280}};
  NSView *view;
  NSTextField *label;
  NSMenu *tabMenu;

  tabNum = 0;

  window = [[NSWindow alloc] initWithContentRect: winRect
                          styleMask: (NSTitledWindowMask
                                      | NSClosableWindowMask
                                      | NSMiniaturizableWindowMask
                                      | NSResizableWindowMask)
                          backing: NSBackingStoreBuffered
                          defer: NO];

  tabView = [[NSTabView alloc] initWithFrame: tabViewRect];
  [tabView setDelegate: [myTabViewDelegate new]];
  [tabView setAutoresizingMask: (NSViewWidthSizable
                               | NSViewHeightSizable)];
  [tabView setAutoresizesSubviews: YES];
  [[window contentView] addSubview: tabView];

  view = [[NSView alloc] initWithFrame:[tabView contentRect]];
  [view setAutoresizingMask: (NSViewWidthSizable
                               | NSViewHeightSizable)];

/*  view = [[tabPlayground alloc] initWithFrame:[tabView contentRect]];
*/
  label = [[NSTextField alloc] initWithFrame:[view frame]];
  [label setEditable: NO];
  [label setSelectable: NO];
  [label setBezeled: NO];
  [label setBordered: NO];
  [label setBackgroundColor: [NSColor redColor]];  
  [label setAlignment: NSCenterTextAlignment];
  [label setStringValue: @"Do you swing, baby?"];

  [view addSubview: label];

  item = [[GSImageTabViewItem alloc] initWithIdentifier:@"Urph"];
  [item setImage: [NSImage imageNamed:@"Smiley"]];
  [item setLabel: @"Natalie"];
  [item setView:  view];
  
  [tabView addTabViewItem: item];

  [window setTitle:@"NSTabView"];
  [window orderFrontRegardless];

  [[NSApp mainMenu] insertItemWithTitle: @"Tabs"
				 action: NULL
			  keyEquivalent: @""
				atIndex: 0];

  tabMenu = [NSMenu new];
  [[NSApp mainMenu] setSubmenu:tabMenu forItem:[[NSApp mainMenu] itemWithTitle:@"Tabs"]];
  [tabMenu addItemWithTitle: @"Add"
                     action: @selector(addTab:)
              keyEquivalent: @"a"];
  [tabMenu addItemWithTitle: @"Remove"
                     action: @selector(removeTab:)
              keyEquivalent: @"r"];
  [tabMenu addItemWithTitle: @"Change Tab Font"
                     action: @selector(changeTabFont:)
               keyEquivalent: @""];
}
@end

@implementation myTabViewDelegate
- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
  NSLog(@"shouldSelectTabViewItem: %@", [tabViewItem label]);

  /*
   * This is a test to see if the delegate is doing its job.
   */

//  if ([[tabViewItem label] isEqual:@"Natalie"])
//    return NO;

  return YES;
}

- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
  NSLog(@"willSelectTabViewItem: %@", [tabViewItem label]);
}

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
  NSLog(@"didSelectTabViewItem: %@", [tabViewItem label]);
}

- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)TabView
{
  NSLog(@"tabViewDidChangeNumberOfTabViewItems: %d", [TabView numberOfTabViewItems]);
}
@end

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

  app = [NSApplication sharedApplication];

  [app setDelegate: [appController new]];

  {
    NSMenu	*menu = [NSMenu new];

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

  [app run];

  [pool release];

  return 0;
}