statusBar.cpp 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #include "main.h"
  2. #include "./statusBar.h"
  3. #include <vector>
  4. #include <strsafe.h>
  5. typedef struct StatusTextBlock
  6. {
  7. wchar_t *text;
  8. size_t length;
  9. long width;
  10. } StatusTextBlock;
  11. typedef struct StaturRecord
  12. {
  13. unsigned int id;
  14. StatusTextBlock mainBlock;
  15. StatusTextBlock rightBlock;
  16. } StatusRecord;
  17. typedef std::vector<StatusRecord*> StatusRecordList;
  18. typedef struct StatusBar
  19. {
  20. HFONT font;
  21. HBRUSH backBrush;
  22. COLORREF textColor;
  23. COLORREF backColor;
  24. StatusRecordList list;
  25. int idealHeight;
  26. long spacing;
  27. long leftBlockMinWidth;
  28. BackBuffer backBuffer;
  29. } StatusBar;
  30. #define STATUSBAR(_hwnd) ((StatusBar*)(LONGX86)GetWindowLongPtrW((_hwnd), 0))
  31. #define STATUSBAR_RET_VOID(_self, _hwnd) {(_self) = STATUSBAR((_hwnd)); if (NULL == (_self)) return;}
  32. #define STATUSBAR_RET_VAL(_self, _hwnd, _error) {(_self) = STATUSBAR((_hwnd)); if (NULL == (_self)) return (_error);}
  33. static LRESULT CALLBACK
  34. StatusBar_WindowProc(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam);
  35. static ATOM
  36. StatusBar_GetClassAtom(HINSTANCE instance)
  37. {
  38. WNDCLASSEXW klass;
  39. ATOM klassAtom;
  40. klassAtom = (ATOM)GetClassInfoExW(instance, STATUSBAR_WINDOW_CLASS, &klass);
  41. if (0 != klassAtom)
  42. return klassAtom;
  43. memset(&klass, 0, sizeof(klass));
  44. klass.cbSize = sizeof(klass);
  45. klass.style = 0;
  46. klass.lpfnWndProc = StatusBar_WindowProc;
  47. klass.cbClsExtra = 0;
  48. klass.cbWndExtra = sizeof(StatusBar*);
  49. klass.hInstance = instance;
  50. klass.hIcon = NULL;
  51. klass.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
  52. klass.hbrBackground = NULL;
  53. klass.lpszMenuName = NULL;
  54. klass.lpszClassName = STATUSBAR_WINDOW_CLASS;
  55. klass.hIconSm = NULL;
  56. klassAtom = RegisterClassExW(&klass);
  57. return klassAtom;
  58. }
  59. HWND
  60. StatusBar_CreateWindow(unsigned long windowExStyle, const wchar_t *text, unsigned long windowStyle,
  61. int x, int y, int width, int height,
  62. HWND parentWindow, unsigned int controlId)
  63. {
  64. HINSTANCE instance = GetModuleHandleW(NULL);
  65. ATOM klassAtom = StatusBar_GetClassAtom(instance);
  66. if (0 == klassAtom)
  67. return NULL;
  68. return CreateWindowExW(WS_EX_NOPARENTNOTIFY | windowExStyle, (LPCWSTR)MAKEINTATOM(klassAtom), text,
  69. WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | windowStyle,
  70. x, y, width, height, parentWindow, (HMENU)controlId, instance, NULL);
  71. }
  72. static void
  73. StatusBar_InitTextBlock(StatusTextBlock *block)
  74. {
  75. if (NULL != block)
  76. {
  77. block->text = NULL;
  78. block->length = ((size_t)-1);
  79. block->width = -1;
  80. }
  81. }
  82. static void
  83. StatusBar_FreeTextBlock(StatusTextBlock *block)
  84. {
  85. if (NULL != block)
  86. {
  87. ResourceString_Free(block->text);
  88. StatusBar_InitTextBlock(block);
  89. }
  90. }
  91. static const wchar_t *
  92. StatusBar_RetriveText(StatusTextBlock *block)
  93. {
  94. const wchar_t *source;
  95. if (NULL == block)
  96. return NULL;
  97. source = block->text;
  98. if (FALSE != IS_INTRESOURCE(source) &&
  99. NULL != source)
  100. {
  101. wchar_t buffer[256] = {0};
  102. if (NULL == WASABI_API_LNG)
  103. return NULL;
  104. WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)source, buffer, ARRAYSIZE(buffer));
  105. block->text = String_Duplicate(buffer);
  106. }
  107. return block->text;
  108. }
  109. static size_t
  110. StatusBar_GetTextBlockLength(StatusTextBlock *block)
  111. {
  112. const wchar_t *text;
  113. if (NULL == block)
  114. return NULL;
  115. if (((size_t)-1) != block->length)
  116. {
  117. text = StatusBar_RetriveText(block);
  118. block->length = (NULL != text) ? lstrlenW(text) : 0;
  119. }
  120. return block->length;
  121. }
  122. static const wchar_t *
  123. StatusBar_GetTextBlockInfo(StatusTextBlock *block, HDC hdc)
  124. {
  125. const wchar_t *text;
  126. text = StatusBar_RetriveText(block);
  127. if (((size_t)-1) == block->length)
  128. {
  129. if (FALSE == IS_STRING_EMPTY(text))
  130. block->length = lstrlenW(text);
  131. else
  132. block->length = 0;
  133. }
  134. if (-1 == block->width)
  135. {
  136. block->width = 0;
  137. if (0 != block->length)
  138. {
  139. RECT rect;
  140. DRAWTEXTPARAMS textParams;
  141. textParams.cbSize = sizeof(textParams);
  142. textParams.iTabLength = 4;
  143. textParams.iLeftMargin = 0;
  144. textParams.iRightMargin = 0;
  145. SetRect(&rect, 0, 0, 0x7FFFFFFF, 0);
  146. if (FALSE != DrawTextEx(hdc, (wchar_t*)text, -1, &rect,
  147. DT_LEFT | DT_TOP | DT_TABSTOP | DT_SINGLELINE |
  148. DT_NOPREFIX | DT_EXPANDTABS | DT_NOCLIP | DT_CALCRECT | DT_EDITCONTROL,
  149. &textParams))
  150. {
  151. block->width = RECTWIDTH(rect);
  152. }
  153. }
  154. TEXTMETRIC metrics;
  155. if (FALSE != GetTextMetrics(hdc, &metrics))
  156. {
  157. block->width += metrics.tmAveCharWidth/2;
  158. }
  159. }
  160. return text;
  161. }
  162. static unsigned int
  163. StatusBar_GetNextFreeId(StatusBar *self)
  164. {
  165. unsigned int id;
  166. size_t index, count;
  167. StatusRecord *record;
  168. BOOL checkId;
  169. count = self->list.size();
  170. id = (unsigned int)count;
  171. do
  172. {
  173. if (0xFFFFFFFF == id)
  174. return STATUS_ERROR;
  175. checkId = FALSE;
  176. index = count;
  177. while(index--)
  178. {
  179. record = self->list[index];
  180. if (record->id == id)
  181. {
  182. id++;
  183. checkId = TRUE;
  184. break;
  185. }
  186. }
  187. } while(FALSE != checkId);
  188. return id;
  189. }
  190. static StatusRecord *
  191. StatusBar_AddRecord(StatusBar *self, const wchar_t *text)
  192. {
  193. StatusRecord *record;
  194. unsigned int id;
  195. id = StatusBar_GetNextFreeId(self);
  196. if (STATUS_ERROR == id)
  197. return NULL;
  198. record = (StatusRecord*)malloc(sizeof(StatusRecord));
  199. if(NULL == record)
  200. return NULL;
  201. record->id = 0;
  202. StatusBar_InitTextBlock(&record->mainBlock);
  203. StatusBar_InitTextBlock(&record->rightBlock);
  204. record->mainBlock.text = ResourceString_Duplicate(text);
  205. self->list.push_back(record);
  206. return record;
  207. }
  208. static StatusRecord *
  209. StatusBar_FindRecord(StatusBar *self, unsigned int id, size_t *indexOut)
  210. {
  211. StatusRecord *record;
  212. size_t index;
  213. index = self->list.size();
  214. while(index--)
  215. {
  216. record = self->list[index];
  217. if (record->id == id)
  218. {
  219. if (NULL != indexOut)
  220. *indexOut = index;
  221. return record;
  222. }
  223. }
  224. return NULL;
  225. }
  226. static StatusRecord *
  227. StatusBar_FindVisibleRecord(StatusBar *self, size_t *indexOut)
  228. {
  229. StatusRecord *record;
  230. size_t index;
  231. const wchar_t *text;
  232. index = self->list.size();
  233. while(index--)
  234. {
  235. record = self->list[index];
  236. text = StatusBar_RetriveText(&record->mainBlock);
  237. if (FALSE != IS_STRING_EMPTY(text))
  238. text = StatusBar_RetriveText(&record->rightBlock);
  239. if (FALSE == IS_STRING_EMPTY(text))
  240. {
  241. if (NULL != indexOut)
  242. *indexOut = index;
  243. return record;
  244. }
  245. }
  246. return NULL;
  247. }
  248. static void
  249. StatusBar_Paint(HWND hwnd, HDC hdc, const RECT *paintRect, BOOL erase)
  250. {
  251. StatusBar *self;
  252. StatusRecord *record;
  253. const wchar_t *text;
  254. STATUSBAR_RET_VOID(self, hwnd);
  255. if (FALSE != erase)
  256. {
  257. FillRect(hdc, paintRect, self->backBrush);
  258. }
  259. record = StatusBar_FindVisibleRecord(self, NULL);
  260. if (NULL != record)
  261. {
  262. COLORREF prevBackColor, prevTextColor;
  263. HFONT prevFont;
  264. RECT clientRect, textRect;
  265. DRAWTEXTPARAMS textParams;
  266. long limit;
  267. prevBackColor = SetBkColor(hdc, self->backColor);
  268. prevTextColor = SetTextColor(hdc, self->textColor);
  269. prevFont = SelectFont(hdc, self->font);
  270. textParams.cbSize = sizeof(textParams);
  271. textParams.iTabLength = 4;
  272. textParams.iLeftMargin = 0;
  273. textParams.iRightMargin = 0;
  274. GetClientRect(hwnd, &clientRect);
  275. if (-1 == self->spacing)
  276. self->spacing = Graphics_GetAveStrWidth(hdc, 2);
  277. if (-1 == self->leftBlockMinWidth)
  278. self->leftBlockMinWidth = Graphics_GetAveStrWidth(hdc, 16);
  279. text = StatusBar_GetTextBlockInfo(&record->rightBlock, hdc);
  280. if (FALSE == IS_STRING_EMPTY(text))
  281. {
  282. CopyRect(&textRect, &clientRect);
  283. textRect.left = textRect.right - record->rightBlock.width;
  284. limit = clientRect.left + self->spacing + self->leftBlockMinWidth;
  285. if (textRect.left < limit)
  286. textRect.left = limit;
  287. if (textRect.left < textRect.right)
  288. {
  289. DrawTextEx(hdc, (wchar_t*)text, -1, &textRect,
  290. DT_LEFT | DT_BOTTOM | DT_TABSTOP | DT_SINGLELINE |
  291. DT_NOPREFIX | DT_EXPANDTABS, &textParams);
  292. }
  293. }
  294. text = StatusBar_GetTextBlockInfo(&record->mainBlock, hdc);
  295. if (FALSE == IS_STRING_EMPTY(text))
  296. {
  297. CopyRect(&textRect, &clientRect);
  298. textRect.right = textRect.left + record->mainBlock.width;
  299. limit = clientRect.right - record->rightBlock.width - 8;
  300. if (limit < self->leftBlockMinWidth)
  301. limit = self->leftBlockMinWidth;
  302. if (textRect.right > limit)
  303. textRect.right = limit;
  304. if (textRect.left < textRect.right)
  305. {
  306. DrawTextEx(hdc, (wchar_t*)text, -1, &textRect,
  307. DT_LEFT | DT_BOTTOM | DT_TABSTOP | DT_SINGLELINE |
  308. DT_NOPREFIX | DT_EXPANDTABS | DT_END_ELLIPSIS, &textParams);
  309. }
  310. }
  311. SetBkColor(hdc, prevBackColor);
  312. SetTextColor(hdc, prevTextColor);
  313. SelectFont(hdc, prevFont);
  314. }
  315. return;
  316. }
  317. static void
  318. StatusBar_PaintBuffered(HWND hwnd, HDC hdc, const RECT *paintRect, BOOL erase)
  319. {
  320. StatusBar *self;
  321. HDC bufferDC, targetDC;
  322. RECT clientRect;
  323. STATUSBAR_RET_VOID(self, hwnd);
  324. GetClientRect(hwnd, &clientRect);
  325. if (FALSE != BackBuffer_EnsureSizeEx(&self->backBuffer,
  326. RECTWIDTH(*paintRect), RECTHEIGHT(*paintRect),
  327. RECTWIDTH(clientRect), RECTHEIGHT(clientRect)))
  328. {
  329. bufferDC = BackBuffer_GetDC(&self->backBuffer);
  330. if (NULL != bufferDC)
  331. {
  332. SetViewportOrgEx(bufferDC, -paintRect->left, -paintRect->top, NULL);
  333. targetDC = hdc;
  334. hdc = bufferDC;
  335. }
  336. }
  337. else
  338. {
  339. bufferDC = NULL;
  340. targetDC = NULL;
  341. }
  342. StatusBar_Paint(hwnd, hdc, paintRect, erase);
  343. if (NULL != bufferDC)
  344. {
  345. hdc = targetDC;
  346. SetViewportOrgEx(bufferDC, 0, 0, NULL);
  347. BackBuffer_Copy(&self->backBuffer, hdc,
  348. paintRect->left, paintRect->top, RECTWIDTH(*paintRect), RECTHEIGHT(*paintRect));
  349. }
  350. return;
  351. }
  352. static BOOL
  353. StatusBar_InvalidateByIndex(StatusBar *self, HWND hwnd, size_t recordIndex)
  354. {
  355. unsigned int windowStyle;
  356. size_t visibleIndex;
  357. windowStyle = GetWindowStyle(hwnd);
  358. if (0 == (WS_VISIBLE & windowStyle))
  359. return FALSE;
  360. if(NULL != StatusBar_FindVisibleRecord(self, &visibleIndex))
  361. {
  362. if (recordIndex == visibleIndex)
  363. return InvalidateRect(hwnd, NULL, FALSE);
  364. }
  365. return FALSE;
  366. }
  367. static BOOL
  368. StatusBar_InvalidateByRecord(StatusBar *self, HWND hwnd, StatusRecord *record)
  369. {
  370. unsigned int windowStyle;
  371. StatusRecord *visibleRecord;
  372. windowStyle = GetWindowStyle(hwnd);
  373. if (0 == (WS_VISIBLE & windowStyle))
  374. return FALSE;
  375. visibleRecord = StatusBar_FindVisibleRecord(self, NULL);
  376. if (record == visibleRecord &&
  377. NULL != visibleRecord)
  378. {
  379. return InvalidateRect(hwnd, NULL, FALSE);
  380. }
  381. return FALSE;
  382. }
  383. static LRESULT
  384. StatusBar_OnCreate(HWND hwnd, CREATESTRUCT *createStruct)
  385. {
  386. StatusBar *self;
  387. self = new StatusBar();
  388. if (NULL == self)
  389. return -1;
  390. SetLastError(ERROR_SUCCESS);
  391. if (!SetWindowLongPtr(hwnd, 0, (LONGX86)self) && ERROR_SUCCESS != GetLastError())
  392. {
  393. delete self;
  394. return -1;
  395. }
  396. if (NULL != createStruct->lpszName)
  397. StatusBar_AddRecord(self, createStruct->lpszName);
  398. self->backBrush = GetSysColorBrush(COLOR_WINDOW);
  399. self->backColor = GetSysColor(COLOR_WINDOW);
  400. self->textColor = GetSysColor(COLOR_WINDOWTEXT);
  401. self->idealHeight = -1;
  402. self->spacing = -1;
  403. self->leftBlockMinWidth = -1;
  404. BackBuffer_Initialize(&self->backBuffer, hwnd);
  405. return 0;
  406. }
  407. static void
  408. StatusBar_OnDestroy(HWND hwnd)
  409. {
  410. StatusBar *self;
  411. size_t index;
  412. StatusRecord *record;
  413. self = STATUSBAR(hwnd);
  414. SetWindowLongPtr(hwnd, 0, 0);
  415. if (NULL == self)
  416. return;
  417. index = self->list.size();
  418. while(index--)
  419. {
  420. record = self->list[index];
  421. StatusBar_FreeTextBlock(&record->mainBlock);
  422. StatusBar_FreeTextBlock(&record->rightBlock);
  423. }
  424. BackBuffer_Uninitialize(&self->backBuffer);
  425. delete self;
  426. }
  427. static void
  428. StatusBar_OnPaint(HWND hwnd)
  429. {
  430. PAINTSTRUCT ps;
  431. if (NULL != BeginPaint(hwnd, &ps))
  432. {
  433. StatusBar_PaintBuffered(hwnd, ps.hdc, &ps.rcPaint, ps.fErase);
  434. EndPaint(hwnd, &ps);
  435. }
  436. }
  437. static void
  438. StatusBar_OnPrintClient(HWND hwnd, HDC hdc, UINT options)
  439. {
  440. RECT clientRect;
  441. if (GetClientRect(hwnd, &clientRect))
  442. {
  443. StatusBar_Paint(hwnd, hdc, &clientRect, TRUE);
  444. }
  445. }
  446. static void
  447. StatusBar_OnWindowPosChanged(HWND hwnd, WINDOWPOS *windowPos)
  448. {
  449. if (SWP_NOSIZE != ((SWP_NOSIZE | SWP_FRAMECHANGED) & windowPos->flags) &&
  450. 0 == (SWP_NOREDRAW & windowPos->flags))
  451. {
  452. InvalidateRect(hwnd, NULL, FALSE);
  453. }
  454. }
  455. static LRESULT
  456. StatusBar_OnSetText(HWND hwnd, LPCWSTR text)
  457. {
  458. StatusBar *self;
  459. StatusRecord *record;
  460. size_t count;
  461. STATUSBAR_RET_VAL(self, hwnd, FALSE);
  462. count = self->list.size();
  463. if (0 == count)
  464. {
  465. if (NULL == text)
  466. return TRUE;
  467. record = StatusBar_AddRecord(self, text);
  468. if (NULL == record)
  469. return FALSE;
  470. }
  471. else
  472. {
  473. record = self->list[count - 1];
  474. StatusBar_FreeTextBlock(&record->mainBlock);
  475. StatusBar_FreeTextBlock(&record->rightBlock);
  476. record->mainBlock.text = ResourceString_Duplicate(text);
  477. }
  478. StatusBar_InvalidateByRecord(self, hwnd, record);
  479. return TRUE;
  480. }
  481. static LRESULT
  482. StatusBar_OnGetText(HWND hwnd, LPWSTR buffer, size_t bufferMax)
  483. {
  484. StatusBar *self;
  485. StatusRecord *record;
  486. const wchar_t *lText, *rText;
  487. size_t count, remaining;
  488. wchar_t *cursor;
  489. HRESULT hr;
  490. STATUSBAR_RET_VAL(self, hwnd, 0);
  491. if (NULL == buffer)
  492. return 0;
  493. count = self->list.size();
  494. if (0 == count)
  495. {
  496. lText = NULL;
  497. rText = NULL;
  498. }
  499. else
  500. {
  501. record = self->list[count - 1];
  502. lText = StatusBar_RetriveText(&record->mainBlock);
  503. rText = StatusBar_RetriveText(&record->rightBlock);
  504. }
  505. cursor = buffer;
  506. remaining = bufferMax;
  507. if (NULL != lText)
  508. hr = StringCchCopyEx(cursor, bufferMax, lText, &cursor, &remaining, 0);
  509. else
  510. hr = S_OK;
  511. if (NULL != rText && SUCCEEDED(hr))
  512. {
  513. hr = StringCchCopyEx(cursor, bufferMax, L"\f", &cursor, &remaining, 0);
  514. if (SUCCEEDED(hr))
  515. hr = StringCchCopyEx(cursor, bufferMax, rText, &cursor, &remaining, 0);
  516. }
  517. return (bufferMax - remaining);
  518. }
  519. static LRESULT
  520. StatusBar_OnGetTextLength(HWND hwnd)
  521. {
  522. StatusBar *self;
  523. StatusRecord *record;
  524. size_t length, r;
  525. STATUSBAR_RET_VAL(self, hwnd, 0);
  526. length = self->list.size();
  527. if (0 == length)
  528. return 0;
  529. record = self->list[length - 1];
  530. length = StatusBar_GetTextBlockLength(&record->mainBlock);
  531. r = StatusBar_GetTextBlockLength(&record->rightBlock);
  532. if (0 != r)
  533. {
  534. length += (r + 1);
  535. }
  536. return length;
  537. }
  538. static void
  539. StatusBar_OnSetFont(HWND hwnd, HFONT font, BOOL redraw)
  540. {
  541. StatusBar *self;
  542. StatusRecord *record;
  543. size_t index;
  544. STATUSBAR_RET_VOID(self, hwnd);
  545. self->font = font;
  546. self->idealHeight = -1;
  547. self->spacing = -1;
  548. self->leftBlockMinWidth = -1;
  549. index = self->list.size();
  550. while(index--)
  551. {
  552. record = self->list[index];
  553. record->mainBlock.width = -1;
  554. record->rightBlock.width = -1;
  555. }
  556. if (NULL != redraw)
  557. InvalidateRect(hwnd, NULL, TRUE);
  558. }
  559. static LRESULT
  560. StatusBar_OnGetFont(HWND hwnd)
  561. {
  562. StatusBar *self;
  563. STATUSBAR_RET_VAL(self, hwnd, NULL);
  564. return (LRESULT)self->font;
  565. }
  566. static LRESULT
  567. StatusBar_OnSetBackBrush(HWND hwnd, HBRUSH brush, BOOL redraw)
  568. {
  569. StatusBar *self;
  570. HBRUSH prevBrush;
  571. STATUSBAR_RET_VAL(self, hwnd, (LRESULT)GetSysColorBrush(COLOR_WINDOW));
  572. prevBrush = self->backBrush;
  573. self->backBrush = brush;
  574. if (NULL != redraw)
  575. InvalidateRect(hwnd, NULL, TRUE);
  576. return (LRESULT)prevBrush;
  577. }
  578. static LRESULT
  579. StatusBar_OnGetBackBrush(HWND hwnd)
  580. {
  581. StatusBar *self;
  582. STATUSBAR_RET_VAL(self, hwnd, (LRESULT)GetSysColorBrush(COLOR_WINDOW));
  583. return (LRESULT)self->backBrush;
  584. }
  585. static LRESULT
  586. StatusBar_OnSetBackColor(HWND hwnd, COLORREF color, BOOL redraw)
  587. {
  588. StatusBar *self;
  589. COLORREF prevColor;
  590. STATUSBAR_RET_VAL(self, hwnd, GetSysColor(COLOR_WINDOW));
  591. prevColor = self->backColor;
  592. self->backColor = color;
  593. if (NULL != redraw)
  594. InvalidateRect(hwnd, NULL, FALSE);
  595. return (LRESULT)prevColor;
  596. }
  597. static LRESULT
  598. StatusBar_OnGetBackColor(HWND hwnd)
  599. {
  600. StatusBar *self;
  601. STATUSBAR_RET_VAL(self, hwnd, GetSysColor(COLOR_WINDOW));
  602. return (LRESULT)self->backColor;
  603. }
  604. static LRESULT
  605. StatusBar_OnSetTextColor(HWND hwnd, COLORREF color, BOOL redraw)
  606. {
  607. StatusBar *self;
  608. COLORREF prevColor;
  609. STATUSBAR_RET_VAL(self, hwnd, GetSysColor(COLOR_WINDOWTEXT));
  610. prevColor = self->textColor;
  611. self->textColor = color;
  612. if (NULL != redraw)
  613. InvalidateRect(hwnd, NULL, FALSE);
  614. return (LRESULT)prevColor;
  615. }
  616. static LRESULT
  617. StatusBar_OnGetTextColor(HWND hwnd)
  618. {
  619. StatusBar *self;
  620. STATUSBAR_RET_VAL(self, hwnd, GetSysColor(COLOR_WINDOWTEXT));
  621. return (LRESULT)self->textColor;
  622. }
  623. static LRESULT
  624. StatusBar_OnGetIdealHeight(HWND hwnd)
  625. {
  626. StatusBar *self;
  627. STATUSBAR_RET_VAL(self, hwnd, 0);
  628. if (self->idealHeight < 0)
  629. {
  630. HDC hdc;
  631. self->idealHeight = 0;
  632. hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  633. if (NULL != hdc)
  634. {
  635. TEXTMETRIC metrics;
  636. HFONT font, prevFont;
  637. font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0L);
  638. prevFont = SelectFont(hdc, font);
  639. if (FALSE != GetTextMetrics(hdc, &metrics))
  640. self->idealHeight = metrics.tmHeight;
  641. SelectFont(hdc, prevFont);
  642. ReleaseDC(hwnd, hdc);
  643. }
  644. }
  645. return (LRESULT)self->idealHeight;
  646. }
  647. static LRESULT
  648. StatusBar_OnAddStatus(HWND hwnd, const wchar_t *text)
  649. {
  650. StatusBar *self;
  651. StatusRecord *record;
  652. STATUSBAR_RET_VAL(self, hwnd, STATUS_ERROR);
  653. record = StatusBar_AddRecord(self, text);
  654. if (NULL == record)
  655. return STATUS_ERROR;
  656. StatusBar_InvalidateByRecord(self, hwnd, record);
  657. return record->id;
  658. }
  659. static LRESULT
  660. StatusBar_OnRemoveStatus(HWND hwnd, unsigned int statusId)
  661. {
  662. StatusBar *self;
  663. StatusRecord *record;
  664. size_t index;
  665. STATUSBAR_RET_VAL(self, hwnd, FALSE);
  666. record = StatusBar_FindRecord(self, statusId, &index);
  667. if (NULL == record)
  668. return FALSE;
  669. StatusBar_InvalidateByRecord(self, hwnd, record);
  670. self->list.erase(self->list.begin() + index);
  671. StatusBar_FreeTextBlock(&record->mainBlock);
  672. StatusBar_FreeTextBlock(&record->rightBlock);
  673. free(record);
  674. return (LRESULT)TRUE;
  675. }
  676. static LRESULT
  677. StatusBar_OnSetStatusText(HWND hwnd, unsigned int statusId, const wchar_t *text)
  678. {
  679. StatusBar *self;
  680. StatusRecord *record;
  681. size_t index;
  682. STATUSBAR_RET_VAL(self, hwnd, FALSE);
  683. record = StatusBar_FindRecord(self, statusId, &index);
  684. if (NULL == record)
  685. return FALSE;
  686. StatusBar_InvalidateByRecord(self, hwnd, record);
  687. StatusBar_FreeTextBlock(&record->mainBlock);
  688. record->mainBlock.text = ResourceString_Duplicate(text);
  689. StatusBar_InvalidateByRecord(self, hwnd, record);
  690. return (LRESULT)TRUE;
  691. }
  692. static LRESULT
  693. StatusBar_OnSetStatusRightText(HWND hwnd, unsigned int statusId, const wchar_t *text)
  694. {
  695. StatusBar *self;
  696. StatusRecord *record;
  697. size_t index;
  698. STATUSBAR_RET_VAL(self, hwnd, FALSE);
  699. record = StatusBar_FindRecord(self, statusId, &index);
  700. if (NULL == record)
  701. return FALSE;
  702. StatusBar_InvalidateByRecord(self, hwnd, record);
  703. StatusBar_FreeTextBlock(&record->rightBlock);
  704. record->rightBlock.text = ResourceString_Duplicate(text);
  705. StatusBar_InvalidateByRecord(self, hwnd, record);
  706. return (LRESULT)TRUE;
  707. }
  708. static LRESULT
  709. StatusBar_OnMoveStatus(HWND hwnd, unsigned int statusId, unsigned int moveCommand)
  710. {
  711. StatusBar *self;
  712. StatusRecord *record;
  713. size_t index, count, newIndex;
  714. STATUSBAR_RET_VAL(self, hwnd, FALSE);
  715. record = StatusBar_FindRecord(self, statusId, &index);
  716. if (NULL == record)
  717. return FALSE;
  718. count = self->list.size();
  719. switch(moveCommand)
  720. {
  721. case STATUS_MOVE_BOTTOM:
  722. if (0 == index)
  723. return TRUE;
  724. self->list.erase(self->list.begin() + index);
  725. self->list.insert(self->list.begin(), record);
  726. newIndex = 0;
  727. break;
  728. case STATUS_MOVE_TOP:
  729. if (index == (count - 1))
  730. return TRUE;
  731. self->list.erase(self->list.begin() + index);
  732. self->list.push_back(record);
  733. newIndex = count - 1;
  734. break;
  735. case STATUS_MOVE_UP:
  736. if (index == (count - 1))
  737. return TRUE;
  738. self->list.erase(self->list.begin() + index);
  739. newIndex = index + 1;
  740. self->list.insert(self->list.begin() + newIndex, record);
  741. break;
  742. case STATUS_MOVE_DOWN:
  743. if (index == 0)
  744. return TRUE;
  745. self->list.erase(self->list.begin() + index);
  746. newIndex = index - 1;
  747. self->list.insert(self->list.begin() + newIndex, record);
  748. break;
  749. }
  750. if (index != newIndex)
  751. {
  752. StatusBar_InvalidateByIndex(self, hwnd, index);
  753. StatusBar_InvalidateByIndex(self, hwnd, newIndex);
  754. }
  755. return (LRESULT)TRUE;
  756. }
  757. static LRESULT CALLBACK
  758. StatusBar_WindowProc(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam)
  759. {
  760. switch(uMsg)
  761. {
  762. case WM_CREATE: return StatusBar_OnCreate(hwnd, (CREATESTRUCT*)lParam);
  763. case WM_DESTROY: StatusBar_OnDestroy(hwnd); return 0;
  764. case WM_PAINT: StatusBar_OnPaint(hwnd); return 0;
  765. case WM_PRINTCLIENT: StatusBar_OnPrintClient(hwnd, (HDC)wParam, (UINT)lParam); return 0;
  766. case WM_PRINT: return 0;
  767. case WM_ERASEBKGND: return 0;
  768. case WM_WINDOWPOSCHANGED: StatusBar_OnWindowPosChanged(hwnd, (WINDOWPOS*)lParam); return 0;
  769. case WM_SIZE: return 0;
  770. case WM_MOVE: return 0;
  771. case WM_SETTEXT: return StatusBar_OnSetText(hwnd, (LPCWSTR)lParam);
  772. case WM_GETTEXT: return StatusBar_OnGetText(hwnd, (LPWSTR)lParam, (INT)wParam);
  773. case WM_GETTEXTLENGTH: return StatusBar_OnGetTextLength(hwnd);
  774. case WM_SETFONT: StatusBar_OnSetFont(hwnd, (HFONT)wParam, (BOOL)LOWORD(lParam)); return 0;
  775. case WM_GETFONT: return StatusBar_OnGetFont(hwnd);
  776. case STATUSBAR_WM_SET_BACK_BRUSH: return StatusBar_OnSetBackBrush(hwnd, (HBRUSH)lParam, (BOOL)wParam);
  777. case STATUSBAR_WM_GET_BACK_BRUSH: return StatusBar_OnGetBackBrush(hwnd);
  778. case STATUSBAR_WM_SET_BACK_COLOR: return StatusBar_OnSetBackColor(hwnd, (COLORREF)lParam, (BOOL)wParam);
  779. case STATUSBAR_WM_GET_BACK_COLOR: return StatusBar_OnGetBackColor(hwnd);
  780. case STATUSBAR_WM_SET_TEXT_COLOR: return StatusBar_OnSetTextColor(hwnd, (COLORREF)lParam, (BOOL)wParam);
  781. case STATUSBAR_WM_GET_TEXT_COLOR: return StatusBar_OnGetTextColor(hwnd);
  782. case STATUSBAR_WM_GET_IDEAL_HEIGHT: return StatusBar_OnGetIdealHeight(hwnd);
  783. case STATUSBAR_WM_ADD_STATUS: return StatusBar_OnAddStatus(hwnd, (const wchar_t*)lParam);
  784. case STATUSBAR_WM_REMOVE_STATUS: return StatusBar_OnRemoveStatus(hwnd, (unsigned int)(wParam));
  785. case STATUSBAR_WM_SET_STATUS_TEXT: return StatusBar_OnSetStatusText(hwnd, (unsigned int)(wParam), (const wchar_t*)lParam);
  786. case STATUSBAR_WM_SET_STATUS_RTEXT: return StatusBar_OnSetStatusRightText(hwnd, (unsigned int)(wParam), (const wchar_t*)lParam);
  787. case STATUSBAR_WM_MOVE_STATUS: return StatusBar_OnMoveStatus(hwnd, (unsigned int)(wParam), (unsigned int)lParam);
  788. }
  789. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  790. }