Debian Codegraph @

third_party/spiro/x3/x3carbon.c

raw symbols SCIP
1 #include "x3.h"
2 #include "x3common.h"
3
4 /* Globals for managing sync */
5
6 #define VERBOSE
7
8 #define kX3ViewClassID CFSTR("com.levien.x3.X3View")
9 #define kX3ViewPrivate 'X3_v'
10
11 /* Some utility-type functions. */
12
13 UInt32 x3mkmultichar(const char *s)
14 {
15 int len = strlen(s);
16 int i;
17 UInt32 result = 0;
18
19 for (i = 0; i < (len > 4 ? 4 : len); i++)
20 result = (result << 8) + (unsigned char)s[i];
21 for (; i < 4; i++)
22 result = (result << 8) + ' ';
23 return result;
24 }
25
26 char *x3multicharstr(UInt32 mc, char buf[5])
27 {
28 int i, len;
29
30 for (i = 0; i < 4; i++)
31 if (((mc >> (8 * i)) & 0xff) != ' ') break;
32 len = 4 - i;
33 for (i = 0; i < len; i++)
34 buf[i] = (mc >> (24 - 8 * i)) & 0xff;
35 buf[len] = 0;
36 return buf;
37 }
38
39 void x3widget_init(x3widget *w, const x3type *type)
40 {
41 w->type = type;
42 w->name = NULL;
43 w->parent = NULL;
44 w->var = x3carbonnone;
45 w->u.window = NULL;
46 w->n_children = 0;
47 w->children = NULL;
48 }
49
50 static x3widget *x3widget_new_container(x3widget *parent, char *name,
51 const x3type *type)
52 {
53 x3widget *result = (x3widget *)malloc(sizeof(x3widget));
54 x3widget_init(result, type);
55 result->name = name ? strdup(name) : NULL;
56 x3add(parent, result);
57 x3qsizereq(result);
58 return result;
59 }
60
61 static x3widget *x3widget_new_hiview(x3widget *parent, char *name,
62 const x3type *type,
63 HIViewRef hiview)
64 {
65 x3widget *result = (x3widget *)malloc(sizeof(x3widget));
66 x3widget_init(result, type);
67 result->name = name ? strdup(name) : NULL;
68 result->var = x3carbonhiview;
69 result->u.hiview = hiview;
70 x3add(parent, result);
71 x3qsizereq(result);
72 return result;
73 }
74
75 static x3widget *x3widget_new_menu(x3widget *parent,
76 const x3type *type,
77 MenuRef menu)
78 {
79 x3widget *result = (x3widget *)malloc(sizeof(x3widget));
80 x3widget_init(result, type);
81 result->var = x3carbonmenu;
82 result->u.menu = menu;
83 x3add(parent, result);
84 return result;
85 }
86
87 static x3widget *x3widget_new_menuitem(x3widget *parent,
88 const x3type *type, int index)
89 {
90 x3widget *result = (x3widget *)malloc(sizeof(x3widget));
91 x3widget_init(result, type);
92 x3add(parent, result);
93 result->var = x3carbonmenuitem;
94 result->u.menuitem = index;
95 return result;
96 }
97
98 void x3init(int *pargc, char ***pargv)
99 {
100 ProcessSerialNumber psn;
101
102 /* Most apps get this done from loading the menu bar from the NIB.
103 Prior to 10.3, there was an undocumented call. This one is official
104 for 10.3 and later. */
105 GetCurrentProcess(&psn);
106 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
107 SetFrontProcess(&psn);
108
109 x3initqs();
110 }
111
112 void x3main(void)
113 {
114 x3sync();
115 RunApplicationEventLoop();
116 }
117
118 void x3_window_show(x3widget *mainwin)
119 {
120 ControlRef root;
121 #if 0
122 TransitionWindow(mainwin->u.window, kWindowZoomTransitionEffect,
123 kWindowShowTransitionAction, NULL);
124 #endif
125
126 #if 0
127 CreateRootControl(mainwin->u.window, &root);
128 #endif
129 //RepositionWindow(mainwin->u.window, NULL, kWindowCascadeOnMainScreen);
130 ShowWindow(mainwin->u.window);
131 SelectWindow(mainwin->u.window);
132 }
133
134 static WindowRef x3window_of(x3widget *w)
135 {
136 while (w->parent) w = w->parent;
137 return w->var == x3carbonwindow ? w->u.window : NULL;
138 }
139
140 typedef struct {
141 x3widget base;
142 x3window_callback callback;
143 void *callback_data;
144 } x3widget_window;
145
146 pascal OSStatus x3carbonWindowEventHandler(EventHandlerCallRef cr,
147 EventRef inEvent, void *data)
148 {
149 x3widget_window *z = (x3widget_window *)data;
150 OSStatus result = noErr;
151 UInt32 eclass = GetEventClass(inEvent);
152 UInt32 ekind = GetEventKind(inEvent);
153 char multicharbuf[5];
154
155 if (eclass == kEventClassCommand && ekind == kEventCommandProcess) {
156 HICommand command;
157 int status;
158
159 GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand,
160 NULL, sizeof(HICommand), NULL, &command);
161 status = z->callback(&z->base, z->callback_data,
162 x3multicharstr(command.commandID, multicharbuf),
163 "command", NULL, NULL);
164 if (status == 1)
165 result = eventNotHandledErr;
166 } else if (eclass = kEventClassWindow && ekind == kEventWindowBoundsChanged) {
167 /* todo: only queue size request when size changes, not just pos */
168 x3qsizereq(&z->base);
169 x3sync();
170 } else {
171 printf("My handler is getting called %s %d!\n",
172 x3multicharstr(eclass, multicharbuf),
173 GetEventKind(inEvent));
174 }
175 result = eventNotHandledErr;
176 return result;
177 }
178
179 void x3window_sizereq(x3widget *w)
180 {
181 }
182
183 void x3window_sizealloc(x3widget *w, x3rect *r)
184 {
185 int i;
186 Rect bounds;
187 x3rect child_r;
188
189 GetWindowBounds(w->u.window, kWindowContentRgn, &bounds);
190 child_r.x0 = 0;
191 child_r.x1 = bounds.right - bounds.left;
192 child_r.y0 = 0;
193 child_r.y1 = bounds.bottom - bounds.top;
194 printf("x3window_sizealloc (%d, %d) - (%d, %d)\n",
195 bounds.left, bounds.top, bounds.right, bounds.bottom);
196 for (i = 0; i < w->n_children; i++) {
197 x3widget *child = w->children[i];
198 if (child->type->sizealloc)
199 child->type->sizealloc(child, &child_r);
200 child->flags &= ~x3flag_needsizealloc;
201 }
202 }
203
204 x3type x3windowtype = { x3window_sizereq,
205 x3window_sizealloc,
206 x3add_default };
207
208 x3widget *x3window(x3windowflags flags, char *label,
209 x3window_callback callback, void *data)
210 {
211 WindowRef window;
212 Rect bounds = { 100, 100, 400, 600 };
213 EventHandlerRef handlerRef;
214 WindowAttributes attrs =
215 kWindowCompositingAttribute |
216 kWindowLiveResizeAttribute |
217 kWindowInWindowMenuAttribute |
218 kWindowFrameworkScaledAttribute |
219 kWindowStandardHandlerAttribute;
220 EventTypeSpec windowEvents[] = {
221 { kEventClassCommand, kEventCommandProcess },
222 //{ kEventClassCommand, kEventCommandUpdateStatus },
223
224 //{ kEventClassMouse, kEventMouseDown },
225
226 //{ kEventClassWindow, kEventWindowClose },
227 //{ kEventClassWindow, kEventWindowGetIdealSize },
228 { kEventClassWindow, kEventWindowBoundsChanged },
229 //{ kEventClassWindow, kEventWindowGetClickActivation },
230 //{ kEventClassWindow, kEventWindowContextualMenuSelect }
231 };
232 CFStringRef cflabel = CFStringCreateWithCString(NULL, label,
233 kCFStringEncodingUTF8);
234 x3widget *result = (x3widget *)malloc(sizeof(x3widget_window));
235
236 if (flags & x3window_main)
237 attrs |= kWindowStandardDocumentAttributes;
238
239 OSStatus err = CreateNewWindow(kDocumentWindowClass, attrs,
240 &bounds, &window);
241
242 SetWindowTitleWithCFString(window, cflabel);
243 CFRelease(cflabel);
244 x3widget_init(result, &x3windowtype);
245 result->var = x3carbonwindow;
246 result->u.window = window;
247 ((x3widget_window *)result)->callback = callback;
248 ((x3widget_window *)result)->callback_data = data;
249
250 InstallWindowEventHandler(window, NewEventHandlerUPP(x3carbonWindowEventHandler),
251 sizeof(windowEvents)/sizeof(EventTypeSpec),
252 windowEvents, result, &handlerRef);
253
254 x3qshow(result);
255 return result;
256 }
257
258 x3type x3menutype = { NULL, NULL, x3add_default };
259
260 x3widget *x3menu(x3widget *parent, char *name)
261 {
262 static int id = 1; /* Note: menu id should probably be kept per-window */
263 MenuRef menu;
264 CFStringRef cflabel = CFStringCreateWithCString(NULL, name,
265 kCFStringEncodingUTF8);
266
267 CreateNewMenu(id++, 0, &menu);
268 SetMenuTitleWithCFString(menu, cflabel);
269 CFRelease(cflabel);
270 InsertMenu(menu, 0);
271 return x3widget_new_menu(parent, &x3menutype, menu);
272 }
273
274 int x3parseshortcut(const char *shortcut, UInt16 *pkey, UInt8 *pmods)
275 {
276 UInt16 key;
277 UInt8 mods = kMenuNoCommandModifier;
278 int i = 0;
279
280 while (shortcut[i] == '<') {
281 if (!strncmp(shortcut + i, "<cmd>", 5)) {
282 mods &= ~kMenuNoCommandModifier;
283 i += 5;
284 } else if (!strncmp(shortcut + i, "<shift>", 7)) {
285 mods |= kMenuShiftModifier;
286 i += 7;
287 } else if (!strncmp(shortcut + i, "<option>", 8)) {
288 mods |= kMenuOptionModifier;
289 i += 8;
290 } else if (!strncmp(shortcut + i, "<ctrl>", 6)) {
291 mods |= kMenuControlModifier;
292 i += 6;
293 } else
294 return false;
295 }
296 if (shortcut[i] && shortcut[i + 1] == 0) {
297 key = shortcut[i];
298 if (key >= 'a' && key <= 'z') key -= 'a' - 'A';
299 else if (key >= 'A' && key <= 'Z') mods |= kMenuShiftModifier;
300 } else
301 return false;
302
303 *pkey = key;
304 *pmods = mods;
305 return true;
306 }
307
308 x3type x3menuitemtype = { NULL, NULL, x3add_default };
309
310 x3widget *x3menuitem(x3widget *parent, char *name, char *cmd, char *shortcut)
311 {
312 CFStringRef cflabel = CFStringCreateWithCString(NULL, name,
313 kCFStringEncodingUTF8);
314 MenuItemIndex index;
315
316 AppendMenuItemTextWithCFString(parent->u.menu, cflabel,
317 0,
318 x3mkmultichar(cmd), &index);
319 if (shortcut) {
320 UInt16 key;
321 UInt8 mods;
322
323 if (x3parseshortcut(shortcut, &key, &mods)) {
324 SetMenuItemCommandKey(parent->u.menu, index, false, key);
325 SetMenuItemModifiers(parent->u.menu, index, mods);
326 }
327 }
328 CFRelease(cflabel);
329 return x3widget_new_menuitem(parent, &x3menuitemtype, index);
330 }
331
332 x3widget *x3menusep(x3widget *parent)
333 {
334 Str255 str = {1, '-'};
335
336 AppendMenu(parent->u.menu, str);
337 return x3widget_new_menuitem(parent, &x3menuitemtype, -1);
338 }
339
340 void x3button_sizereq(x3widget *w)
341 {
342 SInt16 offset;
343 Rect r = { 0, 0, 0, 0 };
344
345 GetBestControlRect(w->u.hiview, &r, &offset);
346 w->sizerequest.x0 = r.left;
347 w->sizerequest.y0 = r.top;
348 w->sizerequest.x1 = r.right;
349 w->sizerequest.y1 = r.bottom;
350 #ifdef VERBOSE
351 printf("button sizereq = (%g, %g) - (%g, %g)\n",
352 w->sizerequest.x0, w->sizerequest.y0,
353 w->sizerequest.x1, w->sizerequest.y1);
354 #endif
355 }
356
357 void x3button_sizealloc(x3widget *w, x3rect *r)
358 {
359 Rect bounds;
360
361 bounds.left = r->x0;
362 bounds.top = r->y0;
363 bounds.right = r->x1;
364 bounds.bottom = r->y1;
365 /* TODO probably want to use HIViewSetFrame instead */
366 printf("button sizealloc = (%g, %g) - (%g, %g)\n",
367 r->x0, r->y0, r->x1, r->y1);
368 SetControlBounds(w->u.hiview, &bounds);
369 }
370
371 x3type x3buttontype = { x3button_sizereq,
372 x3button_sizealloc,
373 x3add_default };
374
375 x3widget *x3button(x3widget *parent, char *cmd, char *label)
376 {
377 WindowRef window = x3window_of(parent);
378 Rect r = {10, 10, 30, 100};
379 ControlRef control;
380 OSStatus err;
381 CFStringRef cflabel = CFStringCreateWithCString(NULL, label,
382 kCFStringEncodingUTF8);
383
384 err = CreatePushButtonControl(window, &r, cflabel, &control);
385 CFRelease(cflabel);
386 SetControlCommandID(control, x3mkmultichar(cmd));
387 //SetWindowDefaultButton(window, control);
388 return x3widget_new_hiview(parent, cmd, &x3buttontype, control);
389 }
390
391 x3widget *x3label(x3widget *parent, char *text)
392 {
393 WindowRef window = x3window_of(parent);
394 Rect r = {10, 10, 30, 100};
395 ControlRef control;
396 OSStatus err;
397 Boolean singleline = true;
398 CFStringRef cftext = CFStringCreateWithCString(NULL, text,
399 kCFStringEncodingUTF8);
400
401 err = CreateStaticTextControl(window, &r, cftext,
402 NULL, &control);
403 CFRelease(cftext);
404 #if 0
405 SetControlData(control, kControlEntireControl,
406 kControlEditTextSingleLineTag, sizeof(Boolean),
407 &singleline);
408 #endif
409 return x3widget_new_hiview(parent, NULL, &x3buttontype, control);
410 }
411
412 x3widget *x3edittext(x3widget *parent, char *cmd)
413 {
414 WindowRef window = x3window_of(parent);
415 Rect r = {10, 10, 30, 100};
416 ControlRef control;
417 OSStatus err;
418 Boolean singleline = true;
419
420 err = CreateEditUnicodeTextControl(window, &r, CFSTR(""),
421 false, NULL, &control);
422 SetControlCommandID(control, x3mkmultichar(cmd));
423 #if 0
424 SetControlData(control, kControlEntireControl,
425 kControlEditTextSingleLineTag, sizeof(Boolean),
426 &singleline);
427 #endif
428 return x3widget_new_hiview(parent, cmd, &x3buttontype, control);
429 }
430
431 x3widget *x3hpane(x3widget *parent)
432 {
433 return NULL;
434 }
435
436 x3widget *x3vpane(x3widget *parent)
437 {
438 return NULL;
439 }
440
441 typedef struct
442 {
443 HIViewRef view;
444 x3viewflags flags;
445 x3viewclient *vc;
446 } x3view_data;
447
448 static OSStatus
449 x3view_construct(EventRef inEvent)
450 {
451 OSStatus err;
452 x3view_data *data;
453
454 data = (x3view_data *)malloc(sizeof(x3view_data));
455 require_action(data != NULL, CantMalloc, err = memFullErr);
456 err = GetEventParameter(inEvent, kEventParamHIObjectInstance,
457 typeHIObjectRef, NULL, sizeof(HIObjectRef), NULL,
458 (HIObjectRef *)&data->view);
459 require_noerr(err, ParameterMissing);
460 err = SetEventParameter(inEvent, kEventParamHIObjectInstance,
461 typeVoidPtr, sizeof(x3view_data *), &data);
462
463 data->vc = NULL;
464
465 ParameterMissing:
466 if (err != noErr)
467 free(data);
468
469 CantMalloc:
470 return err;
471 }
472
473 static OSStatus
474 x3view_destruct(EventRef inEvent, x3view_data *inData)
475 {
476 free(inData);
477 return noErr;
478 }
479
480 static OSStatus
481 x3view_initialize(EventHandlerCallRef inCallRef, EventRef inEvent,
482 x3view_data *inData)
483 {
484 OSStatus err;
485 HIRect bounds;
486
487 err = CallNextEventHandler(inCallRef, inEvent);
488 require_noerr(err, TroubleInSuperClass);
489
490 err = GetEventParameter(inEvent, 'Boun', typeHIRect,
491 NULL, sizeof(HIRect), NULL, &bounds);
492 require_noerr(err, ParameterMissing);
493
494 HIViewSetFrame(inData->view, &bounds);
495
496 ParameterMissing:
497 TroubleInSuperClass:
498 return err;
499 }
500
501 static OSStatus
502 x3view_draw(EventRef inEvent, x3view_data *inData)
503 {
504 OSStatus err;
505 CGContextRef ctx;
506
507 err = GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef,
508 NULL, sizeof(CGContextRef), NULL, &ctx);
509 require_noerr(err, ParameterMissing);
510
511 #ifdef VERBOSE
512 printf("x3view_draw!\n");
513 #endif
514
515 if (inData->vc && inData->vc->draw) {
516 x3dc dc;
517
518 /* set up bounds */
519 if (inData->flags & x3view_2d) {
520 dc.ctx = ctx;
521 dc.path = NULL;
522 dc.buf = NULL;
523
524 inData->vc->draw(inData->vc, &dc);
525 if (dc.path) {
526 CGPathRelease(dc.path);
527 }
528 } else if (inData->flags & x3view_rgb) {
529 /* todo */
530 }
531 }
532
533 ParameterMissing:
534 return err;
535 }
536
537 static OSStatus
538 x3view_get_data(EventRef inEvent, x3view_data *inData)
539 {
540 OSStatus err;
541 OSType tag;
542 Ptr ptr;
543 Size outSize;
544
545 /* Probably could use a bit more error checking here, for type
546 and size match. Also, just returning an x3view_data seems a
547 little hacky. */
548 err = GetEventParameter(inEvent, kEventParamControlDataTag, typeEnumeration,
549 NULL, sizeof(OSType), NULL, &tag);
550 require_noerr(err, ParameterMissing);
551
552 err = GetEventParameter(inEvent, kEventParamControlDataBuffer, typePtr,
553 NULL, sizeof(Ptr), NULL, &ptr);
554
555 if (tag == kX3ViewPrivate) {
556 *((x3view_data **)ptr) = inData;
557 outSize = sizeof(x3view_data *);
558 } else
559 err = errDataNotSupported;
560
561 if (err == noErr)
562 err = SetEventParameter(inEvent, kEventParamControlDataBufferSize, typeLongInteger,
563 sizeof(Size), &outSize);
564
565 ParameterMissing:
566 return err;
567 }
568
569 static OSStatus
570 x3view_set_data(EventRef inEvent, x3view_data *inData)
571 {
572 OSStatus err;
573 Ptr ptr;
574 OSType tag;
575
576 err = GetEventParameter(inEvent, kEventParamControlDataTag, typeEnumeration,
577 NULL, sizeof(OSType), NULL, &tag);
578 require_noerr(err, ParameterMissing);
579
580 err = GetEventParameter(inEvent, kEventParamControlDataBuffer, typePtr,
581 NULL, sizeof(Ptr), NULL, &ptr);
582 require_noerr(err, ParameterMissing);
583
584 if (tag == 'X3vc') {
585 inData->vc = *(x3viewclient **)ptr;
586 } else if (tag == 'X3vf') {
587 inData->flags = *(x3viewflags *)ptr;
588 } else
589 err = errDataNotSupported;
590
591 ParameterMissing:
592 return err;
593 }
594
595 static OSStatus
596 x3view_hittest(EventRef inEvent, x3view_data *inData)
597 {
598 OSStatus err;
599 HIPoint where;
600 HIRect bounds;
601 ControlPartCode part;
602
603 err = GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint,
604 NULL, sizeof(HIPoint), NULL, &where);
605 require_noerr(err, ParameterMissing);
606
607 err = HIViewGetBounds(inData->view, &bounds);
608 require_noerr(err, ParameterMissing);
609
610 if (CGRectContainsPoint(bounds, where))
611 part = 1;
612 else
613 part = kControlNoPart;
614 err = SetEventParameter(inEvent, kEventParamControlPart,
615 typeControlPartCode, sizeof(ControlPartCode),
616 &part);
617 printf("hittest %g, %g!\n", where.x, where.y);
618
619 ParameterMissing:
620 return err;
621 }
622
623 /*
624 If we need more sophisticated tracking (like mixing key events), there's
625 a good discussion here:
626 http://lists.apple.com/archives/Carbon-development/2001/Apr/msg01688.html
627 */
628 static OSStatus
629 x3view_track(EventRef inEvent, x3view_data *inData)
630 {
631 OSStatus err;
632 HIPoint where;
633 MouseTrackingResult mouseStatus;
634 HIRect bounds;
635 Rect windBounds;
636 Point theQDPoint;
637 UInt32 mods;
638
639 err = GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint,
640 NULL, sizeof(HIPoint), NULL, &where);
641 require_noerr(err, ParameterMissing);
642
643 err = GetEventParameter(inEvent, kEventParamKeyModifiers, typeUInt32,
644 NULL, sizeof(UInt32), NULL, &mods);
645 require_noerr(err, ParameterMissing);
646
647 err = HIViewGetBounds(inData->view, &bounds);
648 require_noerr(err, ParameterMissing);
649
650 GetWindowBounds(GetControlOwner(inData->view), kWindowStructureRgn, &windBounds);
651
652 if (inData->vc && inData->vc->mouse)
653 inData->vc->mouse(inData->vc, 1, mods, where.x, where.y);
654
655 #ifdef VERBOSE
656 printf("press: %g, %g!\n", where.x, where.y);
657 #endif
658 //pe_view_button_press(inData, where.x, where.y, 0);
659
660 mouseStatus = kMouseTrackingMouseDown;
661 while (mouseStatus != kMouseTrackingMouseUp) {
662 TrackMouseLocation(NULL, &theQDPoint, &mouseStatus);
663 where.x = theQDPoint.h - windBounds.left;
664 where.y = theQDPoint.v - windBounds.top;
665 HIViewConvertPoint(&where, NULL, inData->view);
666 #ifdef VERBOSE
667 printf("track %d: %g, %g!\n", mouseStatus, where.x, where.y);
668 #endif
669 if (mouseStatus == kMouseTrackingMouseUp) {
670 if (inData->vc && inData->vc->mouse)
671 inData->vc->mouse(inData->vc, -1, mods, where.x, where.y);
672 } else if (mouseStatus == kMouseTrackingKeyModifiersChanged) {
673 mods = GetCurrentEventKeyModifiers();
674 if (inData->vc && inData->vc->mouse)
675 inData->vc->mouse(inData->vc, 0, mods, where.x, where.y);
676 } else {
677 if (inData->vc && inData->vc->mouse)
678 inData->vc->mouse(inData->vc, 0, mods, where.x, where.y);
679 }
680 }
681
682 ParameterMissing:
683 return err;
684 }
685
686 pascal OSStatus
687 x3view_handler(EventHandlerCallRef inCallRef,
688 EventRef inEvent,
689 void* inUserData )
690 {
691 OSStatus err = eventNotHandledErr;
692 UInt32 eventClass = GetEventClass(inEvent);
693 UInt32 eventKind = GetEventKind(inEvent);
694 x3view_data *data = (x3view_data *)inUserData;
695
696 printf("view handler %c%c%c%c %d\n",
697 (eventClass >> 24) & 0xff,
698 (eventClass >> 16) & 0xff,
699 (eventClass >> 8) & 0xff,
700 (eventClass >> 0) & 0xff,
701 eventKind);
702 switch (eventClass) {
703 case kEventClassHIObject:
704 switch (eventKind) {
705 case kEventHIObjectConstruct:
706 err = x3view_construct(inEvent);
707 break;
708 case kEventHIObjectInitialize:
709 err = x3view_initialize(inCallRef, inEvent, data);
710 break;
711 case kEventHIObjectDestruct:
712 err = x3view_destruct(inEvent, data);
713 break;
714 }
715 break;
716 case kEventClassControl:
717 switch (eventKind) {
718 case kEventControlInitialize:
719 err = noErr;
720 break;
721 case kEventControlDraw:
722 err = x3view_draw(inEvent, data);
723 break;
724 case kEventControlGetData:
725 err = x3view_get_data(inEvent, data);
726 break;
727 case kEventControlSetData:
728 err = x3view_set_data(inEvent, data);
729 break;
730 case kEventControlTrack:
731 err = x3view_track(inEvent, data);
732 break;
733 case kEventControlHitTest:
734 err = x3view_hittest(inEvent, data);
735 break;
736 case kEventControlClick:
737 printf("click event\n");
738 break;
739 /*...*/
740 }
741 break;
742 }
743 return err;
744 }
745
746 static OSStatus
747 x3view_register(void)
748 {
749 OSStatus err = noErr;
750 static HIObjectClassRef x3view_ClassRef = NULL;
751
752 if (x3view_ClassRef == NULL) {
753 EventTypeSpec eventList[] = {
754 { kEventClassHIObject, kEventHIObjectConstruct },
755 { kEventClassHIObject, kEventHIObjectInitialize },
756 { kEventClassHIObject, kEventHIObjectDestruct },
757
758 { kEventClassControl, kEventControlActivate },
759 { kEventClassControl, kEventControlDeactivate },
760 { kEventClassControl, kEventControlDraw },
761 { kEventClassControl, kEventControlHiliteChanged },
762 { kEventClassControl, kEventControlHitTest },
763 { kEventClassControl, kEventControlInitialize },
764 { kEventClassControl, kEventControlGetData },
765 { kEventClassControl, kEventControlSetData },
766 { kEventClassControl, kEventControlTrack },
767 { kEventClassControl, kEventControlClick }
768 };
769 err = HIObjectRegisterSubclass(kX3ViewClassID,
770 kHIViewClassID,
771 0,
772 x3view_handler,
773 GetEventTypeCount(eventList),
774 eventList,
775 NULL,
776 &x3view_ClassRef);
777 }
778 return err;
779 }
780
781 OSStatus x3view_create(
782 WindowRef inWindow,
783 const HIRect* inBounds,
784 HIViewRef* outView)
785 {
786 OSStatus err;
787 EventRef event;
788
789 err = x3view_register();
790 require_noerr(err, CantRegister);
791
792 err = CreateEvent(NULL, kEventClassHIObject, kEventHIObjectInitialize,
793 GetCurrentEventTime(), 0, &event);
794 require_noerr(err, CantCreateEvent);
795
796 if (inBounds != NULL) {
797 err = SetEventParameter(event, 'Boun', typeHIRect, sizeof(HIRect),
798 inBounds);
799 require_noerr(err, CantSetParameter);
800 }
801
802 err = HIObjectCreate(kX3ViewClassID, event, (HIObjectRef*)outView);
803 require_noerr(err, CantCreate);
804
805 if (inWindow != NULL) {
806 HIViewRef root;
807 err = GetRootControl(inWindow, &root);
808 require_noerr(err, CantGetRootView);
809 err = HIViewAddSubview(root, *outView);
810 }
811 CantCreate:
812 CantGetRootView:
813 CantSetParameter:
814 CantCreateEvent:
815 ReleaseEvent(event);
816 CantRegister:
817 return err;
818 }
819
820 x3widget *x3view(x3widget *parent, x3viewflags flags, x3viewclient *vc)
821 {
822 WindowRef window = x3window_of(parent);
823 HIRect r = { {10, 10}, {30, 100} };
824 OSStatus err;
825 HIViewRef view;
826
827 err = x3view_create(window, &r, &view);
828 err = SetControlData(view, 1, 'X3vc', sizeof(x3view_data *), &vc);
829 err = SetControlData(view, 1, 'X3vf', sizeof(x3viewflags), &flags);
830 HIViewSetVisible(view, true);
831 return x3widget_new_hiview(parent, NULL, &x3buttontype, view);
832 }
833
834 void
835 x3view_dirty(x3widget *w)
836 {
837 if (w->var == x3carbonhiview)
838 HIViewSetNeedsDisplay(w->u.hiview, true);
839 }
840
841 void x3view_scrollto(x3widget *w, int x, int y, int width, int height)
842 {
843 /* todo */
844 }
845
846 void x3viewclient_init(x3viewclient *vc)
847 {
848 vc->destroy = NULL;
849 vc->mouse = NULL;
850 vc->key = NULL;
851 vc->draw = NULL;
852 }
853
854 /* Functions for manipulating widget state - some fairly polymorphic. */
855
856 void x3setactive(x3widget *w, int active)
857 {
858 if (w->var == x3carbonmenuitem) {
859 MenuRef menu = w->parent->u.menu;
860 if (active)
861 EnableMenuItem(menu, w->u.menuitem);
862 else
863 DisableMenuItem(menu, w->u.menuitem);
864 /* According to Carbon docs, we need to redraw menu. */
865 } else if (w->var == x3carbonhiview) {
866 if (active)
867 ActivateControl(w->u.hiview);
868 else
869 DeactivateControl(w->u.hiview);
870 }
871 }
872
873 int x3hasfocus(x3widget *w)
874 {
875 if (w->var == x3carbonhiview) {
876 return HIViewSubtreeContainsFocus(w->u.hiview);
877 } else
878 return 0;
879 }
880
881 /* 2d drawing functions, implemented using Quartz */
882
883 void
884 x3moveto(x3dc *dc, double x, double y)
885 {
886 if (dc->path == NULL) {
887 dc->path = CGPathCreateMutable();
888 }
889 CGPathMoveToPoint(dc->path, NULL, x, y);
890 }
891
892 void
893 x3lineto(x3dc *dc, double x, double y)
894 {
895 CGPathAddLineToPoint(dc->path, NULL, x, y);
896 }
897
898 void
899 x3curveto(x3dc *dc,
900 double x1, double y1,
901 double x2, double y2,
902 double x3, double y3)
903 {
904 CGPathAddCurveToPoint(dc->path, NULL, x1, y1, x2, y2, x3, y3);
905 }
906
907 void
908 x3closepath(x3dc *dc)
909 {
910 CGPathCloseSubpath(dc->path);
911 }
912
913 void
914 x3rectangle(x3dc *dc, double x, double y, double width, double height)
915 {
916 CGRect rect;
917
918 if (dc->path == NULL) {
919 dc->path = CGPathCreateMutable();
920 }
921 rect.origin.x = x;
922 rect.origin.y = y;
923 rect.size.width = width;
924 rect.size.height = height;
925 CGPathAddRect(dc->path, NULL, rect);
926 }
927
928 void
929 x3getcurrentpoint(x3dc *dc, double *px, double *py)
930 {
931 CGPoint point = CGPathGetCurrentPoint(dc->path);
932
933 *px = point.x;
934 *py = point.y;
935 }
936
937 void
938 x3setrgba(x3dc *dc, unsigned int rgba)
939 {
940 CGContextSetRGBFillColor(dc->ctx,
941 ((rgba >> 24) & 0xff) * (1.0/255),
942 ((rgba >> 16) & 0xff) * (1.0/255),
943 ((rgba >> 8) & 0xff) * (1.0/255),
944 (rgba & 0xff) * (1.0/255));
945 CGContextSetRGBStrokeColor(dc->ctx,
946 ((rgba >> 24) & 0xff) * (1.0/255),
947 ((rgba >> 16) & 0xff) * (1.0/255),
948 ((rgba >> 8) & 0xff) * (1.0/255),
949 (rgba & 0xff) * (1.0/255));
950 }
951
952 void
953 x3setlinewidth(x3dc *dc, double w)
954 {
955 CGContextSetLineWidth(dc->ctx, w);
956 }
957
958 void
959 x3fill(x3dc *dc)
960 {
961 CGContextAddPath(dc->ctx, dc->path);
962 CGPathRelease(dc->path);
963 dc->path = NULL;
964 CGContextFillPath(dc->ctx);
965 }
966
967 void
968 x3stroke(x3dc *dc)
969 {
970 CGContextAddPath(dc->ctx, dc->path);
971 CGPathRelease(dc->path);
972 dc->path = NULL;
973 CGContextStrokePath(dc->ctx);
974 }
975
976 void
977 x3selectfont(x3dc *dc, char *fontname, int slant, int weight)
978 {
979 CGContextSelectFont(dc->ctx, fontname, 8.0, kCGEncodingMacRoman);
980 }
981
982 void
983 x3setfontsize(x3dc *dc, double size)
984 {
985 CGContextSetFontSize(dc->ctx, size);
986 }
987
988 void
989 x3showtext(x3dc *dc, char *text)
990 {
991 CGPoint point = CGPathGetCurrentPoint(dc->path);
992 CGAffineTransform textmat;
993
994 textmat = CGAffineTransformMakeScale(1, -1);
995 CGContextSetTextMatrix(dc->ctx, textmat);
996 CGContextShowTextAtPoint(dc->ctx, point.x, point.y, text, strlen(text));
997 }
998
999 void x3textextents(x3dc *dc, char *text, x3extents *extents)
1000 {
1001 /* todo */
1002 }