1/* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19#include "ControlPanel.h"
20#include "ConfigDialog.h"
21#include "ConfigPropertySheet.h"
22#include "resource.h"
23
24#include <DebugServices.h>
25
26
27#ifdef _DEBUG
28#define new DEBUG_NEW
29#undef THIS_FILE
30static char THIS_FILE[] = __FILE__;
31#endif
32
33
34static CCPApp theApp;
35
36
37//---------------------------------------------------------------------------------------------------------------------------
38//	GetControlPanelApp
39//---------------------------------------------------------------------------------------------------------------------------
40
41CCPApp*
42GetControlPanelApp()
43{
44	CCPApp * pApp = (CCPApp*) AfxGetApp();
45
46	check( pApp );
47	check( pApp->IsKindOf( RUNTIME_CLASS( CCPApp ) ) );
48
49	return pApp;
50}
51
52
53//---------------------------------------------------------------------------------------------------------------------------
54//	CPlApplet
55//---------------------------------------------------------------------------------------------------------------------------
56
57LONG APIENTRY
58CPlApplet(HWND hWndCPl, UINT uMsg, LONG lParam1, LONG lParam2)
59{
60	AFX_MANAGE_STATE(AfxGetStaticModuleState());
61
62	CCPApp * pApp = GetControlPanelApp();
63
64	return ( LONG ) pApp->OnCplMsg(hWndCPl, uMsg, lParam1, lParam2);
65}
66
67
68IMPLEMENT_DYNAMIC(CCPApplet, CCmdTarget);
69
70
71//---------------------------------------------------------------------------------------------------------------------------
72//	CCPApplet::CCPApplet
73//---------------------------------------------------------------------------------------------------------------------------
74
75CCPApplet::CCPApplet(UINT resourceId, UINT descId, CRuntimeClass * uiClass)
76:
77	m_resourceId(resourceId),
78	m_descId(descId),
79	m_uiClass(uiClass),
80	m_pageNumber(0)
81{
82	check( uiClass );
83	check( uiClass->IsDerivedFrom( RUNTIME_CLASS( CDialog ) ) ||
84	       uiClass->IsDerivedFrom( RUNTIME_CLASS( CPropertySheet ) ) );
85
86	m_name.LoadString(resourceId);
87}
88
89
90//---------------------------------------------------------------------------------------------------------------------------
91//	CCPApplet::~CCPApplet
92//---------------------------------------------------------------------------------------------------------------------------
93
94CCPApplet::~CCPApplet()
95{
96}
97
98
99//---------------------------------------------------------------------------------------------------------------------------
100//	CCPApplet::OnStartParms
101//---------------------------------------------------------------------------------------------------------------------------
102
103LRESULT
104CCPApplet::OnStartParms(CWnd * pParentWnd, LPCTSTR extra)
105{
106	DEBUG_UNUSED( pParentWnd );
107
108	if ( extra )
109	{
110		m_pageNumber = ::_ttoi( extra ) - 1;
111	}
112
113	return 0;
114}
115
116
117//---------------------------------------------------------------------------------------------------------------------------
118//	CCPApplet::OnRun
119//---------------------------------------------------------------------------------------------------------------------------
120
121LRESULT
122CCPApplet::OnRun(CWnd* pParentWnd)
123{
124	LRESULT		lResult = 1;
125	CWnd	*	pWnd;
126
127	InitCommonControls();
128
129	pWnd = (CWnd*) m_uiClass->CreateObject();
130
131	if ( pWnd )
132	{
133		lResult = ERROR_SUCCESS;
134
135		if ( pWnd->IsKindOf( RUNTIME_CLASS( CPropertySheet ) ) )
136		{
137			CPropertySheet * pSheet = (CPropertySheet*) pWnd;
138
139			pSheet->Construct(m_name, pParentWnd, m_pageNumber);
140
141			pSheet->DoModal();
142		}
143		else
144		{
145			check( pWnd->IsKindOf( RUNTIME_CLASS( CDialog ) ) );
146
147			CDialog * pDialog = (CDialog*) pWnd;
148
149      		pDialog->DoModal();
150    	}
151
152		delete pWnd;
153  	}
154
155	return lResult;
156}
157
158
159//---------------------------------------------------------------------------------------------------------------------------
160//	CCPApplet::OnInquire
161//---------------------------------------------------------------------------------------------------------------------------
162
163LRESULT
164CCPApplet::OnInquire(CPLINFO* pInfo)
165{
166	pInfo->idIcon = m_resourceId;
167	pInfo->idName = m_resourceId;
168	pInfo->idInfo = m_descId;
169	pInfo->lData  = reinterpret_cast<LONG>(this);
170
171	return 0;
172}
173
174
175//---------------------------------------------------------------------------------------------------------------------------
176//	CCPApplet::OnNewInquire
177//---------------------------------------------------------------------------------------------------------------------------
178
179LRESULT
180CCPApplet::OnNewInquire(NEWCPLINFO* pInfo)
181{
182	DEBUG_UNUSED( pInfo );
183
184	return 1;
185}
186
187
188//---------------------------------------------------------------------------------------------------------------------------
189//	CCPApplet::OnSelect
190//---------------------------------------------------------------------------------------------------------------------------
191
192LRESULT
193CCPApplet::OnSelect()
194{
195	return 0;
196}
197
198
199//---------------------------------------------------------------------------------------------------------------------------
200//	CCPApplet::OnStop
201//---------------------------------------------------------------------------------------------------------------------------
202
203LRESULT
204CCPApplet::OnStop()
205{
206	return 0;
207}
208
209
210IMPLEMENT_DYNAMIC(CCPApp, CWinApp);
211
212//---------------------------------------------------------------------------------------------------------------------------
213//	CCPApp::CCPApp
214//---------------------------------------------------------------------------------------------------------------------------
215
216CCPApp::CCPApp()
217{
218	debug_initialize( kDebugOutputTypeWindowsEventLog, "DNS-SD Control Panel", GetModuleHandle( NULL ) );
219	debug_set_property( kDebugPropertyTagPrintLevel, kDebugLevelInfo );
220}
221
222
223//---------------------------------------------------------------------------------------------------------------------------
224//	CCPApp::~CCPApp
225//---------------------------------------------------------------------------------------------------------------------------
226
227CCPApp::~CCPApp()
228{
229	while ( !m_applets.IsEmpty() )
230	{
231    	delete m_applets.RemoveHead();
232	}
233}
234
235
236//---------------------------------------------------------------------------------------------------------------------------
237//	CCPApp::AddApplet
238//---------------------------------------------------------------------------------------------------------------------------
239
240void
241CCPApp::AddApplet( CCPApplet * applet )
242{
243	check( applet );
244
245	m_applets.AddTail( applet );
246}
247
248
249//---------------------------------------------------------------------------------------------------------------------------
250//	CCPApp::OnInit
251//---------------------------------------------------------------------------------------------------------------------------
252
253LRESULT
254CCPApp::OnInit()
255{
256	CCPApplet * applet;
257
258	try
259	{
260		applet = new CCPApplet( IDR_APPLET, IDS_APPLET_DESCRIPTION, RUNTIME_CLASS( CConfigPropertySheet ) );
261	}
262	catch (...)
263	{
264		applet = NULL;
265	}
266
267	require_action( applet, exit, kNoMemoryErr );
268
269	AddApplet( applet );
270
271exit:
272
273	return m_applets.GetCount();
274}
275
276
277//---------------------------------------------------------------------------------------------------------------------------
278//	CCPApp::OnExit
279//---------------------------------------------------------------------------------------------------------------------------
280
281LRESULT
282CCPApp::OnExit()
283{
284  return 1;
285}
286
287
288//---------------------------------------------------------------------------------------------------------------------------
289//	CCPApp::OnCplMsg
290//---------------------------------------------------------------------------------------------------------------------------
291
292LRESULT
293CCPApp::OnCplMsg(HWND hWndCPl, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
294{
295	LRESULT lResult = 1;
296
297	switch ( uMsg )
298	{
299		case CPL_INIT:
300		{
301			lResult = OnInit();
302		}
303		break;
304
305		case CPL_EXIT:
306		{
307			lResult = OnExit();
308		}
309		break;
310
311		case CPL_GETCOUNT:
312		{
313    		lResult = m_applets.GetCount();
314  		}
315		break;
316
317		default:
318  		{
319    		POSITION pos = m_applets.FindIndex( lParam1 );
320			check( pos );
321
322			CCPApplet * applet = m_applets.GetAt( pos );
323			check( applet );
324
325    		switch (uMsg)
326    		{
327      			case CPL_INQUIRE:
328      			{
329					LPCPLINFO pInfo = reinterpret_cast<LPCPLINFO>(lParam2);
330        			lResult = applet->OnInquire(pInfo);
331				}
332        		break;
333
334				case CPL_NEWINQUIRE:
335      			{
336        			LPNEWCPLINFO pInfo = reinterpret_cast<LPNEWCPLINFO>(lParam2);
337					lResult = applet->OnNewInquire(pInfo);
338				}
339        		break;
340
341				case CPL_STARTWPARMS:
342      			{
343        			CWnd * pParentWnd = CWnd::FromHandle(hWndCPl);
344        			LPCTSTR lpszExtra = reinterpret_cast<LPCTSTR>(lParam2);
345        			lResult = applet->OnStartParms(pParentWnd, lpszExtra);
346				}
347				break;
348
349				case CPL_DBLCLK:
350				{
351        			CWnd* pParentWnd = CWnd::FromHandle(hWndCPl);
352        			lResult = applet->OnRun(pParentWnd);
353				}
354        		break;
355
356				case CPL_SELECT:
357				{
358        			lResult = applet->OnSelect();
359				}
360				break;
361
362				case CPL_STOP:
363				{
364					lResult = applet->OnStop();
365				}
366				break;
367
368				default:
369				{
370					// TRACE(_T("Warning, Received an unknown control panel message:%d\n"), uMsg);
371					lResult = 1;
372				}
373				break;
374    		}
375  		}
376		break;
377	}
378
379	return lResult;
380}
381