home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / VDLib / source / Dialog.cpp next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  12.7 KB  |  574 lines

  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <commctrl.h>
  4. #include <vd2/system/w32assist.h>
  5. #include <vd2/VDLib/Dialog.h>
  6.  
  7. extern HINSTANCE g_hInst;
  8.  
  9. ///////////////////////////////////////////////////////////////////////////////
  10.  
  11. class VDUIDropFileListW32 : public IVDUIDropFileList {
  12. public:
  13.     VDUIDropFileListW32(VDZHDROP hdrop);
  14.  
  15.     bool GetFileName(int index, VDStringW& fileName);
  16.  
  17. protected:
  18.     const HDROP mhdrop;
  19.     const int mFileCount;
  20. };
  21.  
  22. VDUIDropFileListW32::VDUIDropFileListW32(VDZHDROP hdrop)
  23.     : mhdrop(hdrop)
  24.     , mFileCount(DragQueryFile(mhdrop, 0xFFFFFFFF, NULL, 0))
  25. {
  26. }
  27.  
  28. bool VDUIDropFileListW32::GetFileName(int index, VDStringW& fileName) {
  29.     if (index < 0 || index >= mFileCount)
  30.         return false;
  31.  
  32.     if (VDIsWindowsNT()) {
  33.         wchar_t fileBufW[MAX_PATH];
  34.  
  35.         if (!DragQueryFileW(mhdrop, index, fileBufW, MAX_PATH))
  36.             return false;
  37.  
  38.         fileName = fileBufW;
  39.         return true;
  40.     } else {
  41.         char fileBufA[MAX_PATH];
  42.  
  43.         if (!DragQueryFileA(mhdrop, index, fileBufA, MAX_PATH))
  44.             return false;
  45.  
  46.         fileName = VDTextAToW(fileBufA);
  47.         return true;
  48.     }
  49. }
  50.  
  51. ///////////////////////////////////////////////////////////////////////////////
  52.  
  53. VDDialogFrameW32::VDDialogFrameW32(uint32 dlgid)
  54.     : mpDialogResourceName(MAKEINTRESOURCE(dlgid))
  55.     , mbIsModal(false)
  56.     , mhdlg(NULL)
  57. {
  58. }
  59.  
  60. bool VDDialogFrameW32::Create(VDGUIHandle parent) {
  61.     if (!mhdlg) {
  62.         mbIsModal = false;
  63.  
  64.         if (VDIsWindowsNT())
  65.             CreateDialogParamW(g_hInst, IS_INTRESOURCE(mpDialogResourceName) ? (LPCWSTR)mpDialogResourceName : VDTextAToW(mpDialogResourceName).c_str(), (HWND)parent, StaticDlgProc, (LPARAM)this);
  66.         else
  67.             CreateDialogParamA(g_hInst, mpDialogResourceName, (HWND)parent, StaticDlgProc, (LPARAM)this);
  68.     }
  69.  
  70.     return mhdlg != NULL;
  71. }
  72.  
  73. void VDDialogFrameW32::Destroy() {
  74.     if (mhdlg)
  75.         DestroyWindow(mhdlg);
  76. }
  77.  
  78. sintptr VDDialogFrameW32::ShowDialog(VDGUIHandle parent) {
  79.     mbIsModal = true;
  80.     if (VDIsWindowsNT())
  81.         return DialogBoxParamW(g_hInst, IS_INTRESOURCE(mpDialogResourceName) ? (LPCWSTR)mpDialogResourceName : VDTextAToW(mpDialogResourceName).c_str(), (HWND)parent, StaticDlgProc, (LPARAM)this);
  82.     else
  83.         return DialogBoxParamA(g_hInst, mpDialogResourceName, (HWND)parent, StaticDlgProc, (LPARAM)this);
  84. }
  85.  
  86. void VDDialogFrameW32::Show() {
  87.     if (mhdlg)
  88.         ShowWindow(mhdlg, SW_SHOWNA);
  89. }
  90.  
  91. void VDDialogFrameW32::Hide() {
  92.     if (mhdlg)
  93.         ShowWindow(mhdlg, SW_HIDE);
  94. }
  95.  
  96. void VDDialogFrameW32::End(sintptr result) {
  97.     if (!mhdlg)
  98.         return;
  99.  
  100.     if (mbIsModal)
  101.         EndDialog(mhdlg, result);
  102.     else
  103.         DestroyWindow(mhdlg);
  104. }
  105.  
  106. void VDDialogFrameW32::AddProxy(VDUIProxyControl *proxy, uint32 id) {
  107.     HWND hwnd = GetControl(id);
  108.  
  109.     if (hwnd) {
  110.         proxy->Attach(hwnd);
  111.         mMsgDispatcher.AddControl(proxy);
  112.     }
  113. }
  114.  
  115. VDZHWND VDDialogFrameW32::GetControl(uint32 id) {
  116.     if (!mhdlg)
  117.         return NULL;
  118.  
  119.     return GetDlgItem(mhdlg, id);
  120. }
  121.  
  122. void VDDialogFrameW32::SetFocusToControl(uint32 id) {
  123.     if (!mhdlg)
  124.         return;
  125.  
  126.     HWND hwnd = GetDlgItem(mhdlg, id);
  127.     if (hwnd)
  128.         SendMessage(mhdlg, WM_NEXTDLGCTL, (WPARAM)hwnd, TRUE);
  129. }
  130.  
  131. void VDDialogFrameW32::EnableControl(uint32 id, bool enabled) {
  132.     if (!mhdlg)
  133.         return;
  134.  
  135.     HWND hwnd = GetDlgItem(mhdlg, id);
  136.     if (hwnd)
  137.         EnableWindow(hwnd, enabled);
  138. }
  139.  
  140. void VDDialogFrameW32::SetCaption(uint32 id, const wchar_t *format) {
  141.     if (mhdlg)
  142.         VDSetWindowTextW32(mhdlg, format);
  143. }
  144.  
  145. void VDDialogFrameW32::SetControlText(uint32 id, const wchar_t *s) {
  146.     if (!mhdlg)
  147.         return;
  148.  
  149.     HWND hwnd = GetDlgItem(mhdlg, id);
  150.     if (hwnd)
  151.         VDSetWindowTextW32(hwnd, s);
  152. }
  153.  
  154. void VDDialogFrameW32::SetControlTextF(uint32 id, const wchar_t *format, ...) {
  155.     if (!mhdlg)
  156.         return;
  157.  
  158.     HWND hwnd = GetDlgItem(mhdlg, id);
  159.     if (hwnd) {
  160.         VDStringW s;
  161.         va_list val;
  162.  
  163.         va_start(val, format);
  164.         s.append_vsprintf(format, val);
  165.         va_end(val);
  166.  
  167.         VDSetWindowTextW32(hwnd, s.c_str());
  168.     }
  169. }
  170.  
  171. uint32 VDDialogFrameW32::GetControlValueUint32(uint32 id) {
  172.     if (!mhdlg) {
  173.         FailValidation(id);
  174.         return 0;
  175.     }
  176.  
  177.     HWND hwnd = GetDlgItem(mhdlg, id);
  178.     if (!hwnd) {
  179.         FailValidation(id);
  180.         return 0;
  181.     }
  182.  
  183.     VDStringW s(VDGetWindowTextW32(hwnd));
  184.     unsigned val;
  185.     wchar_t tmp;
  186.     if (1 != swscanf(s.c_str(), L" %u %c", &val, &tmp)) {
  187.         FailValidation(id);
  188.         return 0;
  189.     }
  190.  
  191.     return val;
  192. }
  193.  
  194. double VDDialogFrameW32::GetControlValueDouble(uint32 id) {
  195.     if (!mhdlg) {
  196.         FailValidation(id);
  197.         return 0;
  198.     }
  199.  
  200.     HWND hwnd = GetDlgItem(mhdlg, id);
  201.     if (!hwnd) {
  202.         FailValidation(id);
  203.         return 0;
  204.     }
  205.  
  206.     VDStringW s(VDGetWindowTextW32(hwnd));
  207.     double val;
  208.     wchar_t tmp;
  209.     if (1 != swscanf(s.c_str(), L" %lg %c", &val, &tmp)) {
  210.         FailValidation(id);
  211.         return 0;
  212.     }
  213.  
  214.     return val;
  215. }
  216.  
  217. VDStringW VDDialogFrameW32::GetControlValueString(uint32 id) {
  218.     if (!mhdlg) {
  219.         FailValidation(id);
  220.         return VDStringW();
  221.     }
  222.  
  223.     HWND hwnd = GetDlgItem(mhdlg, id);
  224.     if (!hwnd) {
  225.         FailValidation(id);
  226.         return VDStringW();
  227.     }
  228.  
  229.     return VDGetWindowTextW32(hwnd);
  230. }
  231.  
  232. void VDDialogFrameW32::ExchangeControlValueBoolCheckbox(bool write, uint32 id, bool& val) {
  233.     if (write) {
  234.         val = IsButtonChecked(id);
  235.     } else {
  236.         CheckButton(id, val);
  237.     }
  238. }
  239.  
  240. void VDDialogFrameW32::ExchangeControlValueDouble(bool write, uint32 id, const wchar_t *format, double& val, double minVal, double maxVal) {
  241.     if (write) {
  242.         val = GetControlValueDouble(id);
  243.         if (val < minVal || val > maxVal)
  244.             FailValidation(id);
  245.     } else {
  246.         SetControlTextF(id, format, val);
  247.     }
  248. }
  249.  
  250. void VDDialogFrameW32::ExchangeControlValueString(bool write, uint32 id, VDStringW& s) {
  251.     if (write)
  252.         s = GetControlValueString(id);
  253.     else
  254.         SetControlText(id, s.c_str());
  255. }
  256.  
  257. void VDDialogFrameW32::CheckButton(uint32 id, bool checked) {
  258.     CheckDlgButton(mhdlg, id, checked ? BST_CHECKED : BST_UNCHECKED);
  259. }
  260.  
  261. bool VDDialogFrameW32::IsButtonChecked(uint32 id) {
  262.     return IsDlgButtonChecked(mhdlg, id) != 0;
  263. }
  264.  
  265. void VDDialogFrameW32::BeginValidation() {
  266.     mbValidationFailed = false;
  267. }
  268.  
  269. bool VDDialogFrameW32::EndValidation() {
  270.     if (mbValidationFailed) {
  271.         SignalFailedValidation(mFailedId);
  272.         return false;
  273.     }
  274.  
  275.     return true;
  276. }
  277.  
  278. void VDDialogFrameW32::FailValidation(uint32 id) {
  279.     mbValidationFailed = true;
  280.     mFailedId = id;
  281. }
  282.  
  283. void VDDialogFrameW32::SignalFailedValidation(uint32 id) {
  284.     if (!mhdlg)
  285.         return;
  286.  
  287.     HWND hwnd = GetDlgItem(mhdlg, id);
  288.  
  289.     MessageBeep(MB_ICONEXCLAMATION);
  290.     if (hwnd)
  291.         SetFocus(hwnd);
  292. }
  293.  
  294. void VDDialogFrameW32::LBClear(uint32 id) {
  295.     SendDlgItemMessage(mhdlg, id, LB_RESETCONTENT, 0, 0);
  296. }
  297.  
  298. sint32 VDDialogFrameW32::LBGetSelectedIndex(uint32 id) {
  299.     return SendDlgItemMessage(mhdlg, id, LB_GETCURSEL, 0, 0);
  300. }
  301.  
  302. void VDDialogFrameW32::LBSetSelectedIndex(uint32 id, sint32 idx) {
  303.     SendDlgItemMessage(mhdlg, id, LB_SETCURSEL, idx, 0);
  304. }
  305.  
  306. void VDDialogFrameW32::LBAddString(uint32 id, const wchar_t *s) {
  307.     if (VDIsWindowsNT()) {
  308.         SendDlgItemMessageW(mhdlg, id, LB_ADDSTRING, 0, (LPARAM)s);
  309.     } else {
  310.         SendDlgItemMessageA(mhdlg, id, LB_ADDSTRING, 0, (LPARAM)VDTextWToA(s).c_str());        
  311.     }
  312. }
  313.  
  314. void VDDialogFrameW32::LBAddStringF(uint32 id, const wchar_t *format, ...) {
  315.     VDStringW s;
  316.     va_list val;
  317.  
  318.     va_start(val, format);
  319.     s.append_vsprintf(format, val);
  320.     va_end(val);
  321.  
  322.     LBAddString(id, s.c_str());
  323. }
  324.  
  325. sint32 VDDialogFrameW32::TBGetValue(uint32 id) {
  326.     return SendDlgItemMessage(mhdlg, id, TBM_GETPOS, 0, 0);
  327. }
  328.  
  329. void VDDialogFrameW32::TBSetValue(uint32 id, sint32 value) {
  330.     SendDlgItemMessage(mhdlg, id, TBM_SETPOS, TRUE, value);
  331. }
  332.  
  333. void VDDialogFrameW32::TBSetRange(uint32 id, sint32 minval, sint32 maxval) {
  334.     SendDlgItemMessage(mhdlg, id, TBM_SETRANGEMIN, FALSE, minval);
  335.     SendDlgItemMessage(mhdlg, id, TBM_SETRANGEMAX, TRUE, maxval);
  336. }
  337.  
  338. void VDDialogFrameW32::OnDataExchange(bool write) {
  339. }
  340.  
  341. bool VDDialogFrameW32::OnLoaded() {
  342.     OnDataExchange(false);
  343.     return false;
  344. }
  345.  
  346. bool VDDialogFrameW32::OnOK() {
  347.     BeginValidation();
  348.     OnDataExchange(true);
  349.     return !EndValidation();
  350. }
  351.  
  352. bool VDDialogFrameW32::OnCancel() {
  353.     return false;
  354. }
  355.  
  356. void VDDialogFrameW32::OnSize() {
  357. }
  358.  
  359. void VDDialogFrameW32::OnDestroy() {
  360.     mMsgDispatcher.RemoveAllControls();
  361. }
  362.  
  363. bool VDDialogFrameW32::OnTimer(uint32 id) {
  364.     return false;
  365. }
  366.  
  367. bool VDDialogFrameW32::OnCommand(uint32 id, uint32 extcode) {
  368.     return false;
  369. }
  370.  
  371. void VDDialogFrameW32::OnDropFiles(VDZHDROP hdrop) {
  372.     VDUIDropFileListW32 dropList(hdrop);
  373.  
  374.     OnDropFiles(&dropList);
  375.     DragFinish(hdrop);
  376. }
  377.  
  378. void VDDialogFrameW32::OnDropFiles(IVDUIDropFileList *dropFileList) {
  379. }
  380.  
  381. void VDDialogFrameW32::OnHScroll(uint32 code, int id) {
  382. }
  383.  
  384. void VDDialogFrameW32::OnVScroll(uint32 code, int id) {
  385. }
  386.  
  387. bool VDDialogFrameW32::PreNCDestroy() {
  388.     return false;
  389. }
  390.  
  391. VDZINT_PTR VDZCALLBACK VDDialogFrameW32::StaticDlgProc(VDZHWND hwnd, VDZUINT msg, VDZWPARAM wParam, VDZLPARAM lParam) {
  392.     VDDialogFrameW32 *pThis = (VDDialogFrameW32 *)GetWindowLongPtr(hwnd, DWLP_USER);
  393.  
  394.     if (msg == WM_INITDIALOG) {
  395.         SetWindowLongPtr(hwnd, DWLP_USER, lParam);
  396.         pThis = (VDDialogFrameW32 *)lParam;
  397.         pThis->mhdlg = hwnd;
  398.     } else if (msg == WM_NCDESTROY) {
  399.         if (pThis) {
  400.             bool deleteMe = pThis->PreNCDestroy();
  401.  
  402.             pThis->mhdlg = NULL;
  403.             SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)(void *)NULL);
  404.  
  405.             if (deleteMe)
  406.                 delete pThis;
  407.  
  408.             pThis = NULL;
  409.             return FALSE;
  410.         }
  411.     }
  412.  
  413.     return pThis ? pThis->DlgProc(msg, wParam, lParam) : FALSE;
  414. }
  415.  
  416. VDZINT_PTR VDDialogFrameW32::DlgProc(VDZUINT msg, VDZWPARAM wParam, VDZLPARAM lParam) {
  417.     switch(msg) {
  418.         case WM_INITDIALOG:
  419.             return !OnLoaded();
  420.  
  421.         case WM_COMMAND:
  422.             {
  423.                 uint32 id = LOWORD(wParam);
  424.  
  425.                 if (id == IDOK) {
  426.                     if (!OnOK())
  427.                         End(true);
  428.  
  429.                     return TRUE;
  430.                 } else if (id == IDCANCEL) {
  431.                     if (!OnCancel())
  432.                         End(false);
  433.  
  434.                     return TRUE;
  435.                 } else {
  436.                     if (OnCommand(id, HIWORD(wParam)))
  437.                         return TRUE;
  438.                 }
  439.             }
  440.  
  441.             SetWindowLongPtr(mhdlg, DWLP_MSGRESULT, mMsgDispatcher.Dispatch_WM_COMMAND(wParam, lParam));
  442.             return TRUE;
  443.  
  444.         case WM_NOTIFY:
  445.             SetWindowLongPtr(mhdlg, DWLP_MSGRESULT, mMsgDispatcher.Dispatch_WM_NOTIFY(wParam, lParam));
  446.             return TRUE;
  447.  
  448.         case WM_DESTROY:
  449.             OnDestroy();
  450.             break;
  451.  
  452.         case WM_SIZE:
  453.             OnSize();
  454.             return FALSE;
  455.  
  456.         case WM_TIMER:
  457.             return OnTimer((uint32)wParam);
  458.  
  459.         case WM_DROPFILES:
  460.             OnDropFiles((VDZHDROP)wParam);
  461.             return 0;
  462.  
  463.         case WM_HSCROLL:
  464.             OnHScroll(lParam ? GetWindowLong((HWND)lParam, GWL_ID) : 0, LOWORD(wParam));
  465.             return 0;
  466.  
  467.         case WM_VSCROLL:
  468.             OnVScroll(lParam ? GetWindowLong((HWND)lParam, GWL_ID) : 0, LOWORD(wParam));
  469.             return 0;
  470.     }
  471.  
  472.     return FALSE;
  473. }
  474.  
  475. ///////////////////////////////////////////////////////////////////////////////
  476.  
  477. VDDialogResizerW32::VDDialogResizerW32() {
  478. }
  479.  
  480. VDDialogResizerW32::~VDDialogResizerW32() {
  481. }
  482.  
  483. void VDDialogResizerW32::Init(HWND hwnd) {
  484.     mhwndBase = hwnd;
  485.     mWidth = 1;
  486.     mHeight = 1;
  487.  
  488.     RECT r;
  489.     if (GetClientRect(hwnd, &r)) {
  490.         mWidth = r.right;
  491.         mHeight = r.bottom;
  492.     }
  493. }
  494.  
  495. void VDDialogResizerW32::Relayout() {
  496.     RECT r;
  497.  
  498.     if (GetClientRect(mhwndBase, &r))
  499.         Relayout(r.right, r.bottom);
  500. }
  501.  
  502. void VDDialogResizerW32::Relayout(int width, int height) {
  503.     HDWP hdwp = BeginDeferWindowPos(mControls.size());
  504.  
  505.     mWidth = width;
  506.     mHeight = height;
  507.  
  508.     const int xAnchors[4]={ 0, width >> 1, width, width };
  509.     const int yAnchors[4]={ 0, height >> 1, height, height };
  510.  
  511.     Controls::const_iterator it(mControls.begin()), itEnd(mControls.end());
  512.     for(; it!=itEnd; ++it) {
  513.         const ControlEntry& ent = *it;
  514.         uint32 flags = SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOCOPYBITS;
  515.         const uint8 alignment = ent.mAlignment;
  516.  
  517.         if (!(alignment & kX1Y1Mask))
  518.             flags |= SWP_NOMOVE;
  519.  
  520.         if ((alignment & kX1Y1Mask) == (alignment & kX2Y2Mask))
  521.             flags |= SWP_NOSIZE;
  522.  
  523.         int x1 = ent.mX1 + xAnchors[(alignment >> 0) & 3];
  524.         int x2 = ent.mX2 + xAnchors[(alignment >> 2) & 3];
  525.         int y1 = ent.mY1 + yAnchors[(alignment >> 4) & 3];
  526.         int y2 = ent.mY2 + yAnchors[(alignment >> 6) & 3];
  527.  
  528.         int w = x2 - x1;
  529.         int h = y2 - y1;
  530.  
  531.         if (w < 0)
  532.             w = 0;
  533.  
  534.         if (h < 0)
  535.             h = 0;
  536.  
  537.         if (hdwp) {
  538.             HDWP hdwp2 = DeferWindowPos(hdwp, ent.mhwnd, NULL, x1, y1, w, h, flags);
  539.  
  540.             if (hdwp2) {
  541.                 hdwp = hdwp2;
  542.                 continue;
  543.             }
  544.         }
  545.  
  546.         SetWindowPos(ent.mhwnd, NULL, x1, y1, w, h, flags);
  547.     }
  548.  
  549.     if (hdwp)
  550.         EndDeferWindowPos(hdwp);
  551. }
  552.  
  553. void VDDialogResizerW32::Add(uint32 id, int alignment) {
  554.     HWND hwndControl = GetDlgItem(mhwndBase, id);
  555.     if (!hwndControl)
  556.         return;
  557.  
  558.     RECT r;
  559.     if (!GetWindowRect(hwndControl, &r))
  560.         return;
  561.  
  562.     if (!MapWindowPoints(NULL, mhwndBase, (LPPOINT)&r, 2))
  563.         return;
  564.  
  565.     ControlEntry& ce = mControls.push_back();
  566.  
  567.     ce.mhwnd        = hwndControl;
  568.     ce.mAlignment    = alignment;
  569.     ce.mX1            = r.left   - ((mWidth  * ((alignment >> 0) & 3)) >> 1);
  570.     ce.mY1            = r.top    - ((mHeight * ((alignment >> 4) & 3)) >> 1);
  571.     ce.mX2            = r.right  - ((mWidth  * ((alignment >> 2) & 3)) >> 1);
  572.     ce.mY2            = r.bottom - ((mHeight * ((alignment >> 6) & 3)) >> 1);
  573. }
  574.