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