]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/colordlgosx.mm
Include wx/object.h according to precompiled headers of wx/wx.h (with other minor...
[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 #include "wx/mac/colordlg.h"
22 #include "wx/fontdlg.h"
23
24 // ============================================================================
25 // implementation
26 // ============================================================================
27
28 //Mac OSX 10.2+ only
29 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
30
31 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
32
33 // Cocoa headers
34 #include "wx/cocoa/autorelease.h"
35 #include "wx/cocoa/string.h"
36
37 #import <AppKit/NSFont.h>
38 #import <AppKit/NSFontManager.h>
39 #import <AppKit/NSFontPanel.h>
40 #import <AppKit/NSColor.h>
41 #import <AppKit/NSColorPanel.h>
42
43 // ---------------------------------------------------------------------------
44 // wxCPWCDelegate - Window Closed delegate
45 // ---------------------------------------------------------------------------
46
47 @interface wxCPWCDelegate : NSObject
48 {
49 bool m_bIsClosed;
50 }
51
52 // Delegate methods
53 - (id)init;
54 - (BOOL)windowShouldClose:(id)sender;
55 - (BOOL)isClosed;
56 @end // interface wxNSFontPanelDelegate : NSObject
57
58 @implementation wxCPWCDelegate : NSObject
59
60 - (id)init
61 {
62 [super init];
63 m_bIsClosed = false;
64
65 return self;
66 }
67
68 - (BOOL)windowShouldClose:(id)sender
69 {
70 m_bIsClosed = true;
71
72 [NSApp abortModal];
73 [NSApp stopModal];
74 return YES;
75 }
76
77 - (BOOL)isClosed
78 {
79 return m_bIsClosed;
80 }
81
82 @end // wxNSFontPanelDelegate
83
84 /*
85 * wxColourDialog
86 */
87
88 wxColourDialog::wxColourDialog()
89 {
90 m_dialogParent = NULL;
91 }
92
93 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
94 {
95 Create(parent, data);
96 }
97
98 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
99 {
100 m_dialogParent = parent;
101
102 if (data)
103 m_colourData = *data;
104
105 //
106 // This is the key call - this initializes
107 // events and window stuff for cocoa for carbon
108 // applications.
109 //
110 // This is also the only call here that is
111 // 10.2+ specific (the rest is OSX only),
112 // which, ironically, the carbon font
113 // panel requires.
114 //
115 bool bOK = NSApplicationLoad();
116
117 //autorelease pool - req'd for carbon
118 NSAutoreleasePool *thePool;
119 thePool = [[NSAutoreleasePool alloc] init];
120
121 if(m_colourData.m_dataColour.Ok())
122 [[NSColorPanel sharedColorPanel] setColor:
123 [NSColor colorWithCalibratedRed:m_colourData.m_dataColour.Red() / 255.0
124 green:m_colourData.m_dataColour.Green() / 255.0
125 blue:m_colourData.m_dataColour.Blue() / 255.0
126 alpha:1.0]
127 ];
128 else
129 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
130
131 //We're done - free up the pool
132 [thePool release];
133
134 return bOK;
135 }
136 int wxColourDialog::ShowModal()
137 {
138 //Start the pool. Required for carbon interaction
139 //(For those curious, the only thing that happens
140 //if you don't do this is a bunch of error
141 //messages about leaks on the console,
142 //with no windows shown or anything).
143 NSAutoreleasePool *thePool;
144 thePool = [[NSAutoreleasePool alloc] init];
145
146 //Get the shared color and font panel
147 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
148
149 //Create and assign the delegates (cocoa event handlers) so
150 //we can tell if a window has closed/open or not
151 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
152 [theColorPanel setDelegate:theCPDelegate];
153
154 //
155 // Start the color panel modal loop
156 //
157 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
158 for (;;)
159 {
160 [NSApp runModalSession:session];
161
162 //If the color panel is closed, return the font panel modal loop
163 if ([theCPDelegate isClosed])
164 break;
165 }
166 [NSApp endModalSession:session];
167
168 //free up the memory for the delegates - we don't need them anymore
169 [theCPDelegate release];
170
171 //Get the shared color panel along with the chosen color and set the chosen color
172 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
173
174 m_colourData.m_dataColour.Set(
175 (unsigned char) ([theColor redComponent] * 255.0),
176 (unsigned char) ([theColor greenComponent] * 255.0),
177 (unsigned char) ([theColor blueComponent] * 255.0)
178 );
179
180 //Release the pool, we're done :)
181 [thePool release];
182
183 //Return ID_OK - there are no "apply" buttons or the like
184 //on either the font or color panel
185 return wxID_OK;
186 }
187
188 #endif //use native font dialog
189