gdi32: Slightly simplify result handling in PolyDraw.
[wine.git] / dlls / mshtml / editor.c
blob2a02567352237dfde75ea50534e25eedda8eed38
1 /*
2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31 #include "mshtmcid.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "mshtml_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
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"
73 #define NSSTATE_ATTRIBUTE "state_attribute"
74 #define NSSTATE_ALL "state_all"
76 #define NSALIGN_CENTER "center"
77 #define NSALIGN_LEFT "left"
78 #define NSALIGN_RIGHT "right"
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
86 static const WCHAR wszFont[] = {'f','o','n','t',0};
87 static const WCHAR wszSize[] = {'s','i','z','e',0};
89 static void do_ns_command(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
91 nsICommandManager *cmdmgr;
92 nsIInterfaceRequestor *iface_req;
93 nsresult nsres;
95 TRACE("(%p)\n", This);
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;
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;
112 nsres = nsICommandManager_DoCommand(cmdmgr, cmd, nsparam, NULL);
113 if(NS_FAILED(nsres))
114 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
116 nsICommandManager_Release(cmdmgr);
119 static void do_ns_editor_command(NSContainer *This, const char *cmd)
121 nsresult nsres;
123 if(!This->editor_controller)
124 return;
126 nsres = nsIController_DoCommand(This->editor_controller, cmd);
127 if(NS_FAILED(nsres))
128 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
131 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
133 nsICommandManager *cmdmgr;
134 nsIInterfaceRequestor *iface_req;
135 nsresult nsres;
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;
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;
152 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, NULL, nsparam);
153 if(NS_FAILED(nsres))
154 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
156 nsICommandManager_Release(cmdmgr);
157 return nsres;
160 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
162 nsICommandParams *nsparam;
163 PRBool b = FALSE;
165 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
166 return OLECMDF_SUPPORTED;
168 if(This->nscontainer && nscmd) {
169 nsparam = create_nscommand_params();
170 get_ns_command_state(This->nscontainer, nscmd, nsparam);
172 nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
174 nsICommandParams_Release(nsparam);
177 return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
180 static void set_ns_align(HTMLDocument *This, const char *align_str)
182 nsICommandParams *nsparam;
184 if(!This->nscontainer)
185 return;
187 nsparam = create_nscommand_params();
188 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
190 do_ns_command(This->nscontainer, NSCMD_ALIGN, nsparam);
192 nsICommandParams_Release(nsparam);
195 static DWORD query_align_status(HTMLDocument *This, const char *align_str)
197 nsICommandParams *nsparam;
198 char *align = NULL;
200 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
201 return OLECMDF_SUPPORTED;
203 if(This->nscontainer) {
204 nsparam = create_nscommand_params();
205 get_ns_command_state(This->nscontainer, NSCMD_ALIGN, nsparam);
207 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &align);
209 nsICommandParams_Release(nsparam);
212 return OLECMDF_SUPPORTED | OLECMDF_ENABLED
213 | (align && !strcmp(align_str, align) ? OLECMDF_LATCHED : 0);
217 static nsISelection *get_ns_selection(HTMLDocument *This)
219 nsIDOMWindow *dom_window;
220 nsISelection *nsselection = NULL;
221 nsresult nsres;
223 if(!This->nscontainer)
224 return NULL;
226 nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
227 if(NS_FAILED(nsres))
228 return NULL;
230 nsIDOMWindow_GetSelection(dom_window, &nsselection);
231 nsIDOMWindow_Release(dom_window);
233 return nsselection;
237 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
239 PRBool has_children;
240 PRUint32 child_cnt, i;
241 nsIDOMNode *child_node;
242 nsIDOMNodeList *node_list;
243 PRUint16 node_type;
245 nsIDOMElement_HasChildNodes(elem, &has_children);
246 if(!has_children)
247 return;
249 nsIDOMElement_GetChildNodes(elem, &node_list);
250 nsIDOMNodeList_GetLength(node_list, &child_cnt);
252 for(i=0; i<child_cnt; i++) {
253 nsIDOMNodeList_Item(node_list, i, &child_node);
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;
261 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
263 nsAString_Init(&tag_str, NULL);
264 nsIDOMElement_GetTagName(child_elem, &tag_str);
265 nsAString_GetData(&tag_str, &ctag, NULL);
267 if(!strcmpiW(ctag, tag))
268 /* FIXME: remove node if there are no more attributes */
269 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
271 nsAString_Finish(&tag_str);
273 remove_child_attr(child_elem, tag, attr_str);
275 nsIDOMNode_Release(child_elem);
278 nsIDOMNode_Release(child_node);
281 nsIDOMNodeList_Release(node_list);
284 static void get_font_size(HTMLDocument *This, WCHAR *ret)
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;
294 *ret = 0;
296 if(!nsselection)
297 return;
299 nsISelection_GetFocusNode(nsselection, &node);
300 nsISelection_Release(nsselection);
302 while(node) {
303 nsres = nsIDOMNode_GetNodeType(node, &node_type);
304 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
305 break;
307 if(node_type == ELEMENT_NODE) {
308 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
310 nsAString_Init(&tag_str, NULL);
311 nsIDOMElement_GetTagName(elem, &tag_str);
312 nsAString_GetData(&tag_str, &tag, NULL);
314 if(!strcmpiW(tag, wszFont)) {
315 nsAString size_str, val_str;
316 LPCWSTR val;
318 TRACE("found font tag %p\n", elem);
320 nsAString_Init(&size_str, wszSize);
321 nsAString_Init(&val_str, NULL);
323 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
324 nsAString_GetData(&val_str, &val, NULL);
326 if(*val) {
327 TRACE("found size %s\n", debugstr_w(val));
328 strcpyW(ret, val);
331 nsAString_Finish(&size_str);
332 nsAString_Finish(&val_str);
335 nsAString_Finish(&tag_str);
337 nsIDOMElement_Release(elem);
340 if(*ret)
341 break;
343 tmp_node = node;
344 nsIDOMNode_GetParentNode(node, &node);
345 nsIDOMNode_Release(tmp_node);
348 if(node)
349 nsIDOMNode_Release(node);
352 static void set_font_size(HTMLDocument *This, LPCWSTR size)
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;
365 nsselection = get_ns_selection(This);
367 if(!nsselection)
368 return;
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;
379 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
380 if(NS_FAILED(nsres))
381 return;
383 nsAString_Init(&font_str, wszFont);
384 nsAString_Init(&size_str, wszSize);
385 nsAString_Init(&val_str, size);
387 nsIDOMDocument_CreateElement(nsdoc, &font_str, &elem);
388 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
390 nsISelection_GetRangeAt(nsselection, 0, &range);
391 nsISelection_GetIsCollapsed(nsselection, &collapsed);
392 nsISelection_RemoveAllRanges(nsselection);
394 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
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);
404 nsIDOMRange_Release(range);
405 nsIDOMElement_Release(elem);
407 nsAString_Finish(&font_str);
408 nsAString_Finish(&size_str);
409 nsAString_Finish(&val_str);
411 nsISelection_Release(nsselection);
412 nsIDOMDocument_Release(nsdoc);
415 static BOOL is_visible_text_node(nsIDOMNode *node)
417 nsIDOMCharacterData *char_data;
418 nsAString data_str;
419 LPCWSTR data, ptr;
420 PRUint32 len;
422 nsIDOMNode_QueryInterface(node, &IID_nsIDOMCharacterData, (void**)&char_data);
424 nsIDOMCharacterData_GetLength(char_data, &len);
426 nsAString_Init(&data_str, NULL);
427 nsIDOMCharacterData_GetData(char_data, &data_str);
428 nsAString_GetData(&data_str, &data, NULL);
430 if(*data == '\n') {
431 len--;
432 for(ptr=data+1; ptr && isspaceW(*ptr); ptr++)
433 len--;
436 nsAString_Finish(&data_str);
438 nsIDOMCharacterData_Release(char_data);
440 return len != 0;
443 static nsIDOMNode *get_child_text_node(nsIDOMNode *node, BOOL first)
445 nsIDOMNode *iter, *iter2;
447 if(first)
448 nsIDOMNode_GetFirstChild(node, &iter);
449 else
450 nsIDOMNode_GetLastChild(node, &iter);
452 while(iter) {
453 PRUint16 node_type;
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;
468 if(first)
469 nsIDOMNode_GetNextSibling(iter, &iter2);
470 else
471 nsIDOMNode_GetPreviousSibling(iter, &iter2);
473 nsIDOMNode_Release(iter);
474 iter = iter2;
477 return NULL;
480 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char **cmds)
482 int i=0;
483 PRBool b;
485 nsIDOMKeyEvent_GetCtrlKey(event, &b);
486 if(b)
487 i |= 1;
489 nsIDOMKeyEvent_GetShiftKey(event, &b);
490 if(b)
491 i |= 2;
493 if(cmds[i])
494 do_ns_editor_command(This->nscontainer, cmds[i]);
496 nsIDOMKeyEvent_PreventDefault(event);
499 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
501 nsIDOMKeyEvent *key_event;
502 PRUint32 code;
504 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
506 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
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
517 TRACE("left\n");
518 handle_arrow_key(This, key_event, cmds);
519 break;
521 case DOM_VK_RIGHT: {
522 static const char *cmds[] = {
523 NSCMD_CHARNEXT,
524 NSCMD_WORDNEXT,
525 NSCMD_SELECTCHARNEXT,
526 NSCMD_SELECTWORDNEXT
529 TRACE("right\n");
530 handle_arrow_key(This, key_event, cmds);
531 break;
533 case DOM_VK_UP: {
534 static const char *cmds[] = {
535 NSCMD_LINEPREVIOUS,
536 NSCMD_MOVEPAGEUP,
537 NSCMD_SELECTLINEPREVIOUS,
538 NSCMD_SELECTPAGEUP
541 TRACE("up\n");
542 handle_arrow_key(This, key_event, cmds);
543 break;
545 case DOM_VK_DOWN: {
546 static const char *cmds[] = {
547 NSCMD_LINENEXT,
548 NSCMD_MOVEPAGEDOWN,
549 NSCMD_SELECTLINENEXT,
550 NSCMD_SELECTPAGEDOWN
553 TRACE("down\n");
554 handle_arrow_key(This, key_event, cmds);
555 break;
557 case DOM_VK_DELETE: {
558 static const char *cmds[] = {
559 NSCMD_DELETECHARFORWARD,
560 NSCMD_DELETEWORDFORWARD,
561 NULL, NULL
564 TRACE("delete\n");
565 handle_arrow_key(This, key_event, cmds);
566 break;
570 nsIDOMKeyEvent_Release(key_event);
573 static void set_ns_fontname(NSContainer *This, const char *fontname)
575 nsICommandParams *nsparam = create_nscommand_params();
577 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
578 do_ns_command(This, NSCMD_FONTFACE, nsparam);
579 nsICommandParams_Release(nsparam);
582 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
584 TRACE("(%p)->(%p %p)\n", This, in, out);
586 if(This->nscontainer)
587 do_ns_editor_command(This->nscontainer, NSCMD_DELETECHARFORWARD);
589 update_doc(This, UPDATE_UI);
590 return S_OK;
593 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
595 TRACE("(%p)->(%p %p)\n", This, in, out);
597 if(!This->nscontainer) {
598 update_doc(This, UPDATE_UI);
599 return E_FAIL;
602 if(in) {
603 char *stra;
604 DWORD len;
606 if(V_VT(in) != VT_BSTR) {
607 FIXME("Unsupported vt=%d\n", V_VT(out));
608 return E_INVALIDARG;
611 TRACE("%s\n", debugstr_w(V_BSTR(in)));
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);
617 set_ns_fontname(This->nscontainer, stra);
619 mshtml_free(stra);
621 update_doc(This, UPDATE_UI);
624 if(out) {
625 nsICommandParams *nsparam;
626 LPWSTR strw;
627 char *stra;
628 DWORD len;
629 nsresult nsres;
631 nsparam = create_nscommand_params();
633 nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
634 if(NS_FAILED(nsres))
635 return S_OK;
637 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
638 nsICommandParams_Release(nsparam);
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);
645 V_VT(out) = VT_BSTR;
646 V_BSTR(out) = SysAllocString(strw);
647 mshtml_free(strw);
650 return S_OK;
653 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
655 TRACE("(%p)->(%p %p)\n", This, in, out);
657 if(in) {
658 if(V_VT(in) == VT_I4) {
659 nsICommandParams *nsparam = create_nscommand_params();
660 char color_str[10];
662 sprintf(color_str, "#%02x%02x%02x",
663 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
665 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
666 do_ns_command(This->nscontainer, NSCMD_FONTCOLOR, nsparam);
668 nsICommandParams_Release(nsparam);
669 }else {
670 FIXME("unsupported in vt=%d\n", V_VT(in));
673 update_doc(This, UPDATE_UI);
676 if(out) {
677 FIXME("unsupported out\n");
678 return E_NOTIMPL;
681 return S_OK;
684 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
686 TRACE("(%p)->(%p %p)\n", This, in, out);
688 if(out) {
689 WCHAR val[10] = {0};
691 get_font_size(This, val);
692 V_VT(out) = VT_I4;
693 V_I4(out) = strtolW(val, NULL, 10);
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;
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));
712 update_doc(This, UPDATE_UI);
715 return S_OK;
718 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
720 TRACE("(%p)\n", This);
722 if(in || out)
723 FIXME("unsupported args\n");
725 if(This->nscontainer)
726 do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
728 update_doc(This, UPDATE_UI);
729 return S_OK;
732 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
734 TRACE("(%p)\n", This);
736 if(in || out)
737 FIXME("unsupported args\n");
739 if(This->nscontainer)
740 do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
742 update_doc(This, UPDATE_UI);
743 return S_OK;
746 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
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;
767 return S_OK;
770 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
772 TRACE("(%p)\n", This);
774 if(in || out)
775 FIXME("unsupported args\n");
777 set_ns_align(This, NSALIGN_CENTER);
778 update_doc(This, UPDATE_UI);
779 return S_OK;
782 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
784 TRACE("(%p)\n", This);
786 if(in || out)
787 FIXME("unsupported args\n");
789 set_ns_align(This, NSALIGN_LEFT);
790 update_doc(This, UPDATE_UI);
791 return S_OK;
794 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
796 TRACE("(%p)\n", This);
798 if(in || out)
799 FIXME("unsupported args\n");
801 set_ns_align(This, NSALIGN_RIGHT);
802 update_doc(This, UPDATE_UI);
803 return S_OK;
806 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
808 TRACE("(%p)\n", This);
810 if(in || out)
811 FIXME("unsupported args\n");
813 if(This->nscontainer)
814 do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
816 update_doc(This, UPDATE_UI);
817 return S_OK;
820 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
822 TRACE("(%p)\n", This);
824 if(in || out)
825 FIXME("unsupported args\n");
827 if(This->nscontainer)
828 do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
830 update_doc(This, UPDATE_UI);
831 return S_OK;
834 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
836 TRACE("(%p)\n", This);
838 if(in || out)
839 FIXME("unsupported args\n");
841 if(This->nscontainer)
842 do_ns_command(This->nscontainer, NSCMD_OL, NULL);
844 update_doc(This, UPDATE_UI);
845 return S_OK;
848 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
850 TRACE("(%p)\n", This);
852 if(in || out)
853 FIXME("unsupported args\n");
855 if(This->nscontainer)
856 do_ns_command(This->nscontainer, NSCMD_UL, NULL);
858 update_doc(This, UPDATE_UI);
859 return S_OK;
862 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
864 TRACE("(%p)\n", This);
866 if(in || out)
867 FIXME("unsupported args\n");
869 if(This->nscontainer)
870 do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
872 update_doc(This, UPDATE_UI);
873 return S_OK;
876 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
878 TRACE("(%p)\n", This);
880 if(in || out)
881 FIXME("unsupported args\n");
883 if(This->nscontainer)
884 do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
886 update_doc(This, UPDATE_UI);
887 return S_OK;
890 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
892 WCHAR *ptr;
894 if(out || !in || V_VT(in) != VT_BSTR) {
895 WARN("invalid arg\n");
896 return E_INVALIDARG;
899 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
901 update_doc(This, UPDATE_UI);
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;
910 if(*++ptr == '1')
911 exec_italic(This, cmdexecopt, NULL, NULL);
912 ptr = strchrW(ptr, ',');
913 if(!ptr)
914 return S_OK;
916 if(*++ptr == '1')
917 exec_underline(This, cmdexecopt, NULL, NULL);
918 ptr = strchrW(ptr, ',');
919 if(!ptr)
920 return S_OK;
922 if(isdigitW(*++ptr)) {
923 VARIANT v;
925 V_VT(&v) = VT_I4;
926 V_I4(&v) = *ptr-'0';
928 exec_fontsize(This, cmdexecopt, &v, NULL);
930 ptr = strchrW(ptr, ',');
931 if(!ptr)
932 return S_OK;
934 if(*++ptr != ',')
935 FIXME("set font color\n");
936 ptr = strchrW(ptr, ',');
937 if(!ptr)
938 return S_OK;
940 if(*++ptr != ',')
941 FIXME("set background color\n");
942 ptr = strchrW(ptr, ',');
943 if(!ptr)
944 return S_OK;
946 ptr++;
947 if(*ptr) {
948 VARIANT v;
950 V_VT(&v) = VT_BSTR;
951 V_BSTR(&v) = SysAllocString(ptr);
953 exec_fontname(This, cmdexecopt, &v, NULL);
955 SysFreeString(V_BSTR(&v));
958 return S_OK;
961 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
963 update_doc(This, UPDATE_UI);
965 if(!This->nscontainer)
966 return E_FAIL;
968 do_ns_editor_command(This->nscontainer, NSCMD_COPY);
969 return S_OK;
972 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
974 update_doc(This, UPDATE_UI);
976 if(!This->nscontainer)
977 return E_FAIL;
979 do_ns_editor_command(This->nscontainer, NSCMD_CUT);
980 return S_OK;
983 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
985 update_doc(This, UPDATE_UI);
987 if(!This->nscontainer)
988 return E_FAIL;
990 do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
991 return S_OK;
994 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
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;
1047 return S_OK;
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}
1070 void init_editor(HTMLDocument *This)
1072 update_doc(This, UPDATE_UI);
1074 if(!This->nscontainer)
1075 return;
1077 set_ns_fontname(This->nscontainer, "Times New Roman");