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