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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TInsertObjectDlg
  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. const char szNotInsertable[] = "\\NotInsertable";
  27. const char szProStdEditSrv[] = "\\protocol\\StdFileEditing\\server";
  28. const char szInsertable[]    = "\\Insertable";
  29. const char szCLSID2[]        = "CLSID\\";
  30. const char szInProcServer[]  = "\\InProcServer";
  31. const char szLocalServer[]   = "\\LocalServer";
  32.  
  33. //
  34. //
  35. //
  36. static bool
  37. GetAssociatedExecutable(char* /*extension*/, char* /*executable*/)
  38. {
  39.   return false; //! BB  For now
  40. }
  41.  
  42. //
  43. //
  44. //
  45. TInsertObjectDlg::TData::TData()
  46. {
  47.   Flags         = 0;
  48.   cClsIdExclude = 0;
  49.   lpClsIdExclude= 0;
  50.   MetaPict      = 0;
  51.   FileName      = 0;
  52.   ClsId         = CLSID_NULL;
  53. }
  54.  
  55.  
  56. DEFINE_RESPONSE_TABLE1(TInsertObjectDlg, TOleDialog)
  57.   EV_BN_CLICKED(IDC_CREATENEW,      CreateNewClicked),
  58.   EV_BN_CLICKED(IDC_CREATEFROMFILE, CreateFromFileClicked),
  59.   EV_BN_CLICKED(IDC_BROWSE,         BrowseFileClicked),
  60.   EV_BN_CLICKED(IDC_LINK,           LinkToFileClicked),
  61.   EV_BN_CLICKED(IDC_DISPLAYASICON,  DisplayAsIconClicked),
  62.   EV_BN_CLICKED(IDC_CHANGEICON,     ChangeIconClicked),
  63.   EV_LBN_SELCHANGE(IDC_OBJECTLIST,  ObjectListChanged),
  64.   EV_LBN_DBLCLK(IDC_OBJECTLIST,     ObjectListDoubleClicked),
  65.   EV_EN_CHANGE(IDC_FILENAME,        FileNameChanged),
  66.   EV_EN_KILLFOCUS(IDC_FILENAME,     FileNameKillFocus),
  67. END_RESPONSE_TABLE;
  68.  
  69. //
  70. // Static variales of TInserObjectDlg
  71. //
  72. CLSID   TInsertObjectDlg::DynamicScopeClsid = CLSID_NULL;
  73. OLECHAR TInsertObjectDlg::DynamicScopePath[MaxPathLen] = {0};
  74.  
  75. //
  76. //
  77. //
  78. TInsertObjectDlg::TInsertObjectDlg(TWindow* parent,
  79.                                    TOcInitInfo &initInfo,
  80.                                    TData  *data,
  81.                                    TResId templateId,
  82.                                    const char far* title,
  83.                                    TModule* module)
  84. :
  85.   TOleDialog(parent, templateId ? templateId : TResId(DLG_INSERTOBJECT),
  86.              title, module),
  87.   InitInfo(initInfo), Helper(*new THelper)
  88. {
  89.   ObjectList      = new TListBox(this, IDC_OBJECTLIST);
  90.   ObjectType      = new TStatic(this, IDC_OBJECTTYPE);
  91.   Link            = new TCheckBox(this, IDC_LINK);
  92.   File            = new TStatic(this, IDC_FILE);
  93.   Browse          = new TButton(this, IDC_BROWSE);
  94.   FileName        = new TEdit(this, IDC_FILENAME);
  95.   FileType        = new TStatic(this, IDC_FILETYPE);
  96.   ResultText      = new TStatic(this, IDC_RESULTTEXT);
  97.   DisplayAsIcon   = new TCheckBox(this, IDC_DISPLAYASICON);
  98.   ChangeIcon      = new TButton(this, IDC_CHANGEICON);
  99.   ResultImage     = new TResultImage(this, IDC_RESULTIMAGE);
  100.   IconImage       = new TIconImage(this, IDC_ICONIMAGE);
  101.   CreateNew       = new TRadioButton(this, IDC_CREATENEW);
  102.   CreateFromFile  = new TRadioButton(this, IDC_CREATEFROMFILE);
  103.   Help            = new TButton(this, IDHELP);
  104.   Ok              = new TButton(this, IDOK);
  105.  
  106.   Data       = data;
  107.   DeleteData = false;
  108. }
  109.  
  110. //
  111. //
  112. //
  113. TInsertObjectDlg::~TInsertObjectDlg()
  114. {
  115.   delete &Helper;
  116.  
  117.   if (DeleteData)
  118.     delete Data;
  119. }
  120.  
  121. //
  122. // Retrieves default parameters for InsertObject [if none was
  123. // specified] before excuting the dialog
  124. //
  125. int
  126. TInsertObjectDlg::DoExecute()
  127. {
  128.   if (!Data) {
  129.     if (!GetDefaultInsertObjectData())
  130.       return IDCANCEL;
  131.   }
  132.   return TDialog::DoExecute();
  133. }
  134.  
  135. bool
  136. TInsertObjectDlg::GetDefaultInsertObjectData()
  137. {
  138.   Data = new TData;
  139.   DeleteData = true;
  140.  
  141.   // Init Data members
  142.   //
  143.   Data->Flags         = ioSelectCreateNew|ioShowHelp;
  144.   Data->cClsIdExclude = 0;    //! BB Add code to exclude ourselves using
  145.   Data->lpClsIdExclude= 0;    //  OLE calls
  146.   Data->MetaPict      = 0;
  147.   Data->FileName      = DynamicScopePath;
  148.   Data->ClsId         = CLSID_NULL;
  149.  
  150.   return true;
  151. }
  152.  
  153. bool
  154. TInsertObjectDlg::EvInitDialog(HWND hwndFocus)
  155. {
  156.   TOleDialog::EvInitDialog(hwndFocus);
  157.  
  158.   // Make Copy of Flags
  159.   //
  160.   Helper.Flags    = Data->Flags;
  161.   Helper.ClsId    = Data->ClsId;
  162.   Helper.MetaPict = 0;
  163.  
  164.   if (Data->FileName  &&  *(Data->FileName))
  165.     strcpyn(Helper.FileName, Data->FileName, MaxPathLen);
  166.   else
  167.     Helper.FileName[0]=0;
  168.  
  169.   // Set Control Font
  170.   //
  171.   if (Font) {
  172.     ResultText->SetWindowFont(*Font, false);
  173.     FileType->SetWindowFont(*Font, false);
  174.   }
  175.  
  176.   // Fill ListBox with Insertable Servers
  177.   //
  178.   FillClassList();
  179.  
  180.   // Adjust ListBox
  181.   //
  182.   TRect lRect;
  183.   uint32 dbUnits = GetDialogBaseUnits();
  184.   ObjectList->GetClientRect(lRect);
  185.   int tabWidth = (8*lRect.right)/LOWORD(dbUnits);
  186.   ObjectList->SetTabStops(1, &tabWidth);
  187.  
  188.   // Initialize filename
  189.   //
  190.   if (Helper.FileName[0]) {
  191.     FileName->SetText(Helper.FileName);
  192.     Helper.FileDirty = ValidateInsertFile(false);
  193.   }
  194.   else {
  195.     char curDir[MaxPathLen];
  196.     if(getcwd(curDir, sizeof(curDir)));
  197.       FileName->SetText(curDir);
  198.  
  199.     Helper.FileDirty = true;
  200.   }
  201.  
  202.   //
  203.   // Init dialog for either CreateNew or CreateFromFile
  204.   //
  205.   if (Helper.Flags & ioSelectCreateNew) {
  206.     Activate(File, false);
  207.     Activate(FileType, false);
  208.     Activate(FileName, false);
  209.     Activate(Link, false);
  210.     Activate(Browse, false);
  211.  
  212.     CheckRadioButton(IDC_CREATENEW, IDC_CREATEFROMFILE, IDC_CREATENEW);
  213.     ObjectList->SetFocus();
  214.     Helper.AsIconNew = (Helper.Flags & ioCheckDisplayAsIcon) ? true : false;
  215.   }
  216.   else {  // ioSelectCreateFromFile
  217.     Activate(ObjectList, false);
  218.     Activate(ObjectType, false);
  219.  
  220.     if (Helper.FileDirty)
  221.       Helper.Flags &= ~ioCheckDisplayAsIcon;
  222.  
  223.     if (Helper.Flags & ioDisableLink) {
  224.       Activate(Link, false);
  225.     }
  226.     else {
  227.       if (Helper.Flags & ioCheckLink)
  228.         Link->Check();
  229.     }
  230.  
  231.     CheckRadioButton(IDC_CREATENEW, IDC_CREATEFROMFILE, IDC_CREATEFROMFILE);
  232.     Helper.AsIconFile = (Helper.Flags & ioCheckDisplayAsIcon) ? true : false;
  233.     FileName->SetFocus();
  234.   }
  235.  
  236.   // Handled Icon display flags
  237.   //
  238.   bool fAsIcon = (Helper.Flags & ioCheckDisplayAsIcon) ? true : false;
  239.   DisplayAsIcon->SetCheck(fAsIcon == false ? BF_UNCHECKED : BF_CHECKED);
  240.   Activate(ChangeIcon, fAsIcon);
  241.   Activate(IconImage, fAsIcon);
  242.  
  243.   // Handle ShowHelp flags
  244.   //
  245.   if (!(Helper.Flags & ioShowHelp))
  246.     Activate(Help, false);
  247.  
  248.   // Update Result display
  249.   //
  250.   UpdateClassIcon();
  251.   SetInsertObjectResults();
  252.  
  253.   // Disable IconDisplay control if requested
  254.   //
  255.   if (Helper.Flags & ioDisableDisplayAsIcon) {
  256.     Activate(DisplayAsIcon, false);
  257.     Activate(ChangeIcon, false);
  258.     Activate(IconImage, false);
  259.   }
  260.  
  261.   return false;
  262. }
  263.  
  264. //
  265. //  Update TOcInitInfo member using the Data information.
  266. //
  267. bool
  268. TInsertObjectDlg::OleDlgOk()
  269. {
  270.   // Set Focus on OK button
  271.   //
  272.   if (GetFocus() != *Ok)
  273.     Ok->SetFocus();
  274.  
  275.   if ((Helper.Flags & ioSelectCreateFromFile)  &&  Helper.FileDirty) {
  276.     if (ValidateInsertFile()) {
  277.       Helper.FileDirty = false;
  278.       Helper.FileValid = true;
  279.       UpdateClassIcon();
  280.       UpdateClassType(true);
  281.     }
  282.     else {  //  Invalid file name specified
  283.       Helper.FileDirty = false;
  284.       Helper.FileValid = false;
  285.       FileName->SetFocus();
  286.       FileName->SetSelection(0, -1);
  287.       return false;
  288.     }
  289.   }
  290.   else if ((Helper.Flags & ioSelectCreateFromFile) && !Helper.FileValid) {
  291.     char file[MaxPathLen];
  292.     if (FileName->GetText(file, sizeof(file))) {
  293.       OpenFileError(Helper.ErrCode, file);
  294.     }
  295.     Helper.FileDirty = false;
  296.     Helper.FileValid = false;
  297.  
  298.     FileName->SetFocus();
  299.     FileName->SetSelection(0, -1);
  300.  
  301.     UpdateClassType(false);
  302.     return false;
  303.   }
  304.  
  305.   LPSTR lpszCLSID;
  306.   char buff[MaxKeyLen+CLSIDStringLen];
  307.  
  308.   // Copy info to user's structure
  309.   //
  310.   Data->Flags = Helper.Flags;
  311.  
  312.   if (Helper.Flags & ioSelectCreateNew) {
  313.     int selIndex = ObjectList->GetSelIndex();
  314.  
  315.     // Retrieve Icon and set to 0 to prevent deletion
  316.     //
  317.     if (Helper.Flags & ioCheckDisplayAsIcon) {
  318.       Data->MetaPict = (HGLOBAL)ObjectList->GetItemData(selIndex);
  319.       ObjectList->SetItemData(selIndex, 0);
  320.     }
  321.     else {
  322.       Data->MetaPict = 0;
  323.     }
  324.  
  325.     // Retrieve CLSID
  326.     //
  327.     ObjectList->GetString(buff, selIndex);
  328.     lpszCLSID = PtrToNthField(buff, 2, '\t');
  329.     CLSIDFromString(lpszCLSID, &Data->ClsId);
  330.   }
  331.   else {    //  ioSelectCreateFromFile
  332.     if (Helper.Flags & ioCheckDisplayAsIcon) {
  333.       Data->MetaPict = IconImage->GetMetaPict();
  334.     }
  335.     else {
  336.       Data->MetaPict = 0;
  337.     }
  338.     FileName->GetText(Helper.FileName, MaxPathLen);
  339.     strcpyn(Data->FileName, Helper.FileName, MaxPathLen);
  340.   }
  341.  
  342.   //
  343.   // Update TOcInitInfo members
  344.   //
  345.   InitInfo.HIcon =(Data->Flags & ioCheckDisplayAsIcon) ?
  346.                   (HICON)Data->MetaPict : NULL;
  347.   if (Data->Flags & ioSelectCreateNew) {
  348.     InitInfo.Where    = iwNew;
  349.     InitInfo.How      = ihEmbed;
  350.     DynamicScopeClsid = Data->ClsId;
  351.     InitInfo.CId      = (BCID)&DynamicScopeClsid;
  352.   }
  353.   else if (Data->Flags & ioSelectCreateFromFile) {
  354.     InitInfo.Where = iwFile;
  355.     InitInfo.How   = (Data->Flags & ioCheckLink) ? ihLink : ihEmbed;
  356.     InitInfo.Path  = strnewdup(Data->FileName);
  357.   }
  358.   return true;
  359. }
  360.  
  361. //
  362. // Cleanup metafiles stored as itemData in ListBox
  363. //
  364. void
  365. TInsertObjectDlg::CleanupWindow()
  366. {
  367.   int count = ObjectList->GetCount();
  368.  
  369.   HGLOBAL hMetaPict;
  370.   for (int i=0; i<count; i++) {
  371.     hMetaPict = (HGLOBAL)ObjectList->GetItemData(i);
  372.     if (hMetaPict) {
  373.       TOleMetaPict::Free(hMetaPict);
  374.       ObjectList->SetItemData(i, 0);
  375.     }
  376.   }
  377.  
  378.   TOleDialog::CleanupWindow();
  379. }
  380.  
  381. //
  382. //
  383. //
  384. int
  385. TInsertObjectDlg::FillClassList()
  386. {
  387.   HKEY hKey;
  388.   CLSID clsid;
  389.   LPSTR pszID= 0;
  390.   int strIndex = 0;
  391.  
  392.   char* pExec = new char[MaxKeyLen]; 
  393.   TPointer<char> _pe = pExec;
  394.  
  395.   char* pClass= new char[MaxKeyLen];
  396.   TPointer<char> _pc = pClass;
  397.  
  398.   char* pKey  = new char[MaxKeyLen];
  399.   TPointer<char> _pk = pKey;
  400.  
  401.   //
  402.   // Retrieve task memory allocator
  403.   //
  404.   TOleAllocator oleAllocator;
  405.  
  406.   //
  407.   // Return error if  cannot open the root key
  408.   //
  409.   if (RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey) != ERROR_SUCCESS) {
  410.     TRACEX(OwlOleDlg, 1, "Unable to open Reg. DB");
  411.     return -1;
  412.   }
  413.  
  414.   // Clear ListBox
  415.   //
  416.   ObjectList->ClearList();
  417.  
  418.   while(RegEnumKey(hKey, strIndex++, pClass, MaxKeyLen) == ERROR_SUCCESS) {
  419.     if (pszID) {
  420.       oleAllocator.Free(pszID);
  421.       pszID = 0;
  422.     }
  423.  
  424.     int classLen = strlen(pClass);
  425.  
  426.     // Skip entries with not-insertable subkeys
  427.     //
  428.     LONG buffSize = MaxKeyLen;
  429.     strcpy(pClass+classLen, szNotInsertable);
  430.     if (RegQueryValue(hKey, pClass, pKey, &buffSize) == ERROR_SUCCESS)
  431.       continue;
  432.  
  433.     // Append and check for "\\protocol\\StdFileEditiing\\server" entry
  434.     //
  435.     buffSize = MaxKeyLen;
  436.     strcpy(pClass+classLen, szProStdEditSrv);
  437.     if (RegQueryValue(hKey, pClass, pKey, &buffSize) == ERROR_SUCCESS) {
  438.  
  439.       // Retrieve full user type name
  440.       //
  441.       *(pClass+classLen) = 0;
  442.       buffSize = MaxKeyLen;
  443.       if (RegQueryValue(hKey, pClass, pKey, &buffSize) != ERROR_SUCCESS)
  444.         continue;
  445.     }
  446.     else {
  447.       // Skip if no "\\Insertable" entry is found
  448.       //
  449.       strcpy(pClass+classLen, szInsertable);
  450.       buffSize = MaxKeyLen;
  451.       if (RegQueryValue(hKey, pClass, pKey, &buffSize) != ERROR_SUCCESS)
  452.         continue;
  453.  
  454.       // Reset class
  455.       //
  456.       *(pClass+classLen) = 0;
  457.  
  458.       // Check for "\\CLSID" subkey as hint to look for second entry
  459.       // [GUID entries] of registration
  460.       //
  461.       buffSize = MaxKeyLen;
  462.       pszID    = (char*)oleAllocator.Alloc(MaxKeyLen);
  463.       strcat(pClass+classLen, ClsIdStr);
  464.       if (RegQueryValue(hKey, pClass, pszID, &buffSize) != ERROR_SUCCESS)
  465.         continue;
  466.  
  467.       // Look for ROOT\CLSID\'GUID'\LocalServer
  468.       //
  469.       strcpy(pExec, szCLSID2);
  470.       strcat(pExec, pszID);
  471.       int execLen = strlen(pExec);
  472.       strcpy(pExec+execLen, szLocalServer);
  473.  
  474.       buffSize = MaxKeyLen;
  475.       if (RegQueryValue(hKey, pExec, pKey, &buffSize) != ERROR_SUCCESS) {
  476.  
  477.         // Look for ROOT\CLSID\'GUID'\InProcServer
  478.         //
  479.         strcpy(pExec+execLen, szInProcServer);
  480.  
  481.         buffSize = MaxKeyLen;
  482.         if (RegQueryValue(hKey, pExec, pKey, &buffSize)!= ERROR_SUCCESS)
  483.           continue;
  484.       }
  485.     }
  486.  
  487.     if (!pszID) {
  488.       HRESULT result = CLSIDFromProgID((LPCOLESTR)(char*)pClass, &clsid);
  489.       if (result != S_OK)
  490.         continue;
  491.  
  492.       StringFromCLSID(clsid,  &pszID);
  493.     }
  494.     else {
  495.       CLSIDFromString(pszID, &clsid);
  496.     }
  497.  
  498.     if (!pszID || IsEqualCLSID(clsid, CLSID_NULL))
  499.       continue;
  500.  
  501.     // Tag classID to string and add latter to the list if
  502.     // it's not already there.
  503.     //
  504.     if (ObjectList->FindString(pKey, 0) == LB_ERR) {
  505.       strcat(pKey, "\t");
  506.       strcat(pKey, pszID);
  507.       ObjectList->AddString(pKey);
  508.  
  509.       TRACEX(OwlOleDlg, 1, "Adding " << pKey);
  510.     }
  511.  
  512.     // Free CLSID String using task allocator
  513.     //
  514.     if (pszID) {
  515.       oleAllocator.Free(pszID);
  516.       pszID = 0;
  517.     }
  518.   }
  519.   RegCloseKey(hKey);
  520.  
  521.   // Select first entry
  522.   //
  523.   ObjectList->SetSelIndex(0);
  524.  
  525.   // Return list count
  526.   //
  527.   return ObjectList->GetCount();
  528. }
  529.  
  530. //
  531. //
  532. //
  533. bool
  534. TInsertObjectDlg::ToggleObjectSource(TInsertObjectFlags flag)
  535. {
  536.   PRECONDITION((flag == ioSelectCreateNew) ||
  537.                (flag == ioSelectCreateFromFile));
  538.  
  539.   if (Helper.Flags & flag)
  540.     return true;
  541.  
  542.   // Store current file icon
  543.   //
  544.   if (flag == ioSelectCreateNew  &&  (Helper.Flags & ioCheckDisplayAsIcon))
  545.     Helper.MetaPict = IconImage->GetMetaPict();
  546.  
  547.   // Enable/Disable Icon related
  548.   //
  549.   bool asIcon = (flag == ioSelectCreateNew) ? Helper.AsIconNew :
  550.                                               Helper.AsIconFile;
  551.   if (asIcon) {
  552.     Helper.Flags |=  ioCheckDisplayAsIcon;
  553.     DisplayAsIcon->Check();
  554.   }
  555.   else {
  556.     Helper.Flags &= ~ioCheckDisplayAsIcon;
  557.     DisplayAsIcon->Uncheck();
  558.   }
  559.  
  560.   ChangeIcon->EnableWindow(asIcon);
  561.  
  562.   // Hide/Show appropriate controls
  563.   //
  564.   bool enable = (flag == ioSelectCreateNew) ? true : Helper.FileSelected;
  565.   DisplayAsIcon->EnableWindow(enable);
  566.   Ok->EnableWindow(enable);
  567.   Link->EnableWindow(enable);
  568.  
  569.   enable = (flag == ioSelectCreateNew) ? true : false;
  570.   FileName->EnableWindow(enable);
  571.   Browse->EnableWindow(enable);
  572.   Activate(ObjectList, enable);
  573.   Activate(ObjectType, enable);
  574.  
  575.   enable = (flag == ioSelectCreateFromFile) ? true : false;
  576.   Activate(File, enable);
  577.   Activate(FileType, enable);
  578.   Activate(FileName, enable);
  579.   Activate(Browse, enable);
  580.  
  581.   if (Helper.Flags & ioDisableLink)
  582.     enable = false;
  583.   Activate(Link, enable);
  584.  
  585.   // Update Flag
  586.   //
  587.   Helper.Flags =
  588.        (Helper.Flags & ~(ioSelectCreateFromFile|ioSelectCreateNew)) | flag;
  589.  
  590.   enable = (Helper.Flags & ioCheckDisplayAsIcon) ? true : false;
  591.   Activate(ChangeIcon, enable);
  592.   Activate(IconImage, enable);
  593.  
  594.   // Update result display
  595.   //
  596.   SetInsertObjectResults();
  597.  
  598.   // Set Focus to newly enable control
  599.   //
  600.   if (flag == ioSelectCreateNew) {
  601.     UpdateClassIcon();
  602.     ObjectList->SetFocus();
  603.   }
  604.   else { // ioSelectCreateFromFile
  605.     if (Helper.AsIconFile  &&  Helper.MetaPict) {
  606.       IconImage->SetMetaPict(Helper.MetaPict);
  607.       Helper.MetaPict = 0;
  608.     }
  609.     else {
  610.       UpdateClassIcon();
  611.     }
  612.     Browse->SetFocus();
  613.   }
  614.   return false;
  615. }
  616.  
  617. //
  618. //
  619. //
  620. void
  621. TInsertObjectDlg::UpdateClassType(bool setText)
  622. {
  623.   CLSID clsid;
  624.   char  fileName[MaxPathLen];
  625.   char  fileType[MaxLabelLen];
  626.  
  627.   fileType[0] = 0;
  628.  
  629.   if (setText) {
  630.     FileName->GetText(fileName, sizeof(fileName));
  631.     if (GetClassFile(fileName, &clsid) == S_OK)
  632.       GetUserTypeOfClass(clsid, fileType, sizeof(fileType));
  633.   }
  634.  
  635.   FileType->SetText(fileType);
  636. }
  637.  
  638. //
  639. //
  640. //
  641. void
  642. TInsertObjectDlg::UpdateClassIcon()
  643. {
  644.   if (!(Helper.Flags & ioCheckDisplayAsIcon))
  645.     return;
  646.  
  647.   int index = ObjectList->GetSelIndex();
  648.   if (index == LB_ERR)
  649.     return;
  650.  
  651.   HGLOBAL iconMeta = 0;
  652.  
  653.   // Handle 'CreateNew'
  654.   //
  655.   if (Helper.Flags & ioSelectCreateNew) {
  656.     // Check if we did not store the icon earlier
  657.     //
  658.     uint32 itemData = ObjectList->GetItemData(index);
  659.     if (itemData) {
  660.       IconImage->SetMetaPict((HGLOBAL)itemData);
  661.       return;
  662.     }
  663.  
  664.     // Retrieve CLSID [and Icon] of selection
  665.     //
  666.     if (GetCurrentCLSID(Helper.ClsId)) {
  667.       iconMeta = GetIconFromClass(Helper.ClsId, 0, true);
  668.     }
  669.  
  670.     // Store Icon for future use
  671.     //
  672.     ObjectList->SetItemData(index, (uint32)iconMeta);
  673.   }
  674.   else { // ioSelectCreateFromFile
  675.     char *fileName = new char[MaxPathLen];
  676.  
  677.     FileName->GetText(fileName, MaxPathLen);
  678.  
  679.     CLSID clsid;
  680.     bool  isChkLnk = (Helper.Flags & ioCheckLink) ? true : false;
  681.  
  682.     if (isChkLnk || (GetClassFile(fileName, &clsid) == S_OK)) {
  683.       iconMeta = GetIconFromFile(fileName, isChkLnk ? true : false);
  684.     }
  685.     else {
  686.       iconMeta = GetIconFromClass(clsid, 0, true);
  687.     }
  688.   }
  689.  
  690.   // Update icon display
  691.   //
  692.   IconImage->SetMetaPict(iconMeta);
  693.   Activate(IconImage, iconMeta ? true : false );
  694. }
  695.  
  696. //
  697. //
  698. //
  699. void
  700. TInsertObjectDlg::SetInsertObjectResults()
  701. {
  702.   TModule *module = GetModule();
  703.  
  704.   const int buffLen = 0x200;
  705.  
  706.   TPointer<char> str1 = new char[buffLen];
  707.   TPointer<char> str2 = new char[buffLen];
  708.   TPointer<char> str3 = new char[buffLen];
  709.   uint strID1=0, strID2=0;
  710.  
  711.   uint imageIndex = 0;
  712.   bool asIcon = (Helper.Flags & ioCheckDisplayAsIcon) ? true : false;
  713.  
  714.   if (Helper.Flags & ioSelectCreateNew) {
  715.     strID1 = asIcon ? IDS_IORESULTNEWICON : IDS_IORESULTNEW;
  716.     imageIndex = asIcon ? riEmbedIcon : riEmbed;
  717.   }
  718.  
  719.   if (Helper.Flags & ioSelectCreateFromFile) {
  720.     if (Helper.Flags & ioCheckLink) {
  721.       strID1 = asIcon ? IDS_IORESULTLINKFILEICON1 : IDS_IORESULTLINKFILE1;
  722.       strID2 = asIcon ? IDS_IORESULTLINKFILEICON2 : IDS_IORESULTLINKFILE2;
  723.       imageIndex = asIcon ? riLinkIcon : riLink;
  724.     }
  725.     else {
  726.       strID1 = IDS_IORESULTFROMFILE1;
  727.       strID2 = (asIcon==true) ? IDS_IORESULTFROMFILEICON2 :
  728.                                 IDS_IORESULTFROMFILE2;
  729.       imageIndex = asIcon ? riEmbedIcon : riEmbed;
  730.     }
  731.   }
  732.  
  733.   *((char*)str1) = 0;
  734.   if (module->LoadString(strID1, str1, buffLen)) {
  735.     if (strID2  &&  module->LoadString(strID2, str2, buffLen))
  736.       strcat(str1, str2);
  737.  
  738.     if (Helper.Flags & ioSelectCreateNew) {
  739.       int index = ObjectList->GetSelIndex();
  740.       if (index != LB_ERR) {
  741.         int len = ObjectList->GetStringLen(index)+1;
  742.         TPointer<char> lstEntry = new char[len];
  743.         if (ObjectList->GetString(lstEntry, index) != LB_ERR) {
  744.           char *pTab = strchr(lstEntry, '\t');
  745.           if (pTab)
  746.             *pTab = 0;
  747.  
  748.           wsprintf((LPSTR)(char*)str3, (LPSTR)(char*)str1,
  749.                    (LPSTR)(char*)lstEntry);
  750.           strcpy(str1, str3);
  751.         }
  752.       }
  753.     }
  754.   }
  755.   ResultText->SetText(str1);
  756.   ResultImage->SetBitmapIndex(imageIndex);
  757. }
  758.  
  759. //
  760. // Validates file name currently specified in edit control and
  761. // attempts to substitute a full path for any partial/relative
  762. // filenames.
  763. //
  764. bool
  765. TInsertObjectDlg::ValidateInsertFile(bool showErr)
  766. {
  767.   // Retrieve current name
  768.   //
  769.   char fileName[MaxPathLen];
  770.   if (!FileName->GetText(fileName, sizeof(fileName)))
  771.     return false;
  772.  
  773.   OFSTRUCT of;
  774.   HFILE hFile;
  775.   hFile = DoesFileExist(fileName, of);
  776.  
  777.   // Sharing Violation's OK since OleCreateFromFile and
  778.   // OleCreateLinkToFile can still succeed under these circumstances.
  779.   //
  780.   const uint ofErrSharingViolation = 0x0020;
  781.   if (hFile==HFILE_ERROR  &&  of.nErrCode != ofErrSharingViolation) {
  782.     if (showErr) {
  783.       OpenFileError(of.nErrCode, fileName);
  784.     }
  785.     return false;
  786.   }
  787.   else {
  788.     OemToAnsi(of.szPathName, of.szPathName);
  789.     FileName->SetText(of.szPathName);
  790.     return true;
  791.   }
  792. }
  793.  
  794. //
  795. //
  796. //
  797. bool
  798. TInsertObjectDlg::GetCurrentCLSID(CLSID& clsid)
  799. {
  800.   PRECONDITION(Helper.Flags & ioSelectCreateNew);
  801.  
  802.   int index = ObjectList->GetSelIndex();
  803.   if (index == LB_ERR)
  804.     return false;
  805.  
  806.   char *listEntry = new char[MaxKeyLen];
  807.   TPointer<char> p(listEntry);
  808.  
  809.   // Retrieve ListBox entry and get to CLSID stored beyond tab
  810.   //
  811.   ObjectList->GetString(listEntry, index);
  812.   LPSTR lpszCLSID = PtrToNthField(listEntry, 2, '\t');
  813.   return (CLSIDFromString(lpszCLSID, &clsid) == S_OK) ? true : false;
  814. }
  815.  
  816. //
  817. //
  818. //
  819. void
  820. TInsertObjectDlg::CreateNewClicked()
  821. {
  822.   ToggleObjectSource(ioSelectCreateNew);
  823. }
  824.  
  825. //
  826. //
  827. //
  828. void
  829. TInsertObjectDlg::CreateFromFileClicked()
  830. {
  831.   ToggleObjectSource(ioSelectCreateFromFile);
  832. }
  833.  
  834. //
  835. //
  836. //
  837. void
  838. TInsertObjectDlg::LinkToFileClicked()
  839. {
  840.   if (Link->GetCheck() == BF_CHECKED) {
  841.     Helper.Flags |= ioCheckLink;
  842.   }
  843.   else {
  844.     Helper.Flags &= ~ioCheckLink;
  845.   }
  846.   SetInsertObjectResults();
  847.   UpdateClassIcon();
  848. }
  849.  
  850. //
  851. //
  852. //
  853. void
  854. TInsertObjectDlg::BrowseFileClicked()
  855. {
  856.   char* initDirPtr = 0;
  857.   char  initDirBuf[MaxPathLen];
  858.  
  859.   // Save current name
  860.   //
  861.   char curName[MaxPathLen];
  862.   int  nChars = FileName->GetText(curName, sizeof(curName));
  863.  
  864.   // Try to retrieve initial directory from name
  865.   //
  866.   if (ValidateInsertFile(false)) {
  867.     nChars = FileName->GetText(curName, sizeof(curName));
  868.     GetFileTitle(curName, Helper.FileName, MaxPathLen);
  869.     strcpyn(initDirBuf, curName, nChars-strlen(Helper.FileName));
  870.     initDirPtr = initDirBuf;
  871.   }
  872.   else {    // Default to current directory
  873.     if (getcwd(initDirBuf, MaxPathLen))
  874.       initDirPtr = initDirBuf;
  875.  
  876.     Helper.FileName[0] = 0;
  877.   }
  878.  
  879.   uint32 flags = OFN_FILEMUSTEXIST;
  880.   if (Data->Flags & ioShowHelp)
  881.     flags |= OFN_SHOWHELP;
  882.  
  883.   if (BrowseDlg(Helper.FileName, initDirPtr, IDS_FILTERS, flags)) {
  884.     if (strcmpi(Helper.FileName, curName)) {
  885.       FileName->SetText(Helper.FileName);
  886.       Helper.FileSelected = true;
  887.  
  888.       if (ValidateInsertFile(true)) {
  889.         Helper.FileDirty = false;
  890.         Helper.FileValid = true;
  891.  
  892.         UpdateClassIcon();
  893.         UpdateClassType(true);
  894.         Ok->SetFocus();
  895.       }
  896.       else {
  897.         Helper.FileDirty = false;
  898.         Helper.FileValid = false;
  899.         FileName->SetFocus();
  900.         FileName->SetSelection(0, -1);
  901.       }
  902.  
  903.       DisplayAsIcon->EnableWindow(true);
  904.       Ok->EnableWindow(true);
  905.     }
  906.   }
  907. }
  908.  
  909. //
  910. //
  911. //
  912. void
  913. TInsertObjectDlg::DisplayAsIconClicked()
  914. {
  915.   bool isChecked = (DisplayAsIcon->GetCheck()==BF_CHECKED) ? true : false;
  916.  
  917.   ChangeIcon->EnableWindow(isChecked);
  918.  
  919.   // Update flags
  920.   //
  921.   if (isChecked)
  922.     Helper.Flags |= ioCheckDisplayAsIcon;
  923.   else
  924.     Helper.Flags &= ~ioCheckDisplayAsIcon;
  925.  
  926.   if (Helper.Flags & ioSelectCreateNew)
  927.     Helper.AsIconNew = isChecked;
  928.   else
  929.     Helper.AsIconFile = isChecked;
  930.  
  931.   if (isChecked) {
  932.     if (Helper.Flags & ioSelectCreateFromFile) {
  933.       if (ValidateInsertFile()) {
  934.         Helper.FileDirty = false;
  935.         Helper.FileValid = true;
  936.         UpdateClassIcon();
  937.         UpdateClassType(true);
  938.       }
  939.       else {
  940.         Helper.AsIconFile = false;
  941.         Helper.FileDirty  = false;
  942.         Helper.FileValid  = false;
  943.  
  944.         IconImage->SetMetaPict(0);
  945.         UpdateClassType(false);
  946.  
  947.         Helper.Flags &= ~ioCheckDisplayAsIcon;
  948.  
  949.         DisplayAsIcon->Uncheck();
  950.  
  951.         FileName->SetFocus();
  952.         FileName->SetSelection(0, -1);
  953.         return;
  954.       }
  955.     }
  956.     else { // ioSelectCreateNew
  957.       UpdateClassIcon();
  958.     }
  959.   }
  960.  
  961.   SetInsertObjectResults();
  962.  
  963.   Activate(ChangeIcon, isChecked);
  964.   Activate(IconImage, isChecked);
  965. }
  966.  
  967. //
  968. //
  969. //
  970. void
  971. TInsertObjectDlg::ChangeIconClicked()
  972. {
  973.   if (Helper.Flags & ioSelectCreateFromFile) {
  974.     if (Helper.FileDirty  &&  !ValidateInsertFile()) {
  975.       Helper.FileDirty = true;
  976.       FileName->SetFocus();
  977.       FileName->SetSelection(0, -1);
  978.       return;
  979.     }
  980.     else {
  981.       Helper.FileDirty = false;
  982.     }
  983.   }
  984.  
  985.   TChangeIconDlg::TData chgIconData;
  986.   chgIconData.MetaPict = IconImage->GetMetaPict();
  987.   chgIconData.Flags    = ciSelectCurrent;
  988.  
  989.   if (Helper.Flags & ioShowHelp)
  990.     chgIconData.Flags |= ciShowHelp;
  991.  
  992.   if (Helper.Flags & ioSelectCreateNew) {
  993.     GetCurrentCLSID(chgIconData.ClsId);
  994.   }
  995.   else {  // ioSelectCreateFromFile
  996.     char fileName[MaxPathLen];
  997.     if (FileName->GetText(fileName, sizeof(fileName))) {
  998.       if (GetClassFile(fileName, &chgIconData.ClsId) == S_OK) {
  999.         char* extension;
  1000.         int   nameLen = strlen(fileName);
  1001.         extension = fileName+nameLen+1;
  1002.  
  1003.         while((extension > fileName) && (*extension != '.'))
  1004.           extension--;
  1005.  
  1006.         if (GetAssociatedExecutable(extension, chgIconData.IconExe)) {
  1007.           chgIconData.Flags |= ciUseIconExe;
  1008.         }
  1009.       }
  1010.     }
  1011.   }
  1012.  
  1013.   TRY {
  1014.     TChangeIconDlg chgIconDlg(this, chgIconData);
  1015.     if (chgIconDlg.Execute() == IDOK) {
  1016.       IconImage->SetMetaPict(chgIconData.MetaPict);
  1017.       if (Helper.Flags & ioSelectCreateNew) {
  1018.         int index = ObjectList->GetSelIndex();
  1019.         if (index != LB_ERR) {
  1020.           ObjectList->SetItemData(index, (uint32)chgIconData.MetaPict);
  1021.         }
  1022.       }
  1023.     }
  1024.   }
  1025.   CATCH( (xmsg& msg){ GetModule()->Error(msg, 0); } )
  1026. }
  1027.  
  1028. //
  1029. //
  1030. //
  1031. void
  1032. TInsertObjectDlg::ObjectListChanged()
  1033. {
  1034.   UpdateClassIcon();
  1035.   SetInsertObjectResults();
  1036. }
  1037.  
  1038. //
  1039. //
  1040. //
  1041. void
  1042. TInsertObjectDlg::ObjectListDoubleClicked()
  1043. {
  1044.   SendNotification(IDOK, BN_CLICKED, GetDlgItem(IDOK));
  1045. }
  1046.  
  1047. //
  1048. //
  1049. //
  1050. void
  1051. TInsertObjectDlg::FileNameChanged()
  1052. {
  1053.   bool hasText = FileName->GetLineLength(0) ? true : false;
  1054.  
  1055.   Helper.FileDirty = true;
  1056.   Helper.FileValid = false;
  1057.   Helper.FileSelected = hasText;
  1058.  
  1059.   Link->EnableWindow(hasText);
  1060.   DisplayAsIcon->EnableWindow(hasText);
  1061.   ChangeIcon->EnableWindow(hasText);
  1062.   Ok->EnableWindow(hasText);
  1063. }
  1064.  
  1065. //
  1066. //
  1067. //
  1068. void
  1069. TInsertObjectDlg::FileNameKillFocus()
  1070. {
  1071.   if (ValidateInsertFile(false)) {
  1072.     Helper.FileDirty = false;
  1073.     Helper.FileValid = true;
  1074.     UpdateClassIcon();
  1075.     UpdateClassType(true);
  1076.   }
  1077.   else {
  1078.     Helper.FileDirty = false;
  1079.     Helper.FileValid = false;
  1080.     UpdateClassType(false);
  1081.   }
  1082. }
  1083.  
  1084. //
  1085. // Initialized helper object used internally by TInsertObjectDlg
  1086. //
  1087. TInsertObjectDlg::THelper::THelper()
  1088. {
  1089.   Flags        = 0;
  1090.   ClsId        = CLSID_NULL;
  1091.   FileName[0]  = 0;
  1092.   FileSelected = false;
  1093.   AsIconNew    = false;
  1094.   AsIconFile   = false;
  1095.   FileDirty    = true;
  1096.   FileValid    = false;
  1097.   MetaPict     = 0;
  1098. }
  1099.