1
0

fileview_toolbar.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include "./fileview.h"
  2. #include "./fileview_internal.h"
  3. #include "./resource.h"
  4. #include "../nu/menushortcuts.h"
  5. #include <windowsx.h>
  6. #include <strsafe.h>
  7. #define FVTOOLBAR_DATAW L"FVTOOLBAR"
  8. typedef struct _FVTOOLBAR
  9. {
  10. HMENU hMenu;
  11. } FVTOOLBAR;
  12. #define GetToolbar(__hwnd) ((FVTOOLBAR*)GetPropW((__hwnd), FVTOOLBAR_DATAW))
  13. static INT_PTR CALLBACK FileViewToolbar_DialogProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  14. static HMLIMGLST hmlilModes = NULL;
  15. HWND FileViewToolbar_Create(HWND hwndParent)
  16. {
  17. HWND hwnd = WASABI_API_CREATEDIALOGPARAMW(IDD_FILEVIEW_TOOLBAR, hwndParent, FileViewToolbar_DialogProc, 0L);
  18. return hwnd;
  19. }
  20. static LRESULT FileViewToolbar_NotifyParent(HWND hdlg, UINT uCode, NMHDR *phdr)
  21. {
  22. HWND hParent = GetParent(hdlg);
  23. if (!phdr || !hParent) return 0L;
  24. phdr->code = uCode;
  25. phdr->hwndFrom = hdlg;
  26. phdr->idFrom = GetDlgCtrlID(hdlg);
  27. return SendMessageW(hParent, WM_NOTIFY, (WPARAM)phdr->idFrom, (LPARAM)phdr);
  28. }
  29. static void FileViewToolbar_LoadImages()
  30. {
  31. MLIMAGESOURCE_I mlis;
  32. if (NULL != hmlilModes) return;
  33. ZeroMemory(&mlis, sizeof(MLIMAGESOURCE_I));
  34. mlis.type = SRC_TYPE_PNG;
  35. mlis.hInst = plugin.hDllInstance;
  36. hmlilModes = MLImageListI_Create(18, 12, MLILC_COLOR32, 3, 2, 3, hmlifMngr);
  37. if (NULL != hmlilModes)
  38. {
  39. INT imageList[] = { IDB_FILEVIEW_ICON, IDB_FILEVIEW_LIST, IDB_FILEVIEW_DETAIL };
  40. for(int i = 0; i < sizeof(imageList)/sizeof(imageList[0]); i++)
  41. {
  42. mlis.lpszName = MAKEINTRESOURCEW(imageList[i]);
  43. MLImageListI_Add(hmlilModes, &mlis, MLIF_BUTTONBLENDPLUSCOLOR_UID, imageList[i]);
  44. }
  45. }
  46. }
  47. static void FileViewToolbar_LayoutWindows(HWND hdlg, BOOL bRedraw)
  48. {
  49. INT buttonList[] = { IDC_BTN_VIEW_ICON, IDC_BTN_VIEW_LIST, IDC_BTN_VIEW_DETAIL, IDC_BTN_ARRANGEBY, IDC_BTN_OPTIONS, };
  50. HDWP hdwp;
  51. DWORD flags, size;
  52. RECT rc;
  53. if (!GetClientRect(hdlg, &rc)) return;
  54. flags = SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW;
  55. hdwp = BeginDeferWindowPos(sizeof(buttonList)/sizeof(buttonList[0]));
  56. if (!hdwp) return;
  57. for(int i =0; i < sizeof(buttonList)/sizeof(buttonList[0]); i++)
  58. {
  59. HWND hctrl = GetDlgItem(hdlg, buttonList[i]);
  60. if (NULL != hctrl)
  61. {
  62. size = MLSkinnedButton_GetIdealSize(hctrl, NULL);
  63. INT width = LOWORD(size);
  64. hdwp = DeferWindowPos(hdwp, hctrl, NULL, 0, 0, width, rc.bottom - rc.top, flags);
  65. }
  66. }
  67. EndDeferWindowPos(hdwp);
  68. SetWindowPos(hdlg, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE |
  69. SWP_NOZORDER | SWP_FRAMECHANGED | ((bRedraw) ? 0 : SWP_NOREDRAW));
  70. }
  71. static BOOL FileViewToolbar_OnInitDialog(HWND hdlg, HWND hwndFocus, LPARAM lParam)
  72. {
  73. FVTOOLBAR *ptb;
  74. ptb = (FVTOOLBAR*)calloc(1, sizeof(FVTOOLBAR));
  75. if (ptb)
  76. {
  77. if (!SetPropW(hdlg, FVTOOLBAR_DATAW, ptb))
  78. {
  79. free(ptb);
  80. ptb = NULL;
  81. }
  82. }
  83. if (!ptb)
  84. {
  85. DestroyWindow(hdlg);
  86. return 0;
  87. }
  88. FileViewToolbar_LoadImages();
  89. HWND hctrl;
  90. SkinWindowEx(hdlg, SKINNEDWND_TYPE_AUTO, SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWS_USESKINFONT);
  91. hctrl = GetDlgItem(hdlg, IDC_BTN_VIEW_ICON);
  92. SkinWindowEx(hctrl, SKINNEDWND_TYPE_BUTTON, SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWS_USESKINFONT | SWBS_TOOLBAR);
  93. MLSkinnedButton_SetImageList(hctrl, hmlilModes, 0, 0, 0, 0);
  94. hctrl = GetDlgItem(hdlg, IDC_BTN_VIEW_LIST);
  95. SkinWindowEx(hctrl, SKINNEDWND_TYPE_BUTTON, SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWS_USESKINFONT | SWBS_TOOLBAR);
  96. MLSkinnedButton_SetImageList(hctrl, hmlilModes, 1, 1, 1, 1);
  97. hctrl = GetDlgItem(hdlg, IDC_BTN_VIEW_DETAIL);
  98. SkinWindowEx(hctrl, SKINNEDWND_TYPE_BUTTON, SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWS_USESKINFONT | SWBS_TOOLBAR);
  99. MLSkinnedButton_SetImageList(hctrl, hmlilModes, 2, 2, 2, 2);
  100. hctrl = GetDlgItem(hdlg, IDC_BTN_ARRANGEBY);
  101. SkinWindowEx(hctrl, SKINNEDWND_TYPE_BUTTON, SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWS_USESKINFONT | SWBS_DROPDOWNBUTTON | SWBS_TOOLBAR);
  102. hctrl = GetDlgItem(hdlg, IDC_BTN_OPTIONS);
  103. SkinWindowEx(hctrl, SKINNEDWND_TYPE_BUTTON, SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWS_USESKINFONT | SWBS_DROPDOWNBUTTON | SWBS_TOOLBAR);
  104. FileViewToolbar_LayoutWindows(hdlg, FALSE);
  105. return FALSE;
  106. }
  107. static void FileViewToolbar_OnDestroy(HWND hdlg)
  108. {
  109. FVTOOLBAR *ptb = GetToolbar(hdlg);
  110. if (ptb)
  111. {
  112. RemovePropW(hdlg, FVTOOLBAR_DATAW);
  113. if (ptb->hMenu) DestroyMenu(ptb->hMenu);
  114. free(ptb);
  115. }
  116. }
  117. static void FileViewToolbar_OnWindowPosChanged(HWND hdlg, WINDOWPOS *pwp)
  118. {
  119. HWND hctrl;
  120. RECT rc, rw;
  121. UINT flags;
  122. LONG right, left;
  123. DWORD ws;
  124. if (SWP_NOSIZE == ((SWP_NOSIZE | SWP_FRAMECHANGED) & pwp->flags)) return;
  125. GetClientRect(hdlg, &rc);
  126. right = rc.right -2;
  127. left = rc.left + 2;
  128. rc.bottom -= 2;
  129. HDWP hdwp = BeginDeferWindowPos(4);
  130. if (NULL == hdwp) return;
  131. flags = SWP_NOACTIVATE | SWP_NOZORDER | ((SWP_NOREDRAW | SWP_NOCOPYBITS) & pwp->flags);
  132. if (NULL != (hctrl = GetDlgItem(hdlg, IDC_BTN_VIEW_ICON)) && GetWindowRect(hctrl, &rw))
  133. {
  134. hdwp = DeferWindowPos(hdwp, hctrl, NULL, left, rc.top, rw.right - rw.left, rc.bottom - rc.top, flags);
  135. left += (rw.right - rw.left) + 2;
  136. }
  137. if (NULL != (hctrl = GetDlgItem(hdlg, IDC_BTN_VIEW_LIST)) && GetWindowRect(hctrl, &rw))
  138. {
  139. hdwp = DeferWindowPos(hdwp, hctrl, NULL, left, rc.top, rw.right - rw.left, rc.bottom - rc.top, flags);
  140. left += (rw.right - rw.left) + 2;
  141. }
  142. if (NULL != (hctrl = GetDlgItem(hdlg, IDC_BTN_VIEW_DETAIL)) && GetWindowRect(hctrl, &rw))
  143. {
  144. hdwp = DeferWindowPos(hdwp, hctrl, NULL, left, rc.top, rw.right - rw.left, rc.bottom - rc.top, flags);
  145. left += (rw.right - rw.left) + 2;
  146. }
  147. if (NULL != (hctrl = GetDlgItem(hdlg, IDC_BTN_OPTIONS)) && GetWindowRect(hctrl, &rw))
  148. {
  149. right -= (rw.right - rw.left);
  150. ws = GetWindowLongPtrW(hctrl, GWL_STYLE);
  151. if (right < (left + 8)) { if (WS_VISIBLE & ws) ShowWindow(hctrl, SW_HIDE);}
  152. else { if (0 == (WS_VISIBLE & ws)) ShowWindow(hctrl, SW_SHOWNA); }
  153. hdwp = DeferWindowPos(hdwp, hctrl, NULL, right, rc.top, rw.right - rw.left, rc.bottom - rc.top, flags);
  154. right -= 4;
  155. }
  156. if (NULL != (hctrl = GetDlgItem(hdlg, IDC_BTN_ARRANGEBY)) && GetWindowRect(hctrl, &rw))
  157. {
  158. right -= (rw.right - rw.left);
  159. ws = GetWindowLongPtrW(hctrl, GWL_STYLE);
  160. if (right < (left + 8)) { if (WS_VISIBLE & ws) ShowWindow(hctrl, SW_HIDE);}
  161. else { if (0 == (WS_VISIBLE & ws)) ShowWindow(hctrl, SW_SHOWNA); }
  162. hdwp = DeferWindowPos(hdwp, hctrl, NULL, right, rc.top, rw.right - rw.left, rc.bottom - rc.top, flags);
  163. }
  164. EndDeferWindowPos(hdwp);
  165. }
  166. static INT FileViewToolbar_OnGetBestHeight(HWND hdlg)
  167. {
  168. INT height = 0;
  169. HWND hctrl;
  170. if (NULL != (hctrl = GetDlgItem(hdlg, IDC_BTN_OPTIONS)))
  171. {
  172. DWORD sz = MLSkinnedButton_GetIdealSize(hctrl, NULL);
  173. height = HIWORD(sz);
  174. }
  175. if (NULL != (hctrl = GetDlgItem(hdlg, IDC_BTN_VIEW_ICON)))
  176. {
  177. DWORD sz = MLSkinnedButton_GetIdealSize(hctrl, NULL);
  178. if (height < HIWORD(sz)) height = HIWORD(sz);
  179. }
  180. height += 1;
  181. if (height < 12) height = 12;
  182. return height;
  183. }
  184. static void FolderBrowserToolbar_NotifyViewSwitch(HWND hdlg)
  185. {
  186. INT nCmd = 0;
  187. if (BST_CHECKED == IsDlgButtonChecked(hdlg, IDC_BTN_VIEW_LIST)) nCmd = ID_FILEVIEW_SETMODE_LIST;
  188. else if (BST_CHECKED == IsDlgButtonChecked(hdlg, IDC_BTN_VIEW_ICON)) nCmd = ID_FILEVIEW_SETMODE_ICON;
  189. else if (BST_CHECKED == IsDlgButtonChecked(hdlg, IDC_BTN_VIEW_DETAIL)) nCmd = ID_FILEVIEW_SETMODE_DETAIL;
  190. SendMessageW(GetParent(hdlg), WM_COMMAND, MAKEWPARAM(nCmd, 0), 0L);
  191. }
  192. static void FileViewToolbar_InvertParentStyle(HWND hdlg, UINT style)
  193. {
  194. HWND hParent = GetParent(hdlg);
  195. if (hParent)
  196. {
  197. FileView_SetStyle(hParent, FileView_GetStyle(hParent) ^ style, style);
  198. FileView_Refresh(hParent, FALSE);
  199. }
  200. }
  201. static void FileViewToolbar_DisplayOptionsMenu(HWND hdlg, HWND hButton)
  202. {
  203. RECT r;
  204. if (!hButton || !GetWindowRect(hButton, &r))
  205. {
  206. GetCursorPos((POINT*)&r);
  207. SetRect(&r, r.left, r.top, r.left, r.top);
  208. }
  209. if (hButton) MLSkinnedButton_SetDropDownState(hButton, TRUE);
  210. FileView_DisplayPopupMenu(GetParent(hdlg), FVMENU_OPTIONS,
  211. TPM_RIGHTBUTTON | TPM_LEFTBUTTON | TPM_TOPALIGN | TPM_RIGHTALIGN | TPM_VERPOSANIMATION,
  212. *(((POINT*)&r) + 1));
  213. if (hButton) MLSkinnedButton_SetDropDownState(hButton, FALSE);
  214. }
  215. static void FileViewToolbar_DisplayArrangeByMenu(HWND hdlg, HWND hButton)
  216. {
  217. RECT r;
  218. if (!hButton || !GetWindowRect(hButton, &r))
  219. {
  220. GetCursorPos((POINT*)&r);
  221. SetRect(&r, r.left, r.top, r.left, r.top);
  222. }
  223. if (hButton) MLSkinnedButton_SetDropDownState(hButton, TRUE);
  224. FileView_DisplayPopupMenu(GetParent(hdlg),
  225. FVMENU_ARRANGEBY, TPM_RIGHTBUTTON | TPM_LEFTBUTTON | TPM_TOPALIGN | TPM_RIGHTALIGN | TPM_VERPOSANIMATION,
  226. *(((POINT*)&r) + 1));
  227. if (hButton) MLSkinnedButton_SetDropDownState(hButton, FALSE);
  228. }
  229. static void FileViewToolbar_OnCommand(HWND hdlg, INT eventId, INT ctrlId, HWND hwndCtrl)
  230. {
  231. switch (ctrlId)
  232. {
  233. case IDC_BTN_VIEW_ICON:
  234. case IDC_BTN_VIEW_LIST:
  235. case IDC_BTN_VIEW_DETAIL:
  236. switch(eventId)
  237. {
  238. case BN_CLICKED: FolderBrowserToolbar_NotifyViewSwitch(hdlg); break;
  239. }
  240. break;
  241. case IDC_BTN_OPTIONS:
  242. switch(eventId)
  243. {
  244. case MLBN_DROPDOWN: FileViewToolbar_DisplayOptionsMenu(hdlg, hwndCtrl); break;
  245. }
  246. break;
  247. case IDC_BTN_ARRANGEBY:
  248. switch(eventId)
  249. {
  250. case MLBN_DROPDOWN: FileViewToolbar_DisplayArrangeByMenu(hdlg, hwndCtrl); break;
  251. }
  252. break;
  253. }
  254. }
  255. static void FileViewToolbar_OnSetStyle(HWND hdlg, UINT uStyle, UINT uStyleMask)
  256. {
  257. if (FVS_VIEWMASK & uStyleMask)
  258. {
  259. UINT idc = ((UINT)-1);
  260. switch(FVS_VIEWMASK & uStyle)
  261. {
  262. case FVS_LISTVIEW: idc = IDC_BTN_VIEW_LIST; break;
  263. case FVS_ICONVIEW: idc = IDC_BTN_VIEW_ICON; break;
  264. case FVS_DETAILVIEW: idc = IDC_BTN_VIEW_DETAIL; break;
  265. }
  266. CheckRadioButton(hdlg, IDC_BTN_VIEW_ICON, IDC_BTN_VIEW_DETAIL, idc);
  267. }
  268. }
  269. static INT_PTR CALLBACK FileViewToolbar_DialogProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  270. {
  271. switch(uMsg)
  272. {
  273. case WM_INITDIALOG: return FileViewToolbar_OnInitDialog(hdlg, (HWND)wParam, lParam);
  274. case WM_DESTROY: FileViewToolbar_OnDestroy(hdlg); return TRUE;
  275. case WM_WINDOWPOSCHANGED: FileViewToolbar_OnWindowPosChanged(hdlg, (WINDOWPOS*)lParam); return TRUE;
  276. case WM_COMMAND: FileViewToolbar_OnCommand(hdlg, HIWORD(wParam), LOWORD(wParam), (HWND)lParam); return TRUE;
  277. case FVM_GETIDEALHEIGHT: SetWindowLongPtrW(hdlg, DWLP_MSGRESULT, FileViewToolbar_OnGetBestHeight(hdlg)); return TRUE;
  278. case WM_SETFONT: FileViewToolbar_LayoutWindows(hdlg, LOWORD(lParam)); return TRUE;
  279. case FVM_SETSTYLE: FileViewToolbar_OnSetStyle(hdlg, (UINT)lParam, (UINT)wParam); return TRUE;
  280. }
  281. return 0;
  282. }