Fix wxColour(NSColor) ctor and use it in wxOSX code
wxColour ctor from NSColor added inb478f24288
didn't work correctly as it didn't initialize wxColour::m_cgColour and so any attempts to use the colour created by it resulted in an immediate crash (so the code added in16671f229a
likely didn't work neither). It also assumed that the NSColor given to it was always in the RGBA colorspace and crashed if it wasn't (so56ebe6dfac
fixed compilation at the price of making the code crash at run-time). Now explicitly request the RGBA colorspace and leave the colour uninitialized, which is better than crashing, if it can't be obtained. After making this ctor safe to use, there is no reason to reproduce its logic in wxColourDialog and wxFontDialog, so just use it from there. Also make the ctor explicit as it's a non-trivial operation which shouldn't be performed implicitly and document that it doesn't take ownership of NSColor, unlike the similar ctor from CGColorRef.
This commit is contained in:
@@ -58,7 +58,8 @@ public:
|
||||
wxColour& operator=(const RGBColor& col);
|
||||
#endif
|
||||
#if wxOSX_USE_COCOA
|
||||
wxColour(WX_NSColor color);
|
||||
// This ctor does not take ownership of the color.
|
||||
explicit wxColour(WX_NSColor color);
|
||||
WX_NSColor OSXGetNSColor() const;
|
||||
#endif
|
||||
wxColour& operator=(CGColorRef col);
|
||||
|
@@ -157,14 +157,7 @@ int wxColourDialog::ShowModal()
|
||||
[theCPDelegate release];
|
||||
|
||||
//Get the shared color panel along with the chosen color and set the chosen color
|
||||
NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
|
||||
|
||||
m_colourData.GetColour().Set(
|
||||
(unsigned char) ([theColor redComponent] * 255.0),
|
||||
(unsigned char) ([theColor greenComponent] * 255.0),
|
||||
(unsigned char) ([theColor blueComponent] * 255.0),
|
||||
(unsigned char) ([theColor alphaComponent] * 255.0)
|
||||
);
|
||||
m_colourData.GetColour() = wxColour([theColorPanel color]);
|
||||
|
||||
//Release the pool, we're done :)
|
||||
[thePool release];
|
||||
|
@@ -208,11 +208,7 @@ int RunMixedFontDialog(wxFontDialog* dialog)
|
||||
fontdata.m_chosenFont = wxFont( theFont );
|
||||
|
||||
//Get the shared color panel along with the chosen color and set the chosen color
|
||||
NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
|
||||
|
||||
fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
|
||||
(unsigned char) ([theColor greenComponent] * 255.0),
|
||||
(unsigned char) ([theColor blueComponent] * 255.0));
|
||||
fontdata.m_fontColour = wxColour([[NSColorPanel sharedColorPanel] color]);
|
||||
#endif
|
||||
retval = wxID_OK ;
|
||||
}
|
||||
@@ -525,11 +521,7 @@ int wxFontDialog::ShowModal()
|
||||
theFontWeight >= 9 ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
|
||||
|
||||
//Get the shared color panel along with the chosen color and set the chosen color
|
||||
NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
|
||||
|
||||
m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
|
||||
(unsigned char) ([theColor greenComponent] * 255.0),
|
||||
(unsigned char) ([theColor blueComponent] * 255.0));
|
||||
m_fontData.m_fontColour = wxColour([theColorPanel color]);
|
||||
|
||||
//Friendly debug stuff
|
||||
#ifdef FONTDLGDEBUG
|
||||
|
@@ -1,10 +1,9 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: src/osx/cocoa/colour.mm
|
||||
// Purpose: Cocoa additions to wxColour class
|
||||
// Author: Kevin Ollivier
|
||||
// Modified by:
|
||||
// Created: 2009-10-31
|
||||
// Copyright: (c) Kevin Ollivier
|
||||
// Purpose: Conversions between NSColor and wxColour
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2015-11-26 (completely replacing the old version of the file)
|
||||
// Copyright: (c) 2015 Vadim Zeitlin
|
||||
// Licence: wxWindows licence
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -12,31 +11,34 @@
|
||||
|
||||
#include "wx/colour.h"
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/gdicmn.h"
|
||||
#endif
|
||||
|
||||
#include "wx/osx/private.h"
|
||||
|
||||
// Helper function to avoid writing too many casts in wxColour ctor.
|
||||
static inline wxColour::ChannelType NSColorChannelToWX(CGFloat c)
|
||||
{
|
||||
return static_cast<wxColour::ChannelType>(c * 255 + 0.5);
|
||||
}
|
||||
|
||||
wxColour::wxColour(WX_NSColor col)
|
||||
{
|
||||
size_t noComp = [col numberOfComponents];
|
||||
|
||||
CGFloat components[4];
|
||||
CGFloat *p;
|
||||
if ( noComp < 1 || noComp > WXSIZEOF(components) )
|
||||
// Simplest case is when we can directly get the RGBA components:
|
||||
if ( NSColor* colRGBA = [col colorUsingColorSpaceName:NSCalibratedRGBColorSpace] )
|
||||
{
|
||||
// TODO verify whether we really are on a RGB color space
|
||||
m_alpha = wxALPHA_OPAQUE;
|
||||
[col getComponents: components];
|
||||
p = components;
|
||||
}
|
||||
else // Unsupported colour format.
|
||||
{
|
||||
p = NULL;
|
||||
InitRGBA
|
||||
(
|
||||
NSColorChannelToWX([colRGBA redComponent]),
|
||||
NSColorChannelToWX([colRGBA greenComponent]),
|
||||
NSColorChannelToWX([colRGBA blueComponent]),
|
||||
NSColorChannelToWX([colRGBA alphaComponent])
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
InitFromComponents(components, noComp);
|
||||
// Don't assert here, this will more likely than not result in a crash as
|
||||
// colours are often created in drawing code which will be called again
|
||||
// when the assert dialog is shown, resulting in a recursive assertion
|
||||
// failure and, hence, a crash.
|
||||
NSLog(@"Failed to convert NSColor \"%@\" to wxColour.", col);
|
||||
}
|
||||
|
||||
WX_NSColor wxColour::OSXGetNSColor() const
|
||||
|
Reference in New Issue
Block a user