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