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