From ddc8c2e3abd656601dfba93ea1eab42aeeaededf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 1 Oct 1999 18:01:54 +0000 Subject: [PATCH] wxFontEnumerator class for Motif/X git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@3781 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- distrib/msw/tmake/filelist.txt | 1 + include/wx/fontenum.h | 51 ++++++++ samples/controls/controls.cpp | 1 + samples/font/font.cpp | 49 +++++++- samples/text/Makefile.in | 2 +- samples/text/text.cpp | 12 +- src/motif/fontenum.cpp | 206 +++++++++++++++++++++++++++++++++ src/msw/makefile.b32 | 2 +- src/msw/makefile.bcc | 2 +- src/msw/makefile.dos | 2 +- src/msw/makefile.g95 | 2 +- src/msw/makefile.sc | 2 +- src/msw/makefile.vc | 2 +- src/msw/makefile.wat | 2 +- 14 files changed, 314 insertions(+), 22 deletions(-) create mode 100644 include/wx/fontenum.h create mode 100644 src/motif/fontenum.cpp diff --git a/distrib/msw/tmake/filelist.txt b/distrib/msw/tmake/filelist.txt index 2763c91124..891772df07 100644 --- a/distrib/msw/tmake/filelist.txt +++ b/distrib/msw/tmake/filelist.txt @@ -358,6 +358,7 @@ dcscreen.cpp X dialog.cpp X filedlg.cpp X font.cpp X +fontenum.cpp X frame.cpp X gauge.cpp X gdiobj.cpp X diff --git a/include/wx/fontenum.h b/include/wx/fontenum.h new file mode 100644 index 0000000000..8ee785ffc6 --- /dev/null +++ b/include/wx/fontenum.h @@ -0,0 +1,51 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: fontenum.h +// Purpose: wxFontEnumerator class for getting available fonts +// Author: Julian Smart, Vadim Zeitlin +// Modified by: extended to enumerate more than just font families and work ot +// only on Windows (VZ) +// Created: 04/01/98 +// RCS-ID: $Id$ +// Copyright: (c) Julian Smart, Vadim Zeitlin +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_FONTENUM_H_ +#define _WX_FONTENUM_H_ + +#ifdef __GNUG__ + #pragma interface "fontenum.h" +#endif + +// ---------------------------------------------------------------------------- +// wxFontEnumerator enumerates all available fonts on the system or only the +// fonts with given attributes +// ---------------------------------------------------------------------------- + +class wxFontEnumerator +{ +public: + // start enumerating font families - will result in OnFontFamily() being + // called for each available font family (unless it returns FALSE) + virtual bool EnumerateFamilies(bool fixedWidthOnly = FALSE); + + // enumerate the different encodings either for given font family or for + // all font families - will result in OnFontEncoding() being called for + // each available (family, encoding) couple + virtual bool EnumerateEncodings(const wxString& family = _T("")); + + // callbacks which are called after one of EnumerateXXX() functions from + // above is invoked - all of them may return FALSE to stop enumeration or + // TRUE to continue with it + + // called by EnumerateFamilies + virtual bool OnFontFamily(const wxString& WXUNUSED(family)) + { return FALSE; } + + // called by EnumerateEncodings + virtual bool OnFontEncoding(const wxString& WXUNUSED(family), + const wxString& WXUNUSED(encoding)) + { return FALSE; } +}; + +#endif // _WX_FONTENUM_H_ diff --git a/samples/controls/controls.cpp b/samples/controls/controls.cpp index 55d24885bb..7613a9f99f 100644 --- a/samples/controls/controls.cpp +++ b/samples/controls/controls.cpp @@ -395,6 +395,7 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) panel = new wxPanel(m_notebook); m_choice = new wxChoice( panel, ID_CHOICE, wxPoint(10,10), wxSize(120,-1), 5, choices ); + m_choice->SetSelection(2); m_choice->SetBackgroundColour( "red" ); (void)new wxButton( panel, ID_CHOICE_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) ); (void)new wxButton( panel, ID_CHOICE_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) ); diff --git a/samples/font/font.cpp b/samples/font/font.cpp index d18e23eea7..60c3a39cf5 100644 --- a/samples/font/font.cpp +++ b/samples/font/font.cpp @@ -25,6 +25,7 @@ #endif #include +#include // ---------------------------------------------------------------------------- // private classes @@ -81,8 +82,14 @@ public: void OnAbout(wxCommandEvent& event); void OnSelectFont(wxCommandEvent& event); void OnCreateFont(wxCommandEvent& event); + void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event)) + { DoEnumerateFamilies(FALSE); } + void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event)) + { DoEnumerateFamilies(TRUE); } protected: + void DoEnumerateFamilies(bool fixedWidthOnly); + MyCanvas *m_canvas; private: @@ -119,7 +126,10 @@ enum Font_Quit = 1, Font_About, Font_Choose = 100, - Font_Create + Font_Create, + Font_EnumFamilies, + Font_EnumFixedFamilies, + Font_Max }; // ---------------------------------------------------------------------------- @@ -134,6 +144,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Font_About, MyFrame::OnAbout) EVT_MENU(Font_Choose, MyFrame::OnSelectFont) EVT_MENU(Font_Create, MyFrame::OnCreateFont) + EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies) + EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies) END_EVENT_TABLE() // Create a new application object: this macro will allow wxWindows to create @@ -155,7 +167,7 @@ IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { // Create the main application window - MyFrame *frame = new MyFrame("Minimal wxWindows App", + MyFrame *frame = new MyFrame("Font wxWindows demo", wxPoint(50, 50), wxSize(450, 340)); // Show it and tell the application that it's our main window @@ -176,9 +188,6 @@ bool MyApp::OnInit() MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame((wxFrame *)NULL, -1, title, pos, size) { - // set the frame icon - SetIcon(wxICON(mondrian)); - // create a menu bar wxMenu *menuFile = new wxMenu; @@ -187,10 +196,14 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) menuFile->Append(Font_Quit, "E&xit\tAlt-X", "Quit this program"); wxMenu *menuFont = new wxMenu; - menuFont->Append(Font_Choose, "&Select font...\tCtrl-F", + menuFont->Append(Font_Choose, "&Select font...\tCtrl-S", "Select a standard font"); menuFont->Append(Font_Create, "&Create font...\tCtrl-C", "Create a custom font"); + menuFont->AppendSeparator(); + menuFont->Append(Font_EnumFamilies, "&Enumerate font families\tCtrl-E"); + menuFont->Append(Font_EnumFixedFamilies, + "&Enumerate fixed font families\tCtrl-F"); // now append the freshly created menu to the menu bar... wxMenuBar *menuBar = new wxMenuBar; @@ -210,6 +223,30 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) // event handlers +void MyFrame::DoEnumerateFamilies(bool fixedWidthOnly) +{ + class MyFontEnumerator : public wxFontEnumerator + { + public: + MyFontEnumerator() { m_n = 0; } + + protected: + virtual bool OnFontFamily(const wxString& family) + { + wxLogMessage("Font family %d: %s\n", ++m_n, family.c_str()); + + return TRUE; + } + + private: + size_t m_n; + } fontEnumerator; + + wxLogMessage("Enumerating %s font families:", + fixedWidthOnly ? "fixed width" : "all"); + fontEnumerator.EnumerateFamilies(fixedWidthOnly); +} + void MyFrame::OnCreateFont(wxCommandEvent& WXUNUSED(event)) { MyFontDialog dialog(this); diff --git a/samples/text/Makefile.in b/samples/text/Makefile.in index fc853c6528..01ee0927d1 100644 --- a/samples/text/Makefile.in +++ b/samples/text/Makefile.in @@ -13,7 +13,7 @@ top_srcdir = @top_srcdir@ top_builddir = ../.. program_dir = samples/text -DATAFILES = text.cpp +DATAFILES = text.rc PROGRAM=text diff --git a/samples/text/text.cpp b/samples/text/text.cpp index c60909d53f..43a2a663e3 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -32,10 +32,6 @@ #include "wx/tooltip.h" #endif -#if defined(__WXGTK__) || defined(__WXMOTIF__) - #include "mondrian.xpm" -#endif - // We test for wxUSE_DRAG_AND_DROP also, because data objects may not be // implemented for compilers that can't cope with the OLE parts in // wxUSE_DRAG_AND_DROP. @@ -171,8 +167,7 @@ bool MyApp::OnInit() { // Create the main frame window MyFrame *frame = new MyFrame((wxFrame *) NULL, - "Text wxWindows App", - 50, 50, 640, 420); + "Text wxWindows sample", 50, 50, 640, 420); frame->SetSizeHints( 500, 400 ); wxMenu *file_menu = new wxMenu; @@ -454,7 +449,8 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) m_horizontal = new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.", wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL ); - m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxBOLD)); + m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL, + FALSE, "", wxFONTENCODING_KOI8)); m_multitext = new MyTextCtrl( this, -1, "Multi line.", wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE ); @@ -668,7 +664,7 @@ void MyFrame::OnToggleTooltips(wxCommandEvent& event) void MyFrame::OnFileLoad(wxCommandEvent& event) { - if ( m_panel->m_multitext->LoadFile("text.cpp") ) + if ( m_panel->m_multitext->LoadFile("text.rc") ) wxLogStatus(this, _T("Successfully loaded file")); else wxLogStatus(this, _T("Couldn't load the file")); diff --git a/src/motif/fontenum.cpp b/src/motif/fontenum.cpp new file mode 100644 index 0000000000..33c661dfa4 --- /dev/null +++ b/src/motif/fontenum.cpp @@ -0,0 +1,206 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: src/motif/fontenum.cpp +// Purpose: wxFontEnumerator class for X11/GDK +// Author: Vadim Zeitlin +// Modified by: +// Created: 01.10.99 +// RCS-ID: $Id$ +// Copyright: (c) Vadim Zeitlin +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#ifdef __GNUG__ + #pragma implementation "fontenum.h" +#endif + +#include "wx/defs.h" +#include "wx/dynarray.h" +#include "wx/string.h" +#include "wx/utils.h" + +#include "wx/fontenum.h" + +#include + +// ---------------------------------------------------------------------------- +// private functions +// ---------------------------------------------------------------------------- + +// compare function for sorted array of strings +static int CMPFUNC_CONV CompareStrings(const char *s1, const char *s2); + +// create the list of all fonts with the given spacing +static char **CreateFontList(wxChar spacing, int *nFonts); + +// extract all font families from the given font list and call our +// OnFontFamily() for each of them +static bool ProcessFamiliesFromFontList(wxFontEnumerator *This, + char **fonts, + int nFonts); + + +// ---------------------------------------------------------------------------- +// private types +// ---------------------------------------------------------------------------- + +WX_DEFINE_SORTED_ARRAY(const char *, wxSortedStringArray); + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// helpers +// ---------------------------------------------------------------------------- + +static int CMPFUNC_CONV CompareStrings(const char *s1, const char *s2) +{ + return strcmp(s1, s2); +} + +static char **CreateFontList(wxChar spacing, int *nFonts) +{ + wxString pattern; + pattern.Printf(_T("-*-*-*-*-*-*-*-*-*-*-%c-*-*-*"), spacing); + + // get the list of all fonts + return XListFonts((Display *)wxGetDisplay(), pattern, 32767, nFonts); +} + +static bool ProcessFamiliesFromFontList(wxFontEnumerator *This, + char **fonts, + int nFonts) +{ + // extract the list of (unique) font families + wxSortedStringArray families(CompareStrings); + for ( int n = 0; n < nFonts; n++ ) + { + char *font = fonts[n]; + if ( !wxString(font).Matches("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") ) + { + // it's not a full font name (probably an alias) + continue; + } + + char *dash = strchr(font + 1, '-'); + char *family = dash + 1; + dash = strchr(family, '-'); + *dash = '\0'; // !NULL because Matches() above succeeded + + if ( families.Index(family) == wxNOT_FOUND ) + { + if ( !This->OnFontFamily(family) ) + { + // stop enumerating + return FALSE; + } + + families.Add(family); + } + //else: already seen + } + + return TRUE; +} + +// ---------------------------------------------------------------------------- +// wxFontEnumerator +// ---------------------------------------------------------------------------- + +bool wxFontEnumerator::EnumerateFamilies(bool fixedWidthOnly) +{ + int nFonts; + char **fonts; + + if ( fixedWidthOnly ) + { + bool cont = TRUE; + fonts = CreateFontList(_T('m'), &nFonts); + if ( fonts ) + { + cont = ProcessFamiliesFromFontList(this, fonts, nFonts); + + XFreeFontNames(fonts); + } + + if ( !cont ) + { + return TRUE; + } + + fonts = CreateFontList(_T('c'), &nFonts); + if ( !fonts ) + { + return TRUE; + } + } + else + { + fonts = CreateFontList(_T('*'), &nFonts); + + if ( !fonts ) + { + wxFAIL_MSG(_T("No fonts at all on this system?")); + + return FALSE; + } + } + + (void)ProcessFamiliesFromFontList(this, fonts, nFonts); + + XFreeFontNames(fonts); + + return TRUE; +} + +bool wxFontEnumerator::EnumerateEncodings(const wxString& family) +{ +#if 0 + wxString pattern; + pattern.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"), + family.IsEmpty() ? _T("*") : family.c_str()); + + // get the list of all fonts + int nFonts; + char **fonts = XListFonts((Display *)wxGetDisplay(), pattern, + 32767, nFonts); + + if ( !fonts ) + { + // unknown family? + return FALSE; + } + + // extract the list of (unique) encodings + wxSortedStringArray families(CompareStrings); + for ( int n = 0; n < nFonts; n++ ) + { + char *font = fonts[n]; + if ( !wxString(font).Matches("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") ) + { + // it's not a full font name (probably an alias) + continue; + } + + // extract the family + char *dash = strchr(font + 1, '-'); + char *family = dash + 1; + dash = strchr(family, '-'); + *dash = '\0'; // !NULL because Matches() above succeeded + + // now extract the registry/encoding + } + + return TRUE; +#endif // 0 + + return FALSE; // TODO +} diff --git a/src/msw/makefile.b32 b/src/msw/makefile.b32 index 60e0cd35b9..9ad8a4a539 100644 --- a/src/msw/makefile.b32 +++ b/src/msw/makefile.b32 @@ -1,6 +1,6 @@ -# This file was automatically generated by tmake at 15:48, 1999/10/01 +# This file was automatically generated by tmake at 20:00, 1999/10/01 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE B32.T! # diff --git a/src/msw/makefile.bcc b/src/msw/makefile.bcc index c15f467b2e..5a881e60d9 100644 --- a/src/msw/makefile.bcc +++ b/src/msw/makefile.bcc @@ -1,6 +1,6 @@ -# This file was automatically generated by tmake at 15:48, 1999/10/01 +# This file was automatically generated by tmake at 20:00, 1999/10/01 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE BCC.T! # diff --git a/src/msw/makefile.dos b/src/msw/makefile.dos index 5aa303f208..124d7a5756 100644 --- a/src/msw/makefile.dos +++ b/src/msw/makefile.dos @@ -1,6 +1,6 @@ -# This file was automatically generated by tmake at 15:48, 1999/10/01 +# This file was automatically generated by tmake at 20:00, 1999/10/01 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE DOS.T! # diff --git a/src/msw/makefile.g95 b/src/msw/makefile.g95 index d818f6715e..df39ab1845 100644 --- a/src/msw/makefile.g95 +++ b/src/msw/makefile.g95 @@ -1,5 +1,5 @@ -# This file was automatically generated by tmake at 15:48, 1999/10/01 +# This file was automatically generated by tmake at 20:00, 1999/10/01 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE G95.T! # diff --git a/src/msw/makefile.sc b/src/msw/makefile.sc index bb8a7036d2..e6a5ae8fd7 100644 --- a/src/msw/makefile.sc +++ b/src/msw/makefile.sc @@ -1,6 +1,6 @@ -# This file was automatically generated by tmake at 15:48, 1999/10/01 +# This file was automatically generated by tmake at 20:00, 1999/10/01 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE SC.T! # Symantec C++ makefile for the msw objects diff --git a/src/msw/makefile.vc b/src/msw/makefile.vc index ebc10723df..c3999e489a 100644 --- a/src/msw/makefile.vc +++ b/src/msw/makefile.vc @@ -1,4 +1,4 @@ -# This file was automatically generated by tmake at 15:48, 1999/10/01 +# This file was automatically generated by tmake at 20:01, 1999/10/01 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE VC.T! # File: makefile.vc diff --git a/src/msw/makefile.wat b/src/msw/makefile.wat index ce3059af20..30a88a097b 100644 --- a/src/msw/makefile.wat +++ b/src/msw/makefile.wat @@ -1,6 +1,6 @@ -# This file was automatically generated by tmake at 15:48, 1999/10/01 +# This file was automatically generated by tmake at 20:01, 1999/10/01 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE WAT.T! #!/binb/wmake.exe