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 / chap21 / cosmo / cosmo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  20.6 KB  |  897 lines

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