home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / utility / oemclip.zip / OEMCLIP.CPP < prev    next >
C/C++ Source or Header  |  1993-06-03  |  8KB  |  253 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // OEMCLIP.CPP                                 (C) Alberto R. Lopez Siemens, 1993
  3. //
  4. // Freeware, Public domain -  Also included OEMCLIP.PRJ
  5. //
  6. // OEM-Text to ANSI-text automatic clipboard translator
  7. //
  8. //    This program has been developed to fix a problem I encountered when
  9. // tried to copy a portion of a DOS screen into my Word Processor.
  10. //
  11. // The problem is the following:
  12. // ----------------------------
  13. //
  14. //    Supose you have a DOS application running in a Window. and want to
  15. // copy its screen (or a portion of it) into a Windows application such as
  16. // a Word Processor.
  17. //
  18. // The first thing you'd do is select Edit|Mark from the system bar menu,
  19. // select the portion of the screen and press Enter. Then, switch to your
  20. // word processor and paste the data.
  21. //
  22. // It simply doesn't work as expected !
  23. //
  24. // If the screen capture has ASCII characters greater than 127, as
  25. // line-drawing, or international characters, you'll find that when pasted
  26. // into a Windows application that caracters are translated. The good-
  27. // looking line-drawing characters are transformed in ugly character
  28. // equivalences as '-', '|' and '+'.
  29. //
  30. // That's not what I want. I want my screen as I see it. This is a
  31. // Windows feature where "What you see is NOT what you get".
  32. // 
  33. // People at Microsoft wasn't be able to give me a solution. They said
  34. // that Windows uses ANSI character set only. Period. And that I can't
  35. // do what I want. So here is the solution. (Actually they gave me some
  36. // trivial solutions as search-and-replacing the characters or use
  37. // Alt-PrtScrn to get a bitmap representation of the screen).
  38. //
  39. // This program works registering itself as a Windows Clipboard Viewer.
  40. // When it detects that a DOS application pastes data into the clipboard
  41. // it simply translates it from ASCII to ANSI.
  42. //
  43. // To install OEMCLIP just double-click its icon or File|Run from
  44. // Program Manager. As default it run minimized. You can maximize it but
  45. // the contents of the window is meaningless. It is only a poor
  46. // representation of the data in the clipboard.
  47. //
  48. // To use pasted text You'll need a Windows font with ASCII character set.
  49. //    At this moment I know two: LotusLineDraw (comes with AmiPro or Lotus 123
  50. // for Windows) and Video Terminal Screen (You can find it in DTPFORUM, 
  51. // Lib 9, File: VTSR-T.ZIP (Shareware)
  52. //
  53. // Not too bad since this is my first Windows Program (I didn't even try
  54. //    a WinHello.CPP). 
  55. //
  56. // You need BC++ 3.1 and ObjectWindows library to recompile this program.
  57. //
  58. // Suggestions & comments are welcome:
  59. //    Mail Address:
  60. //            DigiSoft SRL
  61. //         Ing. Alberto R. Lopez Siemens
  62. //            Sarmiento 1426 6to Piso
  63. //            (1042) Buenos Aires
  64. //            Argentina
  65. //
  66. //    Voice/Fax #'s:
  67. //            (541) 40-5714
  68. //            or
  69. //            (541) 46-2151 (Remember we'll answer the phone in spanish !)
  70. //
  71. //     CIS Mail: 70501,1574
  72. //
  73. /////////////////////////////////////////////////////////////////////////
  74.  
  75. #include <owl.h>
  76.  
  77. #include <string.h>
  78.  
  79. class TMyApp : public TApplication
  80. {
  81. public:
  82.   TMyApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  83.             LPSTR lpCmdLine, int nCmdShow) :
  84.      TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  85.      {
  86.         ClipboardChanged = FALSE;
  87.      }
  88.   virtual void InitMainWindow();
  89.   virtual void IdleAction();
  90.  
  91.   BOOL    ClipboardChanged;
  92.   HWND    clipOwner;
  93. };
  94.  
  95. _CLASSDEF(TMyWindow)
  96. class TMyWindow : public TWindow
  97. {
  98. public:
  99.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle) :
  100.     TWindow(AParent, ATitle),
  101.     oldHWin(NULL) {};
  102.  
  103.   virtual BOOL Create();
  104.   virtual void Destroy();
  105.  
  106. // Windows that are part of the clipboard-viewer chain must respond to
  107. // WM_CHANGECBCHAIN, WM_DRAWCLIPBOARD, and WM_DESTROY messages.
  108. // To remove itself from the clipboard-viewer chain, an application must
  109. // call the ChangeClipboardChain function.
  110.   virtual void WMDrawClipboard(RTMessage)
  111.      = [WM_FIRST + WM_DRAWCLIPBOARD];
  112.  
  113.   virtual void WMChageCBChain(RTMessage)
  114.      = [WM_FIRST + WM_CHANGECBCHAIN];
  115.  
  116.   HWND    oldHWin;
  117. };
  118.  
  119. void TMyWindow::WMDrawClipboard(RTMessage Msg)
  120. {
  121.     if(oldHWin){
  122.         SendMessage(oldHWin, Msg.Message, Msg.WParam, Msg.LParam);
  123.  
  124.         TMyApp *app = (TMyApp *)GetApplication();
  125.         app->ClipboardChanged = TRUE;
  126.         app->clipOwner = GetFocus();
  127. // The GetFocus function retrieves the handle of the window that
  128. // currently has the input focus.
  129.     }
  130.     Msg.Result = 0l;
  131.     DefWndProc(Msg);
  132. }
  133.  
  134. void TMyWindow::WMChageCBChain(RTMessage Msg)
  135. {
  136. // Each window that receives the WM_CHANGECBCHAIN message should call the
  137. // SendMessage function to pass the message on to the next window in the
  138. // clipboard-viewer chain. If the window being removed is the next window
  139. // in the chain, the window specified by the hwndNext parameter becomes the 
  140. // next window and clipboard messages are passed on to it. 
  141.  
  142. // WM_CHANGECBCHAIN
  143. // hwndRemoved = (HWND) wParam;        /* handle of removed window */
  144. // hwndNext = (HWND) LOWORD(lParam);   /* handle of next window    */
  145.  
  146.     SendMessage(oldHWin, Msg.Message, Msg.WParam, Msg.LParam);
  147.  
  148.     if((HWND) Msg.WParam == oldHWin)
  149.         oldHWin = (HWND) LOWORD(Msg.LParam);
  150.  
  151.    Msg.Result = 0l;
  152.     DefWndProc(Msg);
  153. }
  154.  
  155. BOOL TMyWindow::Create()
  156. {
  157.     if(TWindow::Create()){
  158.         oldHWin = SetClipboardViewer(HWindow);
  159.         return TRUE;
  160.     }
  161.     return FALSE;
  162. }
  163.  
  164. void TMyWindow::Destroy()
  165. {
  166.     ChangeClipboardChain(HWindow,oldHWin);
  167.  
  168.    TWindow::Destroy();
  169. }
  170.  
  171. void TMyApp::InitMainWindow()
  172. {
  173.   MainWindow = new TMyWindow(NULL, Name);
  174. }
  175.  
  176. #define    print(s)    {\
  177.                     TextOut(dc, 0, yPos,(LPSTR)s, strlen(s));\
  178.                     yPos+=15;}
  179.  
  180. #define    nprint(s,n)    {\
  181.                     TextOut(dc, 0, yPos,(LPSTR)s, n);\
  182.                     yPos+=15;}
  183.  
  184. void TMyApp::IdleAction()
  185. {
  186.     if(ClipboardChanged){
  187.         // Check to see if the Window that had the focus when the clipboard
  188.         // changed was a DOS-app.
  189.         // I haven't found documentation about this, but the method used here
  190.       // seems to work well
  191.  
  192.         char bufName[64];
  193.  
  194.         GetClassName(clipOwner, bufName, sizeof(bufName));
  195.         if(stricmp(bufName,"tty") == 0 &&
  196.         IsClipboardFormatAvailable(CF_OEMTEXT)){
  197.             TMyWindow     *win = (TMyWindow *)MainWindow;
  198.             HDC            dc   = GetDC(win->HWindow);
  199.             DWORD         pos  = GetCurrentPosition(dc);
  200.             WORD            yPos = HIWORD(pos);
  201.  
  202.             if(OpenClipboard(win->HWindow)){
  203.                 HANDLE     hClip  = GetClipboardData(CF_OEMTEXT);
  204.                 DWORD        szClip = GlobalSize(hClip);
  205.                 HANDLE     hTemp  = GlobalAlloc(GPTR,szClip);
  206.                 void FAR    *pClip = GlobalLock(hClip),
  207.                       FAR *pTemp = GlobalLock(hTemp);
  208.  
  209.                 _fmemcpy(pTemp, pClip, (size_t)szClip);
  210.  
  211.                 // Translate characters not found in the ANSI character set
  212.                 int    i = 0;
  213.                 BYTE    *sTemp = (BYTE *)pTemp;
  214.  
  215.                 while(i < szClip && sTemp[i] != 0){
  216.                     if(sTemp[i] < 32 &&
  217.                sTemp[i] != 10 &&
  218.                     sTemp[i] != 13)
  219.                         sTemp[i] = ' ';
  220.                i++;
  221.                 }
  222.  
  223.             // This is for debugging purposes only.
  224.                 nprint(pTemp,(size_t)szClip);
  225.  
  226.                 GlobalUnlock(hTemp);
  227.                 GlobalUnlock(hClip);
  228.  
  229.                 // Put the data into the clipboard but change
  230.                 // the Format Identifier to CF_TEXT.
  231.                 // Simple but effective.
  232.  
  233.                 SetClipboardData(CF_TEXT, hTemp);
  234.                 CloseClipboard();
  235.             }
  236.  
  237.             ReleaseDC(win->HWindow,dc);
  238.         }
  239.  
  240.         ClipboardChanged = FALSE;
  241.     }
  242. }
  243.  
  244. #pragma argsused
  245. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  246.   LPSTR lpCmdLine, int nCmdShow)
  247. {
  248.   TMyApp MyApp("OEM-Text ClipBoard Translator", hInstance, hPrevInstance,
  249.                     lpCmdLine, SW_SHOWMINNOACTIVE);
  250.   MyApp.Run();
  251.   return MyApp.Status;
  252. }
  253.