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 #include "deployment.h"
23 EventArgs::EventArgs ()
24 : DependencyObject (Type::EVENTARGS
)
28 EventArgs::EventArgs (Type::Kind kind
)
29 : DependencyObject (kind
)
33 EventArgs::~EventArgs ()
37 RenderingEventArgs::RenderingEventArgs (TimeSpan renderingTime
)
38 : EventArgs (Type::RENDERINGEVENTARGS
)
41 this->renderingTime
= renderingTime
;
44 RenderingEventArgs::~RenderingEventArgs ()
49 RenderingEventArgs::GetRenderingTime ()
56 CollectionChangedEventArgs::CollectionChangedEventArgs ()
57 : EventArgs (Type::COLLECTIONCHANGEDEVENTARGS
)
59 action
= CollectionChangedActionAdd
;
65 CollectionChangedEventArgs::CollectionChangedEventArgs (CollectionChangedAction action
, Value
*new_item
, Value
*old_item
, int index
)
66 : EventArgs (Type::COLLECTIONCHANGEDEVENTARGS
)
68 this->action
= action
;
69 this->new_item
= new_item
;
70 this->old_item
= old_item
;
74 CollectionChangedEventArgs::~CollectionChangedEventArgs ()
79 CollectionChangedEventArgs::SetChangedAction (CollectionChangedAction action
)
81 this->action
= action
;
84 CollectionChangedAction
85 CollectionChangedEventArgs::GetChangedAction ()
91 CollectionChangedEventArgs::SetNewItem (Value
*item
)
97 CollectionChangedEventArgs::GetNewItem ()
103 CollectionChangedEventArgs::SetOldItem (Value
*item
)
109 CollectionChangedEventArgs::GetOldItem ()
115 CollectionChangedEventArgs::SetIndex (int index
)
121 CollectionChangedEventArgs::GetIndex ()
126 DownloadProgressEventArgs::DownloadProgressEventArgs (double progress
)
127 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS
)
129 this->progress
= progress
;
132 DownloadProgressEventArgs::DownloadProgressEventArgs ()
133 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS
)
138 DownloadProgressEventArgs::~DownloadProgressEventArgs ()
143 DownloadProgressEventArgs::SetProgress (double progress
)
145 this->progress
= progress
;
149 DownloadProgressEventArgs::GetProgress ()
154 RoutedEventArgs::RoutedEventArgs (DependencyObject
*source
)
155 : EventArgs (Type::ROUTEDEVENTARGS
)
160 this->source
= source
;
164 RoutedEventArgs::RoutedEventArgs ()
165 : EventArgs (Type::ROUTEDEVENTARGS
)
171 RoutedEventArgs::RoutedEventArgs (Type::Kind kind
)
178 RoutedEventArgs::RoutedEventArgs (DependencyObject
*source
, Type::Kind kind
)
182 this->source
= source
;
186 RoutedEventArgs::~RoutedEventArgs ()
193 RoutedEventArgs::SetHandled (bool handled
)
195 this->handled
= handled
;
199 RoutedEventArgs::GetHandled ()
205 RoutedEventArgs::GetSource ()
211 RoutedEventArgs::SetSource (DependencyObject
*el
)
220 LogReadyRoutedEventArgs::LogReadyRoutedEventArgs ()
221 : RoutedEventArgs (Type::LOGREADYROUTEDEVENTARGS
)
224 log_source
= (LogSource
) 0;
227 MouseEventArgs::MouseEventArgs (GdkEvent
*event
)
228 : RoutedEventArgs (Type::MOUSEEVENTARGS
)
230 this->event
= gdk_event_copy (event
);
233 MouseEventArgs::MouseEventArgs (Type::Kind kind
, GdkEvent
*event
)
234 : RoutedEventArgs (kind
)
236 this->event
= gdk_event_copy (event
);
239 MouseEventArgs::MouseEventArgs ()
240 : RoutedEventArgs (Type::MOUSEEVENTARGS
)
242 event
= gdk_event_new (GDK_MOTION_NOTIFY
);
245 MouseEventArgs::~MouseEventArgs ()
247 gdk_event_free (event
);
251 MouseEventArgs::GetState ()
253 GdkModifierType state
;
254 gdk_event_get_state (event
, &state
);
259 MouseEventArgs::GetPosition (UIElement
*relative_to
, double *x
, double *y
)
262 if (gdk_event_get_coords (event
, x
, y
)) {
264 // FIXME this a nasty place to do this we should be able to
265 // reduce the problem for this kind of hit testing.
266 if (relative_to
->IsAttached ())
267 relative_to
->GetDeployment ()->GetSurface ()->ProcessDirtyElements ();
270 relative_to
->TransformPoint (x
, y
);
276 MouseEventArgs::GetStylusInfo ()
278 TabletDeviceType type
= TabletDeviceTypeMouse
;
279 bool is_inverted
= false;
280 GdkDevice
*gdk_device
;
282 switch (event
->type
) {
283 case GDK_MOTION_NOTIFY
:
284 gdk_device
= ((GdkEventMotion
*)event
)->device
;
286 case GDK_BUTTON_PRESS
:
287 case GDK_BUTTON_RELEASE
:
288 gdk_device
= ((GdkEventButton
*)event
)->device
;
292 case GDK_ENTER_NOTIFY
:
293 case GDK_LEAVE_NOTIFY
:
294 /* GdkEventCrossing doesn't have a device field. ugh */
300 switch (gdk_device
->source
) {
302 case GDK_SOURCE_ERASER
:
303 type
= TabletDeviceTypeStylus
;
305 case GDK_SOURCE_MOUSE
:
306 case GDK_SOURCE_CURSOR
: /* XXX not sure where to lump this in.. in the stylus block? */
308 type
= TabletDeviceTypeMouse
;
312 is_inverted
= (gdk_device
->source
== GDK_SOURCE_ERASER
);
315 StylusInfo
*info
= new StylusInfo ();
317 info
->SetValue (StylusInfo::DeviceTypeProperty
, Value (type
));
318 info
->SetValue (StylusInfo::IsInvertedProperty
, Value (is_inverted
));
323 StylusPointCollection
*
324 MouseEventArgs::GetStylusPoints (UIElement
*ink_presenter
)
326 StylusPointCollection
*points
= new StylusPointCollection ();
330 GetPosition (ink_presenter
, &x
, &y
);
331 if (!((GdkEventMotion
*) event
)->device
|| !gdk_event_get_axis (event
, GDK_AXIS_PRESSURE
, &pressure
))
334 StylusPoint
*point
= new StylusPoint ();
335 point
->SetValue (StylusPoint::XProperty
, Value(x
));
336 point
->SetValue (StylusPoint::YProperty
, Value(y
));
337 point
->SetValue (StylusPoint::PressureFactorProperty
, Value(pressure
));
346 MouseButtonEventArgs::MouseButtonEventArgs (GdkEvent
*event
)
347 : MouseEventArgs (Type::MOUSEBUTTONEVENTARGS
, event
)
351 MouseButtonEventArgs::MouseButtonEventArgs ()
352 : MouseEventArgs (Type::MOUSEBUTTONEVENTARGS
, NULL
)
354 event
= gdk_event_new (GDK_BUTTON_PRESS
);
358 MouseButtonEventArgs::GetButton ()
360 switch (event
->type
) {
361 case GDK_BUTTON_RELEASE
:
362 case GDK_3BUTTON_PRESS
:
363 case GDK_2BUTTON_PRESS
:
364 case GDK_BUTTON_PRESS
:
365 return ((GdkEventButton
*) event
)->button
;
373 MouseButtonEventArgs::GetClickCount ()
375 switch (event
->type
) {
376 case GDK_3BUTTON_PRESS
:
378 case GDK_2BUTTON_PRESS
:
380 case GDK_BUTTON_PRESS
:
387 MouseButtonEventArgs::~MouseButtonEventArgs ()
391 MouseWheelEventArgs::MouseWheelEventArgs (GdkEvent
*event
)
392 : MouseEventArgs (Type::MOUSEWHEELEVENTARGS
, event
)
396 MouseWheelEventArgs::MouseWheelEventArgs ()
397 : MouseEventArgs (Type::MOUSEWHEELEVENTARGS
, NULL
)
399 event
= gdk_event_new (GDK_SCROLL
);
403 MouseWheelEventArgs::~MouseWheelEventArgs ()
407 #define MOON_SCROLL_WHEEL_DELTA 10
410 MouseWheelEventArgs::GetWheelDelta ()
412 /* we only handle UP/DOWN scroll events for the time being */
413 switch (((GdkEventScroll
*)event
)->direction
) {
415 return MOON_SCROLL_WHEEL_DELTA
;
416 case GDK_SCROLL_DOWN
:
417 return -MOON_SCROLL_WHEEL_DELTA
;
420 case GDK_SCROLL_LEFT
:
421 case GDK_SCROLL_RIGHT
:
426 KeyEventArgs::KeyEventArgs (GdkEventKey
*event
)
427 : RoutedEventArgs (Type::KEYEVENTARGS
)
429 this->event
= (GdkEventKey
*) gdk_event_copy ((GdkEvent
*)event
);
432 KeyEventArgs::KeyEventArgs ()
433 : RoutedEventArgs (Type::KEYEVENTARGS
)
435 event
= (GdkEventKey
*) gdk_event_new (GDK_KEY_PRESS
);
438 KeyEventArgs::~KeyEventArgs ()
440 gdk_event_free ((GdkEvent
*) event
);
444 KeyEventArgs::GetEvent ()
450 KeyEventArgs::GetKey ()
452 return Keyboard::MapKeyValToKey (event
->keyval
);
456 KeyEventArgs::GetPlatformKeyCode ()
458 return event
->hardware_keycode
;
462 KeyEventArgs::GetModifiers ()
464 return (GdkModifierType
) event
->state
;
468 KeyEventArgs::IsModifier ()
470 #if GTK_CHECK_VERSION(2,10,0)
471 if (gtk_check_version(2,10,0))
472 return event
->is_modifier
;
475 switch (event
->keyval
) {
495 KeyEventArgs::GetKeyVal ()
497 return event
->keyval
;
501 KeyEventArgs::GetUnicode ()
503 return gdk_keyval_to_unicode (event
->keyval
);
510 ErrorEventArgs::ErrorEventArgs (Type::Kind kind
, ErrorEventArgsType type
, const MoonError error
)
512 Initialize (kind
, type
, error
, 0, NULL
);
515 ErrorEventArgs::ErrorEventArgs (ErrorEventArgsType type
, MoonError error
)
517 Initialize (Type::ERROREVENTARGS
, type
, error
, 0, NULL
);
520 ErrorEventArgs::ErrorEventArgs (ErrorEventArgsType type
, MoonError error
, int extended_error_code
, const char *extended_msg
)
522 Initialize (Type::ERROREVENTARGS
, type
, error
, extended_error_code
, extended_msg
);
526 ErrorEventArgs::Initialize (Type::Kind kind
, ErrorEventArgsType type
, const MoonError
&error
, int extended_error_code
, const char *extended_msg
)
528 SetObjectType (kind
);
530 this->error
= new MoonError (error
);
531 extended_message
= g_strdup (extended_msg
);
532 extended_code
= extended_error_code
;
535 ErrorEventArgs::~ErrorEventArgs ()
538 g_free (extended_message
);
543 // ImageErrorEventArgs
546 ImageErrorEventArgs::ImageErrorEventArgs (MoonError error
)
547 : ErrorEventArgs (Type::IMAGEERROREVENTARGS
, ImageError
, error
)
551 ImageErrorEventArgs::~ImageErrorEventArgs ()
558 // ParserErrorEventArgs
561 ParserErrorEventArgs::ParserErrorEventArgs (const char *msg
, const char *file
,
562 int line
, int column
, int error_code
,
563 const char *element
, const char *attribute
)
564 : ErrorEventArgs (Type::PARSERERROREVENTARGS
, ParserError
, MoonError (MoonError::XAML_PARSE_EXCEPTION
, error_code
, msg
))
566 xml_attribute
= g_strdup (attribute
);
567 xml_element
= g_strdup (element
);
568 xaml_file
= g_strdup (file
);
569 char_position
= column
;
573 ParserErrorEventArgs::~ParserErrorEventArgs ()
576 g_free (xml_element
);
577 g_free (xml_attribute
);
581 // TimelineMarkerRoutedEventArgs
584 TimelineMarkerRoutedEventArgs::TimelineMarkerRoutedEventArgs (TimelineMarker
*marker
)
585 : RoutedEventArgs (Type::TIMELINEMARKERROUTEDEVENTARGS
)
587 this->marker
= marker
;
592 TimelineMarkerRoutedEventArgs::~TimelineMarkerRoutedEventArgs ()