]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/fontdlg.cpp
made GetTempBuffer() static to avoid duplicate function errors during linking
[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 FMFontFamily fontfamily;
40 FMFontStyle fontstyle;
41 FMFontSize fontsize;
42 RGBColor fontcolor;
43
44 status = GetEventParameter (event, kEventParamFMFontFamily,
45 typeFMFontFamily, NULL,
46 sizeof (FMFontFamily),
47 NULL, &(fontfamily));
48 check_noerr (status);
49
50 status = GetEventParameter (event, kEventParamFMFontStyle,
51 typeFMFontStyle, NULL,
52 sizeof (FMFontStyle),
53 NULL, &(fontstyle));
54 check_noerr (status);
55
56 status = GetEventParameter (event, kEventParamFMFontSize,
57 typeFMFontSize, NULL,
58 sizeof (FMFontSize),
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 //now do the conversion to the wx font data
69 wxFontData theFontData;
70 wxFont theFont;
71
72 //set color
73 wxColour theColor;
74 theColor.Set(&(WXCOLORREF&)fontcolor);
75 theFontData.SetColour(theColor);
76
77 //set size
78 theFont.SetPointSize(fontsize);
79
80 //set name
81 Str255 theFontName;
82 GetFontName(fontfamily, theFontName);
83 theFont.SetFaceName(wxMacMakeStringFromPascal(theFontName));
84
85 //TODOTODO: Get font family - mayby by the script code?
86 theFont.SetFamily(wxDEFAULT);
87
88 //TODOTODO: Get other styles?
89 theFont.SetStyle(((fontstyle & italic) ? wxFONTSTYLE_ITALIC : 0));
90 theFont.SetWeight((fontstyle & bold) ? wxBOLD : wxNORMAL);
91 theFont.SetUnderlined(((fontstyle & underline) ? true : false));
92
93 //phew!! We're done - set the chosen font
94 theFontData.SetChosenFont(theFont);
95 ((wxFontDialog*)pData)->SetData(theFontData);
96
97 return status;
98 }
99
100 DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler );
101
102 //---------------------------
103 // Class implementation
104 //---------------------------
105
106 wxFontDialog::wxFontDialog() :
107 m_dialogParent(NULL), m_pEventHandlerRef(NULL)
108 {
109 }
110
111 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
112 {
113 Create(parent, data);
114 }
115
116 wxFontDialog::~wxFontDialog()
117 {
118 if (m_pEventHandlerRef)
119 ::RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
120 }
121
122 void wxFontDialog::SetData(wxFontData& fontdata)
123 {
124 m_fontData = fontdata;
125 }
126
127 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
128 {
129 m_dialogParent = parent;
130 m_fontData = data;
131
132 //Register the events that will return this dialog
133 EventTypeSpec ftEventList[] = { { kEventClassFont, kEventFontSelection } };
134
135 OSStatus err = noErr;
136
137 //FIXMEFIXME: Why doesn't it recieve events if there's a parent?
138 // if (parent)
139 // {
140 // err = InstallWindowEventHandler(
141 // MAC_WXHWND(parent->GetHandle()),
142 // GetwxFontDialogEventHandlerUPP(),
143 // GetEventTypeCount(ftEventList), ftEventList,
144 // this, (&(EventHandlerRef&)m_pEventHandlerRef));
145 //
146 // }
147 // else //no parent - send to app
148 // {
149 err = InstallApplicationEventHandler(
150 GetwxFontDialogEventHandlerUPP(),
151 GetEventTypeCount(ftEventList), ftEventList,
152 this, (&(EventHandlerRef&)m_pEventHandlerRef));
153 // }
154
155 return err == noErr;
156 }
157
158 bool wxFontDialog::IsShown() const
159 {
160 return FPIsFontPanelVisible();
161 }
162
163 int wxFontDialog::ShowModal()
164 {
165 wxASSERT(!FPIsFontPanelVisible());
166
167 //set up initial font
168 wxFont theInitialFont = m_fontData.GetInitialFont();
169
170 //create ATSU style
171 ATSUStyle theStyle;
172 OSStatus status = ATSUCreateStyle(&theStyle);
173 check_noerr(status);
174
175 //put stuff into the style - we don't need kATSUColorTag
176 ATSUFontID fontid = theInitialFont.MacGetATSUFontID();
177 Fixed fontsize = theInitialFont.MacGetFontSize() << 16;
178 ATSUAttributeTag theTags[2] = { kATSUFontTag, kATSUSizeTag };
179 ByteCount theSizes[2] = { sizeof(ATSUFontID), sizeof(Fixed) };
180 ATSUAttributeValuePtr theValues[2] = { &fontid,
181 &fontsize };
182
183 //set the stuff for the ATSU style
184 verify_noerr (ATSUSetAttributes (theStyle, 2, theTags, theSizes, theValues) );
185
186 //they set us up the bomb! Set the initial font of the dialog
187 SetFontInfoForSelection(kFontSelectionATSUIType,
188 1,
189 &theStyle,
190 (HIObjectRef)
191 (m_dialogParent ?
192 GetWindowEventTarget(MAC_WXHWND(m_dialogParent->GetHandle())) :
193 GetApplicationEventTarget())
194 );
195
196 //dispose of the style
197 status = ATSUDisposeStyle(theStyle);
198 check_noerr(status);
199
200 //in case the user doesn't choose anything -
201 //if he doesn't we'll get a bad font with red text
202 m_fontData.SetChosenFont(m_fontData.GetInitialFont());
203 m_fontData.SetColour(wxColour(0,0,0));
204
205 //finally, show the font dialog
206 if( (status = FPShowHideFontPanel()) == noErr)
207 {
208 while(FPIsFontPanelVisible())
209 {
210 //yeild so we can get events
211 ::wxSafeYield(m_dialogParent, false);
212 }
213 }
214 else
215 return wxID_CANCEL;
216
217 return wxID_OK;
218 }
219
220
221 #else
222 //10.2+ only
223
224
225 //
226 // no native implementation
227 //
228
229 wxFontDialog::wxFontDialog()
230 {
231 m_dialogParent = NULL;
232 }
233
234 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
235 {
236 Create(parent, data);
237 }
238
239 void wxFontDialog::SetData(wxFontData& fontdata)
240 {
241 m_fontData = fontdata;
242 }
243
244 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
245 {
246 m_dialogParent = parent;
247
248 m_fontData = data;
249
250 // TODO: you may need to do dialog creation here, unless it's
251 // done in ShowModal.
252 return TRUE;
253 }
254
255 bool wxFontDialog::IsShown() const
256 {
257 return false;
258 }
259
260 int wxFontDialog::ShowModal()
261 {
262 // TODO: show (maybe create) the dialog
263 return wxID_CANCEL;
264 }
265
266 #endif // 10.2+