]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/fontdlg.cpp
initial implementation for MSW
[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 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "fontdlg.h"
22 #endif
23
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
32 #include "wx/cmndata.h"
33 #endif
34
35 #include "wx/mac/fontdlg.h"
36
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
39 #endif
40
41 #include "wx/mac/private.h"
42
43 //Mac OSX 10.2+ only
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
50 //color isn't working in carbon impl
51 #define ISCOLORWORKING 0
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 // ---------------------------------------------------------------------------
58 // Carbon event callback(s)
59 // ---------------------------------------------------------------------------
60
61 pascal 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
69 FMFontFamily fontfamily;
70 FMFontStyle fontstyle;
71 FMFontSize fontsize;
72 #if ISCOLORWORKING
73 RGBColor fontcolor;
74 #endif
75
76 status = GetEventParameter (event, kEventParamFMFontFamily,
77 typeFMFontFamily, NULL,
78 sizeof (FMFontFamily),
79 NULL, &(fontfamily));
80 check_noerr (status);
81
82 status = GetEventParameter (event, kEventParamFMFontStyle,
83 typeFMFontStyle, NULL,
84 sizeof (FMFontStyle),
85 NULL, &(fontstyle));
86 check_noerr (status);
87
88 status = GetEventParameter (event, kEventParamFMFontSize,
89 typeFMFontSize, NULL,
90 sizeof (FMFontSize),
91 NULL, &(fontsize));
92
93 check_noerr (status);
94
95 #if ISCOLORWORKING
96 status = GetEventParameter (event, kEventParamFontColor,
97 typeRGBColor, NULL,
98 sizeof( RGBColor ), NULL, &fontcolor);
99 check_noerr (status);
100 #endif
101
102 //now do the conversion to the wx font data
103 wxFontData theFontData;
104 wxFont theFont;
105
106 #if ISCOLORWORKING
107 //set color
108 wxColour theColor;
109 theColor.Set(&(WXCOLORREF&)fontcolor);
110 theFontData.SetColour(theColor);
111 #endif
112
113 //set size
114 theFont.SetPointSize(fontsize);
115
116 //set name
117 Str255 theFontName;
118 GetFontName(fontfamily, theFontName);
119 theFont.SetFaceName(wxMacMakeStringFromPascal(theFontName));
120
121 //TODOTODO: Get font family - mayby by the script code?
122 theFont.SetFamily(wxDEFAULT);
123
124 //TODOTODO: Get other styles?
125 theFont.SetStyle(((fontstyle & italic) ? wxFONTSTYLE_ITALIC : 0));
126 theFont.SetWeight((fontstyle & bold) ? wxBOLD : wxNORMAL);
127 theFont.SetUnderlined(((fontstyle & underline) ? true : false));
128
129 //phew!! We're done - set the chosen font
130 theFontData.SetChosenFont(theFont);
131 ((wxFontDialog*)pData)->SetData(theFontData);
132
133 return status;
134 }
135
136 DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler );
137
138 // ---------------------------------------------------------------------------
139 // wxFontDialog
140 // ---------------------------------------------------------------------------
141
142 wxFontDialog::wxFontDialog() :
143 m_dialogParent(NULL), m_pEventHandlerRef(NULL)
144 {
145 }
146
147 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
148 {
149 Create(parent, data);
150 }
151
152 wxFontDialog::~wxFontDialog()
153 {
154 if (m_pEventHandlerRef)
155 ::RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
156 }
157
158 void wxFontDialog::SetData(wxFontData& fontdata)
159 {
160 m_fontData = fontdata;
161 }
162
163 bool 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
194 bool wxFontDialog::IsShown() const
195 {
196 return FPIsFontPanelVisible();
197 }
198
199 int 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
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
241 //finally, show the font dialog
242 if( (status = FPShowHideFontPanel()) == noErr)
243 {
244 while(FPIsFontPanelVisible())
245 {
246 //yeild so we can get events
247 wxTheApp->Yield(false);
248 }
249 }
250 else
251 return wxID_CANCEL;
252
253 return wxID_OK;
254 }
255
256
257 #else
258 //10.2+ only
259
260 // ---------------------------------------------------------------------------
261 // wxFontDialog stub for mac OS's without a native font dialog
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 wxFontDialog::~wxFontDialog()
275 {
276 // empty
277 }
278
279 void wxFontDialog::SetData(wxFontData& fontdata)
280 {
281 m_fontData = fontdata;
282 }
283
284 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
285 {
286 m_dialogParent = parent;
287
288 m_fontData = data;
289
290 // TODO: you may need to do dialog creation here, unless it's
291 // done in ShowModal.
292 return TRUE;
293 }
294
295 bool wxFontDialog::IsShown() const
296 {
297 return false;
298 }
299
300 int wxFontDialog::ShowModal()
301 {
302 // TODO: show (maybe create) the dialog
303 return wxID_CANCEL;
304 }
305
306 #endif // 10.2+