changed from control.cpp to control.c, changed \ to / in control.c
[reactos.git] / reactos / lib / cpl / control / control.c
1 /*
2 * ReactOS
3 * Copyright (C) 2004 ReactOS Team
4 * Copyright (C) 2004 GkWare e.K.
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 /* $Id: control.c,v 1.1 2004/06/19 00:11:44 kuehng Exp $
21 *
22 * PROJECT: ReactOS System Control Panel
23 * FILE: lib/cpl/system/control.cpp
24 * PURPOSE: ReactOS System Control Panel
25 * PROGRAMMER: Gero Kuehn (reactos.filter@gkware.com)
26 * UPDATE HISTORY:
27 * 06-13-2004 Created
28 */
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <tchar.h>
33 #include <windows.h>
34
35 #ifdef _MSC_VER
36 #include <commctrl.h>
37 #include <cpl.h>
38 #endif
39
40 #include "resource.h"
41
42 //#define CONTROL_DEBUG_ENABLE
43
44 #ifdef CONTROL_DEBUG_ENABLE
45 #define CTL_DEBUG(x) dbgprint x
46 #else
47 #define CTL_DEBUG(x)
48 #endif
49
50 HINSTANCE hInst;
51 HWND hMainWnd;
52
53 void dbgprint(TCHAR *format,...)
54 {
55 TCHAR buf[1000];
56 va_list va;
57 va_start(va,format);
58 _vstprintf(buf,format,va);
59 OutputDebugString(buf);
60 va_end(va);
61 }
62
63 #define MYWNDCLASS _T("CTLPANELCLASS")
64 HWND hListView;
65
66 typedef LONG (CALLBACK *CPLAPPLETFUNC)(HWND hwndCPL, UINT uMsg, LPARAM lParam1, LPARAM lParam2);
67
68 typedef struct CPLLISTENTRY{
69 TCHAR pszPath[MAX_PATH];
70 HMODULE hDLL;
71 CPLAPPLETFUNC pFunc;
72 CPLINFO CPLInfo;
73 int nIndex;
74 } CPLLISTENTRY;
75
76 void PopulateCPLList(HWND hLisCtrl)
77 {
78 WIN32_FIND_DATA fd;
79 HANDLE hFind;
80 TCHAR pszSearchPath[MAX_PATH];
81 HIMAGELIST hImgListSmall;
82 HIMAGELIST hImgListLarge;
83 GetSystemDirectory(pszSearchPath,MAX_PATH);
84 _tcscat(pszSearchPath,_T("\\*.cpl"));
85 hFind = FindFirstFile(pszSearchPath,&fd);
86 hImgListSmall = ImageList_Create(16,16,ILC_COLOR,256,1000);
87 hImgListLarge = ImageList_Create(32,32,ILC_COLOR,256,1000);
88 while(hFind != INVALID_HANDLE_VALUE)
89 {
90 CPLLISTENTRY *pEntry;
91 CTL_DEBUG((_T("Found %s\r\n"),fd.cFileName));
92 pEntry = (CPLLISTENTRY*)malloc(sizeof(CPLLISTENTRY));
93 if(!pEntry)
94 break;
95 _tcscpy(pEntry->pszPath,pszSearchPath);
96 *_tcsrchr(pEntry->pszPath,'\\')=0;
97 _tcscat(pEntry->pszPath,_T("\\"));
98 _tcscat(pEntry->pszPath,fd.cFileName);
99
100 pEntry->hDLL = LoadLibrary(pEntry->pszPath);
101 CTL_DEBUG((_T("Handle %08X\r\n"),pEntry->hDLL));
102 pEntry->pFunc = (CPLAPPLETFUNC)GetProcAddress(pEntry->hDLL,_T("CPlApplet"));
103 CTL_DEBUG((_T("CPLFunc %08X\r\n"),pEntry->pFunc));
104 if(pEntry->pFunc && pEntry->pFunc(hLisCtrl,CPL_INIT,0,0))
105 {
106 int i;
107 for(i=0;i<pEntry->pFunc(hLisCtrl,CPL_GETCOUNT,0,0);i++)
108 {
109 HICON hIcon;
110 TCHAR Name[MAX_PATH];
111 int index;
112 pEntry->pFunc(hLisCtrl,CPL_INQUIRE,0,(LPARAM)&pEntry->CPLInfo);
113
114 hIcon = LoadIcon(pEntry->hDLL,MAKEINTRESOURCE(pEntry->CPLInfo.idIcon));
115 index = ImageList_AddIcon(hImgListSmall,hIcon);
116 ImageList_AddIcon(hImgListLarge,hIcon);
117
118
119 LoadString(pEntry->hDLL,pEntry->CPLInfo.idName,Name,MAX_PATH);
120 if(_tcslen(Name))
121 {
122
123 LV_ITEM lvi;
124
125 memset(&lvi,0x00,sizeof(lvi));
126 lvi.mask=LVIF_TEXT|LVIF_PARAM|LVIF_STATE|LVIF_IMAGE;
127 lvi.pszText = Name;
128 lvi.state=0;
129 lvi.iImage=index;
130 lvi.lParam = (LPARAM)pEntry;
131 pEntry->nIndex = ListView_InsertItem(hLisCtrl,&lvi);
132
133 LoadString(pEntry->hDLL,pEntry->CPLInfo.idInfo,Name,MAX_PATH);
134 ListView_SetItemText(hLisCtrl,pEntry->nIndex,1,Name);
135
136 ListView_SetImageList(hLisCtrl,hImgListSmall,LVSIL_SMALL);
137 ListView_SetImageList(hLisCtrl,hImgListLarge,LVSIL_NORMAL);
138 }
139 }
140 }
141
142 if(!FindNextFile(hFind,&fd))
143 hFind = INVALID_HANDLE_VALUE;
144 }
145 }
146
147 LRESULT CALLBACK MyWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
148 {
149 switch(uMsg)
150 {
151 case WM_CREATE:
152 {
153 RECT rect;
154 LV_COLUMN column;
155 GetClientRect(hWnd,&rect);
156 hListView = CreateWindow(WC_LISTVIEW,_T(""),LVS_REPORT | LVS_ALIGNLEFT | LVS_AUTOARRANGE | LVS_SINGLESEL | WS_VISIBLE | WS_CHILD|WS_BORDER|WS_TABSTOP,0,0,rect.right ,rect.bottom,hWnd,NULL,hInst,0);
157 CTL_DEBUG((_T("Listview Window %08X\r\n"),hListView));
158
159 memset(&column,0x00,sizeof(column));
160 column.mask=LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM|LVCF_TEXT;
161 column.fmt=LVCFMT_LEFT;
162 column.cx = 200;
163 column.iSubItem = 0;
164 column.pszText = _T("Name");
165 ListView_InsertColumn(hListView,0,&column);
166 column.cx = 200;
167 column.iSubItem = 1;
168 column.pszText = _T("Comment");
169 ListView_InsertColumn(hListView,1,&column);
170 PopulateCPLList(hListView);
171 ListView_SetColumnWidth(hListView,2,LVSCW_AUTOSIZE_USEHEADER);
172 ListView_Update(hListView,0);
173 }
174 break;
175 case WM_DESTROY:
176 PostQuitMessage(0);
177 break;
178 case WM_SIZE:
179 {
180 RECT rect;
181 GetClientRect(hWnd,&rect);
182 MoveWindow(hListView,0,0,rect.right,rect.bottom,TRUE);
183 }
184 break;
185 case WM_NOTIFY:
186 {
187 NMHDR *phdr;
188 phdr = (NMHDR*)lParam;
189 switch(phdr->code)
190 {
191 case NM_DBLCLK:
192 {
193 int nSelect;
194 LV_ITEM lvi;
195 CPLLISTENTRY *pEntry;
196 nSelect=SendMessage(hListView,LVM_GETNEXTITEM,(WPARAM)-1,LVNI_FOCUSED);
197
198 if(nSelect==-1) // no items
199 {
200 MessageBox(hWnd,_T("No Items in ListView"),_T("Error"),MB_OK|MB_ICONINFORMATION);
201 break;
202 }
203 CTL_DEBUG((_T("Select %d\r\n"),nSelect));
204 memset(&lvi,0x00,sizeof(lvi));
205 lvi.iItem = nSelect;
206 lvi.mask = LVIF_PARAM;
207 ListView_GetItem(hListView,&lvi);
208 pEntry = (CPLLISTENTRY *)lvi.lParam;
209 CTL_DEBUG((_T("Listview DblClk Entry %08X\r\n"),pEntry));
210 if(pEntry) {
211 CTL_DEBUG((_T("Listview DblClk Entry Func %08X\r\n"),pEntry->pFunc));
212 }
213 if(pEntry && pEntry->pFunc)
214 pEntry->pFunc(hListView,CPL_DBLCLK,pEntry->CPLInfo.lData,0);
215 }
216 }
217 }
218 break;
219 case WM_COMMAND:
220 switch(LOWORD(wParam))
221 {
222 case CM_LARGEICONS:
223 SetWindowLong(hListView,GWL_STYLE,LVS_ICON | LVS_ALIGNLEFT | LVS_AUTOARRANGE | LVS_SINGLESEL | WS_VISIBLE | WS_CHILD|WS_BORDER|WS_TABSTOP);
224 break;
225 case CM_SMALLICONS:
226 SetWindowLong(hListView,GWL_STYLE,LVS_SMALLICON | LVS_ALIGNLEFT | LVS_AUTOARRANGE | LVS_SINGLESEL | WS_VISIBLE | WS_CHILD|WS_BORDER|WS_TABSTOP);
227 break;
228 case CM_LIST:
229 SetWindowLong(hListView,GWL_STYLE,LVS_LIST | LVS_ALIGNLEFT | LVS_AUTOARRANGE | LVS_SINGLESEL | WS_VISIBLE | WS_CHILD|WS_BORDER|WS_TABSTOP);
230 break;
231 case CM_DETAILS:
232 SetWindowLong(hListView,GWL_STYLE,LVS_REPORT | LVS_ALIGNLEFT | LVS_AUTOARRANGE | LVS_SINGLESEL | WS_VISIBLE | WS_CHILD|WS_BORDER|WS_TABSTOP);
233 break;
234 case CM_CLOSE:
235 DestroyWindow(hWnd);
236 break;
237 case CM_ABOUT:
238 MessageBox(hWnd,_T("Simple Control Panel (not Shell-namespace based)\rCopyright 2004 GkWare e.K.\rhttp://www.gkware.com\rReleased under the GPL"),_T("About the Control Panel"),MB_OK | MB_ICONINFORMATION);
239 break;
240 }
241 break;
242 default:
243 return DefWindowProc(hWnd,uMsg,wParam,lParam);
244 }
245 return 0;
246 }
247
248 #ifdef _MSVC
249 int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,WCHAR *lpCmdLine,int nCmdShow)
250 #else
251 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,char *lpCmdLine,int nCmdShow)
252 #endif
253 {
254 MSG msg;
255 WNDCLASS wc;
256 hInst = hInstance;
257 CTL_DEBUG((_T("My Control Panel\r\n")));
258 memset(&wc,0x00,sizeof(wc));
259 wc.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_MAINICON));
260 wc.lpszClassName = MYWNDCLASS;
261 wc.lpszMenuName = _T("MAINMENU");
262 wc.lpfnWndProc = MyWindowProc;
263 RegisterClass(&wc);
264 InitCommonControls();
265 hMainWnd = CreateWindow(MYWNDCLASS,_T("Control Panel"),WS_OVERLAPPEDWINDOW,300,300,500,300,NULL,LoadMenu(hInst,MAKEINTRESOURCE(IDM_MAINMENU)),hInst,0);
266 if(!hMainWnd) {
267 CTL_DEBUG((_T("Unable to create window\r\n")));
268 return -1;
269 }
270 ShowWindow(hMainWnd,nCmdShow);
271 while(GetMessage(&msg,0,0,0))
272 {
273 TranslateMessage(&msg);
274 DispatchMessage(&msg);
275 }
276
277 return 0;
278 }