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;
109 //autorelease pool - req'd for carbon
110 NSAutoreleasePool *thePool;
111 thePool = [[NSAutoreleasePool alloc] init];
113 if(m_colourData.GetColour().IsOk())
114 [[NSColorPanel sharedColorPanel] setColor:
115 [NSColor colorWithCalibratedRed:(CGFloat) (m_colourData.GetColour().Red() / 255.0)
116 green:(CGFloat) (m_colourData.GetColour().Green() / 255.0)
117 blue:(CGFloat) (m_colourData.GetColour().Blue() / 255.0)
121 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
123 //We're done - free up the pool
128 int wxColourDialog::ShowModal()
130 //Start the pool. Required for carbon interaction
131 //(For those curious, the only thing that happens
132 //if you don't do this is a bunch of error
133 //messages about leaks on the console,
134 //with no windows shown or anything).
135 NSAutoreleasePool *thePool;
136 thePool = [[NSAutoreleasePool alloc] init];
138 //Get the shared color and font panel
139 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
141 //Create and assign the delegates (cocoa event handlers) so
142 //we can tell if a window has closed/open or not
143 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
144 [theColorPanel setDelegate:theCPDelegate];
147 // Start the color panel modal loop
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];
160 //free up the memory for the delegates - we don't need them anymore
161 [theColorPanel setDelegate:nil];
162 [theCPDelegate release];
164 //Get the shared color panel along with the chosen color and set the chosen color
165 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
167 m_colourData.GetColour().Set(
168 (unsigned char) ([theColor redComponent] * 255.0),
169 (unsigned char) ([theColor greenComponent] * 255.0),
170 (unsigned char) ([theColor blueComponent] * 255.0)
173 //Release the pool, we're done :)
176 //Return ID_OK - there are no "apply" buttons or the like
177 //on either the font or color panel
181 #endif //use native font dialog