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"
25 #include "wx/modalhook.h"
27 // ============================================================================
29 // ============================================================================
32 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
34 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
36 #include "wx/osx/private.h"
38 #import <Foundation/Foundation.h>
39 #import <AppKit/AppKit.h>
41 // ---------------------------------------------------------------------------
42 // wxCPWCDelegate - Window Closed delegate
43 // ---------------------------------------------------------------------------
45 @interface wxCPWCDelegate : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
52 - (BOOL)windowShouldClose:(id)sender;
54 @end // interface wxNSFontPanelDelegate : NSObject
56 @implementation wxCPWCDelegate : NSObject
66 - (BOOL)windowShouldClose:(id)sender
82 @end // wxNSFontPanelDelegate
88 wxColourDialog::wxColourDialog()
90 m_dialogParent = NULL;
93 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
98 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
100 m_dialogParent = parent;
103 m_colourData = *data;
105 //autorelease pool - req'd for carbon
106 NSAutoreleasePool *thePool;
107 thePool = [[NSAutoreleasePool alloc] init];
109 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
110 if(m_colourData.GetColour().IsOk())
111 [[NSColorPanel sharedColorPanel] setColor:
112 [NSColor colorWithCalibratedRed:(CGFloat) (m_colourData.GetColour().Red() / 255.0)
113 green:(CGFloat) (m_colourData.GetColour().Green() / 255.0)
114 blue:(CGFloat) (m_colourData.GetColour().Blue() / 255.0)
115 alpha:(CGFloat) (m_colourData.GetColour().Alpha() / 255.0)]
118 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
120 //We're done - free up the pool
125 int wxColourDialog::ShowModal()
127 WX_HOOK_MODAL_DIALOG();
129 //Start the pool. Required for carbon interaction
130 //(For those curious, the only thing that happens
131 //if you don't do this is a bunch of error
132 //messages about leaks on the console,
133 //with no windows shown or anything).
134 NSAutoreleasePool *thePool;
135 thePool = [[NSAutoreleasePool alloc] init];
137 //Get the shared color and font panel
138 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
140 //Create and assign the delegates (cocoa event handlers) so
141 //we can tell if a window has closed/open or not
142 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
143 [theColorPanel setDelegate:theCPDelegate];
146 // Start the color panel modal loop
148 wxDialog::OSXBeginModalDialog();
149 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
152 [NSApp runModalSession:session];
154 //If the color panel is closed, return the font panel modal loop
155 if ([theCPDelegate isClosed])
158 [NSApp endModalSession:session];
159 wxDialog::OSXEndModalDialog();
161 //free up the memory for the delegates - we don't need them anymore
162 [theColorPanel setDelegate:nil];
163 [theCPDelegate release];
165 //Get the shared color panel along with the chosen color and set the chosen color
166 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
168 m_colourData.GetColour().Set(
169 (unsigned char) ([theColor redComponent] * 255.0),
170 (unsigned char) ([theColor greenComponent] * 255.0),
171 (unsigned char) ([theColor blueComponent] * 255.0),
172 (unsigned char) ([theColor alphaComponent] * 255.0)
175 //Release the pool, we're done :)
178 //Return ID_OK - there are no "apply" buttons or the like
179 //on either the font or color panel
183 #endif //use native font dialog