summaryrefslogtreecommitdiff
path: root/third_party/spiro/ppedit/pe_view.c
blob: 9909680aa5556b6c7e4e9dc4c77ba04b318874d6 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
ppedit - A pattern plate editor for Spiro splines.
Copyright (C) 2007 Raph Levien

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

*/
/* This module implements a Carbon HIView object for a pattern plate
   editor. */

#include <Carbon/Carbon.h>

#include "bezctx.h"
#include "bezctx_quartz.h"
#include "plate.h"
#include "pe_view.h"

#define kPEViewClassID CFSTR("com.levien.ppedit.PEView")
#define kPEViewPrivate 'PE_v'

typedef struct
{
    HIViewRef view;
    int show_knots;
    plate *p;
} pe_view_data;

static OSStatus
pe_view_construct(EventRef inEvent)
{
    OSStatus err;
    pe_view_data *data;

    data = (pe_view_data *)malloc(sizeof(pe_view_data));
    require_action(data != NULL, CantMalloc, err = memFullErr);
    err = GetEventParameter(inEvent, kEventParamHIObjectInstance,
			    typeHIObjectRef, NULL, sizeof(HIObjectRef), NULL,
			    (HIObjectRef *)&data->view);
    require_noerr(err, ParameterMissing);
    err = SetEventParameter(inEvent, kEventParamHIObjectInstance,
			    typeVoidPtr, sizeof(pe_view_data *), &data);

    data->p = NULL;
    data->show_knots = 1;

 ParameterMissing:
    if (err != noErr)
	free(data);

 CantMalloc:
    return err;
}

static OSStatus
pe_view_destruct(EventRef inEvent, pe_view_data *inData)
{
    free(inData);
    return noErr;
}

static OSStatus
pe_view_initialize(EventHandlerCallRef inCallRef, EventRef inEvent,
		   pe_view_data *inData)
{
    OSStatus err;
    HIRect bounds;

    err = CallNextEventHandler(inCallRef, inEvent);
    require_noerr(err, TroubleInSuperClass);

    err = GetEventParameter(inEvent, 'Boun', typeHIRect,
			    NULL, sizeof(HIRect), NULL, &bounds);
    require_noerr(err, ParameterMissing);

    HIViewSetFrame(inData->view, &bounds);

 ParameterMissing:
 TroubleInSuperClass:
    return err;
}

#ifndef M_PI
#define M_PI            3.14159265358979323846  /* pi */
#endif

static void
cgcontext_set_rgba(CGContextRef ctx, unsigned int rgba)
{
    const double norm = 1.0 / 255;
    CGContextSetRGBFillColor(ctx,
			     ((rgba >> 24) & 0xff) * norm,
			     ((rgba >> 16) & 0xff) * norm,
			     ((rgba >> 8) & 0xff) * norm,
			     (rgba & 0xff) * norm);
}

static void
draw_dot(CGContextRef ctx, double x, double y, double r,
	 unsigned int rgba)
{
    cgcontext_set_rgba(ctx, rgba);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddArc(path, NULL, x, y, r, 0, 2 * M_PI, false);
    CGContextAddPath(ctx, path);
    CGPathRelease(path);
    CGContextFillPath(ctx);
}

static void
draw_raw_rect(CGContextRef ctx, double x0, double y0, double x1, double y1,
	      unsigned int rgba)
{
    HIRect rect;

    cgcontext_set_rgba(ctx, rgba);
    rect.origin.x = x0;
    rect.origin.y = y0;
    rect.size.width = x1 - x0;
    rect.size.height = y1 - y0;
    CGContextFillRect(ctx, rect);
}

static void
draw_rect(CGContextRef ctx, double x, double y, double r,
	 unsigned int rgba)
{
    draw_raw_rect(ctx, x - r, y - r, x + r, y + r, rgba);
}

