2009-10-09 Chris Toshok <toshok@ximian.com>
[moon.git] / src / eventargs.cpp
blob2c8ce5a8840994ba11f25c4db4fccfafb7f151ff
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"
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 ()
46 TimeSpan
47 RenderingEventArgs::GetRenderingTime ()
49 return renderingTime;
54 CollectionChangedEventArgs::CollectionChangedEventArgs ()
55 : EventArgs (Type::COLLECTIONCHANGEDEVENTARGS)
57 action = CollectionChangedActionAdd;
58 old_item = NULL;
59 new_item = NULL;
60 index = -1;
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;
69 this->index = index;
72 CollectionChangedEventArgs::~CollectionChangedEventArgs ()
76 void
77 CollectionChangedEventArgs::SetChangedAction (CollectionChangedAction action)
79 this->action = action;
82 CollectionChangedAction
83 CollectionChangedEventArgs::GetChangedAction ()
85 return action;
88 void
89 CollectionChangedEventArgs::SetNewItem (Value *item)
91 new_item = item;
94 Value *
95 CollectionChangedEventArgs::GetNewItem ()
97 return new_item;
100 void
101 CollectionChangedEventArgs::SetOldItem (Value *item)
103 old_item = item;
106 Value *
107 CollectionChangedEventArgs::GetOldItem ()
109 return old_item;
112 void
113 CollectionChangedEventArgs::SetIndex (int index)
115 this->index = index;
119 CollectionChangedEventArgs::GetIndex ()
121 return index;
124 DownloadProgressEventArgs::DownloadProgressEventArgs (double progress)
125 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS)
127 this->progress = progress;
130 DownloadProgressEventArgs::DownloadProgressEventArgs ()
131 : EventArgs (Type::DOWNLOADPROGRESSEVENTARGS)
133 progress = 0.0;
136 DownloadProgressEventArgs::~DownloadProgressEventArgs ()
140 void
141 DownloadProgressEventArgs::SetProgress (double progress)
143 this->progress = progress;
146 double
147 DownloadProgressEventArgs::GetProgress ()
149 return progress;
152 RoutedEventArgs::RoutedEventArgs (DependencyObject *source)
153 : EventArgs (Type::ROUTEDEVENTARGS)
155 if (source)
156 source->ref ();
158 this->source = source;
159 handled = false;
162 RoutedEventArgs::RoutedEventArgs ()
163 : EventArgs (Type::ROUTEDEVENTARGS)
165 source = NULL;
166 handled = false;
169 RoutedEventArgs::RoutedEventArgs (Type::Kind kind)
170 : EventArgs (kind)
172 source = NULL;
173 handled = false;
176 RoutedEventArgs::RoutedEventArgs (DependencyObject *source, Type::Kind kind)
178 if (source)
179 source->ref ();
180 this->source = source;
181 handled = false;
184 RoutedEventArgs::~RoutedEventArgs ()
186 if (source)
187 source->unref ();
190 void
191 RoutedEventArgs::SetHandled (bool handled)
193 this->handled = handled;
196 bool
197 RoutedEventArgs::GetHandled ()
199 return handled;
202 DependencyObject *
203 RoutedEventArgs::GetSource ()
205 return source;
208 void
209 RoutedEventArgs::SetSource (DependencyObject *el)
211 if (source)
212 source->unref();
213 source = el;
214 if (source)
215 source->ref();
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);
246 return (int)state;
249 void
250 MouseEventArgs::GetPosition (UIElement *relative_to, double *x, double *y)
252 *x = *y = 0.0;
253 if (gdk_event_get_coords (event, x, y)) {
254 if (relative_to) {
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);
266 StylusInfo*
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;
276 break;
277 case GDK_BUTTON_PRESS:
278 case GDK_BUTTON_RELEASE:
279 gdk_device = ((GdkEventButton*)event)->device;
280 break;
282 default:
283 case GDK_ENTER_NOTIFY:
284 case GDK_LEAVE_NOTIFY:
285 /* GdkEventCrossing doesn't have a device field. ugh */
286 gdk_device = NULL;
287 break;
290 if (gdk_device) {
291 switch (gdk_device->source) {
292 case GDK_SOURCE_PEN:
293 case GDK_SOURCE_ERASER:
294 type = TabletDeviceTypeStylus;
295 break;
296 case GDK_SOURCE_MOUSE:
297 case GDK_SOURCE_CURSOR: /* XXX not sure where to lump this in.. in the stylus block? */
298 default:
299 type = TabletDeviceTypeMouse;
300 break;
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));
311 return info;
314 StylusPointCollection*
315 MouseEventArgs::GetStylusPoints (UIElement *ink_presenter)
317 StylusPointCollection *points = new StylusPointCollection ();
318 double pressure;
319 double x, y;
321 GetPosition (ink_presenter, &x, &y);
322 if (!((GdkEventMotion *) event)->device || !gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &pressure))
323 pressure = 0.0;
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));
330 points->Add (point);
332 point->unref ();
334 return points;
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;
357 break;
358 default:
359 return 0;
364 MouseButtonEventArgs::GetClickCount ()
366 switch (event->type) {
367 case GDK_3BUTTON_PRESS:
368 return 3;
369 case GDK_2BUTTON_PRESS:
370 return 2;
371 case GDK_BUTTON_PRESS:
372 return 1;
373 default:
374 return 0;
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) {
405 case GDK_SCROLL_UP:
406 return MOON_SCROLL_WHEEL_DELTA;
407 case GDK_SCROLL_DOWN:
408 return -MOON_SCROLL_WHEEL_DELTA;
410 default:
411 case GDK_SCROLL_LEFT:
412 case GDK_SCROLL_RIGHT:
413 return 0;
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);
434 GdkEventKey *
435 KeyEventArgs::GetEvent ()
437 return event;
441 KeyEventArgs::GetKey ()
443 return Keyboard::MapKeyValToKey (event->keyval);
447 KeyEventArgs::GetPlatformKeyCode ()
449 return event->hardware_keycode;
452 GdkModifierType
453 KeyEventArgs::GetModifiers ()
455 return (GdkModifierType) event->state;
458 bool
459 KeyEventArgs::IsModifier ()
461 #if GTK_CHECK_VERSION(2,10,0)
462 if (gtk_check_version(2,10,0))
463 return event->is_modifier;
464 else
465 #endif
466 switch (event->keyval) {
467 case GDK_Shift_L:
468 case GDK_Shift_R:
469 case GDK_Control_L:
470 case GDK_Control_R:
471 case GDK_Meta_L:
472 case GDK_Meta_R:
473 case GDK_Alt_L:
474 case GDK_Alt_R:
475 case GDK_Super_L:
476 case GDK_Super_R:
477 case GDK_Hyper_L:
478 case GDK_Hyper_R:
479 return true;
480 default:
481 return false;
485 guint
486 KeyEventArgs::GetKeyVal ()
488 return event->keyval;
491 gunichar
492 KeyEventArgs::GetUnicode ()
494 return gdk_keyval_to_unicode (event->keyval);
499 // ErrorEventArgs
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);
516 void
517 ErrorEventArgs::Initialize (Type::Kind kind, ErrorEventArgsType type, const MoonError &error, int extended_error_code, const char *extended_msg)
519 SetObjectType (kind);
520 error_type = type;
521 this->error = new MoonError (error);
522 extended_message = g_strdup (extended_msg);
523 extended_code = extended_error_code;
526 ErrorEventArgs::~ErrorEventArgs ()
528 delete error;
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;
561 line_number = line;
564 ParserErrorEventArgs::~ParserErrorEventArgs ()
566 g_free (xaml_file);
567 g_free (xml_element);
568 g_free (xml_attribute);