home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / xdialog.cpp < prev    next >
C/C++ Source or Header  |  1998-03-27  |  10KB  |  347 lines

  1. #include "xmodlg.h"
  2. #include "xmodal.h"
  3. #include "xres.h"
  4. #include "xreslib.h"
  5. #include "xexcept.h"
  6. #include "xapp.h"
  7.  
  8. extern LONG MAXX;
  9. extern LONG MAXY;
  10. extern SHORT cxDlgFrame;
  11. extern SHORT cyDlgFrame;
  12. extern SHORT cxBorder;
  13. extern SHORT cyBorder;
  14. extern SHORT cxSizeBorder;
  15. extern SHORT cySizeBorder;
  16.  
  17. void GetScreenValues();
  18. void BuildChilds(HWND dlgHandle);
  19. MRESULT HandleDefault(XWindow * w, ULONG msg, MPARAM mp1, MPARAM mp2, BOOL & handled);
  20.  
  21.  
  22. void SetCentered( HWND winhandle)
  23. {
  24.      if (MAXX == 0)
  25.       GetScreenValues();
  26.  
  27.    SWP swp;
  28.    WinQueryWindowPos(winhandle, &swp);
  29.    WinSetWindowPos( winhandle, HWND_TOP, MAXX / 2 - swp.cx / 2, MAXY / 2 - swp.cy / 2, swp.cx, swp.cy, SWP_MOVE);
  30. }
  31.  
  32.  
  33. MRESULT EXPENTRY ModelessDProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  34. {
  35.    XModelessDialog *w = (XModelessDialog *) WinQueryWindowPtr(hwnd, 0);
  36.  
  37.    if (w)
  38.    {
  39.       if(msg == WM_CLOSE )
  40.       {
  41.          WinSetWindowULong(hwnd, 0, 0);
  42.          delete w;
  43.          return WinDefDlgProc(hwnd, msg, mp1, mp2);
  44.       }
  45.       else if(msg == WM_COMMAND )
  46.       {
  47.          if( w->DoCommand( SHORT1FROMMP(mp1) ) == TRUE)
  48.          {
  49.             WinSetWindowULong(hwnd, 0, 0);
  50.             delete w;
  51.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  52.          }
  53.          else
  54.             return (MRESULT) FALSE;
  55.       }
  56.       else if(msg == WM_SYSCOMMAND)
  57.       {
  58.          if( w->DoSysCommand( SHORT1FROMMP(mp1)) == TRUE)
  59.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  60.          return 0;
  61.       }
  62.       else if(msg == WM_MOVE)
  63.       {
  64.          MRESULT mr = WinDefDlgProc(hwnd, msg, mp1, mp2);
  65.          w->DoMove();
  66.          return mr;
  67.       }
  68.       else if(msg == WM_ADJUSTWINDOWPOS )
  69. //      else if(msg == WM_WINDOWPOSCHANGED )
  70.       {
  71. //         XProcess::Beep(200,200);
  72.          MRESULT mr = WinDefDlgProc(hwnd, msg, mp1, mp2);
  73.          PSWP pswp = (PSWP) mp1;
  74.          XSize size( pswp->cx, pswp->cy);
  75.          w->DoSize(&size);
  76.          return mr;
  77.       } /* end if */
  78.  
  79.       BOOL handeld = FALSE;
  80.       MRESULT mr = HandleDefault(w, msg, mp1, mp2, handeld);
  81.  
  82.       if (handeld)
  83.          return mr;
  84.    }
  85.    return WinDefDlgProc(hwnd, msg, mp1, mp2);
  86. }
  87.  
  88.  
  89. XDialog :: ~XDialog()
  90. {
  91.    HENUM enumWindow = WinBeginEnumWindows(winhandle);
  92.    HWND hwnd;
  93.  
  94.    while ((hwnd = WinGetNextWindow(enumWindow)) != NULLHANDLE)
  95.    {
  96.  
  97.       XWindow *w = (XWindow *) WinQueryWindowPtr(hwnd, 0);
  98.  
  99.       if (w && w != this)
  100.       {
  101.          delete w;
  102.       }
  103.    }
  104.    WinEndEnumWindows(enumWindow);
  105.  
  106.    XApplication::GetApplication()->RemoveWindow(this);
  107. }
  108.  
  109.  
  110. /*@
  111. @class XDialog
  112. @parent XWindow
  113. @type overview
  114. @symbol _
  115. */
  116.  
  117.  
  118. /*@
  119. @class XModelessDialog
  120. @parent XDialog
  121. @type overview
  122. @symbol _
  123. */
  124.  
  125.  
  126. /*@ XModelessDialog :: XModelessDialog( const XResource * id, const XWindow * owner, const BOOL center)
  127. @group constructors/destructors
  128. @remarks Construct a modeless dialog.
  129. @parameters
  130. <t '°' c=2>
  131. °XResource *   °a resource which describes the dialog template
  132. °XWindow *       °a pointer to the owner-window (can be NULL)
  133. °BOOL            °TRUE=show centered<BR>FALSE=not centered<BR>(default is TRUE)
  134. </t>
  135. */
  136. XModelessDialog :: XModelessDialog( const XResource * id, const XWindow * owner, const BOOL center)
  137. {
  138.    CreateDialog( id->GetID(), id->GetResourceLibrary(), owner, center);
  139. }
  140.  
  141.  
  142. /*@ XModelessDialog :: XModelessDialog( const ULONG id, const XWindow * owner, const BOOL center)
  143. @group constructors/destructors
  144. @remarks Construct a modeless dialog.
  145. @parameters
  146. <t '°' c=2>
  147. °ULONG         °ID of the dialog template, the dialog-resources mus be linked to the exe-file
  148. °XWindow *       °a pointer to the owner-window (can be NULL)
  149. °BOOL            °TRUE=show centered<BR>FALSE=not centered<BR>(default is TRUE)
  150. </t>
  151. */
  152. XModelessDialog :: XModelessDialog( const ULONG id, const XWindow * owner, const BOOL center)
  153. {
  154.    CreateDialog( id, NULL, owner, center);
  155. }
  156.  
  157.  
  158. void XModelessDialog :: CreateDialog( const ULONG id, const XResourceLibrary * lib = NULL, const XWindow * owner, const BOOL center)
  159. {
  160.    HWND hwnd = HWND_DESKTOP;
  161.    if(owner)
  162.       hwnd = owner->GetHandle();
  163.    if(lib)
  164.       winhandle = WinLoadDlg( HWND_DESKTOP, hwnd, (PFNWP) ModelessDProc, lib->GetModuleHandle(), id, NULL);
  165.    else
  166.       winhandle = WinLoadDlg( HWND_DESKTOP, hwnd, (PFNWP) ModelessDProc, XApplication::GetApplication()->GetResourceLibrary()->GetModuleHandle(), id, NULL);
  167.  
  168.    if (winhandle == 0)
  169.    {
  170.       OOLThrow("could not create dialog from resources!", 0);
  171.       return;
  172.    }
  173.    WinSetWindowPtr(winhandle, 0, this);
  174.  
  175.    if(center)
  176.       SetCentered( winhandle );
  177.  
  178.    BuildChilds(winhandle);
  179.    XApplication::GetApplication()->AddWindow(this);
  180. }
  181.  
  182.  
  183. MRESULT EXPENTRY ModalDProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  184. {
  185.    XModalDialog *w = (XModalDialog *) WinQueryWindowPtr(hwnd, 0);
  186.  
  187.    if (w)
  188.    {
  189.        if(msg == WM_CLOSE )
  190.       {
  191.          WinSetWindowULong(hwnd, 0, 0);
  192.          WinDismissDlg( w->winhandle, -1 );
  193.          delete w;
  194.          return WinDefDlgProc(hwnd, msg, mp1, mp2);
  195.       }
  196.       if(msg == WM_COMMAND )
  197.       {
  198.          if( w->DoCommand( SHORT1FROMMP(mp1) ) == TRUE)
  199.          {
  200.             WinSetWindowULong(hwnd, 0, 0);
  201.             WinDismissDlg( w->winhandle, SHORT1FROMMP(mp1) );
  202.             delete w;
  203.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  204.          }
  205.          else
  206.             return (MRESULT) FALSE;
  207.       }
  208.       else if(msg == WM_SYSCOMMAND)
  209.       {
  210.          if( w->DoSysCommand( SHORT1FROMMP(mp1)) == TRUE)
  211.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  212.          return 0;
  213.       }
  214.       else if(msg == WM_MOVE)
  215.       {
  216.          MRESULT mr = WinDefDlgProc(hwnd, msg, mp1, mp2);
  217.          w->DoMove();
  218.          return mr;
  219.       }
  220.       else if(msg == WM_ADJUSTWINDOWPOS )
  221.       {
  222.          MRESULT mr = WinDefDlgProc(hwnd, msg, mp1, mp2);
  223.          PSWP pswp = (PSWP) mp1;
  224.          XSize size( pswp->cx, pswp->cy);
  225.          w->DoSize(&size);
  226.          return mr;
  227.       } /* end if */
  228.  
  229.       BOOL handeld = FALSE;
  230.       MRESULT mr = HandleDefault(w, msg, mp1, mp2, handeld);
  231.  
  232.       if (handeld)
  233.          return (MRESULT) mr;
  234.    }
  235.    return WinDefDlgProc(hwnd, msg, mp1, mp2);
  236. }
  237.  
  238.  
  239. /*@
  240. @class XModalDialog
  241. @parent XDialog
  242. @type overview
  243. @symbol _
  244. */
  245.  
  246. /*@ XModalDialog :: XModalDialog( const XResource * id, const XWindow * owner, const BOOL center)
  247. @group constructors/destructors
  248. @remarks Construct a modal dialog. You must call XModalDialog::Start() after
  249. constructing an instance to make the dialog work. When constructing the dialog you can initialize
  250. the dialog controls.
  251. @parameters
  252. <t '°' c=2>
  253. °XResource *       °A resource which describes the dialog template
  254. °XWindow *          °A pointer to the owner-window. If now owner is given an error occures.
  255. °BOOL            °TRUE=show centered<BR>FALSE=not centered<BR>(default is TRUE)
  256. </t>
  257. */
  258. XModalDialog :: XModalDialog( const XResource * id, const XWindow * owner, const BOOL center)
  259. {
  260.    CreateDialog( id->GetID(), id->GetResourceLibrary(), owner, center);
  261. }
  262.  
  263.  
  264. /*@ XModalDialog :: XModalDialog( const ULONG id, const XWindow * owner, const BOOL center)
  265. @group constructors/destructors
  266. @remarks Construct a modal dialog. You must call XModalDialog::Start() after
  267. constructing an instance to make the dialog work. When constructing the dialog you can initialize
  268. the dialog controls.
  269. @parameters
  270. <t '°' c=2>
  271. °ULONG          °ID of the dialog template. The dialog-resources must be linked to the exe-file.
  272. °XWindow *          °A pointer to the owner-window. If now owner is given an error occures.
  273. °BOOL            °TRUE=show centered<BR>FALSE=not centered<BR>(default is TRUE)
  274. </t>
  275. */
  276. XModalDialog :: XModalDialog( const ULONG id, const XWindow * owner, const BOOL center)
  277. {
  278.    CreateDialog( id, NULL, owner, center);
  279. }
  280.  
  281.  
  282. void XModalDialog :: CreateDialog( const ULONG id, const XResourceLibrary * lib, const XWindow * owner, BOOL center)
  283. {
  284.    HWND hwnd = HWND_DESKTOP;
  285.  
  286.    if(owner)
  287.       hwnd = owner->GetHandle();
  288.    else
  289.       OOLThrow("XModalDialog need a parent window!", 0);
  290.  
  291.    if( lib )
  292.       winhandle = WinLoadDlg( HWND_DESKTOP, hwnd, (PFNWP) ModalDProc, lib->GetModuleHandle(), id, NULL);
  293.    else
  294.       winhandle = WinLoadDlg( HWND_DESKTOP, hwnd, (PFNWP) ModalDProc, XApplication::GetApplication()->GetResourceLibrary()->GetModuleHandle(), id, NULL);
  295.  
  296.    if (winhandle == 0)
  297.    {
  298.       OOLThrow("could not create dialog from resources!", 0);
  299.       return;
  300.    }
  301.    WinSetWindowPtr(winhandle, 0, this);
  302.  
  303.    if( center )
  304.       SetCentered(winhandle);
  305.  
  306.    BuildChilds(winhandle);
  307.  
  308.    XApplication::GetApplication()->AddWindow(this);
  309. }
  310.  
  311.  
  312.  
  313. /*@ XModalDialog :: Start( void )
  314. @group misc
  315. @remarks To make a modal dialog work you must call Start(). this functions returns the value
  316. of the last command.
  317. @returns LONG command-value, -1 if the dialog was closed with the system-menu
  318. @example
  319. XModalDialog * dlg = new XModalDialog(...)
  320. int command = dlg->Start();
  321. */
  322. LONG XModalDialog :: Start( void )
  323. {
  324.    return WinProcessDlg( winhandle);
  325. }
  326.  
  327.  
  328. //Docs only
  329. /*@
  330. @class XDialog
  331. @type overview
  332. @symbol _
  333. @remarks XDialog is the base class for XModalDialog and XModelessDialog.
  334. You cannot construct an instance of XDialog directly, you have to chose
  335. one of the child-classes.
  336. */
  337.  
  338.  
  339. /*@ XDialog::DoCommand(LONG)
  340. @remarks If a command was received by a dialog, eg. from a button, you must
  341. decide if the dialog can be destroyed or not. Override the XDialog::DoCommand() function,
  342. return TRUE if the dialog can be destroyed, otherwise return FALSE.
  343. @new
  344. @parameters LONG command
  345. @returns BOOL TRUE=destroy, FALSE=do not destroy
  346. */
  347.