forked from boardhead/aged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PColourWindow.cxx
340 lines (292 loc) · 10.9 KB
/
PColourWindow.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
//==============================================================================
// File: PColourWindow.cxx
//
// Created: 03/02/00 - Phil Harvey
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <Xm/Form.h>
#include <Xm/Scale.h>
#include <Xm/Label.h>
#include <Xm/Text.h>
#include <Xm/PushB.h>
#include <Xm/DrawingA.h>
#include <Xm/Separator.h>
#include "PColourWindow.h"
#include "PColourWheel.h"
#include "PColourPicker.h"
#include "PResourceManager.h"
#include "PUtils.h"
#include "ImageData.h"
const int kPickerHeight = 26 * (int)((kNumPickerColours + 2) / 3);
#define WIN_WIDTH 425
#define WIN_HEIGHT (270 + kPickerHeight)
#define RGB_YPOS 18
PColourWindow::PColourWindow(ImageData *data)
: PImageWindow(data)
{
int n;
Arg wargs[20];
Widget w, but, canvas;
mPrintable = 0; // not printable
n = 0;
XtSetArg(wargs[n], XmNtitle, "Aged Colors"); ++n;
XtSetArg(wargs[n], XmNx, 320); ++n;
XtSetArg(wargs[n], XmNy, 100); ++n;
XtSetArg(wargs[n], XmNminWidth, WIN_WIDTH); ++n;
XtSetArg(wargs[n], XmNminHeight, WIN_HEIGHT); ++n;
SetShell(CreateShell("colPop",data->toplevel,wargs,n));
n = 0;
XtSetArg(wargs[n], XmNwidth, WIN_WIDTH); ++n;
XtSetArg(wargs[n], XmNheight, WIN_HEIGHT); ++n;
w = XtCreateManagedWidget("agedForm",xmFormWidgetClass,GetShell(),wargs,n);
SetMainPane(w);
n = 0;
XtSetArg(wargs[n], XmNy, 242); ++n;
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); ++n;
XtCreateManagedWidget("sep",xmSeparatorWidgetClass,w,wargs,n);
// create colour picker
n = 0;
XtSetArg(wargs[n], XmNx, 10); ++n;
XtSetArg(wargs[n], XmNy, 249); ++n;
XtSetArg(wargs[n], XmNwidth, 400); ++n;
XtSetArg(wargs[n], XmNheight, 10 + kPickerHeight); ++n;
canvas = XtCreateManagedWidget("colourPicker", xmDrawingAreaWidgetClass, w, wargs, n);
mColourPicker = new PColourPicker(this, canvas);
// create colour wheel (done after colour picker so it becomes our default ImageCanvas)
n = 0;
XtSetArg(wargs[n], XmNx, 12); ++n;
XtSetArg(wargs[n], XmNy, 11); ++n;
XtSetArg(wargs[n], XmNwidth, 178); ++n;
XtSetArg(wargs[n], XmNheight, 178); ++n;
canvas = XtCreateManagedWidget("colourWheel", xmDrawingAreaWidgetClass, w, wargs, n);
// install a colour wheel in the window
// Note: this must be done _after_ installing the colourPicker, because
// we want this to be our default image canvas (to set the cursor, etc)
mColourWheel = new PColourWheel(this, canvas, 178);
n = 0;
XtSetArg(wargs[n], XmNx, 194); ++n;
XtSetArg(wargs[n], XmNy, 15); ++n;
XtCreateManagedWidget("Intensity", xmLabelWidgetClass, w, wargs, n);
n = 0;
#ifdef LESSTIF
XtSetArg(wargs[n], XmNx, 180); ++n;
#else
XtSetArg(wargs[n], XmNx, 199); ++n;
#endif
XtSetArg(wargs[n], XmNy, 40); ++n;
XtSetArg(wargs[n], XmNorientation, XmVERTICAL); ++n;
XtSetArg(wargs[n], XmNheight, 150); ++n;
XtSetArg(wargs[n], XmNwidth, 20); ++n;
XtSetArg(wargs[n], XmNshowValue, TRUE); ++n;
XtSetArg(wargs[n], XmNminimum, 0); ++n;
// the slider scale is set to give even values when sliding...
XtSetArg(wargs[n], XmNmaximum, 255); ++n;
XtSetArg(wargs[n], XmNvalue, 255); ++n;
mSlide = XtCreateManagedWidget("slide",xmScaleWidgetClass,w,wargs,n);
XtAddCallback(mSlide,XmNvalueChangedCallback,(XtCallbackProc)IntensityProc,this);
XtAddCallback(mSlide,XmNdragCallback,(XtCallbackProc)ScaleDragProc,this);
n = 0;
XtSetArg(wargs[n], XmNx, 20); ++n;
XtSetArg(wargs[n], XmNy, 200); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
but = XtCreateManagedWidget("OK",xmPushButtonWidgetClass,w,wargs,n);
XtAddCallback(but, XmNactivateCallback, (XtCallbackProc)OkProc, this);
// set initial focus to this button
n = 0;
XtSetArg(wargs[n], XmNinitialFocus, but); ++n;
XtSetValues(w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 120); ++n;
XtSetArg(wargs[n], XmNy, 200); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
but = XtCreateManagedWidget("Apply",xmPushButtonWidgetClass,w,wargs,n);
XtAddCallback(but, XmNactivateCallback, (XtCallbackProc)ApplyProc, this);
n = 0;
XtSetArg(wargs[n], XmNx, 220); ++n;
XtSetArg(wargs[n], XmNy, 200); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
but = XtCreateManagedWidget("Revert",xmPushButtonWidgetClass,w,wargs,n);
XtAddCallback(but, XmNactivateCallback, (XtCallbackProc)RevertProc, this);
n = 0;
XtSetArg(wargs[n], XmNx, 320); ++n;
XtSetArg(wargs[n], XmNy, 200); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
but = XtCreateManagedWidget("Cancel",xmPushButtonWidgetClass,w,wargs,n);
XtAddCallback(but, XmNactivateCallback, (XtCallbackProc)CancelProc, this);
n = 0;
XtSetArg(wargs[n], XmNx, 300); ++n;
XtSetArg(wargs[n], XmNy, 15); ++n;
XtCreateManagedWidget("Components", xmLabelWidgetClass, w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 262); ++n;
XtSetArg(wargs[n], XmNy, 35+RGB_YPOS); ++n;
XtSetArg(wargs[n], XmNwidth, 50); ++n;
XtSetArg(wargs[n], XmNalignment, XmALIGNMENT_END); ++n;
XtCreateManagedWidget("Red:", xmLabelWidgetClass, w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 320); ++n;
XtSetArg(wargs[n], XmNy, 30+RGB_YPOS); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
XtSetArg(wargs[n], XmNmarginHeight, 3); ++n;
mText[0] = XtCreateManagedWidget("red", xmTextWidgetClass, w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 262); ++n;
XtSetArg(wargs[n], XmNy, 85+RGB_YPOS); ++n;
XtSetArg(wargs[n], XmNwidth, 50); ++n;
XtSetArg(wargs[n], XmNalignment, XmALIGNMENT_END); ++n;
XtCreateManagedWidget("Green:", xmLabelWidgetClass, w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 320); ++n;
XtSetArg(wargs[n], XmNy, 80+RGB_YPOS); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
XtSetArg(wargs[n], XmNmarginHeight, 3); ++n;
mText[1] = XtCreateManagedWidget("green", xmTextWidgetClass, w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 262); ++n;
XtSetArg(wargs[n], XmNy, 135+RGB_YPOS); ++n;
XtSetArg(wargs[n], XmNwidth, 50); ++n;
XtSetArg(wargs[n], XmNalignment, XmALIGNMENT_END); ++n;
XtCreateManagedWidget("Blue:", xmLabelWidgetClass, w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 320); ++n;
XtSetArg(wargs[n], XmNy, 130+RGB_YPOS); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
XtSetArg(wargs[n], XmNmarginHeight, 3); ++n;
mText[2] = XtCreateManagedWidget("blue", xmTextWidgetClass, w, wargs, n);
// add the callbacks to the text widgets
for (int i=0; i<3; ++i) {
XtAddCallback(mText[i],XmNactivateCallback,(XtCallbackProc)TextProc,this);
XtAddCallback(mText[i],XmNlosingFocusCallback,(XtCallbackProc)TextProc, this);
}
// set our initial colour
PickerColourChanged();
}
PColourWindow::~PColourWindow()
{
// Note: mColourWindow will be deleted by the base class
// since it is the 'mImage'.
delete mColourPicker;
}
void PColourWindow::UpdateSelf()
{
// draw the colour wheel if necessary
if (mColourWheel->IsDirty()) {
mColourWheel->Draw();
}
// draw colour picker too
if (mColourPicker->IsDirty()) {
mColourPicker->Draw();
}
}
// set current color from current picker colour
void PColourWindow::PickerColourChanged()
{
int col3[3];
// get RGB of current picker colour
mColourPicker->GetColourRGB(col3);
// set current wheel colour
mColourWheel->SetColourRGB(col3);
// update text and slider for current colour
UpdateText();
UpdateSliderPosition();
}
// WheelColourChanging - animate necessary items while wheel colour is changing
void PColourWindow::WheelColourChanging()
{
int col3[3];
mColourWheel->GetColourRGB(col3);
mColourPicker->SetColourRGB(col3);
}
// WheelColourChanged - colour is done changing, update all required items
void PColourWindow::WheelColourChanged()
{
UpdateText();
}
void PColourWindow::UpdateSliderPosition()
{
Arg warg;
XtSetArg(warg, XmNvalue, mColourWheel->GetIntensity());
XtSetValues(mSlide, &warg, 1);
}
void PColourWindow::UpdateText()
{
char buff[32];
int col3[3];
mColourWheel->GetColourRGB(col3);
for (int i=0; i<3; ++i) {
sprintf(buff,"%d", col3[i]);
setTextString(mText[i], buff);
}
}
void PColourWindow::SetIntensity(int val)
{
// set colour wheel intensity
mColourWheel->SetIntensity(val);
UpdateSliderPosition();
}
void PColourWindow::IntensityProc(Widget w, PColourWindow *cwin, XmScaleCallbackStruct *call_data)
{
PColourWheel *cwheel = (PColourWheel *)cwin->mImage;
cwheel->SetIntensity(call_data->value, 1); // set intensity
cwin->WheelColourChanging();
// done changing our colour wheel
cwin->WheelColourChanged();
// update the colour wheel now if it wasn't done dynamically
cwheel->AnimateDone();
}
void PColourWindow::ScaleDragProc(Widget w, PColourWindow *cwin, XmScaleCallbackStruct *call_data)
{
PColourWheel *cwheel = (PColourWheel *)cwin->mImage;
// only update colour wheel for fast animation
cwheel->SetIntensity(call_data->value, 1);
// the wheel colour is changing -- do required animation
cwin->WheelColourChanging();
}
void PColourWindow::OkProc(Widget w, PColourWindow *cwin, caddr_t call_data)
{
TextProc(NULL, cwin, NULL); // do this in case text was changed
cwin->mColourPicker->ApplyCurrentColours();
delete(cwin);
}
void PColourWindow::ApplyProc(Widget w, PColourWindow *cwin, caddr_t call_data)
{
TextProc(NULL, cwin, NULL); // do this in case text was changed
cwin->mColourPicker->ApplyCurrentColours();
}
// revert to last applied colours
void PColourWindow::RevertProc(Widget w, PColourWindow *cwin, caddr_t call_data)
{
if (cwin->mColourPicker->RevertColours()) {
cwin->PickerColourChanged();
}
}
void PColourWindow::CancelProc(Widget w, PColourWindow *cwin, caddr_t call_data)
{
if (cwin->mColourPicker->RevertColours()) {
cwin->PickerColourChanged();
}
delete(cwin);
}
void PColourWindow::TextProc(Widget w, PColourWindow *cwin, caddr_t call_data)
{
int col3[3];
int oldCol[3];
for (int i=0; i<3; ++i) {
char *str = XmTextGetString(cwin->mText[i]);
col3[i] = atoi(str);
if (col3[i] < 0) col3[i] = 0;
else if (col3[i] > 255) col3[i] = 255;
XtFree(str); // must free the string
}
cwin->mColourWheel->GetColourRGB(oldCol);
if (memcmp(oldCol, col3, sizeof(col3))) {
cwin->mColourWheel->SetColourRGB(col3);
cwin->mColourPicker->SetColourRGB(col3);
cwin->UpdateSliderPosition();
}
cwin->UpdateText(); // update the text in case an entry wasn't in standard format
}