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);
|
wxColour& operator=(const RGBColor& col);
|
||||||
#endif
|
#endif
|
||||||
#if wxOSX_USE_COCOA
|
#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;
|
WX_NSColor OSXGetNSColor() const;
|
||||||
#endif
|
#endif
|
||||||
wxColour& operator=(CGColorRef col);
|
wxColour& operator=(CGColorRef col);
|
||||||
|
@@ -157,14 +157,7 @@ int wxColourDialog::ShowModal()
|
|||||||
[theCPDelegate release];
|
[theCPDelegate release];
|
||||||
|
|
||||||
//Get the shared color panel along with the chosen color and set the chosen color
|
//Get the shared color panel along with the chosen color and set the chosen color
|
||||||
NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
|
m_colourData.GetColour() = wxColour([theColorPanel color]);
|
||||||
|
|
||||||
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)
|
|
||||||
);
|
|
||||||
|
|
||||||
//Release the pool, we're done :)
|
//Release the pool, we're done :)
|
||||||
[thePool release];
|
[thePool release];
|
||||||
|
@@ -208,11 +208,7 @@ int RunMixedFontDialog(wxFontDialog* dialog)
|
|||||||
fontdata.m_chosenFont = wxFont( theFont );
|
fontdata.m_chosenFont = wxFont( theFont );
|
||||||
|
|
||||||
//Get the shared color panel along with the chosen color and set the chosen color
|
//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 = wxColour([[NSColorPanel sharedColorPanel] color]);
|
||||||
|
|
||||||
fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
|
|
||||||
(unsigned char) ([theColor greenComponent] * 255.0),
|
|
||||||
(unsigned char) ([theColor blueComponent] * 255.0));
|
|
||||||
#endif
|
#endif
|
||||||
retval = wxID_OK ;
|
retval = wxID_OK ;
|
||||||
}
|
}
|
||||||
@@ -525,11 +521,7 @@ int wxFontDialog::ShowModal()
|
|||||||
theFontWeight >= 9 ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
|
theFontWeight >= 9 ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
|
||||||
|
|
||||||
//Get the shared color panel along with the chosen color and set the chosen color
|
//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 = wxColour([theColorPanel color]);
|
||||||
|
|
||||||
m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
|
|
||||||
(unsigned char) ([theColor greenComponent] * 255.0),
|
|
||||||
(unsigned char) ([theColor blueComponent] * 255.0));
|
|
||||||
|
|
||||||
//Friendly debug stuff
|
//Friendly debug stuff
|
||||||
#ifdef FONTDLGDEBUG
|
#ifdef FONTDLGDEBUG
|
||||||
|
@@ -1,10 +1,9 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
// Name: src/osx/cocoa/colour.mm
|
// Name: src/osx/cocoa/colour.mm
|
||||||
// Purpose: Cocoa additions to wxColour class
|
// Purpose: Conversions between NSColor and wxColour
|
||||||
// Author: Kevin Ollivier
|
// Author: Vadim Zeitlin
|
||||||
// Modified by:
|
// Created: 2015-11-26 (completely replacing the old version of the file)
|
||||||
// Created: 2009-10-31
|
// Copyright: (c) 2015 Vadim Zeitlin
|
||||||
// Copyright: (c) Kevin Ollivier
|
|
||||||
// Licence: wxWindows licence
|
// Licence: wxWindows licence
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -12,31 +11,34 @@
|
|||||||
|
|
||||||
#include "wx/colour.h"
|
#include "wx/colour.h"
|
||||||
|
|
||||||
#ifndef WX_PRECOMP
|
|
||||||
#include "wx/gdicmn.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "wx/osx/private.h"
|
#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)
|
wxColour::wxColour(WX_NSColor col)
|
||||||
{
|
{
|
||||||
size_t noComp = [col numberOfComponents];
|
// Simplest case is when we can directly get the RGBA components:
|
||||||
|
if ( NSColor* colRGBA = [col colorUsingColorSpaceName:NSCalibratedRGBColorSpace] )
|
||||||
CGFloat components[4];
|
|
||||||
CGFloat *p;
|
|
||||||
if ( noComp < 1 || noComp > WXSIZEOF(components) )
|
|
||||||
{
|
{
|
||||||
// TODO verify whether we really are on a RGB color space
|
InitRGBA
|
||||||
m_alpha = wxALPHA_OPAQUE;
|
(
|
||||||
[col getComponents: components];
|
NSColorChannelToWX([colRGBA redComponent]),
|
||||||
p = components;
|
NSColorChannelToWX([colRGBA greenComponent]),
|
||||||
}
|
NSColorChannelToWX([colRGBA blueComponent]),
|
||||||
else // Unsupported colour format.
|
NSColorChannelToWX([colRGBA alphaComponent])
|
||||||
{
|
);
|
||||||
p = NULL;
|
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
|
WX_NSColor wxColour::OSXGetNSColor() const
|
||||||
|
Reference in New Issue
Block a user