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