static void
draw_plate(CGContextRef ctx, pe_view_data *pe)
{
    plate *p = pe->p;
    int i, j;
    bezctx *bc;
    subpath *sp;
    spiro_seg **ss = znew(spiro_seg *, p->n_sp);

    for (i = 0; i < p->n_sp; i++) {
	bc = new_bezctx_quartz();
	ss[i] = draw_subpath(&p->sp[i], bc);
	CGMutablePathRef path = bezctx_to_quartz(bc);
	CGContextAddPath(ctx, path);
	CGPathRelease(path);
	CGContextStrokePath(ctx);
    }

    for (i = 0; i < p->n_sp; i++) {
	if (pe->show_knots) {
	    sp = &p->sp[i];
	    for (j = 0; j < sp->n_kt; j++) {
		knot *kt = &sp->kt[j];
		kt_flags kf = kt->flags;
		if ((kf & KT_SELECTED) && (kf & KT_OPEN)) {
		    draw_dot(ctx, kt->x, kt->y,
			     3, 0x000000ff);
		    draw_dot(ctx, kt->x, kt->y,
			     1.5, 0xffffffff);
		} else if ((kf & KT_SELECTED) && (kf & KT_CORNER)) {
		    draw_rect(ctx, kt->x, kt->y,
			      3, 0x000000ff);
		    draw_rect(ctx, kt->x, kt->y,
			      1.5, 0xffffffff);
		} else if (!(kf & KT_SELECTED) && (kf & KT_CORNER)) {
		    draw_rect(ctx, kt->x, kt->y,
			      2.5, 0x000080ff);
		} else {
		    draw_dot(ctx, kt->x, kt->y,
			     2, 0x000080ff);
		}
	    }
	}
	spiro_free(ss[i]);
    }
    zfree(ss);
}

static void
draw_selection(CGContextRef ctx, pe_view_data *pe)
{
    plate *p = pe->p;

    if (p->motmode == MOTION_MODE_SELECT) {
	double rx0 = p->sel_x0;
	double ry0 = p->sel_y0;
	double rx1 = p->x0;
	double ry1 = p->y0;
	if (rx0 > rx1) {
	    double tmp = rx1;
	    rx1 = rx0;
	    rx0 = tmp;
	}
	if (ry0 > ry1) {
	    double tmp = ry1;
	    ry1 = ry0;
	    ry0 = tmp;
	}
	if (rx1 > rx0 && ry1 > ry0)
	    draw_raw_rect(ctx, rx0, ry0, rx1, ry1, 0x0000ff20);
    }
}

static OSStatus
pe_view_draw(EventRef inEvent, pe_view_data *inData)
{
    OSStatus err;
    CGContextRef ctx;

    err = GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef,
			    NULL, sizeof(CGContextRef), NULL, &ctx);
    require_noerr(err, ParameterMissing);

    if (inData->p) {
	draw_plate(ctx, inData);
	draw_selection(ctx, inData);
    }

 ParameterMissing:
    return err;
}

static OSStatus
pe_view_get_data(EventRef inEvent, pe_view_data *inData)
{
    OSStatus err;
    OSType tag;
    Ptr ptr;
    Size outSize;

    /* Probably could use a bit more error checking here, for type
       and size match. Also, just returning a pe_view_data seems a
       little hacky. */
    err = GetEventParameter(inEvent, kEventParamControlDataTag, typeEnumeration,
			    NULL, sizeof(OSType), NULL, &tag);
    require_noerr(err, ParameterMissing);

    err = GetEventParameter(inEvent, kEventParamControlDataBuffer, typePtr,
			    NULL, sizeof(Ptr), NULL, &ptr);

    if (tag == kPEViewPrivate) {
	*((pe_view_data **)ptr) = inData;
	outSize = sizeof(pe_view_data *);
    } else
	err = errDataNotSupported;

    if (err == noErr)
	err = SetEventParameter(inEvent, kEventParamControlDataBufferSize, typeLongInteger,
				sizeof(Size), &outSize);

 ParameterMissing:
    return err;
}

