rpcrt4: Try a lot harder to resuse existing connections by comparing inside the RpcQu...
[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
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39
40 #define NSCMD_ALIGN        "cmd_align"
41 #define NSCMD_BOLD         "cmd_bold"
42 #define NSCMD_CHARNEXT     "cmd_charNext"
43 #define NSCMD_CHARPREVIOUS "cmd_charPrevious"
44 #define NSCMD_COPY         "cmd_copy"
45 #define NSCMD_CUT          "cmd_cut"
46 #define NSCMD_FONTCOLOR    "cmd_fontColor"
47 #define NSCMD_FONTFACE     "cmd_fontFace"
48 #define NSCMD_INDENT       "cmd_indent"
49 #define NSCMD_INSERTHR     "cmd_insertHR"
50 #define NSCMD_ITALIC       "cmd_italic"
51 #define NSCMD_LINENEXT     "cmd_lineNext"
52 #define NSCMD_LINEPREVIOUS "cmd_linePrevious"
53 #define NSCMD_MOVEPAGEDOWN "cmd_movePageDown"
54 #define NSCMD_MOVEPAGEUP   "cmd_movePageUp"
55 #define NSCMD_OL           "cmd_ol"
56 #define NSCMD_OUTDENT      "cmd_outdent"
57 #define NSCMD_PASTE        "cmd_paste"
58 #define NSCMD_SELECTCHARNEXT      "cmd_selectCharNext"
59 #define NSCMD_SELECTCHARPREVIOUS  "cmd_selectCharPrevious"
60 #define NSCMD_SELECTLINENEXT      "cmd_selectLineNext"
61 #define NSCMD_SELECTLINEPREVIOUS  "cmd_selectLinePrevious"
62 #define NSCMD_SELECTPAGEDOWN      "cmd_selectPageDown"
63 #define NSCMD_SELECTPAGEUP        "cmd_selectPageUp"
64 #define NSCMD_SELECTWORDNEXT      "cmd_selectWordNext"
65 #define NSCMD_SELECTWORDPREVIOUS  "cmd_selectWordPrevious"
66 #define NSCMD_UL           "cmd_ul"
67 #define NSCMD_UNDERLINE    "cmd_underline"
68 #define NSCMD_WORDNEXT     "cmd_wordNext"
69 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
70
71 #define NSSTATE_ATTRIBUTE "state_attribute"
72 #define NSSTATE_ALL       "state_all"
73
74 #define NSALIGN_CENTER "center"
75 #define NSALIGN_LEFT   "left"
76 #define NSALIGN_RIGHT  "right"
77
78 #define DOM_VK_LEFT  VK_LEFT
79 #define DOM_VK_UP    VK_UP
80 #define DOM_VK_RIGHT VK_RIGHT
81 #define DOM_VK_DOWN  VK_DOWN
82
83 static const WCHAR wszFont[] = {'f','o','n','t',0};
84 static const WCHAR wszSize[] = {'s','i','z','e',0};
85
86 static void do_ns_command(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
87 {
88     nsICommandManager *cmdmgr;
89     nsIInterfaceRequestor *iface_req;
90     nsresult nsres;
91
92     TRACE("(%p)\n", This);
93
94     nsres = nsIWebBrowser_QueryInterface(This->webbrowser,
95             &IID_nsIInterfaceRequestor, (void**)&iface_req);
96     if(NS_FAILED(nsres)) {
97         ERR("Could not get nsIInterfaceRequestor: %08x\n", nsres);
98         return;
99     }
100
101     nsres = nsIInterfaceRequestor_GetInterface(iface_req, &IID_nsICommandManager,
102                                                (void**)&cmdmgr);
103     nsIInterfaceRequestor_Release(iface_req);
104     if(NS_FAILED(nsres)) {
105         ERR("Could not get nsICommandManager: %08x\n", nsres);
106         return;
107     }
108
109     nsres = nsICommandManager_DoCommand(cmdmgr, cmd, nsparam, NULL);
110     if(NS_FAILED(nsres))
111         ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
112
113     nsICommandManager_Release(cmdmgr);
114 }
115
116 static void do_ns_editor_command(NSContainer *This, const char *cmd)
117 {
118     nsresult nsres;
119
120     if(!This->editor_controller)
121         return;
122
123     nsres = nsIController_DoCommand(This->editor_controller, cmd);
124     if(NS_FAILED(nsres))
125         ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
126 }
127
128 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
129 {
130     nsICommandManager *cmdmgr;
131     nsIInterfaceRequestor *iface_req;
132     nsresult nsres;
133
134     nsres = nsIWebBrowser_QueryInterface(This->webbrowser,
135             &IID_nsIInterfaceRequestor, (void**)&iface_req);
136     if(NS_FAILED(nsres)) {
137         ERR("Could not get nsIInterfaceRequestor: %08x\n", nsres);
138         return nsres;
139     }
140
141     nsres = nsIInterfaceRequestor_GetInterface(iface_req, &IID_nsICommandManager,
142                                                (void**)&cmdmgr);
143     nsIInterfaceRequestor_Release(iface_req);
144     if(NS_FAILED(nsres)) {
145         ERR("Could not get nsICommandManager: %08x\n", nsres);
146         return nsres;
147     }
148
149     nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, NULL, nsparam);
150     if(NS_FAILED(nsres))
151         ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
152
153     nsICommandManager_Release(cmdmgr);
154     return nsres;
155 }
156
157 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
158 {
159     nsICommandParams *nsparam;
160     PRBool b = FALSE;
161
162     if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
163         return OLECMDF_SUPPORTED;
164
165     if(This->nscontainer && nscmd) {
166         nsparam = create_nscommand_params();
167         get_ns_command_state(This->nscontainer, nscmd, nsparam);
168
169         nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
170
171         nsICommandParams_Release(nsparam);
172     }
173
174     return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
175 }
176
177 static void set_ns_align(HTMLDocument *This, const char *align_str)
178 {
179     nsICommandParams *nsparam;
180
181     if(!This->nscontainer)
182         return;
183
184     nsparam = create_nscommand_params();
185     nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
186
187     do_ns_command(This->nscontainer, NSCMD_ALIGN, nsparam);
188
189     nsICommandParams_Release(nsparam);
190 }
191
192 static DWORD query_align_status(HTMLDocument *This, const char *align_str)
193 {
194     nsICommandParams *nsparam;
195     char *align = NULL;
196
197     if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
198         return OLECMDF_SUPPORTED;
199
200     if(This->nscontainer) {
201         nsparam = create_nscommand_params();
202         get_ns_command_state(This->nscontainer, NSCMD_ALIGN, nsparam);
203
204         nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &align);
205
206         nsICommandParams_Release(nsparam);
207     }
208
209     return OLECMDF_SUPPORTED | OLECMDF_ENABLED
210         | (align && !strcmp(align_str, align) ? OLECMDF_LATCHED : 0);
211 }
212
213
214 static nsISelection *get_ns_selection(HTMLDocument *This)
215 {
216     nsIDOMWindow *dom_window;
217     nsISelection *nsselection = NULL;
218     nsresult nsres;
219
220     if(!This->nscontainer)
221         return NULL;
222
223     nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
224     if(NS_FAILED(nsres))
225         return NULL;
226
227     nsIDOMWindow_GetSelection(dom_window, &nsselection);
228     nsIDOMWindow_Release(dom_window);
229
230     return nsselection;
231
232 }
233
234 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
235 {
236     PRBool has_children;
237     PRUint32 child_cnt, i;
238     nsIDOMNode *child_node;
239     nsIDOMNodeList *node_list;
240     PRUint16 node_type;
241
242     nsIDOMElement_HasChildNodes(elem, &has_children);
243     if(!has_children)
244         return;
245
246     nsIDOMElement_GetChildNodes(elem, &node_list);
247     nsIDOMNodeList_GetLength(node_list, &child_cnt);
248
249     for(i=0; i<child_cnt; i++) {
250         nsIDOMNodeList_Item(node_list, i, &child_node);
251
252         nsIDOMNode_GetNodeType(child_node, &node_type);
253         if(node_type == ELEMENT_NODE) {
254             nsIDOMElement *child_elem;
255             nsAString tag_str;
256             const PRUnichar *ctag;
257
258             nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
259
260             nsAString_Init(&tag_str, NULL);
261             nsIDOMElement_GetTagName(child_elem, &tag_str);
262             nsAString_GetData(&tag_str, &ctag, NULL);
263
264             if(!strcmpiW(ctag, tag))
265                 /* FIXME: remove node if there are no more attributes */
266                 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
267
268             nsAString_Finish(&tag_str);
269
270             remove_child_attr(child_elem, tag, attr_str);
271
272             nsIDOMNode_Release(child_elem);
273         }
274
275         nsIDOMNode_Release(child_node);
276     }
277
278     nsIDOMNodeList_Release(node_list);
279 }
280
281 static void get_font_size(HTMLDocument *This, WCHAR *ret)
282 {
283     nsISelection *nsselection = get_ns_selection(This);
284     nsIDOMElement *elem = NULL;
285     nsIDOMNode *node = NULL, *tmp_node;
286     nsAString tag_str;
287     LPCWSTR tag;
288     PRUint16 node_type;
289     nsresult nsres;
290
291     *ret = 0;
292
293     if(!nsselection)
294         return;
295
296     nsISelection_GetFocusNode(nsselection, &node);
297     nsISelection_Release(nsselection);
298
299     while(node) {
300         nsres = nsIDOMNode_GetNodeType(node, &node_type);
301         if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
302             break;
303
304         if(node_type == ELEMENT_NODE) {
305             nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
306
307             nsAString_Init(&tag_str, NULL);
308             nsIDOMElement_GetTagName(elem, &tag_str);
309             nsAString_GetData(&tag_str, &tag, NULL);
310
311             if(!strcmpiW(tag, wszFont)) {
312                 nsAString size_str, val_str;
313                 LPCWSTR val;
314
315                 TRACE("found font tag %p\n", elem);
316
317                 nsAString_Init(&size_str, wszSize);
318                 nsAString_Init(&val_str, NULL);
319
320                 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
321                 nsAString_GetData(&val_str, &val, NULL);
322
323                 if(*val) {
324                     TRACE("found size %s\n", debugstr_w(val));
325                     strcpyW(ret, val);
326                 }
327
328                 nsAString_Finish(&size_str);
329                 nsAString_Finish(&val_str);
330             }
331
332             nsAString_Finish(&tag_str);
333
334             nsIDOMElement_Release(elem);
335         }
336
337         if(*ret)
338             break;
339
340         tmp_node = node;
341         nsIDOMNode_GetParentNode(node, &node);
342         nsIDOMNode_Release(tmp_node);
343     }
344
345     if(node)
346         nsIDOMNode_Release(node);
347 }
348
349 static void set_font_size(HTMLDocument *This, LPCWSTR size)
350 {
351     nsISelection *nsselection;
352     PRBool collapsed;
353     nsIDOMDocument *nsdoc;
354     nsIDOMElement *elem;
355     nsIDOMRange *range;
356     PRInt32 range_cnt = 0;
357     nsAString font_str;
358     nsAString size_str;
359     nsAString val_str;
360     nsresult nsres;
361
362     nsselection = get_ns_selection(This);
363
364     if(!nsselection)
365         return;
366
367     nsISelection_GetRangeCount(nsselection, &range_cnt);
368     if(range_cnt != 1) {
369         FIXME("range_cnt %d not supprted\n", range_cnt);
370         if(!range_cnt) {
371             nsISelection_Release(nsselection);
372             return;
373         }
374     }
375
376     nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
377     if(NS_FAILED(nsres))
378         return;
379
380     nsAString_Init(&font_str, wszFont);
381     nsAString_Init(&size_str, wszSize);
382     nsAString_Init(&val_str, size);
383
384     nsIDOMDocument_CreateElement(nsdoc, &font_str, &elem);
385     nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
386
387     nsISelection_GetRangeAt(nsselection, 0, &range);
388     nsISelection_GetIsCollapsed(nsselection, &collapsed);
389     nsISelection_RemoveAllRanges(nsselection);
390
391     nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
392
393     if(collapsed) {
394         nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
395     }else {
396         /* Remove all size attrbutes from the range */
397         remove_child_attr(elem, wszFont, &size_str);
398         nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
399     }
400
401     nsIDOMRange_Release(range);
402     nsIDOMElement_Release(elem);
403
404     nsAString_Finish(&font_str);
405     nsAString_Finish(&size_str);
406     nsAString_Finish(&val_str);
407
408     nsISelection_Release(nsselection);
409     nsIDOMDocument_Release(nsdoc);
410 }
411
412 static BOOL is_visible_text_node(nsIDOMNode *node)
413 {
414     nsIDOMCharacterData *char_data;
415     nsAString data_str;
416     LPCWSTR data, ptr;
417     PRUint32 len;
418
419     nsIDOMNode_QueryInterface(node, &IID_nsIDOMCharacterData, (void**)&char_data);
420
421     nsIDOMCharacterData_GetLength(char_data, &len);
422
423     nsAString_Init(&data_str, NULL);
424     nsIDOMCharacterData_GetData(char_data, &data_str);
425     nsAString_GetData(&data_str, &data, NULL);
426
427     if(*data == '\n') {
428         len--;
429         for(ptr=data+1; ptr && isspaceW(*ptr); ptr++)
430             len--;
431     }
432
433     nsAString_Finish(&data_str);
434
435     nsIDOMCharacterData_Release(char_data);
436
437     return len != 0;
438 }
439
440 static nsIDOMNode *get_child_text_node(nsIDOMNode *node, BOOL first)
441 {
442     nsIDOMNode *iter, *iter2;
443
444     if(first)
445         nsIDOMNode_GetFirstChild(node, &iter);
446     else
447         nsIDOMNode_GetLastChild(node, &iter);
448
449     while(iter) {
450         PRUint16 node_type;
451
452         nsIDOMNode_GetNodeType(iter, &node_type);
453         switch(node_type) {
454         case TEXT_NODE:
455             if(is_visible_text_node(iter))
456                 return iter;
457         case ELEMENT_NODE:
458             iter2 = get_child_text_node(iter, first);
459             if(iter2) {
460                 nsIDOMNode_Release(iter);
461                 return iter2;
462             }
463         }
464
465         if(first)
466             nsIDOMNode_GetNextSibling(iter, &iter2);
467         else
468             nsIDOMNode_GetPreviousSibling(iter, &iter2);
469
470         nsIDOMNode_Release(iter);
471         iter = iter2;
472     }
473
474     return NULL;
475 }
476
477 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char **cmds)
478 {
479     int i=0;
480     PRBool b;
481
482     nsIDOMKeyEvent_GetCtrlKey(event, &b);
483     if(b)
484         i |= 1;
485
486     nsIDOMKeyEvent_GetShiftKey(event, &b);
487     if(b)
488         i |= 2;
489
490     do_ns_editor_command(This->nscontainer, cmds[i]);
491
492     nsIDOMKeyEvent_PreventDefault(event);
493 }
494
495 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
496 {
497     nsIDOMKeyEvent *key_event;
498     PRUint32 code;
499
500     nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
501
502     nsIDOMKeyEvent_GetKeyCode(key_event, &code);
503
504     switch(code) {
505     case DOM_VK_LEFT: {
506         static const char *cmds[] = {
507             NSCMD_CHARPREVIOUS,
508             NSCMD_WORDPREVIOUS,
509             NSCMD_SELECTCHARPREVIOUS,
510             NSCMD_SELECTWORDPREVIOUS
511         };
512
513         TRACE("left\n");
514         handle_arrow_key(This, key_event, cmds);
515         break;
516     }
517     case DOM_VK_RIGHT: {
518         static const char *cmds[] = {
519             NSCMD_CHARNEXT,
520             NSCMD_WORDNEXT,
521             NSCMD_SELECTCHARNEXT,
522             NSCMD_SELECTWORDNEXT
523         };
524
525         TRACE("right\n");
526         handle_arrow_key(This, key_event, cmds);
527         break;
528     }
529     case DOM_VK_UP: {
530         static const char *cmds[] = {
531             NSCMD_LINEPREVIOUS,
532             NSCMD_MOVEPAGEUP,
533             NSCMD_SELECTLINEPREVIOUS,
534             NSCMD_SELECTPAGEUP
535         };
536
537         TRACE("up\n");
538         handle_arrow_key(This, key_event, cmds);
539         break;
540     }
541     case DOM_VK_DOWN: {
542         static const char *cmds[] = {
543             NSCMD_LINENEXT,
544             NSCMD_MOVEPAGEDOWN,
545             NSCMD_SELECTLINENEXT,
546             NSCMD_SELECTPAGEDOWN
547         };
548
549         TRACE("down\n");
550         handle_arrow_key(This, key_event, cmds);
551         break;
552     }
553     };
554
555     nsIDOMKeyEvent_Release(key_event);
556 }
557
558 static void set_ns_fontname(NSContainer *This, const char *fontname)
559 {
560     nsICommandParams *nsparam = create_nscommand_params();
561
562     nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
563     do_ns_command(This, NSCMD_FONTFACE, nsparam);
564     nsICommandParams_Release(nsparam);
565 }
566
567 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
568 {
569     TRACE("(%p)->(%p %p)\n", This, in, out);
570
571     if(!This->nscontainer) {
572         update_doc(This, UPDATE_UI);
573         return E_FAIL;
574     }
575
576     if(in) {
577         char *stra;
578         DWORD len;
579
580         if(V_VT(in) != VT_BSTR) {
581             FIXME("Unsupported vt=%d\n", V_VT(out));
582             return E_INVALIDARG;
583         }
584
585         TRACE("%s\n", debugstr_w(V_BSTR(in)));
586
587         len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, NULL, 0, NULL, NULL);
588         stra = mshtml_alloc(len);
589         WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, stra, -1, NULL, NULL);
590
591         set_ns_fontname(This->nscontainer, stra);
592
593         mshtml_free(stra);
594
595         update_doc(This, UPDATE_UI);
596     }
597
598     if(out) {
599         nsICommandParams *nsparam;
600         LPWSTR strw;
601         char *stra;
602         DWORD len;
603         nsresult nsres;
604
605         nsparam = create_nscommand_params();
606
607         nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
608         if(NS_FAILED(nsres))
609             return S_OK;
610
611         nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
612         nsICommandParams_Release(nsparam);
613
614         len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
615         strw = mshtml_alloc(len*sizeof(WCHAR));
616         MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, -1);
617         nsfree(stra);
618
619         V_VT(out) = VT_BSTR;
620         V_BSTR(out) = SysAllocString(strw);
621         mshtml_free(strw);
622     }
623
624     return S_OK;
625 }
626
627 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
628 {
629     TRACE("(%p)->(%p %p)\n", This, in, out);
630
631     if(in) {
632         if(V_VT(in) == VT_I4) {
633             nsICommandParams *nsparam = create_nscommand_params();
634             char color_str[10];
635
636             sprintf(color_str, "#%02x%02x%02x",
637                     V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
638
639             nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
640             do_ns_command(This->nscontainer, NSCMD_FONTCOLOR, nsparam);
641
642             nsICommandParams_Release(nsparam);
643         }else {
644             FIXME("unsupported in vt=%d\n", V_VT(in));
645         }
646
647         update_doc(This, UPDATE_UI);
648     }
649
650     if(out) {
651         FIXME("unsupported out\n");
652         return E_NOTIMPL;
653     }
654
655     return S_OK;
656 }
657
658 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
659 {
660     TRACE("(%p)->(%p %p)\n", This, in, out);
661
662     if(out) {
663         WCHAR val[10] = {0};
664
665         get_font_size(This, val);
666         V_VT(out) = VT_I4;
667         V_I4(out) = strtolW(val, NULL, 10);
668     }
669
670     if(in) {
671         switch(V_VT(in)) {
672         case VT_I4: {
673             WCHAR size[10];
674             static const WCHAR format[] = {'%','d',0};
675             wsprintfW(size, format, V_I4(in));
676             set_font_size(This, size);
677             break;
678         }
679         case VT_BSTR:
680             set_font_size(This, V_BSTR(in));
681             break;
682         default:
683             FIXME("unsupported vt %d\n", V_VT(in));
684         }
685
686         update_doc(This, UPDATE_UI);
687     }
688
689     return S_OK;
690 }
691
692 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
693 {
694     TRACE("(%p)\n", This);
695
696     if(in || out)
697         FIXME("unsupported args\n");
698
699     if(This->nscontainer)
700         do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
701
702     update_doc(This, UPDATE_UI);
703     return S_OK;
704 }
705
706 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
707 {
708     TRACE("(%p)\n", This);
709
710     if(in || out)
711         FIXME("unsupported args\n");
712
713     if(This->nscontainer)
714         do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
715
716     update_doc(This, UPDATE_UI);
717     return S_OK;
718 }
719
720 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
721 {
722     switch(cmd->cmdID) {
723     case IDM_JUSTIFYCENTER:
724         TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
725         cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
726         break;
727     case IDM_JUSTIFYLEFT:
728         TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
729         /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
730         if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
731             cmd->cmdf = OLECMDF_SUPPORTED;
732         else
733             cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
734         break;
735     case IDM_JUSTIFYRIGHT:
736         TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
737         cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
738         break;
739     }
740
741     return S_OK;
742 }
743
744 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
745 {
746     TRACE("(%p)\n", This);
747
748     if(in || out)
749         FIXME("unsupported args\n");
750
751     set_ns_align(This, NSALIGN_CENTER);
752     update_doc(This, UPDATE_UI);
753     return S_OK;
754 }
755
756 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
757 {
758     TRACE("(%p)\n", This);
759
760     if(in || out)
761         FIXME("unsupported args\n");
762
763     set_ns_align(This, NSALIGN_LEFT);
764     update_doc(This, UPDATE_UI);
765     return S_OK;
766 }
767
768 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
769 {
770     TRACE("(%p)\n", This);
771
772     if(in || out)
773         FIXME("unsupported args\n");
774
775     set_ns_align(This, NSALIGN_RIGHT);
776     update_doc(This, UPDATE_UI);
777     return S_OK;
778 }
779
780 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
781 {
782     TRACE("(%p)\n", This);
783
784     if(in || out)
785         FIXME("unsupported args\n");
786
787     if(This->nscontainer)
788         do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
789
790     update_doc(This, UPDATE_UI);
791     return S_OK;
792 }
793
794 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
795 {
796     TRACE("(%p)\n", This);
797
798     if(in || out)
799         FIXME("unsupported args\n");
800
801     if(This->nscontainer)
802         do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
803
804     update_doc(This, UPDATE_UI);
805     return S_OK;
806 }
807
808 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
809 {
810     TRACE("(%p)\n", This);
811
812     if(in || out)
813         FIXME("unsupported args\n");
814
815     if(This->nscontainer)
816         do_ns_command(This->nscontainer, NSCMD_OL, NULL);
817
818     update_doc(This, UPDATE_UI);
819     return S_OK;
820 }
821
822 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
823 {
824     TRACE("(%p)\n", This);
825
826     if(in || out)
827         FIXME("unsupported args\n");
828
829     if(This->nscontainer)
830         do_ns_command(This->nscontainer, NSCMD_UL, NULL);
831
832     update_doc(This, UPDATE_UI);
833     return S_OK;
834 }
835
836 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
837 {
838     TRACE("(%p)\n", This);
839
840     if(in || out)
841         FIXME("unsupported args\n");
842
843     if(This->nscontainer)
844         do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
845
846     update_doc(This, UPDATE_UI);
847     return S_OK;
848 }
849
850 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
851 {
852     TRACE("(%p)\n", This);
853
854     if(in || out)
855         FIXME("unsupported args\n");
856
857     if(This->nscontainer)
858         do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
859
860     update_doc(This, UPDATE_UI);
861     return S_OK;
862 }
863
864 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
865 {
866     WCHAR *ptr;
867
868     if(out || !in || V_VT(in) != VT_BSTR) {
869         WARN("invalid arg\n");
870         return E_INVALIDARG;
871     }
872
873     TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
874
875     update_doc(This, UPDATE_UI);
876
877     ptr = V_BSTR(in);
878     if(*ptr == '1')
879         exec_bold(This, cmdexecopt, NULL, NULL);
880     ptr = strchrW(ptr, ',');
881     if(!ptr)
882         return S_OK;
883
884     if(*++ptr == '1')
885         exec_italic(This, cmdexecopt, NULL, NULL);
886     ptr = strchrW(ptr, ',');
887     if(!ptr)
888         return S_OK;
889
890     if(*++ptr == '1')
891         exec_underline(This, cmdexecopt, NULL, NULL);
892     ptr = strchrW(ptr, ',');
893     if(!ptr)
894         return S_OK;
895
896     if(isdigitW(*++ptr)) {
897         VARIANT v;
898
899         V_VT(&v) = VT_I4;
900         V_I4(&v) = *ptr-'0';
901
902         exec_fontsize(This, cmdexecopt, &v, NULL);
903     }
904     ptr = strchrW(ptr, ',');
905     if(!ptr)
906         return S_OK;
907
908     if(*++ptr != ',')
909         FIXME("set font color\n");
910     ptr = strchrW(ptr, ',');
911     if(!ptr)
912         return S_OK;
913
914     if(*++ptr != ',')
915         FIXME("set background color\n");
916     ptr = strchrW(ptr, ',');
917     if(!ptr)
918         return S_OK;
919
920     ptr++;
921     if(*ptr) {
922         VARIANT v;
923
924         V_VT(&v) = VT_BSTR;
925         V_BSTR(&v) = SysAllocString(ptr);
926
927         exec_fontname(This, cmdexecopt, &v, NULL);
928
929         SysFreeString(V_BSTR(&v));
930     }
931
932     return S_OK;
933 }
934
935 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
936 {
937     update_doc(This, UPDATE_UI);
938
939     if(!This->nscontainer)
940         return E_FAIL;
941
942     do_ns_editor_command(This->nscontainer, NSCMD_COPY);
943     return S_OK;
944 }
945
946 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
947 {
948     update_doc(This, UPDATE_UI);
949
950     if(!This->nscontainer)
951         return E_FAIL;
952
953     do_ns_editor_command(This->nscontainer, NSCMD_CUT);
954     return S_OK;
955 }
956
957 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
958 {
959     update_doc(This, UPDATE_UI);
960
961     if(!This->nscontainer)
962         return E_FAIL;
963
964     do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
965     return S_OK;
966 }
967
968 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
969 {
970     switch(cmd->cmdID) {
971     case IDM_FONTNAME:
972         TRACE("CGID_MSHTML: IDM_FONTNAME\n");
973         cmd->cmdf = query_ns_edit_status(This, NULL);
974         break;
975     case IDM_FONTSIZE:
976         TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
977         cmd->cmdf = query_ns_edit_status(This, NULL);
978         break;
979     case IDM_BOLD:
980         TRACE("CGID_MSHTML: IDM_BOLD\n");
981         cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
982         break;
983     case IDM_FORECOLOR:
984         TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
985         cmd->cmdf = query_ns_edit_status(This, NULL);
986         break;
987     case IDM_ITALIC:
988         TRACE("CGID_MSHTML: IDM_ITALIC\n");
989         cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
990         break;
991     case IDM_UNDERLINE:
992         TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
993         cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
994         break;
995     case IDM_HORIZONTALLINE:
996         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
997         cmd->cmdf = query_ns_edit_status(This, NULL);
998         break;
999     case IDM_ORDERLIST:
1000         TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1001         cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1002         break;
1003     case IDM_UNORDERLIST:
1004         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1005         cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1006         break;
1007     case IDM_INDENT:
1008         TRACE("CGID_MSHTML: IDM_INDENT\n");
1009         cmd->cmdf = query_ns_edit_status(This, NULL);
1010         break;
1011     case IDM_OUTDENT:
1012         TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1013         cmd->cmdf = query_ns_edit_status(This, NULL);
1014         break;
1015     }
1016
1017     return S_OK;
1018 }
1019
1020 const cmdtable_t editmode_cmds[] = {
1021     {IDM_FONTNAME,        query_edit_status,    exec_fontname},
1022     {IDM_FONTSIZE,        query_edit_status,    exec_fontsize},
1023     {IDM_FORECOLOR,       query_edit_status,    exec_forecolor},
1024     {IDM_BOLD,            query_edit_status,    exec_bold},
1025     {IDM_ITALIC,          query_edit_status,    exec_italic},
1026     {IDM_JUSTIFYCENTER,   query_justify,        exec_justifycenter},
1027     {IDM_JUSTIFYRIGHT,    query_justify,        exec_justifyright},
1028     {IDM_JUSTIFYLEFT,     query_justify,        exec_justifyleft},
1029     {IDM_UNDERLINE,       query_edit_status,    exec_underline},
1030     {IDM_HORIZONTALLINE,  query_edit_status,    exec_horizontalline},
1031     {IDM_ORDERLIST,       query_edit_status,    exec_orderlist},
1032     {IDM_UNORDERLIST,     query_edit_status,    exec_unorderlist},
1033     {IDM_INDENT,          query_edit_status,    exec_indent},
1034     {IDM_OUTDENT,         query_edit_status,    exec_outdent},
1035     {IDM_COMPOSESETTINGS, NULL,                 exec_composesettings},
1036     {0,NULL,NULL}
1037 };
1038
1039 void init_editor(HTMLDocument *This)
1040 {
1041     update_doc(This, UPDATE_UI);
1042
1043     if(!This->nscontainer)
1044         return;
1045
1046     set_ns_fontname(This->nscontainer, "Times New Roman");
1047 }