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