commandbar.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "main.h"
  2. #include "./commandbar.h"
  3. typedef struct _CMDBAR
  4. {
  5. HWND hwndOwner;
  6. DLGPROC fnDlgProc;
  7. ULONG_PTR uData;
  8. } CMDBAR;
  9. #define GetBarData(/*HWND*/ __hwndCmdBar) ((CMDBAR*)GetPropW((__hwndCmdBar), L"MLDISCCMDBAR"))
  10. static BOOL CommandBar_OnInitDialog(HWND hdlg, HWND hwndFocus, LPARAM lParam)
  11. {
  12. CMDBARCREATESTRUCT *pcbcs = (CMDBARCREATESTRUCT*)lParam;
  13. if (pcbcs && pcbcs->fnDialogProc)
  14. {
  15. CMDBAR *pcb = (CMDBAR*)calloc(1, sizeof(CMDBAR));
  16. if (pcb)
  17. {
  18. pcb->fnDlgProc = pcbcs->fnDialogProc;
  19. pcb->hwndOwner = pcbcs->hwndOwner;
  20. pcb->uData = pcbcs->uData;
  21. if (!SetPropW(hdlg, L"MLDISCCMDBAR", (HANDLE)pcb))
  22. {
  23. free(pcb);
  24. DestroyWindow(hdlg);
  25. }
  26. else return pcbcs->fnDialogProc(hdlg, WM_INITDIALOG, (WPARAM)hwndFocus, pcbcs->uData);
  27. }
  28. }
  29. return FALSE;
  30. }
  31. static void CommandBar_OnDestroy(HWND hdlg)
  32. {
  33. CMDBAR *pcb = GetBarData(hdlg);
  34. if (pcb)
  35. {
  36. RemovePropW(hdlg, L"MLDISCCMDBAR");
  37. free(pcb);
  38. }
  39. }
  40. static BOOL CALLBACK CommandBar_EnumChildHeight(HWND hwnd, LPARAM param)
  41. {
  42. if (!param) return FALSE;
  43. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE);
  44. if (hdc)
  45. {
  46. HFONT hf = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  47. if (NULL == hf) hf = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  48. if (NULL != hf)
  49. {
  50. TEXTMETRICW tm = {0};
  51. HFONT hfo = (HFONT)SelectObject(hdc, hf);
  52. if (GetTextMetricsW(hdc, &tm))
  53. {
  54. INT *pmh = (INT*)param;
  55. if (tm.tmHeight > *pmh) *pmh = tm.tmHeight;
  56. }
  57. SelectObject(hdc, hfo);
  58. }
  59. ReleaseDC(hwnd, hdc);
  60. }
  61. return TRUE;
  62. }
  63. static INT CommandBar_OnGetBestHeight(HWND hwnd)
  64. {
  65. INT maxHeight = 0;
  66. EnumChildWindows(hwnd, CommandBar_EnumChildHeight, (LPARAM)&maxHeight);
  67. if (maxHeight != 0) maxHeight += 2;
  68. return maxHeight;
  69. }
  70. INT_PTR CALLBACK CommandBar_DialogProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  71. {
  72. CMDBAR *pcb = GetBarData(hdlg);
  73. if (!pcb)
  74. {
  75. if (WM_INITDIALOG == uMsg) return CommandBar_OnInitDialog(hdlg, (HWND)wParam, lParam);
  76. return 0;
  77. }
  78. if (pcb->fnDlgProc(hdlg, uMsg, wParam, lParam))
  79. {
  80. if (WM_DESTROY == uMsg) CommandBar_OnDestroy(hdlg);
  81. return TRUE;
  82. }
  83. switch(uMsg)
  84. {
  85. case WM_DESTROY: CommandBar_OnDestroy(hdlg); return TRUE;
  86. case WM_COMMAND:
  87. if (pcb->hwndOwner) SendMessageW(pcb->hwndOwner, uMsg, wParam, lParam); return TRUE;
  88. break;
  89. case CBM_GETBESTHEIGHT: MSGRESULT(hdlg, CommandBar_OnGetBestHeight(hdlg));
  90. case CBM_GETOWNER: MSGRESULT(hdlg, pcb->hwndOwner);
  91. case CBM_SETOWNER:
  92. SetWindowLongPtrW(hdlg, DWLP_MSGRESULT, (LONGX86)(LONG_PTR)pcb->hwndOwner);
  93. pcb->hwndOwner = (HWND)lParam;
  94. return TRUE;
  95. case CBM_GETDATA:
  96. {
  97. CMDBAR *pBar = GetBarData(hdlg);
  98. MSGRESULT(hdlg, (pBar) ? pBar->uData : NULL);
  99. }
  100. case CBM_SETDATA:
  101. {
  102. CMDBAR *pBar = GetBarData(hdlg);
  103. if (pBar) pBar->uData = (ULONG_PTR)lParam;
  104. MSGRESULT(hdlg, (NULL != pBar));
  105. }
  106. }
  107. return 0;
  108. }