Skip to content

Commit 6312ec9

Browse files
committed
Not using NewCWindow and GWorld if ColorQD is not present
1 parent 97aa60f commit 6312ec9

10 files changed

Lines changed: 124 additions & 27 deletions

File tree

examples/kitchen-sink/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int main() {
4848

4949
tests["Message box"] = []() {
5050
app->CKNewMsgBoxNote("If you are seeing this, then compile was fine!", "Hello world", "OK", "Quit", [](int button) {
51-
CKLog("Got button index: %d", button);
51+
// CKLog("Got button index: %d", button);
5252
if (button == 0) {
5353
app->CKQuit();
5454
}

include/ckCanvas.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ class CKCanvas : public CKControl {
3636
bool DrawResourceIcon(short resourceId, CKPoint where);
3737

3838
private:
39-
GWorldPtr __gworldptr;
40-
};
39+
GWorldPtr __gworldptr = NULL;
40+
};

include/ckTypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#pragma once
1515

16+
#include "ckPlatform.h"
1617
#include "ckMacros.h"
1718
#include "ckObject.h"
1819
#include "ckProperty.h"
@@ -544,4 +545,4 @@ enum class CKScrollType {
544545
vertical = 1,
545546
horizontal = 2,
546547
both = 3
547-
};
548+
};

include/ckUtils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void CKConsolePrint(const char* toPrint);
9797
* Profiling
9898
*/
9999
#ifdef kCKAPPDEBUG
100+
#include <Timer.h>
100101
#include <vector>
101102
class CKProfilerData {
102103
public:
@@ -153,4 +154,6 @@ void __CKWriteToExitFile(const char* s, ...);
153154
#endif
154155

155156
bool CKHasAppearanceManager();
156-
UInt32 CKMillis();
157+
bool CKHasColorQuickDraw();
158+
bool CKHasDebugger();
159+
UInt32 CKMillis();

src/ckApp.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ CKWindow* CKApp::__CreateAlertDialog(const char* message, const char* title, con
371371
CKWindow* toReturn = this->CKNewWindow(params);
372372

373373
int labelX = padding;
374-
if (icon != CKSystemIcon::noIcon) {
374+
const bool shouldShowIcon = icon != CKSystemIcon::noIcon && CKHasColorQuickDraw();
375+
if (shouldShowIcon) {
375376
labelX += 32 + padding;
376377
}
377378
CKLabel* label = CKNew CKLabel({message, CKRect(labelX, padding, params.size.width - (padding + labelX), 0)});
@@ -382,7 +383,7 @@ CKWindow* CKApp::__CreateAlertDialog(const char* message, const char* title, con
382383
toReturn->AddControl(label);
383384

384385
CKCanvas* iconCanvas;
385-
if (icon != CKSystemIcon::noIcon) {
386+
if (shouldShowIcon) {
386387
iconCanvas = CKNew CKCanvas(CKSize(32, 32));
387388
iconCanvas->rect->origin->x = padding;
388389
iconCanvas->rect->origin->y = padding;
@@ -1203,4 +1204,4 @@ void CKApp::CKUpdateMenuBarItems() {
12031204
}
12041205
}
12051206
}
1206-
}
1207+
}

src/ckCanvas.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ CKCanvas::CKCanvas(const CKControlInitParams& params)
2121

2222
CKPROFILE
2323

24+
this->__gworldptr = NULL;
25+
26+
if (!CKHasColorQuickDraw()) {
27+
CKLog("CKCanvas requires Color QuickDraw; offscreen drawing disabled.");
28+
return;
29+
}
30+
2431
Rect bounds = {0, 0, (short)params.height, (short)params.width};
2532
OSErr err = NewGWorld(&(this->__gworldptr), 16, &bounds, NULL, NULL, 0);
2633
if (err != noErr) {
@@ -48,6 +55,10 @@ void CKCanvas::Redraw() {
4855
return;
4956
}
5057

58+
if (!this->__gworldptr) {
59+
return;
60+
}
61+
5162
#ifdef CKDEBUGTIMEDRAWS
5263
long t_start = TickCount();
5364
#endif
@@ -91,6 +102,10 @@ void CKCanvas::FillRect(CKRect rect, CKColor c) {
91102

92103
CKPROFILE
93104

105+
if (!this->__gworldptr) {
106+
return;
107+
}
108+
94109
RGBColor rgb = c.ToOS();
95110
PixPatHandle pixPat = NewPixPat();
96111
if (pixPat != NULL) {
@@ -133,6 +148,10 @@ void CKCanvas::SetPixel(CKPoint p, CKColor c) {
133148

134149
CKPROFILE
135150

151+
if (!this->__gworldptr) {
152+
return;
153+
}
154+
136155
if (p.x < 0 || p.x > this->rect->size->width) {
137156
return;
138157
}
@@ -160,6 +179,10 @@ void CKCanvas::SetPixel(CKPoint p, CKColor c) {
160179

161180
void CKCanvas::DrawLine(CKPoint start, CKPoint end, CKColor c) {
162181

182+
if (!this->__gworldptr) {
183+
return;
184+
}
185+
163186
RGBColor rgb = c.ToOS();
164187
CGrafPtr oldPort;
165188
GDHandle oldGD;
@@ -175,6 +198,10 @@ void CKCanvas::DrawLine(CKPoint start, CKPoint end, CKColor c) {
175198

176199
bool CKCanvas::DrawResourceIcon(short resourceId, CKPoint where) {
177200

201+
if (!this->__gworldptr) {
202+
return false;
203+
}
204+
178205
PixMapHandle offscreenPixMap = GetGWorldPixMap(this->__gworldptr);
179206
if (!LockPixels(offscreenPixMap)) {
180207
CKLog("Can't lock GWorldPixMap.. Purged, maybe?");

src/ckTextArea.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,19 @@ void CKTextArea::Redraw() {
178178

179179
if (this->__needsFullRedraw) {
180180
if (this->enabled) {
181-
RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
182-
RGBForeColor(&white);
181+
if (CKHasColorQuickDraw()) {
182+
RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
183+
RGBForeColor(&white);
184+
} else {
185+
ForeColor(whiteColor);
186+
}
183187
} else {
184-
RGBColor gray = {0xC000, 0xC000, 0xC000};
185-
RGBForeColor(&gray);
188+
if (CKHasColorQuickDraw()) {
189+
RGBColor gray = {0xC000, 0xC000, 0xC000};
190+
RGBForeColor(&gray);
191+
} else {
192+
ForeColor(blackColor);
193+
}
186194
}
187195
EraseRect(&cr);
188196
this->__needsFullRedraw = false;
@@ -357,4 +365,4 @@ void CKTextArea::__UpdateTextScroll(int vDelta, int hDelta) {
357365
TEPinScroll(-hDelta, -vDelta, this->__teHandle);
358366
this->__needsPreparing = true;
359367
this->MarkAsDirty();
360-
}
368+
}

src/ckTextField.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@ void CKTextField::Redraw() {
4343
} else {
4444

4545
if (this->enabled) {
46-
RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
47-
RGBForeColor(&white);
46+
if (CKHasColorQuickDraw()) {
47+
RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
48+
RGBForeColor(&white);
49+
} else {
50+
ForeColor(whiteColor);
51+
}
4852
} else {
49-
RGBColor gray = {0xC000, 0xC000, 0xC000};
50-
RGBForeColor(&gray);
53+
if (CKHasColorQuickDraw()) {
54+
RGBColor gray = {0xC000, 0xC000, 0xC000};
55+
RGBForeColor(&gray);
56+
} else {
57+
ForeColor(blackColor);
58+
}
5159
}
5260
PaintRect(&r);
5361
ForeColor(blackColor);
@@ -101,12 +109,20 @@ void CKTextField::PrepareForDraw() {
101109
trecord->fontAscent = this->fontSize;
102110

103111
if (this->enabled) {
104-
RGBColor color = this->color.get().ToOS();
105-
RGBForeColor(&color);
112+
if (CKHasColorQuickDraw()) {
113+
RGBColor color = this->color.get().ToOS();
114+
RGBForeColor(&color);
115+
} else {
116+
ForeColor(blackColor);
117+
}
106118
} else {
107119
// 50% gray.
108-
RGBColor gray = {0x8000, 0x8000, 0x8000};
109-
RGBForeColor(&gray);
120+
if (CKHasColorQuickDraw()) {
121+
RGBColor gray = {0x8000, 0x8000, 0x8000};
122+
RGBForeColor(&gray);
123+
} else {
124+
ForeColor(blackColor);
125+
}
110126
}
111127

112128
// TODO: This padding works perfectly fine for
@@ -166,4 +182,4 @@ bool CKTextField::CanPerformPaste() {
166182
long offset; // TODO: needed?
167183
long r = GetScrap(nil, 'TEXT', &offset);
168184
return r != noTypeErr;
169-
}
185+
}

src/ckUtils.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
#include <Appearance.h>
1616
#include <Gestalt.h>
1717
#include <Timer.h>
18+
#include <Quickdraw.h>
19+
20+
#ifndef gestaltQuickdrawFeatures
21+
#define gestaltQuickdrawFeatures 'qd '
22+
#endif
23+
24+
#ifndef gestaltHasColor
25+
#define gestaltHasColor 0
26+
#endif
27+
28+
#ifndef gestaltHasDeepGWorlds
29+
#define gestaltHasDeepGWorlds 1
30+
#endif
1831

1932
#ifdef kCKAPPDEBUG
2033
std::vector<CKProfilerData*> _profilerData;
@@ -260,6 +273,21 @@ bool CKHasAppearanceManager() {
260273
return (Gestalt(gestaltAppearanceAttr, &result) == noErr) && (result & (1 << gestaltAppearanceExists));
261274
}
262275

276+
/**
277+
* @ingroup Utils
278+
* @brief Checks if Color QuickDraw is available.
279+
* @return True if Color QuickDraw is available
280+
*/
281+
bool CKHasColorQuickDraw() {
282+
283+
long result;
284+
if (Gestalt(gestaltQuickdrawFeatures, &result) != noErr) {
285+
return false;
286+
}
287+
288+
return (result & (1 << gestaltHasColor)) != 0;
289+
}
290+
263291
/**
264292
* @ingroup Utils
265293
* @brief Return the number of milliseconds since computer booted up.
@@ -270,4 +298,4 @@ UInt32 CKMillis() {
270298
UnsignedWide usec;
271299
Microseconds(&usec);
272300
return (usec.hi << (32 - 10)) | (usec.lo >> 10);
273-
}
301+
}

src/ckWindow.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ CKWindow::CKWindow(CKWindowInitParams params)
6464
}
6565

6666
this->__type = params.type;
67-
this->__windowPtr = NewCWindow(nil, &r, "\p", false, windowProcId, 0, this->closable, 0);
67+
if (CKHasColorQuickDraw()) {
68+
this->__windowPtr = NewCWindow(nil, &r, "\p", false, windowProcId, 0, this->closable, 0);
69+
} else {
70+
this->__windowPtr = NewWindow(nil, &r, "\p", false, windowProcId, 0, this->closable, 0);
71+
}
72+
73+
if (!this->__windowPtr) {
74+
throw CKNew CKException("Can't create window.");
75+
}
6876

6977
if (CKHasAppearanceManager()) {
7078
SetThemeWindowBackground(this->__windowPtr, kThemeBrushDialogBackgroundActive, true);
@@ -316,8 +324,13 @@ void CKWindow::Redraw(CKRect rectToRedraw) {
316324

317325
if (this->hasCustomBackgroundColor) {
318326
// Use the user's picked color.
319-
RGBColor c = this->backgroundColor->ToOS();
320-
RGBBackColor(&c);
327+
if (CKHasColorQuickDraw()) {
328+
RGBColor c = this->backgroundColor->ToOS();
329+
RGBBackColor(&c);
330+
} else {
331+
// No color QuickDraw – fall back to black/white palette.
332+
BackColor(whiteColor);
333+
}
321334
EraseRect(&r);
322335
} else if (CKHasAppearanceManager()) {
323336
// Mac OS 8 and above: use theme color.
@@ -609,4 +622,4 @@ void CKWindow::RaisePropertyChange(const char* propertyName) {
609622
this->__InvalidateEntireWindow();
610623
}
611624
CKObject::RaisePropertyChange(propertyName);
612-
}
625+
}

0 commit comments

Comments
 (0)