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

  1. // mainwnd.cpp : Defines the behavior of the main window 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. static UINT idPaint = 0;
  18. static CFont oemFont;
  19. static CFont ansiFixedFont;
  20. static CFont ansiVarFont;
  21. static CFont deviceFont;
  22.  
  23. // used for mutual exclusion of menus
  24. static UINT idPrevVAlign = IDM_ALIGNBASE;
  25. static UINT idPrevHAlign = IDM_ALIGNLEFT;
  26. static UINT idPrevFont = IDM_SYSTEM;
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Initialization and cleanup
  30.  
  31. CMainWindow::CMainWindow(const char* szTitle)
  32. {
  33.     VERIFY(Create(NULL, szTitle,
  34.         WS_OVERLAPPEDWINDOW, rectDefault, NULL, "ShowFont"));
  35. }
  36.  
  37. int CMainWindow::OnCreate(LPCREATESTRUCT)
  38. {
  39.     OnFontChange();
  40.     myFont = new CFont;
  41.     myFont->CreateFont(
  42.         10,                                      /* height           */
  43.         10,                                      /* width            */
  44.         0,                                       /* escapement       */
  45.         0,                                       /* orientation      */
  46.         FW_NORMAL,                               /* weight           */
  47.         FALSE,                                   /* italic           */
  48.         FALSE,                                   /* underline        */
  49.         FALSE,                                   /* strikeout        */
  50.         OEM_CHARSET,                             /* charset          */
  51.         OUT_DEFAULT_PRECIS,                      /* out precision    */
  52.         CLIP_DEFAULT_PRECIS,                     /* clip precision   */
  53.         DEFAULT_QUALITY,                         /* quality          */
  54.         FIXED_PITCH | FF_MODERN,                 /* pitch and family */
  55.         "Courier");                              /* typeface         */
  56.  
  57.     VERIFY(oemFont.CreateStockObject(OEM_FIXED_FONT));
  58.     VERIFY(ansiFixedFont.CreateStockObject(ANSI_FIXED_FONT));
  59.     VERIFY(ansiVarFont.CreateStockObject(ANSI_VAR_FONT));
  60.     VERIFY(systemFont.CreateStockObject(SYSTEM_FONT));
  61.     VERIFY(deviceFont.CreateStockObject(DEVICE_DEFAULT_FONT));
  62.  
  63.     pTheFont = &systemFont;
  64.     LOGFONT logFont;
  65.     pTheFont->GetObject(sizeof(LOGFONT), &logFont);
  66.     strcpy(szWindowTitle, szAppName);
  67.     strcat(szWindowTitle, "SYSTEM");
  68.     SetWindowText(szWindowTitle);
  69.  
  70.     for (int i=0; i<64; i++)
  71.     {
  72.         outputText[0][i] = i;
  73.         outputText[1][i] = i+64;
  74.         outputText[2][i] = i+128;
  75.         outputText[3][i] = i+192;
  76.     }
  77.     return 0;
  78. }
  79.  
  80. void CMainWindow::SetFaceName()
  81. {
  82.     char buf[80];
  83.  
  84.     CClientDC dc(this);
  85.     dc.SelectObject(pTheFont);
  86.     strcpy(szWindowTitle, szAppName);
  87.     dc.GetTextFace(80, buf);
  88.     strcat(szWindowTitle, buf);
  89.     SetWindowText(szWindowTitle);
  90. }
  91.  
  92. void CMainWindow::OnExit()
  93. {
  94.     DestroyWindow();
  95. }
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // Main message handling
  99.  
  100. void CMainWindow::OnPaint()
  101. {
  102.     CPaintDC dc(this);
  103.     InitDC(dc); // Prepare the DC in a common way.
  104.  
  105.     // Character set is drawn as a background.
  106.     //
  107.     if (idPaint == IDM_SHOWCHARSET)
  108.         ShowCharacterSet(dc, pTheFont);
  109. }
  110.  
  111. void CMainWindow::OnLButtonUp(UINT, CPoint pt)
  112. {
  113.     // Draw the string where the mouse was clicked.
  114.     //
  115.     ptCurrent = pt;
  116.     ShowString();
  117. }
  118.  
  119. void CMainWindow::OnShowCharSet()
  120. {
  121.     Invalidate(TRUE);
  122.     idPaint = GetCurrentMessage()->wParam;
  123. }
  124.  
  125. void CMainWindow::OnClear()
  126. {
  127.     // Clear the window.
  128.     //
  129.     Invalidate(TRUE);
  130.     idPaint = 0;
  131. }
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134. // Menu commands for selecting from standard fonts.
  135.  
  136. void CMainWindow::SetFontFromMenu(CFont& newFont)
  137. {
  138.     // call from inside message callback
  139.     UINT id = GetCurrentMessage()->wParam;
  140.  
  141.     pTheFont = &newFont;
  142.     SetFaceName();                  /* Sets window title for us. */
  143.     GetMenu()->CheckMenuItem(idPrevFont, MF_UNCHECKED);
  144.     GetMenu()->CheckMenuItem(id, MF_CHECKED);
  145.     idPrevFont = id;
  146. }
  147.  
  148. void CMainWindow::OnOemFont()
  149. {
  150.     SetFontFromMenu(oemFont);
  151. }
  152.  
  153. void CMainWindow::OnAnsiFixedFont()
  154. {
  155.     SetFontFromMenu(ansiFixedFont);
  156. }
  157.  
  158. void CMainWindow::OnAnsiVarFont()
  159. {
  160.     SetFontFromMenu(ansiVarFont);
  161. }
  162.  
  163. void CMainWindow::OnSystemFont()
  164. {
  165.     SetFontFromMenu(systemFont);
  166. }
  167.  
  168. void CMainWindow::OnDeviceFont()
  169. {
  170.     SetFontFromMenu(deviceFont);
  171. }
  172.  
  173. /////////////////////////////////////////////////////////////////////////////
  174.  
  175. // This class handles the menu commands for selecting drawing colors.
  176. //
  177. // Note we completely define this modal dialog here, then use it in the
  178. // CMainWindow member functions, OnSetTextColor and OnSetBackgroundColor.
  179.  
  180. class CColorDlg : public CModalDialog
  181. {
  182. public:
  183.     CColorDlg() : CModalDialog("Colors")
  184.         { }
  185.  
  186.     // Attributes
  187.     DWORD m_rgbColor; // set this before invoking, and retrieve it afterwards
  188.  
  189.     UINT GetRed()
  190.         { return GetDlgItemInt(ID_RED, NULL, FALSE); }
  191.     void SetRed(UINT nColor)
  192.         { SetDlgItemInt(ID_RED, nColor, FALSE); }
  193.  
  194.     UINT GetGreen()
  195.         { return GetDlgItemInt(ID_GREEN, NULL, FALSE); }
  196.     void SetGreen(UINT nColor)
  197.         { SetDlgItemInt(ID_GREEN, nColor, FALSE); }
  198.  
  199.     UINT GetBlue()
  200.         { return GetDlgItemInt(ID_BLUE, NULL, FALSE); }
  201.     void SetBlue(UINT nColor)
  202.         { SetDlgItemInt(ID_BLUE, nColor, FALSE); }
  203.  
  204.     BOOL OnInitDialog()
  205.     {
  206.         SetRed(GetRValue(m_rgbColor));
  207.         SetGreen(GetGValue(m_rgbColor));
  208.         SetBlue(GetBValue(m_rgbColor));
  209.         return TRUE;
  210.     }
  211.  
  212.     void OnOK()
  213.     {
  214.         m_rgbColor = RGB(GetRed(), GetGreen(), GetBlue());
  215.         EndDialog(IDOK);
  216.     }
  217.  
  218.     // Note: OnCancel predefined to end dialog and return IDCANCEL.
  219. };
  220.  
  221. void CMainWindow::OnSetTextColor()
  222. {
  223.     CColorDlg   dlg;
  224.     dlg.m_rgbColor = rgbTextColor;
  225.     if (dlg.DoModal() == IDOK)
  226.         rgbTextColor = dlg.m_rgbColor;
  227. }
  228.  
  229. void CMainWindow::OnSetBackgroundColor()
  230. {
  231.     CColorDlg   dlg;
  232.     dlg.m_rgbColor = rgbBkColor;
  233.     if (dlg.DoModal() == IDOK)
  234.         rgbBkColor = dlg.m_rgbColor;
  235. }
  236.  
  237. /////////////////////////////////////////////////////////////////////////////
  238.  
  239. // These handle the menu commands for selecting drawing modes.
  240.  
  241. void CMainWindow::OnSetOpaque()
  242. {
  243.     nBkMode = OPAQUE;
  244.     GetMenu()->CheckMenuItem(IDM_TRANSPARENT, MF_UNCHECKED);
  245.     GetMenu()->CheckMenuItem(IDM_OPAQUE, MF_CHECKED);
  246. }
  247.  
  248. void CMainWindow::OnSetTransparent()
  249. {
  250.     nBkMode = TRANSPARENT;
  251.     GetMenu()->CheckMenuItem(IDM_OPAQUE,  MF_UNCHECKED);
  252.     GetMenu()->CheckMenuItem(IDM_TRANSPARENT,  MF_CHECKED);
  253. }
  254.  
  255. /////////////////////////////////////////////////////////////////////////////
  256.  
  257. // These routines handle the menu commands for selecting drawing alignment.
  258.  
  259. void CMainWindow::SetAlignFromMenu(short newAlign)
  260. {
  261.     // This routine assumes that a function listed in a message map is
  262.     // calling us, so that "GetCurrentMessage" works.
  263.     //
  264.     UINT id = GetCurrentMessage()->wParam;
  265.  
  266.     nAlignLCR = newAlign;
  267.     GetMenu()->CheckMenuItem(idPrevHAlign, MF_UNCHECKED);
  268.     GetMenu()->CheckMenuItem(id, MF_CHECKED);
  269.     idPrevHAlign = id;
  270. }
  271.  
  272. void CMainWindow::OnSetAlignLeft()
  273. {
  274.     SetAlignFromMenu(TA_LEFT);
  275. }
  276.  
  277. void CMainWindow::OnSetAlignCenter()
  278. {
  279.     SetAlignFromMenu(TA_CENTER);
  280. }
  281.  
  282. void CMainWindow::OnSetAlignRight()
  283. {
  284.     SetAlignFromMenu(TA_RIGHT);
  285. }
  286.  
  287. void CMainWindow::SetVAlignFromMenu(short newAlign)
  288. {
  289.     // This routine assumes that a function listed in a message map is
  290.     // calling us, so that "GetCurrentMessage" works.
  291.     //
  292.     UINT id = GetCurrentMessage()->wParam;
  293.  
  294.     nAlignTBB = newAlign;
  295.     GetMenu()->CheckMenuItem(idPrevVAlign, MF_UNCHECKED);
  296.     GetMenu()->CheckMenuItem(id, MF_CHECKED);
  297.     idPrevVAlign = id;
  298. }
  299.  
  300. void CMainWindow::OnSetAlignTop()
  301. {
  302.     SetVAlignFromMenu(TA_TOP);
  303. }
  304.  
  305. void CMainWindow::OnSetAlignBase()
  306. {
  307.     SetVAlignFromMenu(TA_BASELINE);
  308. }
  309.  
  310. void CMainWindow::OnSetAlignBottom()
  311. {
  312.     SetVAlignFromMenu(TA_BOTTOM);
  313. }
  314.  
  315. /////////////////////////////////////////////////////////////////////////////
  316.  
  317. // OnCreateFont:
  318. // Handles the menu command for creating a new font.
  319. //
  320. void CMainWindow::OnCreateFont()
  321. {
  322.     LOGFONT logFont;
  323.  
  324.     myFont->GetObject(sizeof(LOGFONT), &logFont);
  325.     if (DoCreateFontDlg(this, logFont))
  326.     {
  327.         delete myFont;
  328.         pTheFont = myFont = new CFont;
  329.         pTheFont->CreateFontIndirect(&logFont);
  330.         SetFaceName();
  331.         InvalidateRect(NULL, TRUE);
  332.     }
  333. }
  334.  
  335. /////////////////////////////////////////////////////////////////////////////
  336.  
  337. // CMainWindow message map:
  338. //
  339. BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
  340.  
  341.     // Window messages
  342.     ON_WM_CREATE()
  343.     ON_WM_PAINT()
  344.     ON_WM_LBUTTONUP()
  345.     ON_WM_DESTROY()
  346.     ON_WM_FONTCHANGE()
  347.  
  348.     // File menu
  349.     ON_COMMAND(IDM_ADDFONT, OnAddFont)
  350.     ON_COMMAND(IDM_DELFONT, OnDeleteFont)
  351.     ON_COMMAND(IDM_EXIT, OnExit)
  352.     ON_COMMAND(IDM_ABOUT, OnAbout)
  353.  
  354.     // Show menu
  355.     ON_COMMAND(IDM_SHOWSTRING, ShowString)
  356.     ON_COMMAND(IDM_SHOWCHARSET, OnShowCharSet)
  357.     ON_COMMAND(IDM_SHOWLOGFONT, OnShowLogFont)
  358.     ON_COMMAND(IDM_SHOWTEXTMETRICS, OnShowTextMetric)
  359.     ON_COMMAND(IDM_CLEAR, OnClear)
  360.  
  361.     // Font menu
  362.     ON_COMMAND(IDM_SYSTEM, OnSystemFont)
  363.     ON_COMMAND(IDM_ANSIFIXED, OnAnsiFixedFont)
  364.     ON_COMMAND(IDM_ANSIVAR, OnAnsiVarFont)
  365.     ON_COMMAND(IDM_OEM, OnOemFont)
  366.     ON_COMMAND(IDM_DEVICEDEF, OnDeviceFont)
  367.     ON_COMMAND(IDM_SELECTFONT, OnSelectFont)
  368.     ON_COMMAND(IDM_CFONT, OnCreateFont)
  369.  
  370.     // Options menu
  371.     ON_COMMAND(IDM_TEXTCOLOR, OnSetTextColor)
  372.     ON_COMMAND(IDM_BACKGROUNDCOLOR, OnSetBackgroundColor)
  373.     ON_COMMAND(IDM_OPAQUE, OnSetOpaque)
  374.     ON_COMMAND(IDM_TRANSPARENT, OnSetTransparent)
  375.     ON_COMMAND(IDM_ALIGNLEFT, OnSetAlignLeft)
  376.     ON_COMMAND(IDM_ALIGNCENTER, OnSetAlignCenter)
  377.     ON_COMMAND(IDM_ALIGNRIGHT, OnSetAlignRight)
  378.     ON_COMMAND(IDM_ALIGNTOP, OnSetAlignTop)
  379.     ON_COMMAND(IDM_ALIGNBASE, OnSetAlignBase)
  380.     ON_COMMAND(IDM_ALIGNBOTTOM, OnSetAlignBottom)
  381. END_MESSAGE_MAP()
  382.