diff --git a/progmgr/group.c b/progmgr/group.c index 142a319..4804b3c 100644 --- a/progmgr/group.c +++ b/progmgr/group.c @@ -106,14 +106,19 @@ HWND CreateGroup(_In_ PGROUP pg) if (pg == NULL) return NULL; + // error checking, if we get trash don't try to + // make a group out of it + if (pg->dwSignature != GRP_SIGNATURE) + return NULL; + // allocate memory for a new group - pGroup = (PGROUP)malloc(CalculateGroupMemory(pg, 0)); + pGroup = (PGROUP)malloc(CalculateGroupMemory(pg, 0, 0)); if (pGroup == NULL) return NULL; // clean up the memory if it's valid - ZeroMemory(pGroup, CalculateGroupMemory(pg, 0)); + ZeroMemory(pGroup, sizeof(*pGroup)); // copy over the group structure *pGroup = *pg; @@ -157,7 +162,7 @@ HWND CreateGroup(_In_ PGROUP pg) DestroyIcon(hIconTemp); } - // get the group window rect + // Get the group window rect GetClientRect(hWndGroup, &rcGroupWindow); // Create the group window ListView control @@ -172,18 +177,18 @@ HWND CreateGroup(_In_ PGROUP pg) // ((LVS_AUTOARRANGE & bAutoArrange) * LVS_AUTOARRANGE) - // resize it to fit the window + // Resize it to fit the window SetWindowPos(hWndListView, NULL, rcGroupWindow.left, rcGroupWindow.top, rcGroupWindow.right - rcGroupWindow.left, rcGroupWindow.bottom - rcGroupWindow.top, SWP_NOZORDER); - // add the explorer style because it looks good + // Add the explorer style because it looks good SetWindowTheme(hWndListView, TEXT("Explorer"), NULL); - // get this bad boy an image list + // Get this bad boy an image list // TODO: if cxicon/cyicon change make sure we handle that - // don't require a program restart + // don't require a program restart, DPI!!! hImageList = ImageList_Create(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), ILC_COLOR32, 0, 1); ListView_SetImageList(hWndListView, hImageList, LVSIL_NORMAL); @@ -262,7 +267,7 @@ PITEM CreateItem(_In_ HWND hWndGroup, _In_ PITEM pi) return NULL; // if we reallocate memory then send the new pointer in - pNewGroup = realloc(pGroup, CalculateGroupMemory(pGroup, 1)); + pNewGroup = realloc(pGroup, CalculateGroupMemory(pGroup, 1, 0)); if (pNewGroup != NULL) { pGroup = pNewGroup; @@ -341,7 +346,7 @@ BOOL ExecuteItem(_In_ PITEM pi) } /* * * *\ - SaveGroup - + UpdateGroup - Updates group information to prepare it for being saved. ABSTRACT - @@ -378,7 +383,7 @@ VOID UpdateGroup(_In_ PGROUP pg) RETURNS - Size in bytes that a group should take up. \* * * */ -UINT CalculateGroupMemory(_In_ PGROUP pGroup, _In_ UINT cItems) +UINT CalculateGroupMemory(_In_ PGROUP pGroup, _In_ UINT cItems, _In_ BOOL bLean) { UINT cbGroupSize = 0; UINT cItemBlock = 0; @@ -391,8 +396,9 @@ UINT CalculateGroupMemory(_In_ PGROUP pGroup, _In_ UINT cItems) // have some memory ready cItemBlock = pGroup->cItemArray + cItems; - // round the amount of items to the nearest but highest ITEM_BATCH_COUNT - cItemBlock = ((cItemBlock + ITEM_BATCH_COUNT) / ITEM_BATCH_COUNT) * ITEM_BATCH_COUNT; + if (!bLean) + // round the amount of items to the nearest but highest ITEM_BATCH_COUNT + cItemBlock = ((cItemBlock + ITEM_BATCH_COUNT) / ITEM_BATCH_COUNT) * ITEM_BATCH_COUNT; // finally calculate the total group size cbGroupSize += cItemBlock * sizeof(ITEM); diff --git a/progmgr/group.h b/progmgr/group.h index 68768be..d04c784 100644 --- a/progmgr/group.h +++ b/progmgr/group.h @@ -85,6 +85,6 @@ BOOL ExecuteItem(_In_ PITEM pi); // Save/Load helper functions VOID UpdateGroup(_In_ PGROUP pg); // Helper functions -UINT CalculateGroupMemory(_In_ PGROUP pGroup, _In_ UINT cItems); +UINT CalculateGroupMemory(_In_ PGROUP pGroup, _In_ UINT cItems, _In_ BOOL bLean); // Group Window LRESULT CALLBACK GroupWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); diff --git a/progmgr/registry.c b/progmgr/registry.c index 0c8445b..598bd9b 100644 --- a/progmgr/registry.c +++ b/progmgr/registry.c @@ -129,7 +129,7 @@ DWORD RegistrySaveGroup(_In_ PGROUP pg) // Save group UpdateGroup(pg); if (!RegSetValueEx(hKeyProgramGroups, pg->szName, 0, REG_BINARY, - (const BYTE*)pg, CalculateGroupMemory(pg, 0)) == ERROR_SUCCESS) + (const BYTE*)pg, sizeof(*pg)) == ERROR_SUCCESS) dwConfigStatus = dwConfigStatus && RCE_GROUPS; return dwConfigStatus; @@ -213,22 +213,7 @@ DWORD SaveConfig(_In_ BOOL bSettings, _In_ BOOL bPos, _In_ BOOL bGroups, _In_ BO // save each one as an individual subkey based // on the name of the group - EnumChildWindows(hWndMDIClient, &SaveWindowEnumProc, bExit); - - /* - while ((hWndGroup = (HWND)SendMessage(hWndMDIClient, - WM_MDIGETACTIVE, 0, (LPARAM)NULL)) != NULL) - { - // save it... - dwConfigStatus = dwConfigStatus && - RegistrySaveGroup((PGROUP)GetWindowLongPtr(hWndGroup, - GWLP_USERDATA)); - - // close it... - if (!RemoveGroup(hWndGroup, FALSE)) - dwConfigStatus = dwConfigStatus && RCE_GROUPS; - } - */ + EnumChildWindows(hWndMDIClient, &SaveWindowEnumProc, (LPARAM)bExit); } return dwConfigStatus; @@ -248,12 +233,27 @@ DWORD SaveConfig(_In_ BOOL bSettings, _In_ BOOL bPos, _In_ BOOL bGroups, _In_ BO \* * * */ BOOL CALLBACK SaveWindowEnumProc(HWND hWndGroup, LPARAM lParam) { + PGROUP pNewGroup = NULL; + PGROUP pGroup = NULL; + if (hWndGroup == NULL) return FALSE; + pGroup = (PGROUP)GetWindowLongPtr(hWndGroup, GWLP_USERDATA); + + if (pGroup == NULL) + return FALSE; + + // lean it... + pNewGroup = realloc(pGroup, CalculateGroupMemory(pGroup, 1, 1)); + if (pNewGroup != NULL) + { + pGroup = pNewGroup; + SetWindowLongPtr(hWndGroup, GWLP_USERDATA, (LONG_PTR)pGroup); + } + // save it... - if (RegistrySaveGroup((PGROUP)GetWindowLongPtr(hWndGroup, - GWLP_USERDATA)) != RCE_SUCCESS) + if (RegistrySaveGroup(pGroup) != RCE_SUCCESS) return FALSE; // close it... @@ -310,13 +310,14 @@ DWORD LoadConfig(_In_ BOOL bSettings, _In_ BOOL bPos, _In_ BOOL bGroups) { DWORD dwBufferSize = sizeof(dwSettingsMask); DWORD dwType = REG_BINARY; - PGROUP pgrp = NULL; + PGROUP pGroup = NULL; + UINT cbGroup = 0; UINT cGroupKeys = 0; - UINT cGroupCycle = 0; + UINT cGroupIndex = 0; - if (!RegQueryInfoKey(hKeyProgramGroups, NULL, NULL, NULL, + if (!(RegQueryInfoKey(hKeyProgramGroups, NULL, NULL, NULL, NULL, NULL, NULL, &cGroupKeys, - NULL, NULL, NULL, NULL) == ERROR_SUCCESS) + NULL, NULL, NULL, NULL) == ERROR_SUCCESS)) { dwConfigStatus = dwConfigStatus && RCE_SETTINGS; @@ -325,13 +326,37 @@ DWORD LoadConfig(_In_ BOOL bSettings, _In_ BOOL bPos, _In_ BOOL bGroups) return dwConfigStatus; } - if (cGroupKeys < 1) + if (cGroupKeys > 0) { - while (cGroupKeys >= cGroupCycle) + while (cGroupKeys >= cGroupIndex) { - + WCHAR szValueName[MAX_TITLE_LENGTH] = TEXT(""); + UINT cbValueName = 0; + + // TODO: figure out where i'm really going to store the + // group name, if not in the group structure then in + // the name of the registry key (val 3 here) + + // get the size of the group + RegEnumValue(hKeyProgramGroups, cGroupIndex, (LPWSTR)&szValueName, + &cbValueName, NULL, NULL, NULL, &cbGroup); + + // allocate memory for the group + pGroup = malloc(cbGroup); + + // get the group + RegQueryValueEx(hKeyProgramGroups, (LPWSTR)&szValueName, NULL, NULL, + (LPBYTE)pGroup, &cbGroup); + + // load the group + CreateGroup(pGroup); + + // free memory + if (pGroup) + free(pGroup); + // increment - cGroupCycle++; + cGroupIndex++; } } } diff --git a/progmgr/wndproc.c b/progmgr/wndproc.c index cec2fdd..5bd99d8 100644 --- a/progmgr/wndproc.c +++ b/progmgr/wndproc.c @@ -35,6 +35,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_CREATE: { + // Now we can create the groups + if(LoadConfig(FALSE, FALSE, TRUE) != RCE_SUCCESS) + return FALSE; + + // TODO: if loading groups fails, throw an error + return TRUE; }