home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / src / edtplugStubs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.4 KB  |  174 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef XP_MAC
  20. #define IMPLEMENT_netscape_plugin_composer_PluginManager
  21. #define IMPLEMENT_netscape_plugin_composer_MozillaCallback
  22. #define IMPLEMENT_netscape_plugin_composer_Composer
  23. #include "_jri/netscape_plugin_composer_PluginManager.c"
  24. #include "_jri/netscape_plugin_composer_MozillaCallback.c"
  25. #include "_jri/netscape_plugin_composer_Composer.c"
  26. #else
  27. #define IMPLEMENT_netscape_plugin_composer_PluginManager
  28. #define IMPLEMENT_netscape_plugin_composer_MozillaCallback
  29. #define IMPLEMENT_netscape_plugin_composer_Composer
  30. #include "n_p_composer_PluginManager.c"
  31. #include "n_p_composer_MozillaCallback.c"
  32. #include "n_plugin_composer_Composer.c"
  33. #endif
  34.  
  35. #ifndef NSPR20
  36. #include "prevent.h"
  37. #else
  38. #include "plevent.h"
  39. #endif
  40. #include "prmon.h"
  41. #include "prmem.h"
  42. #include "edtplug.h"
  43.  
  44. extern PREventQueue* mozilla_event_queue;
  45.  
  46. /* Dummy method used to ensure this code is linked into the main application */
  47. void _java_edtplug_init(void) { }
  48.  
  49.  
  50. /* Call back from plugin thread into the editor. */
  51.  
  52. /******************************************************************************/
  53.  
  54. typedef struct MozillaEvent_MozillaCallback {
  55.     PREvent    event;
  56.     JRIEnv* env;
  57.     JRIGlobalRef this;
  58. } MozillaEvent_MozillaCallback;
  59.  
  60. typedef void (*ComposerPluginCallback)(jint, jint, struct java_lang_Object*);
  61.  
  62. static EditURLCallback gEditURLCallback;
  63.  
  64. /* 97-05-06 pkc -- declare PR_PUBLIC_API on Mac to force export
  65.  * out of shared library
  66.  */
  67. #ifdef XP_MAC
  68. PR_PUBLIC_API(void) EDTPLUG_RegisterEditURLCallback(EditURLCallback callback){
  69.     gEditURLCallback = callback;
  70. }
  71. #else
  72. void EDTPLUG_RegisterEditURLCallback(EditURLCallback callback){
  73.     gEditURLCallback = callback;
  74. }
  75. #endif /* XP_MAC */ 
  76.  
  77. static JRIEnv* getEnv(MozillaEvent_MozillaCallback* e)
  78. {
  79.     JRIEnv* ee = e->env;
  80.     if ( ! ee ) {
  81.         ee = JRI_GetCurrentEnv();
  82.     }
  83.     return ee;
  84. }
  85.  
  86. static int bMozillaCallbackInitialized = 0;
  87.  
  88. void PR_CALLBACK
  89. edtplug_HandleEvent_MozillaCallback(MozillaEvent_MozillaCallback* e)
  90. {
  91.     JRIEnv* ee = getEnv(e);
  92.     struct netscape_plugin_composer_MozillaCallback* obj =
  93.         (struct netscape_plugin_composer_MozillaCallback*)
  94.             JRI_GetGlobalRef(ee, e->this);
  95.     /* If JavaScript calls netscape.plugin.composer.Document.editDocument(),
  96.      * then this will be the first time that we've ever tried
  97.      * to call into netscape_plugin_composer_MozillaCallback.
  98.      * Therefore, we need to call the use function.
  99.      * We know that it is fast and safe to call
  100.      * multiple times.
  101.      */
  102.     if ( ! bMozillaCallbackInitialized ) {
  103.         use_netscape_plugin_composer_MozillaCallback(ee);
  104.         bMozillaCallbackInitialized = 1;
  105.     }
  106.     netscape_plugin_composer_MozillaCallback_perform(ee, obj);
  107. }
  108.  
  109. void PR_CALLBACK
  110. edtplug_DestroyEvent_MozillaCallback(MozillaEvent_MozillaCallback* event)
  111. {
  112.     JRIEnv* ee = getEnv(event);
  113.     JRI_DisposeGlobalRef(ee, event->this);
  114.     PR_DELETE(event);
  115. }
  116.  
  117. JRI_PUBLIC_API(void)
  118. native_netscape_plugin_composer_MozillaCallback_pEnqueue(
  119.     JRIEnv* env,
  120.     struct netscape_plugin_composer_MozillaCallback* object,
  121.     jint mozenv)
  122. {
  123.     MozillaEvent_MozillaCallback* event;
  124.     PR_ENTER_EVENT_QUEUE_MONITOR(mozilla_event_queue);
  125.  
  126.     event = PR_NEW(MozillaEvent_MozillaCallback);
  127.     if (event == NULL) goto done;
  128.     PR_InitEvent(&event->event, NULL,
  129.                  (PRHandleEventProc)edtplug_HandleEvent_MozillaCallback,
  130.                  (PRDestroyEventProc)edtplug_DestroyEvent_MozillaCallback);
  131.     /* Tell Java not to garbage collect the object that's stored in this event */
  132.     event->env = (JRIEnv*) mozenv;
  133.     event->this = JRI_NewGlobalRef(env, object);
  134.  
  135.     PR_PostEvent(mozilla_event_queue, &event->event);
  136.  
  137.   done:
  138.     PR_EXIT_EVENT_QUEUE_MONITOR(mozilla_event_queue);
  139. }
  140.  
  141. JRI_PUBLIC_API(void)
  142. native_netscape_plugin_composer_Composer_mtCallback(
  143.     JRIEnv* env,
  144.     netscape_plugin_composer_Composer* this,
  145.     jint context,
  146.     jint callback,
  147.     jint action,
  148.     struct java_lang_Object* arg)
  149. {
  150.     ComposerPluginCallback f;
  151.  
  152.     f = (ComposerPluginCallback) callback;
  153.  
  154.     if ( f ){
  155.         (*f)(context, action, arg);
  156.     }
  157.     else {
  158.         /* Static callbacks */
  159.         switch(action ) {
  160.             case 4: /* OpenEditor Must match Composer.PLUGIN_EDITURL */
  161.                 {
  162.                     /* str is garbage collected by Java. */
  163.                     const char* str = JRI_GetStringPlatformChars(env, (struct java_lang_String*)arg, "", 0);
  164.                     if ( str ) {
  165.                         if ( gEditURLCallback ) {
  166.                             (*gEditURLCallback)(str);
  167.                         }
  168.                     }
  169.                 }
  170.             break;
  171.         }
  172.     }
  173. }
  174.