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 / chap14 / cosmo / cosmo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-22  |  21.0 KB  |  913 lines

  1. /*
  2.  * COSMO.CPP
  3.  * Cosmo Chapter 14
  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 "cosmo.h"
  17.  
  18. //CHAPTER14MOD
  19. /*
  20.  * These are for proper implementation of the class factory,
  21.  * automation support, and shutdown conditions.
  22.  */
  23. ULONG g_cObj=0;
  24. ULONG g_cLock=0;
  25. HWND  g_hWnd=NULL;
  26. BOOL  g_fUser=FALSE;
  27. //End CHAPTER14MOD
  28.  
  29.  
  30. /*
  31.  * WinMain
  32.  *
  33.  * Purpose:
  34.  *  Main entry point of application.  Should register the app class
  35.  *  if a previous instance has not done so and do any other one-time
  36.  *  initializations.
  37.  */
  38.  
  39. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  40.     , LPSTR pszCmdLine, int nCmdShow)
  41.     {
  42.     PCCosmoFrame    pFR;
  43.     FRAMEINIT       fi;
  44.     WPARAM          wRet;
  45.  
  46.     SETMESSAGEQUEUE;
  47.  
  48.     //Attempt to allocate and initialize the application
  49.     pFR=new CCosmoFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  50.  
  51.     if (NULL==pFR)
  52.         return -1;
  53.  
  54.     fi.idsMin=IDS_FRAMEMIN;
  55.     fi.idsMax=IDS_FRAMEMAX;
  56.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  57.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  58.     fi.idStatMenuMin=ID_MENUFILE;
  59.     fi.idStatMenuMax=ID_MENUHELP;
  60.     fi.iPosWindowMenu=WINDOW_MENU;
  61.     fi.cMenus=CMENUS;
  62.  
  63.     fi.x=CW_USEDEFAULT;
  64.     fi.y=CW_USEDEFAULT;
  65.     fi.cx=440;
  66.     fi.cy=460;
  67.  
  68.     //If we can initialize pFR, start chugging messages
  69.     if (pFR->Init(&fi))
  70.         wRet=pFR->MessageLoop();
  71.  
  72.     delete pFR;
  73.     return wRet;
  74.     }
  75.  
  76.  
  77.  
  78. //CHAPTER14MOD
  79. /*
  80.  * ObjectDestroyed
  81.  *
  82.  * Purpose:
  83.  *  Function for various objects to call when they are destroyed.
  84.  *  The main window is closed if the proper conditions are met.
  85.  */
  86.  
  87. void ObjectDestroyed(void)
  88.     {
  89.     g_cObj--;
  90.  
  91.     //No more objects, no locks, no user control, shut the app down.
  92.     if (0L==g_cObj && 0L==g_cLock && IsWindow(g_hWnd) && !g_fUser)
  93.         PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
  94.  
  95.     return;
  96.     }
  97. //CHAPTER14MOD
  98.  
  99.  
  100.  
  101. /*
  102.  * CCosmoFrame::CCosmoFrame
  103.  * CCosmoFrame::~CCosmoFrame
  104.  *
  105.  * Constructor Parameters:
  106.  *  hInst           HINSTANCE from WinMain
  107.  *  hInstPrev       HINSTANCE from WinMain
  108.  *  pszCmdLine      LPSTR from WinMain
  109.  *  nCmdShow        int from WInMain
  110.  */
  111.  
  112. CCosmoFrame::CCosmoFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  113.     , LPSTR pszCmdLine, int nCmdShow)
  114.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  115.     {
  116.     UINT        i;
  117.  
  118.     for (i=0; i<5; i++)
  119.         m_hBmpLines[i]=NULL;
  120.  
  121.     m_uIDCurLine=0;
  122.     m_fInitialized=FALSE;
  123.     m_pIClassDataTran=NULL;
  124.  
  125.     //CHAPTER14MOD
  126.     m_dwRegCOApp=0;
  127.     m_dwRegCOFig=0;
  128.     m_pIClassFactoryApp=NULL;
  129.     m_pIClassFactoryFig=NULL;
  130.  
  131.     m_fEmbedding=FALSE;
  132.     m_fAutomation=FALSE;
  133.  
  134.     m_pAutoApp=NULL;
  135.     m_dwActiveApp=0L;
  136.     //End CHAPTER14MOD
  137.     return;
  138.     }
  139.  
  140.  
  141. CCosmoFrame::~CCosmoFrame(void)
  142.     {
  143.     UINT        i;
  144.  
  145.     //CHAPTER14MOD
  146.     if (0L!=m_dwActiveApp)
  147.         {
  148.         RevokeActiveObject(m_dwActiveApp, NULL);
  149.         m_dwActiveApp=0L;
  150.         }
  151.  
  152.     //m_pAutoApp is cleaned up in CAutoApp::Release
  153.  
  154.     //Reverse CoRegisterClassObject, takes class factory refs to 1
  155.     if (0L!=m_dwRegCOApp)
  156.         CoRevokeClassObject(m_dwRegCOApp);
  157.  
  158.     if (0L!=m_dwRegCOFig)
  159.         CoRevokeClassObject(m_dwRegCOFig);
  160.  
  161.     //Should be last Releases to free the class factories
  162.     ReleaseInterface(m_pIClassFactoryApp);
  163.     ReleaseInterface(m_pIClassFactoryFig);
  164.     //End CHAPTER14MOD
  165.  
  166.     for (i=0; i<5; i++)
  167.         {
  168.         if (NULL!=m_hBmpLines[i])
  169.             DeleteObject(m_hBmpLines[i]);
  170.         }
  171.  
  172.     if (NULL!=m_pIClassDataTran)
  173.         {
  174.         m_pIClassDataTran->LockServer(FALSE);
  175.         m_pIClassDataTran->Release();
  176.         }
  177.  
  178.     OleFlushClipboard();
  179.  
  180.     if (m_fInitialized)
  181.         OleUninitialize();
  182.  
  183.     return;
  184.     }
  185.  
  186.  
  187.  
  188.  
  189. /*
  190.  * CCosmoFrame::Init
  191.  *
  192.  * Purpose:
  193.  *  Call CoInitialize then calling down into the base class
  194.  *  initialization.
  195.  *
  196.  * Parameters:
  197.  *  pFI             PFRAMEINIT containing initialization parameters.
  198.  *
  199.  * Return Value:
  200.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  201.  */
  202.  
  203. BOOL CCosmoFrame::Init(PFRAMEINIT pFI)
  204.     {
  205.     HRESULT     hr;
  206.     //CHAPTER14MOD
  207.     UINT        i;
  208.     //End CHAPTER14MOD
  209.  
  210.     CHECKVER_OLE;
  211.  
  212.     if (FAILED(OleInitialize(NULL)))
  213.         return FALSE;
  214.  
  215.     m_fInitialized=TRUE;
  216.  
  217.     /*
  218.      * Obtain the Data Transfer Object class factory and lock it.
  219.      * This will improve the speed of clipboard and (later)
  220.      * drag & drop operations that involve this class.
  221.      */
  222.     hr=CoGetClassObject(CLSID_DataTransferObject
  223.         , CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory
  224.         , (PPVOID)&m_pIClassDataTran);
  225.  
  226.     if (SUCCEEDED(hr))
  227.         m_pIClassDataTran->LockServer(TRUE);
  228.  
  229.     //CHAPTER14MOD
  230.     //Check for command line flags
  231.     ParseCommandLine();
  232.  
  233.     for (i=0; i < m_cCmdArgs; i++)
  234.         {
  235.         if(0==lstrcmpi(m_ppszCmdArgs[i], TEXT("-Embedding"))
  236.            || 0==lstrcmpi(m_ppszCmdArgs[i], TEXT("/Embedding")))
  237.             m_fEmbedding=TRUE;
  238.  
  239.         if(0==lstrcmpi(m_ppszCmdArgs[i], TEXT("-Automation"))
  240.            || 0==lstrcmpi(m_ppszCmdArgs[i], TEXT("/Automation")))
  241.             m_fAutomation=TRUE;
  242.         }
  243.  
  244.     /*
  245.      * Create the 'application' object for automation and make
  246.      * it the 'active' one with SetActiveObject.
  247.      */
  248.     m_pAutoApp=new CAutoApp(this);
  249.  
  250.     if (NULL==m_pAutoApp)
  251.         return FALSE;
  252.  
  253.     if (!m_pAutoApp->Init(TRUE))
  254.         return FALSE;
  255.  
  256.     /*
  257.      * We potentially have two class factories to register.
  258.      * We always register the multi-use factory for
  259.      * CLSID_CosmoFigure.  We only register the single-use
  260.      * factory for CLSID_Cosmo2Application if the /Automation
  261.      * flag is present, that is, if we actually were launched
  262.      * for that express purpose.
  263.      */
  264.  
  265.     if (m_fAutomation)
  266.         {
  267.         m_pIClassFactoryApp=new CClassFactory(this
  268.             , CLSID_Cosmo2Application);
  269.  
  270.         if (NULL==m_pIClassFactoryApp)
  271.             return FALSE;
  272.  
  273.         m_pIClassFactoryApp->AddRef();
  274.         hr=CoRegisterClassObject(CLSID_Cosmo2Application
  275.             , m_pIClassFactoryApp, CLSCTX_LOCAL_SERVER
  276.             , REGCLS_SINGLEUSE, &m_dwRegCOApp);
  277.  
  278.         if (FAILED(hr))
  279.             return FALSE;
  280.  
  281.         /*
  282.          * When registering the application as active, you can
  283.          * either register weak, which will not AddRef your object,
  284.          * or you can register strong and implement
  285.          * IExternalConnection.  For Cosmo it is most appropriate
  286.          * to register weak, but in order to work on Win16 as well
  287.          * as demonstrate the more complex technique, we'll register
  288.          * strong and implement IExternalConnection.  This interface
  289.          * is handled in CAutoBase and simply calls Release to
  290.          * match the AddRef here, which will do shutdown as needed.
  291.          */
  292.         RegisterActiveObject((IUnknown *)m_pAutoApp
  293.             , CLSID_Cosmo2Application, ACTIVEOBJECT_STRONG
  294.            , &m_dwActiveApp);
  295.  
  296.         /*
  297.          * This is necessary to balance the Release in
  298.          * IExternalConnection::ReleaseConnection
  299.          */
  300.         m_pAutoApp->AddRef();
  301.         }
  302.  
  303.     m_pIClassFactoryFig=new CClassFactory(this, CLSID_CosmoFigure);
  304.  
  305.     if (NULL==m_pIClassFactoryFig)
  306.         return FALSE;
  307.  
  308.     m_pIClassFactoryFig->AddRef();
  309.     hr=CoRegisterClassObject(CLSID_CosmoFigure
  310.         , m_pIClassFactoryFig, CLSCTX_LOCAL_SERVER
  311.         , REGCLS_MULTIPLEUSE, &m_dwRegCOFig);
  312.  
  313.     if (FAILED(hr))
  314.         return FALSE;
  315.     //End CHAPTER14MOD
  316.  
  317.     return CFrame::Init(pFI);
  318.     }
  319.  
  320.  
  321.  
  322.  
  323. /*
  324.  * CCosmoFrame::CreateCClient
  325.  *
  326.  * Purpose:
  327.  *  Constructs a new client specific to the application.
  328.  *
  329.  * Parameters:
  330.  *  None
  331.  *
  332.  * Return Value:
  333.  *  PCClient        Pointer to the new client object.
  334.  */
  335.  
  336. PCClient CCosmoFrame::CreateCClient(void)
  337.     {
  338.     return (PCClient)(new CCosmoClient(m_hInst, this));
  339.     }
  340.  
  341.  
  342.  
  343.  
  344.  
  345. /*
  346.  * CCosmoFrame::RegisterAllClasses
  347.  *
  348.  * Purpose:
  349.  *  Registers all classes used in this application.
  350.  *
  351.  * Parameters:
  352.  *  None
  353.  *
  354.  * Return Value:
  355.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  356.  */
  357.  
  358. BOOL CCosmoFrame::RegisterAllClasses(void)
  359.     {
  360.     WNDCLASS        wc;
  361.  
  362.     //First let the standard frame do its thing
  363.     if (!CFrame::RegisterAllClasses())
  364.         return FALSE;
  365.  
  366.     /*
  367.      * We want a different background color for the document
  368.      * because the Polyline we put in the document will paint
  369.      * with COLOR_WINDOW which by default which is CLASSLIB's
  370.      * default document color.
  371.      */
  372.  
  373.     GetClassInfo(m_hInst, SZCLASSDOCUMENT, &wc);
  374.     UnregisterClass(SZCLASSDOCUMENT, m_hInst);
  375.  
  376.     wc.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE+1);
  377.  
  378.     if (!RegisterClass(&wc))
  379.         return FALSE;
  380.  
  381.     //Register the Polyline window.
  382.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  383.     wc.hInstance     = m_hInst;
  384.     wc.cbClsExtra    = 0;
  385.     wc.lpfnWndProc   = PolylineWndProc;
  386.     wc.cbWndExtra    = CBPOLYLINEWNDEXTRA;
  387.     wc.hIcon         = NULL;
  388.     wc.hCursor       = LoadCursor(NULL, IDC_CROSS);
  389.     wc.hbrBackground = NULL;
  390.     wc.lpszMenuName  = NULL;
  391.     wc.lpszClassName = SZCLASSPOLYLINE;
  392.  
  393.     if (!RegisterClass(&wc))
  394.         return FALSE;
  395.  
  396.     return TRUE;
  397.     }
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404. /*
  405.  * CCosmoFrame::PreShowInit
  406.  *
  407.  * Purpose:
  408.  *  Called from Init before intially showing the window.  We do
  409.  *  whatever else we want here, modifying nCmdShow as necessary
  410.  *  which affects ShowWindow in Init.
  411.  *
  412.  * Parameters:
  413.  *  None
  414.  *
  415.  * Return Value:
  416.  *  BOOL            TRUE if this initialization succeeded,
  417.  *                  FALSE otherwise.
  418.  */
  419.  
  420. BOOL CCosmoFrame::PreShowInit(void)
  421.     {
  422.     CreateLineMenu();
  423.     CheckLineSelection(IDM_LINESOLID);
  424.  
  425.     //CHAPTER14MOD
  426.     //Save the window handle for shutdown if necessary.
  427.     g_hWnd=m_hWnd;
  428.  
  429.     //If we're under OLE control, don't show the main window.
  430.     if (m_fEmbedding || m_fAutomation)
  431.         m_nCmdShow=SW_HIDE;
  432.     //End CHAPTER14MOD
  433.     return TRUE;
  434.     }
  435.  
  436.  
  437.  
  438.  
  439. /*
  440.  * CCosmoFrame::CreateLineMenu
  441.  *
  442.  * Purpose:
  443.  *  Initializes the bitmaps used to create the Line menu and
  444.  *  replaces the text items defined in the application resources
  445.  *  with these bitmaps.  Note that the contents of m_hBmpLines
  446.  *  must be cleaned up when the application terminates.
  447.  *
  448.  * Parameters:
  449.  *  None
  450.  *
  451.  * Return Value:
  452.  *  None
  453.  */
  454.  
  455. void CCosmoFrame::CreateLineMenu(void)
  456.     {
  457.     HMENU       hMenu;
  458.     HDC         hDC, hMemDC;
  459.     HPEN        hPen;
  460.     HGDIOBJ     hObj;
  461.     TEXTMETRIC  tm;
  462.     UINT        i, cx, cy;
  463.  
  464.  
  465.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  466.     hDC=GetDC(m_hWnd);
  467.  
  468.     //Create each line in a menu item 8 chars wide, one char high.
  469.     GetTextMetrics(hDC, &tm);
  470.     cx=tm.tmAveCharWidth*8;
  471.     cy=tm.tmHeight;
  472.  
  473.     /*
  474.      * Create a memory DC in which to draw lines, and bitmaps
  475.      * for each line.
  476.      */
  477.     hMemDC=CreateCompatibleDC(hDC);
  478.     ReleaseDC(m_hWnd, hDC);
  479.  
  480.     for (i=0; i<5; i++)
  481.         {
  482.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  483.         SelectObject(hMemDC, m_hBmpLines[i]);
  484.  
  485.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  486.  
  487.         hPen=CreatePen(i, 1, 0L);       //i=line style like PS_SOLID
  488.         hObj=SelectObject(hMemDC, hPen);
  489.  
  490.         MoveToEx(hMemDC, 0, cy/2, NULL);
  491.         LineTo(hMemDC, cx, cy/2);
  492.  
  493.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  494.             , IDM_LINEMIN+i, (LPTSTR)(LONG)(UINT)m_hBmpLines[i]);
  495.  
  496.         SelectObject(hMemDC, hObj);
  497.         DeleteObject(hPen);
  498.         }
  499.  
  500.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  501.     DeleteDC(hMemDC);
  502.  
  503.     return;
  504.     }
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514. /*
  515.  * CCosmoFrame::CreateToolbar
  516.  *
  517.  * Purpose:
  518.  *  Procedure to create all the necessary toolbar buttons.
  519.  *
  520.  * Parameters:
  521.  *  None
  522.  *
  523.  * Return Value:
  524.  *  UINT            Number of tools added to the bar.
  525.  */
  526.  
  527. UINT CCosmoFrame::CreateToolbar(void)
  528.     {
  529.     UINT            iLast;
  530.     UINT            uState=GIZMO_NORMAL;
  531.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  532.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  533.  
  534.     //Insert the standard ones.
  535.     iLast=CFrame::CreateToolbar();
  536.  
  537.     /*
  538.      * Insert File Import in the 5th position and account for
  539.      * it in iLast.
  540.      */
  541.     m_pTB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB
  542.         , NULL, m_hBmp, 2, uState);
  543.     iLast++;
  544.  
  545.     //Separator
  546.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  547.         , NULL, NULL, 0, uState);
  548.  
  549.     /*
  550.      * For the Background bitmap, preserve our use of black
  551.      * (part of the image)
  552.      */
  553.     m_pTB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB
  554.         , NULL, m_hBmp, 3, GIZMO_NORMAL | PRESERVE_BLACK);
  555.  
  556.     m_pTB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB
  557.         , NULL, m_hBmp, 4, uState);
  558.  
  559.     //Separator
  560.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  561.         , NULL, NULL, 0, uState);
  562.  
  563.     //Line styles.
  564.     m_pTB->Add(utEx, iLast++, IDM_LINESOLID, m_dxB, m_dyB
  565.         , NULL, m_hBmp, 5, uState);
  566.     m_pTB->Add(utEx, iLast++, IDM_LINEDASH, m_dxB, m_dyB
  567.         , NULL, m_hBmp, 6, uState);
  568.     m_pTB->Add(utEx, iLast++, IDM_LINEDOT, m_dxB, m_dyB
  569.         , NULL, m_hBmp, 7, uState);
  570.     m_pTB->Add(utEx, iLast++, IDM_LINEDASHDOT, m_dxB, m_dyB
  571.         , NULL, m_hBmp, 8, uState);
  572.     m_pTB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB
  573.         , NULL, m_hBmp, 9, uState);
  574.  
  575.     return iLast;
  576.     }
  577.  
  578.  
  579.  
  580.  
  581. /*
  582.  * CCosmoFrame::OnCommand
  583.  *
  584.  * Purpose:
  585.  *  WM_COMMAND handler for the Cosmo frame window that just
  586.  *  processes the line menu and the color menu leaving the
  587.  *  CFrame to do everything else.
  588.  *
  589.  * Parameters:
  590.  *  hWnd            HWND of the frame window.
  591.  *  wParam          WPARAM of the message.
  592.  *  lParam          LPARAM of the message.
  593.  *
  594.  * Return Value:
  595.  *  LRESULT         Return value for the message.
  596.  */
  597.  
  598. LRESULT CCosmoFrame::OnCommand(HWND hWnd, WPARAM wParam
  599.     , LPARAM lParam)
  600.     {
  601.     PCCosmoDoc      pDoc;
  602.     TCHAR           szFile[CCHPATHMAX];
  603.     BOOL            fOK;
  604.     UINT            i, uTemp;
  605.     COLORREF        rgColors[16];
  606.     CHOOSECOLOR     cc;
  607.  
  608.     COMMANDPARAMS(wID, wCode, hWndMsg);
  609.  
  610.     /*
  611.      * Don't bother with anything during first initialization,
  612.      * skipping many toolbar notifications.
  613.      */
  614.     if (m_fInit)
  615.         return 0L;
  616.  
  617.     pDoc=(PCCosmoDoc)m_pCL->ActiveDocument();
  618.  
  619.     /*
  620.      * Check for the line style commands which are
  621.      * IDM_LINEMIN+<style>.  We handle this by changing the menu
  622.      * and toolbar, then we pass it to the document for real
  623.      * processing.
  624.      */
  625.     if (NULL!=pDoc && IDM_LINEMIN <= wID && IDM_LINEMAX >=wID)
  626.         {
  627.         CheckLineSelection(wID);
  628.         pDoc->LineStyleSet(wID-IDM_LINEMIN);
  629.         return 0L;
  630.         }
  631.  
  632.     switch (wID)
  633.         {
  634.         case IDM_FILEIMPORT:
  635.             szFile[0]=0;
  636.             fOK=SaveOpenDialog(szFile, CCHPATHMAX, IDS_FILEIMPORT
  637.                 , TRUE, &i);
  638.  
  639.             if (fOK)
  640.                 {
  641.                 uTemp=pDoc->Load(FALSE, szFile);
  642.                 pDoc->ErrorMessage(uTemp);
  643.                 }
  644.  
  645.             return (LRESULT)fOK;
  646.  
  647.  
  648.         case IDM_COLORBACKGROUND:
  649.         case IDM_COLORLINE:
  650.             //Invoke the color chooser for either color
  651.             uTemp=(IDM_COLORBACKGROUND==wID)
  652.                 ? DOCCOLOR_BACKGROUND : DOCCOLOR_LINE;
  653.  
  654.             for (i=0; i<16; i++)
  655.                 rgColors[i]=RGB(0, 0, i*16);
  656.  
  657.             memset(&cc, 0, sizeof(CHOOSECOLOR));
  658.             cc.lStructSize=sizeof(CHOOSECOLOR);
  659.             cc.lpCustColors=rgColors;
  660.             cc.hwndOwner=hWnd;
  661.             cc.Flags=CC_RGBINIT;
  662.             cc.rgbResult=pDoc->ColorGet(uTemp);
  663.  
  664.             if (ChooseColor(&cc))
  665.                 pDoc->ColorSet(uTemp, cc.rgbResult);
  666.  
  667.             break;
  668.  
  669.         //CHAPTER14MOD
  670.         case IDM_FILENEW:
  671.         case IDM_FILEOPEN:
  672.             /*
  673.              * When we're open for OLE Automation, the use might
  674.              * create a new document or such in which case we have
  675.              * to make suer we don't just shut down prematurely.
  676.              */
  677.             g_fUser=TRUE;
  678.  
  679.             //FALL THROUGH to default processing
  680.         //End CHAPTER14MOD
  681.  
  682.         default:
  683.            CFrame::OnCommand(hWnd, wParam, lParam);
  684.         }
  685.  
  686.     return 0L;
  687.     }
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694. /*
  695.  * CCosmoFrame::OnDocumentDataChange
  696.  *
  697.  * Purpose:
  698.  *  Update the Line menu and toolbar if the style in the data
  699.  *  changes.
  700.  *
  701.  * Parameters:
  702.  *  pDoc            PCDocument notifying the sink.
  703.  *
  704.  * Return Value:
  705.  *  None
  706.  */
  707.  
  708. void CCosmoFrame::OnDocumentDataChange(PCDocument pDoc)
  709.     {
  710.     CheckLineSelection(IDM_LINEMIN
  711.         +((PCCosmoDoc)pDoc)->LineStyleGet());
  712.     return;
  713.     }
  714.  
  715.  
  716.  
  717.  
  718. /*
  719.  * CCosmoFrame::OnDocumentActivate
  720.  *
  721.  * Purpose:
  722.  *  Informs us that document activation changed, so update the UI
  723.  *  for that new document.
  724.  *
  725.  * Parameters:
  726.  *  pDoc            PCDocument notifying the sink.
  727.  *
  728.  * Return Value:
  729.  *  None
  730.  */
  731.  
  732. void CCosmoFrame::OnDocumentActivate(PCDocument pDoc)
  733.     {
  734.     CheckLineSelection(IDM_LINEMIN
  735.         +((PCCosmoDoc)pDoc)->LineStyleGet());
  736.     return;
  737.     }
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745. /*
  746.  * CCosmoFrame::UpdateMenus
  747.  *
  748.  * Purpose:
  749.  *  Handles the WM_INITMENU message for the frame window.  Depending
  750.  *  on the existence of an active window, menu items are selectively
  751.  *  enabled and disabled.
  752.  *
  753.  * Parameters:
  754.  *  hMenu           HMENU of the menu to intialize
  755.  *  iMenu           UINT position of the menu.
  756.  *
  757.  * Return Value:
  758.  *  None
  759.  */
  760.  
  761. void CCosmoFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  762.     {
  763.     PCDocument  pDoc;
  764.     BOOL        fOK=FALSE;
  765.     BOOL        fCallDefault=TRUE;
  766.     UINT        i;
  767.     UINT        uTemp;
  768.     UINT        uTempE;
  769.     UINT        uTempD;
  770.  
  771.     pDoc=m_pCL->ActiveDocument();
  772.  
  773.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  774.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  775.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  776.  
  777.     //File menu:  If there is document window, disable Import.
  778.     if (m_phMenu[0]==hMenu)
  779.         EnableMenuItem(hMenu, IDM_FILEIMPORT, uTemp);
  780.  
  781.     //Color menu:  no document, no commands
  782.     if (m_phMenu[2]==hMenu)
  783.         {
  784.         EnableMenuItem(hMenu, IDM_COLORBACKGROUND, uTemp);
  785.         EnableMenuItem(hMenu, IDM_COLORLINE,       uTemp);
  786.         fCallDefault=FALSE;
  787.         }
  788.  
  789.     //Line menu:  no document, no commands
  790.     if (m_phMenu[3]==hMenu)
  791.         {
  792.         for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  793.             EnableMenuItem(hMenu, i, uTemp);
  794.  
  795.         fCallDefault=FALSE;
  796.         }
  797.  
  798.     if (fCallDefault)
  799.         CFrame::UpdateMenus(hMenu, iMenu);
  800.  
  801.     return;
  802.     }
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809. /*
  810.  * CCosmoFrame::UpdateToolbar
  811.  *
  812.  * Purpose:
  813.  *  Enables and disables tools depending on whether we have
  814.  *  a document or not.
  815.  *
  816.  * Parameters:
  817.  *  None
  818.  *
  819.  * Return Value:
  820.  *  None
  821.  */
  822.  
  823. void CCosmoFrame::UpdateToolbar(void)
  824.     {
  825.     BOOL        fLast;
  826.     UINT        i;
  827.  
  828.     //Save the last enabled state before CFrame changes it
  829.     fLast=m_fLastEnable;
  830.  
  831.     //Let the default hack on its tools
  832.     CFrame::UpdateToolbar();
  833.  
  834.     /*
  835.      * If CFrame::UpdateToolbar changed anything, then we need
  836.      * to change as well--if nothing changes, nothing to do.
  837.      */
  838.     if (fLast!=m_fLastEnable)
  839.         {
  840.         m_pTB->Enable(IDM_FILEIMPORT, m_fLastEnable);
  841.  
  842.         m_pTB->Enable(IDM_COLORBACKGROUND, m_fLastEnable);
  843.         m_pTB->Enable(IDM_COLORLINE,       m_fLastEnable);
  844.  
  845.         for (i=IDM_LINEMIN; i <= IDM_LINEMAX; i++)
  846.             m_pTB->Enable(i, m_fLastEnable);
  847.         }
  848.  
  849.     return;
  850.     }
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857. /*
  858.  * CCosmoFrame::CheckLineSelection
  859.  *
  860.  * Purpose:
  861.  *  Maintains the bitmap menu and the tools for the line selection.
  862.  *  Both are mutially exclusive option lists where a selection in
  863.  *  one has to affect the other.
  864.  *
  865.  * Parameters:
  866.  *  uID             UINT ID of the item to be selected
  867.  *
  868.  * Return Value:
  869.  *  None
  870.  */
  871.  
  872. void CCosmoFrame::CheckLineSelection(UINT uID)
  873.     {
  874.     UINT        i;
  875.     HMENU       hMenu;
  876.  
  877.     //Update menus and tools if the selection changed.
  878.     if (uID!=m_uIDCurLine)
  879.         {
  880.         m_uIDCurLine=uID;
  881.         hMenu=GetMenu(m_hWnd);
  882.  
  883.         //Uncheck all lines initially.
  884.         for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  885.             CheckMenuItem(hMenu, i, MF_UNCHECKED | MF_BYCOMMAND);
  886.  
  887.         CheckMenuItem(hMenu, uID, MF_CHECKED | MF_BYCOMMAND);
  888.         m_pTB->Check(uID, TRUE);
  889.         }
  890.  
  891.     return;
  892.     }
  893.  
  894.  
  895.  
  896.  
  897. //CHAPTER14MOD
  898. /*
  899.  * CCosmoFrame::AutoApp
  900.  *
  901.  * Purpose:
  902.  *  Returns the 'application' automation object pointer
  903.  *
  904.  * Return Value:
  905.  *  PCAutoApp       Pointer to the application object
  906.  */
  907.  
  908. PCAutoApp CCosmoFrame::AutoApp(void)
  909.     {
  910.     return m_pAutoApp;
  911.     }
  912. //End CHAPTER14MOD
  913.