]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/colordlgosx.mm
should have been part of r74664: Avoid calling gtk_window_get_position() from "config...
[wxWidgets.git] / src / osx / carbon / colordlgosx.mm
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.
5 // Author: Ryan Norton
6 // Modified by:
7 // Created: 2004-11-16
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #include "wx/colordlg.h"
23 #include "wx/fontdlg.h"
24 #include "wx/modalhook.h"
25
26 // ============================================================================
27 // implementation
28 // ============================================================================
29
30 //Mac OSX 10.2+ only
31 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
32
33 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
34
35 #include "wx/osx/private.h"
36
37 #import <Foundation/Foundation.h>
38 #import <AppKit/AppKit.h>
39
40 // ---------------------------------------------------------------------------
41 // wxCPWCDelegate - Window Closed delegate
42 // ---------------------------------------------------------------------------
43
44 @interface wxCPWCDelegate : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
45 {
46 bool m_bIsClosed;
47 }
48
49 // Delegate methods
50 - (id)init;
51 - (BOOL)windowShouldClose:(id)sender;
52 - (BOOL)isClosed;
53 @end // interface wxNSFontPanelDelegate : NSObject
54
55 @implementation wxCPWCDelegate : NSObject
56
57 - (id)init
58 {
59 self = [super init];
60 m_bIsClosed = false;
61
62 return self;
63 }
64
65 - (BOOL)windowShouldClose:(id)sender
66 {
67 wxUnusedVar(sender);
68
69 m_bIsClosed = true;
70
71 [NSApp abortModal];
72 [NSApp stopModal];
73 return YES;
74 }
75
76 - (BOOL)isClosed
77 {
78 return m_bIsClosed;
79 }
80
81 @end // wxNSFontPanelDelegate
82
83 /*
84 * wxColourDialog
85 */
86
87 wxColourDialog::wxColourDialog()
88 {
89 m_dialogParent = NULL;
90 }
91
92 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
93 {
94 Create(parent, data);
95 }
96
97 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
98 {
99 m_dialogParent = parent;
100
101 if (data)
102 m_colourData = *data;
103
104 //autorelease pool - req'd for carbon
105 NSAutoreleasePool *thePool;
106 thePool = [[NSAutoreleasePool alloc] init];
107
108 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
109 if(m_colourData.GetColour().IsOk())
110 [[NSColorPanel sharedColorPanel] setColor:
111 [NSColor colorWithCalibratedRed:(CGFloat) (m_colourData.GetColour().Red() / 255.0)
112 green:(CGFloat) (m_colourData.GetColour().Green() / 255.0)
113 blue:(CGFloat) (m_colourData.GetColour().Blue() / 255.0)
114 alpha:(CGFloat) (m_colourData.GetColour().Alpha() / 255.0)]
115 ];
116 else
117 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
118
119 //We're done - free up the pool
120 [thePool release];
121
122 return true;
123 }
124 int wxColourDialog::ShowModal()
125 {
126 WX_HOOK_MODAL_DIALOG();
127
128 //Start the pool. Required for carbon interaction
129 //(For those curious, the only thing that happens
130 //if you don't do this is a bunch of error
131 //messages about leaks on the console,
132 //with no windows shown or anything).
133 NSAutoreleasePool *thePool;
134 thePool = [[NSAutoreleasePool alloc] init];
135
136 //Get the shared color and font panel
137 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
138
139 //Create and assign the delegates (cocoa event handlers) so
140 //we can tell if a window has closed/open or not
141 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
142 [theColorPanel setDelegate:theCPDelegate];
143
144 //
145 // Start the color panel modal loop
146 //
147 wxDialog::OSXBeginModalDialog();
148 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
149 for (;;)
150 {
151 [NSApp runModalSession:session];
152
153 //If the color panel is closed, return the font panel modal loop
154 if ([theCPDelegate isClosed])
155 break;
156 }
157 [NSApp endModalSession:session];
158 wxDialog::OSXEndModalDialog();
159
160 //free up the memory for the delegates - we don't need them anymore
161 [theColorPanel setDelegate:nil];
162 [theCPDelegate release];
163
164 //Get the shared color panel along with the chosen color and set the chosen color
165 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
166
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),
171 (unsigned char) ([theColor alphaComponent] * 255.0)
172 );
173
174 //Release the pool, we're done :)
175 [thePool release];
176
177 //Return ID_OK - there are no "apply" buttons or the like
178 //on either the font or color panel
179 return wxID_OK;
180 }
181
182 #endif //use native font dialog
183