/
ui_carbon.c
265 lines (213 loc) · 7.15 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
67
68
69
70
71
72
} /* ui_init */
void ui_title(const char *str)
{
CFStringRef cfstr = CFStringCreateWithBytes(NULL, str, strlen(str),
kCFStringEncodingISOLatin1, 0);
SetWindowTitleWithCFString(window, cfstr);
} /* ui_title */
void ui_deinit(void)
{
/* !!! FIXME */
73
/* carbon_ui_initialized = 0; */
74
75
76
77
78
79
} /* ui_deinit */
void ui_pump(void)
{
EventRef theEvent;
80
81
82
83
84
85
EventTargetRef theTarget;
if (!carbon_ui_initialized)
return;
theTarget = GetEventDispatcherTarget();
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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)
{
// !!! FIXME
printf("MojoPatch%s: %s\n", debugging ? " [debug]" : "", str);
} /* ui_add_to_log */
101
102
103
static int do_msgbox(const char *str, AlertType alert_type,
AlertStdCFStringAlertParamRec *param,
DialogItemIndex *idx)
104
105
{
const char *_title = "MojoPatch";
106
107
int retval = 0;
DialogItemIndex val = 0;
108
109
110
111
112
113
114
115
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;
116
117
118
119
120
if (CreateStandardAlert(alert_type, title, msg, param, &dlg) == noErr)
{
RunStandardAlert(dlg, NULL, (idx) ? idx : &val);
retval = 1;
} /* if */
121
122
123
124
125
126
127
} /* if */
if (msg != NULL)
CFRelease(msg);
if (title != NULL)
CFRelease(title);
128
129
return(retval);
130
131
132
} /* do_msgbox */
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
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 */
int manually_locate_product(char *buf, size_t bufsize)
{
NavDialogCreationOptions dlgopt;
NavDialogRef dlg;
NavReplyRecord reply;
NavUserAction action;
AEKeyword keyword;
AEDesc desc;
FSRef fsref;
OSStatus rc;
int retval = 0;
int yn;
yn = ui_prompt_yn("We can't find your installation."
" Would you like to show us where it is?");
if (!yn)
{
_log("User chose not to manually locate installation");
return(0);
} /* if */
_log("Creating file selector dialog...");
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);
}
227
228
void ui_fatal(const char *str)
{
229
230
231
232
if (!carbon_ui_initialized)
fprintf(stderr, "FATAL ERROR: %s\n", str);
else
do_msgbox(str, kAlertStopAlert, NULL, NULL);
233
234
235
236
237
} /* ui_fatal */
void ui_success(const char *str)
{
238
do_msgbox(str, kAlertNoteAlert, NULL, NULL);
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
} /* 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 ... */