]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/colordlgosx.mm
notify the event loop that synthesized events are on the queue, wait for them to...
[wxWidgets.git] / src / osx / carbon / colordlgosx.mm
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/osx/carbon/colordlgosx.mm
489468fe
SC
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
17811bf9 23#include "wx/colordlg.h"
489468fe 24#include "wx/fontdlg.h"
691745ab 25#include "wx/modalhook.h"
489468fe
SC
26
27// ============================================================================
28// implementation
29// ============================================================================
30
31//Mac OSX 10.2+ only
32#if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
33
34IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
35
c8fdb345 36#include "wx/osx/private.h"
489468fe 37
c8fdb345
SC
38#import <Foundation/Foundation.h>
39#import <AppKit/AppKit.h>
489468fe
SC
40
41// ---------------------------------------------------------------------------
42// wxCPWCDelegate - Window Closed delegate
43// ---------------------------------------------------------------------------
44
c8fdb345 45@interface wxCPWCDelegate : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
489468fe
SC
46{
47 bool m_bIsClosed;
48}
49
50// Delegate methods
51- (id)init;
52- (BOOL)windowShouldClose:(id)sender;
53- (BOOL)isClosed;
54@end // interface wxNSFontPanelDelegate : NSObject
55
56@implementation wxCPWCDelegate : NSObject
57
58- (id)init
59{
415dfa55 60 self = [super init];
489468fe
SC
61 m_bIsClosed = false;
62
63 return self;
64}
65
66- (BOOL)windowShouldClose:(id)sender
67{
68 wxUnusedVar(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
88wxColourDialog::wxColourDialog()
89{
90 m_dialogParent = NULL;
91}
92
93wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
94{
95 Create(parent, data);
96}
97
98bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
99{
100 m_dialogParent = parent;
101
102 if (data)
103 m_colourData = *data;
104
489468fe
SC
105 //autorelease pool - req'd for carbon
106 NSAutoreleasePool *thePool;
107 thePool = [[NSAutoreleasePool alloc] init];
108
97e015d1 109 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
6869b469 110 if(m_colourData.GetColour().IsOk())
489468fe 111 [[NSColorPanel sharedColorPanel] setColor:
5b304314
SC
112 [NSColor colorWithCalibratedRed:(CGFloat) (m_colourData.GetColour().Red() / 255.0)
113 green:(CGFloat) (m_colourData.GetColour().Green() / 255.0)
114 blue:(CGFloat) (m_colourData.GetColour().Blue() / 255.0)
d485bda1 115 alpha:(CGFloat) (m_colourData.GetColour().Alpha() / 255.0)]
489468fe
SC
116 ];
117 else
118 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
119
120 //We're done - free up the pool
121 [thePool release];
122
b3ba1043 123 return true;
489468fe
SC
124}
125int wxColourDialog::ShowModal()
126{
691745ab 127 WX_HOOK_MODAL_DIALOG();
643e9cf9 128
489468fe
SC
129 //Start the pool. Required for carbon interaction
130 //(For those curious, the only thing that happens
131 //if you don't do this is a bunch of error
132 //messages about leaks on the console,
133 //with no windows shown or anything).
134 NSAutoreleasePool *thePool;
135 thePool = [[NSAutoreleasePool alloc] init];
136
137 //Get the shared color and font panel
138 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
139
140 //Create and assign the delegates (cocoa event handlers) so
141 //we can tell if a window has closed/open or not
142 wxCPWCDelegate* theCPDelegate = [[wxCPWCDelegate alloc] init];
143 [theColorPanel setDelegate:theCPDelegate];
144
145 //
146 // Start the color panel modal loop
147 //
445e564f 148 wxDialog::OSXBeginModalDialog();
489468fe
SC
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];
445e564f 159 wxDialog::OSXEndModalDialog();
489468fe
SC
160
161 //free up the memory for the delegates - we don't need them anymore
b3ba1043 162 [theColorPanel setDelegate:nil];
489468fe
SC
163 [theCPDelegate release];
164
165 //Get the shared color panel along with the chosen color and set the chosen color
166 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
167
6869b469 168 m_colourData.GetColour().Set(
489468fe
SC
169 (unsigned char) ([theColor redComponent] * 255.0),
170 (unsigned char) ([theColor greenComponent] * 255.0),
d485bda1
SC
171 (unsigned char) ([theColor blueComponent] * 255.0),
172 (unsigned char) ([theColor alphaComponent] * 255.0)
173 );
489468fe
SC
174
175 //Release the pool, we're done :)
176 [thePool release];
177
178 //Return ID_OK - there are no "apply" buttons or the like
179 //on either the font or color panel
180 return wxID_OK;
181}
182
183#endif //use native font dialog
184