Skip to content

Latest commit

 

History

History
298 lines (236 loc) · 8.38 KB

ui_carbon.c

File metadata and controls

298 lines (236 loc) · 8.38 KB
 
Apr 22, 2005
Apr 22, 2005
1
2
3
4
5
#if !PLATFORM_MACOSX
int ui_init_carbon(void) { return(0); } /* not implemented if not MacOS. */
#else
Jan 5, 2004
Jan 5, 2004
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
Aug 14, 2004
Aug 14, 2004
15
static WindowRef window;
Jan 5, 2004
Jan 5, 2004
16
17
18
static ControlRef progress;
static ControlRef status;
Apr 22, 2005
Apr 22, 2005
19
static void ui_pump_carbon(void)
Jan 5, 2004
Jan 5, 2004
20
{
Apr 22, 2005
Apr 22, 2005
21
22
EventRef theEvent;
EventTargetRef theTarget;
May 25, 2004
May 25, 2004
23
Apr 22, 2005
Apr 22, 2005
24
25
theTarget = GetEventDispatcherTarget();
if (ReceiveNextEvent(0, NULL, 0, true, &theEvent) == noErr)
May 25, 2004
May 25, 2004
26
{
Apr 22, 2005
Apr 22, 2005
27
28
SendEventToEventTarget(theEvent, theTarget);
ReleaseEvent(theEvent);
May 25, 2004
May 25, 2004
29
} /* if */
Apr 22, 2005
Apr 22, 2005
30
} /* ui_pump_carbon */
Jan 5, 2004
Jan 5, 2004
31
32
Apr 22, 2005
Apr 22, 2005
33
static void ui_title_carbon(const char *str)
Jan 5, 2004
Jan 5, 2004
34
{
Jan 27, 2006
Jan 27, 2006
35
36
37
CFStringRef cfstr = CFStringCreateWithBytes(NULL,
(const unsigned char *) str,
strlen(str),
Jan 5, 2004
Jan 5, 2004
38
39
kCFStringEncodingISOLatin1, 0);
SetWindowTitleWithCFString(window, cfstr);
Feb 26, 2005
Feb 26, 2005
40
CFRelease(cfstr);
Apr 22, 2005
Apr 22, 2005
41
42
ui_pump_carbon();
} /* ui_title_carbon */
Jan 5, 2004
Jan 5, 2004
43
44
Apr 22, 2005
Apr 22, 2005
45
static void ui_real_deinit_carbon(void)
Jan 5, 2004
Jan 5, 2004
46
47
{
/* !!! FIXME */
Apr 22, 2005
Apr 22, 2005
48
} /* ui_real_deinit_carbon */
Jan 5, 2004
Jan 5, 2004
49
50
Apr 22, 2005
Apr 22, 2005
51
static void ui_add_to_log_carbon(const char *str, int debugging)
Jan 5, 2004
Jan 5, 2004
52
{
Apr 22, 2005
Apr 22, 2005
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 ...
*/
May 25, 2004
May 25, 2004
57
May 30, 2004
May 30, 2004
58
/* !!! FIXME */
Jan 5, 2004
Jan 5, 2004
59
printf("MojoPatch%s: %s\n", debugging ? " [debug]" : "", str);
Apr 22, 2005
Apr 22, 2005
60
} /* ui_add_to_log_carbon */
Jan 5, 2004
Jan 5, 2004
61
62
May 25, 2004
May 25, 2004
63
64
65
static int do_msgbox(const char *str, AlertType alert_type,
AlertStdCFStringAlertParamRec *param,
DialogItemIndex *idx)
Jan 5, 2004
Jan 5, 2004
66
67
{
const char *_title = "MojoPatch";
May 25, 2004
May 25, 2004
68
69
int retval = 0;
DialogItemIndex val = 0;
Jan 27, 2006
Jan 27, 2006
70
71
72
CFStringRef title = CFStringCreateWithBytes(NULL,
(const unsigned char *) _title,
strlen(_title),
Jan 5, 2004
Jan 5, 2004
73
kCFStringEncodingISOLatin1, 0);
Jan 27, 2006
Jan 27, 2006
74
75
76
77
CFStringRef msg = CFStringCreateWithBytes(NULL,
(const unsigned char *) str,
strlen(str),
kCFStringEncodingISOLatin1, 0);
Jan 5, 2004
Jan 5, 2004
78
79
80
81
if ((msg != NULL) && (title != NULL))
{
DialogRef dlg = NULL;
May 25, 2004
May 25, 2004
82
83
84
85
86
if (CreateStandardAlert(alert_type, title, msg, param, &dlg) == noErr)
{
RunStandardAlert(dlg, NULL, (idx) ? idx : &val);
retval = 1;
} /* if */
Jan 5, 2004
Jan 5, 2004
87
88
89
90
91
92
93
} /* if */
if (msg != NULL)
CFRelease(msg);
if (title != NULL)
CFRelease(title);
May 25, 2004
May 25, 2004
94
95
return(retval);
Jan 5, 2004
Jan 5, 2004
96
97
98
} /* do_msgbox */
May 25, 2004
May 25, 2004
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(&params, 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, &params, &item))
return(0); /* oh well. */
return(item == kAlertStdAlertOKButton);
} /* ui_prompt_yes_or_no */
Apr 22, 2005
Apr 22, 2005
121
122
static int ui_prompt_yn_carbon(const char *question)
May 25, 2004
May 25, 2004
123
124
{
return(ui_prompt_yes_or_no(question, 1));
Apr 22, 2005
Apr 22, 2005
125
} /* ui_prompt_yn_carbon */
May 25, 2004
May 25, 2004
126
Apr 22, 2005
Apr 22, 2005
127
128
static int ui_prompt_ny_carbon(const char *question)
May 25, 2004
May 25, 2004
129
130
{
return(ui_prompt_yes_or_no(question, 1)); /* !!! FIXME! should be zero. */
Apr 22, 2005
Apr 22, 2005
131
} /* ui_prompt_ny_carbon */
May 25, 2004
May 25, 2004
132
133
Apr 22, 2005
Apr 22, 2005
134
static int ui_file_picker_carbon(char *buf, size_t bufsize)
May 25, 2004
May 25, 2004
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;
Apr 22, 2005
Apr 22, 2005
150
dlgopt.windowTitle = CFSTR("Please select the product's icon and click 'OK'."); /* !!! FIXME! */
May 25, 2004
May 25, 2004
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)
Apr 22, 2005
Apr 22, 2005
162
_fatal("Unexpected error in AEGetNthDesc: %d", (int) rc);
May 25, 2004
May 25, 2004
163
164
165
166
else
{
/* !!! FIXME: Check return values here! */
BlockMoveData(*desc.dataHandle, &fsref, sizeof (fsref));
Jan 27, 2006
Jan 27, 2006
167
FSRefMakePath(&fsref, (unsigned char *) buf, bufsize - 1);
May 25, 2004
May 25, 2004
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);
Apr 22, 2005
Apr 22, 2005
182
} /* ui_file_picker_carbon */
May 25, 2004
May 25, 2004
183
184
Apr 22, 2005
Apr 22, 2005
185
static void ui_fatal_carbon(const char *str)
Jan 5, 2004
Jan 5, 2004
186
{
Apr 22, 2005
Apr 22, 2005
187
do_msgbox(str, kAlertStopAlert, NULL, NULL);
Jan 5, 2004
Jan 5, 2004
188
189
190
} /* ui_fatal */
Apr 22, 2005
Apr 22, 2005
191
static void ui_success_carbon(const char *str)
Jan 5, 2004
Jan 5, 2004
192
{
May 25, 2004
May 25, 2004
193
do_msgbox(str, kAlertNoteAlert, NULL, NULL);
Apr 22, 2005
Apr 22, 2005
194
} /* ui_success_carbon */
Jan 5, 2004
Jan 5, 2004
195
196
Apr 22, 2005
Apr 22, 2005
197
static void ui_msgbox_carbon(const char *str)
May 30, 2004
May 30, 2004
198
199
{
do_msgbox(str, kAlertNoteAlert, NULL, NULL);
Apr 22, 2005
Apr 22, 2005
200
} /* ui_msgbox_carbon */
May 30, 2004
May 30, 2004
201
202
Apr 22, 2005
Apr 22, 2005
203
static void ui_total_progress_carbon(int percent)
Jan 5, 2004
Jan 5, 2004
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 */
Apr 22, 2005
Apr 22, 2005
215
} /* ui_total_progress_carbon */
Jan 5, 2004
Jan 5, 2004
216
217
Apr 22, 2005
Apr 22, 2005
218
static void ui_status_carbon(const char *str)
Jan 5, 2004
Jan 5, 2004
219
220
221
222
{
SetControlData(status, kControlEditTextPart, kControlStaticTextTextTag,
strlen(str), str);
Draw1Control(status);
Apr 22, 2005
Apr 22, 2005
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
Jan 5, 2004
Jan 5, 2004
296
297
/* end of ui_carbon.c ... */