WebCore:
[WebKit-https.git] / WebKitExamplePlugins / NetscapeInputMethodPlugin / main.m
1 /*
2  IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
3  consideration of your agreement to the following terms, and your use, installation, 
4  modification or redistribution of this Apple software constitutes acceptance of these 
5  terms.  If you do not agree with these terms, please do not use, install, modify or 
6  redistribute this Apple software.
7  
8  In consideration of your agreement to abide by the following terms, and subject to these 
9  terms, Apple grants you a personal, non-exclusive license, under Appleā€™s copyrights in 
10  this original Apple software (the "Apple Software"), to use, reproduce, modify and 
11  redistribute the Apple Software, with or without modifications, in source and/or binary 
12  forms; provided that if you redistribute the Apple Software in its entirety and without 
13  modifications, you must retain this notice and the following text and disclaimers in all 
14  such redistributions of the Apple Software.  Neither the name, trademarks, service marks 
15  or logos of Apple Computer, Inc. may be used to endorse or promote products derived from 
16  the Apple Software without specific prior written permission from Apple. Except as expressly
17  stated in this notice, no other rights or licenses, express or implied, are granted by Apple
18  herein, including but not limited to any patent rights that may be infringed by your 
19  derivative works or by other works in which the Apple Software may be incorporated.
20  
21  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES, 
22  EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, 
23  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS 
24  USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
25  
26  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 
27  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
28  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
29  REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND 
30  WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR 
31  OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #import <WebKit/npapi.h>
35 #import <WebKit/npfunctions.h>
36 #import <WebKit/npruntime.h>
37 #import <WebKit/nptextinput.h>
38
39 #import <Cocoa/Cocoa.h>
40
41 // Browser function table
42 static NPNetscapeFuncs* browser;
43
44 // Structure for per-instance storage
45 typedef struct PluginObject
46 {
47     NPP npp;
48     
49     NPWindow window;
50     
51     bool pluginHasFocus;
52     
53     bool textFieldHasFocus;
54     NSRect textFieldRect;
55     
56     NSRange markedRange;
57     NSRange selectedRange;
58     NSTextStorage *textStorage;
59     NSLayoutManager *layoutManager;
60     NSTextContainer *textContainer;
61     
62 } PluginObject;
63
64 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved);
65 NPError NPP_Destroy(NPP instance, NPSavedData** save);
66 NPError NPP_SetWindow(NPP instance, NPWindow* window);
67 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype);
68 NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
69 int32 NPP_WriteReady(NPP instance, NPStream* stream);
70 int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer);
71 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
72 void NPP_Print(NPP instance, NPPrint* platformPrint);
73 int16 NPP_HandleEvent(NPP instance, void* event);
74 void NPP_URLNotify(NPP instance, const char* URL, NPReason reason, void* notifyData);
75 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
76 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
77
78 #pragma export on
79 // Mach-o entry points
80 NPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
81 NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
82 void NP_Shutdown(void);
83 #pragma export off
84
85 NPError NP_Initialize(NPNetscapeFuncs* browserFuncs)
86 {
87     browser = browserFuncs;
88     return NPERR_NO_ERROR;
89 }
90
91 NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
92 {
93     pluginFuncs->version = 11;
94     pluginFuncs->size = sizeof(pluginFuncs);
95     pluginFuncs->newp = NPP_New;
96     pluginFuncs->destroy = NPP_Destroy;
97     pluginFuncs->setwindow = NPP_SetWindow;
98     pluginFuncs->newstream = NPP_NewStream;
99     pluginFuncs->destroystream = NPP_DestroyStream;
100     pluginFuncs->asfile = NPP_StreamAsFile;
101     pluginFuncs->writeready = NPP_WriteReady;
102     pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
103     pluginFuncs->print = NPP_Print;
104     pluginFuncs->event = NPP_HandleEvent;
105     pluginFuncs->urlnotify = NPP_URLNotify;
106     pluginFuncs->getvalue = NPP_GetValue;
107     pluginFuncs->setvalue = NPP_SetValue;
108     
109     return NPERR_NO_ERROR;
110 }
111
112 void NP_Shutdown(void)
113 {
114 }
115
116 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
117 {
118     // Create per-instance storage
119     PluginObject *obj = (PluginObject *)malloc(sizeof(PluginObject));
120     bzero(obj, sizeof(PluginObject));
121     
122     obj->npp = instance;
123     instance->pdata = obj;
124     
125     // Ask the browser if it supports the CoreGraphics drawing model
126     NPBool supportsCoreGraphics;
127     if (browser->getvalue(instance, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics) != NPERR_NO_ERROR)
128         supportsCoreGraphics = FALSE;
129     
130     if (!supportsCoreGraphics)
131         return NPERR_INCOMPATIBLE_VERSION_ERROR;
132     
133     // If the browser supports the CoreGraphics drawing model, enable it.
134     browser->setvalue(instance, NPPVpluginDrawingModel, (void *)NPDrawingModelCoreGraphics);
135
136     // If the browser supports the Cocoa event model, enable it.
137     NPBool supportsCocoa;
138     if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR)
139         supportsCocoa = FALSE;
140     
141     if (!supportsCocoa)
142         return NPERR_INCOMPATIBLE_VERSION_ERROR;
143     
144     browser->setvalue(instance, NPPVpluginEventModel, (void *)NPEventModelCocoa);
145
146     obj->textFieldRect = NSMakeRect(10, 10, 200, 100);
147
148     obj->textStorage = [[NSTextStorage alloc] initWithString:@""];
149     obj->layoutManager = [[NSLayoutManager alloc] init];
150     [obj->textStorage addLayoutManager:obj->layoutManager];
151     
152     obj->textContainer = [[NSTextContainer alloc] initWithContainerSize:obj->textFieldRect.size];
153     [obj->layoutManager addTextContainer:obj->textContainer];
154
155     obj->selectedRange.location = [obj->textStorage length];
156     
157     obj->markedRange = NSMakeRange(NSNotFound, 0);
158
159     return NPERR_NO_ERROR;
160 }
161
162 NPError NPP_Destroy(NPP instance, NPSavedData** save)
163 {
164     // Free per-instance storage
165     PluginObject *obj = instance->pdata;
166     
167     [obj->textStorage release];
168     [obj->layoutManager release];
169     
170     free(obj);
171     
172     return NPERR_NO_ERROR;
173 }
174
175 NPError NPP_SetWindow(NPP instance, NPWindow* window)
176 {
177     PluginObject *obj = instance->pdata;
178     obj->window = *window;
179
180     return NPERR_NO_ERROR;
181 }
182  
183
184 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
185 {
186     *stype = NP_ASFILEONLY;
187     return NPERR_NO_ERROR;
188 }
189
190 NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
191 {
192     return NPERR_NO_ERROR;
193 }
194
195 int32 NPP_WriteReady(NPP instance, NPStream* stream)
196 {
197     return 0;
198 }
199
200 int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
201 {
202     return 0;
203 }
204
205 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
206 {
207 }
208
209 void NPP_Print(NPP instance, NPPrint* platformPrint)
210 {
211
212 }
213
214 static void handleDraw(PluginObject *obj)
215 {
216     NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
217     
218     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:((NP_CGContext *)obj->window.window)->context
219                                                                             flipped:YES];
220
221
222     [NSGraphicsContext setCurrentContext:context];
223     
224     NSRect rect = NSMakeRect(0, 0, obj->window.width, obj->window.height);
225     
226     [[NSColor lightGrayColor] set];
227     [NSBezierPath fillRect:rect];
228
229     if (obj->pluginHasFocus) {
230         [[NSColor blackColor] set];
231         [NSBezierPath strokeRect:rect];
232     }
233     
234     [[NSColor whiteColor] set];
235     [NSBezierPath fillRect:obj->textFieldRect];
236
237     // Draw the text
238     NSRange glyphRange = [obj->layoutManager glyphRangeForTextContainer:obj->textContainer];
239     if (glyphRange.length > 0) {
240         [obj->layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:obj->textFieldRect.origin];
241         [obj->layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:obj->textFieldRect.origin];
242     }
243     
244     NSBezierPath *textInputBorder = [NSBezierPath bezierPathWithRect:obj->textFieldRect];
245     [[NSColor blackColor] set];
246     
247     if (obj->pluginHasFocus && obj->textFieldHasFocus)
248         [textInputBorder setLineWidth:2];
249     else
250         [textInputBorder setLineWidth:1];
251     
252     [textInputBorder stroke];
253     
254     if (obj->pluginHasFocus && obj->textFieldHasFocus) {
255         NSUInteger rectCount;
256         NSRect *rectArray = [obj->layoutManager rectArrayForCharacterRange:obj->selectedRange
257                                             withinSelectedCharacterRange:obj->selectedRange
258                                                         inTextContainer:obj->textContainer
259                                                                 rectCount:&rectCount];
260         
261         [[NSColor blackColor] set];
262         for (unsigned i = 0; i < rectCount; i++) {
263             NSRect rect = rectArray[i];
264             rect.origin.x += obj->textFieldRect.origin.x;
265             rect.origin.y += obj->textFieldRect.origin.y;
266             
267             [NSBezierPath strokeRect:rect];
268         }        
269     }
270     
271     [NSGraphicsContext setCurrentContext:oldContext];
272 }
273
274 static void invalidatePlugin(PluginObject *obj)
275 {
276     NPRect rect;
277     rect.left = 0;
278     rect.top = 0;
279     rect.right = obj->window.width;
280     rect.bottom = obj->window.height;
281     
282     browser->invalidaterect(obj->npp, &rect);    
283 }
284
285 static void handleFocusChanged(NPCocoaEvent *cocoaEvent, PluginObject *obj)
286 {
287     obj->pluginHasFocus = cocoaEvent->event.focus.hasFocus;
288     
289     invalidatePlugin(obj);
290 }
291
292 static void handleMouseMoved(NPCocoaEvent *cocoaEvent, PluginObject *obj)
293 {
294     NSPoint point = NSMakePoint(cocoaEvent->event.mouse.pluginX, cocoaEvent->event.mouse.pluginY);
295     
296     if (NSPointInRect(point, obj->textFieldRect))
297         [[NSCursor IBeamCursor] set];
298     else
299         [[NSCursor arrowCursor] set];
300 }
301
302 static void handleMouseDown(NPCocoaEvent *cocoaEvent, PluginObject *obj) 
303 {
304     NSPoint point = NSMakePoint(cocoaEvent->event.mouse.pluginX, cocoaEvent->event.mouse.pluginY);
305     
306     obj->textFieldHasFocus = NSPointInRect(point, obj->textFieldRect);
307     
308     invalidatePlugin(obj);
309 }
310
311 int16 NPP_HandleEvent(NPP instance, void* event)
312 {
313     PluginObject *obj = instance->pdata;
314
315     NPCocoaEvent *cocoaEvent = event;
316     
317     switch (cocoaEvent->type) {
318         case NPCocoaEventDrawRect:
319             handleDraw(obj);
320             return 1;
321         case NPCocoaEventFocusChanged:
322             handleFocusChanged(cocoaEvent, obj);
323             return 1;
324         case NPCocoaEventMouseMoved:
325             handleMouseMoved(cocoaEvent, obj);
326             return 1;
327         case NPCocoaEventMouseDown:
328             handleMouseDown(cocoaEvent, obj);
329             return 1;
330         case NPCocoaEventKeyDown:
331             // If the text field has focus we ignore the event, causing it
332             // to be sent to the input manager.
333             if (obj->textFieldHasFocus)
334                 return 0;
335             else
336                 return 1;
337                 
338     }
339     
340     return 0;
341 }
342
343 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
344 {
345
346 }
347
348 static NSRange selectionRange(PluginObject *obj)
349 {
350     if (obj->markedRange.location != NSNotFound)
351         return obj->markedRange;
352     else
353         return obj->selectedRange;
354 }
355
356 /* Text Input */
357
358 void NPP_InsertText(NPP npp, id aString)
359 {
360     PluginObject *obj = npp->pdata;
361     
362     NSRange range = selectionRange(obj);
363     
364     // Get rid of the marked text
365     if (NPP_HasMarkedText(npp)) {
366         [obj->textStorage deleteCharactersInRange:obj->markedRange];
367         range.length = 0;
368     }
369     
370     [obj->textStorage replaceCharactersInRange:range withString:aString];
371     
372     obj->selectedRange.location = range.location + [aString length];
373     obj->selectedRange.length = 0;
374     
375     obj->markedRange = NSMakeRange(NSNotFound, 0);
376
377     invalidatePlugin(obj);
378 }
379
380 void NPP_DoCommandBySelector(NPP npp, SEL aSelector)
381 {
382     PluginObject *obj = npp->pdata;
383
384     if (aSelector == @selector(moveRight:)) {
385         if (obj->selectedRange.location == [obj->textStorage length])
386             return;
387         
388         obj->selectedRange.location++;  
389         invalidatePlugin(obj);
390     } else if (aSelector == @selector(moveLeft:)) {
391         if (obj->selectedRange.location == 0)
392             return;
393         
394         obj->selectedRange.location--;
395         invalidatePlugin(obj);
396     }
397 }
398
399 static NSDictionary *markedTextAttributes()
400 {
401     static NSDictionary *markedTextAttributes = nil;
402     if (!markedTextAttributes) {
403         NSTextView *tv = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
404         markedTextAttributes = [[tv markedTextAttributes] retain];
405         [tv release];
406     }
407     
408     return markedTextAttributes;
409 }
410
411 void NPP_SetMarkedText(NPP npp, id aString, NSRange selRange)
412 {
413     PluginObject *obj = npp->pdata;
414
415     BOOL isAttributedString = [aString isKindOfClass:[NSAttributedString class]];
416     
417     NSRange range = selectionRange(obj);
418     
419     if (!isAttributedString)        
420         aString = [[[NSAttributedString alloc] initWithString:aString attributes:markedTextAttributes()] autorelease];
421     
422     [obj->textStorage replaceCharactersInRange:range withAttributedString:aString];
423     
424     obj->selectedRange.location = range.location + selRange.location;
425     obj->selectedRange.length = selRange.length;
426     
427     obj->markedRange = NSMakeRange(range.location, [aString length]);
428     
429     invalidatePlugin(obj);
430 }
431
432 void NPP_UnmarkText(NPP npp)
433 {
434 }
435
436 BOOL NPP_HasMarkedText(NPP npp)
437 {
438     PluginObject *obj = npp->pdata;
439
440     return obj->markedRange.location != NSNotFound;
441 }
442
443 NSAttributedString *NPP_AttributedSubstringFromRange(NPP npp, NSRange theRange)
444 {
445     return nil;
446 }
447
448 NSRange NPP_MarkedRange(NPP npp)
449 {
450     PluginObject *obj = npp->pdata;
451
452     return obj->markedRange;
453 }
454
455 NSRange NPP_SelectedRange(NPP npp)
456 {
457     PluginObject *obj = npp->pdata;
458     
459     return obj->selectedRange;
460 }
461
462 NSRect NPP_FirstRectForCharacterRange(NPP npp, NSRange theRange)
463 {
464     PluginObject *obj = npp->pdata;
465
466     NSUInteger rectCount;
467     NSRect *rectArray = [obj->layoutManager rectArrayForCharacterRange:theRange
468                                      withinSelectedCharacterRange:theRange
469                                                   inTextContainer:obj->textContainer
470                                                         rectCount:&rectCount];
471     
472     return rectArray[0];
473 }
474
475 static NPPluginTextInputFuncs* pluginTextInputFuncs()
476 {
477     static NPPluginTextInputFuncs textInputFuncs;
478     static bool initialized = false;
479     
480     if (!initialized) {
481         textInputFuncs.version = 0;
482         textInputFuncs.size = sizeof(textInputFuncs);
483
484         textInputFuncs.insertText = NPP_InsertText;
485         textInputFuncs.doCommandBySelector = NPP_DoCommandBySelector;
486         textInputFuncs.setMarkedText = NPP_SetMarkedText;
487         textInputFuncs.unmarkText = NPP_UnmarkText;
488         textInputFuncs.hasMarkedText = NPP_HasMarkedText;
489         textInputFuncs.attributedSubstringFromRange = NPP_AttributedSubstringFromRange;
490         textInputFuncs.markedRange = NPP_MarkedRange;
491         textInputFuncs.selectedRange = NPP_SelectedRange;
492         textInputFuncs.firstRectForCharacterRange = NPP_FirstRectForCharacterRange;
493
494         initialized = true;
495     }
496     
497     return &textInputFuncs;
498 }
499
500 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
501 {
502     switch (variable) {
503         case NPPVpluginTextInputFuncs:
504             *(NPPluginTextInputFuncs**)value = pluginTextInputFuncs();            
505             return NPERR_NO_ERROR;
506     }
507     
508     return NPERR_GENERIC_ERROR;
509 }
510
511 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
512 {
513     return NPERR_GENERIC_ERROR;
514 }