devdraw: cocoa updates

R=rsc
CC=plan9port.codebot
http://codereview.appspot.com/5131041
This commit is contained in:
David Jeannot 2011-09-26 08:19:56 -07:00 committed by Russ Cox
parent 8bc80ada76
commit 513ce18d64

View file

@ -60,7 +60,7 @@ main(int argc, char **argv)
open("/dev/null", OREAD); open("/dev/null", OREAD);
open("/dev/null", OWRITE); open("/dev/null", OWRITE);
// Libdraw don't permit arguments currently. // Libdraw doesn't permit arguments currently.
ARGBEGIN{ ARGBEGIN{
case 'D': // only for good ps -a listings case 'D': // only for good ps -a listings
@ -80,13 +80,14 @@ main(int argc, char **argv)
} }
struct { struct {
NSWindow *std;
NSWindow *ofs;
NSWindow *p; NSWindow *p;
NSView *content; NSView *content;
Cursor *cursor; Cursor *cursor;
NSString *title; NSString *title;
QLock titlel; QLock titlel;
char *rectstr; char *rectstr;
NSRect lastrect;
NSBitmapImageRep *img; NSBitmapImageRep *img;
NSRect flushrect; NSRect flushrect;
int needflush; int needflush;
@ -94,7 +95,6 @@ struct {
static void drawimg(void); static void drawimg(void);
static void flushwin(void); static void flushwin(void);
static void fullscreen(void);
static void getmousepos(void); static void getmousepos(void);
static void makeicon(void); static void makeicon(void);
static void makemenu(void); static void makemenu(void);
@ -102,6 +102,7 @@ static void makewin(void);
static void resize(void); static void resize(void);
static void sendmouse(int); static void sendmouse(int);
static void setcursor0(void); static void setcursor0(void);
static void togglefs(void);
@implementation appdelegate @implementation appdelegate
- (void)applicationDidFinishLaunching:(id)arg - (void)applicationDidFinishLaunching:(id)arg
@ -135,6 +136,16 @@ static void setcursor0(void);
{ {
resize(); resize();
} }
- (void)windowDidChangeScreenProfile:(id)arg
{
resize();
}
- (void)windowDidChangeScreen:(id)arg
{
[win.ofs setFrame:[[win.p screen] frame] display:YES];
resize();
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(id)arg - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(id)arg
{ {
return YES; return YES;
@ -146,9 +157,9 @@ static void setcursor0(void);
} }
+ (void)calldrawimg:(id)arg{ drawimg();} + (void)calldrawimg:(id)arg{ drawimg();}
+ (void)callflushwin:(id)arg{ flushwin();} + (void)callflushwin:(id)arg{ flushwin();}
- (void)callfullscreen:(id)arg{ fullscreen();}
+ (void)callmakewin:(id)arg{ makewin();} + (void)callmakewin:(id)arg{ makewin();}
+ (void)callsetcursor0:(id)arg{ setcursor0();} + (void)callsetcursor0:(id)arg{ setcursor0();}
- (void)calltogglefs:(id)arg{ togglefs();}
@end @end
static Memimage* makeimg(void); static Memimage* makeimg(void);
@ -191,6 +202,10 @@ attachscreen(char *label, char *winsize)
{ {
return 0; return 0;
} }
- (BOOL)canBecomeKeyWindow
{
return YES; // else no keyboard focus with NSBorderlessWindowMask
}
@end @end
enum enum
@ -205,46 +220,50 @@ static void
makewin(void) makewin(void)
{ {
NSRect r, sr; NSRect r, sr;
NSView *v;
NSWindow *w;
Rectangle wr; Rectangle wr;
char *s; char *s;
int set; int set;
s = win.rectstr; s = win.rectstr;
sr = [[NSScreen mainScreen] frame];
if(s && *s){ if(s && *s){
if(parsewinsize(s, &wr, &set) < 0) if(parsewinsize(s, &wr, &set) < 0)
sysfatal("%r"); sysfatal("%r");
}else{ }else{
sr = [[NSScreen mainScreen] frame];
wr = Rect(0, 0, sr.size.width*2/3, sr.size.height*2/3); wr = Rect(0, 0, sr.size.width*2/3, sr.size.height*2/3);
set = 0; set = 0;
} }
// The origin is the left-bottom corner with Cocoa. // The origin is the left-bottom corner with Cocoa.
r = NSMakeRect(wr.min.x, r.size.height-wr.min.y, Dx(wr), Dy(wr)); r = NSMakeRect(wr.min.x, r.size.height-wr.min.y, Dx(wr), Dy(wr));
v = [appview new]; win.std = [[appwin alloc]
[v setAcceptsTouchEvents:YES];
w = [[appwin alloc]
initWithContentRect:r initWithContentRect:r
styleMask:Winstyle styleMask:Winstyle
backing:NSBackingStoreBuffered backing:NSBackingStoreBuffered defer:NO];
defer:NO];
if(!set) if(!set)
[w center]; [win.std center];
#if OSX_VERSION >= 100700 #if OSX_VERSION >= 100700
[w setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; [win.std setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
#else
[win.std setShowsResizeIndicator:YES];
#endif #endif
[w setAcceptsMouseMovedEvents:YES]; [win.std setMinSize:NSMakeSize(128,128)];
[w setContentView:v]; [win.std setAcceptsMouseMovedEvents:YES];
[w setDelegate:[NSApp delegate]]; [win.std setDelegate:[NSApp delegate]];
[w setMinSize:NSMakeSize(128,128)];
[w makeKeyAndOrderFront:nil];
win.content = v; win.ofs = [[appwin alloc]
win.p = w; initWithContentRect:sr
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer:NO];
[win.ofs setAcceptsMouseMovedEvents:YES];
[win.ofs setDelegate:[NSApp delegate]];
win.content = [appview new];
[win.content setAcceptsTouchEvents:YES];
win.p = win.std;
[win.p setContentView:win.content];
[win.p makeKeyAndOrderFront:nil];
} }
static Memimage* static Memimage*
@ -592,7 +611,7 @@ getgesture(NSEvent *e)
case NSEventTypeMagnify: case NSEventTypeMagnify:
// if(fabs([e magnification]) > 0.025) // if(fabs([e magnification]) > 0.025)
fullscreen(); togglefs();
break; break;
case NSEventTypeSwipe: case NSEventTypeSwipe:
@ -740,7 +759,7 @@ Return:
[toucha[i] release]; [toucha[i] release];
toucha[i] = nil; toucha[i] = nil;
} }
for(i=0; i<3; i++){ for(i=0; i<ntouch; i++){
assert(toucha[i] == nil); assert(toucha[i] == nil);
assert(touchb[i] == nil); assert(touchb[i] == nil);
} }
@ -845,7 +864,7 @@ setmouse(Point p)
} }
static void static void
fullscreen(void) togglefs(void)
{ {
#if OSX_VERSION >= 100700 #if OSX_VERSION >= 100700
if(useoldfullscreen == 0){ if(useoldfullscreen == 0){
@ -857,30 +876,23 @@ fullscreen(void)
int opt; int opt;
screen = [win.p screen]; screen = [win.p screen];
[win.content retain];
[win.p orderOut:nil];
[win.p setContentView:nil];
if(NSEqualRects([win.p frame], [screen frame])){ if(NSEqualRects([win.p frame], [screen frame])){
opt = NSApplicationPresentationDefault; opt = NSApplicationPresentationDefault;
[NSApp setPresentationOptions:opt]; [NSApp setPresentationOptions:opt];
[win.p setStyleMask:Winstyle]; win.p = win.std;
[win.p setFrame:win.lastrect display:YES];
}else{ }else{
win.lastrect = [win.p frame];
opt = NSApplicationPresentationAutoHideDock opt = NSApplicationPresentationAutoHideDock
| NSApplicationPresentationAutoHideMenuBar; | NSApplicationPresentationAutoHideMenuBar;
[NSApp setPresentationOptions:opt]; [NSApp setPresentationOptions:opt];
[win.p setStyleMask:NSBorderlessWindowMask]; win.p = win.ofs;
[win.p setFrame:[screen frame] display:YES];
} }
// On OS X Lion, after "setStyleMask", window is activated (the gesture work for example), but no keyboard input until mouse or trackpad pressed [win.p setContentView:win.content];
// What I tried without success on OS X Lion: [win.p makeKeyAndOrderFront:nil];
// [NSApp activateIgnoringOtherApps:YES]; [win.content release];
// [win.p makeKeyAndOrderFront:nil]; resize();
// implementing canBecomeKeyWindow
// implementing canBecomeMainWindow
// saving/restoring [win.content nextResponder]
// using enterFullScreenMode instead
// What I didn't try:
// using 2 windows instead: one for each mode
qlock(&win.titlel); qlock(&win.titlel);
[win.p setTitle:win.title]; [win.p setTitle:win.title];
@ -907,7 +919,7 @@ makemenu(void)
title = @"Full Screen"; title = @"Full Screen";
item = [[NSMenuItem alloc] item = [[NSMenuItem alloc]
initWithTitle:title initWithTitle:title
action:@selector(callfullscreen:) keyEquivalent:@"f"]; action:@selector(calltogglefs:) keyEquivalent:@"f"];
[menu addItem:item]; [menu addItem:item];
[item release]; [item release];