1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxColourDialog class. NOTE: you can use the generic class
4 // if you wish, instead of implementing this.
5 // Author: Stefan Csomor
9 // Copyright: (c) Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
15 // ===========================================================================
17 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
22 #pragma implementation "colordlg.h"
25 #include "wx/mac/colordlg.h"
26 #include "wx/fontdlg.h"
28 // ============================================================================
30 // ============================================================================
33 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
35 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
40 #include "wx/cocoa/autorelease.h"
41 #include "wx/cocoa/string.h"
43 #import <AppKit/NSFont.h>
44 #import <AppKit/NSFontManager.h>
45 #import <AppKit/NSFontPanel.h>
46 #import <AppKit/NSColor.h>
47 #import <AppKit/NSColorPanel.h>
49 // ---------------------------------------------------------------------------
50 // wxCPWCDelegate - Window Closed delegate
51 // ---------------------------------------------------------------------------
53 @interface wxCPWCDelegate : NSObject
60 - (BOOL)windowShouldClose:(id)sender;
62 @end // interface wxNSFontPanelDelegate : NSObject
64 @implementation wxCPWCDelegate : NSObject
74 - (BOOL)windowShouldClose:(id)sender
88 @end // wxNSFontPanelDelegate
94 wxColourDialog::wxColourDialog()
96 m_dialogParent = NULL;
99 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
101 Create(parent, data);
104 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
106 m_dialogParent = parent;
109 m_colourData = *data;
112 // This is the key call - this initializes
113 // events and window stuff for cocoa for carbon
116 // This is also the only call here that is
117 // 10.2+ specific (the rest is OSX only),
118 // which, ironically, the carbon font
121 bool bOK = NSApplicationLoad();
123 //autorelease pool - req'd for carbon
124 NSAutoreleasePool *thePool;
125 thePool = [[NSAutoreleasePool alloc] init];
127 if(m_colourData.m_dataColour.Ok())
128 [[NSColorPanel sharedColorPanel] setColor:
129 [NSColor colorWithCalibratedRed:m_colourData.m_dataColour.Red() / 255.0
130 green:m_colourData.m_dataColour.Red() / 255.0
131 blue:m_colourData.m_dataColour.Red() / 255.0
135 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
137 //We're done - free up the pool
142 int wxColourDialog::ShowModal()
144 //Start the pool. Required for carbon interaction
145 //(For those curious, the only thing that happens
146 //if you don't do this is a bunch of error
147 //messages about leaks on the console,
148 //with no windows shown or anything).
149 NSAutoreleasePool *thePool;
150 thePool = [[NSAutoreleasePool alloc] init];
152 //Get the shared color and font panel
153 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
155 //Create and assign the delegates (cocoa event handlers) so
156 //we can tell if a window has closed/open or not
157 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
158 [theColorPanel setDelegate:theCPDelegate];
161 // Start the color panel modal loop
163 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
166 [NSApp runModalSession:session];
168 //If the color panel is closed, return the font panel modal loop
169 if ([theCPDelegate isClosed])
172 [NSApp endModalSession:session];
174 //free up the memory for the delegates - we don't need them anymore
175 [theCPDelegate release];
177 //Get the shared color panel along with the chosen color and set the chosen color
178 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
180 m_colourData.m_dataColour.Set(
181 (unsigned char) ([theColor redComponent] * 255.0),
182 (unsigned char) ([theColor greenComponent] * 255.0),
183 (unsigned char) ([theColor blueComponent] * 255.0)
186 //Release the pool, we're done :)
189 //Return ID_OK - there are no "apply" buttons or the like
190 //on either the font or color panel
194 #endif //use native font dialog