-
Notifications
You must be signed in to change notification settings - Fork 2
/
PImageCanvas.cxx
452 lines (400 loc) · 12.8 KB
/
PImageCanvas.cxx
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
#include <stdio.h>
#include <stdlib.h>
#include <X11/StringDefs.h>
#include <Xm/DrawingA.h>
#include "PImageCanvas.h"
#include "PImageWindow.h"
#include "PHitInfoWindow.h"
#include "PResourceManager.h"
#include "PDrawXPixmap.h"
#include "PDrawPostscriptFile.h"
#include "PMenu.h"
#include "XSnoedWindow.h"
#include "xsnoed.h"
const short kPrintScaling = 5; // coordinate scaling for printed images
const short kLabelClickMargin = 8;
//----------------------------------------------------------------------------------------------
// PImageCanvas constructor
//
PImageCanvas::PImageCanvas(PImageWindow *owner, Widget canvas, EventMask eventMask)
{
ImageData *data = owner->GetData();
mDpy = data->display;
mOwner = owner;
mWidth = 0;
mHeight = 0;
mCanvasWidth = 0;
mCanvasHeight = 0;
mDrawable = 0;
mCanvas = NULL;
mEventMask = eventMask;
mDrawLabel = 1;
mAllowLabel = 1;
mLabelText = NULL;
mLabelHeight = 0;
mDirty = kDirtyPix;
SetCanvas(canvas);
owner->SetImage(this);
data->mSpeaker->AddListener(this);
}
PImageCanvas::~PImageCanvas()
{
if (mDrawable) delete mDrawable;
if (mCanvas) {
XtRemoveCallback(mCanvas, XmNexposeCallback, (XtCallbackProc)CanvasExposeProc, this);
XtRemoveCallback(mCanvas, XmNresizeCallback, (XtCallbackProc)CanvasResizeProc, this);
if (mEventMask) {
XtRemoveEventHandler(mCanvas, mEventMask, FALSE, (XtEventHandler)CanvasMotionProc, this);
}
}
}
void PImageCanvas::Listen(int message, void *dataPt)
{
switch (message) {
case kMessageLabelChanged:
if (mDrawLabel) {
SetDirty();
}
break;
}
}
// SetDirty - mark the image as needing redrawing
void PImageCanvas::SetDirty(int flag)
{
mDirty |= flag; // set our dirty flags
mOwner->SetDirty(flag); // set our owner's dirty flag
}
/* must be called AFTER widget is realized */
void PImageCanvas::SetCursor(int type)
{
/* define the cursor for our image */
XDefineCursor(mDpy, XtWindow(mCanvas), PResourceManager::sResource.cursor[type]);
}
void PImageCanvas::SetCursorForPos(int x, int y)
{
if (IsInLabel(x,y)) {
if (IsLabelOn()) {
SetCursor(CURSOR_ARROW_DOWN);
} else {
SetCursor(CURSOR_ARROW_UP);
}
} else {
XUndefineCursor(mDpy, XtWindow(mCanvas));
}
}
void PImageCanvas::SetCanvas(Widget canvas)
{
mCanvas = canvas;
if (canvas) {
mDpy = XtDisplay(canvas);
if (mDrawable) delete mDrawable;
mDrawable = new PDrawXPixmap(mDpy, mOwner->GetData()->gc,
DefaultDepthOfScreen(XtScreen(canvas)), canvas);
XtAddCallback(canvas, XmNexposeCallback, (XtCallbackProc)CanvasExposeProc, this);
XtAddCallback(canvas, XmNresizeCallback, (XtCallbackProc)CanvasResizeProc, this);
if (mEventMask) {
XtAddEventHandler(canvas, mEventMask, FALSE, (XtEventHandler)CanvasMotionProc, this);
}
}
}
// GetCanvasSize - get size of our canvas
// - returns non-zero (width of canvas) if canvas is realized
int PImageCanvas::GetCanvasSize()
{
// return zero if our widget isn't available or isn't realized
if (!mCanvas || !XtIsRealized(mCanvas)) return(0);
// get the current size of the canvas
Arg wargs[10];
Dimension width,height;
XtSetArg(wargs[0],XtNwidth, &width);
XtSetArg(wargs[1],XtNheight,&height);
XtGetValues(mCanvas,wargs,2);
if (mCanvasWidth!=width || mCanvasHeight!=height) {
ImageData *data = mOwner->GetData();
mCanvasWidth = mWidth = width;
mCanvasHeight = mHeight = height;
if (data->show_label) {
mHeight -= GetScaling() * mLabelHeight;
// handle case where mHeight is negative (remember, this is unsigned math)
if (mHeight > height) mHeight = 0;
}
mDirty |= kDirtyPix; // flag indicates pixmap requires redrawing
Resize(); // call this whenever the image size changes
}
return((int)width);
}
void PImageCanvas::CanvasExposeProc(Widget w, PImageCanvas *anImage, XmDrawingAreaCallbackStruct *call_data)
{
XExposeEvent * event = (XExposeEvent *)call_data->event;
if (!anImage->mCanvasWidth) {
anImage->GetCanvasSize();
}
// if pixmap is available and doesn't require drawing, copy the exposed area to screen
if (!anImage->IsDirty() &&
anImage->mDrawable->CopyArea(event->x,event->y,event->width,event->height,XtWindow(anImage->mCanvas)))
{
if (event->count == 0) {
// call this routine after last copy to screen
anImage->AfterDrawing();
}
} else if (event->count == 0) { // only draw on last expose event
// pixmap is dirty or unavailable -- draw the image from scratch
anImage->Draw();
}
}
void PImageCanvas::CanvasResizeProc(Widget w, PImageCanvas *anImage, XmDrawingAreaCallbackStruct *call_data)
{
int wasDrawn = (anImage->mCanvasWidth != 0);
if (anImage->GetCanvasSize()) {
if (wasDrawn) {
// clear the canvas on the screen until we can redraw it
//(causes flickering) XClearArea(anImage->mDpy,XtWindow(w),0,0,0,0,TRUE);
}
}
}
void PImageCanvas::CanvasMotionProc(Widget w, PImageCanvas *anImage, XEvent *event)
{
// are we waiting for a window to be selected?
if (PImageWindow::IsPendingRaise()) {
// must look for button presses too, in case the window manager
// doesn't raise the window on a button click
if (event->type == ButtonPress) {
anImage->mOwner->WasRaised();
}
} else {
// handle events normally
anImage->HandleEvents(event);
}
}
// Print - print image to postscript file
int PImageCanvas::Print(char *filename, int flags)
{
int printOK = 0;
PDrawPostscriptFile drawable(filename, (flags & kPrintLandscape));
PDrawable *oldDrawable = mDrawable;
int oldWidth = mWidth;
int oldHeight = mHeight;
int oldCanvasWidth = mCanvasWidth;
int oldCanvasHeight = mCanvasHeight;
mDrawable = &drawable;
// change scaling to improve printed resolution
drawable.SetScaling(kPrintScaling);
mWidth *= kPrintScaling;
mHeight *= kPrintScaling;
mCanvasWidth *= kPrintScaling;
mCanvasHeight *= kPrintScaling;
Resize(); // call this whenever the image size changes
// draw image to postscript file
if (drawable.BeginDrawing(mCanvasWidth,mCanvasHeight)) {
Prepare();
DrawSelf();
drawable.EndDrawing();
printOK = 1;
}
// restore original settings
mDrawable = oldDrawable;
mWidth = oldWidth;
mHeight = oldHeight;
mCanvasWidth = oldCanvasWidth;
mCanvasHeight = oldCanvasHeight;
Resize(); // call this whenever the image size changes
return(printOK);
}
//---------------------------------------------------------------------------------------
// Draw
//
void PImageCanvas::Draw()
{
// get our canvas dimensions if not done yet
if (!mCanvasWidth) {
if (!GetCanvasSize()) return; // return if widget not realized
}
if (mDrawable->BeginDrawing(mCanvasWidth, mCanvasHeight)) {
Prepare();
// set dirty pixmap flag too if we have arrived here without a pixmap
if (!mDrawable->HasPixmap()) {
mDirty |= kDirtyPix;
}
DrawSelf();
mDrawable->EndDrawing();
// copy the image to the screen
mDrawable->CopyArea(0,0,mCanvasWidth,mCanvasHeight,XtWindow(mCanvas));
// reset all dirty flags since we have just successfully drawn ourself
mDirty = 0;
AfterDrawing(); // call this after any drawing to screen
}
}
//---------------------------------------------------------------------------------------
// DrawLabel
//
void PImageCanvas::DrawLabel(int x,int y,ETextAlign_q align)
{
if (mLabelText && mLabelHeight) {
SetForeground(TEXT_COL);
y -= GetScaling() * mLabelHeight;
for (TextSpec *ts=mLabelText; ts->string; ++ts) {
SetFont(ts->font);
y += GetScaling() * ts->font->ascent;
DrawString(x, y, ts->string, align);
y += GetScaling() * ts->font->descent;
}
}
}
//---------------------------------------------------------------------------------------
// Prepare - prepare to draw into canvas
//
void PImageCanvas::Prepare()
{
// resize drawing area according to current label size
ImageData *data = mOwner->GetData();
int newHeight;
if (mDrawLabel && data->show_label) {
XSnoedWindow *mainWindow = mOwner->GetData()->mMainWindow;
mLabelText = mainWindow->GetLabelText();
newHeight = mainWindow->GetLabelHeight();
} else {
mLabelText = NULL;
newHeight = 0;
}
// get current number of lines in label
if (mLabelHeight != newHeight) {
// calculate new image height
mHeight = mCanvasHeight - GetScaling() * newHeight;
if (mHeight > mCanvasHeight) mHeight = 0;
mLabelHeight = newHeight;
Resize(); // call this whenever the image size changes
}
}
//---------------------------------------------------------------------------------------
// DrawSelf
//
void PImageCanvas::DrawSelf()
{
// clear the canvas with the background colour */
SetForeground(BKG_COL);
FillRectangle(0, 0, mCanvasWidth, mCanvasHeight);
if (mLabelText) {
DrawLabel(mCanvasWidth/2, mCanvasHeight-2*GetScaling(), kTextAlignBottomCenter);
}
}
int PImageCanvas::IsInLabel(int x, int y)
{
if (mAllowLabel) {
return(mOwner->GetData()->show_label && (y > mCanvasHeight - mLabelHeight - kLabelClickMargin));
} else {
return(0);
}
}
void PImageCanvas::ShowLabel(int on)
{
if (mDrawLabel != on) {
mDrawLabel = on;
if (mCanvas && XtIsRealized(mCanvas)) {
Prepare(); // do this now so any subsequent cursor changes will use new configuration
SetDirty(); // redraw ourself
}
}
}
void PImageCanvas::AllowLabel(int on)
{
mAllowLabel = on;
if (!on) ShowLabel(on);
}
// utility to create image canvas plus specified scrollbars
void PImageCanvas::CreateCanvas(char *name, int scrollBarMask)
{
int n;
Arg wargs[16];
unsigned char attach;
Widget topWidget;
if (mOwner->GetMenu()) {
topWidget = mOwner->GetMenu()->GetWidget();
} else {
topWidget = 0;
}
// get right attachment of top widget
if (topWidget) {
n = 0;
XtSetArg(wargs[n], XmNrightAttachment, &attach); ++n;
XtGetValues(topWidget, wargs, n);
}
if (scrollBarMask & kScrollLeftMask) {
n = 0;
if (topWidget) {
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNtopWidget, topWidget); ++n;
} else {
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); ++n;
}
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNwidth, 15); ++n;
XtSetArg(wargs[n], XmNorientation, XmVERTICAL); ++n;
mOwner->NewScrollBar(kScrollLeft,"xsnoedScroll1",wargs,n);
}
if (scrollBarMask & kScrollRightMask) {
n = 0;
if (topWidget && attach==XmATTACH_FORM) {
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNtopWidget, topWidget); ++n;
} else {
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); ++n;
}
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNwidth, 15); ++n;
XtSetArg(wargs[n], XmNorientation, XmVERTICAL); ++n;
mOwner->NewScrollBar(kScrollRight,"xsnoedScroll2",wargs,n);
}
if (scrollBarMask & kScrollBottomMask) {
n = 0;
if (scrollBarMask & kScrollLeftMask) {
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNleftWidget, mOwner->GetScrollBar(kScrollLeft)->GetWidget()); ++n;
} else {
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); ++n;
}
if (scrollBarMask & kScrollRightMask) {
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNrightWidget, mOwner->GetScrollBar(kScrollRight)->GetWidget()); ++n;
} else {
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); ++n;
}
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNheight, 15); ++n;
XtSetArg(wargs[n], XmNorientation, XmHORIZONTAL); ++n;
mOwner->NewScrollBar(kScrollBottom,"xsnoedScroll3",wargs,n);
}
// finally create canvas
n = 0;
if (topWidget && attach==XmATTACH_FORM) {
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNtopWidget, topWidget); ++n;
} else {
// attach top to form if no top widget, or if top widget doesn't span form
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); ++n;
}
if (scrollBarMask & kScrollLeftMask) {
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNleftWidget, mOwner->GetScrollBar(kScrollLeft)->GetWidget()); ++n;
} else {
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); ++n;
}
if (scrollBarMask & kScrollBottomMask) {
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNbottomWidget, mOwner->GetScrollBar(kScrollBottom)->GetWidget()); ++n;
} else {
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
}
if (scrollBarMask & kScrollRightMask) {
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNrightWidget, mOwner->GetScrollBar(kScrollRight)->GetWidget()); ++n;
} else {
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); ++n;
}
Widget canvas = XtCreateManagedWidget(name, xmDrawingAreaWidgetClass, mOwner->GetMainPane(), wargs, n);
SetCanvas(canvas);
/* set up this as the default scrollbar handler */
mOwner->SetScrollHandler(this);
SetScrolls();
}