- Support control panel applications that provide more than one control panel.
[reactos.git] / reactos / lib / cpl / control / control.c
index 3b35fd6..9fc6515 100644 (file)
@@ -17,7 +17,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: control.c,v 1.3 2004/06/29 12:03:56 ekohl Exp $
+/* $Id: control.c,v 1.6 2004/10/29 12:27:58 ekohl Exp $
  *
  * PROJECT:         ReactOS System Control Panel
  * FILE:            lib/cpl/system/control.c
  * UPDATE HISTORY:
  *      06-13-2004  Created
  */
+#include <windows.h>
+#include <commctrl.h>
+#include <cpl.h>
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <tchar.h>
-#include <windows.h>
-
-#ifdef _MSC_VER
-#include <commctrl.h>
-#include <cpl.h>
-#endif
 
 #include "resource.h"
 
@@ -47,7 +45,6 @@
 #define CTL_DEBUG(x)
 #endif
 
-
 #define MYWNDCLASS _T("CTLPANELCLASS")
 
 typedef LONG (CALLBACK *CPLAPPLETFUNC)(HWND hwndCPL, UINT uMsg, LPARAM lParam1, LPARAM lParam2);
@@ -55,173 +52,221 @@ typedef LONG (CALLBACK *CPLAPPLETFUNC)(HWND hwndCPL, UINT uMsg, LPARAM lParam1,
 typedef struct CPLLISTENTRY
 {
        TCHAR pszPath[MAX_PATH];
-       HMODULE hDLL;
+       HMODULE hDll;
        CPLAPPLETFUNC pFunc;
-       CPLINFO CPLInfo;
+       CPLINFO CplInfo;
        int nIndex;
-} CPLLISTENTRY;
+} CPLLISTENTRY, *PCPLLISTENTRY;
 
 
 HWND hListView;
 HINSTANCE hInst;
 HWND hMainWnd;
+DEVMODE pDevMode;
 
-void dbgprint(TCHAR *format,...)
+VOID dbgprint(TCHAR *format,...)
 {
        TCHAR buf[1000];
        va_list va;
+
        va_start(va,format);
        _vstprintf(buf,format,va);
        OutputDebugString(buf);
        va_end(va);
 }
 
-void PopulateCPLList(HWND hLisCtrl)
+VOID PopulateCPLList(HWND hLisCtrl)
 {
        WIN32_FIND_DATA fd;
        HANDLE hFind;
        TCHAR pszSearchPath[MAX_PATH];
        HIMAGELIST hImgListSmall;
        HIMAGELIST hImgListLarge;
+       int ColorDepth;
+       HMODULE hDll;
+       CPLAPPLETFUNC pFunc;
+       TCHAR pszPath[MAX_PATH];
+
+       /* Icon drawing mode */
+       pDevMode.dmSize = sizeof(DEVMODE);
+       pDevMode.dmDriverExtra = 0;
+
+       EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&pDevMode);
+       switch (pDevMode.dmBitsPerPel)
+       {
+               case 32: ColorDepth = ILC_COLOR32; break;
+               case 24: ColorDepth = ILC_COLOR24; break;
+               case 16: ColorDepth = ILC_COLOR16; break;
+               case  8: ColorDepth = ILC_COLOR8;  break;
+               case  4: ColorDepth = ILC_COLOR4;  break;
+               default: ColorDepth = ILC_COLOR;  break;
+       }
+
+       hImgListSmall = ImageList_Create(16,16,ColorDepth | ILC_MASK,5,5);
+       hImgListLarge = ImageList_Create(32,32,ColorDepth | ILC_MASK,5,5);
+
        GetSystemDirectory(pszSearchPath,MAX_PATH);
        _tcscat(pszSearchPath,_T("\\*.cpl"));
+
        hFind = FindFirstFile(pszSearchPath,&fd);
