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