Three Cocoa-related changes:

- correct window resize in mandelbrot demo
- clip to 1-pixel and 0-pixel width now work
- modifier key presses are now correctly reported

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7029 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy 2010-01-24 10:27:21 +00:00
parent 6f89a3baca
commit 1eb95cd823
4 changed files with 72 additions and 38 deletions

View File

@ -83,7 +83,7 @@ inline Fl_Region XRectangleRegion(int x, int y, int w, int h) {
Fl_Region R = (Fl_Region)malloc(sizeof(*R)); Fl_Region R = (Fl_Region)malloc(sizeof(*R));
R->count = 1; R->count = 1;
R->rects = (CGRect *)malloc(sizeof(CGRect)); R->rects = (CGRect *)malloc(sizeof(CGRect));
*(R->rects) = CGRectMake(x, y, w - 1, h - 1); *(R->rects) = CGRectMake(x, y, w > 0 ? w - 0.9 : 0, h > 0 ? h - 0.9 : 0);
return R; return R;
} }
inline void XDestroyRegion(Fl_Region r) { inline void XDestroyRegion(Fl_Region r) {

View File

@ -1474,7 +1474,7 @@ void Fl_Widget::damage(uchar fl, int X, int Y, int W, int H) {
XDestroyRegion(R); XDestroyRegion(R);
#elif defined(__APPLE_QUARTZ__) #elif defined(__APPLE_QUARTZ__)
#ifdef __APPLE_COCOA__ #ifdef __APPLE_COCOA__
CGRect arg = CGRectMake(X,Y,W - 1,H - 1); CGRect arg = CGRectMake(X, Y, W - 0.9, H - 0.9);
int j;//don't add a rectangle totally inside the Fl_Region int j;//don't add a rectangle totally inside the Fl_Region
for(j = 0; j < i->region->count; j++) { for(j = 0; j < i->region->count; j++) {
if(CGRectContainsRect(i->region->rects[j], arg)) break; if(CGRectContainsRect(i->region->rects[j], arg)) break;

View File

@ -122,7 +122,6 @@ Fl_Window *Fl_Window::current_;
// forward declarations of variables in this file // forward declarations of variables in this file
static int got_events = 0; static int got_events = 0;
static Fl_Window* resize_from_system; static Fl_Window* resize_from_system;
static NSView *viewWithLockedFocus = nil;
static SInt32 MACsystemVersion; static SInt32 MACsystemVersion;
#if CONSOLIDATE_MOTION #if CONSOLIDATE_MOTION
@ -198,9 +197,9 @@ static unsigned int mods_to_e_state( NSUInteger mods )
/* /*
* convert the current mouse chord into the FLTK keysym * convert the current key chord into the FLTK keysym
*/ */
/*
static void mods_to_e_keysym( NSUInteger mods ) static void mods_to_e_keysym( NSUInteger mods )
{ {
if ( mods & NSCommandKeyMask ) Fl::e_keysym = FL_Meta_L; if ( mods & NSCommandKeyMask ) Fl::e_keysym = FL_Meta_L;
@ -212,7 +211,7 @@ static unsigned int mods_to_e_state( NSUInteger mods )
else Fl::e_keysym = 0; else Fl::e_keysym = 0;
//printf( "to sym 0x%08x (%04x)\n", Fl::e_keysym, mods ); //printf( "to sym 0x%08x (%04x)\n", Fl::e_keysym, mods );
} }
*/
// these pointers are set by the Fl::lock() function: // these pointers are set by the Fl::lock() function:
static void nothing() {} static void nothing() {}
void (*fl_lock_function)() = nothing; void (*fl_lock_function)() = nothing;
@ -774,7 +773,19 @@ static void cocoaMouseHandler(NSEvent *theEvent)
NSUInteger mods = [theEvent modifierFlags]; NSUInteger mods = [theEvent modifierFlags];
int sendEvent = 0; int sendEvent = 0;
switch ( [theEvent type] ) { NSEventType etype = [theEvent type];
if (etype == NSLeftMouseDown || etype == NSRightMouseDown || etype == NSOtherMouseDown) {
if (btn == 1) Fl::e_state |= FL_BUTTON1;
else if (btn == 3) Fl::e_state |= FL_BUTTON2;
else if (btn == 2) Fl::e_state |= FL_BUTTON3;
}
else if (etype == NSLeftMouseUp || etype == NSRightMouseUp || etype == NSOtherMouseUp) {
if (btn == 1) Fl::e_state &= ~FL_BUTTON1;
else if (btn == 3) Fl::e_state &= ~FL_BUTTON2;
else if (btn == 2) Fl::e_state &= ~FL_BUTTON3;
}
switch ( etype ) {
case NSLeftMouseDown: case NSLeftMouseDown:
case NSRightMouseDown: case NSRightMouseDown:
case NSOtherMouseDown: case NSOtherMouseDown:
@ -782,9 +793,6 @@ static void cocoaMouseHandler(NSEvent *theEvent)
sendEvent = FL_PUSH; sendEvent = FL_PUSH;
Fl::e_is_click = 1; Fl::e_is_click = 1;
px = (int)pos.x; py = (int)pos.y; px = (int)pos.x; py = (int)pos.y;
if (btn == 1) Fl::e_state |= FL_BUTTON1;
else if (btn == 3) Fl::e_state |= FL_BUTTON2;
else if (btn == 2) Fl::e_state |= FL_BUTTON3;
if (clickCount>1) if (clickCount>1)
Fl::e_clicks++; Fl::e_clicks++;
else else
@ -793,7 +801,6 @@ static void cocoaMouseHandler(NSEvent *theEvent)
case NSLeftMouseUp: case NSLeftMouseUp:
case NSRightMouseUp: case NSRightMouseUp:
case NSOtherMouseUp: case NSOtherMouseUp:
Fl::e_state &= 0xff0000;
if (suppressed) { if (suppressed) {
suppressed = 0; suppressed = 0;
break; break;
@ -1533,13 +1540,7 @@ void Fl::get_mouse(int &x, int &y)
void Fl_X::flush() void Fl_X::flush()
{ {
w->flush(); w->flush();
if (fl_gc) { if (fl_gc) CGContextFlush(fl_gc);
CGContextFlush(fl_gc);
if (viewWithLockedFocus) {
[viewWithLockedFocus unlockFocus];
viewWithLockedFocus = nil;
}
}
} }
/* /*
@ -1717,6 +1718,7 @@ static void q_set_window_title(NSWindow *nsw, const char * name ) {
- (void)scrollWheel:(NSEvent *)theEvent; - (void)scrollWheel:(NSEvent *)theEvent;
- (void)keyDown:(NSEvent *)theEvent; - (void)keyDown:(NSEvent *)theEvent;
- (void)keyUp:(NSEvent *)theEvent; - (void)keyUp:(NSEvent *)theEvent;
- (void)flagsChanged:(NSEvent *)theEvent;
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender; - (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender;
- (NSDragOperation)draggingUpdated:(id < NSDraggingInfo >)sender; - (NSDragOperation)draggingUpdated:(id < NSDraggingInfo >)sender;
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender; - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
@ -1780,6 +1782,27 @@ static void q_set_window_title(NSWindow *nsw, const char * name ) {
- (void)keyUp:(NSEvent *)theEvent { - (void)keyUp:(NSEvent *)theEvent {
cocoaKeyboardHandler(theEvent); cocoaKeyboardHandler(theEvent);
} }
- (void)flagsChanged:(NSEvent *)theEvent {
fl_lock_function();
static UInt32 prevMods = mods_to_e_state( GetCurrentKeyModifiers() );
NSUInteger mods = [theEvent modifierFlags];
Fl_Window *window = (Fl_Window*)[(FLWindow*)[theEvent window] getFl_Window];
UInt32 tMods = prevMods ^ mods;
int sendEvent = 0;
if ( tMods )
{
mods_to_e_keysym( tMods );
if ( Fl::e_keysym )
sendEvent = ( prevMods<mods ) ? FL_KEYBOARD : FL_KEYUP;
Fl::e_length = 0;
Fl::e_text = "";
prevMods = mods;
}
mods_to_e_state( mods );
while (window->parent()) window = window->window();
if (sendEvent) Fl::handle(sendEvent,window);
fl_unlock_function();
}
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender - (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender
{ {
Fl_Window *target = [(FLWindow*)[self window] getFl_Window]; Fl_Window *target = [(FLWindow*)[self window] getFl_Window];
@ -2222,8 +2245,8 @@ void Fl_Window::make_current()
win = (Fl_Window*)win->window(); win = (Fl_Window*)win->window();
} }
viewWithLockedFocus = [(NSWindow*)i->xid contentView]; [[NSView focusView] unlockFocus];
[viewWithLockedFocus lockFocusIfCanDraw];// important [[(NSWindow*)i->xid contentView] lockFocus];
i->gc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; i->gc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
fl_gc = i->gc; fl_gc = i->gc;
if ( fl_window_region ) XDestroyRegion(fl_window_region); if ( fl_window_region ) XDestroyRegion(fl_window_region);
@ -2604,7 +2627,7 @@ Fl_Region MacRegionMinusRect(Fl_Region r, int x,int y,int w,int h)
Fl_Region outr = (Fl_Region)malloc(sizeof(*outr)); Fl_Region outr = (Fl_Region)malloc(sizeof(*outr));
outr->rects = (CGRect*)malloc(4 * r->count * sizeof(CGRect)); outr->rects = (CGRect*)malloc(4 * r->count * sizeof(CGRect));
outr->count = 0; outr->count = 0;
CGRect rect = CGRectMake(x,y,w - 1,h - 1); CGRect rect = CGRectMake(x,y,w - 0.9,h - 0.9);
for( int i = 0; i < r->count; i++) { for( int i = 0; i < r->count; i++) {
CGRect A = r->rects[i]; CGRect A = r->rects[i];
CGRect test = CGRectIntersection(A, rect); CGRect test = CGRectIntersection(A, rect);
@ -2649,7 +2672,7 @@ Fl_Region MacRectRegionIntersect(Fl_Region current, int x,int y,int w, int h)
*/ */
{ {
if (current == NULL) return XRectangleRegion(x,y,w,h); if (current == NULL) return XRectangleRegion(x,y,w,h);
CGRect r = CGRectMake(x, y, w - 1, h - 1); CGRect r = CGRectMake(x, y, w - 0.9, h - 0.9);
Fl_Region outr = (Fl_Region)malloc(sizeof(*outr)); Fl_Region outr = (Fl_Region)malloc(sizeof(*outr));
outr->count = current->count; outr->count = current->count;
outr->rects =(CGRect*)malloc(outr->count * sizeof(CGRect)); outr->rects =(CGRect*)malloc(outr->count * sizeof(CGRect));
@ -3213,8 +3236,6 @@ unsigned char *MACbitmapFromRectOfWindow(Fl_Window *win, int x, int y, int w, in
y += win->y(); y += win->y();
win = win->window(); win = win->window();
} }
NSView *myview = [(NSWindow*)Fl_X::i(win)->xid contentView];
[myview lockFocus];
CGFloat epsilon = 0; CGFloat epsilon = 0;
if (MACsystemVersion >= 0x1060) epsilon = 0.001; if (MACsystemVersion >= 0x1060) epsilon = 0.001;
// The epsilon offset is absolutely necessary under 10.6. Without it, the top pixel row and // The epsilon offset is absolutely necessary under 10.6. Without it, the top pixel row and
@ -3222,10 +3243,23 @@ unsigned char *MACbitmapFromRectOfWindow(Fl_Window *win, int x, int y, int w, in
// Under 10.5, we want no offset. // Under 10.5, we want no offset.
NSRect rect = NSMakeRect(x - epsilon, y - epsilon, w, h); NSRect rect = NSMakeRect(x - epsilon, y - epsilon, w, h);
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:rect]; NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:rect];
[myview unlockFocus];
*bytesPerPixel = [bitmap bitsPerPixel]/8; *bytesPerPixel = [bitmap bitsPerPixel]/8;
int bpp = (int)[bitmap bytesPerPlane];
int bpr = (int)[bitmap bytesPerRow];
int hh = bpp/bpr; // sometimes hh = h-1 for unclear reason
int ww = bpr/(*bytesPerPixel); // sometimes ww = w-1
unsigned char *data = new unsigned char[w * h * *bytesPerPixel]; unsigned char *data = new unsigned char[w * h * *bytesPerPixel];
memcpy(data, [bitmap bitmapData], w * h * *bytesPerPixel); if (w == ww) {
memcpy(data, [bitmap bitmapData], w * hh * *bytesPerPixel);
} else {
unsigned char *p = [bitmap bitmapData];
unsigned char *q = data;
for(int i = 0;i < hh; i++) {
memcpy(q, p, *bytesPerPixel * ww);
p += bpr;
q += w * *bytesPerPixel;
}
}
[bitmap release]; [bitmap release];
return data; return data;
} }

View File

@ -684,10 +684,10 @@ void fl_push_clip(int x, int y, int w, int h) {
r = CreateRectRgn(0,0,0,0); r = CreateRectRgn(0,0,0,0);
#elif defined(__APPLE_QUARTZ__) #elif defined(__APPLE_QUARTZ__)
#ifdef __APPLE_COCOA__ #ifdef __APPLE_COCOA__
r = NULL; r = XRectangleRegion(0,0,0,0);
#else #else
r = NewRgn(); r = NewRgn();
SetEmptyRgn(r); SetEmptyRgn(r);
#endif #endif
#else #else
# error unsupported platform # error unsupported platform
@ -747,12 +747,12 @@ int fl_not_clipped(int x, int y, int w, int h) {
#elif defined(__APPLE_QUARTZ__) #elif defined(__APPLE_QUARTZ__)
if (!r) return 1; if (!r) return 1;
#ifdef __APPLE_COCOA__ #ifdef __APPLE_COCOA__
CGRect arg = CGRectMake(x,y,w - 1,h - 1); CGRect arg = CGRectMake(x, y, w - 0.9, h - 0.9);
for(int i = 0; i < r->count; i++) { for(int i = 0; i < r->count; i++) {
CGRect test = CGRectIntersection(r->rects[i], arg); CGRect test = CGRectIntersection(r->rects[i], arg);
if( ! CGRectIsEmpty(test)) return 1; if( ! CGRectIsEmpty(test)) return 1;
} }
return 0; return 0;
#else #else
Rect rect; Rect rect;
rect.left = x; rect.top = y; rect.right = x+w; rect.bottom = y+h; rect.left = x; rect.top = y; rect.right = x+w; rect.bottom = y+h;
@ -825,7 +825,7 @@ int fl_clip_box(int x, int y, int w, int h, int& X, int& Y, int& W, int& H){
return ret; return ret;
#elif defined(__APPLE_QUARTZ__) #elif defined(__APPLE_QUARTZ__)
#ifdef __APPLE_COCOA__ #ifdef __APPLE_COCOA__
CGRect arg = CGRectMake(x,y,w - 1,h - 1); CGRect arg = CGRectMake(x, y, w - 0.9, h - 0.9);
CGRect u = CGRectMake(0,0,0,0); CGRect u = CGRectMake(0,0,0,0);
CGRect test; CGRect test;
for(int i = 0; i < r->count; i++) { for(int i = 0; i < r->count; i++) {
@ -837,8 +837,8 @@ int fl_clip_box(int x, int y, int w, int h, int& X, int& Y, int& W, int& H){
} }
X = u.origin.x; X = u.origin.x;
Y = u.origin.y; Y = u.origin.y;
W = u.size.width; W = u.size.width + 1;
H = u.size.height; H = u.size.height + 1;
if(CGRectIsEmpty(u)) W = H = 0; if(CGRectIsEmpty(u)) W = H = 0;
return ! CGRectEqualToRect(arg, u); return ! CGRectEqualToRect(arg, u);
#else #else