-       hImgListSmall = ImageList_Create(16,16,ILC_COLOR | ILC_MASK,256,1000);
-       hImgListLarge = ImageList_Create(32,32,ILC_COLOR | ILC_MASK,256,1000);
-       while(hFind != INVALID_HANDLE_VALUE)
+       while (hFind != INVALID_HANDLE_VALUE)
        {
-               CPLLISTENTRY *pEntry;
-               CTL_DEBUG((_T("Found %s\r\n"),fd.cFileName));
-               pEntry = (CPLLISTENTRY*)malloc(sizeof(CPLLISTENTRY));
-               if(!pEntry)
-                       break;
-               _tcscpy(pEntry->pszPath,pszSearchPath);
-               *_tcsrchr(pEntry->pszPath,'\\')=0;
-               _tcscat(pEntry->pszPath,_T("\\"));
-               _tcscat(pEntry->pszPath,fd.cFileName);
-
-               pEntry->hDLL = LoadLibrary(pEntry->pszPath);
-               CTL_DEBUG((_T("Handle %08X\r\n"),pEntry->hDLL));
-               pEntry->pFunc = (CPLAPPLETFUNC)GetProcAddress(pEntry->hDLL,"CPlApplet");
-               CTL_DEBUG((_T("CPLFunc %08X\r\n"),pEntry->pFunc));
-               if(pEntry->pFunc && pEntry->pFunc(hLisCtrl,CPL_INIT,0,0))
+               PCPLLISTENTRY pEntry;
+               CTL_DEBUG((_T("Found %s\r\n"), fd.cFileName));
+
+               _tcscpy(pszPath, pszSearchPath);
+               *_tcsrchr(pszPath, '\\')=0;
+               _tcscat(pszPath, _T("\\"));
+               _tcscat(pszPath, fd.cFileName);
+
+               hDll = LoadLibrary(pszPath);
+               CTL_DEBUG((_T("Handle %08X\r\n"), hDll));
+
+               pFunc = (CPLAPPLETFUNC)GetProcAddress(hDll, "CPlApplet");
+               CTL_DEBUG((_T("CPLFunc %08X\r\n"), pFunc));
+
+               if (pFunc && pFunc(hLisCtrl, CPL_INIT, 0, 0))
                {
-                       int i;
-                       for(i=0;i<pEntry->pFunc(hLisCtrl,CPL_GETCOUNT,0,0);i++)
+                       UINT i, uPanelCount;
+
+                       uPanelCount = (UINT)pFunc(hLisCtrl, CPL_GETCOUNT, 0, 0);
+                       for (i = 0; i < uPanelCount; i++)
                        {
                                HICON hIcon;
                                TCHAR Name[MAX_PATH];
                                int index;
-                               pEntry->pFunc(hLisCtrl,CPL_INQUIRE,0,(LPARAM)&pEntry->CPLInfo);
 
-                               hIcon = LoadImage(pEntry->hDLL,MAKEINTRESOURCE(pEntry->CPLInfo.idIcon),IMAGE_ICON,16,16,LR_DEFAULTCOLOR);
+                               pEntry = (PCPLLISTENTRY)malloc(sizeof(CPLLISTENTRY));
+                               if (pEntry == NULL)
+                                       return;
+
+                               memset(pEntry, 0, sizeof(CPLLISTENTRY));
+                               pEntry->hDll = hDll;
+                               pEntry->pFunc = pFunc;
+                               _tcscpy(pEntry->pszPath, pszPath);
+
+                               pEntry->pFunc(hLisCtrl, CPL_INQUIRE, (LPARAM)i, (LPARAM)&pEntry->CplInfo);
+                               hIcon = LoadImage(pEntry->hDll,MAKEINTRESOURCE(pEntry->CplInfo.idIcon),IMAGE_ICON,16,16,LR_DEFAULTCOLOR);
                                index = ImageList_AddIcon(hImgListSmall,hIcon);
                                DestroyIcon(hIcon);
-                               hIcon = LoadImage(pEntry->hDLL,MAKEINTRESOURCE(pEntry->CPLInfo.idIcon),IMAGE_ICON,32,32,LR_DEFAULTCOLOR);
+                               hIcon = LoadImage(pEntry->hDll,MAKEINTRESOURCE(pEntry->CplInfo.idIcon),IMAGE_ICON,32,32,LR_DEFAULTCOLOR);
                                ImageList_AddIcon(hImgListLarge,hIcon);
                                DestroyIcon(hIcon);
 
-                               LoadString(pEntry->hDLL,pEntry->CPLInfo.idName,Name,MAX_PATH);
-                               if(_tcslen(Name))
+                               if (LoadString(pEntry->hDll, pEntry->CplInfo.idName, Name, MAX_PATH))
                                {
-                               LV_ITEM lvi;
+                                       LV_ITEM lvi;
 
-                               memset(&lvi,0x00,sizeof(lvi));
-                               lvi.mask=LVIF_TEXT|LVIF_PARAM|LVIF_STATE|LVIF_IMAGE;
-                               lvi.pszText = Name;
-                               lvi.state=0;
-                               lvi.iImage=index;
-                               lvi.lParam = (LPARAM)pEntry;
-                               pEntry->nIndex = ListView_InsertItem(hLisCtrl,&lvi);
+                                       memset(&lvi,0x00,sizeof(lvi));
+                                       lvi.mask = LVIF_TEXT|LVIF_PARAM|LVIF_STATE|LVIF_IMAGE;
+                                       lvi.pszText = Name;
+                                       lvi.state = 0;
+                                       lvi.iImage = index;
+                                       lvi.lParam = (LPARAM)pEntry;
+                                       pEntry->nIndex = ListView_InsertItem(hLisCtrl,&lvi);
 
-                               LoadString(pEntry->hDLL,pEntry->CPLInfo.idInfo,Name,MAX_PATH);
-                               ListView_SetItemText(hLisCtrl,pEntry->nIndex,1,Name);
-
-                               ListView_SetImageList(hLisCtrl,hImgListSmall,LVSIL_SMALL);
-                               ListView_SetImageList(hLisCtrl,hImgListLarge,LVSIL_NORMAL);
+                                       if (LoadString(pEntry->hDll, pEntry->CplInfo.idInfo, Name, MAX_PATH))
+                                               ListView_SetItemText(hLisCtrl, pEntry->nIndex, 1, Name);
                                }
                        }
                }
 
-               if(!FindNextFile(hFind,&fd))
+               if (!FindNextFile(hFind,&fd))
                        hFind = INVALID_HANDLE_VALUE;
        }
