]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/colordlgosx.mm
Allow custom wxDataViewCtrl renderers to easily use attributes.
[wxWidgets.git] / src / osx / carbon / colordlgosx.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/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 //autorelease pool - req'd for carbon
110 NSAutoreleasePool *thePool;
111 thePool = [[NSAutoreleasePool alloc] init];
112
113 if(m_colourData.GetColour().IsOk())
114 [[NSColorPanel sharedColorPanel] setColor:
115 [NSColor colorWithCalibratedRed:(CGFloat) (m_colourData.GetColour().Red() / 255.0)
116 green:(CGFloat) (m_colourData.GetColour().Green() / 255.0)
117 blue:(CGFloat) (m_colourData.GetColour().Blue() / 255.0)
118 alpha:(CGFloat) 1.0]
119 ];
120 else
121 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
122
123 //We're done - free up the pool
124 [thePool release];
125
126 return true;
127 }
128 int wxColourDialog::ShowModal()
129 {
130 //Start the pool. Required for carbon interaction
131 //(For those curious, the only thing that happens
132 //if you don't do this is a bunch of error
133 //messages about leaks on the console,
134 //with no windows shown or anything).
135 NSAutoreleasePool *thePool;
136 thePool = [[NSAutoreleasePool alloc] init];
137
138 //Get the shared color and font panel
139 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
140
141 //Create and assign the delegates (cocoa event handlers) so
142 //we can tell if a window has closed/open or not
143 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
144 [theColorPanel setDelegate:theCPDelegate];
145
146 //
147 // Start the color panel modal loop
148 //
149 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
150 for (;;)
151 {
152 [NSApp runModalSession:session];
153
154 //If the color panel is closed, return the font panel modal loop
155 if ([theCPDelegate isClosed])
156 break;
157 }
158 [NSApp endModalSession:session];
159
160 //free up the memory for the delegates - we don't need them anymore
161 [theColorPanel setDelegate:nil];
162 [theCPDelegate release];
163
164 //Get the shared color panel along with the chosen color and set the chosen color
165 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
166
167 m_colourData.GetColour().Set(
168 (unsigned char) ([theColor redComponent] * 255.0),
169 (unsigned char) ([theColor greenComponent] * 255.0),
170 (unsigned char) ([theColor blueComponent] * 255.0)
171 );
172
173 //Release the pool, we're done :)
174 [thePool release];
175
176 //Return ID_OK - there are no "apply" buttons or the like
177 //on either the font or color panel
178 return wxID_OK;
179 }
180
181 #endif //use native font dialog
182