1 #pragma once 2 3 #include "rapps.h" 4 #include "rosui.h" 5 #include "crichedit.h" 6 #include "asyncinet.h" 7 8 #include <shlobj_undoc.h> 9 #include <shlguid_undoc.h> 10 11 #include <atlbase.h> 12 #include <atlcom.h> 13 #include <atltypes.h> 14 #include <atlwin.h> 15 #include <wininet.h> 16 #include <shellutils.h> 17 #include <rosctrls.h> 18 #include <gdiplus.h> 19 #include <math.h> 20 21 using namespace Gdiplus; 22 23 #define LISTVIEW_ICON_SIZE 32 24 25 // default broken-image icon size 26 #define BROKENIMG_ICON_SIZE 96 27 28 // the boundary of w/h ratio of scrnshot preview window 29 #define SCRNSHOT_MAX_ASPECT_RAT 2.5 30 31 // padding between scrnshot preview and richedit (in pixel) 32 #define INFO_DISPLAY_PADDING 10 33 34 // minimum width of richedit 35 #define RICHEDIT_MIN_WIDTH 160 36 37 // padding between controls in toolbar 38 #define TOOLBAR_PADDING 6 39 40 // user-defined window message 41 #define WM_RAPPS_DOWNLOAD_COMPLETE (WM_USER + 1) // notify download complete. wParam is error code, and lParam is a pointer to ScrnshotDownloadParam 42 #define WM_RAPPS_RESIZE_CHILDREN (WM_USER + 2) // ask parent window to resize children. 43 44 enum SCRNSHOT_STATUS 45 { 46 SCRNSHOT_PREV_EMPTY, // show nothing 47 SCRNSHOT_PREV_LOADING, // image is loading (most likely downloading) 48 SCRNSHOT_PREV_IMAGE, // display image from a file 49 SCRNSHOT_PREV_FAILED // image can not be shown (download failure or wrong image) 50 }; 51 52 #define TIMER_LOADING_ANIMATION 1 // Timer ID 53 54 #define LOADING_ANIMATION_PERIOD 3 // Animation cycling period (in seconds) 55 #define LOADING_ANIMATION_FPS 18 // Animation Frame Per Second 56 57 58 #define PI 3.1415927 59 60 // retrieve the value using a mask 61 #define STATEIMAGETOINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12) 62 63 // for listview with extend style LVS_EX_CHECKBOXES, State image 1 is the unchecked box, and state image 2 is the checked box. 64 // see this: https://docs.microsoft.com/en-us/windows/win32/controls/extended-list-view-styles 65 #define STATEIMAGE_UNCHECKED 1 66 #define STATEIMAGE_CHECKED 2 67 68 class CMainWindow; 69 70 enum APPLICATION_VIEW_TYPE 71 { 72 AppViewTypeEmpty, 73 AppViewTypeAvailableApps, 74 AppViewTypeInstalledApps 75 }; 76 77 typedef struct __ScrnshotDownloadParam 78 { 79 LONGLONG ID; 80 HANDLE hFile; 81 HWND hwndNotify; 82 ATL::CStringW DownloadFileName; 83 } ScrnshotDownloadParam; 84 85 86 class CAppRichEdit : 87 public CUiWindow<CRichEdit> 88 { 89 private: 90 VOID LoadAndInsertText(UINT uStringID, 91 const ATL::CStringW &szText, 92 DWORD StringFlags, 93 DWORD TextFlags); 94 95 VOID LoadAndInsertText(UINT uStringID, 96 DWORD StringFlags); 97 98 VOID InsertVersionInfo(CAvailableApplicationInfo *Info); 99 100 VOID InsertLicenseInfo(CAvailableApplicationInfo *Info); 101 102 VOID InsertLanguageInfo(CAvailableApplicationInfo *Info); 103 104 public: 105 BOOL ShowAvailableAppInfo(CAvailableApplicationInfo *Info); 106 107 inline VOID InsertTextWithString(UINT StringID, DWORD StringFlags, const ATL::CStringW &Text, DWORD TextFlags); 108 109 BOOL ShowInstalledAppInfo(CInstalledApplicationInfo *Info); 110 111 VOID SetWelcomeText(); 112 }; 113 114 int ScrnshotDownloadCallback( 115 pASYNCINET AsyncInet, 116 ASYNC_EVENT Event, 117 WPARAM wParam, 118 LPARAM lParam, 119 VOID *Extension 120 ); 121 122 class CAppScrnshotPreview : 123 public CWindowImpl<CAppScrnshotPreview> 124 { 125 private: 126 127 SCRNSHOT_STATUS ScrnshotPrevStauts = SCRNSHOT_PREV_EMPTY; 128 Image *pImage = NULL; 129 HICON hBrokenImgIcon = NULL; 130 BOOL bLoadingTimerOn = FALSE; 131 int LoadingAnimationFrame = 0; 132 int BrokenImgSize = BROKENIMG_ICON_SIZE; 133 pASYNCINET AsyncInet = NULL; 134 LONGLONG ContentID = 0; // used to determine whether image has been switched when download complete. Increase by 1 each time the content of this window changed 135 ATL::CStringW TempImagePath; // currently displayed temp file 136 137 BOOL ProcessWindowMessage(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT &theResult, DWORD dwMapId); 138 139 VOID DisplayLoading(); 140 141 VOID DisplayFailed(); 142 143 BOOL DisplayFile(LPCWSTR lpszFileName); 144 145 VOID SetStatus(SCRNSHOT_STATUS Status); 146 147 VOID PaintOnDC(HDC hdc, int width, int height, BOOL bDrawBkgnd); 148 149 float GetLoadingDotWidth(int width, int height); 150 151 float GetFrameDotShift(int Frame, int width, int height); 152 153 public: 154 static ATL::CWndClassInfo &GetWndClassInfo(); 155 156 HWND Create(HWND hParent); 157 158 VOID PreviousDisplayCleanup(); 159 160 VOID DisplayEmpty(); 161 162 BOOL DisplayImage(LPCWSTR lpszLocation); 163 164 // calculate requested window width by given height 165 int GetRequestedWidth(int Height); 166 167 ~CAppScrnshotPreview(); 168 }; 169 170 class CAppInfoDisplay : 171 public CUiWindow<CWindowImpl<CAppInfoDisplay>> 172 { 173 LPWSTR pLink = NULL; 174 175 private: 176 BOOL ProcessWindowMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT &theResult, DWORD dwMapId); 177 178 VOID ResizeChildren(); 179 180 VOID ResizeChildren(int Width, int Height); 181 182 VOID OnLink(ENLINK *Link); 183 184 public: 185 186 CAppRichEdit *RichEdit = NULL; 187 CAppScrnshotPreview *ScrnshotPrev = NULL; 188 189 static ATL::CWndClassInfo &GetWndClassInfo(); 190 191 HWND Create(HWND hwndParent); 192 193 BOOL ShowAvailableAppInfo(CAvailableApplicationInfo *Info); 194 195 BOOL ShowInstalledAppInfo(CInstalledApplicationInfo *Info); 196 197 VOID SetWelcomeText(); 198 199 VOID OnCommand(WPARAM wParam, LPARAM lParam); 200 201 ~CAppInfoDisplay(); 202 }; 203 204 class CAppsListView : 205 public CUiWindow<CListView> 206 { 207 struct SortContext 208 { 209 CAppsListView *lvw; 210 INT iSubItem; 211 }; 212 213 BOOL bIsAscending = TRUE; 214 BOOL bHasCheckboxes = FALSE; 215 216 INT ItemCount = 0; 217 INT CheckedItemCount = 0; 218 INT ColumnCount = 0; 219 220 INT nLastHeaderID = -1; 221 222 APPLICATION_VIEW_TYPE ApplicationViewType = AppViewTypeEmpty; 223 224 HIMAGELIST m_hImageListView = NULL; 225 226 public: 227 CAppsListView(); 228 ~CAppsListView(); 229 230 VOID SetCheckboxesVisible(BOOL bIsVisible); 231 232 VOID ColumnClick(LPNMLISTVIEW pnmv); 233 234 BOOL AddColumn(INT Index, ATL::CStringW &Text, INT Width, INT Format); 235 236 int AddColumn(INT Index, LPWSTR lpText, INT Width, INT Format); 237 238 void DeleteColumn(INT Index); 239 240 INT AddItem(INT ItemIndex, INT IconIndex, LPCWSTR lpText, LPARAM lParam); 241 242 HIMAGELIST GetImageList(int iImageList); 243 244 static INT CALLBACK s_CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort); 245 246 INT CompareFunc(LPARAM lParam1, LPARAM lParam2, INT iSubItem); 247 248 HWND Create(HWND hwndParent); 249 250 BOOL GetCheckState(INT item); 251 252 VOID SetCheckState(INT item, BOOL fCheck); 253 254 VOID CheckAll(); 255 256 PVOID GetFocusedItemData(); 257 258 BOOL SetDisplayAppType(APPLICATION_VIEW_TYPE AppType); 259 260 BOOL SetViewMode(DWORD ViewMode); 261 262 BOOL AddInstalledApplication(CInstalledApplicationInfo *InstAppInfo, LPVOID CallbackParam); 263 264 BOOL AddAvailableApplication(CAvailableApplicationInfo *AvlbAppInfo, BOOL InitCheckState, LPVOID CallbackParam); 265 266 // this function is called when parent window receiving an notification about checkstate changing 267 VOID ItemCheckStateNotify(int iItem, BOOL bCheck); 268 }; 269 270 class CMainToolbar : 271 public CUiWindow< CToolbar<> > 272 { 273 const INT m_iToolbarHeight; 274 DWORD m_dButtonsWidthMax; 275 276 WCHAR szInstallBtn[MAX_STR_LEN]; 277 WCHAR szUninstallBtn[MAX_STR_LEN]; 278 WCHAR szModifyBtn[MAX_STR_LEN]; 279 WCHAR szSelectAll[MAX_STR_LEN]; 280 281 VOID AddImageToImageList(HIMAGELIST hImageList, UINT ImageIndex); 282 283 HIMAGELIST InitImageList(); 284 285 public: 286 287 CMainToolbar(); 288 289 VOID OnGetDispInfo(LPTOOLTIPTEXT lpttt); 290 291 HWND Create(HWND hwndParent); 292 293 VOID HideButtonCaption(); 294 295 VOID ShowButtonCaption(); 296 297 DWORD GetMaxButtonsWidth() const; 298 }; 299 300 class CSearchBar : 301 public CWindow 302 { 303 public: 304 const INT m_Width; 305 const INT m_Height; 306 307 CSearchBar(); 308 309 VOID SetText(LPCWSTR lpszText); 310 311 HWND Create(HWND hwndParent); 312 313 }; 314 315 class CComboBox : 316 public CWindow 317 { 318 // ID refers to different types of view 319 enum 320 { m_AppDisplayTypeDetails, m_AppDisplayTypeList, m_AppDisplayTypeTile }; 321 322 // string ID for different. this should correspond with the enum above. 323 const UINT m_TypeStringID[3] = 324 { IDS_APP_DISPLAY_DETAILS, IDS_APP_DISPLAY_LIST, IDS_APP_DISPLAY_TILE }; 325 326 const int m_DefaultSelectType = m_AppDisplayTypeDetails; 327 public: 328 329 int m_Width; 330 int m_Height; 331 332 CComboBox(); 333 334 HWND Create(HWND hwndParent); 335 }; 336 337 class CApplicationView : 338 public CUiWindow<CWindowImpl<CApplicationView>> 339 { 340 private: 341 CUiPanel *m_Panel = NULL; 342 CMainToolbar *m_Toolbar = NULL; 343 CUiWindow<CComboBox> *m_ComboBox = NULL; 344 CUiWindow<CSearchBar> *m_SearchBar = NULL; 345 CAppsListView *m_ListView = NULL; 346 CAppInfoDisplay *m_AppsInfo = NULL; 347 CUiSplitPanel *m_HSplitter = NULL; 348 CMainWindow *m_MainWindow = NULL; 349 APPLICATION_VIEW_TYPE ApplicationViewType = AppViewTypeEmpty; 350 351 BOOL ProcessWindowMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT &theResult, DWORD dwMapId); 352 353 BOOL CreateToolbar(); 354 BOOL CreateSearchBar(); 355 BOOL CreateComboBox(); 356 BOOL CreateHSplitter(); 357 BOOL CreateListView(); 358 BOOL CreateAppInfoDisplay(); 359 360 VOID OnSize(HWND hwnd, WPARAM wParam, LPARAM lParam); 361 VOID OnCommand(WPARAM wParam, LPARAM lParam); 362 public: 363 364 CApplicationView(CMainWindow *MainWindow); 365 ~CApplicationView(); 366 367 static ATL::CWndClassInfo &GetWndClassInfo(); 368 369 HWND Create(HWND hwndParent); 370 void SetRedraw(BOOL bRedraw); 371 BOOL SetDisplayAppType(APPLICATION_VIEW_TYPE AppType); 372 373 BOOL AddInstalledApplication(CInstalledApplicationInfo *InstAppInfo, LPVOID param); 374 BOOL AddAvailableApplication(CAvailableApplicationInfo *AvlbAppInfo, BOOL InitCheckState, LPVOID param); 375 376 void CheckAll(); 377 PVOID GetFocusedItemData(); 378 int GetItemCount(); 379 VOID AppendTabOrderWindow(int Direction, ATL::CSimpleArray<HWND> &TabOrderList); 380 381 // this function is called when a item of listview get focus. 382 // CallbackParam is the param passed to listview when adding the item (the one getting focus now). 383 BOOL ItemGetFocus(LPVOID CallbackParam); 384 385 // this function is called when a item of listview is checked/unchecked 386 // CallbackParam is the param passed to listview when adding the item (the one getting focus now). 387 BOOL ItemCheckStateChanged(BOOL bChecked, LPVOID CallbackParam); 388 389 }; 390