static OSStatus
pe_view_set_data(EventRef inEvent, pe_view_data *inData)
{
    OSStatus err;
    Ptr ptr;
    OSType tag;

    err = GetEventParameter(inEvent, kEventParamControlDataTag, typeEnumeration,
			    NULL, sizeof(OSType), NULL, &tag);
    require_noerr(err, ParameterMissing);

    err = GetEventParameter(inEvent, kEventParamControlDataBuffer, typePtr,
			    NULL, sizeof(Ptr), NULL, &ptr);
    require_noerr(err, ParameterMissing);

    if (tag == 'Plat') {
	inData->p = *(plate **)ptr;
    } else
	err = errDataNotSupported;

 ParameterMissing:
    return err;
}

static OSStatus
pe_view_hittest(EventRef inEvent, pe_view_data *inData)
{
    OSStatus err;
    HIPoint where;
    HIRect bounds;
    ControlPartCode part;

    err = GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint,
			    NULL, sizeof(HIPoint), NULL, &where);
    require_noerr(err, ParameterMissing);

    err = HIViewGetBounds(inData->view, &bounds);
    require_noerr(err, ParameterMissing);

    if (CGRectContainsPoint(bounds, where))
	part = 1;
    else
	part = kControlNoPart;
    err = SetEventParameter(inEvent, kEventParamControlPart,
			    typeControlPartCode, sizeof(ControlPartCode),
			    &part);
    printf("hittest %g, %g!\n", where.x, where.y);

 ParameterMissing:
    return err;
}

static void
pe_view_queue_draw(pe_view_data *pe)
{
    HIViewSetNeedsDisplay(pe->view, true);
}

static int
pe_view_button_press(pe_view_data *pe, double x, double y, press_mod mods)
{
    pe->p->description = NULL;
    plate_press(pe->p, x, y, mods);
    pe_view_queue_draw(pe);
    return 1;
}

static int
pe_view_motion(pe_view_data *pe, double x, double y)
{
    if (pe->p->motmode == MOTION_MODE_MOVE)
	plate_motion_move(pe->p, x, y);
    else if (pe->p->motmode == MOTION_MODE_SELECT)
	plate_motion_select(pe->p, x, y);
    pe_view_queue_draw(pe);
    return 1;
}

static int
pe_view_button_release(pe_view_data *pe)
{
    int need_redraw;
    
    need_redraw = (pe->p->motmode == MOTION_MODE_SELECT);

    plate_unpress(pe->p);

    if (need_redraw)
	pe_view_queue_draw(pe);
    return 1;
}

static OSStatus
pe_view_track(EventRef inEvent, pe_view_data *inData)
{
    OSStatus err;
    HIPoint where;
    MouseTrackingResult mouseStatus;
    HIRect bounds;
    Rect windBounds;
    Point theQDPoint;

    err = GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint,
			    NULL, sizeof(HIPoint), NULL, &where);
    require_noerr(err, ParameterMissing);

    err = HIViewGetBounds(inData->view, &bounds);
    require_noerr(err, ParameterMissing);

    GetWindowBounds(GetControlOwner(inData->view), kWindowStructureRgn, &windBounds);

#ifdef VERBOSE
    printf("press: %g, %g!\n", where.x, where.y);
#endif
    pe_view_button_press(inData, where.x, where.y, 0);

    mouseStatus = kMouseTrackingMouseDown;
    while (mouseStatus != kMouseTrackingMouseUp) {
	TrackMouseLocation(NULL, &theQDPoint, &mouseStatus);
	where.x = theQDPoint.h - windBounds.left;
	where.y = theQDPoint.v - windBounds.top;
	HIViewConvertPoint(&where, NULL, inData->view);
#ifdef VERBOSE
	printf("track %d: %g, %g!\n", mouseStatus, where.x, where.y);
#endif
	if (mouseStatus == kMouseTrackingMouseUp) {
	    pe_view_button_release(inData);
	} else {
	    pe_view_motion(inData, where.x, where.y);
	}
    }

 ParameterMissing:
    return err;
}