+
+       ListView_SetImageList(hLisCtrl,hImgListSmall,LVSIL_SMALL);
+       ListView_SetImageList(hLisCtrl,hImgListLarge,LVSIL_NORMAL);
 }
 
 LRESULT CALLBACK MyWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
 {
-       switch(uMsg)
+       switch (uMsg)
        {
        case WM_CREATE:
                {
-               RECT rect;
-               LV_COLUMN column;
-               GetClientRect(hWnd,&rect);
-               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);
-               CTL_DEBUG((_T("Listview Window %08X\r\n"),hListView));
-
-               memset(&column,0x00,sizeof(column));
-               column.mask=LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM|LVCF_TEXT;
-               column.fmt=LVCFMT_LEFT;
-               column.cx = (rect.right - rect.left) / 3;
-               column.iSubItem = 0;
-               column.pszText = _T("Name");
-               ListView_InsertColumn(hListView,0,&column);
-               column.cx = (rect.right - rect.left) - ((rect.right - rect.left) / 3) - 1;
-               column.iSubItem = 1;
-               column.pszText = _T("Comment");
-               ListView_InsertColumn(hListView,1,&column);
-               PopulateCPLList(hListView);
-               ListView_SetColumnWidth(hListView,2,LVSCW_AUTOSIZE_USEHEADER);
-               ListView_Update(hListView,0);
+                       RECT rect;
+                       LV_COLUMN column;
+
+                       GetClientRect(hWnd,&rect);
+                       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);
+                       CTL_DEBUG((_T("Listview Window %08X\r\n"),hListView));
+
+                       memset(&column,0x00,sizeof(column));
+                       column.mask=LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM|LVCF_TEXT;
+                       column.fmt=LVCFMT_LEFT;
+                       column.cx = (rect.right - rect.left) / 3;
+                       column.iSubItem = 0;
+                       column.pszText = _T("Name");
+                       ListView_InsertColumn(hListView,0,&column);
+                       column.cx = (rect.right - rect.left) - ((rect.right - rect.left) / 3) - 1;
+                       column.iSubItem = 1;
+                       column.pszText = _T("Comment");
+                       ListView_InsertColumn(hListView,1,&column);
+                       PopulateCPLList(hListView);
+                       ListView_SetColumnWidth(hListView,2,LVSCW_AUTOSIZE_USEHEADER);
+                       ListView_Update(hListView,0);
+
+                       SetFocus(hListView);
                }
                break;
+
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
+
        case WM_SIZE:
                {
-               RECT rect;
-               GetClientRect(hWnd,&rect);
-               MoveWindow(hListView,0,0,rect.right,rect.bottom,TRUE);
+                       RECT rect;
+
+                       GetClientRect(hWnd,&rect);
+                       MoveWindow(hListView,0,0,rect.right,rect.bottom,TRUE);
                }
                break;
