deviceView.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. #include "main.h"
  2. #include "./deviceView.h"
  3. #include <wincodec.h>
  4. #include <commctrl.h>
  5. #include <strsafe.h>
  6. #include <vector>
  7. #include <algorithm>
  8. #define DEVICEVIEW_PROP L"NullsoftDevicesViewProp"
  9. static ATOM DEVICEVIEW_ATOM = 0;
  10. typedef struct DeviceView
  11. {
  12. Device *device;
  13. } DeviceView;
  14. typedef std::vector<DeviceIconInfo*> DeviceIconInfoList;
  15. static INT_PTR
  16. DeviceView_DialogProc(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam);
  17. #define DEVICEVIEW(_hwnd) ((DeviceView*)GetPropW((_hwnd), MAKEINTATOM(DEVICEVIEW_ATOM)))
  18. #define DEVICEVIEW_RET_VOID(_view, _hwnd) { (_view) = DEVICEVIEW((_hwnd)); if (NULL == (_view)) return; }
  19. #define DEVICEVIEW_RET_VAL(_view, _hwnd, _error) { (_view) = DEVICEVIEW((_hwnd)); if (NULL == (_view)) return (_error); }
  20. static HBITMAP
  21. DeviceView_HBitmapFromWicSource(IWICBitmapSource *wicSource)
  22. {
  23. HRESULT hr;
  24. HBITMAP bitmap;
  25. BITMAPINFO bitmapInfo;
  26. unsigned int width, height;
  27. void *pixelData = NULL;
  28. HDC windowDC;
  29. unsigned int strideSize, imageSize;
  30. if (NULL == wicSource)
  31. return NULL;
  32. hr = wicSource->GetSize(&width, &height);
  33. if (FAILED(hr))
  34. return NULL;
  35. ZeroMemory(&bitmapInfo, sizeof(bitmapInfo));
  36. bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  37. bitmapInfo.bmiHeader.biWidth = width;
  38. bitmapInfo.bmiHeader.biHeight = -(LONG)height;
  39. bitmapInfo.bmiHeader.biPlanes = 1;
  40. bitmapInfo.bmiHeader.biBitCount = 32;
  41. bitmapInfo.bmiHeader.biCompression = BI_RGB;
  42. windowDC = GetDCEx(NULL, NULL, DCX_WINDOW | DCX_CACHE);
  43. bitmap = CreateDIBSection(windowDC, &bitmapInfo, DIB_RGB_COLORS, &pixelData, NULL, 0);
  44. if (NULL != windowDC)
  45. ReleaseDC(NULL, windowDC);
  46. if (NULL == bitmap)
  47. return NULL;
  48. hr = UIntMult(width, sizeof(DWORD), &strideSize);
  49. if (SUCCEEDED(hr))
  50. {
  51. hr = UIntMult(strideSize, height, &imageSize);
  52. if (SUCCEEDED(hr))
  53. hr = wicSource->CopyPixels(NULL, strideSize, imageSize, (BYTE*)pixelData);
  54. }
  55. if (FAILED(hr))
  56. {
  57. DeleteObject(bitmap);
  58. bitmap = NULL;
  59. }
  60. return bitmap;
  61. }
  62. static HBITMAP
  63. DeviceView_LoadIcon(const wchar_t *path)
  64. {
  65. IWICImagingFactory *wicFactory;
  66. IWICBitmapDecoder *wicDecoder;
  67. HRESULT hr;
  68. HBITMAP bitmap;
  69. hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
  70. IID_PPV_ARGS(&wicFactory));
  71. if (FAILED(hr))
  72. return NULL;
  73. bitmap = NULL;
  74. hr = wicFactory->CreateDecoderFromFilename(path, NULL, GENERIC_READ,
  75. WICDecodeMetadataCacheOnDemand, &wicDecoder);
  76. if (SUCCEEDED(hr))
  77. {
  78. IWICBitmapFrameDecode *wicFrame;
  79. hr = wicDecoder->GetFrame(0, &wicFrame);
  80. if(SUCCEEDED(hr))
  81. {
  82. IWICBitmapSource *wicSource;
  83. hr = wicFrame->QueryInterface(IID_IWICBitmapSource,
  84. reinterpret_cast<void **>(&wicSource));
  85. if (SUCCEEDED(hr))
  86. {
  87. WICPixelFormatGUID pixelFormat;
  88. hr = wicSource->GetPixelFormat(&pixelFormat);
  89. if (FAILED(hr) ||
  90. (GUID_WICPixelFormat32bppPBGRA != pixelFormat &&
  91. GUID_WICPixelFormat32bppBGR != pixelFormat &&
  92. GUID_WICPixelFormat32bppBGRA != pixelFormat &&
  93. GUID_WICPixelFormat32bppRGBA != pixelFormat &&
  94. GUID_WICPixelFormat32bppPRGBA != pixelFormat))
  95. {
  96. // try to convert
  97. IWICFormatConverter *wicConverter;
  98. hr = wicFactory->CreateFormatConverter(&wicConverter);
  99. if (SUCCEEDED(hr))
  100. {
  101. hr = wicConverter->Initialize(wicSource, GUID_WICPixelFormat32bppPBGRA,
  102. WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeCustom);
  103. if (SUCCEEDED(hr))
  104. {
  105. IWICBitmapSource *wicConvertedSource;
  106. hr = wicConverter->QueryInterface(IID_IWICBitmapSource,
  107. reinterpret_cast<void **>(&wicConvertedSource));
  108. if (SUCCEEDED(hr))
  109. {
  110. wicSource->Release();
  111. wicSource = wicConvertedSource;
  112. }
  113. }
  114. wicConverter->Release();
  115. }
  116. }
  117. if (SUCCEEDED(hr))
  118. bitmap = DeviceView_HBitmapFromWicSource(wicSource);
  119. wicSource->Release();
  120. }
  121. wicFrame->Release();
  122. }
  123. wicDecoder->Release();
  124. }
  125. wicFactory->Release();
  126. return bitmap;
  127. }
  128. HWND
  129. DeviceView_CreateWindow(HWND parentWindow, Device *device)
  130. {
  131. HWND hwnd;
  132. if (NULL == device)
  133. return NULL;
  134. if (0 == DEVICEVIEW_ATOM)
  135. {
  136. DEVICEVIEW_ATOM = GlobalAddAtom(DEVICEVIEW_PROP);
  137. if (0 == DEVICEVIEW_ATOM)
  138. return NULL;
  139. }
  140. hwnd = WASABI_API_CREATEDIALOGPARAMW((INT_PTR)IDD_DEVICE_VIEW, parentWindow,
  141. DeviceView_DialogProc, (LPARAM)device);
  142. return hwnd;
  143. }
  144. static void
  145. DeviceView_InitCapacity(HWND hwnd)
  146. {
  147. DeviceView *self;
  148. HWND controlWindow;
  149. uint64_t totalSpace, usedSpace;
  150. DEVICEVIEW_RET_VOID(self, hwnd);
  151. if (NULL != self->device)
  152. {
  153. if (FAILED(self->device->GetTotalSpace(&totalSpace)))
  154. totalSpace = 0;
  155. if (FAILED(self->device->GetUsedSpace(&usedSpace)))
  156. usedSpace = 0;
  157. }
  158. else
  159. {
  160. totalSpace = 0;
  161. usedSpace = 0;
  162. }
  163. controlWindow = GetDlgItem(hwnd, IDC_SPIN_TOTALSPACE);
  164. if (NULL != controlWindow)
  165. {
  166. SendMessage(controlWindow, UDM_SETRANGE32, (WPARAM)0, (LPARAM)0xFFFFFF);
  167. SendMessage(controlWindow, UDM_SETPOS32, (WPARAM)0, (LPARAM)totalSpace);
  168. }
  169. controlWindow = GetDlgItem(hwnd, IDC_SPIN_USEDSPACE);
  170. if (NULL != controlWindow)
  171. {
  172. SendMessage(controlWindow, UDM_SETRANGE32, (WPARAM)0, (LPARAM)totalSpace);
  173. SendMessage(controlWindow, UDM_SETPOS32, (WPARAM)0, (LPARAM)usedSpace);
  174. }
  175. }
  176. static int
  177. DeviceView_CompareDeviceIconInfo(const void *elem1, const void *elem2)
  178. {
  179. DeviceIconInfo *info1;
  180. DeviceIconInfo *info2;
  181. info1 = *((DeviceIconInfo**)elem1);
  182. info2 = *((DeviceIconInfo**)elem2);
  183. if (NULL == info1 || NULL == info2)
  184. return (int)(info1 - info2);
  185. if (info1->width != info2->width)
  186. return (int)(info1->width - info2->width);
  187. if (info1->height != info2->height)
  188. return (int)(info1->height - info2->height);
  189. if (NULL == info1->path || NULL == info2->path)
  190. return (int)(info1->path - info2->path);
  191. return CompareString(CSTR_INVARIANT, NORM_IGNORECASE, info1->path, -1, info2->path, -1) - 2;
  192. }
  193. static int
  194. DeviceView_CompareDeviceIconInfo_V2(const void* elem1, const void* elem2)
  195. {
  196. return DeviceView_CompareDeviceIconInfo(elem1, elem2) < 0;
  197. }
  198. static BOOL
  199. DeviceView_EnumerateIcons(const wchar_t *path, unsigned int width, unsigned int height, void *user)
  200. {
  201. DeviceIconInfo *info;
  202. DeviceIconInfoList *list = (DeviceIconInfoList*)user;
  203. if( NULL == list)
  204. return FALSE;
  205. info = (DeviceIconInfo*)malloc(sizeof(DeviceIconInfo));
  206. if (NULL != info)
  207. {
  208. info->height = height;
  209. info->width = width;
  210. info->path = String_Duplicate(path);
  211. list->push_back(info);
  212. }
  213. return TRUE;
  214. }
  215. static void
  216. DeviceView_DestroyIcons(HWND hwnd)
  217. {
  218. HWND controlWindow;
  219. int count, index;
  220. DeviceIconInfo *info;
  221. controlWindow = GetDlgItem(hwnd, IDC_COMBO_ICONS);
  222. if (NULL == controlWindow)
  223. return;
  224. count = (int)SendMessage(controlWindow, CB_GETCOUNT, 0, 0L);
  225. for(index = 0; index < count; index++)
  226. {
  227. info = (DeviceIconInfo*)SendMessage(controlWindow, CB_GETITEMDATA, index, 0L);
  228. if (CB_ERR != (INT_PTR)info)
  229. {
  230. String_Free(info->path);
  231. free(info);
  232. }
  233. }
  234. SendMessage(controlWindow, CB_RESETCONTENT, 0, 0L);
  235. controlWindow = GetDlgItem(hwnd, IDC_STATIC_PREVIEWICON);
  236. if (NULL != controlWindow)
  237. {
  238. HBITMAP bitmap;
  239. bitmap = (HBITMAP)SendMessage(controlWindow, STM_GETIMAGE, IMAGE_BITMAP, 0L);
  240. if(NULL != bitmap)
  241. DeleteObject(bitmap);
  242. }
  243. }
  244. static void
  245. DeviceView_InitIcons(HWND hwnd, const wchar_t *selectPath)
  246. {
  247. DeviceView *self;
  248. DeviceIconInfoList list;
  249. HWND controlWindow;
  250. wchar_t buffer[2048];
  251. DEVICEVIEW_RET_VOID(self, hwnd);
  252. DeviceView_DestroyIcons(hwnd);
  253. controlWindow = GetDlgItem(hwnd, IDC_COMBO_ICONS);
  254. if (NULL != controlWindow)
  255. {
  256. size_t index, count;
  257. int iItem, iSelect;
  258. DeviceIconInfo *info;
  259. if (NULL != self->device)
  260. {
  261. self->device->EnumerateIcons(DeviceView_EnumerateIcons, &list);
  262. }
  263. count = list.size();
  264. //qsort(list.begin(), count, sizeof(DeviceIconInfo**), DeviceView_CompareDeviceIconInfo);
  265. std::sort(list.begin(), list.end(), DeviceView_CompareDeviceIconInfo_V2);
  266. iSelect = 0;
  267. for(index = 0; index < count; index++)
  268. {
  269. info = list[index];
  270. if (1 == info->width && 1 == info->height)
  271. {
  272. StringCchPrintf(buffer, ARRAYSIZE(buffer), L"%s",
  273. info->path);
  274. }
  275. else
  276. {
  277. StringCchPrintf(buffer, ARRAYSIZE(buffer), L"[%dx%d] - %s",
  278. info->width, info->height, info->path);
  279. }
  280. iItem = (int)SendMessage(controlWindow, CB_ADDSTRING, 0, (LPARAM)buffer);
  281. if (CB_ERR != iItem)
  282. {
  283. if (CB_ERR == (int)SendMessage(controlWindow, CB_SETITEMDATA, index, (LPARAM)info))
  284. {
  285. SendMessage(controlWindow, CB_DELETESTRING, index, 0L);
  286. iItem = CB_ERR;
  287. }
  288. }
  289. if (CB_ERR == iItem)
  290. {
  291. free(info);
  292. }
  293. else if (NULL != selectPath &&
  294. CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE,
  295. info->path, -1, selectPath, -1))
  296. {
  297. iSelect = iItem;
  298. }
  299. }
  300. iItem = (int)SendMessage(controlWindow, CB_GETCOUNT, 0, 0L);
  301. if (CB_ERR == iItem)
  302. iItem = 0;
  303. EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_EDITICON), (0 != iItem));
  304. EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_REMOVEICON),(0 != iItem));
  305. SendMessage(controlWindow, CB_SETCURSEL, iSelect, 0L);
  306. PostMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_COMBO_ICONS, CBN_SELENDOK), (LPARAM)controlWindow);
  307. }
  308. }
  309. static void
  310. DeviceView_NewIcon(HWND hwnd)
  311. {
  312. DeviceView *self;
  313. DeviceIconInfo info;
  314. INT_PTR result;
  315. DEVICEVIEW_RET_VOID(self, hwnd);
  316. ZeroMemory(&info, sizeof(info));
  317. result = DeviceIconEditor_Show(hwnd, &info);
  318. if (IDOK == result)
  319. {
  320. if (NULL != self->device &&
  321. SUCCEEDED(self->device->AddIcon(info.path, info.width, info.height)))
  322. {
  323. DeviceView_InitIcons(hwnd, info.path);
  324. }
  325. String_Free(info.path);
  326. }
  327. }
  328. static void
  329. DeviceView_RemoveIcon(HWND hwnd)
  330. {
  331. DeviceView *self;
  332. DeviceIconInfo *info;
  333. int index, count;
  334. HWND controlWindow;
  335. DEVICEVIEW_RET_VOID(self, hwnd);
  336. controlWindow = GetDlgItem(hwnd, IDC_COMBO_ICONS);
  337. if (NULL == controlWindow)
  338. return;
  339. index = (int)SendMessage(controlWindow, CB_GETCURSEL, 0, 0L);
  340. if (CB_ERR == index)
  341. return;
  342. info = (DeviceIconInfo*)SendMessage(controlWindow, CB_GETITEMDATA, index, 0L);
  343. if (CB_ERR != (INT_PTR)info)
  344. {
  345. if (NULL != self->device)
  346. self->device->RemoveIcon(info->width, info->height);
  347. String_Free(info->path);
  348. free(info);
  349. }
  350. SendMessage(controlWindow, CB_DELETESTRING, index, 0L);
  351. count = (int)SendMessage(controlWindow, CB_GETCOUNT, 0, 0L);
  352. if (count > 0)
  353. {
  354. if (index > count)
  355. index = count - 1;
  356. SendMessage(controlWindow, CB_SETCURSEL, index, 0L);
  357. }
  358. else
  359. {
  360. SendMessage(controlWindow, CB_SETCURSEL, -1, 0L);
  361. EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_EDITICON),FALSE);
  362. EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_REMOVEICON),FALSE);
  363. }
  364. PostMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_COMBO_ICONS, CBN_SELENDOK), (LPARAM)controlWindow);
  365. UpdateWindow(controlWindow);
  366. }
  367. static void
  368. DeviceView_EditIcon(HWND hwnd)
  369. {
  370. DeviceView *self;
  371. DeviceIconInfo *info;
  372. int index;
  373. unsigned int width, height;
  374. HWND controlWindow;
  375. INT_PTR result;
  376. DEVICEVIEW_RET_VOID(self, hwnd);
  377. controlWindow = GetDlgItem(hwnd, IDC_COMBO_ICONS);
  378. if (NULL == controlWindow)
  379. return;
  380. index = (int)SendMessage(controlWindow, CB_GETCURSEL, 0, 0L);
  381. if (CB_ERR == index)
  382. return;
  383. info = (DeviceIconInfo*)SendMessage(controlWindow, CB_GETITEMDATA, index, 0L);
  384. if (CB_ERR == (INT_PTR)info)
  385. return;
  386. width = info->width;
  387. height = info->height;
  388. result = DeviceIconEditor_Show(hwnd, info);
  389. if (IDOK == result)
  390. {
  391. if (NULL != self->device)
  392. {
  393. self->device->RemoveIcon(width, height);
  394. self->device->AddIcon(info->path, info->width, info->height);
  395. DeviceView_InitIcons(hwnd, info->path);
  396. }
  397. }
  398. UpdateWindow(controlWindow);
  399. }
  400. static void
  401. DeviceView_InitView(HWND hwnd)
  402. {
  403. DeviceView *self;
  404. HWND controlWindow;
  405. wchar_t buffer[1024];
  406. DEVICEVIEW_RET_VOID(self, hwnd);
  407. controlWindow = GetDlgItem(hwnd, IDC_EDIT_NAME);
  408. if (NULL != controlWindow)
  409. {
  410. if (NULL == self->device ||
  411. 0 == MultiByteToWideChar(CP_UTF8, 0, self->device->GetName(), -1, buffer, ARRAYSIZE(buffer)))
  412. {
  413. StringCchCopy(buffer, ARRAYSIZE(buffer), L"<unknown>");
  414. }
  415. SetWindowText(controlWindow, buffer);
  416. }
  417. controlWindow = GetDlgItem(hwnd, IDC_EDIT_TITLE);
  418. if (NULL != controlWindow)
  419. {
  420. if (NULL == self->device ||
  421. FAILED(self->device->GetDisplayName(buffer, ARRAYSIZE(buffer))))
  422. {
  423. StringCchCopy(buffer, ARRAYSIZE(buffer), L"<unknown>");
  424. }
  425. SetWindowText(controlWindow, buffer);
  426. }
  427. controlWindow = GetDlgItem(hwnd, IDC_EDIT_TYPE);
  428. if (NULL != controlWindow)
  429. {
  430. buffer[0] = L'\0';
  431. if (NULL != self->device)
  432. {
  433. const char *typeName;
  434. ifc_devicetype *type;
  435. typeName = self->device->GetType();
  436. if (NULL != WASABI_API_DEVICES &&
  437. S_OK == WASABI_API_DEVICES->TypeFind(typeName, &type))
  438. {
  439. if (FAILED(type->GetDisplayName(buffer, ARRAYSIZE(buffer))))
  440. buffer[0] = L'\0';
  441. type->Release();
  442. }
  443. if (L'\0' == *buffer)
  444. MultiByteToWideChar(CP_UTF8, 0, typeName, -1, buffer, ARRAYSIZE(buffer));
  445. }
  446. if (L'\0' == *buffer)
  447. StringCchCopy(buffer, ARRAYSIZE(buffer), L"<unknown>");
  448. SetWindowText(controlWindow, buffer);
  449. }
  450. controlWindow = GetDlgItem(hwnd, IDC_EDIT_CONNECTION);
  451. if (NULL != controlWindow)
  452. {
  453. buffer[0] = L'\0';
  454. if (NULL != self->device)
  455. {
  456. const char *connectionName;
  457. ifc_deviceconnection *connection;
  458. connectionName = self->device->GetConnection();
  459. if (NULL != WASABI_API_DEVICES &&
  460. S_OK == WASABI_API_DEVICES->ConnectionFind(connectionName, &connection))
  461. {
  462. if (FAILED(connection->GetDisplayName(buffer, ARRAYSIZE(buffer))))
  463. buffer[0] = L'\0';
  464. connection->Release();
  465. }
  466. if (L'\0' == *buffer)
  467. MultiByteToWideChar(CP_UTF8, 0, connectionName, -1, buffer, ARRAYSIZE(buffer));
  468. }
  469. if (L'\0' == *buffer)
  470. StringCchCopy(buffer, ARRAYSIZE(buffer), L"<unknown>");
  471. SetWindowText(controlWindow, buffer);
  472. }
  473. controlWindow = GetDlgItem(hwnd, IDC_COMBO_ATTACHED);
  474. if (NULL != controlWindow)
  475. {
  476. SendMessage(controlWindow, CB_RESETCONTENT, 0, 0L);
  477. if (NULL != self->device)
  478. {
  479. const wchar_t *searchString;
  480. SendMessage(controlWindow, CB_ADDSTRING, 0, (LPARAM)L"Yes");
  481. SendMessage(controlWindow, CB_ADDSTRING, 0, (LPARAM)L"No");
  482. if (FALSE == self->device->GetAttached())
  483. searchString = L"No";
  484. else
  485. searchString = L"Yes";
  486. SendMessage(controlWindow, CB_SELECTSTRING, -1, (LPARAM)searchString);
  487. }
  488. else
  489. {
  490. SendMessage(controlWindow, CB_ADDSTRING, 0, (LPARAM)L"<unknown>");
  491. SendMessage(controlWindow, CB_SETCURSEL, 0, 0L);
  492. }
  493. }
  494. controlWindow = GetDlgItem(hwnd, IDC_COMBO_VISIBLE);
  495. if (NULL != controlWindow)
  496. {
  497. SendMessage(controlWindow, CB_RESETCONTENT, 0, 0L);
  498. if (NULL != self->device)
  499. {
  500. const wchar_t *searchString;
  501. SendMessage(controlWindow, CB_ADDSTRING, 0, (LPARAM)L"Yes");
  502. SendMessage(controlWindow, CB_ADDSTRING, 0, (LPARAM)L"No");
  503. if (FALSE != self->device->GetHidden())
  504. searchString = L"No";
  505. else
  506. searchString = L"Yes";
  507. SendMessage(controlWindow, CB_SELECTSTRING, -1, (LPARAM)searchString);
  508. }
  509. else
  510. {
  511. SendMessage(controlWindow, CB_ADDSTRING, 0, (LPARAM)L"<unknown>");
  512. SendMessage(controlWindow, CB_SETCURSEL, 0, 0L);
  513. }
  514. }
  515. DeviceView_InitCapacity(hwnd);
  516. DeviceView_InitIcons(hwnd, NULL);
  517. }
  518. static INT_PTR
  519. DeviceView_OnInitDialog(HWND hwnd, HWND focusWindow, LPARAM param)
  520. {
  521. DeviceView *self;
  522. self = (DeviceView*)malloc(sizeof(DeviceView));
  523. if (NULL != self)
  524. {
  525. ZeroMemory(self, sizeof(DeviceView));
  526. if (FALSE == SetProp(hwnd, MAKEINTATOM(DEVICEVIEW_ATOM), self))
  527. {
  528. free(self);
  529. self = NULL;
  530. }
  531. }
  532. if (NULL == self)
  533. {
  534. DestroyWindow(hwnd);
  535. return 0;
  536. }
  537. self->device = (Device*)param;
  538. if (NULL != self->device)
  539. self->device->AddRef();
  540. DeviceView_InitView(hwnd);
  541. SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
  542. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
  543. return 0;
  544. }
  545. static void
  546. DeviceView_OnDestroy(HWND hwnd)
  547. {
  548. DeviceView *self;
  549. DeviceView_DestroyIcons(hwnd);
  550. self = DEVICEVIEW(hwnd);
  551. RemoveProp(hwnd, MAKEINTATOM(DEVICEVIEW_ATOM));
  552. if (NULL == self)
  553. return;
  554. if (NULL != self->device)
  555. self->device->Release();
  556. free(self);
  557. }
  558. static void
  559. DeviceView_OnTitleEditChanged(HWND hwnd)
  560. {
  561. DeviceView *self;
  562. HWND controlWindow;
  563. wchar_t buffer[1024];
  564. DEVICEVIEW_RET_VOID(self, hwnd);
  565. controlWindow = GetDlgItem(hwnd, IDC_EDIT_TITLE);
  566. if (NULL == controlWindow)
  567. return;
  568. if (NULL == self->device)
  569. return;
  570. GetWindowText(controlWindow, buffer, ARRAYSIZE(buffer));
  571. self->device->SetDisplayName(buffer);
  572. }
  573. static void
  574. DeviceView_OnAttachedComboChanged(HWND hwnd)
  575. {
  576. DeviceView *self;
  577. HWND controlWindow;
  578. wchar_t buffer[1024];
  579. int index;
  580. DEVICEVIEW_RET_VOID(self, hwnd);
  581. controlWindow = GetDlgItem(hwnd, IDC_COMBO_ATTACHED);
  582. if (NULL == controlWindow)
  583. return;
  584. if (NULL == self->device)
  585. return;
  586. index = (int)SendMessage(controlWindow, CB_GETCURSEL, 0, 0);
  587. if (CB_ERR != index &&
  588. CB_ERR != SendMessage(controlWindow, CB_GETLBTEXT, index, (LPARAM)buffer))
  589. {
  590. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, buffer, -1, L"Yes", -1))
  591. {
  592. self->device->Attach(NULL);
  593. }
  594. else if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, buffer, -1, L"No", -1))
  595. {
  596. self->device->Detach(NULL);
  597. }
  598. }
  599. }
  600. static void
  601. DeviceView_OnVisibleComboChanged(HWND hwnd)
  602. {
  603. DeviceView *self;
  604. HWND controlWindow;
  605. wchar_t buffer[1024];
  606. int index;
  607. DEVICEVIEW_RET_VOID(self, hwnd);
  608. controlWindow = GetDlgItem(hwnd, IDC_COMBO_VISIBLE);
  609. if (NULL == controlWindow)
  610. return;
  611. if (NULL == self->device)
  612. return;
  613. index = (int)SendMessage(controlWindow, CB_GETCURSEL, 0, 0);
  614. if (-1 != index &&
  615. CB_ERR != SendMessage(controlWindow, CB_GETLBTEXT, index, (LPARAM)buffer))
  616. {
  617. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, buffer, -1, L"Yes", -1))
  618. {
  619. self->device->SetHidden(FALSE);
  620. }
  621. else if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, buffer, -1, L"No", -1))
  622. {
  623. self->device->SetHidden(TRUE);
  624. }
  625. }
  626. }
  627. static void
  628. DeviceView_OnIconsComboChanged(HWND hwnd)
  629. {
  630. DeviceView *self;
  631. HWND controlWindow, pictureWindow;
  632. int index;
  633. HBITMAP bitmap, previousBitmap;
  634. DEVICEVIEW_RET_VOID(self, hwnd);
  635. controlWindow = GetDlgItem(hwnd, IDC_COMBO_ICONS);
  636. if (NULL == controlWindow)
  637. return;
  638. if (NULL == self->device)
  639. return;
  640. pictureWindow = GetDlgItem(hwnd, IDC_STATIC_PREVIEWICON);
  641. if (NULL == pictureWindow)
  642. return;
  643. bitmap = NULL;
  644. index = (int)SendMessage(controlWindow, CB_GETCURSEL, 0, 0);\
  645. if (CB_ERR != index)
  646. {
  647. DeviceIconInfo *info;
  648. info = (DeviceIconInfo*)SendMessage(controlWindow, CB_GETITEMDATA, index, 0L);
  649. if (CB_ERR != (INT_PTR)info && NULL != info->path)
  650. {
  651. bitmap = DeviceView_LoadIcon(info->path);
  652. }
  653. }
  654. previousBitmap = (HBITMAP)SendMessage(pictureWindow, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bitmap);
  655. if (NULL != previousBitmap)
  656. DeleteObject(previousBitmap);
  657. previousBitmap = (HBITMAP)SendMessage(pictureWindow, STM_GETIMAGE, IMAGE_BITMAP, 0L);
  658. if(previousBitmap != bitmap)
  659. {
  660. if (NULL != bitmap)
  661. DeleteObject(bitmap);
  662. }
  663. }
  664. static void
  665. DeviceView_OnTotalSpaceEditChanged(HWND hwnd)
  666. {
  667. DeviceView *self;
  668. HWND controlWindow;
  669. uint64_t totalSpace;
  670. BOOL error;
  671. DEVICEVIEW_RET_VOID(self, hwnd);
  672. controlWindow = GetDlgItem(hwnd, IDC_SPIN_TOTALSPACE);
  673. if (NULL == controlWindow)
  674. return;
  675. if (NULL == self->device)
  676. return;
  677. totalSpace = (size_t)SendMessage(controlWindow, UDM_GETPOS32, 0, (LPARAM)&error);
  678. if (FALSE != error)
  679. return;
  680. if (FAILED(self->device->SetTotalSpace(totalSpace)))
  681. {
  682. if (FAILED(self->device->GetTotalSpace(&totalSpace)))
  683. totalSpace = 0;
  684. }
  685. controlWindow = GetDlgItem(hwnd, IDC_SPIN_USEDSPACE);
  686. if (NULL != controlWindow)
  687. {
  688. SendMessage(controlWindow, UDM_SETRANGE32, (WPARAM)0, (LPARAM)totalSpace);
  689. }
  690. }
  691. static void
  692. DeviceView_OnUsedSpaceEditChanged(HWND hwnd)
  693. {
  694. DeviceView *self;
  695. HWND controlWindow;
  696. uint64_t usedSpace;
  697. BOOL error;
  698. DEVICEVIEW_RET_VOID(self, hwnd);
  699. controlWindow = GetDlgItem(hwnd, IDC_SPIN_USEDSPACE);
  700. if (NULL == controlWindow)
  701. return;
  702. if (NULL == self->device)
  703. return;
  704. usedSpace = (size_t)SendMessage(controlWindow, UDM_GETPOS32, 0, (LPARAM)&error);
  705. if (FALSE != error)
  706. return;
  707. if (FAILED(self->device->SetUsedSpace(usedSpace)))
  708. {
  709. if (FAILED(self->device->GetTotalSpace(&usedSpace)))
  710. usedSpace = 0;
  711. }
  712. }
  713. static void
  714. DeviceView_OnCommand(HWND hwnd, INT commandId, INT eventId, HWND controlWindow)
  715. {
  716. switch(commandId)
  717. {
  718. case IDC_EDIT_TITLE:
  719. switch(eventId)
  720. {
  721. case EN_CHANGE:
  722. DeviceView_OnTitleEditChanged(hwnd);
  723. break;
  724. }
  725. break;
  726. case IDC_COMBO_ATTACHED:
  727. switch(eventId)
  728. {
  729. case CBN_SELENDOK:
  730. DeviceView_OnAttachedComboChanged(hwnd);
  731. break;
  732. }
  733. break;
  734. case IDC_COMBO_VISIBLE:
  735. switch(eventId)
  736. {
  737. case CBN_SELENDOK:
  738. DeviceView_OnVisibleComboChanged(hwnd);
  739. break;
  740. }
  741. break;
  742. case IDC_COMBO_ICONS:
  743. switch(eventId)
  744. {
  745. case CBN_SELENDOK:
  746. DeviceView_OnIconsComboChanged(hwnd);
  747. break;
  748. }
  749. break;
  750. case IDC_EDIT_TOTALSPACE:
  751. switch(eventId)
  752. {
  753. case EN_CHANGE:
  754. DeviceView_OnTotalSpaceEditChanged(hwnd);
  755. break;
  756. }
  757. break;
  758. case IDC_EDIT_USEDSPACE:
  759. switch(eventId)
  760. {
  761. case EN_CHANGE:
  762. DeviceView_OnUsedSpaceEditChanged(hwnd);
  763. break;
  764. }
  765. break;
  766. case IDC_BUTTON_NEWICON:
  767. switch(eventId)
  768. {
  769. case BN_CLICKED:
  770. DeviceView_NewIcon(hwnd);
  771. break;
  772. }
  773. break;
  774. case IDC_BUTTON_REMOVEICON:
  775. switch(eventId)
  776. {
  777. case BN_CLICKED:
  778. DeviceView_RemoveIcon(hwnd);
  779. break;
  780. }
  781. break;
  782. case IDC_BUTTON_EDITICON:
  783. switch(eventId)
  784. {
  785. case BN_CLICKED:
  786. DeviceView_EditIcon(hwnd);
  787. break;
  788. }
  789. break;
  790. }
  791. }
  792. static INT_PTR
  793. DeviceView_DialogProc(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam)
  794. {
  795. switch(uMsg)
  796. {
  797. case WM_INITDIALOG: return DeviceView_OnInitDialog(hwnd, (HWND)wParam, lParam);
  798. case WM_DESTROY: DeviceView_OnDestroy(hwnd); return TRUE;
  799. case WM_COMMAND: DeviceView_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); return TRUE;
  800. }
  801. return 0;
  802. }