home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / source / xwindow.cpp < prev    next >
C/C++ Source or Header  |  1997-04-04  |  14KB  |  522 lines

  1. #include "XWindow.h"
  2.  
  3. #include "XString.h"
  4. #include "XRect.h"
  5. #include "XColor.h"
  6. #include "XHandler.h"
  7. #include "XPoint.h"
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12.  
  13. /*@ 
  14. @class XWindow
  15. @type overview
  16. @symbol _
  17. @remarks XWindow is the base class for several window classes.
  18. */
  19.  
  20.  
  21. void XWindow::SetParent(const XWindow * w, const BOOL redraw) const
  22. {
  23.     WinSetParent(winhandle, w->GetHandle(), redraw);
  24. }
  25.  
  26.  
  27. void XWindow::SetOwner(const XWindow * w) const
  28. {
  29.     WinSetOwner(winhandle, w->GetHandle());
  30. }
  31.  
  32.  
  33. /*@ XWindow::SetStyle(const ULONG style)
  34. @group style
  35. @remarks Set the style of a window
  36. @parameters ULONG style    the new syle (depending on the type of window)
  37. */
  38. void XWindow::SetStyle(const ULONG style)
  39. {
  40.     WinSetWindowULong(winhandle, QWL_STYLE, style);
  41. }
  42.  
  43.  
  44. /*@ XWindow::GetStyle(void)
  45. @group style
  46. @remarks Query the current style of the window.
  47. @returns ULONG style
  48. */
  49. ULONG XWindow::GetStyle(void) const
  50. {
  51.     return WinQueryWindowULong(winhandle, QWL_STYLE);
  52. }
  53.  
  54.  
  55. /*@ XWindow::DoControl(XControlEvent * e)
  56. @group user input
  57. @remarks If the user has done some interaction with a window this function is called.
  58. To get information about the event, override this function, the parameter holds
  59. information of your interest.
  60. @parameters   XControlEvent * pointerOfEvent   a pointer to an instance of XControlEvent
  61. */
  62. void XWindow::DoControl(XControlEvent * e)
  63. {
  64.     XWindow *w = QueryWindow(QW_PARENT);
  65.  
  66.     if (w)
  67.         w->DoControl(e);
  68. }
  69.  
  70.  
  71. /*@ XWindow::SetPointer(const OOL_POINTERHANDLE thePointer)
  72. @group pointer
  73. @remarks Set the actual pointer. You can receive a pointer-handle by calling GetSystemPointer()
  74. or load a pointer from a resource with XResourceLibrary::LoadIcon.
  75. @parameters OOL_POINTERHANDLE handle   the pointer
  76. */
  77. BOOL XWindow::SetPointer(const OOL_POINTERHANDLE thePointer)
  78. {
  79.     return WinSetPointer(HWND_DESKTOP, thePointer);
  80. }
  81.  
  82.  
  83. /*@ XWindow::GetPointerHandle(void)
  84. @group pointer
  85. @remarks Returns the handle of the actual pointer.
  86. @returns OOL_POINTERHANDLE handle
  87. */
  88. OOL_POINTERHANDLE XWindow::GetPointerHandle(void)
  89. {
  90.     return WinQueryPointer(HWND_DESKTOP);
  91. }
  92.  
  93.  
  94. /*@ XWindow::GetSystemPointerHandle(LONG theID, BOOL copy)
  95. @group pointer
  96. @remarks Returns the handle of the pointer identified by theID.
  97. @parameters   <t '°' c=2>
  98.                     °LONG theID             °ID of the needed pointer. Valid values are:
  99.                                                     <t '°' c=1>
  100.                                                °PTR_ARROW
  101.                                                °PTR_TEXT
  102.                                                °PTR_WAIT
  103.                                                °PTR_SIZE
  104.                                                °PTR_SIZENWSE
  105.                                                °PTR_SIZENESW
  106.                                                °PTR_SIZEWE
  107.                                                °PTR_SIZENS
  108.                                                °PTR_MOVE
  109.                                                °PTR_ILLEGAL
  110.                                                     </t>
  111.                   °BOOL copy              °If you need a copy of the pointer (to modify
  112.                                          it) set copy TRUE, otherwise FALSE.<BR>
  113.                                       Default is FALSE.
  114.                     </t>
  115. @returns OOL_POINTERHANDLE handle
  116. */
  117. OOL_POINTERHANDLE XWindow::GetSystemPointerHandle(LONG theID, BOOL copy)
  118. {
  119.     return WinQuerySysPointer(HWND_DESKTOP, theID, copy);
  120. }
  121.  
  122.  
  123. /*@ XWindow::GetPointerPos(XPoint * p)
  124. @group pointer
  125. @remarks Returns the position of the pointer
  126. @parameters XPoint * point    buffer to hold the datas
  127. */
  128. void XWindow::GetPointerPos(XPoint * p)
  129. {
  130.     POINTL pointl;
  131.  
  132.     WinQueryPointerPos(HWND_DESKTOP, &pointl);
  133.     p->Set(pointl.x, pointl.y);
  134. }
  135.  
  136.  
  137. /*@ XWindow::Activate(void)
  138. @group misc
  139. @remarks Activate the window
  140. */
  141. void XWindow::Activate(void)
  142. {
  143.     WinSetActiveWindow(HWND_DESKTOP, winhandle);
  144. }
  145.  
  146.  
  147. /*@ XWindow::IsVisible(void)
  148. @group misc
  149. @remarks Query if the window is visible or not
  150. @returns BOOL result
  151. */
  152. BOOL XWindow::IsVisible(void) const
  153. {
  154.     return WinIsWindowVisible(winhandle);
  155. }
  156.  
  157.  
  158. /*@ XWindow::GetSize(XRect * rect) 
  159. @group size/position/order
  160. @remarks Query the size and position of the window
  161. @parameters XRect * rect   buffer to hold the datas
  162. */
  163. void XWindow::GetSize(XRect * rect) const
  164. {
  165.     SWP swp;
  166.  
  167.     WinQueryWindowPos(winhandle, &swp);
  168.     rect->x = swp.x;
  169.     rect->y = swp.y;
  170.     rect->cx = swp.cx;
  171.     rect->cy = swp.cy;
  172. }
  173.  
  174.  
  175. /*@ XWindow::GetWindowID(void)
  176. @group misc
  177. @remarks Query the ID of the window
  178. @returns SHORT theID
  179. */
  180. SHORT XWindow::GetWindowID(void) const
  181. {
  182.     return WinQueryWindowUShort(winhandle, QWS_ID);
  183. }
  184.  
  185.  
  186. /*@ XWindow::QueryWindow(const ULONG id)
  187. @group misc
  188. @remarks Find a window with the given relationship
  189. @parameters <t '°' c=2>
  190.                 °ULONG relation    °relationship:
  191.                                             <t '°' c=1>
  192.                                  °WIN_PARENT
  193.                                  °WIN_NEXT
  194.                                  °WIN_PREV
  195.                                  °WIN_TOP
  196.                                  °WIN_BOTTOM
  197.                                  °WIN_OWNER
  198.                                  °WIN_PARENT
  199.                                  °WIN_NEXTTOP
  200.                                  °WIN_PREVTOP
  201.                                             </t>
  202.                 </t>
  203. @returns XWindow * pointer
  204. */
  205. XWindow * XWindow::QueryWindow(const ULONG id) const
  206. {
  207.     HWND hwnd = WinQueryWindow(winhandle, id);
  208.     return (XWindow *) WinQueryWindowPtr(hwnd, 0);
  209. }
  210.  
  211.  
  212. /*@ XWindow::GetWindow(const ULONG id)
  213. @group misc
  214. @remarks Find a child-window with the given ID
  215. @parameters ULONG theID    ID of the window to find
  216. @returns XWindow * pointer
  217. */
  218. XWindow * XWindow::GetWindow(const ULONG id)
  219. {
  220.     HWND hwnd = WinWindowFromID(GetHandle(), id);
  221.  
  222.     return (XWindow *) WinQueryWindowPtr(hwnd, 0);
  223. }
  224.  
  225.  
  226. /*@ XWindow::SetTop(void)
  227. @group size/position/order
  228. @remarks The window is set to the top in z-order
  229. */
  230. void XWindow::SetTop(void) const
  231. {
  232.     WinSetWindowPos(winhandle, HWND_TOP, 0, 0, 0, 0, SWP_ZORDER);
  233. }
  234.  
  235.  
  236. /*@ XWindow::Enable(const BOOL enable)
  237. @group misc
  238. @remarks Enable/disable the window
  239. @parameters BOOL enabe     TRUE=enable, FALSE=disable
  240. */
  241. void XWindow::Enable(const BOOL enable)
  242. {
  243.     WinEnableWindow(winhandle, enable);
  244. }
  245.  
  246.  
  247. /*@ XWindow::EnableWindowUpdate(const BOOL enable)
  248. @group misc
  249. @remarks Enable/disable window-update
  250. @parameters BOOL enabe     TRUE=enable, FALSE=disable window-update
  251. */
  252. void XWindow::EnableWindowUpdate(const BOOL enable) const
  253. {
  254.     WinEnableWindowUpdate(winhandle, enable);
  255. }
  256.  
  257.  
  258. /*@ XWindow::GetText(XString * buffer)
  259. @group text functions
  260. @remarks Query the text of the window
  261. @parameters XString * buffer     buffer to hold the text
  262. */
  263. void XWindow::GetText(XString * buffer)
  264. {
  265.     SHORT r = WinQueryWindowTextLength(winhandle);
  266.  
  267.     r = WinQueryWindowText(winhandle, r + 1, (PCH) buffer->GetBuffer(r));
  268.     buffer->ReleaseBuffer(r);
  269. }
  270.  
  271.  
  272. /*@ XWindow::GetUpdateRect(XRect * r)
  273. @group size
  274. @remarks Get the rectangle which should be redrawn
  275. @parameters XRect * rect        buffer
  276. */
  277. void XWindow::GetUpdateRect(XRect * r)
  278. {
  279.     RECTL rect;
  280.  
  281.     WinQueryUpdateRect(winhandle, &rect);
  282.     r->SetX(rect.xLeft);
  283.     r->SetY(rect.yBottom);
  284.     r->SetWidth(rect.xRight - rect.xLeft);
  285.     r->SetHeight(rect.yTop - rect.yBottom);
  286. }
  287.  
  288.  
  289. /*@ XWindow::SetSize(const XRect * rect)
  290. @group size
  291. @remarks Set the size and position of the window
  292. @parameters XRect * rect   new size and position
  293. */
  294. void XWindow::SetSize(const XRect * rect) const
  295. {
  296.     WinSetWindowPos(winhandle, 0, rect->x, rect->y, rect->cx, rect->cy, SWP_SHOW | SWP_MOVE | SWP_SIZE);
  297. }
  298.  
  299.  
  300. /*@ XWindow::SetText(const char *text)
  301. @group text functions
  302. @remarks Set the text of the window
  303. @parameters char * text    text to display
  304. */
  305. void XWindow::SetText(const char *text)
  306. {
  307.     WinSetWindowText(winhandle, (PSZ) text);
  308. }
  309.  
  310.  
  311. /*@ XWindow::Show(const BOOL show)
  312. @group size/position/order
  313. @remarks Show/hide the window
  314. @parameters BOOL show            TRUE=show, FALSE=hide
  315. */
  316. void XWindow::Show(const BOOL show)
  317. {
  318.     if (show)
  319.         WinSetWindowPos(winhandle, HWND_TOP, 0, 0, 0, 0, SWP_SHOW | SWP_ZORDER | SWP_ACTIVATE | SWP_RESTORE);
  320.     else
  321.         WinSetWindowPos(winhandle, 0, 0, 0, 0, 0, SWP_HIDE);
  322. }
  323.  
  324.  
  325. /*@ XWindow::SetFocus(void)
  326. @group misc
  327. @remarks Set the focus to this window
  328. @returns BOOL result
  329. */
  330. BOOL XWindow::SetFocus(void) const
  331. {
  332.     return WinSetFocus(HWND_DESKTOP, winhandle);
  333. }
  334.  
  335.  
  336. /*@ XWindow::GetTextLength(void)
  337. @group text functions
  338. @remarks Query the length of the windows text
  339. @returns LONG length
  340. */
  341. LONG XWindow::GetTextLength(void)
  342. {
  343.     return WinQueryWindowTextLength(winhandle);
  344. }
  345.  
  346.  
  347. /*@ XWindow::Invalidate(BOOL invalidateChilds)
  348. @group misc
  349. @remarks Invalidates the window content, it will be redrawn
  350. @parameters BOOL childs    TRUE=childs of the window are invalidated too
  351.                            FALSE=childs are not invalidated
  352. */
  353. void XWindow::Invalidate(BOOL invalidateChilds)
  354. {
  355.     WinInvalidateRegion(winhandle, NULLHANDLE, invalidateChilds);
  356. }
  357.  
  358.  
  359. /*@ XWindow::SetBottom(void)
  360. @group size/position/order
  361. @remarks The window is set to the bottom in z-order
  362. */
  363. void XWindow::SetBottom(void) const
  364. {
  365.     WinSetWindowPos(winhandle, HWND_BOTTOM, 0, 0, 0, 0, SWP_ZORDER);
  366. }
  367.  
  368.  
  369. /*@ XWindow::GetForegroundColor(XColor * rgb)
  370. @group colors
  371. @remarks Query the foreground-color of the window
  372. @parameters XColor * color buffer to hold data
  373. */
  374. void XWindow::GetForegroundColor(XColor * rgb)
  375. {
  376.     LONG c;
  377.  
  378.     WinQueryPresParam(winhandle, PP_FOREGROUNDCOLOR, 0, NULL, 4, &c, QPF_PURERGBCOLOR);
  379.     rgb->SetColor(c);
  380. }
  381.  
  382.  
  383. /*@ XWindow::GetBackgroundColor(XColor * rgb)
  384. @group colors
  385. @remarks Query the background-color of the window
  386. @parameters XColor * color buffer to hold data
  387. */
  388. void XWindow::GetBackgroundColor(XColor * rgb)
  389. {
  390.     LONG c;
  391.  
  392.     WinQueryPresParam(winhandle, PP_BACKGROUNDCOLOR, 0, NULL, 4, &c, QPF_PURERGBCOLOR);
  393.     rgb->SetColor(c);
  394. }
  395.  
  396.  
  397. /*@ XWindow::SetForegroundColor(const XColor * rgb)
  398. @group colors
  399. @remarks Set the foregrund-color of the window
  400. @parameters XColor * color   the new color
  401. @returns BOOL success
  402. */
  403. BOOL XWindow::SetForegroundColor(const XColor * rgb) const
  404. {
  405.     LONG c = rgb->GetColor();
  406.  
  407.     return WinSetPresParam(winhandle, PP_FOREGROUNDCOLOR, 4, (PVOID) (ULONG) & c);
  408. }
  409.  
  410.  
  411. /*@ XWindow::SetBackgroundColor(const XColor * rgb)
  412. @group colors
  413. @remarks Set the foregrund-color of the window
  414. @parameters XColor * color   the new color
  415. @returns BOOL success
  416. */
  417. BOOL XWindow::SetBackgroundColor(const XColor * rgb) const
  418. {
  419.     LONG c = rgb->GetColor();
  420.  
  421.     return WinSetPresParam(winhandle, PP_BACKGROUNDCOLOR, 4, (PVOID) (ULONG) & c);
  422. }
  423.  
  424.  
  425. /*@ XWindow::SetFont(const char *fontName, const unsigned short fontSize)
  426. @group fonts
  427. @remarks Set the font of the window
  428. @parameters char * fontName   the name of the font<BR>
  429.             USHORT fontSize   the size of the font
  430. @returns BOOL success
  431. */
  432. BOOL XWindow::SetFont(const char *fontName, const unsigned short fontSize) const
  433. {
  434.     short l;
  435.     char st[FACESIZE + 3];
  436.  
  437.     sprintf(st, "%i.%s", fontSize, fontName);
  438.     l = strlen(st);
  439.  
  440.     return WinSetPresParam(winhandle, PP_FONTNAMESIZE, l, (PVOID) st);
  441. }
  442.  
  443.  
  444. /*@ XWindow::SetFont(const char *fontNameSize)
  445. @group fonts
  446. @remarks Set the font of the window
  447. @parameters char * fontName   the name of the font in format "10.Helvetica"
  448. @returns BOOL success
  449. */
  450. BOOL XWindow::SetFont(const char *fontNameSize) const
  451. {
  452.     return WinSetPresParam(winhandle, PP_FONTNAMESIZE, strlen(fontNameSize), (PVOID) fontNameSize);
  453. }
  454.  
  455.  
  456. /*@ XWindow :: ~XWindow()
  457. @group constructors/destructors
  458. @remarks Destructors of windows are called automaticaly when they are closed. All destructors
  459. of child-windows are called too. All handlers registered at the window aredestructed.
  460. You can destroy a window by calling the destructor.
  461. */
  462. XWindow :: ~XWindow()
  463. {
  464.     SHORT i;
  465.  
  466.     WinSetWindowPtr(winhandle, 0, NULL);
  467.     WinDestroyWindow(winhandle);
  468.     for (i = 0; i < handlers; i++)
  469.     {
  470.         regHandlers[i]->handleFor = NULL;
  471.         delete regHandlers[i];
  472.     }
  473.     handlers = 0;
  474.     free(regHandlers);
  475. }
  476.  
  477.  
  478. BOOL XWindow::IsEnabled(void) const
  479. {
  480.     return WinIsWindowEnabled(winhandle);
  481. }
  482.  
  483.  
  484. /*@ XWindow::GetFontName(XString * font)
  485. @group fonts
  486. @remarks Query the font of the window
  487. @parameters XString * buffer   the name of the font in format "10.Helvetica"
  488. */
  489. void XWindow::GetFontName(XString * font)
  490. {
  491.     WinQueryPresParam(winhandle, PP_FONTNAMESIZE, 0, NULL, 100, font->GetBuffer(100), QPF_NOINHERIT);
  492.     font->ReleaseBuffer();
  493. }
  494.  
  495.  
  496. ////////////////////////docs only
  497. /*@ XWindow::DoCommand()
  498. @group user input
  499. @remarks If the user selected a XPopupMenu or if the
  500. user pressed a button which is attached to a window, this function
  501. is called. To get the ID of the command the user requested, override this function.
  502. Return TRUE if you have handled the command, otherwise return FALSE. if you return FALSE
  503. the command is posted to the owner of this window.
  504. @parameters   LONG theCommandID    the ID of the menuitem/toolbar-button
  505. @returns      BOOL handled
  506. */
  507.  
  508.  
  509. /*@ XWindow::DoSize()
  510. @group size/position/order
  511. @remarks DoSize informs the application if the window is resized. Override
  512. this function if you need this information.
  513. @parameters XSize * size
  514. */
  515.  
  516.  
  517. /*@ XWindow::DoMove()
  518. @group size/position/order
  519. @remarks DoMove informs the application if the window is moved. Override
  520. this function if you need this information.
  521. */
  522.