home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osr1.exe / src / InfoUtil.cpp < prev    next >
C/C++ Source or Header  |  1997-03-21  |  51KB  |  1,799 lines

  1. /* @(#)Z 1.4 com/src/utils/InfoUtil.cpp, odcore, od96os2, odos29712d 97/03/21 17:20:52 (96/12/17 13:54:57) */
  2. //====START_GENERATED_PROLOG======================================
  3. //
  4. //
  5. //   COMPONENT_NAME: odcore
  6. //
  7. //   CLASSES: none
  8. //
  9. //   ORIGINS: 82,27
  10. //
  11. //
  12. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  13. //   All Rights Reserved
  14. //   Licensed Materials - Property of IBM
  15. //   US Government Users Restricted Rights - Use, duplication or
  16. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  17. //       
  18. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  22. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  23. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  24. //   OR PERFORMANCE OF THIS SOFTWARE.
  25. //
  26. //====END_GENERATED_PROLOG========================================
  27. //
  28. /*
  29.     File:        InfoUtil.cpp
  30.  
  31.     Contains:    implementation of Info getters & setters
  32.  
  33.     Owned by:    Tantek éelik
  34.  
  35.     Copyright:    ⌐ 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  36.  
  37.     
  38.     In Progress:
  39.         
  40. */
  41.  
  42. #ifndef _PLFMDEF_
  43. #include "PlfmDef.h"
  44. #endif
  45.  
  46. #ifndef _STDTYPIO_
  47. #include "StdTypIO.h"
  48. #endif
  49.  
  50. #if !defined(_PLATFORM_WIN32_)
  51. #ifndef _USERSRCM_
  52. #include "UseRsrcM.h"
  53. #endif
  54. #endif // !defined(_PLATFORM_WIN32_)
  55.  
  56. #ifndef SOM_ODSession_xh
  57. #include <ODSessn.xh>
  58. #endif
  59.  
  60. #ifndef SOM_ODDraft_xh
  61. #include <Draft.xh>
  62. #endif
  63.  
  64. #ifndef SOM_ODDocument_xh
  65. #include <Document.xh>
  66. #endif
  67.  
  68. #ifndef SOM_ODContainer_xh
  69. #include <ODCtr.xh>
  70. #endif
  71.  
  72. #ifndef SOM_ODWindowState_xh
  73. #include <WinStat.xh>
  74. #endif
  75.  
  76. #ifndef SOM_ODWindow_xh
  77. #include <Window.xh>
  78. #endif
  79.  
  80. #ifndef SOM_ODStorageUnit_xh
  81. #include <StorageU.xh>
  82. #endif
  83.  
  84. #ifndef SOM_Module_OpenDoc_StdProps_defined
  85. #include <StdProps.xh>
  86. #endif
  87.  
  88. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  89. #include <StdTypes.xh>
  90. #endif
  91.  
  92. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  93. #include <StdDefs.xh>
  94. #endif
  95.  
  96. #ifndef _ODUTILS_
  97. #include <ODUtils.h>
  98. #endif
  99.  
  100. #ifndef _DLOGUTIL_
  101. #include <DlogUtil.h>
  102. #endif
  103.  
  104. #ifndef SOM_ODFrame_xh
  105. #include <Frame.xh>
  106. #endif
  107.  
  108. #ifndef SOM_ODFacet_xh
  109. #include <Facet.xh>
  110. #endif
  111.  
  112. #ifndef SOM_ODPart_xh
  113. #include <Part.xh>
  114. #endif
  115.  
  116. #ifndef SOM_ODBinding_xh
  117. #include <ODBindng.xh>
  118. #endif
  119.  
  120. #ifndef SOM_ODStorageUnitView_xh
  121. #include <SUView.xh>
  122. #endif
  123.  
  124. #ifndef _PASCLSTR_
  125. #include "PasclStr.h"
  126. #endif
  127.  
  128. #ifndef _ODMEMORY_
  129. #include "ODMemory.h"
  130. #endif
  131.  
  132. #ifndef _PLFMFILE_
  133. #include <PlfmFile.h>
  134. #endif
  135.  
  136. #ifndef _ITEXT_
  137. #include <IText.h>
  138. #endif
  139.  
  140. #ifndef SOM_ODTypeList_xh
  141. #include <TypeList.xh>
  142. #endif
  143.  
  144. #ifndef SOM_ODTypeListIterator_xh
  145. #include <TypLsItr.xh>
  146. #endif
  147.  
  148. #ifndef _EXCEPT_
  149. #include "Except.h"
  150. #endif
  151.  
  152. #ifndef _BNDNSUTL_
  153. #include "BndNSUtl.h"
  154. #endif
  155.  
  156. #if !defined(_PLATFORM_WIN32_)
  157. #ifdef __SC__
  158. #ifndef __PACKAGES__
  159. #include <Packages.h>
  160. #endif
  161. #else
  162. #ifndef __TEXTUTILS__
  163. #include <TextUtils.h>
  164. #endif
  165. #endif
  166.  
  167. #ifndef __STRING__
  168. #include <String.h>
  169. #endif
  170.  
  171. #ifndef __GESTALTEQU__
  172. #include <GestaltEqu.h>
  173. #endif
  174.  
  175. #ifndef __DIALOGS__
  176. #include <Dialogs.h>
  177. #endif
  178.  
  179. #ifndef __TOOLUTILS__
  180. #include <ToolUtils.h>
  181. #endif
  182.  
  183. #ifndef __FONTS__
  184. #include <Fonts.h>
  185. #endif
  186.  
  187. #ifndef __CONTROLS__
  188. #include <Controls.h>
  189. #endif
  190.  
  191. #ifndef __ICONS__
  192. #include <Icons.h>
  193. #endif
  194.  
  195. #ifndef __FINDER__
  196. #include <Finder.h>
  197. #endif
  198. #endif // !defined(_PLATFORM_WIN32_)
  199.  
  200. #ifndef _INFOUTIL_
  201. #include "InfoUtil.h"
  202. #endif
  203.  
  204. #ifndef SOM_ODTranslation_xh
  205. #include <Translt.xh>
  206. #endif
  207.  
  208. #ifndef _EDITRSET_
  209. #include "EditrSet.h"
  210. #endif
  211.  
  212. #ifndef _STORUTIL_
  213. #include <StorUtil.h>
  214. #endif
  215.  
  216. #ifndef _ISOSTR_
  217. #include "ISOStr.h"
  218. #endif
  219.  
  220. #ifndef SOM_ODWindowIterator_xh
  221. #include <WinIter.xh>
  222. #endif
  223.  
  224. #ifndef SOM_ODNameSpaceManager_xh
  225. #include <NmSpcMg.xh>
  226. #endif
  227.  
  228. #ifndef SOM_ODValueNameSpace_xh
  229. #include <ValueNS.xh>
  230. #endif
  231.  
  232. #ifndef _NMSPCUTL_
  233. #include <NmSpcUtl.h>
  234. #endif
  235.  
  236. #ifndef _UTILDEFS_
  237. #include "UtilDefs.h"
  238. #endif
  239.  
  240. #ifndef _TEMPOBJ_
  241. #include <TempObj.h>
  242. #endif
  243.  
  244. #ifndef _ORDCOLL_
  245. #include "OrdColl.h"
  246. #endif
  247.  
  248. #pragma segment Info
  249.  
  250. //==============================================================================
  251. // Constants
  252. //==============================================================================
  253.  
  254. const short kControlInactive = 255;
  255. const short kControlActive = 0;
  256. static const char* const         kODIconFamilyBaseString = kODIconFamily ":";
  257. #if !defined(_PLATFORM_WIN32_)
  258. static const ResType            kIconType[6] = {'ICN#','icl4','icl8','ics#','ics4','ics8'};
  259. #endif // !defined(_PLATFORM_WIN32_)
  260. static const short                kIconSize[6] = {256,   512,   1024,  64,    128,   256   };
  261.  
  262. #if !defined(_PLATFORM_WIN32_)
  263. const ODPlatformType kODFileType_hfs = 0x68667320;    // 'hfs '
  264. #else // defined(_PLATFORM_WIN32_)
  265. // Talk to Leigh Williamson about the definition of ODPlatformType
  266. const ODPlatformType kODFileType_hfs = (ODPlatformType) 0x68667320;    // 'hfs '
  267. #endif // !defined(_PLATFORM_WIN32_)
  268.  
  269. //==============================================================================
  270. // Static Function declarations
  271. //==============================================================================
  272.  
  273. ODTime    ODGetDateProperty(Environment* ev, 
  274.             ODStorageUnit* su,
  275.             ODPropertyName prop);
  276.  
  277. void    ODSetDateProperty(Environment* ev, 
  278.             ODStorageUnit* su,
  279.             ODPropertyName prop,
  280.             ODTime dateTime);
  281.  
  282. ODStatic ODBoolean         ODUserRenameFile(Environment* ev,
  283.                                 ODSession* session,
  284.                                 PlatformFile*    usersFile, 
  285.                                 ODIText* name,
  286.                                 DescType replaceOption);
  287. // If PlatformFile is made a shared utility, then the above function should probably be
  288. // integrated into the PlatformFile::Rename method.
  289.  
  290. ODStatic ODIconFamily    GetGenericIcon( short iconID );
  291.  
  292. ODStatic OSErr             DetachIconProc( ResType, Handle *theIcon, void */*yourDataPtr*/ );
  293.  
  294. ODStatic ODBoolean        CommentsDontMatch(ODIText* dtComments, ODIText* propComments);
  295.  
  296. //==============================================================================
  297. // Info functions
  298. //==============================================================================
  299.  
  300.  
  301. ODStorageUnit* ODGetSUFromPstObj(Environment* ev, ODPersistentObject* pstobj)
  302. {
  303.     return pstobj->GetStorageUnit(ev);
  304. }
  305.  
  306. //-------------------------------------------------------------------------------------
  307. // Part & Frame Property getters and setters
  308. //-------------------------------------------------------------------------------------
  309.  
  310. ODIText*
  311. ODGetPOComments(Environment* ev, ODPart *part, ODIText* comments)
  312. {
  313.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, part);
  314.     ODIText*        propComments;
  315.  
  316. #if (defined _PLATFORM_WIN32_ || defined _PLATFORM_UNIX_ || defined _PLATFORM_OS2_)
  317.     propComments = ODGetITextProp(ev, su, kODPropComments, kODPlatformIText,
  318.                                       comments);
  319. #elif (defined _PLATFORM_MACINTOSH_)
  320.     propComments = ODGetITextProp(ev, su, kODPropComments, kODMacIText,
  321.                                       comments);
  322.     return propComments;
  323. #endif
  324. }
  325.  
  326. void    ODSetPOComments(Environment* ev, ODPart *part, ODIText* comments)
  327. {
  328.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, part);
  329.     
  330. #if (defined _PLATFORM_WIN32_ || defined _PLATFORM_UNIX_ || defined _PLATFORM_OS2_)
  331.     ODSetITextProp(ev, su, kODPropComments, kODPlatformIText, comments);
  332. #elif (defined _PLATFORM_MACINTOSH_)
  333.     ODSetITextProp(ev, su, kODPropComments, kODMacIText, comments);
  334. #endif
  335. }
  336.  
  337.  
  338. ODIText*
  339. ODGetComments(Environment* ev, ODFrame* frame, ODIText* comments)
  340. {
  341.     TempODPart        part = frame->AcquirePart(ev);
  342.     ODIText*        propComments;
  343.     ODIText*        dtComments;
  344.  
  345. #if !defined(_PLATFORM_WIN32_)
  346.     propComments = ODGetPOComments(ev, part, comments);
  347.     if (comments && !propComments)
  348.         propComments = comments;
  349.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  350.     if (usersFile)
  351.     {
  352.         dtComments = usersFile->GetComments();
  353.         if (dtComments && GetITextStringLength(dtComments) == 0)
  354.         {
  355.             // since there are no desktop comments
  356.             // make the desktop comments match the property
  357.             usersFile->SetComments(propComments);
  358.         }
  359.         else if (CommentsDontMatch(dtComments, propComments) && dtComments)
  360.         {
  361.             // if there are desktop comments and the property comments don't match
  362.             // then the desktop comments take precedence, so return the desktop comments
  363.             DisposeIText(propComments);
  364.             propComments = dtComments;
  365.             dtComments = kODNULL;
  366.         }
  367.         DisposeIText(dtComments);
  368.     }
  369. #endif // !defined(_PLATFORM_WIN32_)
  370.     return propComments;
  371. }
  372.  
  373. void    ODSetComments(Environment* ev, ODFrame* frame, ODIText* comments)
  374. {
  375. #if !defined(_PLATFORM_WIN32_)
  376.     TempODPart        part = frame->AcquirePart(ev);
  377.     ODSetPOComments(ev, part, comments);
  378.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  379.     if (usersFile)
  380.         usersFile->SetComments(comments);
  381. #endif // !defined(_PLATFORM_WIN32_)
  382. }
  383.  
  384.  
  385. ODULong    ODGetPOSize(Environment* ev, ODPersistentObject* pstobj)
  386. {
  387.     ODStorageUnit* su = ODGetSUFromPstObj(ev, pstobj);
  388.     su->Focus(ev, kODNULL, kODPosAll, kODNULL,0, kODPosAll);
  389.     return su->GetSize(ev);
  390. }
  391.  
  392. ODID ODGetPOID(Environment* ev, ODPersistentObject* pstobj)
  393. {
  394.     ODStorageUnit* su = ODGetSUFromPstObj(ev, pstobj);
  395.     su->Focus(ev, kODNULL, kODPosAll, kODNULL,0, kODPosAll);
  396.     return su->GetID(ev);
  397. }
  398.  
  399. ODTime    ODGetDateProperty(Environment* ev, 
  400.             ODStorageUnit* su,
  401.             ODPropertyName prop)
  402. {    return ODGetTime_TProp(ev, su, prop, kODTime_T);}
  403.  
  404. void    ODSetDateProperty(Environment* ev, 
  405.             ODStorageUnit* su,
  406.             ODPropertyName prop,
  407.             ODTime dateTime)
  408. {    ODSetTime_TProp(ev, su, prop, kODTime_T, dateTime);}
  409.  
  410.  
  411. ODULong    ODGetCreationDate(Environment* ev, 
  412.                 ODStorageUnit* su)
  413. {    return ODGetDateProperty(ev, su, kODPropCreateDate);}
  414.  
  415. void    ODSetCreationDate(Environment* ev, 
  416.                 ODStorageUnit* su,
  417.                 ODTime dateTime)
  418. {    ODSetDateProperty(ev, su, kODPropCreateDate, dateTime);}
  419.  
  420.  
  421. ODULong    ODGetModificationDate(Environment* ev, 
  422.                 ODStorageUnit* su)
  423. {    return ODGetDateProperty(ev, su, kODPropModDate);}
  424.  
  425. void    ODSetModificationDate(Environment* ev, 
  426.                 ODStorageUnit* su,
  427.                 ODTime dateTime)
  428. {    ODSetDateProperty(ev, su, kODPropModDate, dateTime);}
  429.  
  430.  
  431. ODIText*    ODGetModifiedBy(Environment* ev, 
  432.                 ODStorageUnit* su, ODIText* userName)
  433. {
  434. #if (defined _PLATFORM_WIN32_ || defined _PLATFORM_UNIX_ || defined _PLATFORM_OS2_)
  435.     return ODGetITextProp(ev, su, kODPropModUser, kODPlatformIText, 
  436.                               userName);
  437. #elif (defined _PLATFORM_MACINTOSH_)
  438.     return ODGetITextProp(ev, su, kODPropModUser, kODMacIText, userName);
  439. #endif
  440. }
  441.  
  442. void        ODSetModifiedBy(Environment* ev, 
  443.                 ODStorageUnit* su, ODIText* userName)
  444. {
  445. #if (defined _PLATFORM_WIN32_ || defined _PLATFORM_UNIX_ || defined _PLATFORM_OS2_)
  446.     ODSetITextProp(ev, su, kODPropModUser, kODPlatformIText, userName);
  447. #elif (defined _PLATFORM_MACINTOSH_)
  448.     ODSetITextProp(ev, su, kODPropModUser, kODMacIText, userName);
  449. #endif
  450. }
  451.  
  452.  
  453. //-------------------------------------------------------------------------------------
  454. // Part only Property getters and setters
  455. //-------------------------------------------------------------------------------------
  456. ODIText*    ODGetPartName(Environment* ev, 
  457.                 ODFrame* frame, ODIText* name)
  458. {
  459.     TempODPart part = frame->AcquirePart(ev);
  460.     TempODIText partName = ODGetPOName(ev, part, name);
  461.     
  462.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  463.     if (usersFile)
  464.     {
  465.         TempODIText fileName = usersFile->GetName();
  466.         if ( CommentsDontMatch( fileName, partName ) )
  467.             return CopyIText( fileName );
  468.     }
  469.     return CopyIText( partName );
  470. }
  471.  
  472. ODBoolean    ODSetPartName(Environment* ev, 
  473.                 ODFrame* frame, ODIText* name, DescType replaceOption)
  474.         // Renames the part of the frame passed in.
  475.         // Returns whether or not it succeeded.
  476.         // The frame helps more easily determine whether
  477.         // the part is the root part of the document or not.
  478.         // For example, renaming the root part of the document could fail
  479.         // if there was a file with the same name and the user canceled the
  480.         // "Replace?" dialog.
  481. {
  482.     ASSERT(frame != kODNULL, kODErrIllegalNullInput);
  483.     
  484.     TempODPart part = frame->AcquirePart(ev);
  485.     TempODWindow window = frame->AcquireWindow(ev);
  486.                 
  487.     ODFrame*     contFrame = frame->AcquireContainingFrame(ev);
  488.     ODBoolean     isRootPart = (contFrame == kODNULL);
  489.     ODReleaseObject(ev, contFrame);
  490.     
  491.     ODBoolean    isRootPartOfDocument = isRootPart && window->IsRootWindow(ev);
  492.     ODBoolean    succeeded = kODTrue;
  493.     
  494. #if !defined(_PLATFORM_WIN32_)
  495.     TRY
  496.         ODName*         oldName = ODGetPartName(ev, frame, kODNULL); 
  497.         ODStorageUnit*    partSU    = ODGetSUFromPstObj(ev, part);
  498.         ODSession*        session = partSU->GetSession(ev);
  499.  
  500.         if (isRootPartOfDocument)
  501.         {
  502.             ODDraft*        draft = partSU->GetDraft(ev);
  503.             PlatformFile*    usersFile = 
  504.                 GetPlatformFileFromContainer(ev, draft->GetDocument(ev)->GetContainer(ev));
  505.  
  506.             if (ODUserRenameFile(ev, session, usersFile, name, replaceOption))
  507.             {
  508.                 ODWindowState* windowState = session->GetWindowState(ev);
  509.                 windowState->SetDefaultWindowTitles(ev, draft);
  510.                 ODSetPOName(ev, part, name);
  511.                 ODRenamePartWindows(ev,session,part,oldName,name);
  512.                 if (isRootPart)
  513.                 {
  514.                     Str255 pName;
  515.                     GetITextPString(name, pName);
  516.                     SetWTitle(window->GetPlatformWindow(ev), pName);
  517.                 }
  518.             }
  519.             else
  520.                 succeeded = kODFalse;
  521.             ODDeleteObject(usersFile);
  522.         }
  523.         else
  524.         {
  525.             ODSetPOName(ev, part,name);
  526.             ODRenamePartWindows(ev,session,part,oldName,name);
  527.         }
  528.         
  529.         DisposeIText(oldName);
  530.  
  531.     CATCH_ALL
  532.         WARN("ODSetPartName failed, error %ld",ErrorCode());
  533.         succeeded = kODFalse;
  534.     ENDTRY
  535.     
  536. #endif // !defined(_PLATFORM_WIN32_)
  537.     return succeeded;
  538. }
  539.  
  540. void
  541. ODRenamePartWindows( Environment *ev, ODSession *session,
  542.                      ODPart *part, ODIText *oldName, ODIText *name )
  543. {
  544.     // go through all windows which have this part as their root, and
  545.     // have the current name of the part as a prefix
  546.     // rename their prefix to be the new name
  547.     Str255    pOldName;
  548.     Str255    pNewName;
  549.     Str255    pWinTitle;
  550.     ODUByte pOldNameLen;
  551.     ODUByte pNewNameLen;
  552.     
  553.     GetITextPString(oldName, pOldName);
  554.     pOldNameLen = pOldName[0];
  555.     GetITextPString(name, pNewName);
  556.     pNewNameLen = pNewName[0];
  557.     
  558. #if !defined(_PLATFORM_WIN32_)
  559.     ODWindowIterator* iter = session->GetWindowState(ev)->CreateWindowIterator(ev);
  560.     for (ODWindow* window = iter->First(ev); 
  561.          iter->IsNotComplete(ev);
  562.          window = iter->Next(ev))
  563.     {
  564.         TempODPart winPart = window->GetRootFrame(ev)->AcquirePart(ev);
  565.         if (ODObjectsAreEqual(ev, part, winPart))
  566.         {
  567.             // Check to see if name of window has pOldName as prefix
  568.             ODPlatformWindow pwindow = window->GetPlatformWindow(ev);
  569.             GetWTitle(pwindow, pWinTitle);
  570.             if (!strncmp((char*)&pWinTitle[1], (char*)&pOldName[1], pOldNameLen) &&
  571.                     !EqualPascalStrings(pWinTitle, pNewName))
  572.             {
  573.                 ODUByte suffixLen = pWinTitle[0] - pOldNameLen;
  574.                 if (suffixLen + pNewNameLen > 255) 
  575.                     suffixLen = 255 - pNewNameLen;
  576.                 strncpy((char*)&pNewName[pNewNameLen+1], 
  577.                         (char*)&pWinTitle[pOldNameLen+1],
  578.                         suffixLen);
  579.                 pNewName[0] = pNewNameLen+suffixLen;
  580.                 SetWTitle(pwindow, pNewName);
  581.             }
  582.         }
  583.     }
  584.     ODDeleteObject(iter);
  585. #endif // !defined(_PLATFORM_WIN32_)
  586. }
  587.  
  588.  
  589. ODIText*    ODGetPOName(Environment* ev, ODPersistentObject* pstobj,ODIText* name)
  590. {
  591. #if (defined _PLATFORM_WIN32_ || defined _PLATFORM_UNIX_ || defined _PLATFORM_OS2_)
  592.     return ODGetITextProp(ev, ODGetSUFromPstObj(ev, pstobj), kODPropName, 
  593.                               kODPlatformIText,name);
  594. #elif (defined _PLATFORM_MACINTOSH_)
  595.     return ODGetITextProp(ev, ODGetSUFromPstObj(ev, pstobj), kODPropName, 
  596.                               kODMacIText,name);
  597. #endif
  598. }
  599.  
  600. void        ODSetPOName(Environment* ev, ODPersistentObject* pstobj,ODIText* name)
  601. {
  602. #if (defined _PLATFORM_WIN32_ || defined _PLATFORM_UNIX_ || defined _PLATFORM_OS2_)
  603.     ODSetITextProp(ev, ODGetSUFromPstObj(ev, pstobj), kODPropName,
  604.                        kODPlatformIText, name);
  605. #elif (defined _PLATFORM_MACINTOSH_)
  606.     ODSetITextProp(ev, ODGetSUFromPstObj(ev, pstobj), kODPropName,
  607.                        kODMacIText, name);
  608. #endif
  609. }
  610.  
  611. void        ODSetPONameUsingSU(Environment* ev, ODStorageUnit* su, ODIText* name)
  612. {
  613. #if (defined _PLATFORM_WIN32_ || defined _PLATFORM_UNIX_ || defined _PLATFORM_OS2_)
  614.     ODSetITextProp(ev, su, kODPropName, kODPlatformIText, name);
  615. #elif (defined _PLATFORM_MACINTOSH_)
  616.     ODSetITextProp(ev, su, kODPropName, kODMacIText, name);
  617. #endif
  618. }
  619.  
  620. ODName*    ODGetCategory(Environment* ev, ODPart* part, ODNameSpaceManager* nsm)
  621. {
  622.     return ODGetCatFromPartSU(ev, ODGetSUFromPstObj(ev, part), nsm);
  623. }
  624.  
  625. ODName*    ODGetCatFromPartSU(Environment* ev, ODStorageUnit* su, ODNameSpaceManager* nsm)
  626. {
  627.     ODType kind = ODGetKindFromPartSU(ev, su);
  628.     ODName*    categoryName = ODGetCatFromKind(ev, kind, nsm);
  629.     ODDisposePtr( kind );
  630.  
  631.     return categoryName;
  632. }
  633.  
  634. ODName*    ODGetCatFromKind(Environment* ev, ODType kind, ODNameSpaceManager* nsm)
  635. {
  636.     StringHandle strHandle = 0;
  637.     ODTypeList* categoryList = GetCategoriesOfKind(nsm, kind);
  638.     ODTypeListIterator* catIter = kODNULL;
  639.     ODType category = kODNULL;
  640.     ODName*    categoryName = kODNULL;
  641.     
  642.  
  643. #if !defined(_PLATFORM_WIN32_)
  644.     if (categoryList)
  645.     {
  646.         catIter = categoryList->CreateTypeListIterator(ev);
  647.         category = catIter->First(ev);
  648.         ODDeleteObject(catIter);
  649.     }
  650.     
  651.     if (category == kODNULL || !GetUserCatFromCat(nsm, category, &categoryName)) {
  652.         CUsingLibraryResources r;
  653.         categoryName = GetODIText(kODPartInfoStrUnknownID);
  654.     }
  655.     //else // make a disposable copy // NO NEED TO! GetUserCatFromCat returns a copy!
  656.     //    categoryName = CopyIText(categoryName);
  657.         
  658.     ODDisposePtr(category);
  659.  
  660. #endif // !defined(_PLATFORM_WIN32_)
  661.     return categoryName;
  662. }
  663.  
  664. ODType    ODGetKindFromPartSU(Environment* ev, ODStorageUnit* su)
  665. {
  666.     ODULong unused;
  667.     ODType kind = ODGetISOStrProp(ev, su, kODPropPreferredKind, kODISOStr, kODNULL, &unused);
  668.     if ( kind == kODNULL )
  669.         if ( su->Exists(ev, kODPropContents, (ODValueType) kODNULL,1) )
  670.         {
  671.             su->Focus(ev, kODPropContents, kODPosSame, (ODValueType) kODNULL,1, kODPosSame);
  672.             kind = su->GetType(ev);
  673.  
  674.             ODTranslation* translation = su->GetSession(ev)->GetTranslation(ev);
  675.             TempODType hfsType = 
  676.                 translation->GetISOTypeFromPlatformType(ev, kODFileType_hfs, kODPlatformDataType);
  677.  
  678.             if ( ODISOStrEqual(kind, hfsType) )
  679.             {
  680.                 ODDisposePtr(kind);
  681.                 kind = kODNULL;
  682.                 if ( su->Exists(ev, kODPropContents, (ODValueType) kODNULL,2) )
  683.                 {
  684.                     su->Focus(ev, kODPropContents, kODPosSame, (ODValueType) kODNULL,2, kODPosSame);
  685.                     kind = su->GetType(ev);
  686.                 }
  687.             }
  688.         }
  689.     return kind;
  690. }
  691.  
  692. ODType    ODGetKind(Environment* ev, ODPart* part)
  693. {
  694.     return ODGetKindFromPartSU(ev, ODGetSUFromPstObj(ev, part));
  695. }
  696.  
  697. ODPlatformType ODGetIconFilePlatformTypeFromPartSU(Environment* ev, ODStorageUnit* su)
  698. {
  699.     ODSession* session = su->GetSession(ev);
  700.     
  701.     ODType partKind = ODGetKindFromPartSU(ev, su);
  702.     ODPlatformType partOSType = session->GetTranslation(ev)->
  703.                             GetPlatformTypeFromISOType(ev, partKind);
  704. #if !defined(_PLATFORM_WIN32_)
  705.     if (partOSType != kODNULL)
  706.     {
  707.         ((ODUByte*)(&partOSType))[0] = kODPlatformKindFileChar1;
  708.     }
  709.     else
  710.     {
  711.         ODBoolean    partOSTypeFound = kODFalse;
  712.         ODValueNameSpace*    osTypeNameSpace = (ODValueNameSpace*)
  713.             (session->GetNameSpaceManager(ev)->HasNameSpace(ev, kODKindOldMacOSType));
  714.     
  715.         if (osTypeNameSpace)
  716.         {
  717.             ODPlatformType* typePtr;
  718.             ODULong        unusedLength;
  719.             partOSTypeFound = ValueNameSpaceGetEntry(osTypeNameSpace, 
  720.                                                         ev, partKind, 
  721.                                                         (ODPtr*) &typePtr, 
  722.                                                         &unusedLength);
  723.             if (partOSTypeFound)
  724.             {
  725.                 partOSType = *typePtr;
  726.                 ODDeleteObject( typePtr );
  727.             }
  728.         }
  729.         if (!partOSTypeFound)
  730.             partOSType = kODShellSignature;
  731.     }
  732.     
  733. #endif // !defined(_PLATFORM_WIN32_)
  734.     ODDisposePtr(partKind);
  735.  
  736.     return partOSType;
  737. }
  738.  
  739.  
  740. //--------------------------------------------------------------------
  741. // ODGetPOIconFamily
  742. //--------------------------------------------------------------------
  743.  
  744. ODIconFamily
  745. ODGetPOIconFamily(Environment* ev, ODPart* part)
  746. {
  747. #if !defined(_PLATFORM_WIN32_)
  748.     //// FIRST, get the Mac icon value if one exists:
  749.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, part);
  750.     ODIconFamily icons = ODGetIconFamilyProp(ev, su, 
  751.                                 kODPropCustomIcon, kODIconFamily, kAllIconsMask);
  752.     if (icons )
  753.         return icons;
  754.         
  755.     //// OTHERWISE, look for other platform icon values:
  756.     if (ODSUExistsThenFocus(ev,su, kODPropCustomIcon, kODNULL))
  757.     {
  758.         ODULong numValues = su->CountValues(ev);
  759.         for( ODULong i = 1; i <= numValues; i++ )
  760.         {
  761.             su->Focus(ev, kODNULL, kODPosSame, kODNULL, 0, kODPosNextSib);    // Next value
  762.             TempODValueType type = su->GetType(ev);
  763.             if( strncmp(type,kODIconFamilyBaseString,strlen(kODIconFamilyBaseString)) == 0 )
  764.             {
  765.                 // This value is an icon family for some platform. Use the b/w icons
  766.                 // because we don't know what color table the color ones may use.
  767.                 icons = ODGetIconFamilyProp(ev, su, kODPropCustomIcon, type, kBWIconsMask);
  768.                 if( icons )
  769.                     return icons;
  770.             }
  771.         }
  772.         return kODNULL;
  773.     }
  774.     return icons;
  775. #else // defined(_PLATFORM_WIN32_)
  776.     return NULL;
  777. #endif // !defined(_PLATFORM_WIN32_)
  778. }
  779.  
  780.  
  781.  
  782. //--------------------------------------------------------------------
  783. // ODSetPOIconFamily
  784. //--------------------------------------------------------------------
  785.  
  786. void    ODSetPOIconFamily(Environment* ev, ODPart* part, 
  787.             ODIconFamily icons, ODBoolean deleteOtherPlatformIcons)
  788. {
  789. #if !defined(_PLATFORM_WIN32_)
  790.     ODSetIconFamilyProp(ev, ODGetSUFromPstObj(ev, part), 
  791.                                 kODPropCustomIcon, kODIconFamily,
  792.                                 icons, deleteOtherPlatformIcons);
  793. #endif // !defined(_PLATFORM_WIN32_)
  794. }
  795.  
  796. //--------------------------------------------------------------------
  797. // ODGetIconFamily
  798. //--------------------------------------------------------------------
  799.  
  800. ODIconFamily
  801. ODGetIconFamily(Environment* ev, ODFrame* frame)
  802. {
  803.     TempODPart        part = frame->AcquirePart(ev);
  804.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, (ODPart*)part);
  805.     ODIconFamily    icons = kODNULL;
  806.     
  807. #if !defined(_PLATFORM_WIN32_)
  808.     // FIRST we check for the custom icon resources
  809.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  810.     if (usersFile)
  811.         icons = usersFile->GetCustomIconFamily();
  812.     if (icons)
  813.         return icons;
  814.     
  815.     //// THEN get the property icon value if one exists:
  816.     icons = ODGetPOIconFamily(ev, part);
  817.     if (icons)
  818.         return icons;
  819.  
  820.     //// THEN get the editor's icon from the Mac desktop database:
  821.     ODPlatformType fileType = ODGetIconFilePlatformTypeFromPartSU(ev, su);
  822.     ODPlatformType fileCreator = kODShellSignature;
  823.     
  824.     // Query the desktop database:
  825.     DTPBRec pb;
  826.     pb.ioNamePtr = kODNULL;
  827.     pb.ioVRefNum = -1; //spec.vRefNum;
  828.     THROW_IF_ERROR( PBDTGetPath( &pb ) );        // Sets pb.ioDTRefNum
  829.     
  830.     THROW_IF_ERROR( NewIconSuite(&icons) );
  831.     
  832.     ODBoolean hasIcons = kODFalse;
  833.     TRY{
  834.         char iconBuffer[kLarge8BitIconSize];
  835.         for( pb.ioIconType=1; pb.ioIconType<=6; pb.ioIconType++ ) {
  836.             pb.ioTagInfo = 0;
  837.             pb.ioDTBuffer = (Ptr)&iconBuffer;
  838.             pb.ioDTReqCount = sizeof(iconBuffer);
  839.             pb.ioFileCreator = fileCreator; //info.fdCreator;
  840.             pb.ioFileType = fileType; //info.fdType;
  841.             OSErr err= PBDTGetIconSync(&pb);
  842.             
  843.             if( err == noErr ) {
  844.                 Handle hIcon;
  845.                 WASSERT(pb.ioDTActCount==kIconSize[pb.ioIconType-1]);
  846.                 THROW_IF_ERROR( PtrToHand(iconBuffer,&hIcon, kIconSize[pb.ioIconType-1]) );
  847.                 ASSERT(hIcon!=NULL, kODErrAssertionFailed);
  848.                 ASSERT(*hIcon!=NULL, kODErrAssertionFailed);
  849.                 err= AddIconToSuite(hIcon,icons, kIconType[pb.ioIconType-1] );
  850.                 if( err ) {
  851.                     DisposeHandle(hIcon);
  852.                     THROW_IF_ERROR(err);
  853.                 }
  854.                 hasIcons = kODTrue;
  855.             } else if( err != afpItemNotFound )
  856.                 THROW_IF_ERROR(err);
  857.         }
  858.     
  859.     }CATCH_ALL{
  860.         DisposeIconSuite(icons, kODTrue);
  861.         RERAISE;
  862.     }ENDTRY
  863.     
  864.     // FINALLY use a generic icon:
  865.     if( !hasIcons ) {
  866.         DisposeIconSuite(icons, kODTrue);
  867.  
  868.         short id;
  869.                 if( ODGetIsStationery(ev, frame) )
  870.                     id = genericStationeryIconResource;
  871.                 else
  872.                     id = genericDocumentIconResource;
  873.                 icons= GetGenericIcon(id);
  874.             }
  875. #endif // !defined(_PLATFORM_WIN32_)
  876.     return icons;
  877. }
  878.  
  879.  
  880. //--------------------------------------------------------------------
  881. // ODSetIconFamily
  882. //--------------------------------------------------------------------
  883.  
  884. void    ODSetIconFamily(Environment* ev, 
  885.                 ODFrame* frame, ODIconFamily icons, ODBoolean deleteOtherPlatformIcons)
  886. {
  887. #if !defined(_PLATFORM_WIN32_)
  888.     TempODPart         part = frame->AcquirePart(ev);
  889.     
  890.     ODSetPOIconFamily(ev, part, icons, deleteOtherPlatformIcons);
  891.     
  892.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  893.     if (usersFile)
  894.         usersFile->SetCustomIconFamily(icons);
  895. #endif // !defined(_PLATFORM_WIN32_)
  896. }
  897.  
  898. //--------------------------------------------------------------------
  899. // ODGetFileIfRoot
  900. //--------------------------------------------------------------------
  901. PlatformFile*    ODGetFileIfRoot(Environment* ev, ODFrame* frame)
  902. {
  903.     PlatformFile*    usersFile = kODNULL;
  904.     ODBoolean        isRootPartOfDocument;
  905.  
  906.     ASSERT(frame != kODNULL, kODErrIllegalNullInput);
  907.  
  908.     ODFrame*     contFrame = frame->AcquireContainingFrame(ev);
  909.     ODBoolean     isRootPart = (contFrame == kODNULL);
  910.     ODReleaseObject(ev, contFrame);
  911.  
  912.     { TempODWindow window = frame->AcquireWindow(ev);
  913.       isRootPartOfDocument = isRootPart && window->IsRootWindow(ev);
  914.     }
  915.     
  916.     if (isRootPartOfDocument)
  917.     {
  918.         ODDraft* draft;
  919.         { TempODPart part = frame->AcquirePart(ev);
  920.           draft = ODGetSUFromPstObj(ev, part)->GetDraft(ev);
  921.         }
  922.         usersFile = GetPlatformFileFromContainer(ev, 
  923.                                 draft->GetDocument(ev)->GetContainer(ev));
  924.     }
  925.     return usersFile;
  926. }
  927.  
  928.  
  929. //--------------------------------------------------------------------
  930. // DetachIconProc
  931. //--------------------------------------------------------------------
  932.  
  933. static OSErr DetachIconProc( ResType, Handle *theIcon, void */*yourDataPtr*/ )
  934. {
  935. #if !defined(_PLATFORM_WIN32_)
  936.     if( *theIcon ) {
  937.         DetachResource(*theIcon);
  938.         return ResError();
  939.     } else
  940. #endif // !defined(_PLATFORM_WIN32_)
  941.         return noErr;
  942. }
  943.  
  944. //--------------------------------------------------------------------
  945. // GetGenericIcon
  946. //--------------------------------------------------------------------
  947.  
  948. ODStatic ODIconFamily GetGenericIcon( short iconID )
  949. {
  950.     ODIconFamily icon;
  951. #if !defined(_PLATFORM_WIN32_)
  952.     THROW_IF_ERROR( GetIconSuite( &icon, iconID, svAllAvailableData ) );
  953.     IconActionUPP detachProc = NewIconActionProc(&DetachIconProc);
  954.     (void) ForEachIconDo(icon, svAllAvailableData, detachProc, kODNULL);
  955.     DisposeRoutineDescriptor(detachProc);
  956. #endif // !defined(_PLATFORM_WIN32_)
  957.     return icon;
  958. }
  959.  
  960.  
  961. //--------------------------------------------------------------------
  962. // ODGetIsStationery
  963. //--------------------------------------------------------------------
  964.  
  965. ODBoolean    ODGetIsStationery(Environment* ev, 
  966.                 ODFrame* frame)
  967. {
  968.     TempODPart        part = frame->AcquirePart(ev);
  969.     ODBoolean        isStationery;
  970.     
  971.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  972.     if (usersFile)
  973.         isStationery = usersFile->IsStationery();
  974.     else
  975.         isStationery = ODGetSUIsStationery(ev, ODGetSUFromPstObj(ev, part));
  976.     return isStationery;
  977. }
  978.  
  979. //--------------------------------------------------------------------
  980. // ODSetIsStationery
  981. //--------------------------------------------------------------------
  982.  
  983. void        ODSetIsStationery(Environment* ev, 
  984.                 ODFrame* frame, ODBoolean isStationery)
  985. {
  986.     { TempODPart part = frame->AcquirePart(ev);
  987.       ODSetBooleanProp( ev, ODGetSUFromPstObj(ev, part),
  988.                             kODPropIsStationery, kODBoolean, isStationery);
  989.     }
  990.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  991.     if (usersFile)
  992.         usersFile->SetStationery(isStationery);
  993. }
  994.  
  995. //--------------------------------------------------------------------
  996. // ODSetSUIsStationery
  997. //--------------------------------------------------------------------
  998.  
  999. void        ODSetSUIsStationery(Environment* ev, 
  1000.                 ODStorageUnit* su, ODBoolean isStationery)
  1001. {
  1002.     ODSetBooleanProp( ev, su, kODPropIsStationery, kODBoolean, isStationery);
  1003. }
  1004.  
  1005. //--------------------------------------------------------------------
  1006. // ODGetSUIsStationery
  1007. //--------------------------------------------------------------------
  1008.  
  1009. ODBoolean        ODGetSUIsStationery(Environment* ev, 
  1010.                 ODStorageUnit* su)
  1011. {
  1012.     ODBoolean isStationery;
  1013.     
  1014.     if (su->Exists(ev, kODPropIsStationery, kODBoolean, 0))
  1015.         isStationery = ODGetBooleanProp( ev, su, kODPropIsStationery, kODBoolean);
  1016.     else
  1017.         isStationery = kODFalse;
  1018.         
  1019.     return isStationery;
  1020. }
  1021.  
  1022. //-------------------------------------------------------------------------------------
  1023. // Editor and Kind Menu manipulation functions
  1024. //-------------------------------------------------------------------------------------
  1025. // copied from LinkDlgs.cpp (with changes by Té)
  1026. //------------------------------------------------------------------------------
  1027. // AddTypesToMenu
  1028. //------------------------------------------------------------------------------
  1029.  
  1030. void AddTypesToMenu(ODTypeList* typeList, MenuHandle kindMenu, ODSession* session)
  1031. {
  1032.     ODName*                name;
  1033.     ODSShort            item = 0;
  1034. #if !defined(_PLATFORM_WIN32_)
  1035.     Environment*        ev = somGetGlobalEnvironment();
  1036.     ODNameSpaceManager*    nsm = session->GetNameSpaceManager(ev);
  1037.     
  1038.     ODTypeListIterator* typeIter = typeList->CreateTypeListIterator(ev);
  1039.     
  1040.     EditorSet*            editorList = new EditorSet;
  1041.     editorList->InitEditorSet();
  1042.  
  1043.     for (ODType type = typeIter->First(ev);
  1044.         typeIter->IsNotComplete(ev);
  1045.         type = typeIter->Next(ev))
  1046.     {
  1047.         if ( GetUserKindFromKind(nsm, type, &name) )
  1048.         {
  1049.             Str255 itemString;
  1050.             IntlToPStr(name, itemString);
  1051.             InsertMenuItem(kindMenu, itemString, item++);
  1052.             
  1053.             ScriptCode itemScript = GetITextScriptCode(name);
  1054.             SetPopupItemScript(kindMenu, item, itemScript);
  1055.             
  1056.             DisposeIText( name );
  1057.         }
  1058.         else
  1059.         {
  1060.             // there's no name for this type
  1061.             Str255 pKindStr;
  1062.             ODTranslation* translation = session->GetTranslation(ev);
  1063.             ODPlatformType platformType = 
  1064.                 translation->GetPlatformTypeFromISOType(ev, type);
  1065.             if ( platformType == kODNULL )
  1066.             {
  1067.                 // Special menu item
  1068.                 CUsingLibraryResources r;
  1069.                 GetIndString(pKindStr, kODInfoUtilStrsID, kODStrIndNoKinds); 
  1070.             }
  1071.             else
  1072.             {
  1073.                 ODBlockMove(&platformType, &(pKindStr[1]), (ODULong) sizeof(ODPlatformType));
  1074.                 pKindStr[0] = (unsigned char) sizeof(ODPlatformType);
  1075.             }
  1076.             // Allow metacharacters in item text
  1077.             InsertMenuItem(kindMenu, "\p ", item++);
  1078.             SetMenuItemText(kindMenu, item, pKindStr);
  1079.         }
  1080.     
  1081.         if (!GetAllEditorsForKind(nsm, type, editorList))
  1082.             DisableItem(kindMenu, item);
  1083.         editorList->RemoveAllEditors();
  1084.  
  1085.         ODDisposePtr(type);
  1086.     }
  1087.     ODDeleteObject( editorList );
  1088.     ODDeleteObject( typeIter );
  1089. #endif // !defined(_PLATFORM_WIN32_)
  1090. }
  1091.  
  1092. //------------------------------------------------------------------------------
  1093. // EnableTypesInMenu
  1094. //------------------------------------------------------------------------------
  1095.  
  1096. ODUShort EnableTypesInMenu(
  1097.                     ODTypeList*            typeList,
  1098.                     MenuHandle            kindMenu,
  1099.                     ODEditor            editor,
  1100.                     ODNameSpaceManager*    nsm)
  1101. {
  1102.     Environment* ev = somGetGlobalEnvironment();
  1103.     ODUShort itemStart = 0;
  1104.  
  1105. #if !defined(_PLATFORM_WIN32_)
  1106.     if ( kindMenu && typeList && (typeList->Count(ev) > 0) )
  1107.     {
  1108.         ODTypeListIterator* typeIter = typeList->CreateTypeListIterator(ev);
  1109.         
  1110.         for (ODType type = typeIter->First(ev);
  1111.             typeIter->IsNotComplete(ev);
  1112.             type = typeIter->Next(ev))
  1113.         {
  1114.             if ( editor == kODNULL )
  1115.             {
  1116.                 // Editor is not fixed; Aassume kind can be embedded even if there is
  1117.                 // no editor installed for it
  1118.                 EnableItem(kindMenu, ++itemStart);
  1119.             }
  1120.             else
  1121.             {
  1122.                 if ( EditorSupportsKind(nsm, editor, type) )
  1123.                     EnableItem(kindMenu, ++itemStart);
  1124.                 else
  1125.                     DisableItem(kindMenu, ++itemStart);
  1126.             }
  1127.             ODDisposePtr(type);
  1128.         }
  1129.     
  1130.         ODDeleteObject( typeIter );
  1131.     }
  1132.     
  1133. #endif // !defined(_PLATFORM_WIN32_)
  1134.     return itemStart;
  1135. }
  1136.  
  1137. //------------------------------------------------------------------------------
  1138. // DeleteAllMenuItems
  1139. //------------------------------------------------------------------------------
  1140. ODStatic void DeleteAllMenuItems(MenuHandle theMenu)
  1141. {
  1142. #if !defined(_PLATFORM_WIN32_)
  1143.     ODUShort i;
  1144.  
  1145.     for (i = CountMItems(theMenu);  i > 0;  --i)
  1146.     {
  1147.         DeleteMenuItem(theMenu, i);
  1148.     }
  1149. #endif // !defined(_PLATFORM_WIN32_)
  1150. }
  1151.  
  1152. //------------------------------------------------------------------------------
  1153. // SetupEditorMenu
  1154. //------------------------------------------------------------------------------
  1155. ODBoolean SetupEditorMenu(ODType kind,
  1156.                             EditorSet* editorList,
  1157.                             MenuHandle editorMenu, 
  1158.                             ControlHandle popupCtlHndl, 
  1159.                             ODSession* session)
  1160. {
  1161.     ODBoolean anyEditors = (editorList->GetEditorCount() > 0);
  1162.     
  1163. #if !defined(_PLATFORM_WIN32_)
  1164.     Environment* ev = somGetGlobalEnvironment();
  1165.     ODNameSpaceManager* nsm = session->GetNameSpaceManager(ev);
  1166.  
  1167.     // And remove all items from the editors menu
  1168.     DeleteAllMenuItems(editorMenu);
  1169.     
  1170.     // Get all editors supporting that kind; Preferred editor must be first
  1171.     if ( kind && !anyEditors)
  1172.     {
  1173.         ODEditor prefEditor = GetSysPrefEditorForKind(nsm, kind);
  1174.         if ( prefEditor )
  1175.             editorList->AddEditor(prefEditor);
  1176.         ODDisposePtr((ODPtr) prefEditor);
  1177.         anyEditors = GetAllEditorsForKind(nsm, kind, editorList);
  1178.     }
  1179.  
  1180.     if ( anyEditors )
  1181.     {
  1182.         AddEditorsToMenu(editorList, editorMenu, session);
  1183.         if ( popupCtlHndl )
  1184.         {
  1185.             SetControlMaximum(popupCtlHndl, editorList->GetEditorCount());
  1186.             HiliteControl(popupCtlHndl, kControlActive);
  1187.         }
  1188.     }
  1189.     else
  1190.     {
  1191.         // The editor popup should be inactive and contain one blank item
  1192.         AppendMenu(editorMenu, "\p");
  1193.         if ( popupCtlHndl )
  1194.         {
  1195.             SetControlMaximum(popupCtlHndl, 1);
  1196.             HiliteControl(popupCtlHndl, kControlInactive);
  1197.         }
  1198.     }
  1199.  
  1200.     if ( popupCtlHndl )
  1201.     {
  1202.         SetControlMinimum(popupCtlHndl, 1);
  1203.         SetControlValue(popupCtlHndl, 1);
  1204.     }
  1205.     
  1206. #endif // !defined(_PLATFORM_WIN32_)
  1207.     return anyEditors;
  1208. }
  1209.  
  1210. //------------------------------------------------------------------------------
  1211. // AddEditorsToMenu
  1212. //------------------------------------------------------------------------------
  1213.  
  1214. void AddEditorsToMenu(EditorSet* editorList,
  1215.                                 MenuHandle editorMenu, 
  1216.                                 ODSession* session)
  1217. {
  1218.     ODName*        name;
  1219. #if !defined(_PLATFORM_WIN32_)
  1220.     Str255         pEditorString;
  1221.     ODSShort        item = 0;
  1222.     Environment*         ev = somGetGlobalEnvironment();
  1223.     ODNameSpaceManager*    nsm = session->GetNameSpaceManager(ev);
  1224.     
  1225.     EditorSetIterator* editorIter = editorList->CreateIterator();
  1226.     
  1227.     for (ODType editor = editorIter->First();
  1228.         editorIter->IsNotComplete();
  1229.         editor = editorIter->Next())
  1230.     {
  1231.         if (GetUserEditorFromEditor(nsm, editor, &name))
  1232.         {
  1233.             TempODName tempName = name; // DMc: ensure it's deleted
  1234.             IntlToPStr(name, pEditorString);
  1235.             InsertMenuItem(editorMenu, pEditorString, item);
  1236.             EnableItem(editorMenu, ++item);
  1237.             // comments for SetPopupItemScript say don't call unless the script
  1238.             // needs setting, but there's no way to test that here.  Sounds like
  1239.             // an optimization to me....
  1240.             SetPopupItemScript( editorMenu, item, GetITextScriptCode(name) );
  1241.         }
  1242.         else
  1243.         {
  1244.             // Special menu item in italics
  1245.             CUsingLibraryResources r;
  1246.             GetIndString(pEditorString, kODInfoUtilStrsID, kODStrIndNoEditors); 
  1247.  
  1248.             InsertMenuItem(editorMenu, pEditorString, item);
  1249.             DisableItem(editorMenu, ++item);
  1250.         }
  1251.     }
  1252.  
  1253.     ODDeleteObject( editorIter );
  1254. #endif // !defined(_PLATFORM_WIN32_)
  1255. }
  1256.  
  1257. //------------------------------------------------------------------------------
  1258. // GetThisEditorFromList
  1259. //------------------------------------------------------------------------------
  1260. ODEditor GetThisEditorFromList(ODSShort editorIndex, EditorSet* editorList)
  1261. {
  1262.     ODEditor result = kODNULL;
  1263.     
  1264.     if ( editorIndex > 0 )
  1265.     {
  1266.         ODEditor editor;
  1267.         EditorSetIterator* iter = editorList->CreateIterator();
  1268.         
  1269.         for ( editor = iter->First(); 
  1270.               (--editorIndex > 0) && iter->IsNotComplete(); 
  1271.               editor = iter->Next() )
  1272.         {
  1273.         }
  1274.     
  1275.         delete iter;
  1276.         
  1277.         if ( editor != kODNULL )
  1278.             result = ODISOStrFromCStr((char *) editor);
  1279.     }
  1280.  
  1281.     return result;
  1282. }
  1283.  
  1284. //------------------------------------------------------------------------------
  1285. // IndexOfEditorInList
  1286. //------------------------------------------------------------------------------
  1287. ODSShort IndexOfEditorInList(EditorSet* editorList, ODEditor editor)
  1288. {
  1289.     ODSShort retVal = 0;
  1290.     
  1291.     if ( editorList->GetEditorCount() )
  1292.     {
  1293.         ODSShort i = 1;
  1294.         EditorSetIterator* editorIter = editorList->CreateIterator();
  1295.         
  1296.         for (ODEditor anEditor = editorIter->First();
  1297.             editorIter->IsNotComplete();
  1298.             anEditor = editorIter->Next())
  1299.         {
  1300.             if ( (anEditor == editor) || ODISOStrEqual(anEditor, editor) )
  1301.             {
  1302.                 retVal = i;
  1303.                 break;
  1304.             }
  1305.             i++;
  1306.         }
  1307.  
  1308.         ODDeleteObject( editorIter );
  1309.     }
  1310.     
  1311.     return retVal;
  1312. }
  1313.  
  1314. //------------------------------------------------------------------------------
  1315. // IndexOfKindInList
  1316. //------------------------------------------------------------------------------
  1317. ODSShort IndexOfKindInList(ODTypeList* kindList, ODType kind)
  1318. {
  1319.     ODSShort retVal = 0;
  1320.     
  1321.     Environment* ev = somGetGlobalEnvironment();
  1322.  
  1323.     if ( kindList->Count(ev) )
  1324.     {
  1325.         ODSShort i = 1;
  1326.         ODTypeListIterator* kindIter = kindList->CreateTypeListIterator(ev);
  1327.         
  1328.         TRY
  1329.         
  1330.             for (ODType type = kindIter->First(ev);
  1331.                 kindIter->IsNotComplete(ev);
  1332.                 type = kindIter->Next(ev), i++)
  1333.             {
  1334.                 if ( ODISOStrEqual(type, kind) )
  1335.                     retVal = i;
  1336.                 ODDisposePtr(type);
  1337.                 if ( retVal != 0 )
  1338.                     break;
  1339.             }
  1340.  
  1341.         CATCH_ALL
  1342.             ODDeleteObject(kindIter);
  1343.             RERAISE;
  1344.         ENDTRY
  1345.         
  1346.         ODDeleteObject(kindIter);
  1347.     }
  1348.     
  1349.     return retVal;
  1350. }
  1351.  
  1352. //------------------------------------------------------------------------------
  1353. // GetThisKindFromList
  1354. //------------------------------------------------------------------------------
  1355. ODType GetThisKindFromList(ODSShort kindItem, ODTypeList* kindList)
  1356. {
  1357.     Environment* ev = somGetGlobalEnvironment();
  1358.     ODType type = kODNULL;
  1359.     ODTypeListIterator* kindIter = kindList->CreateTypeListIterator(ev);
  1360.  
  1361.     for ( type = kindIter->First(ev); 
  1362.           (--kindItem > 0) && kindIter->IsNotComplete(ev); 
  1363.           type = kindIter->Next(ev) )
  1364.     {
  1365.         ODDisposePtr(type);
  1366.         type = kODNULL;
  1367.     }
  1368.  
  1369.     ODDeleteObject(kindIter);
  1370.  
  1371.     return type;
  1372. }
  1373.  
  1374.  
  1375. //------------------------------------------------------------------------------
  1376. // ContentValueTypes
  1377. //------------------------------------------------------------------------------
  1378. void ContentValueTypes(ODStorageUnit* contentSU, ODTypeList* typeList)
  1379. {
  1380.     TempODType hfsType = kODNULL;
  1381.  
  1382.     TRY
  1383.         ODULong        count;
  1384.         ODULong        index;
  1385.         
  1386.         Environment* ev = somGetGlobalEnvironment();
  1387.  
  1388.         ODTranslation* translation = contentSU->GetSession(ev)->GetTranslation(ev);
  1389.         hfsType = translation->GetISOTypeFromPlatformType(ev, kODFileType_hfs, kODPlatformDataType);
  1390.  
  1391.         contentSU->Focus(ev, kODPropContents, kODPosUndefined, 0, 0, kODPosUndefined);
  1392.         count = contentSU->CountValues(ev);
  1393.         for (index = 1; index <= count; ++index)
  1394.         {
  1395.             contentSU->Focus(ev, kODPropContents, kODPosUndefined, 0, index, kODPosUndefined);
  1396.             TempODType type = contentSU->GetType(ev);
  1397.             if ( !ODISOStrEqual(type, hfsType) )
  1398.                 typeList->AddLast(ev, type);
  1399.         }
  1400.     CATCH_ALL
  1401.     ENDTRY
  1402. }
  1403.  
  1404. //------------------------------------------------------------------------------
  1405. // ODUserRenameFile
  1406. //------------------------------------------------------------------------------
  1407.  
  1408.  
  1409. ODStatic ODBoolean ODUserRenameFile(Environment* ev,
  1410.                     ODSession* session, 
  1411.                     PlatformFile*    usersFile, 
  1412.                     ODIText* name,
  1413.                     DescType replaceOption)
  1414. {
  1415.     ODBoolean    fileRenamed = kODFalse;
  1416. #if !defined(_PLATFORM_WIN32_)
  1417.     
  1418.     // name to aStr255
  1419.     Str255 aStr255;
  1420.     
  1421.     GetITextPString(name, aStr255);
  1422.  
  1423.     TRY
  1424.         usersFile->Rename(aStr255);
  1425.         fileRenamed = kODTrue;
  1426.     CATCH_ALL
  1427.         if (ErrorCode() == dupFNErr)
  1428.         {
  1429.             switch (replaceOption) {
  1430.             case kAEAsk:
  1431.                 ParamText(aStr255, "\p","\p","\p");
  1432.                 fileRenamed = ODAskUserReplace(ev, session);
  1433.                 break;
  1434.             case kAEYes:
  1435.                 fileRenamed = kODTrue; break;
  1436.             case kAENo:
  1437.                 fileRenamed = kODFalse; break;
  1438.             }
  1439.             
  1440.             if (fileRenamed)
  1441.             {
  1442.                 ODFileSpec fsspec = usersFile->GetFileSpec();
  1443.                 ODError result = HDelete(fsspec.vRefNum,fsspec.parID,aStr255);
  1444.                 if ( result!=wPrErr && result!=fLckdErr && 
  1445.                                     result!=fBsyErr && result!=afpAccessDenied )
  1446.                 {
  1447.                     TRY
  1448.                         usersFile->Rename(aStr255);
  1449.                     CATCH_ALL
  1450.                         WARN("File renaming failed, err %ld",ErrorCode());
  1451.                         fileRenamed = kODFalse;
  1452.                     ENDTRY
  1453.                 }
  1454.                 else
  1455.                 {
  1456.                     fileRenamed = kODFalse;
  1457.                     if (replaceOption == kAEAsk)
  1458.                         switch(result)
  1459.                         {
  1460.                             case wPrErr:
  1461.                             case fLckdErr:
  1462.                             case fBsyErr:
  1463.                             {
  1464.                                 CUsingLibraryResources res;
  1465.                                 StopAlert(kFilelockedAlrtID, GetODDialogFilter());
  1466.                                 break;
  1467.                             }
  1468.                             default:
  1469.                                 SysBeep(2);
  1470.                         }
  1471.                     else
  1472.                         SysBeep(2);
  1473.                 }
  1474.             }
  1475.         }
  1476.         else 
  1477.             RERAISE;
  1478.     ENDTRY
  1479. #endif // !defined(_PLATFORM_WIN32_)
  1480.     return( fileRenamed );
  1481. }
  1482.  
  1483. //------------------------------------------------------------------------------
  1484. // ODAskUserReplace
  1485. //------------------------------------------------------------------------------
  1486.  
  1487. ODBoolean    ODAskUserReplace(Environment* ev,
  1488.                             ODSession* session)
  1489. // Pass in the name in question via ParamText(^0).
  1490. {  
  1491. #if !defined(_PLATFORM_WIN32_)
  1492.     CUsingLibraryResources r;
  1493.     ODBoolean    doReplace    = kODFalse;
  1494.     short          itemHit     = 0;
  1495.     DialogPtr     dlg         = ODGetNewDialog(ev, kNameConflictDlgID, 
  1496.                                     session, kODFalse );
  1497.     if (dlg)
  1498.     {
  1499.         Handle        scratchHandle = kODNULL;
  1500.         Rect        scratchRect;
  1501.  
  1502.         SetPort(dlg);
  1503.         
  1504.             // Set the draw routine for the default button outline item
  1505.         GetDialogItem(dlg, kNameConflictDefaultButtonItem, &itemHit, &scratchHandle, &scratchRect);
  1506.         SetDialogItem(dlg, kNameConflictDefaultButtonItem, itemHit, 
  1507.                  (Handle)GetODOutlineDefaultButtonDrawProc(), &scratchRect);
  1508.                 
  1509.         ShowWindow(dlg);
  1510.  
  1511.         do {
  1512.             ModalDialog(GetODDialogFilter(), &itemHit);
  1513.             switch(itemHit)
  1514.             {
  1515.                 case kNameConflictReplaceBtn:
  1516.                     doReplace = kODTrue;
  1517.                     break;
  1518.                 case kNameConflictCancelBtn:
  1519.                     break;
  1520.             }
  1521.         } while ((itemHit != kNameConflictReplaceBtn) && 
  1522.                                         (itemHit != kNameConflictCancelBtn));
  1523.                 
  1524.         ODDisposeDialog(dlg);
  1525.     }
  1526.     return doReplace;
  1527. #else // defined(_PLATFORM_WIN32_)
  1528.     return kODFalse;
  1529. #endif // !defined(_PLATFORM_WIN32_)
  1530. }
  1531.  
  1532. //------------------------------------------------------------------------------
  1533. //    CommentsDontMatch
  1534. //
  1535. //    This compares 2 ODIText strings to see if they match and returns TRUE if
  1536. //    they do not match.  It take into account the fact that the comments field
  1537. //    of the desktop database clips its string to 200 characters.
  1538. //
  1539. //    Returns false if the strings are identical but the script/language are
  1540. //    not.  This is because these values in dtComments have been set using
  1541. //    system defaults while those in propComments were actually saved, and
  1542. //    should therefore take precedence. <eeh>
  1543. //
  1544. //    propComments can be NULL, dtComments cannot
  1545. //------------------------------------------------------------------------------
  1546.  
  1547. ODBoolean
  1548. CommentsDontMatch(ODIText* dtComments, ODIText* propComments)
  1549. {
  1550.     ODBoolean    result;
  1551. #if !defined(_PLATFORM_WIN32_)
  1552.     ODULong        dtLen, propLen;
  1553.     Str255        dtStr, propStr;
  1554.     
  1555.     if (dtComments != kODNULL)
  1556.         dtLen = GetITextStringLength(dtComments);
  1557.     else
  1558.         dtLen = 0;
  1559.     if (propComments == kODNULL)
  1560.         return (dtLen != 0);
  1561.     propLen = GetITextStringLength(propComments);
  1562.     if (dtLen == 0)
  1563.         return (propLen != 0);
  1564.     
  1565.     // if either string is length 0 (or NULL) we won't get here
  1566.     WASSERT(dtComments && propComments);
  1567.     result = kODTrue;
  1568.  
  1569.     // because the Finder does not store script and language with the
  1570.     // comments or file names, we ignore these in comparing strings.  That
  1571.     // is, if the strings are identical byte-for-byte, we consider them
  1572.     // to be equal.  That way, the string *we* stored will be used, as will
  1573.     // the script and language information we stored with it.
  1574.  
  1575.     ODBoolean lengthsMatch =
  1576.             (dtLen == propLen) || (dtLen == 200 && propLen >= 200);
  1577.     if ( lengthsMatch )
  1578.     {
  1579.         GetITextPString(dtComments, (StringPtr) &dtStr);
  1580.         GetITextPString(propComments, (StringPtr) &propStr);
  1581.         propStr[0] = dtStr[0];            // fix the 200 case
  1582.         result = !EqualString( dtStr, propStr, kODTrue, kODTrue );
  1583.     }
  1584. #endif // !defined(_PLATFORM_WIN32_)
  1585.     return result;
  1586.  
  1587. }
  1588.  
  1589.  
  1590. void SetAllWindowShowLinks(Environment* ev, ODWindowState* winState, ODBoolean showLinks)
  1591. {
  1592.     ODWindowIterator* iter = kODNULL; ODVolatile(iter);
  1593.             
  1594.     TRY
  1595.  
  1596.         iter = winState->CreateWindowIterator(ev);
  1597.     
  1598.         for (ODWindow* window = iter->First(ev); iter->IsNotComplete(ev);
  1599.                 window = iter->Next(ev))
  1600.         {
  1601.             if (window)
  1602.             {
  1603.                 window->SetShouldShowLinks(ev, showLinks);
  1604.                 ODFrame* root = window->GetRootFrame(ev);
  1605.                 root->Invalidate(ev, kODNULL, kODNULL);
  1606.             }
  1607.         }
  1608.         ODDeleteObject(iter);
  1609.  
  1610.     
  1611.     CATCH_ALL
  1612.  
  1613.         ODDeleteObject(iter);
  1614.         RERAISE;
  1615.  
  1616.     ENDTRY
  1617. }
  1618.  
  1619. //------------------------------------------------------------------------------
  1620. // TranslateValueTypes
  1621. //------------------------------------------------------------------------------
  1622. void TranslateValueTypes(
  1623.                 ODTypeList*    kindList,
  1624.                 ODTypeList*    translateToList,
  1625.                 OrderedCollection* translateFromList,
  1626.                 ODSession*    session)
  1627. {
  1628.     Environment* ev = somGetGlobalEnvironment();
  1629.  
  1630.     ODTranslation* translation = session->GetTranslation(ev);
  1631.  
  1632.     ODTypeListIterator* kindIter = kindList->CreateTypeListIterator(ev);
  1633.     
  1634.     ODUShort kindIndex = 0;
  1635.  
  1636.     for (ODType kind = kindIter->First(ev);
  1637.         kindIter->IsNotComplete(ev);
  1638.         kind = kindIter->Next(ev))
  1639.     {
  1640.         ++kindIndex;
  1641.  
  1642.         TRY
  1643.             ODTypeList* toList = translation->GetTranslationOf(ev, kind);
  1644.             
  1645.             ODTypeListIterator* toIter = toList->CreateTypeListIterator(ev);
  1646.             
  1647.             for (ODType toKind = toIter->First(ev);
  1648.                 toIter->IsNotComplete(ev);
  1649.                 toKind = toIter->Next(ev))
  1650.             {
  1651.                 TRY
  1652.                 
  1653.                     if ( (kindList->Contains(ev, toKind) == kODFalse)
  1654.                         &&
  1655.                          (translateToList->Contains(ev, toKind) == kODFalse)
  1656.                        )
  1657.                     {
  1658.                         translateToList->AddLast(ev, toKind);
  1659.                         translateFromList->AddLast((void*) kindIndex);
  1660.                     }
  1661.                 
  1662.                 CATCH_ALL
  1663.                 ENDTRY
  1664.                 ODDisposePtr(toKind);
  1665.             }
  1666.             delete toIter;
  1667.             delete toList;
  1668.  
  1669.         CATCH_ALL
  1670.         ENDTRY
  1671.  
  1672.         ODDisposePtr(kind);
  1673.     }
  1674.     delete kindIter;
  1675. }
  1676.  
  1677. //------------------------------------------------------------------------------
  1678. // StringPtrFromStrHandle
  1679. //------------------------------------------------------------------------------
  1680.  
  1681. ODStatic StringPtr StringPtrFromStrHandle(StringHandle strHandle)
  1682. {
  1683.     StringPtr result = kODNULL;
  1684.  
  1685. #if !defined(_PLATFORM_WIN32_)
  1686.     if ( strHandle )
  1687.     {
  1688.         ODULong size = GetHandleSize((Handle) strHandle);
  1689.         StringPtr strPtr = (StringPtr) ODLockHandle((ODHandle) strHandle);
  1690.         result = (StringPtr) ODNewPtr(size);
  1691.         ODBlockMove(strPtr, result, size);
  1692.         ODUnlockHandle((ODHandle) strHandle);
  1693.     }
  1694.  
  1695. #endif // !defined(_PLATFORM_WIN32_)
  1696.     return result;
  1697. }
  1698.  
  1699. //------------------------------------------------------------------------------
  1700. // DefaultMenuStringForKind
  1701. //------------------------------------------------------------------------------
  1702.  
  1703. ODStatic StringPtr DefaultMenuStringForKind()
  1704. {
  1705. #if !defined(_PLATFORM_WIN32_)
  1706.     CUsingLibraryResources r;
  1707.  
  1708.     StringPtr result = kODNULL;
  1709.  
  1710.     StringHandle strHandle = GetString(kODPartInfoStrUnknownID);
  1711.     if ( strHandle )
  1712.     {
  1713.         result = StringPtrFromStrHandle(strHandle);
  1714.         ReleaseResource((Handle) strHandle);
  1715.     }
  1716.     else
  1717.     {
  1718.         strHandle = NewString("\p");
  1719.         result = StringPtrFromStrHandle(strHandle);
  1720.         ODDisposeHandle((ODHandle) strHandle);
  1721.     }
  1722.     return result;
  1723. #else // defined(_PLATFORM_WIN32_)
  1724.     return kODNULL;
  1725. #endif // !defined(_PLATFORM_WIN32_)
  1726. }
  1727.  
  1728. //------------------------------------------------------------------------------
  1729. // GetMenuStringForKind
  1730. //------------------------------------------------------------------------------
  1731.  
  1732. ODStatic void GetMenuStringForKind(
  1733.     ODSession*        session,
  1734.     ODType            kind,
  1735.     StringPtr*        itemString,
  1736.     ODScriptCode*    itemScript)
  1737. {
  1738. #if !defined(_PLATFORM_WIN32_)
  1739.     ODName*                name;
  1740.     Environment*        ev = somGetGlobalEnvironment();
  1741.     ODNameSpaceManager*    nsm = session->GetNameSpaceManager(ev);
  1742.  
  1743.     if ( GetUserKindFromKind(nsm, kind, &name) )
  1744.     {
  1745.         TempODName tempName = name; // DMc: ensure it's deleted
  1746.         *itemString = (StringPtr) ODNewPtr(GetITextStringLength(name)+1);
  1747.         IntlToPStr(name, *itemString);
  1748.         *itemScript = GetITextScriptCode(name);
  1749.     }
  1750.     else
  1751.     {
  1752.         // No available editors support this kind
  1753.         ODTranslation* translation = session->GetTranslation(ev);
  1754.         ODPlatformType platformType = translation->GetPlatformTypeFromISOType(ev, kind);
  1755.         if ( platformType != kODNULL )
  1756.         {
  1757.             *itemString = (StringPtr) ODNewPtr(sizeof(ODPlatformType) + 1);
  1758.             ODBlockMove(&platformType, &((*itemString)[1]), (ODULong) sizeof(ODPlatformType));
  1759.             (*itemString)[0] = (unsigned char) sizeof(ODPlatformType);
  1760.         }
  1761.         else
  1762.         {
  1763.             *itemString = DefaultMenuStringForKind();
  1764.         }
  1765.         *itemScript = smSystemScript;
  1766.     }
  1767. #endif // !defined(_PLATFORM_WIN32_)
  1768. }
  1769.  
  1770. //------------------------------------------------------------------------------
  1771. // AddTranslationKindToMenu
  1772. //------------------------------------------------------------------------------
  1773.  
  1774. void AddTranslationKindToMenu(
  1775.         ODType        kind, 
  1776.         MenuHandle    kindMenu,
  1777.         short        item,
  1778.         ODBoolean    insert,
  1779.         ODSession*    session)
  1780. {
  1781. #if !defined(_PLATFORM_WIN32_)
  1782.     StringPtr itemString;
  1783.     ODScriptCode itemScript;
  1784.     
  1785.     GetMenuStringForKind(session, kind, &itemString, &itemScript);
  1786.     TempODString tempItemString = (char*) itemString; // DMc: ensure it's deleted
  1787.  
  1788.     if ( insert )
  1789.     {
  1790.         InsertMenuItem(kindMenu, itemString, item);
  1791.         item += 1;
  1792.     }
  1793.     else
  1794.         SetMenuItemText(kindMenu, item, itemString);
  1795.     
  1796.     SetPopupItemScript(kindMenu, item, itemScript);
  1797. #endif // !defined(_PLATFORM_WIN32_)
  1798. }
  1799.