iexplore: Added self-registration code.
[wine] / dlls / mshtml / editor.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31 #include "mshtmcid.h"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 #include "mshtml_private.h"
37 #include "resource.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40
41 #define NSCMD_ALIGN        "cmd_align"
42 #define NSCMD_BEGINLINE    "cmd_beginLine"
43 #define NSCMD_BOLD         "cmd_bold"
44 #define NSCMD_CHARNEXT     "cmd_charNext"
45 #define NSCMD_CHARPREVIOUS "cmd_charPrevious"
46 #define NSCMD_COPY         "cmd_copy"
47 #define NSCMD_CUT          "cmd_cut"
48 #define NSCMD_DELETECHARFORWARD   "cmd_deleteCharForward"
49 #define NSCMD_DELETEWORDFORWARD   "cmd_deleteWordForward"
50 #define NSCMD_ENDLINE      "cmd_endLine"
51 #define NSCMD_FONTCOLOR    "cmd_fontColor"
52 #define NSCMD_FONTFACE     "cmd_fontFace"
53 #define NSCMD_INDENT       "cmd_indent"
54 #define NSCMD_INSERTHR     "cmd_insertHR"
55 #define NSCMD_INSERTLINKNOUI    "cmd_insertLinkNoUI"
56 #define NSCMD_ITALIC       "cmd_italic"
57 #define NSCMD_LINENEXT     "cmd_lineNext"
58 #define NSCMD_LINEPREVIOUS "cmd_linePrevious"
59 #define NSCMD_MOVEBOTTOM   "cmd_moveBottom"
60 #define NSCMD_MOVEPAGEDOWN "cmd_movePageDown"
61 #define NSCMD_MOVEPAGEUP   "cmd_movePageUp"
62 #define NSCMD_MOVETOP      "cmd_moveTop"
63 #define NSCMD_OL           "cmd_ol"
64 #define NSCMD_OUTDENT      "cmd_outdent"
65 #define NSCMD_PASTE        "cmd_paste"
66 #define NSCMD_SELECTALL           "cmd_selectAll"
67 #define NSCMD_SELECTBEGINLINE     "cmd_selectBeginLine"
68 #define NSCMD_SELECTBOTTOM        "cmd_selectBottom"
69 #define NSCMD_SELECTCHARNEXT      "cmd_selectCharNext"
70 #define NSCMD_SELECTCHARPREVIOUS  "cmd_selectCharPrevious"
71 #define NSCMD_SELECTENDLINE       "cmd_selectEndLine"
72 #define NSCMD_SELECTLINENEXT      "cmd_selectLineNext"
73 #define NSCMD_SELECTLINEPREVIOUS  "cmd_selectLinePrevious"
74 #define NSCMD_SELECTPAGEDOWN      "cmd_selectPageDown"
75 #define NSCMD_SELECTPAGEUP        "cmd_selectPageUp"
76 #define NSCMD_SELECTTOP           "cmd_selectTop"
77 #define NSCMD_SELECTWORDNEXT      "cmd_selectWordNext"
78 #define NSCMD_SELECTWORDPREVIOUS  "cmd_selectWordPrevious"
79 #define NSCMD_UL           "cmd_ul"
80 #define NSCMD_UNDERLINE    "cmd_underline"
81 #define NSCMD_WORDNEXT     "cmd_wordNext"
82 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
83
84 #define NSSTATE_ATTRIBUTE "state_attribute"
85 #define NSSTATE_ALL       "state_all"
86
87 #define NSALIGN_CENTER "center"
88 #define NSALIGN_LEFT   "left"
89 #define NSALIGN_RIGHT  "right"
90
91 #define DOM_VK_LEFT     VK_LEFT
92 #define DOM_VK_UP       VK_UP
93 #define DOM_VK_RIGHT    VK_RIGHT
94 #define DOM_VK_DOWN     VK_DOWN
95 #define DOM_VK_DELETE   VK_DELETE
96 #define DOM_VK_HOME     VK_HOME
97 #define DOM_VK_END      VK_END
98
99 static const WCHAR wszFont[] = {'f','o','n','t',0};
100 static const WCHAR wszSize[] = {'s','i','z','e',0};
101
102 static void do_ns_editor_command(NSContainer *This, const char *cmd)
103 {
104     nsresult nsres;
105
106     if(!This->editor_controller)
107         return;
108
109     nsres = nsIController_DoCommand(This->editor_controller, cmd);
110     if(NS_FAILED(nsres))
111         ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
112 }
113
114 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
115 {
116     nsICommandManager *cmdmgr;
117     nsresult nsres;
118
119     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsICommandManager, (void**)&cmdmgr);
120     if(NS_FAILED(nsres)) {
121         ERR("Could not get nsICommandManager: %08x\n", nsres);
122         return nsres;
123     }
124
125     nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, NULL, nsparam);
126     if(NS_FAILED(nsres))
127         ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
128
129     nsICommandManager_Release(cmdmgr);
130     return nsres;
131 }
132
133 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
134 {
135     nsICommandParams *nsparam;
136     PRBool b = FALSE;
137
138     if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
139         return OLECMDF_SUPPORTED;
140
141     if(This->nscontainer && nscmd) {
142         nsparam = create_nscommand_params();
143         get_ns_command_state(This->nscontainer, nscmd, nsparam);
144
145         nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
146
147         nsICommandParams_Release(nsparam);
148     }
149
150     return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
151 }
152
153 static void set_ns_align(HTMLDocument *This, const char *align_str)
154 {
155     nsICommandParams *nsparam;
156
157     if(!This->nscontainer)
158         return;
159
160     nsparam = create_nscommand_params();
161     nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
162
163     do_ns_command(This->nscontainer, NSCMD_ALIGN, nsparam);
164
165     nsICommandParams_Release(nsparam);
166 }
167
168 static DWORD query_align_status(HTMLDocument *This, const char *align_str)
169 {
170     nsICommandParams *nsparam;
171     char *align = NULL;
172
173     if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
174         return OLECMDF_SUPPORTED;
175
176     if(This->nscontainer) {
177         nsparam = create_nscommand_params();
178         get_ns_command_state(This->nscontainer, NSCMD_ALIGN, nsparam);
179
180         nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &align);
181
182         nsICommandParams_Release(nsparam);
183     }
184
185     return OLECMDF_SUPPORTED | OLECMDF_ENABLED
186         | (align && !strcmp(align_str, align) ? OLECMDF_LATCHED : 0);
187 }
188
189
190 static nsISelection *get_ns_selection(HTMLDocument *This)
191 {
192     nsIDOMWindow *dom_window;
193     nsISelection *nsselection = NULL;
194     nsresult nsres;
195
196     if(!This->nscontainer)
197         return NULL;
198
199     nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
200     if(NS_FAILED(nsres))
201         return NULL;
202
203     nsIDOMWindow_GetSelection(dom_window, &nsselection);
204     nsIDOMWindow_Release(dom_window);
205
206     return nsselection;
207
208 }
209
210 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
211 {
212     PRBool has_children;
213     PRUint32 child_cnt, i;
214     nsIDOMNode *child_node;
215     nsIDOMNodeList *node_list;
216     PRUint16 node_type;
217
218     nsIDOMElement_HasChildNodes(elem, &has_children);
219     if(!has_children)
220         return;
221
222     nsIDOMElement_GetChildNodes(elem, &node_list);
223     nsIDOMNodeList_GetLength(node_list, &child_cnt);
224
225     for(i=0; i<child_cnt; i++) {
226         nsIDOMNodeList_Item(node_list, i, &child_node);
227
228         nsIDOMNode_GetNodeType(child_node, &node_type);
229         if(node_type == ELEMENT_NODE) {
230             nsIDOMElement *child_elem;
231             nsAString tag_str;
232             const PRUnichar *ctag;
233
234             nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
235
236             nsAString_Init(&tag_str, NULL);
237             nsIDOMElement_GetTagName(child_elem, &tag_str);
238             nsAString_GetData(&tag_str, &ctag, NULL);
239
240             if(!strcmpiW(ctag, tag))
241                 /* FIXME: remove node if there are no more attributes */
242                 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
243
244             nsAString_Finish(&tag_str);
245
246             remove_child_attr(child_elem, tag, attr_str);
247
248             nsIDOMNode_Release(child_elem);
249         }
250
251         nsIDOMNode_Release(child_node);
252     }
253
254     nsIDOMNodeList_Release(node_list);
255 }
256
257 static void get_font_size(HTMLDocument *This, WCHAR *ret)
258 {
259     nsISelection *nsselection = get_ns_selection(This);
260     nsIDOMElement *elem = NULL;
261     nsIDOMNode *node = NULL, *tmp_node;
262     nsAString tag_str;
263     LPCWSTR tag;
264     PRUint16 node_type;
265     nsresult nsres;
266
267     *ret = 0;
268
269     if(!nsselection)
270         return;
271
272     nsISelection_GetFocusNode(nsselection, &node);
273     nsISelection_Release(nsselection);
274
275     while(node) {
276         nsres = nsIDOMNode_GetNodeType(node, &node_type);
277         if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
278             break;
279
280         if(node_type == ELEMENT_NODE) {
281             nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
282
283             nsAString_Init(&tag_str, NULL);
284             nsIDOMElement_GetTagName(elem, &tag_str);
285             nsAString_GetData(&tag_str, &tag, NULL);
286
287             if(!strcmpiW(tag, wszFont)) {
288                 nsAString size_str, val_str;
289                 LPCWSTR val;
290
291                 TRACE("found font tag %p\n", elem);
292
293                 nsAString_Init(&size_str, wszSize);
294                 nsAString_Init(&val_str, NULL);
295
296                 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
297                 nsAString_GetData(&val_str, &val, NULL);
298
299                 if(*val) {
300                     TRACE("found size %s\n", debugstr_w(val));
301                     strcpyW(ret, val);
302                 }
303
304                 nsAString_Finish(&size_str);
305                 nsAString_Finish(&val_str);
306             }
307
308             nsAString_Finish(&tag_str);
309
310             nsIDOMElement_Release(elem);
311         }
312
313         if(*ret)
314             break;
315
316         tmp_node = node;
317         nsIDOMNode_GetParentNode(node, &node);
318         nsIDOMNode_Release(tmp_node);
319     }
320
321     if(node)
322         nsIDOMNode_Release(node);
323 }
324
325 static void set_font_size(HTMLDocument *This, LPCWSTR size)
326 {
327     nsISelection *nsselection;
328     PRBool collapsed;
329     nsIDOMDocument *nsdoc;
330     nsIDOMElement *elem;
331     nsIDOMRange *range;
332     PRInt32 range_cnt = 0;
333     nsAString font_str;
334     nsAString size_str;
335     nsAString val_str;
336     nsresult nsres;
337
338     nsselection = get_ns_selection(This);
339
340     if(!nsselection)
341         return;
342
343     nsISelection_GetRangeCount(nsselection, &range_cnt);
344     if(range_cnt != 1) {
345         FIXME("range_cnt %d not supprted\n", range_cnt);
346         if(!range_cnt) {
347             nsISelection_Release(nsselection);
348             return;
349         }
350     }
351
352     nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
353     if(NS_FAILED(nsres))
354         return;
355
356     nsAString_Init(&font_str, wszFont);
357     nsAString_Init(&size_str, wszSize);
358     nsAString_Init(&val_str, size);
359
360     nsIDOMDocument_CreateElement(nsdoc, &font_str, &elem);
361     nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
362
363     nsISelection_GetRangeAt(nsselection, 0, &range);
364     nsISelection_GetIsCollapsed(nsselection, &collapsed);
365     nsISelection_RemoveAllRanges(nsselection);
366
367     nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
368
369     if(collapsed) {
370         nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
371     }else {
372         /* Remove all size attrbutes from the range */
373         remove_child_attr(elem, wszFont, &size_str);
374         nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
375     }
376
377     nsIDOMRange_Release(range);
378     nsIDOMElement_Release(elem);
379
380     nsAString_Finish(&font_str);
381     nsAString_Finish(&size_str);
382     nsAString_Finish(&val_str);
383
384     nsISelection_Release(nsselection);
385     nsIDOMDocument_Release(nsdoc);
386 }
387
388 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
389 {
390     int i=0;
391     PRBool b;
392
393     nsIDOMKeyEvent_GetCtrlKey(event, &b);
394     if(b)
395         i |= 1;
396
397     nsIDOMKeyEvent_GetShiftKey(event, &b);
398     if(b)
399         i |= 2;
400
401     if(cmds[i])
402         do_ns_editor_command(This->nscontainer, cmds[i]);
403
404     nsIDOMKeyEvent_PreventDefault(event);
405 }
406
407 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
408 {
409     nsIDOMKeyEvent *key_event;
410     PRUint32 code;
411
412     nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
413
414     nsIDOMKeyEvent_GetKeyCode(key_event, &code);
415
416     switch(code) {
417     case DOM_VK_LEFT: {
418         static const char * const cmds[] = {
419             NSCMD_CHARPREVIOUS,
420             NSCMD_WORDPREVIOUS,
421             NSCMD_SELECTCHARPREVIOUS,
422             NSCMD_SELECTWORDPREVIOUS
423         };
424
425         TRACE("left\n");
426         handle_arrow_key(This, key_event, cmds);
427         break;
428     }
429     case DOM_VK_RIGHT: {
430         static const char * const cmds[] = {
431             NSCMD_CHARNEXT,
432             NSCMD_WORDNEXT,
433             NSCMD_SELECTCHARNEXT,
434             NSCMD_SELECTWORDNEXT
435         };
436
437         TRACE("right\n");
438         handle_arrow_key(This, key_event, cmds);
439         break;
440     }
441     case DOM_VK_UP: {
442         static const char * const cmds[] = {
443             NSCMD_LINEPREVIOUS,
444             NSCMD_MOVEPAGEUP,
445             NSCMD_SELECTLINEPREVIOUS,
446             NSCMD_SELECTPAGEUP
447         };
448
449         TRACE("up\n");
450         handle_arrow_key(This, key_event, cmds);
451         break;
452     }
453     case DOM_VK_DOWN: {
454         static const char * const cmds[] = {
455             NSCMD_LINENEXT,
456             NSCMD_MOVEPAGEDOWN,
457             NSCMD_SELECTLINENEXT,
458             NSCMD_SELECTPAGEDOWN
459         };
460
461         TRACE("down\n");
462         handle_arrow_key(This, key_event, cmds);
463         break;
464     }
465     case DOM_VK_DELETE: {
466         static const char * const cmds[] = {
467             NSCMD_DELETECHARFORWARD,
468             NSCMD_DELETEWORDFORWARD,
469             NULL, NULL
470         };
471
472         TRACE("delete\n");
473         handle_arrow_key(This, key_event, cmds);
474         break;
475     }
476     case DOM_VK_HOME: {
477         static const char * const cmds[] = {
478             NSCMD_BEGINLINE,
479             NSCMD_MOVETOP,
480             NSCMD_SELECTBEGINLINE,
481             NSCMD_SELECTTOP
482         };
483
484         TRACE("home\n");
485         handle_arrow_key(This, key_event, cmds);
486         break;
487     }
488     case DOM_VK_END: {
489         static const char * const cmds[] = {
490             NSCMD_ENDLINE,
491             NSCMD_MOVEBOTTOM,
492             NSCMD_SELECTENDLINE,
493             NSCMD_SELECTBOTTOM
494         };
495
496         TRACE("end\n");
497         handle_arrow_key(This, key_event, cmds);
498         break;
499     }
500     }
501
502     nsIDOMKeyEvent_Release(key_event);
503 }
504
505 void handle_edit_load(HTMLDocument *This)
506 {
507     get_editor_controller(This->nscontainer);
508 }
509
510 static void set_ns_fontname(NSContainer *This, const char *fontname)
511 {
512     nsICommandParams *nsparam = create_nscommand_params();
513
514     nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
515     do_ns_command(This, NSCMD_FONTFACE, nsparam);
516     nsICommandParams_Release(nsparam);
517 }
518
519 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
520 {
521     TRACE("(%p)->(%p %p)\n", This, in, out);
522
523     if(This->nscontainer)
524         do_ns_editor_command(This->nscontainer, NSCMD_DELETECHARFORWARD);
525
526     update_doc(This, UPDATE_UI);
527     return S_OK;
528 }
529
530 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
531 {
532     TRACE("(%p)->(%p %p)\n", This, in, out);
533
534     if(!This->nscontainer) {
535         update_doc(This, UPDATE_UI);
536         return E_FAIL;
537     }
538
539     if(in) {
540         char *stra;
541         DWORD len;
542
543         if(V_VT(in) != VT_BSTR) {
544             FIXME("Unsupported vt=%d\n", V_VT(out));
545             return E_INVALIDARG;
546         }
547
548         TRACE("%s\n", debugstr_w(V_BSTR(in)));
549
550         len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, NULL, 0, NULL, NULL);
551         stra = mshtml_alloc(len);
552         WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, stra, -1, NULL, NULL);
553
554         set_ns_fontname(This->nscontainer, stra);
555
556         mshtml_free(stra);
557
558         update_doc(This, UPDATE_UI);
559     }
560
561     if(out) {
562         nsICommandParams *nsparam;
563         LPWSTR strw;
564         char *stra;
565         DWORD len;
566         nsresult nsres;
567
568         nsparam = create_nscommand_params();
569
570         nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
571         if(NS_FAILED(nsres))
572             return S_OK;
573
574         nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
575         nsICommandParams_Release(nsparam);
576
577         len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
578         strw = mshtml_alloc(len*sizeof(WCHAR));
579         MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, -1);
580         nsfree(stra);
581
582         V_VT(out) = VT_BSTR;
583         V_BSTR(out) = SysAllocString(strw);
584         mshtml_free(strw);
585     }
586
587     return S_OK;
588 }
589
590 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
591 {
592     TRACE("(%p)->(%p %p)\n", This, in, out);
593
594     if(in) {
595         if(V_VT(in) == VT_I4) {
596             nsICommandParams *nsparam = create_nscommand_params();
597             char color_str[10];
598
599             sprintf(color_str, "#%02x%02x%02x",
600                     V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
601
602             nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
603             do_ns_command(This->nscontainer, NSCMD_FONTCOLOR, nsparam);
604
605             nsICommandParams_Release(nsparam);
606         }else {
607             FIXME("unsupported in vt=%d\n", V_VT(in));
608         }
609
610         update_doc(This, UPDATE_UI);
611     }
612
613     if(out) {
614         FIXME("unsupported out\n");
615         return E_NOTIMPL;
616     }
617
618     return S_OK;
619 }
620
621 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
622 {
623     TRACE("(%p)->(%p %p)\n", This, in, out);
624
625     if(out) {
626         WCHAR val[10] = {0};
627
628         get_font_size(This, val);
629         V_VT(out) = VT_I4;
630         V_I4(out) = strtolW(val, NULL, 10);
631     }
632
633     if(in) {
634         switch(V_VT(in)) {
635         case VT_I4: {
636             WCHAR size[10];
637             static const WCHAR format[] = {'%','d',0};
638             wsprintfW(size, format, V_I4(in));
639             set_font_size(This, size);
640             break;
641         }
642         case VT_BSTR:
643             set_font_size(This, V_BSTR(in));
644             break;
645         default:
646             FIXME("unsupported vt %d\n", V_VT(in));
647         }
648
649         update_doc(This, UPDATE_UI);
650     }
651
652     return S_OK;
653 }
654
655 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
656 {
657
658     FIXME("(%p)->(%p %p)\n", This, in, out);
659     return E_NOTIMPL;
660 }
661
662 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
663 {
664     TRACE("(%p)\n", This);
665
666     if(in || out)
667         FIXME("unsupported args\n");
668
669     if(This->nscontainer)
670         do_ns_command(This->nscontainer, NSCMD_SELECTALL, NULL);
671
672     update_doc(This, UPDATE_UI);
673     return S_OK;
674 }
675
676 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
677 {
678     TRACE("(%p)\n", This);
679
680     if(in || out)
681         FIXME("unsupported args\n");
682
683     if(This->nscontainer)
684         do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
685
686     update_doc(This, UPDATE_UI);
687     return S_OK;
688 }
689
690 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
691 {
692     TRACE("(%p)\n", This);
693
694     if(in || out)
695         FIXME("unsupported args\n");
696
697     if(This->nscontainer)
698         do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
699
700     update_doc(This, UPDATE_UI);
701     return S_OK;
702 }
703
704 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
705 {
706     switch(cmd->cmdID) {
707     case IDM_JUSTIFYCENTER:
708         TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
709         cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
710         break;
711     case IDM_JUSTIFYLEFT:
712         TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
713         /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
714         if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
715             cmd->cmdf = OLECMDF_SUPPORTED;
716         else
717             cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
718         break;
719     case IDM_JUSTIFYRIGHT:
720         TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
721         cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
722         break;
723     }
724
725     return S_OK;
726 }
727
728 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
729 {
730     TRACE("(%p)\n", This);
731
732     if(in || out)
733         FIXME("unsupported args\n");
734
735     set_ns_align(This, NSALIGN_CENTER);
736     update_doc(This, UPDATE_UI);
737     return S_OK;
738 }
739
740 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
741 {
742     TRACE("(%p)\n", This);
743
744     if(in || out)
745         FIXME("unsupported args\n");
746
747     set_ns_align(This, NSALIGN_LEFT);
748     update_doc(This, UPDATE_UI);
749     return S_OK;
750 }
751
752 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
753 {
754     TRACE("(%p)\n", This);
755
756     if(in || out)
757         FIXME("unsupported args\n");
758
759     set_ns_align(This, NSALIGN_RIGHT);
760     update_doc(This, UPDATE_UI);
761     return S_OK;
762 }
763
764 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
765 {
766     TRACE("(%p)\n", This);
767
768     if(in || out)
769         FIXME("unsupported args\n");
770
771     if(This->nscontainer)
772         do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
773
774     update_doc(This, UPDATE_UI);
775     return S_OK;
776 }
777
778 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
779 {
780     TRACE("(%p)\n", This);
781
782     if(in || out)
783         FIXME("unsupported args\n");
784
785     if(This->nscontainer)
786         do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
787
788     update_doc(This, UPDATE_UI);
789     return S_OK;
790 }
791
792 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
793 {
794     TRACE("(%p)\n", This);
795
796     if(in || out)
797         FIXME("unsupported args\n");
798
799     if(This->nscontainer)
800         do_ns_command(This->nscontainer, NSCMD_OL, NULL);
801
802     update_doc(This, UPDATE_UI);
803     return S_OK;
804 }
805
806 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
807 {
808     TRACE("(%p)\n", This);
809
810     if(in || out)
811         FIXME("unsupported args\n");
812
813     if(This->nscontainer)
814         do_ns_command(This->nscontainer, NSCMD_UL, NULL);
815
816     update_doc(This, UPDATE_UI);
817     return S_OK;
818 }
819
820 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
821 {
822     TRACE("(%p)\n", This);
823
824     if(in || out)
825         FIXME("unsupported args\n");
826
827     if(This->nscontainer)
828         do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
829
830     update_doc(This, UPDATE_UI);
831     return S_OK;
832 }
833
834 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
835 {
836     TRACE("(%p)\n", This);
837
838     if(in || out)
839         FIXME("unsupported args\n");
840
841     if(This->nscontainer)
842         do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
843
844     update_doc(This, UPDATE_UI);
845     return S_OK;
846 }
847
848 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
849 {
850     WCHAR *ptr;
851
852     if(out || !in || V_VT(in) != VT_BSTR) {
853         WARN("invalid arg\n");
854         return E_INVALIDARG;
855     }
856
857     TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
858
859     update_doc(This, UPDATE_UI);
860
861     ptr = V_BSTR(in);
862     if(*ptr == '1')
863         exec_bold(This, cmdexecopt, NULL, NULL);
864     ptr = strchrW(ptr, ',');
865     if(!ptr)
866         return S_OK;
867
868     if(*++ptr == '1')
869         exec_italic(This, cmdexecopt, NULL, NULL);
870     ptr = strchrW(ptr, ',');
871     if(!ptr)
872         return S_OK;
873
874     if(*++ptr == '1')
875         exec_underline(This, cmdexecopt, NULL, NULL);
876     ptr = strchrW(ptr, ',');
877     if(!ptr)
878         return S_OK;
879
880     if(isdigitW(*++ptr)) {
881         VARIANT v;
882
883         V_VT(&v) = VT_I4;
884         V_I4(&v) = *ptr-'0';
885
886         exec_fontsize(This, cmdexecopt, &v, NULL);
887     }
888     ptr = strchrW(ptr, ',');
889     if(!ptr)
890         return S_OK;
891
892     if(*++ptr != ',')
893         FIXME("set font color\n");
894     ptr = strchrW(ptr, ',');
895     if(!ptr)
896         return S_OK;
897
898     if(*++ptr != ',')
899         FIXME("set background color\n");
900     ptr = strchrW(ptr, ',');
901     if(!ptr)
902         return S_OK;
903
904     ptr++;
905     if(*ptr) {
906         VARIANT v;
907
908         V_VT(&v) = VT_BSTR;
909         V_BSTR(&v) = SysAllocString(ptr);
910
911         exec_fontname(This, cmdexecopt, &v, NULL);
912
913         SysFreeString(V_BSTR(&v));
914     }
915
916     return S_OK;
917 }
918
919 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
920 {
921     update_doc(This, UPDATE_UI);
922
923     if(!This->nscontainer)
924         return E_FAIL;
925
926     do_ns_editor_command(This->nscontainer, NSCMD_COPY);
927     return S_OK;
928 }
929
930 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
931 {
932     update_doc(This, UPDATE_UI);
933
934     if(!This->nscontainer)
935         return E_FAIL;
936
937     do_ns_editor_command(This->nscontainer, NSCMD_CUT);
938     return S_OK;
939 }
940
941 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
942 {
943     update_doc(This, UPDATE_UI);
944
945     if(!This->nscontainer)
946         return E_FAIL;
947
948     do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
949     return S_OK;
950 }
951
952 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
953 {
954     switch(cmd->cmdID) {
955     case IDM_DELETE:
956         TRACE("CGID_MSHTML: IDM_DELETE\n");
957         cmd->cmdf = query_ns_edit_status(This, NULL);
958         break;
959     case IDM_FONTNAME:
960         TRACE("CGID_MSHTML: IDM_FONTNAME\n");
961         cmd->cmdf = query_ns_edit_status(This, NULL);
962         break;
963     case IDM_FONTSIZE:
964         TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
965         cmd->cmdf = query_ns_edit_status(This, NULL);
966         break;
967     case IDM_BOLD:
968         TRACE("CGID_MSHTML: IDM_BOLD\n");
969         cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
970         break;
971     case IDM_FORECOLOR:
972         TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
973         cmd->cmdf = query_ns_edit_status(This, NULL);
974         break;
975     case IDM_ITALIC:
976         TRACE("CGID_MSHTML: IDM_ITALIC\n");
977         cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
978         break;
979     case IDM_UNDERLINE:
980         TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
981         cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
982         break;
983     case IDM_HORIZONTALLINE:
984         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
985         cmd->cmdf = query_ns_edit_status(This, NULL);
986         break;
987     case IDM_ORDERLIST:
988         TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
989         cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
990         break;
991     case IDM_UNORDERLIST:
992         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
993         cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
994         break;
995     case IDM_INDENT:
996         TRACE("CGID_MSHTML: IDM_INDENT\n");
997         cmd->cmdf = query_ns_edit_status(This, NULL);
998         break;
999     case IDM_OUTDENT:
1000         TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1001         cmd->cmdf = query_ns_edit_status(This, NULL);
1002         break;
1003     case IDM_HYPERLINK:
1004         TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1005         cmd->cmdf = query_ns_edit_status(This, NULL);
1006         break;
1007     }
1008
1009     return S_OK;
1010 }
1011
1012 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1013 {
1014     static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1015
1016     switch (msg)
1017     {
1018         case WM_INITDIALOG:
1019         {
1020             static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1021             static const WCHAR wszFtp[] = {'f','t','p',':',0};
1022             static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1023             static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1024             static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1025             static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1026             INT def_idx;
1027             HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1028             HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1029             INT len;
1030
1031             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1032
1033             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1034             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1035             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1036             def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1037             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1038             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1039             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1040             SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1041
1042             /* force the updating of the URL edit box */
1043             SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1044
1045             SetFocus(hwndURL);
1046             len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1047             SendMessageW(hwndURL, EM_SETSEL, 0, len);
1048
1049             return FALSE;
1050         }
1051         case WM_COMMAND:
1052             switch (wparam)
1053             {
1054                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1055                     EndDialog(hwnd, wparam);
1056                     return TRUE;
1057                 case MAKEWPARAM(IDOK, BN_CLICKED):
1058                 {
1059                     BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1060                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1061                     INT len = GetWindowTextLengthW(hwndURL);
1062                     *url = SysAllocStringLen(NULL, len + 1);
1063                     GetWindowTextW(hwndURL, *url, len + 1);
1064                     EndDialog(hwnd, wparam);
1065                     return TRUE;
1066                 }
1067                 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1068                 {
1069                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1070                     INT item;
1071                     INT len;
1072                     LPWSTR type;
1073                     LPWSTR url;
1074                     LPWSTR p;
1075                     static const WCHAR wszSlashSlash[] = {'/','/'};
1076
1077                     /* get string of currently selected hyperlink type */
1078                     item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1079                     len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1080                     type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1081                     SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1082
1083                     if (!strcmpW(type, wszOther))
1084                         *type = '\0';
1085
1086                     /* get current URL */
1087                     len = GetWindowTextLengthW(hwndURL);
1088                     url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1089                     GetWindowTextW(hwndURL, url, len + 1);
1090
1091                     /* strip off old protocol */
1092                     p = strchrW(url, ':');
1093                     if (p && p[1] == '/' && p[2] == '/')
1094                         p += 3;
1095                     if (!p) p = url;
1096                     memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1097
1098                     /* add new protocol */
1099                     if (*type != '\0')
1100                     {
1101                         memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1102                         memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1103                     }
1104
1105                     SetWindowTextW(hwndURL, url);
1106
1107                     HeapFree(GetProcessHeap(), 0, url);
1108                     HeapFree(GetProcessHeap(), 0, type);
1109                     return TRUE;
1110                 }
1111             }
1112             return FALSE;
1113         case WM_CLOSE:
1114             EndDialog(hwnd, IDCANCEL);
1115             return TRUE;
1116         default:
1117             return FALSE;
1118     }
1119 }
1120
1121 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1122 {
1123     BSTR url = NULL;
1124     INT ret;
1125     nsAString ns_url;
1126     PRBool insert_link_at_caret;
1127     nsISelection *nsselection;
1128
1129     FIXME("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1130
1131     if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1132     {
1133         if (!in || V_VT(in) != VT_BSTR)
1134         {
1135             WARN("invalid arg\n");
1136             return E_INVALIDARG;
1137         }
1138         url = V_BSTR(in);
1139     }
1140     else
1141     {
1142         ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1143         if (ret != IDOK)
1144             return OLECMDERR_E_CANCELED;
1145     }
1146
1147     nsselection = get_ns_selection(This);
1148     if (!nsselection)
1149         return E_FAIL;
1150
1151     nsAString_Init(&ns_url, url);
1152
1153     nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1154
1155     if (insert_link_at_caret)
1156     {
1157         static const WCHAR wszA[] = {'a',0};
1158         static const WCHAR wszHref[] = {'h','r','e','f',0};
1159         nsIHTMLEditor *html_editor;
1160         nsIDOMDocument *nsdoc;
1161         nsIDOMNode *text_node;
1162         nsIDOMElement *anchor_elem;
1163         nsIDOMNode *unused_node;
1164         nsAString a_str;
1165         nsAString href_str;
1166         nsresult nsres;
1167
1168         nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
1169         if(NS_FAILED(nsres))
1170             return E_FAIL;
1171
1172         nsAString_Init(&a_str, wszA);
1173         nsAString_Init(&href_str, wszHref);
1174
1175         /* create an element for the link */
1176         nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
1177         nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1178
1179         nsAString_Finish(&href_str);
1180         nsAString_Finish(&a_str);
1181
1182         /* create an element with text of URL */
1183         nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
1184
1185         /* wrap the <a> tags around the text element */
1186         nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1187         nsIDOMNode_Release(text_node);
1188         nsIDOMNode_Release(unused_node);
1189
1190         nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1191         if (html_editor)
1192         {
1193             /* add them to the document at the caret position */
1194             nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
1195             nsIHTMLEditor_Release(html_editor);
1196         }
1197
1198         nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1199
1200         nsIDOMElement_Release(anchor_elem);
1201         nsIDOMDocument_Release(nsdoc);
1202     }
1203     else
1204     {
1205         nsICommandParams *nsparam = create_nscommand_params();
1206
1207         nsICommandParams_SetStringValue(nsparam, NSSTATE_ATTRIBUTE, &ns_url);
1208         do_ns_command(This->nscontainer, NSCMD_INSERTLINKNOUI, nsparam);
1209         nsICommandParams_Release(nsparam);
1210     }
1211
1212     nsAString_Finish(&ns_url);
1213
1214     nsISelection_Release(nsselection);
1215
1216     if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1217         SysFreeString(url);
1218
1219     return S_OK;
1220 }
1221
1222 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1223 {
1224     TRACE("(%p)->(%p)\n", This, cmd);
1225
1226     cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1227     return S_OK;
1228 }
1229
1230 const cmdtable_t editmode_cmds[] = {
1231     {IDM_DELETE,          query_edit_status,    exec_delete},
1232     {IDM_FONTNAME,        query_edit_status,    exec_fontname},
1233     {IDM_FONTSIZE,        query_edit_status,    exec_fontsize},
1234     {IDM_SELECTALL,       query_selall_status , exec_selectall},
1235     {IDM_FORECOLOR,       query_edit_status,    exec_forecolor},
1236     {IDM_BOLD,            query_edit_status,    exec_bold},
1237     {IDM_ITALIC,          query_edit_status,    exec_italic},
1238     {IDM_JUSTIFYCENTER,   query_justify,        exec_justifycenter},
1239     {IDM_JUSTIFYRIGHT,    query_justify,        exec_justifyright},
1240     {IDM_JUSTIFYLEFT,     query_justify,        exec_justifyleft},
1241     {IDM_FONT,            NULL,                 exec_font},
1242     {IDM_UNDERLINE,       query_edit_status,    exec_underline},
1243     {IDM_HORIZONTALLINE,  query_edit_status,    exec_horizontalline},
1244     {IDM_ORDERLIST,       query_edit_status,    exec_orderlist},
1245     {IDM_UNORDERLIST,     query_edit_status,    exec_unorderlist},
1246     {IDM_INDENT,          query_edit_status,    exec_indent},
1247     {IDM_OUTDENT,         query_edit_status,    exec_outdent},
1248     {IDM_COMPOSESETTINGS, NULL,                 exec_composesettings},
1249     {IDM_HYPERLINK,       query_edit_status,    exec_hyperlink},
1250     {0,NULL,NULL}
1251 };
1252
1253 void init_editor(HTMLDocument *This)
1254 {
1255     update_doc(This, UPDATE_UI);
1256
1257     if(!This->nscontainer)
1258         return;
1259
1260     set_ns_fontname(This->nscontainer, "Times New Roman");
1261 }
1262
1263 HRESULT editor_is_dirty(HTMLDocument *This)
1264 {
1265     PRBool modified;
1266
1267     if(!This->nscontainer || !This->nscontainer->editor)
1268         return S_FALSE;
1269
1270     nsIEditor_GetDocumentModified(This->nscontainer->editor, &modified);
1271
1272     return modified ? S_OK : S_FALSE;
1273 }