-
Notifications
You must be signed in to change notification settings - Fork 2
/
oDrawingCanvas.cpp
848 lines (701 loc) · 23.1 KB
/
oDrawingCanvas.cpp
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
* omnis.xcomp.framework
* =====================
*
* oDrawingCanvas.cpp
*
* Drawing canvas class
*
* Bastiaan Olij
*
* https://github.com/BastiaanOlij/omnis.xcomp.framework
*/
#include "oDrawingCanvas.h"
// Initialize our instance
void oDrawingCanvas::init(qapp pApp) {
mApp = pApp;
mHDC = 0;
mTextFont = 0;
mOldFont = 0;
mBackpatBrush = 0;
mFontHeight = 0;
};
// Setup a drawing canvas based on an existing HDC
oDrawingCanvas::oDrawingCanvas(qapp pApp, HDC pHdc, qrect pRect) {
init(pApp);
mHDC = pHdc;
mBaseRect = pRect;
mOwnHDC = false;
};
// Create a bitmap based canvas
oDrawingCanvas::oDrawingCanvas(qapp pApp, qdim pWidth, qdim pHeight) {
init(pApp);
GDIcreateAlphaDC(&mHDC, pWidth, pHeight);
mBaseRect.left = 0;
mBaseRect.top = 0;
mBaseRect.right = pWidth - 1;
mBaseRect.bottom = pHeight - 1;
mOwnHDC = true;
};
// Destruct our canvas
oDrawingCanvas::~oDrawingCanvas() {
// cleanup..
if (mBackpatBrush != 0) {
GDIdeleteObject(mBackpatBrush);
};
if (mOldFont != 0) {
GDIselectObject(mHDC, mOldFont);
};
if (mTextFont != 0) {
GDIdeleteObject(mTextFont);
};
clearTextCache();
if (mOwnHDC) {
HBITMAP bitmap;
GDIdeleteAlphaDC(mHDC, &bitmap);
GDIdeleteBitmap(bitmap);
mOwnHDC = false;
mHDC = 0;
};
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Properties
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// access to our HDC if we really need to.
HDC oDrawingCanvas::hdc() {
return mHDC;
};
// get our text spec
GDItextSpecStruct oDrawingCanvas::textSpec() {
return mTextSpec;
};
// set our text spec
void oDrawingCanvas::setTextSpec(GDItextSpecStruct pSpec) {
mTextSpec = pSpec;
clearTextCache();
// and select our font
HFONT lvNewFont = GDIcreateFont(&mTextSpec.mFnt, mTextSpec.mSty);
HFONT lvOldFont = GDIselectObject(mHDC, lvNewFont);
if (mTextFont != 0) {
// safe to delete the old selected font now that we've created a new one
GDIdeleteObject(mTextFont);
}
mTextFont = lvNewFont;
mFontHeight = GDIfontHeight(mHDC);
if (mOldFont == 0) {
// remember this so we reselect it when our canvas gets destructed..
mOldFont = lvOldFont;
};
GDIsetTextColor(mHDC, mTextSpec.mTextColor);
};
////////////////////////////////////////////////////////////////
// colour functions
////////////////////////////////////////////////////////////////
qcol oDrawingCanvas::mixColors(qcol pQ1, qcol pQ2) {
// get real colors...
pQ1 = GDIgetRealColor(pQ1);
pQ2 = GDIgetRealColor(pQ2);
short cnt;
qcol res;
unsigned char mix;
unsigned char *cA = (unsigned char *)&pQ1;
unsigned char *cB = (unsigned char *)&pQ2;
unsigned char *cR = (unsigned char *)&res;
// This should mix R, G, B and alpha independently of eachother...
for (cnt = 0; cnt < sizeof(qcol); cnt++) {
if (*cA == *cB) {
*cR = *cA;
} else if (*cB > *cA) {
mix = *cB - *cA;
mix = mix >> 1;
*cR = *cA + mix;
} else {
mix = *cA - *cB;
mix = mix >> 1;
*cR = *cB + mix;
};
cA++;
cB++;
cR++;
};
return res;
};
// setup our backpattern brush
void oDrawingCanvas::setBackpatBrush(qpat pPat) {
if (mBackpatBrush != 0) {
GDIdeleteObject(mBackpatBrush);
};
mBackpatBrush = GDIcreateBrush(pPat);
};
// set our background color
void oDrawingCanvas::setBkColor(qcol pColor) {
GDIsetBkColor(mHDC, pColor);
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// clipping functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Clip to given rectangle and put on stack, will optionally union with the current clipping. Will return false if we can't draw in the resulting rectangle and we could thus not clip.
bool oDrawingCanvas::clipRect(qrect pRect, bool pUnion) {
if (mHDC != 0) {
if (pUnion) {
qrect lvCliprect;
int lvStackSize = mClipStack.size();
if (mClipStack.empty()) {
lvCliprect = mBaseRect;
} else {
lvCliprect = mClipStack.back();
};
// union the two rectangles so we clip only where the two rectangles overlap
// note that if the rectangles do not overlap left will be bigger then right or top below the bottom.
pRect.left = pRect.left > lvCliprect.left ? pRect.left : lvCliprect.left;
pRect.right = pRect.right < lvCliprect.right ? pRect.right : lvCliprect.right;
pRect.top = pRect.top > lvCliprect.top ? pRect.top : lvCliprect.top;
pRect.bottom = pRect.bottom < lvCliprect.bottom ? pRect.bottom : lvCliprect.bottom;
};
if ((pRect.left < pRect.right) && (pRect.top < pRect.bottom)) {
// clip
GDIsetClipRect(mHDC, &pRect);
// now add our rectangle to our clipstack
mClipStack.push_back(pRect);
return true;
} else {
return false;
};
} else {
return false;
};
};
// Pop our last clipping rectangle off the stack, do not call if clipRect returned false!
void oDrawingCanvas::unClip() {
if (mHDC != 0) {
// remove the top one, that is our current clipping
mClipStack.pop_back();
if (mClipStack.empty()) {
GDIclearClip(mHDC);
} else {
qrect lvCliprect = mClipStack.back();
GDIsetClipRect(mHDC, &lvCliprect);
};
};
};
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Font functions
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Free memory associated with our text cache
void oDrawingCanvas::clearTextCache() {
while (!mTextCache.empty()) {
mTextCache.erase(mTextCache.begin()); // remove it from our cache
};
};
// passthrought to GDIdrawTextJst with clipping
void oDrawingCanvas::drawTextJst(GDIdrawTextStruct *pTextInfo, qrect pRect, bool pAdjJst) {
qrect lvClipRect = pRect;
if (lvClipRect.left < pTextInfo->mX) lvClipRect.left = pTextInfo->mX;
if (lvClipRect.top < pTextInfo->mY) lvClipRect.top = pTextInfo->mY;
if (clipRect(lvClipRect, true)) {
if (pAdjJst && (pTextInfo->mColumnJsts != 0)) {
qdim wasX = pTextInfo->mX;
qint1 *wasJst = pTextInfo->mColumnJsts;
qint1 jst = pTextInfo->mColumnJsts[0];
pTextInfo->mColumnJsts = 0; // unset this... we're doing our own.
if (jst != jstLeft) {
qdim availWidth = pRect.right - pTextInfo->mX + 1;
qdim textWidth = getTextWidth(pTextInfo->mText, pTextInfo->mTextLen, pTextInfo->mFlags == 1);
if (jst == jstRight) {
pTextInfo->mX = pRect.right - textWidth;
} else if ((jst == jstCenter) && (availWidth > textWidth)) {
pTextInfo->mX = pRect.left + ((availWidth - textWidth) / 2);
};
};
// now draw it
GDIdrawTextJst(pTextInfo);
// undo any changes
pTextInfo->mX = wasX;
pTextInfo->mColumnJsts = wasJst;
} else {
GDIdrawTextJst(pTextInfo);
};
unClip();
};
};
// get the height of the current selected font
qdim oDrawingCanvas::getFontHeight() {
return mFontHeight;
};
// Get the width of a word (from cache)
qdim oDrawingCanvas::getWordWidth(qstring &pWord) {
int len = pWord.length();
/* in theory we can store 50 character wide strings but just in case we have any special characters, cut it off at 40 */
if (len <= 40) {
qTextWidthMap::iterator it;
it = mTextCache.find(pWord.cString());
if (it != mTextCache.end()) {
// oBaseComponent::addToTraceLog("Found cached word %qs, size = %li", &pWord, it->second);
return it->second;
};
};
// not found?
GDItextSpecStruct lvTextSpec = mTextSpec; // Copy of our text spec we're using
lvTextSpec.mJst = jstLeft; // Set to left justification or our text width fails
GDIdrawTextStruct drawinfo(
mHDC,
0,
0,
(qchar *)pWord.cString(), // for some reason Omnis never declared this a constant but it doesn't change the buffer (i hope)..
pWord.length(),
&lvTextSpec,
0, // pColumnArray
0, // pColumnCount
1, // pFlags: 1 = styled text
mApp,
0 // pColumnJsts
);
qdim width = GDItextWidthJst(&drawinfo) + 2;
if (len <= 40) {
// can we cache it?
mTextCache.insert(std::pair<qchar50, qdim>(pWord.cString(), width));
// oBaseComponent::addToTraceLog("Cached word %qs, size = %li", &pWord, width);
// } else {
// oBaseComponent::addToTraceLog("Width word %qs, size = %li", &pWord, width);
};
return width;
};
// add a word to a string
qdim oDrawingCanvas::addWord(qstring &pTo, qstring &pWord, qstring &pPrefix, qdim pCurrWidth, qdim pMaxWidth) {
if (pWord.length() == 0) {
return pCurrWidth; // No word to add..
} else {
qdim wordWidth = getWordWidth(pWord);
if (pCurrWidth == 0) {
// just add it even if it doesn't fit, if it doesn't fit it doesn't fit.. maybe some day we'll break up the word..
pTo += pWord;
return wordWidth;
} else {
qdim prefixWidth = getWordWidth(pPrefix);
if (pCurrWidth + prefixWidth + wordWidth <= pMaxWidth) {
// it fits! hurray!
pTo += pPrefix;
pTo += pWord;
return pCurrWidth + prefixWidth + wordWidth;
} else {
// it doesn't fit :( add to a new line
pTo += (qchar)'\n'; // prevent expensive UTF-8 => UTF-32 conversion
pTo += pWord;
return wordWidth;
};
};
};
};
// wrap text, inserts newlines where needed to wrap the text
qstring oDrawingCanvas::wrapText(const qchar *pText, qdim pMaxWidth) {
qstring retval;
qstring word;
qstring prefix;
qstring characters(":;?/\\'\".,<>(){}[]!@#$%^&*-_=+");
int pos = 0;
qdim width = 0;
if (pMaxWidth < 10) {
retval += pText;
} else {
while (pText[pos] != 0) {
if (pText[pos] == (qchar)'\r') {
addWord(retval, word, prefix, width, pMaxWidth);
word = "";
prefix = "";
retval += (qchar)'\n'; // prevent expensive UTF-8 => UTF-32 conversion
width = 0; // we are on a new line, so reset width
} else if (pText[pos] == (qchar)'\n') {
if (pText[pos + 1] == (qchar)'\r') { // windows newline?
pos++;
};
addWord(retval, word, prefix, width, pMaxWidth);
word = "";
prefix = "";
retval += (qchar)'\n'; // prevent expensive UTF-8 => UTF-32 conversion
width = 0; // we are on a new line, so reset width
} else if (pText[pos] == (qchar)' ') {
if (word.length() > 0) {
width = addWord(retval, word, prefix, width, pMaxWidth);
word = "";
prefix = "";
};
prefix += pText[pos];
} else if (pText[pos] == txtEsc && pText[pos + 10] == txtAsciiEnd) {
if (word.length() > 0) {
width = addWord(retval, word, prefix, width, pMaxWidth);
word = "";
prefix = "";
};
retval.appendString(&pText[pos], 11); // just add our style, we ignore tabs and only images take space, we'll do something for those in due time
pos += 10; // we'll add one more in a minute
} else if (characters.pos(pText[pos]) >= 0) {
if (word.length() > 0) {
width = addWord(retval, word, prefix, width, pMaxWidth);
word = "";
prefix = "";
};
// these we just add in...
word += pText[pos];
width = addWord(retval, word, prefix, width, pMaxWidth);
word = "";
prefix = "";
} else {
word += pText[pos];
};
pos++;
};
// any last words to add?
if (word.length() > 0) {
width = addWord(retval, word, prefix, width, pMaxWidth);
};
};
return retval;
};
// Get the width of text
qdim oDrawingCanvas::getTextWidth(const qchar *pText, qshort pLen, bool pStyled) {
if (pLen == 0) {
return 0;
}
GDItextSpecStruct lvTextSpec = mTextSpec; // Copy of our text spec we're using
lvTextSpec.mJst = jstLeft; // Set to left justification or our text width fails
// It seems that on the OS4 SDK GDItextWidthJst gives a wrong result on a retina display, so only using it in unicode
GDIdrawTextStruct drawinfo(
mHDC,
0,
0,
(qchar *)pText, // for some reason Omnis never declared this a constant but it doesn't change the buffer (i hope)..
pLen,
&lvTextSpec,
0, // pColumnArray
0, // pColumnCount
pStyled ? 1 : 0, // pFlags: 1 = styled text
mApp,
0 // pColumnJsts
);
qdim width = GDItextWidthJst(&drawinfo) + 2;
return width;
};
// Get the heigth of text
qdim oDrawingCanvas::getTextHeight(const qchar *pText, qdim pWidth, bool pStyled, bool pWrap) {
if (pWrap) {
qstring wrapped = wrapText(pText, pWidth);
return getTextHeight(wrapped.cString(), pWidth, pStyled, false);
} else {
qdim top = 0;
int pos = 0;
bool hasWord = false;
while (pText[pos] != 0) {
if (pText[pos] == '\n') {
top += mFontHeight;
hasWord = false;
} else {
hasWord = true;
};
pos++;
};
if (hasWord) top += mFontHeight;
return top;
};
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Drawing functions
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Draws text with a specific text struct (left aligned, no wrapping or clipping)
void oDrawingCanvas::drawText(const qchar *pText, qpoint pWhere, const GDItextSpecStruct &pTextSpec) {
GDIdrawTextStruct drawinfo(
mHDC,
pWhere.h,
pWhere.v,
(qchar *)pText, // for some reason Omnis never declared this a constant but it doesn't change the buffer (i hope)..
OMstrlen(pText),
(GDItextSpecStruct *)&pTextSpec,
0, // pColumnArray
0, // pColumnCount
1, // pFlags: 1 = styled text
mApp,
0 // pColumnJsts
);
// draw the whole text
GDIdrawTextJst(&drawinfo);
};
// Draws the text clipped within the specified rectangle
qdim oDrawingCanvas::drawText(const qchar *pText, qrect pWhere, qcol pColor, qjst pJst, bool pStyled, bool pWrap) {
if ((pWhere.right <= pWhere.left) || (pWhere.bottom <= pWhere.top)) {
// fluffy don't fit!
return 0;
};
if (pWrap) {
qstring wrapped = wrapText(pText, pWhere.right - pWhere.left + 1);
return drawText(wrapped.cString(), pWhere, pColor, pJst, pStyled, false);
} else {
GDItextSpecStruct lvTextSpec = mTextSpec; // Copy of our text spec we're using
qcol wascol = GDIgetTextColor(mHDC);
qlong len = OMstrlen(pText);
qdim fontheight = mFontHeight;
qshort pos = 0;
qshort start = 0;
qdim top = pWhere.top;
// Need to find a better way to get a unicode character :)
qchar newline = '\n';
qchar space = ' ';
// Set our text color
if (pColor != GDI_COLOR_QDEFAULT) lvTextSpec.mTextColor = pColor;
// setup our justification
if (pJst == jstNone) pJst = lvTextSpec.mJst;
lvTextSpec.mJst = jstLeft;
// now loop through to find our lines or until we're below our drawing rectangle.
// while ((pos <= len) && ((top + fontheight) <= pWhere.bottom)) { // note that we add fontheight to our check because our clipping doesn't work very well with drawTextJst
while ((pos <= len) && (top <= pWhere.bottom)) { // Clipping should be fixed in 6.1
if ((pText[pos] == 0x00) || (pText[pos] == newline)) {
if (pos > start) {
// draw our text...
qdim left = pWhere.left;
// justify
if (pJst != jstLeft) {
qdim width = getTextWidth((qchar *)&pText[start], pos - start, pStyled);
if (pJst == jstRight) {
left += pWhere.width() - width;
} else if (pJst == jstCenter) {
left += (pWhere.width() - width) / 2;
};
};
GDIdrawTextStruct drawinfo(
mHDC,
left,
top,
(qchar *)&pText[start], // for some reason Omnis never declared this a constant but it doesn't change the buffer (i hope)..
pos - start,
&lvTextSpec,
0, // pColumnArray
0, // pColumnCount
pStyled ? 1 : 0, // pFlags: 1 = styled text
mApp,
0 // pColumnJsts
);
// draw the whole text
drawTextJst(&drawinfo, pWhere, true);
if (pStyled) {
// As suggested by Stefan Csomor, check the text we just drew and update our text spec
qshort checkUntil = pos <= len - 11 ? pos : len - 11; // make sure we stay within bounds
qshort checkstyle = start;
while (checkstyle < checkUntil) {
if (pText[checkstyle] == txtEsc && pText[checkstyle + 10] == txtAsciiEnd) {
switch (pText[checkstyle + 1]) {
case txtEscSty: {
lvTextSpec.mSty = (qsty)oBaseComponent::HexToLong((qchar *)&pText[checkstyle + 2]);
}; break;
case txtEscCol: {
lvTextSpec.mTextColor = (qcol)oBaseComponent::HexToLong((qchar *)&pText[checkstyle + 2]);
}; break;
default:
break;
};
checkstyle += 11;
} else {
checkstyle++;
};
};
};
};
// get start position for next line..
start = pos + 1;
top += fontheight;
};
pos++;
};
// restore our text color
GDIsetTextColor(mHDC, wascol);
return top;
};
};
// Draw a icon at this position, height/width of rectangle is only used for centering, no clipping!
qdim oDrawingCanvas::drawIcon(qlong pIconId, qrect pAt, qjst pHorzJst, qjst pVertJst, bool pEnabled) {
EXTBMPref tmpIcon(pIconId);
if (tmpIcon.getIconId() == 0) {
// pIconId could just hold our size info if someone reset the iconid back to 0
return 0;
} else {
qdim pxsize = 16;
ePicSize picsize = tmpIcon.getBmpSize(pIconId);
switch (picsize) {
case ePic16:
pxsize = 16;
break;
case ePic32:
pxsize = 32;
break;
case ePic48:
pxsize = 48;
break;
default:
// don't know the size, assume 48, need to find out how to get our default size...
pxsize = 48;
break;
};
if (clipRect(pAt, true)) {
tmpIcon.draw(mHDC, &pAt, picsize, picNormal, !pEnabled, colNone, qfalse, pHorzJst, pVertJst);
unClip();
};
return pxsize;
};
};
// Draws a line between two points using the current selected pen
void oDrawingCanvas::drawLine(qpoint pFrom, qpoint pTo) {
GDImoveTo(mHDC, &pFrom);
GDIlineTo(mHDC, &pTo);
};
// Draws a line between two points
void oDrawingCanvas::drawLine(qpoint pFrom, qpoint pTo, qdim pWidth, qcol pCol, qpat pPat) {
HPEN newPen = GDIcreatePen(pWidth, pCol, pPat);
HPEN oldPen = GDIselectObject(mHDC, newPen);
qcol oldCol = GDIgetTextColor(mHDC);
GDIsetTextColor(mHDC, pCol); // shouldn't be needed but for some reason in the SDK documentation they also set the text color...
drawLine(pFrom, pTo);
GDIsetTextColor(mHDC, oldCol);
GDIselectObject(mHDC, oldPen);
GDIdeleteObject(newPen); // and clean up...
};
// fills a rectangle using our standard pattern brush
void oDrawingCanvas::drawRect(qrect pRect, qcol pFillColor) {
drawRect(pRect, pFillColor, mBackpatBrush);
};
// fills a rectangle with a certain pattern
void oDrawingCanvas::drawRect(qrect pRect, qcol pFillColor, HBRUSH pBrush) {
qcol wasColor = GDIgetTextColor(mHDC);
GDIsetTextColor(mHDC, pFillColor);
GDIfillRect(mHDC, &pRect, pBrush);
// leave it as it was...
GDIsetTextColor(mHDC, wasColor);
};
// draw a rectangle. Note, if pFillColor is set to GDI_COLOR_QDEFAULT we do not fill the rectangle
void oDrawingCanvas::drawRect(qrect pRect, qcol pFillColor, qcol pBorder) {
qcol wasColor = GDIgetTextColor(mHDC);
if (pFillColor != GDI_COLOR_QDEFAULT) {
drawRect(pRect, pFillColor, GDIgetStockBrush(BLACK_BRUSH));
};
if ((pFillColor != pBorder) && (pBorder != GDI_COLOR_QDEFAULT)) {
HPEN borderPen = GDIcreatePen(1, pBorder, patFill);
HPEN oldPen = GDIselectObject(mHDC, borderPen);
// draw our border
GDIsetTextColor(mHDC, pBorder);
GDIframeRect(mHDC, &pRect);
GDIselectObject(mHDC, oldPen);
GDIdeleteObject(borderPen);
};
// leave it as it was...
GDIsetTextColor(mHDC, wasColor);
};
// draw a rounded rectangle. Note, if pFillColor is set to GDI_COLOR_QDEFAULT we do not fill the rectangle
void oDrawingCanvas::drawRoundedRect(qrect pRect, qdim pDiameter, qcol pFillColor, qcol pBorder) {
qcol wasColor = GDIgetTextColor(mHDC);
if (pFillColor != GDI_COLOR_QDEFAULT) {
GDIsetTextColor(mHDC, pFillColor);
GDIfillRoundRect(mHDC, &pRect, pDiameter, pDiameter, GDIgetStockBrush(BLACK_BRUSH));
};
if ((pFillColor != pBorder) && (pBorder != GDI_COLOR_QDEFAULT)) {
HPEN borderPen = GDIcreatePen(1, pBorder, patFill);
HPEN oldPen = GDIselectObject(mHDC, borderPen);
// draw our border
GDIsetTextColor(mHDC, pBorder);
GDIframeRoundRect(mHDC, &pRect, pDiameter, pDiameter);
GDIselectObject(mHDC, oldPen);
GDIdeleteObject(borderPen);
};
// leave it as it was...
GDIsetTextColor(mHDC, wasColor);
};
// Draws a filled ellipse within the rectangle with a gradient color from top to bottom
void oDrawingCanvas::drawEllipse(qrect pRect, qcol pTop, qcol pBottom, qcol pBorder, qint pSpacing) {
int height = pRect.bottom - pRect.top + 1;
float fHeight = (float)height;
float fHalfHeight = fHeight / 2;
int midX = (pRect.right - pRect.left + 1 - pSpacing) >> 1;
float fMidX = (float)midX;
int lastX = 0;
// get real colors...
pTop = GDIgetRealColor(pTop);
pBottom = GDIgetRealColor(pBottom);
// create our pens
HPEN fillPen = GDIcreatePen(1, pTop, patFill), borderPen;
HPEN oldPen = GDIselectObject(mHDC, fillPen);
if (pBorder != -1) {
borderPen = GDIcreatePen(1, pBorder, patFill);
};
for (int Y = 0; Y < height + 1; Y++) {
float fX;
fX = (float)Y;
fX = (fX - fHalfHeight) / fHalfHeight;
fX = fMidX * sin(acos(fX));
int X = (int)fX;
if ((pTop != pBottom) && (Y != 0)) {
// delete our pen, change our color, and select new pen
GDIselectObject(mHDC, oldPen); // just in case our pen was seleted
GDIdeleteObject(fillPen); // delete the pen we no longer need
// mix our colours
qcol gradient;
unsigned char *Col1 = (unsigned char *)&pTop;
unsigned char *Col2 = (unsigned char *)&pBottom;
unsigned char *Grad = (unsigned char *)&gradient;
for (int cnt = 0; cnt < sizeof(qcol); cnt++) {
if (*Col1 == *Col2) {
*Grad = *Col1;
} else {
int diff = *Col2 - *Col1;
diff = diff * Y;
diff = diff / height;
diff = diff + *Col1;
*Grad = diff & 0xFF;
};
Col1++;
Col2++;
Grad++;
};
// create a new pen with our new colour
fillPen = GDIcreatePen(1, gradient, patFill);
GDIselectObject(mHDC, fillPen);
};
if (pBorder == -1) {
// no border? then draw as is
GDImoveTo(mHDC, pRect.left + midX - X, pRect.top + Y);
GDIlineTo(mHDC, pRect.left + midX + pSpacing + X, pRect.top + Y);
} else if ((Y > 0) && (Y < height)) {
// only fill if we're not going over our border
int left = pRect.left + midX - X + 1;
int right = pRect.left + midX + pSpacing + X - 1;
if (left <= right) {
GDImoveTo(mHDC, left, pRect.top + Y);
GDIlineTo(mHDC, right, pRect.top + Y);
};
};
if (pBorder != -1) {
// select our border pen
GDIselectObject(mHDC, borderPen);
// move to our starting position
if (Y == 0) {
// ---
GDImoveTo(mHDC, pRect.left + midX - X, pRect.top + Y);
GDIlineTo(mHDC, pRect.left + midX + pSpacing + X, pRect.top + Y);
} else if ((Y > 0) && (Y < height)) {
// | |
GDImoveTo(mHDC, pRect.left + midX - lastX, pRect.top + Y - 1);
GDIlineTo(mHDC, pRect.left + midX - X, pRect.top + Y);
GDImoveTo(mHDC, pRect.left + midX + pSpacing + X, pRect.top + Y);
GDIlineTo(mHDC, pRect.left + midX + pSpacing + lastX, pRect.top + Y - 1);
} else {
// \ /
// ---
GDImoveTo(mHDC, pRect.left + midX - lastX, pRect.top + Y - 1);
GDIlineTo(mHDC, pRect.left + midX - X, pRect.top + Y);
GDIlineTo(mHDC, pRect.left + midX + pSpacing + X, pRect.top + Y);
GDIlineTo(mHDC, pRect.left + midX + pSpacing + lastX, pRect.top + Y - 1);
};
GDIselectObject(mHDC, fillPen);
lastX = X;
};
};
GDIselectObject(mHDC, oldPen);
GDIdeleteObject(fillPen);
if (pBorder != -1) {
GDIdeleteObject(borderPen);
};
};