home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK8 / MFC / SAMPLES / SHOWFONT / CFONT.CP$ / cfont
Encoding:
Text File  |  1992-03-07  |  10.1 KB  |  357 lines

  1. // cfont.cpp : Defines the CFontDlg modal dialog for ShowFont.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library. 
  4. // Copyright (C) 1992 Microsoft Corporation 
  5. // All rights reserved. 
  6. //  
  7. // This source code is only intended as a supplement to the 
  8. // Microsoft Foundation Classes Reference and Microsoft 
  9. // QuickHelp documentation provided with the library. 
  10. // See these sources for detailed information regarding the 
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "showfont.h"
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16.  
  17. // CFontDlg:
  18. // This class is completely declared and defined in this file.
  19. // Unlike most classes, the declaration is not in a separate header file
  20. // which is included in other sources.  Instead, a simple function
  21. // creates an object and invokes the dialog.
  22. //
  23. // This technique may simplify the use of classes whose objects can be
  24. // created and destroyed without much other interaction; modal dialogs like
  25. // this are often candidates for this approach.
  26. //
  27. class CFontDlg : public CModalDialog
  28. {
  29. private:
  30.     LOGFONT&  logFont;
  31. public:
  32.     CFontDlg(CWnd* pWnd, LOGFONT& rLogFont)
  33.         : logFont(rLogFont), CModalDialog("CFont", pWnd)
  34.             { }
  35.  
  36.  
  37.     // One way of getting access to members type-safely is to use
  38.     // member functions like the below that do the appropriate call
  39.     // to GetDlgItem() and cast the result to the correct C++ type.
  40.  
  41.     // simple edit item
  42.     CEdit&      FaceEdit()
  43.                     { return *((CEdit*) GetDlgItem(ID_FACE)); } 
  44.  
  45.     // styles (checkboxes)
  46.     CButton&    ItalicCheck()
  47.                     { return *((CButton*) GetDlgItem(ID_ITALIC)); } 
  48.     CButton&    UnderlineCheck()
  49.                     { return *((CButton*) GetDlgItem(ID_UNDERLINE)); } 
  50.     CButton&    StrikeOutCheck()
  51.                     { return *((CButton*) GetDlgItem(ID_STRIKEOUT)); } 
  52.  
  53.  
  54.     // When dealing with edit controls used for parsed number values,
  55.     // inline routines that call GetDlgItemInt/SetDlgItemInt are used.
  56.  
  57.     int         GetHeight()
  58.                     { return GetDlgItemInt(ID_HEIGHT); }
  59.     void        SetHeight(int n)
  60.                     { SetDlgItemInt(ID_HEIGHT, n); }
  61.     int         GetWidth()
  62.                     { return GetDlgItemInt(ID_WIDTH); }
  63.     void        SetWidth(int n)
  64.                     { SetDlgItemInt(ID_WIDTH, n); }
  65.     int         GetEscapement()
  66.                     { return GetDlgItemInt(ID_ESCAPEMENT); }
  67.     void        SetEscapement(int n)
  68.                     { SetDlgItemInt(ID_ESCAPEMENT, n); }
  69.     int         GetOrientation()
  70.                     { return GetDlgItemInt(ID_ORIENTATION); }
  71.     void        SetOrientation(int n)
  72.                     { SetDlgItemInt(ID_ORIENTATION, n); }
  73.     int         GetWeight()
  74.                     { return GetDlgItemInt(ID_WEIGHT); }
  75.     void        SetWeight(int n)
  76.                     { SetDlgItemInt(ID_WEIGHT, n); }
  77.     int         GetCharSetNum()
  78.                     { return GetDlgItemInt(ID_CHARSET); }
  79.     void        SetCharSetNum(int n)
  80.                     { SetDlgItemInt(ID_CHARSET, n); }
  81.  
  82.  
  83.     // When dealing with radio groups - it is easy to provide Get and Set
  84.     // functions that treat the group as one value.
  85.     //
  86.     // In the following we convert the radio button IDs to zero based values.
  87.  
  88.     int     GetOutPrecision()
  89.                 { return GetCheckedRadioButton(ID_OUT_STRING, ID_OUT_DEFAULT)
  90.                     - ID_OUT_STRING; }
  91.     void    SetOutPrecision(int n)
  92.                 { CheckRadioButton(ID_OUT_STRING, ID_OUT_DEFAULT,
  93.                     ID_OUT_STRING + n); }
  94.     int     GetClipPrecision()
  95.                 { return GetCheckedRadioButton(ID_CLIP_CHAR, ID_CLIP_DEFAULT)
  96.                     - ID_CLIP_CHAR; }
  97.     void    SetClipPrecision(int n)
  98.                 { CheckRadioButton(ID_CLIP_CHAR, ID_CLIP_DEFAULT,
  99.                     ID_CLIP_CHAR + n); }
  100.     int     GetQuality()
  101.                 { return GetCheckedRadioButton(ID_PROOF, ID_DEF_QUALITY)
  102.                     - ID_PROOF; }
  103.     void    SetQuality(int n)
  104.                 { CheckRadioButton(ID_PROOF, ID_DEF_QUALITY, ID_PROOF + n); }
  105.     int     GetPitch()
  106.                 { return GetCheckedRadioButton(ID_FIXED, ID_DEF_PITCH)
  107.                     - ID_FIXED; }
  108.     void    SetPitch(int n)
  109.                 { CheckRadioButton(ID_FIXED, ID_DEF_PITCH, ID_FIXED + n); }
  110.     int     GetFamily()
  111.                 { return GetCheckedRadioButton(ID_ROMAN, ID_DEF_FAMILY)
  112.                     - ID_ROMAN; }
  113.     void    SetFamily(int n)
  114.                 { CheckRadioButton(ID_ROMAN, ID_DEF_FAMILY, ID_ROMAN + n); }
  115.     int     GetCharSetOption()
  116.                 { return GetCheckedRadioButton(ID_ANSI, ID_OEM) - ID_ANSI; }
  117.     void    SetCharSetOption(int n)
  118.                 { CheckRadioButton(ID_ANSI, ID_OEM, ID_ANSI + n); }
  119.  
  120.     afx_msg void OnLight()
  121.     {
  122.         SetWeight(FW_LIGHT);
  123.     }
  124.  
  125.     afx_msg void OnNormal()
  126.     {
  127.         SetWeight(FW_NORMAL);
  128.     }
  129.  
  130.     afx_msg void OnBold()
  131.     {
  132.         SetWeight(FW_BOLD);
  133.     }
  134.  
  135.     afx_msg void OnChangeWeight()
  136.     {
  137.         // set a specific weight (uncheck all)
  138.         CheckRadioButton(ID_LIGHT, ID_BOLD, 0);
  139.     }
  140.  
  141.     afx_msg void OnAnsi()
  142.     {
  143.         SetCharSetNum(ANSI_CHARSET);
  144.     }
  145.  
  146.     afx_msg void OnOEM()
  147.     {
  148.         SetCharSetNum(OEM_CHARSET);
  149.     }
  150.  
  151.     afx_msg void OnCharSet()
  152.     {
  153.         // specific character set size - assume neither ANSI not OEM
  154.         CheckRadioButton(ID_ANSI, ID_OEM, 0);
  155.     }
  156.  
  157.     BOOL    OnInitDialog();
  158.     virtual void OnOK();
  159.  
  160.     DECLARE_MESSAGE_MAP()
  161. };
  162.  
  163. // CFontDlg message map:
  164. // This map ties each child control's notification messages (clicks) to
  165. // the appropriate member functions.
  166. //
  167. BEGIN_MESSAGE_MAP(CFontDlg, CModalDialog)
  168.     ON_COMMAND(ID_LIGHT, OnLight)
  169.     ON_COMMAND(ID_NORMAL, OnNormal)
  170.     ON_COMMAND(ID_BOLD, OnBold)
  171.     ON_COMMAND(ID_WEIGHT, OnChangeWeight)
  172.     ON_COMMAND(ID_ANSI, OnAnsi)
  173.     ON_COMMAND(ID_OEM, OnOEM)
  174.     ON_COMMAND(ID_CHARSET, OnCharSet)
  175.     // Note that OnOK is already inherited virtually.
  176. END_MESSAGE_MAP()
  177.  
  178. /////////////////////////////////////////////////////////////////////////////
  179. // RadioGroup mapping tables
  180.  
  181. // In the dialog, radio groups are represented with 0 based numbers.
  182. // Outside of the dialog, we want those numbers to represent specific
  183. // values to set or get from a LOGFONT.  The 'MatchValue' function takes
  184. // one of these values, and turns it into a zero based index.
  185. // The 'GetValue' function takes the zero based index and returns
  186. // the appropriate value.
  187.  
  188. static int MatchValue(int value, int * pValues)
  189.     // return the index of the match, or -1
  190. {
  191.     for (int index = 0; *pValues != -1; index++)
  192.     {
  193.         if (value == *pValues++)
  194.             return index;
  195.     }
  196.     return -1;
  197. }
  198.  
  199. static int GetValue(int index, int * pValues)
  200. {
  201.     // index into array with special case for negative values
  202.     if (index < 0)
  203.         return 0;       // hopefully a sensible default
  204.     else
  205.         return pValues[index];
  206. }
  207.  
  208. // The following tables must be in the same order as the radio buttons
  209. // defined in the resource file.
  210.  
  211. static int rgOutPrecision[] =
  212. {
  213.     OUT_STRING_PRECIS,
  214.     OUT_CHARACTER_PRECIS,
  215.     OUT_STROKE_PRECIS,
  216.     OUT_DEFAULT_PRECIS,
  217.     -1 /* end */
  218. };
  219.  
  220. static int rgClipPrecision[] =
  221. {
  222.     CLIP_CHARACTER_PRECIS,
  223.     CLIP_STROKE_PRECIS,
  224.     CLIP_DEFAULT_PRECIS,
  225.     -1 /* end */
  226. };
  227.  
  228. static int rgQuality[] =
  229. {
  230.     PROOF_QUALITY,
  231.     DRAFT_QUALITY,
  232.     DEFAULT_QUALITY,
  233.     -1 /* end */
  234. };
  235.  
  236. static int rgPitch[] =
  237. {
  238.     FIXED_PITCH,
  239.     VARIABLE_PITCH,
  240.     DEFAULT_PITCH,
  241.     -1 /* end */
  242. };
  243.  
  244. static int rgFamily[] =
  245. {
  246.     FF_ROMAN,
  247.     FF_SWISS,
  248.     FF_MODERN,
  249.     FF_SCRIPT,
  250.     FF_DECORATIVE,
  251.     FF_DONTCARE,
  252.     -1 /* end */
  253. };
  254.  
  255. static int rgCharSet[] =
  256. {
  257.     ANSI_CHARSET,
  258.     OEM_CHARSET,
  259.     -1 /* end */
  260. };
  261.  
  262. /////////////////////////////////////////////////////////////////////////////
  263. // OnInitDialog:
  264. // This is called when the dialog is invoked.  We set the state of all of
  265. // the child controls to appropriate states from our logFont information.
  266. //
  267. BOOL CFontDlg::OnInitDialog()
  268. {
  269.     SetHeight(logFont.lfHeight);
  270.     SetWidth(logFont.lfWidth);
  271.     SetEscapement(logFont.lfEscapement);
  272.     SetOrientation(logFont.lfOrientation);
  273.     FaceEdit().SetWindowText(logFont.lfFaceName);
  274.     SetCharSetNum(logFont.lfCharSet);
  275.  
  276.     ItalicCheck().SetCheck(logFont.lfItalic);
  277.     StrikeOutCheck().SetCheck(logFont.lfStrikeOut);
  278.     UnderlineCheck().SetCheck(logFont.lfUnderline);
  279.  
  280.     SetOutPrecision(MatchValue(logFont.lfOutPrecision, rgOutPrecision));
  281.     SetClipPrecision(MatchValue(logFont.lfClipPrecision, rgClipPrecision));
  282.     SetQuality(MatchValue(logFont.lfQuality, rgQuality));
  283.     SetCharSetOption(MatchValue(logFont.lfCharSet, rgCharSet));
  284.  
  285.     BYTE pitch = logFont.lfPitchAndFamily & 3;
  286.     BYTE family = logFont.lfPitchAndFamily & 0xf0;
  287.     SetPitch(MatchValue(pitch, rgPitch));
  288.     SetFamily(MatchValue(family, rgFamily));
  289.  
  290.     // If the weight matches one of the special values, check that radio
  291.     // button.
  292.     //
  293.     SetWeight(logFont.lfWeight);
  294.     switch (logFont.lfWeight)
  295.     {
  296.     case FW_LIGHT:
  297.         CheckRadioButton(ID_LIGHT, ID_BOLD, ID_LIGHT);
  298.         break;
  299.  
  300.     case FW_NORMAL:
  301.         CheckRadioButton(ID_LIGHT, ID_BOLD, ID_NORMAL);
  302.         break;
  303.  
  304.     case FW_BOLD:
  305.         CheckRadioButton(ID_LIGHT, ID_BOLD, ID_BOLD);
  306.         break;
  307.     }
  308.  
  309.     return TRUE;
  310. }
  311.  
  312. /////////////////////////////////////////////////////////////////////////////
  313. // OnOK:
  314. // This is automatically called when the OK button is pushed.  We update
  315. // the logFont information and end the dialog with the "successful" return
  316. // of IDOK.
  317. //
  318. void CFontDlg::OnOK()
  319. {
  320.     logFont.lfHeight = GetHeight();
  321.     logFont.lfWidth = GetWidth();
  322.     logFont.lfEscapement = GetEscapement();
  323.     logFont.lfOrientation = GetOrientation();
  324.     FaceEdit().GetWindowText(logFont.lfFaceName, 32);
  325.     logFont.lfWeight = GetWeight();
  326.     logFont.lfCharSet = GetCharSetNum();
  327.  
  328.     logFont.lfItalic = ItalicCheck().GetCheck();
  329.     logFont.lfStrikeOut = StrikeOutCheck().GetCheck();
  330.     logFont.lfUnderline = UnderlineCheck().GetCheck();
  331.  
  332.     logFont.lfOutPrecision = GetValue(GetOutPrecision(), rgOutPrecision);
  333.     logFont.lfClipPrecision = GetValue(GetClipPrecision(), rgClipPrecision);
  334.     logFont.lfQuality = GetValue(GetQuality(), rgQuality);
  335.     // lfCharSet is not set by radio group (get from edit text)
  336.  
  337.     // pitch and family are combined
  338.     BYTE pitch = GetValue(GetPitch(), rgPitch);
  339.     BYTE family = GetValue(GetFamily(), rgFamily);
  340.     logFont.lfPitchAndFamily = pitch | family;  // put back together
  341.  
  342.     EndDialog(IDOK);
  343. }
  344.  
  345. /////////////////////////////////////////////////////////////////////////////
  346. // DoCreateFontDlg:
  347. // This function is the only interface required to use this class.  Calling
  348. // it will create and invoke a CFontDlg object, and will not return until
  349. // the modal dialog has been closed.  Include showfont.h for the prototype.
  350. //
  351. int DoCreateFontDlg(CWnd* pWnd, LOGFONT& rLogFont)
  352. {
  353.     CFontDlg dlg(pWnd, rLogFont);
  354.     return dlg.DoModal();
  355. }
  356.  
  357.