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 char *align_str)
185 nsICommandParams *nsparam;
188 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
189 return OLECMDF_SUPPORTED;
191 if(This->doc_obj->nscontainer) {
192 nsparam = create_nscommand_params();
193 get_ns_command_state(This->doc_obj->nscontainer, NSCMD_ALIGN, nsparam);
195 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &align);
197 nsICommandParams_Release(nsparam);
200 return OLECMDF_SUPPORTED | OLECMDF_ENABLED
201 | (align && !strcmp(align_str, align) ? OLECMDF_LATCHED : 0);
205 static nsISelection *get_ns_selection(HTMLDocument *This)
207 nsISelection *nsselection = NULL;
210 nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
212 ERR("GetSelection failed %08x\n", nsres);
218 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
221 PRUint32 child_cnt, i;
222 nsIDOMNode *child_node;
223 nsIDOMNodeList *node_list;
226 nsIDOMElement_HasChildNodes(elem, &has_children);
230 nsIDOMElement_GetChildNodes(elem, &node_list);
231 nsIDOMNodeList_GetLength(node_list, &child_cnt);
233 for(i=0; i<child_cnt; i++) {
234 nsIDOMNodeList_Item(node_list, i, &child_node);
236 nsIDOMNode_GetNodeType(child_node, &node_type);
237 if(node_type == ELEMENT_NODE) {
238 nsIDOMElement *child_elem;
240 const PRUnichar *ctag;
242 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
244 nsAString_Init(&tag_str, NULL);
245 nsIDOMElement_GetTagName(child_elem, &tag_str);
246 nsAString_GetData(&tag_str, &ctag);
248 if(!strcmpiW(ctag, tag))
249 /* FIXME: remove node if there are no more attributes */
250 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
252 nsAString_Finish(&tag_str);
254 remove_child_attr(child_elem, tag, attr_str);
256 nsIDOMNode_Release(child_elem);
259 nsIDOMNode_Release(child_node);
262 nsIDOMNodeList_Release(node_list);
265 static void get_font_size(HTMLDocument *This, WCHAR *ret)
267 nsISelection *nsselection = get_ns_selection(This);
268 nsIDOMElement *elem = NULL;
269 nsIDOMNode *node = NULL, *tmp_node;
280 nsISelection_GetFocusNode(nsselection, &node);
281 nsISelection_Release(nsselection);
284 nsres = nsIDOMNode_GetNodeType(node, &node_type);
285 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
288 if(node_type == ELEMENT_NODE) {
289 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
291 nsAString_Init(&tag_str, NULL);
292 nsIDOMElement_GetTagName(elem, &tag_str);
293 nsAString_GetData(&tag_str, &tag);
295 if(!strcmpiW(tag, fontW)) {
296 nsAString size_str, val_str;
299 TRACE("found font tag %p\n", elem);
301 nsAString_InitDepend(&size_str, sizeW);
302 nsAString_Init(&val_str, NULL);
304 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
305 nsAString_GetData(&val_str, &val);
308 TRACE("found size %s\n", debugstr_w(val));
312 nsAString_Finish(&size_str);
313 nsAString_Finish(&val_str);
316 nsAString_Finish(&tag_str);
318 nsIDOMElement_Release(elem);
325 nsIDOMNode_GetParentNode(node, &node);
326 nsIDOMNode_Release(tmp_node);
330 nsIDOMNode_Release(node);
333 static void set_font_size(HTMLDocument *This, LPCWSTR size)
335 nsISelection *nsselection;
337 nsIDOMHTMLElement *elem;
339 PRInt32 range_cnt = 0;
343 if(!This->doc_node->nsdoc) {
344 WARN("NULL nsdoc\n");
348 nsselection = get_ns_selection(This);
352 nsISelection_GetRangeCount(nsselection, &range_cnt);
354 FIXME("range_cnt %d not supprted\n", range_cnt);
356 nsISelection_Release(nsselection);
361 create_nselem(This->doc_node, fontW, &elem);
363 nsAString_InitDepend(&size_str, sizeW);
364 nsAString_InitDepend(&val_str, size);
366 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
367 nsAString_Finish(&val_str);
369 nsISelection_GetRangeAt(nsselection, 0, &range);
370 nsISelection_GetIsCollapsed(nsselection, &collapsed);
371 nsISelection_RemoveAllRanges(nsselection);
373 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
376 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
378 /* Remove all size attributes from the range */
379 remove_child_attr((nsIDOMElement*)elem, fontW, &size_str);
380 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
383 nsISelection_Release(nsselection);
384 nsIDOMRange_Release(range);
385 nsIDOMElement_Release(elem);
387 nsAString_Finish(&size_str);
389 set_dirty(This, VARIANT_TRUE);
392 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
397 nsIDOMKeyEvent_GetCtrlKey(event, &b);
401 nsIDOMKeyEvent_GetShiftKey(event, &b);
406 do_ns_editor_command(This->doc_obj->nscontainer, cmds[i]);
408 nsIDOMKeyEvent_PreventDefault(event);
411 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
413 nsIDOMKeyEvent *key_event;
416 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
418 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
422 static const char * const cmds[] = {
425 NSCMD_SELECTCHARPREVIOUS,
426 NSCMD_SELECTWORDPREVIOUS
430 handle_arrow_key(This, key_event, cmds);
434 static const char * const cmds[] = {
437 NSCMD_SELECTCHARNEXT,
442 handle_arrow_key(This, key_event, cmds);
446 static const char * const cmds[] = {
449 NSCMD_SELECTLINEPREVIOUS,
454 handle_arrow_key(This, key_event, cmds);
458 static const char * const cmds[] = {
461 NSCMD_SELECTLINENEXT,
466 handle_arrow_key(This, key_event, cmds);
469 case DOM_VK_DELETE: {
470 static const char * const cmds[] = {
471 NSCMD_DELETECHARFORWARD,
472 NSCMD_DELETEWORDFORWARD,
477 handle_arrow_key(This, key_event, cmds);
481 static const char * const cmds[] = {
484 NSCMD_SELECTBEGINLINE,
489 handle_arrow_key(This, key_event, cmds);
493 static const char * const cmds[] = {
501 handle_arrow_key(This, key_event, cmds);
506 nsIDOMKeyEvent_Release(key_event);
509 void handle_edit_load(HTMLDocument *This)
511 get_editor_controller(This->doc_obj->nscontainer);
514 static void set_ns_fontname(HTMLDocument *This, const char *fontname)
516 nsICommandParams *nsparam = create_nscommand_params();
518 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
519 do_ns_command(This, NSCMD_FONTFACE, nsparam);
520 nsICommandParams_Release(nsparam);
523 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
525 TRACE("(%p)->(%p %p)\n", This, in, out);
527 if(This->doc_obj->nscontainer)
528 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_DELETECHARFORWARD);
530 update_doc(This, UPDATE_UI);
534 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
536 TRACE("(%p)->(%p %p)\n", This, in, out);
538 if(!This->doc_obj->nscontainer) {
539 update_doc(This, UPDATE_UI);
546 if(V_VT(in) != VT_BSTR) {
547 FIXME("Unsupported vt=%d\n", V_VT(out));
551 TRACE("%s\n", debugstr_w(V_BSTR(in)));
553 stra = heap_strdupWtoA(V_BSTR(in));
554 set_ns_fontname(This, stra);
557 update_doc(This, UPDATE_UI);
561 nsICommandParams *nsparam;
570 nsparam = create_nscommand_params();
572 nsres = get_ns_command_state(This->doc_obj->nscontainer, NSCMD_FONTFACE, nsparam);
576 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
577 nsICommandParams_Release(nsparam);
579 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
580 strw = heap_alloc(len*sizeof(WCHAR));
581 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, len);
584 V_BSTR(out) = SysAllocString(strw);
591 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
593 TRACE("(%p)->(%p %p)\n", This, in, out);
596 if(V_VT(in) == VT_I4) {
597 nsICommandParams *nsparam = create_nscommand_params();
600 sprintf(color_str, "#%02x%02x%02x",
601 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
603 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
604 do_ns_command(This, NSCMD_FONTCOLOR, nsparam);
606 nsICommandParams_Release(nsparam);
608 FIXME("unsupported in vt=%d\n", V_VT(in));
611 update_doc(This, UPDATE_UI);
615 FIXME("unsupported out\n");
622 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
624 TRACE("(%p)->(%p %p)\n", This, in, out);
629 get_font_size(This, val);
631 V_I4(out) = strtolW(val, NULL, 10);
638 static const WCHAR format[] = {'%','d',0};
639 wsprintfW(size, format, V_I4(in));
640 set_font_size(This, size);
644 set_font_size(This, V_BSTR(in));
647 FIXME("unsupported vt %d\n", V_VT(in));
650 update_doc(This, UPDATE_UI);
656 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
659 FIXME("(%p)->(%p %p)\n", This, in, out);
663 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
665 TRACE("(%p)\n", This);
668 FIXME("unsupported args\n");
670 if(This->doc_obj->nscontainer)
671 do_ns_command(This, NSCMD_SELECTALL, NULL);
673 update_doc(This, UPDATE_UI);
677 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
679 TRACE("(%p)\n", This);
682 FIXME("unsupported args\n");
684 if(This->doc_obj->nscontainer)
685 do_ns_command(This, NSCMD_BOLD, NULL);
687 update_doc(This, UPDATE_UI);
691 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
693 TRACE("(%p)\n", This);
696 FIXME("unsupported args\n");
698 if(This->doc_obj->nscontainer)
699 do_ns_command(This, NSCMD_ITALIC, NULL);
701 update_doc(This, UPDATE_UI);
705 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
708 case IDM_JUSTIFYCENTER:
709 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
710 cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
712 case IDM_JUSTIFYLEFT:
713 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
714 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
715 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
716 cmd->cmdf = OLECMDF_SUPPORTED;
718 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
720 case IDM_JUSTIFYRIGHT:
721 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
722 cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
729 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
731 TRACE("(%p)\n", This);
734 FIXME("unsupported args\n");
736 set_ns_align(This, NSALIGN_CENTER);
737 update_doc(This, UPDATE_UI);
741 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
743 TRACE("(%p)\n", This);
746 FIXME("unsupported args\n");
748 set_ns_align(This, NSALIGN_LEFT);
749 update_doc(This, UPDATE_UI);
753 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
755 TRACE("(%p)\n", This);
758 FIXME("unsupported args\n");
760 set_ns_align(This, NSALIGN_RIGHT);
761 update_doc(This, UPDATE_UI);
765 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
767 TRACE("(%p)\n", This);
770 FIXME("unsupported args\n");
772 do_ns_command(This, NSCMD_UNDERLINE, NULL);
773 update_doc(This, UPDATE_UI);
777 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
779 TRACE("(%p)\n", This);
782 FIXME("unsupported args\n");
784 do_ns_command(This, NSCMD_INSERTHR, NULL);
785 update_doc(This, UPDATE_UI);
789 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
791 TRACE("(%p)\n", This);
794 FIXME("unsupported args\n");
796 do_ns_command(This, NSCMD_OL, NULL);
797 update_doc(This, UPDATE_UI);
801 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
803 TRACE("(%p)\n", This);
806 FIXME("unsupported args\n");
808 do_ns_command(This, NSCMD_UL, NULL);
809 update_doc(This, UPDATE_UI);
813 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
815 TRACE("(%p)\n", This);
818 FIXME("unsupported args\n");
820 do_ns_command(This, NSCMD_INDENT, NULL);
821 update_doc(This, UPDATE_UI);
825 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
827 TRACE("(%p)\n", This);
830 FIXME("unsupported args\n");
832 do_ns_command(This, NSCMD_OUTDENT, NULL);
833 update_doc(This, UPDATE_UI);
837 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
841 if(out || !in || V_VT(in) != VT_BSTR) {
842 WARN("invalid arg\n");
846 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
848 update_doc(This, UPDATE_UI);
852 exec_bold(This, cmdexecopt, NULL, NULL);
853 ptr = strchrW(ptr, ',');
858 exec_italic(This, cmdexecopt, NULL, NULL);
859 ptr = strchrW(ptr, ',');
864 exec_underline(This, cmdexecopt, NULL, NULL);
865 ptr = strchrW(ptr, ',');
869 if(isdigitW(*++ptr)) {
875 exec_fontsize(This, cmdexecopt, &v, NULL);
877 ptr = strchrW(ptr, ',');
882 FIXME("set font color\n");
883 ptr = strchrW(ptr, ',');
888 FIXME("set background color\n");
889 ptr = strchrW(ptr, ',');
898 V_BSTR(&v) = SysAllocString(ptr);
900 exec_fontname(This, cmdexecopt, &v, NULL);
902 SysFreeString(V_BSTR(&v));
908 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
910 update_doc(This, UPDATE_UI);
912 if(!This->doc_obj->nscontainer)
915 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_COPY);
919 HRESULT editor_exec_cut(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_CUT);
930 HRESULT editor_exec_paste(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_PASTE);
941 static HRESULT exec_setdirty(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
943 TRACE("(%p)->(%08x %p %p)\n", This, cmdexecopt, in, out);
948 if(V_VT(in) == VT_BOOL)
949 set_dirty(This, V_BOOL(in));
951 FIXME("unsupported vt=%d\n", V_VT(in));
956 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
960 TRACE("CGID_MSHTML: IDM_DELETE\n");
961 cmd->cmdf = query_ns_edit_status(This, NULL);
964 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
965 cmd->cmdf = query_ns_edit_status(This, NULL);
968 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
969 cmd->cmdf = query_ns_edit_status(This, NULL);
972 TRACE("CGID_MSHTML: IDM_BOLD\n");
973 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
976 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
977 cmd->cmdf = query_ns_edit_status(This, NULL);
980 TRACE("CGID_MSHTML: IDM_ITALIC\n");
981 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
984 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
985 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
987 case IDM_HORIZONTALLINE:
988 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
989 cmd->cmdf = query_ns_edit_status(This, NULL);
992 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
993 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
995 case IDM_UNORDERLIST:
996 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
997 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1000 TRACE("CGID_MSHTML: IDM_INDENT\n");
1001 cmd->cmdf = query_ns_edit_status(This, NULL);
1004 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1005 cmd->cmdf = query_ns_edit_status(This, NULL);
1008 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1009 cmd->cmdf = query_ns_edit_status(This, NULL);
1016 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1018 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1024 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1025 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1026 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1027 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1028 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1029 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1031 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1032 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1035 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1037 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1038 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1039 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1040 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1041 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1042 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1043 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1044 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1046 /* force the updating of the URL edit box */
1047 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1050 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1051 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1058 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1059 EndDialog(hwnd, wparam);
1061 case MAKEWPARAM(IDOK, BN_CLICKED):
1063 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1064 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1065 INT len = GetWindowTextLengthW(hwndURL);
1066 *url = SysAllocStringLen(NULL, len + 1);
1067 GetWindowTextW(hwndURL, *url, len + 1);
1068 EndDialog(hwnd, wparam);
1071 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1073 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1079 static const WCHAR wszSlashSlash[] = {'/','/'};
1081 /* get string of currently selected hyperlink type */
1082 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1083 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1084 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1085 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1087 if (!strcmpW(type, wszOther))
1090 /* get current URL */
1091 len = GetWindowTextLengthW(hwndURL);
1092 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1093 GetWindowTextW(hwndURL, url, len + 1);
1095 /* strip off old protocol */
1096 p = strchrW(url, ':');
1097 if (p && p[1] == '/' && p[2] == '/')
1100 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1102 /* add new protocol */
1105 memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1106 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1109 SetWindowTextW(hwndURL, url);
1111 HeapFree(GetProcessHeap(), 0, url);
1112 HeapFree(GetProcessHeap(), 0, type);
1118 EndDialog(hwnd, IDCANCEL);
1125 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1127 nsAString href_str, ns_url;
1128 nsIHTMLEditor *html_editor;
1129 nsIDOMHTMLElement *anchor_elem;
1130 PRBool insert_link_at_caret;
1131 nsISelection *nsselection;
1134 HRESULT hres = E_FAIL;
1136 static const WCHAR aW[] = {'a',0};
1137 static const WCHAR hrefW[] = {'h','r','e','f',0};
1139 TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1141 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1143 if (!in || V_VT(in) != VT_BSTR)
1145 WARN("invalid arg\n");
1146 return E_INVALIDARG;
1152 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1154 return OLECMDERR_E_CANCELED;
1157 if(!This->doc_node->nsdoc) {
1158 WARN("NULL nsdoc\n");
1159 return E_UNEXPECTED;
1162 nsselection = get_ns_selection(This);
1166 /* create an element for the link */
1167 create_nselem(This->doc_node, aW, &anchor_elem);
1169 nsAString_InitDepend(&href_str, hrefW);
1170 nsAString_InitDepend(&ns_url, url);
1171 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1172 nsAString_Finish(&href_str);
1174 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1176 /* create an element with text of URL */
1177 if (insert_link_at_caret) {
1178 nsIDOMNode *text_node, *unused_node;
1180 nsIDOMDocument_CreateTextNode(This->doc_node->nsdoc, &ns_url, (nsIDOMText **)&text_node);
1182 /* wrap the <a> tags around the text element */
1183 nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1184 nsIDOMNode_Release(text_node);
1185 nsIDOMNode_Release(unused_node);
1188 nsAString_Finish(&ns_url);
1190 nsIEditor_QueryInterface(This->doc_obj->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1194 if (insert_link_at_caret) {
1195 /* add them to the document at the caret position */
1196 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, (nsIDOMElement*)anchor_elem, FALSE);
1197 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1198 }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1199 nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, (nsIDOMElement*)anchor_elem);
1201 nsIHTMLEditor_Release(html_editor);
1202 hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1205 nsISelection_Release(nsselection);
1206 nsIDOMElement_Release(anchor_elem);
1208 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1211 TRACE("-- 0x%08x\n", hres);
1215 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1217 TRACE("(%p)->(%p)\n", This, cmd);
1219 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1223 const cmdtable_t editmode_cmds[] = {
1224 {IDM_DELETE, query_edit_status, exec_delete},
1225 {IDM_FONTNAME, query_edit_status, exec_fontname},
1226 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1227 {IDM_SELECTALL, query_selall_status , exec_selectall},
1228 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1229 {IDM_BOLD, query_edit_status, exec_bold},
1230 {IDM_ITALIC, query_edit_status, exec_italic},
1231 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1232 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1233 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1234 {IDM_FONT, NULL, exec_font},
1235 {IDM_UNDERLINE, query_edit_status, exec_underline},
1236 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1237 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1238 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1239 {IDM_INDENT, query_edit_status, exec_indent},
1240 {IDM_OUTDENT, query_edit_status, exec_outdent},
1241 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1242 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1243 {IDM_SETDIRTY, NULL, exec_setdirty},
1247 void init_editor(HTMLDocument *This)
1249 update_doc(This, UPDATE_UI);
1251 set_ns_fontname(This, "Times New Roman");
1254 HRESULT editor_is_dirty(HTMLDocument *This)
1258 if(!This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
1261 nsIEditor_GetDocumentModified(This->doc_obj->nscontainer->editor, &modified);
1263 return modified ? S_OK : S_FALSE;