home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / MSJMAR94.ZIP / DYNDLG.ZIP / DYNDLG1.ZIP / DEMODLG.C next >
C/C++ Source or Header  |  1994-03-01  |  5KB  |  223 lines

  1. /**************************************************************************
  2.     Dynamic Dialog Boxes Demo
  3.     Demo Dialog Module
  4.     Written by Atif Aziz
  5. **************************************************************************/
  6.  
  7. #define STRICT
  8.  
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include "winxext.h"
  12. #include "dialogs.h"
  13. #include "ids.h"
  14.  
  15. #ifdef __BORLANDC__
  16. #pragma warn -eff    // Control API produces 'code has no effect'
  17. #endif            // warnings under Borland C++, so turn it off.
  18.  
  19.     // Category
  20.  
  21. typedef struct tagCATEGORY
  22. {
  23.       LPCTSTR    lpszName;
  24.       int    nFrameID;
  25.       int    nFirstID;
  26.       int    nLastID;
  27. }
  28. CATEGORY;
  29.  
  30. static CATEGORY category[] =
  31.         {
  32.             {
  33.                 TEXT("Font"),
  34.                 IDC_FRAME_FONT,
  35.                 IDC_FONT_FIRST,
  36.                 IDC_FONT_LAST
  37.             },
  38.             {
  39.                 TEXT("Text"),
  40.                 IDC_FRAME_TEXT,
  41.                 IDC_TEXT_FIRST,
  42.                 IDC_TEXT_LAST
  43.             },
  44.             {
  45.                 TEXT("Patterns"),
  46.                 IDC_FRAME_PATTERNS,
  47.                 IDC_PATTERNS_FIRST,
  48.                 IDC_PATTERNS_LAST
  49.             }
  50.         };
  51.  
  52. static int    nCurProperty = 0;
  53.  
  54.     // Function prototypes
  55.  
  56. BOOL CALLBACK    DynDlgBox(HWND, UINT, WPARAM, LPARAM);
  57.  
  58. static LRESULT    DynDlgBox_DlgProc(HWND, UINT, WPARAM, LPARAM);
  59. static BOOL    DynDlgBox_OnInitDialog(HWND, HWND, LPARAM);
  60. static void    DynDlgBox_OnCommand(HWND, int, HWND, UINT);
  61.  
  62. #ifdef __BORLANDC__
  63. #pragma argsused        // Parameter 'hinstPrev' is not used.
  64. #endif
  65.  
  66. int PASCAL WinMain(hinstA, hinstPrev, lpszCmdLine, nCmdShow)
  67. HINSTANCE       hinstA;
  68. HINSTANCE       hinstPrev;
  69. LPSTR           lpszCmdLine;
  70. int             nCmdShow;
  71. {
  72.   FARPROC    farproc;
  73.  
  74.   farproc = MakeProcInstance((FARPROC) DynDlgBox, hinstA);
  75.   DialogBox(hinstA, TEXT("TextFormat"), NULL, (DLGPROC) farproc);
  76.   FreeProcInstance(farproc);
  77.  
  78.   return 0;
  79. }
  80.  
  81. BOOL CALLBACK DynDlgBox(hwndDlg, uMessage, wParam, lParam)
  82. HWND    hwndDlg;
  83. UINT    uMessage;
  84. WPARAM    wParam;
  85. LPARAM    lParam;
  86. {
  87.   CheckDefDlgRecursion(&g_fDefDlgEx);
  88.   return SetDlgMsgResult(hwndDlg, uMessage,
  89.     DynDlgBox_DlgProc(hwndDlg, uMessage, wParam, lParam));
  90. }
  91.  
  92. static LRESULT DynDlgBox_DlgProc(hwndDlg, uMessage, wParam, lParam)
  93. HWND    hwndDlg;
  94. UINT    uMessage;
  95. WPARAM    wParam;
  96. LPARAM    lParam;
  97. {
  98.   switch (uMessage)
  99.   {
  100.     case WM_CTLCOLOR :
  101.     return TRUE;
  102.    
  103.     HANDLE_MSG(hwndDlg, WM_INITDIALOG, DynDlgBox_OnInitDialog);
  104.     HANDLE_MSG(hwndDlg, WM_COMMAND, DynDlgBox_OnCommand);
  105.  
  106.     default :
  107.     return CommonDlg_DefProc(hwndDlg, uMessage, wParam, lParam);
  108.   }
  109. }
  110.  
  111. #ifdef __BORLANDC__
  112. #pragma argsused    // Parameters 'hwndFocus' and 'lParam' not used.
  113. #endif            
  114.  
  115. static BOOL DynDlgBox_OnInitDialog(hwndDlg, hwndFocus, lparam)
  116. HWND    hwndDlg;
  117. HWND    hwndFocus;
  118. LPARAM    lparam;
  119. {
  120.   int    i;
  121.   HWND    hwndProp;
  122.  
  123.   hwndProp = GetDlgItem(hwndDlg, IDC_PROPERTY);
  124.  
  125.   for (i = 0; i < sizeof(category) / sizeof(CATEGORY); i++)
  126.   {
  127.     ListBox_AddString(hwndProp, category[i].lpszName);
  128.  
  129.     if (i != nCurProperty)
  130.     {
  131.       // If this is not the category to be initially shown, then hide it.
  132.  
  133.       ShowCategory(hwndDlg, category[i].nFirstID, category[i].nLastID, FALSE);
  134.     }
  135.     else
  136.     {
  137.       // Select the default in the category list box.
  138.  
  139.       ListBox_SetCurSel(hwndProp, i);
  140.     }
  141.  
  142.     // Move the category controls from outside to the inside of the
  143.     // dialog box.
  144.  
  145.     MoveCategory(hwndDlg, category[i].nFirstID, category[i].nLastID,
  146.       category[i].nFrameID, IDC_FRAME_DIALOGBOX);
  147.   }
  148.  
  149.   return TRUE;
  150. }
  151.  
  152. #ifdef __BORLANDC__
  153. #pragma argsused    // Parameters 'hwndCtl' and 'uNotifyCode' not used.
  154. #endif            
  155.  
  156. static void DynDlgBox_OnCommand(hwndDlg, nID, hwndCtl, uNotifyCode)
  157. HWND    hwndDlg;
  158. int    nID;
  159. HWND    hwndCtl;
  160. UINT    uNotifyCode;
  161. {
  162.   int        i;
  163.   int        nCurSel;
  164.   RECT        rc;
  165.  
  166.   switch (nID)
  167.   {
  168.     case IDC_PROPERTY :
  169.  
  170.         if (uNotifyCode == LBN_SELCHANGE)
  171.         {
  172.  
  173.         // Select the corresponding category from the array based
  174.         // on the current selection in the list box.
  175.  
  176.           nCurSel = ListBox_GetCurSel(hwndCtl);
  177.  
  178.           if (nCurSel != nCurProperty && nCurSel != LB_ERR)
  179.           {
  180.             // Avoid unnecessary screen refresh by suppressing
  181.             // redraws. This will optimise the display radically.
  182.  
  183.             SetWindowRedraw(hwndDlg, FALSE);
  184.  
  185.             // First hide the category currently on display.
  186.  
  187.             ShowCategory(hwndDlg, category[nCurProperty].nFirstID,
  188.               category[nCurProperty].nLastID, FALSE);
  189.  
  190.             nCurProperty = nCurSel;
  191.  
  192.             // Now show the new category's controls.
  193.  
  194.             ShowCategory(hwndDlg, category[nCurProperty].nFirstID,
  195.               category[nCurProperty].nLastID, TRUE);
  196.  
  197.             // Set the redraw back on and invalidate the changes
  198.                     // resulting from hiding and showing controls.
  199.  
  200.             SetWindowRedraw(hwndDlg, TRUE);
  201.  
  202.             GetWindowRect(GetDlgItem(hwndDlg, IDC_FRAME_DIALOGBOX),
  203.               &rc);
  204.             MapWindowRect(NULL, hwndDlg, &rc);
  205.             InvalidateRect(hwndDlg, &rc, TRUE);
  206.           }
  207.         }
  208.         break;
  209.  
  210.     case IDOK :
  211.     case IDCANCEL :
  212.  
  213.         // Make sure we clean up the allocations made and properties
  214.         // set by ShowCategory in order to overcome the Windows bug.
  215.  
  216.         for (i = 0; i < sizeof(category) / sizeof(CATEGORY); i++)
  217.           RemoveCategoryTextProps(hwndDlg, category[i].nFirstID,
  218.             category[i].nLastID);
  219.  
  220.         EndDialog(hwndDlg, nID == IDOK);
  221.   }
  222. }
  223.