forked from AlessandroSangiuliano/rik.theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSSearchFieldCell+Rik.m
241 lines (205 loc) · 8.51 KB
/
NSSearchFieldCell+Rik.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
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
/**
* Copyright (C) 2013 Alessandro Sangiuliano
* Author: Alessandro Sangiuliano <[email protected]>
* Date: 31 December 2013
*/
#import "Rik.h"
#import "NSSearchFieldCell+Rik.h"
#import "Rik+Button.h"
#define ICON_WIDTH 16
@interface NSSearchFieldCell (RikTheme)
- (void) RIKdrawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView;
- (NSRect) RIKsearchTextRectForBounds: (NSRect)rect;
- (void) _RIKdrawBorderAndBackgroundWithFrame: (NSRect)cellFrame
inView: (NSView*)controlView;
- (void) RIKdrawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView;
- (void) _RIKdrawEditorWithFrame: (NSRect)cellFrame
inView: (NSView *)controlView;
- (NSRect) RIKtitleRectForBounds: (NSRect)theRect;
- (NSRect) RIKsearchButtonRectForBounds: (NSRect)rect;
- (NSRect) RIKcancelButtonRectForBounds: (NSRect)rect;
@end
@implementation Rik(NSSearchFieldCell)
- (void) _overrideNSSearchFieldCellMethod_drawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView {
RIKLOG(@"_overrideNSSearchFieldCellMethod_drawWithFrame:inView");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
[xself RIKdrawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView];
}
- (NSRect) _overrideNSSearchFieldCellMethod_searchTextRectForBounds: (NSRect)rect {
RIKLOG(@"_overrideNSSearchFieldCellMethod_searchTextRectForBounds:");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
return [xself RIKsearchTextRectForBounds:rect];
}
- (void) _overrideNSSearchFieldCellMethod__drawBorderAndBackgroundWithFrame: (NSRect)cellFrame
inView: (NSView*)controlView {
RIKLOG(@"_overrideNSSearchFieldCellMethod__drawBorderAndBackgroundWithFrame:inView:");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
[xself _RIKdrawBorderAndBackgroundWithFrame:cellFrame inView:controlView];
}
- (void) _overrideNSSearchFieldCellMethod_drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView {
RIKLOG(@"_overrideNSSearchFieldCellMethod_drawInteriorWithFrame:inView:");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
[xself RIKdrawInteriorWithFrame:cellFrame inView:controlView];
}
- (void) _overrideNSSearchFieldCellMethod__drawEditorWithFrame: (NSRect)cellFrame
inView: (NSView *)controlView {
RIKLOG(@"_overrideNSSearchFieldCellMethod__drawEditorWithFrame:inView:");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
[xself _RIKdrawEditorWithFrame:cellFrame inView:controlView];
}
- (NSRect) _overrideNSSearchFieldCellMethod_titleRectForBounds: (NSRect)theRect {
RIKLOG(@"_overrideNSSearchFieldCellMethod_titleRectForBounds:");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
return [xself RIKtitleRectForBounds:theRect];
}
- (NSRect) _overrideNSSearchFieldCellMethod_searchButtonRectForBounds: (NSRect)rect {
RIKLOG(@"_overrideNSSearchFieldCellMethod_searchButtonRectForBounds:");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
return [xself RIKsearchButtonRectForBounds:rect];
}
- (NSRect) _overrideNSSearchFieldCellMethod_cancelButtonRectForBounds: (NSRect)rect {
RIKLOG(@"_overrideNSSearchFieldCellMethod_cancelButtonRectForBounds:");
NSSearchFieldCell *xself = (NSSearchFieldCell*)self;
return [xself RIKcancelButtonRectForBounds:rect];
}
@end
@implementation NSSearchFieldCell (RikTheme)
- (void) RIKdrawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
// TS: unused
// NSRect frame = cellFrame;
[super drawWithFrame: [self searchTextRectForBounds: cellFrame ]
inView: controlView];
[_search_button_cell drawWithFrame: [self searchButtonRectForBounds: cellFrame] inView: controlView];
if ([[self stringValue] length] > 0)
[_cancel_button_cell drawWithFrame: [self cancelButtonRectForBounds: cellFrame]
inView: controlView];
}
/* This method put the "x" cell inside the Text cell */
- (NSRect) RIKsearchTextRectForBounds: (NSRect)rect
{
NSRect search, text, part;
if (_search_button_cell)
{
part = rect;
/*set the right point and size*/
part.origin.x +=0;
part.size.width -= 1;
}
else
{
NSDivideRect(rect, &search, &part, ICON_WIDTH, NSMinXEdge);
}
text = part;
return text;
}
- (void) _RIKdrawBorderAndBackgroundWithFrame: (NSRect)cellFrame
inView: (NSView*)controlView
{
NSColor* whiteColor = [NSColor colorWithCalibratedRed: 1
green: 1
blue: 1
alpha: 0.8];
NSColor* clearColor = [NSColor colorWithCalibratedRed: 1
green: 1
blue: 1
alpha: 0];
NSColor * strokeBaseColor = [Rik controlStrokeColor];
NSColor * strokeLightColor = [strokeBaseColor highlightWithLevel: 0.3];
NSGradient* lightGradient = [[NSGradient alloc] initWithColorsAndLocations:
clearColor, 0.0,
whiteColor, 0.97, nil];
NSGradient* bezelBorderGradient = [[NSGradient alloc] initWithColorsAndLocations:
strokeBaseColor, 1.0,
strokeLightColor, 0.5, nil];
NSGradient* fillGradient = [[NSGradient alloc] initWithColorsAndLocations:
[strokeBaseColor highlightWithLevel: 0.7], 0.0,
[NSColor whiteColor], 0.2, nil];
NSRect rect = cellFrame;
CGFloat radius = rect.size.height / 2.0;
NSBezierPath* lightPath = [NSBezierPath bezierPathWithRoundedRect: rect
xRadius: radius
yRadius: radius];
NSBezierPath* bezelPath = [NSBezierPath bezierPathWithRoundedRect: NSInsetRect(rect, 1, 1)
xRadius: radius-2
yRadius: radius-2];
NSBezierPath* fillPath = [NSBezierPath bezierPathWithRoundedRect: NSInsetRect(rect, 2, 2)
xRadius: radius-2
yRadius: radius-2];
[lightGradient drawInBezierPath: lightPath angle: 90];
[bezelBorderGradient drawInBezierPath: bezelPath angle: -90];
[fillGradient drawInBezierPath: fillPath angle: 90];
}
- (void) RIKdrawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
if (_cell.in_editing)
[self _drawEditorWithFrame: cellFrame inView: controlView];
else
{
NSRect titleRect;
/* Make sure we are a text cell; titleRect might return an incorrect
rectangle otherwise. Note that the type could be different if the
user has set an image on us, which we just ignore (OS X does so as
well). */
_cell.type = NSTextCellType;
titleRect = [self titleRectForBounds: cellFrame];
[[self _drawAttributedString] drawInRect: titleRect];
}
}
- (void) _RIKdrawEditorWithFrame: (NSRect)cellFrame
inView: (NSView *)controlView
{
if ([controlView isKindOfClass: [NSControl class]])
{
/* Adjust the text editor's frame to match cell's frame (w/o border) */
NSRect titleRect = [self titleRectForBounds: cellFrame];
NSText *textObject = [(NSControl*)controlView currentEditor];
NSView *clipView = [textObject superview];
if ([clipView isKindOfClass: [NSClipView class]])
{
[clipView setFrame: titleRect];
}
else
{
[textObject setFrame: titleRect];
}
}
}
- (NSRect) RIKtitleRectForBounds: (NSRect)theRect
{
if (_cell.type == NSTextCellType)
{
NSRect frame = [self drawingRectForBounds: theRect];
//Add spacing between border and inside
if (_cell.is_bordered || _cell.is_bezeled)
{
frame.origin.x += 16;
frame.size.width -= 30;
/*By Slex: If you modify this value, then the chars. will overlap when the text field is *full filled of charcters. You'll see part of characters like 'p' or 'g' or 'j' taking
*the next line of the editor text field, looking very bad
*/
frame.size.height += 0;
}
return frame;
}
else
{
return theRect;
}
}
- (NSRect) RIKsearchButtonRectForBounds: (NSRect)rect
{
NSRect search, part;
NSDivideRect(rect, &search, &part, ICON_WIDTH, NSMinXEdge);
search.origin.x += 4;
search.origin.y += 0;
return search;
}
- (NSRect) RIKcancelButtonRectForBounds: (NSRect)rect
{
NSRect part, clear;
NSDivideRect(rect, &clear, &part, ICON_WIDTH, NSMaxXEdge);
clear.origin.x -= 5; //This set the position inside the textsearch box
return clear;
}
@end