Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of libdom.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
  6.  */
  7.  
  8. #include <stdlib.h>
  9.  
  10. #include "events/mutation_event.h"
  11. #include "core/document.h"
  12.  
  13. static void _virtual_dom_mutation_event_destroy(struct dom_event *evt);
  14.  
  15. static struct dom_event_private_vtable _event_vtable = {
  16.         _virtual_dom_mutation_event_destroy
  17. };
  18.  
  19. /* Constructor */
  20. dom_exception _dom_mutation_event_create(struct dom_document *doc,
  21.                 struct dom_mutation_event **evt)
  22. {
  23.         *evt = malloc(sizeof(dom_mutation_event));
  24.         if (*evt == NULL)
  25.                 return DOM_NO_MEM_ERR;
  26.        
  27.         ((struct dom_event *) *evt)->vtable = &_event_vtable;
  28.  
  29.         return _dom_mutation_event_initialise(doc, *evt);
  30. }
  31.  
  32. /* Destructor */
  33. void _dom_mutation_event_destroy(struct dom_mutation_event *evt)
  34. {
  35.         _dom_mutation_event_finalise(evt);
  36.  
  37.         free(evt);
  38. }
  39.  
  40. /* Initialise function */
  41. dom_exception _dom_mutation_event_initialise(struct dom_document *doc,
  42.                 struct dom_mutation_event *evt)
  43. {
  44.         evt->related_node = NULL;
  45.         evt->prev_value = NULL;
  46.         evt->new_value = NULL;
  47.         evt->attr_name = NULL;
  48.  
  49.         return _dom_event_initialise(doc, &evt->base);
  50. }
  51.  
  52. /* Finalise function */
  53. void _dom_mutation_event_finalise(struct dom_mutation_event *evt)
  54. {
  55.         dom_node_unref(evt->related_node);
  56.         dom_string_unref(evt->prev_value);
  57.         dom_string_unref(evt->new_value);
  58.         dom_string_unref(evt->attr_name);
  59.        
  60.         evt->related_node = NULL;
  61.         evt->prev_value = NULL;
  62.         evt->new_value = NULL;
  63.         evt->attr_name = NULL;
  64.  
  65.         _dom_event_finalise(&evt->base);
  66. }
  67.  
  68. /* The virtual destroy function */
  69. void _virtual_dom_mutation_event_destroy(struct dom_event *evt)
  70. {
  71.         _dom_mutation_event_destroy((dom_mutation_event *) evt);
  72. }
  73.  
  74. /*----------------------------------------------------------------------*/
  75. /* The public API */
  76.  
  77. /**
  78.  * Get the related node
  79.  *
  80.  * \param evt   The Event object
  81.  * \param node  The related node
  82.  * \return DOM_NO_ERR.
  83.  */
  84. dom_exception _dom_mutation_event_get_related_node(dom_mutation_event *evt,
  85.                 struct dom_node **node)
  86. {
  87.         *node = evt->related_node;
  88.         dom_node_ref(*node);
  89.  
  90.         return DOM_NO_ERR;
  91. }
  92.  
  93. /**
  94.  * Get the old value
  95.  *
  96.  * \param evt  The Event object
  97.  * \param ret  The old value
  98.  * \return DOM_NO_ERR.
  99.  */
  100. dom_exception _dom_mutation_event_get_prev_value(dom_mutation_event *evt,
  101.                 dom_string **ret)
  102. {
  103.         *ret = evt->prev_value;
  104.         dom_string_ref(*ret);
  105.  
  106.         return DOM_NO_ERR;
  107. }
  108.  
  109. /**
  110.  * Get the new value
  111.  *
  112.  * \param evt  The Event object
  113.  * \param ret  The new value
  114.  * \return DOM_NO_ERR.
  115.  */
  116. dom_exception _dom_mutation_event_get_new_value(dom_mutation_event *evt,
  117.                 dom_string **ret)
  118. {
  119.         *ret = evt->new_value;
  120.         dom_string_ref(*ret);
  121.  
  122.         return DOM_NO_ERR;
  123. }
  124.  
  125. /**
  126.  * Get the attr name
  127.  *
  128.  * \param evt  The Event object
  129.  * \param ret  The attribute name
  130.  * \return DOM_NO_ERR.
  131.  */
  132. dom_exception _dom_mutation_event_get_attr_name(dom_mutation_event *evt,
  133.                 dom_string **ret)
  134. {
  135.         *ret = evt->attr_name;
  136.         dom_string_ref(*ret);
  137.  
  138.         return DOM_NO_ERR;
  139. }
  140.  
  141. /**
  142.  * Get the way the attribute change
  143.  *
  144.  * \param evt   The Event object
  145.  * \param type  The change type
  146.  * \return DOM_NO_ERR.
  147.  */
  148. dom_exception _dom_mutation_event_get_attr_change(dom_mutation_event *evt,
  149.                 dom_mutation_type *type)
  150. {
  151.         *type = evt->change;
  152.  
  153.         return DOM_NO_ERR;
  154. }
  155.  
  156. /**
  157.  * Initialise the MutationEvent
  158.  *
  159.  * \param evt         The Event object
  160.  * \param type        The type of this UIEvent
  161.  * \param bubble      Whether this event can bubble
  162.  * \param cancelable  Whether this event is cancelable
  163.  * \param node        The mutation node
  164.  * \param prev_value  The old value
  165.  * \param new_value   The new value
  166.  * \param attr_name   The attribute's name
  167.  * \param change      The change type
  168.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  169.  */
  170. dom_exception _dom_mutation_event_init(dom_mutation_event *evt,
  171.                 dom_string *type, bool bubble, bool cancelable,
  172.                 struct dom_node *node, dom_string *prev_value,
  173.                 dom_string *new_value, dom_string *attr_name,
  174.                 dom_mutation_type change)
  175. {
  176.         evt->related_node = node;
  177.         dom_node_ref(node);
  178.  
  179.         evt->prev_value = prev_value;
  180.         dom_string_ref(prev_value);
  181.  
  182.         evt->new_value = new_value;
  183.         dom_string_ref(new_value);
  184.  
  185.         evt->attr_name = attr_name;
  186.         dom_string_ref(attr_name);
  187.  
  188.         evt->change = change;
  189.  
  190.         return _dom_event_init(&evt->base, type, bubble, cancelable);
  191. }
  192.  
  193. /**
  194.  * Initialise the MutationEvent with namespace
  195.  *
  196.  * \param evt         The Event object
  197.  * \param namespace   The namespace
  198.  * \param type        The type of this UIEvent
  199.  * \param bubble      Whether this event can bubble
  200.  * \param cancelable  Whether this event is cancelable
  201.  * \param node        The mutation node
  202.  * \param prev_value  The old value
  203.  * \param new_value   The new value
  204.  * \param attr_name   The attribute's name
  205.  * \param change      The change type
  206.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  207.  */
  208. dom_exception _dom_mutation_event_init_ns(dom_mutation_event *evt,
  209.                 dom_string *namespace, dom_string *type,
  210.                 bool bubble, bool cancelable, struct dom_node *node,
  211.                 dom_string *prev_value, dom_string *new_value,
  212.                 dom_string *attr_name, dom_mutation_type change)
  213. {
  214.         evt->related_node = node;
  215.         dom_node_ref(node);
  216.  
  217.         evt->prev_value = prev_value;
  218.         dom_string_ref(prev_value);
  219.  
  220.         evt->new_value = new_value;
  221.         dom_string_ref(new_value);
  222.  
  223.         evt->attr_name = attr_name;
  224.         dom_string_ref(attr_name);
  225.  
  226.         evt->change = change;
  227.  
  228.         return _dom_event_init_ns(&evt->base, namespace, type, bubble,
  229.                         cancelable);
  230. }
  231.  
  232.