Skip to content

Latest commit

 

History

History
274 lines (221 loc) · 7.42 KB

ui_carbon.c

File metadata and controls

274 lines (221 loc) · 7.42 KB
 
Jan 5, 2004
Jan 5, 2004
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;
May 25, 2004
May 25, 2004
13
static int carbon_ui_initialized = 0;
Jan 5, 2004
Jan 5, 2004
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;
May 25, 2004
May 25, 2004
24
25
26
27
if (carbon_ui_initialized) /* already initialized? */
return(1);
if (CreateNibReference(CFSTR("mojopatch"), &nibRef) != noErr)
May 27, 2004
May 27, 2004
28
29
{
fprintf(stderr, "MOJOPATCH: You probably don't have a .nib file!\n");
May 25, 2004
May 25, 2004
30
return(0); /* usually .nib isn't found. */
May 27, 2004
May 27, 2004
31
} /* if */
May 25, 2004
May 25, 2004
32
Jan 5, 2004
Jan 5, 2004
33
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
May 25, 2004
May 25, 2004
34
35
if (err == noErr)
err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
Jan 5, 2004
Jan 5, 2004
36
DisposeNibReference( nibRef );
May 25, 2004
May 25, 2004
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);
Jan 5, 2004
Jan 5, 2004
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);
May 28, 2004
May 28, 2004
67
ui_pump();
Jan 5, 2004
Jan 5, 2004
68
69
70
71
72
73
} /* ui_title */
void ui_deinit(void)
{
/* !!! FIXME */
May 25, 2004
May 25, 2004
74
/* carbon_ui_initialized = 0; */
Jan 5, 2004
Jan 5, 2004
75
76
77
78
79
80
} /* ui_deinit */
void ui_pump(void)
{
EventRef theEvent;
May 25, 2004
May 25, 2004
81
82
83
84
85
86
EventTargetRef theTarget;
if (!carbon_ui_initialized)
return;
theTarget = GetEventDispatcherTarget();
Jan 5, 2004
Jan 5, 2004
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)
{
May 30, 2004
May 30, 2004
97
/* !!! FIXME */
Jan 5, 2004
Jan 5, 2004
98
99
100
101
printf("MojoPatch%s: %s\n", debugging ? " [debug]" : "", str);
} /* ui_add_to_log */
May 25, 2004
May 25, 2004
102
103
104
static int do_msgbox(const char *str, AlertType alert_type,
AlertStdCFStringAlertParamRec *param,
DialogItemIndex *idx)
Jan 5, 2004
Jan 5, 2004
105
106
{
const char *_title = "MojoPatch";
May 25, 2004
May 25, 2004
107
108
int retval = 0;
DialogItemIndex val = 0;
Jan 5, 2004
Jan 5, 2004
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;
May 25, 2004
May 25, 2004
117
118
119
120
121
if (CreateStandardAlert(alert_type, title, msg, param, &dlg) == noErr)
{
RunStandardAlert(dlg, NULL, (idx) ? idx : &val);
retval = 1;
} /* if */
Jan 5, 2004
Jan 5, 2004
122
123
124
125
126
127
128
} /* if */
if (msg != NULL)
CFRelease(msg);
if (title != NULL)
CFRelease(title);
May 25, 2004
May 25, 2004
129
130
return(retval);
Jan 5, 2004
Jan 5, 2004
131
132
133
} /* do_msgbox */
May 25, 2004
May 25, 2004
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(&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 */
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 */
May 27, 2004
May 27, 2004
167
int manually_locate_product(const char *name, char *buf, size_t bufsize)
May 25, 2004
May 25, 2004
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;
May 27, 2004
May 27, 2004
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);
May 25, 2004
May 25, 2004
182
May 27, 2004
May 27, 2004
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);
May 25, 2004
May 25, 2004
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);
May 30, 2004
May 30, 2004
233
} /* manually_locate_product */
May 25, 2004
May 25, 2004
234
235
Jan 5, 2004
Jan 5, 2004
236
237
void ui_fatal(const char *str)
{
May 25, 2004
May 25, 2004
238
239
240
241
if (!carbon_ui_initialized)
fprintf(stderr, "FATAL ERROR: %s\n", str);
else
do_msgbox(str, kAlertStopAlert, NULL, NULL);
Jan 5, 2004
Jan 5, 2004
242
243
244
245
246
} /* ui_fatal */
void ui_success(const char *str)
{
May 25, 2004
May 25, 2004
247
do_msgbox(str, kAlertNoteAlert, NULL, NULL);
Jan 5, 2004
Jan 5, 2004
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 ... */