home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / plugins / nullplugin / npshell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.6 KB  |  375 lines

  1. /* -*- Mode: C; tab-width: 8; 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.  * npshell.c
  20.  *
  21.  * Netscape Client Plugin API
  22.  * - Function that need to be implemented by plugin developers
  23.  *
  24.  * This file defines a "shell" plugin that plugin developers can use
  25.  * as the basis for a real plugin.  This shell just provides empty
  26.  * implementations of all functions that the plugin can implement
  27.  * that will be called by Netscape (the NPP_xxx methods defined in 
  28.  * npapi.h). 
  29.  *
  30.  * dp Suresh <dp@netscape.com>
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <X11/Xlib.h>
  35. #include "npapi.h"
  36.  
  37. #include <Xm/Xm.h>
  38. #include <Xm/Form.h>
  39. #include <Xm/PushB.h>
  40. #include "nullplugin.h"
  41.  
  42. /***********************************************************************
  43.  *
  44.  * Implementations of plugin API functions
  45.  *
  46.  ***********************************************************************/
  47.  
  48. char*
  49. NPP_GetMIMEDescription(void)
  50. {
  51.     return(MIME_TYPES_HANDLED);
  52. }
  53.  
  54. NPError
  55. NPP_GetValue(void *future, NPPVariable variable, void *value)
  56. {
  57.     NPError err = NPERR_NO_ERROR;
  58.  
  59.     switch (variable) {
  60.         case NPPVpluginNameString:
  61.             *((char **)value) = PLUGIN_NAME;
  62.             break;
  63.         case NPPVpluginDescriptionString:
  64.             *((char **)value) = PLUGIN_DESCRIPTION;
  65.             break;
  66.         default:
  67.             err = NPERR_GENERIC_ERROR;
  68.     }
  69.     return err;
  70. }
  71.  
  72. NPError
  73. NPP_Initialize(void)
  74. {
  75.     return NPERR_NO_ERROR;
  76. }
  77.  
  78. jref
  79. NPP_GetJavaClass()
  80. {
  81.     return NULL;
  82. }
  83.  
  84. void
  85. NPP_Shutdown(void)
  86. {
  87. }
  88.  
  89.  
  90. NPError 
  91. NPP_New(NPMIMEType pluginType,
  92.     NPP instance,
  93.     uint16 mode,
  94.     int16 argc,
  95.     char* argn[],
  96.     char* argv[],
  97.     NPSavedData* saved)
  98. {
  99.         PluginInstance* This;
  100.  
  101.     if (instance == NULL)
  102.         return NPERR_INVALID_INSTANCE_ERROR;
  103.         
  104.     instance->pdata = NPN_MemAlloc(sizeof(PluginInstance));
  105.     
  106.     This = (PluginInstance*) instance->pdata;
  107.     memset(This, 0, sizeof(PluginInstance));
  108.  
  109.     if (This != NULL)
  110.     {
  111.         /* mode is NP_EMBED, NP_FULL, or NP_BACKGROUND (see npapi.h) */
  112.         This->mode = mode;
  113.         This->window = (Window) 0;
  114.         This->type = dupMimeType(pluginType);
  115.         This->instance = instance;
  116.         This->pluginsPageUrl = NULL;
  117.  
  118.         /* Parse argument list passed to plugin instance */
  119.         /* We are interested in these arguments
  120.          *    PLUGINSPAGE = <url>
  121.          */
  122.         while (argc > 0)
  123.         {
  124.             if (argv[argc-1] != NULL)
  125.             {
  126.             if (!strcasecmp(argn[argc-1], "PLUGINSPAGE"))
  127.                 This->pluginsPageUrl = strdup(argv[argc-1]);
  128.             else if (!strcasecmp(argn[argc-1], "PLUGINURL"))
  129.                 This->pluginsFileUrl = strdup(argv[argc-1]);
  130.             else if (!strcasecmp(argn[argc-1], "CODEBASE"))
  131.                 This->pluginsPageUrl = strdup(argv[argc-1]);
  132.             else if (!strcasecmp(argn[argc-1], "CLASSID"))
  133.                 This->pluginsFileUrl = strdup(argv[argc-1]);
  134.             }
  135.             argc --;
  136.         }
  137.  
  138.         return NPERR_NO_ERROR;
  139.     }
  140.     else
  141.         return NPERR_OUT_OF_MEMORY_ERROR;
  142. }
  143.  
  144.  
  145. NPError 
  146. NPP_Destroy(NPP instance, NPSavedData** save)
  147. {
  148.     PluginInstance* This;
  149.  
  150.     if (instance == NULL)
  151.         return NPERR_INVALID_INSTANCE_ERROR;
  152.  
  153.     This = (PluginInstance*) instance->pdata;
  154.  
  155.     if (This != NULL) {
  156.         if (This->type)
  157.             NPN_MemFree(This->type);
  158.         if (This->pluginsPageUrl)
  159.             NPN_MemFree(This->pluginsPageUrl);
  160.         if (This->pluginsFileUrl)
  161.                 NPN_MemFree(This->pluginsFileUrl);
  162.         NPN_MemFree(instance->pdata);
  163.         instance->pdata = NULL;
  164.     }
  165.  
  166.     return NPERR_NO_ERROR;
  167. }
  168.  
  169.  
  170.  
  171. NPError 
  172. NPP_SetWindow(NPP instance, NPWindow* window)
  173. {
  174.     PluginInstance* This;
  175.     NPSetWindowCallbackStruct *ws_info;
  176.  
  177.     if (instance == NULL)
  178.         return NPERR_INVALID_INSTANCE_ERROR;
  179.  
  180.     This = (PluginInstance*) instance->pdata;
  181.     ws_info = (NPSetWindowCallbackStruct *)window->ws_info;
  182.  
  183.     if (window->window == NULL) {
  184.         /* The page with the plugin is being resized.
  185.            Save any UI information because the next time
  186.            around expect a SetWindow with a new window
  187.            id.
  188.         */
  189. #ifdef DEBUG
  190.         fprintf(stderr, "Nullplugin: plugin received window resize.\n");
  191. #endif
  192.         return NPERR_NO_ERROR;
  193.     }
  194.  
  195.     This->window = (Window) window->window;
  196.     This->x = window->x;
  197.     This->y = window->y;
  198.     This->width = window->width;
  199.     This->height = window->height;
  200.     This->display = ws_info->display;
  201.     This->visual = ws_info->visual;
  202.     This->depth = ws_info->depth;
  203.     This->colormap = ws_info->colormap;
  204.  
  205.     {
  206.         Widget netscape_widget;
  207.         Widget form;
  208.         Arg av[20];
  209.         int ac;
  210.         XmString xmstr = XmStringCreateLtoR("Download Plugin", XmSTRING_DEFAULT_CHARSET);
  211.  
  212.         netscape_widget = XtWindowToWidget(This->display, This->window);
  213.  
  214.         ac = 0;
  215.         XtSetArg(av[ac], XmNx, 0); ac++;
  216.         XtSetArg(av[ac], XmNy, 0); ac++;
  217.         XtSetArg(av[ac], XmNwidth, This->width); ac++;
  218.         XtSetArg(av[ac], XmNheight, This->height); ac++;
  219.         XtSetArg(av[ac], XmNborderWidth, 0); ac++;
  220.         XtSetArg(av[ac], XmNnoResize, True); ac++;
  221.         form = XmCreateForm(netscape_widget, "pluginForm",
  222.                     av, ac);
  223.  
  224.         ac = 0;
  225.         XtSetArg(av[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
  226.         XtSetArg(av[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
  227.         XtSetArg(av[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
  228.         XtSetArg(av[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
  229.         XtSetArg (av[ac], XmNeditMode, XmMULTI_LINE_EDIT); ac++;
  230.         XtSetArg (av[ac], XmNlabelString, xmstr); ac++;
  231.         This->button = XmCreatePushButton (form, "pluginButton", av, ac);
  232.         XtAddCallback (This->button, XmNactivateCallback, showPluginDialog,
  233.                 (XtPointer)This);
  234.  
  235.         XtManageChild(This->button);
  236.         XtManageChild(form);
  237.  
  238.         /* Popup the pluginDialog if this is the first time we encounter
  239.            this MimeType */
  240.         if (addToList(&head, This->type))
  241.         showPluginDialog(This->button, (XtPointer) This, NULL);
  242.  
  243.         XmStringFree(xmstr);
  244.     }
  245.  
  246.     return NPERR_NO_ERROR;
  247. }
  248.  
  249.  
  250. NPError 
  251. NPP_NewStream(NPP instance,
  252.           NPMIMEType type,
  253.           NPStream *stream, 
  254.           NPBool seekable,
  255.           uint16 *stype)
  256. {
  257.     PluginInstance* This;
  258.  
  259.     if (instance == NULL)
  260.         return NPERR_INVALID_INSTANCE_ERROR;
  261.  
  262.     This = (PluginInstance*) instance->pdata;
  263.  
  264.     return NPERR_NO_ERROR;
  265. }
  266.  
  267.  
  268. int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
  269.                    * mode so we can take any size stream in our
  270.                    * write call (since we ignore it) */
  271.  
  272. int32 
  273. NPP_WriteReady(NPP instance, NPStream *stream)
  274. {
  275.     PluginInstance* This;
  276.     if (instance != NULL)
  277.         This = (PluginInstance*) instance->pdata;
  278.  
  279.     /* Number of bytes ready to accept in NPP_Write() */
  280.     return STREAMBUFSIZE;
  281. }
  282.  
  283.  
  284. int32 
  285. NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
  286. {
  287.     PluginInstance* This;
  288.  
  289.     if (instance != NULL)
  290.         This = (PluginInstance*) instance->pdata;
  291.     else
  292.         return len;
  293.  
  294.     return len;        /* The number of bytes accepted */
  295. }
  296.  
  297.  
  298. NPError 
  299. NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason)
  300. {
  301.     PluginInstance* This;
  302.  
  303.     if (instance == NULL)
  304.         return NPERR_INVALID_INSTANCE_ERROR;
  305.     This = (PluginInstance*) instance->pdata;
  306.  
  307.     return NPERR_NO_ERROR;
  308. }
  309.  
  310.  
  311. void 
  312. NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
  313. {
  314.     PluginInstance* This;
  315.     if (instance != NULL)
  316.         This = (PluginInstance*) instance->pdata;
  317. }
  318.  
  319.  
  320. void 
  321. NPP_Print(NPP instance, NPPrint* printInfo)
  322. {
  323.     if(printInfo == NULL)
  324.         return;
  325.  
  326.     if (instance != NULL) {
  327.         PluginInstance* This = (PluginInstance*) instance->pdata;
  328.     
  329.         if (printInfo->mode == NP_FULL) {
  330.             /*
  331.              * PLUGIN DEVELOPERS:
  332.              *    If your plugin would like to take over
  333.              *    printing completely when it is in full-screen mode,
  334.              *    set printInfo->pluginPrinted to TRUE and print your
  335.              *    plugin as you see fit.  If your plugin wants Netscape
  336.              *    to handle printing in this case, set
  337.              *    printInfo->pluginPrinted to FALSE (the default) and
  338.              *    do nothing.  If you do want to handle printing
  339.              *    yourself, printOne is true if the print button
  340.              *    (as opposed to the print menu) was clicked.
  341.              *    On the Macintosh, platformPrint is a THPrint; on
  342.              *    Windows, platformPrint is a structure
  343.              *    (defined in npapi.h) containing the printer name, port,
  344.              *    etc.
  345.              */
  346.  
  347.             void* platformPrint =
  348.                 printInfo->print.fullPrint.platformPrint;
  349.             NPBool printOne =
  350.                 printInfo->print.fullPrint.printOne;
  351.             
  352.             /* Do the default*/
  353.             printInfo->print.fullPrint.pluginPrinted = FALSE;
  354.         }
  355.         else {    /* If not fullscreen, we must be embedded */
  356.             /*
  357.              * PLUGIN DEVELOPERS:
  358.              *    If your plugin is embedded, or is full-screen
  359.              *    but you returned false in pluginPrinted above, NPP_Print
  360.              *    will be called with mode == NP_EMBED.  The NPWindow
  361.              *    in the printInfo gives the location and dimensions of
  362.              *    the embedded plugin on the printed page.  On the
  363.              *    Macintosh, platformPrint is the printer port; on
  364.              *    Windows, platformPrint is the handle to the printing
  365.              *    device context.
  366.              */
  367.  
  368.             NPWindow* printWindow =
  369.                 &(printInfo->print.embedPrint.window);
  370.             void* platformPrint =
  371.                 printInfo->print.embedPrint.platformPrint;
  372.         }
  373.     }
  374. }
  375.