home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap13 / cocosmo / cocosmo.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  15KB  |  680 lines

  1. /*
  2.  * COCOSMO.CPP
  3.  * Component Cosmo Chapter 13
  4.  *
  5.  * WinMain and CCosmoFrame implementations.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #define INITGUIDS
  16. #include "cocosmo.h"
  17.  
  18.  
  19. /*
  20.  * WinMain
  21.  *
  22.  * Purpose:
  23.  *  Main entry point of application.  Should register the app class
  24.  *  if a previous instance has not done so and do any other one-time
  25.  *  initializations.
  26.  */
  27.  
  28. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  29.     , LPSTR pszCmdLine, int nCmdShow)
  30.     {
  31.     PCCosmoFrame    pFR;
  32.     FRAMEINIT       fi;
  33.     WPARAM          wRet;
  34.  
  35.     //Attempt to allocate and initialize the application
  36.     pFR=new CCosmoFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  37.  
  38.     if (NULL==pFR)
  39.         return -1;
  40.  
  41.     fi.idsMin=IDS_FRAMEMIN;
  42.     fi.idsMax=IDS_FRAMEMAX;
  43.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  44.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  45.     fi.idStatMenuMin=ID_MENUFILE;
  46.     fi.idStatMenuMax=ID_MENUHELP;
  47.     fi.iPosWindowMenu=WINDOW_MENU;
  48.     fi.cMenus=CMENUS;
  49.  
  50.     fi.x=CW_USEDEFAULT;
  51.     fi.y=CW_USEDEFAULT;
  52.     fi.cx=440;
  53.     fi.cy=460;
  54.  
  55.     //If we can initialize pFR, start chugging messages
  56.     if (pFR->Init(&fi))
  57.         wRet=pFR->MessageLoop();
  58.  
  59.     delete pFR;
  60.     return wRet;
  61.     }
  62.  
  63.  
  64.  
  65.  
  66. /*
  67.  * CCosmoFrame::CCosmoFrame
  68.  * CCosmoFrame::~CCosmoFrame
  69.  *
  70.  * Constructor Parameters:
  71.  *  hInst           HINSTANCE from WinMain
  72.  *  hInstPrev       HINSTANCE from WinMain
  73.  *  pszCmdLine      LPSTR from WinMain
  74.  *  nCmdShow        int from WInMain
  75.  */
  76.  
  77. CCosmoFrame::CCosmoFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  78.     , LPSTR pszCmdLine, int nCmdShow)
  79.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  80.     {
  81.     UINT        i;
  82.  
  83.     for (i=0; i<5; i++)
  84.         m_hBmpLines[i]=NULL;
  85.  
  86.     m_uIDCurLine=0;
  87.     m_fInitialized=FALSE;
  88.     return;
  89.     }
  90.  
  91.  
  92. CCosmoFrame::~CCosmoFrame(void)
  93.     {
  94.     UINT        i;
  95.  
  96.     for (i=0; i<5; i++)
  97.         {
  98.         if (NULL!=m_hBmpLines[i])
  99.             DeleteObject(m_hBmpLines[i]);
  100.         }
  101.  
  102.     if (m_fInitialized)
  103.         OleUninitialize();
  104.  
  105.     return;
  106.     }
  107.  
  108.  
  109.  
  110.  
  111. /*
  112.  * CCosmoFrame::Init
  113.  *
  114.  * Purpose:
  115.  *  Call OleInitialize then calling down into the base class
  116.  *  initialization.
  117.  *
  118.  * Parameters:
  119.  *  pFI             PFRAMEINIT containing initialization
  120.  *                  parameters.
  121.  *
  122.  * Return Value:
  123.  *  BOOL            TRUE if initialization succeeded,
  124.  *                  FALSE otherwise.
  125.  */
  126.  
  127. BOOL CCosmoFrame::Init(PFRAMEINIT pFI)
  128.     {
  129.     CHECKVER_OLE;
  130.  
  131.     if (FAILED(OleInitialize(NULL)))
  132.         return FALSE;
  133.  
  134.     m_fInitialized=TRUE;
  135.  
  136.     return CFrame::Init(pFI);
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.  
  143. /*
  144.  * CCosmoFrame::CreateCClient
  145.  *
  146.  * Purpose:
  147.  *  Constructs a new client specific to the application.
  148.  *
  149.  * Parameters:
  150.  *  None
  151.  *
  152.  * Return Value:
  153.  *  PCClient        Pointer to the new client object.
  154.  */
  155.  
  156. PCClient CCosmoFrame::CreateCClient(void)
  157.     {
  158.     return (PCClient)(new CCosmoClient(m_hInst, this));
  159.     }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. /*
  169.  * CCosmoFrame::RegisterAllClasses
  170.  *
  171.  * Purpose:
  172.  *  Registers all classes used in this application.
  173.  *
  174.  * Parameters:
  175.  *  None
  176.  *
  177.  * Return Value:
  178.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  179.  */
  180.  
  181. BOOL CCosmoFrame::RegisterAllClasses(void)
  182.     {
  183.     WNDCLASS        wc;
  184.  
  185.     //First let the standard frame do its thing
  186.     if (!CFrame::RegisterAllClasses())
  187.         return FALSE;
  188.  
  189.     /*
  190.      * We want a different background color for the document
  191.      * because the Polyline we put in the document will paint
  192.      * with COLOR_WINDOW which by default which is CLASSLIB's
  193.      * default document color.
  194.      */
  195.  
  196.     GetClassInfo(m_hInst, SZCLASSDOCUMENT, &wc);
  197.     UnregisterClass(SZCLASSDOCUMENT, m_hInst);
  198.  
  199.     wc.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE+1);
  200.  
  201.     if (!RegisterClass(&wc))
  202.         return FALSE;
  203.  
  204.     //No need to register the Polyline window now...
  205.  
  206.     return TRUE;
  207.     }
  208.  
  209.  
  210.  
  211. /*
  212.  * CCosmoFrame::PreShowInit
  213.  *
  214.  * Purpose:
  215.  *  Called from Init before intially showing the window.  We do
  216.  *  whatever else we want here, modifying nCmdShow as necessary
  217.  *  which affects ShowWindow in Init.
  218.  *
  219.  * Parameters:
  220.  *  None
  221.  *
  222.  * Return Value:
  223.  *  BOOL            TRUE if this initialization succeeded,
  224.  *                  FALSE otherwise.
  225.  */
  226.  
  227. BOOL CCosmoFrame::PreShowInit(void)
  228.     {
  229.     CreateLineMenu();
  230.     CheckLineSelection(IDM_LINESOLID);
  231.     return TRUE;
  232.     }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. /*
  240.  * CCosmoFrame::CreateLineMenu
  241.  *
  242.  * Purpose:
  243.  *  Initializes the bitmaps used to create the Line menu and
  244.  *  replaces the text items defined in the application resources
  245.  *  with these bitmaps.  Note that the contents of m_hBmpLines
  246.  *  must be cleaned up when the application terminates.
  247.  *
  248.  * Parameters:
  249.  *  None
  250.  *
  251.  * Return Value:
  252.  *  None
  253.  */
  254.  
  255. void CCosmoFrame::CreateLineMenu(void)
  256.     {
  257.     HMENU       hMenu;
  258.     HDC         hDC, hMemDC;
  259.     HPEN        hPen;
  260.     HGDIOBJ     hObj;
  261.     TEXTMETRIC  tm;
  262.     UINT        i, cx, cy;
  263.  
  264.  
  265.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  266.     hDC=GetDC(m_hWnd);
  267.  
  268.     //Create each line in a menu item 8 chars wide, one char high.
  269.     GetTextMetrics(hDC, &tm);
  270.     cx=tm.tmAveCharWidth*8;
  271.     cy=tm.tmHeight;
  272.  
  273.     /*
  274.      * Create a memory DC in which to draw lines, and bitmaps
  275.      * for each line.
  276.      */
  277.     hMemDC=CreateCompatibleDC(hDC);
  278.     ReleaseDC(m_hWnd, hDC);
  279.  
  280.     for (i=0; i<5; i++)
  281.         {
  282.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  283.         SelectObject(hMemDC, m_hBmpLines[i]);
  284.  
  285.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  286.  
  287.         hPen=CreatePen(i, 1, 0L);       //i=line style like PS_SOLID
  288.         hObj=SelectObject(hMemDC, hPen);
  289.  
  290.         MoveToEx(hMemDC, 0, cy/2, NULL);
  291.         LineTo(hMemDC, cx, cy/2);
  292.  
  293.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  294.             , IDM_LINEMIN+i, (LPTSTR)(LONG)(UINT)m_hBmpLines[i]);
  295.  
  296.         SelectObject(hMemDC, hObj);
  297.         DeleteObject(hPen);
  298.         }
  299.  
  300.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  301.     DeleteDC(hMemDC);
  302.  
  303.     return;
  304.     }
  305.  
  306.  
  307.  
  308.  
  309. /*
  310.  * CCosmoFrame::CreateToolbar
  311.  *
  312.  * Purpose:
  313.  *  Procedure to create all the necessary toolbar buttons.
  314.  *
  315.  * Parameters:
  316.  *  None
  317.  *
  318.  * Return Value:
  319.  *  UINT            Number of tools added to the bar.
  320.  */
  321.  
  322. UINT CCosmoFrame::CreateToolbar(void)
  323.     {
  324.     UINT            iLast;
  325.     UINT            uState=GIZMO_NORMAL;
  326.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  327.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  328.  
  329.     //Insert the standard ones.
  330.     iLast=CFrame::CreateToolbar();
  331.  
  332.     /*
  333.      * Insert File Import in the 5th position and account for
  334.      * it in iLast.
  335.      */
  336.     m_pTB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB
  337.         , NULL, m_hBmp, 2, uState);
  338.     iLast++;
  339.  
  340.     //Separator
  341.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  342.         , NULL, NULL, 0, uState);
  343.  
  344.     /*
  345.      * For the Background bitmap, preserve our use of black
  346.      * (part of the image)
  347.      */
  348.     m_pTB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB
  349.         , NULL, m_hBmp, 3, GIZMO_NORMAL | PRESERVE_BLACK);
  350.  
  351.     m_pTB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB
  352.         , NULL, m_hBmp, 4, uState);
  353.  
  354.     //Separator
  355.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  356.         , NULL, NULL, 0, uState);
  357.  
  358.     //Line styles.
  359.     m_pTB->Add(utEx, iLast++, IDM_LINESOLID, m_dxB, m_dyB
  360.         , NULL, m_hBmp, 5, uState);
  361.     m_pTB->Add(utEx, iLast++, IDM_LINEDASH, m_dxB, m_dyB
  362.         , NULL, m_hBmp, 6, uState);
  363.     m_pTB->Add(utEx, iLast++, IDM_LINEDOT, m_dxB, m_dyB
  364.         , NULL, m_hBmp, 7, uState);
  365.     m_pTB->Add(utEx, iLast++, IDM_LINEDASHDOT, m_dxB, m_dyB
  366.         , NULL, m_hBmp, 8, uState);
  367.     m_pTB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB
  368.         , NULL, m_hBmp, 9, uState);
  369.  
  370.     return iLast;
  371.     }
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380. /*
  381.  * CCosmoFrame::OnCommand
  382.  *
  383.  * Purpose:
  384.  *  WM_COMMAND handler for the Cosmo frame window that just
  385.  *  processes the line menu and the color menu leaving the
  386.  *  CFrame to do everything else.
  387.  *
  388.  * Parameters:
  389.  *  hWnd            HWND of the frame window.
  390.  *  wParam          WPARAM of the message.
  391.  *  lParam          LPARAM of the message.
  392.  *
  393.  * Return Value:
  394.  *  LRESULT         Return value for the message.
  395.  */
  396.  
  397. LRESULT CCosmoFrame::OnCommand(HWND hWnd, WPARAM wParam
  398.     , LPARAM lParam)
  399.     {
  400.     PCCosmoDoc      pDoc;
  401.     TCHAR           szFile[CCHPATHMAX];
  402.     BOOL            fOK;
  403.     UINT            i, uTemp;
  404.     COLORREF        rgColors[16];
  405.     CHOOSECOLOR     cc;
  406.  
  407.     COMMANDPARAMS(wID, wCode, hWndMsg);
  408.  
  409.     /*
  410.      * Don't bother with anything during first initialization,
  411.      * skipping many toolbar notifications.
  412.      */
  413.     if (m_fInit)
  414.         return 0L;
  415.  
  416.     pDoc=(PCCosmoDoc)m_pCL->ActiveDocument();
  417.  
  418.     /*
  419.      * Check for the line style commands which are
  420.      * IDM_LINEMIN+<style>.  We handle this by changing the menu
  421.      * and toolbar, then we pass it to the document for real
  422.      * processing.
  423.      */
  424.     if (NULL!=pDoc && IDM_LINEMIN <= wID && IDM_LINEMAX >=wID)
  425.         {
  426.         CheckLineSelection(wID);
  427.         pDoc->LineStyleSet(wID-IDM_LINEMIN);
  428.         return 0L;
  429.         }
  430.  
  431.     switch (wID)
  432.         {
  433.         case IDM_FILEIMPORT:
  434.             szFile[0]=0;
  435.             fOK=SaveOpenDialog(szFile, CCHPATHMAX, IDS_FILEIMPORT
  436.                 , TRUE, &i);
  437.  
  438.             if (fOK)
  439.                 {
  440.                 uTemp=pDoc->Load(FALSE, szFile);
  441.                 pDoc->ErrorMessage(uTemp);
  442.                 }
  443.  
  444.             return (LRESULT)fOK;
  445.  
  446.  
  447.         case IDM_COLORBACKGROUND:
  448.         case IDM_COLORLINE:
  449.             //Invoke the color chooser for either color
  450.             uTemp=(IDM_COLORBACKGROUND==wID)
  451.                 ? DOCCOLOR_BACKGROUND : DOCCOLOR_LINE;
  452.  
  453.             for (i=0; i<16; i++)
  454.                 rgColors[i]=RGB(0, 0, i*16);
  455.  
  456.             memset(&cc, 0, sizeof(CHOOSECOLOR));
  457.             cc.lStructSize=sizeof(CHOOSECOLOR);
  458.             cc.lpCustColors=rgColors;
  459.             cc.hwndOwner=hWnd;
  460.             cc.Flags=CC_RGBINIT;
  461.             cc.rgbResult=pDoc->ColorGet(uTemp);
  462.  
  463.             if (ChooseColor(&cc))
  464.                 pDoc->ColorSet(uTemp, cc.rgbResult);
  465.  
  466.             break;
  467.  
  468.  
  469.         default:
  470.            CFrame::OnCommand(hWnd, wParam, lParam);
  471.         }
  472.  
  473.     return 0L;
  474.     }
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481. /*
  482.  * CCosmoFrame::OnDocumentDataChange
  483.  *
  484.  * Purpose:
  485.  *  Update the Line menu and toolbar if the style in the data
  486.  *  changes.
  487.  *
  488.  * Parameters:
  489.  *  pDoc            PCDocument notifying the sink.
  490.  *
  491.  * Return Value:
  492.  *  None
  493.  */
  494.  
  495. void CCosmoFrame::OnDocumentDataChange(PCDocument pDoc)
  496.     {
  497.     CheckLineSelection(IDM_LINEMIN
  498.         +((PCCosmoDoc)pDoc)->LineStyleGet());
  499.     return;
  500.     }
  501.  
  502.  
  503.  
  504.  
  505. /*
  506.  * CCosmoFrame::OnDocumentActivate
  507.  *
  508.  * Purpose:
  509.  *  Informs us that document activation changed, so update the UI
  510.  *  for that new document.
  511.  *
  512.  * Parameters:
  513.  *  pDoc            PCDocument notifying the sink.
  514.  *
  515.  * Return Value:
  516.  *  None
  517.  */
  518.  
  519. void CCosmoFrame::OnDocumentActivate(PCDocument pDoc)
  520.     {
  521.     CheckLineSelection(IDM_LINEMIN
  522.         +((PCCosmoDoc)pDoc)->LineStyleGet());
  523.     return;
  524.     }
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. /*
  533.  * CCosmoFrame::UpdateMenus
  534.  *
  535.  * Purpose:
  536.  *  Handles the WM_INITMENU message for the frame window.  Depending
  537.  *  on the existence of an active window, menu items are selectively
  538.  *  enabled and disabled.
  539.  *
  540.  * Parameters:
  541.  *  hMenu           HMENU of the menu to intialize
  542.  *  iMenu           UINT position of the menu.
  543.  *
  544.  * Return Value:
  545.  *  None
  546.  */
  547.  
  548. void CCosmoFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  549.     {
  550.     PCDocument  pDoc;
  551.     BOOL        fOK=FALSE;
  552.     BOOL        fCallDefault=TRUE;
  553.     UINT        i;
  554.     UINT        uTemp;
  555.     UINT        uTempE;
  556.     UINT        uTempD;
  557.  
  558.     pDoc=m_pCL->ActiveDocument();
  559.  
  560.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  561.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  562.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  563.  
  564.     //File menu:  If there is document window, disable Import.
  565.     if (m_phMenu[0]==hMenu)
  566.         EnableMenuItem(hMenu, IDM_FILEIMPORT, uTemp);
  567.  
  568.     //Color menu:  no document, no commands
  569.     if (m_phMenu[2]==hMenu)
  570.         {
  571.         EnableMenuItem(hMenu, IDM_COLORBACKGROUND, uTemp);
  572.         EnableMenuItem(hMenu, IDM_COLORLINE,       uTemp);
  573.         fCallDefault=FALSE;
  574.         }
  575.  
  576.     //Line menu:  no document, no commands
  577.     if (m_phMenu[3]==hMenu)
  578.         {
  579.         for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  580.             EnableMenuItem(hMenu, i, uTemp);
  581.  
  582.         fCallDefault=FALSE;
  583.         }
  584.  
  585.     if (fCallDefault)
  586.         CFrame::UpdateMenus(hMenu, iMenu);
  587.  
  588.     return;
  589.     }
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596. /*
  597.  * CCosmoFrame::UpdateToolbar
  598.  *
  599.  * Purpose:
  600.  *  Enables and disables tools depending on whether we have
  601.  *  a document or not.
  602.  *
  603.  * Parameters:
  604.  *  None
  605.  *
  606.  * Return Value:
  607.  *  None
  608.  */
  609.  
  610. void CCosmoFrame::UpdateToolbar(void)
  611.     {
  612.     BOOL        fLast;
  613.     UINT        i;
  614.  
  615.     //Save the last enabled state before CFrame changes it
  616.     fLast=m_fLastEnable;
  617.  
  618.     //Let the default hack on its tools
  619.     CFrame::UpdateToolbar();
  620.  
  621.     /*
  622.      * If CFrame::UpdateToolbar changed anything, then we need
  623.      * to change as well--if nothing changes, nothing to do.
  624.      */
  625.     if (fLast!=m_fLastEnable)
  626.         {
  627.         m_pTB->Enable(IDM_FILEIMPORT, m_fLastEnable);
  628.  
  629.         m_pTB->Enable(IDM_COLORBACKGROUND, m_fLastEnable);
  630.         m_pTB->Enable(IDM_COLORLINE,       m_fLastEnable);
  631.  
  632.         for (i=IDM_LINEMIN; i <= IDM_LINEMAX; i++)
  633.             m_pTB->Enable(i, m_fLastEnable);
  634.         }
  635.  
  636.     return;
  637.     }
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644. /*
  645.  * CCosmoFrame::CheckLineSelection
  646.  *
  647.  * Purpose:
  648.  *  Maintains the bitmap menu and the tools for the line selection.
  649.  *  Both are mutially exclusive option lists where a selection in
  650.  *  one has to affect the other.
  651.  *
  652.  * Parameters:
  653.  *  uID             UINT ID of the item to be selected
  654.  *
  655.  * Return Value:
  656.  *  None
  657.  */
  658.  
  659. void CCosmoFrame::CheckLineSelection(UINT uID)
  660.     {
  661.     UINT        i;
  662.     HMENU       hMenu;
  663.  
  664.     //Update menus and tools if the selection changed.
  665.     if (uID!=m_uIDCurLine)
  666.         {
  667.         m_uIDCurLine=uID;
  668.         hMenu=GetMenu(m_hWnd);
  669.  
  670.         //Uncheck all lines initially.
  671.         for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  672.             CheckMenuItem(hMenu, i, MF_UNCHECKED | MF_BYCOMMAND);
  673.  
  674.         CheckMenuItem(hMenu, uID, MF_CHECKED | MF_BYCOMMAND);
  675.         m_pTB->Check(uID, TRUE);
  676.         }
  677.  
  678.     return;
  679.     }
  680.