/
ui_carbon.c
298 lines (236 loc) · 8.38 KB
1
2
3
4
5
#if !PLATFORM_MACOSX
int ui_init_carbon(void) { return(0); } /* not implemented if not MacOS. */
#else
6
7
8
9
10
11
12
13
14
#include <Carbon/Carbon.h>
#include "platform.h"
#include "ui.h"
#define MOJOPATCH_SIG 'mjpt'
#define MOJOPATCH_STATUS_ID 0
#define MOJOPATCH_PROGRESS_ID 1
15
static WindowRef window;
16
17
18
static ControlRef progress;
static ControlRef status;
19
static void ui_pump_carbon(void)
20
{
21
22
EventRef theEvent;
EventTargetRef theTarget;
23
24
25
theTarget = GetEventDispatcherTarget();
if (ReceiveNextEvent(0, NULL, 0, true, &theEvent) == noErr)
26
{
27
28
SendEventToEventTarget(theEvent, theTarget);
ReleaseEvent(theEvent);
29
} /* if */
30
} /* ui_pump_carbon */
31
32
33
static void ui_title_carbon(const char *str)
34
{
35
36
37
CFStringRef cfstr = CFStringCreateWithBytes(NULL,
(const unsigned char *) str,
strlen(str),
38
39
kCFStringEncodingISOLatin1, 0);
SetWindowTitleWithCFString(window, cfstr);
40
CFRelease(cfstr);
41
42
ui_pump_carbon();
} /* ui_title_carbon */
43
44
45
static void ui_real_deinit_carbon(void)
46
47
{
/* !!! FIXME */
48
} /* ui_real_deinit_carbon */
49
50
51
static void ui_add_to_log_carbon(const char *str, int debugging)
52
{
53
54
55
56
/*
* stdout in a Mac GUI app shows up in the system console, which can be
* viewed via /Applications/Utilities/Console.app ...
*/
57
58
/* !!! FIXME */
59
printf("MojoPatch%s: %s\n", debugging ? " [debug]" : "", str);
60
} /* ui_add_to_log_carbon */
61
62
63
64
65
static int do_msgbox(const char *str, AlertType alert_type,
AlertStdCFStringAlertParamRec *param,
DialogItemIndex *idx)
66
67
{
const char *_title = "MojoPatch";
68
69
int retval = 0;
DialogItemIndex val = 0;
70
71
72
CFStringRef title = CFStringCreateWithBytes(NULL,
(const unsigned char *) _title,
strlen(_title),
73
kCFStringEncodingISOLatin1, 0);
74
75
76
77
CFStringRef msg = CFStringCreateWithBytes(NULL,
(const unsigned char *) str,
strlen(str),
kCFStringEncodingISOLatin1, 0);
78
79
80
81
if ((msg != NULL) && (title != NULL))
{
DialogRef dlg = NULL;
82
83
84
85
86
if (CreateStandardAlert(alert_type, title, msg, param, &dlg) == noErr)
{
RunStandardAlert(dlg, NULL, (idx) ? idx : &val);
retval = 1;
} /* if */
87
88
89
90
91
92
93
} /* if */
if (msg != NULL)
CFRelease(msg);
if (title != NULL)
CFRelease(title);
94
95
return(retval);
96
97
98
} /* do_msgbox */
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
static int ui_prompt_yes_or_no(const char *question, int yes)
{
OSStatus err;
DialogItemIndex item;
AlertStdCFStringAlertParamRec params;
err = GetStandardAlertDefaultParams(¶ms, kStdCFStringAlertVersionOne);
if (err != noErr)
return(0);
params.movable = TRUE;
params.helpButton = FALSE;
params.defaultText = CFSTR("Yes");
params.cancelText = CFSTR("No");
params.defaultButton = (yes) ? kAlertStdAlertOKButton :
kAlertStdAlertCancelButton;
params.cancelButton = kAlertStdAlertCancelButton;
if (!do_msgbox(question, kAlertCautionAlert, ¶ms, &item))
return(0); /* oh well. */
return(item == kAlertStdAlertOKButton);
} /* ui_prompt_yes_or_no */
121
122
static int ui_prompt_yn_carbon(const char *question)
123
124
{
return(ui_prompt_yes_or_no(question, 1));
125
} /* ui_prompt_yn_carbon */
126
127
128
static int ui_prompt_ny_carbon(const char *question)
129
130
{
return(ui_prompt_yes_or_no(question, 1)); /* !!! FIXME! should be zero. */
131
} /* ui_prompt_ny_carbon */
132
133
134
static int ui_file_picker_carbon(char *buf, size_t bufsize)
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
NavDialogCreationOptions dlgopt;
NavDialogRef dlg;
NavReplyRecord reply;
NavUserAction action;
AEKeyword keyword;
AEDesc desc;
FSRef fsref;
OSStatus rc;
int retval = 0;
NavGetDefaultDialogCreationOptions(&dlgopt);
dlgopt.optionFlags |= kNavSupportPackages;
dlgopt.optionFlags |= kNavAllowOpenPackages;
dlgopt.optionFlags &= ~kNavAllowMultipleFiles;
150
dlgopt.windowTitle = CFSTR("Please select the product's icon and click 'OK'."); /* !!! FIXME! */
151
152
153
154
155
156
157
158
159
160
161
dlgopt.actionButtonLabel = CFSTR("OK");
NavCreateChooseFolderDialog(&dlgopt, NULL, NULL, NULL, &dlg);
NavDialogRun(dlg);
action = NavDialogGetUserAction(dlg);
if (action == kNavUserActionCancel)
_log("User cancelled file selector!");
else
{
NavDialogGetReply(dlg, &reply);
rc = AEGetNthDesc(&reply.selection, 1, typeFSRef, &keyword, &desc);
if (rc != noErr)
162
_fatal("Unexpected error in AEGetNthDesc: %d", (int) rc);
163
164
165
166
else
{
/* !!! FIXME: Check return values here! */
BlockMoveData(*desc.dataHandle, &fsref, sizeof (fsref));
167
FSRefMakePath(&fsref, (unsigned char *) buf, bufsize - 1);
168
169
170
171
172
173
174
175
176
177
178
179
180
181
buf[bufsize - 1] = '\0';
AEDisposeDesc(&desc);
retval = 1;
} /* if */
NavDisposeReply(&reply);
} /* else */
NavDialogDispose(dlg);
_log("File selector complete. User %s path.",
retval ? "selected" : "did NOT select");
return(retval);
182
} /* ui_file_picker_carbon */
183
184
185
static void ui_fatal_carbon(const char *str)
186
{
187
do_msgbox(str, kAlertStopAlert, NULL, NULL);
188
189
190
} /* ui_fatal */
191
static void ui_success_carbon(const char *str)
192
{
193
do_msgbox(str, kAlertNoteAlert, NULL, NULL);
194
} /* ui_success_carbon */
195
196
197
static void ui_msgbox_carbon(const char *str)
198
199
{
do_msgbox(str, kAlertNoteAlert, NULL, NULL);
200
} /* ui_msgbox_carbon */
201
202
203
static void ui_total_progress_carbon(int percent)
204
205
206
207
208
209
210
211
212
213
214
{
static int lastpercent = -1;
if (percent != lastpercent)
{
Boolean indeterminate = (percent < 0) ? TRUE : FALSE;
SetControlData(progress, kControlEntireControl,
kControlProgressBarIndeterminateTag,
sizeof (indeterminate), &indeterminate);
SetControl32BitValue(progress, percent);
lastpercent = percent;
} /* if */
215
} /* ui_total_progress_carbon */
216
217
218
static void ui_status_carbon(const char *str)
219
220
221
222
{
SetControlData(status, kControlEditTextPart, kControlStaticTextTextTag,
strlen(str), str);
Draw1Control(status);
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
} /* ui_status_carbon */
static int ui_show_readme_carbon(const char *fname, const char *text)
{
/*
* Just let the Finder pick the right program to view the file...
* this lets you ship with a .txt, .html, .rtf, or whatever, for a
* readme on this platform.
*/
size_t allocsize = strlen(fname) + 32;
char *cmd = (char *) alloca(allocsize);
if (!cmd)
{
_fatal("Out of memory.");
return(0);
} /* if */
snprintf(cmd, allocsize, "open %s", fname);
system(cmd); /* !!! FIXME: error check? */
return(1);
} /* ui_show_readme_carbon */
/* user interface stuff you implement. */
int ui_init_carbon(void)
{
ControlID statusID = { MOJOPATCH_SIG, MOJOPATCH_STATUS_ID };
ControlID progressID = { MOJOPATCH_SIG, MOJOPATCH_PROGRESS_ID };
IBNibRef nibRef = NULL;
OSStatus err;
Boolean b = TRUE;
/* !!! FIXME: This is corrupting the "basedir" variable in platform_unix.c ! */
if (CreateNibReference(CFSTR("mojopatch"), &nibRef) != noErr)
{
fprintf(stderr, "MOJOPATCH: Carbon UI failed to initialize!\n");
fprintf(stderr, "MOJOPATCH: You probably don't have a .nib file!\n");
return(0); /* usually .nib isn't found. */
} /* if */
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
if (err == noErr)
err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
DisposeNibReference( nibRef );
if (err == noErr)
err = GetControlByID(window, &statusID, &status);
if (err == noErr)
err = GetControlByID(window, &progressID, &progress);
if (err == noErr)
{
ShowWindow(window);
err = ActivateWindow(window, TRUE);
} /* if */
if (err == noErr)
{
err = SetControlData(progress, kControlEntireControl,
kControlProgressBarAnimatingTag,
sizeof (b), &b);
} /* if */
if (err == noErr);
UI_SET_FUNC_POINTERS(carbon);
return(err == noErr);
} /* ui_init_carbon */
#endif
296
297
/* end of ui_carbon.c ... */