Ihtmldomnode usage (the DOM tree class used to display web pages)

Source: Internet
Author: User
This article (2008.10.17) describes how to connect ihtmldomnode with ihtmldomchildrencollection and use it.
  1. The DOM tree class used to display web pages, inherited from ctreectrl -- source file
  2. // Domtree. cpp: implementation file
  3. # Include "stdafx. H"
  4. # Include "domtree. H"
  5. # Include <queue>
  6. # Define idm_expandall 10001
  7. # Define idm_collapseall 10002
  8. Implement_dynamic (cdomtree, ctreectrl)
  9. Begin_message_map (cdomtree, ctreectrl)
  10. On_policy_reflect (tvn_deleteitem, ontvndeleteitem)
  11. On_wm_destroy ()
  12. On_policy_reflect (tvn_selchanged, ontvnselchanged)
  13. On_command (idm_expandall, onexpandall)
  14. On_command (idm_collapseall, oncollapseall)
  15. On_wm_rbuttondown ()
  16. End_message_map ()
  17. Void cdomtree: ontvndeleteitem (nmhdr * pnmhdr, lresult * presult)
  18. {
  19. Lpnmtreeview pnmtreeview = reinterpret_cast <lpnmtreeview> (pnmhdr );
  20. Tvitem * pitem = & pnmtreeview-> itemold;
  21. If (pitem & pitem-> lparam)
  22. {
  23. Iunknown * pnodeunknown = reinterpret_cast <iunknown *> (pitem-> lparam );
  24. # If defined (_ Debug)
  25. Long relcnt = pnodeunknown-> release (); // For addref when added to the tree item data
  26. Atlassert (relcnt = 0 );
  27. # Else
  28. Pnodeunknown-> release ();
  29. # Endif
  30. }
  31. * Presult = 0;
  32. }
  33. Void cdomtree: ondestroy ()
  34. {
  35. Ctreectrl: ondestroy ();
  36. }
  37. Void cdomtree: updatedom (ihtmldocument2 * pdoc)
  38. {
  39. Setredraw (false );
  40. Deleteallitems ();
  41. M_spdoc2 = pdoc;
  42. Preparedomtree ();
  43. Setredraw (true );
  44. }
  45. // A breadth first tree creating algorithm
  46. Bool cdomtree: preparedomtree ()
  47. {
  48. Ccomqiptr <ihtmldocument3> spdoc3 = m_spdoc2;
  49. Bool Bret = spdoc3! = NULL;
  50. If (! Bret ){
  51. Atlassert (0 );
  52. Return Bret;
  53. }
  54. Ccomptr <ihtmlelement> sprootelement;
  55. Bret = succeeded (spdoc3-> get_documentelement (& sprootelement ));
  56. If (! Bret) return Bret;
  57. Ccomqiptr <ihtmldomnode> sprootnode = sprootelement;
  58. Bret = sprootnode! = NULL;
  59. If (! Bret) return Bret;
  60. STD: queue
  61. STD: queue <ccomqiptr <ihtmldomnode> queue_domnodes;
  62. Queue_domnodes. Push (sprootnode );
  63. Queue_tree_nodes.push (tvi_root );
  64. While (! Queue_tree_nodes.empty ())
  65. {
  66. Ccomqiptr <ihtmldomnode> spdomnode = queue_domnodes.front ();
  67. Htreeitem parent = queue_tree_nodes.front ();
  68. Queue_domnodes.pop ();
  69. Queue_tree_nodes.pop ();
  70. Htreeitem thisnode = insertdomnode (spdomnode, parent );
  71. Ccomptr <idispatch> spcollectiondispatch;
  72. If (succeeded (spdomnode-> get_childnodes (& spcollectiondispatch )))
  73. {
  74. Long numchildren = 0;
  75. Ccomqiptr <ihtmldomchildrencollection> spcollection = spcollectiondispatch;
  76. If (!! Spcollection)
  77. {
  78. Spcollection-> get_length (& numchildren );
  79. For (long I = 0; I <numchildren; I ++)
  80. {
  81. Ccomptr <idispatch> spitemdispatch;
  82. Spcollection-> item (I, & spitemdispatch );
  83. If (!! Spitemdispatch)
  84. {
  85. Ccomqiptr <ihtmldomnode> spitemnode = spitemdispatch;
  86. If (!! Spitemnode)
  87. {
  88. Queue_domnodes.push (spitemnode );
  89. Queue_tree_nodes.push (thisnode );
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. Return Bret;
  97. }
  98. Htreeitem cdomtree: insertdomnode (ihtmldomnode * pinode, htreeitem hparent)
  99. {
  100. Ccomptr <idispatch> spcollectiondispatch;
  101. Ccomptr <ihtmldomnode> spnode (pinode );
  102. Ccombstr nodename;
  103. TV _insertstruct Tvis;
  104. Tvis. hparent = hparent;
  105. Tvis. hinsertafter = tvi_last;
  106. Tvis. item. Mask = tvif_text | tvif_param;
  107. If (succeeded (spnode-> get_nodename (& nodename )))
  108. {
  109. Cstring strnodename (nodename );
  110. Tvis. item. psztext = strdup (strnodename );
  111. }
  112. // Need to addref because we'll be keeping interface pointer As Treeview item data
  113. Pinode-> addref ();
  114. Tvis. item. lparam = reinterpret_cast <lparam> (pinode );
  115. Htreeitem hthisitem = insertitem (& Tvis );
  116. If (hthisitem = NULL)
  117. {
  118. Pinode-> release ();
  119. Return NULL;
  120. }
  121. Return hthisitem;
  122. }
  123. Void cdomtree: ontvnselchanged (nmhdr * pnmhdr, lresult * presult)
  124. {
  125. Lpnmtreeview pnmtreeview = reinterpret_cast <lpnmtreeview> (pnmhdr );
  126. Tvitem * pitem = & pnmtreeview-> itemnew;
  127. Ihtmldomnode * pdomnode = reinterpret_cast <ihtmldomnode *> (pitem-> lparam );
  128. * Presult = 0;
  129. }
  130. Void cdomtree: onexpandall ()
  131. {
  132. Expandall ();
  133. }
  134. Void cdomtree: oncollapseall ()
  135. {
  136. Collapseall ();
  137. }
  138. Void cdomtree: onrbuttondown (uint nflags, cpoint point)
  139. {
  140. Cmenu context;
  141. Context. createpopupmenu ();
  142. Clienttoscreen (& Point );
  143. Context. appendmenu (mf_string | mf_enabled, idm_expandall, _ T ("Expand All "));
  144. Context. appendmenu (mf_string | mf_enabled, idm_collapseall, _ T ("collapse all "));
  145. Context. trackpopupmenu (tpm_leftalign | tpm_leftbutton | tpm_rightbutton, point. X, point. Y, this );
  146. Ctreectrl: onrbuttondown (nflags, point );
  147. }
  148. Void cdomtree: expandall ()
  149. {
  150. Htreeitem hcurrent = getselecteditem ();
  151. If (! Hcurrent)
  152. Hcurrent = getrootitem ();
  153. If (hcurrent)
  154. {
  155. Setredraw (false );
  156. // When expanding we must expand parent before its children
  157. Unsigned int flags = doparentfirst;
  158. Doforitemandchildren (hcurrent, expanditem, flags );
  159. Ensurevisible (hcurrent );
  160. Setredraw (true );
  161. }
  162. }
  163. // Recursive helper that calla method, that Param FP points to, for a hparent Node
  164. // And all of its children, its children's children, etc.
  165. // This is one of few cases where I used-> * notation for method call through a pointer
  166. Void cdomtree: doforitemandchildren (htreeitem hparent, foreachitem FP, unsigned int flags/* = 0 */)
  167. {
  168. // Doparentfirst flag tells us if we want FP to be called on parent node first, then children, or viceversa
  169. If (flags & doparentfirst)
  170. {
  171. If (! (Flags & skiptopparent ))
  172. (This-> * FP) (hparent );
  173. }
  174. Htreeitem hchild = getchilditem (hparent );
  175. While (hchild)
  176. {
  177. Doforitemandchildren (hchild, FP, (flags & doparentfirst )? Doparentfirst: 0 );
  178. Hchild = getnextsiblingitem (hchild );
  179. };
  180. If (! (Flags & doparentfirst ))
  181. {
  182. If (! (Flags & skiptopparent ))
  183. (This-> * FP) (hparent );
  184. }
  185. }
  186. Void cdomtree: collapseall ()
  187. {
  188. Htreeitem hcurrent = getselecteditem ();
  189. If (! Hcurrent)
  190. Hcurrent = getrootitem ();
  191. If (hcurrent)
  192. {
  193. Setredraw (false );
  194. Expand (hcurrent, tve_collapse );
  195. Setredraw (true );
  196. }
  197. }

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.