]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "fontdlg.h" | |
14 | #endif | |
15 | ||
519cb848 | 16 | #include "wx/mac/fontdlg.h" |
e9576ca5 SC |
17 | #include "wx/cmndata.h" |
18 | ||
2f1ae414 | 19 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 20 | IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog) |
2f1ae414 | 21 | #endif |
e9576ca5 | 22 | |
72fcdc75 RN |
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 | ||
d22c8abe | 106 | Ptr szBuffer = NewPtr(theActualLength); |
72fcdc75 RN |
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 | |
d22c8abe RN |
118 | theFont.SetFaceName(wxConvLocal.cMB2WX((char*)szBuffer)); |
119 | DisposePtr(szBuffer); | |
72fcdc75 RN |
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(0 + | |
126 | ((fontstyle & bold) ? wxFONTFLAG_BOLD : 0) + | |
127 | ((fontstyle & italic) ? wxFONTFLAG_ITALIC : 0) + | |
128 | ((fontstyle & underline) ? wxFONTFLAG_UNDERLINED : 0) | |
129 | ); | |
130 | theFont.SetWeight((fontstyle & bold) ? wxBOLD : wxNORMAL); | |
131 | ||
132 | //for debugging | |
133 | //wxPrintf(wxT("FaceName:%s\nSize:%i\n"), theFont.GetFaceName().c_str(), theFont.GetPointSize()); | |
134 | ||
135 | //phew!! We're done - set the chosen font | |
136 | theFontData.SetChosenFont(theFont); | |
137 | ((wxFontDialog*)pData)->SetData(theFontData); | |
138 | ||
139 | return status; | |
140 | } | |
141 | ||
142 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler ); | |
143 | ||
144 | //--------------------------- | |
145 | // Class implementation | |
146 | //--------------------------- | |
147 | ||
148 | wxFontDialog::wxFontDialog() : | |
149 | m_dialogParent(NULL), m_pEventHandlerRef(NULL) | |
150 | { | |
151 | } | |
152 | ||
153 | wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data) | |
154 | { | |
155 | Create(parent, data); | |
156 | } | |
157 | ||
158 | wxFontDialog::~wxFontDialog() | |
159 | { | |
160 | if (m_pEventHandlerRef) | |
161 | ::RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef); | |
162 | } | |
163 | ||
164 | void wxFontDialog::SetData(wxFontData& fontdata) | |
165 | { | |
166 | m_fontData = fontdata; | |
167 | } | |
168 | ||
169 | bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data) | |
170 | { | |
171 | m_dialogParent = parent; | |
172 | m_fontData = data; | |
173 | ||
174 | //Register the events that will return this dialog | |
175 | EventTypeSpec ftEventList[] = { { kEventClassFont, kEventFontSelection } }; | |
176 | ||
177 | OSStatus err = noErr; | |
178 | ||
179 | //FIXMEFIXME: Why doesn't it recieve events if there's a parent? | |
180 | // if (parent) | |
181 | // { | |
182 | // err = InstallWindowEventHandler( | |
183 | // MAC_WXHWND(parent->GetHandle()), | |
184 | // GetwxFontDialogEventHandlerUPP(), | |
185 | // GetEventTypeCount(ftEventList), ftEventList, | |
186 | // this, (&(EventHandlerRef&)m_pEventHandlerRef)); | |
187 | // | |
188 | // } | |
189 | // else //no parent - send to app | |
190 | // { | |
191 | err = InstallApplicationEventHandler( | |
192 | GetwxFontDialogEventHandlerUPP(), | |
193 | GetEventTypeCount(ftEventList), ftEventList, | |
194 | this, (&(EventHandlerRef&)m_pEventHandlerRef)); | |
195 | // } | |
196 | ||
197 | return err == noErr; | |
198 | } | |
199 | ||
200 | bool wxFontDialog::IsShown() const | |
201 | { | |
202 | return FPIsFontPanelVisible(); | |
203 | } | |
204 | ||
205 | int wxFontDialog::ShowModal() | |
206 | { | |
207 | wxASSERT(!FPIsFontPanelVisible()); | |
208 | ||
209 | //set up initial font | |
210 | wxFont theInitialFont = m_fontData.GetInitialFont(); | |
211 | ||
212 | //create ATSU style | |
213 | ATSUStyle theStyle; | |
214 | OSStatus status = ATSUCreateStyle(&theStyle); | |
215 | check_noerr(status); | |
216 | ||
217 | //put stuff into the style - we don't need kATSUColorTag | |
218 | ATSUFontID fontid = theInitialFont.MacGetATSUFontID(); | |
219 | Fixed fontsize = theInitialFont.MacGetFontSize() << 16; | |
220 | ATSUAttributeTag theTags[2] = { kATSUFontTag, kATSUSizeTag }; | |
221 | ByteCount theSizes[2] = { sizeof(ATSUFontID), sizeof(Fixed) }; | |
222 | ATSUAttributeValuePtr theValues[2] = { &fontid, | |
223 | &fontsize }; | |
224 | ||
225 | //set the stuff for the ATSU style | |
226 | verify_noerr (ATSUSetAttributes (theStyle, 2, theTags, theSizes, theValues) ); | |
227 | ||
228 | //they set us up the bomb! Set the initial font of the dialog | |
229 | SetFontInfoForSelection(kFontSelectionATSUIType, | |
230 | 1, | |
231 | &theStyle, | |
232 | (HIObjectRef) | |
233 | (m_dialogParent ? | |
234 | GetWindowEventTarget(MAC_WXHWND(m_dialogParent->GetHandle())) : | |
235 | GetApplicationEventTarget()) | |
236 | ); | |
237 | ||
238 | //dispose of the style | |
239 | status = ATSUDisposeStyle(theStyle); | |
240 | check_noerr(status); | |
241 | ||
242 | //finally, show the font dialog | |
243 | if( (status = FPShowHideFontPanel()) == noErr) | |
244 | { | |
245 | while(FPIsFontPanelVisible()) | |
246 | { | |
247 | //yeild so we can get events | |
248 | ::wxSafeYield(m_dialogParent, false); | |
249 | } | |
250 | } | |
251 | else | |
252 | return wxID_CANCEL; | |
253 | ||
254 | return wxID_OK; | |
255 | } | |
256 | ||
257 | ||
258 | #else | |
259 | //10.2+ only | |
260 | ||
261 | ||
262 | // | |
263 | // no native implementation | |
264 | // | |
e9576ca5 SC |
265 | |
266 | wxFontDialog::wxFontDialog() | |
267 | { | |
268 | m_dialogParent = NULL; | |
269 | } | |
270 | ||
baaae89f | 271 | wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data) |
e9576ca5 SC |
272 | { |
273 | Create(parent, data); | |
274 | } | |
275 | ||
72fcdc75 RN |
276 | void wxFontDialog::SetData(wxFontData& fontdata) |
277 | { | |
278 | m_fontData = fontdata; | |
279 | } | |
280 | ||
baaae89f | 281 | bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data) |
e9576ca5 SC |
282 | { |
283 | m_dialogParent = parent; | |
284 | ||
baaae89f | 285 | m_fontData = data; |
e9576ca5 SC |
286 | |
287 | // TODO: you may need to do dialog creation here, unless it's | |
288 | // done in ShowModal. | |
289 | return TRUE; | |
290 | } | |
291 | ||
72fcdc75 RN |
292 | bool wxFontDialog::IsShown() const |
293 | { | |
294 | return false; | |
295 | } | |
296 | ||
e9576ca5 SC |
297 | int wxFontDialog::ShowModal() |
298 | { | |
299 | // TODO: show (maybe create) the dialog | |
300 | return wxID_CANCEL; | |
301 | } | |
302 | ||
72fcdc75 | 303 | #endif // 10.2+ |