1 /////////////////////////////////////////////////////////////////////////////
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/mac/colordlg.h"
22 #include "wx/fontdlg.h"
24 // ============================================================================
26 // ============================================================================
29 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
31 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
34 #include "wx/cocoa/autorelease.h"
35 #include "wx/cocoa/string.h"
37 #import <AppKit/NSFont.h>
38 #import <AppKit/NSFontManager.h>
39 #import <AppKit/NSFontPanel.h>
40 #import <AppKit/NSColor.h>
41 #import <AppKit/NSColorPanel.h>
43 // ---------------------------------------------------------------------------
44 // wxCPWCDelegate - Window Closed delegate
45 // ---------------------------------------------------------------------------
47 @interface wxCPWCDelegate : NSObject
54 - (BOOL)windowShouldClose:(id)sender;
56 @end // interface wxNSFontPanelDelegate : NSObject
58 @implementation wxCPWCDelegate : NSObject
68 - (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;
106 // This is the key call - this initializes
107 // events and window stuff for cocoa for carbon
110 // This is also the only call here that is
111 // 10.2+ specific (the rest is OSX only),
112 // which, ironically, the carbon font
115 bool bOK = NSApplicationLoad();
117 //autorelease pool - req'd for carbon
118 NSAutoreleasePool *thePool;
119 thePool = [[NSAutoreleasePool alloc] init];
121 if(m_colourData.m_dataColour.Ok())
122 [[NSColorPanel sharedColorPanel] setColor:
123 [NSColor colorWithCalibratedRed:m_colourData.m_dataColour.Red() / 255.0
124 green:m_colourData.m_dataColour.Green() / 255.0
125 blue:m_colourData.m_dataColour.Blue() / 255.0
129 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
131 //We're done - free up the pool
136 int wxColourDialog::ShowModal()
138 //Start the pool. Required for carbon interaction
139 //(For those curious, the only thing that happens
140 //if you don't do this is a bunch of error
141 //messages about leaks on the console,
142 //with no windows shown or anything).
143 NSAutoreleasePool *thePool;
144 thePool = [[NSAutoreleasePool alloc] init];
146 //Get the shared color and font panel
147 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
149 //Create and assign the delegates (cocoa event handlers) so
150 //we can tell if a window has closed/open or not
151 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
152 [theColorPanel setDelegate:theCPDelegate];
155 // Start the color panel modal loop
157 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
160 [NSApp runModalSession:session];
162 //If the color panel is closed, return the font panel modal loop
163 if ([theCPDelegate isClosed])
166 [NSApp endModalSession:session];
168 //free up the memory for the delegates - we don't need them anymore
169 [theCPDelegate release];
171 //Get the shared color panel along with the chosen color and set the chosen color
172 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
174 m_colourData.m_dataColour.Set(
175 (unsigned char) ([theColor redComponent] * 255.0),
176 (unsigned char) ([theColor greenComponent] * 255.0),
177 (unsigned char) ([theColor blueComponent] * 255.0)
180 //Release the pool, we're done :)
183 //Return ID_OK - there are no "apply" buttons or the like
184 //on either the font or color panel
188 #endif //use native font dialog