Back to index

lightning-sunbird  0.9+nobinonly
printplugin.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Dantifer Dang <dantifer.dang@sun.com>.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Roland Mainz <roland.mainz@nrubsig.org>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* Xlib/Xt stuff */
00039 #ifdef MOZ_X11
00040 #include <X11/Xlib.h>
00041 #include <X11/Intrinsic.h>
00042 #include <X11/cursorfont.h>
00043 #endif /* MOZ_X11 */
00044 
00045 #include "npapi.h"
00046 #include "printplugin.h"
00047 #include "prprf.h"
00048 
00049 #include <stdlib.h>
00050 #include <stdio.h>
00051 
00052 /* MIMETypeList maintenance routines */
00053 NPMIMEType
00054 dupMimeType(NPMIMEType type)
00055 {
00056     NPMIMEType mimetype = NPN_MemAlloc(strlen(type)+1);
00057     if (mimetype)
00058         strcpy(mimetype, type);
00059     return mimetype;
00060 }
00061 
00062 static void
00063 privatePrintScreenMessage(PluginInstance *This)
00064 {
00065 #ifdef MOZ_X11
00066     GC gc;
00067     unsigned int h,w;
00068     int x,y,l;
00069     const char *string;
00070     gc = XCreateGC(This->display, This->window, 0, NULL);
00071 
00072     /* draw a rectangle */
00073     h = This->height/2;
00074     w = 3 * This->width/4;
00075     x = (This->width - w)/2; /* center */
00076     y = h/2;
00077     XDrawRectangle(This->display, This->window, gc, x, y, w, h);
00078 
00079     /* draw a string */
00080     string = This->pluginsPrintMessage;
00081     if (string && *string)
00082     {
00083         l = strlen(string);
00084         x += This->width/10;
00085         XDrawString(This->display, This->window, gc, x, This->height/2, string, l);
00086     }
00087     XFreeGC(This->display, gc);
00088 #endif /* MOZ_X11 */
00089 }
00090 
00091 static void
00092 setCursor (PluginInstance *This)
00093 {
00094 #ifdef MOZ_X11
00095     static Cursor nullPluginCursor = None;
00096     if (!nullPluginCursor)
00097     {
00098         nullPluginCursor = XCreateFontCursor(This->display, XC_hand2);
00099     }
00100     if (nullPluginCursor)
00101     {
00102         XDefineCursor(This->display, This->window, nullPluginCursor);
00103     }
00104 #endif /* MOZ_X11 */
00105 }
00106 
00107 #ifdef MOZ_X11
00108 static void
00109 xt_event_handler(Widget xt_w, PluginInstance *This, XEvent *xevent, Boolean *b)
00110 {
00111     switch (xevent->type)
00112     {
00113         case Expose:
00114             /* get rid of all other exposure events */
00115             while(XCheckTypedWindowEvent(This->display, This->window, Expose, xevent))
00116                 privatePrintScreenMessage(This);
00117             break;
00118         case ButtonRelease:
00119             break;
00120         default:
00121             break;
00122     }
00123 }
00124 #endif /* MOZ_X11 */
00125 
00126 static void
00127 addXtEventHandler(PluginInstance *This)
00128 {
00129 #ifdef MOZ_X11
00130      Display *dpy = (Display*) This->display;
00131      Window xwin = (Window) This->window;
00132      Widget xt_w = XtWindowToWidget(dpy, xwin);
00133      if (xt_w)
00134      {
00135          long event_mask = ExposureMask | ButtonReleaseMask | ButtonPressMask;
00136          XSelectInput(dpy, xwin, event_mask);
00137          XtAddEventHandler(xt_w, event_mask, False, (XtEventHandler)xt_event_handler, This);
00138      }
00139 #endif /* MOZ_X11 */
00140 }
00141 
00142 void printScreenMessage(PluginInstance *This)
00143 {
00144     privatePrintScreenMessage(This);
00145     setCursor(This);
00146     addXtEventHandler(This);
00147 }
00148 
00149 
00150 void printEPSMessage(PluginInstance *This, FILE *output, NPWindow window)
00151 {
00152     char *string;
00153     int x,y,h,w;
00154     
00155     if (!output)
00156       return;
00157 
00158     fprintf(output, "%%!PS-Adobe-3.0 EPSF-3.0\n");
00159     fprintf(output, "%%%%BoundingBox: 0 0 %d %d\n", window.width, window.height);
00160     fprintf(output, "%%%%EndComments\n");
00161     fprintf(output, "gsave\n");
00162 
00163     w = 3 * window.width  / 4;
00164     h =     window.height / 2;
00165     x = (window.width - w)/2; /* center */
00166     y = h/2;
00167 
00168     /* draw a rectangle */
00169     fprintf(output, "newpath\n");
00170     fprintf(output, "%d %d moveto 0 %d rlineto %d 0 rlineto 0 %d rlineto\n",
00171             x, y, h, w, -h);
00172     fprintf(output, "closepath\n");
00173     fprintf(output, "stroke\n");
00174 
00175     /* draw a string */
00176     string = This->pluginsPrintMessage;
00177     if (string && *string)
00178     {
00179         fprintf(output, "/Times-Roman findfont 300 scalefont setfont\n");
00180         fprintf(output, "%d %d moveto\n", x + window.width/10, window.height / 2);
00181         fprintf(output, "(%s) show\n",string);
00182     }
00183 
00184     fprintf(output,"grestore\n");
00185     fprintf(output,"%%%%EOF\n");
00186 }
00187