]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/colordlgosx.mm
c792d21bff37f7b9836715f4147ef943986cc9ca
[wxWidgets.git] / src / mac / carbon / colordlgosx.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: colordlg.cpp
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 #ifdef __GNUG__
22 #pragma implementation "colordlg.h"
23 #endif
24
25 #include "wx/mac/colordlg.h"
26 #include "wx/fontdlg.h"
27
28 // ============================================================================
29 // implementation
30 // ============================================================================
31
32 //Mac OSX 10.2+ only
33 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
34
35 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
36
37 // Cocoa headers
38 #include "wx/cocoa/autorelease.h"
39 #include "wx/cocoa/string.h"
40
41 #import <AppKit/NSFont.h>
42 #import <AppKit/NSFontManager.h>
43 #import <AppKit/NSFontPanel.h>
44 #import <AppKit/NSColor.h>
45 #import <AppKit/NSColorPanel.h>
46
47 // ---------------------------------------------------------------------------
48 // wxCPWCDelegate - Window Closed delegate
49 // ---------------------------------------------------------------------------
50
51 @interface wxCPWCDelegate : NSObject
52 {
53 bool m_bIsClosed;
54 }
55
56 // Delegate methods
57 - (id)init;
58 - (BOOL)windowShouldClose:(id)sender;
59 - (BOOL)isClosed;
60 @end // interface wxNSFontPanelDelegate : NSObject
61
62 @implementation wxCPWCDelegate : NSObject
63
64 - (id)init
65 {
66 [super init];
67 m_bIsClosed = false;
68
69 return self;
70 }
71
72 - (BOOL)windowShouldClose:(id)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