1 module bindbc.cocoa.controls.nscontrol;
2 
3 import bindbc.cocoa.runtime;
4 import bindbc.cocoa.foundation;
5 import bindbc.cocoa.appkit;
6 import bindbc.cocoa.controls.nscell;
7 import bindbc.cocoa.controls.nsfont;
8 
9 alias NSTextAlignment = NSUInteger;
10 enum : NSTextAlignment {
11     left, right, center, justified, natural
12 }
13 
14 struct NSControl
15 {
16 nothrow @nogc:
17     NSView parent;
18     alias parent this;
19 
20     mixin NSObjectTemplate!(NSControl, "NSControl");
21 
22     Class cellClass(){
23         alias fun_t = extern(C) Class function (id, SEL) nothrow @nogc;
24         return (cast(fun_t)objc_msgSend)(_id, sel!"cellClass");
25     }
26 
27     void setCellClass(Class factoryId){
28 
29         alias fun_t = extern(C) void function (id, SEL, Class) nothrow @nogc;
30         (cast(fun_t)objc_msgSend)(_id, sel!"setCellClass:", factoryId);
31     }
32 
33     id cell(){
34         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
35         return (cast(fun_t)objc_msgSend)(_id, sel!"cell");
36     }
37 
38     void setCell(NSCell aCell){
39 
40         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
41         (cast(fun_t)objc_msgSend)(_id, sel!"setCell:", aCell._id);
42     }
43 
44     BOOL isEnabled(){
45         alias fun_t = extern(C) BOOL function (id, SEL) nothrow @nogc;
46         return (cast(fun_t)objc_msgSend)(_id, sel!"isEnabled");
47     }
48     
49     void setEnabled(BOOL flag){
50 
51         alias fun_t = extern(C) void function (id, SEL, BOOL) nothrow @nogc;
52         (cast(fun_t)objc_msgSend)(_id, sel!"setEnabled:", flag);
53     }
54 
55     id selectedCell(){
56         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
57         return (cast(fun_t)objc_msgSend)(_id, sel!"selectedCell");
58     }
59     
60     void setDoubleValue(double aDouble){
61 
62         alias fun_t = extern(C) void function (id, SEL, double) nothrow @nogc;
63         (cast(fun_t)objc_msgSend)(_id, sel!"setDoubleValue:", aDouble);
64     }
65     
66     double doubleValue(){
67         alias fun_t = extern(C) double function (id, SEL) nothrow @nogc;
68         return (cast(fun_t)objc_msgSend)(_id, sel!"doubleValue");
69     }
70 
71     void setFloatValue(float aFloat){
72 
73         alias fun_t = extern(C) void function (id, SEL, float) nothrow @nogc;
74         (cast(fun_t)objc_msgSend)(_id, sel!"setFloatValue:", aFloat);
75     }
76     
77     float floatValue(){
78         alias fun_t = extern(C) float function (id, SEL) nothrow @nogc;
79         return (cast(fun_t)objc_msgSend)(_id, sel!"floatValue");
80     }
81 
82     void setIntValue(int anInt){
83 
84         alias fun_t = extern(C) void function (id, SEL, int) nothrow @nogc;
85         (cast(fun_t)objc_msgSend)(_id, sel!"setIntValue:", anInt);
86     }
87     
88     int intValue(){
89         alias fun_t = extern(C) int function (id, SEL) nothrow @nogc;
90         return (cast(fun_t)objc_msgSend)(_id, sel!"intValue");
91     }
92 
93     void setIntegerValue(NSInteger anInt){
94 
95         alias fun_t = extern(C) void function (id, SEL, NSInteger) nothrow @nogc;
96         (cast(fun_t)objc_msgSend)(_id, sel!"setIntValue:", anInt);
97     }
98     
99     NSInteger integerValue(){
100         alias fun_t = extern(C) NSInteger function (id, SEL) nothrow @nogc;
101         return (cast(fun_t)objc_msgSend)(_id, sel!"integerValue");
102     }
103 
104     void takeIntegerValueFrom(id sender)
105     {
106         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
107         (cast(fun_t)objc_msgSend)(_id, sel!"takeIntegerValueFrom:", sender);
108     }
109 
110     NSSize sizeThatFits(NSSize size){
111         alias fun_t = extern(C) NSSize function (id, SEL, NSSize) nothrow @nogc;
112         return (cast(fun_t)objc_msgSend)(_id, sel!"sizeThatFits:", size);
113     }
114 
115     void setStringValue(NSString aString)
116     {
117         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
118         (cast(fun_t)objc_msgSend)(_id, sel!"setStringValue:", aString._id);
119     }
120     
121     NSString stringValue()
122     {
123         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
124         return NSString((cast(fun_t)objc_msgSend)(_id, sel!"stringValue"));
125     }
126 
127     void setObjectValue(id anObject)
128     {
129         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
130         (cast(fun_t)objc_msgSend)(_id, sel!"setStringValue:", anObject);
131     }
132 
133     id objectValue(){
134         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
135         return (cast(fun_t)objc_msgSend)(_id, sel!"objectValue");
136     }
137 
138     void setNeedsDisplay()
139     {
140         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
141         (cast(fun_t)objc_msgSend)(_id, sel!"setNeedsDisplay");
142     }
143 
144     void takeDoubleValueFrom(id sender)
145     {
146         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
147         (cast(fun_t)objc_msgSend)(_id, sel!"takeDoubleValueFrom:", sender);
148     }
149     
150     void takeFloatValueFrom(id sender)
151     {
152         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
153         (cast(fun_t)objc_msgSend)(_id, sel!"takeFloatValueFrom:", sender);
154     }
155     
156     void takeIntValueFrom(id sender)
157     {
158         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
159         (cast(fun_t)objc_msgSend)(_id, sel!"takeIntValueFrom:", sender);
160     }
161     
162     void takeStringValueFrom(id sender)
163     {
164         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
165         (cast(fun_t)objc_msgSend)(_id, sel!"takeStringValueFrom:", sender);
166     }
167     
168     void takeObjectValueFrom(id sender)
169     {
170         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
171         (cast(fun_t)objc_msgSend)(_id, sel!"takeObjectValueFrom:", sender);
172     }
173 
174     NSTextAlignment alignment(){
175         alias fun_t = extern(C) NSTextAlignment function (id, SEL) nothrow @nogc;
176         return (cast(fun_t)objc_msgSend)(_id, sel!"alignment");
177     }
178 
179     NSFont font(){
180         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
181         return NSFont((cast(fun_t)objc_msgSend)(_id, sel!"font"));
182     }
183 
184     void setAlignment(NSTextAlignment mode){
185         alias fun_t = extern(C) void function (id, SEL, NSTextAlignment) nothrow @nogc;
186         (cast(fun_t)objc_msgSend)(_id, sel!"setAlignment:", mode);
187     }
188 
189     void setFont(NSFont fontObject){
190         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
191         (cast(fun_t)objc_msgSend)(_id, sel!"setFont:", fontObject._id);
192     }
193 
194     // TODO 
195     /+
196     - (void)setFloatingPointFormat:(BOOL)autoRange
197                 left:(NSUInteger)leftDigits
198                 right:(NSUInteger)rightDigits;
199     #if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
200     - (void)setFormatter:(NSFormatter*)newFormatter;
201     - (id)formatter;
202     #endif
203     #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
204     - (NSWritingDirection)baseWritingDirection;
205     - (void)setBaseWritingDirection:(NSWritingDirection)direction;
206     #endif
207 
208     //
209     // Managing the Field Editor 
210     //
211     - (BOOL)abortEditing;
212     - (NSText *)currentEditor;
213     - (void)validateEditing;
214 
215     //
216     // Resizing the Control 
217     //
218     - (void)calcSize;
219     - (void)sizeToFit;
220 
221     //
222     // Displaying the Control and Cell 
223     //
224     - (void)drawCell:(NSCell *)aCell;
225     - (void)drawCellInside:(NSCell *)aCell;
226     - (void)selectCell:(NSCell *)aCell;
227     - (void)updateCell:(NSCell *)aCell;
228     - (void)updateCellInside:(NSCell *)aCell;
229 
230     //
231     // Target and Action 
232     +/
233 
234     NSInteger selectedTag(){
235         alias fun_t = extern(C) NSInteger function (id, SEL) nothrow @nogc;
236         return (cast(fun_t)objc_msgSend)(_id, sel!"selectedTag");
237     }
238 
239     SEL action(){
240         alias fun_t = extern(C) SEL function (id, SEL) nothrow @nogc;
241         return (cast(fun_t)objc_msgSend)(_id, sel!"action");
242     }
243 
244     void setAction(Func)(Func fun){
245         
246         import core.stdc.stdio;
247         
248         char* _cp = cast(char*)_id;
249 
250         char[32] m_name;
251         sprintf(m_name.ptr, "a%p_action:", _cp);
252 
253         SEL _sel = varsel_registerName(m_name.ptr);
254 
255         class_addMethod(NSObject.getClass, _sel, cast(IMP)fun, "@:i@");
256         setAction(_sel);
257     }
258 
259     void setAction(ref SEL _sel){
260         alias fun_t = extern(C) void function (id, SEL, SEL) nothrow @nogc;
261         (cast(fun_t)objc_msgSend)(_id, sel!"setAction:", _sel);
262     }
263 
264     void setTarget(id anObject){
265 
266         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
267         (cast(fun_t)objc_msgSend)(_id, sel!"setTarget:", anObject);
268     }
269 
270     void setTag(NSInteger anInt){
271 
272         alias fun_t = extern(C) void function (id, SEL, NSInteger) nothrow @nogc;
273         (cast(fun_t)objc_msgSend)(_id, sel!"setTag:", anInt);
274     }
275 
276     NSInteger tag(){
277         alias fun_t = extern(C) NSInteger function (id, SEL) nothrow @nogc;
278         return (cast(fun_t)objc_msgSend)(_id, sel!"tag");
279     }
280 }
281 
282 /+ TODO
283 
284 
285 //
286 // Enabling and Disabling the Control 
287 //
288 - (BOOL)isEnabled;
289 - (void)setEnabled:(BOOL)flag;
290 
291 //
292 // Identifying the Selected Cell 
293 //
294 - (id)selectedCell;
295 - (NSInteger)selectedTag;
296 
297 //
298 // Setting the Control's Value 
299 //
300 - (void) setDoubleValue: (double)aDouble;
301 - (double) doubleValue;
302 
303 - (void) setFloatValue: (float)aFloat;
304 - (float) floatValue;
305 
306 - (void) setIntValue: (int)anInt;
307 - (int) intValue;
308 
309 #if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST)
310 - (NSInteger) integerValue;
311 - (void) setIntegerValue: (NSInteger)anInt;
312 - (void) takeIntegerValueFrom: (id)sender;
313 #endif
314 
315 #if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
316 - (NSSize) sizeThatFits: (NSSize)size;
317 #endif
318 
319 - (void) setStringValue: (NSString *)aString;
320 - (NSString *) stringValue;
321 
322 - (void) setObjectValue: (id)anObject;
323 - (id) objectValue;
324 
325 - (void) setNeedsDisplay;
326 
327 //
328 // Interacting with Other Controls 
329 //
330 - (void) takeDoubleValueFrom: (id)sender;
331 - (void) takeFloatValueFrom: (id)sender;
332 - (void) takeIntValueFrom: (id)sender;
333 - (void) takeStringValueFrom: (id)sender;
334 - (void) takeObjectValueFrom: (id)sender;
335 
336 +/