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. #include <string.h>
  10.  
  11. #include "events/mouse_event.h"
  12. #include "core/document.h"
  13.  
  14. #include "utils/utils.h"
  15.  
  16. static void _virtual_dom_mouse_event_destroy(struct dom_event *evt);
  17.  
  18. static struct dom_event_private_vtable _event_vtable = {
  19.         _virtual_dom_mouse_event_destroy
  20. };
  21.  
  22. /* Constructor */
  23. dom_exception _dom_mouse_event_create(struct dom_document *doc,
  24.                 struct dom_mouse_event **evt)
  25. {
  26.         *evt = malloc(sizeof(dom_mouse_event));
  27.         if (*evt == NULL)
  28.                 return DOM_NO_MEM_ERR;
  29.        
  30.         ((struct dom_event *) *evt)->vtable = &_event_vtable;
  31.  
  32.         return _dom_mouse_event_initialise(doc, *evt);
  33. }
  34.  
  35. /* Destructor */
  36. void _dom_mouse_event_destroy(struct dom_mouse_event *evt)
  37. {
  38.         _dom_mouse_event_finalise((dom_ui_event *) evt);
  39.  
  40.         free(evt);
  41. }
  42.  
  43. /* Initialise function */
  44. dom_exception _dom_mouse_event_initialise(struct dom_document *doc,
  45.                 struct dom_mouse_event *evt)
  46. {
  47.         evt->modifier_state = 0;
  48.  
  49.         return _dom_ui_event_initialise(doc, (dom_ui_event *) evt);
  50. }
  51.  
  52. /* The virtual destroy function */
  53. void _virtual_dom_mouse_event_destroy(struct dom_event *evt)
  54. {
  55.         _dom_mouse_event_destroy((dom_mouse_event *) evt);
  56. }
  57.  
  58. /*----------------------------------------------------------------------*/
  59. /* The public API */
  60.  
  61. /**
  62.  * Get screenX
  63.  *
  64.  * \param evt  The Event object
  65.  * \param x    The returned screenX
  66.  * \return DOM_NO_ERR.
  67.  */
  68. dom_exception _dom_mouse_event_get_screen_x(dom_mouse_event *evt,
  69.                 int32_t *x)
  70. {
  71.         *x = evt->sx;
  72.  
  73.         return DOM_NO_ERR;
  74. }
  75.  
  76. /**
  77.  * Get screenY
  78.  *
  79.  * \param evt  The Event object
  80.  * \param y    The returned screenY
  81.  * \return DOM_NO_ERR.
  82.  */
  83. dom_exception _dom_mouse_event_get_screen_y(dom_mouse_event *evt,
  84.                 int32_t *y)
  85. {
  86.         *y = evt->sy;
  87.  
  88.         return DOM_NO_ERR;
  89. }
  90.  
  91. /**
  92.  * Get clientX
  93.  *
  94.  * \param evt  The Event object
  95.  * \param x    The returned clientX
  96.  * \return DOM_NO_ERR.
  97.  */
  98. dom_exception _dom_mouse_event_get_client_x(dom_mouse_event *evt,
  99.                 int32_t *x)
  100. {
  101.         *x = evt->cx;
  102.  
  103.         return DOM_NO_ERR;
  104. }
  105.  
  106. /**
  107.  * Get clientY
  108.  *
  109.  * \param evt  The Event object
  110.  * \param y    The returned clientY
  111.  * \return DOM_NO_ERR.
  112.  */
  113. dom_exception _dom_mouse_event_get_client_y(dom_mouse_event *evt,
  114.                 int32_t *y)
  115. {
  116.         *y = evt->cy;
  117.  
  118.         return DOM_NO_ERR;
  119. }
  120.  
  121. /**
  122.  * Get the ctrl key state
  123.  *
  124.  * \param evt  The Event object
  125.  * \param key  Whether the Control key is pressed down
  126.  * \return DOM_NO_ERR.
  127.  */
  128. dom_exception _dom_mouse_event_get_ctrl_key(dom_mouse_event *evt,
  129.                 bool *key)
  130. {
  131.         *key = ((evt->modifier_state & DOM_MOD_CTRL) != 0);
  132.  
  133.         return DOM_NO_ERR;
  134. }
  135.  
  136. /**
  137.  * Get the shift key state
  138.  *
  139.  * \param evt  The Event object
  140.  * \param key  Whether the Shift key is pressed down
  141.  * \return DOM_NO_ERR.
  142.  */
  143. dom_exception _dom_mouse_event_get_shift_key(dom_mouse_event *evt,
  144.                 bool *key)
  145. {
  146.         *key = ((evt->modifier_state & DOM_MOD_SHIFT) != 0);
  147.  
  148.         return DOM_NO_ERR;
  149. }
  150.                
  151. /**
  152.  * Get the alt key state
  153.  *
  154.  * \param evt  The Event object
  155.  * \param key  Whether the Alt key is pressed down
  156.  * \return DOM_NO_ERR.
  157.  */
  158. dom_exception _dom_mouse_event_get_alt_key(dom_mouse_event *evt,
  159.                 bool *key)
  160. {
  161.         *key = ((evt->modifier_state & DOM_MOD_ALT) != 0);
  162.  
  163.         return DOM_NO_ERR;
  164. }
  165.  
  166. /**
  167.  * Get the meta key state
  168.  *
  169.  * \param evt  The Event object
  170.  * \param key  Whether the Meta key is pressed down
  171.  * \return DOM_NO_ERR.
  172.  */
  173. dom_exception _dom_mouse_event_get_meta_key(dom_mouse_event *evt,
  174.                 bool *key)
  175. {
  176.         *key = ((evt->modifier_state & DOM_MOD_META) != 0);
  177.  
  178.         return DOM_NO_ERR;
  179. }
  180.  
  181. /**
  182.  * Get the button which get pressed
  183.  *
  184.  * \param evt     The Event object
  185.  * \param button  The pressed mouse button
  186.  * \return DOM_NO_ERR.
  187.  */
  188. dom_exception _dom_mouse_event_get_button(dom_mouse_event *evt,
  189.                 unsigned short *button)
  190. {
  191.         *button = evt->button;
  192.  
  193.         return DOM_NO_ERR;
  194. }
  195.  
  196. /**
  197.  * Get the related target
  198.  *
  199.  * \param evt  The Event object
  200.  * \param et   The related EventTarget
  201.  * \return DOM_NO_ERR.
  202.  */
  203. dom_exception _dom_mouse_event_get_related_target(dom_mouse_event *evt,
  204.                 dom_event_target **et)
  205. {
  206.         *et = evt->related_target;
  207.  
  208.         return DOM_NO_ERR;
  209. }
  210.  
  211. /**
  212.  * Query the state of a modifier using a key identifier
  213.  *
  214.  * \param evt    The event object
  215.  * \param ml     The modifier identifier, such as "Alt", "Control", "Meta",
  216.  *               "AltGraph", "CapsLock", "NumLock", "Scroll", "Shift".
  217.  * \param state  Whether the modifier key is pressed
  218.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  219.  *
  220.  * @note: If an application wishes to distinguish between right and left
  221.  * modifiers, this information could be deduced using keyboard events and
  222.  * KeyboardEvent.keyLocation.
  223.  */
  224. dom_exception _dom_mouse_event_get_modifier_state(dom_mouse_event *evt,
  225.                 dom_string *m, bool *state)
  226. {
  227.         const char *data;
  228.         size_t len;
  229.  
  230.         if (m == NULL) {
  231.                 *state = false;
  232.                 return DOM_NO_ERR;
  233.         }
  234.  
  235.         data = dom_string_data(m);
  236.         len = dom_string_byte_length(m);
  237.  
  238.         if (len == SLEN("AltGraph") && strncmp(data, "AltGraph", len) == 0) {
  239.                 *state = ((evt->modifier_state & DOM_MOD_ALT_GRAPH) != 0);
  240.         } else if (len == SLEN("Alt") && strncmp(data, "Alt", len) == 0) {
  241.                 *state = ((evt->modifier_state & DOM_MOD_ALT) != 0);
  242.         } else if (len == SLEN("CapsLock") &&
  243.                         strncmp(data, "CapsLock", len) == 0) {
  244.                 *state = ((evt->modifier_state & DOM_MOD_CAPS_LOCK) != 0);
  245.         } else if (len == SLEN("Control") &&
  246.                         strncmp(data, "Control", len) == 0) {
  247.                 *state = ((evt->modifier_state & DOM_MOD_CTRL) != 0);
  248.         } else if (len == SLEN("Meta") && strncmp(data, "Meta", len) == 0) {
  249.                 *state = ((evt->modifier_state & DOM_MOD_META) != 0);
  250.         } else if (len == SLEN("NumLock") &&
  251.                         strncmp(data, "NumLock", len) == 0) {
  252.                 *state = ((evt->modifier_state & DOM_MOD_NUM_LOCK) != 0);
  253.         } else if (len == SLEN("Scroll") &&
  254.                         strncmp(data, "Scroll", len) == 0) {
  255.                 *state = ((evt->modifier_state & DOM_MOD_SCROLL) != 0);
  256.         } else if (len == SLEN("Shift") && strncmp(data, "Shift", len) == 0) {
  257.                 *state = ((evt->modifier_state & DOM_MOD_SHIFT) != 0);
  258.         }
  259.  
  260.         return DOM_NO_ERR;
  261. }
  262.  
  263. /**
  264.  * Initialise this mouse event
  265.  *
  266.  * \param evt         The Event object
  267.  * \param type        The event's type
  268.  * \param bubble      Whether this is a bubbling event
  269.  * \param cancelable  Whether this is a cancelable event
  270.  * \param view        The AbstractView associated with this event
  271.  * \param detail      The detail information of this mouse event
  272.  * \param screen_x    The x position of the mouse pointer in screen
  273.  * \param screen_y    The y position of the mouse pointer in screen
  274.  * \param client_x    The x position of the mouse pointer in client window
  275.  * \param client_y    The y position of the mouse pointer in client window
  276.  * \param alt         The state of Alt key, true for pressed, false otherwise
  277.  * \param shift       The state of Shift key, true for pressed, false otherwise
  278.  * \param mata        The state of Meta key, true for pressed, false otherwise
  279.  * \param button      The mouse button pressed
  280.  * \param et          The related target of this event, may be NULL
  281.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  282.  */
  283. dom_exception _dom_mouse_event_init(dom_mouse_event *evt,
  284.                 dom_string *type, bool bubble, bool cancelable,
  285.                 struct dom_abstract_view *view, int32_t detail, int32_t screen_x,
  286.                 int32_t screen_y, int32_t client_x, int32_t client_y, bool ctrl,
  287.                 bool alt, bool shift, bool meta, unsigned short button,
  288.                 dom_event_target *et)
  289. {
  290.         evt->sx = screen_x;
  291.         evt->sy = screen_y;
  292.         evt->cx = client_x;
  293.         evt->cy = client_y;
  294.  
  295.         evt->modifier_state = 0;
  296.         if (ctrl == true) {
  297.                 evt->modifier_state = evt->modifier_state | DOM_MOD_CTRL;
  298.         }
  299.         if (alt == true) {
  300.                 evt->modifier_state = evt->modifier_state | DOM_MOD_ALT;
  301.         }
  302.         if (shift == true) {
  303.                 evt->modifier_state = evt->modifier_state | DOM_MOD_SHIFT;
  304.         }
  305.         if (meta == true) {
  306.                 evt->modifier_state = evt->modifier_state | DOM_MOD_META;
  307.         }
  308.        
  309.         evt->button = button;
  310.         evt->related_target = et;
  311.  
  312.         return _dom_ui_event_init(&evt->base, type, bubble, cancelable, view,
  313.                         detail);
  314. }
  315.  
  316. /**
  317.  * Initialise the event with namespace
  318.  *
  319.  * \param evt         The Event object
  320.  * \param namespace   The namespace of this event
  321.  * \param type        The event's type
  322.  * \param bubble      Whether this is a bubbling event
  323.  * \param cancelable  Whether this is a cancelable event
  324.  * \param view        The AbstractView associated with this event
  325.  * \param detail      The detail information of this mouse event
  326.  * \param screen_x    The x position of the mouse pointer in screen
  327.  * \param screen_y    The y position of the mouse pointer in screen
  328.  * \param client_x    The x position of the mouse pointer in client window
  329.  * \param client_y    The y position of the mouse pointer in client window
  330.  * \param alt         The state of Alt key, true for pressed, false otherwise
  331.  * \param shift       The state of Shift key, true for pressed, false otherwise
  332.  * \param mata        The state of Meta key, true for pressed, false otherwise
  333.  * \param button      The mouse button pressed
  334.  * \param et          The related target of this event, may be NULL
  335.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  336.  */
  337. dom_exception _dom_mouse_event_init_ns(dom_mouse_event *evt,
  338.                 dom_string *namespace, dom_string *type,
  339.                 bool bubble, bool cancelable, struct dom_abstract_view *view,
  340.                 int32_t detail, int32_t screen_x, int32_t screen_y, int32_t client_x,
  341.                 int32_t client_y, bool ctrl, bool alt, bool shift, bool meta,
  342.                 unsigned short button, dom_event_target *et)
  343. {
  344.         evt->sx = screen_x;
  345.         evt->sy = screen_y;
  346.         evt->cx = client_x;
  347.         evt->cy = client_y;
  348.  
  349.         evt->modifier_state = 0;
  350.         if (ctrl == true) {
  351.                 evt->modifier_state = evt->modifier_state | DOM_MOD_CTRL;
  352.         }
  353.         if (alt == true) {
  354.                 evt->modifier_state = evt->modifier_state | DOM_MOD_ALT;
  355.         }
  356.         if (shift == true) {
  357.                 evt->modifier_state = evt->modifier_state | DOM_MOD_SHIFT;
  358.         }
  359.         if (meta == true) {
  360.                 evt->modifier_state = evt->modifier_state | DOM_MOD_META;
  361.         }
  362.        
  363.         evt->button = button;
  364.         evt->related_target = et;
  365.  
  366.         return _dom_ui_event_init_ns(&evt->base, namespace, type, bubble,
  367.                         cancelable, view, detail);
  368. }
  369.  
  370.