changeset 16843:6519afe07cae draft

(svn r21577) -Codechange: [OSX] Deduplicate code by moving the handling of the window class for screen drivers to a common class
author planetmaker <planetmaker@openttd.org>
date Tue, 21 Dec 2010 16:02:55 +0000
parents 61bbac9b2b61
children cade9c3e698e
files src/video/cocoa/cocoa_v.h src/video/cocoa/cocoa_v.mm src/video/cocoa/wnd_quartz.mm src/video/cocoa/wnd_quickdraw.mm
diffstat 4 files changed, 127 insertions(+), 224 deletions(-) [+]
line wrap: on
line diff
--- a/src/video/cocoa/cocoa_v.h
+++ b/src/video/cocoa/cocoa_v.h
@@ -99,6 +99,9 @@
 	virtual bool MouseIsInsideView(NSPoint *pt) = 0;
 
 	virtual bool IsActive() = 0;
+
+	virtual void SetPortAlphaOpaque() { return; };
+	virtual bool WindowResized() { return false; };
 };
 
 extern CocoaSubdriver *_cocoa_subdriver;
@@ -124,6 +127,22 @@
 
 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 */
 @interface OTTD_CocoaView : NSView {
 	CocoaSubdriver *driver;
--- a/src/video/cocoa/cocoa_v.mm
+++ b/src/video/cocoa/cocoa_v.mm
@@ -356,7 +356,6 @@
 	return _cocoa_subdriver->IsFullscreen() == full_screen;
 }
 
-
 /* This is needed since sometimes assert is called before the videodriver is initialized */
 void CocoaDialog(const char *title, const char *message, const char *buttonLabel)
 {
@@ -409,6 +408,112 @@
 
 
 
+@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
 /**
  * Initialize the driver
--- a/src/video/cocoa/wnd_quartz.mm
+++ b/src/video/cocoa/wnd_quartz.mm
@@ -45,23 +45,6 @@
 
 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 */
 @interface OTTD_QuartzView : OTTD_CocoaView
 - (void)setDriver:(WindowQuartzSubdriver*)drv;
@@ -136,98 +119,6 @@
 }
 
 
-@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
 
 - (void)setDriver:(WindowQuartzSubdriver*)drv
@@ -350,7 +241,7 @@
 		style |= NSResizableWindowMask;
 
 		/* Manually create a window, avoids having a nib file resource */
-		this->window = [ [ OTTD_QuartzWindow alloc ]
+		this->window = [ [ OTTD_CocoaWindow alloc ]
 							initWithContentRect:contentRect
 							styleMask:style
 							backing:NSBackingStoreBuffered
--- a/src/video/cocoa/wnd_quickdraw.mm
+++ b/src/video/cocoa/wnd_quickdraw.mm
@@ -46,22 +46,6 @@
 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 {
 private:
 	/**
@@ -130,102 +114,6 @@
 	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_height = 16;
 
@@ -291,7 +179,7 @@
 		style |= NSResizableWindowMask;
 
 		/* 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 ];
 
 		if (this->window == nil) {