home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mlelib.zip / sample.cpp < prev    next >
C/C++ Source or Header  |  1993-06-07  |  6KB  |  250 lines

  1. /*************************************************************************
  2. *
  3. * filename        : sample.cpp
  4. *
  5. * description    : show the use of class EDITOR
  6. *
  7. * functions     : main            - create a little editor
  8. *                  procTest        - editor's message processing
  9. *
  10. * APIs            : WinSetPointer         WinInitialize
  11. *                  WinCreateMsgQueue        WinRegisterClass
  12. *                 WinCreateStdWindow    WinSetWindowPos
  13. *                  WinShowWindow            WinGetMsg
  14. *                  WinDispatchMsg        WinDestroyMsgQueue
  15. *                  WinTerminate            WinBeginPaint
  16. *                  WinFillRect            WinEndPaint
  17. *                  WinPostMsg            WinDefWindowProc
  18. *
  19. * Used Classes    : class EDITOR
  20. *
  21. * copyright (C) 1993 Jörg Caumanns (caumanns@cs.tu-berlin.de)
  22. *
  23. *************************************************************************/
  24. #define INCL_WINWINDOWMGR
  25. #define INCL_WINDIALOGS
  26. #define INCL_WINFRAMEMGR
  27. #define INCL_WINMESSAGES
  28. #define INCL_WINPOINTERS
  29. #include <os2.h>
  30.  
  31. #include "editor.h"
  32. #include "sample.h"
  33.  
  34. /*
  35. * forward declarations
  36. */
  37. MRESULT    EXPENTRY procTest(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  38.  
  39.  
  40. /*************************************************************************
  41. *
  42. * Name    : main
  43. *
  44. * Descr.: Create the editor main-window
  45. *
  46. * Impl. : Create a standard-window with 'File' and 'Edit' menus
  47. *
  48. * APIs     : WinSetPointer             WinInitialize
  49. *             WinCreateMsgQueue            WinRegisterClass
  50. *         WinCreateStdWindow        WinSetWindowPos
  51. *             WinShowWindow                WinGetMsg
  52. *             WinDispatchMsg            WinDestroyMsgQueue
  53. *             WinTerminate
  54. *
  55. * Class.: [none]
  56. *
  57. * Param.: [none]
  58. *
  59. * Return: fSuccess
  60. *
  61. *************************************************************************/
  62. int main(void)    {
  63.  
  64.     /*
  65.     * change icon to clock
  66.     */
  67.     WinSetPointer(HWND_DESKTOP,
  68.                     WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT, FALSE));
  69.  
  70.     /*
  71.     * create message queue
  72.     */
  73.     HAB hab;
  74.     if((hab = WinInitialize(0)) == NULLHANDLE)    { return 0;    }
  75.  
  76.     HMQ hmq;
  77.     if((hmq = WinCreateMsgQueue(hab, 0)) == NULLHANDLE)    { return 0;    }
  78.  
  79.     /*
  80.     * register window class
  81.     */
  82.     if(!WinRegisterClass(hab,
  83.                          "TEST_MLE",
  84.                          (PFNWP)procTest,
  85.                          CS_SIZEREDRAW | CS_CLIPCHILDREN,
  86.                          32L))    {
  87.         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  88.                       "Can't register window class!", "FATAL ERROR",
  89.                       0, MB_ERROR | MB_CANCEL);
  90.         WinDestroyMsgQueue(hmq);
  91.         WinTerminate(hab);
  92.         return 0;
  93.         }
  94.  
  95.     /*
  96.     * create main-window
  97.     */
  98.     HWND hwndFrame, hwndClient;
  99.     ULONG fctlFrame = FCF_BORDER | FCF_MENU | FCF_TASKLIST | FCF_TITLEBAR;
  100.  
  101.     if((hwndFrame = (HWND)WinCreateStdWindow(HWND_DESKTOP,
  102.                                             WS_VISIBLE,
  103.                                             &fctlFrame,
  104.                                             "TEST_MLE",
  105.                                             "MiniEditor",
  106.                                             0,
  107.                                             (HMODULE)NULL,
  108.                                             IDR_TEST,
  109.                                             &hwndClient)) == NULLHANDLE)    {
  110.         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  111.                       "Can't create window!", "FATAL ERROR",
  112.                       0, MB_ERROR | MB_CANCEL);
  113.         WinDestroyMsgQueue(hmq);
  114.         WinTerminate(hab);
  115.         return 0;
  116.         }
  117.  
  118.     SWP swp;
  119.     WinQueryWindowPos(HWND_DESKTOP, &swp);
  120.     WinSetWindowPos(hwndFrame,
  121.                     HWND_TOP,
  122.                     swp.x + swp.cx - 160,
  123.                     swp.y + swp.cy - 40,
  124.                     160,
  125.                     40,
  126.                     SWP_MOVE | SWP_SIZE | SWP_ACTIVATE);
  127.     WinShowWindow(hwndFrame, 1);
  128.  
  129.     /*
  130.     * Message-Loop
  131.     */
  132.     QMSG  qmsg;
  133.     while(WinGetMsg(hmq, (PQMSG)&qmsg, NULLHANDLE, 0, 0))
  134.         WinDispatchMsg(hmq, (PQMSG)&qmsg);
  135.  
  136.     /*
  137.     * Termination
  138.     */
  139.     WinDestroyMsgQueue(hmq);
  140.     WinTerminate(hab);
  141.     return 1;
  142.     }
  143.  
  144.  
  145. /*************************************************************************
  146. *
  147. * Name    : procTest
  148. *
  149. * Descr.: Main-window message-processing
  150. *
  151. * Impl. : Create an instance of class EDITOR and send all menu-commands
  152. *          to it.
  153. *
  154. * APIs    : WinPostMsg                WinBeginPaint
  155. *          WinFillRect                WinEndPaint
  156. *          WinDefWindowProc
  157. *
  158. * Class.: class EDITOR    - create and manage MLEs
  159. *
  160. * Param.: HWND     hwnd     - window handle (client area)
  161. *          ULONG  msg       - message ID
  162. *          MPARAM mp1,mp2 - message parameters
  163. *
  164. * return: message result
  165. *
  166. *************************************************************************/
  167. MRESULT    EXPENTRY procTest(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)    {
  168.     HPS hps;
  169.     RECTL rcl;
  170.     static EDITOR *fm;
  171.  
  172.     switch(msg)    {
  173.  
  174.         /*
  175.         * client window creation
  176.         */
  177.         case WM_CREATE:
  178.             fm = new EDITOR;
  179.             break;
  180.  
  181.         /*
  182.         * repaint the client area
  183.         */
  184.         case WM_PAINT:
  185.             hps = WinBeginPaint(hwnd, (HPS)NULL, &rcl);
  186.             WinFillRect(hps, &rcl, CLR_BACKGROUND);
  187.             WinEndPaint(hps);
  188.             break;
  189.  
  190.         /*
  191.         * a menu was selected
  192.         */
  193.         case WM_COMMAND:
  194.  
  195.             switch(SHORT1FROMMP(mp1))    {
  196.                 case IDM_NEW:
  197.                     fm->NewFile();      // create an empty MLE
  198.                     break;
  199.  
  200.                 case IDM_OPEN:
  201.                     fm->OpenFile();        // create an MLE and load a file
  202.                     break;                // into it
  203.  
  204.                 case IDM_SAVE:
  205.                     fm->SaveFile();        // save the contents of the
  206.                     break;                // active MLE
  207.  
  208.                 case IDM_SAVEAS:        // save the contents of the
  209.                     fm->SaveFileAs();    // active MLE after asking for
  210.                     break;                // a new filename
  211.  
  212.                 case IDM_EXIT:
  213.                     WinPostMsg(hwnd, WM_CLOSE, MPVOID, MPVOID);
  214.                     break;
  215.  
  216.                 case IDM_CUT:            // cut, copy, paste and clear
  217.                     fm->Cut();            // do the appropriate actions
  218.                     break;                // on the marked area of the
  219.                 case IDM_COPY:            // active MLE.
  220.                     fm->Copy();
  221.                     break;
  222.                 case IDM_PASTE:
  223.                     fm->Paste();
  224.                     break;
  225.                 case IDM_CLEAR:
  226.                     fm->Clear();
  227.                     break;
  228.  
  229.                 case IDM_BLUEWHITE:        // change MLE colors
  230.                     fm->SetColor(CLR_DARKBLUE, CLR_WHITE);
  231.                     break;
  232.                 case IDM_WHITEBLUE:
  233.                     fm->SetColor(CLR_WHITE, CLR_DARKBLUE);
  234.                     break;
  235.                 default:
  236.                     break;
  237.                 }
  238.             break;
  239.  
  240.         case WM_CLOSE:                    // close all open MLEs (and ask
  241.             fm->CloseAllFiles();        // the user if its contents
  242.                                         // should be saved
  243.         default:
  244.             return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  245.         }
  246.     return((MRESULT)NULL);
  247.     }
  248.  
  249.  
  250.