+
        case WM_NOTIFY:
                {
-               NMHDR *phdr;
-               phdr = (NMHDR*)lParam;
-               switch(phdr->code)
-               {
-               case NM_DBLCLK:
-                       {
-                       int nSelect;
-                       LV_ITEM lvi;
-                       CPLLISTENTRY *pEntry;
-                       nSelect=SendMessage(hListView,LVM_GETNEXTITEM,(WPARAM)-1,LVNI_FOCUSED);
-                       
-                       if(nSelect==-1) // no items
+                       NMHDR *phdr;
+                       phdr = (NMHDR*)lParam;
+                       switch(phdr->code)
                        {
-                               MessageBox(hWnd,_T("No Items in ListView"),_T("Error"),MB_OK|MB_ICONINFORMATION);
-                               break;
-                       }
-                       CTL_DEBUG((_T("Select %d\r\n"),nSelect));
-                       memset(&lvi,0x00,sizeof(lvi));
-                       lvi.iItem = nSelect;
-                       lvi.mask = LVIF_PARAM;
-                       ListView_GetItem(hListView,&lvi);
-                       pEntry = (CPLLISTENTRY *)lvi.lParam;
-                       CTL_DEBUG((_T("Listview DblClk Entry %08X\r\n"),pEntry));
-                       if(pEntry) {
-                               CTL_DEBUG((_T("Listview DblClk Entry Func %08X\r\n"),pEntry->pFunc));
-                       }
-                       if(pEntry && pEntry->pFunc)
-                               pEntry->pFunc(hListView,CPL_DBLCLK,pEntry->CPLInfo.lData,0);
+                       case NM_RETURN:
+                       case NM_DBLCLK:
+                               {
+                                       int nSelect;
+                                       LV_ITEM lvi;
+                                       PCPLLISTENTRY pEntry;
+
+                                       nSelect=SendMessage(hListView,LVM_GETNEXTITEM,(WPARAM)-1,LVNI_FOCUSED);
+
+                                       if (nSelect==-1)
+                                       {
+                                               /* no items */
+                                               MessageBox(hWnd,_T("No Items in ListView"),_T("Error"),MB_OK|MB_ICONINFORMATION);
+                                               break;
+                                       }
+
+                                       CTL_DEBUG((_T("Select %d\r\n"),nSelect));
+                                       memset(&lvi,0x00,sizeof(lvi));
+                                       lvi.iItem = nSelect;
+                                       lvi.mask = LVIF_PARAM;
+                                       ListView_GetItem(hListView,&lvi);
+                                       pEntry = (PCPLLISTENTRY)lvi.lParam;
+                                       CTL_DEBUG((_T("Listview DblClk Entry %08X\r\n"),pEntry));
+                                       if (pEntry)
+                                       {
+                                               CTL_DEBUG((_T("Listview DblClk Entry Func %08X\r\n"),pEntry->pFunc));
+                                       }
+
+                                       if (pEntry && pEntry->pFunc)
+                                               pEntry->pFunc(hListView,CPL_DBLCLK,pEntry->CplInfo.lData,0);
+                               }
                        }
                }
-               }
                break;
+
        case WM_COMMAND:
-               switch(LOWORD(wParam))
+               switch (LOWORD(wParam))
                {
                case IDM_LARGEICONS:
                        SetWindowLong(hListView,GWL_STYLE,LVS_ICON | LVS_ALIGNLEFT | LVS_AUTOARRANGE | LVS_SINGLESEL   | WS_VISIBLE | WS_CHILD|WS_BORDER|WS_TABSTOP);
@@ -243,12 +288,15 @@ LRESULT CALLBACK MyWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
                        break;
                }
                break;
+
        default:
                return DefWindowProc(hWnd,uMsg,wParam,lParam);
        }
+
        return 0;
 }
 
+
 #ifdef _MSVC
 int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,WCHAR *lpCmdLine,int nCmdShow)
 #else
@@ -257,6 +305,7 @@ int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,char *lpCmdLine,i
 {
        MSG msg;
        WNDCLASS wc;
+
        hInst = hInstance;
        CTL_DEBUG((_T("My Control Panel\r\n")));
        memset(&wc,0x00,sizeof(wc));
@@ -267,16 +316,17 @@ int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,char *lpCmdLine,i
        RegisterClass(&wc);
        InitCommonControls();
        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);
-       if(!hMainWnd) {
+       if (!hMainWnd)
+       {
                CTL_DEBUG((_T("Unable to create window\r\n")));
                return -1;
        }
        ShowWindow(hMainWnd,nCmdShow);
-       while(GetMessage(&msg,0,0,0))
+       while (GetMessage(&msg,0,0,0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
-       
+
        return 0;
 }