-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataControlView.m
144 lines (120 loc) · 3.63 KB
/
DataControlView.m
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
//
// DataControlView.m
// DataViewTest
//
// Created by RenŽ Puls on 27.02.05.
// Copyright 2005 RenŽ Puls. All rights reserved.
//
#import "DataControlView.h"
#import "DataView.h"
@implementation DataControlView
#pragma mark Init and Cleanup
+ (float)defaultHeight
{
return 28.0;
}
- (id)initWithFrame:(NSRect)aFrame dataView:(DataView *)myDataView
{
NSRect myFrame;
NSParameterAssert(myDataView != nil);
myFrame = NSMakeRect(NSMinX(aFrame), NSMinY(aFrame), NSWidth(aFrame), [[self class] defaultHeight]);
if ((self == [super initWithFrame:myFrame])) {
dataView = myDataView;
// Initialize the type selector
typeSegmentedControl = [[NSSegmentedControl alloc] initWithFrame:NSZeroRect];
{
NSSegmentedCell *segmentedCell = [typeSegmentedControl cell];
NSString *segmentLabel;
int segmentTag;
NSRect segmentedFrame;
int segmentCount = 3;
// Set up appearance
[segmentedCell setControlSize:NSSmallControlSize];
[segmentedCell setFont:[NSFont controlContentFontOfSize:10.0]];
// Set up segments
[segmentedCell setSegmentCount:segmentCount];
for (int currentSegment=0; currentSegment<segmentCount; currentSegment++) {
switch (currentSegment) {
case 0:
segmentLabel = @"Text";
segmentTag = DataViewTextContentType;
break;
case 1:
segmentLabel = @"Image";
segmentTag = DataViewImageContentType;
break;
case 2:
segmentLabel = @"HTML";
segmentTag = DataViewWebContentType;
break;
default:
segmentLabel = nil;
segmentTag = 0;
NSAssert(FALSE, @"Unhandled content type");
break;
}
[segmentedCell setLabel:segmentLabel forSegment:currentSegment];
[segmentedCell setTag:segmentTag forSegment:currentSegment];
}
[typeSegmentedControl setSelectedSegment:0];
[typeSegmentedControl sizeToFit];
// Adjust the frame to make it right-aligned
segmentedFrame = [typeSegmentedControl frame];
segmentedFrame.origin.x = NSWidth(myFrame) - NSWidth(segmentedFrame) - 16;
segmentedFrame.origin.y = NSHeight(myFrame) / 2 - NSHeight(segmentedFrame) / 2 - 1;
[typeSegmentedControl setFrameOrigin:segmentedFrame.origin];
[typeSegmentedControl setAutoresizingMask:NSViewMinXMargin];
[typeSegmentedControl setTarget:self];
[typeSegmentedControl setAction:@selector(switchContentType:)];
[self addSubview:typeSegmentedControl];
[typeSegmentedControl release];
}
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
#pragma mark Drawing and Geometry
- (void)drawRect:(NSRect)rect
{
NSBezierPath *borderPath;
[[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] set];
NSRectFill(rect);
[[NSColor colorWithCalibratedWhite:0.5 alpha:1.0] set];
borderPath = [[NSBezierPath alloc] init];
[borderPath moveToPoint:NSMakePoint(0,0)];
[borderPath lineToPoint:NSMakePoint(NSWidth([self bounds]), 0)];
[borderPath moveToPoint:NSMakePoint(0,NSHeight([self bounds])-1)];
[borderPath lineToPoint:NSMakePoint(NSWidth([self bounds]), NSHeight([self bounds])-1)];
// [borderPath stroke];
[borderPath release];
}
#pragma mark Actions
- (void)setSelectedContentType:(DataViewContentType)newType
{
int segmentNum = -1;
switch (newType) {
case DataViewTextContentType:
segmentNum = 0;
break;
case DataViewImageContentType:
segmentNum = 1;
break;
case DataViewWebContentType:
segmentNum = 2;
break;
default:
NSAssert(FALSE, @"Unhandled content type");
break;
}
[typeSegmentedControl setSelectedSegment:segmentNum];
}
- (void)switchContentType:(id)sender
{
int tag;
tag = [[sender cell] tagForSegment:[sender selectedSegment]];
[dataView setContentType:tag];
}
@end