1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * eventargs.cpp: specialized code for dealing with mouse/stylus/keyboard event args.
5 * Copyright 2007 Novell, Inc. (http://www.novell.com)
7 * See the LICENSE file included with the distribution for details.
13 #include <gdk/gdkkeysyms.h>
15 #include "eventargs.h"
16 #include "uielement.h"
17 #include "collection.h"
21 EventArgs::EventArgs ()
22 : DependencyObject (Type::EVENTARGS
)
26 EventArgs::EventArgs (Type::Kind kind
)
27 : DependencyObject (kind
)
31 EventArgs::~EventArgs ()
35 RenderingEventArgs::RenderingEventArgs (TimeSpan renderingTime
)
36 : EventArgs (Type::RENDERINGEVENTARGS
)
39 this->renderingTime
= renderingTime
;
42 RenderingEventArgs::~RenderingEventArgs ()
47 RenderingEventArgs::GetRenderingTime ()
54 CollectionChangedEventArgs::CollectionChangedEventArgs ()
55 : EventArgs (Type::COLLECTIONCHANGEDEVENTARGS
)
57 action
= CollectionChangedActionAdd
;
63 CollectionChangedEventArgs::CollectionChangedEventArgs (CollectionChangedAction action
, Value
*new_item
, Value
*old_item
, int index
)
64 : EventArgs (Type::COLLECTIONCHANGEDEVENTARGS
)
66 this->action
= action
;
67 this->new_item
= new_item
;
68 this->old_item
= old_item
;
72 CollectionChangedEventArgs::~CollectionChangedEventArgs ()
77 CollectionChangedEventArgs::SetChangedAction (CollectionChangedAction action
)
79 this->action
= action
;
82 CollectionChangedAction
83 CollectionChangedEventArgs::GetChangedAction ()
89 CollectionChangedEventArgs::SetNewItem (Value
*item
)
95 CollectionChangedEventArgs::GetNewItem ()
101 CollectionChangedEventArgs::SetOldItem (Value
*item
)
107 CollectionChangedEventArgs::GetOldItem ()
113 CollectionChangedEventArgs::SetIndex (int index
)
119 CollectionChangedEventArgs::GetIndex ()
124 DownloadProgressEventArgs::DownloadProgressEventArgs (double progress
)
125 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS
)
127 this->progress
= progress
;
130 DownloadProgressEventArgs::DownloadProgressEventArgs ()
131 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS
)
136 DownloadProgressEventArgs::~DownloadProgressEventArgs ()
141 DownloadProgressEventArgs::SetProgress (double progress
)
143 this->progress
= progress
;
147 DownloadProgressEventArgs::GetProgress ()
152 RoutedEventArgs::RoutedEventArgs (DependencyObject
*source
)
153 : EventArgs (Type::ROUTEDEVENTARGS
)
158 this->source
= source
;
162 RoutedEventArgs::RoutedEventArgs ()
163 : EventArgs (Type::ROUTEDEVENTARGS
)
169 RoutedEventArgs::RoutedEventArgs (Type::Kind kind
)
176 RoutedEventArgs::RoutedEventArgs (DependencyObject
*source
, Type::Kind kind
)
180 this->source
= source
;
184 RoutedEventArgs::~RoutedEventArgs ()
191 RoutedEventArgs::SetHandled (bool handled
)
193 this->handled
= handled
;
197 RoutedEventArgs::GetHandled ()
203 RoutedEventArgs::GetSource ()
209 RoutedEventArgs::SetSource (DependencyObject
*el
)
218 MouseEventArgs::MouseEventArgs (GdkEvent
*event
)
219 : RoutedEventArgs (Type::MOUSEEVENTARGS
)
221 this->event
= gdk_event_copy (event
);
224 MouseEventArgs::MouseEventArgs (Type::Kind kind
, GdkEvent
*event
)
225 : RoutedEventArgs (kind
)
227 this->event
= gdk_event_copy (event
);
230 MouseEventArgs::MouseEventArgs ()
231 : RoutedEventArgs (Type::MOUSEEVENTARGS
)
233 event
= gdk_event_new (GDK_MOTION_NOTIFY
);
236 MouseEventArgs::~MouseEventArgs ()
238 gdk_event_free (event
);
242 MouseEventArgs::GetState ()
244 GdkModifierType state
;
245 gdk_event_get_state (event
, &state
);
250 MouseEventArgs::GetPosition (UIElement
*relative_to
, double *x
, double *y
)
253 if (gdk_event_get_coords (event
, x
, y
)) {
255 // FIXME this a nasty place to do this we should be able to
256 // reduce the problem for this kind of hit testing.
257 if (relative_to
->GetSurface())
258 relative_to
->GetSurface()->ProcessDirtyElements ();
261 relative_to
->TransformPoint (x
, y
);
267 MouseEventArgs::GetStylusInfo ()
269 TabletDeviceType type
= TabletDeviceTypeMouse
;
270 bool is_inverted
= false;
271 GdkDevice
*gdk_device
;
273 switch (event
->type
) {
274 case GDK_MOTION_NOTIFY
:
275 gdk_device
= ((GdkEventMotion
*)event
)->device
;
277 case GDK_BUTTON_PRESS
:
278 case GDK_BUTTON_RELEASE
:
279 gdk_device
= ((GdkEventButton
*)event
)->device
;
283 case GDK_ENTER_NOTIFY
:
284 case GDK_LEAVE_NOTIFY
:
285 /* GdkEventCrossing doesn't have a device field. ugh */
291 switch (gdk_device
->source
) {
293 case GDK_SOURCE_ERASER
:
294 type
= TabletDeviceTypeStylus
;
296 case GDK_SOURCE_MOUSE
:
297 case GDK_SOURCE_CURSOR
: /* XXX not sure where to lump this in.. in the stylus block? */
299 type
= TabletDeviceTypeMouse
;
303 is_inverted
= (gdk_device
->source
== GDK_SOURCE_ERASER
);
306 StylusInfo
*info
= new StylusInfo ();
308 info
->SetValue (StylusInfo::DeviceTypeProperty
, Value (type
));
309 info
->SetValue (StylusInfo::IsInvertedProperty
, Value (is_inverted
));
314 StylusPointCollection
*
315 MouseEventArgs::GetStylusPoints (UIElement
*ink_presenter
)
317 StylusPointCollection
*points
= new StylusPointCollection ();
321 GetPosition (ink_presenter
, &x
, &y
);
322 if (!((GdkEventMotion
*) event
)->device
|| !gdk_event_get_axis (event
, GDK_AXIS_PRESSURE
, &pressure
))
325 StylusPoint
*point
= new StylusPoint ();
326 point
->SetValue (StylusPoint::XProperty
, Value(x
));
327 point
->SetValue (StylusPoint::YProperty
, Value(y
));
328 point
->SetValue (StylusPoint::PressureFactorProperty
, Value(pressure
));
337 MouseButtonEventArgs::MouseButtonEventArgs (GdkEvent
*event
)
338 : MouseEventArgs (Type::MOUSEBUTTONEVENTARGS
, event
)
342 MouseButtonEventArgs::MouseButtonEventArgs ()
343 : MouseEventArgs (Type::MOUSEBUTTONEVENTARGS
, NULL
)
345 event
= gdk_event_new (GDK_BUTTON_PRESS
);
349 MouseButtonEventArgs::GetButton ()
351 switch (event
->type
) {
352 case GDK_BUTTON_RELEASE
:
353 case GDK_3BUTTON_PRESS
:
354 case GDK_2BUTTON_PRESS
:
355 case GDK_BUTTON_PRESS
:
356 return ((GdkEventButton
*) event
)->button
;
364 MouseButtonEventArgs::GetClickCount ()
366 switch (event
->type
) {
367 case GDK_3BUTTON_PRESS
:
369 case GDK_2BUTTON_PRESS
:
371 case GDK_BUTTON_PRESS
:
378 MouseButtonEventArgs::~MouseButtonEventArgs ()
382 MouseWheelEventArgs::MouseWheelEventArgs (GdkEvent
*event
)
383 : MouseEventArgs (Type::MOUSEWHEELEVENTARGS
, event
)
387 MouseWheelEventArgs::MouseWheelEventArgs ()
388 : MouseEventArgs (Type::MOUSEWHEELEVENTARGS
, NULL
)
390 event
= gdk_event_new (GDK_SCROLL
);
394 MouseWheelEventArgs::~MouseWheelEventArgs ()
398 #define MOON_SCROLL_WHEEL_DELTA 10
401 MouseWheelEventArgs::GetWheelDelta ()
403 /* we only handle UP/DOWN scroll events for the time being */
404 switch (((GdkEventScroll
*)event
)->direction
) {
406 return MOON_SCROLL_WHEEL_DELTA
;
407 case GDK_SCROLL_DOWN
:
408 return -MOON_SCROLL_WHEEL_DELTA
;
411 case GDK_SCROLL_LEFT
:
412 case GDK_SCROLL_RIGHT
:
417 KeyEventArgs::KeyEventArgs (GdkEventKey
*event
)
418 : RoutedEventArgs (Type::KEYEVENTARGS
)
420 this->event
= (GdkEventKey
*) gdk_event_copy ((GdkEvent
*)event
);
423 KeyEventArgs::KeyEventArgs ()
424 : RoutedEventArgs (Type::KEYEVENTARGS
)
426 event
= (GdkEventKey
*) gdk_event_new (GDK_KEY_PRESS
);
429 KeyEventArgs::~KeyEventArgs ()
431 gdk_event_free ((GdkEvent
*) event
);
435 KeyEventArgs::GetEvent ()
441 KeyEventArgs::GetKey ()
443 return Keyboard::MapKeyValToKey (event
->keyval
);
447 KeyEventArgs::GetPlatformKeyCode ()
449 return event
->hardware_keycode
;
453 KeyEventArgs::GetModifiers ()
455 return (GdkModifierType
) event
->state
;
459 KeyEventArgs::IsModifier ()
461 #if GTK_CHECK_VERSION(2,10,0)
462 if (gtk_check_version(2,10,0))
463 return event
->is_modifier
;
466 switch (event
->keyval
) {
486 KeyEventArgs::GetKeyVal ()
488 return event
->keyval
;
492 KeyEventArgs::GetUnicode ()
494 return gdk_keyval_to_unicode (event
->keyval
);
501 ErrorEventArgs::ErrorEventArgs (Type::Kind kind
, ErrorEventArgsType type
, const MoonError error
)
503 Initialize (kind
, type
, error
, 0, NULL
);
506 ErrorEventArgs::ErrorEventArgs (ErrorEventArgsType type
, MoonError error
)
508 Initialize (Type::ERROREVENTARGS
, type
, error
, 0, NULL
);
511 ErrorEventArgs::ErrorEventArgs (ErrorEventArgsType type
, MoonError error
, int extended_error_code
, const char *extended_msg
)
513 Initialize (Type::ERROREVENTARGS
, type
, error
, extended_error_code
, extended_msg
);
517 ErrorEventArgs::Initialize (Type::Kind kind
, ErrorEventArgsType type
, const MoonError
&error
, int extended_error_code
, const char *extended_msg
)
519 SetObjectType (kind
);
521 this->error
= new MoonError (error
);
522 extended_message
= g_strdup (extended_msg
);
523 extended_code
= extended_error_code
;
526 ErrorEventArgs::~ErrorEventArgs ()
529 g_free (extended_message
);
534 // ImageErrorEventArgs
537 ImageErrorEventArgs::ImageErrorEventArgs (MoonError error
)
538 : ErrorEventArgs (Type::IMAGEERROREVENTARGS
, ImageError
, error
)
542 ImageErrorEventArgs::~ImageErrorEventArgs ()
549 // ParserErrorEventArgs
552 ParserErrorEventArgs::ParserErrorEventArgs (const char *msg
, const char *file
,
553 int line
, int column
, int error_code
,
554 const char *element
, const char *attribute
)
555 : ErrorEventArgs (Type::PARSERERROREVENTARGS
, ParserError
, MoonError (MoonError::XAML_PARSE_EXCEPTION
, error_code
, msg
))
557 xml_attribute
= g_strdup (attribute
);
558 xml_element
= g_strdup (element
);
559 xaml_file
= g_strdup (file
);
560 char_position
= column
;
564 ParserErrorEventArgs::~ParserErrorEventArgs ()
567 g_free (xml_element
);
568 g_free (xml_attribute
);