123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- #include "./setupDetails.h"
- #include "../common.h"
- #include "../resource.h"
- #include "../wasabi.h"
- #include <ifc_omservice.h>
- #include <ifc_omservicedetails.h>
- #include <ifc_omcachemanager.h>
- #include <ifc_omserviceevent.h>
- #include <ifc_omcachegroup.h>
- #include <ifc_omcacherecord.h>
- #include <ifc_imageloader.h>
- #include <ifc_omgraphics.h>
- #include <ifc_omserviceeventmngr.h>
- #include <shlwapi.h>
- #include <strsafe.h>
- #define GetPanel(__hwnd) ((ServicePanel*)GetPropW((__hwnd), MAKEINTATOM(DETAILS_PROP)))
- #define GET_IDETAILS(__service, __details)\
- (NULL != (service) && SUCCEEDED((service)->QueryInterface(IFC_OmServiceDetails, (void**)&(__details))))
- static INT_PTR WINAPI ServicePanel_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- HWND OmSetupDetails_CreateServiceView(HWND hParent, ifc_omservice *service)
- {
- return WASABI_API_CREATEDIALOGPARAMW(IDD_SETUP_SERVICEDETAILS, hParent, ServicePanel_DialogProc, (LPARAM)service);
- }
- static HFONT ServicePanel_PickTitleFont(HWND hwnd, LPCWSTR pszTitle, INT cchTitle, INT maxWidth)
- {
- HFONT dialogFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0L);
-
- LOGFONT lf;
- if (0 == GetObject(dialogFont, sizeof(LOGFONT), &lf))
- return NULL;
- HFONT titleFont = NULL;
- if (cchTitle > 0)
- {
- LOGFONT lf;
- if (0 != GetObject(dialogFont, sizeof(LOGFONT), &lf))
- {
- StringCchCopy(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), L"Arial Bold");
- lf.lfWidth = 0;
- lf.lfWeight = FW_DONTCARE;
- lf.lfQuality = 5/*ANTIALIASED_QUALITY*/;
-
- HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
- if (NULL != hdc)
- {
- HFONT origFont = (HFONT)GetCurrentObject(hdc, OBJ_FONT);
- SIZE textSize;
- INT heightLimit = (lf.lfHeight < 0) ? 1 : -1;
- lf.lfHeight += (lf.lfHeight < 0) ? -2 : +2;
- do
- {
- textSize.cx = 0;
- if (NULL != titleFont) DeleteObject(titleFont);
- titleFont = CreateFontIndirect(&lf);
- if (NULL != titleFont)
- {
- SelectObject(hdc, titleFont);
- GetTextExtentPoint32(hdc, pszTitle, cchTitle, &textSize);
- }
- lf.lfHeight += (lf.lfHeight < 0) ? 1 : -1;
- } while(textSize.cx > maxWidth && lf.lfHeight != heightLimit);
-
- if (0 == textSize.cx)
- {
- DeleteObject(titleFont);
- titleFont = NULL;
- }
- SelectObject(hdc, origFont);
- ReleaseDC(hwnd, hdc);
- }
- }
- }
- if (NULL == titleFont &&
- 0 != GetObject(dialogFont, sizeof(LOGFONT), &lf))
- {
- titleFont = CreateFontIndirect(&lf);
- }
- return titleFont;
- }
- static void ServicePanel_SetServiceName(HWND hwnd, ifc_omservice *service)
- {
- HWND hTitle = GetDlgItem(hwnd, IDC_TITLE);
- if (NULL == hTitle) return;
- WCHAR szBuffer[128];
- if (NULL == service ||
- FAILED(service->GetName(szBuffer, ARRAYSIZE(szBuffer))))
- {
- szBuffer[0] = L'\0';
- }
-
-
- SERVICEDETAILS *details = GetDetails(hwnd);
- if (NULL != details)
- {
- INT cchBuffer = lstrlen(szBuffer);
- RECT rc;
- GetClientRect(hTitle, &rc);
- HFONT titleFont = ServicePanel_PickTitleFont(hwnd, szBuffer, cchBuffer, rc.right - rc.left);
- if (NULL != titleFont)
- {
- if (NULL != details->fontTitle) DeleteObject(details->fontTitle);
- details->fontTitle = titleFont;
- SendMessage(hTitle, WM_SETFONT, (WPARAM)details->fontTitle, 0L);
- }
- }
- SetWindowText(hTitle, szBuffer);
- InvalidateRect(hTitle, NULL, TRUE);
- }
- static void ServicePanel_SetServiceDescription(HWND hwnd, ifc_omservice *service)
- {
- HWND hDescription = GetDlgItem(hwnd, IDC_DESCRIPTION);
- if (NULL == hDescription) return;
- WCHAR szBuffer[4096] = {0};
- ifc_omservicedetails *details = 0;
- if (GET_IDETAILS(service, details))
- {
- details->GetDescription(szBuffer, ARRAYSIZE(szBuffer));
- details->Release();
- }
- OmSetupDetails_SetDescription(hDescription, szBuffer);
- }
- static LPCWSTR ServicePanel_FormatDate(LPCWSTR pszDate, LPWSTR pszBuffer, INT cchBufferMax)
- {
- SYSTEMTIME st;
- ZeroMemory(&st, sizeof(SYSTEMTIME));
- LPCWSTR cursor;
-
- cursor = pszDate;
- INT index = 0;
- for(;;)
- {
-
- INT iVal;
- if (FALSE == StrToIntEx(cursor, STIF_DEFAULT, &iVal) || iVal < 1)
- {
- index = 0;
- break;
- }
- if (0 == index)
- {
- if (iVal < 2000 || iVal > 2100)
- break;
- st.wYear = iVal;
- index++;
- }
- else if (1 == index)
- {
- if (iVal < 1 || iVal > 12)
- break;
- st.wMonth = iVal;
- index++;
- }
- else if (2 == index)
- {
- if (iVal < 1 || iVal > 31)
- break;
- st.wDay = iVal;
- index++;
- }
- else
- {
- index = 0;
- break;
- }
-
- while(L'\0' != *cursor && L'-' != *cursor) cursor++;
- if (L'-' == *cursor) cursor++;
- if (L'\0' == *cursor)
- break;
-
- }
- if (3 == index &&
- 0 != GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, pszBuffer, cchBufferMax))
- {
- return pszBuffer;
- }
- return pszDate;
- }
- static HRESULT ServicePanel_GetFullName(ifc_omservicedetails *details, LPWSTR pszBuffer, UINT cchBufferMax)
- {
- if (NULL == pszBuffer)
- return E_POINTER;
-
- *pszBuffer = L'\0';
- HRESULT hr = S_OK;
- if (NULL != details)
- {
- hr = details->GetAuthorFirst(pszBuffer, cchBufferMax);
- if (SUCCEEDED(hr))
- {
- UINT cchBuffer = lstrlen(pszBuffer);
- LPWSTR cursor = pszBuffer + cchBuffer;
- size_t remaining = cchBufferMax - cchBuffer;
- if (cursor != pszBuffer)
- {
- hr = StringCchCopyEx(cursor, remaining, L" ", &cursor, &remaining, 0);
- if (SUCCEEDED(hr))
- {
- hr = details->GetAuthorLast(cursor, (UINT)remaining);
- if (FAILED(hr) || L'\0' == *cursor)
- {
- pszBuffer[cchBuffer] = L'\0';
- }
- }
- }
- }
- }
- return hr;
- }
- static void ServicePanel_SetServiceMeta(HWND hwnd, ifc_omservice *service)
- {
- HWND hMeta = GetDlgItem(hwnd, IDC_SERVICEMETA);
- if (NULL == hMeta) return;
- WCHAR szBuffer[512] = {0};
- ifc_omservicedetails *svcdetails = 0;
- if (GET_IDETAILS(service, svcdetails))
- {
- HRESULT hr = S_OK;
- LPWSTR cursor = szBuffer;
- WCHAR szValue[256] = {0}, szPrefix[64] = {0};
- size_t remaining = ARRAYSIZE(szBuffer);
- if (SUCCEEDED(ServicePanel_GetFullName(svcdetails, szValue, ARRAYSIZE(szValue))) && L'\0' != szValue[0])
- {
- WASABI_API_LNGSTRINGW_BUF(IDS_SERVICE_BYAUTHOR, szPrefix, ARRAYSIZE(szPrefix));
- hr = StringCchPrintfEx(cursor, remaining, &cursor, &remaining, STRSAFE_NULL_ON_FAILURE,
- L"%s%s", szPrefix, szValue);
- }
- if (SUCCEEDED(svcdetails->GetUpdated(szValue, ARRAYSIZE(szValue))) && L'\0' != szValue[0])
- {
- if (cursor != szBuffer)
- hr = StringCchCopyEx(cursor, remaining, L"\r\n", &cursor, &remaining, STRSAFE_NULL_ON_FAILURE);
- if (SUCCEEDED(hr))
- {
- WCHAR szDate[128] = {0};
- WASABI_API_LNGSTRINGW_BUF(IDS_SERVICE_LASTUPDATED, szPrefix, ARRAYSIZE(szPrefix));
- StringCchPrintfEx(cursor, remaining, &cursor, &remaining, STRSAFE_NULL_ON_FAILURE,
- L"%s%s", szPrefix, ServicePanel_FormatDate(szValue, szDate, ARRAYSIZE(szDate)));
- }
- }
- svcdetails->Release();
- }
- SERVICEDETAILS *details = GetDetails(hwnd);
- if (NULL != details && NULL == details->fontMeta)
- {
- HFONT dialogFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0L);
- LOGFONT lf;
- if (0 != GetObject(dialogFont, sizeof(LOGFONT), &lf))
- {
- StringCchCopy(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), L"Tahoma");
- lf.lfWidth = 0;
- lf.lfHeight += (lf.lfHeight < 0) ? 1 : -1;
- lf.lfQuality = ANTIALIASED_QUALITY;
- details->fontMeta = CreateFontIndirect(&lf);
- }
- if (NULL != details->fontMeta)
- {
- SendMessage(hMeta, WM_SETFONT, (WPARAM)details->fontMeta, 0L);
- }
- }
- SetWindowText(hMeta, szBuffer);
- if (0 != ShowWindow(hMeta, (L'\0' != szBuffer[0]) ? SW_SHOWNA : SW_HIDE))
- InvalidateRect(hMeta, NULL, TRUE);
- }
- static void ServicePanel_SetThumbnail(HWND hwnd, ifc_omservice *service)
- {
- HWND hThumbnail = GetDlgItem(hwnd, IDC_THUMBNAIL);
- if (NULL == hThumbnail) return;
- SendMessage(hThumbnail, WM_SETREDRAW, FALSE, 0L);
- HBITMAP hBitmap;
- BITMAPINFOHEADER header;
- void *pixelData;
- WCHAR szPath[2048];
- ifc_omservicedetails *details = 0;
- if (GET_IDETAILS(service, details))
- {
- if (SUCCEEDED(details->GetThumbnail(szPath, ARRAYSIZE(szPath))))
- {
- ifc_omcachemanager *cacheManager;
- if (SUCCEEDED(OMUTILITY->GetCacheManager(&cacheManager)))
- {
- ifc_omcachegroup *cacheGroup;
- if (SUCCEEDED(cacheManager->Find(L"thumbnail", TRUE, &cacheGroup, NULL)))
- {
- ifc_omcacherecord *cacheRecord;
- if (SUCCEEDED(cacheGroup->Find(szPath, TRUE, &cacheRecord, FALSE)))
- {
- cacheRecord->Release();
- }
- cacheGroup->Release();
- }
- cacheManager->Release();
- }
- }
- details->Release();
- }
- ifc_omimageloader *imageLoader;
- if (SUCCEEDED(OMUTILITY->QueryImageLoader(NULL, szPath, FALSE, &imageLoader)))
- {
- if (FAILED(imageLoader->LoadBitmapEx(&hBitmap, &header, &pixelData)))
- hBitmap = NULL;
- imageLoader->Release();
- }
-
- if (NULL == hBitmap &&
- SUCCEEDED(OMUTILITY->QueryImageLoader(WASABI_API_ORIG_HINST, MAKEINTRESOURCE(IDR_SERVICE64X64_IMAGE), FALSE, &imageLoader)))
- {
- if (FAILED(imageLoader->LoadBitmapEx(&hBitmap, &header, &pixelData)))
- hBitmap = NULL;
- imageLoader->Release();
- }
-
- HBITMAP hTest = (HBITMAP)SendMessage(hThumbnail, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
- if (NULL != hTest)
- DeleteObject(hTest);
- if (NULL != hBitmap)
- {
- hTest = (HBITMAP)SendMessage(hThumbnail, STM_GETIMAGE, IMAGE_BITMAP, 0L);
- if (hTest != hBitmap)
- { // this is XP and up image copy was created and alpha channel will be handled properly
- DeleteObject(hBitmap);
- }
- else
- { // fix alpha channel
- if (32 == header.biBitCount)
- {
- HDC hdcFixed = CreateCompatibleDC(NULL);
- if (NULL != hdcFixed)
- {
- BITMAPINFOHEADER headerFixed;
- CopyMemory(&headerFixed, &header, sizeof(BITMAPINFOHEADER));
- BYTE *pixelsFixed;
- INT cx = header.biWidth;
- INT cy = abs(header.biHeight);
- HBITMAP bitmapFixed = CreateDIBSection(NULL, (LPBITMAPINFO)&headerFixed, DIB_RGB_COLORS, (void**)&pixelsFixed, NULL, 0);
-
- if (NULL != bitmapFixed)
- {
- HBITMAP bitmapOrig = (HBITMAP)SelectObject(hdcFixed, bitmapFixed);
- HBRUSH hb = (HBRUSH)SendMessage(hwnd, WM_CTLCOLORDLG, (WPARAM)hdcFixed, (LPARAM)hwnd);
- if (NULL == hb)
- hb = GetSysColorBrush(COLOR_3DFACE);
- RECT rect;
- SetRect(&rect, 0, 0, cx, cy);
- FillRect(hdcFixed, &rect, hb);
-
- ifc_omgraphics *graphics;
- if (SUCCEEDED(OMUTILITY->GetGraphics(&graphics)))
- {
- HDC hdcSrc = CreateCompatibleDC(NULL);
- if (NULL != hdcSrc)
- {
- HBITMAP bitmapSrcOrig = (HBITMAP)SelectObject(hdcSrc, hBitmap);
- BLENDFUNCTION bf;
- bf.BlendOp = AC_SRC_OVER;
- bf.BlendFlags = 0;
- bf.SourceConstantAlpha = 0xFF;
- bf.AlphaFormat = AC_SRC_ALPHA;
- RECT blendRect;
- SetRect(&blendRect, 0, 0, cx, cy);
- graphics->Premultiply((BYTE*)pixelData, cx, cy);
- graphics->AlphaBlend(hdcFixed, &blendRect, hdcSrc, &blendRect, bf);
- SelectObject(hdcSrc, bitmapSrcOrig);
- DeleteDC(hdcSrc);
- }
- graphics->Release();
- }
- SelectObject(hdcFixed, bitmapOrig);
- SendMessage(hThumbnail, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bitmapFixed);
- DeleteObject(hBitmap);
-
- }
- DeleteDC(hdcFixed);
- }
- }
- }
- }
- RECT clientRect;
- if (GetClientRect(hThumbnail, &clientRect))
- {
- INT cx = clientRect.right - clientRect.left;
- INT cy = clientRect.bottom - clientRect.top;
- if (64 != cx || 64 != cy)
- {
- SetWindowPos(hThumbnail, NULL, 0, 0, 64, 64, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
- }
- }
-
- SendMessage(hThumbnail, WM_SETREDRAW, TRUE, 0L);
- if (0 != ShowWindow(hThumbnail, (NULL != hBitmap) ? SW_SHOWNA : SW_HIDE))
- InvalidateRect(hThumbnail, NULL, TRUE);
- }
- static void CALLBACK ServicePanel_OnServiceNotify(UINT serviceUid, UINT callbackType, UINT callbackParam, ULONG_PTR user)
- {
- HWND hwnd = (HWND)user;
- if (NULL == hwnd) return;
- SERVICEDETAILS *details = GetDetails(hwnd);
-
- if (NULL == details ||
- NULL == details->service ||
- serviceUid != details->service->GetId())
- {
- return;
- }
- switch(callbackType)
- {
- case OmService::eventServiceModified:
- if (0 != (OmService::modifiedName & callbackParam))
- ServicePanel_SetServiceName(hwnd, details->service);
- if (0 != (OmService::modifiedDescription & callbackParam))
- ServicePanel_SetServiceDescription(hwnd, details->service);
- if (0 != ((OmService::modifiedAuthor | OmService::modifiedDate) & callbackParam))
- ServicePanel_SetServiceMeta(hwnd, details->service);
- if (0 != (OmService::modifiedThumbnail & callbackParam))
- ServicePanel_SetThumbnail(hwnd, details->service);
- break;
- }
- }
- static INT_PTR ServicePanel_OnInitDialog(HWND hwnd, HWND hFocus, LPARAM lParam)
- {
- ifc_omservice *service = (ifc_omservice*)lParam;
- SERVICEDETAILS *details = (SERVICEDETAILS*)malloc(sizeof(SERVICEDETAILS));
- if (NULL == details) return FALSE;
- ZeroMemory(details, sizeof(SERVICEDETAILS));
- if (!SetProp(hwnd, MAKEINTATOM(DETAILS_PROP), details))
- return FALSE;
- details->service = service;
- details->service->AddRef();
-
- ServicePanel_SetServiceName(hwnd, service);
- ServicePanel_SetServiceDescription(hwnd, service);
- ServicePanel_SetThumbnail(hwnd, service);
- ServicePanel_SetServiceMeta(hwnd, service);
- if (NULL != service)
- {
- ifc_omserviceeventmngr *eventManager;
- if (SUCCEEDED(service->GetEventManager(&eventManager)))
- {
- if (SUCCEEDED(eventManager->RegisterHandler(eventHander)))
- {
- details->eventHandler = eventHandler;
- }
- else
- {
- eventHandler->Release();
- }
- }
- }
- return FALSE;
- }
- static void ServicePanel_OnDestroy(HWND hwnd)
- {
- OMSERVICEMNGR->UnregisterCallback(ServicePanel_OnServiceNotify, (ULONG_PTR)hwnd);
- SERVICEDETAILS *details = GetDetails(hwnd);
- RemoveProp(hwnd, MAKEINTATOM(DETAILS_PROP));
-
- if (NULL != details)
- {
- if (NULL != details->service)
- details->service->Release();
- if (NULL != details->fontTitle)
- DeleteObject(details->fontTitle);
- if (NULL != details->fontMeta)
- DeleteObject(details->fontMeta);
- }
- HWND hThumbnail = GetDlgItem(hwnd, IDC_THUMBNAIL);
- if (NULL != hThumbnail)
- {
- HBITMAP hBitmap = (HBITMAP)SendMessage(hThumbnail, STM_SETIMAGE, IMAGE_BITMAP, 0L);
- if (NULL != hBitmap)
- DeleteObject(hBitmap);
- }
- }
- static INT_PTR ServicePanel_OnDialogColor(HWND hwnd, HDC hdc, HWND hControl)
- {
- HWND hParent = GetAncestor(hwnd, GA_PARENT);
- if (NULL != hParent && hParent != hwnd)
- return (INT_PTR)SendMessage(hParent, WM_CTLCOLORDLG, (WPARAM)hdc, (LPARAM)hControl);
- return 0;
- }
- static INT_PTR ServicePanel_OnStaticColor(HWND hwnd, HDC hdc, HWND hControl)
- {
- INT_PTR result = 0;
- HWND hParent = GetAncestor(hwnd, GA_PARENT);
- if (NULL != hParent && hParent != hwnd)
- result = (INT_PTR)SendMessage(hParent, WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hControl);
-
- INT controlId = GetDlgCtrlID(hControl);
- switch(controlId)
- {
- case IDC_SERVICEMETA:
- {
- COLORREF rgbBk = GetBkColor(hdc);
- COLORREF rgbFg = GetTextColor(hdc);
-
- ifc_omgraphics *graphics;
- if (SUCCEEDED(OMUTILITY->GetGraphics(&graphics)))
- {
- graphics->BlendColor(rgbFg, rgbBk, 180, &rgbFg);
- graphics->Release();
- }
- SetTextColor(hdc, rgbFg);
- }
- break;
- }
- return result;
- }
- static INT_PTR WINAPI ServicePanel_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch(uMsg)
- {
- case WM_INITDIALOG: return ServicePanel_OnInitDialog(hwnd, (HWND)wParam, lParam);
- case WM_DESTROY: ServicePanel_OnDestroy(hwnd); break;
- case WM_CTLCOLORDLG: return ServicePanel_OnDialogColor(hwnd, (HDC)wParam, (HWND)lParam);
- case WM_CTLCOLORSTATIC: return ServicePanel_OnStaticColor(hwnd, (HDC)wParam, (HWND)lParam);
- }
- return 0;
- }
|