Implemented message box transience on Mac OS X.

This commit is contained in:
Pietro Gagliardi 2014-06-05 00:53:26 -04:00
parent 8e0a38dc47
commit 60de6d05c5
4 changed files with 52 additions and 15 deletions

View File

@ -9,6 +9,7 @@
// see the hack below; we'll include everything first just in case some other headers get included below; if the hack ever gets resolved, we can use the ones below instead
#import <Foundation/NSAutoreleasePool.h>
#import <AppKit/NSEvent.h>
#import <AppKit/NSAlert.h>
// HACK.
// Apple's header files are bugged: there's an enum that was introduced in 10.7 with new values added in 10.8, but instead of wrapping the whole enum in a version check, they wrap just the fields. This means that on 10.6 that enum will be empty, which is illegal.
@ -33,6 +34,7 @@
#import <AppKit/NSWindow.h>
#import <Foundation/NSAutoreleasePool.h>
#import <AppKit/NSEvent.h>
#import <AppKit/NSAlert.h>
extern NSRect dummyRect;
@ -68,6 +70,10 @@ extern NSRect dummyRect;
return NSTerminateCancel;
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
}
@end
id makeAppDelegate(void)

View File

@ -9,10 +9,15 @@ import (
// #include "objc_darwin.h"
import "C"
func _msgBox(primarytext string, secondarytext string, style uintptr) {
func _msgBox(parent *Window, primarytext string, secondarytext string, style uintptr) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
var pwin C.id = nil
if parent != nil {
pwin = parent.sysData.id
}
primary := toNSString(primarytext)
secondary := C.id(nil)
if secondarytext != "" {
@ -20,19 +25,19 @@ func _msgBox(primarytext string, secondarytext string, style uintptr) {
}
switch style {
case 0: // normal
C.msgBox(primary, secondary)
C.msgBox(pwin, primary, secondary)
case 1: // error
C.msgBoxError(primary, secondary)
C.msgBoxError(pwin, primary, secondary)
}
ret <- struct{}{}
}
<-ret
}
func msgBox(primarytext string, secondarytext string) {
_msgBox(primarytext, secondarytext, 0)
func msgBox(parent *Window, primarytext string, secondarytext string) {
_msgBox(parent, primarytext, secondarytext, 0)
}
func msgBoxError(primarytext string, secondarytext string) {
_msgBox(primarytext, secondarytext, 1)
func msgBoxError(parent *Window, primarytext string, secondarytext string) {
_msgBox(parent, primarytext, secondarytext, 1)
}

View File

@ -3,7 +3,27 @@
#include "objc_darwin.h"
#import <AppKit/NSAlert.h>
static void alert(NSString *primary, NSString *secondary, NSAlertStyle style)
// see delegateuitask_darwin.m
// in this case, NSWindow.h includes NSApplication.h
#ifdef MAC_OS_X_VERSION_10_7
#undef MAC_OS_X_VERSION_MIN_REQUIRED
#undef MAC_OS_X_VERSION_MAX_ALLOWED
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_7
#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_7
#endif
#import <AppKit/NSApplication.h>
#undef MAC_OS_X_VERSION_MIN_REQUIRED
#undef MAC_OS_X_VERSION_MAX_ALLOWED
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6
#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_6
#import <AppKit/NSWindow.h>
#define to(T, x) ((T *) (x))
#define toNSWindow(x) to(NSWindow, (x))
static void alert(id parent, NSString *primary, NSString *secondary, NSAlertStyle style)
{
NSAlert *box;
@ -14,15 +34,21 @@ static void alert(NSString *primary, NSString *secondary, NSAlertStyle style)
[box setAlertStyle:style];
// TODO is there a named constant? will also need to be changed when we add different dialog types
[box addButtonWithTitle:@"OK"];
[box runModal];
if (parent == nil)
[box runModal];
else
[box beginSheetModalForWindow:toNSWindow(parent)
modalDelegate:[NSApp delegate]
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
void msgBox(id primary, id secondary)
void msgBox(id parent, id primary, id secondary)
{
alert((NSString *) primary, (NSString *) secondary, NSInformationalAlertStyle);
alert(parent, (NSString *) primary, (NSString *) secondary, NSInformationalAlertStyle);
}
void msgBoxError(id primary, id secondary)
void msgBoxError(id parent, id primary, id secondary)
{
alert((NSString *) primary, (NSString *) secondary, NSCriticalAlertStyle);
alert(parent, (NSString *) primary, (NSString *) secondary, NSCriticalAlertStyle);
}

View File

@ -61,8 +61,8 @@ extern void breakMainLoop(void);
extern void cocoaMainLoop(void);
/* dialog_darwin.m */
extern void msgBox(id, id);
extern void msgBoxError(id, id);
extern void msgBox(id, id, id);
extern void msgBoxError(id, id, id);
/* listbox_darwin.m */
extern id toListboxItem(id, id);