1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontDialog class for carbon 10.2+.
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "fontdlg.h"
16 #include "wx/mac/fontdlg.h"
17 #include "wx/cmndata.h"
19 #if !USE_SHARED_LIBRARY
20 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog
, wxDialog
)
23 #include "wx/mac/private.h"
25 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
27 #include <ATSUnicode.h>
29 #include "wx/msgdlg.h"
31 pascal OSStatus
wxFontDialogEventHandler( EventHandlerCallRef inHandlerCallRef
,
32 EventRef event
, void* pData
)
34 wxASSERT(GetEventClass(event
) == kEventClassFont
&&
35 GetEventKind(event
) == kEventFontSelection
);
37 OSStatus status
= noErr
;
42 FMFontStyle fontstyle
;
44 status
= GetEventParameter (event
, kEventParamFMFontStyle
,
45 typeFMFontStyle
, NULL
,
50 status
= GetEventParameter (event
, kEventParamATSUFontID
,
56 status
= GetEventParameter (event
, kEventParamATSUFontSize
,
63 status
= GetEventParameter (event
, kEventParamFontColor
,
65 sizeof( RGBColor
), NULL
, &fontcolor
);
69 //TODO: Make a utility function for converting ATSU to WX font (what we're doing :))
72 //now do the conversion to the wx font data
73 wxFontData theFontData
;
76 //first, the easy part - set the color
78 theColor
.Set(&(WXCOLORREF
&)fontcolor
);
79 theFontData
.SetColour(theColor
);
82 //now the hard part - convert the atsu font to a wx font
85 //point size - fixed, but straight conversion, I think
86 theFont
.SetPointSize(fontsize
>> 16);
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
95 ByteCount theActualLength
;
96 ATSUGetIndFontName(fontid
,
97 0, //first font in index array
98 0, //need to get length first
106 Ptr szBuffer
= NewPtr(theActualLength
);
107 ATSUGetIndFontName(fontid
,
108 0, //first font in index array
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
);
121 //TODOTODO: Get font family - mayby by the script code?
122 theFont
.SetFamily(wxDEFAULT
);
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));
130 //wxPrintf(wxT("FaceName:%s\nSize:%i\nStyle:%i\n"), theFont.GetFaceName().c_str(), theFont.GetPointSize(),
131 //theFont.GetStyle());
133 //phew!! We're done - set the chosen font
134 theFontData
.SetChosenFont(theFont
);
135 ((wxFontDialog
*)pData
)->SetData(theFontData
);
140 DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler
);
142 //---------------------------
143 // Class implementation
144 //---------------------------
146 wxFontDialog::wxFontDialog() :
147 m_dialogParent(NULL
), m_pEventHandlerRef(NULL
)
151 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
153 Create(parent
, data
);
156 wxFontDialog::~wxFontDialog()
158 if (m_pEventHandlerRef
)
159 ::RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
162 void wxFontDialog::SetData(wxFontData
& fontdata
)
164 m_fontData
= fontdata
;
167 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
169 m_dialogParent
= parent
;
172 //Register the events that will return this dialog
173 EventTypeSpec ftEventList
[] = { { kEventClassFont
, kEventFontSelection
} };
175 OSStatus err
= noErr
;
177 //FIXMEFIXME: Why doesn't it recieve events if there's a parent?
180 // err = InstallWindowEventHandler(
181 // MAC_WXHWND(parent->GetHandle()),
182 // GetwxFontDialogEventHandlerUPP(),
183 // GetEventTypeCount(ftEventList), ftEventList,
184 // this, (&(EventHandlerRef&)m_pEventHandlerRef));
187 // else //no parent - send to app
189 err
= InstallApplicationEventHandler(
190 GetwxFontDialogEventHandlerUPP(),
191 GetEventTypeCount(ftEventList
), ftEventList
,
192 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
198 bool wxFontDialog::IsShown() const
200 return FPIsFontPanelVisible();
203 int wxFontDialog::ShowModal()
205 wxASSERT(!FPIsFontPanelVisible());
207 //set up initial font
208 wxFont theInitialFont
= m_fontData
.GetInitialFont();
212 OSStatus status
= ATSUCreateStyle(&theStyle
);
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
,
223 //set the stuff for the ATSU style
224 verify_noerr (ATSUSetAttributes (theStyle
, 2, theTags
, theSizes
, theValues
) );
226 //they set us up the bomb! Set the initial font of the dialog
227 SetFontInfoForSelection(kFontSelectionATSUIType
,
232 GetWindowEventTarget(MAC_WXHWND(m_dialogParent
->GetHandle())) :
233 GetApplicationEventTarget())
236 //dispose of the style
237 status
= ATSUDisposeStyle(theStyle
);
240 //finally, show the font dialog
241 if( (status
= FPShowHideFontPanel()) == noErr
)
243 while(FPIsFontPanelVisible())
245 //yeild so we can get events
246 ::wxSafeYield(m_dialogParent
, false);
261 // no native implementation
264 wxFontDialog::wxFontDialog()
266 m_dialogParent
= NULL
;
269 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
271 Create(parent
, data
);
274 void wxFontDialog::SetData(wxFontData
& fontdata
)
276 m_fontData
= fontdata
;
279 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
281 m_dialogParent
= parent
;
285 // TODO: you may need to do dialog creation here, unless it's
286 // done in ShowModal.
290 bool wxFontDialog::IsShown() const
295 int wxFontDialog::ShowModal()
297 // TODO: show (maybe create) the dialog