]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/colordlgosx.mm
b40911c3a0c04c191cd97fd7d91470b29f004d7a
[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 m_bIsClosed = true;
73
74 [NSApp abortModal];
75 [NSApp stopModal];
76 return YES;
77 }
78
79 - (BOOL)isClosed
80 {
81 return m_bIsClosed;
82 }
83
84 @end // wxNSFontPanelDelegate
85
86 /*
87 * wxColourDialog
88 */
89
90 wxColourDialog::wxColourDialog()
91 {
92 m_dialogParent = NULL;
93 }
94
95 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
96 {
97 Create(parent, data);
98 }
99
100 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
101 {
102 m_dialogParent = parent;
103
104 if (data)
105 m_colourData = *data;
106
107 //
108 // This is the key call - this initializes
109 // events and window stuff for cocoa for carbon
110 // applications.
111 //
112 // This is also the only call here that is
113 // 10.2+ specific (the rest is OSX only),
114 // which, ironically, the carbon font
115 // panel requires.
116 //
117 bool bOK = NSApplicationLoad();
118
119 //autorelease pool - req'd for carbon
120 NSAutoreleasePool *thePool;
121 thePool = [[NSAutoreleasePool alloc] init];
122
123 if(m_colourData.m_dataColour.Ok())
124 [[NSColorPanel sharedColorPanel] setColor:
125 [NSColor colorWithCalibratedRed:m_colourData.m_dataColour.Red() / 255.0
126 green:m_colourData.m_dataColour.Green() / 255.0
127 blue:m_colourData.m_dataColour.Blue() / 255.0
128 alpha:1.0]
129 ];
130 else
131 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
132
133 //We're done - free up the pool
134 [thePool release];
135
136 return bOK;
137 }
138 int wxColourDialog::ShowModal()
139 {
140 //Start the pool. Required for carbon interaction
141 //(For those curious, the only thing that happens
142 //if you don't do this is a bunch of error
143 //messages about leaks on the console,
144 //with no windows shown or anything).
145 NSAutoreleasePool *thePool;
146 thePool = [[NSAutoreleasePool alloc] init];
147
148 //Get the shared color and font panel
149 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
150
151 //Create and assign the delegates (cocoa event handlers) so
152 //we can tell if a window has closed/open or not
153 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
154 [theColorPanel setDelegate:theCPDelegate];
155
156 //
157 // Start the color panel modal loop
158 //
159 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
160 for (;;)
161 {
162 [NSApp runModalSession:session];
163
164 //If the color panel is closed, return the font panel modal loop
165 if ([theCPDelegate isClosed])
166 break;
167 }
168 [NSApp endModalSession:session];
169
170 //free up the memory for the delegates - we don't need them anymore
171 [theCPDelegate release];
172
173 //Get the shared color panel along with the chosen color and set the chosen color
174 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
175
176 m_colourData.m_dataColour.Set(
177 (unsigned char) ([theColor redComponent] * 255.0),
178 (unsigned char) ([theColor greenComponent] * 255.0),
179 (unsigned char) ([theColor blueComponent] * 255.0)
180 );
181
182 //Release the pool, we're done :)
183 [thePool release];
184
185 //Return ID_OK - there are no "apply" buttons or the like
186 //on either the font or color panel
187 return wxID_OK;
188 }
189
190 #endif //use native font dialog
191