1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontDialog class for carbon 10.2+.
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "fontdlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/mac/fontdlg.h"
33 #include "wx/cmndata.h"
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog
, wxDialog
)
40 #include "wx/mac/private.h"
43 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
45 #include <ATSUnicode.h>
47 #include "wx/msgdlg.h"
49 // ============================================================================
51 // ============================================================================
53 // ---------------------------------------------------------------------------
54 // Carbon event callback(s)
55 // ---------------------------------------------------------------------------
57 pascal OSStatus
wxFontDialogEventHandler( EventHandlerCallRef inHandlerCallRef
,
58 EventRef event
, void* pData
)
60 wxASSERT(GetEventClass(event
) == kEventClassFont
&&
61 GetEventKind(event
) == kEventFontSelection
);
63 OSStatus status
= noErr
;
65 FMFontFamily fontfamily
;
66 FMFontStyle fontstyle
;
70 status
= GetEventParameter (event
, kEventParamFMFontFamily
,
71 typeFMFontFamily
, NULL
,
72 sizeof (FMFontFamily
),
76 status
= GetEventParameter (event
, kEventParamFMFontStyle
,
77 typeFMFontStyle
, NULL
,
82 status
= GetEventParameter (event
, kEventParamFMFontSize
,
89 status
= GetEventParameter (event
, kEventParamFontColor
,
91 sizeof( RGBColor
), NULL
, &fontcolor
);
94 //now do the conversion to the wx font data
95 wxFontData theFontData
;
100 theColor
.Set(&(WXCOLORREF
&)fontcolor
);
101 theFontData
.SetColour(theColor
);
104 theFont
.SetPointSize(fontsize
);
108 GetFontName(fontfamily
, theFontName
);
109 theFont
.SetFaceName(wxMacMakeStringFromPascal(theFontName
));
111 //TODOTODO: Get font family - mayby by the script code?
112 theFont
.SetFamily(wxDEFAULT
);
114 //TODOTODO: Get other styles?
115 theFont
.SetStyle(((fontstyle
& italic
) ? wxFONTSTYLE_ITALIC
: 0));
116 theFont
.SetWeight((fontstyle
& bold
) ? wxBOLD
: wxNORMAL
);
117 theFont
.SetUnderlined(((fontstyle
& underline
) ? true : false));
119 //phew!! We're done - set the chosen font
120 theFontData
.SetChosenFont(theFont
);
121 ((wxFontDialog
*)pData
)->SetData(theFontData
);
126 DEFINE_ONE_SHOT_HANDLER_GETTER( wxFontDialogEventHandler
);
128 // ---------------------------------------------------------------------------
130 // ---------------------------------------------------------------------------
132 wxFontDialog::wxFontDialog() :
133 m_dialogParent(NULL
), m_pEventHandlerRef(NULL
)
137 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
139 Create(parent
, data
);
142 wxFontDialog::~wxFontDialog()
144 if (m_pEventHandlerRef
)
145 ::RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
148 void wxFontDialog::SetData(wxFontData
& fontdata
)
150 m_fontData
= fontdata
;
153 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
155 m_dialogParent
= parent
;
158 //Register the events that will return this dialog
159 EventTypeSpec ftEventList
[] = { { kEventClassFont
, kEventFontSelection
} };
161 OSStatus err
= noErr
;
163 //FIXMEFIXME: Why doesn't it recieve events if there's a parent?
166 // err = InstallWindowEventHandler(
167 // MAC_WXHWND(parent->GetHandle()),
168 // GetwxFontDialogEventHandlerUPP(),
169 // GetEventTypeCount(ftEventList), ftEventList,
170 // this, (&(EventHandlerRef&)m_pEventHandlerRef));
173 // else //no parent - send to app
175 err
= InstallApplicationEventHandler(
176 GetwxFontDialogEventHandlerUPP(),
177 GetEventTypeCount(ftEventList
), ftEventList
,
178 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
184 bool wxFontDialog::IsShown() const
186 return FPIsFontPanelVisible();
189 int wxFontDialog::ShowModal()
191 wxASSERT(!FPIsFontPanelVisible());
193 //set up initial font
194 wxFont theInitialFont
= m_fontData
.GetInitialFont();
198 OSStatus status
= ATSUCreateStyle(&theStyle
);
201 //put stuff into the style - we don't need kATSUColorTag
202 ATSUFontID fontid
= theInitialFont
.MacGetATSUFontID();
203 Fixed fontsize
= theInitialFont
.MacGetFontSize() << 16;
204 ATSUAttributeTag theTags
[2] = { kATSUFontTag
, kATSUSizeTag
};
205 ByteCount theSizes
[2] = { sizeof(ATSUFontID
), sizeof(Fixed
) };
206 ATSUAttributeValuePtr theValues
[2] = { &fontid
,
209 //set the stuff for the ATSU style
210 verify_noerr (ATSUSetAttributes (theStyle
, 2, theTags
, theSizes
, theValues
) );
212 //they set us up the bomb! Set the initial font of the dialog
213 SetFontInfoForSelection(kFontSelectionATSUIType
,
218 GetWindowEventTarget(MAC_WXHWND(m_dialogParent
->GetHandle())) :
219 GetApplicationEventTarget())
222 //dispose of the style
223 status
= ATSUDisposeStyle(theStyle
);
226 //in case the user doesn't choose anything -
227 //if he doesn't we'll get a bad font with red text
228 m_fontData
.SetChosenFont(m_fontData
.GetInitialFont());
229 m_fontData
.SetColour(wxColour(0,0,0));
231 //finally, show the font dialog
232 if( (status
= FPShowHideFontPanel()) == noErr
)
234 while(FPIsFontPanelVisible())
236 //yeild so we can get events
237 ::wxSafeYield(m_dialogParent
, false);
250 // ---------------------------------------------------------------------------
251 // wxFontDialog stub for mac OS's without a native font dialog
252 // ---------------------------------------------------------------------------
254 wxFontDialog::wxFontDialog()
256 m_dialogParent
= NULL
;
259 wxFontDialog::wxFontDialog(wxWindow
*parent
, const wxFontData
& data
)
261 Create(parent
, data
);
264 void wxFontDialog::SetData(wxFontData
& fontdata
)
266 m_fontData
= fontdata
;
269 bool wxFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
271 m_dialogParent
= parent
;
275 // TODO: you may need to do dialog creation here, unless it's
276 // done in ShowModal.
280 bool wxFontDialog::IsShown() const
285 int wxFontDialog::ShowModal()
287 // TODO: show (maybe create) the dialog