mshtml: Remove no longer used defines.
[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 BOOL is_visible_text_node(nsIDOMNode *node)
389 {
390     nsIDOMCharacterData *char_data;
391     nsAString data_str;
392     LPCWSTR data, ptr;
393     PRUint32 len;
394
395     nsIDOMNode_QueryInterface(node, &IID_nsIDOMCharacterData, (void**)&char_data);
396
397     nsIDOMCharacterData_GetLength(char_data, &len);
398
399     nsAString_Init(&data_str, NULL);
400     nsIDOMCharacterData_GetData(char_data, &data_str);
401     nsAString_GetData(&data_str, &data, NULL);
402
403     if(*data == '\n') {
404         len--;
405         for(ptr=data+1; ptr && isspaceW(*ptr); ptr++)
406             len--;
407     }
408
409     nsAString_Finish(&data_str);
410
411     nsIDOMCharacterData_Release(char_data);
412
413     return len != 0;
414 }
415
416 static nsIDOMNode *get_child_text_node(nsIDOMNode *node, BOOL first)
417 {
418     nsIDOMNode *iter, *iter2;
419
420     if(first)
421         nsIDOMNode_GetFirstChild(node, &iter);
422     else
423         nsIDOMNode_GetLastChild(node, &iter);
424
425     while(iter) {
426         PRUint16 node_type;
427
428         nsIDOMNode_GetNodeType(iter, &node_type);
429         switch(node_type) {
430         case TEXT_NODE:
431             if(is_visible_text_node(iter))
432                 return iter;
433         case ELEMENT_NODE:
434             iter2 = get_child_text_node(iter, first);
435             if(iter2) {
436                 nsIDOMNode_Release(iter);
437                 return iter2;
438             }
439         }
440
441         if(first)
442             nsIDOMNode_GetNextSibling(iter, &iter2);
443         else
444             nsIDOMNode_GetPreviousSibling(iter, &iter2);
445
446         nsIDOMNode_Release(iter);
447         iter = iter2;
448     }
449
450     return NULL;
451 }
452
453 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
454 {
455     int i=0;
456     PRBool b;
457
458     nsIDOMKeyEvent_GetCtrlKey(event, &b);
459     if(b)
460         i |= 1;
461
462     nsIDOMKeyEvent_GetShiftKey(event, &b);
463     if(b)
464         i |= 2;
465
466     if(cmds[i])
467         do_ns_editor_command(This->nscontainer, cmds[i]);
468
469     nsIDOMKeyEvent_PreventDefault(event);
470 }
471
472 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
473 {
474     nsIDOMKeyEvent *key_event;
475     PRUint32 code;
476
477     nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
478
479     nsIDOMKeyEvent_GetKeyCode(key_event, &code);
480
481     switch(code) {
482     case DOM_VK_LEFT: {
483         static const char * const cmds[] = {
484             NSCMD_CHARPREVIOUS,
485             NSCMD_WORDPREVIOUS,
486             NSCMD_SELECTCHARPREVIOUS,
487             NSCMD_SELECTWORDPREVIOUS
488         };
489
490         TRACE("left\n");
491         handle_arrow_key(This, key_event, cmds);
492         break;
493     }
494     case DOM_VK_RIGHT: {
495         static const char * const cmds[] = {
496             NSCMD_CHARNEXT,
497             NSCMD_WORDNEXT,
498             NSCMD_SELECTCHARNEXT,
499             NSCMD_SELECTWORDNEXT
500         };
501
502         TRACE("right\n");
503         handle_arrow_key(This, key_event, cmds);
504         break;
505     }
506     case DOM_VK_UP: {
507         static const char * const cmds[] = {
508             NSCMD_LINEPREVIOUS,
509             NSCMD_MOVEPAGEUP,
510             NSCMD_SELECTLINEPREVIOUS,
511             NSCMD_SELECTPAGEUP
512         };
513
514         TRACE("up\n");
515         handle_arrow_key(This, key_event, cmds);
516         break;
517     }
518     case DOM_VK_DOWN: {
519         static const char * const cmds[] = {
520             NSCMD_LINENEXT,
521             NSCMD_MOVEPAGEDOWN,
522             NSCMD_SELECTLINENEXT,
523             NSCMD_SELECTPAGEDOWN
524         };
525
526         TRACE("down\n");
527         handle_arrow_key(This, key_event, cmds);
528         break;
529     }
530     case DOM_VK_DELETE: {
531         static const char * const cmds[] = {
532             NSCMD_DELETECHARFORWARD,
533             NSCMD_DELETEWORDFORWARD,
534             NULL, NULL
535         };
536
537         TRACE("delete\n");
538         handle_arrow_key(This, key_event, cmds);
539         break;
540     }
541     case DOM_VK_HOME: {
542         static const char * const cmds[] = {
543             NSCMD_BEGINLINE,
544             NSCMD_MOVETOP,
545             NSCMD_SELECTBEGINLINE,
546             NSCMD_SELECTTOP
547         };
548
549         TRACE("home\n");
550         handle_arrow_key(This, key_event, cmds);
551         break;
552     }
553     case DOM_VK_END: {
554         static const char * const cmds[] = {
555             NSCMD_ENDLINE,
556             NSCMD_MOVEBOTTOM,
557             NSCMD_SELECTENDLINE,
558             NSCMD_SELECTBOTTOM
559         };
560
561         TRACE("end\n");
562         handle_arrow_key(This, key_event, cmds);
563         break;
564     }
565     }
566
567     nsIDOMKeyEvent_Release(key_event);
568 }
569
570 void handle_edit_load(HTMLDocument *This)
571 {
572     get_editor_controller(This->nscontainer);
573
574     if(This->ui_active) {
575         OLECHAR wszHTMLDocument[30];
576         RECT rcBorderWidths;
577
578         if(This->ip_window)
579             IOleInPlaceUIWindow_SetActiveObject(This->ip_window, NULL, NULL);
580         if(This->hostui)
581             IDocHostUIHandler_HideUI(This->hostui);
582
583         if(This->hostui)
584             IDocHostUIHandler_ShowUI(This->hostui, DOCHOSTUITYPE_AUTHOR, ACTOBJ(This), CMDTARGET(This),
585                 This->frame, This->ip_window);
586
587         LoadStringW(hInst, IDS_HTMLDOCUMENT, wszHTMLDocument,
588                     sizeof(wszHTMLDocument)/sizeof(WCHAR));
589
590         if(This->ip_window)
591             IOleInPlaceUIWindow_SetActiveObject(This->ip_window, ACTOBJ(This), wszHTMLDocument);
592
593         memset(&rcBorderWidths, 0, sizeof(rcBorderWidths));
594         IOleInPlaceFrame_SetBorderSpace(This->frame, &rcBorderWidths);
595     }
596 }
597
598 static void set_ns_fontname(NSContainer *This, const char *fontname)
599 {
600     nsICommandParams *nsparam = create_nscommand_params();
601
602     nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
603     do_ns_command(This, NSCMD_FONTFACE, nsparam);
604     nsICommandParams_Release(nsparam);
605 }
606
607 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
608 {
609     TRACE("(%p)->(%p %p)\n", This, in, out);
610
611     if(This->nscontainer)
612         do_ns_editor_command(This->nscontainer, NSCMD_DELETECHARFORWARD);
613
614     update_doc(This, UPDATE_UI);
615     return S_OK;
616 }
617
618 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
619 {
620     TRACE("(%p)->(%p %p)\n", This, in, out);
621
622     if(!This->nscontainer) {
623         update_doc(This, UPDATE_UI);
624         return E_FAIL;
625     }
626
627     if(in) {
628         char *stra;
629         DWORD len;
630
631         if(V_VT(in) != VT_BSTR) {
632             FIXME("Unsupported vt=%d\n", V_VT(out));
633             return E_INVALIDARG;
634         }
635
636         TRACE("%s\n", debugstr_w(V_BSTR(in)));
637
638         len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, NULL, 0, NULL, NULL);
639         stra = mshtml_alloc(len);
640         WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, stra, -1, NULL, NULL);
641
642         set_ns_fontname(This->nscontainer, stra);
643
644         mshtml_free(stra);
645
646         update_doc(This, UPDATE_UI);
647     }
648
649     if(out) {
650         nsICommandParams *nsparam;
651         LPWSTR strw;
652         char *stra;
653         DWORD len;
654         nsresult nsres;
655
656         nsparam = create_nscommand_params();
657
658         nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
659         if(NS_FAILED(nsres))
660             return S_OK;
661
662         nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
663         nsICommandParams_Release(nsparam);
664
665         len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
666         strw = mshtml_alloc(len*sizeof(WCHAR));
667         MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, -1);
668         nsfree(stra);
669
670         V_VT(out) = VT_BSTR;
671         V_BSTR(out) = SysAllocString(strw);
672         mshtml_free(strw);
673     }
674
675     return S_OK;
676 }
677
678 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
679 {
680     TRACE("(%p)->(%p %p)\n", This, in, out);
681
682     if(in) {
683         if(V_VT(in) == VT_I4) {
684             nsICommandParams *nsparam = create_nscommand_params();
685             char color_str[10];
686
687             sprintf(color_str, "#%02x%02x%02x",
688                     V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
689
690             nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
691             do_ns_command(This->nscontainer, NSCMD_FONTCOLOR, nsparam);
692
693             nsICommandParams_Release(nsparam);
694         }else {
695             FIXME("unsupported in vt=%d\n", V_VT(in));
696         }
697
698         update_doc(This, UPDATE_UI);
699     }
700
701     if(out) {
702         FIXME("unsupported out\n");
703         return E_NOTIMPL;
704     }
705
706     return S_OK;
707 }
708
709 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
710 {
711     TRACE("(%p)->(%p %p)\n", This, in, out);
712
713     if(out) {
714         WCHAR val[10] = {0};
715
716         get_font_size(This, val);
717         V_VT(out) = VT_I4;
718         V_I4(out) = strtolW(val, NULL, 10);
719     }
720
721     if(in) {
722         switch(V_VT(in)) {
723         case VT_I4: {
724             WCHAR size[10];
725             static const WCHAR format[] = {'%','d',0};
726             wsprintfW(size, format, V_I4(in));
727             set_font_size(This, size);
728             break;
729         }
730         case VT_BSTR:
731             set_font_size(This, V_BSTR(in));
732             break;
733         default:
734             FIXME("unsupported vt %d\n", V_VT(in));
735         }
736
737         update_doc(This, UPDATE_UI);
738     }
739
740     return S_OK;
741 }
742
743 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
744 {
745
746     FIXME("(%p)->(%p %p)\n", This, in, out);
747     return E_NOTIMPL;
748 }
749
750 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
751 {
752     TRACE("(%p)\n", This);
753
754     if(in || out)
755         FIXME("unsupported args\n");
756
757     if(This->nscontainer)
758         do_ns_command(This->nscontainer, NSCMD_SELECTALL, NULL);
759
760     update_doc(This, UPDATE_UI);
761     return S_OK;
762 }
763
764 static HRESULT exec_bold(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_BOLD, NULL);
773
774     update_doc(This, UPDATE_UI);
775     return S_OK;
776 }
777
778 static HRESULT exec_italic(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_ITALIC, NULL);
787
788     update_doc(This, UPDATE_UI);
789     return S_OK;
790 }
791
792 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
793 {
794     switch(cmd->cmdID) {
795     case IDM_JUSTIFYCENTER:
796         TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
797         cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
798         break;
799     case IDM_JUSTIFYLEFT:
800         TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
801         /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
802         if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
803             cmd->cmdf = OLECMDF_SUPPORTED;
804         else
805             cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
806         break;
807     case IDM_JUSTIFYRIGHT:
808         TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
809         cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
810         break;
811     }
812
813     return S_OK;
814 }
815
816 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
817 {
818     TRACE("(%p)\n", This);
819
820     if(in || out)
821         FIXME("unsupported args\n");
822
823     set_ns_align(This, NSALIGN_CENTER);
824     update_doc(This, UPDATE_UI);
825     return S_OK;
826 }
827
828 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
829 {
830     TRACE("(%p)\n", This);
831
832     if(in || out)
833         FIXME("unsupported args\n");
834
835     set_ns_align(This, NSALIGN_LEFT);
836     update_doc(This, UPDATE_UI);
837     return S_OK;
838 }
839
840 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
841 {
842     TRACE("(%p)\n", This);
843
844     if(in || out)
845         FIXME("unsupported args\n");
846
847     set_ns_align(This, NSALIGN_RIGHT);
848     update_doc(This, UPDATE_UI);
849     return S_OK;
850 }
851
852 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
853 {
854     TRACE("(%p)\n", This);
855
856     if(in || out)
857         FIXME("unsupported args\n");
858
859     if(This->nscontainer)
860         do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
861
862     update_doc(This, UPDATE_UI);
863     return S_OK;
864 }
865
866 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
867 {
868     TRACE("(%p)\n", This);
869
870     if(in || out)
871         FIXME("unsupported args\n");
872
873     if(This->nscontainer)
874         do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
875
876     update_doc(This, UPDATE_UI);
877     return S_OK;
878 }
879
880 static HRESULT exec_orderlist(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     if(This->nscontainer)
888         do_ns_command(This->nscontainer, NSCMD_OL, NULL);
889
890     update_doc(This, UPDATE_UI);
891     return S_OK;
892 }
893
894 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
895 {
896     TRACE("(%p)\n", This);
897
898     if(in || out)
899         FIXME("unsupported args\n");
900
901     if(This->nscontainer)
902         do_ns_command(This->nscontainer, NSCMD_UL, NULL);
903
904     update_doc(This, UPDATE_UI);
905     return S_OK;
906 }
907
908 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
909 {
910     TRACE("(%p)\n", This);
911
912     if(in || out)
913         FIXME("unsupported args\n");
914
915     if(This->nscontainer)
916         do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
917
918     update_doc(This, UPDATE_UI);
919     return S_OK;
920 }
921
922 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
923 {
924     TRACE("(%p)\n", This);
925
926     if(in || out)
927         FIXME("unsupported args\n");
928
929     if(This->nscontainer)
930         do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
931
932     update_doc(This, UPDATE_UI);
933     return S_OK;
934 }
935
936 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
937 {
938     WCHAR *ptr;
939
940     if(out || !in || V_VT(in) != VT_BSTR) {
941         WARN("invalid arg\n");
942         return E_INVALIDARG;
943     }
944
945     TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
946
947     update_doc(This, UPDATE_UI);
948
949     ptr = V_BSTR(in);
950     if(*ptr == '1')
951         exec_bold(This, cmdexecopt, NULL, NULL);
952     ptr = strchrW(ptr, ',');
953     if(!ptr)
954         return S_OK;
955
956     if(*++ptr == '1')
957         exec_italic(This, cmdexecopt, NULL, NULL);
958     ptr = strchrW(ptr, ',');
959     if(!ptr)
960         return S_OK;
961
962     if(*++ptr == '1')
963         exec_underline(This, cmdexecopt, NULL, NULL);
964     ptr = strchrW(ptr, ',');
965     if(!ptr)
966         return S_OK;
967
968     if(isdigitW(*++ptr)) {
969         VARIANT v;
970
971         V_VT(&v) = VT_I4;
972         V_I4(&v) = *ptr-'0';
973
974         exec_fontsize(This, cmdexecopt, &v, NULL);
975     }
976     ptr = strchrW(ptr, ',');
977     if(!ptr)
978         return S_OK;
979
980     if(*++ptr != ',')
981         FIXME("set font color\n");
982     ptr = strchrW(ptr, ',');
983     if(!ptr)
984         return S_OK;
985
986     if(*++ptr != ',')
987         FIXME("set background color\n");
988     ptr = strchrW(ptr, ',');
989     if(!ptr)
990         return S_OK;
991
992     ptr++;
993     if(*ptr) {
994         VARIANT v;
995
996         V_VT(&v) = VT_BSTR;
997         V_BSTR(&v) = SysAllocString(ptr);
998
999         exec_fontname(This, cmdexecopt, &v, NULL);
1000
1001         SysFreeString(V_BSTR(&v));
1002     }
1003
1004     return S_OK;
1005 }
1006
1007 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1008 {
1009     update_doc(This, UPDATE_UI);
1010
1011     if(!This->nscontainer)
1012         return E_FAIL;
1013
1014     do_ns_editor_command(This->nscontainer, NSCMD_COPY);
1015     return S_OK;
1016 }
1017
1018 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1019 {
1020     update_doc(This, UPDATE_UI);
1021
1022     if(!This->nscontainer)
1023         return E_FAIL;
1024
1025     do_ns_editor_command(This->nscontainer, NSCMD_CUT);
1026     return S_OK;
1027 }
1028
1029 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1030 {
1031     update_doc(This, UPDATE_UI);
1032
1033     if(!This->nscontainer)
1034         return E_FAIL;
1035
1036     do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
1037     return S_OK;
1038 }
1039
1040 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
1041 {
1042     switch(cmd->cmdID) {
1043     case IDM_DELETE:
1044         TRACE("CGID_MSHTML: IDM_DELETE\n");
1045         cmd->cmdf = query_ns_edit_status(This, NULL);
1046         break;
1047     case IDM_FONTNAME:
1048         TRACE("CGID_MSHTML: IDM_FONTNAME\n");
1049         cmd->cmdf = query_ns_edit_status(This, NULL);
1050         break;
1051     case IDM_FONTSIZE:
1052         TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
1053         cmd->cmdf = query_ns_edit_status(This, NULL);
1054         break;
1055     case IDM_BOLD:
1056         TRACE("CGID_MSHTML: IDM_BOLD\n");
1057         cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
1058         break;
1059     case IDM_FORECOLOR:
1060         TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
1061         cmd->cmdf = query_ns_edit_status(This, NULL);
1062         break;
1063     case IDM_ITALIC:
1064         TRACE("CGID_MSHTML: IDM_ITALIC\n");
1065         cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
1066         break;
1067     case IDM_UNDERLINE:
1068         TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
1069         cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
1070         break;
1071     case IDM_HORIZONTALLINE:
1072         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1073         cmd->cmdf = query_ns_edit_status(This, NULL);
1074         break;
1075     case IDM_ORDERLIST:
1076         TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1077         cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1078         break;
1079     case IDM_UNORDERLIST:
1080         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1081         cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1082         break;
1083     case IDM_INDENT:
1084         TRACE("CGID_MSHTML: IDM_INDENT\n");
1085         cmd->cmdf = query_ns_edit_status(This, NULL);
1086         break;
1087     case IDM_OUTDENT:
1088         TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1089         cmd->cmdf = query_ns_edit_status(This, NULL);
1090         break;
1091     case IDM_HYPERLINK:
1092         TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1093         cmd->cmdf = query_ns_edit_status(This, NULL);
1094         break;
1095     }
1096
1097     return S_OK;
1098 }
1099
1100 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1101 {
1102     static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1103
1104     switch (msg)
1105     {
1106         case WM_INITDIALOG:
1107         {
1108             static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1109             static const WCHAR wszFtp[] = {'f','t','p',':',0};
1110             static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1111             static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1112             static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1113             static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1114             INT def_idx;
1115             HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1116             HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1117             INT len;
1118
1119             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1120
1121             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1122             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1123             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1124             def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1125             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1126             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1127             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1128             SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1129
1130             /* force the updating of the URL edit box */
1131             SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1132
1133             SetFocus(hwndURL);
1134             len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1135             SendMessageW(hwndURL, EM_SETSEL, 0, len);
1136
1137             return FALSE;
1138         }
1139         case WM_COMMAND:
1140             switch (wparam)
1141             {
1142                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1143                     EndDialog(hwnd, wparam);
1144                     return TRUE;
1145                 case MAKEWPARAM(IDOK, BN_CLICKED):
1146                 {
1147                     BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1148                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1149                     INT len = GetWindowTextLengthW(hwndURL);
1150                     *url = SysAllocStringLen(NULL, len + 1);
1151                     GetWindowTextW(hwndURL, *url, len + 1);
1152                     EndDialog(hwnd, wparam);
1153                     return TRUE;
1154                 }
1155                 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1156                 {
1157                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1158                     INT item;
1159                     INT len;
1160                     LPWSTR type;
1161                     LPWSTR url;
1162                     LPWSTR p;
1163                     static const WCHAR wszSlashSlash[] = {'/','/'};
1164
1165                     /* get string of currently selected hyperlink type */
1166                     item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1167                     len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1168                     type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1169                     SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1170
1171                     if (!strcmpW(type, wszOther))
1172                         *type = '\0';
1173
1174                     /* get current URL */
1175                     len = GetWindowTextLengthW(hwndURL);
1176                     url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1177                     GetWindowTextW(hwndURL, url, len + 1);
1178
1179                     /* strip off old protocol */
1180                     p = strchrW(url, ':');
1181                     if (p && p[1] == '/' && p[2] == '/')
1182                         p += 3;
1183                     if (!p) p = url;
1184                     memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1185
1186                     /* add new protocol */
1187                     if (*type != '\0')
1188                     {
1189                         memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1190                         memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1191                     }
1192
1193                     SetWindowTextW(hwndURL, url);
1194
1195                     HeapFree(GetProcessHeap(), 0, url);
1196                     HeapFree(GetProcessHeap(), 0, type);
1197                     return TRUE;
1198                 }
1199             }
1200             return FALSE;
1201         case WM_CLOSE:
1202             EndDialog(hwnd, IDCANCEL);
1203             return TRUE;
1204         default:
1205             return FALSE;
1206     }
1207 }
1208
1209 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1210 {
1211     BSTR url = NULL;
1212     INT ret;
1213     nsAString ns_url;
1214     PRBool insert_link_at_caret;
1215     nsISelection *nsselection;
1216
1217     FIXME("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1218
1219     if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1220     {
1221         if (!in || V_VT(in) != VT_BSTR)
1222         {
1223             WARN("invalid arg\n");
1224             return E_INVALIDARG;
1225         }
1226         url = V_BSTR(in);
1227     }
1228     else
1229     {
1230         ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1231         if (ret != IDOK)
1232             return OLECMDERR_E_CANCELED;
1233     }
1234
1235     nsselection = get_ns_selection(This);
1236     if (!nsselection)
1237         return E_FAIL;
1238
1239     nsAString_Init(&ns_url, url);
1240
1241     nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1242
1243     if (insert_link_at_caret)
1244     {
1245         static const WCHAR wszA[] = {'a',0};
1246         static const WCHAR wszHref[] = {'h','r','e','f',0};
1247         nsIHTMLEditor *html_editor;
1248         nsIDOMDocument *nsdoc;
1249         nsIDOMNode *text_node;
1250         nsIDOMElement *anchor_elem;
1251         nsIDOMNode *unused_node;
1252         nsAString a_str;
1253         nsAString href_str;
1254         nsresult nsres;
1255
1256         nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
1257         if(NS_FAILED(nsres))
1258             return E_FAIL;
1259
1260         nsAString_Init(&a_str, wszA);
1261         nsAString_Init(&href_str, wszHref);
1262
1263         /* create an element for the link */
1264         nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
1265         nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1266
1267         nsAString_Finish(&href_str);
1268         nsAString_Finish(&a_str);
1269
1270         /* create an element with text of URL */
1271         nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
1272
1273         /* wrap the <a> tags around the text element */
1274         nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1275         nsIDOMNode_Release(text_node);
1276         nsIDOMNode_Release(unused_node);
1277
1278         nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1279         if (html_editor)
1280         {
1281             /* add them to the document at the caret position */
1282             nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
1283             nsIHTMLEditor_Release(html_editor);
1284         }
1285
1286         nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1287
1288         nsIDOMElement_Release(anchor_elem);
1289         nsIDOMDocument_Release(nsdoc);
1290     }
1291     else
1292     {
1293         nsICommandParams *nsparam = create_nscommand_params();
1294
1295         nsICommandParams_SetStringValue(nsparam, NSSTATE_ATTRIBUTE, &ns_url);
1296         do_ns_command(This->nscontainer, NSCMD_INSERTLINKNOUI, nsparam);
1297         nsICommandParams_Release(nsparam);
1298     }
1299
1300     nsAString_Finish(&ns_url);
1301
1302     nsISelection_Release(nsselection);
1303
1304     if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1305         SysFreeString(url);
1306
1307     return S_OK;
1308 }
1309
1310 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1311 {
1312     TRACE("(%p)->(%p)\n", This, cmd);
1313
1314     cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1315     return S_OK;
1316 }
1317
1318 const cmdtable_t editmode_cmds[] = {
1319     {IDM_DELETE,          query_edit_status,    exec_delete},
1320     {IDM_FONTNAME,        query_edit_status,    exec_fontname},
1321     {IDM_FONTSIZE,        query_edit_status,    exec_fontsize},
1322     {IDM_SELECTALL,       query_selall_status , exec_selectall},
1323     {IDM_FORECOLOR,       query_edit_status,    exec_forecolor},
1324     {IDM_BOLD,            query_edit_status,    exec_bold},
1325     {IDM_ITALIC,          query_edit_status,    exec_italic},
1326     {IDM_JUSTIFYCENTER,   query_justify,        exec_justifycenter},
1327     {IDM_JUSTIFYRIGHT,    query_justify,        exec_justifyright},
1328     {IDM_JUSTIFYLEFT,     query_justify,        exec_justifyleft},
1329     {IDM_FONT,            NULL,                 exec_font},
1330     {IDM_UNDERLINE,       query_edit_status,    exec_underline},
1331     {IDM_HORIZONTALLINE,  query_edit_status,    exec_horizontalline},
1332     {IDM_ORDERLIST,       query_edit_status,    exec_orderlist},
1333     {IDM_UNORDERLIST,     query_edit_status,    exec_unorderlist},
1334     {IDM_INDENT,          query_edit_status,    exec_indent},
1335     {IDM_OUTDENT,         query_edit_status,    exec_outdent},
1336     {IDM_COMPOSESETTINGS, NULL,                 exec_composesettings},
1337     {IDM_HYPERLINK,       query_edit_status,    exec_hyperlink},
1338     {0,NULL,NULL}
1339 };
1340
1341 void init_editor(HTMLDocument *This)
1342 {
1343     update_doc(This, UPDATE_UI);
1344
1345     if(!This->nscontainer)
1346         return;
1347
1348     set_ns_fontname(This->nscontainer, "Times New Roman");
1349 }
1350
1351 HRESULT editor_is_dirty(HTMLDocument *This)
1352 {
1353     PRBool modified;
1354
1355     if(!This->nscontainer || !This->nscontainer->editor)
1356         return S_FALSE;
1357
1358     nsIEditor_GetDocumentModified(This->nscontainer->editor, &modified);
1359
1360     return modified ? S_OK : S_FALSE;
1361 }