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
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 #define NSCMD_ALIGN "cmd_align"
38 #define NSCMD_BEGINLINE "cmd_beginLine"
39 #define NSCMD_BOLD "cmd_bold"
40 #define NSCMD_CHARNEXT "cmd_charNext"
41 #define NSCMD_CHARPREVIOUS "cmd_charPrevious"
42 #define NSCMD_COPY "cmd_copy"
43 #define NSCMD_CUT "cmd_cut"
44 #define NSCMD_DELETECHARFORWARD "cmd_deleteCharForward"
45 #define NSCMD_DELETEWORDFORWARD "cmd_deleteWordForward"
46 #define NSCMD_ENDLINE "cmd_endLine"
47 #define NSCMD_FONTCOLOR "cmd_fontColor"
48 #define NSCMD_FONTFACE "cmd_fontFace"
49 #define NSCMD_INDENT "cmd_indent"
50 #define NSCMD_INSERTHR "cmd_insertHR"
51 #define NSCMD_INSERTLINKNOUI "cmd_insertLinkNoUI"
52 #define NSCMD_ITALIC "cmd_italic"
53 #define NSCMD_LINENEXT "cmd_lineNext"
54 #define NSCMD_LINEPREVIOUS "cmd_linePrevious"
55 #define NSCMD_MOVEBOTTOM "cmd_moveBottom"
56 #define NSCMD_MOVEPAGEDOWN "cmd_movePageDown"
57 #define NSCMD_MOVEPAGEUP "cmd_movePageUp"
58 #define NSCMD_MOVETOP "cmd_moveTop"
59 #define NSCMD_OL "cmd_ol"
60 #define NSCMD_OUTDENT "cmd_outdent"
61 #define NSCMD_PASTE "cmd_paste"
62 #define NSCMD_SELECTALL "cmd_selectAll"
63 #define NSCMD_SELECTBEGINLINE "cmd_selectBeginLine"
64 #define NSCMD_SELECTBOTTOM "cmd_selectBottom"
65 #define NSCMD_SELECTCHARNEXT "cmd_selectCharNext"
66 #define NSCMD_SELECTCHARPREVIOUS "cmd_selectCharPrevious"
67 #define NSCMD_SELECTENDLINE "cmd_selectEndLine"
68 #define NSCMD_SELECTLINENEXT "cmd_selectLineNext"
69 #define NSCMD_SELECTLINEPREVIOUS "cmd_selectLinePrevious"
70 #define NSCMD_SELECTPAGEDOWN "cmd_selectPageDown"
71 #define NSCMD_SELECTPAGEUP "cmd_selectPageUp"
72 #define NSCMD_SELECTTOP "cmd_selectTop"
73 #define NSCMD_SELECTWORDNEXT "cmd_selectWordNext"
74 #define NSCMD_SELECTWORDPREVIOUS "cmd_selectWordPrevious"
75 #define NSCMD_UL "cmd_ul"
76 #define NSCMD_UNDERLINE "cmd_underline"
77 #define NSCMD_WORDNEXT "cmd_wordNext"
78 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
80 #define NSSTATE_ATTRIBUTE "state_attribute"
81 #define NSSTATE_ALL "state_all"
83 #define NSALIGN_CENTER "center"
84 #define NSALIGN_LEFT "left"
85 #define NSALIGN_RIGHT "right"
87 #define DOM_VK_LEFT VK_LEFT
88 #define DOM_VK_UP VK_UP
89 #define DOM_VK_RIGHT VK_RIGHT
90 #define DOM_VK_DOWN VK_DOWN
91 #define DOM_VK_DELETE VK_DELETE
92 #define DOM_VK_HOME VK_HOME
93 #define DOM_VK_END VK_END
95 static const WCHAR fontW[] = {'f','o','n','t',0};
96 static const WCHAR sizeW[] = {'s','i','z','e',0};
98 void set_dirty(HTMLDocument *This, VARIANT_BOOL dirty)
102 if(This->doc_obj->usermode != EDITMODE || !This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
106 nsres = nsIEditor_IncrementModificationCount(This->doc_obj->nscontainer->editor, 1);
108 ERR("IncrementModificationCount failed: %08x\n", nsres);
110 nsres = nsIEditor_ResetModificationCount(This->doc_obj->nscontainer->editor);
112 ERR("ResetModificationCount failed: %08x\n", nsres);
116 static void do_ns_editor_command(NSContainer *This, const char *cmd)
120 if(!This->editor_controller)
123 nsres = nsIController_DoCommand(This->editor_controller, cmd);
125 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
128 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
130 nsICommandManager *cmdmgr;
133 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsICommandManager, (void**)&cmdmgr);
134 if(NS_FAILED(nsres)) {
135 ERR("Could not get nsICommandManager: %08x\n", nsres);
139 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, This->doc->basedoc.window->nswindow, nsparam);
141 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
143 nsICommandManager_Release(cmdmgr);
147 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
149 nsICommandParams *nsparam;
152 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
153 return OLECMDF_SUPPORTED;
155 if(This->doc_obj->nscontainer && nscmd) {
156 nsparam = create_nscommand_params();
157 get_ns_command_state(This->doc_obj->nscontainer, nscmd, nsparam);
159 nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
161 nsICommandParams_Release(nsparam);
164 return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
167 static void set_ns_align(HTMLDocument *This, const char *align_str)
169 nsICommandParams *nsparam;
171 if(!This->doc_obj->nscontainer)
174 nsparam = create_nscommand_params();
175 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
177 do_ns_command(This, NSCMD_ALIGN, nsparam);
179 nsICommandParams_Release(nsparam);
182 static DWORD query_align_status(HTMLDocument *This, const WCHAR *align)
184 DWORD ret = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
185 nsIDOMNSHTMLDocument *nsdoc;
186 nsAString justify_str;
190 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
191 return OLECMDF_SUPPORTED;
194 nsres = nsIDOMHTMLDocument_QueryInterface(This->doc_node->nsdoc, &IID_nsIDOMNSHTMLDocument,
196 if(NS_FAILED(nsres)) {
197 ERR("Could not get nsIDOMNSHTMLDocument iface: %08x\n", nsres);
201 nsAString_Init(&justify_str, align);
202 nsres = nsIDOMNSHTMLDocument_QueryCommandState(nsdoc, &justify_str, &b);
203 nsAString_Finish(&justify_str);
204 if(NS_SUCCEEDED(nsres) && b)
205 ret |= OLECMDF_LATCHED;
207 nsIDOMNSHTMLDocument_Release(nsdoc);
212 static nsISelection *get_ns_selection(HTMLDocument *This)
214 nsISelection *nsselection = NULL;
217 nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
219 ERR("GetSelection failed %08x\n", nsres);
225 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
228 PRUint32 child_cnt, i;
229 nsIDOMNode *child_node;
230 nsIDOMNodeList *node_list;
233 nsIDOMElement_HasChildNodes(elem, &has_children);
237 nsIDOMElement_GetChildNodes(elem, &node_list);
238 nsIDOMNodeList_GetLength(node_list, &child_cnt);
240 for(i=0; i<child_cnt; i++) {
241 nsIDOMNodeList_Item(node_list, i, &child_node);
243 nsIDOMNode_GetNodeType(child_node, &node_type);
244 if(node_type == ELEMENT_NODE) {
245 nsIDOMElement *child_elem;
247 const PRUnichar *ctag;
249 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
251 nsAString_Init(&tag_str, NULL);
252 nsIDOMElement_GetTagName(child_elem, &tag_str);
253 nsAString_GetData(&tag_str, &ctag);
255 if(!strcmpiW(ctag, tag))
256 /* FIXME: remove node if there are no more attributes */
257 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
259 nsAString_Finish(&tag_str);
261 remove_child_attr(child_elem, tag, attr_str);
263 nsIDOMNode_Release(child_elem);
266 nsIDOMNode_Release(child_node);
269 nsIDOMNodeList_Release(node_list);
272 static void get_font_size(HTMLDocument *This, WCHAR *ret)
274 nsISelection *nsselection = get_ns_selection(This);
275 nsIDOMElement *elem = NULL;
276 nsIDOMNode *node = NULL, *tmp_node;
287 nsISelection_GetFocusNode(nsselection, &node);
288 nsISelection_Release(nsselection);
291 nsres = nsIDOMNode_GetNodeType(node, &node_type);
292 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
295 if(node_type == ELEMENT_NODE) {
296 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
298 nsAString_Init(&tag_str, NULL);
299 nsIDOMElement_GetTagName(elem, &tag_str);
300 nsAString_GetData(&tag_str, &tag);
302 if(!strcmpiW(tag, fontW)) {
303 nsAString size_str, val_str;
306 TRACE("found font tag %p\n", elem);
308 nsAString_InitDepend(&size_str, sizeW);
309 nsAString_Init(&val_str, NULL);
311 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
312 nsAString_GetData(&val_str, &val);
315 TRACE("found size %s\n", debugstr_w(val));
319 nsAString_Finish(&size_str);
320 nsAString_Finish(&val_str);
323 nsAString_Finish(&tag_str);
325 nsIDOMElement_Release(elem);
332 nsIDOMNode_GetParentNode(node, &node);
333 nsIDOMNode_Release(tmp_node);
337 nsIDOMNode_Release(node);
340 static void set_font_size(HTMLDocument *This, LPCWSTR size)
342 nsISelection *nsselection;
344 nsIDOMHTMLElement *elem;
346 PRInt32 range_cnt = 0;
350 if(!This->doc_node->nsdoc) {
351 WARN("NULL nsdoc\n");
355 nsselection = get_ns_selection(This);
359 nsISelection_GetRangeCount(nsselection, &range_cnt);
361 FIXME("range_cnt %d not supprted\n", range_cnt);
363 nsISelection_Release(nsselection);
368 create_nselem(This->doc_node, fontW, &elem);
370 nsAString_InitDepend(&size_str, sizeW);
371 nsAString_InitDepend(&val_str, size);
373 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
374 nsAString_Finish(&val_str);
376 nsISelection_GetRangeAt(nsselection, 0, &range);
377 nsISelection_GetIsCollapsed(nsselection, &collapsed);
378 nsISelection_RemoveAllRanges(nsselection);
380 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
383 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
385 /* Remove all size attributes from the range */
386 remove_child_attr((nsIDOMElement*)elem, fontW, &size_str);
387 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
390 nsISelection_Release(nsselection);
391 nsIDOMRange_Release(range);
392 nsIDOMElement_Release(elem);
394 nsAString_Finish(&size_str);
396 set_dirty(This, VARIANT_TRUE);
399 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
404 nsIDOMKeyEvent_GetCtrlKey(event, &b);
408 nsIDOMKeyEvent_GetShiftKey(event, &b);
413 do_ns_editor_command(This->doc_obj->nscontainer, cmds[i]);
415 nsIDOMKeyEvent_PreventDefault(event);
418 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
420 nsIDOMKeyEvent *key_event;
423 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
425 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
429 static const char * const cmds[] = {
432 NSCMD_SELECTCHARPREVIOUS,
433 NSCMD_SELECTWORDPREVIOUS
437 handle_arrow_key(This, key_event, cmds);
441 static const char * const cmds[] = {
444 NSCMD_SELECTCHARNEXT,
449 handle_arrow_key(This, key_event, cmds);
453 static const char * const cmds[] = {
456 NSCMD_SELECTLINEPREVIOUS,
461 handle_arrow_key(This, key_event, cmds);
465 static const char * const cmds[] = {
468 NSCMD_SELECTLINENEXT,
473 handle_arrow_key(This, key_event, cmds);
476 case DOM_VK_DELETE: {
477 static const char * const cmds[] = {
478 NSCMD_DELETECHARFORWARD,
479 NSCMD_DELETEWORDFORWARD,
484 handle_arrow_key(This, key_event, cmds);
488 static const char * const cmds[] = {
491 NSCMD_SELECTBEGINLINE,
496 handle_arrow_key(This, key_event, cmds);
500 static const char * const cmds[] = {
508 handle_arrow_key(This, key_event, cmds);
513 nsIDOMKeyEvent_Release(key_event);
516 void handle_edit_load(HTMLDocument *This)
518 get_editor_controller(This->doc_obj->nscontainer);
521 static void set_ns_fontname(HTMLDocument *This, const char *fontname)
523 nsICommandParams *nsparam = create_nscommand_params();
525 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
526 do_ns_command(This, NSCMD_FONTFACE, nsparam);
527 nsICommandParams_Release(nsparam);
530 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
532 TRACE("(%p)->(%p %p)\n", This, in, out);
534 if(This->doc_obj->nscontainer)
535 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_DELETECHARFORWARD);
537 update_doc(This, UPDATE_UI);
541 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
543 TRACE("(%p)->(%p %p)\n", This, in, out);
545 if(!This->doc_obj->nscontainer) {
546 update_doc(This, UPDATE_UI);
553 if(V_VT(in) != VT_BSTR) {
554 FIXME("Unsupported vt=%d\n", V_VT(out));
558 TRACE("%s\n", debugstr_w(V_BSTR(in)));
560 stra = heap_strdupWtoA(V_BSTR(in));
561 set_ns_fontname(This, stra);
564 update_doc(This, UPDATE_UI);
568 nsICommandParams *nsparam;
577 nsparam = create_nscommand_params();
579 nsres = get_ns_command_state(This->doc_obj->nscontainer, NSCMD_FONTFACE, nsparam);
583 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
584 nsICommandParams_Release(nsparam);
586 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
587 strw = heap_alloc(len*sizeof(WCHAR));
588 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, len);
591 V_BSTR(out) = SysAllocString(strw);
598 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
600 TRACE("(%p)->(%p %p)\n", This, in, out);
603 if(V_VT(in) == VT_I4) {
604 nsICommandParams *nsparam = create_nscommand_params();
607 sprintf(color_str, "#%02x%02x%02x",
608 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
610 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
611 do_ns_command(This, NSCMD_FONTCOLOR, nsparam);
613 nsICommandParams_Release(nsparam);
615 FIXME("unsupported in vt=%d\n", V_VT(in));
618 update_doc(This, UPDATE_UI);
622 FIXME("unsupported out\n");
629 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
631 TRACE("(%p)->(%p %p)\n", This, in, out);
636 get_font_size(This, val);
638 V_I4(out) = strtolW(val, NULL, 10);
645 static const WCHAR format[] = {'%','d',0};
646 wsprintfW(size, format, V_I4(in));
647 set_font_size(This, size);
651 set_font_size(This, V_BSTR(in));
654 FIXME("unsupported vt %d\n", V_VT(in));
657 update_doc(This, UPDATE_UI);
663 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
666 FIXME("(%p)->(%p %p)\n", This, in, out);
670 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
672 TRACE("(%p)\n", This);
675 FIXME("unsupported args\n");
677 if(This->doc_obj->nscontainer)
678 do_ns_command(This, NSCMD_SELECTALL, NULL);
680 update_doc(This, UPDATE_UI);
684 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
686 TRACE("(%p)\n", This);
689 FIXME("unsupported args\n");
691 if(This->doc_obj->nscontainer)
692 do_ns_command(This, NSCMD_BOLD, NULL);
694 update_doc(This, UPDATE_UI);
698 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
700 TRACE("(%p)\n", This);
703 FIXME("unsupported args\n");
705 if(This->doc_obj->nscontainer)
706 do_ns_command(This, NSCMD_ITALIC, NULL);
708 update_doc(This, UPDATE_UI);
712 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
714 static const PRUnichar justifycenterW[] = {'j','u','s','t','i','f','y','c','e','n','t','e','r',0};
715 static const PRUnichar justifyrightW[] = {'j','u','s','t','i','f','y','r','i','g','h','t',0};
718 case IDM_JUSTIFYCENTER:
719 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
720 cmd->cmdf = query_align_status(This, justifycenterW);
722 case IDM_JUSTIFYLEFT:
723 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
724 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
725 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
726 cmd->cmdf = OLECMDF_SUPPORTED;
728 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
730 case IDM_JUSTIFYRIGHT:
731 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
732 cmd->cmdf = query_align_status(This, justifyrightW);
739 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
741 TRACE("(%p)\n", This);
744 FIXME("unsupported args\n");
746 set_ns_align(This, NSALIGN_CENTER);
747 update_doc(This, UPDATE_UI);
751 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
753 TRACE("(%p)\n", This);
756 FIXME("unsupported args\n");
758 set_ns_align(This, NSALIGN_LEFT);
759 update_doc(This, UPDATE_UI);
763 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
765 TRACE("(%p)\n", This);
768 FIXME("unsupported args\n");
770 set_ns_align(This, NSALIGN_RIGHT);
771 update_doc(This, UPDATE_UI);
775 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
777 TRACE("(%p)\n", This);
780 FIXME("unsupported args\n");
782 do_ns_command(This, NSCMD_UNDERLINE, NULL);
783 update_doc(This, UPDATE_UI);
787 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
789 TRACE("(%p)\n", This);
792 FIXME("unsupported args\n");
794 do_ns_command(This, NSCMD_INSERTHR, NULL);
795 update_doc(This, UPDATE_UI);
799 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
801 TRACE("(%p)\n", This);
804 FIXME("unsupported args\n");
806 do_ns_command(This, NSCMD_OL, NULL);
807 update_doc(This, UPDATE_UI);
811 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
813 TRACE("(%p)\n", This);
816 FIXME("unsupported args\n");
818 do_ns_command(This, NSCMD_UL, NULL);
819 update_doc(This, UPDATE_UI);
823 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
825 TRACE("(%p)\n", This);
828 FIXME("unsupported args\n");
830 do_ns_command(This, NSCMD_INDENT, NULL);
831 update_doc(This, UPDATE_UI);
835 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
837 TRACE("(%p)\n", This);
840 FIXME("unsupported args\n");
842 do_ns_command(This, NSCMD_OUTDENT, NULL);
843 update_doc(This, UPDATE_UI);
847 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
851 if(out || !in || V_VT(in) != VT_BSTR) {
852 WARN("invalid arg\n");
856 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
858 update_doc(This, UPDATE_UI);
862 exec_bold(This, cmdexecopt, NULL, NULL);
863 ptr = strchrW(ptr, ',');
868 exec_italic(This, cmdexecopt, NULL, NULL);
869 ptr = strchrW(ptr, ',');
874 exec_underline(This, cmdexecopt, NULL, NULL);
875 ptr = strchrW(ptr, ',');
879 if(isdigitW(*++ptr)) {
885 exec_fontsize(This, cmdexecopt, &v, NULL);
887 ptr = strchrW(ptr, ',');
892 FIXME("set font color\n");
893 ptr = strchrW(ptr, ',');
898 FIXME("set background color\n");
899 ptr = strchrW(ptr, ',');
908 V_BSTR(&v) = SysAllocString(ptr);
910 exec_fontname(This, cmdexecopt, &v, NULL);
912 SysFreeString(V_BSTR(&v));
918 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
920 update_doc(This, UPDATE_UI);
922 if(!This->doc_obj->nscontainer)
925 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_COPY);
929 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
931 update_doc(This, UPDATE_UI);
933 if(!This->doc_obj->nscontainer)
936 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_CUT);
940 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
942 update_doc(This, UPDATE_UI);
944 if(!This->doc_obj->nscontainer)
947 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_PASTE);
951 static HRESULT exec_setdirty(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
953 TRACE("(%p)->(%08x %p %p)\n", This, cmdexecopt, in, out);
958 if(V_VT(in) == VT_BOOL)
959 set_dirty(This, V_BOOL(in));
961 FIXME("unsupported vt=%d\n", V_VT(in));
966 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
970 TRACE("CGID_MSHTML: IDM_DELETE\n");
971 cmd->cmdf = query_ns_edit_status(This, NULL);
974 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
975 cmd->cmdf = query_ns_edit_status(This, NULL);
978 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
979 cmd->cmdf = query_ns_edit_status(This, NULL);
982 TRACE("CGID_MSHTML: IDM_BOLD\n");
983 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
986 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
987 cmd->cmdf = query_ns_edit_status(This, NULL);
990 TRACE("CGID_MSHTML: IDM_ITALIC\n");
991 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
994 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
995 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
997 case IDM_HORIZONTALLINE:
998 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
999 cmd->cmdf = query_ns_edit_status(This, NULL);
1002 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1003 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1005 case IDM_UNORDERLIST:
1006 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1007 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1010 TRACE("CGID_MSHTML: IDM_INDENT\n");
1011 cmd->cmdf = query_ns_edit_status(This, NULL);
1014 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1015 cmd->cmdf = query_ns_edit_status(This, NULL);
1018 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1019 cmd->cmdf = query_ns_edit_status(This, NULL);
1026 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1028 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1034 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1035 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1036 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1037 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1038 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1039 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1041 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1042 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1045 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1047 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1048 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1049 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1050 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1051 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1052 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1053 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1054 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1056 /* force the updating of the URL edit box */
1057 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1060 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1061 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1068 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1069 EndDialog(hwnd, wparam);
1071 case MAKEWPARAM(IDOK, BN_CLICKED):
1073 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1074 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1075 INT len = GetWindowTextLengthW(hwndURL);
1076 *url = SysAllocStringLen(NULL, len + 1);
1077 GetWindowTextW(hwndURL, *url, len + 1);
1078 EndDialog(hwnd, wparam);
1081 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1083 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1089 static const WCHAR wszSlashSlash[] = {'/','/'};
1091 /* get string of currently selected hyperlink type */
1092 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1093 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1094 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1095 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1097 if (!strcmpW(type, wszOther))
1100 /* get current URL */
1101 len = GetWindowTextLengthW(hwndURL);
1102 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1103 GetWindowTextW(hwndURL, url, len + 1);
1105 /* strip off old protocol */
1106 p = strchrW(url, ':');
1107 if (p && p[1] == '/' && p[2] == '/')
1110 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1112 /* add new protocol */
1115 memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1116 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1119 SetWindowTextW(hwndURL, url);
1121 HeapFree(GetProcessHeap(), 0, url);
1122 HeapFree(GetProcessHeap(), 0, type);
1128 EndDialog(hwnd, IDCANCEL);
1135 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1137 nsAString href_str, ns_url;
1138 nsIHTMLEditor *html_editor;
1139 nsIDOMHTMLElement *anchor_elem;
1140 PRBool insert_link_at_caret;
1141 nsISelection *nsselection;
1144 HRESULT hres = E_FAIL;
1146 static const WCHAR aW[] = {'a',0};
1147 static const WCHAR hrefW[] = {'h','r','e','f',0};
1149 TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1151 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1153 if (!in || V_VT(in) != VT_BSTR)
1155 WARN("invalid arg\n");
1156 return E_INVALIDARG;
1162 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1164 return OLECMDERR_E_CANCELED;
1167 if(!This->doc_node->nsdoc) {
1168 WARN("NULL nsdoc\n");
1169 return E_UNEXPECTED;
1172 nsselection = get_ns_selection(This);
1176 /* create an element for the link */
1177 create_nselem(This->doc_node, aW, &anchor_elem);
1179 nsAString_InitDepend(&href_str, hrefW);
1180 nsAString_InitDepend(&ns_url, url);
1181 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1182 nsAString_Finish(&href_str);
1184 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1186 /* create an element with text of URL */
1187 if (insert_link_at_caret) {
1188 nsIDOMNode *text_node, *unused_node;
1190 nsIDOMDocument_CreateTextNode(This->doc_node->nsdoc, &ns_url, (nsIDOMText **)&text_node);
1192 /* wrap the <a> tags around the text element */
1193 nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1194 nsIDOMNode_Release(text_node);
1195 nsIDOMNode_Release(unused_node);
1198 nsAString_Finish(&ns_url);
1200 nsIEditor_QueryInterface(This->doc_obj->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1204 if (insert_link_at_caret) {
1205 /* add them to the document at the caret position */
1206 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, (nsIDOMElement*)anchor_elem, FALSE);
1207 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1208 }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1209 nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, (nsIDOMElement*)anchor_elem);
1211 nsIHTMLEditor_Release(html_editor);
1212 hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1215 nsISelection_Release(nsselection);
1216 nsIDOMElement_Release(anchor_elem);
1218 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1221 TRACE("-- 0x%08x\n", hres);
1225 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1227 TRACE("(%p)->(%p)\n", This, cmd);
1229 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1233 const cmdtable_t editmode_cmds[] = {
1234 {IDM_DELETE, query_edit_status, exec_delete},
1235 {IDM_FONTNAME, query_edit_status, exec_fontname},
1236 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1237 {IDM_SELECTALL, query_selall_status , exec_selectall},
1238 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1239 {IDM_BOLD, query_edit_status, exec_bold},
1240 {IDM_ITALIC, query_edit_status, exec_italic},
1241 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1242 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1243 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1244 {IDM_FONT, NULL, exec_font},
1245 {IDM_UNDERLINE, query_edit_status, exec_underline},
1246 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1247 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1248 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1249 {IDM_INDENT, query_edit_status, exec_indent},
1250 {IDM_OUTDENT, query_edit_status, exec_outdent},
1251 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1252 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1253 {IDM_SETDIRTY, NULL, exec_setdirty},
1257 void init_editor(HTMLDocument *This)
1259 update_doc(This, UPDATE_UI);
1261 set_ns_fontname(This, "Times New Roman");
1264 HRESULT editor_is_dirty(HTMLDocument *This)
1268 if(!This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
1271 nsIEditor_GetDocumentModified(This->doc_obj->nscontainer->editor, &modified);
1273 return modified ? S_OK : S_FALSE;