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 / chap08 / cocosmo / cocosmo.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  15KB  |  679 lines

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