-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWinNSTabView.m
206 lines (180 loc) · 7.8 KB
/
WinNSTabView.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
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
//
// WinNSTabView.m
// WinUXTheme
//
// Created by Marcian Lytwyn on 1/9/18.
// Copyright © 2018 Marcian Lytwyn. All rights reserved.
//
#import "WinUXTheme.h"
#include <uxtheme.h>
@implementation WinUXTheme (NSTabView)
static int _TabStateForThemeControlState(NSTabState state)
{
switch (state)
{
case NSPressedTab:
return TIS_FOCUSED;
case NSSelectedTab:
return TIS_SELECTED;
case NSBackgroundTab:
default:
return TIS_NORMAL;
}
}
- (void) drawTabViewBezelRect: (NSRect)aRect
tabViewType: (NSTabViewType)type
inView: (NSView *)view
{
if (IsThemeActive())
{
HTHEME hTheme = [self themeWithClassName: @"tab"];
if (hTheme != NULL)
{
if ([self drawThemeBackground: hTheme
inRect: aRect
part: TABP_BODY
state: 0])
{
[self releaseTheme: hTheme];
return;
}
[self releaseTheme: hTheme];
}
}
[super drawTabViewBezelRect: aRect
tabViewType: type
inView: view];
}
- (CGFloat) tabHeightForType: (NSTabViewType)type
{
HTHEME hTheme = [self themeWithClassName: @"tab"];
NSSize size = NSMakeSize(0, 17.0);
if (hTheme == NULL)
{
// Default to GSTheme...
size.height = [super tabHeightForType: type];
}
else
{
size = [self sizeForTheme: hTheme part: TABP_TABITEM state: TIS_NORMAL type: WIN32ThemeSizeBestFit];
[self releaseTheme: hTheme];
// Check for minimum as sometimes the windows theme is messed up for tab...
// Initialized in -initialize method for WinUXTheme class...
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"GSMinimumTabHeight"])
{
CGFloat minimum = [[NSUserDefaults standardUserDefaults] floatForKey:@"GSMinimumTabHeight"];
CGFloat maximum = [[NSUserDefaults standardUserDefaults] floatForKey:@"GSMaximumTabHeightPrivate"];
if ((0.0 < minimum) && (minimum < maximum))
{
NSDebugMLLog(@"WinNSTabView", @"size.height: %f min: %f max: %f", size.height, minimum, maximum);
size.height = MAX(size.height, minimum);
}
}
}
return size.height;
}
- (void) drawTabViewRect: (NSRect)rect
inView: (NSView *)view
withItems: (NSArray *)items
selectedItem: (NSTabViewItem *)selected
{
if (!IsThemeActive())
{
[super drawTabViewRect: rect
inView: view
withItems: items
selectedItem: selected];
}
else
{
// Ensure the requested drawing rectangle includes the tab area...
const NSTabViewType type = [(NSTabView *)view tabViewType];
const CGFloat tabHeight = [self tabHeightForType: type];
const NSRect bounds = [view bounds];
const NSRect frame = [view frame];
NSRect aRect = [self tabViewBackgroundRectForBounds: bounds tabViewType: type];
NSPoint iP = ((type == NSTopTabsBezelBorder) ?
NSMakePoint(bounds.origin.x, bounds.origin.y) :
NSMakePoint(rect.origin.x, NSMaxY(aRect)));
const NSRect check = NSMakeRect(iP.x, iP.y, bounds.size.width, tabHeight);
NSDebugMLLog(@"WinNSTabView", @"frame: %@ rect: %@ check: %@ intersection: %@",
NSStringFromRect(frame),
NSStringFromRect(rect), NSStringFromRect(check),
NSStringFromRect(NSIntersectionRect(rect, check)));
// Avoid drawing tabs if the tab view part is NOT in the requested rectangle...
if (NSIntersectsRect(rect, check))
{
// Using tab class...
HTHEME hTheme = [self themeWithClassName: @"tab"];
if (hTheme == NULL)
{
// Default to GSTheme...
[super drawTabViewRect: rect
inView: view
withItems: items
selectedItem: selected];
}
else
{
static const CGFloat FRAME_ADJUST = 12.0;
const NSTabViewType type = [(NSTabView *)view tabViewType];
const BOOL truncate = [(NSTabView *)view allowsTruncatedLabels];
int part = TABP_TABITEMBOTHEDGE;
id item = nil;
id firstItem = [items firstObject];
id lastItem = [items lastObject];
NSInteger itemCount = [items count];
NSDebugMLLog(@"WinNSTabView", @"rect: %@ aRect: %@",
NSStringFromRect(rect), NSStringFromRect(aRect));
// Draw the background...
// FIXME: Use super as Windows Theming may not match GNUstep GSTheme
// for processing that DOES NOT include WinUXTheme...
// Unfortunately this means we're mixing HDC and GNUstep drawing but
// unavoidable for now...
[super drawTabViewBezelRect: bounds tabViewType: type inView: view];
// Loop thru tab items and draw each one...
for (item in items)
{
const NSSize s = [item sizeOfLabel: truncate];
NSRect tabFrame = NSMakeRect(iP.x, iP.y, s.width+FRAME_ADJUST, tabHeight);
NSRect labelFrame = NSMakeRect(iP.x+(FRAME_ADJUST/2), iP.y, s.width, tabHeight);
int drawState = _TabStateForThemeControlState([item tabState]);
NSDebugMLLog(@"WinNSTabView", @"label: %@ tabFrame: %@ labelFrame: %@ intersection: %@",
[item label],
NSStringFromRect(tabFrame), NSStringFromRect(labelFrame),
NSStringFromRect(NSIntersectionRect(rect, tabFrame)));
// Avoid drawing a tab if the view part is NOT in the requested rectangle...
if (NSIntersectsRect(rect, tabFrame))
{
if (item == firstItem) // could be one item in this case...
part = ((itemCount == 1) ? TABP_TABITEMBOTHEDGE : TABP_TABITEMLEFTEDGE);
else if (item == lastItem)
part = TABP_TABITEMRIGHTEDGE;
else // Middle item - no edge...
part = TABP_TABITEM;
// Draw the part...
[self drawThemeBackground: hTheme inRect: tabFrame part: part state: drawState];
// Label
[item drawLabel: truncate inRect: labelFrame];
}
// Update frame values...
iP.x += tabFrame.size.width;
}
// Draw the background fill for the rest of the tab rectangle area...
if (iP.x < NSMaxX(bounds))
{
NSRect tabFrame = NSMakeRect(iP.x, iP.y, NSMaxX(bounds) - iP.x, tabHeight);
// FIXME: Use super as Windows Theming may not match GNUstep GSTheme
// for processing that DOES NOT include WinUXTheme...
// Unfortunately this means we're mixing HDC and GNUstep drawing but
// unavoidable for now...
[self drawTabFillInRect: tabFrame forPart: GSTabBackgroundFill type: type];
}
// Save the current graphics context state...
//DPSgrestore(ctxt);
[self releaseTheme: hTheme];
}
}
}
}
@end