mshtml: Implement IDM_HYPERLINK.
[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_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
784 {
785     TRACE("(%p)\n", This);
786
787     if(in || out)
788         FIXME("unsupported args\n");
789
790     if(This->nscontainer)
791         do_ns_command(This->nscontainer, NSCMD_SELECTALL, NULL);
792
793     update_doc(This, UPDATE_UI);
794     return S_OK;
795 }
796
797 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
798 {
799     TRACE("(%p)\n", This);
800
801     if(in || out)
802         FIXME("unsupported args\n");
803
804     if(This->nscontainer)
805         do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
806
807     update_doc(This, UPDATE_UI);
808     return S_OK;
809 }
810
811 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
812 {
813     TRACE("(%p)\n", This);
814
815     if(in || out)
816         FIXME("unsupported args\n");
817
818     if(This->nscontainer)
819         do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
820
821     update_doc(This, UPDATE_UI);
822     return S_OK;
823 }
824
825 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
826 {
827     switch(cmd->cmdID) {
828     case IDM_JUSTIFYCENTER:
829         TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
830         cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
831         break;
832     case IDM_JUSTIFYLEFT:
833         TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
834         /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
835         if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
836             cmd->cmdf = OLECMDF_SUPPORTED;
837         else
838             cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
839         break;
840     case IDM_JUSTIFYRIGHT:
841         TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
842         cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
843         break;
844     }
845
846     return S_OK;
847 }
848
849 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
850 {
851     TRACE("(%p)\n", This);
852
853     if(in || out)
854         FIXME("unsupported args\n");
855
856     set_ns_align(This, NSALIGN_CENTER);
857     update_doc(This, UPDATE_UI);
858     return S_OK;
859 }
860
861 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
862 {
863     TRACE("(%p)\n", This);
864
865     if(in || out)
866         FIXME("unsupported args\n");
867
868     set_ns_align(This, NSALIGN_LEFT);
869     update_doc(This, UPDATE_UI);
870     return S_OK;
871 }
872
873 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
874 {
875     TRACE("(%p)\n", This);
876
877     if(in || out)
878         FIXME("unsupported args\n");
879
880     set_ns_align(This, NSALIGN_RIGHT);
881     update_doc(This, UPDATE_UI);
882     return S_OK;
883 }
884
885 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
886 {
887     TRACE("(%p)\n", This);
888
889     if(in || out)
890         FIXME("unsupported args\n");
891
892     if(This->nscontainer)
893         do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
894
895     update_doc(This, UPDATE_UI);
896     return S_OK;
897 }
898
899 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
900 {
901     TRACE("(%p)\n", This);
902
903     if(in || out)
904         FIXME("unsupported args\n");
905
906     if(This->nscontainer)
907         do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
908
909     update_doc(This, UPDATE_UI);
910     return S_OK;
911 }
912
913 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
914 {
915     TRACE("(%p)\n", This);
916
917     if(in || out)
918         FIXME("unsupported args\n");
919
920     if(This->nscontainer)
921         do_ns_command(This->nscontainer, NSCMD_OL, NULL);
922
923     update_doc(This, UPDATE_UI);
924     return S_OK;
925 }
926
927 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
928 {
929     TRACE("(%p)\n", This);
930
931     if(in || out)
932         FIXME("unsupported args\n");
933
934     if(This->nscontainer)
935         do_ns_command(This->nscontainer, NSCMD_UL, NULL);
936
937     update_doc(This, UPDATE_UI);
938     return S_OK;
939 }
940
941 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
942 {
943     TRACE("(%p)\n", This);
944
945     if(in || out)
946         FIXME("unsupported args\n");
947
948     if(This->nscontainer)
949         do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
950
951     update_doc(This, UPDATE_UI);
952     return S_OK;
953 }
954
955 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
956 {
957     TRACE("(%p)\n", This);
958
959     if(in || out)
960         FIXME("unsupported args\n");
961
962     if(This->nscontainer)
963         do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
964
965     update_doc(This, UPDATE_UI);
966     return S_OK;
967 }
968
969 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
970 {
971     WCHAR *ptr;
972
973     if(out || !in || V_VT(in) != VT_BSTR) {
974         WARN("invalid arg\n");
975         return E_INVALIDARG;
976     }
977
978     TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
979
980     update_doc(This, UPDATE_UI);
981
982     ptr = V_BSTR(in);
983     if(*ptr == '1')
984         exec_bold(This, cmdexecopt, NULL, NULL);
985     ptr = strchrW(ptr, ',');
986     if(!ptr)
987         return S_OK;
988
989     if(*++ptr == '1')
990         exec_italic(This, cmdexecopt, NULL, NULL);
991     ptr = strchrW(ptr, ',');
992     if(!ptr)
993         return S_OK;
994
995     if(*++ptr == '1')
996         exec_underline(This, cmdexecopt, NULL, NULL);
997     ptr = strchrW(ptr, ',');
998     if(!ptr)
999         return S_OK;
1000
1001     if(isdigitW(*++ptr)) {
1002         VARIANT v;
1003
1004         V_VT(&v) = VT_I4;
1005         V_I4(&v) = *ptr-'0';
1006
1007         exec_fontsize(This, cmdexecopt, &v, NULL);
1008     }
1009     ptr = strchrW(ptr, ',');
1010     if(!ptr)
1011         return S_OK;
1012
1013     if(*++ptr != ',')
1014         FIXME("set font color\n");
1015     ptr = strchrW(ptr, ',');
1016     if(!ptr)
1017         return S_OK;
1018
1019     if(*++ptr != ',')
1020         FIXME("set background color\n");
1021     ptr = strchrW(ptr, ',');
1022     if(!ptr)
1023         return S_OK;
1024
1025     ptr++;
1026     if(*ptr) {
1027         VARIANT v;
1028
1029         V_VT(&v) = VT_BSTR;
1030         V_BSTR(&v) = SysAllocString(ptr);
1031
1032         exec_fontname(This, cmdexecopt, &v, NULL);
1033
1034         SysFreeString(V_BSTR(&v));
1035     }
1036
1037     return S_OK;
1038 }
1039
1040 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1041 {
1042     update_doc(This, UPDATE_UI);
1043
1044     if(!This->nscontainer)
1045         return E_FAIL;
1046
1047     do_ns_editor_command(This->nscontainer, NSCMD_COPY);
1048     return S_OK;
1049 }
1050
1051 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1052 {
1053     update_doc(This, UPDATE_UI);
1054
1055     if(!This->nscontainer)
1056         return E_FAIL;
1057
1058     do_ns_editor_command(This->nscontainer, NSCMD_CUT);
1059     return S_OK;
1060 }
1061
1062 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1063 {
1064     update_doc(This, UPDATE_UI);
1065
1066     if(!This->nscontainer)
1067         return E_FAIL;
1068
1069     do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
1070     return S_OK;
1071 }
1072
1073 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
1074 {
1075     switch(cmd->cmdID) {
1076     case IDM_DELETE:
1077         TRACE("CGID_MSHTML: IDM_DELETE\n");
1078         cmd->cmdf = query_ns_edit_status(This, NULL);
1079         break;
1080     case IDM_FONTNAME:
1081         TRACE("CGID_MSHTML: IDM_FONTNAME\n");
1082         cmd->cmdf = query_ns_edit_status(This, NULL);
1083         break;
1084     case IDM_FONTSIZE:
1085         TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
1086         cmd->cmdf = query_ns_edit_status(This, NULL);
1087         break;
1088     case IDM_BOLD:
1089         TRACE("CGID_MSHTML: IDM_BOLD\n");
1090         cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
1091         break;
1092     case IDM_FORECOLOR:
1093         TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
1094         cmd->cmdf = query_ns_edit_status(This, NULL);
1095         break;
1096     case IDM_ITALIC:
1097         TRACE("CGID_MSHTML: IDM_ITALIC\n");
1098         cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
1099         break;
1100     case IDM_UNDERLINE:
1101         TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
1102         cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
1103         break;
1104     case IDM_HORIZONTALLINE:
1105         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1106         cmd->cmdf = query_ns_edit_status(This, NULL);
1107         break;
1108     case IDM_ORDERLIST:
1109         TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1110         cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1111         break;
1112     case IDM_UNORDERLIST:
1113         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1114         cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1115         break;
1116     case IDM_INDENT:
1117         TRACE("CGID_MSHTML: IDM_INDENT\n");
1118         cmd->cmdf = query_ns_edit_status(This, NULL);
1119         break;
1120     case IDM_OUTDENT:
1121         TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1122         cmd->cmdf = query_ns_edit_status(This, NULL);
1123         break;
1124     case IDM_HYPERLINK:
1125         TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1126         cmd->cmdf = query_ns_edit_status(This, NULL);
1127         break;
1128     }
1129
1130     return S_OK;
1131 }
1132
1133 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1134 {
1135     static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1136
1137     switch (msg)
1138     {
1139         case WM_INITDIALOG:
1140         {
1141             static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1142             static const WCHAR wszFtp[] = {'f','t','p',':',0};
1143             static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1144             static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1145             static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1146             static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1147             INT def_idx;
1148             HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1149             HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1150             INT len;
1151
1152             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1153
1154             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1155             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1156             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1157             def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1158             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1159             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1160             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1161             SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1162
1163             /* force the updating of the URL edit box */
1164             SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1165
1166             SetFocus(hwndURL);
1167             len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1168             SendMessageW(hwndURL, EM_SETSEL, 0, len);
1169
1170             return FALSE;
1171         }
1172         case WM_COMMAND:
1173             switch (wparam)
1174             {
1175                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1176                     EndDialog(hwnd, wparam);
1177                     return TRUE;
1178                 case MAKEWPARAM(IDOK, BN_CLICKED):
1179                 {
1180                     BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1181                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1182                     INT len = GetWindowTextLengthW(hwndURL);
1183                     *url = SysAllocStringLen(NULL, len + 1);
1184                     GetWindowTextW(hwndURL, *url, len + 1);
1185                     EndDialog(hwnd, wparam);
1186                     return TRUE;
1187                 }
1188                 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1189                 {
1190                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1191                     INT item;
1192                     INT len;
1193                     LPWSTR type;
1194                     LPWSTR url;
1195                     LPWSTR p;
1196                     static const WCHAR wszSlashSlash[] = {'/','/'};
1197
1198                     /* get string of currently selected hyperlink type */
1199                     item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1200                     len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1201                     type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1202                     SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1203
1204                     if (!strcmpW(type, wszOther))
1205                         *type = '\0';
1206
1207                     /* get current URL */
1208                     len = GetWindowTextLengthW(hwndURL);
1209                     url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1210                     GetWindowTextW(hwndURL, url, len + 1);
1211
1212                     /* strip off old protocol */
1213                     p = strchrW(url, ':');
1214                     if (p && p[1] == '/' && p[2] == '/')
1215                         p += 3;
1216                     if (!p) p = url;
1217                     memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1218
1219                     /* add new protocol */
1220                     if (*type != '\0')
1221                     {
1222                         memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1223                         memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1224                     }
1225
1226                     SetWindowTextW(hwndURL, url);
1227
1228                     HeapFree(GetProcessHeap(), 0, url);
1229                     HeapFree(GetProcessHeap(), 0, type);
1230                     return TRUE;
1231                 }
1232             }
1233             return FALSE;
1234         case WM_CLOSE:
1235             EndDialog(hwnd, IDCANCEL);
1236             return TRUE;
1237         default:
1238             return FALSE;
1239     }
1240 }
1241
1242 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1243 {
1244     BSTR url = NULL;
1245     INT ret;
1246     nsAString ns_url;
1247     PRBool insert_link_at_caret;
1248     nsISelection *nsselection;
1249
1250     FIXME("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1251
1252     if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1253     {
1254         if (!in || V_VT(in) != VT_BSTR)
1255         {
1256             WARN("invalid arg\n");
1257             return E_INVALIDARG;
1258         }
1259         url = V_BSTR(in);
1260     }
1261     else
1262     {
1263         ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1264         if (ret != IDOK)
1265             return OLECMDERR_E_CANCELED;
1266     }
1267
1268     nsselection = get_ns_selection(This);
1269     if (!nsselection)
1270         return E_FAIL;
1271
1272     nsAString_Init(&ns_url, url);
1273
1274     nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1275
1276     if (insert_link_at_caret)
1277     {
1278         static const WCHAR wszA[] = {'a',0};
1279         static const WCHAR wszHref[] = {'h','r','e','f',0};
1280         nsIHTMLEditor *html_editor;
1281         nsIDOMDocument *nsdoc;
1282         nsIDOMNode *text_node;
1283         nsIDOMElement *anchor_elem;
1284         nsIDOMNode *unused_node;
1285         nsAString a_str;
1286         nsAString href_str;
1287         nsresult nsres;
1288
1289         nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
1290         if(NS_FAILED(nsres))
1291             return E_FAIL;
1292
1293         nsAString_Init(&a_str, wszA);
1294         nsAString_Init(&href_str, wszHref);
1295
1296         /* create an element for the link */
1297         nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
1298         nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1299
1300         nsAString_Finish(&href_str);
1301         nsAString_Finish(&a_str);
1302
1303         /* create an element with text of URL */
1304         nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
1305
1306         /* wrap the <a> tags around the text element */
1307         nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1308         nsIDOMNode_Release(text_node);
1309         nsIDOMNode_Release(unused_node);
1310
1311         nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1312         if (html_editor)
1313         {
1314             /* add them to the document at the caret position */
1315             nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
1316             nsIHTMLEditor_Release(html_editor);
1317         }
1318
1319         nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1320
1321         nsIDOMElement_Release(anchor_elem);
1322         nsIDOMDocument_Release(nsdoc);
1323     }
1324     else
1325     {
1326         nsICommandParams *nsparam = create_nscommand_params();
1327
1328         nsICommandParams_SetStringValue(nsparam, NSSTATE_ATTRIBUTE, &ns_url);
1329         do_ns_command(This->nscontainer, NSCMD_INSERTLINKNOUI, nsparam);
1330         nsICommandParams_Release(nsparam);
1331     }
1332
1333     nsAString_Finish(&ns_url);
1334
1335     nsISelection_Release(nsselection);
1336
1337     if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1338         SysFreeString(url);
1339
1340     return S_OK;
1341 }
1342
1343 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1344 {
1345     TRACE("(%p)->(%p)\n", This, cmd);
1346
1347     cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1348     return S_OK;
1349 }
1350
1351 const cmdtable_t editmode_cmds[] = {
1352     {IDM_DELETE,          query_edit_status,    exec_delete},
1353     {IDM_FONTNAME,        query_edit_status,    exec_fontname},
1354     {IDM_FONTSIZE,        query_edit_status,    exec_fontsize},
1355     {IDM_SELECTALL,       query_selall_status , exec_selectall},
1356     {IDM_FORECOLOR,       query_edit_status,    exec_forecolor},
1357     {IDM_BOLD,            query_edit_status,    exec_bold},
1358     {IDM_ITALIC,          query_edit_status,    exec_italic},
1359     {IDM_JUSTIFYCENTER,   query_justify,        exec_justifycenter},
1360     {IDM_JUSTIFYRIGHT,    query_justify,        exec_justifyright},
1361     {IDM_JUSTIFYLEFT,     query_justify,        exec_justifyleft},
1362     {IDM_UNDERLINE,       query_edit_status,    exec_underline},
1363     {IDM_HORIZONTALLINE,  query_edit_status,    exec_horizontalline},
1364     {IDM_ORDERLIST,       query_edit_status,    exec_orderlist},
1365     {IDM_UNORDERLIST,     query_edit_status,    exec_unorderlist},
1366     {IDM_INDENT,          query_edit_status,    exec_indent},
1367     {IDM_OUTDENT,         query_edit_status,    exec_outdent},
1368     {IDM_COMPOSESETTINGS, NULL,                 exec_composesettings},
1369     {IDM_HYPERLINK,       query_edit_status,    exec_hyperlink},
1370     {0,NULL,NULL}
1371 };
1372
1373 void init_editor(HTMLDocument *This)
1374 {
1375     update_doc(This, UPDATE_UI);
1376
1377     if(!This->nscontainer)
1378         return;
1379
1380     set_ns_fontname(This->nscontainer, "Times New Roman");
1381 }
1382
1383 HRESULT editor_is_dirty(HTMLDocument *This)
1384 {
1385     PRBool modified;
1386
1387     if(!This->nscontainer || !This->nscontainer->editor)
1388         return S_FALSE;
1389
1390     nsIEditor_GetDocumentModified(This->nscontainer->editor, &modified);
1391
1392     return modified ? S_OK : S_FALSE;
1393 }