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
;
43 FMFontFamily fontfamily
;
45 status
= GetEventParameter (event
, kEventParamFMFontFamily
,
46 typeFMFontFamily
, NULL
,
47 sizeof (FMFontFamily
),
51 status
= GetEventParameter (event
, kEventParamFMFontStyle
,
52 typeFMFontStyle
, NULL
,
57 status
= GetEventParameter (event
, kEventParamATSUFontID
,
63 status
= GetEventParameter (event
, kEventParamATSUFontSize
,
70 status
= GetEventParameter (event
, kEventParamFontColor
,
72 sizeof( RGBColor
), NULL
, &fontcolor
);
76 //TODO: Make a utility function for converting ATSU to WX font (what we're doing :))
79 //now do the conversion to the wx font data
80 wxFontData theFontData
;
83 //first, the easy part - set the color
85 theColor
.Set(&(WXCOLORREF
&)fontcolor
);
86 theFontData
.SetColour(theColor
);
89 //now the hard part - convert the atsu font to a wx font
92 //point size - fixed, but straight conversion, I think
93 theFont
.SetPointSize(fontsize
>> 16);
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
102 ByteCount theActualLength
;
103 ItemCount numFontFaces
;
106 ATSUCountFontNames(fontid,
109 ATSUGetIndFontName(fontid,
110 numFontFaces-1, //first font in index array
111 0, //need to get length first
119 Ptr szBuffer = NewPtr(theActualLength);
120 ATSUGetIndFontName(fontid,
121 numFontFaces-1, //first font in index array
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);
136 GetFontName(fontfamily
, theFontName
);
137 theFont
.SetFaceName(wxMacMakeStringFromPascal(theFontName
));
139 //TODOTODO: Get font family - mayby by the script code?
140 theFont
.SetFamily(wxDEFAULT
);
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));
148 //wxPrintf(wxT("FaceName:%s\nSize:%i\nStyle:%i\n"), theFont.GetFaceName().c_str(), theFont.GetPointSize(),
149 //theFont.GetStyle());
151 //phew!! We're done - set the chosen font
152 theFontData
.SetChosenFont(theFont
);
153 ((wxFontDialog
*)pData
)->SetData(theFontData
);
158 DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler
);
160 //---------------------------
161 // Class implementation
162 //---------------------------
164 wxFontDialog::wxFontDialog() :
165 m_dialogParent(NULL
), m_pEventHandlerRef(NULL
)
169 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
171 Create(parent
, data
);
174 wxFontDialog::~wxFontDialog()
176 if (m_pEventHandlerRef
)
177 ::RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
180 void wxFontDialog::SetData(wxFontData
& fontdata
)
182 m_fontData
= fontdata
;
185 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
187 m_dialogParent
= parent
;
190 //Register the events that will return this dialog
191 EventTypeSpec ftEventList
[] = { { kEventClassFont
, kEventFontSelection
} };
193 OSStatus err
= noErr
;
195 //FIXMEFIXME: Why doesn't it recieve events if there's a parent?
198 // err = InstallWindowEventHandler(
199 // MAC_WXHWND(parent->GetHandle()),
200 // GetwxFontDialogEventHandlerUPP(),
201 // GetEventTypeCount(ftEventList), ftEventList,
202 // this, (&(EventHandlerRef&)m_pEventHandlerRef));
205 // else //no parent - send to app
207 err
= InstallApplicationEventHandler(
208 GetwxFontDialogEventHandlerUPP(),
209 GetEventTypeCount(ftEventList
), ftEventList
,
210 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
216 bool wxFontDialog::IsShown() const
218 return FPIsFontPanelVisible();
221 int wxFontDialog::ShowModal()
223 wxASSERT(!FPIsFontPanelVisible());
225 //set up initial font
226 wxFont theInitialFont
= m_fontData
.GetInitialFont();
230 OSStatus status
= ATSUCreateStyle(&theStyle
);
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
,
241 //set the stuff for the ATSU style
242 verify_noerr (ATSUSetAttributes (theStyle
, 2, theTags
, theSizes
, theValues
) );
244 //they set us up the bomb! Set the initial font of the dialog
245 SetFontInfoForSelection(kFontSelectionATSUIType
,
250 GetWindowEventTarget(MAC_WXHWND(m_dialogParent
->GetHandle())) :
251 GetApplicationEventTarget())
254 //dispose of the style
255 status
= ATSUDisposeStyle(theStyle
);
258 //finally, show the font dialog
259 if( (status
= FPShowHideFontPanel()) == noErr
)
261 while(FPIsFontPanelVisible())
263 //yeild so we can get events
264 ::wxSafeYield(m_dialogParent
, false);
279 // no native implementation
282 wxFontDialog::wxFontDialog()
284 m_dialogParent
= NULL
;
287 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
289 Create(parent
, data
);
292 void wxFontDialog::SetData(wxFontData
& fontdata
)
294 m_fontData
= fontdata
;
297 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
299 m_dialogParent
= parent
;
303 // TODO: you may need to do dialog creation here, unless it's
304 // done in ShowModal.
308 bool wxFontDialog::IsShown() const
313 int wxFontDialog::ShowModal()
315 // TODO: show (maybe create) the dialog