]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/carbon/fontdlg.cpp
Performance optimization
[wxWidgets.git] / src / mac / carbon / fontdlg.cpp
index ab69a605f9b756682adc4731501cd54a8cdca113..7c037fc8b42fa5689e23b15fb5ffbc91e9495f28 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        fontdlg.cpp
+// Name:        src/mac/carbon/fontdlg.cpp
 // Purpose:     wxFontDialog class for carbon 10.2+.
 // Author:      Ryan Norton
 // Modified by:
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
+#if wxUSE_FONTDLG
+
 #ifdef __BORLANDC__
     #pragma hdrstop
 #endif
 
 #ifndef WX_PRECOMP
-    #include "wx/cmndata.h"
+    #include "wx/intl.h"
+    #include "wx/crt.h"
+    #include "wx/dcclient.h"
+    #include "wx/frame.h"
+    #include "wx/textctrl.h"
+    #include "wx/listbox.h"
+    #include "wx/checkbox.h"
+    #include "wx/choice.h"
+    #include "wx/sizer.h"
+    #include "wx/stattext.h"
+    #include "wx/button.h"
 #endif
 
 #include "wx/fontdlg.h"
+
+#if wxMAC_USE_EXPERIMENTAL_FONTDIALOG
+
+IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
+
+#include "wx/mac/uma.h"
+
+// ---------------------------------------------------------------------------
+// wxFontDialog
+// ---------------------------------------------------------------------------
+
+static const EventTypeSpec eventList[] =
+{
+    { kEventClassFont, kEventFontSelection } ,
+} ;
+
+
+pascal OSStatus
+wxMacCarbonFontPanelHandler(EventHandlerCallRef WXUNUSED(nextHandler),
+                            EventRef event,
+                            void *userData)
+{
+    OSStatus result = eventNotHandledErr ;
+    wxFontDialog *fontdialog = (wxFontDialog*) userData ;
+    wxFontData& fontdata= fontdialog->GetFontData() ;
+    
+    wxMacCarbonEvent cEvent( event );
+    switch(cEvent.GetKind())
+    {
+        case kEventFontSelection :
+        {
+            bool setup = false ;
+#if wxMAC_USE_CORE_TEXT
+            if (  UMAGetSystemVersion() >= 0x1050 )
+            {
+                CTFontDescriptorRef descr;
+                if ( cEvent.GetParameter<CTFontDescriptorRef>( kEventParamCTFontDescriptor, typeCTFontDescriptorRef, &descr ) == noErr )
+                {
+                    wxFont font;
+                    font.MacCreateFromCTFontDescriptor(descr);
+                    fontdata.SetChosenFont( font ) ;
+                    setup = true;
+                }
+            }
+#endif
+#if wxMAC_USE_ATSU_TEXT
+            ATSUFontID fontId = 0 ;
+            if ( !setup && (cEvent.GetParameter<ATSUFontID>(kEventParamATSUFontID, &fontId) == noErr) )
+            {
+                FMFontStyle fontStyle = cEvent.GetParameter<FMFontStyle>(kEventParamFMFontStyle);
+                FMFontSize fontSize = cEvent.GetParameter<FMFontSize>(kEventParamFMFontSize);
+                
+                CFStringRef cfName = NULL;
+#if 1
+                FMFontFamily fontFamily = cEvent.GetParameter<FMFontFamily>(kEventParamFMFontFamily);
+                ATSFontFamilyRef atsfontfamilyref = FMGetATSFontFamilyRefFromFontFamily( fontFamily ) ;
+                OSStatus err = ATSFontFamilyGetName( atsfontfamilyref , kATSOptionFlagsDefault , &cfName ) ;
+                wxASSERT_MSG( err == noErr , wxT("ATSFontFamilyGetName failed") );
+#else
+                // we don't use the ATSU naming anymore
+                ByteCount actualLength = 0;
+                char *c = NULL;
+                OSStatus err = ATSUFindFontName(fontId , kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode,
+                                                kFontNoLanguageCode , 0 , NULL , &actualLength , NULL );
+                if ( err == noErr)
+                {
+                    actualLength += 1 ;
+                    char *c = (char*)malloc( actualLength );
+                    err = ATSUFindFontName(fontId, kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode,
+                                           kFontNoLanguageCode, actualLength, c , NULL, NULL);
+                    cfName = CFStringCreateWithCharacters(NULL, (UniChar*) c, (actualLength-1) >> 1);
+                }
+                else
+                {
+                    err = ATSUFindFontName(fontId , kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode,
+                                           kFontNoLanguageCode , 0 , NULL , &actualLength , NULL );
+                    if ( err == noErr )
+                    {
+                        actualLength += 1 ;
+                        c = (char*)malloc(actualLength);
+                        err = ATSUFindFontName(fontId, kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode,
+                                               kFontNoLanguageCode, actualLength, c , NULL, NULL);
+                        c[actualLength-1] = 0;
+                        cfName = CFStringCreateWithCString(NULL, c, kCFStringEncodingMacRoman );
+                    }
+                }
+                if ( c!=NULL )
+                    free(c);
+#endif
+                if ( cfName!=NULL )
+                {
+                    fontdata.m_chosenFont.SetFaceName(wxCFStringRef(cfName).AsString(wxLocale::GetSystemEncoding()));
+                    fontdata.m_chosenFont.SetPointSize(fontSize);
+                    fontdata.m_chosenFont.SetStyle(fontStyle & italic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL);
+                    fontdata.m_chosenFont.SetUnderlined((fontStyle & underline)!=0);
+                    fontdata.m_chosenFont.SetWeight(fontStyle & bold ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
+                }
+            }
+#endif // wxMAC_USE_ATSU_TEXT
+            
+            // retrieving the color
+            RGBColor fontColor ;
+            if ( cEvent.GetParameter<RGBColor>(kEventParamFontColor, &fontColor) == noErr )
+            {
+                fontdata.m_fontColour = fontColor;
+            }
+            else
+            {
+                CFDictionaryRef dict ;
+                if ( cEvent.GetParameter<CFDictionaryRef>(kEventParamDictionary, &dict) == noErr )
+                {
+                    CFDictionaryRef attributesDict ;
+                    if ( CFDictionaryGetValueIfPresent(dict, kFontPanelAttributesKey, (const void **)&attributesDict) )
+                    {
+                        CFDataRef tagsData;
+                        CFDataRef sizesData;
+                        CFDataRef valuesData;
+                        if ( CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeTagsKey, (const void **)&tagsData) &&
+                            CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeSizesKey, (const void **)&sizesData) &&
+                            CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeValuesKey, (const void **)&valuesData) )
+                        {
+                            ItemCount count = CFDataGetLength(tagsData)/sizeof(ATSUAttributeTag);
+                            ATSUAttributeTag *tagPtr = (ATSUAttributeTag *)CFDataGetBytePtr(tagsData);
+                            ByteCount *sizePtr = (ByteCount *)CFDataGetBytePtr(sizesData);
+                            UInt32 *bytePtr = (UInt32*)CFDataGetBytePtr(valuesData);
+                            ATSUAttributeValuePtr valuesPtr = bytePtr ;
+                            for ( ItemCount i = 0 ; i < count ; ++i)
+                            {
+                                if ( tagPtr[i] == kATSUColorTag && sizePtr[i] == sizeof(RGBColor))
+                                {
+                                    fontdata.m_fontColour = *(RGBColor *)valuesPtr;
+                                    break ;
+                                }
+                                bytePtr = (UInt32*)( (UInt8*)bytePtr + sizePtr[i]);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+            break ;
+    } ;
     
+    return result ;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacCarbonFontPanelHandler )
+
+wxFontDialog::wxFontDialog()
+{
+}
+
+wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData&  data)
+{
+    Create(parent, data);
+}
+
+wxFontDialog::~wxFontDialog()
+{
+}
+
+bool wxFontDialog::Create(wxWindow *WXUNUSED(parent), const wxFontData& data)
+{
+    m_fontData = data;
+    return true ;
+}
+
+int wxFontDialog::ShowModal()
+{
+    OSStatus err ;
+    wxFont font = *wxNORMAL_FONT ;
+    if ( m_fontData.m_initialFont.Ok() )
+    {
+        font = m_fontData.m_initialFont ;
+    }
+
+    bool setup = false;
+#if wxMAC_USE_CORE_TEXT
+    if ( UMAGetSystemVersion() >= 0x1050 )
+    {
+        CTFontDescriptorRef descr = (CTFontDescriptorRef)font.MacGetCTFontDescriptor();
+        err = SetFontInfoForSelection (kFontSelectionCoreTextType,1, &descr , NULL);
+        setup = true;
+    }
+#endif
+#if wxMAC_USE_ATSU_TEXT
+    if ( !setup )
+    {
+        ATSUStyle style = (ATSUStyle)font.MacGetATSUStyle();
+        err = SetFontInfoForSelection (kFontSelectionATSUIType,1, &style , NULL);
+        setup = true;
+    }
+#endif
+    // just clicking on ENTER will not send us any font setting event, therefore we have to make sure
+    // that field is already correct
+    m_fontData.m_chosenFont = font ;
+
+    EventHandlerRef handler ;
+
+    err = InstallApplicationEventHandler( GetwxMacCarbonFontPanelHandlerUPP(), GetEventTypeCount(eventList), eventList, this , &handler );
+
+    if ( !FPIsFontPanelVisible() )
+        FPShowHideFontPanel();
+
+    int retval = RunMixedFontDialog(this);
+
+    ::RemoveEventHandler(handler);
+
+    return retval ;
+}
+
+#else
 
 #if !USE_NATIVE_FONT_DIALOG_FOR_MACOSX
 
 // wxFontDialog stub for mac OS's without a native font dialog
 // ---------------------------------------------------------------------------
 
-static const wxChar *wxFontFamilyIntToString(int family);
-static int wxFontFamilyStringToInt(const wxChar *family);
+static const wxChar *FontFamilyIntToString(int family);
+static int FontFamilyStringToInt(const wxChar *family);
 
 
 //-----------------------------------------------------------------------------
@@ -101,7 +324,7 @@ class wxFontColourSwatchCtrl: public wxControl
     DECLARE_CLASS(wxFontColourSwatchCtrl)
 public:
     wxFontColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0);
-    ~wxFontColourSwatchCtrl();
+    virtual ~wxFontColourSwatchCtrl();
 
     void OnPaint(wxPaintEvent& event);
     void OnMouseEvent(wxMouseEvent& event);
@@ -148,11 +371,11 @@ void wxFontColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
             parent = parent->GetParent();
 
         wxColourData data;
-        data.SetChooseFull(TRUE);
+        data.SetChooseFull(true);
         data.SetColour(m_colour);
         wxColourDialog *dialog = new wxColourDialog(parent, &data);
         // Crashes on wxMac (no m_peer)
-#ifndef __WXMAC__                
+#ifndef __WXMAC__
         dialog->SetTitle(_("Background colour"));
 #endif
         if (dialog->ShowModal() == wxID_OK)
@@ -165,7 +388,7 @@ void wxFontColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
         Refresh();
 
         wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
-        GetEventHandler()->ProcessEvent(event);
+        HandleWindowEvent(event);
     }
 }
 
