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 / chap18 / cosmo / cosmo.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  21KB  |  910 lines

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