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"
31 #include "wine/unicode.h"
33 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 #define NSCMD_ALIGN "cmd_align"
39 #define NSCMD_BEGINLINE "cmd_beginLine"
40 #define NSCMD_BOLD "cmd_bold"
41 #define NSCMD_CHARNEXT "cmd_charNext"
42 #define NSCMD_CHARPREVIOUS "cmd_charPrevious"
43 #define NSCMD_COPY "cmd_copy"
44 #define NSCMD_CUT "cmd_cut"
45 #define NSCMD_DELETECHARFORWARD "cmd_deleteCharForward"
46 #define NSCMD_DELETEWORDFORWARD "cmd_deleteWordForward"
47 #define NSCMD_ENDLINE "cmd_endLine"
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_INSERTLINKNOUI "cmd_insertLinkNoUI"
53 #define NSCMD_ITALIC "cmd_italic"
54 #define NSCMD_LINENEXT "cmd_lineNext"
55 #define NSCMD_LINEPREVIOUS "cmd_linePrevious"
56 #define NSCMD_MOVEBOTTOM "cmd_moveBottom"
57 #define NSCMD_MOVEPAGEDOWN "cmd_movePageDown"
58 #define NSCMD_MOVEPAGEUP "cmd_movePageUp"
59 #define NSCMD_MOVETOP "cmd_moveTop"
60 #define NSCMD_OL "cmd_ol"
61 #define NSCMD_OUTDENT "cmd_outdent"
62 #define NSCMD_PASTE "cmd_paste"
63 #define NSCMD_SELECTALL "cmd_selectAll"
64 #define NSCMD_SELECTBEGINLINE "cmd_selectBeginLine"
65 #define NSCMD_SELECTBOTTOM "cmd_selectBottom"
66 #define NSCMD_SELECTCHARNEXT "cmd_selectCharNext"
67 #define NSCMD_SELECTCHARPREVIOUS "cmd_selectCharPrevious"
68 #define NSCMD_SELECTENDLINE "cmd_selectEndLine"
69 #define NSCMD_SELECTLINENEXT "cmd_selectLineNext"
70 #define NSCMD_SELECTLINEPREVIOUS "cmd_selectLinePrevious"
71 #define NSCMD_SELECTPAGEDOWN "cmd_selectPageDown"
72 #define NSCMD_SELECTPAGEUP "cmd_selectPageUp"
73 #define NSCMD_SELECTTOP "cmd_selectTop"
74 #define NSCMD_SELECTWORDNEXT "cmd_selectWordNext"
75 #define NSCMD_SELECTWORDPREVIOUS "cmd_selectWordPrevious"
76 #define NSCMD_UL "cmd_ul"
77 #define NSCMD_UNDERLINE "cmd_underline"
78 #define NSCMD_WORDNEXT "cmd_wordNext"
79 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
81 #define NSSTATE_ATTRIBUTE "state_attribute"
82 #define NSSTATE_ALL "state_all"
84 #define NSALIGN_CENTER "center"
85 #define NSALIGN_LEFT "left"
86 #define NSALIGN_RIGHT "right"
88 #define DOM_VK_LEFT VK_LEFT
89 #define DOM_VK_UP VK_UP
90 #define DOM_VK_RIGHT VK_RIGHT
91 #define DOM_VK_DOWN VK_DOWN
92 #define DOM_VK_DELETE VK_DELETE
93 #define DOM_VK_HOME VK_HOME
94 #define DOM_VK_END VK_END
96 static const WCHAR fontW[] = {'f','o','n','t',0};
97 static const WCHAR sizeW[] = {'s','i','z','e',0};
99 void set_dirty(HTMLDocument *This, VARIANT_BOOL dirty)
103 if(This->doc_obj->usermode != EDITMODE || !This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
107 nsres = nsIEditor_IncrementModificationCount(This->doc_obj->nscontainer->editor, 1);
109 ERR("IncrementModificationCount failed: %08x\n", nsres);
111 nsres = nsIEditor_ResetModificationCount(This->doc_obj->nscontainer->editor);
113 ERR("ResetModificationCount failed: %08x\n", nsres);
117 static void do_ns_editor_command(NSContainer *This, const char *cmd)
121 if(!This->editor_controller)
124 nsres = nsIController_DoCommand(This->editor_controller, cmd);
126 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
129 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
131 nsICommandManager *cmdmgr;
134 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsICommandManager, (void**)&cmdmgr);
135 if(NS_FAILED(nsres)) {
136 ERR("Could not get nsICommandManager: %08x\n", nsres);
140 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, This->doc->basedoc.window->nswindow, nsparam);
142 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
144 nsICommandManager_Release(cmdmgr);
148 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
150 nsICommandParams *nsparam;
153 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
154 return OLECMDF_SUPPORTED;
156 if(This->doc_obj->nscontainer && nscmd) {
157 nsparam = create_nscommand_params();
158 get_ns_command_state(This->doc_obj->nscontainer, nscmd, nsparam);
160 nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
162 nsICommandParams_Release(nsparam);
165 return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
168 static void set_ns_align(HTMLDocument *This, const char *align_str)
170 nsICommandParams *nsparam;
172 if(!This->doc_obj->nscontainer)
175 nsparam = create_nscommand_params();
176 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
178 do_ns_command(This, NSCMD_ALIGN, nsparam);
180 nsICommandParams_Release(nsparam);
183 static DWORD query_align_status(HTMLDocument *This, const WCHAR *align)
185 DWORD ret = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
186 nsIDOMNSHTMLDocument *nsdoc;
187 nsAString justify_str;
191 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
192 return OLECMDF_SUPPORTED;
195 nsres = nsIDOMHTMLDocument_QueryInterface(This->doc_node->nsdoc, &IID_nsIDOMNSHTMLDocument,
197 if(NS_FAILED(nsres)) {
198 ERR("Could not get nsIDOMNSHTMLDocument iface: %08x\n", nsres);
202 nsAString_Init(&justify_str, align);
203 nsres = nsIDOMNSHTMLDocument_QueryCommandState(nsdoc, &justify_str, &b);
204 nsAString_Finish(&justify_str);
205 if(NS_SUCCEEDED(nsres) && b)
206 ret |= OLECMDF_LATCHED;
208 nsIDOMNSHTMLDocument_Release(nsdoc);
213 static nsISelection *get_ns_selection(HTMLDocument *This)
215 nsISelection *nsselection = NULL;
218 nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
220 ERR("GetSelection failed %08x\n", nsres);
226 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
229 PRUint32 child_cnt, i;
230 nsIDOMNode *child_node;
231 nsIDOMNodeList *node_list;
234 nsIDOMElement_HasChildNodes(elem, &has_children);
238 nsIDOMElement_GetChildNodes(elem, &node_list);
239 nsIDOMNodeList_GetLength(node_list, &child_cnt);
241 for(i=0; i<child_cnt; i++) {
242 nsIDOMNodeList_Item(node_list, i, &child_node);
244 nsIDOMNode_GetNodeType(child_node, &node_type);
245 if(node_type == ELEMENT_NODE) {
246 nsIDOMElement *child_elem;
248 const PRUnichar *ctag;
250 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
252 nsAString_Init(&tag_str, NULL);
253 nsIDOMElement_GetTagName(child_elem, &tag_str);
254 nsAString_GetData(&tag_str, &ctag);
256 if(!strcmpiW(ctag, tag))
257 /* FIXME: remove node if there are no more attributes */
258 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
260 nsAString_Finish(&tag_str);
262 remove_child_attr(child_elem, tag, attr_str);
264 nsIDOMNode_Release(child_elem);
267 nsIDOMNode_Release(child_node);
270 nsIDOMNodeList_Release(node_list);
273 static void get_font_size(HTMLDocument *This, WCHAR *ret)
275 nsISelection *nsselection = get_ns_selection(This);
276 nsIDOMElement *elem = NULL;
277 nsIDOMNode *node = NULL, *tmp_node;
288 nsISelection_GetFocusNode(nsselection, &node);
289 nsISelection_Release(nsselection);
292 nsres = nsIDOMNode_GetNodeType(node, &node_type);
293 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
296 if(node_type == ELEMENT_NODE) {
297 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
299 nsAString_Init(&tag_str, NULL);
300 nsIDOMElement_GetTagName(elem, &tag_str);
301 nsAString_GetData(&tag_str, &tag);
303 if(!strcmpiW(tag, fontW)) {
304 nsAString size_str, val_str;
307 TRACE("found font tag %p\n", elem);
309 nsAString_InitDepend(&size_str, sizeW);
310 nsAString_Init(&val_str, NULL);
312 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
313 nsAString_GetData(&val_str, &val);
316 TRACE("found size %s\n", debugstr_w(val));
320 nsAString_Finish(&size_str);
321 nsAString_Finish(&val_str);
324 nsAString_Finish(&tag_str);
326 nsIDOMElement_Release(elem);
333 nsIDOMNode_GetParentNode(node, &node);
334 nsIDOMNode_Release(tmp_node);
338 nsIDOMNode_Release(node);
341 static void set_font_size(HTMLDocument *This, LPCWSTR size)
343 nsISelection *nsselection;
345 nsIDOMHTMLElement *elem;
347 PRInt32 range_cnt = 0;
351 if(!This->doc_node->nsdoc) {
352 WARN("NULL nsdoc\n");
356 nsselection = get_ns_selection(This);
360 nsISelection_GetRangeCount(nsselection, &range_cnt);
362 FIXME("range_cnt %d not supprted\n", range_cnt);
364 nsISelection_Release(nsselection);
369 create_nselem(This->doc_node, fontW, &elem);
371 nsAString_InitDepend(&size_str, sizeW);
372 nsAString_InitDepend(&val_str, size);
374 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
375 nsAString_Finish(&val_str);
377 nsISelection_GetRangeAt(nsselection, 0, &range);
378 nsISelection_GetIsCollapsed(nsselection, &collapsed);
379 nsISelection_RemoveAllRanges(nsselection);
381 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
384 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
386 /* Remove all size attributes from the range */
387 remove_child_attr((nsIDOMElement*)elem, fontW, &size_str);
388 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
391 nsISelection_Release(nsselection);
392 nsIDOMRange_Release(range);
393 nsIDOMElement_Release(elem);
395 nsAString_Finish(&size_str);
397 set_dirty(This, VARIANT_TRUE);
400 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
405 nsIDOMKeyEvent_GetCtrlKey(event, &b);
409 nsIDOMKeyEvent_GetShiftKey(event, &b);
414 do_ns_editor_command(This->doc_obj->nscontainer, cmds[i]);
416 nsIDOMKeyEvent_PreventDefault(event);
419 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
421 nsIDOMKeyEvent *key_event;
424 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
426 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
430 static const char * const cmds[] = {
433 NSCMD_SELECTCHARPREVIOUS,
434 NSCMD_SELECTWORDPREVIOUS
438 handle_arrow_key(This, key_event, cmds);
442 static const char * const cmds[] = {
445 NSCMD_SELECTCHARNEXT,
450 handle_arrow_key(This, key_event, cmds);
454 static const char * const cmds[] = {
457 NSCMD_SELECTLINEPREVIOUS,
462 handle_arrow_key(This, key_event, cmds);
466 static const char * const cmds[] = {
469 NSCMD_SELECTLINENEXT,
474 handle_arrow_key(This, key_event, cmds);
477 case DOM_VK_DELETE: {
478 static const char * const cmds[] = {
479 NSCMD_DELETECHARFORWARD,
480 NSCMD_DELETEWORDFORWARD,
485 handle_arrow_key(This, key_event, cmds);
489 static const char * const cmds[] = {
492 NSCMD_SELECTBEGINLINE,
497 handle_arrow_key(This, key_event, cmds);
501 static const char * const cmds[] = {
509 handle_arrow_key(This, key_event, cmds);
514 nsIDOMKeyEvent_Release(key_event);
517 void handle_edit_load(HTMLDocument *This)
519 get_editor_controller(This->doc_obj->nscontainer);
522 static void set_ns_fontname(HTMLDocument *This, const char *fontname)
524 nsICommandParams *nsparam = create_nscommand_params();
526 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
527 do_ns_command(This, NSCMD_FONTFACE, nsparam);
528 nsICommandParams_Release(nsparam);
531 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
533 TRACE("(%p)->(%p %p)\n", This, in, out);
535 if(This->doc_obj->nscontainer)
536 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_DELETECHARFORWARD);
538 update_doc(This, UPDATE_UI);
542 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
544 TRACE("(%p)->(%p %p)\n", This, in, out);
546 if(!This->doc_obj->nscontainer) {
547 update_doc(This, UPDATE_UI);
554 if(V_VT(in) != VT_BSTR) {
555 FIXME("Unsupported vt=%d\n", V_VT(out));
559 TRACE("%s\n", debugstr_w(V_BSTR(in)));
561 stra = heap_strdupWtoA(V_BSTR(in));
562 set_ns_fontname(This, stra);
565 update_doc(This, UPDATE_UI);
569 nsICommandParams *nsparam;
578 nsparam = create_nscommand_params();
580 nsres = get_ns_command_state(This->doc_obj->nscontainer, NSCMD_FONTFACE, nsparam);
584 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
585 nsICommandParams_Release(nsparam);
587 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
588 strw = heap_alloc(len*sizeof(WCHAR));
589 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, len);
592 V_BSTR(out) = SysAllocString(strw);
599 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
601 TRACE("(%p)->(%p %p)\n", This, in, out);
604 if(V_VT(in) == VT_I4) {
605 nsICommandParams *nsparam = create_nscommand_params();
608 sprintf(color_str, "#%02x%02x%02x",
609 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
611 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
612 do_ns_command(This, NSCMD_FONTCOLOR, nsparam);
614 nsICommandParams_Release(nsparam);
616 FIXME("unsupported in vt=%d\n", V_VT(in));
619 update_doc(This, UPDATE_UI);
623 FIXME("unsupported out\n");
630 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
632 TRACE("(%p)->(%p %p)\n", This, in, out);
637 get_font_size(This, val);
639 V_I4(out) = strtolW(val, NULL, 10);
646 static const WCHAR format[] = {'%','d',0};
647 wsprintfW(size, format, V_I4(in));
648 set_font_size(This, size);
652 set_font_size(This, V_BSTR(in));
655 FIXME("unsupported vt %d\n", V_VT(in));
658 update_doc(This, UPDATE_UI);
664 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
667 FIXME("(%p)->(%p %p)\n", This, in, out);
671 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
673 TRACE("(%p)\n", This);
676 FIXME("unsupported args\n");
678 if(This->doc_obj->nscontainer)
679 do_ns_command(This, NSCMD_SELECTALL, NULL);
681 update_doc(This, UPDATE_UI);
685 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
687 TRACE("(%p)\n", This);
690 FIXME("unsupported args\n");
692 if(This->doc_obj->nscontainer)
693 do_ns_command(This, NSCMD_BOLD, NULL);
695 update_doc(This, UPDATE_UI);
699 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
701 TRACE("(%p)\n", This);
704 FIXME("unsupported args\n");
706 if(This->doc_obj->nscontainer)
707 do_ns_command(This, NSCMD_ITALIC, NULL);
709 update_doc(This, UPDATE_UI);
713 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
715 static const PRUnichar justifycenterW[] = {'j','u','s','t','i','f','y','c','e','n','t','e','r',0};
716 static const PRUnichar justifyrightW[] = {'j','u','s','t','i','f','y','r','i','g','h','t',0};
719 case IDM_JUSTIFYCENTER:
720 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
721 cmd->cmdf = query_align_status(This, justifycenterW);
723 case IDM_JUSTIFYLEFT:
724 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
725 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
726 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
727 cmd->cmdf = OLECMDF_SUPPORTED;
729 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
731 case IDM_JUSTIFYRIGHT:
732 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
733 cmd->cmdf = query_align_status(This, justifyrightW);
740 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
742 TRACE("(%p)\n", This);
745 FIXME("unsupported args\n");
747 set_ns_align(This, NSALIGN_CENTER);
748 update_doc(This, UPDATE_UI);
752 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
754 TRACE("(%p)\n", This);
757 FIXME("unsupported args\n");
759 set_ns_align(This, NSALIGN_LEFT);
760 update_doc(This, UPDATE_UI);
764 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
766 TRACE("(%p)\n", This);
769 FIXME("unsupported args\n");
771 set_ns_align(This, NSALIGN_RIGHT);
772 update_doc(This, UPDATE_UI);
776 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
778 TRACE("(%p)\n", This);
781 FIXME("unsupported args\n");
783 do_ns_command(This, NSCMD_UNDERLINE, NULL);
784 update_doc(This, UPDATE_UI);
788 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
790 TRACE("(%p)\n", This);
793 FIXME("unsupported args\n");
795 do_ns_command(This, NSCMD_INSERTHR, NULL);
796 update_doc(This, UPDATE_UI);
800 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
802 TRACE("(%p)\n", This);
805 FIXME("unsupported args\n");
807 do_ns_command(This, NSCMD_OL, NULL);
808 update_doc(This, UPDATE_UI);
812 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
814 TRACE("(%p)\n", This);
817 FIXME("unsupported args\n");
819 do_ns_command(This, NSCMD_UL, NULL);
820 update_doc(This, UPDATE_UI);
824 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
826 TRACE("(%p)\n", This);
829 FIXME("unsupported args\n");
831 do_ns_command(This, NSCMD_INDENT, NULL);
832 update_doc(This, UPDATE_UI);
836 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
838 TRACE("(%p)\n", This);
841 FIXME("unsupported args\n");
843 do_ns_command(This, NSCMD_OUTDENT, NULL);
844 update_doc(This, UPDATE_UI);
848 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
852 if(out || !in || V_VT(in) != VT_BSTR) {
853 WARN("invalid arg\n");
857 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
859 update_doc(This, UPDATE_UI);
863 exec_bold(This, cmdexecopt, NULL, NULL);
864 ptr = strchrW(ptr, ',');
869 exec_italic(This, cmdexecopt, NULL, NULL);
870 ptr = strchrW(ptr, ',');
875 exec_underline(This, cmdexecopt, NULL, NULL);
876 ptr = strchrW(ptr, ',');
880 if(isdigitW(*++ptr)) {
886 exec_fontsize(This, cmdexecopt, &v, NULL);
888 ptr = strchrW(ptr, ',');
893 FIXME("set font color\n");
894 ptr = strchrW(ptr, ',');
899 FIXME("set background color\n");
900 ptr = strchrW(ptr, ',');
909 V_BSTR(&v) = SysAllocString(ptr);
911 exec_fontname(This, cmdexecopt, &v, NULL);
913 SysFreeString(V_BSTR(&v));
919 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
921 update_doc(This, UPDATE_UI);
923 if(!This->doc_obj->nscontainer)
926 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_COPY);
930 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
932 update_doc(This, UPDATE_UI);
934 if(!This->doc_obj->nscontainer)
937 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_CUT);
941 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
943 update_doc(This, UPDATE_UI);
945 if(!This->doc_obj->nscontainer)
948 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_PASTE);
952 static HRESULT exec_setdirty(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
954 TRACE("(%p)->(%08x %p %p)\n", This, cmdexecopt, in, out);
959 if(V_VT(in) == VT_BOOL)
960 set_dirty(This, V_BOOL(in));
962 FIXME("unsupported vt=%d\n", V_VT(in));
967 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
971 TRACE("CGID_MSHTML: IDM_DELETE\n");
972 cmd->cmdf = query_ns_edit_status(This, NULL);
975 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
976 cmd->cmdf = query_ns_edit_status(This, NULL);
979 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
980 cmd->cmdf = query_ns_edit_status(This, NULL);
983 TRACE("CGID_MSHTML: IDM_BOLD\n");
984 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
987 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
988 cmd->cmdf = query_ns_edit_status(This, NULL);
991 TRACE("CGID_MSHTML: IDM_ITALIC\n");
992 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
995 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
996 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
998 case IDM_HORIZONTALLINE:
999 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1000 cmd->cmdf = query_ns_edit_status(This, NULL);
1003 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1004 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1006 case IDM_UNORDERLIST:
1007 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1008 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1011 TRACE("CGID_MSHTML: IDM_INDENT\n");
1012 cmd->cmdf = query_ns_edit_status(This, NULL);
1015 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1016 cmd->cmdf = query_ns_edit_status(This, NULL);
1019 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1020 cmd->cmdf = query_ns_edit_status(This, NULL);
1027 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1029 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1035 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1036 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1037 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1038 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1039 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1040 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1042 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1043 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1046 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1048 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1049 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1050 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1051 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1052 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1053 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1054 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1055 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1057 /* force the updating of the URL edit box */
1058 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1061 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1062 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1069 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1070 EndDialog(hwnd, wparam);
1072 case MAKEWPARAM(IDOK, BN_CLICKED):
1074 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1075 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1076 INT len = GetWindowTextLengthW(hwndURL);
1077 *url = SysAllocStringLen(NULL, len + 1);
1078 GetWindowTextW(hwndURL, *url, len + 1);
1079 EndDialog(hwnd, wparam);
1082 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1084 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1090 static const WCHAR wszSlashSlash[] = {'/','/'};
1092 /* get string of currently selected hyperlink type */
1093 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1094 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1095 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1096 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1098 if (!strcmpW(type, wszOther))
1101 /* get current URL */
1102 len = GetWindowTextLengthW(hwndURL);
1103 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1104 GetWindowTextW(hwndURL, url, len + 1);
1106 /* strip off old protocol */
1107 p = strchrW(url, ':');
1108 if (p && p[1] == '/' && p[2] == '/')
1111 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1113 /* add new protocol */
1116 memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1117 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1120 SetWindowTextW(hwndURL, url);
1122 HeapFree(GetProcessHeap(), 0, url);
1123 HeapFree(GetProcessHeap(), 0, type);
1129 EndDialog(hwnd, IDCANCEL);
1136 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1138 nsAString href_str, ns_url;
1139 nsIHTMLEditor *html_editor;
1140 nsIDOMHTMLElement *anchor_elem;
1141 PRBool insert_link_at_caret;
1142 nsISelection *nsselection;
1145 HRESULT hres = E_FAIL;
1147 static const WCHAR aW[] = {'a',0};
1148 static const WCHAR hrefW[] = {'h','r','e','f',0};
1150 TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1152 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1154 if (!in || V_VT(in) != VT_BSTR)
1156 WARN("invalid arg\n");
1157 return E_INVALIDARG;
1163 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1165 return OLECMDERR_E_CANCELED;
1168 if(!This->doc_node->nsdoc) {
1169 WARN("NULL nsdoc\n");
1170 return E_UNEXPECTED;
1173 nsselection = get_ns_selection(This);
1177 /* create an element for the link */
1178 create_nselem(This->doc_node, aW, &anchor_elem);
1180 nsAString_InitDepend(&href_str, hrefW);
1181 nsAString_InitDepend(&ns_url, url);
1182 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1183 nsAString_Finish(&href_str);
1185 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1187 /* create an element with text of URL */
1188 if (insert_link_at_caret) {
1189 nsIDOMNode *text_node, *unused_node;
1191 nsIDOMDocument_CreateTextNode(This->doc_node->nsdoc, &ns_url, (nsIDOMText **)&text_node);
1193 /* wrap the <a> tags around the text element */
1194 nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1195 nsIDOMNode_Release(text_node);
1196 nsIDOMNode_Release(unused_node);
1199 nsAString_Finish(&ns_url);
1201 nsIEditor_QueryInterface(This->doc_obj->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1205 if (insert_link_at_caret) {
1206 /* add them to the document at the caret position */
1207 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, (nsIDOMElement*)anchor_elem, FALSE);
1208 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1209 }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1210 nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, (nsIDOMElement*)anchor_elem);
1212 nsIHTMLEditor_Release(html_editor);
1213 hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1216 nsISelection_Release(nsselection);
1217 nsIDOMElement_Release(anchor_elem);
1219 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1222 TRACE("-- 0x%08x\n", hres);
1226 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1228 TRACE("(%p)->(%p)\n", This, cmd);
1230 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1234 const cmdtable_t editmode_cmds[] = {
1235 {IDM_DELETE, query_edit_status, exec_delete},
1236 {IDM_FONTNAME, query_edit_status, exec_fontname},
1237 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1238 {IDM_SELECTALL, query_selall_status , exec_selectall},
1239 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1240 {IDM_BOLD, query_edit_status, exec_bold},
1241 {IDM_ITALIC, query_edit_status, exec_italic},
1242 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1243 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1244 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1245 {IDM_FONT, NULL, exec_font},
1246 {IDM_UNDERLINE, query_edit_status, exec_underline},
1247 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1248 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1249 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1250 {IDM_INDENT, query_edit_status, exec_indent},
1251 {IDM_OUTDENT, query_edit_status, exec_outdent},
1252 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1253 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1254 {IDM_SETDIRTY, NULL, exec_setdirty},
1258 void init_editor(HTMLDocument *This)
1260 update_doc(This, UPDATE_UI);
1262 set_ns_fontname(This, "Times New Roman");
1265 HRESULT editor_is_dirty(HTMLDocument *This)
1269 if(!This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
1272 nsIEditor_GetDocumentModified(This->doc_obj->nscontainer->editor, &modified);
1274 return modified ? S_OK : S_FALSE;