2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / eventargs.cpp
blob186755f3d34cc06b301ffb9b5bffc33abe2edbb5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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.
9 */
11 #include <config.h>
13 #include <gdk/gdkkeysyms.h>
15 #include "eventargs.h"
16 #include "uielement.h"
17 #include "collection.h"
18 #include "stylus.h"
19 #include "runtime.h"
20 #include "timeline.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 ()
48 TimeSpan
49 RenderingEventArgs::GetRenderingTime ()
51 return renderingTime;
56 CollectionChangedEventArgs::CollectionChangedEventArgs ()
57 : EventArgs (Type::COLLECTIONCHANGEDEVENTARGS)
59 action = CollectionChangedActionAdd;
60 old_item = NULL;
61 new_item = NULL;
62 index = -1;
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;
71 this->index = index;
74 CollectionChangedEventArgs::~CollectionChangedEventArgs ()
78 void
79 CollectionChangedEventArgs::SetChangedAction (CollectionChangedAction action)
81 this->action = action;
84 CollectionChangedAction
85 CollectionChangedEventArgs::GetChangedAction ()
87 return action;
90 void
91 CollectionChangedEventArgs::SetNewItem (Value *item)
93 new_item = item;
96 Value *
97 CollectionChangedEventArgs::GetNewItem ()
99 return new_item;
102 void
103 CollectionChangedEventArgs::SetOldItem (Value *item)
105 old_item = item;
108 Value *
109 CollectionChangedEventArgs::GetOldItem ()
111 return old_item;
114 void
115 CollectionChangedEventArgs::SetIndex (int index)
117 this->index = index;
121 CollectionChangedEventArgs::GetIndex ()
123 return index;
126 DownloadProgressEventArgs::DownloadProgressEventArgs (double progress)
127 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS)
129 this->progress = progress;
132 DownloadProgressEventArgs::DownloadProgressEventArgs ()
133 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS)
135 progress = 0.0;
138 DownloadProgressEventArgs::~DownloadProgressEventArgs ()
142 void
143 DownloadProgressEventArgs::SetProgress (double progress)
145 this->progress = progress;
148 double
149 DownloadProgressEventArgs::GetProgress ()
151 return progress;
154 RoutedEventArgs::RoutedEventArgs (DependencyObject *source)
155 : EventArgs (Type::ROUTEDEVENTARGS)
157 if (source)
158 source->ref ();
160 this->source = source;
161 handled = false;
164 RoutedEventArgs::RoutedEventArgs ()
165 : EventArgs (Type::ROUTEDEVENTARGS)
167 source = NULL;
168 handled = false;
171 RoutedEventArgs::RoutedEventArgs (Type::Kind kind)
172 : EventArgs (kind)
174 source = NULL;
175 handled = false;
178 RoutedEventArgs::RoutedEventArgs (DependencyObject *source, Type::Kind kind)
180 if (source)
181 source->ref ();
182 this->source = source;
183 handled = false;
186 RoutedEventArgs::~RoutedEventArgs ()
188 if (source)
189 source->unref ();
192 void
193 RoutedEventArgs::SetHandled (bool handled)
195 this->handled = handled;
198 bool
199 RoutedEventArgs::GetHandled ()
201 return handled;
204 DependencyObject *
205 RoutedEventArgs::GetSource ()
207 return source;
210 void
211 RoutedEventArgs::SetSource (DependencyObject *el)
213 if (source)
214 source->unref();
215 source = el;
216 if (source)
217 source->ref();
220 LogReadyRoutedEventArgs::LogReadyRoutedEventArgs ()
221 : RoutedEventArgs (Type::LOGREADYROUTEDEVENTARGS)
223 log = NULL;
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);
255 return (int)state;
258 void
259 MouseEventArgs::GetPosition (UIElement *relative_to, double *x, double *y)
261 *x = *y = 0.0;
262 if (gdk_event_get_coords (event, x, y)) {
263 if (relative_to) {
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);
275 StylusInfo*
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;
285 break;
286 case GDK_BUTTON_PRESS:
287 case GDK_BUTTON_RELEASE:
288 gdk_device = ((GdkEventButton*)event)->device;
289 break;
291 default:
292 case GDK_ENTER_NOTIFY:
293 case GDK_LEAVE_NOTIFY:
294 /* GdkEventCrossing doesn't have a device field. ugh */
295 gdk_device = NULL;
296 break;
299 if (gdk_device) {
300 switch (gdk_device->source) {
301 case GDK_SOURCE_PEN:
302 case GDK_SOURCE_ERASER:
303 type = TabletDeviceTypeStylus;
304 break;
305 case GDK_SOURCE_MOUSE:
306 case GDK_SOURCE_CURSOR: /* XXX not sure where to lump this in.. in the stylus block? */
307 default:
308 type = TabletDeviceTypeMouse;
309 break;
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));
320 return info;
323 StylusPointCollection*
324 MouseEventArgs::GetStylusPoints (UIElement *ink_presenter)
326 StylusPointCollection *points = new StylusPointCollection ();
327 double pressure;
328 double x, y;
330 GetPosition (ink_presenter, &x, &y);
331 if (!((GdkEventMotion *) event)->device || !gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &pressure))
332 pressure = 0.0;
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));
339 points->Add (point);
341 point->unref ();
343 return points;
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;
366 break;
367 default:
368 return 0;
373 MouseButtonEventArgs::GetClickCount ()
375 switch (event->type) {
376 case GDK_3BUTTON_PRESS:
377 return 3;
378 case GDK_2BUTTON_PRESS:
379 return 2;
380 case GDK_BUTTON_PRESS:
381 return 1;
382 default:
383 return 0;
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) {
414 case GDK_SCROLL_UP:
415 return MOON_SCROLL_WHEEL_DELTA;
416 case GDK_SCROLL_DOWN:
417 return -MOON_SCROLL_WHEEL_DELTA;
419 default:
420 case GDK_SCROLL_LEFT:
421 case GDK_SCROLL_RIGHT:
422 return 0;
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);
443 GdkEventKey *
444 KeyEventArgs::GetEvent ()
446 return event;
450 KeyEventArgs::GetKey ()
452 return Keyboard::MapKeyValToKey (event->keyval);
456 KeyEventArgs::GetPlatformKeyCode ()
458 return event->hardware_keycode;
461 GdkModifierType
462 KeyEventArgs::GetModifiers ()
464 return (GdkModifierType) event->state;
467 bool
468 KeyEventArgs::IsModifier ()
470 #if GTK_CHECK_VERSION(2,10,0)
471 if (gtk_check_version(2,10,0))
472 return event->is_modifier;
473 else
474 #endif
475 switch (event->keyval) {
476 case GDK_Shift_L:
477 case GDK_Shift_R:
478 case GDK_Control_L:
479 case GDK_Control_R:
480 case GDK_Meta_L:
481 case GDK_Meta_R:
482 case GDK_Alt_L:
483 case GDK_Alt_R:
484 case GDK_Super_L:
485 case GDK_Super_R:
486 case GDK_Hyper_L:
487 case GDK_Hyper_R:
488 return true;
489 default:
490 return false;
494 guint
495 KeyEventArgs::GetKeyVal ()
497 return event->keyval;
500 gunichar
501 KeyEventArgs::GetUnicode ()
503 return gdk_keyval_to_unicode (event->keyval);
508 // ErrorEventArgs
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);
525 void
526 ErrorEventArgs::Initialize (Type::Kind kind, ErrorEventArgsType type, const MoonError &error, int extended_error_code, const char *extended_msg)
528 SetObjectType (kind);
529 error_type = type;
530 this->error = new MoonError (error);
531 extended_message = g_strdup (extended_msg);
532 extended_code = extended_error_code;
535 ErrorEventArgs::~ErrorEventArgs ()
537 delete error;
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;
570 line_number = line;
573 ParserErrorEventArgs::~ParserErrorEventArgs ()
575 g_free (xaml_file);
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;
588 if (marker)
589 marker->ref ();
592 TimelineMarkerRoutedEventArgs::~TimelineMarkerRoutedEventArgs ()
594 if (marker)
595 marker->unref ();