]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/font/font.cpp
technote about translations added
[wxWidgets.git] / samples / font / font.cpp
index acc320e27082c02f1d0237c401a80d9114b04a7a..8c1a0b630a44c4ad9f69db4ab3f84a9a703f4df5 100644 (file)
@@ -28,6 +28,7 @@
 #include <wx/fontdlg.h>
 #include <wx/fontenum.h>
 #include <wx/fontmap.h>
+#include <wx/encconv.h>
 #include <wx/textfile.h>
 
 // ----------------------------------------------------------------------------
@@ -234,34 +235,36 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
     SetStatusText("Welcome to wxWindows font demo!");
 }
 
+// --------------------------------------------------------
 
-// event handlers
-void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
+class MyEncodingEnumerator : public wxFontEnumerator
 {
-    class MyEncodingEnumerator : public wxFontEnumerator
-    {
-    public:
-        MyEncodingEnumerator() { m_n = 0; }
-
-        const wxString& GetText() const { return m_text; }
+public:
+    MyEncodingEnumerator() 
+        { m_n = 0; }
 
-    protected:
-        virtual bool OnFontEncoding(const wxString& facename,
-                                    const wxString& encoding)
-        {
-            wxString text;
-            text.Printf("Encoding %d: %s (available in facename '%s')\n",
-                         ++m_n, encoding.c_str(), facename.c_str());
-            m_text += text;
+    const wxString& GetText() const 
+        { return m_text; }
 
-            return TRUE;
-        }
+protected:
+    virtual bool OnFontEncoding(const wxString& facename,
+                                const wxString& encoding)
+    {
+        wxString text;
+        text.Printf("Encoding %d: %s (available in facename '%s')\n",
+                    ++m_n, encoding.c_str(), facename.c_str());
+        m_text += text;
+        return TRUE;
+    }
 
-    private:
-        size_t m_n;
+private:
+    size_t m_n;
+    wxString m_text;
+};
 
-        wxString m_text;
-    } fontEnumerator;
+void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
+{
+    MyEncodingEnumerator fontEnumerator;
 
     fontEnumerator.EnumerateEncodings();
 
@@ -269,56 +272,76 @@ void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
                  fontEnumerator.GetText().c_str());
 }
 
-bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly,
-                                  wxFontEncoding encoding,
-                                  bool silent)
-{
-    class MyFontEnumerator : public wxFontEnumerator
-    {
-    public:
-        bool GotAny() const { return !m_facenames.IsEmpty(); }
+// -------------------------------------------------------------
 
-        const wxArrayString& GetFacenames() const { return m_facenames; }
+class MyFontEnumerator : public wxFontEnumerator
+{
+public:
+    bool GotAny() const 
+        { return !m_facenames.IsEmpty(); }
 
-    protected:
-        virtual bool OnFacename(const wxString& facename)
-        {
-            m_facenames.Add(facename);
+    const wxArrayString& GetFacenames() const 
+        { return m_facenames; }
 
-            return TRUE;
-        }
+protected:
+    virtual bool OnFacename(const wxString& facename)
+    {
+        m_facenames.Add(facename);
+        return TRUE;
+    }
 
     private:
         wxArrayString m_facenames;
-    } fontEnumerator;
+} fontEnumerator;
+
+bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly,
+                                  wxFontEncoding encoding,
+                                  bool silent)
+{
+    MyFontEnumerator fontEnumerator;
 
     fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly);
 
     if ( fontEnumerator.GotAny() )
     {
-        int n, nFacenames = fontEnumerator.GetFacenames().GetCount();
-        wxLogStatus(this, "Found %d %sfonts",
-                    nFacenames, fixedWidthOnly ? "fixed width " : "");
-
-        wxString *facenames = new wxString[nFacenames];
-        for ( n = 0; n < nFacenames; n++ )
-            facenames[n] = fontEnumerator.GetFacenames().Item(n);
+        int nFacenames = fontEnumerator.GetFacenames().GetCount();
+        if ( !silent )
+        {
+            wxLogStatus(this, "Found %d %sfonts",
+                        nFacenames, fixedWidthOnly ? "fixed width " : "");
+        }
 
+        wxString facename;
         if ( silent )
-            n = 1;
+        {
+            // choose the first
+            facename = fontEnumerator.GetFacenames().Item(0);
+        }
         else
+        {
+            // let the user choose
+            wxString *facenames = new wxString[nFacenames];
+            int n;
+            for ( n = 0; n < nFacenames; n++ )
+                facenames[n] = fontEnumerator.GetFacenames().Item(n);
+
             n = wxGetSingleChoiceIndex("Choose a facename", "Font demo",
                                        nFacenames, facenames, this);
-        if ( n != -1 )
+
+            if ( n != -1 )
+                facename = facenames[n];
+
+            delete [] facenames;
+        }
+
+        if ( !facename.IsEmpty() )
         {
-            wxFont font(14, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
-                        wxFONTWEIGHT_NORMAL, FALSE, facenames[n], encoding);
+            wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
+                        wxFONTWEIGHT_NORMAL, FALSE, facename, encoding);
 
             DoChangeFont(font);
         }
 
-        delete [] facenames;
-
         return TRUE;
     }
     else if ( !silent )
@@ -474,10 +497,37 @@ void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
         return;
     }
 
+    m_textctrl->LoadFile(filename);
+
+    if (!wxTheFontMapper->IsEncodingAvailable(fontenc))
+    {
+        // try to find some similar encoding:
+        wxFontEncoding encAlt;
+        if ( wxTheFontMapper->GetAltForEncoding(fontenc, &encAlt) )
+        {
+            wxEncodingConverter conv;
+            
+            if (conv.Init(fontenc, encAlt))
+            {
+                fontenc = encAlt;
+                m_textctrl -> SetValue(conv.Convert(m_textctrl -> GetValue()));
+            }
+            else
+            {
+                wxLogWarning("Cannot convert from '%s' to '%s'.",
+                             wxFontMapper::GetEncodingDescription(fontenc).c_str(),
+                             wxFontMapper::GetEncodingDescription(encAlt).c_str());
+            }
+        }
+        else
+            wxLogWarning("No fonts for encoding '%s' on this system.",
+                         wxFontMapper::GetEncodingDescription(fontenc).c_str());
+    }
+
     // and now create the correct font
     if ( !DoEnumerateFamilies(FALSE, fontenc, TRUE /* silent */) )
     {
-        wxFont font(14, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
+        wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
                     wxFONTWEIGHT_NORMAL, FALSE /* !underlined */,
                     wxEmptyString /* facename */, fontenc);
         if ( font.Ok() )
@@ -490,8 +540,6 @@ void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
                          wxFontMapper::GetEncodingDescription(fontenc).c_str());
         }
     }
-
-    m_textctrl->LoadFile(filename);
 }
 
 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
@@ -542,10 +590,9 @@ BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
 END_EVENT_TABLE()
 
 MyCanvas::MyCanvas( wxWindow *parent )
-        : wxWindow( parent, -1 )
+        : wxWindow( parent, -1 ),
+          m_colour(*wxRED), m_font(*wxNORMAL_FONT)
 {
-    m_font = *wxNORMAL_FONT;
-    m_colour = *wxRED;
 }
 
 MyCanvas::~MyCanvas()