added MouseWheel event support for Silverlight 3.0
[moon.git] / plugin / plugin-class.cpp
blobf83662a387b245c9b9ac35729bc210ce64f6601b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * plugin-class.cpp: MoonLight browser plugin.
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #include <config.h>
16 #include <ctype.h>
18 #include "plugin-class.h"
19 #include "plugin-accessibility.h"
20 #include "browser-bridge.h"
21 #include "plugin.h"
22 #include "deployment.h"
23 #include "bitmapimage.h"
24 #include "uri.h"
25 #include "textbox.h"
26 #include "grid.h"
27 #include "multiscaleimage.h"
28 #include "tilesource.h"
29 #include "deepzoomimagetilesource.h"
31 #ifdef DEBUG
32 #define DEBUG_WARN_NOTIMPLEMENTED(x) printf ("not implemented: (%s) " G_STRLOC "\n", x)
33 #define d(x) x
34 #else
35 #define DEBUG_WARN_NOTIMPLEMENTED(x)
36 #define d(x)
37 #endif
39 // debug scriptable object
40 #define ds(x)
42 // warnings
43 #define w(x) x
46 static char*
47 npidentifier_to_downstr (NPIdentifier id)
49 if (!NPN_IdentifierIsString (id))
50 return NULL;
52 NPUTF8 *strname = NPN_UTF8FromIdentifier (id);
53 char *p = strname;
54 while (*p) {
55 *p = g_ascii_tolower (*p);
56 p++;
59 return strname;
62 /* for use with bsearch & qsort */
63 static int
64 compare_mapping (const void *m1, const void *m2)
66 MoonNameIdMapping *map1 = (MoonNameIdMapping*) m1;
67 MoonNameIdMapping *map2 = (MoonNameIdMapping*) m2;
68 return strcmp(map1->name, map2->name);
71 static int
72 map_name_to_id (NPIdentifier name, const MoonNameIdMapping mapping[], int count)
74 char *strname = npidentifier_to_downstr (name);
75 if (!strname)
76 return NoMapping;
78 MoonNameIdMapping key, *result;
80 key.name = strname;
81 result = (MoonNameIdMapping*)bsearch(&key, mapping, count,
82 sizeof(MoonNameIdMapping), compare_mapping);
85 NPN_MemFree (strname);
86 if (!result)
87 return NoMapping;
89 return result->id;
92 static const char *
93 map_moon_id_to_event_name (int moon_id)
95 const char *name = NULL;
97 switch (moon_id) {
98 case MoonId_BufferingProgressChanged: name = "BufferingProgressChanged"; break;
99 case MoonId_CurrentStateChanged: name = "CurrentStateChanged"; break;
100 case MoonId_DownloadProgressChanged: name = "DownloadProgressChanged"; break;
101 case MoonId_GotFocus: name = "GotFocus"; break;
102 case MoonId_KeyDown: name = "KeyDown"; break;
103 case MoonId_KeyUp: name = "KeyUp"; break;
104 case MoonId_LostFocus: name = "LostFocus"; break;
105 case MoonId_Loaded: name = "Loaded"; break;
106 case MoonId_MarkerReached: name = "MarkerReached"; break;
107 case MoonId_MediaEnded: name = "MediaEnded"; break;
108 case MoonId_MediaFailed: name = "MediaFailed"; break;
109 case MoonId_MediaOpened: name = "MediaOpened"; break;
110 case MoonId_MouseEnter: name = "MouseEnter"; break;
111 case MoonId_MouseLeave: name = "MouseLeave"; break;
112 case MoonId_MouseMove: name = "MouseMove"; break;
113 case MoonId_MouseLeftButtonDown: name = "MouseLeftButtonDown"; break;
114 case MoonId_MouseLeftButtonUp: name = "MouseLeftButtonUp"; break;
115 case MoonId_OnResize: name = "Resize"; break;
116 case MoonId_OnFullScreenChange: name = "FullScreenChange"; break;
117 case MoonId_OnError: name = "Error"; break;
118 case MoonId_OnLoad: name = "Load"; break;
119 case MoonId_OnSourceDownloadProgressChanged: name = "SourceDownloadProgressChanged"; break;
120 case MoonId_OnSourceDownloadComplete: name = "SourceDownloadComplete"; break;
123 return name;
127 void
128 string_to_npvariant (const char *value, NPVariant *result)
130 char *retval;
132 if (value)
133 retval = NPN_strdup ((char *)value);
134 else
135 retval = NPN_strdup ("");
137 STRINGZ_TO_NPVARIANT (retval, *result);
140 static void
141 value_to_variant (NPObject *npobj, Value *v, NPVariant *result, DependencyObject *parent_obj = NULL, DependencyProperty *parent_property = NULL)
143 char utf8[8];
144 int n;
146 if (!v) {
147 NULL_TO_NPVARIANT (*result);
148 return;
151 switch (v->GetKind ()) {
152 case Type::BOOL:
153 BOOLEAN_TO_NPVARIANT (v->AsBool(), *result);
154 break;
155 case Type::CHAR:
156 n = g_unichar_to_utf8 (v->AsChar (), utf8);
157 utf8[n] = '\0';
158 string_to_npvariant (utf8, result);
159 break;
160 case Type::INT32:
161 INT32_TO_NPVARIANT (v->AsInt32(), *result);
162 break;
163 case Type::UINT32:
164 // XXX not much else we can do here...
165 INT32_TO_NPVARIANT ((gint32)v->AsUInt32(), *result);
166 break;
167 case Type::DOUBLE:
168 DOUBLE_TO_NPVARIANT (v->AsDouble(), *result);
169 break;
170 case Type::STRING:
171 string_to_npvariant (v->AsString(), result);
172 break;
173 case Type::POINT: {
174 MoonlightPoint *point = (MoonlightPoint *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightPointClass);
175 point->point = *v->AsPoint ();
176 OBJECT_TO_NPVARIANT (point, *result);
177 break;
179 case Type::RECT: {
180 MoonlightRect *rect = (MoonlightRect *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightRectClass);
181 rect->rect = *v->AsRect ();
182 OBJECT_TO_NPVARIANT (rect, *result);
183 break;
185 case Type::DURATION: {
186 MoonlightDuration *duration = (MoonlightDuration *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightDurationClass);
187 duration->SetParentInfo (parent_obj, parent_property);
188 OBJECT_TO_NPVARIANT (duration, *result);
189 break;
191 case Type::TIMESPAN: {
192 MoonlightTimeSpan *timespan = (MoonlightTimeSpan *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightTimeSpanClass);
193 timespan->SetParentInfo (parent_obj, parent_property);
194 OBJECT_TO_NPVARIANT (timespan, *result);
195 break;
197 case Type::GRIDLENGTH: {
198 MoonlightGridLength *gridlength = (MoonlightGridLength *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightGridLengthClass);
199 gridlength->SetParentInfo (parent_obj, parent_property);
200 OBJECT_TO_NPVARIANT (gridlength, *result);
201 break;
203 case Type::URI: {
204 char *uri_string = v->AsUri() ? v->AsUri()->ToString() : NULL;
205 string_to_npvariant (uri_string ? uri_string : "", result);
206 g_free (uri_string);
207 break;
209 case Type::FONTFAMILY: {
210 char *family = v->AsFontFamily() ? v->AsFontFamily()->source : NULL;
211 string_to_npvariant (family ? family : "", result);
212 break;
214 case Type::FONTWEIGHT: {
215 const char *weight = enums_int_to_str ("FontWeight", v->AsFontWeight() ? v->AsFontWeight()->weight : FontWeightsNormal);
216 string_to_npvariant (weight, result);
217 break;
219 case Type::FONTSTYLE: {
220 const char *style = enums_int_to_str ("FontStyle", v->AsFontStyle() ? v->AsFontStyle()->style : FontStylesNormal);
221 string_to_npvariant (style, result);
222 break;
224 case Type::FONTSTRETCH: {
225 const char *stretch = enums_int_to_str ("FontStretch", v->AsFontStretch() ? v->AsFontStretch()->stretch : FontStretchesNormal);
226 string_to_npvariant (stretch, result);
227 break;
229 case Type::COLOR: {
230 Color *c = v->AsColor ();
231 gint32 color = ((((gint32)(c->a * 255.0)) << 24) | (((gint32)(c->r * 255.0)) << 16) |
232 (((gint32)(c->g * 255.0)) << 8) | ((gint32)(c->b * 255.0)));
233 INT32_TO_NPVARIANT (color, *result);
234 break;
236 case Type::KEYTIME: {
237 MoonlightKeyTime *keytime = (MoonlightKeyTime *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightKeyTimeClass);
238 keytime->SetParentInfo (parent_obj, parent_property);
239 OBJECT_TO_NPVARIANT (keytime, *result);
240 break;
242 case Type::THICKNESS: {
243 MoonlightThickness *thickness = (MoonlightThickness *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightThicknessClass);
244 thickness->SetParentInfo (parent_obj, parent_property);
245 OBJECT_TO_NPVARIANT (thickness, *result);
246 break;
248 case Type::CORNERRADIUS: {
249 MoonlightCornerRadius *corner_radius = (MoonlightCornerRadius *) NPN_CreateObject (((MoonlightObject *) npobj)->instance, MoonlightCornerRadiusClass);
250 corner_radius->SetParentInfo (parent_obj, parent_property);
251 OBJECT_TO_NPVARIANT (corner_radius, *result);
252 break;
254 case Type::NPOBJ: {
255 OBJECT_TO_NPVARIANT ((NPObject *) v->AsNPObj (), *result);
256 break;
258 default:
259 /* more builtins.. */
260 if (v->Is (Type::DEPENDENCY_OBJECT)) {
261 MoonlightEventObjectObject *depobj =
262 EventObjectCreateWrapper (((MoonlightObject *) npobj)->instance, v->AsDependencyObject ());
263 OBJECT_TO_NPVARIANT (depobj, *result);
264 } else {
265 printf ("value_to_variant, can't create a variant of a %i = %s\n", v->GetKind (), Type::Find (v->GetKind ())->GetName ());
266 NULL_TO_NPVARIANT (*result);
268 break;
272 void
273 variant_to_value (const NPVariant *v, Value **result)
275 switch (v->type) {
276 case NPVariantType_Bool:
277 *result = new Value (NPVARIANT_TO_BOOLEAN (*v));
278 break;
279 case NPVariantType_Int32:
280 *result = new Value ((int32_t) NPVARIANT_TO_INT32 (*v));
281 break;
282 case NPVariantType_Double:
283 *result = new Value (NPVARIANT_TO_DOUBLE (*v));
284 break;
285 case NPVariantType_String: {
286 char *value = STRDUP_FROM_VARIANT (*v);
287 *result = new Value (value);
288 g_free (value);
289 break;
291 case NPVariantType_Void:
292 DEBUG_WARN_NOTIMPLEMENTED ("void variant type");
293 *result = NULL;
294 break;
295 case NPVariantType_Null:
296 DEBUG_WARN_NOTIMPLEMENTED ("null variant type");
297 *result = new Value (Type::DEPENDENCY_OBJECT);
298 break;
299 case NPVariantType_Object:
300 *result = new Value (Type::NPOBJ, NPVARIANT_TO_OBJECT (*v));
301 break;
302 default:
303 d (printf ("Got invalid value from javascript.\n"));
304 *result = NULL;
305 break;
309 enum DependencyObjectClassNames {
310 COLLECTION_CLASS,
311 CONTROL_CLASS,
312 DEPENDENCY_OBJECT_CLASS,
313 UI_ELEMENT_CLASS,
314 DOWNLOADER_CLASS,
315 IMAGE_BRUSH_CLASS,
316 IMAGE_CLASS,
317 MEDIA_ELEMENT_CLASS,
318 STORYBOARD_CLASS,
319 STYLUS_INFO_CLASS,
320 STYLUS_POINT_COLLECTION_CLASS,
321 STROKE_COLLECTION_CLASS,
322 STROKE_CLASS,
323 TEXT_BOX_CLASS,
324 PASSWORD_BOX_CLASS,
325 TEXT_BLOCK_CLASS,
326 EVENT_ARGS_CLASS,
327 ROUTED_EVENT_ARGS_CLASS,
328 ERROR_EVENT_ARGS_CLASS,
329 KEY_EVENT_ARGS_CLASS,
330 MARKER_REACHED_EVENT_ARGS_CLASS,
331 MOUSE_EVENT_ARGS_CLASS,
332 DOWNLOAD_PROGRESS_EVENT_ARGS_CLASS,
333 MULTI_SCALE_IMAGE_CLASS,
335 DEPENDENCY_OBJECT_CLASS_NAMES_LAST
338 NPClass *dependency_object_classes[DEPENDENCY_OBJECT_CLASS_NAMES_LAST];
340 bool
341 npobject_is_dependency_object (NPObject *obj)
343 for (int i = 0; i < DEPENDENCY_OBJECT_CLASS_NAMES_LAST; i++) {
344 if (dependency_object_classes [i] == obj->_class)
345 return true;
347 return false;
350 static bool
351 npvariant_is_dependency_object (NPVariant var)
353 if (!NPVARIANT_IS_OBJECT (var))
354 return false;
356 return npobject_is_dependency_object (NPVARIANT_TO_OBJECT (var));
359 static bool
360 npvariant_is_object_class (NPVariant var, int type)
362 NPObject *obj;
364 if (type < 0 || type >= DEPENDENCY_OBJECT_CLASS_NAMES_LAST)
365 return false;
367 if (!NPVARIANT_IS_OBJECT (var))
368 return false;
370 obj = NPVARIANT_TO_OBJECT (var);
372 return obj->_class == dependency_object_classes[type];
375 #define npvariant_is_downloader(v) npvariant_is_object_class (v, DOWNLOADER_CLASS)
377 static bool
378 npvariant_is_moonlight_object (NPVariant var)
380 NPClass *moonlight_types[] = {
381 MoonlightContentClass,
382 MoonlightDurationClass,
383 MoonlightObjectClass,
384 MoonlightPointClass,
385 MoonlightScriptableObjectClass,
386 MoonlightScriptControlClass,
387 MoonlightSettingsClass,
388 MoonlightTimeSpanClass
390 NPObject *obj;
391 guint i;
393 if (!NPVARIANT_IS_OBJECT (var))
394 return false;
396 obj = NPVARIANT_TO_OBJECT (var);
397 if (npobject_is_dependency_object (obj))
398 return true;
400 for (i = 0; i < G_N_ELEMENTS (moonlight_types); i++) {
401 if (obj->_class == moonlight_types[i])
402 return true;
405 return false;
408 EventListenerProxy::EventListenerProxy (NPP instance, const char *event_name, const char *cb_name)
409 : EventObject (Type::EVENTLISTENERPROXY)
411 this->instance = instance;
412 this->event_name = g_strdup (event_name);
413 this->event_id = -1;
414 this->target_object = NULL;
415 this->owner = NULL;
416 this->one_shot = false;
417 this->is_func = false;
418 if (!strncmp (cb_name, "javascript:", strlen ("javascript:")))
419 cb_name += strlen ("javascript:");
420 this->callback = g_strdup (cb_name);
423 EventListenerProxy::EventListenerProxy (NPP instance, const char *event_name, const NPVariant *cb)
424 : EventObject (Type::EVENTLISTENERPROXY)
426 this->instance = instance;
427 this->event_name = g_strdup (event_name);
428 this->event_id = -1;
429 this->target_object = NULL;
430 this->owner = NULL;
431 this->one_shot = false;
433 if (NPVARIANT_IS_OBJECT (*cb)) {
434 this->is_func = true;
435 this->callback = NPVARIANT_TO_OBJECT (*cb);
436 NPN_RetainObject ((NPObject *) this->callback);
437 } else {
438 this->is_func = false;
439 this->callback = STRDUP_FROM_VARIANT (*cb);
443 EventListenerProxy::~EventListenerProxy ()
445 if (is_func) {
446 // XXX we *want* to be able to do this, we really do, but we have no
447 // good means to invalidate EventListenerProxy, and thereby set the
448 // callback to NULL.
450 // instead we do it in ::RemoveHandler, which is only invoked via JS's removeEventListener
452 // if (callback != NULL)
453 // NPN_ReleaseObject ((NPObject *) callback);
455 else {
456 g_free (callback);
459 g_free (event_name);
462 const char *
463 EventListenerProxy::GetCallbackAsString ()
465 if (is_func)
466 return "";
468 return (const char *)callback;
471 void
472 EventListenerProxy::SetOwner (MoonlightObject *owner)
474 this->owner = owner;
478 EventListenerProxy::AddHandler (EventObject *obj)
480 target_object = obj;
482 event_id = obj->GetType()->LookupEvent (event_name);
484 if (event_id == -1) {
485 d(printf ("object of type `%s' does not provide an event named `%s'\n",
486 obj->GetTypeName(), event_name));
487 return -1;
490 token = obj->AddHandler (event_id, proxy_listener_to_javascript, this, on_handler_removed);
491 return token;
495 EventListenerProxy::AddXamlHandler (EventObject *obj)
497 target_object = obj;
499 event_id = obj->GetType()->LookupEvent (event_name);
501 if (event_id == -1) {
502 d(printf ("object of type `%s' does not provide an event named `%s'\n",
503 obj->GetTypeName(), event_name));
504 return -1;
507 token = obj->AddXamlHandler (event_id, proxy_listener_to_javascript, this, on_handler_removed);
509 return token;
512 void
513 EventListenerProxy::RemoveHandler ()
515 if (target_object && event_id != -1) {
516 target_object->RemoveHandler (event_id, token);
517 if (is_func && callback) {
518 NPN_ReleaseObject ((NPObject *) callback);
519 callback = NULL;
522 else {
523 on_handler_removed (this);
527 void
528 EventListenerProxy::on_handler_removed (gpointer closure)
530 // by the time we get here, the target_object has disclaimed
531 // all knowledge of this proxy.
533 EventListenerProxy *proxy = (EventListenerProxy *) closure;
535 if (proxy->owner) {
536 proxy->owner->ClearEventProxy (proxy);
538 else {
539 // we don't have an owner, so there's nothing special
540 // for us to do here.
543 proxy->target_object = NULL;
544 proxy->event_id = -1;
545 proxy->unref_delayed();
548 void
549 EventListenerProxy::proxy_listener_to_javascript (EventObject *sender, EventArgs *calldata, gpointer closure)
551 EventListenerProxy *proxy = (EventListenerProxy *) closure;
552 EventObject *js_sender = sender;
553 NPVariant args[2];
554 NPVariant result;
555 int argcount = 1;
556 PluginInstance *plugin = (PluginInstance *)proxy->instance->pdata;
557 Deployment *previous_deployment;
559 if (plugin == NULL) {
560 // Firefox can invalidate our NPObjects after the plugin itself
561 // has been destroyed. During this invalidation our NPObjects call
562 // into the moonlight runtime, which then emits events.
563 d(printf ("Moonlight: The plugin has been deleted, but we're still emitting events?\n"));
564 return;
567 // do not let cross-domain application re-register the events (e.g. via scripting)
568 if (plugin->IsCrossDomainApplication ()) {
569 // g_warning ("xdomain restriction on javascript event: %s", proxy->GetCallbackAsString ());
570 return;
573 previous_deployment = Deployment::GetCurrent ();
574 Deployment::SetCurrent (plugin->GetDeployment ());
576 if (js_sender->GetObjectType () == Type::SURFACE) {
577 // This is somewhat hackish, but is required for
578 // the FullScreenChanged event (js expects the
579 // sender to be the toplevel canvas, not the surface,
580 // nor the content).
581 js_sender = ((Surface*) js_sender)->GetToplevel ();
584 MoonlightEventObjectObject *depobj = NULL;
585 if (js_sender) {
586 depobj = EventObjectCreateWrapper (proxy->instance, js_sender);
587 plugin->AddCleanupPointer (&depobj);
588 OBJECT_TO_NPVARIANT (depobj, args[0]);
589 } else {
590 NULL_TO_NPVARIANT (args[0]);
593 //printf ("proxying event %s to javascript, sender = %p (%s)\n", proxy->event_name, sender, sender->GetTypeName ());
594 MoonlightEventObjectObject *depargs = NULL;
595 if (calldata) {
596 depargs = EventObjectCreateWrapper (proxy->instance, calldata);
597 plugin->AddCleanupPointer (&depargs);
598 OBJECT_TO_NPVARIANT (depargs, args[1]);
599 argcount++;
602 if (proxy->is_func && proxy->callback) {
603 /* the event listener was added with a JS function object */
604 if (NPN_InvokeDefault (proxy->instance, (NPObject *) proxy->callback, args, argcount, &result))
605 NPN_ReleaseVariantValue (&result);
606 } else {
607 /* the event listener was added with a JS string (the function name) */
608 NPObject *object = NULL;
610 if (NPN_GetValue (proxy->instance, NPNVWindowNPObject, &object) == NPERR_NO_ERROR) {
611 if (NPN_Invoke (proxy->instance, object, NPID ((char *) proxy->callback), args, argcount, &result))
612 NPN_ReleaseVariantValue (&result);
616 if (depobj) {
617 plugin->RemoveCleanupPointer (&depobj);
618 NPN_ReleaseObject (depobj);
620 if (depargs) {
621 plugin->RemoveCleanupPointer (&depargs);
622 NPN_ReleaseObject (depargs);
624 if (proxy->one_shot)
625 proxy->RemoveHandler();
627 Deployment::SetCurrent (previous_deployment);
630 void
631 event_object_add_xaml_listener (EventObject *obj, PluginInstance *plugin, const char *event_name, const char *cb_name)
633 EventListenerProxy *proxy = new EventListenerProxy (plugin->GetInstance (), event_name, cb_name);
634 proxy->AddXamlHandler (obj);
637 class NamedProxyPredicate {
638 public:
639 NamedProxyPredicate (char *name) { this->name = g_strdup (name); }
640 ~NamedProxyPredicate () { g_free (name); }
642 static bool matches (EventHandler cb_handler, gpointer cb_data, gpointer data)
644 if (cb_handler != EventListenerProxy::proxy_listener_to_javascript)
645 return false;
646 if (cb_data == NULL)
647 return false;
648 EventListenerProxy *proxy = (EventListenerProxy*)cb_data;
649 NamedProxyPredicate *pred = (NamedProxyPredicate*)data;
651 return !strcasecmp (proxy->GetCallbackAsString(), pred->name);
653 private:
654 char *name;
657 /*** EventArgs **/
659 static NPObject *
660 event_args_allocate (NPP instance, NPClass *klass)
662 return new MoonlightEventArgs (instance);
665 MoonlightEventArgsType::MoonlightEventArgsType ()
667 allocate = event_args_allocate;
670 MoonlightEventArgsType *MoonlightEventArgsClass;
672 /*** RoutedEventArgs ***/
673 static NPObject *
674 routedeventargs_allocate (NPP instance, NPClass *klass)
676 return new MoonlightRoutedEventArgs (instance);
679 static const MoonNameIdMapping
680 routedeventargs_mapping[] = {
681 { "source", MoonId_Source },
684 bool
685 MoonlightRoutedEventArgs::GetProperty (int id, NPIdentifier name, NPVariant *result)
687 RoutedEventArgs *args = GetRoutedEventArgs ();
689 switch (id) {
690 case MoonId_Source: {
691 DependencyObject *source = args->GetSource ();
692 if (source) {
693 MoonlightEventObjectObject *source_obj = EventObjectCreateWrapper (instance, source);
694 OBJECT_TO_NPVARIANT (source_obj, *result);
696 else {
697 NULL_TO_NPVARIANT (*result);
700 return true;
703 default:
704 return MoonlightEventArgs::GetProperty (id, name, result);
708 MoonlightRoutedEventArgsType::MoonlightRoutedEventArgsType ()
710 allocate = routedeventargs_allocate;
712 AddMapping (routedeventargs_mapping, G_N_ELEMENTS (routedeventargs_mapping));
715 MoonlightRoutedEventArgsType *MoonlightRoutedEventArgsClass;
718 /*** ErrorEventArgs ***/
719 static NPObject *
720 erroreventargs_allocate (NPP instance, NPClass *klass)
722 return new MoonlightErrorEventArgs (instance);
725 static const MoonNameIdMapping
726 erroreventargs_mapping[] = {
727 { "charposition", MoonId_CharPosition },
728 { "errorcode", MoonId_ErrorCode },
729 { "errormessage", MoonId_ErrorMessage },
730 { "errortype", MoonId_ErrorType },
731 { "linenumber", MoonId_LineNumber },
732 { "methodname", MoonId_MethodName },
733 { "xamlfile", MoonId_XamlFile },
736 bool
737 MoonlightErrorEventArgs::GetProperty (int id, NPIdentifier name, NPVariant *result)
739 ErrorEventArgs *args = GetErrorEventArgs ();
741 switch (id) {
742 case MoonId_ErrorCode:
743 INT32_TO_NPVARIANT (args->GetErrorCode(), *result);
744 return true;
746 case MoonId_ErrorType:
747 switch (args->GetErrorType()) {
748 case NoError: string_to_npvariant ("NoError", result); break;
749 case UnknownError: string_to_npvariant ("UnknownError", result); break;
750 case InitializeError: string_to_npvariant ("InitializeError", result); break;
751 case ParserError: string_to_npvariant ("ParserError", result); break;
752 case ObjectModelError: string_to_npvariant ("ObjectModelError", result); break;
753 case RuntimeError: string_to_npvariant ("RuntimeError", result); break;
754 case DownloadError: string_to_npvariant ("DownloadError", result); break;
755 case MediaError: string_to_npvariant ("MediaError", result); break;
756 case ImageError: string_to_npvariant ("ImageError", result); break;
758 return true;
759 case MoonId_ErrorMessage:
760 string_to_npvariant (args->GetErrorMessage(), result);
761 return true;
762 case MoonId_LineNumber:
763 if (args->GetErrorType() == ParserError) {
764 INT32_TO_NPVARIANT (((ParserErrorEventArgs*)args)->line_number, *result);
765 } else {
766 DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.lineNumber");
767 INT32_TO_NPVARIANT (0, *result);
769 return true;
770 case MoonId_CharPosition:
771 if (args->GetErrorType() == ParserError) {
772 INT32_TO_NPVARIANT (((ParserErrorEventArgs*)args)->char_position, *result);
773 } else {
774 DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.charPosition");
775 INT32_TO_NPVARIANT (0, *result);
777 return true;
778 case MoonId_MethodName:
779 DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.methodName");
780 NULL_TO_NPVARIANT (*result);
781 return true;
782 case MoonId_XamlFile:
783 if (args->GetErrorType() == ParserError) {
784 string_to_npvariant (((ParserErrorEventArgs*)args)->xaml_file, result);
785 } else {
786 DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.xamlFile");
787 NULL_TO_NPVARIANT (*result);
789 return true;
790 default:
791 return MoonlightEventArgs::GetProperty (id, name, result);
795 MoonlightErrorEventArgsType::MoonlightErrorEventArgsType ()
797 allocate = erroreventargs_allocate;
799 AddMapping (erroreventargs_mapping, G_N_ELEMENTS (erroreventargs_mapping));
802 MoonlightErrorEventArgsType *MoonlightErrorEventArgsClass;
804 /*** Points ***/
805 static NPObject *
806 point_allocate (NPP instance, NPClass *klass)
808 return new MoonlightPoint (instance);
811 static const MoonNameIdMapping
812 point_mapping[] = {
813 { "x", MoonId_X },
814 { "y", MoonId_Y }
818 bool
819 MoonlightPoint::GetProperty (int id, NPIdentifier name, NPVariant *result)
821 switch (id) {
822 case MoonId_X:
823 DOUBLE_TO_NPVARIANT (point.x, *result);
824 return true;
826 case MoonId_Y:
827 DOUBLE_TO_NPVARIANT (point.y, *result);
828 return true;
830 default:
831 return MoonlightObject::GetProperty (id, name, result);
835 bool
836 MoonlightPoint::SetProperty (int id, NPIdentifier name, const NPVariant *value)
838 switch (id) {
839 case MoonId_X:
840 point.x = NPVARIANT_TO_DOUBLE (*value);
841 return true;
842 case MoonId_Y:
843 point.y = NPVARIANT_TO_DOUBLE (*value);
844 return true;
845 default:
846 return MoonlightObject::SetProperty (id, name, value);
850 MoonlightPointType::MoonlightPointType ()
852 allocate = point_allocate;
854 AddMapping (point_mapping, G_N_ELEMENTS (point_mapping));
857 MoonlightPointType *MoonlightPointClass;
859 /*** Rects ***/
860 static NPObject *
861 rect_allocate (NPP instance, NPClass *klass)
863 return new MoonlightRect (instance);
866 static const MoonNameIdMapping
867 rect_mapping[] = {
868 { "height", MoonId_Height },
869 { "width", MoonId_Width },
870 { "x", MoonId_X },
871 { "y", MoonId_Y },
874 bool
875 MoonlightRect::GetProperty (int id, NPIdentifier name, NPVariant *result)
877 switch (id) {
878 case MoonId_X:
879 DOUBLE_TO_NPVARIANT (rect.x, *result);
880 return true;
882 case MoonId_Y:
883 DOUBLE_TO_NPVARIANT (rect.y, *result);
884 return true;
886 case MoonId_Width:
887 DOUBLE_TO_NPVARIANT (rect.width, *result);
888 return true;
890 case MoonId_Height:
891 DOUBLE_TO_NPVARIANT (rect.height, *result);
892 return true;
894 default:
895 return MoonlightObject::GetProperty (id, name, result);
899 bool
900 MoonlightRect::SetProperty (int id, NPIdentifier name, const NPVariant *value)
902 switch (id) {
903 case MoonId_X:
904 rect.x = NPVARIANT_TO_DOUBLE (*value);
905 return true;
907 case MoonId_Y:
908 rect.y = NPVARIANT_TO_DOUBLE (*value);
909 return true;
911 case MoonId_Width:
912 rect.width = NPVARIANT_TO_DOUBLE (*value);
913 return true;
915 case MoonId_Height:
916 rect.height = NPVARIANT_TO_DOUBLE (*value);
917 return true;
919 default:
920 return MoonlightObject::SetProperty (id, name, value);;
925 MoonlightRectType::MoonlightRectType ()
927 allocate = rect_allocate;
929 AddMapping (rect_mapping, G_N_ELEMENTS (rect_mapping));
932 MoonlightRectType *MoonlightRectClass;
935 /*** Durations ***/
936 static NPObject *
937 duration_allocate (NPP instance, NPClass *klass)
939 return new MoonlightDuration (instance);
942 static const MoonNameIdMapping
943 duration_mapping[] = {
944 { "name", MoonId_Name },
945 { "seconds", MoonId_Seconds }
948 void
949 MoonlightDuration::SetParentInfo (DependencyObject *parent_obj, DependencyProperty *parent_property)
951 this->parent_obj = parent_obj;
952 this->parent_property = parent_property;
953 parent_obj->ref();
956 double
957 MoonlightDuration::GetValue()
959 Value *v = parent_obj->GetValue (parent_property);
960 return v ? v->AsDuration()->ToSecondsFloat () : 0.0;
963 bool
964 MoonlightDuration::GetProperty (int id, NPIdentifier name, NPVariant *result)
966 switch (id) {
967 case MoonId_Name:
968 string_to_npvariant ("", result);
969 return true;
971 case MoonId_Seconds:
972 DOUBLE_TO_NPVARIANT (GetValue(), *result);
973 return true;
975 default:
976 return MoonlightObject::GetProperty (id, name, result);
980 bool
981 MoonlightDuration::SetProperty (int id, NPIdentifier name, const NPVariant *value)
983 switch (id) {
984 case MoonId_Name:
985 return true;
987 case MoonId_Seconds:
988 parent_obj->SetValue (parent_property, Value(Duration::FromSecondsFloat (NPVARIANT_TO_DOUBLE (*value))));
989 return true;
991 default:
992 return MoonlightObject::SetProperty (id, name, value);
996 MoonlightDuration::~MoonlightDuration ()
998 if (parent_obj)
999 parent_obj->unref();
1002 MoonlightDurationType::MoonlightDurationType ()
1004 allocate = duration_allocate;
1006 AddMapping (duration_mapping, G_N_ELEMENTS (duration_mapping));
1009 MoonlightDurationType *MoonlightDurationClass;
1012 /*** TimeSpans ***/
1013 static NPObject *
1014 timespan_allocate (NPP instance, NPClass *klass)
1016 return new MoonlightTimeSpan (instance);
1019 static const MoonNameIdMapping
1020 timespan_mapping[] = {
1021 { "name", MoonId_Name },
1022 { "seconds", MoonId_Seconds }
1025 void
1026 MoonlightTimeSpan::SetParentInfo (DependencyObject *parent_obj, DependencyProperty *parent_property)
1028 this->parent_obj = parent_obj;
1029 this->parent_property = parent_property;
1030 parent_obj->ref();
1033 TimeSpan
1034 MoonlightTimeSpan::GetValue()
1036 Value *v = parent_obj->GetValue (parent_property);
1037 return v ? v->AsTimeSpan() : (TimeSpan)0;
1040 bool
1041 MoonlightTimeSpan::GetProperty (int id, NPIdentifier name, NPVariant *result)
1043 switch (id) {
1044 case MoonId_Name:
1045 string_to_npvariant ("", result);
1046 return true;
1047 case MoonId_Seconds:
1048 DOUBLE_TO_NPVARIANT (TimeSpan_ToSecondsFloat (GetValue ()), *result);
1049 return true;
1050 default:
1051 return MoonlightObject::GetProperty (id, name, result);
1055 bool
1056 MoonlightTimeSpan::SetProperty (int id, NPIdentifier name, const NPVariant *value)
1058 switch (id) {
1059 case MoonId_Name:
1060 return true;
1062 case MoonId_Seconds:
1063 if (NPVARIANT_IS_INT32 (*value)) {
1064 parent_obj->SetValue (parent_property, Value(TimeSpan_FromSecondsFloat (NPVARIANT_TO_INT32 (*value)), Type::TIMESPAN));
1065 } else if (NPVARIANT_IS_DOUBLE (*value)) {
1066 parent_obj->SetValue (parent_property, Value(TimeSpan_FromSecondsFloat (NPVARIANT_TO_DOUBLE (*value)), Type::TIMESPAN));
1067 } else {
1068 return false;
1070 return true;
1072 default:
1073 return MoonlightObject::SetProperty (id, name, value);
1077 MoonlightTimeSpan::~MoonlightTimeSpan ()
1079 if (parent_obj)
1080 parent_obj->unref ();
1083 MoonlightTimeSpanType::MoonlightTimeSpanType ()
1085 allocate = timespan_allocate;
1087 AddMapping (timespan_mapping, G_N_ELEMENTS (timespan_mapping));
1090 MoonlightTimeSpanType *MoonlightTimeSpanClass;
1092 /*** KeyTime ***/
1093 static NPObject *
1094 keytime_allocate (NPP instance, NPClass *klass)
1096 return new MoonlightKeyTime (instance);
1099 static const MoonNameIdMapping
1100 keytime_mapping[] = {
1101 { "name", MoonId_Name },
1102 { "seconds", MoonId_Seconds }
1105 void
1106 MoonlightKeyTime::SetParentInfo (DependencyObject *parent_obj, DependencyProperty *parent_property)
1108 this->parent_obj = parent_obj;
1109 this->parent_property = parent_property;
1110 parent_obj->ref();
1113 KeyTime*
1114 MoonlightKeyTime::GetValue()
1116 Value *v = parent_obj->GetValue (parent_property);
1117 return (v ? v->AsKeyTime() : NULL);
1120 bool
1121 MoonlightKeyTime::GetProperty (int id, NPIdentifier name, NPVariant *result)
1123 switch (id) {
1124 case MoonId_Name:
1125 string_to_npvariant ("", result);
1126 return true;
1127 case MoonId_Seconds:
1128 DOUBLE_TO_NPVARIANT (TimeSpan_ToSecondsFloat (GetValue ()->GetTimeSpan ()), *result);
1129 return true;
1130 default:
1131 return MoonlightObject::GetProperty (id, name, result);
1135 bool
1136 MoonlightKeyTime::SetProperty (int id, NPIdentifier name, const NPVariant *value)
1138 switch (id) {
1139 case MoonId_Name:
1140 return true;
1142 case MoonId_Seconds:
1143 if (NPVARIANT_IS_INT32 (*value))
1144 parent_obj->SetValue (parent_property, Value(KeyTime::FromTimeSpan (TimeSpan_FromSecondsFloat (NPVARIANT_TO_INT32 (*value)))));
1145 else if (NPVARIANT_IS_DOUBLE (*value))
1146 parent_obj->SetValue (parent_property, Value(KeyTime::FromTimeSpan (TimeSpan_FromSecondsFloat (NPVARIANT_TO_DOUBLE (*value)))));
1148 return true;
1149 default:
1150 return MoonlightObject::SetProperty (id, name, value);
1154 MoonlightKeyTime::~MoonlightKeyTime ()
1156 if (parent_obj)
1157 parent_obj->unref ();
1160 MoonlightKeyTimeType::MoonlightKeyTimeType ()
1162 allocate = keytime_allocate;
1164 AddMapping (keytime_mapping, G_N_ELEMENTS (keytime_mapping));
1167 MoonlightKeyTimeType *MoonlightKeyTimeClass;
1170 /*** Thickness ***/
1171 static NPObject *
1172 thickness_allocate (NPP instance, NPClass *klass)
1174 return new MoonlightThickness (instance);
1177 static const MoonNameIdMapping
1178 thickness_mapping[] = {
1179 { "bottom", MoonId_Bottom },
1180 { "left", MoonId_Left },
1181 { "right", MoonId_Right },
1182 { "top", MoonId_Top }
1185 void
1186 MoonlightThickness::SetParentInfo (DependencyObject *parent_obj, DependencyProperty *parent_property)
1188 this->parent_obj = parent_obj;
1189 this->parent_property = parent_property;
1190 parent_obj->ref();
1193 Thickness*
1194 MoonlightThickness::GetValue()
1196 Value *v = parent_obj->GetValue (parent_property);
1197 return (v ? v->AsThickness() : NULL);
1200 bool
1201 MoonlightThickness::GetProperty (int id, NPIdentifier name, NPVariant *result)
1203 switch (id) {
1204 case MoonId_Name:
1205 string_to_npvariant ("", result);
1206 return true;
1207 case MoonId_Left:
1208 DOUBLE_TO_NPVARIANT (GetValue ()->left, *result);
1209 return true;
1210 case MoonId_Top:
1211 DOUBLE_TO_NPVARIANT (GetValue ()->top, *result);
1212 return true;
1213 case MoonId_Right:
1214 DOUBLE_TO_NPVARIANT (GetValue ()->right, *result);
1215 return true;
1216 case MoonId_Bottom:
1217 DOUBLE_TO_NPVARIANT (GetValue ()->bottom, *result);
1218 return true;
1219 default:
1220 return MoonlightObject::GetProperty (id, name, result);
1224 bool
1225 MoonlightThickness::SetProperty (int id, NPIdentifier name, const NPVariant *value)
1227 switch (id) {
1228 case MoonId_Name:
1229 return true;
1231 case MoonId_Left:
1232 case MoonId_Top:
1233 case MoonId_Right:
1234 case MoonId_Bottom:
1235 return true;
1237 default:
1238 return MoonlightObject::SetProperty (id, name, value);
1242 MoonlightThickness::~MoonlightThickness ()
1244 if (parent_obj)
1245 parent_obj->unref ();
1248 MoonlightThicknessType::MoonlightThicknessType ()
1250 allocate = thickness_allocate;
1252 AddMapping (thickness_mapping, G_N_ELEMENTS (thickness_mapping));
1255 MoonlightThicknessType *MoonlightThicknessClass;
1257 /*** CornerRadius ***/
1258 static NPObject *
1259 CornerRadius_allocate (NPP instance, NPClass *klass)
1261 return new MoonlightCornerRadius (instance);
1264 static const MoonNameIdMapping
1265 CornerRadius_mapping[] = {
1266 { "bottomright", MoonId_BottomRight },
1267 { "bottomleft", MoonId_BottomLeft },
1268 { "topright", MoonId_TopRight },
1269 { "topleft", MoonId_TopLeft }
1272 void
1273 MoonlightCornerRadius::SetParentInfo (DependencyObject *parent_obj, DependencyProperty *parent_property)
1275 this->parent_obj = parent_obj;
1276 this->parent_property = parent_property;
1277 parent_obj->ref();
1280 CornerRadius*
1281 MoonlightCornerRadius::GetValue()
1283 Value *v = parent_obj->GetValue (parent_property);
1284 return (v ? v->AsCornerRadius() : NULL);
1287 bool
1288 MoonlightCornerRadius::GetProperty (int id, NPIdentifier name, NPVariant *result)
1290 switch (id) {
1291 case MoonId_Name:
1292 string_to_npvariant ("", result);
1293 return true;
1294 case MoonId_TopLeft:
1295 DOUBLE_TO_NPVARIANT (GetValue ()->topLeft, *result);
1296 return true;
1297 case MoonId_TopRight:
1298 DOUBLE_TO_NPVARIANT (GetValue ()->topRight, *result);
1299 return true;
1300 case MoonId_BottomLeft:
1301 DOUBLE_TO_NPVARIANT (GetValue ()->bottomLeft, *result);
1302 return true;
1303 case MoonId_BottomRight:
1304 DOUBLE_TO_NPVARIANT (GetValue ()->bottomRight, *result);
1305 return true;
1306 default:
1307 return MoonlightObject::GetProperty (id, name, result);
1311 bool
1312 MoonlightCornerRadius::SetProperty (int id, NPIdentifier name, const NPVariant *value)
1314 switch (id) {
1315 case MoonId_Name:
1316 return true;
1318 case MoonId_TopLeft:
1319 case MoonId_TopRight:
1320 case MoonId_BottomLeft:
1321 case MoonId_BottomRight:
1322 return true;
1324 default:
1325 return MoonlightObject::SetProperty (id, name, value);
1329 MoonlightCornerRadius::~MoonlightCornerRadius ()
1331 if (parent_obj)
1332 parent_obj->unref ();
1335 MoonlightCornerRadiusType::MoonlightCornerRadiusType ()
1337 allocate = CornerRadius_allocate;
1339 AddMapping (CornerRadius_mapping, G_N_ELEMENTS (CornerRadius_mapping));
1342 MoonlightCornerRadiusType *MoonlightCornerRadiusClass;
1344 /*** GridLength ***/
1345 static NPObject *
1346 gridlength_allocate (NPP instance, NPClass *klass)
1348 return new MoonlightGridLength (instance);
1351 static const MoonNameIdMapping
1352 gridlength_mapping[] = {
1353 { "gridunittype", MoonId_GridUnitType },
1354 { "value", MoonId_Value },
1357 void
1358 MoonlightGridLength::SetParentInfo (DependencyObject *parent_obj, DependencyProperty *parent_property)
1360 this->parent_obj = parent_obj;
1361 this->parent_property = parent_property;
1362 parent_obj->ref();
1365 GridLength*
1366 MoonlightGridLength::GetValue()
1368 Value *v = parent_obj->GetValue (parent_property);
1369 return (v ? v->AsGridLength() : NULL);
1372 bool
1373 MoonlightGridLength::GetProperty (int id, NPIdentifier name, NPVariant *result)
1375 switch (id) {
1376 case MoonId_Name:
1377 string_to_npvariant ("", result);
1378 return true;
1379 case MoonId_Value:
1380 DOUBLE_TO_NPVARIANT (GetValue ()->val, *result);
1381 return true;
1382 case MoonId_GridUnitType:
1383 string_to_npvariant (enums_int_to_str ("GridUnitType", GetValue()->type), result);
1384 return true;
1385 default:
1386 return MoonlightObject::GetProperty (id, name, result);
1390 bool
1391 MoonlightGridLength::SetProperty (int id, NPIdentifier name, const NPVariant *value)
1393 GridLength *current_gridlength = GetValue();
1394 GridLength gridlength;
1396 if (current_gridlength)
1397 gridlength = *current_gridlength;
1399 switch (id) {
1400 case MoonId_Name:
1401 return true;
1403 case MoonId_Value:
1404 gridlength.val = NPVARIANT_TO_DOUBLE (*value);
1405 parent_obj->SetValue (parent_property, Value(gridlength.val));
1406 return true;
1407 case MoonId_GridUnitType: {
1408 int unit_type = enums_str_to_int ("GridUnitType", NPVARIANT_TO_STRING (*value).utf8characters);
1409 if (unit_type == -1)
1410 return false; // FIXME: throw an exception?
1412 gridlength.type = (GridUnitType)unit_type;
1414 parent_obj->SetValue (parent_property, Value(gridlength));
1415 return true;
1418 default:
1419 return MoonlightObject::SetProperty (id, name, value);
1423 MoonlightGridLength::~MoonlightGridLength ()
1425 if (parent_obj)
1426 parent_obj->unref ();
1429 MoonlightGridLengthType::MoonlightGridLengthType ()
1431 allocate = gridlength_allocate;
1433 AddMapping (gridlength_mapping, G_N_ELEMENTS (gridlength_mapping));
1436 MoonlightGridLengthType *MoonlightGridLengthClass;
1438 /*** MoonlightDownloadProgressEventArgsClass **************************************************************/
1440 static NPObject *
1441 download_progress_event_allocate (NPP instance, NPClass *klass)
1443 return new MoonlightDownloadProgressEventArgs (instance);
1446 static const MoonNameIdMapping
1447 download_progress_event_mapping[] = {
1448 { "progress", MoonId_Progress },
1451 bool
1452 MoonlightDownloadProgressEventArgs::GetProperty (int id, NPIdentifier name, NPVariant *result)
1454 DownloadProgressEventArgs *event_args = GetDownloadProgressEventArgs ();
1456 switch (id) {
1457 case MoonId_Progress:
1458 DOUBLE_TO_NPVARIANT (event_args->GetProgress (), *result);
1459 return true;
1460 default:
1461 return MoonlightEventArgs::GetProperty (id, name, result);
1464 return false;
1467 MoonlightDownloadProgressEventArgsType::MoonlightDownloadProgressEventArgsType ()
1469 allocate = download_progress_event_allocate;
1471 AddMapping (download_progress_event_mapping, G_N_ELEMENTS (download_progress_event_mapping));
1474 MoonlightDownloadProgressEventArgsType *MoonlightDownloadProgressEventArgsClass;
1477 /*** MoonlightMouseEventArgsClass **************************************************************/
1479 static NPObject *
1480 mouse_event_allocate (NPP instance, NPClass *klass)
1482 return new MoonlightMouseEventArgsObject (instance);
1485 static const MoonNameIdMapping
1486 mouse_event_mapping[] = {
1487 { "ctrl", MoonId_Ctrl },
1488 { "handled", MoonId_Handled },
1489 { "getposition", MoonId_GetPosition },
1490 { "getstylusinfo", MoonId_GetStylusInfo },
1491 { "getstyluspoints", MoonId_GetStylusPoints },
1492 { "shift", MoonId_Shift },
1495 bool
1496 MoonlightMouseEventArgsObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
1498 MouseEventArgs *event_args = GetMouseEventArgs ();
1499 int state = event_args->GetState ();
1501 switch (id) {
1502 case MoonId_Shift:
1503 BOOLEAN_TO_NPVARIANT ((state & GDK_SHIFT_MASK) != 0, *result);
1504 return true;
1506 case MoonId_Ctrl:
1507 BOOLEAN_TO_NPVARIANT ((state & GDK_CONTROL_MASK) != 0, *result);
1508 return true;
1510 case MoonId_Handled:
1511 BOOLEAN_TO_NPVARIANT (event_args->GetHandled(), *result);
1512 return true;
1514 default:
1515 return MoonlightRoutedEventArgs::GetProperty (id, name, result);
1519 bool
1520 MoonlightMouseEventArgsObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
1522 MouseEventArgs *event_args = GetMouseEventArgs ();
1524 switch (id) {
1525 case MoonId_Handled:
1526 if (NPVARIANT_IS_BOOLEAN (*value))
1527 event_args->SetHandled (NPVARIANT_TO_BOOLEAN (*value));
1528 return true;
1529 default:
1530 return MoonlightRoutedEventArgs::SetProperty (id, name, value);
1534 bool
1535 MoonlightMouseEventArgsObject::Invoke (int id, NPIdentifier name,
1536 const NPVariant *args, guint32 argCount,
1537 NPVariant *result)
1539 MouseEventArgs *event_args = GetMouseEventArgs ();
1541 switch (id) {
1542 case MoonId_GetPosition: {
1543 if (!check_arg_list ("(no)", argCount, args) && (!NPVARIANT_IS_NULL(args[0]) || !npvariant_is_dependency_object (args[0])))
1544 return true;
1546 double x;
1547 double y;
1549 // The argument is an element
1550 // to calculate the position with respect to (or null
1551 // for screen space)
1553 UIElement *el = NULL;
1555 if (npvariant_is_dependency_object (args[0])) {
1556 DependencyObject *dob = DEPENDENCY_OBJECT_FROM_VARIANT (args [0]);
1557 if (dob->Is (Type::UIELEMENT))
1558 el = (UIElement *)dob;
1561 event_args->GetPosition (el, &x, &y);
1563 MoonlightPoint *point = (MoonlightPoint*)NPN_CreateObject (instance, MoonlightPointClass);
1564 point->point = Point (x, y);
1566 OBJECT_TO_NPVARIANT (point, *result);
1568 return true;
1570 case MoonId_GetStylusInfo: {
1571 if (argCount != 0)
1572 THROW_JS_EXCEPTION ("getStylusInfo");
1574 StylusInfo *info = event_args->GetStylusInfo ();
1575 MoonlightEventObjectObject *info_obj = EventObjectCreateWrapper (instance, info);
1576 info->unref ();
1577 OBJECT_TO_NPVARIANT (info_obj, *result);
1579 return true;
1581 case MoonId_GetStylusPoints: {
1582 if (!check_arg_list ("o", argCount, args))
1583 THROW_JS_EXCEPTION ("getStylusPoints");
1585 if (npvariant_is_dependency_object (args[0])) {
1586 DependencyObject *dob = DEPENDENCY_OBJECT_FROM_VARIANT (args [0]);
1587 if (!dob->Is (Type::INKPRESENTER))
1588 THROW_JS_EXCEPTION ("getStylusPoints");
1590 StylusPointCollection *points = event_args->GetStylusPoints ((UIElement*)dob);
1591 MoonlightEventObjectObject *col_obj = EventObjectCreateWrapper (instance, points);
1592 points->unref ();
1593 OBJECT_TO_NPVARIANT (col_obj, *result);
1596 return true;
1598 default:
1599 return MoonlightRoutedEventArgs::Invoke (id, name, args, argCount, result);
1604 MoonlightMouseEventArgsType::MoonlightMouseEventArgsType ()
1606 allocate = mouse_event_allocate;
1608 AddMapping (mouse_event_mapping, G_N_ELEMENTS (mouse_event_mapping));
1611 MoonlightMouseEventArgsType *MoonlightMouseEventArgsClass;
1614 /*** MoonlightMarkerReachedEventArgsClass **************************************************************/
1616 static NPObject *
1617 marker_reached_event_allocate (NPP instance, NPClass *klass)
1619 return new MoonlightMarkerReachedEventArgsObject (instance);
1622 static const MoonNameIdMapping
1623 marker_reached_event_mapping[] = {
1624 { "marker", MoonId_Marker }
1627 bool
1628 MoonlightMarkerReachedEventArgsObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
1630 MarkerReachedEventArgs *args = GetMarkerReachedEventArgs ();
1631 TimelineMarker *marker = args ? args->GetMarker () : NULL;
1633 switch (id) {
1634 case MoonId_Marker: {
1635 MoonlightEventObjectObject *meoo = EventObjectCreateWrapper (instance, marker);
1636 OBJECT_TO_NPVARIANT (meoo, *result);
1637 return true;
1639 default:
1640 return MoonlightEventArgs::GetProperty (id, name, result);;
1644 MoonlightMarkerReachedEventArgsType::MoonlightMarkerReachedEventArgsType ()
1646 allocate = marker_reached_event_allocate;
1648 AddMapping (marker_reached_event_mapping, G_N_ELEMENTS (marker_reached_event_mapping));
1651 MoonlightMarkerReachedEventArgsType *MoonlightMarkerReachedEventArgsClass;
1653 /*** MoonlightKeyEventArgsClass **************************************************************/
1655 static NPObject *
1656 keyboard_event_allocate (NPP instance, NPClass *klass)
1658 return new MoonlightKeyEventArgsObject (instance);
1661 static const MoonNameIdMapping
1662 keyboard_event_mapping[] = {
1663 { "ctrl", MoonId_Ctrl },
1664 { "handled", MoonId_Handled },
1665 { "key", MoonId_Key },
1666 { "platformkeycode", MoonId_PlatformKeyCode },
1667 { "shift", MoonId_Shift },
1671 bool
1672 MoonlightKeyEventArgsObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
1674 KeyEventArgs *args = GetKeyEventArgs ();
1676 switch (id) {
1677 case MoonId_Shift:
1678 BOOLEAN_TO_NPVARIANT ((args->GetModifiers () & GDK_SHIFT_MASK) != 0, *result);
1679 return true;
1681 case MoonId_Ctrl:
1682 BOOLEAN_TO_NPVARIANT ((args->GetModifiers () & GDK_CONTROL_MASK) != 0, *result);
1683 return true;
1685 case MoonId_Handled:
1686 BOOLEAN_TO_NPVARIANT (args->GetHandled(), *result);
1687 return true;
1689 case MoonId_Key:
1690 INT32_TO_NPVARIANT (args->GetKey (), *result);
1691 return true;
1693 case MoonId_PlatformKeyCode:
1694 INT32_TO_NPVARIANT (args->GetPlatformKeyCode (), *result);
1695 return true;
1697 default:
1698 return MoonlightEventArgs::GetProperty (id, name, result);
1702 MoonlightKeyEventArgsType::MoonlightKeyEventArgsType ()
1704 allocate = keyboard_event_allocate;
1706 AddMapping (keyboard_event_mapping, G_N_ELEMENTS (keyboard_event_mapping));
1709 MoonlightKeyEventArgsType *MoonlightKeyEventArgsClass;
1711 /*** our object base class */
1712 NPObject *
1713 _allocate (NPP instance, NPClass *klass)
1715 PluginInstance *plugin = (PluginInstance *)instance->pdata;
1717 if (plugin)
1718 Deployment::SetCurrent (plugin->GetDeployment ());
1720 return new MoonlightObject (instance);
1723 static void
1724 _set_deployment (NPObject *npobj)
1726 MoonlightObject *obj = (MoonlightObject *) npobj;
1727 PluginInstance *instance = (PluginInstance *)obj->instance->pdata;
1729 if (instance)
1730 Deployment::SetCurrent (instance->GetDeployment ());
1733 static void
1734 _deallocate (NPObject *npobj)
1736 _set_deployment (npobj);
1737 MoonlightObject *obj = (MoonlightObject *) npobj;
1739 delete obj;
1742 static void
1743 detach_xaml_proxy (gpointer key, gpointer value, gpointer closure)
1745 EventListenerProxy *proxy = (EventListenerProxy*)value;
1746 proxy->SetOwner (NULL);
1749 NPObject *
1750 moonlight_object_to_npobject (MoonlightObject *obj)
1752 return (NPObject *) obj;
1755 MoonlightObject *
1756 npobject_to_moonlight_object (NPObject *obj)
1758 return (MoonlightObject *) obj;
1761 MoonlightObject::~MoonlightObject ()
1763 if (event_listener_proxies) {
1764 g_hash_table_foreach (event_listener_proxies, detach_xaml_proxy, NULL);
1765 g_hash_table_destroy (event_listener_proxies);
1766 event_listener_proxies = NULL;
1770 void
1771 MoonlightObject::destroy_proxy (gpointer data)
1773 EventListenerProxy *proxy = (EventListenerProxy*)data;
1774 proxy->RemoveHandler ();
1777 bool
1778 MoonlightObject::HasProperty (NPIdentifier name)
1780 return IS_PROPERTY (LookupName (name));
1783 bool
1784 MoonlightObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
1786 NULL_TO_NPVARIANT (*result);
1787 THROW_JS_EXCEPTION ("AG_E_RUNTIME_GETVALUE");
1788 return true;
1791 bool
1792 MoonlightObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
1794 THROW_JS_EXCEPTION ("AG_E_RUNTIME_SETVALUE");
1795 return true;
1798 bool
1799 MoonlightObject::HasMethod (NPIdentifier name)
1801 return IS_METHOD (LookupName (name));
1804 bool
1805 MoonlightObject::Invoke (int id, NPIdentifier name, const NPVariant *args, guint32 argCount, NPVariant *result)
1807 switch (id) {
1808 case MoonId_ToString:
1809 if (argCount != 0)
1810 return false;
1812 if (moonlight_type != Type::INVALID) {
1813 string_to_npvariant (Type::Find (moonlight_type)->GetName (), result);
1814 return true;
1815 } else {
1816 //string_to_npvariant ("", result);
1817 NULL_TO_NPVARIANT (*result);
1818 return true;
1820 break;
1823 return false;
1827 EventListenerProxy *
1828 MoonlightObject::LookupEventProxy (int event_id)
1830 return (EventListenerProxy*)g_hash_table_lookup (event_listener_proxies, GINT_TO_POINTER (event_id));
1833 void
1834 MoonlightObject::SetEventProxy (EventListenerProxy *proxy)
1836 g_hash_table_insert (event_listener_proxies, GINT_TO_POINTER (proxy->GetEventId()), proxy);
1839 void
1840 MoonlightObject::ClearEventProxy (EventListenerProxy *proxy)
1842 proxy->SetOwner (NULL);
1844 #if SANITY
1845 EventListenerProxy *p = LookupEventProxy (proxy->GetEventId());
1846 if (!p)
1847 abort();
1848 #endif
1849 g_hash_table_remove (event_listener_proxies, GINT_TO_POINTER (proxy->GetEventId()));
1853 static void
1854 _invalidate (NPObject *npobj)
1856 _set_deployment (npobj);
1857 MoonlightObject *obj = (MoonlightObject *) npobj;
1859 obj->Invalidate ();
1862 static bool
1863 _has_method (NPObject *npobj, NPIdentifier name)
1865 _set_deployment (npobj);
1866 MoonlightObject *obj = (MoonlightObject *) npobj;
1867 return obj->HasMethod (name);
1870 static bool
1871 _has_property (NPObject *npobj, NPIdentifier name)
1873 _set_deployment (npobj);
1874 MoonlightObject *obj = (MoonlightObject *) npobj;
1875 return obj->HasProperty (name);
1878 static bool
1879 _get_property (NPObject *npobj, NPIdentifier name, NPVariant *result)
1881 _set_deployment (npobj);
1882 #if ds(!)0
1883 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
1884 printf ("getting object property %s\n", strname);
1885 NPN_MemFree (strname);
1886 #endif
1888 MoonlightObject *obj = (MoonlightObject *) npobj;
1889 int id = obj->LookupName (name);
1890 bool ret = obj->GetProperty (id, name, result);
1891 #if ds(!)0
1892 printf ("==>done ==> %p\n", NPVARIANT_TO_OBJECT (*result));
1893 #endif
1894 return ret;
1897 static bool
1898 _set_property (NPObject *npobj, NPIdentifier name, const NPVariant *value)
1900 _set_deployment (npobj);
1901 MoonlightObject *obj = (MoonlightObject *) npobj;
1902 int id = obj->LookupName (name);
1903 return obj->SetProperty (id, name, value);
1906 static bool
1907 _remove_property (NPObject *npobj, NPIdentifier name)
1909 _set_deployment (npobj);
1910 g_warning ("moonlight_object_remove_property reached");
1911 return false;
1914 static bool
1915 _enumerate (NPObject *npobj, NPIdentifier **value, guint32 *count)
1917 _set_deployment (npobj);
1918 return ((MoonlightObjectType*)npobj->_class)->Enumerate (value, count);
1921 static bool
1922 _invoke (NPObject *npobj, NPIdentifier name,
1923 const NPVariant *args, guint32 argCount,
1924 NPVariant *result)
1926 _set_deployment (npobj);
1927 MoonlightObject *obj = (MoonlightObject *) npobj;
1928 int id = obj->LookupName (name);
1929 return obj->Invoke (id, name, args, argCount, result);
1932 static bool
1933 _invoke_default (NPObject *npobj,
1934 const NPVariant *args, guint32 argCount,
1935 NPVariant *result)
1937 _set_deployment (npobj);
1938 g_warning ("moonlight_object_invoke_default reached");
1939 return false;
1942 static const MoonNameIdMapping
1943 object_mapping[] = {
1944 { "tostring", MoonId_ToString },
1947 MoonlightObjectType::MoonlightObjectType ()
1949 structVersion = 0; /* strictly not needed, but it prevents head-scratching due to uninitialized fields in gdb */
1950 allocate = _allocate;
1951 construct = NULL;
1952 deallocate = _deallocate;
1953 invalidate = _invalidate;
1954 hasMethod = _has_method;
1955 invoke = _invoke;
1956 invokeDefault = _invoke_default;
1957 hasProperty = _has_property;
1958 getProperty = _get_property;
1959 setProperty = _set_property;
1960 removeProperty = _remove_property;
1961 enumerate = _enumerate;
1963 mapping = NULL;
1964 mapping_count = 0;
1966 AddMapping (object_mapping, G_N_ELEMENTS (object_mapping));
1968 last_lookup = NULL;
1969 last_id = 0;
1972 bool
1973 MoonlightObjectType::Enumerate (NPIdentifier **value, guint32 *count)
1975 if (mapping_count == 0) {
1976 *value = NULL;
1977 *count = 0;
1978 return true;
1981 // caller frees this
1982 NPIdentifier *ids = (NPIdentifier*)NPN_MemAlloc (sizeof (NPIdentifier) * mapping_count);
1984 for (int i = 0; i < mapping_count; i ++)
1985 ids[i] = NPN_GetStringIdentifier (mapping[i].name);
1987 *count = mapping_count;
1988 *value = ids;
1990 return true;
1993 void
1994 MoonlightObjectType::AddMapping (const MoonNameIdMapping *mapping, int count)
1996 if (this->mapping) {
1997 MoonNameIdMapping *new_mapping = (MoonNameIdMapping *) g_new (MoonNameIdMapping, count + mapping_count);
1999 memmove (new_mapping, this->mapping, mapping_count * sizeof (MoonNameIdMapping));
2000 memmove ((char *) new_mapping + (mapping_count * sizeof (MoonNameIdMapping)), mapping, count * sizeof (MoonNameIdMapping));
2001 g_free (this->mapping);
2002 this->mapping = new_mapping;
2003 mapping_count += count;
2004 } else {
2005 this->mapping = (MoonNameIdMapping *) g_new (MoonNameIdMapping, count);
2007 memmove (this->mapping, mapping, count * sizeof (MoonNameIdMapping));
2008 mapping_count = count;
2011 qsort (this->mapping, mapping_count, sizeof (MoonNameIdMapping), compare_mapping);
2015 MoonlightObjectType::LookupName (NPIdentifier name)
2017 if (last_lookup == name)
2018 return last_id;
2020 int id = map_name_to_id (name, mapping, mapping_count);
2022 if (id) {
2023 /* only cache hits */
2024 last_lookup = name;
2025 last_id = id;
2028 return id;
2031 MoonlightObjectType *MoonlightObjectClass;
2033 /*** MoonlightScriptControlClass **********************************************************/
2034 static NPObject *
2035 moonlight_scriptable_control_allocate (NPP instance, NPClass *klass)
2037 return new MoonlightScriptControlObject (instance);
2040 static const MoonNameIdMapping
2041 scriptable_control_mapping[] = {
2042 { "content", MoonId_Content },
2043 { "isloaded", MoonId_IsLoaded },
2044 { "createobject", MoonId_CreateObject },
2045 { "initparams", MoonId_InitParams },
2046 { "id", MoonId_Id },
2047 { "isversionsupported", MoonId_IsVersionSupported },
2048 { "onerror", MoonId_OnError },
2049 { "onload", MoonId_OnLoad },
2050 { "settings", MoonId_Settings },
2051 { "source", MoonId_Source },
2052 { "onsourcedownloadprogresschanged", MoonId_OnSourceDownloadProgressChanged },
2053 { "onsourcedownloadcomplete", MoonId_OnSourceDownloadComplete },
2056 MoonlightScriptControlObject::~MoonlightScriptControlObject ()
2058 if (settings) {
2059 NPN_ReleaseObject (settings);
2060 settings = NULL;
2063 if (content) {
2064 NPN_ReleaseObject (content);
2065 content = NULL;
2069 void
2070 MoonlightScriptControlObject::Invalidate ()
2072 MoonlightObject::Invalidate ();
2074 settings = NULL;
2075 content = NULL;
2078 bool
2079 MoonlightScriptControlObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
2081 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2083 switch (id) {
2084 case MoonId_Settings:
2085 NPN_RetainObject (settings);
2086 OBJECT_TO_NPVARIANT (settings, *result);
2087 return true;
2088 case MoonId_Content:
2089 NPN_RetainObject (content);
2090 OBJECT_TO_NPVARIANT (content, *result);
2091 return true;
2092 case MoonId_InitParams:
2093 string_to_npvariant (plugin->GetInitParams (), result);
2094 return true;
2095 case MoonId_IsLoaded:
2096 BOOLEAN_TO_NPVARIANT (plugin->IsLoaded(), *result);
2097 return true;
2098 case MoonId_OnError:
2099 case MoonId_OnLoad:
2100 case MoonId_OnSourceDownloadProgressChanged:
2101 case MoonId_OnSourceDownloadComplete: {
2102 const char *event_name = map_moon_id_to_event_name (id);
2103 EventObject *obj = plugin->GetSurface ();
2105 if (obj != NULL) {
2106 int event_id = obj->GetType()->LookupEvent (event_name);
2107 EventListenerProxy *proxy = LookupEventProxy (event_id);
2108 string_to_npvariant (proxy == NULL ? "" : proxy->GetCallbackAsString (), result);
2109 } else {
2110 string_to_npvariant ("", result);
2112 return true;
2114 case MoonId_Source:
2115 string_to_npvariant (plugin->GetSource (), result);
2116 return true;
2118 case MoonId_Id: {
2119 char *id = plugin->GetId ();
2120 if (id)
2121 string_to_npvariant (id, result);
2122 else
2123 NULL_TO_NPVARIANT (*result);
2125 return true;
2128 default:
2129 return MoonlightObject::GetProperty (id, name, result);
2133 bool
2134 MoonlightScriptControlObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
2136 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2138 switch (id) {
2139 case MoonId_Source: {
2140 char *source = STRDUP_FROM_VARIANT (*value);
2141 plugin->SetSource (source);
2142 g_free (source);
2143 return true;
2145 case MoonId_OnError:
2146 case MoonId_OnLoad:
2147 case MoonId_OnSourceDownloadProgressChanged:
2148 case MoonId_OnSourceDownloadComplete: {
2149 const char *event_name = map_moon_id_to_event_name (id);
2150 EventObject *obj = plugin->GetSurface ();
2152 if (obj != NULL) {
2153 int event_id = obj->GetType()->LookupEvent (event_name);
2155 if (event_id != -1) {
2156 EventListenerProxy *proxy = LookupEventProxy (event_id);
2157 if (proxy)
2158 proxy->RemoveHandler ();
2160 if (!NPVARIANT_IS_NULL (*value)) {
2161 EventListenerProxy *proxy = new EventListenerProxy (instance,
2162 event_name,
2163 value);
2164 proxy->SetOwner (this);
2165 proxy->AddHandler (plugin->GetSurface());
2166 // we only emit that event once, when
2167 // the plugin is initialized, so don't
2168 // leave it in the event list
2169 // afterward.
2170 if (id == MoonId_OnLoad)
2171 proxy->SetOneShot ();
2172 SetEventProxy (proxy);
2175 return true;
2178 return false;
2180 case MoonId_InitParams: {
2181 char *init_params = STRDUP_FROM_VARIANT (*value);
2182 plugin->SetInitParams (init_params);
2183 g_free (init_params);
2184 return true;
2186 default:
2187 return MoonlightObject::SetProperty (id, name, value);
2191 bool
2192 MoonlightScriptControlObject::Invoke (int id, NPIdentifier name,
2193 const NPVariant *args, guint32 argCount,
2194 NPVariant *result)
2196 switch (id) {
2197 case MoonId_CreateObject: {
2198 if (!check_arg_list ("s", argCount, args)) {
2199 NULL_TO_NPVARIANT (*result);
2200 return true;
2203 NPObject *obj = NULL;
2204 char *object_type = STRDUP_FROM_VARIANT (args [0]);
2205 if (!g_ascii_strcasecmp ("downloader", object_type)) {
2206 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2207 Downloader *dl = PluginInstance::CreateDownloader (plugin);
2209 obj = EventObjectCreateWrapper (instance, dl);
2210 dl->unref ();
2212 OBJECT_TO_NPVARIANT (obj, *result);
2213 g_free (object_type);
2214 return true;
2215 } else {
2216 NULL_TO_NPVARIANT (*result);
2217 g_free (object_type);
2219 THROW_JS_EXCEPTION ("createObject");
2220 return true;
2224 case MoonId_IsVersionSupported: {
2225 if (!check_arg_list ("s", argCount, args))
2226 return false;
2228 gchar *version_list = STRDUP_FROM_VARIANT (args [0]);
2229 bool supported = Surface::IsVersionSupported (version_list);
2230 g_free (version_list);
2232 BOOLEAN_TO_NPVARIANT (supported, *result);
2234 return true;
2237 default:
2238 return MoonlightObject::Invoke (id, name, args, argCount, result);
2242 MoonlightScriptControlType::MoonlightScriptControlType ()
2244 allocate = moonlight_scriptable_control_allocate;
2246 AddMapping (scriptable_control_mapping, G_N_ELEMENTS (scriptable_control_mapping));
2249 MoonlightScriptControlType *MoonlightScriptControlClass;
2251 /*** MoonlightSettingsClass ***********************************************************/
2253 static NPObject *
2254 moonlight_settings_allocate (NPP instance, NPClass *klass)
2256 return new MoonlightSettingsObject (instance);
2259 static const MoonNameIdMapping
2260 moonlight_settings_mapping [] = {
2261 { "background", MoonId_Background },
2262 { "enableframeratecounter", MoonId_EnableFramerateCounter },
2263 { "enablehtmlaccess", MoonId_EnableHtmlAccess },
2264 { "enableredrawregions", MoonId_EnableRedrawRegions },
2265 { "maxframerate", MoonId_MaxFrameRate },
2266 { "version", MoonId_Version },
2267 { "windowless", MoonId_Windowless }
2270 bool
2271 MoonlightSettingsObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
2273 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2275 switch (id) {
2276 case MoonId_Background:
2277 string_to_npvariant (plugin->GetBackground (), result);
2278 return true;
2280 case MoonId_EnableFramerateCounter:
2281 BOOLEAN_TO_NPVARIANT (plugin->GetEnableFramerateCounter (), *result);
2282 return true;
2284 case MoonId_EnableRedrawRegions:
2285 BOOLEAN_TO_NPVARIANT (plugin->GetEnableRedrawRegions (), *result);
2286 return true;
2288 case MoonId_EnableHtmlAccess:
2289 BOOLEAN_TO_NPVARIANT (plugin->GetEnableHtmlAccess (), *result);
2290 return true;
2292 case MoonId_MaxFrameRate:
2293 INT32_TO_NPVARIANT (plugin->GetMaxFrameRate (), *result);
2294 return true;
2296 case MoonId_Version:
2297 string_to_npvariant (PLUGIN_VERSION, result);
2298 return true;
2300 case MoonId_Windowless:
2301 BOOLEAN_TO_NPVARIANT (plugin->GetWindowless (), *result);
2302 return true;
2304 default:
2305 return MoonlightObject::GetProperty (id, name, result);
2309 bool
2310 MoonlightSettingsObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
2312 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2314 switch (id) {
2316 case MoonId_Background: {
2317 char *color = STRDUP_FROM_VARIANT (*value);
2318 if (!plugin->SetBackground (color)) {
2319 g_free (color);
2320 THROW_JS_EXCEPTION ("AG_E_RUNTIME_SETVALUE");
2322 g_free (color);
2324 return true;
2326 // Cant be set after initialization so return true
2327 case MoonId_EnableFramerateCounter:
2328 plugin->SetEnableFramerateCounter (NPVARIANT_TO_BOOLEAN (*value));
2329 return true;
2331 case MoonId_EnableRedrawRegions:
2332 plugin->SetEnableRedrawRegions (NPVARIANT_TO_BOOLEAN (*value));
2333 return true;
2335 // Cant be set after initialization so return true
2336 case MoonId_EnableHtmlAccess:
2337 return true;
2339 // not implemented yet.
2340 case MoonId_MaxFrameRate:
2341 plugin->SetMaxFrameRate (NPVARIANT_TO_INT32 (*value));
2342 return true;
2344 // Cant be set after initialization so return true
2345 case MoonId_Windowless:
2346 return true;
2347 default:
2348 return MoonlightObject::SetProperty (id, name, value);
2352 bool
2353 MoonlightSettingsObject::Invoke (int id, NPIdentifier name,
2354 const NPVariant *args, guint32 argCount, NPVariant *result)
2356 switch (id) {
2357 case MoonId_ToString:
2358 if (argCount != 0)
2359 return false;
2361 string_to_npvariant ("Settings", result);
2362 return true;
2363 default:
2364 return MoonlightObject::Invoke (id, name, args, argCount, result);
2368 MoonlightSettingsType::MoonlightSettingsType ()
2370 allocate = moonlight_settings_allocate;
2371 AddMapping (moonlight_settings_mapping, G_N_ELEMENTS (moonlight_settings_mapping));
2374 MoonlightSettingsType *MoonlightSettingsClass;
2377 /*** MoonlightContentClass ************************************************************/
2378 static NPObject *
2379 moonlight_content_allocate (NPP instance, NPClass *klass)
2381 return new MoonlightContentObject (instance);
2384 MoonlightContentObject::~MoonlightContentObject ()
2386 // FIXME: need to free registered scriptable objects
2387 if (registered_scriptable_objects) {
2388 g_hash_table_destroy (registered_scriptable_objects);
2389 registered_scriptable_objects = NULL;
2391 if (accessibility)
2392 accessibility->unref();
2395 static const MoonNameIdMapping
2396 moonlight_content_mapping[] = {
2397 { "accessibility", MoonId_Accessibility },
2398 { "actualheight", MoonId_ActualHeight },
2399 { "actualwidth", MoonId_ActualWidth },
2400 { "createfromxaml", MoonId_CreateFromXaml },
2401 { "createfromxamldownloader", MoonId_CreateFromXamlDownloader },
2402 { "createobject", MoonId_CreateObject },
2403 { "findname", MoonId_FindName },
2404 { "fullscreen", MoonId_FullScreen },
2405 { "onfullscreenchange", MoonId_OnFullScreenChange },
2406 { "onresize", MoonId_OnResize },
2407 { "root", MoonId_Root },
2410 bool
2411 MoonlightContentObject::HasProperty (NPIdentifier name)
2413 #if ds(!)0
2414 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
2415 printf ("content has property %s\n", strname);
2416 NPN_MemFree (strname);
2417 #endif
2419 if (MoonlightObject::HasProperty (name))
2420 return true;
2422 bool ret = g_hash_table_lookup (registered_scriptable_objects, name) != NULL;
2423 return ret;
2426 bool
2427 MoonlightContentObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
2429 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2431 switch (id) {
2432 case MoonId_Accessibility: {
2433 if (!accessibility)
2434 accessibility = new Accessibility ();
2435 MoonlightEventObjectObject *acc = EventObjectCreateWrapper (instance, accessibility);
2437 OBJECT_TO_NPVARIANT (acc, *result);
2438 return true;
2440 case MoonId_ActualHeight:
2441 INT32_TO_NPVARIANT (plugin->GetActualHeight (), *result);
2442 return true;
2443 case MoonId_ActualWidth:
2444 INT32_TO_NPVARIANT (plugin->GetActualWidth (), *result);
2445 return true;
2446 case MoonId_FullScreen:
2447 if (!plugin->GetSurface ()) {
2448 BOOLEAN_TO_NPVARIANT (false, *result);
2449 } else {
2450 BOOLEAN_TO_NPVARIANT (plugin->GetSurface()->GetFullScreen (), *result);
2452 return true;
2453 case MoonId_OnResize:
2454 case MoonId_OnFullScreenChange: {
2455 Surface *surface = plugin->GetSurface ();
2456 const char *event_name;
2457 int event_id;
2459 if (surface == NULL) {
2460 string_to_npvariant ("", result);
2461 } else {
2462 event_name = map_moon_id_to_event_name (id);
2463 event_id = surface->GetType()->LookupEvent (event_name);
2464 EventListenerProxy *proxy = LookupEventProxy (event_id);
2465 string_to_npvariant (proxy == NULL ? "" : proxy->GetCallbackAsString (), result);
2467 return true;
2469 case MoonId_Root: {
2470 Surface *surface = plugin->GetSurface ();
2471 DependencyObject *top;
2473 if (surface == NULL) {
2474 NULL_TO_NPVARIANT (*result);
2475 } else if ((top = surface->GetToplevel ()) == NULL) {
2476 NULL_TO_NPVARIANT (*result);
2477 } else {
2478 MoonlightEventObjectObject *topobj = EventObjectCreateWrapper (instance, top);
2480 OBJECT_TO_NPVARIANT (topobj, *result);
2482 return true;
2484 case NoMapping: {
2485 MoonlightScriptableObjectObject *obj;
2486 gpointer val;
2488 if (!(val = g_hash_table_lookup (registered_scriptable_objects, name)))
2489 return false;
2491 obj = (MoonlightScriptableObjectObject *) val;
2493 NPN_RetainObject (obj);
2494 OBJECT_TO_NPVARIANT (obj, *result);
2495 return true;
2497 default:
2498 return MoonlightObject::GetProperty (id, name, result);
2502 bool
2503 MoonlightContentObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
2505 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2506 Surface *surface = NULL;
2508 switch (id) {
2509 case MoonId_Accessibility:
2510 THROW_JS_EXCEPTION ("AG_E_RUNTIME_SETVALUE");
2511 break;
2512 case MoonId_FullScreen:
2513 surface = plugin->GetSurface ();
2514 if (surface != NULL)
2515 surface->SetFullScreen (NPVARIANT_TO_BOOLEAN (*value));
2516 return true;
2517 case MoonId_OnFullScreenChange:
2518 case MoonId_OnResize: {
2519 const char *event_name = map_moon_id_to_event_name (id);
2520 int event_id;
2522 surface = plugin->GetSurface ();
2523 if (surface == NULL)
2524 return true;
2526 event_id = surface->GetType()->LookupEvent (event_name);
2528 if (event_id != -1) {
2529 EventListenerProxy *proxy = LookupEventProxy (event_id);
2530 if (proxy)
2531 proxy->RemoveHandler();
2533 if (!NPVARIANT_IS_NULL (*value)) {
2534 EventListenerProxy *proxy = new EventListenerProxy (instance,
2535 event_name,
2536 value);
2537 proxy->SetOwner (this);
2538 proxy->AddHandler (plugin->GetSurface());
2539 SetEventProxy (proxy);
2542 return true;
2545 default:
2546 return MoonlightObject::SetProperty (id, name, value);
2550 bool
2551 MoonlightContentObject::Invoke (int id, NPIdentifier name,
2552 const NPVariant *args, guint32 argCount, NPVariant *result)
2554 PluginInstance *plugin = (PluginInstance*) instance->pdata;
2556 switch (id) {
2557 case MoonId_FindName: {
2558 if (!check_arg_list ("s", argCount, args))
2559 THROW_JS_EXCEPTION ("AG_E_RUNTIME_FINDNAME");
2561 if (plugin->IsCrossDomainApplication ())
2562 THROW_JS_EXCEPTION ("XDomain Restriction");
2564 if (!plugin->GetSurface() || !plugin->GetSurface()->GetToplevel ())
2565 return true;
2567 char *name = STRDUP_FROM_VARIANT (args [0]);
2568 DependencyObject *element = plugin->GetSurface()->GetToplevel ()->FindName (name);
2569 g_free (name);
2571 if (!element) {
2572 NULL_TO_NPVARIANT (*result);
2573 return true;
2576 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, element), *result);
2577 return true;
2580 case MoonId_CreateObject:
2581 // not implemented yet
2582 DEBUG_WARN_NOTIMPLEMENTED ("content.createObject");
2583 return true;
2585 case MoonId_CreateFromXaml: {
2586 if (!check_arg_list ("s[b]", argCount, args))
2587 THROW_JS_EXCEPTION ("createFromXaml argException");
2589 bool create_namescope = argCount >= 2 ? NPVARIANT_TO_BOOLEAN (args[1]) : false;
2590 char *xaml = STRDUP_FROM_VARIANT (args[0]);
2592 if (!xaml)
2593 THROW_JS_EXCEPTION ("createFromXaml argNullException");
2595 Type::Kind element_type;
2596 MoonError error;
2597 DependencyObject *dep = NULL;
2598 XamlLoader *loader = PluginXamlLoader::FromStr (NULL/*FIXME*/, xaml, plugin, plugin->GetSurface());
2600 loader->LoadVM ();
2602 Value *val = loader->CreateFromStringWithError (xaml, create_namescope, &element_type, XamlLoader::IMPORT_DEFAULT_XMLNS, &error);
2603 if (val && val->Is (Type::DEPENDENCY_OBJECT))
2604 dep = val->AsDependencyObject ();
2606 delete loader;
2607 g_free (xaml);
2609 if (!dep) {
2610 char *msg = g_strdup_printf ("createFromXaml error: %s", error.message);
2611 THROW_JS_EXCEPTION (msg);
2612 g_free (msg);
2615 MoonlightEventObjectObject *depobj = EventObjectCreateWrapper (instance, dep);
2616 delete val;
2618 OBJECT_TO_NPVARIANT (depobj, *result);
2619 return true;
2622 case MoonId_CreateFromXamlDownloader: {
2623 if (!check_arg_list ("os", argCount, args))
2624 THROW_JS_EXCEPTION ("createFromXamlDownloader");
2626 Downloader *down = (Downloader*)((MoonlightDependencyObjectObject*) NPVARIANT_TO_OBJECT (args [0]))->GetDependencyObject ();
2627 DependencyObject *dep = NULL;
2628 Type::Kind element_type;
2630 char *path = STRDUP_FROM_VARIANT (args [1]);
2631 char *fname = down->GetDownloadedFilename (path);
2632 g_free (path);
2634 if (fname != NULL) {
2635 XamlLoader *loader = PluginXamlLoader::FromFilename (NULL/*FIXME*/, fname, plugin, plugin->GetSurface());
2636 dep = loader->CreateDependencyObjectFromFile (fname, false, &element_type);
2637 delete loader;
2639 g_free (fname);
2642 if (!dep)
2643 THROW_JS_EXCEPTION ("createFromXamlDownloader");
2645 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, dep), *result);
2646 dep->unref ();
2647 return true;
2650 case MoonId_ToString: {
2651 if (argCount != 0)
2652 return false;
2654 string_to_npvariant ("Content", result);
2655 return true;
2658 default:
2659 return MoonlightObject::Invoke (id, name, args, argCount, result);
2663 MoonlightContentType::MoonlightContentType ()
2665 allocate = moonlight_content_allocate;
2667 AddMapping (moonlight_content_mapping, G_N_ELEMENTS (moonlight_content_mapping));
2670 MoonlightContentType *MoonlightContentClass;
2674 /*** MoonlightDependencyObjectClass ***************************************************/
2676 static const MoonNameIdMapping
2677 moonlight_dependency_object_mapping [] = {
2678 { "addeventlistener", MoonId_AddEventListener },
2679 #if DEBUG_JAVASCRIPT
2680 { "dumpnamescope", MoonId_DumpNameScope },
2681 #endif
2682 { "equals", MoonId_Equals },
2683 { "findname", MoonId_FindName },
2684 { "gethost", MoonId_GetHost },
2685 { "getparent", MoonId_GetParent },
2686 { "getvalue", MoonId_GetValue },
2687 { "gotfocus", MoonId_GotFocus },
2688 { "keydown", MoonId_KeyDown },
2689 { "keyup", MoonId_KeyUp },
2690 { "loaded", MoonId_Loaded },
2691 { "lostfocus", MoonId_LostFocus },
2692 { "mouseenter", MoonId_MouseEnter },
2693 { "mouseleave", MoonId_MouseLeave },
2694 { "mouseleftbuttondown", MoonId_MouseLeftButtonDown },
2695 { "mouseleftbuttonup", MoonId_MouseLeftButtonUp },
2696 { "mousemove", MoonId_MouseMove },
2697 #if DEBUG_JAVASCRIPT
2698 { "printf", MoonId_Printf },
2699 #endif
2700 { "removeeventlistener", MoonId_RemoveEventListener },
2701 { "setvalue", MoonId_SetValue },
2704 static NPObject *
2705 moonlight_dependency_object_allocate (NPP instance, NPClass *klass)
2707 return new MoonlightDependencyObjectObject (instance);
2710 static DependencyProperty *
2711 _get_dependency_property (DependencyObject *obj, char *attrname)
2713 // don't need to downcase here since dependency property lookup is already case insensitive
2714 DependencyProperty *p = obj->GetDependencyProperty (attrname);
2716 if (p)
2717 return p;
2719 char *period = strchr (attrname, '.');
2720 if (period) {
2721 char *type_name = g_strndup (attrname, period-attrname);
2722 attrname = period + 1;
2724 Type *type = Type::Find (type_name);
2726 if (type != NULL)
2727 p = DependencyProperty::GetDependencyProperty (type->GetKind (), attrname);
2729 g_free (type_name);
2732 return p;
2735 static bool
2736 _set_dependency_property_value (DependencyObject *dob, DependencyProperty *prop, const NPVariant *value, MoonError *error)
2738 if (npvariant_is_moonlight_object (*value)) {
2739 MoonlightObject *obj = (MoonlightObject *) NPVARIANT_TO_OBJECT (*value);
2740 MoonlightDuration *duration;
2741 MoonlightTimeSpan *ts;
2742 MoonlightPoint *point;
2743 MoonlightRect *rect;
2745 if (Type::IsSubclassOf (obj->moonlight_type, Type::DEPENDENCY_OBJECT) && obj->moonlight_type != Type::INVALID) {
2746 MoonlightDependencyObjectObject *depobj = (MoonlightDependencyObjectObject*) NPVARIANT_TO_OBJECT (*value);
2747 dob->SetValueWithError (prop, Value (depobj->GetDependencyObject ()), error);
2749 return error->number == 0;
2752 switch (obj->moonlight_type) {
2753 case Type::TIMESPAN:
2754 ts = (MoonlightTimeSpan *) obj;
2755 dob->SetValue (prop, Value (ts->GetValue (), Type::TIMESPAN));
2756 break;
2757 case Type::DURATION:
2758 duration = (MoonlightDuration *) obj;
2759 dob->SetValue (prop, Value (duration->GetValue ()));
2760 break;
2761 case Type::RECT:
2762 rect = (MoonlightRect *) obj;
2763 dob->SetValue (prop, Value (rect->rect));
2764 break;
2765 case Type::POINT:
2766 point = (MoonlightPoint *) obj;
2767 dob->SetValue (prop, Value (point->point));
2768 break;
2769 default:
2770 d(printf ("unhandled object type %d - %s in do.set_property\n",
2771 obj->moonlight_type, Type::Find (obj->moonlight_type)->GetName ()));
2772 w(printf ("unhandled object type in do.set_property\n"));
2773 return true;
2775 } else {
2776 char *strval = NULL;
2777 char strbuf[64];
2778 bool rv;
2780 if (NPVARIANT_IS_BOOLEAN (*value)) {
2781 if (NPVARIANT_TO_BOOLEAN (*value))
2782 strcpy (strbuf, "true");
2783 else
2784 strcpy (strbuf, "false");
2786 strval = strbuf;
2787 } else if (NPVARIANT_IS_INT32 (*value)) {
2788 g_snprintf (strbuf, sizeof (strbuf), "%d", NPVARIANT_TO_INT32 (*value));
2790 strval = strbuf;
2791 } else if (NPVARIANT_IS_DOUBLE (*value)) {
2792 g_ascii_dtostr (strbuf, sizeof (strbuf), NPVARIANT_TO_DOUBLE (*value));
2794 strval = strbuf;
2795 } else if (NPVARIANT_IS_STRING (*value)) {
2796 strval = STRDUP_FROM_VARIANT (*value);
2797 } else if (NPVARIANT_IS_NULL (*value)) {
2798 if (Type::IsSubclassOf (prop->GetPropertyType(), Type::DEPENDENCY_OBJECT)) {
2799 DependencyObject *val = NULL;
2801 dob->SetValueWithError (prop, Value (val), error);
2802 } else if (prop->GetPropertyType() == Type::STRING) {
2803 char *val = NULL;
2805 dob->SetValueWithError (prop, Value (val), error);
2806 } else
2807 dob->SetValueWithError (prop, NULL, error);
2809 return error->number == 0;
2810 } else if (NPVARIANT_IS_VOID (*value)) {
2811 d(printf ("unhandled variant type VOID in do.set_property for (%s::%s)\n",
2812 dob->GetTypeName (), prop->GetName()));
2813 return true;
2814 } else {
2815 d(printf ("unhandled variant type in do.set_property for (%s::%s)\n",
2816 dob->GetTypeName (), prop->GetName()));
2817 return true;
2820 rv = xaml_set_property_from_str (dob, prop, strval, error);
2822 if (strval != strbuf)
2823 g_free (strval);
2825 return rv;
2828 return true;
2832 bool
2833 MoonlightDependencyObjectObject::HasProperty (NPIdentifier name)
2835 if (MoonlightObject::HasProperty (name))
2836 return true;
2838 DependencyObject *dob = GetDependencyObject ();
2840 // don't need to downcase here since dependency property lookup is already case insensitive
2841 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
2842 if (!strname)
2843 return false;
2845 DependencyProperty *p = _get_dependency_property (dob, strname);
2846 NPN_MemFree (strname);
2848 return (p != NULL);
2851 bool
2852 MoonlightDependencyObjectObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
2854 // don't need to downcase here since dependency property lookup is already case insensitive
2855 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
2856 DependencyObject *dob = GetDependencyObject ();
2857 DependencyProperty *prop;
2858 const char *event_name;
2859 int event_id;
2861 if (!strname)
2862 return false;
2864 prop = _get_dependency_property (dob, strname);
2865 NPN_MemFree (strname);
2867 if (prop) {
2868 Value *value;
2869 // some default values are different between SL1 and SL2 (even if the SL1 apps runs under SL2)
2870 if (prop->GetId () == UIElement::RenderTransformProperty) {
2871 // e.g. the default RenderTransform is NULL for Javascript, unless it was setted by application code
2872 value = dob->ReadLocalValue (prop);
2873 } else if ((prop->GetId () == FrameworkElement::HeightProperty) || (prop->GetId () == FrameworkElement::WidthProperty)) {
2874 // e.g. the Width and Height are NaN in SL2 and 0 in Javascript (unless set to NaN in managed code)
2875 value = dob->ReadLocalValue (prop);
2876 if (!value) {
2877 DOUBLE_TO_NPVARIANT (0.0, *result);
2878 return true;
2880 } else if (prop->GetId () == MediaElement::CurrentStateProperty) {
2881 // Javascript applications use strings, while managed use an enum
2882 int enum_value = dob->GetValue (prop)->AsInt32 ();
2883 const char *name = enums_int_to_str ("MediaState", enum_value);
2884 string_to_npvariant (name, result);
2885 return true;
2886 } else {
2887 value = dob->GetValue (prop);
2890 if (!value) {
2891 // strings aren't null, they seem to just be empty strings
2892 if (prop->GetPropertyType() == Type::STRING) {
2893 string_to_npvariant ("", result);
2894 return true;
2897 NULL_TO_NPVARIANT (*result);
2898 return true;
2901 if (value->GetKind () == Type::INT32) {
2902 const char *s = enums_int_to_str (prop->GetName(), value->AsInt32 ());
2903 if (s)
2904 string_to_npvariant (s, result);
2905 else
2906 value_to_variant (this, value, result, dob, prop);
2907 } else
2908 value_to_variant (this, value, result, dob, prop);
2910 return true;
2913 // it wasn't a dependency property. let's see if it's an
2914 // event, and hook it up if it is valid on this object.
2915 if (!(event_name = map_moon_id_to_event_name (id)))
2916 return MoonlightObject::GetProperty (id, name, result);
2918 if ((event_id = dob->GetType()->LookupEvent (event_name)) == -1) {
2919 #if false
2920 EventListenerProxy *proxy = LookupEventProxy (event_id);
2921 string_to_npvariant (proxy == NULL ? "" : proxy->GetCallbackAsString (), result);
2922 return true;
2923 #else
2924 // on silverlight, these seem to always return ""
2925 // regardless of how we attempt to set them.
2926 string_to_npvariant ("", result);
2927 return true;
2928 #endif
2931 return MoonlightObject::GetProperty (id, name, result);
2934 bool
2935 MoonlightDependencyObjectObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
2937 // don't need to downcase here since dependency property lookup is already case insensitive
2938 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
2939 DependencyObject *dob = GetDependencyObject ();
2940 DependencyProperty *prop;
2942 if (!strname)
2943 return false;
2945 prop = _get_dependency_property (dob, strname);
2946 NPN_MemFree (strname);
2948 if (prop) {
2949 MoonError error;
2950 if (_set_dependency_property_value (dob, prop, value, &error)) {
2951 return true;
2952 } else {
2953 THROW_JS_EXCEPTION ("AG_E_RUNTIME_SETVALUE");
2957 return MoonlightObject::SetProperty (id, name, value);
2960 bool
2961 MoonlightDependencyObjectObject::Invoke (int id, NPIdentifier name,
2962 const NPVariant *args, guint32 argCount,
2963 NPVariant *result)
2965 DependencyObject *dob = GetDependencyObject ();
2967 switch (id) {
2968 #if DEBUG_JAVASCRIPT
2969 // Some debug code...
2970 // with this it is possible to do obj.printf ("msg") from js
2971 case MoonId_Printf: {
2972 char *message = STRDUP_FROM_VARIANT (args [0]);
2973 fprintf (stderr, "JS message: %s\n", message);
2974 g_free (message);
2975 VOID_TO_NPVARIANT (*result);
2976 return true;
2978 case MoonId_DumpNameScope: {
2979 fprintf (stderr, "dumping namescope for object %p (%s)\n", dob, dob->GetTypeName());
2980 DependencyObject *ns_dob = dob;
2981 NameScope *ns;
2982 while (!(ns = NameScope::GetNameScope(ns_dob)))
2983 ns_dob = ns_dob->GetParent();
2984 if (ns_dob == NULL)
2985 fprintf (stderr, " no namescope in logical hierarchy!\n");
2986 else {
2987 if (ns_dob != dob)
2988 fprintf (stderr, "namescope is actually on object %p (%s)\n", ns_dob, ns_dob->GetTypeName());
2989 ns->Dump ();
2991 return true;
2993 #endif
2994 case MoonId_Equals: {
2995 if (!check_arg_list ("o", argCount, args))
2996 THROW_JS_EXCEPTION ("equals");
2998 NPObject *o = NPVARIANT_TO_OBJECT (args[0]);
2999 if (npobject_is_dependency_object (o)) {
3000 MoonlightDependencyObjectObject *obj = (MoonlightDependencyObjectObject *) o;
3002 BOOLEAN_TO_NPVARIANT (obj->GetDependencyObject() == dob, *result);
3003 } else {
3004 BOOLEAN_TO_NPVARIANT (false, *result);
3007 return true;
3010 case MoonId_SetValue: {
3011 /* obj.setValue (prop, val) is another way of writing obj[prop] = val (or obj.prop = val) */
3012 if (!check_arg_list ("s*", argCount, args))
3013 THROW_JS_EXCEPTION ("setValue");
3015 char *value = STRDUP_FROM_VARIANT (args [0]);
3016 _class->setProperty (this, NPID (value), &args[1]);
3017 g_free (value);
3019 VOID_TO_NPVARIANT (*result);
3020 return true;
3023 case MoonId_GetValue: {
3024 if (!check_arg_list ("s", argCount, args))
3025 THROW_JS_EXCEPTION ("getValue");
3027 char *value = STRDUP_FROM_VARIANT (args [0]);
3028 _class->getProperty (this, NPID (value), result);
3029 g_free (value);
3031 return true;
3034 case MoonId_FindName: {
3035 if (!check_arg_list ("s", argCount, args))
3036 THROW_JS_EXCEPTION ("AG_E_RUNTIME_FINDNAME");
3038 PluginInstance *plugin = (PluginInstance*) instance->pdata;
3039 if (plugin->IsCrossDomainApplication ())
3040 THROW_JS_EXCEPTION ("XDomain Restriction");
3042 char *name = STRDUP_FROM_VARIANT (args [0]);
3044 DependencyObject *element = dob->FindName (name);
3045 if (!element) {
3046 if (dob != plugin->GetSurface()->GetToplevel()) {
3047 // fall back to looking the object up on the toplevel element
3048 element = plugin->GetSurface()->GetToplevel ()->FindName (name);
3051 if (!element) {
3052 g_free (name);
3053 NULL_TO_NPVARIANT (*result);
3054 return true;
3058 g_free (name);
3059 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, element), *result);
3060 return true;
3063 case MoonId_GetHost: {
3064 PluginInstance *plugin = (PluginInstance*) instance->pdata;
3066 if (argCount != 0)
3067 THROW_JS_EXCEPTION ("AG_E_RUNTIME_GETHOST");
3069 OBJECT_TO_NPVARIANT (plugin->GetHost (), *result);
3071 return true;
3074 case MoonId_GetParent: {
3075 if (argCount != 0 || !dob->GetType ()->IsSubclassOf (Type::FRAMEWORKELEMENT))
3076 THROW_JS_EXCEPTION ("AG_E_RUNTIME_GETPARENT");
3078 DependencyObject *parent = ((FrameworkElement*)dob)->GetLogicalParent ();
3079 if (parent)
3080 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, parent), *result);
3081 else
3082 NULL_TO_NPVARIANT (*result);
3084 return true;
3087 case MoonId_AddEventListener: {
3088 /* FIXME: how do we check if args[1] is a function? */
3089 if (!check_arg_list ("s(so)", argCount, args))
3090 THROW_JS_EXCEPTION ("addEventListener");
3092 char *name = STRDUP_FROM_VARIANT (args [0]);
3093 name[0] = toupper(name[0]);
3095 EventListenerProxy *proxy = new EventListenerProxy (instance, name, &args[1]);
3096 int token = proxy->AddHandler (dob);
3097 g_free (name);
3099 if (token == -1)
3100 THROW_JS_EXCEPTION ("AG_E_RUNTIME_ADDEVENT");
3102 INT32_TO_NPVARIANT (token, *result);
3103 return true;
3105 case MoonId_RemoveEventListener: {
3106 if (!check_arg_list ("s(is)", argCount, args))
3107 THROW_JS_EXCEPTION ("removeEventListener");
3109 char *event = STRDUP_FROM_VARIANT (args[0]);
3110 int id = dob->GetType()->LookupEvent (event);
3111 g_free (event);
3113 if (id == -1) {
3114 THROW_JS_EXCEPTION ("AG_E_RUNTIME_DELEVENT");
3115 } else if (NPVARIANT_IS_INT32 (args [1])) {
3116 dob->RemoveHandler (id, NPVARIANT_TO_INT32 (args[1]));
3117 } else if (NPVARIANT_IS_STRING (args[1])) {
3118 char *value = STRDUP_FROM_VARIANT (args[1]);
3119 NamedProxyPredicate predicate (value);
3120 g_free (value);
3122 dob->RemoveMatchingHandlers (id, NamedProxyPredicate::matches, &predicate);
3125 return true;
3128 // FIXME: these next two methods should live in a UIElement
3129 // wrapper class, not in the DependencyObject wrapper.
3131 case MoonId_CaptureMouse:
3132 BOOLEAN_TO_NPVARIANT (((UIElement*)dob)->CaptureMouse (), *result);
3133 return true;
3134 case MoonId_ReleaseMouseCapture:
3135 ((UIElement*)dob)->ReleaseMouseCapture ();
3137 VOID_TO_NPVARIANT (*result);
3138 return true;
3140 default:
3141 return MoonlightObject::Invoke (id, name, args, argCount, result);
3146 MoonlightDependencyObjectType::MoonlightDependencyObjectType ()
3148 allocate = moonlight_dependency_object_allocate;
3150 AddMapping (moonlight_dependency_object_mapping, G_N_ELEMENTS (moonlight_dependency_object_mapping));
3155 /*** MoonlightEventObjectClass ***************************************************/
3157 static NPObject *
3158 moonlight_event_object_allocate (NPP instance, NPClass *klass)
3160 return new MoonlightEventObjectObject (instance);
3163 MoonlightEventObjectObject::~MoonlightEventObjectObject ()
3165 if (eo) {
3166 PluginInstance *plugin;
3167 if ((plugin = (PluginInstance *) instance->pdata))
3168 plugin->RemoveWrappedObject (eo);
3170 moonlight_type = Type::INVALID;
3172 eo->unref ();
3173 eo = NULL;
3177 MoonlightEventObjectType::MoonlightEventObjectType ()
3179 allocate = moonlight_event_object_allocate;
3182 MoonlightEventObjectType *MoonlightEventObjectClass;
3184 MoonlightEventObjectObject *
3185 EventObjectCreateWrapper (NPP instance, EventObject *obj)
3187 PluginInstance *plugin = (PluginInstance *) instance->pdata;
3188 MoonlightEventObjectObject *depobj;
3189 NPClass *np_class;
3191 if (obj == NULL)
3192 return NULL;
3194 depobj = (MoonlightEventObjectObject *) plugin->LookupWrappedObject (obj);
3196 if (depobj) {
3197 NPN_RetainObject (depobj);
3198 return depobj;
3201 /* for EventObject subclasses which have special plugin classes, check here */
3202 Type::Kind kind = obj->GetObjectType ();
3203 switch (kind) {
3204 case Type::STORYBOARD:
3205 np_class = dependency_object_classes [STORYBOARD_CLASS];
3206 break;
3207 case Type::MEDIAELEMENT:
3208 np_class = dependency_object_classes [MEDIA_ELEMENT_CLASS];
3209 break;
3210 case Type::DOWNLOADER:
3211 np_class = dependency_object_classes [DOWNLOADER_CLASS];
3212 break;
3213 case Type::IMAGE:
3214 np_class = dependency_object_classes [IMAGE_CLASS];
3215 break;
3216 case Type::IMAGEBRUSH:
3217 np_class = dependency_object_classes [IMAGE_BRUSH_CLASS];
3218 break;
3219 case Type::TEXTBLOCK:
3220 np_class = dependency_object_classes [TEXT_BLOCK_CLASS];
3221 break;
3222 case Type::TEXTBOX:
3223 np_class = dependency_object_classes [TEXT_BOX_CLASS];
3224 break;
3225 case Type::PASSWORDBOX:
3226 np_class = dependency_object_classes [PASSWORD_BOX_CLASS];
3227 break;
3228 case Type::MULTISCALEIMAGE:
3229 np_class = dependency_object_classes [MULTI_SCALE_IMAGE_CLASS];
3230 break;
3231 case Type::EVENTOBJECT:
3232 case Type::SURFACE:
3233 np_class = MoonlightEventObjectClass;
3234 break;
3235 case Type::STYLUSINFO:
3236 np_class = dependency_object_classes [STYLUS_INFO_CLASS];
3237 break;
3238 case Type::STYLUSPOINT_COLLECTION:
3239 np_class = dependency_object_classes [STYLUS_POINT_COLLECTION_CLASS];
3240 break;
3241 case Type::STROKE_COLLECTION:
3242 np_class = dependency_object_classes [STROKE_COLLECTION_CLASS];
3243 break;
3244 case Type::STROKE:
3245 np_class = dependency_object_classes [STROKE_CLASS];
3246 break;
3247 case Type::ROUTEDEVENTARGS:
3248 np_class = dependency_object_classes [ROUTED_EVENT_ARGS_CLASS];
3249 break;
3250 case Type::MOUSEEVENTARGS:
3251 case Type::MOUSEBUTTONEVENTARGS:
3252 case Type::MOUSEWHEELEVENTARGS:
3253 np_class = dependency_object_classes [MOUSE_EVENT_ARGS_CLASS];
3254 break;
3255 case Type::DOWNLOADPROGRESSEVENTARGS:
3256 np_class = dependency_object_classes [DOWNLOAD_PROGRESS_EVENT_ARGS_CLASS];
3257 break;
3258 case Type::KEYEVENTARGS:
3259 np_class = dependency_object_classes [KEY_EVENT_ARGS_CLASS];
3260 break;
3261 case Type::MARKERREACHEDEVENTARGS:
3262 np_class = dependency_object_classes [MARKER_REACHED_EVENT_ARGS_CLASS];
3263 break;
3264 case Type::ERROREVENTARGS:
3265 case Type::PARSERERROREVENTARGS:
3266 case Type::IMAGEERROREVENTARGS:
3267 np_class = dependency_object_classes [ERROR_EVENT_ARGS_CLASS];
3268 break;
3269 case Type::UIELEMENT:
3270 np_class = dependency_object_classes [UI_ELEMENT_CLASS];
3271 break;
3272 default:
3273 if (Type::Find (kind)->IsSubclassOf (Type::CONTROL))
3274 np_class = dependency_object_classes [CONTROL_CLASS];
3275 else if (Type::Find (kind)->IsSubclassOf (Type::UIELEMENT))
3276 np_class = dependency_object_classes [UI_ELEMENT_CLASS];
3277 else if (Type::Find (kind)->IsSubclassOf (Type::COLLECTION))
3278 np_class = dependency_object_classes [COLLECTION_CLASS];
3279 else if (Type::Find (kind)->IsSubclassOf (Type::EVENTARGS))
3280 np_class = dependency_object_classes [EVENT_ARGS_CLASS];
3281 else
3282 np_class = dependency_object_classes [DEPENDENCY_OBJECT_CLASS];
3285 depobj = (MoonlightEventObjectObject *) NPN_CreateObject (instance, np_class);
3286 depobj->moonlight_type = obj->GetObjectType ();
3287 depobj->eo = obj;
3288 obj->ref ();
3290 plugin->AddWrappedObject (obj, depobj);
3292 return depobj;
3297 /*** MoonlightCollectionClass ***************************************************/
3299 static NPObject *
3300 moonlight_collection_allocate (NPP instance, NPClass *klass)
3302 return new MoonlightCollectionObject (instance);
3305 static const MoonNameIdMapping
3306 moonlight_collection_mapping [] = {
3307 { "add", MoonId_Add },
3308 { "clear", MoonId_Clear },
3309 { "count", MoonId_Count },
3310 { "getitem", MoonId_GetItem },
3311 { "getitembyname", MoonId_GetItemByName },
3312 { "insert", MoonId_Insert },
3313 { "remove", MoonId_Remove },
3314 { "removeat", MoonId_RemoveAt },
3317 bool
3318 MoonlightCollectionObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
3320 Collection *col = (Collection *) GetDependencyObject ();
3322 switch (id) {
3323 case MoonId_Count:
3324 INT32_TO_NPVARIANT (col->GetCount (), *result);
3325 return true;
3326 default:
3327 return MoonlightDependencyObjectObject::GetProperty (id, name, result);
3331 bool
3332 MoonlightCollectionObject::Invoke (int id, NPIdentifier name,
3333 const NPVariant *args, guint32 argCount,
3334 NPVariant *result)
3336 Collection *col = (Collection *) GetDependencyObject ();
3338 switch (id) {
3339 case MoonId_Add: {
3340 if (!check_arg_list ("o", argCount, args) ||
3341 !npvariant_is_dependency_object (args[0]))
3342 THROW_JS_EXCEPTION ("add");
3344 MoonlightDependencyObjectObject *el = (MoonlightDependencyObjectObject *) NPVARIANT_TO_OBJECT (args[0]);
3345 int n = col->Add (Value (el->GetDependencyObject ()));
3347 if (n == -1)
3348 THROW_JS_EXCEPTION ("add");
3350 INT32_TO_NPVARIANT (n, *result);
3352 return true;
3354 case MoonId_Remove: {
3355 if (col->GetObjectType () == Type::RESOURCE_DICTIONARY) {
3356 if (check_arg_list ("s", argCount, args)) {
3357 bool res = ((ResourceDictionary*)col)->Remove (NPVARIANT_TO_STRING (args[0]).utf8characters);
3358 BOOLEAN_TO_NPVARIANT (res, *result);
3359 return true;
3363 if (!check_arg_list ("o", argCount, args) ||
3364 !npvariant_is_dependency_object (args[0]))
3365 THROW_JS_EXCEPTION ("remove");
3367 MoonlightDependencyObjectObject *el = (MoonlightDependencyObjectObject *) NPVARIANT_TO_OBJECT (args[0]);
3368 bool res = col->Remove (Value (el->GetDependencyObject ()));
3370 BOOLEAN_TO_NPVARIANT (res, *result);
3372 return true;
3374 case MoonId_RemoveAt: {
3375 if (!check_arg_list ("i", argCount, args))
3376 THROW_JS_EXCEPTION ("removeAt");
3378 int index = NPVARIANT_TO_INT32 (args [0]);
3380 if (index < 0 || index >= col->GetCount ())
3381 THROW_JS_EXCEPTION ("removeAt");
3383 DependencyObject *obj = col->GetValueAt (index)->AsDependencyObject ();
3384 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, obj), *result);
3386 col->RemoveAt (index);
3388 return true;
3390 case MoonId_Insert: {
3391 if (!check_arg_list ("i[o]", argCount, args)) {
3392 g_warning ("insert 1");
3393 THROW_JS_EXCEPTION ("insert");
3396 if (argCount < 2) {
3397 VOID_TO_NPVARIANT (*result);
3398 return true;
3401 if (!npvariant_is_dependency_object (args[1])) {
3402 g_warning ("insert 2");
3403 THROW_JS_EXCEPTION ("insert");
3406 MoonlightDependencyObjectObject *el = (MoonlightDependencyObjectObject*) NPVARIANT_TO_OBJECT (args[1]);
3407 int index = NPVARIANT_TO_INT32 (args[0]);
3408 MoonError err;
3409 Value val (el->GetDependencyObject ());
3411 if (!col->InsertWithError (index, &val, &err)) {
3412 g_warning ("insert 2: %s", err.message);
3413 THROW_JS_EXCEPTION ("insert");
3416 VOID_TO_NPVARIANT (*result);
3418 return true;
3420 case MoonId_Clear: {
3421 if (argCount != 0)
3422 THROW_JS_EXCEPTION ("clear");
3424 col->Clear ();
3426 VOID_TO_NPVARIANT (*result);
3428 return true;
3430 case MoonId_GetItem: {
3431 if (col->GetObjectType () == Type::RESOURCE_DICTIONARY) {
3432 if (check_arg_list ("s", argCount, args)) {
3433 // handle the string case here, we
3434 // handle the int case in the common
3435 // code below.
3436 bool unused;
3438 Value *v = ((ResourceDictionary*)col)->Get (NPVARIANT_TO_STRING (args[0]).utf8characters, &unused);
3439 value_to_variant (this, v, result);
3440 return true;
3444 if (!check_arg_list ("i", argCount, args))
3445 THROW_JS_EXCEPTION ("getItem");
3447 int index = NPVARIANT_TO_INT32 (args[0]);
3449 if (index < 0)
3450 THROW_JS_EXCEPTION ("getItem");
3452 if (index >= col->GetCount ()) {
3453 NULL_TO_NPVARIANT (*result);
3454 return true;
3457 DependencyObject *obj = col->GetValueAt (index)->AsDependencyObject ();
3458 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, obj), *result);
3460 return true;
3462 case MoonId_GetItemByName: {
3463 if (col->GetObjectType () != Type::MEDIAATTRIBUTE_COLLECTION ||
3464 !check_arg_list ("s", argCount, args))
3465 THROW_JS_EXCEPTION ("getItemByName");
3467 char *name = STRDUP_FROM_VARIANT (args[0]);
3468 DependencyObject *obj = ((MediaAttributeCollection *) col)->GetItemByName (name);
3469 g_free (name);
3471 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, obj), *result);
3473 return true;
3475 default:
3476 return MoonlightDependencyObjectObject::Invoke (id, name, args, argCount, result);
3481 MoonlightCollectionType::MoonlightCollectionType ()
3483 AddMapping (moonlight_collection_mapping, G_N_ELEMENTS (moonlight_collection_mapping));
3485 allocate = moonlight_collection_allocate;
3489 /*** MoonlightStoryboardClass ***************************************************/
3491 static NPObject *
3492 moonlight_storyboard_allocate (NPP instance, NPClass *klass)
3494 return new MoonlightStoryboardObject (instance);
3497 static const MoonNameIdMapping
3498 moonlight_storyboard_mapping [] = {
3499 { "begin", MoonId_Begin },
3500 { "completed", MoonId_Completed },
3501 { "pause", MoonId_Pause },
3502 { "resume", MoonId_Resume },
3503 { "seek", MoonId_Seek },
3504 { "stop", MoonId_Stop }
3507 bool
3508 MoonlightStoryboardObject::Invoke (int id, NPIdentifier name,
3509 const NPVariant *args, guint32 argCount,
3510 NPVariant *result)
3512 // FIXME: all the WithError calls should be using a MoonError,
3513 // which we convert to a JS exception and throw if there's a
3514 // problem.
3515 Storyboard *sb = (Storyboard*)GetDependencyObject ();
3517 switch (id) {
3518 case MoonId_Begin:
3519 if (argCount != 0 || !sb->BeginWithError (NULL))
3520 THROW_JS_EXCEPTION ("begin");
3522 VOID_TO_NPVARIANT (*result);
3524 return true;
3525 case MoonId_Pause:
3526 if (argCount != 0)
3527 THROW_JS_EXCEPTION ("pause");
3529 sb->PauseWithError (NULL);
3531 VOID_TO_NPVARIANT (*result);
3533 return true;
3534 case MoonId_Resume:
3535 if (argCount != 0)
3536 THROW_JS_EXCEPTION ("resume");
3538 sb->ResumeWithError (NULL);
3540 VOID_TO_NPVARIANT (*result);
3542 return true;
3543 case MoonId_Seek: {
3544 if (!check_arg_list ("(is)", argCount, args))
3545 THROW_JS_EXCEPTION ("seek");
3547 TimeSpan ts;
3548 bool ok;
3550 if (NPVARIANT_IS_INT32 (args[0])) {
3551 ts = (TimeSpan) NPVARIANT_TO_INT32 (args[0]);
3552 } else if (NPVARIANT_IS_STRING (args[0])) {
3553 char *span = STRDUP_FROM_VARIANT (args[0]);
3554 ok = time_span_from_str (span, &ts);
3555 g_free (span);
3557 if (!ok)
3558 THROW_JS_EXCEPTION ("seek");
3561 sb->SeekWithError (ts, NULL);
3563 VOID_TO_NPVARIANT (*result);
3565 return true;
3567 case MoonId_Stop:
3568 if (argCount != 0)
3569 THROW_JS_EXCEPTION ("stop");
3571 sb->StopWithError (NULL);
3573 VOID_TO_NPVARIANT (*result);
3575 return true;
3576 default:
3577 return MoonlightDependencyObjectObject::Invoke (id, name, args, argCount, result);
3581 MoonlightStoryboardType::MoonlightStoryboardType ()
3583 AddMapping (moonlight_storyboard_mapping, G_N_ELEMENTS (moonlight_storyboard_mapping));
3585 allocate = moonlight_storyboard_allocate;
3589 /*** MoonlightMediaElementClass ***************************************************/
3591 static NPObject *
3592 moonlight_media_element_allocate (NPP instance, NPClass *)
3594 return new MoonlightMediaElementObject (instance);
3597 static const MoonNameIdMapping
3598 moonlight_media_element_mapping [] = {
3599 { "bufferingprogresschanged", MoonId_BufferingProgressChanged },
3600 { "currentstatechanged", MoonId_CurrentStateChanged },
3601 { "downloadprogresschanged", MoonId_DownloadProgressChanged },
3602 { "markerreached", MoonId_MarkerReached },
3603 { "mediaended", MoonId_MediaEnded },
3604 { "mediafailed", MoonId_MediaFailed },
3605 { "mediaopened", MoonId_MediaOpened },
3606 { "pause", MoonId_Pause },
3607 { "play", MoonId_Play },
3608 { "setsource", MoonId_SetSource },
3609 { "stop", MoonId_Stop },
3612 bool
3613 MoonlightMediaElementObject::Invoke (int id, NPIdentifier name,
3614 const NPVariant *args, guint32 argCount,
3615 NPVariant *result)
3617 MediaElement *media = (MediaElement*)GetDependencyObject ();
3619 switch (id) {
3620 case MoonId_Play:
3621 if (argCount != 0)
3622 THROW_JS_EXCEPTION ("play");
3624 media->Play ();
3626 VOID_TO_NPVARIANT (*result);
3628 return true;
3630 case MoonId_Pause:
3631 if (argCount != 0)
3632 THROW_JS_EXCEPTION ("pause");
3634 media->Pause ();
3636 VOID_TO_NPVARIANT (*result);
3638 return true;
3640 case MoonId_Stop:
3641 if (argCount != 0)
3642 THROW_JS_EXCEPTION ("stop");
3644 media->Stop ();
3646 VOID_TO_NPVARIANT (*result);
3648 return true;
3650 case MoonId_SetSource: {
3651 if (!check_arg_list ("os", argCount, args) ||
3652 !npvariant_is_downloader (args[0]))
3653 THROW_JS_EXCEPTION ("AG_E_RUNTIME_METHOD");
3655 DependencyObject *downloader = ((MoonlightDependencyObjectObject *) NPVARIANT_TO_OBJECT (args[0]))->GetDependencyObject ();
3657 char *part = STRDUP_FROM_VARIANT (args [1]);
3658 media->SetSource ((Downloader *) downloader, part);
3659 g_free (part);
3661 VOID_TO_NPVARIANT (*result);
3663 return true;
3666 default:
3667 return MoonlightUIElementObject::Invoke (id, name, args, argCount, result);
3671 MoonlightMediaElementType::MoonlightMediaElementType ()
3673 AddMapping (moonlight_media_element_mapping, G_N_ELEMENTS (moonlight_media_element_mapping));
3675 allocate = moonlight_media_element_allocate;
3678 /*** MoonlightMultiScaleImageClass *********/
3679 static NPObject *
3680 moonlight_multiscaleimage_allocate (NPP instance, NPClass *klass)
3682 return new MoonlightMultiScaleImageObject (instance);
3685 static const MoonNameIdMapping moonlight_multiscaleimage_mapping[] = {
3686 {"getithsubimage", MoonId_MultiScaleImage_GetIthSubImage},
3687 {"getsubimagecount", MoonId_MultiScaleImage_GetSubImageCount},
3688 {"logicaltoelementx", MoonId_MultiScaleImage_LogicalToElementX},
3689 {"logicaltoelementy", MoonId_MultiScaleImage_LogicalToElementY},
3690 {"source", MoonId_MultiScaleImage_Source },
3691 {"zoomaboutlogicalpoint", MoonId_MultiScaleImage_ZoomAboutLogicalPoint}
3694 bool
3695 MoonlightMultiScaleImageObject::Invoke (int id, NPIdentifier name,
3696 const NPVariant *args, guint32 argCount,
3697 NPVariant *result)
3699 MultiScaleImage *dob = (MultiScaleImage*)GetDependencyObject ();
3701 switch (id) {
3703 case MoonId_MultiScaleImage_GetIthSubImage: {
3704 if (!check_arg_list ("i", argCount, args))
3705 THROW_JS_EXCEPTION ("GetIthSubImage");
3706 int arg0 = NPVARIANT_TO_INT32 (args[0]);
3707 MultiScaleSubImage * ret = dob->GetIthSubImage(arg0);
3708 if (ret)
3709 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, ret), *result);
3710 else
3711 NULL_TO_NPVARIANT (*result);
3712 return true;
3713 break;
3716 case MoonId_MultiScaleImage_GetSubImageCount: {
3717 int ret = dob->GetSubImageCount();
3718 INT32_TO_NPVARIANT (ret, *result);
3719 return true;
3720 break;
3723 case MoonId_MultiScaleImage_LogicalToElementX: {
3724 if (!check_arg_list ("ii", argCount, args))
3725 THROW_JS_EXCEPTION ("LogicalToElementX");
3726 int arg0 = NPVARIANT_TO_INT32 (args[0]);
3727 int arg1 = NPVARIANT_TO_INT32 (args[1]);
3728 int ret = dob->LogicalToElementX(arg0,arg1);
3729 INT32_TO_NPVARIANT (ret, *result);
3730 return true;
3731 break;
3734 case MoonId_MultiScaleImage_LogicalToElementY: {
3735 if (!check_arg_list ("ii", argCount, args))
3736 THROW_JS_EXCEPTION ("LogicalToElementY");
3737 int arg0 = NPVARIANT_TO_INT32 (args[0]);
3738 int arg1 = NPVARIANT_TO_INT32 (args[1]);
3739 int ret = dob->LogicalToElementY(arg0,arg1);
3740 INT32_TO_NPVARIANT (ret, *result);
3741 return true;
3742 break;
3745 case MoonId_MultiScaleImage_ZoomAboutLogicalPoint: {
3746 if (!check_arg_list ("ddd", argCount, args))
3747 THROW_JS_EXCEPTION ("ZoomAboutLogicalPoint");
3748 double arg0 = NPVARIANT_TO_DOUBLE (args[0]);
3749 double arg1 = NPVARIANT_TO_DOUBLE (args[1]);
3750 double arg2 = NPVARIANT_TO_DOUBLE (args[2]);
3751 dob->ZoomAboutLogicalPoint(arg0,arg1,arg2);
3752 VOID_TO_NPVARIANT (*result);
3753 return true;
3754 break;
3758 return MoonlightDependencyObjectObject::Invoke (id, name, args, argCount, result);
3761 bool
3762 MoonlightMultiScaleImageObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
3764 MultiScaleImage *msi = (MultiScaleImage *) GetDependencyObject ();
3765 switch (id) {
3766 case MoonId_MultiScaleImage_Source: {
3767 MultiScaleTileSource *ts = (MultiScaleTileSource *) msi->GetSource ();
3768 if (ts && ts->Is (Type::DEEPZOOMIMAGETILESOURCE)) {
3769 Uri *uri = new Uri ();
3771 uri->Parse (NPVARIANT_TO_STRING (*value).utf8characters);
3772 ((DeepZoomImageTileSource *)ts)->SetUriSource (uri);
3773 delete uri;
3774 return true;
3777 default:
3778 return MoonlightDependencyObjectObject::SetProperty (id, name, value);;
3782 MoonlightMultiScaleImageType::MoonlightMultiScaleImageType ()
3784 AddMapping (moonlight_multiscaleimage_mapping, G_N_ELEMENTS (moonlight_multiscaleimage_mapping));
3786 allocate = moonlight_multiscaleimage_allocate;
3789 /*** MoonlightImageClass ***************************************************/
3791 static NPObject *
3792 moonlight_image_allocate (NPP instance, NPClass *klass)
3794 return new MoonlightImageObject (instance);
3797 static const MoonNameIdMapping
3798 moonlight_image_mapping [] = {
3799 { "downloadprogresschanged", MoonId_DownloadProgressChanged },
3800 { "imagefailed", MoonId_ImageFailed },
3801 { "source", MoonId_Source },
3802 { "setsource", MoonId_SetSource }
3805 bool
3806 MoonlightImageObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
3808 Image *img = (Image *) GetDependencyObject ();
3810 switch (id) {
3811 case MoonId_Source: {
3812 ImageSource *source = (ImageSource *) img->GetSource ();
3813 if (source && source->Is (Type::BITMAPIMAGE)) {
3814 char *uri = ((BitmapImage *)source)->GetUriSource()->ToString();
3815 STRINGN_TO_NPVARIANT (uri, strlen (uri), *result);
3816 } else {
3817 NULL_TO_NPVARIANT (*result);
3820 return true;
3823 default:
3824 return MoonlightUIElementObject::GetProperty (id, name, result);
3828 bool
3829 MoonlightImageObject::Invoke (int id, NPIdentifier name,
3830 const NPVariant *args, guint32 argCount,
3831 NPVariant *result)
3833 Image *img = (Image *) GetDependencyObject ();
3834 DependencyObject *downloader;
3835 char *part;
3837 switch (id) {
3838 case MoonId_SetSource:
3839 if (!check_arg_list ("os", argCount, args) ||
3840 !npvariant_is_downloader (args[0]))
3841 THROW_JS_EXCEPTION ("AG_E_RUNTIME_METHOD");
3843 downloader = ((MoonlightDependencyObjectObject *) NPVARIANT_TO_OBJECT (args[0]))->GetDependencyObject ();
3845 part = STRDUP_FROM_VARIANT (args [1]);
3846 img->SetSource ((Downloader *) downloader, part);
3847 g_free (part);
3849 VOID_TO_NPVARIANT (*result);
3851 return true;
3852 default:
3853 return MoonlightUIElementObject::Invoke (id, name, args, argCount, result);
3857 MoonlightImageType::MoonlightImageType ()
3859 AddMapping (moonlight_image_mapping, G_N_ELEMENTS (moonlight_image_mapping));
3861 allocate = moonlight_image_allocate;
3865 /*** MoonlightImageBrushClass ***************************************************/
3867 static NPObject *
3868 moonlight_image_brush_allocate (NPP instance, NPClass *klass)
3870 return new MoonlightImageBrushObject (instance);
3874 static const MoonNameIdMapping
3875 moonlight_image_brush_mapping [] = {
3876 { "downloadprogresschanged", MoonId_DownloadProgressChanged },
3877 { "imagesource", MoonId_Source },
3878 { "setsource", MoonId_SetSource }
3881 bool
3882 MoonlightImageBrushObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
3884 ImageBrush *brush = (ImageBrush *) GetDependencyObject ();
3886 switch (id) {
3887 case MoonId_Source: {
3888 ImageSource *source = (ImageSource *) brush->GetImageSource ();
3889 if (source && source->Is (Type::BITMAPIMAGE)) {
3890 char *uri = ((BitmapImage *)source)->GetUriSource()->ToString();
3891 STRINGN_TO_NPVARIANT (uri, strlen (uri), *result);
3892 } else {
3893 NULL_TO_NPVARIANT (*result);
3896 return true;
3899 default:
3900 return MoonlightDependencyObjectObject::GetProperty (id, name, result);
3904 bool
3905 MoonlightImageBrushObject::Invoke (int id, NPIdentifier name,
3906 const NPVariant *args, guint32 argCount,
3907 NPVariant *result)
3909 ImageBrush *img = (ImageBrush *) GetDependencyObject ();
3910 DependencyObject *downloader;
3912 switch (id) {
3913 case MoonId_SetSource: {
3914 if (!check_arg_list ("os", argCount, args) ||
3915 !npvariant_is_downloader (args[0]))
3916 THROW_JS_EXCEPTION ("setSource");
3918 downloader = ((MoonlightDependencyObjectObject *) NPVARIANT_TO_OBJECT (args[0]))->GetDependencyObject ();
3920 char *part = STRDUP_FROM_VARIANT (args [1]);
3921 img->SetSource ((Downloader *) downloader, part);
3922 g_free (part);
3924 VOID_TO_NPVARIANT (*result);
3926 return true;
3929 default:
3930 return MoonlightDependencyObjectObject::Invoke (id, name, args, argCount, result);
3934 MoonlightImageBrushType::MoonlightImageBrushType ()
3936 AddMapping (moonlight_image_brush_mapping, G_N_ELEMENTS (moonlight_image_brush_mapping));
3938 allocate = moonlight_image_brush_allocate;
3942 /*** MoonlightControlClass ***************************************************/
3944 static NPObject *
3945 moonlight_control_allocate (NPP instance, NPClass *klass)
3947 return new MoonlightControlObject (instance);
3951 static const MoonNameIdMapping moonlight_control_mapping[] = {
3952 { "focus", MoonId_Focus }
3956 bool
3957 MoonlightControlObject::Invoke (int id, NPIdentifier name,
3958 const NPVariant *args, guint32 argCount,
3959 NPVariant *result)
3961 Control *control = (Control *) GetDependencyObject ();
3963 switch (id) {
3964 case MoonId_Focus:
3965 if (argCount != 0)
3966 THROW_JS_EXCEPTION ("focus");
3968 BOOLEAN_TO_NPVARIANT (control->Focus (), *result);
3970 return true;
3971 default:
3972 return MoonlightUIElementObject::Invoke (id, name, args, argCount, result);
3976 MoonlightControlType::MoonlightControlType ()
3978 AddMapping (moonlight_control_mapping, G_N_ELEMENTS (moonlight_control_mapping));
3980 allocate = moonlight_control_allocate;
3984 /*** MoonlightTextBoxClass ***************************************************/
3986 static NPObject *
3987 moonlight_text_box_allocate (NPP instance, NPClass *klass)
3989 return new MoonlightTextBoxObject (instance);
3993 static const MoonNameIdMapping moonlight_text_box_mapping[] = {
3994 { "select", MoonId_Select },
3995 { "selectall", MoonId_SelectAll }
3999 bool
4000 MoonlightTextBoxObject::Invoke (int id, NPIdentifier name,
4001 const NPVariant *args, guint32 argCount,
4002 NPVariant *result)
4004 TextBox *textbox = (TextBox *) GetDependencyObject ();
4005 MoonError err;
4007 switch (id) {
4008 case MoonId_Select:
4009 if (!check_arg_list ("ii", argCount, args))
4010 THROW_JS_EXCEPTION ("select");
4012 if (!textbox->SelectWithError (NPVARIANT_TO_INT32 (args[0]),
4013 NPVARIANT_TO_INT32 (args[1]),
4014 &err)) {
4015 THROW_JS_EXCEPTION (err.message);
4018 VOID_TO_NPVARIANT (*result);
4020 return true;
4021 case MoonId_SelectAll:
4022 if (argCount != 0)
4023 THROW_JS_EXCEPTION ("selectAll");
4025 textbox->SelectAll ();
4027 VOID_TO_NPVARIANT (*result);
4029 return true;
4030 default:
4031 return MoonlightControlObject::Invoke (id, name, args, argCount, result);
4035 MoonlightTextBoxType::MoonlightTextBoxType ()
4037 AddMapping (moonlight_text_box_mapping, G_N_ELEMENTS (moonlight_text_box_mapping));
4039 allocate = moonlight_text_box_allocate;
4042 /*** MoonlightPasswordBoxClass ***************************************************/
4044 static NPObject *
4045 moonlight_password_box_allocate (NPP instance, NPClass *klass)
4047 return new MoonlightPasswordBoxObject (instance);
4051 static const MoonNameIdMapping moonlight_password_box_mapping[] = {
4052 { "select", MoonId_Select },
4053 { "selectall", MoonId_SelectAll }
4057 bool
4058 MoonlightPasswordBoxObject::Invoke (int id, NPIdentifier name,
4059 const NPVariant *args, guint32 argCount,
4060 NPVariant *result)
4062 PasswordBox *passwordbox = (PasswordBox *) GetDependencyObject ();
4063 MoonError err;
4065 switch (id) {
4066 case MoonId_Select:
4067 if (!check_arg_list ("ii", argCount, args))
4068 THROW_JS_EXCEPTION ("select");
4070 if (!passwordbox->SelectWithError (NPVARIANT_TO_INT32 (args[0]),
4071 NPVARIANT_TO_INT32 (args[1]),
4072 &err)) {
4073 THROW_JS_EXCEPTION (err.message);
4076 VOID_TO_NPVARIANT (*result);
4078 return true;
4079 case MoonId_SelectAll:
4080 if (argCount != 0)
4081 THROW_JS_EXCEPTION ("selectAll");
4083 passwordbox->SelectAll ();
4085 VOID_TO_NPVARIANT (*result);
4087 return true;
4088 default:
4089 return MoonlightControlObject::Invoke (id, name, args, argCount, result);
4093 MoonlightPasswordBoxType::MoonlightPasswordBoxType ()
4095 AddMapping (moonlight_password_box_mapping, G_N_ELEMENTS (moonlight_password_box_mapping));
4097 allocate = moonlight_password_box_allocate;
4101 /*** MoonlightTextBlockClass ***************************************************/
4103 static NPObject *
4104 moonlight_text_block_allocate (NPP instance, NPClass *klass)
4106 return new MoonlightTextBlockObject (instance);
4110 static const MoonNameIdMapping moonlight_text_block_mapping[] = {
4111 { "setfontsource", MoonId_SetFontSource }
4115 bool
4116 MoonlightTextBlockObject::Invoke (int id, NPIdentifier name,
4117 const NPVariant *args, guint32 argCount,
4118 NPVariant *result)
4120 TextBlock *tb = (TextBlock *) GetDependencyObject ();
4121 DependencyObject *downloader = NULL;
4123 switch (id) {
4124 case MoonId_SetFontSource:
4125 if (!check_arg_list ("(no)", argCount, args) && (!NPVARIANT_IS_NULL(args[0]) || !npvariant_is_downloader (args[0])))
4126 THROW_JS_EXCEPTION ("setFontSource");
4128 if (NPVARIANT_IS_OBJECT (args[0]))
4129 downloader = ((MoonlightDependencyObjectObject *) NPVARIANT_TO_OBJECT (args[0]))->GetDependencyObject ();
4131 tb->SetFontSource ((Downloader *) downloader);
4133 VOID_TO_NPVARIANT (*result);
4135 return true;
4136 default:
4137 return MoonlightUIElementObject::Invoke (id, name, args, argCount, result);
4141 MoonlightTextBlockType::MoonlightTextBlockType ()
4143 AddMapping (moonlight_text_block_mapping, G_N_ELEMENTS (moonlight_text_block_mapping));
4145 allocate = moonlight_text_block_allocate;
4149 /*** MoonlightStylusInfoClass ***************************************************/
4151 static NPObject *
4152 moonlight_stylus_info_allocate (NPP instance, NPClass *klass)
4154 return new MoonlightStylusInfoObject (instance);
4157 static const MoonNameIdMapping
4158 moonlight_stylus_info_mapping [] = {
4159 { "devicetype", MoonId_DeviceType },
4160 { "isinverted", MoonId_IsInverted },
4163 bool
4164 MoonlightStylusInfoObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
4166 StylusInfo *info = (StylusInfo *) GetDependencyObject ();
4168 switch (id) {
4169 case MoonId_DeviceType: {
4170 switch (info->GetDeviceType ()) {
4171 case TabletDeviceTypeMouse:
4172 string_to_npvariant ("Mouse", result);
4173 break;
4174 case TabletDeviceTypeStylus:
4175 string_to_npvariant ("Stylus", result);
4176 break;
4177 case TabletDeviceTypeTouch:
4178 string_to_npvariant ("Touch", result);
4179 break;
4180 default:
4181 THROW_JS_EXCEPTION ("deviceType");
4183 return true;
4185 case MoonId_IsInverted: {
4186 BOOLEAN_TO_NPVARIANT (info->GetIsInverted (), *result);
4187 return true;
4190 default:
4191 return MoonlightDependencyObjectObject::GetProperty (id, name, result);
4195 MoonlightStylusInfoType::MoonlightStylusInfoType ()
4197 AddMapping (moonlight_stylus_info_mapping, G_N_ELEMENTS (moonlight_stylus_info_mapping));
4199 allocate = moonlight_stylus_info_allocate;
4203 /*** MoonlightStylusPointCollectionClass ****************************************/
4205 static NPObject *
4206 moonlight_stylus_point_collection_allocate (NPP instance, NPClass *klass)
4208 return new MoonlightStylusPointCollectionObject (instance);
4211 static const MoonNameIdMapping
4212 moonlight_stylus_point_collection_mapping [] = {
4213 { "addstyluspoints", MoonId_AddStylusPoints },
4216 bool
4217 MoonlightStylusPointCollectionObject::Invoke (int id, NPIdentifier name, const NPVariant *args, guint32 argCount, NPVariant *result)
4219 StylusPointCollection *col = (StylusPointCollection *) GetDependencyObject ();
4221 switch (id) {
4222 case MoonId_AddStylusPoints: {
4223 if (!col || !check_arg_list ("o", argCount, args))
4224 return false;
4226 MoonlightStylusPointCollectionObject *spco = (MoonlightStylusPointCollectionObject*) NPVARIANT_TO_OBJECT(args[0]);
4227 double ret = col->AddStylusPoints ((StylusPointCollection*) spco->GetDependencyObject ());
4228 DOUBLE_TO_NPVARIANT (ret, *result);
4229 return true;
4231 default:
4232 return MoonlightCollectionObject::Invoke (id, name, args, argCount, result);
4236 MoonlightStylusPointCollectionType::MoonlightStylusPointCollectionType ()
4238 AddMapping (moonlight_stylus_point_collection_mapping, G_N_ELEMENTS (moonlight_stylus_point_collection_mapping));
4240 allocate = moonlight_stylus_point_collection_allocate;
4244 /*** MoonlightStrokeCollectionClass ****************************************/
4246 static NPObject *
4247 moonlight_stroke_collection_allocate (NPP instance, NPClass *klass)
4249 return new MoonlightStrokeCollectionObject (instance);
4252 static const MoonNameIdMapping
4253 moonlight_stroke_collection_mapping [] = {
4254 { "getbounds", MoonId_GetBounds },
4255 { "hittest", MoonId_HitTest }
4258 bool
4259 MoonlightStrokeCollectionObject::Invoke (int id, NPIdentifier name,
4260 const NPVariant *args, guint32 argCount, NPVariant *result)
4262 StrokeCollection *col = (StrokeCollection *) GetDependencyObject ();
4264 switch (id) {
4265 case MoonId_GetBounds: {
4266 Rect r = col->GetBounds ();
4267 Value v (r);
4268 value_to_variant (this, &v, result);
4269 return true;
4272 case MoonId_HitTest: {
4273 if (!check_arg_list ("o", argCount, args) ||
4274 !npvariant_is_dependency_object (args[0]))
4275 THROW_JS_EXCEPTION ("hitTest");
4277 DependencyObject *dob = DEPENDENCY_OBJECT_FROM_VARIANT (args [0]);
4278 if (!dob->Is (Type::STYLUSPOINT_COLLECTION))
4279 THROW_JS_EXCEPTION ("hitTest");
4281 StrokeCollection *hit_col = col->HitTest ((StylusPointCollection*)dob);
4283 OBJECT_TO_NPVARIANT (EventObjectCreateWrapper (instance, hit_col), *result);
4284 hit_col->unref ();
4285 return true;
4288 default:
4289 return MoonlightCollectionObject::Invoke (id, name, args, argCount, result);
4293 MoonlightStrokeCollectionType::MoonlightStrokeCollectionType ()
4295 AddMapping (moonlight_stroke_collection_mapping, G_N_ELEMENTS (moonlight_stroke_collection_mapping));
4297 allocate = moonlight_stroke_collection_allocate;
4301 /*** MoonlightStrokeClass ****************************************/
4303 static NPObject *
4304 moonlight_stroke_allocate (NPP instance, NPClass *klass)
4306 return new MoonlightStrokeObject (instance);
4309 static const MoonNameIdMapping
4310 moonlight_stroke_mapping [] = {
4311 { "getbounds", MoonId_GetBounds },
4312 { "hittest", MoonId_HitTest }
4315 bool
4316 MoonlightStrokeObject::Invoke (int id, NPIdentifier name,
4317 const NPVariant *args, guint32 argCount, NPVariant *result)
4319 Stroke *stroke = (Stroke *) GetDependencyObject ();
4321 switch (id) {
4322 case MoonId_GetBounds: {
4323 Rect r = stroke->GetBounds ();
4324 Value v (r);
4325 value_to_variant (this, &v, result);
4326 return true;
4329 case MoonId_HitTest: {
4330 if (!check_arg_list ("o", argCount, args) ||
4331 !npvariant_is_dependency_object (args[0]))
4332 THROW_JS_EXCEPTION ("hitTest");
4334 DependencyObject *dob = DEPENDENCY_OBJECT_FROM_VARIANT (args [0]);
4335 if (!dob->Is (Type::STYLUSPOINT_COLLECTION))
4336 THROW_JS_EXCEPTION ("hitTest");
4338 BOOLEAN_TO_NPVARIANT (stroke->HitTest ((StylusPointCollection*)dob), *result);
4339 return true;
4342 default:
4343 return MoonlightDependencyObjectObject::Invoke (id, name, args, argCount, result);
4347 MoonlightStrokeType::MoonlightStrokeType ()
4349 AddMapping (moonlight_stroke_mapping, G_N_ELEMENTS (moonlight_stroke_mapping));
4351 allocate = moonlight_stroke_allocate;
4355 /*** MoonlightDownloaderClass ***************************************************/
4357 static NPObject *
4358 moonlight_downloader_allocate (NPP instance, NPClass *klass)
4360 return new MoonlightDownloaderObject (instance);
4364 static const MoonNameIdMapping
4365 moonlight_downloader_mapping [] = {
4366 { "abort", MoonId_Abort },
4367 { "completed", MoonId_Completed },
4368 { "downloadprogresschanged", MoonId_DownloadProgressChanged },
4369 { "getresponsetext", MoonId_GetResponseText },
4370 { "open", MoonId_Open },
4371 { "responsetext", MoonId_ResponseText },
4372 { "send", MoonId_Send }
4375 bool
4376 MoonlightDownloaderObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
4378 Downloader *downloader = (Downloader *) GetDependencyObject ();
4379 gint64 size;
4380 char *text;
4382 switch (id) {
4383 case MoonId_ResponseText:
4384 if ((text = downloader->GetResponseText (NULL, &size))) {
4385 char *s = (char *) NPN_MemAlloc (size + 1);
4386 memcpy (s, text, size + 1);
4387 g_free (text);
4389 STRINGN_TO_NPVARIANT (s, (guint32) size, *result);
4390 } else {
4391 string_to_npvariant ("", result);
4394 return true;
4395 default:
4396 return MoonlightDependencyObjectObject::GetProperty (id, name, result);
4400 bool
4401 MoonlightDownloaderObject::Invoke (int id, NPIdentifier name,
4402 const NPVariant *args, guint32 argCount,
4403 NPVariant *result)
4405 Downloader *downloader = (Downloader *) GetDependencyObject ();
4406 char *part, *verb, *uri, *text;
4407 gint64 size;
4409 switch (id) {
4410 case MoonId_Abort:
4411 if (argCount != 0)
4412 THROW_JS_EXCEPTION ("abort");
4414 downloader->Abort ();
4416 VOID_TO_NPVARIANT (*result);
4418 return true;
4419 case MoonId_Open:
4420 if (!check_arg_list ("s(ns)", argCount, args))
4421 THROW_JS_EXCEPTION ("open");
4423 verb = STRDUP_FROM_VARIANT (args[0]);
4425 if (NPVARIANT_IS_STRING (args[1]))
4426 uri = STRDUP_FROM_VARIANT (args[1]);
4427 else
4428 uri = NULL;
4430 downloader->Open (verb, uri, DownloaderPolicy);
4431 g_free (verb);
4432 g_free (uri);
4434 VOID_TO_NPVARIANT (*result);
4436 return true;
4437 case MoonId_Send:
4438 if (argCount != 0 || downloader->GetSurface () == NULL)
4439 THROW_JS_EXCEPTION ("send");
4441 downloader->Send ();
4443 VOID_TO_NPVARIANT (*result);
4445 return true;
4446 case MoonId_GetResponseText:
4447 if (!check_arg_list ("s", argCount, args))
4448 THROW_JS_EXCEPTION ("getResponseText");
4450 part = STRDUP_FROM_VARIANT (args[0]);
4451 if ((text = downloader->GetResponseText (part, &size))) {
4452 char *s = (char *) NPN_MemAlloc (size + 1);
4453 memcpy (s, text, size + 1);
4454 g_free (text);
4456 STRINGN_TO_NPVARIANT (s, (guint32) size, *result);
4457 } else {
4458 string_to_npvariant ("", result);
4460 g_free (part);
4462 return true;
4463 default:
4464 return MoonlightDependencyObjectObject::Invoke (id, name, args, argCount, result);
4468 MoonlightDownloaderType::MoonlightDownloaderType ()
4470 AddMapping (moonlight_downloader_mapping, G_N_ELEMENTS (moonlight_downloader_mapping));
4472 allocate = moonlight_downloader_allocate;
4475 /*** MoonlightScriptableObjectClass ***************************************************/
4477 struct ScriptableProperty {
4478 gpointer property_handle;
4479 int property_type;
4480 bool can_read;
4481 bool can_write;
4484 struct ScriptableEvent {
4485 gpointer event_handle;
4488 struct ScriptableMethod {
4489 gpointer method_handle;
4490 int method_return_type;
4491 int *method_parameter_types;
4492 int parameter_count;
4496 static NPObject *
4497 moonlight_scriptable_object_allocate (NPP instance, NPClass *klass)
4499 return new MoonlightScriptableObjectObject (instance);
4502 MoonlightScriptableObjectObject::~MoonlightScriptableObjectObject ()
4504 if (managed_scriptable) {
4505 // FIXME: unref the scriptable object however we need to.
4506 managed_scriptable = NULL;
4509 // FIXME: free the properties, events, and methods hashes.
4510 g_hash_table_destroy (properties);
4511 g_hash_table_destroy (methods);
4512 g_hash_table_destroy (events);
4515 bool
4516 MoonlightScriptableObjectObject::HasProperty (NPIdentifier name)
4518 bool result;
4520 #if ds(!)0
4521 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
4522 printf ("scriptable has property %x = %s\n", name, strname);
4523 #endif
4525 result = (g_hash_table_lookup (properties, name) != NULL
4526 || g_hash_table_lookup (events, name)) || MoonlightObject::HasProperty (name);
4528 #if ds(!)0
4529 printf ("scriptable has property %x = %s: result: %i\n", name, strname, result);
4530 NPN_MemFree (strname);
4531 #endif
4534 return result;
4537 bool
4538 MoonlightScriptableObjectObject::GetProperty (int id, NPIdentifier name, NPVariant *result)
4540 bool res;
4542 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
4543 #if ds(!)0
4544 printf ("getting scriptable object property %x = %s\n", name, strname);
4545 #endif
4547 ScriptableProperty *prop = (ScriptableProperty*)g_hash_table_lookup (properties, name);
4548 if (!prop) {
4549 res = MoonlightObject::GetProperty (id, name, result);
4550 } else {
4551 Value v;
4553 getprop (managed_scriptable, strname, &v);
4555 value_to_variant (this, &v, result);
4556 res = true;
4559 #if ds(!)0
4560 printf ("getting scriptable object property %x = %s result: %i\n", name, strname, result);
4561 #endif
4562 NPN_MemFree (strname);
4564 return res;
4567 bool
4568 MoonlightScriptableObjectObject::SetProperty (int id, NPIdentifier name, const NPVariant *value)
4570 ScriptableProperty *prop;
4571 ScriptableEvent *event;
4572 Value *v;
4574 // first we try the property hash
4575 if ((prop = (ScriptableProperty *) g_hash_table_lookup (properties, name))) {
4577 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
4579 ds(printf ("setting scriptable object property %s\n", strname));
4581 variant_to_value (value, &v);
4582 setprop (managed_scriptable, strname, v);
4583 delete v;
4584 NPN_MemFree (strname);
4586 return true;
4589 // if that fails, look for the event of that name
4590 if ((event = (ScriptableEvent *) g_hash_table_lookup (events, name))) {
4591 #if ds(!)0
4592 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
4593 printf ("adding scriptable object event %s\n", strname);
4594 NPN_MemFree (strname);
4595 #endif
4597 if (NPVARIANT_IS_OBJECT (*value)) {
4598 NPObject *cb_obj = NPVARIANT_TO_OBJECT (*value);
4600 NPN_RetainObject (cb_obj);
4602 addevent (managed_scriptable, event->event_handle, this, cb_obj);
4603 } else {
4604 DEBUG_WARN_NOTIMPLEMENTED ("scriptableobject.register_event (non-object)");
4607 return true;
4610 return MoonlightObject::SetProperty (id, name, value);
4613 #if ds(!)0
4614 void
4615 dump_ptr_npid_hash (gpointer key, gpointer value, gpointer user_data)
4617 NPUTF8 *strname = NPN_UTF8FromIdentifier ((NPIdentifier) key );
4618 printf (" %i (%s) => %p\n", (int) key, strname, value);
4619 NPN_MemFree (strname);
4621 #endif
4623 bool
4624 MoonlightScriptableObjectObject::HasMethod (NPIdentifier name)
4626 bool result;
4628 #if ds(!)0
4629 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
4630 printf ("MoonlightScriptableObjectObject::HasMethod (%x = %s) this: %p hash table: %p\n", name, strname, this, methods);
4631 #endif
4633 result = g_hash_table_lookup (methods, name) != NULL;
4636 #if ds(!)0
4637 if (!result) {
4638 printf ("MoonlightScriptableObjectObject::HasMethod (%x = %s) failed, this: %p\n", name, strname, this);
4639 g_hash_table_foreach (methods, dump_ptr_npid_hash, NULL);
4640 } else {
4641 printf ("MoonlightScriptableObjectObject::HasMethod (%x = %s) this: %p result: %i\n", name, strname, this, result);
4643 NPN_MemFree (strname);
4644 #endif
4646 return result;
4649 bool
4650 MoonlightScriptableObjectObject::Invoke (int id, NPIdentifier name,
4651 const NPVariant *args, guint32 argCount,
4652 NPVariant *result)
4654 PluginInstance *plugin = (PluginInstance*) instance->pdata;
4655 if (plugin->IsCrossDomainApplication ()) {
4656 if (Deployment::GetCurrent ()->GetExternalCallersFromCrossDomain () == CrossDomainAccessNoAccess)
4657 THROW_JS_EXCEPTION ("XDomain Restriction");
4660 ScriptableMethod *method = (ScriptableMethod*)g_hash_table_lookup (methods, name);
4661 Value rv, **vargs = NULL;
4662 guint32 i;
4664 if (!method)
4665 return MoonlightObject::Invoke (id, name, args, argCount, result);
4667 NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
4668 #if ds(!)0
4669 printf ("invoking scriptable object method %s\n", strname);
4670 #endif
4672 if (argCount > 0) {
4673 vargs = new Value*[argCount];
4674 for (i = 0; i < argCount; i++)
4675 variant_to_value (&args[i], &vargs[i]);
4678 invoke (managed_scriptable, method->method_handle, strname, vargs, argCount, &rv);
4680 if (argCount > 0) {
4681 for (i = 0; i < argCount; i++)
4682 delete vargs[i];
4684 delete [] vargs;
4687 ds(printf ("result of invoking %s was %p\n", strname, rv.ToString()));
4689 /* Note: this 1 is "void" */
4690 if (method->method_return_type != 0)
4691 value_to_variant (this, &rv, result);
4692 else
4693 VOID_TO_NPVARIANT (*result);
4695 NPN_MemFree (strname);
4696 return true;
4700 MoonlightScriptableObjectType::MoonlightScriptableObjectType ()
4702 allocate = moonlight_scriptable_object_allocate;
4705 MoonlightScriptableObjectType *MoonlightScriptableObjectClass;
4707 MoonlightScriptableObjectObject *
4708 moonlight_scriptable_object_wrapper_create_root (PluginInstance *plugin, gpointer scriptable,
4709 InvokeDelegate invoke_func,
4710 SetPropertyDelegate setprop_func,
4711 GetPropertyDelegate getprop_func,
4712 EventHandlerDelegate addevent_func,
4713 EventHandlerDelegate removeevent_func)
4716 MoonlightScriptControlObject *root_object = plugin->GetRootObject ();
4717 return moonlight_scriptable_object_wrapper_create (root_object, scriptable,
4718 invoke_func,
4719 setprop_func,
4720 getprop_func,
4721 addevent_func,
4722 removeevent_func);
4725 MoonlightScriptableObjectObject *
4726 moonlight_scriptable_object_wrapper_create (NPObject *parent, gpointer scriptable,
4727 InvokeDelegate invoke_func,
4728 SetPropertyDelegate setprop_func,
4729 GetPropertyDelegate getprop_func,
4730 EventHandlerDelegate addevent_func,
4731 EventHandlerDelegate removeevent_func)
4733 MoonlightScriptableObjectObject *obj = (MoonlightScriptableObjectObject *)
4734 NPN_CreateObject (((MoonlightObject *) parent)->instance,
4735 MoonlightScriptableObjectClass);
4737 obj->managed_scriptable = scriptable;
4738 obj->invoke = invoke_func;
4739 obj->setprop = setprop_func;
4740 obj->getprop = getprop_func;
4741 obj->addevent = addevent_func;
4742 obj->removeevent = removeevent_func;
4744 ds(printf ("creating scriptable object wrapper => %p\n", obj));
4746 return obj;
4750 void
4751 moonlight_scriptable_object_add_property (PluginInstance *plugin,
4752 MoonlightScriptableObjectObject *obj,
4753 gpointer property_handle,
4754 char *property_name,
4755 int property_type,
4756 bool can_read,
4757 bool can_write)
4759 ds(printf ("adding property named %s to scriptable object %p\n", property_name, obj));
4761 ScriptableProperty *prop = new ScriptableProperty ();
4762 prop->property_handle = property_handle;
4763 prop->property_type = property_type;
4764 prop->can_read = can_read;
4765 prop->can_write = can_write;
4767 g_hash_table_insert (obj->properties, NPID(property_name), prop);
4770 void
4771 moonlight_scriptable_object_add_event (PluginInstance *plugin,
4772 MoonlightScriptableObjectObject *obj,
4773 gpointer event_handle,
4774 char *event_name)
4776 ds(printf ("adding event named %s to scriptable object %p\n", event_name, obj));
4778 ScriptableEvent *event = new ScriptableEvent ();
4779 event->event_handle = event_handle;
4781 g_hash_table_insert (obj->events, NPID(event_name), event);
4784 void
4785 moonlight_scriptable_object_add_method (PluginInstance *plugin,
4786 MoonlightScriptableObjectObject *obj,
4787 gpointer method_handle,
4788 char *method_name,
4789 int method_return_type,
4790 int *method_parameter_types,
4791 int parameter_count)
4794 NPIdentifier id = NPID (method_name);
4796 ds(printf ("adding method named %s (npid: %i) (return type = %d) to scriptable object %p\n", method_name, id, method_return_type, obj));
4798 ScriptableMethod *method = new ScriptableMethod ();
4799 method->method_handle = method_handle;
4800 method->method_return_type = method_return_type;
4801 method->method_parameter_types = new int[parameter_count];
4802 memcpy (method->method_parameter_types, method_parameter_types, sizeof (int) * parameter_count);
4803 method->parameter_count = parameter_count;
4805 g_hash_table_insert (obj->methods, id, method);
4808 void
4809 moonlight_scriptable_object_register (PluginInstance *plugin,
4810 char *name,
4811 MoonlightScriptableObjectObject *obj)
4813 MoonlightContentObject *content = (MoonlightContentObject *) plugin->GetRootObject ()->content;
4815 ds(printf ("registering scriptable object '%s' => %p\n", name, obj));
4817 g_hash_table_insert (content->registered_scriptable_objects, NPID (name), obj);
4819 ds(printf (" => done\n"));
4822 void
4823 moonlight_scriptable_object_emit_event (PluginInstance *plugin,
4824 MoonlightScriptableObjectObject *sobj,
4825 MoonlightScriptableObjectObject *event_args,
4826 NPObject *cb_obj)
4828 NPVariant args[2];
4829 NPVariant result;
4831 OBJECT_TO_NPVARIANT (sobj, args[0]);
4832 OBJECT_TO_NPVARIANT (event_args, args[1]);
4834 if (NPN_InvokeDefault (plugin->GetInstance (), cb_obj, args, 2, &result))
4835 NPN_ReleaseVariantValue (&result);
4839 /****************************** HtmlObject *************************/
4841 #if 0
4842 // Debug method to reflect over a NPObject and dump it to stdout.
4843 static void
4844 enumerate_html_object (NPP npp, NPObject *npobj, int recurse, int initial_recurse)
4846 NPVariant npresult;
4847 NPIdentifier *identifiers = NULL;
4848 guint32 id_count = 0;
4850 if (recurse == 0)
4851 return;
4853 char *tab = (char *) g_malloc (sizeof (char *) * (initial_recurse + 1) * 4);
4854 char *tab2 = (char *) g_malloc (sizeof (char *) * (initial_recurse + 1) * 4);
4856 memset (tab, 0, (initial_recurse + 1) * 4);
4857 memset (tab, ' ', (initial_recurse - recurse) * 4);
4859 memset (tab2, 0, (initial_recurse + 1) * 4);
4860 memset (tab2, ' ', (initial_recurse - recurse + 1) * 4);
4862 if (NPN_Enumerate (npp, npobj, &identifiers, &id_count)) {
4863 //printf ("%senumerate_html_object (%p, %p, %i, %i): Enumerating %i identifiers.\n", tab, npp, npobj, recurse, initial_recurse, id_count);
4864 for (guint32 i = 0; i < id_count; i++) {
4865 if (NPN_IdentifierIsString (identifiers [i])) {
4866 printf ("%s'%s': ", tab2, NPN_UTF8FromIdentifier (identifiers [i]));
4867 } else {
4868 printf ("%s%i: ", tab2, NPN_IntFromIdentifier (identifiers [i]));
4871 NPN_GetProperty (npp, npobj, identifiers [i], &npresult);
4873 if (NPVARIANT_IS_VOID (npresult)) {
4874 printf ("void\n");
4875 } else if (NPVARIANT_IS_NULL (npresult)) {
4876 printf ("NULL\n");
4877 } else if (NPVARIANT_IS_STRING (npresult)) {
4878 printf ("String: %s\n", NPVARIANT_TO_STRING (npresult).utf8characters);
4879 } else if (NPVARIANT_IS_BOOLEAN (npresult)) {
4880 printf ("Boolean: %i\n", NPVARIANT_TO_BOOLEAN (npresult));
4881 } else if (NPVARIANT_IS_INT32 (npresult)) {
4882 printf ("Int32: %i\n", NPVARIANT_TO_INT32 (npresult));
4883 } else if (NPVARIANT_IS_DOUBLE (npresult)) {
4884 printf ("Double: %f\n", NPVARIANT_TO_DOUBLE (npresult));
4885 } else if (NPVARIANT_IS_OBJECT (npresult)) {
4886 printf ("Object.\n");
4888 if (recurse >= 1) {
4889 enumerate_html_object (npp, NPVARIANT_TO_OBJECT (npresult), recurse - 1, initial_recurse);
4891 } else {
4892 printf ("Invalid value.\n");
4897 g_free (tab);
4898 g_free (tab2);
4900 #endif
4902 bool
4903 html_object_has_property (PluginInstance *plugin, NPObject *npobj, char *name)
4905 NPP npp = plugin->GetInstance ();
4906 NPObject *window = NULL;
4907 NPIdentifier identifier = NPN_GetStringIdentifier (name);
4908 if (npobj == NULL) {
4909 NPN_GetValue (npp, NPNVWindowNPObject, &window);
4910 npobj = window;
4913 return NPN_HasProperty (npp, npobj, identifier);
4916 void
4917 html_object_get_property (PluginInstance *plugin, NPObject *npobj, char *name, Value *result)
4919 NPVariant npresult;
4920 NPObject *window = NULL;
4921 NPP npp = plugin->GetInstance ();
4922 NPIdentifier identifier = NPN_GetStringIdentifier (name);
4924 if (npobj == NULL) {
4925 NPN_GetValue (npp, NPNVWindowNPObject, &window);
4926 npobj = window;
4929 bool ret = NPN_GetProperty (npp, npobj, identifier, &npresult);
4931 if (ret) {
4932 Value *res = NULL;
4933 if (!NPVARIANT_IS_VOID (npresult) && !NPVARIANT_IS_NULL (npresult)) {
4934 variant_to_value (&npresult, &res);
4935 *result = *res;
4936 } else {
4937 *result = Value (Type::INVALID);
4939 } else {
4940 // can't throw exceptions here for now, they
4941 // get stuck in firefox and subsequent eval calls
4942 // will fail
4943 // THROW_JS_EXCEPTION2 (npobj, name);
4944 *result = Value (Type::INVALID);
4948 void
4949 html_object_set_property (PluginInstance *plugin, NPObject *npobj, char *name, Value *value)
4951 NPVariant npvalue;
4952 NPObject *window = NULL;
4953 NPP npp = plugin->GetInstance ();
4954 NPIdentifier identifier = NPN_GetStringIdentifier (name);
4956 if (npobj == NULL) {
4957 NPN_GetValue (npp, NPNVWindowNPObject, &window);
4958 npobj = window;
4961 value_to_variant (npobj, value, &npvalue);
4963 bool ret = NPN_SetProperty (npp, npobj, identifier, &npvalue);
4964 if (!ret)
4965 d (printf ("Error setting property %s.\n", name));
4968 bool
4969 html_object_invoke (PluginInstance *plugin, NPObject *npobj, char *name,
4970 Value *args, guint32 arg_count, Value *result)
4972 NPVariant npresult;
4973 NPVariant *npargs = NULL;
4974 NPObject *window = NULL;
4975 NPP npp = plugin->GetInstance ();
4976 NPIdentifier identifier = NPN_GetStringIdentifier (name);
4978 if (npobj == NULL) {
4979 NPN_GetValue (npp, NPNVWindowNPObject, &window);
4980 npobj = window;
4983 if (arg_count) {
4984 npargs = new NPVariant [arg_count];
4985 for (guint32 i = 0; i < arg_count; i++)
4986 value_to_variant (npobj, &args [i], &npargs [i]);
4989 bool ret = NPN_Invoke (npp, npobj, identifier, npargs, arg_count, &npresult);
4991 if (arg_count) {
4992 for (guint32 i = 0; i < arg_count; i++)
4993 NPN_ReleaseVariantValue (&npargs [i]);
4994 delete [] npargs;
4997 if (ret)
4999 Value *res = NULL;
5000 if (!NPVARIANT_IS_VOID (npresult) && !NPVARIANT_IS_NULL (npresult)) {
5001 variant_to_value (&npresult, &res);
5002 *result = *res;
5003 } else {
5004 *result = Value (Type::INVALID);
5006 } else {
5007 // can't throw exceptions here for now, they
5008 // get stuck in firefox and subsequent eval calls
5009 // will fail
5010 // THROW_JS_EXCEPTION2 (npobj, name);
5011 *result = Value (Type::INVALID);
5013 return ret;
5016 bool
5017 html_object_invoke_self (PluginInstance *plugin, NPObject *npobj,
5018 Value *args, guint32 arg_count, Value *result)
5020 NPVariant npresult;
5021 NPVariant *npargs = NULL;
5022 NPObject *window = NULL;
5023 NPP npp = plugin->GetInstance ();
5025 if (npobj == NULL) {
5026 NPN_GetValue (npp, NPNVWindowNPObject, &window);
5027 npobj = window;
5030 if (arg_count) {
5031 npargs = new NPVariant [arg_count];
5032 for (guint32 i = 0; i < arg_count; i++)
5033 value_to_variant (npobj, &args [i], &npargs [i]);
5036 bool ret = NPN_InvokeDefault (npp, npobj, npargs, arg_count, &npresult);
5038 if (arg_count) {
5039 for (guint32 i = 0; i < arg_count; i++)
5040 NPN_ReleaseVariantValue (&npargs [i]);
5041 delete [] npargs;
5044 if (ret)
5046 Value *res = NULL;
5047 if (!NPVARIANT_IS_VOID (npresult) && !NPVARIANT_IS_NULL (npresult)) {
5048 variant_to_value (&npresult, &res);
5049 *result = *res;
5050 } else {
5051 *result = Value (Type::INVALID);
5053 } else {
5054 THROW_JS_EXCEPTION2 (npobj, "");
5055 *result = Value (Type::INVALID);
5057 return ret;
5060 const char *
5061 html_get_element_text (PluginInstance *plugin, const char *element_id)
5063 if (!plugin->GetBridge())
5064 return NULL;
5065 return plugin->GetBridge()->HtmlElementGetText(plugin->GetInstance(), element_id);
5068 gpointer
5069 html_object_attach_event (PluginInstance *plugin, NPObject *npobj, char *name, callback_dom_event *cb, gpointer context)
5071 if (!plugin->GetBridge())
5072 return NULL;
5073 return plugin->GetBridge()->HtmlObjectAttachEvent (plugin->GetInstance(), npobj, name, cb, context);
5076 void
5077 html_object_detach_event (PluginInstance *plugin, const char *name, gpointer listener_ptr)
5079 if (!plugin->GetBridge())
5080 return;
5081 plugin->GetBridge()->HtmlObjectDetachEvent (plugin->GetInstance(), name, listener_ptr);
5084 struct release_data {
5085 PluginInstance *plugin;
5086 NPObject *npobj;
5089 static gboolean
5090 html_object_release_callback (gpointer user_data)
5092 release_data *d = (release_data *) user_data;
5093 html_object_release (d->plugin, d->npobj);
5094 d->plugin->unref ();
5095 g_free (d);
5096 return false;
5099 void
5100 html_object_release (PluginInstance *plugin, NPObject *npobj)
5102 if (npobj == NULL)
5103 return;
5105 if (!Surface::InMainThread ()) {
5106 release_data *d = (release_data *) g_malloc (sizeof (release_data));
5107 plugin->ref ();
5108 d->plugin = plugin;
5109 d->npobj = npobj;
5110 g_timeout_add_full (1, G_PRIORITY_DEFAULT, html_object_release_callback, d, NULL);
5111 return;
5114 if (plugin->HasShutdown ()) {
5115 // printf ("html_object_release (%p, %p): plugin has shut down, object has most probably been deleted already.\n", plugin, npobj);
5116 return;
5119 NPN_ReleaseObject (npobj);
5122 void
5123 html_object_retain (PluginInstance *plugin, NPObject *npobj)
5125 if (npobj == NULL)
5126 return;
5129 * trying to use a NPObject when the plugin has shut down is a very bad idea
5130 * since the NPObject has most probably been deleted already.
5132 g_return_if_fail (!plugin->HasShutdown ());
5135 * we can't marshal this to the main thread (if we're not already executing
5136 * in it), since the object might end up deleted by the time the main thread
5137 * retains it
5140 NPN_RetainObject (npobj);
5143 void
5144 browser_do_alert (PluginInstance *plugin, char *msg)
5146 NPVariant npresult, npargs;
5147 NPObject *window = NULL;
5148 NPP npp = plugin->GetInstance ();
5149 NPIdentifier identifier = NPN_GetStringIdentifier ("alert");
5151 NPN_GetValue (npp, NPNVWindowNPObject, &window);
5152 string_to_npvariant (msg, &npargs);
5154 NPN_Invoke (npp, window, identifier, &npargs, 1, &npresult);
5158 void
5159 plugin_init_classes (void)
5162 * All classes that derive from MoonlightDependencyObject should be stored in the dependency_object_classes
5163 * array, so that we can verify arguments passed from JS code are valid dependency objects, and not random
5164 * JS objects. ie element.children.add (new Array ())
5167 dependency_object_classes [COLLECTION_CLASS] = new MoonlightCollectionType ();
5168 dependency_object_classes [DEPENDENCY_OBJECT_CLASS] = new MoonlightDependencyObjectType ();
5169 dependency_object_classes [DOWNLOADER_CLASS] = new MoonlightDownloaderType ();
5170 dependency_object_classes [IMAGE_BRUSH_CLASS] = new MoonlightImageBrushType ();
5171 dependency_object_classes [IMAGE_CLASS] = new MoonlightImageType ();
5172 dependency_object_classes [MEDIA_ELEMENT_CLASS] = new MoonlightMediaElementType ();
5173 dependency_object_classes [STORYBOARD_CLASS] = new MoonlightStoryboardType ();
5174 dependency_object_classes [STYLUS_INFO_CLASS] = new MoonlightStylusInfoType ();
5175 dependency_object_classes [STYLUS_POINT_COLLECTION_CLASS] = new MoonlightStylusPointCollectionType ();
5176 dependency_object_classes [STROKE_COLLECTION_CLASS] = new MoonlightStrokeCollectionType ();
5177 dependency_object_classes [STROKE_CLASS] = new MoonlightStrokeType ();
5178 dependency_object_classes [TEXT_BLOCK_CLASS] = new MoonlightTextBlockType ();
5179 dependency_object_classes [UI_ELEMENT_CLASS] = new MoonlightUIElementType ();
5180 dependency_object_classes [CONTROL_CLASS] = new MoonlightControlType ();
5181 dependency_object_classes [TEXT_BOX_CLASS] = new MoonlightTextBoxType ();
5182 dependency_object_classes [PASSWORD_BOX_CLASS] = new MoonlightPasswordBoxType ();
5183 dependency_object_classes [MULTI_SCALE_IMAGE_CLASS] = new MoonlightMultiScaleImageType ();
5184 /* Event Arg Types */
5185 dependency_object_classes [EVENT_ARGS_CLASS] = new MoonlightEventArgsType ();
5186 dependency_object_classes [ROUTED_EVENT_ARGS_CLASS] = new MoonlightRoutedEventArgsType ();
5187 dependency_object_classes [ERROR_EVENT_ARGS_CLASS] = new MoonlightErrorEventArgsType ();
5188 dependency_object_classes [KEY_EVENT_ARGS_CLASS] = new MoonlightKeyEventArgsType ();
5189 dependency_object_classes [MARKER_REACHED_EVENT_ARGS_CLASS] = new MoonlightMarkerReachedEventArgsType ();
5190 dependency_object_classes [MOUSE_EVENT_ARGS_CLASS] = new MoonlightMouseEventArgsType ();
5191 dependency_object_classes [DOWNLOAD_PROGRESS_EVENT_ARGS_CLASS] = new MoonlightDownloadProgressEventArgsType ();
5193 MoonlightContentClass = new MoonlightContentType ();
5194 MoonlightDurationClass = new MoonlightDurationType ();
5195 MoonlightEventObjectClass = new MoonlightEventObjectType ();
5196 MoonlightObjectClass = new MoonlightObjectType ();
5197 MoonlightPointClass = new MoonlightPointType ();
5198 MoonlightRectClass = new MoonlightRectType ();
5199 MoonlightScriptableObjectClass = new MoonlightScriptableObjectType ();
5200 MoonlightScriptControlClass = new MoonlightScriptControlType ();
5201 MoonlightSettingsClass = new MoonlightSettingsType ();
5202 MoonlightTimeSpanClass = new MoonlightTimeSpanType ();
5203 MoonlightKeyTimeClass = new MoonlightKeyTimeType ();
5204 MoonlightThicknessClass = new MoonlightThicknessType ();
5205 MoonlightCornerRadiusClass = new MoonlightCornerRadiusType ();
5206 MoonlightGridLengthClass = new MoonlightGridLengthType ();
5209 void
5210 plugin_destroy_classes (void)
5212 for (int i = 0; i < DEPENDENCY_OBJECT_CLASS_NAMES_LAST; i++) {
5213 delete dependency_object_classes [i];
5214 dependency_object_classes [i] = NULL;
5217 delete MoonlightContentClass; MoonlightContentClass = NULL;
5218 delete MoonlightEventObjectClass; MoonlightEventObjectClass = NULL;
5219 delete MoonlightErrorEventArgsClass; MoonlightErrorEventArgsClass = NULL;
5220 delete MoonlightMouseEventArgsClass; MoonlightMouseEventArgsClass = NULL;
5221 delete MoonlightDownloadProgressEventArgsClass; MoonlightDownloadProgressEventArgsClass = NULL;
5222 delete MoonlightKeyEventArgsClass; MoonlightKeyEventArgsClass = NULL;
5223 delete MoonlightObjectClass; MoonlightObjectClass = NULL;
5224 delete MoonlightScriptableObjectClass; MoonlightScriptableObjectClass = NULL;
5225 delete MoonlightScriptControlClass; MoonlightScriptControlClass = NULL;
5226 delete MoonlightSettingsClass; MoonlightSettingsClass = NULL;
5227 delete MoonlightRectClass; MoonlightRectClass = NULL;
5228 delete MoonlightPointClass; MoonlightPointClass = NULL;
5229 delete MoonlightDurationClass; MoonlightDurationClass = NULL;
5230 delete MoonlightTimeSpanClass; MoonlightTimeSpanClass = NULL;
5231 delete MoonlightKeyTimeClass; MoonlightKeyTimeClass = NULL;
5232 delete MoonlightThicknessClass; MoonlightThicknessClass = NULL;
5233 delete MoonlightCornerRadiusClass; MoonlightCornerRadiusClass = NULL;
5234 delete MoonlightGridLengthClass; MoonlightGridLengthClass = NULL;
5235 delete MoonlightMarkerReachedEventArgsClass; MoonlightMarkerReachedEventArgsClass = NULL;