home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / PEEPER.PAK / PEEPER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.2 KB  |  348 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/dc.h>
  8. #include <owl/dialog.h>
  9. #include <owl/framewin.h>
  10. #include <owl/listbox.h>
  11. #include <owl/statusba.h>
  12. #include <owl/controlb.h>
  13. #include <owl/buttonga.h>
  14. #include <owl/static.h>
  15. #include <owl/decframe.h>
  16. #include <owl/inputdia.h>
  17. #include <owl/checkbox.h>
  18. #include <cstring.h>
  19. #include "peeper.h"
  20.  
  21. //
  22. // ListBox id.
  23. //
  24. static const WORD ID_LISTBOX =            1000;
  25.  
  26. //
  27. // Variables that represent Windows' macros.
  28. //
  29. static WORD MAXIMIZE_MSG =                SC_MAXIMIZE;
  30. static WORD MINIMIZE_MSG =                SC_MINIMIZE;
  31. static WORD PAINT_MSG =                   WM_PAINT;
  32. static WORD RESTORE_MSG =                 SC_RESTORE;
  33. static WORD PASTE_MSG =                   WM_PASTE;
  34. static WORD CLOSE_MSG =                   WM_CLOSE;
  35.  
  36. //
  37. // TClientWindow response table.
  38. //
  39. DEFINE_RESPONSE_TABLE1(TClientWindow, TWindow)
  40.   EV_WM_LBUTTONDOWN,
  41.   EV_WM_RBUTTONDOWN,
  42.   EV_LBN_SELCHANGE(ID_LISTBOX, DisplayWinInfo),
  43. END_RESPONSE_TABLE;
  44.  
  45. //
  46. // TPeeper Response table
  47. //
  48. DEFINE_RESPONSE_TABLE1(TPeeper, TApplication)
  49.   EV_COMMAND(CM_PEEPING, CmPeeping),
  50.   EV_COMMAND(CM_CHANGE_CAPTION, CmChangeCaption),
  51.   EV_COMMAND(CM_CLEAR_WINDOWLIST, CmClearWindowList),
  52.   EV_COMMAND(CM_SEND_MESSAGE, CmSendMessage),
  53.   EV_COMMAND(CM_HELP, CmHelp),
  54. END_RESPONSE_TABLE;
  55.  
  56.  
  57. //
  58. // Global functions, used with ForEach().
  59. //
  60. void SendWMSetText(HWND& hWnd, void* p)
  61. {
  62.   ::SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)(LPCSTR)p);
  63. }
  64.  
  65. void SendSysMessage(HWND& hWnd, void* p)
  66. {
  67.   ::SendMessage(hWnd, WM_SYSCOMMAND, *(WORD*)p, 0);
  68. }
  69.  
  70. void SendWMMessage(HWND& hWnd, void* p)
  71. {
  72.   ::SendMessage(hWnd, *(WORD*)p, 0, 0);
  73. }
  74.  
  75. //----------------------------------------------------------------------------
  76.  
  77. //
  78. // TClientWindow member functions.
  79. //
  80.  
  81. //
  82. // EvLButtonDown(). Used while peeping. Get caption and handle of window under
  83. // mouse and save.
  84. //
  85. void
  86. TClientWindow::EvLButtonDown(uint, TPoint& point)
  87. {
  88.   TPoint     p = point;
  89.   HWND       chosenWindow;
  90.   char       caption[128];
  91.   TRect      r;
  92.   TPeeper*   peeper = TYPESAFE_DOWNCAST(GetApplication(),TPeeper);
  93.  
  94.   ::ClientToScreen(GetHandle(), &p);
  95.   ::GetWindowRect(peeper->MainWindow->GetHandle(), &r);
  96.  
  97.   if (!r.Contains(p)) {
  98.     chosenWindow = ::WindowFromPoint(p);
  99.     peeper->WindowList->Add(chosenWindow);
  100.     if (!::GetWindowText(chosenWindow, caption, sizeof caption))
  101.       peeper->SelectedWindows->AddString("No caption");
  102.     else
  103.       peeper->SelectedWindows->AddString(caption);
  104.   }
  105. }
  106.  
  107. //
  108. // EvRButtonDown(). Stop peeping. Set status line to 'Command Mode' and
  109. // release mouse capture.
  110. //
  111. void
  112. TClientWindow::EvRButtonDown(uint, TPoint&)
  113. {
  114.   TPeeper*  peeper = TYPESAFE_DOWNCAST(GetApplication(),TPeeper);
  115.  
  116.   peeper->Actions->SetHintCommand(-1);
  117.   peeper->StatusLine->SetText("Command Mode.");
  118.   ReleaseCapture();
  119. }
  120.  
  121. //
  122. // DisplayWinInfo().  Display the following information in a message box when
  123. // item from list box is selected:
  124. //  * Window Class.
  125. //  * demensions of window (left, top, right, bottom).
  126. //  * does the window have a menu?
  127. //  * does the window have children?
  128. //
  129. void
  130. TClientWindow::DisplayWinInfo()
  131. {
  132.   string   infoText;
  133.   char     className[80];
  134.   TRect    winRect;
  135.   HWND     hWnd;
  136.   int      haveMenu;
  137.   int      haveChildren;
  138.   char     buf[80];
  139.   string   comma(", ");
  140.   string   yes("Yes");
  141.   string   no("No");
  142.   string   newLine("\n");
  143.   TPeeper* peeper = TYPESAFE_DOWNCAST(GetApplication(),TPeeper);
  144.  
  145.   hWnd = (*peeper->WindowList)[peeper->SelectedWindows->GetSelIndex()];
  146.  
  147.   ::GetClassName(hWnd, className, 80);
  148.   ::GetWindowRect(hWnd, &winRect);
  149.   haveMenu = ::GetMenu(hWnd) != 0;
  150.   haveChildren = ::GetTopWindow(hWnd) != 0;
  151.  
  152.   infoText += "Window Class: " + string(className) + newLine;
  153.   infoText += "Window Dimensions: ";
  154.   infoText += string(itoa(winRect.left, buf, 10)) + comma +
  155.               string(itoa(winRect.top, buf, 10)) + comma +
  156.               string(itoa(winRect.right, buf, 10)) + comma +
  157.               string(itoa(winRect.bottom, buf, 10)) + newLine;
  158.   infoText += "Window has a menu? " + (haveMenu ? yes : no) + newLine;
  159.   infoText += "Window has Children? " + (haveChildren ? yes : no) + newLine;
  160.  
  161.   ::MessageBox(0, infoText.c_str(), "Window Info", MB_OK);
  162. }
  163.  
  164. //----------------------------------------------------------------------------
  165.  
  166. //
  167. // TPeeper member functions.
  168. //
  169.  
  170. //
  171. //
  172. //
  173. TPeeper::TPeeper()
  174. :
  175.   TApplication("Peeper")
  176. {
  177. }
  178.  
  179. //
  180. //
  181. //
  182. TPeeper::~TPeeper()
  183. {
  184.   delete WindowList;
  185. }
  186.  
  187. //
  188. //
  189. //
  190. void
  191. TPeeper::InitMainWindow()
  192. {
  193.   Client = new TClientWindow;
  194.  
  195.   // Create frame.  Make it look like a dialog.  Enable menu tracking
  196.   // for hints.
  197.   //
  198.   TDecoratedFrame* frame = new TDecoratedFrame(0, "Peeper", Client, true);
  199.   frame->Attr.Style &= ~(WS_MAXIMIZEBOX | WS_THICKFRAME);
  200.   frame->Attr.W = 324;
  201.   frame->Attr.H = 225;
  202.   frame->SetIcon(this, IDI_PEEPER);
  203.  
  204.   // Construct, build and insert a control bar into the frame
  205.   //
  206.   Actions = new TControlBar(frame);
  207.   Actions->Insert(*new TButtonGadget(IDB_EXIT, CM_EXIT));
  208.   Actions->Insert(*new TSeparatorGadget);
  209.   Actions->Insert(*new TButtonGadget(IDB_PEEPING, CM_PEEPING));
  210.   Actions->Insert(*new TButtonGadget(IDB_CLEAR_WINDOWLIST, CM_CLEAR_WINDOWLIST));
  211.   Actions->Insert(*new TSeparatorGadget);
  212.   Actions->Insert(*new TButtonGadget(IDB_CHANGE_CAPTION, CM_CHANGE_CAPTION));
  213.   Actions->Insert(*new TButtonGadget(IDB_MESSAGES, CM_SEND_MESSAGE));
  214.   Actions->Insert(*new TSeparatorGadget);
  215.   Actions->Insert(*new TButtonGadget(IDB_HELP, CM_HELP));
  216.   Actions->SetHintMode(TGadgetWindow::EnterHints);
  217.   frame->Insert(*Actions, TDecoratedFrame::Top);
  218.  
  219.   // Insert message bar into frame. Add some margins since our window has a
  220.   // thin frame & the message bar edges tend to get lost.
  221.   //
  222.   StatusLine = new TMessageBar(0);
  223.   StatusLine->SetText("Command Mode");
  224.  
  225.   TMargins margins;
  226.   margins.Units = TMargins::Pixels;
  227.   margins.Left = 2;
  228.   margins.Right = 2;
  229.   margins.Top = 1;
  230.   margins.Bottom = 1;
  231.   StatusLine->SetMargins(margins);
  232.   frame->Insert(*StatusLine, TDecoratedFrame::Bottom);
  233.  
  234.   // Create list box and it's title.
  235.   //
  236.   SelectedWindows = new TListBox(Client, ID_LISTBOX, 6, 28, 300, 106);
  237.   SelWinText = new TStatic(Client, -1, "SelectedWindows", 6, 4, 125, 16, 20);
  238.  
  239.   MainWindow = frame;
  240.  
  241.   // Create window list vector.
  242.   //
  243.   WindowList = new TCVectorImp<HWND>(20, 2);
  244. }
  245.  
  246. //
  247. // CmPeeping(). Set status line to 'Peeping!' and capture mouse.
  248. //
  249. void
  250. TPeeper::CmPeeping()
  251. {
  252.   SetCapture(Client->GetHandle());
  253.   Actions->SetHintCommand(-1);
  254.   StatusLine->SetText("Peeping Mode. Right-click to end.");
  255. }
  256.  
  257. //
  258. // CmChangeCaption(). Prompt user for string.  Change caption of all windows
  259. // in list to string entered by user.
  260. //
  261. void
  262. TPeeper::CmChangeCaption()
  263. {
  264.   if (WindowList->Count()) {
  265.     char  buffer[81];
  266.     buffer[0] = 0;
  267.     TInputDialog   input(Client, "Enter Text", "Text:", buffer, 80);
  268.  
  269.     if (input.Execute() == IDOK)
  270.        WindowList->ForEach(SendWMSetText, buffer);
  271.   }
  272.   else
  273.     MainWindow->MessageBox("Must choose some windows first", "Error");
  274. }
  275.  
  276. //
  277. // CmClearWindowList(). Clear list box that contains the captions of the
  278. // selected windows.  Also, empty window list.
  279. //
  280. void
  281. TPeeper::CmClearWindowList()
  282. {
  283.   WindowList->Flush();
  284.   SelectedWindows->ClearList();
  285. }
  286.  
  287. //
  288. // CmSendMessage(). Send messages selected by user to all windows in window
  289. // list.
  290. //
  291. void
  292. TPeeper::CmSendMessage()
  293. {
  294.   if (WindowList->Count()) {
  295.     MessagesTB        mtb;      // transfer buffer.
  296.     TMessagesDialog   *messages = new TMessagesDialog(MainWindow, &mtb);
  297.  
  298.     if (messages->Execute() == IDOK) {
  299.       if (mtb.MaxCB)
  300.         WindowList->ForEach(SendSysMessage, &MAXIMIZE_MSG);
  301.       if (mtb.MinCB)
  302.         WindowList->ForEach(SendSysMessage, &MINIMIZE_MSG);
  303.       if (mtb.PaintCB)
  304.         WindowList->ForEach(SendWMMessage, &PAINT_MSG);
  305.       if (mtb.RestoreCB)
  306.         WindowList->ForEach(SendSysMessage, &RESTORE_MSG);
  307.       if (mtb.PasteCB)
  308.         WindowList->ForEach(SendWMMessage, &PASTE_MSG);
  309.       if (mtb.CloseCB) {
  310.         WindowList->ForEach(SendWMMessage, &CLOSE_MSG);
  311.         CmClearWindowList();
  312.       }
  313.     }
  314.   }
  315.   else
  316.     MainWindow->MessageBox("Must choose some windows first", "Error");
  317. }
  318.  
  319. //
  320. // CmHelp(). Display help message box.
  321. //
  322. void
  323. TPeeper::CmHelp()
  324. {
  325.   string  msg;
  326.  
  327.   msg += "Press 'Peep!' button to select windows.\n";
  328.   msg += "While peeping press left mouse button to select window under cursor.\n";
  329.   msg += "When finished peeping press right mouse button to return to command mode.\n";
  330.   MainWindow->MessageBox(msg.c_str(), "Help");
  331. }
  332.  
  333. //----------------------------------------------------------------------------
  334.  
  335. //
  336. //
  337. //
  338. int OwlMain(int, char* [])
  339. {
  340. #if defined(BI_PLAT_WIN32)
  341.   if (!TSystem::Has3dUI()) {
  342.     ::MessageBox(0,"This example requires Win 4.0 or greater", "OWL Examples", MB_OK);
  343.     return 0;
  344.   }
  345. #endif
  346.   return TPeeper().Run();
  347. }
  348.