home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / appwiz / customwz / seqdlg.cpp < prev    next >
C/C++ Source or Header  |  1998-03-05  |  7KB  |  256 lines

  1. // seqdlg.cpp : implementation file
  2. //
  3. // Copyright (c) 1985-1998, Microsoft Corporation. All rights reserved.
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include "customwz.h"
  8. #include "seqdlg.h"
  9. #include "sampleaw.h"
  10. #include "typedlg.h"
  11. #include "options.h"
  12. #include "paint.h"
  13.  
  14. #ifdef _PSEUDO_DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19.  
  20. // Retrieve language suffix ("enu" or "fra", etc.)
  21. void GetLangSuffix(CString& strLang)
  22. {
  23.     int nLeftParen = strLang.Find(_T('('));
  24.     ASSERT(nLeftParen != -1);
  25.     strLang = strLang.Mid(nLeftParen+1);    // Now, strLang starts at the DLL name
  26.     strLang.MakeLower();
  27.     int nPeriod = strLang.Find(_T('.'));
  28.     ASSERT(nPeriod != -1);
  29.     ASSERT(strLang.Left(5) == _T("appwz"));
  30.     strLang = strLang.Mid(5, nPeriod-5);
  31. }
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CSequenceDlg dialog
  35.  
  36.  
  37. CSequenceDlg::CSequenceDlg()
  38.     : CAppWizStepDlg(CSequenceDlg::IDD)
  39. {
  40.     //{{AFX_DATA_INIT(CSequenceDlg)
  41.     m_nApwzSeq = APWZSEQ_EXE;
  42.     //}}AFX_DATA_INIT
  43.     SetSeqBitmap();
  44.     DefineDefaultLangMacros();
  45. }
  46.  
  47. void CSequenceDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49.     CAppWizStepDlg::DoDataExchange(pDX);
  50.     //{{AFX_DATA_MAP(CSequenceDlg)
  51.     DDX_Radio(pDX, IDC_APWZ_SEQ, m_nApwzSeq);
  52.     //}}AFX_DATA_MAP
  53. }
  54.  
  55.  
  56. BEGIN_MESSAGE_MAP(CSequenceDlg, CAppWizStepDlg)
  57.     //{{AFX_MSG_MAP(CSequenceDlg)
  58.     ON_COMMAND_EX(IDC_APWZ_SEQ, OnClickedRadio)
  59.     ON_WM_PAINT()
  60.     ON_COMMAND_EX(IDC_APWZ_SEQ2, OnClickedRadio)
  61.     //}}AFX_MSG_MAP
  62. END_MESSAGE_MAP()
  63.  
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CSequenceDlg message handlers
  67.  
  68. #define STEP2AB_LEFT            8
  69. #define STEP2AB_TOP             40
  70. #define STEP2AB_WIDTH           179
  71. #define STEP2AB_HEIGHT          180
  72.  
  73. // Override OnPaint to draw pictures on the left side
  74. void CSequenceDlg::OnPaint()
  75. {
  76.     CPaintDC dc(this); // device context for painting
  77.  
  78.     PaintBackground(&dc, this);
  79.  
  80.     CDC dcMem;
  81.     if (!dcMem.CreateCompatibleDC(&dc))
  82.         return;
  83.  
  84.     // picture
  85.     PaintBitmap(m_nSeqBitmap, STEP2AB_LEFT, STEP2AB_TOP, STEP2AB_WIDTH, STEP2AB_HEIGHT, &dc, &dcMem);
  86. }
  87.  
  88. // Set member variable to the bitmap ID corresponding to the currently
  89. //  selected AppWizard sequence
  90. void CSequenceDlg::SetSeqBitmap()
  91. {
  92.     static UINT nBmps[] = {IDB_STEP2A, IDB_STEP2B};
  93.     ASSERT (m_nApwzSeq == 0 || m_nApwzSeq == 1);
  94.     m_nSeqBitmap = nBmps[m_nApwzSeq];
  95. }
  96.  
  97. // This handler allows us to dynamically change the bitmap as soon
  98. //  as a new AppWizard sequence is selected.
  99. BOOL CSequenceDlg::OnClickedRadio(UINT nID)
  100. {
  101.     UpdateData(TRUE);
  102.     SetSeqBitmap();
  103.  
  104.     // Invalidate the portion of this dialog on which we draw the picture
  105.     CRect rect(STEP2AB_LEFT, STEP2AB_TOP, STEP2AB_LEFT + STEP2AB_WIDTH, STEP2AB_TOP + STEP2AB_HEIGHT);
  106.     RedrawWindow(&rect, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
  107.  
  108.     EnableLangListbox(m_nApwzSeq == APWZSEQ_EXE);
  109.  
  110.     return TRUE;
  111. }
  112.  
  113. void CSequenceDlg::DefineDefaultLangMacros()
  114. {
  115.     ScanForAvailableLanguages(m_strlLanguages);
  116.  
  117.     // m_strlLanguages is now a stringlist whose entries are of the form:
  118.     //  "U.S. English (appwzenu.dll);0x040904e4"  For the purposes of the
  119.     //  listbox, we want to strip out the translation identifiers
  120.     //  (0x040904e4).  However, we retain the POSITION in m_strlLanguages
  121.     //  for each entry, so that when we generate the custom AppWizard, its
  122.     //  code will correctly call SetSupportedLanguages with the original
  123.     //  entries that AppWizard gave us here, complete with the translation
  124.     //  identifiers (see InitLangList() below).
  125.  
  126.     POSITION pos = m_strlLanguages.GetHeadPosition();
  127.     int nLanguageCount = 0;
  128.     CString strSupportedLangs;
  129.     while (pos != NULL)
  130.     {
  131.         CString strLang = m_strlLanguages.GetNext(pos);
  132.         strSupportedLangs += _T("\\n") + strLang;
  133.         GetLangSuffix(strLang);
  134.         DefineStringMacro(_T("CUSTOM_LANG_SUFFIX"), nLanguageCount++, strLang);
  135.     }
  136.  
  137.     ASSERT(!strSupportedLangs.IsEmpty());
  138.     strSupportedLangs = strSupportedLangs.Mid(2);   // Chop off leading '\n'
  139.     DefineStringMacro(_T("SUPPORTED_LANGS"), strSupportedLangs);
  140.     DefineIntMacro(_T("NUM_CUSTOM_LANGS"), nLanguageCount);
  141. }
  142.  
  143. // Set macros corresponding to the languages selected
  144. #define MAX_SELECTED_LANGS 32
  145. BOOL CSequenceDlg::DefineLangMacros()
  146. {
  147.     CString strSupportedLangs;
  148.     int pnSelectedLangs[MAX_SELECTED_LANGS];
  149.     CListBox* pList = (CListBox*) GetDlgItem(IDC_LANGS);
  150.     int nSelCount = pList->GetSelCount();
  151.  
  152.     if (nSelCount > MAX_SELECTED_LANGS)
  153.     {
  154.         // We use a stack-allocated, static-size array for storing the
  155.         //  listbox selections, since it's cheaper than using the heap.
  156.         //  Print error in the unlikely event there are over 32 languages
  157.         //  installed & selected.
  158.         CString strPrompt;
  159.         strPrompt.LoadString(IDP_TOO_MANY_LANGS);
  160.         strPrompt.Format(strPrompt, MAX_SELECTED_LANGS);
  161.         AfxMessageBox(strPrompt);
  162.         return FALSE;
  163.     }
  164.  
  165.     // Make sure at least one language is selected
  166.     if (nSelCount == 0)
  167.     {
  168.         AfxMessageBox(IDP_NO_LANGS_SELECTED);
  169.         return FALSE;
  170.     }
  171.  
  172.     // Go through listbox and make a string of the names of the selected
  173.     //  languages.  The string will be of the form:
  174.     //  "U.S. English (appwzenu.dll)\nFrench (appwzfra.dll)"
  175.     pList->GetSelItems(MAX_SELECTED_LANGS, pnSelectedLangs);
  176.  
  177.     for (int i=0; i < nSelCount; i++)
  178.     {
  179.         POSITION pos = (POSITION) pList->GetItemData(pnSelectedLangs[i]);
  180.         CString strLang = m_strlLanguages.GetAt(pos);
  181.         strSupportedLangs += _T("\\n") + strLang;
  182.  
  183.         // Retrieve language suffix ("enu" or "fra", etc.)
  184.         GetLangSuffix(strLang);
  185.         DefineStringMacro(_T("CUSTOM_LANG_SUFFIX"), i, strLang);
  186.     }
  187.  
  188.     ASSERT(!strSupportedLangs.IsEmpty());
  189.     strSupportedLangs = strSupportedLangs.Mid(2);   // Chop off leading '\n'
  190.     DefineStringMacro(_T("SUPPORTED_LANGS"), strSupportedLangs);
  191.     DefineIntMacro(_T("NUM_CUSTOM_LANGS"), nSelCount);
  192.  
  193.     return TRUE;
  194. }
  195.  
  196.  
  197. // Set step-related template macros before dismissing
  198. BOOL CSequenceDlg::OnDismiss()
  199. {
  200.     if (!UpdateData(TRUE))
  201.         return FALSE;
  202.  
  203.     if (m_nApwzSeq == APWZSEQ_EXE && !DefineLangMacros())
  204.         return FALSE;
  205.  
  206.     g_options.m_nApwzSeq = m_nApwzSeq;
  207.     g_options.DefineDlgMacros();
  208.  
  209.     return TRUE;
  210. }
  211.  
  212. void CSequenceDlg::EnableLangListbox(BOOL bEnable)
  213. {
  214.     GetDlgItem(IDC_LANGS)->EnableWindow(bEnable);
  215.     GetDlgItem(IDC_LANGS_STATIC)->EnableWindow(bEnable);
  216. }
  217.  
  218. void CSequenceDlg::InitLangList()
  219. {
  220.     CListBox* pList = (CListBox*) GetDlgItem(IDC_LANGS);
  221.  
  222.     int i = 0;
  223.     POSITION pos = m_strlLanguages.GetHeadPosition();
  224.     while (pos != NULL)
  225.     {
  226.         POSITION posCurrent = pos;
  227.         CString strLang = m_strlLanguages.GetNext(pos);
  228.         int nSemicolon = strLang.Find(_T(';'));
  229.         ASSERT(nSemicolon != -1);
  230.  
  231.         // Add the entry minus the translation ID to the listbox
  232.         i = pList->AddString(strLang.Left(nSemicolon));
  233.  
  234.         // Retain original POSITION for use when generating the wizard
  235.         pList->SetItemData(i, (DWORD) posCurrent);
  236.     }
  237.  
  238.     // By default, the custom AppWizard will support the languages AppWizard
  239.     //  supports.  Thus, we start off the listbox by selecting all languages.
  240.     if (i == 0)
  241.         pList->SetSel(0, TRUE);
  242.     else
  243.         pList->SelItemRange(TRUE, 0, i);
  244.  
  245.     EnableLangListbox(m_nApwzSeq == APWZSEQ_EXE);
  246. }
  247.  
  248.  
  249. BOOL CSequenceDlg::OnInitDialog()
  250. {
  251.     CAppWizStepDlg::OnInitDialog();
  252.     InitLangList();
  253.  
  254.     return TRUE;
  255. }
  256.