| 27 */ |
27 */ |
| 28 |
28 |
| 29 #import "text.h" |
29 #import "text.h" |
| 30 #import "EventData.h" |
30 #import "EventData.h" |
| 31 #import "container.h" |
31 #import "container.h" |
| |
32 #import "action.h" |
| 32 #import <objc/runtime.h> |
33 #import <objc/runtime.h> |
| 33 |
34 |
| 34 UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs *args) { |
35 UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs *args) { |
| 35 NSTextView *textview = [[NSTextView alloc] init]; |
36 NSTextView *textview = [[NSTextView alloc] init]; |
| 36 textview.autoresizingMask = NSViewWidthSizable; |
37 textview.autoresizingMask = NSViewWidthSizable; |
| 184 |
185 |
| 185 |
186 |
| 186 |
187 |
| 187 /* -------------------------- TextField -------------------------- */ |
188 /* -------------------------- TextField -------------------------- */ |
| 188 |
189 |
| |
190 static void textfield_geteventdata(id sender, UiVar *var, void **eventdata, int *eventdatatype, int *value) { |
| |
191 |
| |
192 } |
| |
193 |
| 189 static UIWIDGET textfield_create(UiObject *obj, UiTextFieldArgs *args, BOOL password, BOOL frameless) { |
194 static UIWIDGET textfield_create(UiObject *obj, UiTextFieldArgs *args, BOOL password, BOOL frameless) { |
| 190 NSTextField *textfield; |
195 NSTextField *textfield; |
| 191 if(password) { |
196 if(password) { |
| 192 textfield = [[NSSecureTextField alloc] init]; |
197 textfield = [[NSSecureTextField alloc] init]; |
| 193 } else { |
198 } else { |
| 220 s->obj = (__bridge void*)textfield; |
225 s->obj = (__bridge void*)textfield; |
| 221 s->get = ui_textfield_get; |
226 s->get = ui_textfield_get; |
| 222 s->set = ui_textfield_set; |
227 s->set = ui_textfield_set; |
| 223 } |
228 } |
| 224 |
229 |
| |
230 if(args->onactivate || args->onactivate_action) { |
| |
231 EventData *event = [[EventData alloc] init:args->onactivate userdata:args->onactivatedata action:args->onactivate_action]; |
| |
232 event.get_eventdata = textfield_geteventdata; |
| |
233 event.obj = obj; |
| |
234 textfield.target = event; |
| |
235 textfield.action = @selector(handleEventWithEventData:); |
| |
236 objc_setAssociatedObject(textfield, "eventdata", event, OBJC_ASSOCIATION_RETAIN); |
| |
237 ui_cocoa_view_bind_action(obj->ctx, textfield, args->onactivate_action); |
| |
238 } |
| |
239 |
| |
240 if(args->onchange || args->onchange_action) { |
| |
241 TextFieldDelegate *tfd = [[TextFieldDelegate alloc]init:obj var:var]; |
| |
242 tfd.onchange = args->onchange; |
| |
243 tfd.onchangedata = args->onchangedata; |
| |
244 if(args->onchange_action) { |
| |
245 tfd.onchange_action = [[NSString alloc]initWithUTF8String:args->onchange_action]; |
| |
246 } |
| |
247 objc_setAssociatedObject(textfield, "delegate", tfd, OBJC_ASSOCIATION_RETAIN); |
| |
248 textfield.delegate = tfd; |
| |
249 } |
| |
250 |
| 225 return (__bridge void*)textfield; |
251 return (__bridge void*)textfield; |
| 226 } |
252 } |
| 227 |
253 |
| 228 UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs *args) { |
254 UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs *args) { |
| 229 return textfield_create(obj, args, FALSE, FALSE); |
255 return textfield_create(obj, args, FALSE, FALSE); |
| 304 NSTextField *tf = (__bridge NSTextField*)textfield; |
330 NSTextField *tf = (__bridge NSTextField*)textfield; |
| 305 NSTextView *editor = (NSTextView *)[tf currentEditor]; |
331 NSTextView *editor = (NSTextView *)[tf currentEditor]; |
| 306 NSRange selectedRange = [editor selectedRange]; |
332 NSRange selectedRange = [editor selectedRange]; |
| 307 return (int)selectedRange.location; |
333 return (int)selectedRange.location; |
| 308 } |
334 } |
| |
335 |
| |
336 |
| |
337 /* -------------------- textfield delegate -------------------- */ |
| |
338 |
| |
339 @implementation TextFieldDelegate |
| |
340 |
| |
341 - (id)init:(UiObject*)obj var:(UiVar*)var { |
| |
342 self.obj = obj; |
| |
343 self.var = var; |
| |
344 return self; |
| |
345 } |
| |
346 |
| |
347 - (void)controlTextDidChange:(NSNotification *)obj { |
| |
348 UiString *value = _var ? _var->value : NULL; |
| |
349 |
| |
350 UiEvent e; |
| |
351 e.obj = _obj; |
| |
352 e.window = e.obj->window; |
| |
353 e.document = e.obj->ctx->document; |
| |
354 e.eventdata = value; |
| |
355 e.eventdatatype = value ? UI_EVENT_DATA_STRING_VALUE : 0; |
| |
356 e.intval = 0; |
| |
357 e.set = ui_get_setop(); |
| |
358 |
| |
359 if(_onchange) { |
| |
360 _onchange(&e, _onchangedata); |
| |
361 } |
| |
362 |
| |
363 if(_var) { |
| |
364 ui_notify_evt(value->observers, &e); |
| |
365 } |
| |
366 |
| |
367 if(_onchange_action) { |
| |
368 uic_action_callback(&e, _onchange_action.UTF8String); |
| |
369 } |
| |
370 } |
| |
371 |
| |
372 @end |