home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / PASTESPC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  46.4 KB  |  1,780 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Defines type TPasteSpecialDlg
  6. //----------------------------------------------------------------------------
  7. #define INC_OLE2
  8. #include <owl/owlpch.h>
  9. #include <owl/listbox.h>
  10. #include <owl/radiobut.h>
  11. #include <owl/edit.h>
  12. #include <ocf/ocview.h>
  13. #include <owl/oledlg.h>
  14. #include <owl/except.h>
  15. #include <bwcc.h>   // for IDHELP
  16. #include <dir.h>
  17.  
  18. //
  19. // OWL OLE Dialog diagnostic group.
  20. //
  21. DIAG_DECLARE_GROUP(OwlOleDlg);
  22.  
  23. //
  24. //
  25. //
  26. static bool
  27. CompareTargetDevice(DVTARGETDEVICE FAR* ptdLeft,
  28.                     DVTARGETDEVICE FAR* ptdRight)
  29. {
  30.   if (ptdLeft == ptdRight)
  31.     return true;
  32.   else if (!ptdRight || !ptdLeft)
  33.     return false;
  34.   else if (ptdLeft->tdSize != ptdRight->tdSize)
  35.     return false;
  36.   else if (memcmp(ptdLeft, ptdRight, (int)ptdLeft->tdSize) != 0)
  37.     return false;
  38.   return true;
  39. }
  40.  
  41. //
  42. // Returns 0 for exact match, 1 for no match, -1 for partial match (which is
  43. // defined to mean the left is a subset of the right: fewer aspects,
  44. // null target device, fewer medium).
  45. //
  46. static int
  47. CompareFormatEtc(FORMATETC FAR* pFetcLeft, FORMATETC FAR* pFetcRight)
  48. {
  49.   bool bExact = true;
  50.  
  51.   if (pFetcLeft->cfFormat != pFetcRight->cfFormat)
  52.     return 1;
  53.   else if (!CompareTargetDevice (pFetcLeft->ptd, pFetcRight->ptd))
  54.     return 1;
  55.   if (pFetcLeft->dwAspect == pFetcRight->dwAspect)
  56.     //
  57.     // Same aspects implies equality
  58.     //
  59.     ;
  60.  
  61.   else if ((pFetcLeft->dwAspect & ~pFetcRight->dwAspect) != 0)
  62.     //
  63.     // Left not subset of aspects of right implies not equal
  64.     //
  65.     return 1;
  66.   else
  67.     //
  68.     // left subset of right
  69.     //
  70.     bExact = false;
  71.  
  72.   if (pFetcLeft->tymed == pFetcRight->tymed)
  73.     // same medium flags; equal
  74.     ;
  75.   else if ((pFetcLeft->tymed & ~pFetcRight->tymed) != 0)
  76.     //
  77.     // left not subset of medium flags of right; not equal
  78.     //
  79.     return 1;
  80.   else
  81.     //
  82.     // left subset of right
  83.     //
  84.     bExact = false;
  85.  
  86.   return bExact ? 0 : -1;
  87. }
  88.  
  89. DEFINE_RESPONSE_TABLE1(TPasteSpecialDlg, TOleDialog)
  90.   EV_WM_DRAWCLIPBOARD,
  91.   EV_WM_CHANGECBCHAIN,
  92.   EV_BN_CLICKED(IDC_CHANGEICON, ChangeIconClicked),
  93.   EV_BN_CLICKED(IDC_PASTE, PasteClicked),
  94.   EV_BN_CLICKED(IDC_PASTELINK, PasteLinkClicked),
  95.   EV_BN_CLICKED(IDC_DISPLAYASICON, DisplayAsIconClicked),
  96.   EV_LBN_SELCHANGE(IDC_DISPLAYLIST, DisplayListSelChange),
  97.   EV_LBN_DBLCLK(IDC_DISPLAYLIST, DisplayListDblClk),
  98. END_RESPONSE_TABLE;
  99.  
  100. //
  101. // Initialize all data members to 0 [or false]
  102. //
  103. TPasteSpecialDlg::TData::TData()
  104. {
  105.   //
  106.   // This class is really just a PODS, so we'll take
  107.   // advantage of memset
  108.   //
  109.   memset(this, 0, sizeof(TData));
  110. }
  111.  
  112. //
  113. // Initialize all data members to 0 [or false]
  114. //
  115. TPasteSpecialDlg::TPasteListItemData::TPasteListItemData()
  116. {
  117.   //
  118.   // This class is really just a PODS, so we'll take
  119.   // advantage of memset
  120.   //
  121.   memset(this, 0, sizeof(TPasteListItemData));
  122. }
  123.  
  124. //
  125. // Initialize all data members to 0 [or false]
  126. //
  127. TPasteSpecialDlg::TPasteEntry::TPasteEntry()
  128. {
  129.   //
  130.   // This class is really just a PODS, so we'll take
  131.   // advantage of memset
  132.   //
  133.   memset(this, 0, sizeof(TPasteEntry));
  134. }
  135.  
  136. //
  137. // Initialize helper object used internally by TPasteSpecialDlg
  138. //
  139. TPasteSpecialDlg::THelper::THelper()
  140. {
  141.   //
  142.   // This structure is a private PODS which allows
  143.   // us to take advantage of memset.
  144.   //
  145.   memset(this, 0, sizeof(THelper));
  146. }
  147.  
  148. //
  149. // Initialize TPasteSpecial Dialog:
  150. //    - Alias Dialog controls with C++ objects
  151. //    - Initialize internal variables
  152. //
  153. TPasteSpecialDlg::TPasteSpecialDlg(TWindow* parent,
  154.                                    TOcInitInfo &initInfo,
  155.                                    TData *data,
  156.                                    TResId templateId,
  157.                                    const char far *title,
  158.                                    TModule* module):
  159.                   TOleDialog(parent,
  160.                             templateId ? templateId : TResId(DLG_PASTESPECIAL),
  161.                             title,
  162.                             module),
  163.                   InitInfo(initInfo), Helper(*new THelper)
  164. {
  165.   //
  166.   // Wrap controls
  167.   //
  168.   ResultImage     = new TResultImage(this, IDC_RESULTIMAGE);
  169.   IconImage       = new TIconImage(this, IDC_ICONIMAGE);
  170.   DisplayAsIcon   = new TCheckBox(this, IDC_DISPLAYASICON);
  171.   Paste           = new TRadioButton(this, IDC_PASTE);
  172.   PasteLink       = new TRadioButton(this, IDC_PASTELINK);
  173.   DisplayList     = new TListBox(this, IDC_DISPLAYLIST);
  174.   PasteList       = new TListBox(this, IDC_PASTELIST);
  175.   PasteLinkList   = new TListBox(this, IDC_PASTELINKLIST);
  176.   ChangeIcon      = new TButton(this, IDC_CHANGEICON);
  177.   ResultText      = new TStatic(this, IDC_RESULTTEXT);
  178.   SourceText      = new TStatic(this, IDC_SOURCE);
  179.   Help            = new TButton(this, IDHELP);
  180.  
  181.   //
  182.   // Store TData pointer (if specified)
  183.   //
  184.   Data = data;
  185.  
  186.   //
  187.   // Init internal variables
  188.   //
  189.   DeleteData       = false;
  190.   pOleUIEntries    = 0;
  191.   pBOleEntries     = 0;
  192.   pBOleLinkEntries = 0;
  193. }
  194.  
  195. //
  196. // Cleanup memory allocated for TPasteSpecialInfo pointers if
  197. // user relied on default one.
  198. //
  199. TPasteSpecialDlg::~TPasteSpecialDlg()
  200. {
  201.   delete &Helper;
  202.  
  203.   //
  204.   // Delete Data pointer if we allocated it
  205.   //
  206.   if (DeleteData) {
  207.     delete Data;
  208.  
  209.     if (pOleUIEntries)
  210.       delete []pOleUIEntries;
  211.  
  212.     if (pBOleEntries)
  213.       delete []pBOleEntries;
  214.  
  215.     if (pBOleLinkEntries)
  216.       delete []pBOleLinkEntries;
  217.   }
  218. }
  219.  
  220. //
  221. // Check if a TPasteSpecialInfo pointer was specified - Create one
  222. // if necessary.
  223. //
  224. int
  225. TPasteSpecialDlg::DoExecute()
  226. {
  227.   if (!Data) {
  228.     if (!GetDefaultPasteSpecialData()) {
  229.       return IDCANCEL;
  230.     }
  231.   }
  232.   return TDialog::DoExecute();
  233. }
  234.  
  235. //
  236. // Validate flags, Initialize Helper structure,
  237. // Fill Paste/PasteLink listboxes
  238. //
  239. bool
  240. TPasteSpecialDlg::EvInitDialog(HWND hwndFocus)
  241. {
  242.   //
  243.   // Call base to alias controls
  244.   //
  245.   TOleDialog::EvInitDialog(hwndFocus);
  246.  
  247.   //
  248.   // Enable Wait cursor
  249.   //
  250.   HCURSOR oldCursor = HourGlassOn();
  251.  
  252.   //
  253.   // Copy user info. and init helper class
  254.   //
  255.   Helper.Flags = Data->Flags;
  256.   Helper.PasteListCurSel = 0;
  257.   Helper.PasteLinkListCurSel = 0;
  258.  
  259.   //
  260.   // Set control font
  261.   //
  262.   if (Font) {
  263.     ResultText->SetWindowFont(*Font, false);
  264.     SourceText->SetWindowFont(*Font, false);
  265.   }
  266.  
  267.   //
  268.   // Hide help if requested
  269.   //
  270.   if (!(Helper.Flags & psShowHelp))
  271.     Activate(Help, false);
  272.  
  273.   //
  274.   // Hide IconDisplay related controls if requested
  275.   //
  276.   if (Helper.Flags & psDisableDisplayAsIcon) {
  277.     Activate(DisplayAsIcon, false);
  278.     Activate(IconImage, false);
  279.     Activate(ChangeIcon, false);
  280.   }
  281.  
  282.   //
  283.   // Clear 'CheckDisplayAsIcon' [out-flag only]
  284.   //
  285.   Helper.Flags &= ~psCheckDisplayAsIcon;
  286.  
  287.   //
  288.   // Load 'Unknown Source' and 'Unknown Type' strings
  289.   //
  290.   GetModule()->LoadString(IDS_PSUNKNOWNTYPE,
  291.                           Helper.UnknownType, MaxUnknownLen);
  292.   GetModule()->LoadString(IDS_PSUNKNOWNSRC,
  293.                           Helper.UnknownSource, MaxUnknownLen);
  294.   Helper.AppName[0]= 0;
  295.  
  296.   //
  297.   // Fill OBJECTDESCRIPTOR structure
  298.   //
  299.   STGMEDIUM medium;
  300.   CLIPFORMAT cfFormat;
  301.   Helper.ObjDesc = FillObjectDescriptorFromData(Data->lpSrcDataObj,
  302.                                                 &medium, &cfFormat);
  303.   if (Helper.ObjDesc) {
  304.     LPOBJECTDESCRIPTOR lpOD = (LPOBJECTDESCRIPTOR)GlobalLock(Helper.ObjDesc);
  305.  
  306.     //
  307.     // Retrieve FullUserTypeName, SourceOfCopy and CLSID
  308.     //
  309.     if (lpOD->dwFullUserTypeName)
  310.       Helper.FullUserTypeNameOD = (LPSTR)lpOD + (int)lpOD->dwFullUserTypeName;
  311.     else
  312.       Helper.FullUserTypeNameOD = Helper.UnknownType;
  313.  
  314.     if (lpOD->dwSrcOfCopy) {
  315.       Helper.SourceOfDataOD = (LPSTR)lpOD + (int)lpOD->dwSrcOfCopy;
  316.  
  317.       //
  318.       // If CF_FILENAME is offered, source of copy is a path name;
  319.       // Chop the name to fit the static control it will be displayed in
  320.       //
  321.       if (cfFormat == cfFileName)
  322.         Helper.SourceOfDataOD= ChopText(*SourceText, 0, Helper.SourceOfDataOD);
  323.     }
  324.     else {
  325.       Helper.SourceOfDataOD = Helper.UnknownSource;
  326.     }
  327.  
  328.     Helper.ClsIdOD = lpOD->clsid;
  329.     Helper.SizeLOD = lpOD->sizel;
  330.  
  331.     //
  332.     // Update helper class about DVASPECT_ICON & OLEMISC_ONLYICONIC flags
  333.     //
  334.     Helper.SrcAspectIconOD = (lpOD->dwDrawAspect & DVASPECT_ICON) ?
  335.                               true : false;
  336.     Helper.SrcOnlyIconicOD = (lpOD->dwStatus & OLEMISC_ONLYICONIC) ?
  337.                               true : false;
  338.  
  339.     //
  340.     // Get App. Name of source from auxusertype3 in reg. database
  341.     //
  342.     if (!GetAuxUserType(Helper.ClsIdOD, 3, Helper.AppName, MaxKeyLen, 0)) {
  343.       //
  344.       // Default to "the application which created it" as the name
  345.       //
  346.       GetModule()->LoadString(IDS_PSUNKNOWNAPP, Helper.AppName, MaxKeyLen);
  347.     }
  348.  
  349.     //
  350.     // Retrieve an icon from the object
  351.     //
  352.     if (Helper.SrcAspectIconOD) {
  353.       Helper.MetaPictOD = GetData(Data->lpSrcDataObj,
  354.                                   CF_METAFILEPICT, 0,
  355.                                   DVASPECT_ICON, &medium);
  356.     }
  357.  
  358.    //
  359.     // If none, retrieve icon from CLSID
  360.     //
  361.     if (!Helper.MetaPictOD) {
  362.       Helper.MetaPictOD = GetIconFromClass(Helper.ClsIdOD, 0, TRUE);
  363.     }
  364.   }
  365.  
  366.   //
  367.   // Does object offer CF_LINKSRCDESCRIPTOR
  368.   //
  369.   Helper.LinkSrcDesc = GetData(Data->lpSrcDataObj,
  370.                                cfLinkSrcDescriptor, 0,
  371.                                DVASPECT_CONTENT, &medium);
  372.   if (Helper.LinkSrcDesc) {
  373.     LPLINKSRCDESCRIPTOR lpLSD = (LPLINKSRCDESCRIPTOR)
  374.                                  GlobalLock(Helper.LinkSrcDesc);
  375.  
  376.     //
  377.     // Get FullUserTypeName, SourceOfCopy and CLSID
  378.     //
  379.     if (lpLSD->dwFullUserTypeName)
  380.       Helper.FullUserTypeNameLSD = (LPSTR)lpLSD +
  381.                                      (int)lpLSD->dwFullUserTypeName;
  382.     else
  383.       Helper.FullUserTypeNameLSD = Helper.UnknownType;
  384.  
  385.    if (lpLSD->dwSrcOfCopy)
  386.       Helper.SourceOfDataLSD = (LPSTR)lpLSD + (int)lpLSD->dwSrcOfCopy;
  387.     else
  388.       Helper.SourceOfDataLSD = Helper.UnknownSource;
  389.  
  390.     //
  391.     // If no objectDescriptor, use linkSourceDescriptor source string
  392.     //
  393.     if (!Helper.ObjDesc)
  394.       Helper.SourceOfDataOD = Helper.SourceOfDataLSD;
  395.  
  396.     Helper.ClsIdLSD = lpLSD->clsid;
  397.     Helper.SizeLLSD = lpLSD->sizel;
  398.  
  399.     //
  400.     // Update helper class about DVASPECT_ICON & OLEMISC_ONLYICONIC
  401.     //
  402.     Helper.SrcAspectIconLSD = (lpLSD->dwDrawAspect & DVASPECT_ICON) ?
  403.                                true : false;
  404.     Helper.SrcOnlyIconicLSD = (lpLSD->dwStatus & OLEMISC_ONLYICONIC) ?
  405.                                true : false;
  406.  
  407.     //
  408.     // Retrieve an icon from object
  409.     //
  410.     if (Helper.SrcAspectIconLSD) {
  411.       Helper.MetaPictLSD = GetData(Data->lpSrcDataObj,
  412.                                    CF_METAFILEPICT, 0,
  413.                                    DVASPECT_ICON, &medium);
  414.       //
  415.       // If object offers none, retrieve icon from CLSID
  416.       //
  417.       if (!Helper.MetaPictLSD) {
  418.         //
  419.         // Retrieve 1.5 times width of iconbox
  420.         //
  421.         TRect rect;
  422.         IconImage->GetClientRect(rect);
  423.         int width = rect.Width() *3/2;
  424.  
  425.         //
  426.         // Chop label to fit width
  427.         //
  428.         char  label[MaxLabelLen];
  429.         strcpyn(label, Helper.SourceOfDataLSD, sizeof(label));
  430.         label[sizeof(label)-1] = 0;
  431.         LPSTR lpszLabel = ChopText(*IconImage, width, label);
  432.  
  433.         //
  434.         // Retrieve icon
  435.         //
  436.         Helper.MetaPictLSD = GetIconFromClass(Helper.ClsIdLSD,
  437.                                               lpszLabel, FALSE);
  438.       }
  439.     }
  440.   }
  441.   else if (Helper.ObjDesc)  // Offers no LINKSRCDESCRIPTOR but
  442.   {                         // Offers OBJECTDESCRIPTOR - Copy OD values!
  443.     Helper.FullUserTypeNameLSD = Helper.FullUserTypeNameOD;
  444.     Helper.SourceOfDataLSD     = Helper.SourceOfDataOD;
  445.     Helper.ClsIdLSD            = Helper.ClsIdOD;
  446.     Helper.SizeLLSD            = Helper.SizeLOD;
  447.     Helper.SrcAspectIconLSD    = Helper.SrcAspectIconOD;
  448.     Helper.SrcOnlyIconicLSD    = Helper.SrcOnlyIconicOD;
  449.  
  450.     //
  451.     // Grab copy of MetaPict
  452.     //
  453.     if (Helper.SrcAspectIconLSD) {
  454.       Helper.MetaPictLSD = GetData(Data->lpSrcDataObj,
  455.                                    CF_METAFILEPICT, 0,
  456.                                    DVASPECT_ICON, &medium);
  457.     }
  458.  
  459.     //
  460.     // Retrieve icon from CLSID
  461.     //
  462.     if (!Helper.MetaPictLSD) {
  463.       //
  464.       // Retrieve 1.5 times width of iconbox
  465.       //
  466.       TRect rect;
  467.       IconImage->GetClientRect(rect);
  468.       int width = rect.Width() *3/2;
  469.  
  470.       //
  471.       // Chop label to fit width
  472.       //
  473.       char  label[MaxLabelLen];
  474.       strcpyn(label, Helper.SourceOfDataLSD, sizeof(label));
  475.       label[sizeof(label)-1] = 0;
  476.       LPSTR lpszLabel = ChopText(*IconImage, width, label);
  477.  
  478.       //
  479.       // Retrieve Icon
  480.       //
  481.       Helper.MetaPictLSD = GetIconFromClass(Helper.ClsIdLSD,
  482.                                             lpszLabel, FALSE);
  483.     }
  484.   }
  485.  
  486.   //
  487.   // We have a non-OLE object
  488.   //
  489.   if (!Helper.ObjDesc  &&  !Helper.LinkSrcDesc) {
  490.     Helper.FullUserTypeNameLSD =
  491.     Helper.FullUserTypeNameOD  = Helper.UnknownType;
  492.  
  493.     Helper.SourceOfDataLSD =
  494.     Helper.SourceOfDataOD  = Helper.UnknownSource;
  495.  
  496.     Helper.MetaPictLSD = Helper.MetaPictOD = 0;
  497.   }
  498.  
  499.   //
  500.   // Update Flags: paste vs. pastelink
  501.   //
  502.   if (Helper.Flags & psSelectPasteLink)
  503.     Helper.Flags = (Helper.Flags & ~psSelectPaste) | psSelectPasteLink;
  504.   else
  505.     Helper.Flags = (Helper.Flags & ~psSelectPasteLink) | psSelectPaste;
  506.  
  507.   //
  508.   // Mark which PasteEntry formats are available from source data object
  509.   //
  510.   MarkPasteEntryList(Data->lpSrcDataObj, Data->ArrayPasteEntries,
  511.                      Data->cPasteEntries);
  512.  
  513.   //
  514.   // Check if items are available for pasting
  515.   //
  516.   bool pasteAvailable = FillPasteList();
  517.   if (!pasteAvailable) {
  518.     Helper.Flags &= ~psSelectPaste;
  519.     Paste->EnableWindow(false);
  520.   }
  521.  
  522.   //
  523.   // Check if items are available for paste-linking
  524.   //
  525.   bool pasteLinkAvailable = FillPasteLinkList();
  526.   if (!pasteLinkAvailable) {
  527.     Helper.Flags &= ~psSelectPasteLink;
  528.     PasteLink->EnableWindow(false);
  529.   }
  530.  
  531.   //
  532.   // Validate flags base on paste availability
  533.   //
  534.   if (pasteAvailable && !pasteLinkAvailable)
  535.     Helper.Flags |= psSelectPaste;
  536.  
  537.   if (pasteLinkAvailable && !pasteAvailable)
  538.     Helper.Flags |= psSelectPasteLink;
  539.  
  540.   if (Helper.Flags & psSelectPaste) {
  541.     CheckRadioButton(IDC_PASTE, IDC_PASTELINK, IDC_PASTE);
  542.  
  543.     //
  544.     // TogglePaste will set paste flag, so clear it first
  545.     //
  546.     Helper.Flags &= ~psSelectPaste;
  547.     TogglePasteType(psSelectPaste);
  548.   }
  549.   else if (Helper.Flags & psSelectPasteLink) {
  550.     CheckRadioButton(IDC_PASTE, IDC_PASTELINK, IDC_PASTELINK);
  551.  
  552.     //
  553.     // TogglePaste will set pasteLink flag, so clear it first
  554.     //
  555.     Helper.Flags &= psSelectPasteLink;
  556.     TogglePasteType(psSelectPasteLink);
  557.   }
  558.   else {
  559.     EnableDisplayAsIcon();
  560.     SetPasteSpecialHelpResults();
  561.   }
  562.  
  563.   //
  564.   // Set Focus to ListBox
  565.   //
  566.   DisplayList->SetFocus();
  567.  
  568.   //
  569.   // Set property to handle clipboard change notifications
  570.   //
  571.   SetProp(NEXTCBVIEWER, HWND_BROADCAST);
  572.   SetProp(NEXTCBVIEWER, SetClipboardViewer(*this));
  573.   Helper.ClipboardChanged = false;
  574.  
  575.   //
  576.   // Restore previous cursor
  577.   //
  578.   HourGlassOff(oldCursor);
  579.  
  580.   return false;
  581. }
  582.  
  583. //
  584. //
  585. //
  586. void
  587. TPasteSpecialDlg::CleanupWindow()
  588. {
  589.   //
  590.   // Free metafile of non-selected option
  591.   //
  592.   if (Helper.Link)
  593.     TOleMetaPict::Free(Helper.MetaPictOD);
  594.   else
  595.     TOleMetaPict::Free(Helper.MetaPictLSD);
  596.  
  597.   //
  598.   // Free listbox entries
  599.   //
  600.   FreeListData(*PasteList);
  601.   FreeListData(*PasteLinkList);
  602.  
  603.   //
  604.   // Free memory allocations
  605.   //
  606.   if (Helper.ObjDesc)
  607.     GlobalFree(Helper.ObjDesc);
  608.  
  609.   if (Helper.LinkSrcDesc)
  610.     GlobalFree(Helper.LinkSrcDesc);
  611.  
  612.   //
  613.   // Remove ourselves from Clipboard notification chain
  614.   //
  615.   HWND hwndNextViewer;
  616.   hwndNextViewer = (HWND)GetProp(NEXTCBVIEWER);
  617.   if (hwndNextViewer != HWND_BROADCAST) {
  618.     SetProp(NEXTCBVIEWER, HWND_BROADCAST);
  619.     ChangeClipboardChain(*this, hwndNextViewer);
  620.   }
  621.   RemoveProp(NEXTCBVIEWER);
  622. }
  623.  
  624. //
  625. // Handles User activating OK button by retrieving the current
  626. // selection and updating the TPasteSpecialInfo and TOcInitInfo
  627. // structures.
  628. //
  629. bool
  630. TPasteSpecialDlg::OleDlgOk()
  631. {
  632.   //
  633.   // Copy Helper information to Data structure
  634.   //
  635.   bool fDestAspectIcon = (Helper.Flags & psCheckDisplayAsIcon) ? true : false;
  636.   Data->Flags = Helper.Flags;
  637.   Data->SelectedIndex = Helper.SelectedIndex;
  638.   Data->fLink = Helper.Link;
  639.   if (Helper.Link) {
  640.     if (Helper.SrcAspectIconLSD == fDestAspectIcon)
  641.       Data->SizeL = Helper.SizeLLSD;
  642.     else
  643.       Data->SizeL.cx = Data->SizeL.cy = 0;
  644.   }
  645.   else {
  646.     if (Helper.SrcAspectIconOD == fDestAspectIcon)
  647.       Data->SizeL = Helper.SizeLOD;
  648.     else
  649.       Data->SizeL.cx = Data->SizeL.cy = 0;
  650.   }
  651.  
  652.   //
  653.   // Grab selected metafile
  654.   //
  655.   Data->MetaPict = IconImage->GetMetaPict();
  656.  
  657.   //
  658.   // Update TOcInitInfo data
  659.   //
  660.   if (Data->Flags & psCheckDisplayAsIcon)
  661.     InitInfo.HIcon = (HICON)Data->MetaPict;
  662.  
  663.   LPDATAOBJECT pDataObj = Data->lpSrcDataObj;
  664.   int i = Data->SelectedIndex;
  665.  
  666.   //
  667.   // Retrieve selected clipboard format - If we have the BOleFormat handy,
  668.   // go there since it's 'nearer' - otherwise use the Data data
  669.   //
  670.   CLIPFORMAT cf;
  671.   if (pBOleEntries)
  672.     cf = pBOleEntries[i].Id;
  673.   else
  674.     cf = Data->ArrayPasteEntries[i].fmtetc.cfFormat;
  675.  
  676.   if (cf == cfEmbeddedObject) {
  677.     InitInfo.How   = ihEmbed;
  678.     InitInfo.Where = iwDataObject;
  679.     InitInfo.Data  = pDataObj;
  680.   }
  681.   else if (cf == cfEmbedSource) {
  682.     InitInfo.How     = ihEmbed;
  683.     InitInfo.Where   = iwDataObject;
  684.     InitInfo.Data    = pDataObj;
  685.     InitInfo.Storage = 0;
  686.   }
  687.   else if (cf == cfLinkSource) {
  688.     InitInfo.How   = ihLink;
  689.     InitInfo.Where = iwDataObject;
  690.     InitInfo.Data  = pDataObj;
  691.   }
  692.   else {
  693.     STGMEDIUM medium;
  694.     InitInfo.How   = Data->fLink ? ihLink : ihEmbed;
  695.     InitInfo.Where = iwHandle;
  696.     InitInfo.Handle.DataFormat = cf;
  697.  
  698.     if (!Data->fLink)
  699.       InitInfo.Handle.Data = GetData(pDataObj, cf, 0,
  700.                                      DVASPECT_CONTENT, &medium);
  701.     //
  702.     // Release data object since we're only passing back a handle [not
  703.     // the data object]
  704.     //
  705.     pDataObj->Release();
  706.   }
  707.  
  708.   return true;
  709. }
  710.  
  711. //
  712. // Allocate and Initialize a TPasteSpecialData structure [used if no
  713. // TPasteSpecialData pointer was specified with constructing dialog]
  714. //
  715. bool
  716. TPasteSpecialDlg::GetDefaultPasteSpecialData()
  717. {
  718.   //
  719.   // Should be called only if user did not specify a TPasteSpecialInfo ptr.
  720.   //
  721.   PRECONDITION(Data == 0);
  722.  
  723.   //
  724.   // Find container we'll be pasting in
  725.   //
  726.   IBContainer far* pCont = 0;
  727.   if (InitInfo.Container)
  728.     pCont = InitInfo.Container;
  729.   else {
  730.     TRACEX(OwlOleDlg, 1, "Container to paste in not found");
  731.     return false;
  732.   }
  733.  
  734.   //
  735.   // Find data consumer from container
  736.   //
  737.   IBDataConsumer far* pConsumer = 0;
  738.   HRESULT hRes = pCont->QueryInterface(IID_IBDataConsumer,
  739.                                        &(LPVOID)pConsumer);
  740.  
  741.   if (!SUCCEEDED(hRes)) {
  742.     TRACEX(OwlOleDlg, 1, "Container data consumer not found");
  743.     return false;
  744.   }
  745.   else {
  746.     pConsumer->Release();
  747.   }
  748.  
  749.   //
  750.   // Check if clipboard is empty or (maybe) unavailable
  751.   //
  752.   LPDATAOBJECT pDataObj = 0;
  753.   if (OleGetClipboard(&pDataObj) != S_OK) {
  754.     TRACEX(OwlOleDlg, 1, "Clipboard's unavailable to paste from");
  755.     return false;
  756.   }
  757.  
  758.   //
  759.   // Retrieve acceptable formats
  760.   //
  761.   uint nAcceptableFormats = 0;
  762.   nAcceptableFormats = pConsumer->CountFormats();
  763.   if (!nAcceptableFormats) {
  764.     TRACEX(OwlOleDlg, 1, "No acceptable formats from consumer");
  765.     return false;
  766.   }
  767.  
  768.   //
  769.   // Allocate Dialog parameter information
  770.   //
  771.   pOleUIEntries    = new TPasteEntry[nAcceptableFormats];
  772.   pBOleEntries     = new TOcFormatInfo[nAcceptableFormats];
  773.   pBOleLinkEntries = new uint[nAcceptableFormats];
  774.   Data             = new TData;
  775.   DeleteData       = true;
  776.  
  777.   //
  778.   // Iterate through formats to initialize structures and track
  779.   // linkable formats
  780.   //
  781.   uint nLinkableFormats   = 0;
  782.   for (int i=0; i<nAcceptableFormats; i++) {
  783.     pConsumer->GetFormat(i, &pBOleEntries[i]);
  784.  
  785.     InitFormatEtc(pOleUIEntries[i].fmtetc, pBOleEntries[i].Id,
  786.                   pBOleEntries[i].Medium);
  787.     pOleUIEntries[i].lpstrFormatName = pBOleEntries[i].Name[0] ?
  788.                                 OleStr(pBOleEntries[i].Name) : OleText("%s");
  789.     pOleUIEntries[i].lpstrResultText =
  790.                           pBOleEntries[i].ResultName[0] ?
  791.                             OleStr(pBOleEntries[i].ResultName) : OleText("%s");
  792.     pOleUIEntries[i].dwFlags =  pBOleEntries[i].IsLinkable ?
  793.                                 pfPaste : pfPasteOnly;
  794.  
  795.     if (pBOleEntries[i].Id == cfEmbeddedObject ||
  796.         pBOleEntries[i].Id == cfLinkSource     ||
  797.         pBOleEntries[i].Id == cfEmbedSource     ) {
  798.       //
  799.       // PasteOnly and EnableIcon are mutually exclusive
  800.       //
  801.       if (pOleUIEntries[i].dwFlags == pfPaste)
  802.         pOleUIEntries[i].dwFlags |= pfEnableIcon;
  803.     }
  804.  
  805.     if (pBOleEntries[i].IsLinkable) {
  806.       pBOleLinkEntries[nLinkableFormats] = pBOleEntries[i].Id;
  807.  
  808.       DWORD flag;
  809.       switch(nLinkableFormats) {
  810.         case  0:  flag  = pfLinkType1;  break;
  811.         case  1:  flag  = pfLinkType2;  break;
  812.         case  2:  flag  = pfLinkType2;  break;
  813.         case  3:  flag  = pfLinkType2;  break;
  814.         case  4:  flag  = pfLinkType2;  break;
  815.         case  5:  flag  = pfLinkType2;  break;
  816.         case  6:  flag  = pfLinkType2;  break;
  817.         case  7:  flag  = pfLinkType2;  break;
  818.       }
  819.       pOleUIEntries[i].dwFlags |= flag;
  820.       nLinkableFormats++;
  821.     }
  822.   }
  823.  
  824.   //
  825.   // Store all relevant PasteSpecial Initialization information
  826.   //
  827.   Data->ArrayPasteEntries = pOleUIEntries;
  828.   Data->cPasteEntries     = nAcceptableFormats;
  829.   Data->lpSrcDataObj      = pDataObj;
  830.   Data->ArrayLinkTypes    = pBOleLinkEntries;
  831.   Data->cLinkTypes        = nLinkableFormats;
  832.   Data->cClsIdExclude     = 0;
  833.  
  834.   return true;
  835. }
  836.  
  837. //
  838. //  Mark each entry in the PasteEntryList if its format is available from
  839. //  the source IDataObject*. the dwScratchSpace field of each PasteEntry
  840. //  is set to TRUE if available, else FALSE.
  841. //
  842. void TPasteSpecialDlg::MarkPasteEntryList(LPDATAOBJECT      lpSrcDataObj,
  843.                                           TPasteEntry far*  lpPriorityList,
  844.                                           int               cEntries)
  845. {
  846.   LPENUMFORMATETC     lpEnumFmtEtc = NULL;
  847.   FORMATETC           rgfmtetc[MaxFormatEtc];
  848.   int                 i;
  849.   HRESULT             hrErr;
  850.   long                j, cFetched;
  851.  
  852.   //
  853.   // Clear all marks
  854.   //
  855.   for (i = 0; i < cEntries; i++) {
  856.     lpPriorityList[i].dwScratchSpace = FALSE;
  857.  
  858.     if (! lpPriorityList[i].fmtetc.cfFormat) {
  859.       //
  860.       // Caller wants this item always considered available
  861.       // (by specifying a NULL format)
  862.       //
  863.       lpPriorityList[i].dwScratchSpace = TRUE;
  864.     }
  865.     else if (lpPriorityList[i].fmtetc.cfFormat == cfEmbeddedObject
  866.             || lpPriorityList[i].fmtetc.cfFormat == cfEmbedSource
  867.             || lpPriorityList[i].fmtetc.cfFormat == cfFileName) {
  868.  
  869.       //
  870.       // If there is an OLE object format, then handle it
  871.       // specially by calling OleQueryCreateFromData. the caller
  872.       // need only specify one object type format.
  873.       //
  874.       TRACEX(OwlOleDlg, 1, "OleQueryCreateFromData called");
  875.       hrErr = OleQueryCreateFromData(lpSrcDataObj);
  876.  
  877.       if(NOERROR == hrErr) {
  878.         lpPriorityList[i].dwScratchSpace = TRUE;
  879.       }
  880.     }
  881.     else if (lpPriorityList[i].fmtetc.cfFormat == cfLinkSource) {
  882.  
  883.       //
  884.       // If there is OLE 2.0 LinkSource format, then handle it
  885.       // specially by calling OleQueryLinkFromData.
  886.       //
  887.       TRACEX(OwlOleDlg, 1, "OleQueryLinkFromData called");
  888.       hrErr = OleQueryLinkFromData(lpSrcDataObj);
  889.       if(NOERROR == hrErr) {
  890.         lpPriorityList[i].dwScratchSpace = TRUE;
  891.       }
  892.     }
  893.   }
  894.  
  895.   TRACEX(OwlOleDlg, 1, "IDataObject::EnumFormatEtc called" );
  896.   hrErr = lpSrcDataObj->EnumFormatEtc(DATADIR_GET,
  897.                                       (LPENUMFORMATETC FAR*)&lpEnumFmtEtc);
  898.  
  899.   if (hrErr != NOERROR) {
  900.     TRACEX(OwlOleDlg, 1, "Unable to get format enumerator." );
  901.     return;   
  902.   }
  903.  
  904.   //
  905.   // Enumerate the formats offered by the source
  906.   // Loop over all formats offered by the source
  907.   //
  908.   cFetched = 0;
  909.   memset(rgfmtetc, 0, sizeof(rgfmtetc[MaxFormatEtc]));
  910.   if (lpEnumFmtEtc->Next(MaxFormatEtc, rgfmtetc, (ULONG*)&cFetched) == NOERROR
  911.     || (cFetched > 0 && cFetched <= MaxFormatEtc) ) {
  912.  
  913.     for (j = 0; j < cFetched; j++) {
  914.       for (i = 0; i < cEntries; i++) {
  915.         if (! lpPriorityList[i].dwScratchSpace &&
  916.           CompareFormatEtc(&rgfmtetc[(int)j], &lpPriorityList[i].fmtetc)==0) {
  917.           lpPriorityList[i].dwScratchSpace = TRUE;
  918.         }
  919.       }
  920.     }
  921.   } 
  922.  
  923.   //
  924.   // Clean up
  925.   //
  926.   if (lpEnumFmtEtc)
  927.     lpEnumFmtEtc->Release();
  928. }
  929.  
  930. //
  931. // Toggles between Paste and PasteLink. NOTE: The Paste and PasteLink
  932. //  ListBoxes are always invisible. The third visible listbox [displayListBox]
  933. // is filled with the contents of the Paste or PasteLink Listbox
  934. //
  935. bool
  936. TPasteSpecialDlg::TogglePasteType(TPasteSpecialFlags flag)
  937. {
  938.   PRECONDITION(flag == psSelectPaste  ||
  939.                flag == psSelectPasteLink);
  940.  
  941.   //
  942.   // Skip if option's already selected
  943.   //
  944.   if (Helper.Flags & flag)
  945.     return true;
  946.  
  947.   //
  948.   // Update Flags
  949.   //
  950.   Helper.Flags= (Helper.Flags & ~(psSelectPaste | psSelectPasteLink)) | flag;
  951.  
  952.   //
  953.   // Prevent icon display flashes
  954.   //
  955.   Activate(IconImage, false);
  956.  
  957.   //
  958.   // Update Source and Icon displays and save index
  959.   //
  960.   TListBox *fromList = 0;
  961.   if (Helper.Flags & psSelectPaste) {
  962.     //
  963.     // Set source of object in clipboard
  964.     //
  965.     SourceText->SetText(Helper.SourceOfDataOD);
  966.  
  967.     //
  968.     // Update Icon is one's available
  969.     //
  970.     if (Helper.MetaPictOD)
  971.       IconImage->SetMetaPict(Helper.MetaPictOD);
  972.  
  973.     //
  974.     // Save current PasteLink index
  975.     //
  976.     Helper.PasteLinkListCurSel = DisplayList->GetSelIndex();
  977.     if (Helper.PasteLinkListCurSel == LB_ERR)
  978.       Helper.PasteLinkListCurSel = 0;
  979.  
  980.     //
  981.     // Clear link flag
  982.     //
  983.     Helper.Link = false;
  984.  
  985.     fromList    = PasteList;
  986.   }
  987.   else { // PasteLink selected
  988.     //
  989.     // Update source of object in clipboard
  990.     //
  991.     SourceText->SetText(Helper.SourceOfDataLSD);
  992.  
  993.     //
  994.     // Update Icon if one's available
  995.     //
  996.     if (Helper.MetaPictLSD)
  997.       IconImage->SetMetaPict(Helper.MetaPictLSD);
  998.  
  999.     //
  1000.     // Save current Paste index
  1001.     //
  1002.     Helper.PasteListCurSel = DisplayList->GetSelIndex();
  1003.     if (Helper.PasteListCurSel == LB_ERR)
  1004.       Helper.PasteListCurSel = 0;
  1005.  
  1006.     //
  1007.     // Set Link Flag
  1008.     //
  1009.     Helper.Link = true;
  1010.  
  1011.     fromList    = PasteLinkList;
  1012.   }
  1013.  
  1014.   //
  1015.   // Refill display list
  1016.   //
  1017.   DisplayList->SetRedraw(false);
  1018.   DisplayList->ClearList();
  1019.  
  1020.   int itemCount = fromList->GetCount();
  1021.   TPointer<char> buff = new char[GenericBufferLen];
  1022.   for (int i=0; i<itemCount; i++) {
  1023.     //
  1024.     // Store strings
  1025.     //
  1026.     fromList->GetString(buff, i);
  1027.     DisplayList->InsertString(buff, i);
  1028.  
  1029.     //
  1030.     // Store item data
  1031.     //
  1032.     DisplayList->SetItemData(i, fromList->GetItemData(i));
  1033.   }
  1034.  
  1035.   //
  1036.   // Retore saved index
  1037.   //
  1038.   if (Helper.Flags & psSelectPaste)
  1039.     DisplayList->SetSelIndex(Helper.PasteListCurSel);
  1040.   else
  1041.     DisplayList->SetSelIndex(Helper.PasteLinkListCurSel);
  1042.  
  1043.   //
  1044.   // Update Display Listbox
  1045.   //
  1046.   DisplayList->SetRedraw(true);
  1047.   DisplayList->Invalidate();
  1048.   DisplayList->UpdateWindow();
  1049.  
  1050.   //
  1051.   // Focus on Display Listbox
  1052.   //
  1053.   DisplayList->SetFocus();
  1054.  
  1055.   //
  1056.   // Allow updating of 'displayAsIcon', help result and bitmap
  1057.   // according to the new selection
  1058.   //
  1059.   ChangeListSelection();
  1060.  
  1061.   return false;
  1062. }
  1063.  
  1064. //
  1065. // Called when user changes the selection in the Listbox to enable/disable
  1066. // 'DisplayAsIcon' and update the result text and bitmap.
  1067. //
  1068. void
  1069. TPasteSpecialDlg::ChangeListSelection()
  1070. {
  1071.   //
  1072.   // Update result text&bitmap and enable/disable 'DisplayAsIcon'
  1073.   //
  1074.   EnableDisplayAsIcon();
  1075.   SetPasteSpecialHelpResults();
  1076.  
  1077.   //
  1078.   // Retrieve selected index
  1079.   //
  1080.   int curSel = DisplayList->GetSelIndex();
  1081.   if (curSel == LB_ERR)
  1082.     return;
  1083.  
  1084.   //
  1085.   // Retrieve item data associated with index
  1086.   //
  1087.   TPasteListItemData FAR* itemData;
  1088.   itemData = (TPasteListItemData FAR*)DisplayList->GetItemData(curSel);
  1089.  
  1090.   if ((long)itemData == LB_ERR)
  1091.    return;
  1092.  
  1093.   //
  1094.   // Save index
  1095.   //
  1096.   Helper.SelectedIndex = itemData->nPasteEntriesIndex;
  1097. }
  1098.  
  1099. //
  1100. // Enables/Disables 'DisplayAsIcon' based on the current source selection
  1101. // container's specification as outlined in table below:
  1102. //
  1103. //------------------------------------------------------------------------+
  1104. // Src. specifies | Src specifies      | Contnr specifies|  NET  RESULTS  |
  1105. // DVASPECT_ICON  | OLEMISC_ONLYICONIC | pfEnableIcon    | Check - Enable |
  1106. // ===============+====================+=================+================+
  1107. //    N/A         |     N/A            |     N           |   N        N   |
  1108. // ---------------+--------------------+-----------------+----------------|
  1109. //    N/A         |      Y             |     Y           |   Y        N   |
  1110. // ---------------+--------------------+-----------------+----------------|
  1111. //     Y          |      N             |     Y           |   Y        Y   |
  1112. // ---------------+--------------------+-----------------+----------------|
  1113. //     N          |      N             |     Y           |   N        Y   |
  1114. // ---------------+--------------------+-----------------+----------------|
  1115. //
  1116. void
  1117. TPasteSpecialDlg::EnableDisplayAsIcon()
  1118. {
  1119.   bool srcOnlyIconic = (Helper.Link) ? Helper.SrcOnlyIconicLSD :
  1120.                                        Helper.SrcOnlyIconicOD;
  1121.   bool srcAspectIcon = (Helper.Link) ? Helper.SrcAspectIconLSD :
  1122.                                        Helper.SrcAspectIconOD;
  1123.   HGLOBAL metaPict   = (Helper.Link) ? Helper.MetaPictLSD :
  1124.                                        Helper.MetaPictOD;
  1125.   bool cntrEnableIcon = false;;
  1126.   int  index = DisplayList->GetSelIndex();
  1127.  
  1128.   //
  1129.   // Get data of current selection
  1130.   //
  1131.   if (index != LB_ERR) {
  1132.     TPasteListItemData FAR* itemData;
  1133.     itemData = (TPasteListItemData FAR*)DisplayList->GetItemData(index);
  1134.     if ((long)itemData != LB_ERR) {
  1135.       cntrEnableIcon = itemData->fCntrEnableIcon;
  1136.     }
  1137.     else {
  1138.       cntrEnableIcon = false;
  1139.     }
  1140.   }
  1141.  
  1142.   //
  1143.   // If there's an icon available
  1144.   //
  1145.   if (metaPict) {
  1146.     //
  1147.     // Does container specify pfEnableIcon (OLEUIPASTE_ENABLEICON)?
  1148.     //
  1149.     if (!cntrEnableIcon) {
  1150.       //
  1151.       // Uncheck and disable 'DisplayAsIcon'
  1152.       //
  1153.       Helper.Flags &= ~psCheckDisplayAsIcon;
  1154.       DisplayAsIcon->Uncheck();
  1155.       DisplayAsIcon->EnableWindow(false);
  1156.  
  1157.       //
  1158.       // Hide the icon image and 'ChangeIcon'
  1159.       //
  1160.       Activate(ChangeIcon, false);
  1161.       Activate(IconImage, false);
  1162.     }
  1163.     else if (srcOnlyIconic) {     // Does SOURCE specify OLEMISC_ONLYICONIC
  1164.       //
  1165.       // Check and Disable 'DisplayAsIcon'
  1166.       //
  1167.       Helper.Flags |= psCheckDisplayAsIcon;
  1168.       DisplayAsIcon->Check();
  1169.       DisplayAsIcon->EnableWindow(false);
  1170.  
  1171.       //
  1172.       // Show iconImage and ChangeIcon button
  1173.       //
  1174.       Activate(ChangeIcon, true);
  1175.       Activate(IconImage, true);
  1176.     }
  1177.     else if (srcAspectIcon) {     // Does SOURCE specify DVASPECT_ICON
  1178.       //
  1179.       // Check and Enable 'DisplayAsIcon'
  1180.       //
  1181.       Helper.Flags |= psCheckDisplayAsIcon;
  1182.       DisplayAsIcon->Check();
  1183.       DisplayAsIcon->EnableWindow(true);
  1184.  
  1185.       //
  1186.       // Show iconImage and ChangeIcon button
  1187.       //
  1188.       Activate(ChangeIcon, true);
  1189.       Activate(IconImage, true);
  1190.     }
  1191.     else {
  1192.       //
  1193.       // Uncheck and Enable DisplayAsIcon
  1194.       //
  1195.       Helper.Flags &= ~psCheckDisplayAsIcon;
  1196.       DisplayAsIcon->Uncheck();
  1197.       DisplayAsIcon->EnableWindow(true);
  1198.  
  1199.       //
  1200.       // Hide IconImage and ChangeIcon button
  1201.       //
  1202.       Activate(ChangeIcon, false);
  1203.       Activate(IconImage, false);
  1204.     }
  1205.   }
  1206.   else {                      // No Icon is available
  1207.     //
  1208.     // Uncheck and Disable displayAsIcon
  1209.     //
  1210.     Helper.Flags &= ~psCheckDisplayAsIcon;
  1211.     DisplayAsIcon->Uncheck();
  1212.     DisplayAsIcon->EnableWindow(false);
  1213.  
  1214.     //
  1215.     // Hide iconImage and ChangeIcon button
  1216.     //
  1217.     Activate(ChangeIcon, false);
  1218.     Activate(IconImage, false);
  1219.   }
  1220. }
  1221.  
  1222. //
  1223. // Updates the flags; Hides/Shows the IconImage control and the
  1224. // ChangeIcon Button - Also updates the help result text and bitmap.
  1225. //
  1226. void
  1227. TPasteSpecialDlg::ToggleDisplayAsIcon()
  1228. {
  1229.   bool checked = IsDlgButtonChecked(IDC_DISPLAYASICON) ? true : false;
  1230.  
  1231.   //
  1232.   // Update Flags
  1233.   //
  1234.   if (checked)
  1235.     Helper.Flags |= psCheckDisplayAsIcon;
  1236.   else
  1237.     Helper.Flags &= ~psCheckDisplayAsIcon;
  1238.  
  1239.   //
  1240.   // Set result text and bitmap
  1241.   //
  1242.   SetPasteSpecialHelpResults();
  1243.  
  1244.   //
  1245.   // Show/hide Icon and ChgIcon button
  1246.   //
  1247.   Activate(IconImage, checked);
  1248.   Activate(ChangeIcon, checked);
  1249. }
  1250.  
  1251. //
  1252. //
  1253. //
  1254. bool
  1255. TPasteSpecialDlg::HasPercentS(const char far* str)
  1256. {
  1257.   if (!str)
  1258.     return false;
  1259.  
  1260.   //
  1261.   // Copy passed string to local buffer
  1262.   //
  1263.   TPointer<char> buff = new char[GenericBufferLen];
  1264.   strcpy(buff, str);
  1265.  
  1266.   char* pBuff = buff;
  1267.   while(*pBuff) {
  1268.     if (*pBuff == '%') {
  1269.       pBuff = AnsiNext(pBuff);
  1270.       if (*pBuff == 's') {
  1271.         return true;
  1272.       }
  1273.       else if (*pBuff == '%') {
  1274.         pBuff = AnsiNext(pBuff);
  1275.       }
  1276.     }
  1277.     else {
  1278.       pBuff = AnsiNext(pBuff);
  1279.     }
  1280.   }
  1281.   return false;
  1282. }
  1283.  
  1284. //
  1285. //
  1286. //
  1287. void
  1288. TPasteSpecialDlg::SetPasteSpecialHelpResults()
  1289. {
  1290.   int selIndex = DisplayList->GetSelIndex();
  1291.   if (selIndex == LB_ERR) {
  1292.     TRACEX(OwlOleDlg, 1, "No item selected to display HelpResults");
  1293.     return;
  1294.   }
  1295.  
  1296.   TPasteListItemData FAR *lpItemData;
  1297.   lpItemData = (TPasteListItemData FAR*)DisplayList->GetItemData(selIndex);
  1298.   if ((LRESULT)lpItemData == LB_ERR) {
  1299.     TRACEX(OwlOleDlg, 1, "No PasteListItemData found at " << selIndex);
  1300.     return;
  1301.   }
  1302.  
  1303.   int  nPasteEntriesIndex = lpItemData->nPasteEntriesIndex;
  1304.   bool fIsObject = HasPercentS(
  1305.          Data->ArrayPasteEntries[nPasteEntriesIndex].lpstrFormatName
  1306.                      );
  1307.  
  1308.   bool fDisplayAsIcon = (Helper.Flags & psCheckDisplayAsIcon) ? true : false;
  1309.   LPSTR lpszFullUserTypeName = Helper.Link ?  Helper.FullUserTypeNameLSD :
  1310.                                               Helper.FullUserTypeNameOD;
  1311.   LPSTR lpszInsert = lpszFullUserTypeName;
  1312.  
  1313.   uint iString = 0;
  1314.   uint iImage  = 0;
  1315.  
  1316.   if (Helper.Flags & psSelectPaste) {
  1317.     if (fIsObject) {
  1318.       iString = fDisplayAsIcon ? IDS_PSPASTEOBJECTASICON : IDS_PSPASTEOBJECT;
  1319.       iImage  = fDisplayAsIcon ? riEmbedIcon : riEmbed;
  1320.       lpszInsert = Helper.AppName;
  1321.     }
  1322.     else {
  1323.       iString = IDS_PSPASTEDATA;
  1324.       iImage  = riPaste;
  1325.     }
  1326.   }
  1327.   else if (Helper.Flags & psSelectPasteLink) {
  1328.     if (fIsObject) {
  1329.       iString = fDisplayAsIcon ? IDS_PSPASTELINKOBJECTASICON :
  1330.                                  IDS_PSPASTELINKOBJECT;
  1331.       iImage  = fDisplayAsIcon ? riLinkIcon : riLink;
  1332.     }
  1333.     else {
  1334.       iString = IDS_PSPASTELINKDATA;
  1335.       iImage  = riLink;
  1336.     }
  1337.   }
  1338.   else {
  1339.     TRACEX(OwlOleDlg, 1, "Non-OLE object - Error!");
  1340.     iString = IDS_PSNONOLE;
  1341.     iImage  = riPaste;
  1342.   }
  1343.  
  1344.   //
  1345.   // Allocator buffers to build help text
  1346.   //
  1347.   TPointer<char> pstr1 = new char[GenericBufferLen];
  1348.   TPointer<char> pstr2 = new char[GenericBufferLen];
  1349.   TPointer<char> pstr3 = new char[GenericBufferLen];
  1350.   TPointer<char> pstr4 = new char[GenericBufferLen];
  1351.  
  1352.   //
  1353.   // Build String from resource.
  1354.   //
  1355.   *((char*)pstr1) = 0;    // Default to empty string
  1356.   if (GetModule()->LoadString(iString, (LPSTR)(char*)pstr1, GenericBufferLen)) {
  1357.     wsprintf((LPSTR)(char*)pstr3,
  1358.              Data->ArrayPasteEntries[nPasteEntriesIndex].lpstrResultText,
  1359.              lpszInsert);
  1360.  
  1361.     wsprintf((LPSTR)(char*)pstr4, (LPSTR)(char*)pstr1, (LPSTR)(char*)pstr3);
  1362.     strcpy(pstr1, pstr4);
  1363.   }
  1364.  
  1365.   //
  1366.   // Update Display
  1367.   //
  1368.   ResultText->SetText(pstr1);
  1369.   ResultImage->SetBitmapIndex(iImage);
  1370. }
  1371.  
  1372. //
  1373. // Adds the name of an object to the Paste or PasteLink listboxes.
  1374. //
  1375. bool
  1376. TPasteSpecialDlg::AddPasteListItem(TListBox &list, bool insertFirst,
  1377.                                    int pasteEntryIndex,
  1378.                                    const char far* fullUserTypeName)
  1379. {
  1380.   TPointer<char> buff = new char[GenericBufferLen];
  1381.   TPasteListItemData FAR* pItemData = new TPasteListItemData;
  1382.  
  1383.   //
  1384.   // Initialize PasteList Item Data
  1385.   //
  1386.   pItemData->nPasteEntriesIndex = pasteEntryIndex;
  1387.   if (Data->ArrayPasteEntries[pasteEntryIndex].dwFlags &  pfEnableIcon) {
  1388.     pItemData->fCntrEnableIcon = TRUE;
  1389.   }
  1390.   else {
  1391.     pItemData->fCntrEnableIcon = FALSE;
  1392.   }
  1393.  
  1394.   //
  1395.   // Build the listbox entry string
  1396.   //
  1397.   wsprintf((LPSTR)(char*)buff,
  1398.            Data->ArrayPasteEntries[pasteEntryIndex].lpstrFormatName,
  1399.            (LPSTR)fullUserTypeName);
  1400.  
  1401.   //
  1402.   // Add data if it's not a duplicate
  1403.   //
  1404.   if (list.FindString(buff, 0) == LB_ERR) {
  1405.     int index = insertFirst ? list.InsertString(buff, 0) : list.AddString(buff);
  1406.     list.SetItemData(index, (LPARAM)pItemData);
  1407.   }
  1408.   else {
  1409.     delete pItemData;
  1410.   }
  1411.   return true;
  1412. }
  1413.  
  1414. //
  1415. // Fill the (invisible) Paste listbox with the formats offered by the
  1416. // Clipboard object and asked form by the container
  1417. //
  1418. bool
  1419. TPasteSpecialDlg::FillPasteList()
  1420. {
  1421.   int   items = 0;
  1422.   int   defFormat = -1;
  1423.   bool  insertFirst = false;
  1424.   bool  tryObjFmt   = false;
  1425.   bool  exclude     = false;
  1426.  
  1427.   //
  1428.   // Loop through target's priority list of formats
  1429.   //
  1430.   for (int i=0; i<Data->cPasteEntries; i++) {
  1431.     if (Data->ArrayPasteEntries[i].dwFlags != pfPasteOnly  &&
  1432.         !(Data->ArrayPasteEntries[i].dwFlags & pfPaste))
  1433.       continue;
  1434.  
  1435.     insertFirst = false;
  1436.  
  1437.     if (Data->ArrayPasteEntries[i].fmtetc.cfFormat == cfFileName  ||
  1438.         Data->ArrayPasteEntries[i].fmtetc.cfFormat == cfEmbeddedObject ||
  1439.         Data->ArrayPasteEntries[i].fmtetc.cfFormat == cfEmbedSource) {
  1440.       if (!tryObjFmt) {
  1441.         tryObjFmt = true;       // Only use first object format
  1442.         insertFirst = true;     // OLE obj. format should always be first
  1443.  
  1444.         //
  1445.         // Check exclusion list
  1446.         //
  1447.         exclude = false;
  1448.         for( int j=0; j < (int)Data->cClsIdExclude; j++) {
  1449.           if (IsEqualCLSID(Helper.ClsIdOD, *(Data->lpClsIdExclude+j))) {
  1450.             exclude = true;
  1451.             break;
  1452.           }
  1453.         }
  1454.  
  1455.         //
  1456.         // Skip object if in exclusion list
  1457.         //
  1458.         if (exclude)
  1459.           continue;
  1460.       }
  1461.       else {
  1462.         //
  1463.         // Already added an object format to list
  1464.         //
  1465.         continue;
  1466.       }
  1467.     }
  1468.  
  1469.     //
  1470.     // Ass to list if entry is marked true
  1471.     //
  1472.     if (Data->ArrayPasteEntries[i].dwScratchSpace) {
  1473.       if (defFormat < 0) {
  1474.         defFormat = insertFirst ? 0 : items;
  1475.       }
  1476.       else if (insertFirst) {
  1477.         defFormat++;
  1478.       }
  1479.       if (AddPasteListItem(*PasteList, insertFirst, i,
  1480.                            Helper.FullUserTypeNameOD)) {
  1481.         items++;
  1482.       }
  1483.       else {
  1484.         FreeListData(*PasteList);
  1485.         return false;
  1486.       }
  1487.     }
  1488.   }
  1489.  
  1490.   //
  1491.   // Initialize select to first matching format
  1492.   //
  1493.   if (defFormat >= 0)
  1494.     Helper.PasteListCurSel = defFormat;
  1495.  
  1496.   return (items > 0) ? true : false;
  1497. }
  1498.  
  1499. //
  1500. // Fill the (invisible) PasteLink listbox with the formats offered by the
  1501. // Clipboard object and asked form by the container
  1502. //
  1503. bool
  1504. TPasteSpecialDlg::FillPasteLinkList()
  1505. {
  1506.   int  items = 0;
  1507.   int  defFormat = -1;
  1508.   bool tryObjFmt = false;
  1509.   bool insertFirst = true;
  1510.   LPDATAOBJECT lpSrcDataObj = Data->lpSrcDataObj;
  1511.  
  1512.   FORMATETC fmtetc;
  1513.   memset(&fmtetc, 0, sizeof(fmtetc));
  1514.  
  1515.   //
  1516.   // Array of flags indicating which link types are supported
  1517.   //
  1518.   bool arrayLinkTypesSupported[MaxLinkTypes];
  1519.   bool linkTypeSupported = false;
  1520.  
  1521.   for (int i=0; i<Data->cLinkTypes; i++) {
  1522.     if (Data->ArrayLinkTypes[i] == cfLinkSource) {
  1523.       TRACEX(OwlOleDlg, 1, "Calling OleQueryLinkFromData.");
  1524.       if (OleQueryLinkFromData(lpSrcDataObj) == S_OK) {
  1525.         arrayLinkTypesSupported[i] = true;
  1526.         linkTypeSupported = true;
  1527.       }
  1528.       else {
  1529.         arrayLinkTypesSupported[i] = false;
  1530.       };
  1531.     }
  1532.     else {
  1533.       fmtetc.cfFormat = Data->ArrayLinkTypes[i];
  1534.       fmtetc.dwAspect = DVASPECT_CONTENT;
  1535.       fmtetc.tymed    = 0xFFFFFFFFL;      // All tymed values
  1536.       fmtetc.lindex   = -1;
  1537.  
  1538.       TRACEX(OwlOleDlg, 1, "IDataObject::QueryGetData called.");
  1539.       if (lpSrcDataObj->QueryGetData(&fmtetc) == S_OK) {
  1540.         arrayLinkTypesSupported[i] = true;
  1541.         linkTypeSupported = true;
  1542.       }
  1543.       else {
  1544.         arrayLinkTypesSupported[i] = false;
  1545.       }
  1546.     }
  1547.   }
  1548.  
  1549.   //
  1550.   // Check if link types are offered by SourceDataObject
  1551.   //
  1552.   if (!linkTypeSupported) {
  1553.     TRACEX(OwlOleDlg, 1, "No link types are offered by data source.");
  1554.     items = 0;
  1555.     return false;
  1556.   }
  1557.  
  1558.   //
  1559.   // Iterate through acceptable formats
  1560.   //
  1561.   for(i=0; i<Data->cPasteEntries; i++) {
  1562.     linkTypeSupported = false;
  1563.     //
  1564.     // Does container accept an link type offered by source object
  1565.     //
  1566.     if (Data->ArrayPasteEntries[i].dwFlags & pfLinkAnyType)
  1567.       linkTypeSupported = true;
  1568.     else  {
  1569.       //
  1570.       // Checked if link types offered by source are acceptable
  1571.       //
  1572.       int j=0;
  1573.       uint32 pasteFlag;
  1574.       for (pasteFlag = pfLinkType1, j=0;
  1575.            j < Data->cLinkTypes;
  1576.            pasteFlag *=2, j++) {
  1577.         if ((Data->ArrayPasteEntries[i].dwFlags & pasteFlag)  &&
  1578.             arrayLinkTypesSupported[i]) {
  1579.           linkTypeSupported = true;
  1580.           break;
  1581.         }
  1582.       }
  1583.  
  1584.       insertFirst = false;
  1585.       if ((Data->ArrayPasteEntries[i].fmtetc.cfFormat == cfFileName)  ||
  1586.           (Data->ArrayPasteEntries[i].fmtetc.cfFormat == cfLinkSource)) {
  1587.         if  (!tryObjFmt) {
  1588.           tryObjFmt = true;     // Only use first object format
  1589.           insertFirst = true;   // OLE object format should be first
  1590.         }
  1591.         else {
  1592.           continue;             // Already added object format to list
  1593.         }
  1594.       }
  1595.  
  1596.       //
  1597.       // Add to list if entry's marked true
  1598.       //
  1599.       if (linkTypeSupported  &&
  1600.           Data->ArrayPasteEntries[i].dwScratchSpace) {
  1601.         if (defFormat < 0) {
  1602.           defFormat = insertFirst ?  0 : items;
  1603.         }
  1604.         else if (insertFirst) {
  1605.           defFormat++;
  1606.         }
  1607.  
  1608.         if (AddPasteListItem(*PasteLinkList, insertFirst, i,
  1609.                               Helper.FullUserTypeNameLSD)) {
  1610.           items++;
  1611.         }
  1612.         else {
  1613.           FreeListData(*PasteLinkList);
  1614.           return false;
  1615.         }
  1616.       }
  1617.     }
  1618.   }
  1619.  
  1620.   //
  1621.   // Initialize selection to first matching format
  1622.   //
  1623.   if (defFormat >= 0)
  1624.     Helper.PasteLinkListCurSel = defFormat;
  1625.  
  1626.   items = PasteLinkList->GetCount();
  1627.   return (items > 0) ? true : false;
  1628. }
  1629.  
  1630. //
  1631. // Frees PasteListItem data memory associated with each listbox entry.
  1632. //
  1633. void
  1634. TPasteSpecialDlg::FreeListData(TListBox &list)
  1635. {
  1636.   TPasteListItemData FAR *lpItemData;
  1637.  
  1638.   int items = list.GetCount();
  1639.   for (int i=0; i<items; i++) {
  1640.     lpItemData = (TPasteListItemData FAR*)(LPARAM)list.GetItemData(i);
  1641.     if ((LPARAM)lpItemData != LB_ERR)
  1642.       delete lpItemData;
  1643.   }
  1644. }
  1645.  
  1646. //
  1647. // Allows user to change current icon via the ChangeIcon Dialog
  1648. //
  1649. void
  1650. TPasteSpecialDlg::ChangeIconClicked()
  1651. {
  1652.   TRY {
  1653.     //
  1654.     // Allocate and populate structure to hold parameters
  1655.     //
  1656.     TChangeIconDlg::TData &data = *new TChangeIconDlg::TData;
  1657.     TPointer<TChangeIconDlg::TData> _cln1(&data);
  1658.     data.MetaPict = IconImage->GetMetaPict();
  1659.     data.Flags    = ciSelectCurrent;
  1660.     data.ClsId    = Helper.Link ? Helper.ClsIdLSD : Helper.ClsIdOD;
  1661.  
  1662.     if (Helper.Flags & ioShowHelp)
  1663.       data.Flags |= ciShowHelp;
  1664.  
  1665.     //
  1666.     // Allocate dialog and execute
  1667.     //
  1668.     TChangeIconDlg &chgIconDlg = *new TChangeIconDlg(this, data);
  1669.     TPointer<TChangeIconDlg> _cln2(&chgIconDlg);
  1670.     if (chgIconDlg.Execute() == IDOK){
  1671.       //
  1672.       // Update display with new icon
  1673.       //
  1674.       IconImage->SetMetaPict(data.MetaPict);
  1675.  
  1676.       //
  1677.       // Save away new icon
  1678.       //
  1679.       if (Helper.Link)
  1680.         Helper.MetaPictLSD = data.MetaPict;
  1681.       else
  1682.         Helper.MetaPictOD = data.MetaPict;
  1683.     }
  1684.   }
  1685.   CATCH( (xmsg& msg){ GetModule()->Error(msg, 0); } )
  1686. }
  1687.  
  1688. //
  1689. // Switches to the PasteList
  1690. //
  1691. void
  1692. TPasteSpecialDlg::PasteClicked()
  1693. {
  1694.   TogglePasteType(psSelectPaste);
  1695. }
  1696.  
  1697. //
  1698. // Switches to the PasteLink List
  1699. //
  1700. void
  1701. TPasteSpecialDlg::PasteLinkClicked()
  1702. {
  1703.   TogglePasteType(psSelectPasteLink);
  1704. }
  1705.  
  1706. //
  1707. // Handles request to display Paste/Link as Icon
  1708. //
  1709. void
  1710. TPasteSpecialDlg::DisplayAsIconClicked()
  1711. {
  1712.   ToggleDisplayAsIcon();
  1713. }
  1714.  
  1715. //
  1716. // Handles change in selection of items to paste/pasteLink
  1717. //
  1718. void
  1719. TPasteSpecialDlg::DisplayListSelChange()
  1720. {
  1721.   ChangeListSelection();
  1722. }
  1723.  
  1724. //
  1725. // A double click on the Paste/PasteLink listbox is treated
  1726. // similar to hitting the OK button.
  1727. //
  1728. void
  1729. TPasteSpecialDlg::DisplayListDblClk()
  1730. {
  1731.   SendNotification(IDOK, BN_CLICKED, GetDlgItem(IDOK));
  1732. }
  1733.  
  1734. //
  1735. // Handles notification that Clipboard content has changed.
  1736. // This basically means bad news since the data we're currently
  1737. // displaying to the user for Pasting/PasteLinking is now inaccurate.
  1738. //
  1739. void
  1740. TPasteSpecialDlg::EvDrawClipboard()
  1741. {
  1742.   HWND hwndSaved = (HWND)GetProp(NEXTCBVIEWER);
  1743.   if (hwndSaved == HWND_BROADCAST)
  1744.     return;
  1745.  
  1746.   //
  1747.   // Take ourselves out of the loop
  1748.   //
  1749.   SetProp(NEXTCBVIEWER, HWND_BROADCAST);
  1750.   ::ChangeClipboardChain(*this, hwndSaved);
  1751.  
  1752.   //
  1753.   // Bring down the dialog
  1754.   //
  1755. }
  1756.  
  1757. //
  1758. // Handle notification that a Window is being removed from
  1759. // Clipboard chain.
  1760. //
  1761. void
  1762. TPasteSpecialDlg::EvChangeCBChain(HWND hwndRemoved, HWND hwndNext)
  1763. {
  1764.   //
  1765.   // If the one after us is going, save the one following it.
  1766.   //
  1767.   HWND hwndSaved = (HWND)GetProp(NEXTCBVIEWER);
  1768.   if (hwndRemoved == hwndSaved) {
  1769.     SetProp(NEXTCBVIEWER, hwndNext);
  1770.   //
  1771.   // Otherwise pass the buck along
  1772.   //
  1773.   }
  1774.   else if (hwndSaved  && hwndSaved != HWND_BROADCAST) {
  1775.     ::SendMessage(hwndSaved, WM_CHANGECBCHAIN, (WPARAM)hwndRemoved,
  1776.                   (LPARAM)hwndNext);
  1777.   }
  1778. }
  1779.  
  1780.