2009-10-09 Chris Toshok <toshok@ximian.com>
[moon.git] / src / trigger.cpp
blob09e6ca3ca3728e99580b4efe861218571e94b0ca
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * trigger.cpp
5 * Copyright 2007 Novell, Inc. (http://www.novell.com)
7 * See the LICENSE file included with the distribution for details.
8 *
9 */
11 #include <config.h>
13 #include "runtime.h"
14 #include "trigger.h"
15 #include "collection.h"
16 #include "uielement.h"
19 EventTrigger::EventTrigger ()
21 SetObjectType (Type::EVENTTRIGGER);
22 registered_event_id = -1;
25 EventTrigger::~EventTrigger ()
29 void
30 EventTrigger::SetTarget (DependencyObject *target)
32 g_return_if_fail (target);
34 #if AT_SOME_POINT_SILVERLIGHT_GETS_ITS_HEAD_OUT_OF_ITS_
35 const char *event = GetRoutedEvent ();
36 const char *dot;
38 if (event && (dot = strchr (event, '.'))) {
39 char *type = g_strndup (event, dot-event);
40 const char *event_name = dot + 1;
42 Type *event_type = Type::Find (type);
43 g_free (type);
45 if (event_type) {
46 // event type has to exist
47 if (target->GetType()->IsSubclassOf (event_type->GetKind ())) {
48 // the type of the target has
49 // to be a subclass of the
50 // event type for the event to
51 // be available.
52 registered_event_id = event_type->LookupEvent (event_name);
55 } else if (event) {
56 registered_event_id = target->GetType ()->LookupEvent (event);
58 #else
59 registered_event_id = UIElement::LoadedEvent;
60 #endif
62 if (registered_event_id == -1)
63 g_warning ("failed to set target");
65 if (registered_event_id != -1)
66 target->AddHandler (registered_event_id, event_trigger_fire_actions, this);
69 void
70 EventTrigger::RemoveTarget (DependencyObject *target)
72 g_return_if_fail (target);
74 if (registered_event_id != -1) {
75 target->RemoveHandler (registered_event_id, event_trigger_fire_actions, this);
76 registered_event_id = -1;
80 void
81 EventTrigger::event_trigger_fire_actions (EventObject *sender, EventArgs *calldata, gpointer closure)
83 EventTrigger *trigger = (EventTrigger *) closure;
85 g_return_if_fail (trigger);
87 TriggerActionCollection *actions = trigger->GetActions();
89 for (int i = 0; i < actions->GetCount (); i++) {
90 TriggerAction *action = actions->GetValueAt (i)->AsTriggerAction ();
91 action->Fire ();
96 void
97 event_trigger_action_add (EventTrigger *trigger, TriggerAction *action)
99 trigger->GetActions()->Add (action);