This chapter tells how to determine whether a plug-in is windowed or windowless, how to draw and redraw plug-ins, and how to handle plug-in events.
NOTE: Windowless plug-ins are currently not supported on the X Windows platform. §[Top]
NPWindow
structure represents either the native window or a drawable. This structure contains information about coordinate position, size, the state of the plug-in (windowed or windowless), and some platform-specific information.
NOTE: When a plug-in is drawn to a window, the plug-in is responsible for preserving state information and ensuring that the original state is restored. §For windowless plug-ins, Communicator calls the
NPP_SetWindow
method with an NPWindow
structure that represents a drawable. For windowed plug-ins, Communicator calls the NPP_SetWindow
method with an NPWindow
structure that represents a window.
The NPWindow Structure
typedef enum {
NPWindowTypeWindow = 1,
NPWindowTypeDrawable
} NPWindowType;
typedef struct _NPWindow
{
void* window; /* Platform-specific handle */
uint32 x; /* Position of top-left corner */
uint32 y; /* relative to a Netscape page */
uint32 width; /* Maximum window size */
uint32 height;
NPRect clipRect; /* Clipping rectangle in port coordinates */
#ifdef XP_UNIX
void * ws_info; /* Platform-dependent additional data */
#endif /* XP_UNIX */
NPWindowType type; /* Whether this is a window or a drawable */} NPWindow;
The window
parameter is a platform-specific handle to a native window element in the Netscape window hierarchy on Windows and Unix. On Mac OS, window is a pointer to an NP_Port
.
The x
and y
fields specify the top-left corner of the plug-in relative to the page.
The width
and height
fields specify the dimensions of the plug-in area. These values should not be modified by the plug-in.
The clipRect
field defines the clipping rectangle of the plug-in in a coordinate system where the origin is the top-left corner of the drawable or window. Communicator calls NPP_SetWindow
whenever the drawable changes.
The type
field indicates the NPWindow
type of the target area:
NPWindowTypeDrawable
: Windowless plug-in. The window
field holds a platform-specific handle to a drawable, as follows: Windows:
structure.
HDC
Mac OS: pointer to
NP_Port
NPP_HandleEvent
: Deliver a platform-specific event to the instance.
NPP_Print
: Request a platform-specific print operation for the instance.
NPN_ForceRedraw
: Force a paint message to a windowless plug-in.
NPN_InvalidateRect
: Invalidate an area in a windowless plug-in before repainting or refreshing.
NPN_InvalidateRegion
: Invalidate a region in a windowless plug-in before repainting or refreshing.
NPP_Print
method to ask the plug-in instance to print itself.
void NPP_Print(NPP instance, NPPrint *printInfo);The
instance
parameter represents the current plug-in.
The PrintInfo
parameter determines the print mode. It is set to either NP_FULL
to indicate full-page plug-in printing, or NP_EMBED
if this is an embedded plug-in printed as part of the window in which it is embedded.
NPP_Print
is also called with PrintInfo->mode
equal to NP_EMBED
when the instance is embedded. In this case, platformPrint->embedPrint.window
contains the window in which the plug-in should print.
On MS Windows, note that the coordinates for the window rectangle are in TWIPS format. For this reason, you need to convert the x- and y-coordinates using the Windows API call DPtoLP
when you output text.
[Top] [Drawing Plug-ins]
NPP_SetWindow
function to set the window in which a plug-in draws or returns an error code. This window is valid for the life of the instance, or until NPP_SetWindow
is called again with a different value.
Subsequent calls to NPP_SetWindow
for a given instance usually mean that the window has been resized. If either window
or window->window
is null
, the plug-in must not perform any additional graphics operations on the window and should free any associated resources.
NPError NPP_SetWindow(NPP instance, NPWindow *window);The
instance
parameter represents the current plug-in.
The window
parameter is a pointer to the drawing target for the plug-in. For windowless plug-ins, the platform-specific window information specified in window->window
is a platform-specific handle to a drawable.
MS Windows and
Unix
For windowed plug-ins on MS Windows and Unix, the window->window
field
is a handle to a subwindow of the Netscape window hierarchy. §
Mac OS The[Top] [Setting the Window]window->window
field points to anNP_Port
structure. §
NPN_GetValue
method.
NPError NPN_GetValue(NPP instance,The
NPNVariable variable, void *value);
instance
parameter represents the current plug-in.
Unix and MS Windows The queried information is returned in theThevariable
parameter. This parameter is valid only for the Unix and MS Windows platforms. For Unix, the values are either the current display (NPNVxDisplay
) or the application's context (NPNVxtAppContext
). For MS Windows, the value is the native window on which plug-in drawing occurs (NPNVnetscapeWindow
). §
value
parameter contains the name of the plug-in.
You can also use NPN_GetValue
to help create a menu or dialog box for a windowless plug-in.
NOTE: The windowed plug-in was the only plug-in type in Navigator 2.0 and Navigator 3.0. §On Mac OS, Communicator does not give a windowed plug-in a native window, because the Mac OS platform does not support child windows. Instead, the windowed plug-in draws into the graphics port associated with the Communicator window, at the offset that Communicator specifies.
On MS Windows and Unix, Communicator creates a child window for each plug-in instance and passes it a window through NPP_SetWindow
. On Mac OS, the application uses
NPP_SetWindow
to dedicate a rectangular part of its graphics port to each instance. On any platform, the browser should be careful not to draw in the plug-in's area, and vice versa. The data structure passed in NPP_SetWindow
is an NPWindow
object, which contains the coordinates of the instance's area and various platform-specific data.
Typically, Communicator calls NPP_SetWindow
after creating the instance so that the plug-in can begin drawing immediately. However, the browser can create invisible instances for which NPP_SetWindow
is never called and a window is never created. This happens when plug-ins are invoked with the HTML EMBED
tag with the HIDDEN
attribute.
Communicator should call NPP_SetWindow
again whenever the size or position of the instance changes, passing it the same NPWindow
object each time, but with different values.
Communicator can also call NPP_SetWindow
multiple times with different values for the window, including null
. For example, if a user removes an instance from the page, Communicator should call NPP_SetWindow
with a window value of null
. This value prevents the instance from drawing further until it is pasted back on the page and NPP_SetWindow
is called again with a new value.
NP_Port
structure in the window
field of the NPWindow
structure. This structure contains a pointer to the graphics port (CGraphPtr
) into which the plug-in instance should draw and the x- and y-coordinates of the upper-left corner of this port. The plug-in can use these coordinates to call SetOrigin(portx, porty)
to place the upper-left corner of its rectangle at (0,0). The Mac OS GrafPort
structure's clipRgn
field should be set to the clipping rectangle for the instance in port coordinates.
NPP_HandleEvent
; for a complete list of event types, see the reference entry for
NPEvent
.
window
field of NPWindow
. Additionally, Communicator creates an
NPSetWindowCallbackStruct
object and passes it in the ws_info
field of NPWindow
. As on MS Windows, the plug-in can receive all events for the instance, in this case through the widget. If the plug-in needs to receive periodic time messages, it should install a timer or fork a thread.
NPP_SetWindow
. NPP_SetWindow
passes the instance an NPWindow
object containing the native window handle.
MS Windows and Unix
Each instance receives its own child window within the Communicator window
hierarchy, and imaging and event processing are relative to this window. §
Mac OS
This platform does not support child windows. The native window is shared
between the instance and Communicator. The instance must restrict its drawing
to a specified area of the shared window, and it must always save the current
settings, set up the drawing environment, and restore the shared drawing
environment to the previous settings. On Mac OS, events are explicitly
provided to the instance by NPP_HandleEvent
. §
[Top] [Windowed Plug-ins]
HDC
on Windows or CGrafPtr
on Mac OS), which can either be on-screen or off-screen.
Windowless plug-ins provide the plug-in writer with some significant design possibilities:
NPN_SetValue
method.
NPError NPN_SetValue(NPP instance,The
NPPVariable variable, void *value);
instance
parameter represents the current plug-in. The variable
parameter contains plug-in information to set. The value
parameter returns the name of the plug-in.
To specify that a plug-in is windowless, use NPN_SetValue
with NPPVpluginWindowBool
as the value of variable
and false
as the value of value
. The plug-in makes this call from its
NPP_New
method. If a plug-in does not make this call, it is considered a windowed plug-in.
NPN_SetValue
[Top] [Windowless Plug-ins]
typedef enum {
...
...
NPPVpluginWindowBool,
NPPVpluginTransparentBool
} NPPVariable;
NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value);
NPN_InvalidateRect
or NPN_InvalidateRegion
. Both methods perform the same operations:
NPN_ForceRedraw
after calling one of the invalidate methods. If a plug-in calls one of these methods, it receives a paint message asynchronously.
void NPN_InvalidateRect(NPP instance, NPRect *invalidRect);
void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion);The
instance
parameter represents the current plug-in. The invalidRect
and invalidRegion
parameters represent the area to invalidate, specified in a coordinate system whose origin is at the top left of the plug-in.
Both methods cause the
NPP_HandleEvent
method to pass an update event or a paint message to the plug-in.
#ifdef XP_MAC
[Top] [Windowless Plug-ins]
typedef RgnHandle NPRegion;
#elif defined(XP_WIN)
typedef HRGN NPRegion;
#elif defined(XP_UNIX)
typedef Region NPRegion;
#else
typedef void* NPRegion;
#endif /* XP_MAC */
void NPN_InvalidateRect(NPP instance, NPRect *invalidRect);
void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion);
NPN_ForceRedraw
to force a paint message synchronously, once an area has been invalidated with NPN_InvalidateRect
or NPN_InvalidateRegion
.
void NPN_ForceRedraw(NPP instance);
This method results in a synchronous update event or paint message for the plug-in.
A plug-in must not draw into its drawable unless it receives a paint message. It does not need to call the platform-specific function to begin painting within a window. That is, the plug-in does not call BeginPaint
on Windows or BeginUpdate
on Mac OS.
MS Windows
The plug-in receives a WM_PAINT
message. The lParam
parameter of
WM_PAINT
holds a pointer to an NPRect
structure specifying the bounding box
of the update area. Because the plug-in and Communicator share the same
HDC, the plug-in must save the current settings on the HDC, set up its own
environment, draw itself, and restore the HDC to the previous settings. The
HDC settings must be restored whenever control returns to the Communicator,
either before returning from NPP_HandleEvent
or before calling a drawing-
related Netscape method. §
Mac OS
The plug-in receives an update event. The clip region of the drawable's
CGrafPtr
port is set to the update region. As is the case for windowed plug-
ins on Mac OS, the plug-in must first save the current settings of the port,
setting up the drawing environment as appropriate, drawing, and restoring the
port to the previous setting. This should happen before the plug-in returns from
NP_HandleEvent
or before the plug-in calls a drawing-related Communicator
method. §
[Top] [Windowless Plug-ins]
NPN_SetValue
to set NPPVpluginTransparentBool
to false
. The plug-in can call this method any time after specifying that it is a windowless plug-in.
[Top] [Windowless Plug-ins]
NPN_SetValue
method and set NPPVpluginTransparentBool
to the value true
. The plug-in can call this method any time after specifying that it is a windowless plug-in.
NPN_GetValue
to find out where the plug-in draws. Use NPNVnetscapeWindow
as the value for the variable
parameter.
NPError NPN_GetValue(NPP instance,The
NPNVariable variable, void *value);
instance
parameter represents the current plug-in. The variable
parameter contains the information the call is requesting, in this case NPNVnetscapeWindow
(the native window in which plug-in drawing occurs). The requested information, a value of type HWND
, is returned in the value
parameter.
In many cases, a plug-in may still have to create its own window (a transparent child window of the Netscape window) to act as the owner window for pop-up menus and modal dialog boxes. You can give this transparent child window its own WindowProc
process. The plug-in can use this to deal with WM_COMMAND
messages sent to it as a result of tracking the pop-up menu or modal dialog box.
[Top] [Windowless Plug-ins]
NPP_HandleEvent
method. The plug-in must return true
from NPP_HandleEvent
if it has handled the event and false
if it has not. Mac OS uses this mechanism for both windowed and windowless plug-ins; on this platform, NPP_HandleEvent
is the only way the plug-in can receive events from its host application.
int16 NPP_HandleEvent(NPP instance, NPEvent *event);The
instance
parameter represents the current plug-in. For a list of event types the application is responsible for delivering to the plug-in, see the
NPEvent
structure.
This code shows the specific data passed through this method for each platform:
#ifdef XP_MAC
On Mac OS, when
typedef EventRecord NPEvent;
#elif defined(XP_WIN)
typedef struct _NPEvent {
int16 event;
int16 wParam;
int32 lParam;
} NPEvent;
#elif defined(XP_UNIX)
typedef XEvent NPEvent;
#else
typedef void NPEvent;
#endif /* XP_MAC */
int16 NPP_HandleEvent(NPP instance, NPEvent* event);NPP_HandleEvent
is called, the current port is set up correctly so that its origin matches the upper-left corner of the plug-in. A plug-in does not need to set up the current port for mouse coordinate translation.
[Top] [Windowless Plug-ins]
Last Updated: 01/15/97 16:36:49