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.

236 lines
5.4 KiB

  1. // InsertionStringMenu.cpp: implementation of the CInsertionStringMenu class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. //
  5. // Copyright (c) 2000 Microsoft Corporation
  6. //
  7. // 03-15-00 v-marfin : bug 60935 - Set focus back to edit control
  8. // after inserting a string and set
  9. // cursor in proper location a
  10. //
  11. #include "stdafx.h"
  12. #include "snapin.h"
  13. #include "InsertionStringMenu.h"
  14. #include "WbemClassObject.h"
  15. #include "HMObject.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. BOOL CHiddenWnd::OnCommand(WPARAM wParam, LPARAM lParam)
  22. {
  23. if( ! m_pMenu )
  24. {
  25. return CWnd::OnCommand(wParam,lParam);
  26. }
  27. return m_pMenu->OnCommand(wParam,lParam);
  28. }
  29. //////////////////////////////////////////////////////////////////////
  30. // Construction/Destruction
  31. //////////////////////////////////////////////////////////////////////
  32. CInsertionStringMenu::CInsertionStringMenu()
  33. {
  34. m_pEditCtl = NULL;
  35. }
  36. CInsertionStringMenu::~CInsertionStringMenu()
  37. {
  38. }
  39. //////////////////////////////////////////////////////////////////////
  40. // Create
  41. //////////////////////////////////////////////////////////////////////
  42. bool CInsertionStringMenu::Create(CWnd* pEditControl, CHMObject* pObject, bool bRuleMenu /*=true*/)
  43. {
  44. ASSERT(pEditControl);
  45. if( pEditControl == NULL )
  46. {
  47. return false;
  48. }
  49. ASSERT(pEditControl->GetSafeHwnd());
  50. if( pEditControl->GetSafeHwnd() == NULL )
  51. {
  52. return false;
  53. }
  54. ASSERT(pObject);
  55. if( pObject == NULL )
  56. {
  57. return false;
  58. }
  59. m_pEditCtl = pEditControl;
  60. CString sPrefix;
  61. if( ! bRuleMenu )
  62. {
  63. sPrefix = _T("TargetInstance.EmbeddedStatus.");
  64. }
  65. // get the insertion strings
  66. CWbemClassObject ClassObject;
  67. ClassObject.Create(pObject->GetSystemName());
  68. HRESULT hr = ClassObject.GetObject(_T("Microsoft_HMThresholdStatusInstance"));
  69. if( ! CHECKHRESULT(hr) )
  70. {
  71. return false;
  72. }
  73. ClassObject.GetPropertyNames(m_saInsertionStrings);
  74. ClassObject.Destroy();
  75. CWbemClassObject* pParentObject = pObject->GetParentClassObject();
  76. CString sObjectPath;
  77. CString sNamespace;
  78. CStringArray saEmbeddedInstNames;
  79. if( pParentObject )
  80. {
  81. pParentObject->GetProperty(IDS_STRING_MOF_PATH,sObjectPath);
  82. pParentObject->GetProperty(IDS_STRING_MOF_TARGETNAMESPACE,sNamespace);
  83. delete pParentObject;
  84. pParentObject = NULL;
  85. }
  86. if( ! sObjectPath.IsEmpty() )
  87. {
  88. int iIndex = -1;
  89. if( (iIndex = sObjectPath.Find(_T("."))) != -1 )
  90. {
  91. sObjectPath = sObjectPath.Left(iIndex);
  92. }
  93. CWbemClassObject WmiObject;
  94. WmiObject.SetNamespace(_T("\\\\") + pObject->GetSystemName() + _T("\\") + sNamespace);
  95. WmiObject.GetObject(sObjectPath);
  96. WmiObject.GetPropertyNames(saEmbeddedInstNames);
  97. for( int i = 0; i < saEmbeddedInstNames.GetSize(); i++ )
  98. {
  99. saEmbeddedInstNames.SetAt(i,_T("%") + sPrefix + _T("EmbeddedInstance.") + saEmbeddedInstNames[i]+ _T("%"));
  100. }
  101. }
  102. ASSERT(m_saInsertionStrings.GetSize());
  103. if( m_saInsertionStrings.GetSize() == 0 )
  104. {
  105. return false;
  106. }
  107. for( int i = 0; i < m_saInsertionStrings.GetSize(); i++ )
  108. {
  109. m_saInsertionStrings.SetAt(i,_T("%") + sPrefix + m_saInsertionStrings[i] + _T("%"));
  110. }
  111. m_saInsertionStrings.Append(saEmbeddedInstNames);
  112. m_pEditCtl->SetCaretPos(CPoint(0,0));
  113. if( ! m_HiddenWnd.Create(NULL,NULL,WS_CHILD,CRect(0,0,10,10),m_pEditCtl,2411) )
  114. {
  115. m_saInsertionStrings.RemoveAll();
  116. return false;
  117. }
  118. m_HiddenWnd.m_pMenu = this;
  119. m_HiddenWnd.ShowWindow(SW_HIDE);
  120. return true;
  121. }
  122. //////////////////////////////////////////////////////////////////////
  123. // DisplayMenu
  124. //////////////////////////////////////////////////////////////////////
  125. void CInsertionStringMenu::DisplayMenu(CPoint& pt)
  126. {
  127. ASSERT(m_saInsertionStrings.GetSize());
  128. if( m_saInsertionStrings.GetSize() == 0)
  129. {
  130. return;
  131. }
  132. ASSERT(m_pEditCtl);
  133. if( m_pEditCtl == NULL )
  134. {
  135. return;
  136. }
  137. ASSERT(m_pEditCtl->GetSafeHwnd());
  138. if( m_pEditCtl->GetSafeHwnd() == NULL )
  139. {
  140. return;
  141. }
  142. if( ! CreatePopupMenu() )
  143. {
  144. ASSERT(FALSE);
  145. return;
  146. }
  147. // add each insertion string to the menu
  148. for( int i = 0; i < m_saInsertionStrings.GetSize(); i++ )
  149. {
  150. InsertMenu(i,MF_BYPOSITION,i,m_saInsertionStrings[i]);
  151. }
  152. TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,&m_HiddenWnd);
  153. DestroyMenu();
  154. }
  155. BOOL CInsertionStringMenu::OnCommand(WPARAM wParam, LPARAM lParam)
  156. {
  157. ASSERT(m_saInsertionStrings.GetSize());
  158. if( m_saInsertionStrings.GetSize() == 0)
  159. {
  160. return FALSE;
  161. }
  162. ASSERT(m_pEditCtl);
  163. if( m_pEditCtl == NULL )
  164. {
  165. return FALSE;
  166. }
  167. ASSERT(m_pEditCtl->GetSafeHwnd());
  168. if( m_pEditCtl->GetSafeHwnd() == NULL )
  169. {
  170. return FALSE;
  171. }
  172. int id = LOWORD(wParam);
  173. if( id < m_saInsertionStrings.GetSize() && id >= 0 )
  174. {
  175. CPoint point = m_pEditCtl->GetCaretPos();
  176. int iCharIndex = LOWORD(((CEdit*)m_pEditCtl)->CharFromPos(point));
  177. CString sWindowText;
  178. m_pEditCtl->GetWindowText(sWindowText);
  179. sWindowText.Insert(iCharIndex,m_saInsertionStrings[id]);
  180. m_pEditCtl->SetWindowText(sWindowText);
  181. //--------------------------------------------------------
  182. // v-marfin : bug 60935 - Set focus back to edit control
  183. // after inserting a string and set
  184. // cursor in proper location a
  185. m_pEditCtl->SetFocus();
  186. CEdit* pEdit = (CEdit*)m_pEditCtl;
  187. CString sInsertion = m_saInsertionStrings[id];
  188. int nLen = sInsertion.GetLength() + iCharIndex;
  189. pEdit->SetSel(nLen,nLen,TRUE);
  190. //--------------------------------------------------------
  191. return TRUE;
  192. }
  193. return FALSE;
  194. }