1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/colordlgosx.mm
3 // Purpose: wxColourDialog class. NOTE: you can use the generic class
4 // if you wish, instead of implementing this.
9 // Copyright: (c) Ryan Norton
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
15 // ===========================================================================
17 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 #include "wx/wxprec.h"
23 #include "wx/colordlg.h"
24 #include "wx/fontdlg.h"
26 // ============================================================================
28 // ============================================================================
31 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
33 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
35 #include "wx/osx/private.h"
37 #import <Foundation/Foundation.h>
38 #import <AppKit/AppKit.h>
40 // ---------------------------------------------------------------------------
41 // wxCPWCDelegate - Window Closed delegate
42 // ---------------------------------------------------------------------------
44 @interface wxCPWCDelegate : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
51 - (BOOL)windowShouldClose:(id)sender;
53 @end // interface wxNSFontPanelDelegate : NSObject
55 @implementation wxCPWCDelegate : NSObject
65 - (BOOL)windowShouldClose:(id)sender
81 @end // wxNSFontPanelDelegate
87 wxColourDialog::wxColourDialog()
89 m_dialogParent = NULL;
92 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
97 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
99 m_dialogParent = parent;
102 m_colourData = *data;
104 //autorelease pool - req'd for carbon
105 NSAutoreleasePool *thePool;
106 thePool = [[NSAutoreleasePool alloc] init];
108 if(m_colourData.GetColour().IsOk())
109 [[NSColorPanel sharedColorPanel] setColor:
110 [NSColor colorWithCalibratedRed:(CGFloat) (m_colourData.GetColour().Red() / 255.0)
111 green:(CGFloat) (m_colourData.GetColour().Green() / 255.0)
112 blue:(CGFloat) (m_colourData.GetColour().Blue() / 255.0)
116 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
118 //We're done - free up the pool
123 int wxColourDialog::ShowModal()
125 //Start the pool. Required for carbon interaction
126 //(For those curious, the only thing that happens
127 //if you don't do this is a bunch of error
128 //messages about leaks on the console,
129 //with no windows shown or anything).
130 NSAutoreleasePool *thePool;
131 thePool = [[NSAutoreleasePool alloc] init];
133 //Get the shared color and font panel
134 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
136 //Create and assign the delegates (cocoa event handlers) so
137 //we can tell if a window has closed/open or not
138 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
139 [theColorPanel setDelegate:theCPDelegate];
142 // Start the color panel modal loop
144 wxDialog::OSXBeginModalDialog();
145 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
148 [NSApp runModalSession:session];
150 //If the color panel is closed, return the font panel modal loop
151 if ([theCPDelegate isClosed])
154 [NSApp endModalSession:session];
155 wxDialog::OSXEndModalDialog();
157 //free up the memory for the delegates - we don't need them anymore
158 [theColorPanel setDelegate:nil];
159 [theCPDelegate release];
161 //Get the shared color panel along with the chosen color and set the chosen color
162 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
164 m_colourData.GetColour().Set(
165 (unsigned char) ([theColor redComponent] * 255.0),
166 (unsigned char) ([theColor greenComponent] * 255.0),
167 (unsigned char) ([theColor blueComponent] * 255.0)
170 //Release the pool, we're done :)
173 //Return ID_OK - there are no "apply" buttons or the like
174 //on either the font or color panel
178 #endif //use native font dialog