[RAPPS] Set header style when sorting by column
[reactos.git] / base / applications / rapps / gui.cpp
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * FILE: base/applications/rapps/gui.cpp
5 * PURPOSE: GUI classes for RAPPS
6 * COPYRIGHT: Copyright 2015 David Quintana (gigaherz@gmail.com)
7 * Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
8 */
9 #include "rapps.h"
10
11 #include "rapps.h"
12 #include "rosui.h"
13 #include "crichedit.h"
14
15 #include <shlobj_undoc.h>
16 #include <shlguid_undoc.h>
17
18 #include <atlbase.h>
19 #include <atlcom.h>
20 #include <atlwin.h>
21 #include <wininet.h>
22 #include <shellutils.h>
23 #include <rosctrls.h>
24
25 #define SEARCH_TIMER_ID 'SR'
26 #define LISTVIEW_ICON_SIZE 24
27 #define TREEVIEW_ICON_SIZE 24
28
29 HWND hListView = NULL;
30
31 INT GetSystemColorDepth()
32 {
33 DEVMODEW pDevMode;
34 INT ColorDepth;
35
36 pDevMode.dmSize = sizeof(pDevMode);
37 pDevMode.dmDriverExtra = 0;
38
39 if (!EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &pDevMode))
40 {
41 /* TODO: Error message */
42 return ILC_COLOR;
43 }
44
45 switch (pDevMode.dmBitsPerPel)
46 {
47 case 32: ColorDepth = ILC_COLOR32; break;
48 case 24: ColorDepth = ILC_COLOR24; break;
49 case 16: ColorDepth = ILC_COLOR16; break;
50 case 8: ColorDepth = ILC_COLOR8; break;
51 case 4: ColorDepth = ILC_COLOR4; break;
52 default: ColorDepth = ILC_COLOR; break;
53 }
54
55 return ColorDepth;
56 }
57
58 class CAvailableAppView
59 {
60 static inline VOID InsertTextAfterLoaded_RichEdit(UINT uStringID,
61 const ATL::CStringW& szText,
62 DWORD StringFlags,
63 DWORD TextFlags)
64 {
65 ATL::CStringW szLoadedText;
66 if (!szText.IsEmpty() && szLoadedText.LoadStringW(uStringID))
67 {
68 InsertRichEditText(szLoadedText, StringFlags);
69 InsertRichEditText(szText, TextFlags);
70 }
71 }
72
73 static inline VOID InsertLoadedTextNewl_RichEdit(UINT uStringID,
74 DWORD StringFlags)
75 {
76 ATL::CStringW szLoadedText;
77 if (szLoadedText.LoadStringW(uStringID))
78 {
79 InsertRichEditText(L"\n", 0);
80 InsertRichEditText(szLoadedText, StringFlags);
81 InsertRichEditText(L"\n", 0);
82 }
83 }
84
85 static VOID InsertVersionInfo_RichEdit(CAvailableApplicationInfo* Info)
86 {
87 if (Info->IsInstalled())
88 {
89 if (Info->HasInstalledVersion())
90 {
91 if (Info->HasUpdate())
92 InsertLoadedTextNewl_RichEdit(IDS_STATUS_UPDATE_AVAILABLE, CFE_ITALIC);
93 else
94 InsertLoadedTextNewl_RichEdit(IDS_STATUS_INSTALLED, CFE_ITALIC);
95
96 InsertTextAfterLoaded_RichEdit(IDS_AINFO_VERSION, Info->m_szInstalledVersion, CFE_BOLD, 0);
97 }
98 else
99 {
100 InsertLoadedTextNewl_RichEdit(IDS_STATUS_INSTALLED, CFE_ITALIC);
101 }
102 }
103 else
104 {
105 InsertLoadedTextNewl_RichEdit(IDS_STATUS_NOTINSTALLED, CFE_ITALIC);
106 }
107
108 InsertTextAfterLoaded_RichEdit(IDS_AINFO_AVAILABLEVERSION, Info->m_szVersion, CFE_BOLD, 0);
109 }
110
111 static VOID InsertLicenseInfo_RichEdit(CAvailableApplicationInfo* Info)
112 {
113 ATL::CStringW szLicense;
114 switch (Info->m_LicenseType)
115 {
116 case LICENSE_OPENSOURCE:
117 szLicense.LoadStringW(IDS_LICENSE_OPENSOURCE);
118 break;
119 case LICENSE_FREEWARE:
120 szLicense.LoadStringW(IDS_LICENSE_FREEWARE);
121 break;
122 case LICENSE_TRIAL:
123 szLicense.LoadStringW(IDS_LICENSE_TRIAL);
124 break;
125 default:
126 InsertTextAfterLoaded_RichEdit(IDS_AINFO_LICENSE, Info->m_szLicense, CFE_BOLD, 0);
127 return;
128 }
129
130 szLicense += L" (" + Info->m_szLicense + L")";
131 InsertTextAfterLoaded_RichEdit(IDS_AINFO_LICENSE, szLicense, CFE_BOLD, 0);
132 }
133
134 static VOID InsertLanguageInfo_RichEdit(CAvailableApplicationInfo* Info)
135 {
136 if (!Info->HasLanguageInfo())
137 {
138 return;
139 }
140
141 const INT nTranslations = Info->m_LanguageLCIDs.GetSize();
142 ATL::CStringW szLangInfo;
143 ATL::CStringW szLoadedTextAvailability;
144 ATL::CStringW szLoadedAInfoText;
145
146 szLoadedAInfoText.LoadStringW(IDS_AINFO_LANGUAGES);
147
148 if (Info->HasNativeLanguage())
149 {
150 szLoadedTextAvailability.LoadStringW(IDS_LANGUAGE_AVAILABLE_TRANSLATION);
151 if (nTranslations > 1)
152 {
153 ATL::CStringW buf;
154 buf.LoadStringW(IDS_LANGUAGE_MORE_PLACEHOLDER);
155 szLangInfo.Format(buf, nTranslations - 1);
156 }
157 else
158 {
159 szLangInfo.LoadStringW(IDS_LANGUAGE_SINGLE);
160 szLangInfo = L" (" + szLangInfo + L")";
161 }
162 }
163 else if (Info->HasEnglishLanguage())
164 {
165 szLoadedTextAvailability.LoadStringW(IDS_LANGUAGE_ENGLISH_TRANSLATION);
166 if (nTranslations > 1)
167 {
168 ATL::CStringW buf;
169 buf.LoadStringW(IDS_LANGUAGE_AVAILABLE_PLACEHOLDER);
170 szLangInfo.Format(buf, nTranslations - 1);
171 }
172 else
173 {
174 szLangInfo.LoadStringW(IDS_LANGUAGE_SINGLE);
175 szLangInfo = L" (" + szLangInfo + L")";
176 }
177 }
178 else
179 {
180 szLoadedTextAvailability.LoadStringW(IDS_LANGUAGE_NO_TRANSLATION);
181 }
182
183 InsertRichEditText(szLoadedAInfoText, CFE_BOLD);
184 InsertRichEditText(szLoadedTextAvailability, NULL);
185 InsertRichEditText(szLangInfo, CFE_ITALIC);
186 }
187
188 public:
189 static BOOL ShowAvailableAppInfo(INT Index)
190 {
191 CAvailableApplicationInfo* Info = (CAvailableApplicationInfo*) ListViewGetlParam(Index);
192 if (!Info) return FALSE;
193
194 NewRichEditText(Info->m_szName, CFE_BOLD);
195 InsertVersionInfo_RichEdit(Info);
196 InsertLicenseInfo_RichEdit(Info);
197 InsertLanguageInfo_RichEdit(Info);
198
199 InsertTextAfterLoaded_RichEdit(IDS_AINFO_SIZE, Info->m_szSize, CFE_BOLD, 0);
200 InsertTextAfterLoaded_RichEdit(IDS_AINFO_URLSITE, Info->m_szUrlSite, CFE_BOLD, CFE_LINK);
201 InsertTextAfterLoaded_RichEdit(IDS_AINFO_DESCRIPTION, Info->m_szDesc, CFE_BOLD, 0);
202 InsertTextAfterLoaded_RichEdit(IDS_AINFO_URLDOWNLOAD, Info->m_szUrlDownload, CFE_BOLD, CFE_LINK);
203
204 return TRUE;
205 }
206 };
207
208 class CMainToolbar :
209 public CUiWindow< CToolbar<> >
210 {
211 const INT m_iToolbarHeight;
212 DWORD m_dButtonsWidthMax;
213
214 WCHAR szInstallBtn[MAX_STR_LEN];
215 WCHAR szUninstallBtn[MAX_STR_LEN];
216 WCHAR szModifyBtn[MAX_STR_LEN];
217 WCHAR szSelectAll[MAX_STR_LEN];
218
219 VOID AddImageToImageList(HIMAGELIST hImageList, UINT ImageIndex)
220 {
221 HICON hImage;
222
223 if (!(hImage = (HICON) LoadImageW(hInst,
224 MAKEINTRESOURCE(ImageIndex),
225 IMAGE_ICON,
226 m_iToolbarHeight,
227 m_iToolbarHeight,
228 0)))
229 {
230 /* TODO: Error message */
231 }
232
233 ImageList_AddIcon(hImageList, hImage);
234 DeleteObject(hImage);
235 }
236
237 HIMAGELIST InitImageList()
238 {
239 HIMAGELIST hImageList;
240
241 /* Create the toolbar icon image list */
242 hImageList = ImageList_Create(m_iToolbarHeight,//GetSystemMetrics(SM_CXSMICON),
243 m_iToolbarHeight,//GetSystemMetrics(SM_CYSMICON),
244 ILC_MASK | GetSystemColorDepth(),
245 1, 1);
246 if (!hImageList)
247 {
248 /* TODO: Error message */
249 return NULL;
250 }
251
252 AddImageToImageList(hImageList, IDI_INSTALL);
253 AddImageToImageList(hImageList, IDI_UNINSTALL);
254 AddImageToImageList(hImageList, IDI_MODIFY);
255 AddImageToImageList(hImageList, IDI_CHECK_ALL);
256 AddImageToImageList(hImageList, IDI_REFRESH);
257 AddImageToImageList(hImageList, IDI_UPDATE_DB);
258 AddImageToImageList(hImageList, IDI_SETTINGS);
259 AddImageToImageList(hImageList, IDI_EXIT);
260
261 return hImageList;
262 }
263
264 public:
265 CMainToolbar() : m_iToolbarHeight(24)
266 {
267 }
268
269 VOID OnGetDispInfo(LPTOOLTIPTEXT lpttt)
270 {
271 UINT idButton = (UINT) lpttt->hdr.idFrom;
272
273 switch (idButton)
274 {
275 case ID_EXIT:
276 lpttt->lpszText = MAKEINTRESOURCEW(IDS_TOOLTIP_EXIT);
277 break;
278
279 case ID_INSTALL:
280 lpttt->lpszText = MAKEINTRESOURCEW(IDS_TOOLTIP_INSTALL);
281 break;
282
283 case ID_UNINSTALL:
284 lpttt->lpszText = MAKEINTRESOURCEW(IDS_TOOLTIP_UNINSTALL);
285 break;
286
287 case ID_MODIFY:
288 lpttt->lpszText = MAKEINTRESOURCEW(IDS_TOOLTIP_MODIFY);
289 break;
290
291 case ID_SETTINGS:
292 lpttt->lpszText = MAKEINTRESOURCEW(IDS_TOOLTIP_SETTINGS);
293 break;
294
295 case ID_REFRESH:
296 lpttt->lpszText = MAKEINTRESOURCEW(IDS_TOOLTIP_REFRESH);
297 break;
298
299 case ID_RESETDB:
300 lpttt->lpszText = MAKEINTRESOURCEW(IDS_TOOLTIP_UPDATE_DB);
301 break;
302 }
303 }
304
305 HWND Create(HWND hwndParent)
306 {
307 /* Create buttons */
308 TBBUTTON Buttons[] =
309 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
310 { 0, ID_INSTALL, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, (INT_PTR) szInstallBtn },
311 { 1, ID_UNINSTALL, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, (INT_PTR) szUninstallBtn },
312 { 2, ID_MODIFY, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, (INT_PTR) szModifyBtn },
313 { 3, ID_CHECK_ALL, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, (INT_PTR) szSelectAll },
314 { -1, 0, TBSTATE_ENABLED, BTNS_SEP, { 0 }, 0, 0 },
315 { 4, ID_REFRESH, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, 0 },
316 { 5, ID_RESETDB, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, 0 },
317 { -1, 0, TBSTATE_ENABLED, BTNS_SEP, { 0 }, 0, 0 },
318 { 6, ID_SETTINGS, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, 0 },
319 { 7, ID_EXIT, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, 0 },
320 };
321
322 LoadStringW(hInst, IDS_INSTALL, szInstallBtn, _countof(szInstallBtn));
323 LoadStringW(hInst, IDS_UNINSTALL, szUninstallBtn, _countof(szUninstallBtn));
324 LoadStringW(hInst, IDS_MODIFY, szModifyBtn, _countof(szModifyBtn));
325 LoadStringW(hInst, IDS_SELECT_ALL, szSelectAll, _countof(szSelectAll));
326
327 m_hWnd = CreateWindowExW(0, TOOLBARCLASSNAMEW, NULL,
328 WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | TBSTYLE_LIST,
329 0, 0, 0, 0,
330 hwndParent,
331 0, hInst, NULL);
332
333 if (!m_hWnd)
334 {
335 /* TODO: Show error message */
336 return FALSE;
337 }
338
339 SendMessageW(TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_HIDECLIPPEDBUTTONS);
340 SetButtonStructSize();
341
342 /* Set image list */
343 HIMAGELIST hImageList = InitImageList();
344
345 if (!hImageList)
346 {
347 /* TODO: Show error message */
348 return FALSE;
349 }
350
351 ImageList_Destroy((HIMAGELIST) SetImageList(hImageList));
352
353 AddButtons(_countof(Buttons), Buttons);
354
355 /* Remember ideal width to use as a max width of buttons */
356 SIZE size;
357 GetIdealSize(FALSE, &size);
358 m_dButtonsWidthMax = size.cx;
359
360 return m_hWnd;
361 }
362
363 VOID HideButtonCaption()
364 {
365 DWORD dCurrentExStyle = (DWORD) SendMessageW(TB_GETEXTENDEDSTYLE, 0, 0);
366 SendMessageW(TB_SETEXTENDEDSTYLE, 0, dCurrentExStyle | TBSTYLE_EX_MIXEDBUTTONS);
367 }
368
369 VOID ShowButtonCaption()
370 {
371 DWORD dCurrentExStyle = (DWORD) SendMessageW(TB_GETEXTENDEDSTYLE, 0, 0);
372 SendMessageW(TB_SETEXTENDEDSTYLE, 0, dCurrentExStyle & ~TBSTYLE_EX_MIXEDBUTTONS);
373 }
374
375 DWORD GetMaxButtonsWidth() const
376 {
377 return m_dButtonsWidthMax;
378 }
379 };
380
381 class CAppsListView :
382 public CUiWindow<CListView>
383 {
384 struct SortContext
385 {
386 CAppsListView * lvw;
387 INT iSubItem;
388 };
389
390 BOOL bHasAllChecked;
391 BOOL bIsAscending;
392 BOOL bHasCheckboxes;
393
394 INT nLastHeaderID;
395
396 public:
397 CAppsListView() :
398 bHasAllChecked(FALSE),
399 bIsAscending(TRUE),
400 bHasCheckboxes(FALSE),
401 nLastHeaderID(-1)
402 {
403 }
404
405 VOID SetCheckboxesVisible(BOOL bIsVisible)
406 {
407 if (bIsVisible)
408 {
409 SetExtendedListViewStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
410 }
411 else
412 {
413 SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);
414 }
415
416 bHasCheckboxes = bIsVisible;
417 }
418
419 VOID ColumnClick(LPNMLISTVIEW pnmv)
420 {
421 HWND hHeader;
422 HDITEMW hColumn;
423 INT nHeaderID = pnmv->iSubItem;
424
425 if ((GetWindowLongPtr(GWL_STYLE) & ~LVS_NOSORTHEADER) == 0)
426 return;
427
428 hHeader = (HWND) SendMessage(LVM_GETHEADER, 0, 0);
429 ZeroMemory(&hColumn, sizeof(hColumn));
430
431 /* If the sorting column changed, remove the sorting style from the old column */
432 if ((nLastHeaderID != -1) && (nLastHeaderID != nHeaderID))
433 {
434 hColumn.mask = HDI_FORMAT;
435 Header_GetItem(hHeader, nLastHeaderID, &hColumn);
436 hColumn.fmt &= ~(HDF_SORTUP | HDF_SORTDOWN);
437 Header_SetItem(hHeader, nLastHeaderID, &hColumn);
438 }
439
440 /* Set the sorting style to the new column */
441 hColumn.mask = HDI_FORMAT;
442 Header_GetItem(hHeader, nHeaderID, &hColumn);
443
444 hColumn.fmt &= (bIsAscending ? ~HDF_SORTDOWN : ~HDF_SORTUP);
445 hColumn.fmt |= (bIsAscending ? HDF_SORTUP : HDF_SORTDOWN);
446 Header_SetItem(hHeader, nHeaderID, &hColumn);
447
448 /* Sort the list, using the current values of nHeaderID and bIsAscending */
449 SortContext ctx = {this, nHeaderID};
450 SortItems(s_CompareFunc, &ctx);
451
452 /* Save new values */
453 nLastHeaderID = nHeaderID;
454 bIsAscending = !bIsAscending;
455 }
456
457 PVOID GetLParam(INT Index)
458 {
459 INT ItemIndex;
460 LVITEMW Item;
461
462 if (Index == -1)
463 {
464 ItemIndex = (INT) SendMessage(LVM_GETNEXTITEM, -1, LVNI_FOCUSED);
465 if (ItemIndex == -1)
466 return NULL;
467 }
468 else
469 {
470 ItemIndex = Index;
471 }
472
473 ZeroMemory(&Item, sizeof(Item));
474
475 Item.mask = LVIF_PARAM;
476 Item.iItem = ItemIndex;
477 if (!GetItem(&Item))
478 return NULL;
479
480 return (PVOID) Item.lParam;
481 }
482
483 BOOL AddColumn(INT Index, ATL::CStringW& Text, INT Width, INT Format)
484 {
485 return AddColumn(Index, const_cast<LPWSTR>(Text.GetString()), Width, Format);
486 }
487
488 BOOL AddColumn(INT Index, LPWSTR lpText, INT Width, INT Format)
489 {
490 LVCOLUMNW Column;
491
492 ZeroMemory(&Column, sizeof(Column));
493
494 Column.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
495 Column.iSubItem = Index;
496 Column.pszText = lpText;
497 Column.cx = Width;
498 Column.fmt = Format;
499
500 return (InsertColumn(Index, &Column) == -1) ? FALSE : TRUE;
501 }
502
503 INT AddItem(INT ItemIndex, INT IconIndex, LPWSTR lpText, LPARAM lParam)
504 {
505 LVITEMW Item;
506
507 ZeroMemory(&Item, sizeof(Item));
508
509 Item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
510 Item.pszText = lpText;
511 Item.lParam = lParam;
512 Item.iItem = ItemIndex;
513 Item.iImage = IconIndex;
514
515 return InsertItem(&Item);
516 }
517
518 static INT CALLBACK s_CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
519 {
520 SortContext * ctx = ((SortContext*) lParamSort);
521 return ctx->lvw->CompareFunc(lParam1, lParam2, ctx->iSubItem);
522 }
523
524 INT CompareFunc(LPARAM lParam1, LPARAM lParam2, INT iSubItem)
525 {
526 ATL::CStringW Item1, Item2;
527 LVFINDINFOW IndexInfo;
528 INT Index;
529
530 IndexInfo.flags = LVFI_PARAM;
531
532 IndexInfo.lParam = lParam1;
533 Index = FindItem(-1, &IndexInfo);
534 GetItemText(Index, iSubItem, Item1.GetBuffer(MAX_STR_LEN), MAX_STR_LEN);
535 Item1.ReleaseBuffer();
536
537 IndexInfo.lParam = lParam2;
538 Index = FindItem(-1, &IndexInfo);
539 GetItemText(Index, iSubItem, Item2.GetBuffer(MAX_STR_LEN), MAX_STR_LEN);
540 Item2.ReleaseBuffer();
541
542 if (bIsAscending)
543 return Item2 == Item1;
544 else
545 return Item1 == Item2;
546
547 return 0;
548 }
549
550 HWND Create(HWND hwndParent)
551 {
552 RECT r = {205, 28, 465, 250};
553 DWORD style = WS_CHILD | WS_VISIBLE | LVS_SORTASCENDING | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS;
554 HMENU menu = GetSubMenu(LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_APPLICATIONMENU)), 0);
555
556 HWND hwnd = CListView::Create(hwndParent, r, NULL, style, WS_EX_CLIENTEDGE, menu);
557
558 if (hwnd)
559 {
560 SetCheckboxesVisible(FALSE);
561 }
562
563 return hwnd;
564 }
565
566 BOOL GetCheckState(INT item)
567 {
568 return (BOOL) (GetItemState(item, LVIS_STATEIMAGEMASK) >> 12) - 1;
569 }
570
571 VOID SetCheckState(INT item, BOOL fCheck)
572 {
573 if (bHasCheckboxes)
574 {
575 SetItemState(item, INDEXTOSTATEIMAGEMASK((fCheck) ? 2 : 1), LVIS_STATEIMAGEMASK);
576 SetSelected(item, fCheck);
577 }
578 }
579
580 VOID SetSelected(INT item, BOOL value)
581 {
582 if (item < 0)
583 {
584 for (INT i = 0; i >= 0; i = GetNextItem(i, LVNI_ALL))
585 {
586 CAvailableApplicationInfo* pAppInfo = (CAvailableApplicationInfo*) GetItemData(i);
587 if (pAppInfo)
588 {
589 pAppInfo->m_IsSelected = value;
590 }
591 }
592 }
593 else
594 {
595 CAvailableApplicationInfo* pAppInfo = (CAvailableApplicationInfo*) GetItemData(item);
596 if (pAppInfo)
597 {
598 pAppInfo->m_IsSelected = value;
599 }
600 }
601 }
602
603 VOID CheckAll()
604 {
605 if (bHasCheckboxes)
606 {
607 bHasAllChecked = !bHasAllChecked;
608 SetCheckState(-1, bHasAllChecked);
609 }
610 }
611
612 ATL::CSimpleArray<CAvailableApplicationInfo> GetCheckedItems()
613 {
614 if (!bHasCheckboxes)
615 {
616 return ATL::CSimpleArray<CAvailableApplicationInfo>();
617 }
618
619 ATL::CSimpleArray<CAvailableApplicationInfo> list;
620 for (INT i = 0; i >= 0; i = GetNextItem(i, LVNI_ALL))
621 {
622 if (GetCheckState(i) != FALSE)
623 {
624 CAvailableApplicationInfo* pAppInfo = (CAvailableApplicationInfo*) GetItemData(i);
625 list.Add(*pAppInfo);
626 }
627 }
628 return list;
629 }
630
631 CAvailableApplicationInfo* GetSelectedData()
632 {
633 INT item = GetSelectionMark();
634 return (CAvailableApplicationInfo*) GetItemData(item);
635 }
636 };
637
638 class CSideTreeView :
639 public CUiWindow<CTreeView>
640 {
641 HIMAGELIST hImageTreeView;
642
643 public:
644 CSideTreeView() :
645 CUiWindow(),
646 hImageTreeView(ImageList_Create(TREEVIEW_ICON_SIZE, TREEVIEW_ICON_SIZE,
647 GetSystemColorDepth() | ILC_MASK,
648 0, 1))
649 {
650 }
651
652 HTREEITEM AddItem(HTREEITEM hParent, ATL::CStringW &Text, INT Image, INT SelectedImage, LPARAM lParam)
653 {
654 return CUiWindow<CTreeView>::AddItem(hParent, const_cast<LPWSTR>(Text.GetString()), Image, SelectedImage, lParam);
655 }
656
657 HTREEITEM AddCategory(HTREEITEM hRootItem, UINT TextIndex, UINT IconIndex)
658 {
659 ATL::CStringW szText;
660 INT Index;
661 HICON hIcon;
662
663 hIcon = (HICON) LoadImageW(hInst,
664 MAKEINTRESOURCE(IconIndex),
665 IMAGE_ICON,
666 TREEVIEW_ICON_SIZE,
667 TREEVIEW_ICON_SIZE,
668 LR_CREATEDIBSECTION);
669 if (hIcon)
670 {
671 Index = ImageList_AddIcon(hImageTreeView, hIcon);
672 DestroyIcon(hIcon);
673 }
674
675 szText.LoadStringW(TextIndex);
676 return AddItem(hRootItem, szText, Index, Index, TextIndex);
677 }
678
679 HIMAGELIST SetImageList()
680 {
681 return CUiWindow<CTreeView>::SetImageList(hImageTreeView, TVSIL_NORMAL);
682 }
683
684 VOID DestroyImageList()
685 {
686 if (hImageTreeView)
687 ImageList_Destroy(hImageTreeView);
688 }
689
690 ~CSideTreeView()
691 {
692 DestroyImageList();
693 }
694 };
695
696 class CSearchBar :
697 public CWindow
698 {
699 public:
700 const INT m_Width;
701 const INT m_Height;
702
703 CSearchBar() : m_Width(200), m_Height(22)
704 {
705 }
706
707 VOID SetText(LPCWSTR lpszText)
708 {
709 SendMessageW(SB_SETTEXT, SBT_NOBORDERS, (LPARAM) lpszText);
710 }
711
712 HWND Create(HWND hwndParent)
713 {
714 ATL::CStringW szBuf;
715 m_hWnd = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", NULL,
716 WS_CHILD | WS_VISIBLE | ES_LEFT | ES_AUTOHSCROLL,
717 0, 0, m_Width, m_Height,
718 hwndParent, (HMENU) NULL,
719 hInst, 0);
720
721 SendMessageW(WM_SETFONT, (WPARAM) GetStockObject(DEFAULT_GUI_FONT), 0);
722 szBuf.LoadStringW(IDS_SEARCH_TEXT);
723 SetWindowTextW(szBuf);
724 return m_hWnd;
725 }
726
727 };
728
729 class CMainWindow :
730 public CWindowImpl<CMainWindow, CWindow, CFrameWinTraits>
731 {
732 CUiPanel* m_ClientPanel;
733 CUiSplitPanel* m_VSplitter;
734 CUiSplitPanel* m_HSplitter;
735
736 CMainToolbar* m_Toolbar;
737 CAppsListView* m_ListView;
738
739 CSideTreeView* m_TreeView;
740 CUiWindow<CStatusBar>* m_StatusBar;
741 CUiWindow<CRichEdit>* m_RichEdit;
742
743 CUiWindow<CSearchBar>* m_SearchBar;
744 CAvailableApps m_AvailableApps;
745
746 LPWSTR pLink;
747
748 INT nSelectedApps;
749
750 BOOL bSearchEnabled;
751 BOOL bUpdating;
752
753 public:
754 CMainWindow() :
755 m_ClientPanel(NULL),
756 pLink(NULL),
757 bSearchEnabled(FALSE)
758 {
759 }
760
761 private:
762 VOID InitApplicationsList()
763 {
764 ATL::CStringW szText;
765
766 /* Add columns to ListView */
767 szText.LoadStringW(IDS_APP_NAME);
768 m_ListView->AddColumn(0, szText, 250, LVCFMT_LEFT);
769
770 szText.LoadStringW(IDS_APP_INST_VERSION);
771 m_ListView->AddColumn(1, szText, 90, LVCFMT_RIGHT);
772
773 szText.LoadStringW(IDS_APP_DESCRIPTION);
774 m_ListView->AddColumn(3, szText, 300, LVCFMT_LEFT);
775
776 // Unnesesary since the list updates on every TreeView selection
777 // UpdateApplicationsList(ENUM_ALL_COMPONENTS);
778 }
779
780 HTREEITEM AddCategory(HTREEITEM hRootItem, UINT TextIndex, UINT IconIndex)
781 {
782 return m_TreeView->AddCategory(hRootItem, TextIndex, IconIndex);
783 }
784
785 VOID InitCategoriesList()
786 {
787 HTREEITEM hRootItemInstalled, hRootItemAvailable;
788
789 hRootItemInstalled = AddCategory(TVI_ROOT, IDS_INSTALLED, IDI_CATEGORY);
790 AddCategory(hRootItemInstalled, IDS_APPLICATIONS, IDI_APPS);
791 AddCategory(hRootItemInstalled, IDS_UPDATES, IDI_APPUPD);
792
793 AddCategory(TVI_ROOT, IDS_SELECTEDFORINST, IDI_SELECTEDFORINST);
794
795 hRootItemAvailable = AddCategory(TVI_ROOT, IDS_AVAILABLEFORINST, IDI_CATEGORY);
796 AddCategory(hRootItemAvailable, IDS_CAT_AUDIO, IDI_CAT_AUDIO);
797 AddCategory(hRootItemAvailable, IDS_CAT_VIDEO, IDI_CAT_VIDEO);
798 AddCategory(hRootItemAvailable, IDS_CAT_GRAPHICS, IDI_CAT_GRAPHICS);
799 AddCategory(hRootItemAvailable, IDS_CAT_GAMES, IDI_CAT_GAMES);
800 AddCategory(hRootItemAvailable, IDS_CAT_INTERNET, IDI_CAT_INTERNET);
801 AddCategory(hRootItemAvailable, IDS_CAT_OFFICE, IDI_CAT_OFFICE);
802 AddCategory(hRootItemAvailable, IDS_CAT_DEVEL, IDI_CAT_DEVEL);
803 AddCategory(hRootItemAvailable, IDS_CAT_EDU, IDI_CAT_EDU);
804 AddCategory(hRootItemAvailable, IDS_CAT_ENGINEER, IDI_CAT_ENGINEER);
805 AddCategory(hRootItemAvailable, IDS_CAT_FINANCE, IDI_CAT_FINANCE);
806 AddCategory(hRootItemAvailable, IDS_CAT_SCIENCE, IDI_CAT_SCIENCE);
807 AddCategory(hRootItemAvailable, IDS_CAT_TOOLS, IDI_CAT_TOOLS);
808 AddCategory(hRootItemAvailable, IDS_CAT_DRIVERS, IDI_CAT_DRIVERS);
809 AddCategory(hRootItemAvailable, IDS_CAT_LIBS, IDI_CAT_LIBS);
810 AddCategory(hRootItemAvailable, IDS_CAT_OTHER, IDI_CAT_OTHER);
811
812 m_TreeView->SetImageList();
813 m_TreeView->Expand(hRootItemInstalled, TVE_EXPAND);
814 m_TreeView->Expand(hRootItemAvailable, TVE_EXPAND);
815 m_TreeView->SelectItem(hRootItemAvailable);
816 }
817
818 BOOL CreateStatusBar()
819 {
820 m_StatusBar = new CUiWindow<CStatusBar>();
821 m_StatusBar->m_VerticalAlignment = UiAlign_RightBtm;
822 m_StatusBar->m_HorizontalAlignment = UiAlign_Stretch;
823 m_ClientPanel->Children().Append(m_StatusBar);
824
825 return m_StatusBar->Create(m_hWnd, (HMENU) IDC_STATUSBAR) != NULL;
826 }
827
828 BOOL CreateToolbar()
829 {
830 m_Toolbar = new CMainToolbar();
831 m_Toolbar->m_VerticalAlignment = UiAlign_LeftTop;
832 m_Toolbar->m_HorizontalAlignment = UiAlign_Stretch;
833 m_ClientPanel->Children().Append(m_Toolbar);
834
835 return m_Toolbar->Create(m_hWnd) != NULL;
836 }
837
838 BOOL CreateTreeView()
839 {
840 m_TreeView = new CSideTreeView();
841 m_TreeView->m_VerticalAlignment = UiAlign_Stretch;
842 m_TreeView->m_HorizontalAlignment = UiAlign_Stretch;
843 m_VSplitter->First().Append(m_TreeView);
844
845 return m_TreeView->Create(m_hWnd) != NULL;
846 }
847
848 BOOL CreateListView()
849 {
850 m_ListView = new CAppsListView();
851 m_ListView->m_VerticalAlignment = UiAlign_Stretch;
852 m_ListView->m_HorizontalAlignment = UiAlign_Stretch;
853 m_HSplitter->First().Append(m_ListView);
854
855 hListView = m_ListView->Create(m_hWnd);
856 return hListView != NULL;
857 }
858
859 BOOL CreateRichEdit()
860 {
861 m_RichEdit = new CUiWindow<CRichEdit>();
862 m_RichEdit->m_VerticalAlignment = UiAlign_Stretch;
863 m_RichEdit->m_HorizontalAlignment = UiAlign_Stretch;
864 m_HSplitter->Second().Append(m_RichEdit);
865
866 return m_RichEdit->Create(m_hWnd) != NULL;
867 }
868
869 BOOL CreateVSplitter()
870 {
871 m_VSplitter = new CUiSplitPanel();
872 m_VSplitter->m_VerticalAlignment = UiAlign_Stretch;
873 m_VSplitter->m_HorizontalAlignment = UiAlign_Stretch;
874 m_VSplitter->m_DynamicFirst = FALSE;
875 m_VSplitter->m_Horizontal = FALSE;
876 m_VSplitter->m_MinFirst = 0;
877 m_VSplitter->m_MinSecond = 320;
878 m_VSplitter->m_Pos = 240;
879 m_ClientPanel->Children().Append(m_VSplitter);
880
881 return m_VSplitter->Create(m_hWnd) != NULL;
882 }
883
884 BOOL CreateHSplitter()
885 {
886 m_HSplitter = new CUiSplitPanel();
887 m_HSplitter->m_VerticalAlignment = UiAlign_Stretch;
888 m_HSplitter->m_HorizontalAlignment = UiAlign_Stretch;
889 m_HSplitter->m_DynamicFirst = TRUE;
890 m_HSplitter->m_Horizontal = TRUE;
891 m_HSplitter->m_Pos = INT_MAX; //set INT_MAX to use lowest possible position (m_MinSecond)
892 m_HSplitter->m_MinFirst = 10;
893 m_HSplitter->m_MinSecond = 140;
894 m_VSplitter->Second().Append(m_HSplitter);
895
896 return m_HSplitter->Create(m_hWnd) != NULL;
897 }
898
899 BOOL CreateSearchBar()
900 {
901 m_SearchBar = new CUiWindow<CSearchBar>();
902 m_SearchBar->m_VerticalAlignment = UiAlign_LeftTop;
903 m_SearchBar->m_HorizontalAlignment = UiAlign_RightBtm;
904 m_SearchBar->m_Margin.top = 4;
905 m_SearchBar->m_Margin.right = 6;
906
907 return m_SearchBar->Create(m_Toolbar->m_hWnd) != NULL;
908 }
909
910 BOOL CreateLayout()
911 {
912 BOOL b = TRUE;
913 bUpdating = TRUE;
914
915 m_ClientPanel = new CUiPanel();
916 m_ClientPanel->m_VerticalAlignment = UiAlign_Stretch;
917 m_ClientPanel->m_HorizontalAlignment = UiAlign_Stretch;
918
919 // Top level
920 b = b && CreateStatusBar();
921 b = b && CreateToolbar();
922 b = b && CreateSearchBar();
923 b = b && CreateVSplitter();
924
925 // Inside V Splitter
926 b = b && CreateHSplitter();
927 b = b && CreateTreeView();
928
929 // Inside H Splitter
930 b = b && CreateListView();
931 b = b && CreateRichEdit();
932
933 if (b)
934 {
935 RECT rTop;
936 RECT rBottom;
937
938 /* Size status bar */
939 m_StatusBar->SendMessageW(WM_SIZE, 0, 0);
940
941 /* Size tool bar */
942 m_Toolbar->AutoSize();
943
944 ::GetWindowRect(m_Toolbar->m_hWnd, &rTop);
945 ::GetWindowRect(m_StatusBar->m_hWnd, &rBottom);
946
947 m_VSplitter->m_Margin.top = rTop.bottom - rTop.top;
948 m_VSplitter->m_Margin.bottom = rBottom.bottom - rBottom.top;
949 }
950
951 bUpdating = FALSE;
952 return b;
953 }
954
955 BOOL InitControls()
956 {
957 if (CreateLayout())
958 {
959
960 InitApplicationsList();
961 InitCategoriesList();
962
963 nSelectedApps = 0;
964 UpdateStatusBarText();
965
966 return TRUE;
967 }
968
969 return FALSE;
970 }
971
972 VOID OnSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
973 {
974 if (wParam == SIZE_MINIMIZED)
975 return;
976
977 /* Size status bar */
978 m_StatusBar->SendMessage(WM_SIZE, 0, 0);
979
980 /* Size tool bar */
981 m_Toolbar->AutoSize();
982
983 /* Automatically hide captions */
984 DWORD dToolbarTreshold = m_Toolbar->GetMaxButtonsWidth();
985 DWORD dSearchbarMargin = (LOWORD(lParam) - m_SearchBar->m_Width);
986
987 if (dSearchbarMargin > dToolbarTreshold)
988 {
989 m_Toolbar->ShowButtonCaption();
990 }
991 else if (dSearchbarMargin < dToolbarTreshold)
992 {
993 m_Toolbar->HideButtonCaption();
994 }
995
996 RECT r = {0, 0, LOWORD(lParam), HIWORD(lParam)};
997 HDWP hdwp = NULL;
998 INT count = m_ClientPanel->CountSizableChildren();
999
1000 hdwp = BeginDeferWindowPos(count);
1001 if (hdwp)
1002 {
1003 hdwp = m_ClientPanel->OnParentSize(r, hdwp);
1004 if (hdwp)
1005 {
1006 EndDeferWindowPos(hdwp);
1007 }
1008
1009 }
1010
1011 // TODO: Sub-layouts for children of children
1012 count = m_SearchBar->CountSizableChildren();
1013 hdwp = BeginDeferWindowPos(count);
1014 if (hdwp)
1015 {
1016 hdwp = m_SearchBar->OnParentSize(r, hdwp);
1017 if (hdwp)
1018 {
1019 EndDeferWindowPos(hdwp);
1020 }
1021 }
1022
1023 }
1024
1025 BOOL ProcessWindowMessage(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT& theResult, DWORD dwMapId)
1026 {
1027 theResult = 0;
1028 switch (Msg)
1029 {
1030 case WM_CREATE:
1031 if (!InitControls())
1032 ::PostMessageW(hwnd, WM_CLOSE, 0, 0);
1033 break;
1034
1035 case WM_DESTROY:
1036 {
1037 ShowWindow(SW_HIDE);
1038 SaveSettings(hwnd);
1039
1040 FreeLogs();
1041 m_AvailableApps.FreeCachedEntries();
1042
1043 if (IsInstalledEnum(SelectedEnumType))
1044 FreeInstalledAppList();
1045
1046 delete m_ClientPanel;
1047
1048 PostQuitMessage(0);
1049 return 0;
1050 }
1051
1052 case WM_COMMAND:
1053 OnCommand(wParam, lParam);
1054 break;
1055
1056 case WM_NOTIFY:
1057 {
1058 LPNMHDR data = (LPNMHDR) lParam;
1059
1060 switch (data->code)
1061 {
1062 case TVN_SELCHANGED:
1063 {
1064 if (data->hwndFrom == m_TreeView->m_hWnd)
1065 {
1066 switch (((LPNMTREEVIEW) lParam)->itemNew.lParam)
1067 {
1068 case IDS_INSTALLED:
1069 UpdateApplicationsList(ENUM_ALL_INSTALLED);
1070 break;
1071
1072 case IDS_APPLICATIONS:
1073 UpdateApplicationsList(ENUM_INSTALLED_APPLICATIONS);
1074 break;
1075
1076 case IDS_UPDATES:
1077 UpdateApplicationsList(ENUM_UPDATES);
1078 break;
1079
1080 case IDS_AVAILABLEFORINST:
1081 UpdateApplicationsList(ENUM_ALL_AVAILABLE);
1082 break;
1083
1084 case IDS_CAT_AUDIO:
1085 UpdateApplicationsList(ENUM_CAT_AUDIO);
1086 break;
1087
1088 case IDS_CAT_DEVEL:
1089 UpdateApplicationsList(ENUM_CAT_DEVEL);
1090 break;
1091
1092 case IDS_CAT_DRIVERS:
1093 UpdateApplicationsList(ENUM_CAT_DRIVERS);
1094 break;
1095
1096 case IDS_CAT_EDU:
1097 UpdateApplicationsList(ENUM_CAT_EDU);
1098 break;
1099
1100 case IDS_CAT_ENGINEER:
1101 UpdateApplicationsList(ENUM_CAT_ENGINEER);
1102 break;
1103
1104 case IDS_CAT_FINANCE:
1105 UpdateApplicationsList(ENUM_CAT_FINANCE);
1106 break;
1107
1108 case IDS_CAT_GAMES:
1109 UpdateApplicationsList(ENUM_CAT_GAMES);
1110 break;
1111
1112 case IDS_CAT_GRAPHICS:
1113 UpdateApplicationsList(ENUM_CAT_GRAPHICS);
1114 break;
1115
1116 case IDS_CAT_INTERNET:
1117 UpdateApplicationsList(ENUM_CAT_INTERNET);
1118 break;
1119
1120 case IDS_CAT_LIBS:
1121 UpdateApplicationsList(ENUM_CAT_LIBS);
1122 break;
1123
1124 case IDS_CAT_OFFICE:
1125 UpdateApplicationsList(ENUM_CAT_OFFICE);
1126 break;
1127
1128 case IDS_CAT_OTHER:
1129 UpdateApplicationsList(ENUM_CAT_OTHER);
1130 break;
1131
1132 case IDS_CAT_SCIENCE:
1133 UpdateApplicationsList(ENUM_CAT_SCIENCE);
1134 break;
1135
1136 case IDS_CAT_TOOLS:
1137 UpdateApplicationsList(ENUM_CAT_TOOLS);
1138 break;
1139
1140 case IDS_CAT_VIDEO:
1141 UpdateApplicationsList(ENUM_CAT_VIDEO);
1142 break;
1143
1144 case IDS_SELECTEDFORINST:
1145 UpdateApplicationsList(ENUM_CAT_SELECTED);
1146 break;
1147 }
1148 }
1149
1150 HMENU mainMenu = ::GetMenu(hwnd);
1151 HMENU lvwMenu = ::GetMenu(m_ListView->m_hWnd);
1152
1153 /* Disable/enable items based on treeview selection */
1154 if (IsSelectedNodeInstalled())
1155 {
1156 EnableMenuItem(mainMenu, ID_REGREMOVE, MF_ENABLED);
1157 EnableMenuItem(mainMenu, ID_INSTALL, MF_GRAYED);
1158 EnableMenuItem(mainMenu, ID_UNINSTALL, MF_ENABLED);
1159 EnableMenuItem(mainMenu, ID_MODIFY, MF_ENABLED);
1160
1161 EnableMenuItem(lvwMenu, ID_REGREMOVE, MF_ENABLED);
1162 EnableMenuItem(lvwMenu, ID_INSTALL, MF_GRAYED);
1163 EnableMenuItem(lvwMenu, ID_UNINSTALL, MF_ENABLED);
1164 EnableMenuItem(lvwMenu, ID_MODIFY, MF_ENABLED);
1165
1166 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_REGREMOVE, TRUE);
1167 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_INSTALL, FALSE);
1168 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_UNINSTALL, TRUE);
1169 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_MODIFY, TRUE);
1170 }
1171 else
1172 {
1173 EnableMenuItem(mainMenu, ID_REGREMOVE, MF_GRAYED);
1174 EnableMenuItem(mainMenu, ID_INSTALL, MF_ENABLED);
1175 EnableMenuItem(mainMenu, ID_UNINSTALL, MF_GRAYED);
1176 EnableMenuItem(mainMenu, ID_MODIFY, MF_GRAYED);
1177
1178 EnableMenuItem(lvwMenu, ID_REGREMOVE, MF_GRAYED);
1179 EnableMenuItem(lvwMenu, ID_INSTALL, MF_ENABLED);
1180 EnableMenuItem(lvwMenu, ID_UNINSTALL, MF_GRAYED);
1181 EnableMenuItem(lvwMenu, ID_MODIFY, MF_GRAYED);
1182
1183 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_REGREMOVE, FALSE);
1184 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_INSTALL, TRUE);
1185 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_UNINSTALL, FALSE);
1186 m_Toolbar->SendMessageW(TB_ENABLEBUTTON, ID_MODIFY, FALSE);
1187 }
1188 }
1189 break;
1190
1191 case LVN_ITEMCHANGED:
1192 {
1193 LPNMLISTVIEW pnic = (LPNMLISTVIEW) lParam;
1194
1195 if (pnic->hdr.hwndFrom == m_ListView->m_hWnd)
1196 {
1197 /* Check if this is a valid item
1198 * (technically, it can be also an unselect) */
1199 INT ItemIndex = pnic->iItem;
1200 if (ItemIndex == -1 ||
1201 ItemIndex >= ListView_GetItemCount(pnic->hdr.hwndFrom))
1202 {
1203 break;
1204 }
1205
1206 /* Check if the focus has been moved to another item */
1207 if ((pnic->uChanged & LVIF_STATE) &&
1208 (pnic->uNewState & LVIS_FOCUSED) &&
1209 !(pnic->uOldState & LVIS_FOCUSED))
1210 {
1211 if (IsInstalledEnum(SelectedEnumType))
1212 ShowInstalledAppInfo(ItemIndex);
1213 if (IsAvailableEnum(SelectedEnumType))
1214 CAvailableAppView::ShowAvailableAppInfo(ItemIndex);
1215 }
1216 /* Check if the item is checked */
1217 if ((pnic->uNewState & LVIS_STATEIMAGEMASK) && !bUpdating)
1218 {
1219 BOOL checked = m_ListView->GetCheckState(pnic->iItem);
1220 /* FIXME: HAX!
1221 - preventing decremention below zero as a safeguard for ReactOS
1222 In ReactOS this action is triggered whenever user changes *selection*, but should be only when *checkbox* state toggled
1223 Maybe LVIS_STATEIMAGEMASK is set incorrectly
1224 */
1225 nSelectedApps +=
1226 (checked)
1227 ? 1
1228 : ((nSelectedApps > 0)
1229 ? -1
1230 : 0);
1231
1232 /* Update item's selection status */
1233 m_ListView->SetSelected(pnic->iItem, checked);
1234
1235 UpdateStatusBarText();
1236 }
1237 }
1238 }
1239 break;
1240
1241 case LVN_COLUMNCLICK:
1242 {
1243 LPNMLISTVIEW pnmv = (LPNMLISTVIEW) lParam;
1244
1245 m_ListView->ColumnClick(pnmv);
1246 }
1247 break;
1248
1249 case NM_CLICK:
1250 {
1251 if (data->hwndFrom == m_ListView->m_hWnd && ((LPNMLISTVIEW) lParam)->iItem != -1)
1252 {
1253 if (IsInstalledEnum(SelectedEnumType))
1254 ShowInstalledAppInfo(-1);
1255 if (IsAvailableEnum(SelectedEnumType))
1256 CAvailableAppView::ShowAvailableAppInfo(-1);
1257 }
1258 }
1259 break;
1260
1261 case NM_DBLCLK:
1262 {
1263 if (data->hwndFrom == m_ListView->m_hWnd && ((LPNMLISTVIEW) lParam)->iItem != -1)
1264 {
1265 /* this won't do anything if the program is already installed */
1266 SendMessageW(hwnd, WM_COMMAND, ID_INSTALL, 0);
1267 }
1268 }
1269 break;
1270
1271 case NM_RCLICK:
1272 {
1273 if (data->hwndFrom == m_ListView->m_hWnd && ((LPNMLISTVIEW) lParam)->iItem != -1)
1274 {
1275 ShowPopupMenu(m_ListView->m_hWnd, 0, ID_INSTALL);
1276 }
1277 }
1278 break;
1279
1280 case EN_LINK:
1281 OnLink((ENLINK*) lParam);
1282 break;
1283
1284 case TTN_GETDISPINFO:
1285 m_Toolbar->OnGetDispInfo((LPTOOLTIPTEXT) lParam);
1286 break;
1287 }
1288 }
1289 break;
1290
1291 case WM_SIZE:
1292 OnSize(hwnd, wParam, lParam);
1293 break;
1294
1295 case WM_SIZING:
1296 {
1297 LPRECT pRect = (LPRECT) lParam;
1298
1299 if (pRect->right - pRect->left < 565)
1300 pRect->right = pRect->left + 565;
1301
1302 if (pRect->bottom - pRect->top < 300)
1303 pRect->bottom = pRect->top + 300;
1304
1305 return TRUE;
1306 }
1307
1308 case WM_SYSCOLORCHANGE:
1309 {
1310 /* Forward WM_SYSCOLORCHANGE to common controls */
1311 m_ListView->SendMessageW(WM_SYSCOLORCHANGE, 0, 0);
1312 m_TreeView->SendMessageW(WM_SYSCOLORCHANGE, 0, 0);
1313 m_Toolbar->SendMessageW(WM_SYSCOLORCHANGE, 0, 0);
1314 m_ListView->SendMessageW(EM_SETBKGNDCOLOR, 0, GetSysColor(COLOR_BTNFACE));
1315 }
1316 break;
1317
1318 case WM_TIMER:
1319 if (wParam == SEARCH_TIMER_ID)
1320 {
1321 ::KillTimer(hwnd, SEARCH_TIMER_ID);
1322 if (bSearchEnabled)
1323 UpdateApplicationsList(-1);
1324 }
1325 break;
1326 }
1327
1328 return FALSE;
1329 }
1330
1331 virtual VOID OnLink(ENLINK *Link)
1332 {
1333 switch (Link->msg)
1334 {
1335 case WM_LBUTTONUP:
1336 case WM_RBUTTONUP:
1337 {
1338 if (pLink) HeapFree(GetProcessHeap(), 0, pLink);
1339
1340 pLink = (LPWSTR) HeapAlloc(GetProcessHeap(), 0,
1341 (max(Link->chrg.cpMin, Link->chrg.cpMax) -
1342 min(Link->chrg.cpMin, Link->chrg.cpMax) + 1) * sizeof(WCHAR));
1343 if (!pLink)
1344 {
1345 /* TODO: Error message */
1346 return;
1347 }
1348
1349 m_RichEdit->SendMessageW(EM_SETSEL, Link->chrg.cpMin, Link->chrg.cpMax);
1350 m_RichEdit->SendMessageW(EM_GETSELTEXT, 0, (LPARAM) pLink);
1351
1352 ShowPopupMenu(m_RichEdit->m_hWnd, IDR_LINKMENU, -1);
1353 }
1354 break;
1355 }
1356 }
1357
1358 BOOL IsSelectedNodeInstalled()
1359 {
1360 HTREEITEM hSelectedItem = m_TreeView->GetSelection();
1361 TV_ITEM tItem;
1362
1363 tItem.mask = TVIF_PARAM | TVIF_HANDLE;
1364 tItem.hItem = hSelectedItem;
1365 m_TreeView->GetItem(&tItem);
1366 switch (tItem.lParam)
1367 {
1368 case IDS_INSTALLED:
1369 case IDS_APPLICATIONS:
1370 case IDS_UPDATES:
1371 return TRUE;
1372 default:
1373 return FALSE;
1374 }
1375 }
1376
1377 VOID OnCommand(WPARAM wParam, LPARAM lParam)
1378 {
1379 WORD wCommand = LOWORD(wParam);
1380
1381 if (lParam == (LPARAM) m_SearchBar->m_hWnd)
1382 {
1383 ATL::CStringW szBuf;
1384
1385 switch (HIWORD(wParam))
1386 {
1387 case EN_SETFOCUS:
1388 {
1389 ATL::CStringW szWndText;
1390
1391 szBuf.LoadStringW(IDS_SEARCH_TEXT);
1392 m_SearchBar->GetWindowTextW(szWndText);
1393 if (szBuf == szWndText)
1394 {
1395 bSearchEnabled = FALSE;
1396 m_SearchBar->SetWindowTextW(L"");
1397 }
1398 }
1399 break;
1400
1401 case EN_KILLFOCUS:
1402 {
1403 m_SearchBar->GetWindowTextW(szBuf);
1404 if (szBuf.IsEmpty())
1405 {
1406 szBuf.LoadStringW(IDS_SEARCH_TEXT);
1407 bSearchEnabled = FALSE;
1408 m_SearchBar->SetWindowTextW(szBuf.GetString());
1409 }
1410 }
1411 break;
1412
1413 case EN_CHANGE:
1414 {
1415 ATL::CStringW szWndText;
1416
1417 if (!bSearchEnabled)
1418 {
1419 bSearchEnabled = TRUE;
1420 break;
1421 }
1422
1423 szBuf.LoadStringW(IDS_SEARCH_TEXT);
1424 m_SearchBar->GetWindowTextW(szWndText);
1425 if (szBuf == szWndText)
1426 {
1427 szSearchPattern.Empty();
1428 }
1429 else
1430 {
1431 szSearchPattern = szWndText;
1432 }
1433
1434 DWORD dwDelay;
1435 SystemParametersInfoW(SPI_GETMENUSHOWDELAY, 0, &dwDelay, 0);
1436 SetTimer(SEARCH_TIMER_ID, dwDelay);
1437 }
1438 break;
1439 }
1440
1441 return;
1442 }
1443
1444 switch (wCommand)
1445 {
1446 case ID_OPEN_LINK:
1447 ShellExecuteW(m_hWnd, L"open", pLink, NULL, NULL, SW_SHOWNOACTIVATE);
1448 HeapFree(GetProcessHeap(), 0, pLink);
1449 break;
1450
1451 case ID_COPY_LINK:
1452 CopyTextToClipboard(pLink);
1453 HeapFree(GetProcessHeap(), 0, pLink);
1454 break;
1455
1456 case ID_SETTINGS:
1457 CreateSettingsDlg(m_hWnd);
1458 break;
1459
1460 case ID_EXIT:
1461 PostMessageW(WM_CLOSE, 0, 0);
1462 break;
1463
1464 case ID_INSTALL:
1465 if (IsAvailableEnum(SelectedEnumType))
1466 {
1467 if (nSelectedApps > 0)
1468 {
1469 CDownloadManager::DownloadListOfApplications(m_AvailableApps.GetSelected());
1470 UpdateApplicationsList(-1);
1471 }
1472 else if (CDownloadManager::DownloadApplication(m_ListView->GetSelectedData()))
1473 {
1474 UpdateApplicationsList(-1);
1475 }
1476
1477 }
1478 break;
1479
1480 case ID_UNINSTALL:
1481 if (UninstallApplication(-1, FALSE))
1482 UpdateApplicationsList(-1);
1483 break;
1484
1485 case ID_MODIFY:
1486 if (UninstallApplication(-1, TRUE))
1487 UpdateApplicationsList(-1);
1488 break;
1489
1490 case ID_REGREMOVE:
1491 RemoveAppFromRegistry(-1);
1492 break;
1493
1494 case ID_REFRESH:
1495 UpdateApplicationsList(-1);
1496 break;
1497
1498 case ID_RESETDB:
1499 CAvailableApps::ForceUpdateAppsDB();
1500 UpdateApplicationsList(-1);
1501 break;
1502
1503 case ID_HELP:
1504 MessageBoxW(L"Help not implemented yet", NULL, MB_OK);
1505 break;
1506
1507 case ID_ABOUT:
1508 ShowAboutDialog();
1509 break;
1510
1511 case ID_CHECK_ALL:
1512 m_ListView->CheckAll();
1513 break;
1514 }
1515 }
1516
1517 VOID FreeInstalledAppList()
1518 {
1519 INT Count = m_ListView->GetItemCount() - 1;
1520 PINSTALLED_INFO Info;
1521
1522 while (Count >= 0)
1523 {
1524 Info = (PINSTALLED_INFO) ListViewGetlParam(Count);
1525 if (Info)
1526 {
1527 RegCloseKey(Info->hSubKey);
1528 delete Info;
1529 }
1530 Count--;
1531 }
1532 }
1533
1534 static BOOL SearchPatternMatch(LPCWSTR szHaystack, LPCWSTR szNeedle)
1535 {
1536 if (!*szNeedle)
1537 return TRUE;
1538 /* TODO: Improve pattern search beyond a simple case-insensitive substring search. */
1539 return StrStrIW(szHaystack, szNeedle) != NULL;
1540 }
1541
1542 static BOOL CALLBACK s_EnumInstalledAppProc(INT ItemIndex, ATL::CStringW &m_szName, PINSTALLED_INFO Info)
1543 {
1544 PINSTALLED_INFO ItemInfo;
1545 ATL::CStringW szText;
1546 INT Index;
1547
1548 if (!SearchPatternMatch(m_szName.GetString(), szSearchPattern))
1549 {
1550 RegCloseKey(Info->hSubKey);
1551 return TRUE;
1552 }
1553
1554 ItemInfo = new INSTALLED_INFO(*Info);
1555 if (!ItemInfo)
1556 {
1557 RegCloseKey(Info->hSubKey);
1558 return FALSE;
1559 }
1560
1561 Index = ListViewAddItem(ItemIndex, 0, m_szName, (LPARAM) ItemInfo);
1562
1563 /* Get version info */
1564 GetApplicationString(ItemInfo->hSubKey, L"DisplayVersion", szText);
1565 ListView_SetItemText(hListView, Index, 1, const_cast<LPWSTR>(szText.GetString()));
1566
1567 /* Get comments */
1568 GetApplicationString(ItemInfo->hSubKey, L"Comments", szText);
1569 ListView_SetItemText(hListView, Index, 2, const_cast<LPWSTR>(szText.GetString()));
1570
1571 return TRUE;
1572 }
1573
1574 static BOOL CALLBACK s_EnumAvailableAppProc(CAvailableApplicationInfo* Info, LPCWSTR szFolderPath)
1575 {
1576 INT Index;
1577 HICON hIcon = NULL;
1578
1579 HIMAGELIST hImageListView = ListView_GetImageList(hListView, LVSIL_SMALL);
1580
1581 if (!SearchPatternMatch(Info->m_szName.GetString(), szSearchPattern) &&
1582 !SearchPatternMatch(Info->m_szDesc.GetString(), szSearchPattern))
1583 {
1584 return TRUE;
1585 }
1586
1587 /* Load icon from file */
1588 ATL::CStringW szIconPath;
1589 szIconPath.Format(L"%lsicons\\%ls.ico", szFolderPath, Info->m_szName.GetString());
1590 hIcon = (HICON) LoadImageW(NULL,
1591 szIconPath.GetString(),
1592 IMAGE_ICON,
1593 LISTVIEW_ICON_SIZE,
1594 LISTVIEW_ICON_SIZE,
1595 LR_LOADFROMFILE);
1596
1597 if (!hIcon || GetLastError() != ERROR_SUCCESS)
1598 {
1599 /* Load default icon */
1600 hIcon = (HICON) LoadIconW(hInst, MAKEINTRESOURCEW(IDI_MAIN));
1601 }
1602
1603 Index = ImageList_AddIcon(hImageListView, hIcon);
1604 DestroyIcon(hIcon);
1605
1606 Index = ListViewAddItem(Info->m_Category, Index, Info->m_szName.GetString(), (LPARAM) Info);
1607 ListView_SetImageList(hListView, hImageListView, LVSIL_SMALL);
1608
1609 ListView_SetItemText(hListView, Index, 1, const_cast<LPWSTR>(Info->m_szVersion.GetString()));
1610 ListView_SetItemText(hListView, Index, 2, const_cast<LPWSTR>(Info->m_szDesc.GetString()));
1611 ListView_SetCheckState(hListView, Index, Info->m_IsSelected);
1612
1613 return TRUE;
1614 }
1615
1616 VOID UpdateStatusBarText()
1617 {
1618 if (m_StatusBar)
1619 {
1620 ATL::CStringW szBuffer;
1621
1622 szBuffer.Format(IDS_APPS_COUNT, m_ListView->GetItemCount(), nSelectedApps);
1623 m_StatusBar->SetText(szBuffer);
1624 }
1625 }
1626
1627 VOID UpdateApplicationsList(INT EnumType)
1628 {
1629 ATL::CStringW szBuffer1, szBuffer2;
1630 HIMAGELIST hImageListView;
1631 BOOL bWasInInstalled = IsInstalledEnum(SelectedEnumType);
1632
1633 bUpdating = TRUE;
1634 m_ListView->SetRedraw(FALSE);
1635
1636 if (EnumType < 0)
1637 {
1638 EnumType = SelectedEnumType;
1639 }
1640
1641 //if previous one was INSTALLED purge the list
1642 //TODO: make the Installed category a separate class to avoid doing this
1643 if (bWasInInstalled)
1644 {
1645 FreeInstalledAppList();
1646 }
1647
1648 m_ListView->DeleteAllItems();
1649
1650 // Create new ImageList
1651 hImageListView = ImageList_Create(LISTVIEW_ICON_SIZE,
1652 LISTVIEW_ICON_SIZE,
1653 GetSystemColorDepth() | ILC_MASK,
1654 0, 1);
1655 HIMAGELIST hImageListBuf = m_ListView->SetImageList(hImageListView, LVSIL_SMALL);
1656 if (hImageListBuf)
1657 {
1658 ImageList_Destroy(hImageListBuf);
1659 }
1660
1661 if (IsInstalledEnum(EnumType))
1662 {
1663 if (!bWasInInstalled)
1664 {
1665 m_ListView->SetCheckboxesVisible(FALSE);
1666 }
1667
1668 HICON hIcon = (HICON) LoadIconW(hInst, MAKEINTRESOURCEW(IDI_MAIN));
1669 ImageList_AddIcon(hImageListView, hIcon);
1670 DestroyIcon(hIcon);
1671
1672 // Enum installed applications and updates
1673 EnumInstalledApplications(EnumType, TRUE, s_EnumInstalledAppProc);
1674 EnumInstalledApplications(EnumType, FALSE, s_EnumInstalledAppProc);
1675 }
1676 else if (IsAvailableEnum(EnumType))
1677 {
1678 if (bWasInInstalled)
1679 {
1680 m_ListView->SetCheckboxesVisible(TRUE);
1681 }
1682
1683 // Enum available applications
1684 m_AvailableApps.Enum(EnumType, s_EnumAvailableAppProc);
1685 }
1686
1687 SelectedEnumType = EnumType;
1688 UpdateStatusBarText();
1689 SetWelcomeText();
1690
1691 // Set automatic column width for program names if the list is not empty
1692 if (m_ListView->GetItemCount() > 0)
1693 {
1694 ListView_SetColumnWidth(m_ListView->GetWindow(), 0, LVSCW_AUTOSIZE);
1695 }
1696
1697 bUpdating = FALSE;
1698 m_ListView->SetRedraw(TRUE);
1699 }
1700
1701 public:
1702 static ATL::CWndClassInfo& GetWndClassInfo()
1703 {
1704 DWORD csStyle = CS_VREDRAW | CS_HREDRAW;
1705 static ATL::CWndClassInfo wc =
1706 {
1707 {
1708 sizeof(WNDCLASSEX),
1709 csStyle,
1710 StartWindowProc,
1711 0,
1712 0,
1713 NULL,
1714 LoadIconW(_AtlBaseModule.GetModuleInstance(), MAKEINTRESOURCEW(IDI_MAIN)),
1715 LoadCursorW(NULL, IDC_ARROW),
1716 (HBRUSH) (COLOR_BTNFACE + 1),
1717 MAKEINTRESOURCEW(IDR_MAINMENU),
1718 L"RAppsWnd",
1719 NULL
1720 },
1721 NULL, NULL, IDC_ARROW, TRUE, 0, _T("")
1722 };
1723 return wc;
1724 }
1725
1726 HWND Create()
1727 {
1728 ATL::CStringW szWindowName;
1729 szWindowName.LoadStringW(IDS_APPTITLE);
1730
1731 RECT r = {
1732 (SettingsInfo.bSaveWndPos ? SettingsInfo.Left : CW_USEDEFAULT),
1733 (SettingsInfo.bSaveWndPos ? SettingsInfo.Top : CW_USEDEFAULT),
1734 (SettingsInfo.bSaveWndPos ? SettingsInfo.Width : 680),
1735 (SettingsInfo.bSaveWndPos ? SettingsInfo.Height : 450)
1736 };
1737 r.right += r.left;
1738 r.bottom += r.top;
1739
1740 return CWindowImpl::Create(NULL, r, szWindowName.GetString(), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
1741 }
1742
1743 CStatusBar * GetStatusBar()
1744 {
1745 return m_StatusBar;
1746 }
1747
1748 CAppsListView * GetListView()
1749 {
1750 return m_ListView;
1751 }
1752
1753 CRichEdit * GetRichEdit()
1754 {
1755 return m_RichEdit;
1756 }
1757
1758 CAvailableApps * GetAvailableApps()
1759 {
1760 return &m_AvailableApps;
1761 }
1762 };
1763
1764 // global interface
1765 CMainWindow * g_MainWindow;
1766
1767 HWND CreateMainWindow()
1768 {
1769 g_MainWindow = new CMainWindow();
1770 return g_MainWindow->Create();
1771 }
1772
1773 DWORD_PTR ListViewGetlParam(INT item)
1774 {
1775 if (item < 0)
1776 {
1777 item = g_MainWindow->GetListView()->GetSelectionMark();
1778 }
1779 return g_MainWindow->GetListView()->GetItemData(item);
1780 }
1781
1782 VOID SetStatusBarText(LPCWSTR szText)
1783 {
1784 g_MainWindow->GetStatusBar()->SetText(szText);
1785 }
1786
1787 INT ListViewAddItem(INT ItemIndex, INT IconIndex, LPWSTR lpName, LPARAM lParam)
1788 {
1789 return g_MainWindow->GetListView()->AddItem(ItemIndex, IconIndex, lpName, lParam);
1790 }
1791
1792 VOID NewRichEditText(LPCWSTR szText, DWORD flags)
1793 {
1794 g_MainWindow->GetRichEdit()->SetText(szText, flags);
1795 }
1796
1797 VOID InsertRichEditText(LPCWSTR szText, DWORD flags)
1798 {
1799 g_MainWindow->GetRichEdit()->InsertText(szText, flags);
1800 }
1801
1802 CAvailableApps* GetAvailableApps()
1803 {
1804 return g_MainWindow->GetAvailableApps();
1805 }
1806
1807 // ATL version of functions above
1808 VOID SetStatusBarText(const ATL::CStringW& szText)
1809 {
1810 SetStatusBarText(szText.GetString());
1811 }
1812
1813 INT ListViewAddItem(INT ItemIndex, INT IconIndex, const ATL::CStringW& Name, LPARAM lParam)
1814 {
1815 return ListViewAddItem(ItemIndex, IconIndex, const_cast<LPWSTR>(Name.GetString()), lParam);
1816 }
1817
1818 VOID NewRichEditText(const ATL::CStringW& szText, DWORD flags)
1819 {
1820 NewRichEditText(szText.GetString(), flags);
1821 }
1822
1823 VOID InsertRichEditText(const ATL::CStringW& szText, DWORD flags)
1824 {
1825 InsertRichEditText(szText.GetString(), flags);
1826 }