@@ -248,7 +471,7 @@ bool wxFontDialog::Create(wxWindow* parent, const wxFontData& fontData)
  */
 
 void wxFontDialog::CreateControls()
-{    
+{
     wxFontDialog* itemDialog1 = this;
 
     wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
@@ -263,7 +486,7 @@ void wxFontDialog::CreateControls()
     itemBoxSizer3->Add(itemFlexGridSizer4, 1, wxGROW|wxALL, 5);
 
     wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("Font:"), wxDefaultPosition, wxDefaultSize, 0 );
-    itemFlexGridSizer4->Add(itemStaticText5, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL|wxADJUST_MINSIZE, 5);
+    itemFlexGridSizer4->Add(itemStaticText5, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL, 5);
 
     wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxVERTICAL);
     itemFlexGridSizer4->Add(itemBoxSizer6, 0, wxGROW|wxGROW, 5);
@@ -273,7 +496,7 @@ void wxFontDialog::CreateControls()
     itemBoxSizer6->Add(m_facenameCtrl, 0, wxGROW|wxALL, 5);
 
     wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Size:"), wxDefaultPosition, wxDefaultSize, 0 );
-    itemFlexGridSizer4->Add(itemStaticText8, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);
+    itemFlexGridSizer4->Add(itemStaticText8, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
 
     m_sizeCtrl = new wxSpinCtrl( itemDialog1, wxID_FONTDIALOG_FONTSIZE, _T("12"), wxDefaultPosition, wxSize(60, -1), wxSP_ARROW_KEYS, 1, 300, 12 );
     m_sizeCtrl->SetHelpText(_("The font size in points."));
@@ -282,7 +505,7 @@ void wxFontDialog::CreateControls()
     itemFlexGridSizer4->Add(m_sizeCtrl, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
 
     wxStaticText* itemStaticText10 = new wxStaticText( itemDialog1, wxID_STATIC, _("Style:"), wxDefaultPosition, wxDefaultSize, 0 );
-    itemFlexGridSizer4->Add(itemStaticText10, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);
+    itemFlexGridSizer4->Add(itemStaticText10, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
 
     wxBoxSizer* itemBoxSizer11 = new wxBoxSizer(wxHORIZONTAL);
     itemFlexGridSizer4->Add(itemBoxSizer11, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
@@ -310,12 +533,12 @@ void wxFontDialog::CreateControls()
             m_underlinedCtrl->SetToolTip(_("Check to make the font underlined."));
         itemBoxSizer11->Add(m_underlinedCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
     }
-    
+
     if (m_fontData.GetEnableEffects())
     {
         wxStaticText* itemStaticText15 = new wxStaticText( itemDialog1, wxID_STATIC, _("Colour:"), wxDefaultPosition, wxDefaultSize, 0 );
-        itemFlexGridSizer4->Add(itemStaticText15, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL|wxADJUST_MINSIZE, 5);
-        
+        itemFlexGridSizer4->Add(itemStaticText15, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL, 5);
+
         m_colourCtrl = new wxFontColourSwatchCtrl( itemDialog1, wxID_FONTDIALOG_COLOUR, wxDefaultPosition, wxSize(-1, 30), wxSUNKEN_BORDER|wxTAB_TRAVERSAL );
         m_colourCtrl->SetHelpText(_("Click to change the font colour."));
         if (ShowToolTips())
@@ -324,7 +547,7 @@ void wxFontDialog::CreateControls()
     }
 
     wxStaticText* itemStaticText17 = new wxStaticText( itemDialog1, wxID_STATIC, _("Preview:"), wxDefaultPosition, wxDefaultSize, 0 );
-    itemFlexGridSizer4->Add(itemStaticText17, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL|wxADJUST_MINSIZE, 5);
+    itemFlexGridSizer4->Add(itemStaticText17, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL, 5);
 
     m_previewCtrl = new wxFontPreviewCtrl( itemDialog1, wxID_FONTDIALOG_PREVIEW, wxDefaultPosition, wxSize(-1, 70), wxSUNKEN_BORDER|wxTAB_TRAVERSAL );
     m_previewCtrl->SetHelpText(_("Shows a preview of the font."));
@@ -350,18 +573,18 @@ void wxFontDialog::CreateControls()
 
     wxFontEnumerator enumerator;
     enumerator.EnumerateFacenames();
-    wxArrayString* facenames = enumerator.GetFacenames();
+    wxArrayString facenames = enumerator.GetFacenames();
     if (facenames)
     {
-        facenames->Add(_("<Any>"));
-        facenames->Add(_("<Any Roman>"));
-        facenames->Add(_("<Any Decorative>"));
-        facenames->Add(_("<Any Modern>"));
-        facenames->Add(_("<Any Script>"));
-        facenames->Add(_("<Any Swiss>"));
-        facenames->Add(_("<Any Teletype>"));
-        facenames->Sort();
-        m_facenameCtrl->Append(*facenames);
+        facenames.Add(_("<Any>"));
+        facenames.Add(_("<Any Roman>"));
+        facenames.Add(_("<Any Decorative>"));
+        facenames.Add(_("<Any Modern>"));
+        facenames.Add(_("<Any Script>"));
+        facenames.Add(_("<Any Swiss>"));
+        facenames.Add(_("<Any Teletype>"));
+        facenames.Sort();
+        m_facenameCtrl->Append(facenames);
     }
 
     InitializeControls();
@@ -492,11 +715,11 @@ void wxFontDialog::InitializeControls()
     m_boldCtrl->SetValue(m_dialogFont.GetWeight() == wxBOLD);
     m_italicCtrl->SetValue(m_dialogFont.GetStyle() == wxITALIC);
     m_sizeCtrl->SetValue(m_dialogFont.GetPointSize());
-    
+
     wxString facename = m_dialogFont.GetFaceName();
     if (facename.empty() || m_facenameCtrl->FindString(facename) == wxNOT_FOUND)
     {
-        facename = wxFontFamilyIntToString(m_dialogFont.GetFamily());
+        facename = FontFamilyIntToString(m_dialogFont.GetFamily());
     }
     m_facenameCtrl->SetStringSelection(facename);
 
@@ -521,7 +744,7 @@ void wxFontDialog::ChangeFont()
     int size = m_sizeCtrl->GetValue();
     wxString facename = m_facenameCtrl->GetStringSelection();
 
-    int family = wxFontFamilyStringToInt(facename);
+    int family = FontFamilyStringToInt(facename);
     if (family == -1)
         family = wxDEFAULT;
     else
@@ -555,7 +778,7 @@ void wxFontDialog::OnPanelClose()
 {
 }
 
-const wxChar *wxFontFamilyIntToString(int family)
+const wxChar *FontFamilyIntToString(int family)
 {
     switch (family)
     {
@@ -575,12 +798,12 @@ const wxChar *wxFontFamilyIntToString(int family)
     }
 }
 
-int wxFontFamilyStringToInt(const wxChar *family)
+int FontFamilyStringToInt(const wxChar *family)
 {
     if (!family)
         return wxSWISS;
 
-    if (wxStrcmp(family, _("<Any Roman")) == 0)
+    if (wxStrcmp(family, _("<Any Roman>")) == 0)
         return wxROMAN;
     else if (wxStrcmp(family, _("<Any Decorative>")) == 0)
         return wxDECORATIVE;
@@ -597,3 +820,6 @@ int wxFontFamilyStringToInt(const wxChar *family)
 
 #endif // !USE_NATIVE_FONT_DIALOG_FOR_MACOSX
 
+#endif // wxMAC_USE_EXPERIMENTAL_FONTDIALOG
+
+#endif // wxUSE_FONTDLG