1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/colordlg.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/osx/colordlg.h"
24 #include "wx/fontdlg.h"
26 // ============================================================================
28 // ============================================================================
31 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
33 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
36 #include "wx/cocoa/autorelease.h"
37 #include "wx/cocoa/string.h"
39 #import <AppKit/NSFont.h>
40 #import <AppKit/NSFontManager.h>
41 #import <AppKit/NSFontPanel.h>
42 #import <AppKit/NSColor.h>
43 #import <AppKit/NSColorPanel.h>
45 // ---------------------------------------------------------------------------
46 // wxCPWCDelegate - Window Closed delegate
47 // ---------------------------------------------------------------------------
49 @interface wxCPWCDelegate : NSObject
56 - (BOOL)windowShouldClose:(id)sender;
58 @end // interface wxNSFontPanelDelegate : NSObject
60 @implementation wxCPWCDelegate : NSObject
70 - (BOOL)windowShouldClose:(id)sender
86 @end // wxNSFontPanelDelegate
92 wxColourDialog::wxColourDialog()
94 m_dialogParent = NULL;
97 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
102 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
104 m_dialogParent = parent;
107 m_colourData = *data;
110 // This is the key call - this initializes
111 // events and window stuff for cocoa for carbon
114 // This is also the only call here that is
115 // 10.2+ specific (the rest is OSX only),
116 // which, ironically, the carbon font
119 bool bOK = NSApplicationLoad();
121 //autorelease pool - req'd for carbon
122 NSAutoreleasePool *thePool;
123 thePool = [[NSAutoreleasePool alloc] init];
125 if(m_colourData.GetColour().IsOk())
126 [[NSColorPanel sharedColorPanel] setColor:
127 [NSColor colorWithCalibratedRed:m_colourData.GetColour().Red() / 255.0
128 green:m_colourData.GetColour().Green() / 255.0
129 blue:m_colourData.GetColour().Blue() / 255.0
133 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
135 //We're done - free up the pool
140 int wxColourDialog::ShowModal()
142 //Start the pool. Required for carbon interaction
143 //(For those curious, the only thing that happens
144 //if you don't do this is a bunch of error
145 //messages about leaks on the console,
146 //with no windows shown or anything).
147 NSAutoreleasePool *thePool;
148 thePool = [[NSAutoreleasePool alloc] init];
150 //Get the shared color and font panel
151 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
153 //Create and assign the delegates (cocoa event handlers) so
154 //we can tell if a window has closed/open or not
155 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
156 [theColorPanel setDelegate:theCPDelegate];
159 // Start the color panel modal loop
161 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
164 [NSApp runModalSession:session];
166 //If the color panel is closed, return the font panel modal loop
167 if ([theCPDelegate isClosed])
170 [NSApp endModalSession:session];
172 //free up the memory for the delegates - we don't need them anymore
173 [theCPDelegate release];
175 //Get the shared color panel along with the chosen color and set the chosen color
176 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
178 m_colourData.GetColour().Set(
179 (unsigned char) ([theColor redComponent] * 255.0),
180 (unsigned char) ([theColor greenComponent] * 255.0),
181 (unsigned char) ([theColor blueComponent] * 255.0)
184 //Release the pool, we're done :)
187 //Return ID_OK - there are no "apply" buttons or the like
188 //on either the font or color panel
192 #endif //use native font dialog