mirror of https://github.com/OpenTTD/OpenTTD
(svn r21577) -Codechange: [OSX] Deduplicate code by moving the handling of the window class for screen drivers to a common class
parent
e38f96d609
commit
30eef98ec0
|
@ -99,6 +99,9 @@ public:
|
||||||
virtual bool MouseIsInsideView(NSPoint *pt) = 0;
|
virtual bool MouseIsInsideView(NSPoint *pt) = 0;
|
||||||
|
|
||||||
virtual bool IsActive() = 0;
|
virtual bool IsActive() = 0;
|
||||||
|
|
||||||
|
virtual void SetPortAlphaOpaque() { return; };
|
||||||
|
virtual bool WindowResized() { return false; };
|
||||||
};
|
};
|
||||||
|
|
||||||
extern CocoaSubdriver *_cocoa_subdriver;
|
extern CocoaSubdriver *_cocoa_subdriver;
|
||||||
|
@ -124,6 +127,22 @@ void QZ_HideMouse();
|
||||||
|
|
||||||
uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_id, int display_depth);
|
uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_id, int display_depth);
|
||||||
|
|
||||||
|
/* Subclass of NSWindow to cater our special needs */
|
||||||
|
@interface OTTD_CocoaWindow : NSWindow {
|
||||||
|
CocoaSubdriver *driver;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setDriver:(CocoaSubdriver*)drv;
|
||||||
|
|
||||||
|
- (void)miniaturize:(id)sender;
|
||||||
|
- (void)display;
|
||||||
|
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
|
||||||
|
- (void)appDidHide:(NSNotification*)note;
|
||||||
|
- (void)appWillUnhide:(NSNotification*)note;
|
||||||
|
- (void)appDidUnhide:(NSNotification*)note;
|
||||||
|
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag;
|
||||||
|
@end
|
||||||
|
|
||||||
/* Subclass of NSView to fix Quartz rendering */
|
/* Subclass of NSView to fix Quartz rendering */
|
||||||
@interface OTTD_CocoaView : NSView {
|
@interface OTTD_CocoaView : NSView {
|
||||||
CocoaSubdriver *driver;
|
CocoaSubdriver *driver;
|
||||||
|
|
|
@ -356,7 +356,6 @@ bool VideoDriver_Cocoa::ToggleFullscreen(bool full_screen)
|
||||||
return _cocoa_subdriver->IsFullscreen() == full_screen;
|
return _cocoa_subdriver->IsFullscreen() == full_screen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* This is needed since sometimes assert is called before the videodriver is initialized */
|
/* This is needed since sometimes assert is called before the videodriver is initialized */
|
||||||
void CocoaDialog(const char *title, const char *message, const char *buttonLabel)
|
void CocoaDialog(const char *title, const char *message, const char *buttonLabel)
|
||||||
{
|
{
|
||||||
|
@ -409,6 +408,112 @@ void cocoaReleaseAutoreleasePool()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@implementation OTTD_CocoaWindow
|
||||||
|
|
||||||
|
- (void)setDriver:(CocoaSubdriver*)drv
|
||||||
|
{
|
||||||
|
driver = drv;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Minimize the window
|
||||||
|
*/
|
||||||
|
- (void)miniaturize:(id)sender
|
||||||
|
{
|
||||||
|
/* make the alpha channel opaque so anim won't have holes in it */
|
||||||
|
driver->SetPortAlphaOpaque();
|
||||||
|
|
||||||
|
/* window is hidden now */
|
||||||
|
driver->active = false;
|
||||||
|
|
||||||
|
QZ_ShowMouse();
|
||||||
|
|
||||||
|
[ super miniaturize:sender ];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method fires just before the window deminaturizes from the Dock.
|
||||||
|
* We'll save the current visible surface, let the window manager redraw any
|
||||||
|
* UI elements, and restore the surface. This way, no expose event
|
||||||
|
* is required, and the deminiaturize works perfectly.
|
||||||
|
*/
|
||||||
|
- (void)display
|
||||||
|
{
|
||||||
|
driver->SetPortAlphaOpaque();
|
||||||
|
|
||||||
|
/* save current visible surface */
|
||||||
|
[ self cacheImageInRect:[ driver->cocoaview frame ] ];
|
||||||
|
|
||||||
|
/* let the window manager redraw controls, border, etc */
|
||||||
|
[ super display ];
|
||||||
|
|
||||||
|
/* restore visible surface */
|
||||||
|
[ self restoreCachedImage ];
|
||||||
|
|
||||||
|
/* window is visible again */
|
||||||
|
driver->active = true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Define the rectangle we draw our window in
|
||||||
|
*/
|
||||||
|
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
|
||||||
|
{
|
||||||
|
[ super setFrame:frameRect display:flag ];
|
||||||
|
|
||||||
|
/* Don't do anything if the window is currently being created */
|
||||||
|
if (driver->setup) return;
|
||||||
|
|
||||||
|
if (!driver->WindowResized()) error("Cocoa: Failed to resize window.");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Handle hiding of the application
|
||||||
|
*/
|
||||||
|
- (void)appDidHide:(NSNotification*)note
|
||||||
|
{
|
||||||
|
driver->active = false;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Fade-in the application and restore display plane
|
||||||
|
*/
|
||||||
|
- (void)appWillUnhide:(NSNotification*)note
|
||||||
|
{
|
||||||
|
driver->SetPortAlphaOpaque ();
|
||||||
|
|
||||||
|
/* save current visible surface */
|
||||||
|
[ self cacheImageInRect:[ driver->cocoaview frame ] ];
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Unhide and restore display plane and re-activate driver
|
||||||
|
*/
|
||||||
|
- (void)appDidUnhide:(NSNotification*)note
|
||||||
|
{
|
||||||
|
/* restore cached image, since it may not be current, post expose event too */
|
||||||
|
[ self restoreCachedImage ];
|
||||||
|
|
||||||
|
driver->active = true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Initialize event system for the application rectangle
|
||||||
|
*/
|
||||||
|
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
|
||||||
|
{
|
||||||
|
/* Make our window subclass receive these application notifications */
|
||||||
|
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
||||||
|
selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
|
||||||
|
|
||||||
|
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
||||||
|
selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
|
||||||
|
|
||||||
|
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
||||||
|
selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
|
||||||
|
|
||||||
|
return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@implementation OTTD_CocoaView
|
@implementation OTTD_CocoaView
|
||||||
/**
|
/**
|
||||||
* Initialize the driver
|
* Initialize the driver
|
||||||
|
|
|
@ -45,23 +45,6 @@
|
||||||
|
|
||||||
class WindowQuartzSubdriver;
|
class WindowQuartzSubdriver;
|
||||||
|
|
||||||
|
|
||||||
/* Subclass of NSWindow to fix genie effect and support resize events */
|
|
||||||
@interface OTTD_QuartzWindow : NSWindow {
|
|
||||||
WindowQuartzSubdriver *driver;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)setDriver:(WindowQuartzSubdriver*)drv;
|
|
||||||
|
|
||||||
- (void)miniaturize:(id)sender;
|
|
||||||
- (void)display;
|
|
||||||
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
|
|
||||||
- (void)appDidHide:(NSNotification*)note;
|
|
||||||
- (void)appWillUnhide:(NSNotification*)note;
|
|
||||||
- (void)appDidUnhide:(NSNotification*)note;
|
|
||||||
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag;
|
|
||||||
@end
|
|
||||||
|
|
||||||
/* Subclass of OTTD_CocoaView to fix Quartz rendering */
|
/* Subclass of OTTD_CocoaView to fix Quartz rendering */
|
||||||
@interface OTTD_QuartzView : OTTD_CocoaView
|
@interface OTTD_QuartzView : OTTD_CocoaView
|
||||||
- (void)setDriver:(WindowQuartzSubdriver*)drv;
|
- (void)setDriver:(WindowQuartzSubdriver*)drv;
|
||||||
|
@ -136,98 +119,6 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@implementation OTTD_QuartzWindow
|
|
||||||
|
|
||||||
- (void)setDriver:(WindowQuartzSubdriver*)drv
|
|
||||||
{
|
|
||||||
driver = drv;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* we override these methods to fix the miniaturize animation/dock icon bug */
|
|
||||||
- (void)miniaturize:(id)sender
|
|
||||||
{
|
|
||||||
/* make the alpha channel opaque so anim won't have holes in it */
|
|
||||||
driver->SetPortAlphaOpaque ();
|
|
||||||
|
|
||||||
/* window is hidden now */
|
|
||||||
driver->active = false;
|
|
||||||
|
|
||||||
QZ_ShowMouse();
|
|
||||||
|
|
||||||
[ super miniaturize:sender ];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)display
|
|
||||||
{
|
|
||||||
/* This method fires just before the window deminaturizes from the Dock.
|
|
||||||
* We'll save the current visible surface, let the window manager redraw any
|
|
||||||
* UI elements, and restore the surface. This way, no expose event
|
|
||||||
* is required, and the deminiaturize works perfectly.
|
|
||||||
*/
|
|
||||||
|
|
||||||
driver->SetPortAlphaOpaque();
|
|
||||||
|
|
||||||
/* save current visible surface */
|
|
||||||
[ self cacheImageInRect:[ driver->cocoaview frame ] ];
|
|
||||||
|
|
||||||
/* let the window manager redraw controls, border, etc */
|
|
||||||
[ super display ];
|
|
||||||
|
|
||||||
/* restore visible surface */
|
|
||||||
[ self restoreCachedImage ];
|
|
||||||
|
|
||||||
/* window is visible again */
|
|
||||||
driver->active = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
|
|
||||||
{
|
|
||||||
[ super setFrame:frameRect display:flag ];
|
|
||||||
|
|
||||||
/* Don't do anything if the window is currently being created */
|
|
||||||
if (driver->setup) return;
|
|
||||||
|
|
||||||
if (!driver->WindowResized()) error("Cocoa: Failed to resize window.");
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)appDidHide:(NSNotification*)note
|
|
||||||
{
|
|
||||||
driver->active = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
- (void)appWillUnhide:(NSNotification*)note
|
|
||||||
{
|
|
||||||
driver->SetPortAlphaOpaque ();
|
|
||||||
|
|
||||||
/* save current visible surface */
|
|
||||||
[ self cacheImageInRect:[ driver->cocoaview frame ] ];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)appDidUnhide:(NSNotification*)note
|
|
||||||
{
|
|
||||||
/* restore cached image, since it may not be current, post expose event too */
|
|
||||||
[ self restoreCachedImage ];
|
|
||||||
|
|
||||||
driver->active = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
|
|
||||||
{
|
|
||||||
/* Make our window subclass receive these application notifications */
|
|
||||||
[ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
|
|
||||||
|
|
||||||
[ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
|
|
||||||
|
|
||||||
[ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
|
|
||||||
|
|
||||||
return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation OTTD_QuartzView
|
@implementation OTTD_QuartzView
|
||||||
|
|
||||||
- (void)setDriver:(WindowQuartzSubdriver*)drv
|
- (void)setDriver:(WindowQuartzSubdriver*)drv
|
||||||
|
@ -350,7 +241,7 @@ bool WindowQuartzSubdriver::SetVideoMode(int width, int height)
|
||||||
style |= NSResizableWindowMask;
|
style |= NSResizableWindowMask;
|
||||||
|
|
||||||
/* Manually create a window, avoids having a nib file resource */
|
/* Manually create a window, avoids having a nib file resource */
|
||||||
this->window = [ [ OTTD_QuartzWindow alloc ]
|
this->window = [ [ OTTD_CocoaWindow alloc ]
|
||||||
initWithContentRect:contentRect
|
initWithContentRect:contentRect
|
||||||
styleMask:style
|
styleMask:style
|
||||||
backing:NSBackingStoreBuffered
|
backing:NSBackingStoreBuffered
|
||||||
|
|
|
@ -46,22 +46,6 @@
|
||||||
class WindowQuickdrawSubdriver;
|
class WindowQuickdrawSubdriver;
|
||||||
|
|
||||||
|
|
||||||
/* Subclass of NSWindow to fix genie effect and support resize events */
|
|
||||||
@interface OTTD_QuickdrawWindow : NSWindow {
|
|
||||||
WindowQuickdrawSubdriver *driver;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)setDriver:(WindowQuickdrawSubdriver*)drv;
|
|
||||||
|
|
||||||
- (void)miniaturize:(id)sender;
|
|
||||||
- (void)display;
|
|
||||||
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
|
|
||||||
- (void)appDidHide:(NSNotification*)note;
|
|
||||||
- (void)appWillUnhide:(NSNotification*)note;
|
|
||||||
- (void)appDidUnhide:(NSNotification*)note;
|
|
||||||
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag;
|
|
||||||
@end
|
|
||||||
|
|
||||||
class WindowQuickdrawSubdriver: public CocoaSubdriver {
|
class WindowQuickdrawSubdriver: public CocoaSubdriver {
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
|
@ -130,102 +114,6 @@ public:
|
||||||
bool WindowResized();
|
bool WindowResized();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@implementation OTTD_QuickdrawWindow
|
|
||||||
|
|
||||||
- (void)setDriver:(WindowQuickdrawSubdriver*)drv
|
|
||||||
{
|
|
||||||
driver = drv;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* we override these methods to fix the miniaturize animation/dock icon bug */
|
|
||||||
- (void)miniaturize:(id)sender
|
|
||||||
{
|
|
||||||
/* make the alpha channel opaque so anim won't have holes in it */
|
|
||||||
driver->SetPortAlphaOpaque ();
|
|
||||||
|
|
||||||
/* window is hidden now */
|
|
||||||
driver->active = false;
|
|
||||||
|
|
||||||
QZ_ShowMouse();
|
|
||||||
|
|
||||||
[ super miniaturize:sender ];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)display
|
|
||||||
{
|
|
||||||
/* This method fires just before the window deminaturizes from the Dock.
|
|
||||||
* We'll save the current visible surface, let the window manager redraw any
|
|
||||||
* UI elements, and restore the surface. This way, no expose event
|
|
||||||
* is required, and the deminiaturize works perfectly.
|
|
||||||
*/
|
|
||||||
|
|
||||||
driver->SetPortAlphaOpaque();
|
|
||||||
|
|
||||||
/* save current visible surface */
|
|
||||||
[ self cacheImageInRect:[ driver->cocoaview frame ] ];
|
|
||||||
|
|
||||||
/* let the window manager redraw controls, border, etc */
|
|
||||||
[ super display ];
|
|
||||||
|
|
||||||
/* restore visible surface */
|
|
||||||
[ self restoreCachedImage ];
|
|
||||||
|
|
||||||
/* window is visible again */
|
|
||||||
driver->active = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
|
|
||||||
{
|
|
||||||
[ super setFrame:frameRect display:flag ];
|
|
||||||
|
|
||||||
/* Don't do anything if the window is currently being created */
|
|
||||||
if (driver->setup) return;
|
|
||||||
|
|
||||||
if (!driver->WindowResized()) error("Cocoa: Failed to resize window.");
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)appDidHide:(NSNotification*)note
|
|
||||||
{
|
|
||||||
driver->active = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
- (void)appWillUnhide:(NSNotification*)note
|
|
||||||
{
|
|
||||||
driver->SetPortAlphaOpaque ();
|
|
||||||
|
|
||||||
/* save current visible surface */
|
|
||||||
[ self cacheImageInRect:[ driver->cocoaview frame ] ];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)appDidUnhide:(NSNotification*)note
|
|
||||||
{
|
|
||||||
/* restore cached image, since it may not be current, post expose event too */
|
|
||||||
[ self restoreCachedImage ];
|
|
||||||
|
|
||||||
driver->active = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
|
|
||||||
{
|
|
||||||
/* Make our window subclass receive these application notifications */
|
|
||||||
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
|
||||||
selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
|
|
||||||
|
|
||||||
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
|
||||||
selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
|
|
||||||
|
|
||||||
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
|
||||||
selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
|
|
||||||
|
|
||||||
return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
static const int _resize_icon_width = 16;
|
static const int _resize_icon_width = 16;
|
||||||
static const int _resize_icon_height = 16;
|
static const int _resize_icon_height = 16;
|
||||||
|
|
||||||
|
@ -291,7 +179,7 @@ bool WindowQuickdrawSubdriver::SetVideoMode(int width, int height)
|
||||||
style |= NSResizableWindowMask;
|
style |= NSResizableWindowMask;
|
||||||
|
|
||||||
/* Manually create a window, avoids having a nib file resource */
|
/* Manually create a window, avoids having a nib file resource */
|
||||||
this->window = [ [ OTTD_QuickdrawWindow alloc ] initWithContentRect:contentRect
|
this->window = [ [ OTTD_CocoaWindow alloc ] initWithContentRect:contentRect
|
||||||
styleMask:style backing:NSBackingStoreBuffered defer:NO ];
|
styleMask:style backing:NSBackingStoreBuffered defer:NO ];
|
||||||
|
|
||||||
if (this->window == nil) {
|
if (this->window == nil) {
|
||||||
|
|
Loading…
Reference in New Issue