1/*
2* Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions
7* are met:
8* 1. Redistributions of source code must retain the above copyright
9* notice, this list of conditions and the following disclaimer.
10* 2. Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13*
14* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
26
27#include "config.h"
28#include "PlatformMouseEvent.h"
29
30#include "GtkUtilities.h"
31#include "PlatformKeyboardEvent.h"
32#include <gdk/gdk.h>
33#include <wtf/Assertions.h>
34
35namespace WebCore {
36
37// FIXME: Would be even better to figure out which modifier is Alt instead of always using GDK_MOD1_MASK.
38
39// Keep this in sync with the other platform event constructors
40PlatformMouseEvent::PlatformMouseEvent(GdkEventButton* event)
41{
42 m_timestamp = wallTimeForEvent(event);
43 m_position = IntPoint((int)event->x, (int)event->y);
44 m_globalPosition = IntPoint((int)event->x_root, (int)event->y_root);
45 m_button = NoButton;
46 m_clickCount = 0;
47 m_modifierFlags = 0;
48
49 if (event->state & GDK_SHIFT_MASK)
50 m_modifiers.add(PlatformEvent::Modifier::ShiftKey);
51 if (event->state & GDK_CONTROL_MASK)
52 m_modifiers.add(PlatformEvent::Modifier::ControlKey);
53 if (event->state & GDK_MOD1_MASK)
54 m_modifiers.add(PlatformEvent::Modifier::AltKey);
55 if (event->state & GDK_META_MASK)
56 m_modifiers.add(PlatformEvent::Modifier::MetaKey);
57 if (PlatformKeyboardEvent::modifiersContainCapsLock(event->state))
58 m_modifiers.add(PlatformEvent::Modifier::CapsLockKey);
59
60 switch (event->type) {
61 case GDK_BUTTON_PRESS:
62 case GDK_2BUTTON_PRESS:
63 case GDK_3BUTTON_PRESS:
64 case GDK_BUTTON_RELEASE:
65 m_type = PlatformEvent::MousePressed;
66 if (event->type == GDK_BUTTON_RELEASE) {
67 m_type = PlatformEvent::MouseReleased;
68 m_clickCount = 0;
69 } else if (event->type == GDK_BUTTON_PRESS)
70 m_clickCount = 1;
71 else if (event->type == GDK_2BUTTON_PRESS)
72 m_clickCount = 2;
73 else if (event->type == GDK_3BUTTON_PRESS)
74 m_clickCount = 3;
75
76 if (event->button == 1)
77 m_button = LeftButton;
78 else if (event->button == 2)
79 m_button = MiddleButton;
80 else if (event->button == 3)
81 m_button = RightButton;
82 break;
83
84 default:
85 ASSERT_NOT_REACHED();
86 };
87}
88
89PlatformMouseEvent::PlatformMouseEvent(GdkEventMotion* motion)
90{
91 m_timestamp = wallTimeForEvent(motion);
92 m_position = IntPoint((int)motion->x, (int)motion->y);
93 m_globalPosition = IntPoint((int)motion->x_root, (int)motion->y_root);
94 m_button = NoButton;
95 m_clickCount = 0;
96 m_modifierFlags = 0;
97
98 if (motion->state & GDK_SHIFT_MASK)
99 m_modifiers.add(PlatformEvent::Modifier::ShiftKey);
100 if (motion->state & GDK_CONTROL_MASK)
101 m_modifiers.add(PlatformEvent::Modifier::ControlKey);
102 if (motion->state & GDK_MOD1_MASK)
103 m_modifiers.add(PlatformEvent::Modifier::AltKey);
104 if (motion->state & GDK_META_MASK)
105 m_modifiers.add(PlatformEvent::Modifier::MetaKey);
106 if (PlatformKeyboardEvent::modifiersContainCapsLock(motion->state))
107 m_modifiers.add(PlatformEvent::Modifier::CapsLockKey);
108
109 switch (motion->type) {
110 case GDK_MOTION_NOTIFY:
111 m_type = PlatformEvent::MouseMoved;
112 m_button = NoButton;
113 m_clickCount = 0;
114 break;
115 default:
116 ASSERT_NOT_REACHED();
117 };
118
119 if (motion->state & GDK_BUTTON1_MASK)
120 m_button = LeftButton;
121 else if (motion->state & GDK_BUTTON2_MASK)
122 m_button = MiddleButton;
123 else if (motion->state & GDK_BUTTON3_MASK)
124 m_button = RightButton;
125}
126}
127