remove trailing whitespace at end of lines
[reactos.git] / rosapps / templates / dialog / dialog.c
1 /*
2 * ReactOS Standard Dialog Application Template
3 *
4 * dialog.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define WIN32_LEAN_AND_MEAN
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <tchar.h>
27 #include <assert.h>
28 #include "resource.h"
29 #include "trace.h"
30
31
32 #define _USE_MSG_PUMP_
33
34 typedef struct tagDialogData {
35 HWND hWnd;
36 LONG lData;
37 } DialogData;
38
39 HINSTANCE hInst;
40 HWND hTabWnd;
41 HWND hPage1;
42 HWND hPage2;
43 HWND hPage3;
44
45 LRESULT CreateMemoryDialog(HINSTANCE, HWND hwndOwner, LPSTR lpszMessage);
46 LRESULT CALLBACK PageWndProc1(HWND, UINT, WPARAM, LPARAM);
47 LRESULT CALLBACK PageWndProc2(HWND, UINT, WPARAM, LPARAM);
48 LRESULT CALLBACK PageWndProc3(HWND, UINT, WPARAM, LPARAM);
49
50 ////////////////////////////////////////////////////////////////////////////////
51
52 static BOOL OnCreate(HWND hWnd, LONG lData)
53 {
54 TCHAR szTemp[256];
55 TCITEM item;
56
57 // Initialize the Windows Common Controls DLL
58 #ifdef __GNUC__
59 InitCommonControls();
60 #else
61 INITCOMMONCONTROLSEX ex = { sizeof(INITCOMMONCONTROLSEX), ICC_TAB_CLASSES };
62 InitCommonControlsEx(&ex);
63 #endif
64
65 // Create tab pages
66 hTabWnd = GetDlgItem(hWnd, IDC_TAB);
67 hPage1 = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PAGE1), hWnd, (DLGPROC)PageWndProc1);
68 hPage2 = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PAGE2), hWnd, (DLGPROC)PageWndProc2);
69 hPage3 = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PAGE3), hWnd, (DLGPROC)PageWndProc3);
70
71 // Insert tabs
72 _tcscpy(szTemp, _T("Page One"));
73 memset(&item, 0, sizeof(TCITEM));
74 item.mask = TCIF_TEXT;
75 item.pszText = szTemp;
76 TabCtrl_InsertItem(hTabWnd, 0, &item);
77 _tcscpy(szTemp, _T("Page Two"));
78 memset(&item, 0, sizeof(TCITEM));
79 item.mask = TCIF_TEXT;
80 item.pszText = szTemp;
81 TabCtrl_InsertItem(hTabWnd, 1, &item);
82 _tcscpy(szTemp, _T("Page Three"));
83 memset(&item, 0, sizeof(TCITEM));
84 item.mask = TCIF_TEXT;
85 item.pszText = szTemp;
86 TabCtrl_InsertItem(hTabWnd, 2, &item);
87
88 ShowWindow(hPage1, SW_SHOW);
89
90 return TRUE;
91 }
92
93 void OnTabWndSelChange(void)
94 {
95 switch (TabCtrl_GetCurSel(hTabWnd)) {
96 case 0:
97 ShowWindow(hPage1, SW_SHOW);
98 ShowWindow(hPage2, SW_HIDE);
99 ShowWindow(hPage3, SW_HIDE);
100 BringWindowToTop(hPage1);
101 SetFocus(hTabWnd);
102 break;
103 case 1:
104 ShowWindow(hPage1, SW_HIDE);
105 ShowWindow(hPage2, SW_SHOW);
106 ShowWindow(hPage3, SW_HIDE);
107 BringWindowToTop(hPage2);
108 SetFocus(hTabWnd);
109 break;
110 case 2:
111 ShowWindow(hPage1, SW_HIDE);
112 ShowWindow(hPage2, SW_HIDE);
113 ShowWindow(hPage3, SW_SHOW);
114 BringWindowToTop(hPage3);
115 SetFocus(hTabWnd);
116 break;
117 }
118 }
119
120 LRESULT CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
121 {
122 int idctrl;
123 LPNMHDR pnmh;
124 LPCREATESTRUCT lpCS;
125 LONG nThisApp = 0;
126 DialogData* pData = (DialogData*)GetWindowLong(hDlg, DWL_USER);
127 if (pData) nThisApp = pData->lData;
128
129 switch (message) {
130
131 case WM_CREATE:
132 lpCS = (LPCREATESTRUCT)lParam;
133 assert(lpCS);
134 lpCS->style &= ~WS_POPUP;
135 break;
136
137 case WM_INITDIALOG:
138 pData = (DialogData*)lParam;
139 SetWindowLong(hDlg, DWL_USER, (LONG)pData);
140 if (pData) nThisApp = pData->lData;
141 return OnCreate(hDlg, nThisApp);
142
143 #ifdef _USE_MSG_PUMP_
144 case WM_DESTROY:
145 if (pData && (pData->hWnd == hDlg)) {
146 pData->hWnd = 0;
147 PostQuitMessage(0);
148 }
149 break;
150 case WM_COMMAND:
151 switch (LOWORD(wParam)) {
152 case IDOK:
153 //MessageBox(NULL, _T("Good-bye"), _T("dialog"), MB_ICONEXCLAMATION);
154 CreateMemoryDialog(hInst, GetDesktopWindow(), "DisplayMyMessage");
155 case IDCANCEL:
156 if (pData && (pData->hWnd == hDlg)) {
157 DestroyWindow(pData->hWnd);
158 }
159 break;
160 }
161 break;
162 #else
163 case WM_COMMAND:
164 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
165 EndDialog(hDlg, LOWORD(wParam));
166 return TRUE;
167 }
168 #endif
169
170 case WM_NOTIFY:
171 idctrl = (int)wParam;
172 pnmh = (LPNMHDR)lParam;
173 if ((pnmh->hwndFrom == hTabWnd) &&
174 (pnmh->idFrom == IDC_TAB) &&
175 (pnmh->code == TCN_SELCHANGE))
176 {
177 OnTabWndSelChange();
178 }
179 break;
180 }
181 return 0;
182 }
183
184
185 int APIENTRY WinMain(HINSTANCE hInstance,
186 HINSTANCE hPrevInstance,
187 LPSTR lpCmdLine,
188 int nCmdShow)
189 {
190 #ifdef _USE_MSG_PUMP_
191 MSG msg;
192 HACCEL hAccel;
193 DialogData instData = { NULL, 34 };
194
195 hInst = hInstance;
196 instData.hWnd = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_TABBED_DIALOG), NULL, (DLGPROC)DlgProc, (LPARAM)&instData);
197 ShowWindow(instData.hWnd, SW_SHOW);
198 hAccel = LoadAccelerators(hInst, (LPCTSTR)IDR_ACCELERATOR);
199 while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
200 if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) {
201 TranslateMessage(&msg);
202 DispatchMessage(&msg);
203 }
204 }
205 #else
206 hInst = hInstance;
207 DialogBox(hInst, (LPCTSTR)IDD_TABBED_DIALOG, NULL, (DLGPROC)DlgProc);
208 //CreateMemoryDialog(hInst, GetDesktopWindow(), "CreateMemoryDialog");
209 #endif
210 return 0;
211 }