home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / src / generic / fontdlgg.cpp < prev    next >
C/C++ Source or Header  |  2002-11-14  |  13KB  |  449 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name:        src/generic/fontdlgg.cpp
  3. // Purpose:     Generic font dialog
  4. // Author:      Julian Smart
  5. // Modified by:
  6. // Created:     04/01/98
  7. // RCS-ID:      $Id: fontdlgg.cpp,v 1.32.2.3 2002/11/14 20:54:22 JS Exp $
  8. // Copyright:   (c) Julian Smart and Markus Holzem
  9. // Licence:     wxWindows license
  10. /////////////////////////////////////////////////////////////////////////////
  11.  
  12. #ifdef __GNUG__
  13. #pragma implementation "fontdlgg.h"
  14. #endif
  15.  
  16. // For compilers that support precompilation, includes "wx.h".
  17. #include "wx/wxprec.h"
  18.  
  19. #ifdef __BORLANDC__
  20. #pragma hdrstop
  21. #endif
  22.  
  23. #if wxUSE_FONTDLG && (!defined(__WXGTK__) || defined(__WXUNIVERSAL__))
  24.  
  25. #ifndef WX_PRECOMP
  26.     #include <stdio.h>
  27.     #include "wx/utils.h"
  28.     #include "wx/dialog.h"
  29.     #include "wx/listbox.h"
  30.     #include "wx/button.h"
  31.     #include "wx/stattext.h"
  32.     #include "wx/layout.h"
  33.     #include "wx/dcclient.h"
  34.     #include "wx/choice.h"
  35.     #include "wx/checkbox.h"
  36.     #include "wx/intl.h"
  37. #endif
  38.  
  39. #include <string.h>
  40. #include <stdlib.h>
  41.  
  42. #include "wx/cmndata.h"
  43. #include "wx/sizer.h"
  44. #include "wx/fontdlg.h"
  45.  
  46. //-----------------------------------------------------------------------------
  47. // helper class - wxFontPreviewer
  48. //-----------------------------------------------------------------------------
  49.  
  50. class WXDLLEXPORT wxFontPreviewer : public wxWindow
  51. {
  52. public:
  53.     wxFontPreviewer(wxWindow *parent) : wxWindow(parent, -1) {}
  54.  
  55. private:
  56.     void OnPaint(wxPaintEvent& event);
  57.     DECLARE_EVENT_TABLE()
  58. };
  59.  
  60. BEGIN_EVENT_TABLE(wxFontPreviewer, wxWindow)
  61.     EVT_PAINT(wxFontPreviewer::OnPaint)
  62. END_EVENT_TABLE()
  63.  
  64. void wxFontPreviewer::OnPaint(wxPaintEvent& WXUNUSED(event))
  65. {
  66.     wxPaintDC dc(this);
  67.  
  68.     wxSize size = GetSize();
  69.     wxFont font = GetFont();
  70.  
  71.     dc.SetPen(*wxBLACK_PEN);
  72.     dc.SetBrush(*wxWHITE_BRUSH);
  73.     dc.DrawRectangle(0, 0, size.x, size.y);
  74.  
  75.     if ( font.Ok() )
  76.     {
  77.         dc.SetFont(font);
  78.         // Calculate vertical centre
  79.         long w, h;
  80.         dc.GetTextExtent( wxT("X"), &w, &h);
  81.         dc.SetTextForeground(GetForegroundColour());
  82.         dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
  83.         dc.DrawText(_("ABCDEFGabcdefg12345"), 
  84.                      10, size.y/2 - h/2);
  85.         dc.DestroyClippingRegion();
  86.     }
  87. }
  88.  
  89. //-----------------------------------------------------------------------------
  90. // wxGenericFontDialog
  91. //-----------------------------------------------------------------------------
  92.  
  93. IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog)
  94.  
  95. BEGIN_EVENT_TABLE(wxGenericFontDialog, wxDialog)
  96.     EVT_CHECKBOX(wxID_FONT_UNDERLINE, wxGenericFontDialog::OnChangeFont)
  97.     EVT_CHOICE(wxID_FONT_STYLE, wxGenericFontDialog::OnChangeFont)
  98.     EVT_CHOICE(wxID_FONT_WEIGHT, wxGenericFontDialog::OnChangeFont)
  99.     EVT_CHOICE(wxID_FONT_FAMILY, wxGenericFontDialog::OnChangeFont)
  100.     EVT_CHOICE(wxID_FONT_COLOUR, wxGenericFontDialog::OnChangeFont)
  101.     EVT_CHOICE(wxID_FONT_SIZE, wxGenericFontDialog::OnChangeFont)
  102.     EVT_CLOSE(wxGenericFontDialog::OnCloseWindow)
  103. END_EVENT_TABLE()
  104.  
  105.  
  106. #define NUM_COLS 48
  107. static wxString wxColourDialogNames[NUM_COLS]={wxT("ORANGE"),
  108.                     wxT("GOLDENROD"),
  109.                     wxT("WHEAT"),
  110.                     wxT("SPRING GREEN"),
  111.                     wxT("SKY BLUE"),
  112.                     wxT("SLATE BLUE"),
  113.                     wxT("MEDIUM VIOLET RED"),
  114.                     wxT("PURPLE"),
  115.  
  116.                     wxT("RED"),
  117.                     wxT("YELLOW"),
  118.                     wxT("MEDIUM SPRING GREEN"),
  119.                     wxT("PALE GREEN"),
  120.                     wxT("CYAN"),
  121.                     wxT("LIGHT STEEL BLUE"),
  122.                     wxT("ORCHID"),
  123.                     wxT("LIGHT MAGENTA"),
  124.  
  125.                     wxT("BROWN"),
  126.                     wxT("YELLOW"),
  127.                     wxT("GREEN"),
  128.                     wxT("CADET BLUE"),
  129.                     wxT("MEDIUM BLUE"),
  130.                     wxT("MAGENTA"),
  131.                     wxT("MAROON"),
  132.                     wxT("ORANGE RED"),
  133.  
  134.                     wxT("FIREBRICK"),
  135.                     wxT("CORAL"),
  136.                     wxT("FOREST GREEN"),
  137.                     wxT("AQUARAMINE"),
  138.                     wxT("BLUE"),
  139.                     wxT("NAVY"),
  140.                     wxT("THISTLE"),
  141.                     wxT("MEDIUM VIOLET RED"),
  142.  
  143.                     wxT("INDIAN RED"),
  144.                     wxT("GOLD"),
  145.                     wxT("MEDIUM SEA GREEN"),
  146.                     wxT("MEDIUM BLUE"),
  147.                     wxT("MIDNIGHT BLUE"),
  148.                     wxT("GREY"),
  149.                     wxT("PURPLE"),
  150.                     wxT("KHAKI"),
  151.  
  152.                     wxT("BLACK"),
  153.                     wxT("MEDIUM FOREST GREEN"),
  154.                     wxT("KHAKI"),
  155.                     wxT("DARK GREY"),
  156.                     wxT("SEA GREEN"),
  157.                     wxT("LIGHT GREY"),
  158.                     wxT("MEDIUM SLATE BLUE"),
  159.                     wxT("WHITE")
  160.                     };
  161.  
  162. /*
  163.  * Generic wxFontDialog
  164.  */
  165.  
  166. void wxGenericFontDialog::Init()
  167. {
  168.   m_useEvents = FALSE;
  169.   m_previewer = NULL;
  170.   Create( m_parent ) ;
  171. }
  172.  
  173. wxGenericFontDialog::~wxGenericFontDialog()
  174. {
  175. }
  176.  
  177. void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
  178. {
  179.   EndModal(wxID_CANCEL);
  180. }
  181.  
  182. bool wxGenericFontDialog::DoCreate(wxWindow *parent)
  183. {
  184.     if ( !wxDialog::Create( parent , -1 , _T("Choose Font") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
  185.         _T("fontdialog") ) )
  186.     {
  187.         wxFAIL_MSG( wxT("wxFontDialog creation failed") );
  188.         return FALSE;
  189.     }
  190.  
  191.   InitializeFont();
  192.   CreateWidgets();
  193.  
  194.   // sets initial font in preview area
  195.   wxCommandEvent dummy;
  196.   OnChangeFont(dummy);
  197.   
  198.   return TRUE;
  199. }
  200.  
  201. int wxGenericFontDialog::ShowModal()
  202. {
  203.     int ret = wxDialog::ShowModal();
  204.  
  205.     if (ret != wxID_CANCEL)
  206.     {
  207.       m_fontData.chosenFont = dialogFont;
  208.     }
  209.  
  210.     return ret;
  211. }
  212.  
  213. void wxGenericFontDialog::CreateWidgets()
  214. {
  215.   wxBusyCursor bcur;
  216.  
  217.   wxString
  218.      *families = new wxString[6],
  219.      *styles = new wxString[3],
  220.      *weights = new wxString[3];
  221.   families[0] =  _("Roman");
  222.   families[1] = _("Decorative");
  223.   families[2] = _("Modern");
  224.   families[3] = _("Script");
  225.   families[4] = _("Swiss" );
  226.   families[5] = _("Teletype" );
  227.   styles[0] = _("Normal");
  228.   styles[1] = _("Italic");
  229.   styles[2] = _("Slant");
  230.   weights[0] = _("Normal");
  231.   weights[1] = _("Light");
  232.   weights[2] = _("Bold");
  233.  
  234.   familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxDefaultPosition, wxDefaultSize, 5, families);
  235.   styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxDefaultPosition, wxDefaultSize, 3, styles);
  236.   weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxDefaultPosition, wxDefaultSize, 3, weights);
  237.  
  238.   colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxDefaultPosition, wxDefaultSize, NUM_COLS, wxColourDialogNames);
  239.  
  240.   wxString *pointSizes = new wxString[40];
  241.   int i;
  242.   for ( i = 0; i < 40; i++)
  243.   {
  244.     wxChar buf[5];
  245.     wxSprintf(buf, wxT("%d"), i + 1);
  246.     pointSizes[i] = buf;
  247.   }
  248.  
  249.   pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
  250.   underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
  251.  
  252.   m_previewer = new wxFontPreviewer(this);
  253.  
  254.   wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
  255.   wxButton *cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel"));
  256.  
  257.   familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
  258.   styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
  259.   weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
  260.   wxString name(wxTheColourDatabase->FindName(m_fontData.fontColour));
  261.   colourChoice->SetStringSelection(name);
  262.  
  263.   underLineCheckBox->SetValue(dialogFont.GetUnderlined());
  264.   pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
  265.  
  266.   okButton->SetDefault();
  267.  
  268.   wxSizer *topsizer, *sizer;
  269.   topsizer = new wxBoxSizer(wxVERTICAL);
  270.  
  271.   sizer = new wxBoxSizer(wxHORIZONTAL);
  272.   sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
  273.   sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
  274.   sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
  275.   topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
  276.  
  277.   sizer = new wxBoxSizer(wxHORIZONTAL);
  278.   sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
  279.   sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
  280.   sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
  281.   topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
  282.   
  283.   topsizer->Add(m_previewer, 1, wxALL | wxEXPAND, 10);
  284.   topsizer->SetItemMinSize(m_previewer, 430, 100);
  285.  
  286.   sizer = new wxBoxSizer(wxHORIZONTAL);
  287.   sizer->Add(okButton, 0, wxRIGHT, 10);
  288.   sizer->Add(cancelButton, 0, wxRIGHT, 10);
  289.   topsizer->Add(sizer, 0, wxALIGN_RIGHT | wxBOTTOM, 10);
  290.  
  291.   SetAutoLayout(TRUE);
  292.   SetSizer(topsizer);
  293.   topsizer->SetSizeHints(this);
  294.   topsizer->Fit(this);
  295.  
  296.   Centre(wxBOTH);
  297.  
  298.   delete[] families;
  299.   delete[] styles;
  300.   delete[] weights;
  301.   delete[] pointSizes;
  302.   m_useEvents = TRUE;
  303. }
  304.  
  305. void wxGenericFontDialog::InitializeFont()
  306. {
  307.   int fontFamily = wxSWISS;
  308.   int fontWeight = wxNORMAL;
  309.   int fontStyle = wxNORMAL;
  310.   int fontSize = 12;
  311.   int fontUnderline = FALSE;
  312.  
  313.   if (m_fontData.initialFont.Ok())
  314.   {
  315.       fontFamily = m_fontData.initialFont.GetFamily();
  316.       fontWeight = m_fontData.initialFont.GetWeight();
  317.       fontStyle = m_fontData.initialFont.GetStyle();
  318.       fontSize = m_fontData.initialFont.GetPointSize();
  319.       fontUnderline = m_fontData.initialFont.GetUnderlined();
  320.   }
  321.  
  322.   dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
  323.  
  324.   if (m_previewer)
  325.       m_previewer->SetFont(dialogFont);
  326. }
  327.  
  328. void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
  329. {
  330.   if (!m_useEvents) return;
  331.  
  332.   int fontFamily = 0;  /* shut up buggy egcs warnings */
  333.   fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
  334.   int fontWeight = 0;
  335.   fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
  336.   int fontStyle = 0;
  337.   fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
  338.   int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
  339.   int fontUnderline = underLineCheckBox->GetValue();
  340.  
  341.   dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
  342.   m_previewer->SetFont(dialogFont);
  343.   if (colourChoice->GetStringSelection() != wxT(""))
  344.   {
  345.     wxColour *col = (wxColour*) NULL;
  346.     col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
  347.     if (col)
  348.     {
  349.       m_fontData.fontColour = *col;
  350.       m_previewer->SetForegroundColour(*col);
  351.     }
  352.   }
  353.   m_previewer->Refresh();
  354. }
  355.  
  356. const wxChar *wxFontWeightIntToString(int weight)
  357. {
  358.   switch (weight)
  359.   {
  360.     case wxLIGHT:
  361.       return wxT("Light");
  362.     case wxBOLD:
  363.       return wxT("Bold");
  364.     case wxNORMAL:
  365.     default:
  366.       return wxT("Normal");
  367.   }
  368. }
  369.  
  370. const wxChar *wxFontStyleIntToString(int style)
  371. {
  372.   switch (style)
  373.   {
  374.     case wxITALIC:
  375.       return wxT("Italic");
  376.     case wxSLANT:
  377.       return wxT("Slant");
  378.     case wxNORMAL:
  379.     default:
  380.       return wxT("Normal");
  381.   }
  382. }
  383.  
  384. const wxChar *wxFontFamilyIntToString(int family)
  385. {
  386.   switch (family)
  387.   {
  388.     case wxROMAN:
  389.       return wxT("Roman");
  390.     case wxDECORATIVE:
  391.       return wxT("Decorative");
  392.     case wxMODERN:
  393.       return wxT("Modern");
  394.     case wxSCRIPT:
  395.       return wxT("Script");
  396.     case wxTELETYPE:
  397.       return wxT("Teletype");
  398.     case wxSWISS:
  399.     default:
  400.       return wxT("Swiss");
  401.   }
  402. }
  403.  
  404. int wxFontFamilyStringToInt(wxChar *family)
  405. {
  406.   if (!family)
  407.     return wxSWISS;
  408.  
  409.   if (wxStrcmp(family, wxT("Roman")) == 0)
  410.     return wxROMAN;
  411.   else if (wxStrcmp(family, wxT("Decorative")) == 0)
  412.     return wxDECORATIVE;
  413.   else if (wxStrcmp(family, wxT("Modern")) == 0)
  414.     return wxMODERN;
  415.   else if (wxStrcmp(family, wxT("Script")) == 0)
  416.     return wxSCRIPT;
  417.   else if (wxStrcmp(family, wxT("Teletype")) == 0)
  418.     return wxTELETYPE;
  419.   else return wxSWISS;
  420. }
  421.  
  422. int wxFontStyleStringToInt(wxChar *style)
  423. {
  424.   if (!style)
  425.     return wxNORMAL;
  426.   if (wxStrcmp(style, wxT("Italic")) == 0)
  427.     return wxITALIC;
  428.   else if (wxStrcmp(style, wxT("Slant")) == 0)
  429.     return wxSLANT;
  430.   else
  431.     return wxNORMAL;
  432. }
  433.  
  434. int wxFontWeightStringToInt(wxChar *weight)
  435. {
  436.   if (!weight)
  437.     return wxNORMAL;
  438.   if (wxStrcmp(weight, wxT("Bold")) == 0)
  439.     return wxBOLD;
  440.   else if (wxStrcmp(weight, wxT("Light")) == 0)
  441.     return wxLIGHT;
  442.   else
  443.     return wxNORMAL;
  444. }
  445.  
  446. #endif
  447.     // wxUSE_FONTDLG
  448.  
  449.