home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / source / xscrlwnd.cpp < prev    next >
C/C++ Source or Header  |  1997-04-05  |  14KB  |  482 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 *);
  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.                                                         <t '°' c=2>
  41.                                            °FRM_TITLEBAR     °the window gets a titlebar
  42.                                            °FRM_SYSMENU      °the window gets the system menu
  43.                                            °FRM_MINBUTTON    °the titlebar get a button to minimize the window
  44.                                            °FRM_MAXBUTTON    °the titlebar get a button to maximize the window
  45.                                            °FRM_CENTER       °the window is created in the midle of the workplace
  46.                                            °FRM_SIZEBORDER   °the windowsize can be changed by the user
  47.                                            °FRM_DIALOGBORDER °the window gets a thick border
  48.                                            °FRM_BORDER       °the window gets a thin border
  49.                                            °FRM_TASKLIST     °the window is displayed in the tasklist
  50.                                            °FRM_NOMOVEWITHOWNER  °the window dont∩t move when the parent is moved
  51.                                            °FRM_ICON         °the window get an icon wich is identified by theResourceID,
  52.                                                             if the icon is not found in the resource-library, an error ocurses
  53.                                            °FRM_ACCELTABLE    °
  54.                                            °FRM_SYSMODAL     °the window is displayed system-modal
  55.                                            °FRM_SCREENALIGN    °
  56.                                            °FRM_MOUSEALIGN    °
  57.                                            °FRM_HIDEBUTTON    °
  58.                                            °FRM_HIDEMAX    °
  59.                                            °FRM_AUTOICON    °
  60.                                                         </t>
  61.                                         there are three static member-variables for default styles
  62.                                                         <t '°' c=2>
  63.                                            °long defaultStyle          °default setting for a framewindow
  64.                                            °long defaultClientStyle    °default setting for windows wich are displayed as a clientwindow of a framewindow
  65.                                            °long defaultDialogStyle    °default setting for windows wich are displayed as a dialog
  66.                                                         </t>
  67.                                         Default is defaultStyle.
  68.             °XRect * theRectangle        °On default a window is created with length and hight of zero. Windows
  69.                                         which are created with an resource template get the size of the template.
  70.                                         Default is NULL.<BR>
  71.                                         If theRectangle is specified, the window gets the size of it.
  72.             °XFrameWindow * parent       °If parent is specified the window is a client of the parent. The
  73.                                         behavior depends on the styles you have set.<BR>
  74.                                         Default is NULL.
  75.             °BOOL buildFromResource      °If this variable is set OOL try to build the window with a resource
  76.                                         template which is identified by theResourceID. If the template is
  77.                                         not found, an error ocurses.<BR>
  78.                                         Default is FALSE.
  79.             °BOOL animate                °Enable/disable animation on window creation. Default is FALSE
  80.             </t>
  81. */
  82. 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)
  83. {
  84.     WinQueryWindowPos(winhandle, &clientSwp);
  85.     scrollhandler *handler = new scrollhandler(this);
  86.  
  87.     scrollPosX = scrollPosY = 0;
  88.     xStep = yStep = 20;
  89.     xPage = yPage = 60;
  90.  
  91.     if (dlgHandle)
  92.     {
  93.         SWP swp;
  94.  
  95.         WinQueryWindowPos(dlgHandle, &swp);
  96.         SetVirtualX(swp.cx);
  97.         SetVirtualY(swp.cy);
  98.     }
  99.     else
  100.     {
  101.         SetVirtualX(0);
  102.         SetVirtualY(0);
  103.     }
  104. }
  105.  
  106.  
  107. /*@ XScrollWindow::SetVirtualX(const LONG x)
  108. @group scroll-functions
  109. @remarks Set the virtual x-size of the window
  110. @parameters LONG x                       virtual x-size
  111. */
  112. void XScrollWindow::SetVirtualX(const LONG x)
  113. {
  114.     virtualX = x;
  115.     CalcHorzSize();
  116. }
  117.  
  118.  
  119. /*@ XScrollWindow::SetVirtualY(const LONG y)
  120. @group scroll-functions
  121. @remarks Set the virtual y-size of the window
  122. @parameters LONG y                       virtual y-size
  123. */
  124. void XScrollWindow::SetVirtualY(const LONG y)
  125. {
  126.     virtualY = y;
  127.     CalcVertSize();
  128. }
  129.  
  130.  
  131. /*@ XScrollWindow::VScroll(LONG s)
  132. @group scroll-functions
  133. @remarks This function is called when the window-contents must be scrolled vertcal.
  134. On default scrolling is done automaticaly
  135. @parameters LONG s    how much pixels to scroll
  136. */
  137. void XScrollWindow::VScroll(LONG s)
  138. {
  139.     SWP swp;
  140.     HWND cl = winhandle, hwnd;
  141. /*****
  142.     if (scrollPosY + s > virtualY)
  143.         s = virtualY - scrollPosY;
  144.     if (scrollPosY + s < 0)
  145.         s = -scrollPosY;
  146. ***/
  147.     if (dlgHandle)
  148.         cl = dlgHandle;
  149.  
  150.     HENUM enumWindow = WinBeginEnumWindows(cl);
  151.  
  152.     WinEnableWindowUpdate(cl, FALSE);
  153.     char buffer[5];
  154.     SHORT cor;
  155.     ULONG style;
  156.  
  157.     while ((hwnd = WinGetNextWindow(enumWindow)) != 0)
  158.     {
  159.         WinQueryClassName(hwnd, 5, (PSZ) buffer);
  160.         style = WinQueryWindowULong(hwnd, QWL_STYLE);
  161.         if (style & ES_MARGIN && buffer[1] == '6')
  162.             cor = 3;
  163.         else
  164.             cor = 0;
  165.         WinQueryWindowPos(hwnd, &swp);
  166.         swp.y -= s; //-
  167.         WinSetWindowPos(hwnd, 0, swp.x + cor, swp.y + cor, 0, 0, SWP_MOVE);
  168.     };
  169.     WinEndEnumWindows(enumWindow);
  170.     WinEnableWindowUpdate(cl, TRUE);
  171.  
  172.     scrollPosY += s; //+
  173. }
  174.  
  175.  
  176. /*@ XScrollWindow::HScroll(LONG s)
  177. @group scroll-functions
  178. @remarks This function is called when the window-contents must be scrolled horizontal.
  179. On default scrolling is done automaticaly
  180. @parameters LONG s   how much pixels to scroll
  181. */
  182. void XScrollWindow::HScroll(LONG s)
  183. {
  184.     SWP swp;
  185.     HWND cl = winhandle, hwnd;
  186.  
  187.     if (dlgHandle)
  188.         cl = dlgHandle;
  189.  
  190.     HENUM enumWindow = WinBeginEnumWindows(cl);
  191.  
  192.     if (scrollPosX + s > virtualX)
  193.         s = virtualX - scrollPosX;
  194.     if (scrollPosX + s < 0)
  195.         s = -scrollPosX;
  196.  
  197.     WinEnableWindowUpdate(cl, FALSE);
  198.  
  199.     char buffer[5];
  200.     SHORT cor;
  201.     ULONG style;
  202.  
  203.     while ((hwnd = WinGetNextWindow(enumWindow)) != 0)
  204.     {
  205.         WinQueryClassName(hwnd, 5, (PSZ) buffer);
  206.         style = WinQueryWindowULong(hwnd, QWL_STYLE);
  207.         if (style & ES_MARGIN && buffer[1] == '6')
  208.             cor = 3;
  209.         else
  210.             cor = 0;
  211.  
  212.         WinQueryWindowPos(hwnd, &swp);
  213.         swp.x -= s;
  214.         WinSetWindowPos(hwnd, 0, swp.x + cor, swp.y + cor, 0, 0, SWP_MOVE);
  215.     };
  216.     WinEndEnumWindows(enumWindow);
  217.  
  218.     WinEnableWindowUpdate(cl, TRUE);
  219.  
  220.     scrollPosX += s;
  221. }
  222.  
  223.  
  224. void XScrollWindow::DoSize(XSize*s)
  225. {
  226.     clientSwp.cx = s->GetWidth();
  227.     clientSwp.cy = s->GetHeight();
  228.     CalcHorzSize();
  229.     CalcVertSize();
  230. }
  231.  
  232.  
  233. BOOL scrollhandler::HandleEvent(ULONG msg, MPARAM mp1, MPARAM mp2)
  234. {
  235.     SHORT s = 0;
  236.  
  237.     switch (msg)
  238.     {
  239.     case OOL_ADDCLIENT:
  240.         SWP swp;
  241.         WinQueryWindowPos((HWND) mp1, &swp);
  242.         if (swp.cx + swp.x > win->virtualX)
  243.         {
  244.             win->virtualX = swp.x + swp.cx + 5;
  245.             win->CalcHorzSize();
  246.         }
  247.         if (swp.cy + swp.y > win->virtualY)
  248.         {
  249.             win->virtualY = swp.y + swp.cy + 5;
  250.             win->CalcVertSize();
  251.         }
  252.         return TRUE;
  253.     case WM_HSCROLL:
  254.         {
  255.             switch (SHORT2FROMMP(mp2))
  256.             {
  257.             case SB_LINERIGHT:
  258.                 s = win->xStep;
  259.                 break;
  260.             case SB_LINELEFT:
  261.                 s = -win->xStep;
  262.                 break;
  263.             case SB_PAGELEFT:
  264.                 s = -win->xPage;
  265.                 break;
  266.             case SB_PAGERIGHT:
  267.                 s = win->xPage;
  268.                 break;
  269.             case SB_ENDSCROLL:
  270.                 return 0;
  271.             default:
  272.                 s = SHORT1FROMMP(mp2) - win->scrollPosX;
  273.             }
  274.             win->HScroll(s);
  275.             if (win->horz)
  276.                 win->horz->SetPos(win->scrollPosX);
  277.         }
  278.         return TRUE;
  279.     case WM_VSCROLL:
  280.         {
  281.             switch (SHORT2FROMMP(mp2))
  282.             {
  283.             case SB_LINEUP:
  284.                 s = -win->yStep;
  285.                 break;
  286.             case SB_LINEDOWN:
  287.                 s = win->yStep;
  288.                 break;
  289.             case SB_PAGEDOWN:
  290.                 s = win->yPage;
  291.                 break;
  292.             case SB_PAGEUP:
  293.                 s = -win->yPage;
  294.                 break;
  295.             case SB_ENDSCROLL:
  296.                 return 0;
  297.             default:
  298.                 s = SHORT1FROMMP(mp2) - win->scrollPosY;
  299.             }
  300.             win->VScroll(s);
  301.  
  302.             if (win->vert)
  303.                 win->vert->SetPos( win->scrollPosY);//win->virtualY-win->clientSwp.cy-
  304.  
  305.         }
  306.         return TRUE;
  307.     }
  308.     return FALSE;
  309. }
  310.  
  311.  
  312. void XScrollWindow::CalcHorzSize(void)
  313. {
  314.     if (horz)
  315.     {
  316.         if( clientSwp.cx > virtualX)
  317.             HScroll( -scrollPosX);
  318.         horz->SetSliderSize(clientSwp.cx, virtualX);
  319.         horz->SetRange(1, virtualX - clientSwp.cx, scrollPosX);
  320.     }
  321. }
  322.  
  323.  
  324. /*@ XScrollWindow::AddHorzScroller(void)
  325. @group scrollbars
  326. @remarks Display a horizontal scroller
  327. */
  328. void XScrollWindow::AddHorzScroller(void)
  329. {
  330.     if (horz != NULL)
  331.         return;
  332.     scrollPosX = 0;
  333.  
  334.     if (frameWin)
  335.         frameWin->adds += 1;
  336.  
  337.     XRect r;
  338.  
  339.     horz = new XScrollBar(this, &r, 0, SC_HORZ | WIN_VISIBLE);
  340.     WinSetParent(horz->winhandle, frame, FALSE);
  341.     WinSetOwner(horz->winhandle, frame);
  342.  
  343.     if (vert && dummy == 0)
  344.         AddDummy();
  345.  
  346.     WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_HORZSCROLL, 0);
  347.     CalcHorzSize();
  348. }
  349.  
  350.  
  351. void XScrollWindow::CalcVertSize(void)
  352. {
  353.     if (vert)
  354.     {
  355.         if( clientSwp.cy > virtualY )
  356.             VScroll( -scrollPosY);
  357.         vert->SetSliderSize(clientSwp.cy, virtualY);
  358.         vert->SetRange(1, virtualY - clientSwp.cy, scrollPosY);
  359.     }
  360. }
  361.  
  362. /*@ XScrollWindow::AddVertScroller(void)
  363. @group scrollbars
  364. @remarks Display a vertical scroller
  365. */
  366. void XScrollWindow::AddVertScroller(void)
  367. {
  368.     if (vert != NULL)
  369.         return;
  370.  
  371.     scrollPosY = 0;
  372.  
  373.     if (frameWin)
  374.         frameWin->adds += 1;
  375.  
  376.     XRect r;
  377.  
  378.     vert = new XScrollBar(this, &r, 0, SC_VERT | WIN_VISIBLE | SC_AUTO);
  379.     WinSetParent(vert->winhandle, frame, FALSE);
  380.     WinSetOwner(vert->winhandle, frame);
  381.  
  382.     if (horz && dummy == 0)
  383.         AddDummy();
  384.  
  385.     WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_VERTSCROLL, 0);
  386.  
  387.     CalcVertSize();
  388. }
  389.  
  390.  
  391. /*@ XScrollWindow::DeleteVertScroller(void)
  392. @group scrollbars
  393. @remarks Removes the vertical scroller
  394. */
  395. void XScrollWindow::DeleteVertScroller(void)
  396. {
  397.     if (vert == NULL)
  398.         return;
  399.     if (frameWin)
  400.         frameWin->adds -= 1;
  401.  
  402.     WinDestroyWindow(vert->winhandle);
  403.     delete vert;
  404.  
  405.     vert = NULL;
  406.     scrollPosY = 0;
  407.     if (dummy)
  408.         DeleteDummy();
  409.     WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_VERTSCROLL, 0);
  410.     CalcHorzSize();
  411. }
  412.  
  413.  
  414. /*@ XScrollWindow::DeleteHorzScroller(void)
  415. @group scrollbars
  416. @remarks Removes the horizontal scroller
  417. */
  418. void XScrollWindow::DeleteHorzScroller(void)
  419. {
  420.     if (horz == NULL)
  421.         return;
  422.     if (frameWin)
  423.         frameWin->adds -= 1;
  424.     WinDestroyWindow(horz->winhandle);
  425.     delete horz;
  426.  
  427.     scrollPosX = 0;
  428.     horz = NULL;
  429.     if (dummy)
  430.         DeleteDummy();
  431.     WinSendMsg(frame, WM_UPDATEFRAME, (MPARAM) FCF_HORZSCROLL, 0);
  432.     CalcVertSize();
  433. }
  434.  
  435.  
  436. MRESULT EXPENTRY dummyHandler(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  437. {
  438.     if (msg == WM_PAINT)
  439.     {
  440.         RECTL rec;
  441.         HPS hps = WinBeginPaint(hwnd, NULLHANDLE, &rec);
  442.  
  443.         WinFillRect(hps, &rec, CLR_PALEGRAY);
  444.         WinEndPaint(hps);
  445.     }
  446.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  447. }
  448.  
  449.  
  450. void XScrollWindow::AddDummy(void)
  451. {
  452.     if (WinRegisterClass(WinQueryAnchorBlock(frame), (PSZ) "OOL_DUMMY", (PFNWP) dummyHandler, 0, 0) == FALSE)
  453.         OOLThrow("error registering internal class", -10);
  454.  
  455.     if (frameWin)
  456.         frameWin->adds += 1;
  457.  
  458.     dummy = WinCreateWindow(frame, (PSZ) "OOL_DUMMY", (PSZ) "", WS_VISIBLE,
  459.                             0, 0, 0, 0,
  460.                             frame, HWND_TOP, 0, 0, 0);
  461. }
  462.  
  463.  
  464. void XScrollWindow::DeleteDummy(void)
  465. {
  466.     if (dummy == 0)
  467.         return;
  468.  
  469.     if (frameWin)
  470.         frameWin->adds -= 1;
  471.     WinDestroyWindow(dummy);
  472.     dummy = 0;
  473. }
  474.  
  475.  
  476. XScrollWindow :: ~XScrollWindow()
  477. {
  478.     DeleteHorzScroller();
  479.     DeleteVertScroller();
  480. }
  481.  
  482.