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?
126 ((fontstyle
& bold
) ? wxFONTFLAG_BOLD
: 0) +
127 ((fontstyle
& italic
) ? wxFONTFLAG_ITALIC
: 0) +
128 ((fontstyle
& underline
) ? wxFONTFLAG_UNDERLINED
: 0)
130 theFont
.SetWeight((fontstyle
& bold
) ? wxBOLD
: wxNORMAL
);
133 //wxPrintf(wxT("FaceName:%s\nSize:%i\n"), theFont.GetFaceName().c_str(), theFont.GetPointSize());
135 //phew!! We're done - set the chosen font
136 theFontData
.SetChosenFont(theFont
);
137 ((wxFontDialog
*)pData
)->SetData(theFontData
);
142 DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler
);
144 //---------------------------
145 // Class implementation
146 //---------------------------
148 wxFontDialog::wxFontDialog() :
149 m_dialogParent(NULL
), m_pEventHandlerRef(NULL
)
153 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
155 Create(parent
, data
);
158 wxFontDialog::~wxFontDialog()
160 if (m_pEventHandlerRef
)
161 ::RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
164 void wxFontDialog::SetData(wxFontData
& fontdata
)
166 m_fontData
= fontdata
;
169 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
171 m_dialogParent
= parent
;
174 //Register the events that will return this dialog
175 EventTypeSpec ftEventList
[] = { { kEventClassFont
, kEventFontSelection
} };
177 OSStatus err
= noErr
;
179 //FIXMEFIXME: Why doesn't it recieve events if there's a parent?
182 // err = InstallWindowEventHandler(
183 // MAC_WXHWND(parent->GetHandle()),
184 // GetwxFontDialogEventHandlerUPP(),
185 // GetEventTypeCount(ftEventList), ftEventList,
186 // this, (&(EventHandlerRef&)m_pEventHandlerRef));
189 // else //no parent - send to app
191 err
= InstallApplicationEventHandler(
192 GetwxFontDialogEventHandlerUPP(),
193 GetEventTypeCount(ftEventList
), ftEventList
,
194 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
200 bool wxFontDialog::IsShown() const
202 return FPIsFontPanelVisible();
205 int wxFontDialog::ShowModal()
207 wxASSERT(!FPIsFontPanelVisible());
209 //set up initial font
210 wxFont theInitialFont
= m_fontData
.GetInitialFont();
214 OSStatus status
= ATSUCreateStyle(&theStyle
);
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
,
225 //set the stuff for the ATSU style
226 verify_noerr (ATSUSetAttributes (theStyle
, 2, theTags
, theSizes
, theValues
) );
228 //they set us up the bomb! Set the initial font of the dialog
229 SetFontInfoForSelection(kFontSelectionATSUIType
,
234 GetWindowEventTarget(MAC_WXHWND(m_dialogParent
->GetHandle())) :
235 GetApplicationEventTarget())
238 //dispose of the style
239 status
= ATSUDisposeStyle(theStyle
);
242 //finally, show the font dialog
243 if( (status
= FPShowHideFontPanel()) == noErr
)
245 while(FPIsFontPanelVisible())
247 //yeild so we can get events
248 ::wxSafeYield(m_dialogParent
, false);
263 // no native implementation
266 wxFontDialog::wxFontDialog()
268 m_dialogParent
= NULL
;
271 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
273 Create(parent
, data
);
276 void wxFontDialog::SetData(wxFontData
& fontdata
)
278 m_fontData
= fontdata
;
281 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
283 m_dialogParent
= parent
;
287 // TODO: you may need to do dialog creation here, unless it's
288 // done in ShowModal.
292 bool wxFontDialog::IsShown() const
297 int wxFontDialog::ShowModal()
299 // TODO: show (maybe create) the dialog