Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.2 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 000
  5. *
  6. * File: smarticon.cpp
  7. *
  8. * Contents: Implementation file for CSmartIcon
  9. *
  10. * History: 25-Jul-2000 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #include "smarticon.h"
  14. /*+-------------------------------------------------------------------------*
  15. * CSmartIcon::~CSmartIcon
  16. *
  17. * Destroys a CSmartIcon object.
  18. *--------------------------------------------------------------------------*/
  19. CSmartIcon::~CSmartIcon ()
  20. {
  21. Release();
  22. }
  23. /*+-------------------------------------------------------------------------*
  24. * CSmartIcon::CSmartIcon
  25. *
  26. * Copy constructor for a CSmartIcon object.
  27. *--------------------------------------------------------------------------*/
  28. CSmartIcon::CSmartIcon (const CSmartIcon& other)
  29. {
  30. m_pData = other.m_pData;
  31. if (m_pData)
  32. m_pData->AddRef();
  33. }
  34. /*+-------------------------------------------------------------------------*
  35. * CSmartIcon::operator=
  36. *
  37. * Assignment operator for CSmartIcon.
  38. *--------------------------------------------------------------------------*/
  39. CSmartIcon& CSmartIcon::operator= (const CSmartIcon& rhs)
  40. {
  41. if (&rhs != this)
  42. {
  43. Release();
  44. m_pData = rhs.m_pData;
  45. if (m_pData)
  46. m_pData->AddRef();
  47. }
  48. return *this;
  49. }
  50. /*+-------------------------------------------------------------------------*
  51. * CSmartIcon::Attach
  52. *
  53. * Releases the currently held icon and creates a CSmartIconData to hold
  54. * a reference to the given icon.
  55. *
  56. * You would use this method in the same way you'd use CComPtr<T>::Attach.
  57. *
  58. * This method will destroy the icon if the underlying CSmartIconData object
  59. * cannot be created because of insufficient memory.
  60. *--------------------------------------------------------------------------*/
  61. void CSmartIcon::Attach (HICON hIcon)
  62. {
  63. /*
  64. * if we're already attached to this icon, there's nothing to do
  65. */
  66. if (operator HICON() == hIcon)
  67. return;
  68. Release();
  69. ASSERT (m_pData == NULL);
  70. /*
  71. * if we couldn't create a CSmartIconData to hold hIcon, destroy hIcon
  72. */
  73. if ( (hIcon != NULL) &&
  74. ((m_pData = CSmartIconData::CreateInstance (hIcon)) == NULL))
  75. {
  76. DestroyIcon (hIcon);
  77. }
  78. }
  79. /*+-------------------------------------------------------------------------*
  80. * CSmartIcon::Detach
  81. *
  82. * Releases the currently held icon, passing ownership (and responsibility
  83. * for deletion) to the caller.
  84. *
  85. * You would use this method in the same way you'd use CComPtr<T>::Detach.
  86. *--------------------------------------------------------------------------*/
  87. HICON CSmartIcon::Detach ()
  88. {
  89. HICON hIcon = NULL;
  90. /*
  91. * if we've got an icon, detach it from our CSmartIconData
  92. */
  93. if (m_pData != NULL)
  94. {
  95. hIcon = m_pData->Detach();
  96. m_pData = NULL;
  97. }
  98. return (hIcon);
  99. }
  100. /*+-------------------------------------------------------------------------*
  101. * CSmartIcon::Release
  102. *
  103. * Releases this CSmartIcon's reference on its icon. It is safe to call
  104. * this on a CSmartIcon that doesn't refer to an icon.
  105. *
  106. * You would use this method in the same way you'd use CComPtr<T>::Release.
  107. *--------------------------------------------------------------------------*/
  108. void CSmartIcon::Release()
  109. {
  110. if (m_pData)
  111. {
  112. m_pData->Release();
  113. m_pData = NULL;
  114. }
  115. }
  116. /*+-------------------------------------------------------------------------*
  117. * CSmartIcon::CSmartIconData::Detach
  118. *
  119. * Releases the currently held icon, passing ownership (and responsibility
  120. * for deletion) to the caller.
  121. *--------------------------------------------------------------------------*/
  122. HICON CSmartIcon::CSmartIconData::Detach ()
  123. {
  124. HICON hIcon = NULL;
  125. /*
  126. * if there's only one reference on us, then we can return the icon
  127. * we holding directly to the caller
  128. */
  129. if (m_dwRefs == 1)
  130. {
  131. hIcon = m_hIcon;
  132. m_hIcon = NULL; // so our d'tor won't delete it
  133. }
  134. /*
  135. * otherwise, we have more than one reference on us; we need to copy
  136. * the icon we're holding so others who refer to us won't have their
  137. * icons destroyed from underneath them
  138. */
  139. else
  140. hIcon = CopyIcon (m_hIcon);
  141. /*
  142. * let go of our reference
  143. */
  144. Release();
  145. /*
  146. * Hands off! Release() may have deleted this object.
  147. */
  148. return (hIcon);
  149. }