]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/fontdlg.cpp
fixed fatal bug when moving the control after itself in tab order
[wxWidgets.git] / src / mac / carbon / fontdlg.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: fontdlg.cpp
72fcdc75
RN
3// Purpose: wxFontDialog class for carbon 10.2+.
4// Author: Ryan Norton
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
72fcdc75
RN
8// Copyright: (c) Ryan Norton
9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
1553abf4
RN
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
e9576ca5
SC
20#ifdef __GNUG__
21#pragma implementation "fontdlg.h"
22#endif
23
1553abf4
RN
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
1553abf4
RN
32 #include "wx/cmndata.h"
33#endif
e9576ca5 34
154ad28b
RN
35#include "wx/mac/fontdlg.h"
36
2f1ae414 37#if !USE_SHARED_LIBRARY
e9576ca5 38IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
2f1ae414 39#endif
e9576ca5 40
72fcdc75
RN
41#include "wx/mac/private.h"
42
1553abf4 43//Mac OSX 10.2+ only
72fcdc75
RN
44#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
45
46#include <ATSUnicode.h>
47
48#include "wx/msgdlg.h"
49
641b3952
RN
50//color isn't working in carbon impl
51#define ISCOLORWORKING 0
52
1553abf4
RN
53// ============================================================================
54// implementation
55// ============================================================================
56
57// ---------------------------------------------------------------------------
58// Carbon event callback(s)
59// ---------------------------------------------------------------------------
60
72fcdc75
RN
61pascal OSStatus wxFontDialogEventHandler( EventHandlerCallRef inHandlerCallRef,
62 EventRef event, void* pData)
63{
64 wxASSERT(GetEventClass(event) == kEventClassFont &&
65 GetEventKind(event) == kEventFontSelection);
66
67 OSStatus status = noErr;
68
ffa561cf 69 FMFontFamily fontfamily;
c0a6772a
RN
70 FMFontStyle fontstyle;
71 FMFontSize fontsize;
641b3952 72#if ISCOLORWORKING
c0a6772a 73 RGBColor fontcolor;
641b3952 74#endif
72fcdc75 75
ffa561cf
RN
76 status = GetEventParameter (event, kEventParamFMFontFamily,
77 typeFMFontFamily, NULL,
78 sizeof (FMFontFamily),
79 NULL, &(fontfamily));
80 check_noerr (status);
81
72fcdc75
RN
82 status = GetEventParameter (event, kEventParamFMFontStyle,
83 typeFMFontStyle, NULL,
84 sizeof (FMFontStyle),
85 NULL, &(fontstyle));
86 check_noerr (status);
87
c0a6772a
RN
88 status = GetEventParameter (event, kEventParamFMFontSize,
89 typeFMFontSize, NULL,
90 sizeof (FMFontSize),
72fcdc75
RN
91 NULL, &(fontsize));
92
93 check_noerr (status);
94
641b3952 95#if ISCOLORWORKING
72fcdc75
RN
96 status = GetEventParameter (event, kEventParamFontColor,
97 typeRGBColor, NULL,
98 sizeof( RGBColor ), NULL, &fontcolor);
99 check_noerr (status);
641b3952 100#endif
72fcdc75 101
72fcdc75
RN
102 //now do the conversion to the wx font data
103 wxFontData theFontData;
104 wxFont theFont;
641b3952
RN
105
106#if ISCOLORWORKING
c0a6772a 107 //set color
72fcdc75
RN
108 wxColour theColor;
109 theColor.Set(&(WXCOLORREF&)fontcolor);
110 theFontData.SetColour(theColor);
641b3952 111#endif
c0a6772a
RN
112
113 //set size
114 theFont.SetPointSize(fontsize);
72fcdc75 115
c0a6772a 116 //set name
ffa561cf
RN
117 Str255 theFontName;
118 GetFontName(fontfamily, theFontName);
119 theFont.SetFaceName(wxMacMakeStringFromPascal(theFontName));
120
72fcdc75
RN
121 //TODOTODO: Get font family - mayby by the script code?
122 theFont.SetFamily(wxDEFAULT);
123
c0a6772a 124 //TODOTODO: Get other styles?
810d800e 125 theFont.SetStyle(((fontstyle & italic) ? wxFONTSTYLE_ITALIC : 0));
72fcdc75 126 theFont.SetWeight((fontstyle & bold) ? wxBOLD : wxNORMAL);
810d800e 127 theFont.SetUnderlined(((fontstyle & underline) ? true : false));
c0a6772a 128
72fcdc75
RN
129 //phew!! We're done - set the chosen font
130 theFontData.SetChosenFont(theFont);
131 ((wxFontDialog*)pData)->SetData(theFontData);
132
133 return status;
134}
135
136DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler );
137
1553abf4
RN
138// ---------------------------------------------------------------------------
139// wxFontDialog
140// ---------------------------------------------------------------------------
72fcdc75
RN
141
142wxFontDialog::wxFontDialog() :
143 m_dialogParent(NULL), m_pEventHandlerRef(NULL)
144{
145}
146
147wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
148{
149 Create(parent, data);
150}
151
152wxFontDialog::~wxFontDialog()
153{
154 if (m_pEventHandlerRef)
155 ::RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
156}
157
158void wxFontDialog::SetData(wxFontData& fontdata)
159{
160 m_fontData = fontdata;
161}
162
163bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
164{
165 m_dialogParent = parent;
166 m_fontData = data;
167
168 //Register the events that will return this dialog
169 EventTypeSpec ftEventList[] = { { kEventClassFont, kEventFontSelection } };
170
171 OSStatus err = noErr;
172
173//FIXMEFIXME: Why doesn't it recieve events if there's a parent?
174// if (parent)
175// {
176// err = InstallWindowEventHandler(
177// MAC_WXHWND(parent->GetHandle()),
178// GetwxFontDialogEventHandlerUPP(),
179// GetEventTypeCount(ftEventList), ftEventList,
180// this, (&(EventHandlerRef&)m_pEventHandlerRef));
181//
182// }
183// else //no parent - send to app
184// {
185 err = InstallApplicationEventHandler(
186 GetwxFontDialogEventHandlerUPP(),
187 GetEventTypeCount(ftEventList), ftEventList,
188 this, (&(EventHandlerRef&)m_pEventHandlerRef));
189// }
190
191 return err == noErr;
192}
193
194bool wxFontDialog::IsShown() const
195{
196 return FPIsFontPanelVisible();
197}
198
199int wxFontDialog::ShowModal()
200{
201 wxASSERT(!FPIsFontPanelVisible());
202
203 //set up initial font
204 wxFont theInitialFont = m_fontData.GetInitialFont();
205
206 //create ATSU style
207 ATSUStyle theStyle;
208 OSStatus status = ATSUCreateStyle(&theStyle);
209 check_noerr(status);
210
211 //put stuff into the style - we don't need kATSUColorTag
212 ATSUFontID fontid = theInitialFont.MacGetATSUFontID();
213 Fixed fontsize = theInitialFont.MacGetFontSize() << 16;
214 ATSUAttributeTag theTags[2] = { kATSUFontTag, kATSUSizeTag };
215 ByteCount theSizes[2] = { sizeof(ATSUFontID), sizeof(Fixed) };
216 ATSUAttributeValuePtr theValues[2] = { &fontid,
217 &fontsize };
218
219 //set the stuff for the ATSU style
220 verify_noerr (ATSUSetAttributes (theStyle, 2, theTags, theSizes, theValues) );
221
222 //they set us up the bomb! Set the initial font of the dialog
223 SetFontInfoForSelection(kFontSelectionATSUIType,
224 1,
225 &theStyle,
226 (HIObjectRef)
227 (m_dialogParent ?
228 GetWindowEventTarget(MAC_WXHWND(m_dialogParent->GetHandle())) :
229 GetApplicationEventTarget())
230 );
231
232 //dispose of the style
233 status = ATSUDisposeStyle(theStyle);
234 check_noerr(status);
235
f5698096
RN
236 //in case the user doesn't choose anything -
237 //if he doesn't we'll get a bad font with red text
238 m_fontData.SetChosenFont(m_fontData.GetInitialFont());
239 m_fontData.SetColour(wxColour(0,0,0));
240
72fcdc75
RN
241 //finally, show the font dialog
242 if( (status = FPShowHideFontPanel()) == noErr)
243 {
244 while(FPIsFontPanelVisible())
245 {
246 //yeild so we can get events
641b3952 247 wxTheApp->Yield(false);
72fcdc75
RN
248 }
249 }
250 else
251 return wxID_CANCEL;
252
253 return wxID_OK;
254}
255
256
257#else
258 //10.2+ only
259
1553abf4
RN
260// ---------------------------------------------------------------------------
261// wxFontDialog stub for mac OS's without a native font dialog
262// ---------------------------------------------------------------------------
e9576ca5
SC
263
264wxFontDialog::wxFontDialog()
265{
266 m_dialogParent = NULL;
267}
268
baaae89f 269wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
e9576ca5
SC
270{
271 Create(parent, data);
272}
273
72fcdc75
RN
274void wxFontDialog::SetData(wxFontData& fontdata)
275{
276 m_fontData = fontdata;
277}
278
baaae89f 279bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
e9576ca5
SC
280{
281 m_dialogParent = parent;
282
baaae89f 283 m_fontData = data;
e9576ca5
SC
284
285 // TODO: you may need to do dialog creation here, unless it's
286 // done in ShowModal.
287 return TRUE;
288}
289
72fcdc75
RN
290bool wxFontDialog::IsShown() const
291{
292 return false;
293}
294
e9576ca5
SC
295int wxFontDialog::ShowModal()
296{
297 // TODO: show (maybe create) the dialog
298 return wxID_CANCEL;
299}
300
72fcdc75 301#endif // 10.2+