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

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: cliptext.c
  9. //
  10. //  PURPOSE: To show cut/copy/paste of text with the clipboard
  11. //
  12. //
  13. //  FUNCTIONS:
  14. //    WndProc        - Processes messages for the main window.
  15. //    InitClipText   - Creates the initial text for the main window.
  16. //    UninitClipText - Frees resources allocated by InitClipText.
  17. //    MsgCommand     - Handle the WM_COMMAND messages for the main window.
  18. //    MsgDestroy     - Handles the WM_DESTROY message by calling 
  19. //                     PostQuitMessage().
  20. //    CmdCut         - Place the text in the window into the clipboard and 
  21. //                     delete it.
  22. //    CmdCopy        - Place the text in the window into the clipboard.
  23. //    CmdPaste       - Pastes the text from the clipboard in the main window.
  24. //    CmdClear       - Delete's the text in the main window.
  25. //    CmdExit        - Handles the file exit command by calling destory 
  26. //                     window on the main window.
  27. //    OutOfMemory    - Display and "Out of Memory" dialog box.
  28. //    Copy           - Copies the text from hText to the clipboard.
  29. //    Clear          - Deletes the text contained in hText.
  30. //
  31. //
  32. //  COMMENTS:
  33. //
  34. //
  35. //
  36. //  SPECIAL INSTRUCTIONS: N/A
  37. //
  38.  
  39.  
  40. #include <windows.h>            // required for all Windows applications
  41. #ifdef WIN16
  42. #include "win16ext.h"           // required only for win16 applications
  43. #endif
  44. #include "globals.h"            // prototypes specific to this application
  45. #include <assert.h>
  46. #include "resource.h"
  47.  
  48. // Main window message table definition.
  49. MSD rgmsd[] =
  50. {
  51.     {WM_COMMAND,    MsgCommand},
  52.     {WM_DESTROY,    MsgDestroy},
  53.     {WM_INITMENU,   MsgInitMenu},
  54.     {WM_PAINT,      MsgPaint}
  55. };
  56.  
  57. MSDI msdiMain =
  58. {
  59.     sizeof(rgmsd) / sizeof(MSD),
  60.     rgmsd,
  61.     edwpWindow
  62. };
  63.  
  64.  
  65. // Main window command table definition.
  66. CMD rgcmd[] =
  67. {
  68.     {IDM_EXIT,  CmdExit},
  69.     {IDM_ABOUT, CmdAbout},
  70.     {IDM_CUT,   CmdCut},
  71.     {IDM_COPY,  CmdCopy},
  72.     {IDM_CLEAR, CmdClear},
  73.     {IDM_PASTE, CmdPaste}
  74. };
  75.  
  76. CMDI cmdiMain =
  77. {
  78.     sizeof(rgcmd) / sizeof(CMD),
  79.     rgcmd,
  80.     edwpWindow
  81. };
  82.  
  83. //Module specific "globals"
  84.  
  85. char szInitialClientAreaText[] =
  86.     "This program demonstrates the use of the Edit menu to copy and "
  87.     "paste text to and from the clipboard.  Try using the Copy command "
  88.     "to move this text to the clipboard, and the Paste command to replace "
  89.     "this text with data from another application.  \r\n\r\n"
  90.     "You might want to try running Notepad and Clipbrd alongside this "
  91.     "application so that you can watch the data exchanges take place.  ";
  92.  
  93. static HANDLE hText = NULL;
  94.  
  95. VOID OutOfMemory(VOID);
  96. BOOL Copy(HWND);
  97. VOID Clear(HWND);
  98.  
  99. //
  100. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  101. //
  102. //  PURPOSE:  Processes messages for the main window.
  103. //
  104. //  PARAMETERS:
  105. //    hwnd     - window handle
  106. //    uMessage - message number
  107. //    wparam   - additional information (dependant on message number)
  108. //    lparam   - additional information (dependant on message number)
  109. //
  110. //  RETURN VALUE:
  111. //    The return value depends on the message number.  If the message
  112. //    is implemented in the message dispatch table, the return value is
  113. //    the value returned by the message handling function.  Otherwise,
  114. //    the return value is the value returned by the default window procedure.
  115. //
  116. //  COMMENTS:
  117. //    Call the DispMessage() function with the main window's message dispatch
  118. //    information (msdiMain) and the message specific information.
  119. //
  120.  
  121. LRESULT CALLBACK WndProc(HWND   hwnd, 
  122.                          UINT   uMessage, 
  123.                          WPARAM wparam, 
  124.                          LPARAM lparam)
  125. {
  126.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  127. }
  128.  
  129.  
  130. //
  131. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  132. //
  133. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  134. //
  135. //  PARAMETERS:
  136. //    hwnd     - window handle
  137. //    uMessage - WM_COMMAND (Unused)
  138. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  139. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  140. //
  141. //  RETURN VALUE:
  142. //    The return value depends on the message number.  If the message
  143. //    is implemented in the message dispatch table, the return value is
  144. //    the value returned by the message handling function.  Otherwise,
  145. //    the return value is the value returned by the default window procedure.
  146. //
  147. //  COMMENTS:
  148. //    Call the DispCommand() function with the main window's command dispatch
  149. //    information (cmdiMain) and the command specific information.
  150. //
  151.  
  152. #pragma argsused
  153. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  154. {
  155.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  156. }
  157.  
  158.  
  159. //
  160. //  FUNCTION: InitClipText()
  161. //
  162. //  PURPOSE: Creates the initial text for the main window.
  163. //
  164. //  PARAMETERS:
  165. //    NONE.
  166. //
  167. //  RETURN VALUE:
  168. //    TRUE - Success.
  169. //    FALSE - Failure, due to memory allocation.
  170. //
  171. //  COMMENTS:
  172. //
  173. //
  174.  
  175. BOOL InitClipText(VOID)
  176. {
  177.     LPSTR lpszText;
  178.  
  179.     hText = GlobalAlloc(GMEM_MOVEABLE, (DWORD)sizeof(szInitialClientAreaText));
  180.     if (hText == NULL)
  181.     {
  182.         OutOfMemory();
  183.         return FALSE;
  184.     }
  185.  
  186.      lpszText = GlobalLock(hText);
  187.      if (!(lpszText))
  188.      {
  189.           OutOfMemory();
  190.           return FALSE;
  191.     }
  192.  
  193.     lstrcpy(lpszText, szInitialClientAreaText);
  194.     GlobalUnlock(hText);
  195.  
  196.     return TRUE;
  197. }
  198.  
  199. //
  200. //  FUNCTION: UninitClipText()
  201. //
  202. //  PURPOSE: Frees resources allocated by InitClipText.
  203. //
  204. //  PARAMETERS:
  205. //    NONE.
  206. //
  207. //  RETURN VALUE:
  208. //    NONE.
  209. //
  210. //  COMMENTS:
  211. //
  212. //
  213.  
  214. VOID UninitClipText(VOID)
  215. {
  216.     if (hText != NULL)
  217.         GlobalFree(hText);
  218. }
  219.  
  220.  
  221. //
  222. //  FUNCTION: MsgInitMenu(HWND, UINT, WPARAM, LPARAM)
  223. //
  224. //  PURPOSE: Enable/Disable the paste menu command.
  225. //
  226. //  PARAMETERS:
  227. //
  228. //    hwnd      - Window handle
  229. //    uMessage  - Message number (Unused)
  230. //    wparam    - HMENU - The menu about to be activated
  231. //    lparam    - Extra data     (Unused)
  232. //
  233. //  RETURN VALUE:
  234. //
  235. //    0 - The message was handled.
  236. //    1 - The message was not handled - wrong menu.
  237. //
  238. //  COMMENTS:
  239. //
  240. //
  241.  
  242. #pragma argsused
  243. LRESULT MsgInitMenu(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  244. {
  245.      if (GetMenu(hwnd) == (HMENU)wparam)
  246.      {
  247.           UINT mf = MF_GRAYED;
  248.  
  249.           if (OpenClipboard(hwnd))
  250.           {
  251.                 if (IsClipboardFormatAvailable(CF_TEXT) ||
  252.                      IsClipboardFormatAvailable(CF_OEMTEXT))
  253.                 {
  254.                      mf = MF_ENABLED;
  255.                 }
  256.           }
  257.           CloseClipboard();
  258.  
  259.           EnableMenuItem((HMENU)wparam, IDM_PASTE, mf);
  260.  
  261.           return 0;
  262.      }
  263.      else
  264.      {
  265.           return 1;
  266.      }
  267. }
  268.  
  269.  
  270. //
  271. //  FUNCTION: MsgPaint(HWND, UINT, WPARAM, LPARAM)
  272. //
  273. //  PURPOSE: Paint the main window's current text, if any.
  274. //
  275. //  PARAMETERS:
  276. //
  277. //    hwnd      - Window handle
  278. //    uMessage  - Message number (Unused)
  279. //    wparam    - Extra data     (Unused)
  280. //    lparam    - Extra data     (Unused)
  281. //
  282. //  RETURN VALUE:
  283. //
  284. //    Always returns 0 - Message handled
  285. //
  286. //  COMMENTS:
  287. //
  288. //
  289.  
  290. #pragma argsused
  291. LRESULT MsgPaint(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  292. {
  293.      PAINTSTRUCT ps;
  294.      HDC hdc;
  295.  
  296.      hdc = BeginPaint(hwnd, &ps);
  297.      if (hText != NULL)
  298.      {
  299.           LPSTR lpszText;
  300.  
  301.           lpszText = GlobalLock (hText);
  302.           if (!(lpszText))
  303.           {
  304.             OutOfMemory();
  305.         }
  306.           else
  307.         {
  308.             RECT rc;
  309.             GetClientRect (hwnd, &rc);
  310.             DrawText(hdc,
  311.                      lpszText,
  312.                      -1,
  313.                      &rc,
  314.                      DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);
  315.             GlobalUnlock(hText);
  316.         }
  317.     }
  318.     EndPaint(hwnd, &ps);
  319.  
  320.     return 0;
  321. }
  322.  
  323.  
  324.  
  325. //
  326. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  327. //
  328. //  PURPOSE: Calls PostQuitMessage().
  329. //
  330. //  PARAMETERS:
  331. //
  332. //    hwnd      - Window handle  (Unused)
  333. //    uMessage  - Message number (Unused)
  334. //    wparam    - Extra data     (Unused)
  335. //    lparam    - Extra data     (Unused)
  336. //
  337. //  RETURN VALUE:
  338. //
  339. //    Always returns 0 - Message handled
  340. //
  341. //  COMMENTS:
  342. //
  343. //
  344.  
  345. #pragma argsused
  346. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  347. {
  348.     PostQuitMessage(0);
  349.     return 0;
  350. }
  351.  
  352.  
  353.  
  354. //
  355. //  FUNCTION: CmdCut(HWND, WORD, WORD, HWND)
  356. //
  357. //  PURPOSE: Place the text in the window into the clipboard and delete it.
  358. //
  359. //  PARAMETERS:
  360. //    hwnd - The window handle
  361. //    wCommand - IDM_CUT (unused)
  362. //    wNotify   - Notification number (unused)
  363. //    hwndCtrl - NULL (unused)
  364. //
  365. //  RETURN VALUE:
  366. //    Always returns 0 - Message handled
  367. //
  368. //  COMMENTS:
  369. //    The bulk of the work is done by the Copy and Clear functions.
  370. //    If copy to the clipboard does not succeed, Clear is not called.
  371. //
  372. //
  373.  
  374. #pragma argsused
  375. LRESULT CmdCut(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  376. {
  377.      if (Copy(hwnd))
  378.           Clear(hwnd);
  379.  
  380.     InvalidateRect(hwnd, NULL, TRUE);
  381.     return 0;
  382. }
  383.  
  384.  
  385. //
  386. //  FUNCTION: CmdCopy(HWND, WORD, WORD, HWND)
  387. //
  388. //  PURPOSE: Place the text in the window into the clipboard.
  389. //
  390. //  PARAMETERS:
  391. //    hwnd - The window handle
  392. //    wCommand - IDM_COPY (unused)
  393. //    wNotify   - Notification number (unused)
  394. //    hwndCtrl - NULL (unused)
  395. //
  396. //  RETURN VALUE:
  397. //    Always returns 0 - Message handled
  398. //
  399. //  COMMENTS:
  400. //
  401. //
  402.  
  403. #pragma argsused
  404. LRESULT CmdCopy(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  405. {
  406.     (void) Copy(hwnd);
  407.  
  408.     return 0;
  409. }
  410.  
  411.  
  412. //
  413. //  FUNCTION: CmdClear(HWND, WORD, WORD, HWND)
  414. //
  415. //  PURPOSE: Delete's the text in the main window.
  416. //
  417. //  PARAMETERS:
  418. //    hwnd - The window handle
  419. //    wCommand - IDM_CLEAR (unused)
  420. //    wNotify   - Notification number (unused)
  421. //    hwndCtrl - NULL (unused)
  422. //
  423. //  RETURN VALUE:
  424. //    Always returns 0 - Message handled
  425. //
  426. //  COMMENTS:
  427. //
  428. //
  429.  
  430. #pragma argsused
  431. LRESULT CmdClear(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  432. {
  433.     Clear(hwnd);
  434.     InvalidateRect(hwnd, NULL, TRUE);
  435.  
  436.     return 0;
  437. }
  438.  
  439.  
  440. //
  441. //  FUNCTION: CmdPaste(HWND, WORD, WORD, HWND)
  442. //
  443. //  PURPOSE: Pastes the text from the clipboard in the main window.
  444. //
  445. //  PARAMETERS:
  446. //    hwnd - The window handle
  447. //    wCommand - IDM_PASTE (unused)
  448. //    wNotify   - Notification number (unused)
  449. //    hwndCtrl - NULL (unused)
  450. //
  451. //  RETURN VALUE:
  452. //    Always returns 0 - Message handled
  453. //
  454. //  COMMENTS:
  455. //
  456. //
  457.  
  458. #pragma argsused
  459. LRESULT CmdPaste(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  460. {
  461.      HANDLE hData;
  462.     LPSTR  lpszText,lpszClip;
  463.     BOOL   fOpen;
  464.  
  465.     fOpen = OpenClipboard(hwnd);
  466.  
  467.     // If the clipboard can't be opened, this function should
  468.     //  never have been called.
  469.     assert(fOpen);
  470.  
  471.     // Get the text from the clipboard.
  472.  
  473.      hData = GetClipboardData(CF_TEXT);
  474.      if (!(hData))
  475.     {
  476.         CloseClipboard();
  477.         return 0;
  478.     }
  479.  
  480.     // Remove the old window text.
  481.     if (hText != NULL)
  482.     {
  483.         GlobalFree(hText);
  484.     }
  485.  
  486.     hText = GlobalAlloc(GMEM_MOVEABLE, GlobalSize(hData));
  487.      lpszText = GlobalLock(hText);
  488.     lpszClip = GlobalLock(hData);
  489.  
  490.     if (hText == NULL || lpszText == NULL || lpszClip == NULL)
  491.     {
  492.         OutOfMemory();
  493.         CloseClipboard();
  494.         return 0;
  495.     }
  496.  
  497.     lstrcpy(lpszText, lpszClip);
  498.      GlobalUnlock(hData);
  499.     CloseClipboard();
  500.      GlobalUnlock(hText);
  501.     EnableMenuItem(GetMenu(hwnd), IDM_CUT, MF_ENABLED);
  502.     EnableMenuItem(GetMenu(hwnd), IDM_COPY, MF_ENABLED);
  503.     EnableMenuItem(GetMenu(hwnd), IDM_CLEAR, MF_ENABLED);
  504.  
  505.     InvalidateRect(hwnd, NULL, TRUE);
  506.  
  507.     return 0;
  508. }
  509.  
  510.  
  511. //
  512. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  513. //
  514. //  PURPOSE: Exit the application.
  515. //
  516. //  PARAMETERS:
  517. //    hwnd     - The window.
  518. //    wCommand - IDM_EXIT (unused)
  519. //    wNotify  - Notification number (unused)
  520. //    hwndCtrl - NULL (unused)
  521. //
  522. //  RETURN VALUE:
  523. //    Always returns 0 - command handled.
  524. //
  525. //  COMMENTS:
  526. //
  527. //
  528.  
  529. #pragma argsused
  530. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  531. {
  532.      DestroyWindow(hwnd);
  533.      return 0;
  534. }
  535.  
  536.  
  537. //
  538. //  FUNCTION: OutOfMemory
  539. //
  540. //  PURPOSE: Display and "Out of Memory" dialog box.
  541. //
  542. //  PARAMETERS:
  543. //    NONE.
  544. //
  545. //  RETURN VALUE:
  546. //    NONE.
  547. //
  548. //  COMMENTS:
  549. //
  550. //
  551.  
  552. VOID OutOfMemory(VOID)
  553. {
  554.     MessageBox(GetFocus(),
  555.                "Out of Memory",
  556.                NULL,
  557.                MB_ICONHAND | MB_SYSTEMMODAL);
  558. }
  559.  
  560. //
  561. //  FUNCTION: Copy(HWND)
  562. //
  563. //  PURPOSE: Copies the text from hText to the clipboard.
  564. //
  565. //  PARAMETERS:
  566. //    hwnd - The main application window.
  567. //
  568. //  RETURN VALUE:
  569. //    TRUE - Sucessful copy.
  570. //    FALSE - Copy failed.
  571. //
  572. //  COMMENTS:
  573. //
  574. //
  575.  
  576. BOOL Copy(HWND hwnd)
  577. {
  578.     HANDLE hData;
  579.     LPSTR  lpszText,lpszClip;
  580.  
  581.     // If hText is NULL, this point should never be reached, since
  582.     //  the menu item is disabled.
  583.  
  584.     assert(hText != NULL);
  585.  
  586.     // Allocate memory and copy the string to it
  587.  
  588.     hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, GlobalSize(hText));
  589.     if (hData == NULL)
  590.     {
  591.         OutOfMemory();
  592.         return FALSE;
  593.     }
  594.  
  595.      lpszClip = GlobalLock(hData);
  596.      if (!(lpszClip))
  597.     {
  598.         GlobalFree(hData);
  599.         OutOfMemory();
  600.         return FALSE;
  601.      }
  602.  
  603.      lpszText = GlobalLock(hText);
  604.      if (!(lpszText))
  605.      {
  606.         GlobalUnlock(hData);
  607.         GlobalFree(hData);
  608.         OutOfMemory();
  609.         return FALSE;
  610.     }
  611.  
  612.     lstrcpy(lpszClip, lpszText);
  613.     GlobalUnlock(hData);
  614.     GlobalUnlock(hText);
  615.  
  616.     // Clear the current contents of the clipboard, and set
  617.     // the data handle to the new string.
  618.  
  619.     if (OpenClipboard(hwnd))
  620.     {
  621.         EmptyClipboard();
  622.         SetClipboardData(CF_TEXT, hData);
  623.         CloseClipboard();
  624.         return TRUE;
  625.     }
  626.  
  627.     return FALSE;
  628. }
  629.  
  630. //
  631. //  FUNCTION: Clear(HWND)
  632. //
  633. //  PURPOSE: Deletes the text contained in hText.
  634. //
  635. //  PARAMETERS:
  636. //    hwnd - The main application window.
  637. //
  638. //  RETURN VALUE:
  639. //    NONE.
  640. //
  641. //  COMMENTS:
  642. //
  643. //
  644.  
  645. VOID Clear(HWND hwnd)
  646. {
  647.     // If hText is Null, this point should never be reached, since
  648.     //  the menu item is disabled.
  649.  
  650.     assert(hText != NULL);
  651.  
  652.     GlobalFree (hText);
  653.     hText = NULL;
  654.     EnableMenuItem(GetMenu(hwnd), IDM_CUT, MF_GRAYED);
  655.     EnableMenuItem(GetMenu(hwnd), IDM_COPY, MF_GRAYED);
  656.     EnableMenuItem(GetMenu(hwnd), IDM_CLEAR, MF_GRAYED);
  657. }
  658.  
  659.