pascal OSStatus
pe_view_handler(EventHandlerCallRef inCallRef,
		EventRef inEvent,
		void* inUserData )
{
    OSStatus err = eventNotHandledErr;
    UInt32 eventClass = GetEventClass(inEvent);
    UInt32 eventKind = GetEventKind(inEvent);
    pe_view_data *data = (pe_view_data *)inUserData;

    switch (eventClass) {
    case kEventClassHIObject:
	switch (eventKind) {
	case kEventHIObjectConstruct:
	    err = pe_view_construct(inEvent);
	    break;
	case kEventHIObjectInitialize:
	    err = pe_view_initialize(inCallRef, inEvent, data);
	    break;
	case kEventHIObjectDestruct:
	    err = pe_view_destruct(inEvent, data);
	    break;
	}
	break;
    case kEventClassControl:
	switch (eventKind) {
	case kEventControlInitialize:
	    err = noErr;
	    break;
	case kEventControlDraw:
	    err = pe_view_draw(inEvent, data);
	    break;
	case kEventControlGetData:
	    err = pe_view_get_data(inEvent, data);
	    break;
	case kEventControlSetData:
	    err = pe_view_set_data(inEvent, data);
	    break;
	case kEventControlTrack:
	    err = pe_view_track(inEvent, data);
	    break;
	case kEventControlHitTest:
	    err = pe_view_hittest(inEvent, data);
	    break;
	    /*...*/
	}
	break;
    }
    return err;
}

static OSStatus
pe_view_register(void)
{
    OSStatus err = noErr;
    static HIObjectClassRef pe_view_ClassRef = NULL;

    if (pe_view_ClassRef == NULL) {
	EventTypeSpec eventList[] = {
	    { kEventClassHIObject, kEventHIObjectConstruct },
	    { kEventClassHIObject, kEventHIObjectInitialize },
	    { kEventClassHIObject, kEventHIObjectDestruct },

	    { kEventClassControl, kEventControlActivate },
	    { kEventClassControl, kEventControlDeactivate },
	    { kEventClassControl, kEventControlDraw },
	    { kEventClassControl, kEventControlHiliteChanged },
	    { kEventClassControl, kEventControlHitTest },
	    { kEventClassControl, kEventControlInitialize },
	    { kEventClassControl, kEventControlGetData },
	    { kEventClassControl, kEventControlSetData },
	    { kEventClassControl, kEventControlTrack }
	};
	err = HIObjectRegisterSubclass(kPEViewClassID,
				       kHIViewClassID,
				       NULL,
				       pe_view_handler,
				       GetEventTypeCount(eventList),
				       eventList,
				       NULL,
				       &pe_view_ClassRef);
    }
    return err;
}

OSStatus pe_view_create(
	WindowRef			inWindow,
	const HIRect*		inBounds,
	HIViewRef*			outView)
{
    OSStatus err;
    EventRef event;

    err = pe_view_register();
    require_noerr(err, CantRegister);

    err = CreateEvent(NULL, kEventClassHIObject, kEventHIObjectInitialize,
		      GetCurrentEventTime(), 0, &event);
    require_noerr(err, CantCreateEvent);

    if (inBounds != NULL) {
	err = SetEventParameter(event, 'Boun', typeHIRect, sizeof(HIRect),
				inBounds);
	require_noerr(err, CantSetParameter);
    }

    err = HIObjectCreate(kPEViewClassID, event, (HIObjectRef*)outView);
    require_noerr(err, CantCreate);

    if (inWindow != NULL) {
	HIViewRef root;
	err = GetRootControl(inWindow, &root);
	require_noerr(err, CantGetRootView);
	err = HIViewAddSubview(root, *outView);
    }
 CantCreate:
 CantGetRootView:
 CantSetParameter:
 CantCreateEvent:
    ReleaseEvent(event);
 CantRegister:
    return err;
}

void
pe_view_set_plate(HIViewRef view, plate *p)
{
    OSStatus err;

    err = SetControlData(view, 1, 'Plat', 4, &p);
}

void
pe_view_toggle_corner(HIViewRef view)
{
    OSStatus err;
    pe_view_data *pe;

    err = GetControlData(view, 1, kPEViewPrivate, 4, &pe, NULL);
    require_noerr(err, CantGetPrivate);

    plate_toggle_corner(pe->p);
    pe_view_queue_draw(pe);

 CantGetPrivate:
}