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

  1. #include "XScrlWnd.h"
  2. #include "xrect.h"
  3. #include "XScrlBar.h"
  4. #include "XFrame.h"
  5. #include "XDefhdl.h"
  6. #include "xexcept.h"
  7.  
  8. class scrollhandler:public XDefaultHandler
  9. {
  10.    XScrollWindow *win;
  11.    public:
  12.       scrollhandler(XScrollWindow * w):XDefaultHandler(w) {win = w;}
  13.       BOOL HandleEvent(ULONG, void *, void *, LONG*);
  14. };
  15.  
  16.  
  17. /*@
  18. @class XScrollWindow
  19. @parent XFrameWindow
  20. @type overview
  21. @symbol _
  22. */
  23.  
  24.  
  25. /*@ XScrollWindow :: XScrollWindow(const XResource * resource, const char *title, const ULONG style, const XRect * rec, const XFrameWindow * parent, const BOOL build, const BOOL animate)
  26. @group constructors/destructors
  27. @remarks Constructs a frame-window which does scrolling automaticaly.
  28. Note that destructors of windows are called automaticaly when a window is closed! (see ~XFrameWindow)
  29. @parameters <t '°' c=2>
  30.             °XResource * theResourceID   °a XResource contains two informations, an ID and a pointer
  31.                                         to a XResourceLibrary. If you want to create a window out of
  32.                                         a resourcefile you must specify the ID (otherwise it can be zero)
  33.                                         and the XResourceLibrary which contains the window-resource.
  34.                                         The window which is created always belongs to the process who
  35.                                         owns the resource library, so if you work with multiple processes
  36.                                         every process must have its own resource library.
  37.             °char * theTitle             °The title of the window which is displayed in the titlebar
  38.             °ULONG theStyleofWindow      °You can specify the style of the window with the following defines,
  39.                                         which can be or-ed:
  40. <BR>
  41. FRM_TITLEBAR the window gets a titlebar
  42. <BR>
  43. FRM_SYSMENU the window gets the system menu
  44. <BR>
  45. FRM_MINBUTTON the titlebar get a button to minimize the window
  46. <BR>
  47.                                            FRM_MAXBUTTON the titlebar get a button to maximize the window
  48. <BR>
  49.                                            FRM_CENTER the window is created in the midle of the workplace
  50. <BR>
  51.                                            FRM_SIZEBORDER the windowsize can be changed by the user
  52. <BR>
  53.                                            FRM_DIALOGBORDER the window gets a thick border
  54. <BR>
  55.                                            FRM_BORDER the window gets a thin border
  56. <BR>
  57.                                            FRM_TASKLIST the window is displayed in the tasklist
  58. <BR>
  59.                                            FRM_NOMOVEWITHOWNER the window dont∩t move when the parent is moved
  60. <BR>
  61.                                            FRM_ICON  the window get an icon wich is identified by theResourceID,                                                            if the icon is not found in the resource-library, an error ocurses
  62. <BR>
  63.                                            FRM_ACCELTABLE an acceltable will be loaded from the resources with the windows id.
  64. <BR>
  65.                                            FRM_SYSMODAL the window is displayed system-modal
  66. <BR>
  67. FRM_SCREENALIGN
  68. <BR>
  69. FRM_MOUSEALIGN
  70. <BR>
  71. FRM_HIDEBUTTON
  72. <BR>
  73. FRM_HIDEMAX
  74. <BR>
  75. FRM_AUTOICON
  76. <BR>
  77. there are three static member-variables for default styles
  78. <BR>
  79. defaultStyle default setting for a framewindow
  80. <BR>
  81. defaultClientStyle default setting for windows wich are displayed as a clientwindow of a framewindow
  82. <BR>
  83. defaultDialogStyle default setting for windows wich are displayed as a dialog
  84. <BR>
  85.                                         Default is defaultStyle.
  86.             °XRect * theRectangle        °On default a window is created with length and hight of zero. Windows
  87.                                         which are created with an resource template get the size of the template.
  88.                                         Default is NULL.<BR>
  89.                                         If theRectangle is specified, the window gets the size of it.
  90.             °XFrameWindow * parent       °If parent is specified the window is a client of the parent. The
  91.                                         behavior depends on the styles you have set.<BR>
  92.                                         Default is NULL.
  93.             °BOOL buildFromResource      °If this variable is set OOL try to build the window with a resource
  94.                                         template which is identified by theResourceID. If the template is
  95.                                         not found, an error ocurses.<BR>
  96.                                         Default is FALSE.
  97.             °BOOL animate                °Enable/disable animation on window creation. Default is FALSE
  98.          </t>
  99. */
  100. XScrollWindow :: XScrollWindow(const XResource * resource, const char *title, const ULONG style, const XRect& rec, const XFrameWindow * parent, const BOOL build, const BOOL animate):XFrameWindow(resource, title, style, rec, parent, build, animate)
  101. {
  102.    Setup();
  103. }
  104.  
  105.  
  106. /*@ XScrollWindow :: XScrollWindow(const XResource * resource, const char *title, const ULONG style, const XRect * rec, const XFrameWindow * parent, const BOOL build, const BOOL animate)
  107. @group constructors/destructors
  108. @remarks Constructs a frame-window which does scrolling automaticaly.
  109. Note that destructors of windows are called automaticaly when a window is closed! (see ~XFrameWindow)
  110. @parameters <t '°' c=2>
  111.             °ULONG   °ID of the window. If resources are used they must be linked to the exe-file.
  112.             °char *              °The title of the window which is displayed in the titlebar
  113.             °ULONG    °You can specify the style of the window with the following defines,
  114.                                         which can be or-ed:
  115. <BR>
  116. FRM_TITLEBAR the window gets a titlebar
  117. <BR>
  118. FRM_SYSMENU the window gets the system menu
  119. <BR>
  120. FRM_MINBUTTON the titlebar get a button to minimize the window
  121. <BR>
  122.                                            FRM_MAXBUTTON the titlebar get a button to maximize the window
  123. <BR>
  124.                                            FRM_CENTER the window is created in the midle of the workplace
  125. <BR>
  126.                                            FRM_SIZEBORDER the windowsize can be changed by the user
  127. <BR>
  128.                                            FRM_DIALOGBORDER the window gets a thick border
  129. <BR>
  130.                                            FRM_BORDER the window gets a thin border
  131. <BR>
  132.                                            FRM_TASKLIST the window is displayed in the tasklist
  133. <BR>
  134.                                            FRM_NOMOVEWITHOWNER the window dont∩t move when the parent is moved
  135. <BR>
  136.                                            FRM_ICON  the window get an icon wich is identified by theResourceID,                                                            if the icon is not found in the resource-library, an error ocurses
  137. <BR>
  138.                                            FRM_ACCELTABLE an acceltable will be loaded from the resources with the windows id.
  139. <BR>
  140.                                            FRM_SYSMODAL the window is displayed system-modal
  141. <BR>
  142. FRM_SCREENALIGN
  143. <BR>
  144. FRM_MOUSEALIGN
  145. <BR>
  146. FRM_HIDEBUTTON
  147. <BR>
  148. FRM_HIDEMAX
  149. <BR>
  150. FRM_AUTOICON
  151. <BR>
  152. there are three static member-variables for default styles
  153. <BR>
  154. defaultStyle default setting for a framewindow
  155. <BR>
  156. defaultClientStyle default setting for windows wich are displayed as a clientwindow of a framewindow
  157. <BR>
  158. defaultDialogStyle default setting for windows wich are displayed as a dialog
  159. <BR>
  160.                                         Default is defaultStyle.
  161.             °XRect *            °On default a window is created with length and hight of zero. Windows
  162.                                         which are created with an resource template get the size of the template.
  163.                                         Default is NULL.<BR>
  164.                                         If theRectangle is specified, the window gets the size of it.
  165.             °XFrameWindow *        °Parent-window, if parent is specified the window is a client of the parent. The
  166.                                         behavior depends on the styles you have set.<BR>
  167.                                         Default is NULL.
  168.             °BOOL buildFromResource      °If this variable is set OOL try to build the window with a resource
  169.                                         template which is identified by theResourceID. If the template is
  170.                                         not found, an error ocurses.<BR>
  171.                                         Default is FALSE.
  172.             °BOOL animate                °Enable/disable animation on window creation. Default is FALSE
  173.          </t>
  174. */
  175. XScrollWindow :: XScrollWindow(const ULONG id, const char *title, const ULONG style, const XRect& rec, const XFrameWindow * parent, const BOOL build, const BOOL animate):XFrameWindow(id, title, style, rec, parent, build, animate)
  176. {
  177.    Setup();
  178. }
  179.  
  180.  
  181. void XScrollWindow :: Setup()
  182. {
  183.    scrollEnabled = TRUE;
  184.  
  185.    scrollhandler *handler = new scrollhandler(this);
  186.  
  187.    scrollPosX = scrollPosY = 0;
  188.    xStep = yStep = 20;
  189.    xPage = yPage = 60;
  190.  
  191.    if (dlgHandle)
  192.    {
  193.       SWP swp;
  194.  
  195.       WinQueryWindowPos(dlgHandle, &swp);
  196.       SetVirtualX(swp.cx);
  197.       SetVirtualY(swp.cy);
  198.    }
  199.    else
  200.    {
  201.       SetVirtualX(0);
  202.       SetVirtualY(0);
  203.    }
  204. }
  205.  
  206.  
  207. /*@ XScrollWindow::SetVirtualX(const LONG x)
  208. @group scroll-functions
  209. @remarks Set the virtual x-size of the window
  210. @parameters LONG x                       virtual x-size
  211. */
  212. void XScrollWindow::SetVirtualX(const LONG x)
  213. {
  214.    virtualX = x;
  215.    CalcHorzSize();
  216. }
  217.  
  218.  
  219. /*@ XScrollWindow::SetVirtualY(const LONG y)
  220. @group scroll-functions
  221. @remarks Set the virtual y-size of the window
  222. @parameters LONG y                       virtual y-size
  223. */
  224. void XScrollWindow::SetVirtualY(const LONG y)
  225. {
  226.    virtualY = y;
  227.    CalcVertSize();
  228. }
  229.  
  230.  
  231. /*@ XScrollWindow::VScroll(LONG s)
  232. @group scroll-functions
  233. @remarks This function is called when the window-contents must be scrolled vertcal.
  234. On default scrolling is done automaticaly
  235. @parameters LONG s    how much pixels to scroll
  236. */
  237. void XScrollWindow::VScroll(LONG s)
  238. {
  239.    if(scrollEnabled)
  240.    {
  241.       SWP swp;
  242.       HWND cl = winhandle, hwnd;
  243.       if (dlgHandle)
  244.          cl = dlgHandle;
  245.  
  246.       HENUM enumWindow = WinBeginEnumWindows(cl);
  247.  
  248.       WinEnableWindowUpdate(cl, FALSE);
  249.       char buffer[5];
  250.       SHORT cor;
  251.       ULONG style;
  252.  
  253.       while ((hwnd = WinGetNextWindow(enumWindow)) != 0)
  254.       {
  255.          WinQueryClassName(hwnd, 5, (PSZ) buffer);
  256.          style = WinQueryWindowULong(hwnd, QWL_STYLE);
  257.          if (style & ES_MARGIN && buffer[1] == '6')
  258.             cor = 3;
  259.          else
  260.             cor = 0;
  261.          WinQueryWindowPos(hwnd, &swp);
  262.          swp.y -= s; //-
  263.          WinSetWindowPos(hwnd, 0, swp.x + cor, swp.y + cor, 0, 0, SWP_MOVE);
  264.       };
  265.       WinEndEnumWindows(enumWindow);
  266.       WinEnableWindowUpdate(cl, TRUE);
  267.    }
  268.    scrollPosY += s; //+
  269. }
  270.  
  271.  
  272. /*@ XScrollWindow::HScroll(LONG s)
  273. @group scroll-functions
  274. @remarks This function is called when the window-contents must be scrolled horizontal.
  275. On default scrolling is done automaticaly
  276. @parameters LONG s   how much pixels to scroll
  277. */
  278. void XScrollWindow::HScroll(LONG s)
  279. {
  280.    if(scrollEnabled)
  281.    {
  282.    SWP swp;
  283.    HWND cl = winhandle, hwnd;
  284.  
  285.    if (dlgHandle)
  286.       cl = dlgHandle;
  287.  
  288.    HENUM enumWindow = WinBeginEnumWindows(cl);
  289.  
  290.    if (scrollPosX + s > virtualX)
  291.       s = virtualX - scrollPosX;
  292.    if (scrollPosX + s < 0)
  293.       s = -scrollPosX;
  294.  
  295.    WinEnableWindowUpdate(cl, FALSE);
  296.  
  297.    char buffer[5];
  298.    SHORT cor;
  299.    ULONG style;
  300.  
  301.    while ((hwnd = WinGetNextWindow(enumWindow)) != 0)
  302.    {
  303.       WinQueryClassName(hwnd, 5, (PSZ) buffer);
  304.       style = WinQueryWindowULong(hwnd, QWL_STYLE);
  305.       if (style & ES_MARGIN && buffer[1] == '6')
  306.          cor = 3;
  307.       else
  308.          cor = 0;
  309.  
  310.       WinQueryWindowPos(hwnd, &swp);
  311.       swp.x -= s;
  312.       WinSetWindowPos(hwnd, 0, swp.x + cor, swp.y + cor, 0, 0, SWP_MOVE);
  313.    };
  314.    WinEndEnumWindows(enumWindow);
  315.  
  316.    WinEnableWindowUpdate(cl, TRUE);
  317.    }
  318.    scrollPosX += s;
  319. }
  320.  
  321.  
  322. BOOL scrollhandler::HandleEvent(ULONG msg, MPARAM mp1, MPARAM mp2, LONG * retVal)
  323. {
  324.    SHORT s = 0;
  325.    *retVal = TRUE;
  326.  
  327.    switch (msg)
  328.    {
  329.    case OOL_ADDCLIENT:
  330.       SWP swp;
  331.       WinQueryWindowPos((HWND) mp1, &swp);
  332.       if (swp.cx + swp.x > win->virtualX)
  333.       {
  334.          win->virtualX = swp.x + swp.cx + 5;
  335.          win->CalcHorzSize();
  336.       }
  337.       if (swp.cy + swp.y > win->virtualY)
  338.       {
  339.          win->virtualY = swp.y + swp.cy + 5;
  340.          win->CalcVertSize();
  341.       }
  342.       return TRUE;
  343.    case WM_HSCROLL:
  344.       {
  345.          switch (SHORT2FROMMP(mp2))
  346.          {
  347.          case SB_LINERIGHT:
  348.             s = win->xStep;
  349.             break;
  350.          case SB_LINELEFT:
  351.             s = -win->xStep;
  352.             break;
  353.          case SB_PAGELEFT:
  354.             s = -win->xPage;
  355.             break;
  356.          case SB_PAGERIGHT:
  357.             s = win->xPage;
  358.             break;
  359.          case SB_ENDSCROLL:
  360.             return 0;
  361.          default:
  362.             s = SHORT1FROMMP(mp2) - win->scrollPosX;
  363.          }
  364.          win->HScroll(s);
  365.          if (win->horz)
  366.             win->horz->SetPos(win->scrollPosX);
  367.       }
  368.       return TRUE;
  369.    case WM_VSCROLL:
  370.       {
  371.          switch (SHORT2FROMMP(mp2))
  372.          {
  373.          case SB_LINEUP:
  374.             s = -win->yStep;
  375.             break;
  376.          case SB_LINEDOWN:
  377.             s = win->yStep;
  378.             break;
  379.          case SB_PAGEDOWN:
  380.             s = win->yPage;
  381.             break;
  382.          case SB_PAGEUP:
  383.             s = -win->yPage;
  384.             break;
  385.          case SB_ENDSCROLL:
  386.             return 0;
  387.          default:
  388.             s = SHORT1FROMMP(mp2) - win->scrollPosY;
  389.          }
  390.          win->VScroll(s);
  391.          if (win->vert)
  392.             win->vert->SetPos( win->scrollPosY);
  393.       }
  394.       return TRUE;
  395.    }
  396.    *retVal = FALSE;
  397.    return FALSE;
  398. }
  399.  
  400.  
  401. void XScrollWindow::SetXScrollPos( const USHORT s)
  402. {
  403.    scrollPosX = s;
  404.    if(horz)
  405.       horz->SetPos( scrollPosX);
  406. }
  407.  
  408.  
  409. void XScrollWindow::SetYScrollPos( const USHORT s)
  410. {
  411.    scrollPosY = s;
  412.    if(vert)
  413.       vert->SetPos( scrollPosY);
  414. }
  415.  
  416.  
  417. void XScrollWindow::CalcHorzSize(void)
  418. {
  419.    if (horz)
  420.    {
  421.        if( xPage > virtualX)
  422.          HScroll( -scrollPosX);
  423.       horz->SetSliderSize(xPage, virtualX);
  424.       horz->SetRange(1, virtualX - xPage, scrollPosX);
  425.    }
  426. }
  427.  
  428.  
  429. /*@ XScrollWindow::AddHorzScroller(void)
  430. @group scrollbars
  431. @remarks Display a horizontal scroller
  432. */
  433. void XScrollWindow::AddHorzScroller(void)
  434. {
  435.    if (horz != NULL)
  436.       return;
  437.    scrollPosX = 0;
  438.  
  439.    if (frameWin)
  440.       frameWin->adds += 1;
  441.  
  442.    XRect r;
  443.  
  444.    horz = new XScrollBar(this, r, 0, SC_HORZ | WIN_VISIBLE);
  445.    WinSetParent(horz->winhandle, frame, FALSE);
  446.    WinSetOwner(horz->winhandle, frame);
  447.  
  448.    if (vert && dummy == 0)
  449.       AddDummy();
  450.  
  451.    WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_HORZSCROLL, 0);
  452.    CalcHorzSize();
  453. }
  454.  
  455.  
  456. void XScrollWindow::CalcVertSize(void)
  457. {
  458.    if (vert)
  459.    {
  460.        if( yPage > virtualY )
  461.          VScroll( -scrollPosY);
  462.       vert->SetSliderSize(yPage, virtualY);
  463.       vert->SetRange(1, virtualY - yPage, scrollPosY);
  464.    }
  465. }
  466.  
  467. /*@ XScrollWindow::AddVertScroller(void)
  468. @group scrollbars
  469. @remarks Display a vertical scroller
  470. */
  471. void XScrollWindow::AddVertScroller(void)
  472. {
  473.    if (vert != NULL)
  474.       return;
  475.  
  476.    scrollPosY = 0;
  477.  
  478.    if (frameWin)
  479.       frameWin->adds += 1;
  480.  
  481.    XRect r;
  482.  
  483.    vert = new XScrollBar(this, r, 0, SC_VERT | WIN_VISIBLE | SC_AUTO);
  484.    WinSetParent(vert->winhandle, frame, FALSE);
  485.    WinSetOwner(vert->winhandle, frame);
  486.  
  487.    if (horz && dummy == 0)
  488.       AddDummy();
  489.  
  490.    WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_VERTSCROLL, 0);
  491.  
  492.    CalcVertSize();
  493. }
  494.  
  495.  
  496. /*@ XScrollWindow::DeleteVertScroller(void)
  497. @group scrollbars
  498. @remarks Removes the vertical scroller
  499. */
  500. void XScrollWindow::DeleteVertScroller(void)
  501. {
  502.    if (vert == NULL)
  503.       return;
  504.    if (frameWin)
  505.       frameWin->adds -= 1;
  506.  
  507.    WinDestroyWindow(vert->winhandle);
  508.    delete vert;
  509.  
  510.    vert = NULL;
  511.    scrollPosY = 0;
  512.    if (dummy)
  513.       DeleteDummy();
  514.    WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_VERTSCROLL, 0);
  515.    CalcHorzSize();
  516. }
  517.  
  518.  
  519. /*@ XScrollWindow::DeleteHorzScroller(void)
  520. @group scrollbars
  521. @remarks Removes the horizontal scroller
  522. */
  523. void XScrollWindow::DeleteHorzScroller(void)
  524. {
  525.    if (horz == NULL)
  526.       return;
  527.    if (frameWin)
  528.       frameWin->adds -= 1;
  529.    WinDestroyWindow(horz->winhandle);
  530.    delete horz;
  531.  
  532.    scrollPosX = 0;
  533.    horz = NULL;
  534.    if (dummy)
  535.       DeleteDummy();
  536.    WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_HORZSCROLL, 0);
  537.    CalcVertSize();
  538. }
  539.  
  540.  
  541. MRESULT EXPENTRY dummyHandler(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  542. {
  543.    if (msg == WM_PAINT)
  544.    {
  545.       RECTL rec;
  546.       HPS hps = WinBeginPaint(hwnd, NULLHANDLE, &rec);
  547.  
  548.       WinFillRect(hps, &rec, CLR_PALEGRAY);
  549.       WinEndPaint(hps);
  550.    }
  551.    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  552. }
  553.  
  554.  
  555. void XScrollWindow::AddDummy(void)
  556. {
  557.    if (WinRegisterClass(WinQueryAnchorBlock(frame), (PSZ) "OOL_DUMMY", (PFNWP) dummyHandler, 0, 0) == FALSE)
  558.       OOLThrow("error registering internal class", -10);
  559.  
  560.    if (frameWin)
  561.       frameWin->adds += 1;
  562.  
  563.    dummy = WinCreateWindow(frame, (PSZ) "OOL_DUMMY", (PSZ) "", WS_VISIBLE,
  564.                      0, 0, 0, 0,
  565.                      frame, HWND_TOP, 0, 0, 0);
  566. }
  567.  
  568.  
  569. void XScrollWindow::DeleteDummy(void)
  570. {
  571.    if (dummy == 0)
  572.       return;
  573.  
  574.    if (frameWin)
  575.       frameWin->adds -= 1;
  576.    WinDestroyWindow(dummy);
  577.    dummy = 0;
  578. }
  579.  
  580.  
  581. XScrollWindow :: ~XScrollWindow()
  582. {
  583.    DeleteHorzScroller();
  584.    DeleteVertScroller();
  585. }
  586.  
  587.