]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/colordlgosx.mm
cleanup
[wxWidgets.git] / src / mac / carbon / colordlgosx.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/colordlg.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 // RCS-ID: $Id$
9 // Copyright: (c) Ryan Norton
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ===========================================================================
14 // declarations
15 // ===========================================================================
16
17 // ---------------------------------------------------------------------------
18 // headers
19 // ---------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #include "wx/mac/colordlg.h"
24 #include "wx/fontdlg.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 // Cocoa headers
36 #include "wx/cocoa/autorelease.h"
37 #include "wx/cocoa/string.h"
38
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>
44
45 // ---------------------------------------------------------------------------
46 // wxCPWCDelegate - Window Closed delegate
47 // ---------------------------------------------------------------------------
48
49 @interface wxCPWCDelegate : NSObject
50 {
51 bool m_bIsClosed;
52 }
53
54 // Delegate methods
55 - (id)init;
56 - (BOOL)windowShouldClose:(id)sender;
57 - (BOOL)isClosed;
58 @end // interface wxNSFontPanelDelegate : NSObject
59
60 @implementation wxCPWCDelegate : NSObject
61
62 - (id)init
63 {
64 [super init];
65 m_bIsClosed = false;
66
67 return self;
68 }
69
70 - (BOOL)windowShouldClose:(id)sender
71 {
72 wxUnusedVar(sender);
73
74 m_bIsClosed = true;
75
76 [NSApp abortModal];
77 [NSApp stopModal];
78 return YES;
79 }
80
81 - (BOOL)isClosed
82 {
83 return m_bIsClosed;
84 }
85
86 @end // wxNSFontPanelDelegate
87
88 /*
89 * wxColourDialog
90 */
91
92 wxColourDialog::wxColourDialog()
93 {
94 m_dialogParent = NULL;
95 }
96
97 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
98 {
99 Create(parent, data);
100 }
101
102 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
103 {
104 m_dialogParent = parent;
105
106 if (data)
107 m_colourData = *data;
108
109 //
110 // This is the key call - this initializes
111 // events and window stuff for cocoa for carbon
112 // applications.
113 //
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
117 // panel requires.
118 //
119 bool bOK = NSApplicationLoad();
120
121 //autorelease pool - req'd for carbon
122 NSAutoreleasePool *thePool;
123 thePool = [[NSAutoreleasePool alloc] init];
124
125 if(m_colourData.m_dataColour.Ok())
126 [[NSColorPanel sharedColorPanel] setColor:
127 [NSColor colorWithCalibratedRed:m_colourData.m_dataColour.Red() / 255.0
128 green:m_colourData.m_dataColour.Green() / 255.0
129 blue:m_colourData.m_dataColour.Blue() / 255.0
130 alpha:1.0]
131 ];
132 else
133 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
134
135 //We're done - free up the pool
136 [thePool release];
137
138 return bOK;
139 }
140 int wxColourDialog::ShowModal()
141 {
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];
149
150 //Get the shared color and font panel
151 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
152
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];
157
158 //
159 // Start the color panel modal loop
160 //
161 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
162 for (;;)
163 {
164 [NSApp runModalSession:session];
165
166 //If the color panel is closed, return the font panel modal loop
167 if ([theCPDelegate isClosed])
168 break;
169 }
170 [NSApp endModalSession:session];
171
172 //free up the memory for the delegates - we don't need them anymore
173 [theCPDelegate release];
174
175 //Get the shared color panel along with the chosen color and set the chosen color
176 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
177
178 m_colourData.m_dataColour.Set(
179 (unsigned char) ([theColor redComponent] * 255.0),
180 (unsigned char) ([theColor greenComponent] * 255.0),
181 (unsigned char) ([theColor blueComponent] * 255.0)
182 );
183
184 //Release the pool, we're done :)
185 [thePool release];
186
187 //Return ID_OK - there are no "apply" buttons or the like
188 //on either the font or color panel
189 return wxID_OK;
190 }
191
192 #endif //use native font dialog
193