home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / prefs / nsdlg / src / cdialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.9 KB  |  359 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "pch.h"
  20. #ifndef _WIN32
  21. #include <stdlib.h>
  22. #endif
  23. #include <assert.h>
  24. #include "cdialog.h"
  25.  
  26. // This is the window procedure that we specified when we subclassed the
  27. // dialog. This routine is called to process messages sent to the dialog,
  28. // and is called before DefDlgproc is called
  29. LRESULT CALLBACK
  30. WindowFunc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  31. {
  32.     CDialog    *pDialog;
  33.     LRESULT     result;
  34.  
  35.     // Get the "this" pointer for the dialog, which we saved away in
  36.     // the dialog specific user data
  37.     pDialog = (CDialog*)GetWindowLong(hwndDlg, DWL_USER);
  38.     assert(pDialog);
  39.     assert(pDialog->m_hwndDlg == hwndDlg);
  40.  
  41.     // Dispatch the message to the dialog object
  42.     result = pDialog->WindowProc(uMsg, wParam, lParam);
  43.  
  44.     if (uMsg == WM_DESTROY) {
  45.         // Stop subclassing the window
  46.         SubclassWindow(hwndDlg, pDialog->m_wpOriginalProc);
  47.         pDialog->m_wpOriginalProc = NULL;
  48.     }
  49.  
  50.     return result;
  51. }
  52.  
  53. // Callback function for processing messages sent to a dialog box.
  54. // The dialog box function is called by the Windows dialog box
  55. // window procedure DefDlgProc
  56. BOOL CALLBACK
  57. DialogFunc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  58. {
  59.     if (uMsg == WM_INITDIALOG) {
  60.         CDialog    *pDialog;
  61.  
  62.         // The lParam is the "this" pointer
  63.         pDialog = (CDialog*)lParam;
  64.         pDialog->m_hwndDlg = hwndDlg;
  65.  
  66.         // Save the "this" pointer in dialog specific user data
  67.         SetWindowLong(hwndDlg, DWL_USER, (LONG)pDialog);
  68.  
  69.         // Subclass the window
  70.         assert(pDialog->m_wpOriginalProc == NULL);
  71.         pDialog->m_wpOriginalProc = SubclassWindow(hwndDlg, WindowFunc);
  72.         assert(pDialog->m_wpOriginalProc);
  73.  
  74.         // Let the dialog handle WM_INITDIALOG
  75.         return pDialog->InitDialog();
  76.     }
  77.  
  78.     // WM_INITDIALOG is the only message we handle here. All others are
  79.     // handled in the window procedure we passed in when we subclassed
  80.     // the window
  81.     return FALSE;
  82. }
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CDialog implementation
  86.  
  87. CDialog::CDialog(HINSTANCE hInstance, UINT nTemplateID)
  88. {
  89.     m_hInstance = hInstance;
  90.     m_nTemplateID = nTemplateID;
  91.     m_hwndDlg = NULL;
  92.     m_wpOriginalProc = NULL;
  93. }
  94.  
  95. BOOL
  96. CDialog::Create(HWND hwndOwner)
  97. {
  98.     assert(m_hwndDlg == NULL);
  99.     return CreateDialogParam(m_hInstance, MAKEINTRESOURCE(m_nTemplateID), hwndOwner,
  100.         (DLGPROC)DialogFunc, (LPARAM)this) != NULL;
  101. }
  102.  
  103. int
  104. CDialog::DoModal(HWND hwndOwner)
  105. {
  106.     int    nResult;
  107.  
  108.     assert(m_hwndDlg == NULL);
  109.     nResult = DialogBoxParam(m_hInstance, MAKEINTRESOURCE(m_nTemplateID), hwndOwner,
  110.         (DLGPROC)DialogFunc, (LPARAM)this);
  111.     m_hwndDlg = NULL;
  112.     return nResult;
  113. }
  114.  
  115. BOOL
  116. CDialog::DestroyWindow()
  117. {
  118.     assert(m_hwndDlg);
  119.     if (::DestroyWindow(m_hwndDlg)) {
  120.         m_hwndDlg = NULL;
  121.         return TRUE;
  122.     }
  123.  
  124.     return FALSE;
  125. }
  126.  
  127. BOOL
  128. CDialog::InitDialog()
  129. {
  130.     // Transfer data to the dialog
  131.     DoTransfer(FALSE);
  132.     return TRUE;
  133. }
  134.  
  135. BOOL
  136. CDialog::OnCommand(int id, HWND hwndCtl, UINT notifyCode)
  137. {
  138.     if (id == IDOK) {
  139.         OnOK();
  140.         return TRUE;
  141.  
  142.     } else if (id == IDCANCEL) {
  143.         OnCancel();
  144.         return TRUE;
  145.     }
  146.  
  147.     return FALSE;
  148. }
  149.  
  150. void
  151. CDialog::OnOK()
  152. {
  153.     if (DoTransfer(TRUE))
  154.         EndDialog(m_hwndDlg, IDOK);
  155. }
  156.  
  157. void
  158. CDialog::OnCancel()
  159. {
  160.     EndDialog(m_hwndDlg, IDCANCEL);
  161. }
  162.  
  163. void
  164. CDialog::EnableDlgItem(UINT nCtlID, BOOL bEnable)
  165. {
  166.     HWND hwndCtl = GetDlgItem(m_hwndDlg, nCtlID);
  167.  
  168.     assert(hwndCtl);
  169.     EnableWindow(hwndCtl, bEnable);
  170. }
  171.  
  172. // Data transfer for radio buttons. The ID must be the first in a group of
  173. // auto radio buttons
  174. void
  175. CDialog::RadioButtonTransfer(int nIDButton, int &value, BOOL bSaveAndValidate)
  176. {
  177.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDButton);
  178.  
  179.     assert(GetWindowStyle(hwndCtl) & WS_GROUP);
  180.     assert(SendMessage(hwndCtl, WM_GETDLGCODE, 0, 0) & DLGC_RADIOBUTTON);
  181.     
  182.     if (bSaveAndValidate)
  183.         value = -1;  // value if no radio button is set
  184.  
  185.     // Walk all the controls in the group. Stop when we get to the start of another group
  186.     // or there are no controls left
  187.     int iButton = 0;
  188.     do {
  189.         if (SendMessage(hwndCtl, WM_GETDLGCODE, 0, 0) & DLGC_RADIOBUTTON) {
  190.             // Control is a radio button
  191.             if (bSaveAndValidate) {
  192.                 if (Button_GetCheck(hwndCtl) != 0) {
  193.                     assert(value == -1);  // only one button in the group should be set
  194.                     value = iButton;
  195.                 }
  196.             } else {
  197.                 // Set the button state
  198.                 Button_SetCheck(hwndCtl, iButton == value);
  199.             }
  200.  
  201.             iButton++;
  202. #ifdef _DEBUG
  203.         } else {
  204.             OutputDebugString("Warning: skipping non-radio button in group.\n");
  205. #endif
  206.         }
  207.  
  208.         hwndCtl = GetNextSibling(hwndCtl);
  209.  
  210.     } while (hwndCtl && !(GetWindowStyle(hwndCtl) & WS_GROUP));
  211. }
  212.  
  213. // Data transfer for a check box
  214. void
  215. CDialog::CheckBoxTransfer(int nIDButton, BOOL &value, BOOL bSaveAndValidate)
  216. {
  217.     if (bSaveAndValidate)
  218.         value = IsDlgButtonChecked(m_hwndDlg, nIDButton);
  219.     else
  220.         CheckDlgButton(m_hwndDlg, nIDButton, value);
  221. }
  222.  
  223. // Data transfer for a edit field. Transfers the value as a CString
  224. void
  225. CDialog::EditFieldTransfer(int nIDEdit, CString &str, BOOL bSaveAndValidate)
  226. {
  227.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDEdit);
  228.  
  229.     assert(hwndCtl);
  230.     if (bSaveAndValidate)
  231.         str.GetWindowText(hwndCtl);
  232.     else
  233.         Edit_SetText(hwndCtl, (const char *)str);
  234. }
  235.  
  236. // Data transfer for a edit field. Transfers the value as an integer
  237. void
  238. CDialog::EditFieldTransfer(int nIDEdit, int &value, BOOL bSaveAndValidate)
  239. {
  240.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDEdit);
  241.     char    szBuf[32];
  242.  
  243.     assert(hwndCtl);
  244.     if (bSaveAndValidate) {
  245.         Edit_GetText(hwndCtl, szBuf, sizeof(szBuf));
  246.         value = atoi(szBuf);
  247.  
  248.     } else {
  249.         wsprintf(szBuf, "%i", value);
  250.         Edit_SetText(hwndCtl, szBuf);
  251.     }
  252. }
  253.  
  254. // Data transfer for a edit field. Transfers the value as an unsigned integer
  255. void
  256. CDialog::EditFieldTransfer(int nIDEdit, UINT &value, BOOL bSaveAndValidate)
  257. {
  258.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDEdit);
  259.     char    szBuf[32];
  260.  
  261.     assert(hwndCtl);
  262.     if (bSaveAndValidate) {
  263.         Edit_GetText(hwndCtl, szBuf, sizeof(szBuf));
  264.         value = (UINT)strtoul(szBuf, NULL, 10);
  265.  
  266.     } else {
  267.         wsprintf(szBuf, "%u", value);
  268.         Edit_SetText(hwndCtl, szBuf);
  269.     }
  270. }
  271.  
  272. void
  273. CDialog::ListBoxTransfer(int nIDList, int &index, BOOL bSaveAndValidate)
  274. {
  275.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDList);
  276.  
  277.     if (bSaveAndValidate)
  278.         index = ListBox_GetCurSel(hwndCtl);
  279.     else
  280.         ListBox_SetCurSel(hwndCtl, index);
  281. }
  282.  
  283. void
  284. CDialog::ListBoxTransfer(int nIDList, CString &str, BOOL bSaveAndValidate)
  285. {
  286.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDList);
  287.  
  288.     if (bSaveAndValidate) {
  289.         int    nIndex = ListBox_GetCurSel(hwndCtl);
  290.  
  291.         if (nIndex == -1)
  292.             str.Empty();
  293.         else
  294.             ListBox_GetText(hwndCtl, nIndex,
  295.                 str.BufferSetLength(ListBox_GetTextLen(hwndCtl, nIndex)));
  296.     
  297.     } else {
  298.         // Set the current selection
  299.         if (ListBox_SelectString(hwndCtl, -1, (LPCSTR)str) == LB_ERR) {
  300. #ifdef _DEBUG
  301.             OutputDebugString("Warning: no listbox item selected.\n");
  302. #endif
  303.         }
  304.     }
  305. }
  306.  
  307. void
  308. CDialog::ComboBoxTransfer(int nIDCombo, int &index, BOOL bSaveAndValidate)
  309. {
  310.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDCombo);
  311.  
  312.     if (bSaveAndValidate)
  313.         index = ComboBox_GetCurSel(hwndCtl);
  314.     else
  315.         ComboBox_SetCurSel(hwndCtl, index);
  316. }
  317.  
  318. void
  319. CDialog::ComboBoxTransfer(int nIDCombo, CString &str, BOOL bSaveAndValidate)
  320. {
  321.     HWND    hwndCtl = GetDlgItem(m_hwndDlg, nIDCombo);
  322.  
  323.     if (bSaveAndValidate) {
  324.         int nLen = ComboBox_GetTextLength(hwndCtl);
  325.  
  326.         if (nLen == -1) {
  327.             // GetWindowTextLength() doesn't work for drop down combo boxes
  328.             // on Win 3.1, so assume a max of 255 characters
  329.             nLen = 255;
  330.         }
  331.         
  332.         ComboBox_GetText(hwndCtl, str.BufferSetLength(nLen), nLen + 1);
  333.     
  334.     } else if (ComboBox_SelectString(hwndCtl, -1, (LPARAM)(LPCSTR)str) == CB_ERR) {
  335.         SetWindowText(hwndCtl, (LPCSTR)str);  // just set the edit text
  336.     }
  337. }
  338.  
  339. LRESULT
  340. CDialog::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  341. {
  342.     switch (uMsg) {
  343.         case WM_COMMAND:
  344.             if (OnCommand(GET_WM_COMMAND_ID(wParam, lParam),
  345.                           GET_WM_COMMAND_HWND(wParam, lParam),
  346.                           GET_WM_COMMAND_CMD(wParam, lParam)))
  347.                 return 0;  // command was handled
  348.             break;
  349.  
  350.         default:
  351.             break;
  352.     }
  353.  
  354.     // Call the original window procedure
  355.     assert(m_wpOriginalProc);
  356.     return CallWindowProc((FARPROC)m_wpOriginalProc, m_hwndDlg, uMsg, wParam, lParam);
  357. }
  358.  
  359.