home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / wksinst / rwcdemo.pak / RWCDEMO.CPP < prev    next >
C/C++ Source or Header  |  1991-09-09  |  15KB  |  570 lines

  1. // (C) Copyright 1991 by Borland International
  2.  
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #if defined( BWCC)
  7. #include <bwcc.h>
  8. #else
  9. #define IDHELP 210
  10. #endif
  11. #include "rwcdemo.h"
  12. #include "rwcdlg.h"
  13.  
  14. #define TextStart 200
  15.  
  16.  
  17. static char szNotImplemented[] = "Feature Not Implemented";
  18. static char szDemoApp[] = "RWCDemo";
  19. HMENU    hMenuInit;
  20.  
  21.  
  22. HBITMAP bmpStatusLine;
  23. HBITMAP bmpStatusBar;
  24.  
  25. HANDLE    WinApp::hInstance;
  26. HANDLE    WinApp::hPrevInst;
  27. WORD    WinApp::cmdShow;
  28. HANDLE    WinApp::hWndClient;
  29. TMDIChildWindow *WinApp::currentWindow;
  30. WORD    wm_DrawStatusLine;
  31.  
  32. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInst, LPSTR, int cmdShow)
  33. {
  34.     HANDLE    hAccel;
  35.     HWND    hwndFrame;
  36.     MSG    msg;
  37.     WNDCLASS    wndClass;
  38.  
  39.     WinApp::hInstance = hInstance;
  40.     WinApp::hPrevInst = hPrevInst;
  41.     WinApp::cmdShow = cmdShow;
  42.     WinApp::currentWindow = NULL;
  43.  
  44.     if (!WinApp::hPrevInst)
  45.     {
  46.         wndClass.style        = CS_HREDRAW | CS_VREDRAW;
  47.         wndClass.lpfnWndProc    = FrameWndProc;
  48.         wndClass.cbClsExtra     = 0;
  49.         wndClass.cbWndExtra    = 0;
  50.         wndClass.hInstance    = hInstance;
  51.         wndClass.hIcon        = LoadIcon(WinApp::hInstance, MAKEINTRESOURCE(ico_RWCDemo));
  52.         wndClass.hCursor    = LoadCursor(NULL, IDC_ARROW);
  53.         wndClass.hbrBackground    = COLOR_APPWORKSPACE + 1;
  54.         wndClass.lpszMenuName     = NULL;
  55.         wndClass.lpszClassName    = "MDIFrame";
  56.  
  57.         RegisterClass(&wndClass);
  58.  
  59.     }
  60.  
  61.     hMenuInit    = LoadMenu(WinApp::hInstance, MAKEINTRESOURCE(100));
  62.     hAccel     = LoadAccelerators(WinApp::hInstance, MAKEINTRESOURCE(acc_Main));
  63.     wm_DrawStatusLine = RegisterWindowMessage("RWCDrawStatusLine");
  64.     hwndFrame = CreateWindow("MDIFrame", "RWCDemo",
  65.             WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  66.             CW_USEDEFAULT, CW_USEDEFAULT,
  67.             CW_USEDEFAULT, CW_USEDEFAULT,
  68.             NULL, hMenuInit, hInstance, NULL);
  69.  
  70.     WinApp::hWndClient = GetWindow(hwndFrame, GW_CHILD);
  71.  
  72.     ShowWindow(hwndFrame, WinApp::cmdShow);
  73.     UpdateWindow(hwndFrame);
  74.  
  75.     while (GetMessage(&msg, NULL, 0, 0))
  76.         if (!TranslateMDISysAccel(WinApp::hWndClient, &msg) &&
  77.             !TranslateAccelerator(hwndFrame, hAccel, &msg))
  78.         {
  79.             TranslateMessage(&msg);
  80.             DispatchMessage(&msg);
  81.         }
  82.     return msg.wParam;
  83. }
  84.  
  85. void NewWindow(TMDIChildWindow* window)
  86. {
  87.     MDICREATESTRUCT mdiCreate;
  88.  
  89.     mdiCreate.szClass = window->className();
  90.     mdiCreate.szTitle = window->title;
  91.     mdiCreate.hOwner = WinApp::hInstance;
  92.     mdiCreate.x = CW_USEDEFAULT;
  93.     mdiCreate.y = CW_USEDEFAULT;
  94.     mdiCreate.cx = CW_USEDEFAULT;
  95.     mdiCreate.cy = CW_USEDEFAULT;
  96.     mdiCreate.style = 0;
  97.     mdiCreate.lParam = NULL;
  98.     WinApp::currentWindow = window;
  99.  
  100.     SendMessage(WinApp::hWndClient, WM_MDICREATE, 0,
  101.         (LONG) (LPMDICREATESTRUCT) &mdiCreate);
  102. }
  103.  
  104. int ExecuteDialog(HWND hWindow, TDialog *d)
  105. {
  106.   return DialogBoxParam(WinApp::hInstance, d->name, hWindow, d->dialogFunc, (LONG) d);
  107. }
  108.  
  109. int DoCommands(HWND hWindow, WORD wParam, LONG lParam)
  110. {
  111.     TDialog *d;
  112.     int FileType;
  113.  
  114.     switch(wParam)
  115.     {
  116.         case cm_Exit:
  117.             //Iterate through all child windows.  If still can close then
  118.             PostQuitMessage(0);
  119.             return 0;
  120.         case cm_Open:
  121.             d = new TFileOpen(&FileType, "*.*");
  122.             if ( ExecuteDialog(hWindow, d) == IDOK)
  123.             {
  124. #if defined (BWCC)
  125.                 BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  126. #else
  127.                 MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  128. #endif
  129.             }
  130.             delete d;
  131.             return 0;
  132.  
  133.         case cm_New:
  134.             d = new TFileNew(&FileType);
  135.             if (ExecuteDialog(hWindow, d) == IDOK)
  136.                 switch(FileType)
  137.                 {
  138.                     case ScribbleWindow:
  139.                         NewWindow(new TScribbleWindow("Untitled"));
  140.                         break;
  141.                     case GraphWindow:
  142.                         NewWindow(new TShapeWindow("Untitled"));
  143.                         break;
  144.                     default:
  145.                         NewWindow(new TEditWindow("Untitled"));
  146.                         break;
  147.  
  148.                 }
  149.             delete d;
  150.             return 0;
  151.         case cm_SaveAs:
  152.             d = new TDialog(dlg_SaveAs);
  153.  
  154.             if ( ExecuteDialog(hWindow, d) == IDOK)
  155.             {
  156. #if defined (BWCC)
  157.                 BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  158. #else
  159.                 MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  160. #endif
  161.             }
  162.             delete d;
  163.             return 0;
  164.  
  165.         case cm_Print:
  166.             d = new TDialog(dlg_Print);
  167.             ExecuteDialog(hWindow, d);
  168.             delete d;
  169. #if defined (BWCC)
  170.             BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  171. #else
  172.             MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  173. #endif
  174.             return 0;
  175.  
  176.         case cm_About_CUA:
  177.             d = new TDialog(dlg_About);
  178.             ExecuteDialog(hWindow, d);
  179.             return 0;
  180.  
  181.         case cm_Directories:
  182.             d = new TDialog(dlg_Directories);
  183.             ExecuteDialog(hWindow, d);
  184. #if defined (BWCC)
  185.             BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  186. #else
  187.             MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  188. #endif
  189.             return 0;
  190.  
  191.         case cm_Preferences:
  192.             d = new TDialog(dlg_Preferences);
  193.             ExecuteDialog(hWindow, d);
  194. #if defined (BWCC)
  195.             BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  196. #else
  197.             MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  198. #endif
  199.             return 0;
  200.  
  201.         case cm_Mouse:
  202.             d = new TDialog(dlg_Mouse);
  203.             ExecuteDialog(hWindow, d);
  204. #if defined (BWCC)
  205.             BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  206. #else
  207.             MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  208. #endif
  209.             return 0;
  210.         case cm_Options_Open:
  211.             d = new TDialog(dlg_Options_Open);
  212.             ExecuteDialog(hWindow, d);
  213.             return 0;
  214.  
  215.         case cm_Options_Save:
  216. #if defined (BWCC)
  217.             BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  218. #else
  219.             MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  220. #endif
  221.             return 0;
  222.  
  223.         case cm_Options_SaveAs:
  224.             d = new TDialog(dlg_Options_SaveAs);
  225.             ExecuteDialog(hWindow, d);
  226. #if defined (BWCC)
  227.             BWCCMessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  228. #else
  229.             MessageBox(GetFocus(), szNotImplemented, szDemoApp, MB_OK);
  230. #endif
  231.             return 0;
  232.  
  233.         case cm_Tile:
  234.           SendMessage(WinApp::hWndClient, WM_MDITILE, 0, 0L);
  235.           return 0;
  236.         case cm_Cascade:
  237.           SendMessage(WinApp::hWndClient, WM_MDICASCADE, 0, 0L);
  238.           return 0;
  239.         case cm_ArrangeIcons:
  240.           SendMessage(WinApp::hWndClient, WM_MDIICONARRANGE, 0, 0L);
  241.           return 0;
  242.  
  243.         default:
  244.             HWND hChildWnd = LOWORD(SendMessage(WinApp::hWndClient,
  245.                 WM_MDIGETACTIVE, 0, NULL));
  246.  
  247.             if (IsWindow(hChildWnd))
  248.                 SendMessage(hChildWnd, WM_COMMAND, wParam, lParam);
  249.             break;
  250.     }
  251. }
  252.  
  253. void DoSize(HWND hWindow)
  254. {
  255.     RECT rect;
  256.     HDC DC, srcDC, destDC;
  257.     HBITMAP bmp;
  258.     HANDLE oldSrc, oldDest;
  259.  
  260.     GetClientRect(hWindow, &rect);
  261.     MoveWindow(WinApp::hWndClient, 0, 0, rect.right, rect.bottom - 20, 1);
  262.     InvalidateRect(hWindow, NULL, 1);
  263.     DC = GetDC(hWindow);
  264.     srcDC = CreateCompatibleDC(DC);
  265.     destDC = CreateCompatibleDC(DC);
  266.     ReleaseDC(hWindow, DC);
  267.  
  268.     bmp = LoadBitmap(WinApp::hInstance, MAKEINTRESOURCE(bmp_StatusLine));
  269.     oldSrc = SelectObject(srcDC, bmp);
  270.     if (bmpStatusLine)
  271.       DeleteObject(bmpStatusLine);
  272.     bmpStatusLine = CreateCompatibleBitmap(DC, rect.right, 20);
  273.     oldDest = SelectObject(destDC, bmpStatusLine);
  274.     BitBlt(destDC, 0, 0, 5, 20, srcDC, 0, 0, SRCCOPY);
  275.     StretchBlt(destDC, 5, 0, rect.right - 5, 20,
  276.                srcDC, 6, 0, 20, 20, SRCCOPY);
  277.     BitBlt(destDC, rect.right - 5, 0, 5, 20, srcDC, 59, 0, SRCCOPY);
  278.     SelectObject(srcDC, oldSrc);
  279.     SelectObject(destDC, oldDest);
  280.     DeleteDC(srcDC);
  281.     DeleteDC(destDC);
  282.     DeleteObject(bmp);
  283. }
  284.  
  285. void BlastStatusLine(HWND hWindow, HDC PaintDC)
  286. {
  287.     HDC memDC;
  288.     RECT clientRect;
  289.     HANDLE oldBmp;
  290.  
  291.     GetClientRect(hWindow, &clientRect);
  292.     memDC = CreateCompatibleDC(PaintDC);
  293.     oldBmp = SelectObject(memDC, bmpStatusLine);
  294.     BitBlt(PaintDC, 0, clientRect.bottom - 20,
  295.         clientRect.right - clientRect.left, 20,
  296.         memDC, 0, 0, SRCCOPY);
  297.     SelectObject(memDC, bmpStatusBar);
  298.     BitBlt(PaintDC, 40, clientRect.bottom - 20,
  299.                     10, 20, memDC, 0, 0, SRCCOPY);
  300.     BitBlt(PaintDC, 100, clientRect.bottom - 20,
  301.                     10, 20, memDC, 0, 0, SRCCOPY);
  302.     BitBlt(PaintDC, TextStart, clientRect.bottom - 20,
  303.                     10, 20, memDC, 0, 0, SRCCOPY);
  304.     SelectObject(memDC, oldBmp);
  305.     DeleteDC(memDC);
  306. }
  307.  
  308. void DoPaint(HWND hWindow)
  309. {
  310.     HDC PaintDC;
  311.     PAINTSTRUCT PS;
  312.  
  313.     PaintDC = BeginPaint(hWindow, &PS);
  314.     BlastStatusLine(hWindow, PaintDC);
  315.     EndPaint(hWindow, &PS);
  316. }
  317.  
  318. #define WindowMenuPos 4
  319. #define EditFirst                 cm_Undo
  320. #define EnvironmentFirst          cm_Preferences
  321. #define FileFirst                 cm_New
  322. #define HelpFirst                 cm_Index
  323. #define OptionFirst               cm_Directories
  324. #define ViewFirst                 cm_All
  325. #define WindowFirst               cm_Tile
  326. #define StatusLineHeight    20
  327. int CurrentID;
  328. int CurrentPopup;
  329.  
  330. void drawStatusLine(HWND hWindow)
  331. {
  332.   HDC DC;
  333.   RECT Rect;
  334.   char Str[128];
  335.   int StrID;
  336.  
  337.   if (CurrentID)
  338.   {
  339.     switch(CurrentID)
  340.     {
  341.       case cm_New: StrID = sth_FileNew; break;
  342.       case cm_Open: StrID = sth_FileOpen; break;
  343.       case cm_Save: StrID = sth_FileSave; break;
  344.       case cm_SaveAs: StrID = sth_FileSaveAs; break;
  345.       case cm_Print: StrID = sth_FilePrint; break;
  346.       case cm_Exit: StrID = sth_FileExit; break;
  347.       case cm_Undo: StrID = sth_EditUndo; break;
  348.       case cm_Cut: StrID = sth_EditCut; break;
  349.       case cm_Copy: StrID = sth_EditCopy; break;
  350.       case cm_Paste: StrID = sth_EditPaste; break;
  351.       case cm_Delete: StrID = sth_EditDelete; break;
  352.       case cm_Clear: StrID = sth_EditClear; break;
  353.       case cm_Options_Open: StrID = sth_OptionsOpen; break;
  354.       case cm_All: StrID = sth_ViewAll; break;
  355.       case cm_By: StrID = sth_ViewBy; break;
  356.       case cm_Some: StrID = sth_ViewSome; break;
  357.       case cm_Directories: StrID = sth_OptionsDirectory; break;
  358.       case cm_Options_Save: StrID = sth_OptionsSave; break;
  359.       case cm_Options_SaveAs: StrID = sth_OptionsSaveAs; break;
  360.       case cm_Preferences: StrID = sth_EnvironmentPreferences; break;
  361.       case cm_Mouse: StrID = sth_EnvironmentMouse; break;
  362.       case cm_Tile: StrID = sth_WindowTile; break;
  363.       case cm_Cascade: StrID = sth_WindowCascade; break;
  364.       case cm_ArrangeIcons: StrID = sth_WindowArrange; break;
  365.       case cm_CloseAll: StrID = sth_WindowCloseAll; break;
  366.       case cm_Index: StrID = sth_HelpIndex; break;
  367.       case cm_Topic_Search: StrID = sth_HelpTopic; break;
  368.       case cm_Glossary: StrID = sth_HelpGlossary; break;
  369.       case cm_Using_Help: StrID = sth_HelpUsing; break;
  370.       case cm_About_CUA: StrID = sth_HelpAbout; break;
  371.       default:
  372.         return;
  373.     }
  374.   }
  375.   else
  376.   if (CurrentPopup)
  377.   {
  378.     switch(GetMenuItemID(CurrentPopup, 0))
  379.     {
  380.       case FileFirst: StrID = sth_File; break;
  381.       case EditFirst: StrID = sth_Edit; break;
  382.       case ViewFirst: StrID = sth_View; break;
  383.       case WindowFirst: StrID = sth_Window; break;
  384.       case OptionFirst: StrID = sth_Option; break;
  385.       case EnvironmentFirst: StrID = sth_OptionsEnvironment; break;
  386.       case HelpFirst: StrID = sth_Help; break;
  387.       default: return;
  388.     }
  389.   }
  390.  
  391.   DC = GetDC(hWindow);
  392.   BlastStatusLine(hWindow, DC);
  393.   if ((CurrentPopup) || (CurrentID != 0))
  394.   {
  395.         LOGFONT lf;
  396.     HFONT   hSmall, hOld;
  397.     int    TextHeight;
  398.  
  399.     // get a slightly smaller font
  400.  
  401.     hOld = GetStockObject( ANSI_VAR_FONT);
  402.     if ( hOld)
  403.     {
  404.         GetObject( hOld, sizeof( LOGFONT), (LPSTR) &lf);
  405.         if ( lf.lfHeight > 3)
  406.           lf.lfHeight -= 3;
  407.         lf.lfWidth = 0;
  408.         hSmall = CreateFontIndirect( &lf);
  409.     }
  410.     else
  411.         hSmall = 0;
  412.     if ( hSmall)
  413.         hOld = SelectObject( DC, hSmall);
  414.     else
  415.         hOld = 0;
  416.  
  417.     LoadString(WinApp::hInstance, StrID, Str, sizeof(Str));
  418.     GetClientRect(hWindow, &Rect);
  419.     SetBkColor(DC, RGB(192, 192, 192));
  420.     TextHeight = (int) HIWORD( GetTextExtent( DC, Str, 1) );
  421.     TextOut(DC, TextStart+10,
  422.       Rect.bottom - StatusLineHeight + ( ( StatusLineHeight - TextHeight ) / 2),
  423.       Str, strlen(Str));
  424.     if ( hOld)
  425.         SelectObject( DC, hOld);
  426.     if ( hSmall)
  427.         DeleteObject( hSmall);
  428.   }
  429.   ReleaseDC(hWindow, DC);
  430. }
  431.  
  432. void menuSelect(HWND hWindow, WORD wParam, LONG lParam)
  433. {
  434.   HWND CurrentMenu;
  435.   char Str[20];
  436.  
  437.   if (LOWORD(lParam) == 0x0FFFF)
  438.   {
  439.     CurrentPopup = 0;
  440.     CurrentID = 0;
  441.   }
  442.   else
  443.   if (LOWORD(lParam) & MF_POPUP)
  444.   {
  445.     CurrentPopup = wParam;
  446.     CurrentID = 0;
  447.   }
  448.   else
  449.     CurrentID = wParam;
  450.   PostMessage(hWindow,wm_DrawStatusLine, 0, 0);
  451. }
  452.  
  453. long FAR PASCAL _export FrameWndProc(HWND hWindow, WORD message, WORD wParam, LONG lParam)
  454. {
  455.     int rc;
  456.  
  457.     switch(message)
  458.     {
  459.         case WM_CREATE:
  460.             CLIENTCREATESTRUCT clientCreate;
  461.             clientCreate.idFirstChild = 2000;
  462.             clientCreate.hWindowMenu = GetSubMenu(hMenuInit, WindowMenuPos);
  463.             WinApp::hWndClient = CreateWindow("MDICLIENT", NULL,
  464.                 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
  465.                 0, 0, 0, 0, hWindow, 1, WinApp::hInstance,
  466.                 (LPSTR) &clientCreate);
  467.             bmpStatusBar = LoadBitmap(WinApp::hInstance, MAKEINTRESOURCE(bmp_StatusBar));
  468.             return 0;
  469.         case WM_SIZE:
  470.             DoSize(hWindow);
  471.             return 0;
  472.         case WM_PAINT:
  473.             DoPaint(hWindow);
  474.             return 0;
  475.         case WM_COMMAND:
  476.             rc = DoCommands(hWindow, wParam, lParam);
  477.             if (rc)
  478.               return rc;
  479.             break;
  480.         case WM_DESTROY:
  481.             if (bmpStatusLine)
  482.                 DeleteObject(bmpStatusLine);
  483.             DeleteObject(bmpStatusBar);
  484.             PostQuitMessage(0);
  485.             return 0;
  486.  
  487.         case WM_MENUSELECT:
  488.             menuSelect(hWindow, wParam, lParam);
  489.             return 0;
  490.  
  491.         case WM_ENTERIDLE:
  492.  
  493.             // handle F1 key for dialog box help
  494.  
  495.             if
  496.             (
  497.               ( wParam == MSGF_DIALOGBOX) &&
  498.               ( GetKeyState( VK_F1) &0x8000 )
  499.             )
  500.               PostMessage( LOWORD( lParam), WM_COMMAND,
  501.                 IDHELP, 0);
  502.             return 0;
  503.  
  504.         default:
  505.             if (message == wm_DrawStatusLine)
  506.             {
  507.                 drawStatusLine(hWindow);
  508.                 return 0;
  509.             }
  510.             else
  511.                 return DefFrameProc(hWindow, WinApp::hWndClient,
  512.                     message, wParam, lParam);
  513.     }
  514. }
  515.  
  516. long FAR PASCAL _export WndProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
  517. {
  518.     TMDIChildWindow *window = (TMDIChildWindow *) GetWindowWord(hwnd, 0);
  519.  
  520.     switch(message)
  521.     {
  522.         case WM_CREATE:
  523.             if (WinApp::currentWindow != 0)
  524.             {
  525.                 SetWindowWord(hwnd, 0, (WORD) WinApp::currentWindow);
  526.                 window = WinApp::currentWindow;
  527.                 WinApp::currentWindow = NULL;
  528.                 window->hWindow = hwnd;
  529.                 window->create();
  530.                 return 0;
  531.             }
  532.             break;
  533.         case WM_PAINT:
  534.             HDC hdc;
  535.             PAINTSTRUCT ps;
  536.  
  537.             hdc = BeginPaint(hwnd, &ps);
  538.             window->paint(hdc, &ps);
  539.             EndPaint(hwnd, &ps);
  540.             return 0;
  541.         case WM_LBUTTONDOWN:
  542.             window->lButtonDown(wParam, lParam);
  543.             return 0;
  544.  
  545.         case WM_LBUTTONUP:
  546.             window->lButtonUp(wParam, lParam);
  547.             return 0;
  548.  
  549.         case WM_MOUSEMOVE:
  550.             window->mouseMove(wParam, lParam);
  551.             return 0;
  552.         case WM_RBUTTONDOWN:
  553.             window->rButtonDown(lParam);
  554.             return 0;
  555.         case WM_SIZE:
  556.             window->size(wParam, lParam);
  557.             return 0;
  558.  
  559.         case WM_SETFOCUS:
  560.             window->setFocus();
  561.             return 0;
  562.         case WM_DESTROY:
  563.             delete window;
  564.  
  565.     }
  566.     return DefMDIChildProc(hwnd, message, wParam, lParam);
  567. }
  568.  
  569.  
  570.