home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / miconutils.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  3.5 KB  |  123 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // miconutlis.cp
  20.  
  21. // icon utilities
  22.  
  23. #include "miconutils.h"
  24. #include "prtypes.h"
  25.  
  26. struct IconRecord    {
  27.     Int16    fId;
  28.     Handle fIcon;    // CIconHandle for IconSuites
  29.     Int16 fRefCount;
  30. };
  31.  
  32. CIconList CIconList::sIconList;
  33. CIconList CIconList::sIconSuiteList;
  34.  
  35. CIconList::CIconList() : LArray(sizeof(IconRecord)) {}
  36.  
  37. CIconList::~CIconList() {}
  38.  
  39. CIconHandle CIconList::GetIcon(Int16 iconID)
  40. {    
  41.     IconRecord theIcon;
  42.     LArrayIterator iter(sIconList);
  43.     while (iter.Next(&theIcon))
  44.         if (theIcon.fId == iconID)
  45.         {
  46.             Int32 where = sIconList.FetchIndexOf(&theIcon);    // Increase the refcount by 
  47.                                                     // doing a replace
  48.             sIconList.RemoveItemsAt(1, where);
  49.             theIcon.fRefCount += 1;
  50.             sIconList.InsertItemsAt(1, 1, &theIcon);
  51.             return (CIconHandle)theIcon.fIcon;
  52.         }
  53.     // Did not find the icon. Create new one
  54.     theIcon.fId = iconID;
  55.     theIcon.fIcon = (Handle)::GetCIcon (iconID);
  56.     if (theIcon.fIcon == nil)
  57.         return nil;
  58.     theIcon.fRefCount = 1;
  59.     sIconList.InsertItemsAt(1, 1, &theIcon);
  60.     return (CIconHandle)theIcon.fIcon;
  61. }
  62.  
  63. void CIconList::ReturnIcon(CIconHandle iconH)
  64. {
  65.     IconRecord theIcon;
  66.     LArrayIterator iter(sIconList);
  67.     while (iter.Next(&theIcon))
  68.         if (theIcon.fIcon == (Handle)iconH)
  69.         {
  70.             Int32 where = sIconList.FetchIndexOf(&theIcon);    // Increase the refcount by 
  71.                                                     // doing a replace
  72.             theIcon.fRefCount -= 1;
  73.             sIconList.RemoveItemsAt(1, where);
  74.             if (theIcon.fRefCount == 0)
  75.                 DisposeCIcon(iconH);
  76.             else
  77.                 sIconList.InsertItemsAt(1, 1, &theIcon);
  78.         }
  79. }
  80.  
  81. Handle CIconList::GetIconSuite(Int16 iconID)
  82. {    
  83.     IconRecord theIcon;
  84.     LArrayIterator iter(sIconSuiteList);
  85.     while (iter.Next(&theIcon))
  86.         if (theIcon.fId == iconID)
  87.         {
  88.             Int32 where = sIconSuiteList.FetchIndexOf(&theIcon);    // Increase the refcount by 
  89.                                                     // doing a replace
  90.             sIconSuiteList.RemoveItemsAt(1, where);
  91.             theIcon.fRefCount += 1;
  92.             sIconSuiteList.InsertItemsAt(1, 1, &theIcon);
  93.             return theIcon.fIcon;
  94.         }
  95.     // Did not find the icon. Create new one
  96.     theIcon.fId = iconID;
  97.     OSErr err =    ::GetIconSuite(&theIcon.fIcon, iconID, svAllAvailableData);
  98.     if (err != noErr)
  99.         return nil;
  100.     theIcon.fRefCount = 1;
  101.     sIconSuiteList.InsertItemsAt(1, 1, &theIcon);
  102.     return theIcon.fIcon;
  103. }
  104.  
  105. void CIconList::ReturnIconSuite(Handle iconH)
  106. {
  107.     IconRecord theIcon;
  108.     LArrayIterator iter(sIconSuiteList);
  109.     while (iter.Next(&theIcon))
  110.         if (theIcon.fIcon == iconH)
  111.         {
  112.             Int32 where = sIconSuiteList.FetchIndexOf(&theIcon);    // Increase the refcount by 
  113.                                                     // doing a replace
  114.             theIcon.fRefCount -= 1;
  115.             sIconSuiteList.RemoveItemsAt(1, where);
  116.             if (theIcon.fRefCount == 0)
  117.                 DisposeIconSuite(iconH, TRUE);
  118.             else
  119.                 sIconSuiteList.InsertItemsAt(1, 1, &theIcon);
  120.         }
  121. }
  122.  
  123.