]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/fontdlg.cpp
44850225848023fd2ea395f224ec8139a66b093e
[wxWidgets.git] / src / mac / carbon / fontdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fontdlg.cpp
3 // Purpose: wxFontDialog class for carbon 10.2+.
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "fontdlg.h"
14 #endif
15
16 #include "wx/mac/fontdlg.h"
17 #include "wx/cmndata.h"
18
19 #if !USE_SHARED_LIBRARY
20 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
21 #endif
22
23 #include "wx/mac/private.h"
24
25 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
26
27 #include <ATSUnicode.h>
28
29 #include "wx/msgdlg.h"
30
31 pascal OSStatus wxFontDialogEventHandler( EventHandlerCallRef inHandlerCallRef,
32 EventRef event, void* pData)
33 {
34 wxASSERT(GetEventClass(event) == kEventClassFont &&
35 GetEventKind(event) == kEventFontSelection);
36
37 OSStatus status = noErr;
38
39 ATSUFontID fontid;
40 Fixed fontsize;
41 RGBColor fontcolor;
42 FMFontStyle fontstyle;
43
44 status = GetEventParameter (event, kEventParamFMFontStyle,
45 typeFMFontStyle, NULL,
46 sizeof (FMFontStyle),
47 NULL, &(fontstyle));
48 check_noerr (status);
49
50 status = GetEventParameter (event, kEventParamATSUFontID,
51 typeATSUFontID, NULL,
52 sizeof (ATSUFontID),
53 NULL, &(fontid));
54 check_noerr (status);
55
56 status = GetEventParameter (event, kEventParamATSUFontSize,
57 typeATSUSize, NULL,
58 sizeof (Fixed),
59 NULL, &(fontsize));
60
61 check_noerr (status);
62
63 status = GetEventParameter (event, kEventParamFontColor,
64 typeRGBColor, NULL,
65 sizeof( RGBColor ), NULL, &fontcolor);
66 check_noerr (status);
67
68 //TODO:
69 //TODO: Make a utility function for converting ATSU to WX font (what we're doing :))
70 //TODO:
71
72 //now do the conversion to the wx font data
73 wxFontData theFontData;
74 wxFont theFont;
75
76 //first, the easy part - set the color
77 wxColour theColor;
78 theColor.Set(&(WXCOLORREF&)fontcolor);
79 theFontData.SetColour(theColor);
80
81 //
82 //now the hard part - convert the atsu font to a wx font
83 //
84
85 //point size - fixed, but straight conversion, I think
86 theFont.SetPointSize(fontsize >> 16);
87
88 //font name - first get length, then allocate/copy/deallocate name buffer
89 FontNameCode theNameCode;
90 FontPlatformCode thePlatformCode;
91 FontScriptCode theScriptCode;
92 FontLanguageCode theLanguageCode;
93 //the above have types in SFNTTypes.h
94
95 ByteCount theActualLength;
96 ATSUGetIndFontName(fontid,
97 0, //first font in index array
98 0, //need to get length first
99 NULL, //nothin'
100 &theActualLength,
101 &theNameCode,
102 &thePlatformCode,
103 &theScriptCode,
104 &theLanguageCode);
105
106 Ptr szBuffer = NewPtr(theActualLength);
107 ATSUGetIndFontName(fontid,
108 0, //first font in index array
109 theActualLength,
110 szBuffer,
111 &theActualLength,
112 &theNameCode,
113 &thePlatformCode,
114 &theScriptCode,
115 &theLanguageCode);
116
117 //its unicode - convert it to wx's char value and put it in there
118 theFont.SetFaceName(wxConvLocal.cMB2WX((char*)szBuffer));
119 DisposePtr(szBuffer);
120
121 //TODOTODO: Get font family - mayby by the script code?
122 theFont.SetFamily(wxDEFAULT);
123
124 //TODOTODO: Get other styles? Font weight?
125 theFont.SetStyle(((fontstyle & italic) ? wxFONTSTYLE_ITALIC : 0));
126 theFont.SetWeight((fontstyle & bold) ? wxBOLD : wxNORMAL);
127 theFont.SetUnderlined(((fontstyle & underline) ? true : false));
128
129 //for debugging
130 //wxPrintf(wxT("FaceName:%s\nSize:%i\nStyle:%i\n"), theFont.GetFaceName().c_str(), theFont.GetPointSize(),
131 //theFont.GetStyle());
132
133 //phew!! We're done - set the chosen font
134 theFontData.SetChosenFont(theFont);
135 ((wxFontDialog*)pData)->SetData(theFontData);
136
137 return status;
138 }
139
140 DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler );
141
142 //---------------------------
143 // Class implementation
144 //---------------------------
145
146 wxFontDialog::wxFontDialog() :
147 m_dialogParent(NULL), m_pEventHandlerRef(NULL)
148 {
149 }
150
151 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
152 {
153 Create(parent, data);
154 }
155
156 wxFontDialog::~wxFontDialog()
157 {
158 if (m_pEventHandlerRef)
159 ::RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
160 }
161
162 void wxFontDialog::SetData(wxFontData& fontdata)
163 {
164 m_fontData = fontdata;
165 }
166
167 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
168 {
169 m_dialogParent = parent;
170 m_fontData = data;
171
172 //Register the events that will return this dialog
173 EventTypeSpec ftEventList[] = { { kEventClassFont, kEventFontSelection } };
174
175 OSStatus err = noErr;
176
177 //FIXMEFIXME: Why doesn't it recieve events if there's a parent?
178 // if (parent)
179 // {
180 // err = InstallWindowEventHandler(
181 // MAC_WXHWND(parent->GetHandle()),
182 // GetwxFontDialogEventHandlerUPP(),
183 // GetEventTypeCount(ftEventList), ftEventList,
184 // this, (&(EventHandlerRef&)m_pEventHandlerRef));
185 //
186 // }
187 // else //no parent - send to app
188 // {
189 err = InstallApplicationEventHandler(
190 GetwxFontDialogEventHandlerUPP(),
191 GetEventTypeCount(ftEventList), ftEventList,
192 this, (&(EventHandlerRef&)m_pEventHandlerRef));
193 // }
194
195 return err == noErr;
196 }
197
198 bool wxFontDialog::IsShown() const
199 {
200 return FPIsFontPanelVisible();
201 }
202
203 int wxFontDialog::ShowModal()
204 {
205 wxASSERT(!FPIsFontPanelVisible());
206
207 //set up initial font
208 wxFont theInitialFont = m_fontData.GetInitialFont();
209
210 //create ATSU style
211 ATSUStyle theStyle;
212 OSStatus status = ATSUCreateStyle(&theStyle);
213 check_noerr(status);
214
215 //put stuff into the style - we don't need kATSUColorTag
216 ATSUFontID fontid = theInitialFont.MacGetATSUFontID();
217 Fixed fontsize = theInitialFont.MacGetFontSize() << 16;
218 ATSUAttributeTag theTags[2] = { kATSUFontTag, kATSUSizeTag };
219 ByteCount theSizes[2] = { sizeof(ATSUFontID), sizeof(Fixed) };
220 ATSUAttributeValuePtr theValues[2] = { &fontid,
221 &fontsize };
222
223 //set the stuff for the ATSU style
224 verify_noerr (ATSUSetAttributes (theStyle, 2, theTags, theSizes, theValues) );
225
226 //they set us up the bomb! Set the initial font of the dialog
227 SetFontInfoForSelection(kFontSelectionATSUIType,
228 1,
229 &theStyle,
230 (HIObjectRef)
231 (m_dialogParent ?
232 GetWindowEventTarget(MAC_WXHWND(m_dialogParent->GetHandle())) :
233 GetApplicationEventTarget())
234 );
235
236 //dispose of the style
237 status = ATSUDisposeStyle(theStyle);
238 check_noerr(status);
239
240 //finally, show the font dialog
241 if( (status = FPShowHideFontPanel()) == noErr)
242 {
243 while(FPIsFontPanelVisible())
244 {
245 //yeild so we can get events
246 ::wxSafeYield(m_dialogParent, false);
247 }
248 }
249 else
250 return wxID_CANCEL;
251
252 return wxID_OK;
253 }
254
255
256 #else
257 //10.2+ only
258
259
260 //
261 // no native implementation
262 //
263
264 wxFontDialog::wxFontDialog()
265 {
266 m_dialogParent = NULL;
267 }
268
269 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
270 {
271 Create(parent, data);
272 }
273
274 void wxFontDialog::SetData(wxFontData& fontdata)
275 {
276 m_fontData = fontdata;
277 }
278
279 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
280 {
281 m_dialogParent = parent;
282
283 m_fontData = data;
284
285 // TODO: you may need to do dialog creation here, unless it's
286 // done in ShowModal.
287 return TRUE;
288 }
289
290 bool wxFontDialog::IsShown() const
291 {
292 return false;
293 }
294
295 int wxFontDialog::ShowModal()
296 {
297 // TODO: show (maybe create) the dialog
298 return wxID_CANCEL;
299 }
300
301 #endif // 10.2+