-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathview2DPaintingCb.c
674 lines (577 loc) · 16.8 KB
/
view2DPaintingCb.c
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#if defined(__GNUC__)
#ident "University of Edinburgh $Id$"
#else
static char _view2DPaintingCb_c[] = "University of Edinburgh $Id$";
#endif
/*!
* \file view2DPaintingCb.c
* \author Richard Baldock
* \date April 2009
* \version $Id$
* \par
* Address:
* MRC Human Genetics Unit,
* MRC Institute of Genetics and Molecular Medicine,
* University of Edinburgh,
* Western General Hospital,
* Edinburgh, EH4 2XU, UK.
* \par
* Copyright (C), [2012],
* The University Court of the University of Edinburgh,
* Old College, Edinburgh, UK.
*
* 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.
* \ingroup MAPaint
* \brief
*/
#include <stdio.h>
#include <stdlib.h>
#include <MAPaint.h>
#include <HGU_Tablet.h>
/* define tablet events to be different to any
X11 event byt using LASTEvent which is bigger than any true X11 event */
#define TabletButtonPress (ButtonPress+LASTEvent)
#define TabletButtonRelease (ButtonRelease+LASTEvent)
#define TabletMotionNotify (MotionNotify+LASTEvent)
extern void view_canvas_highlight(ThreeDViewStruct *view_struct,
Boolean highlight);
extern void display_pointer_feedback_information(
ThreeDViewStruct *view_struct,
int x,
int y);
extern WlzErrorNum MAPaintEventRemap(
MAPaintContext context,
MAPaintContextMode mode,
XEvent *event);
static int quitPaintingTrigger=0;
void installCurrentPaintTool(
Widget w,
ThreeDViewStruct *view_struct)
{
/* initialise the paint tool */
(*(globals.currentPaintActionInitFunc))(view_struct);
/* install the callback */
XtAddCallback(w, XmNinputCallback, globals.currentPaintActionCbFunc,
(XtPointer) view_struct);
return;
}
void removeCurrentPaintTool(
Widget w,
ThreeDViewStruct *view_struct)
{
/* remove the callback */
XtRemoveCallback(w, XmNinputCallback, globals.currentPaintActionCbFunc,
(XtPointer) view_struct);
/* call the paint tool quit function */
(*(globals.currentPaintActionQuitFunc))(view_struct);
return;
}
void setViewScale(
ThreeDViewStruct *viewStruct,
double newScale,
int fixedX,
int fixedY)
{
WlzThreeDViewStruct *wlzViewStr=viewStruct->wlzViewStr;
Widget menu, widget, scrolledWindow, xScrollBar, yScrollBar;
int oldXSize, oldXMin, oldXMax, oldXVal;
int oldYSize, oldYMin, oldYMax, oldYVal;
int newSize, newMin, newMax, newVal;
int increment, pageIncrement;
int xp;
double a, b;
double mag;
Dimension width, height, widthp, heightp;
/* check scale value and get the widget - should use the scale menu options */
if((menu = XtNameToWidget(viewStruct->controls, "*view_scale"))
== NULL){
return;
}
if( newScale > 12.0 ){
newScale = 16.0;
widget = XtNameToWidget(menu, "*16");
}
else if( newScale > 8.0 ){
newScale = 12.0;
widget = XtNameToWidget(menu, "*12");
}
else if( newScale > 6.0 ){
newScale = 8.0;
widget = XtNameToWidget(menu, "*8");
}
else if( newScale > 4.0 ){
newScale = 6.0;
widget = XtNameToWidget(menu, "*6");
}
else if( newScale > 3.0 ){
newScale = 4.0;
widget = XtNameToWidget(menu, "*4");
}
else if( newScale > 2.0 ){
newScale = 3.0;
widget = XtNameToWidget(menu, "*3");
}
else if( newScale > 1.0 ){
newScale = 2.0;
widget = XtNameToWidget(menu, "*2");
}
else if( newScale < 0.4 ){
newScale = 0.25;
widget = XtNameToWidget(menu, "*0_25");
}
else if( newScale < 0.9 ){
newScale = 0.5;
widget = XtNameToWidget(menu, "*0_5");
}
else {
newScale = 1.0;
widget = XtNameToWidget(menu, "*1");
}
/* if no change then do nothing ! */
if( fabs(newScale - wlzViewStr->scale) < 0.1 ){
return;
}
if( globals.logfileFp ){
char strBuf[48];
sprintf(strBuf, "%f", newScale);
MAPaintLogData("Scale", strBuf, 0, viewStruct->dialog);
}
mag = newScale / wlzViewStr->scale;
/* get old slider and window info */
scrolledWindow = XtNameToWidget(viewStruct->dialog, "*scrolled_window");
XtVaGetValues(scrolledWindow,
XmNhorizontalScrollBar, &xScrollBar,
XmNverticalScrollBar, &yScrollBar,
NULL);
XtVaGetValues(xScrollBar,
XmNsliderSize, &oldXSize,
XmNminimum, &oldXMin,
XmNmaximum, &oldXMax,
XmNvalue, &oldXVal,
NULL);
XtVaGetValues(yScrollBar,
XmNsliderSize, &oldYSize,
XmNminimum, &oldYMin,
XmNmaximum, &oldYMax,
XmNvalue, &oldYVal,
NULL);
XtVaGetValues(viewStruct->canvas,
XmNwidth, &width,
XmNheight, &height,
NULL);
/* test if the fixed point is to be set */
if( fixedX < 0 ){
fixedX = (oldXVal + oldXSize/2) * width / (oldXMax - oldXMin);
}
if( fixedY < 0 ){
fixedY = (oldYVal + oldYSize/2) * height / (oldYMax - oldYMin);
}
/* reset the scale */
/* XtCallCallbacks(widget, XmNactivateCallback, NULL);*/
wlzViewStr->scale = newScale;
widthp = wlzViewStr->maxvals.vtX - wlzViewStr->minvals.vtX + 1;
heightp = wlzViewStr->maxvals.vtY - wlzViewStr->minvals.vtY + 1;
widthp *= wlzViewStr->scale;
heightp *= wlzViewStr->scale;
XtVaSetValues(viewStruct->canvas,
XmNwidth, widthp,
XmNheight, heightp,
NULL);
XtVaSetValues(menu, XmNmenuHistory, widget, NULL);
/* reset the new sliders */
XtVaGetValues(xScrollBar,
XmNsliderSize, &newSize,
XmNminimum, &newMin,
XmNmaximum, &newMax,
XmNincrement, &increment,
XmNpageIncrement, &pageIncrement,
NULL);
a = - ((double) width) / (oldXMax - oldXMin);
b = -a * oldXMin;
xp = fixedX + a * oldXVal + b;
a = - ((double) width) * mag / (newMax - newMin);
b = -a * newMin;
newVal = (xp - mag * fixedX - b) / a;
newVal = WLZ_MIN(newVal, newMax - newSize);
newVal = WLZ_MAX(newVal, newMin);
XmScrollBarSetValues(xScrollBar, newVal, newSize, increment,
pageIncrement, True);
XtVaGetValues(yScrollBar,
XmNsliderSize, &newSize,
XmNminimum, &newMin,
XmNmaximum, &newMax,
XmNincrement, &increment,
XmNpageIncrement, &pageIncrement,
NULL);
a = - ((double) height) / (oldYMax - oldYMin);
b = -a * oldYMin;
xp = fixedY + a * oldYVal + b;
a = - ((double) height) * mag / (newMax - newMin);
b = -a * newMin;
newVal = (xp - mag * fixedY - b) / a;
newVal = WLZ_MIN(newVal, newMax - newSize);
newVal = WLZ_MAX(newVal, newMin);
XmScrollBarSetValues(yScrollBar, newVal, newSize, increment,
pageIncrement, True);
/* redisplay the section */
redisplay_view_cb(viewStruct->canvas, (XtPointer) viewStruct, NULL);
return;
}
void setViewMode(
ThreeDViewStruct *viewStruct,
WlzThreeDViewMode newMode)
{
Widget menu, widget;
/* check mode value and get the widget -
should use the mode menu options */
if((menu = XtNameToWidget(viewStruct->controls, "*view_mode"))
== NULL){
return;
}
switch( newMode ){
default:
case WLZ_UP_IS_UP_MODE:
widget = XtNameToWidget(menu, "*up-is-up");
break;
case WLZ_STATUE_MODE:
widget = XtNameToWidget(menu, "*statue");
break;
case WLZ_ZETA_MODE:
widget = XtNameToWidget(menu, "*absolute");
break;
}
/* if no change then do nothing ! */
/* if( newMode == wlzViewStr->view_mode ){
return;
} */
/* reset the mode */
XtCallCallbacks(widget, XmNactivateCallback, NULL);
XtVaSetValues(menu, XmNmenuHistory, widget, NULL);
return;
}
static int paintLiftFlag=0;
static DomainSelection paintLiftDomain;
static WlzObject *paintLiftObj=NULL;
void cleanDomainsCb(
Widget w,
XtPointer client_data,
XtPointer call_data)
{
ThreeDViewStruct *view_struct = (ThreeDViewStruct *) client_data;
WlzThreeDViewStruct *wlzViewStr= view_struct->wlzViewStr;
WlzObject *obj1;
WlzErrorNum errNum=WLZ_ERR_NONE;
/* some checks */
if( (view_struct == NULL) || (wlzViewStr == NULL) ){
return;
}
/* remove paint from outside of the reference object domain */
/* in paint mode clean current domains */
if( view_struct == paint_key ){
/* push onto undo-stack */
pushUndoDomains(view_struct);
/* find the mask and reset the grey-values */
if((obj1 = WlzDiffDomain(view_struct->view_object,
view_struct->masked_object, &errNum))){
setGreysIncrement(obj1, view_struct);
WlzFreeObj(obj1);
}
}
else {
/* in non-paint mode just re-display */
display_view_cb(w, client_data, call_data);
}
return;
}
void canvasMagPlusCb(
Widget w,
XtPointer client_data,
XtPointer call_data)
{
ThreeDViewStruct *view_struct = (ThreeDViewStruct *) client_data;
WlzThreeDViewStruct *wlzViewStr= view_struct->wlzViewStr;
setViewScale(view_struct, wlzViewStr->scale * 2.0, -1, -1);
return;
}
void canvasMagMinusCb(
Widget w,
XtPointer client_data,
XtPointer call_data)
{
ThreeDViewStruct *view_struct = (ThreeDViewStruct *) client_data;
WlzThreeDViewStruct *wlzViewStr= view_struct->wlzViewStr;
setViewScale(view_struct, wlzViewStr->scale / 2.0, -1, -1);
return;
}
void canvas_2D_painting_cb(
Widget w,
XtPointer client_data,
XtPointer call_data)
{
ThreeDViewStruct *view_struct = (ThreeDViewStruct *) client_data;
WlzThreeDViewStruct *wlzViewStr= view_struct->wlzViewStr;
XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) call_data;
int x, y;
/* check for lock */
if( view_struct->viewLockedFlag ){
return;
}
/* switch on event type */
switch( cbs->event->type ){
case ButtonPress:
/* remap the event */
if( MAPaintEventRemap(MAPAINT_SECT_VIEW_CONTEXT,
MAPAINT_PAINT_MODE, cbs->event) != WLZ_ERR_NONE ){
break;
}
/* note only remap the mouse events not tablet */
case TabletButtonPress:
switch( cbs->event->xbutton.button ){
case Button1:
/* if shift is pressed then increase the magnification
the scrolled window must be set to keep the pointer
position fixed */
switch(cbs->event->xbutton.state & (ShiftMask|ControlMask)){
case ShiftMask: /* magnify */
setViewScale(view_struct, wlzViewStr->scale * 2.0,
cbs->event->xbutton.x, cbs->event->xbutton.y);
break;
/* case ShiftMask|Mod1Mask:
setViewScale(view_struct, wlzViewStr->scale / 2.0,
cbs->event->xbutton.x, cbs->event->xbutton.y);
break;
*/
case ControlMask: /* lift paint */
if( paintLiftFlag ){
break;
}
paintLiftFlag = 1;
if( (paintLiftDomain =
getSelectedDomainType(cbs->event->xbutton.x,
cbs->event->xbutton.y,
view_struct)) < 1 ){
break;
}
paintLiftObj = getSelectedRegion(cbs->event->xbutton.x,
cbs->event->xbutton.y,
view_struct);
paintLiftObj = WlzAssignObject(paintLiftObj, NULL);
/* clear the selected paint object object and redisplay */
if( paintLiftObj ){
setDomainIncrement(paintLiftObj, view_struct, paintLiftDomain, 1);
}
break;
default:
break;
}
break;
case Button2: /* domain and coordinate feedback */
/* if shift is pressed then decrease the magnification
the scrolled window must be set to keep the pointer
position fixed */
if( cbs->event->xbutton.state & ShiftMask ){
setViewScale(view_struct, wlzViewStr->scale / 2.0,
cbs->event->xbutton.x, cbs->event->xbutton.y);
}
else {
x = cbs->event->xbutton.x / wlzViewStr->scale;
y = cbs->event->xbutton.y / wlzViewStr->scale;
display_pointer_feedback_information(view_struct, x, y);
}
break;
case Button3:
quitPaintingTrigger = 1;
break;
default:
break;
}
break;
case ButtonRelease:
case TabletButtonRelease:
switch( cbs->event->xbutton.button ){
case Button1:
if( paintLiftFlag ){
paintLiftFlag = 0;
if( paintLiftObj ){
setDomainIncrement(paintLiftObj, view_struct,
paintLiftDomain, 0);
WlzFreeObj(paintLiftObj);
paintLiftObj = NULL;
}
}
case Button2: /* domain and coordinate feedback */
XtVaSetValues(view_struct->text, XmNvalue, "", NULL);
break;
case Button3: /* release paint selection - autoincrement
if required */
/* check the trigger */
if( !quitPaintingTrigger ){
break;
}
quitPaintingTrigger = 0;
removeCurrentPaintTool(w, view_struct);
paint_key = NULL;
/* clear the undo domains */
clearUndoDomains();
/* clear the section view image */
if( view_struct->view_object ){
WlzFreeObj(view_struct->view_object);
view_struct->view_object = NULL;
}
/* install new domains, update all views */
installViewDomains(view_struct);
/* check for autoincrement here - reclaim the paint key */
if( wlzViewStr->dist <= (wlzViewStr->maxvals.vtZ - 1.0) ){
if( globals.auto_increment ){
paint_key = view_struct;
wlzViewStr->dist += 1.0;
reset_view_struct( view_struct );
display_view_cb(w, (XtPointer) view_struct, call_data);
view_feedback_cb(w, (XtPointer) view_struct, NULL);
getViewDomains(view_struct);
installCurrentPaintTool(w, view_struct);
break;
}
}
/* restart the 3D feedback display */
HGUglwCanvasTbAnimate( globals.canvas );
XtSetSensitive( globals.canvas, True );
/* make controls sensitive */
XtSetSensitive(view_struct->controls, True);
XtSetSensitive(view_struct->slider, True);
setControlButtonsSensitive(view_struct, True);
/* unhighlight this canvas */
view_canvas_highlight( view_struct, False );
/* swap the callbacks to standard input mode */
XtRemoveCallback(w, XmNinputCallback, canvas_2D_painting_cb,
client_data);
XtAddCallback(w, XmNinputCallback, canvas_input_cb,
client_data);
break;
default:
break;
}
break;
case MotionNotify:
case TabletMotionNotify:
if( cbs->event->xmotion.state & Button2Mask )
{
x = cbs->event->xmotion.x / wlzViewStr->scale;
y = cbs->event->xmotion.y / wlzViewStr->scale;
display_pointer_feedback_information(view_struct, x, y);
}
break;
case KeyPress:
if( !cbs->event->xkey.state&(Button1Mask|Button2Mask|Button3Mask) ){
fprintf(stderr, "\007 Too fast with the fingers %s\n", getenv("USER"));
break;
}
switch( XLookupKeysym(&(cbs->event->xkey), 0) ){
case XK_Right:
case XK_KP_Right:
case XK_f:
/* in this mode - next section installing and resetting domains
if <alt> or <alt gr> pressed then change tool option
(tool dependent) */
if( cbs->event->xkey.state&(Mod1Mask|Mod2Mask) ){
break;
}
else {
if( wlzViewStr->dist <= (wlzViewStr->maxvals.vtZ - 1.0) ){
installViewDomains(view_struct);
clearUndoDomains();
wlzViewStr->dist += 1.0;
reset_view_struct( view_struct );
display_view_cb(w, (XtPointer) view_struct, call_data);
view_feedback_cb(w, (XtPointer) view_struct, NULL);
getViewDomains(view_struct);
}
}
break;
case XK_Up:
case XK_p:
/* next domain or next paint tool if <alt> (which seems
to be Mod1Mask) pressed */
if( cbs->event->xkey.state&(Mod1Mask|Mod2Mask) ){
selectNext2DTool(0);
}
else {
selectNextDomain(0);
}
break;
case XK_Left:
case XK_KP_Left:
case XK_b:
/* in this mode - next section installing and resetting domains
if <alt> pressed then change tool option (tool dependent) */
if( cbs->event->xkey.state&(Mod1Mask|Mod2Mask) ){
break;
}
else {
if( wlzViewStr->dist >= (wlzViewStr->minvals.vtZ + 1.0) ){
installViewDomains(view_struct);
clearUndoDomains();
wlzViewStr->dist -= 1.0;
reset_view_struct( view_struct );
display_view_cb(w, (XtPointer) view_struct, call_data);
view_feedback_cb(w, (XtPointer) view_struct, NULL);
getViewDomains(view_struct);
}
}
break;
case XK_Down:
case XK_n:
/* previous paint tool or previous domain if <alt> pressed */
if( cbs->event->xkey.state&(Mod1Mask|Mod2Mask) ){
selectNext2DTool(1);
}
else {
selectNextDomain(1);
}
break;
case XK_w:
/* get a filename for the view image */
break;
case XK_u: /* undo */
case XK_U:
if( !(cbs->event->xkey.state & ControlMask) ){
break;
}
UndoDomains(view_struct);
break;
case XK_r: /* redo */
case XK_R:
if( !(cbs->event->xkey.state & ControlMask) ){
break;
}
RedoDomains(view_struct);
break;
case XK_Undo:
case XK_F14:
UndoDomains(view_struct);
break;
case XK_Redo:
case XK_F12:
RedoDomains(view_struct);
break;
}
break;
default:
break;
}
return;
}