home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / advframe / winview / hwindow.cpp next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  6.7 KB  |  257 lines

  1. //************************************************************
  2. // Advanced Frame - Window Viewer Example
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //************************************************************
  8. #include <ibase.hpp>
  9. #ifdef IC_PM
  10.    #define INCL_WINATOM
  11.    #define INCL_WINWINDOWMGR
  12.    #include <os2.h>
  13. #else
  14.    #include <windows.h>
  15. #endif
  16.  
  17. #include <iostream.h>
  18. #include <fstream.h>
  19. #include <ctype.h>
  20. #include <irect.hpp>
  21. #include <iwindow.hpp>
  22. #include "hwindow.hpp"
  23.  
  24. HWindow::HWindow ( IHandle::Value handle )
  25.   : IWindowHandle( handle )
  26.   {
  27.   }
  28.  
  29. IString  HWindow::asHexString ( ) const
  30. {
  31.    IString result(this->asUnsigned());
  32.    result.d2x().rightJustify(8,'0');
  33.    return result;
  34. }
  35.  
  36. IString  HWindow::id         ( ) const
  37. {
  38.    unsigned short usId(0);
  39.    if (this->isValid())
  40.       {
  41. #ifdef IC_PM
  42.       usId = WinQueryWindowUShort( *this, QWS_ID );
  43. #else
  44.       if (GetWindowLong( *this, GWL_STYLE ) & WS_CHILD)
  45.          usId = (unsigned short)GetWindowLong( *this, GWL_ID  ) ;
  46. #endif
  47.       }
  48.    IString result(usId);
  49.    result.d2x();
  50.    return IString(usId) + IString(" 0x") + result;
  51. }
  52.  
  53. IString  HWindow::text       ( ) const
  54. {
  55.    IString result;
  56.    if (this->isValid())
  57.       {
  58. #ifdef IC_PM
  59.       unsigned long bufLen = WinQueryWindowTextLength( *this ) + 1;
  60.       char*         buf    = new char[bufLen];
  61.       WinQueryWindowText( *this, bufLen, buf );
  62. #else
  63.       unsigned long bufLen = GetWindowTextLength( *this ) + 1;
  64.       char*         buf    = new char[bufLen];
  65.       GetWindowText( *this, buf, (int)bufLen );
  66. #endif
  67.       result = IString(buf);
  68.       delete [] buf;
  69.       }
  70.    return result;
  71. }
  72.  
  73. IString  HWindow::rectangle  ( ) const
  74. {
  75.    IRectangle rect;
  76.    if (this->isValid())
  77.       {
  78. #ifdef IC_PM
  79.       SWP  swp;
  80.       WinQueryWindowPos( *this, &swp );
  81.       rect = IRectangle(swp.x, swp.y,
  82.                         swp.x + swp.cx, swp.y + swp.cy);
  83. #else
  84.       RECT   wrect;
  85.       GetWindowRect( *this, &wrect );
  86.       IWindowHandle  parentHwnd = GetParent( *this );
  87.  
  88.       // If the window is a popup, use the desktop as the
  89.       // parent because:
  90.       //    1) By default this window has no parent
  91.       //    2) A query for the parent always returns
  92.       //       the owner window.
  93.       if ( GetWindowLong( *this, GWL_STYLE) & WS_POPUP )
  94.         parentHwnd = IWindow::desktopWindow()->handle();
  95.       MapWindowPoints( HWND_DESKTOP, parentHwnd,
  96.                        (POINT*)&wrect, 2);
  97.       rect = IRectangle(wrect);
  98. #endif
  99.       }
  100.    return IString("(") +
  101.           IString(rect.minX()) + IString(",") +
  102.           IString(rect.minY()) + IString(",") +
  103.           IString(rect.maxX()) + IString(",") +
  104.           IString(rect.maxY()) + IString(") ") +
  105.           IString(rect.width()) + IString("x") +
  106.           IString(rect.height());
  107. }
  108.  
  109. IString  HWindow::windowClass( ) const
  110. {
  111.    IString result;
  112.    if (this->isValid())
  113.       {
  114.       // For simplicity, we only get the first 16 characters of
  115.       // the class name string.
  116.       char buffer[17];
  117. #ifdef IC_PM
  118.       unsigned int len =
  119.          (unsigned int)WinQueryClassName( *this, 17, buffer);
  120.       buffer[len] = '\0';
  121.       char*  atom = 0;
  122.       if (buffer[0] == '#')
  123.          {
  124.          IString strvalue(&buffer[1], len-1);
  125.          unsigned short value =
  126.             (unsigned short)strvalue.asUnsigned();
  127.          atom = MAKEINTATOM(value) ;
  128.          }
  129.       if      (atom == WC_FRAME)
  130.         result = "WC_FRAME";
  131.       else if (atom == WC_COMBOBOX      )
  132.         result = "WC_COMBOBOX";
  133.       else if (atom == WC_BUTTON        )
  134.         result = "WC_BUTTON";
  135.       else if (atom == WC_MENU          )
  136.         result = "WC_MENU";
  137.       else if (atom == WC_STATIC        )
  138.         result = "WC_STATIC";
  139.       else if (atom == WC_ENTRYFIELD    )
  140.         result = "WC_ENTRYFIELD";
  141.       else if (atom == WC_LISTBOX       )
  142.         result = "WC_LISTBOX";
  143.       else if (atom == WC_SCROLLBAR     )
  144.         result = "WC_SCROLLBAR";
  145.       else if (atom == WC_TITLEBAR      )
  146.         result = "WC_TITLEBAR";
  147.       else if (atom == WC_MLE           )
  148.         result = "WC_MLE";
  149.       else if (atom == WC_SPINBUTTON    )
  150.         result = "WC_SPINBUTTON";
  151.       else if (atom == WC_CONTAINER     )
  152.         result = "WC_CONTAINER";
  153.       else if (atom == WC_SLIDER        )
  154.         result = "WC_SLIDER";
  155.       else if (atom == WC_VALUESET      )
  156.         result = "WC_VALUESET";
  157.       else if (atom == WC_NOTEBOOK      )
  158.         result = "WC_NOTEBOOK";
  159.       else if (atom == WC_CIRCULARSLIDER)
  160.         result = "WC_CIRCULARSLIDER";
  161.       else
  162.         result = IString(buffer);
  163. #else
  164.       int len = GetClassName( *this, buffer, 17);
  165.       if (len)
  166.          result = IString(buffer);
  167. #endif
  168.       }
  169.    return result;
  170. }
  171.  
  172. IString  HWindow::style      ( ) const
  173. {
  174.    unsigned long ulStyle(0), ulExtStyle(0);
  175.    if (this->isValid())
  176.       {
  177. #ifdef IC_PM
  178.       ulStyle = WinQueryWindowULong(*this, QWL_STYLE);
  179. #else
  180.       ulStyle = (unsigned long)GetWindowLong( *this, GWL_STYLE );
  181.       ulExtStyle = (unsigned long)GetWindowLong( *this,
  182.                                                  GWL_EXSTYLE );
  183. #endif
  184.       }
  185.    IString result = IString(ulStyle);
  186.    result.d2x().rightJustify(8,'0');
  187.    if (ulExtStyle != 0)
  188.       {
  189.       IString ext( ulExtStyle );
  190.       ext.d2x().rightJustify(8,'0');
  191.       result += IString(",") + ext;
  192.       }
  193.    return result;
  194. }
  195.  
  196. IBase::Boolean HWindow::isValid    ( ) const
  197. {
  198. #ifdef IC_PM
  199.    // This is faster than getting thread and using WinIsWindow.
  200.    return WinQueryAnchorBlock( *this ) ? true : false;
  201. #else
  202.    return IsWindow( *this ) ? true : false;
  203. #endif
  204. }
  205.  
  206. HWindow::ChildCursor::ChildCursor ( const HWindow& parent )
  207.   : hwnd       ( 0 ),
  208.     hwndParent ( (IHandle::Value)parent )
  209. {
  210. }
  211.  
  212. HWindow::ChildCursor::~ChildCursor ( )
  213. {
  214. }
  215.  
  216. Boolean HWindow::ChildCursor::setToFirst ( )
  217. {
  218. #ifdef IC_PM
  219.   this->hwnd = WinQueryWindow( this->hwndParent, QW_TOP );
  220. #else
  221.   this->hwnd = GetWindow( this->hwndParent, GW_CHILD);
  222. #endif
  223.   return this->isValid();
  224. }
  225.  
  226. Boolean HWindow::ChildCursor::setToNext ( )
  227. {
  228.   if ( !this->hwnd )
  229.      return setToFirst();
  230.   else
  231.      {
  232. #ifdef IC_PM
  233.      this->hwnd = WinQueryWindow( this->hwnd, QW_NEXT );
  234. #else
  235.      this->hwnd = GetWindow( this->hwnd, GW_HWNDNEXT ) ;
  236. #endif
  237.      }
  238.   return this->isValid();
  239. }
  240.  
  241. Boolean HWindow::ChildCursor::isValid ( ) const
  242. {
  243.   return this->hwnd != 0;
  244. }
  245.  
  246. void HWindow::ChildCursor::invalidate ( )
  247. {
  248.   this->hwnd = 0;
  249. }
  250.  
  251. HWindow HWindow::ChildCursor::hWindow ( ) const
  252. {
  253.   return HWindow(this->hwnd);
  254. }
  255.  
  256.  
  257.