/
ui_carbon.c
274 lines (221 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
#include <Carbon/Carbon.h>
#include "platform.h"
#include "ui.h"
#define MOJOPATCH_SIG 'mjpt'
#define MOJOPATCH_STATUS_ID 0
#define MOJOPATCH_PROGRESS_ID 1
static WindowPtr window;
static ControlRef progress;
static ControlRef status;
13
static int carbon_ui_initialized = 0;
14
15
16
17
18
19
20
21
22
23
/* user interface stuff you implement. */
int ui_init(void)
{
ControlID statusID = { MOJOPATCH_SIG, MOJOPATCH_STATUS_ID };
ControlID progressID = { MOJOPATCH_SIG, MOJOPATCH_PROGRESS_ID };
IBNibRef nibRef;
OSStatus err;
Boolean b = TRUE;
24
25
26
27
if (carbon_ui_initialized) /* already initialized? */
return(1);
if (CreateNibReference(CFSTR("mojopatch"), &nibRef) != noErr)
28
29
{
fprintf(stderr, "MOJOPATCH: You probably don't have a .nib file!\n");
30
return(0); /* usually .nib isn't found. */
31
} /* if */
32
33
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
34
35
if (err == noErr)
err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
36
DisposeNibReference( nibRef );
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 */
carbon_ui_initialized = 1;
return(err == noErr);
59
60
61
62
63
64
65
66
} /* ui_init */
void ui_title(const char *str)
{
CFStringRef cfstr = CFStringCreateWithBytes(NULL, str, strlen(str),
kCFStringEncodingISOLatin1, 0);
SetWindowTitleWithCFString(window, cfstr);
67
ui_pump();
68
69
70
71
72
73
} /* ui_title */
void ui_deinit(void)
{
/* !!! FIXME */
74
/* carbon_ui_initialized = 0; */
75
76
77
78
79
80
} /* ui_deinit */
void ui_pump(void)
{
EventRef theEvent;
81
82
83
84
85
86
EventTargetRef theTarget;
if (!carbon_ui_initialized)
return;
theTarget = GetEventDispatcherTarget();
87
88
89
90
91
92
93
94
95
96
if (ReceiveNextEvent(0, NULL, 0, true, &theEvent) == noErr)
{
SendEventToEventTarget(theEvent, theTarget);
ReleaseEvent(theEvent);
} /* if */
} /* ui_pump */
void ui_add_to_log(const char *str, int debugging)
{
97
/* !!! FIXME */
98
99
100
101
printf("MojoPatch%s: %s\n", debugging ? " [debug]" : "", str);
} /* ui_add_to_log */
102
103
104
static int do_msgbox(const char *str, AlertType alert_type,
AlertStdCFStringAlertParamRec *param,
DialogItemIndex *idx)
105
106
{
const char *_title = "MojoPatch";
107
108
int retval = 0;
DialogItemIndex val = 0;
109
110
111
112
113
114
115
116
CFStringRef title = CFStringCreateWithBytes(NULL, _title, strlen(_title),
kCFStringEncodingISOLatin1, 0);
CFStringRef msg = CFStringCreateWithBytes(NULL, str, strlen(str),
kCFStringEncodingISOLatin1, 0);
if ((msg != NULL) && (title != NULL))
{
DialogRef dlg = NULL;
117
118
119
120
121
if (CreateStandardAlert(alert_type, title, msg, param, &dlg) == noErr)
{
RunStandardAlert(dlg, NULL, (idx) ? idx : &val);
retval = 1;
} /* if */
122
123
124
125
126
127
128
} /* if */
if (msg != NULL)
CFRelease(msg);
if (title != NULL)
CFRelease(title);
129
130
return(retval);
131
132
133
} /* do_msgbox */
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
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 */
int ui_prompt_yn(const char *question)
{
return(ui_prompt_yes_or_no(question, 1));
} /* ui_prompt_yn */
int ui_prompt_ny(const char *question)
{
return(ui_prompt_yes_or_no(question, 1)); /* !!! FIXME! should be zero. */
} /* ui_prompt_ny */
167
int manually_locate_product(const char *name, char *buf, size_t bufsize)
168
169
170
171
172
173
174
175
176
177
178
{
NavDialogCreationOptions dlgopt;
NavDialogRef dlg;
NavReplyRecord reply;
NavUserAction action;
AEKeyword keyword;
AEDesc desc;
FSRef fsref;
OSStatus rc;
int retval = 0;
int yn;
179
180
181
const char *promptfmt = "We can't find your \"%s\" installation."
" Would you like to show us where it is?";
char *promptstr = alloca(strlen(name) + strlen(promptfmt) + 1);
182
183
184
185
186
187
188
189
190
if (promptstr == NULL)
{
_fatal("Out of memory.");
return(0);
} /* if */
sprintf(promptstr, promptfmt, name);
yn = ui_prompt_yn(promptstr);
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
if (!yn)
{
_log("User chose not to manually locate installation");
return(0);
} /* if */
NavGetDefaultDialogCreationOptions(&dlgopt);
dlgopt.optionFlags |= kNavSupportPackages;
dlgopt.optionFlags |= kNavAllowOpenPackages;
dlgopt.optionFlags &= ~kNavAllowMultipleFiles;
dlgopt.windowTitle = CFSTR("Please select the product's icon and click 'OK'.");
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)
_fatal("Unexpected error in AEGetNthDesc: %d\n", (int) rc);
else
{
/* !!! FIXME: Check return values here! */
BlockMoveData(*desc.dataHandle, &fsref, sizeof (fsref));
FSRefMakePath(&fsref, buf, bufsize - 1);
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);
233
} /* manually_locate_product */
234
235
236
237
void ui_fatal(const char *str)
{
238
239
240
241
if (!carbon_ui_initialized)
fprintf(stderr, "FATAL ERROR: %s\n", str);
else
do_msgbox(str, kAlertStopAlert, NULL, NULL);
242
243
244
245
246
} /* ui_fatal */
void ui_success(const char *str)
{
247
do_msgbox(str, kAlertNoteAlert, NULL, NULL);
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
} /* ui_success */
void ui_total_progress(int percent)
{
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 */
} /* ui_total_progress */
void ui_status(const char *str)
{
SetControlData(status, kControlEditTextPart, kControlStaticTextTextTag,
strlen(str), str);
Draw1Control(status);
} /* ui_status */
/* end of ui_carbon.c ... */