an experiment in making a cocoa webkit browser manageable under X11

implement WebUIDelegate methods for javascript alert() and confirm()

sheets are nicer than panels because they stay with the window
rather than pop up alert boxes that get placed in the middle of the
screen

however, sheets return right away and use callbacks to return the
result, but since javascript apps expect the dialog to block until a
response, we have to block after the sheet is put up

to get around this, make the callback just set a variable
sheetResponse, which a while loop waits for and blocks until it
gets, which it can then return to javascript

+61
+4
WKWindow.h
··· 21 int resourceCount; 22 int resourceCompletedCount; 23 int resourceFailedCount; 24 } 25 26 - (void)setPosition:(NSArray *)aCoords; ··· 30 - (void)setTitle:(NSString *)text; 31 - (void)loadURL:(NSString *)url; 32 - (void)loadURLFromTextField; 33 34 @end
··· 21 int resourceCount; 22 int resourceCompletedCount; 23 int resourceFailedCount; 24 + 25 + int sheetResponse; 26 } 27 28 - (void)setPosition:(NSArray *)aCoords; ··· 32 - (void)setTitle:(NSString *)text; 33 - (void)loadURL:(NSString *)url; 34 - (void)loadURLFromTextField; 35 + 36 + - (void)handleSheetResponse:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo; 37 38 @end
+57
WKWindow.m
··· 230 [self setStatusToResourceCounts]; 231 } 232 233 /* WebUIDelegate glue */ 234 235 @end
··· 230 [self setStatusToResourceCounts]; 231 } 232 233 + 234 /* WebUIDelegate glue */ 235 + 236 + - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame 237 + { 238 + sheetResponse = -1; 239 + 240 + NSBeginInformationalAlertSheet( 241 + [currentURL absoluteString], 242 + nil, 243 + nil, 244 + nil, 245 + [sender window], 246 + self, 247 + @selector(handleSheetResponse:returnCode:contextInfo:), 248 + nil, 249 + nil, 250 + message); 251 + 252 + /* block until the user responds */ 253 + while (sheetResponse == -1) 254 + [NSApp sendEvent:[NSApp nextEventMatchingMask:NSAnyEventMask 255 + untilDate:[NSDate distantFuture] 256 + inMode:NSModalPanelRunLoopMode dequeue:YES]]; 257 + } 258 + 259 + - (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame 260 + { 261 + sheetResponse = -1; 262 + 263 + NSBeginInformationalAlertSheet( 264 + NSLocalizedString(@"JavaScript", @""), 265 + NSLocalizedString(@"OK", @""), 266 + NSLocalizedString(@"Cancel", @""), 267 + nil, 268 + [sender window], 269 + self, 270 + @selector(handleSheetResponse:returnCode:contextInfo:), 271 + nil, 272 + nil, 273 + message); 274 + 275 + /* block until the user responds */ 276 + while (sheetResponse == -1) 277 + [NSApp sendEvent:[NSApp nextEventMatchingMask:NSAnyEventMask 278 + untilDate:[NSDate distantFuture] 279 + inMode:NSModalPanelRunLoopMode dequeue:YES]]; 280 + 281 + if (sheetResponse == NSAlertDefaultReturn) 282 + return YES; 283 + else 284 + return NO; 285 + } 286 + 287 + - (void)handleSheetResponse:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo 288 + { 289 + sheetResponse = returnCode; 290 + } 291 292 @end