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
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "mshtml_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 #define NSCMD_ALIGN "cmd_align"
42 #define NSCMD_BEGINLINE "cmd_beginLine"
43 #define NSCMD_BOLD "cmd_bold"
44 #define NSCMD_CHARNEXT "cmd_charNext"
45 #define NSCMD_CHARPREVIOUS "cmd_charPrevious"
46 #define NSCMD_COPY "cmd_copy"
47 #define NSCMD_CUT "cmd_cut"
48 #define NSCMD_DELETECHARFORWARD "cmd_deleteCharForward"
49 #define NSCMD_DELETEWORDFORWARD "cmd_deleteWordForward"
50 #define NSCMD_ENDLINE "cmd_endLine"
51 #define NSCMD_FONTCOLOR "cmd_fontColor"
52 #define NSCMD_FONTFACE "cmd_fontFace"
53 #define NSCMD_INDENT "cmd_indent"
54 #define NSCMD_INSERTHR "cmd_insertHR"
55 #define NSCMD_INSERTLINKNOUI "cmd_insertLinkNoUI"
56 #define NSCMD_ITALIC "cmd_italic"
57 #define NSCMD_LINENEXT "cmd_lineNext"
58 #define NSCMD_LINEPREVIOUS "cmd_linePrevious"
59 #define NSCMD_MOVEBOTTOM "cmd_moveBottom"
60 #define NSCMD_MOVEPAGEDOWN "cmd_movePageDown"
61 #define NSCMD_MOVEPAGEUP "cmd_movePageUp"
62 #define NSCMD_MOVETOP "cmd_moveTop"
63 #define NSCMD_OL "cmd_ol"
64 #define NSCMD_OUTDENT "cmd_outdent"
65 #define NSCMD_PASTE "cmd_paste"
66 #define NSCMD_SELECTALL "cmd_selectAll"
67 #define NSCMD_SELECTBEGINLINE "cmd_selectBeginLine"
68 #define NSCMD_SELECTBOTTOM "cmd_selectBottom"
69 #define NSCMD_SELECTCHARNEXT "cmd_selectCharNext"
70 #define NSCMD_SELECTCHARPREVIOUS "cmd_selectCharPrevious"
71 #define NSCMD_SELECTENDLINE "cmd_selectEndLine"
72 #define NSCMD_SELECTLINENEXT "cmd_selectLineNext"
73 #define NSCMD_SELECTLINEPREVIOUS "cmd_selectLinePrevious"
74 #define NSCMD_SELECTPAGEDOWN "cmd_selectPageDown"
75 #define NSCMD_SELECTPAGEUP "cmd_selectPageUp"
76 #define NSCMD_SELECTTOP "cmd_selectTop"
77 #define NSCMD_SELECTWORDNEXT "cmd_selectWordNext"
78 #define NSCMD_SELECTWORDPREVIOUS "cmd_selectWordPrevious"
79 #define NSCMD_UL "cmd_ul"
80 #define NSCMD_UNDERLINE "cmd_underline"
81 #define NSCMD_WORDNEXT "cmd_wordNext"
82 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
84 #define NSSTATE_ATTRIBUTE "state_attribute"
85 #define NSSTATE_ALL "state_all"
87 #define NSALIGN_CENTER "center"
88 #define NSALIGN_LEFT "left"
89 #define NSALIGN_RIGHT "right"
91 #define DOM_VK_LEFT VK_LEFT
92 #define DOM_VK_UP VK_UP
93 #define DOM_VK_RIGHT VK_RIGHT
94 #define DOM_VK_DOWN VK_DOWN
95 #define DOM_VK_DELETE VK_DELETE
96 #define DOM_VK_HOME VK_HOME
97 #define DOM_VK_END VK_END
99 static const WCHAR wszFont[] = {'f','o','n','t',0};
100 static const WCHAR wszSize[] = {'s','i','z','e',0};
102 static void do_ns_editor_command(NSContainer *This, const char *cmd)
106 if(!This->editor_controller)
109 nsres = nsIController_DoCommand(This->editor_controller, cmd);
111 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
114 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
116 nsICommandManager *cmdmgr;
119 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsICommandManager, (void**)&cmdmgr);
120 if(NS_FAILED(nsres)) {
121 ERR("Could not get nsICommandManager: %08x\n", nsres);
125 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, NULL, nsparam);
127 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
129 nsICommandManager_Release(cmdmgr);
133 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
135 nsICommandParams *nsparam;
138 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
139 return OLECMDF_SUPPORTED;
141 if(This->nscontainer && nscmd) {
142 nsparam = create_nscommand_params();
143 get_ns_command_state(This->nscontainer, nscmd, nsparam);
145 nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
147 nsICommandParams_Release(nsparam);
150 return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
153 static void set_ns_align(HTMLDocument *This, const char *align_str)
155 nsICommandParams *nsparam;
157 if(!This->nscontainer)
160 nsparam = create_nscommand_params();
161 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
163 do_ns_command(This->nscontainer, NSCMD_ALIGN, nsparam);
165 nsICommandParams_Release(nsparam);
168 static DWORD query_align_status(HTMLDocument *This, const char *align_str)
170 nsICommandParams *nsparam;
173 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
174 return OLECMDF_SUPPORTED;
176 if(This->nscontainer) {
177 nsparam = create_nscommand_params();
178 get_ns_command_state(This->nscontainer, NSCMD_ALIGN, nsparam);
180 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &align);
182 nsICommandParams_Release(nsparam);
185 return OLECMDF_SUPPORTED | OLECMDF_ENABLED
186 | (align && !strcmp(align_str, align) ? OLECMDF_LATCHED : 0);
190 static nsISelection *get_ns_selection(HTMLDocument *This)
192 nsIDOMWindow *dom_window;
193 nsISelection *nsselection = NULL;
196 if(!This->nscontainer)
199 nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
203 nsIDOMWindow_GetSelection(dom_window, &nsselection);
204 nsIDOMWindow_Release(dom_window);
210 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
213 PRUint32 child_cnt, i;
214 nsIDOMNode *child_node;
215 nsIDOMNodeList *node_list;
218 nsIDOMElement_HasChildNodes(elem, &has_children);
222 nsIDOMElement_GetChildNodes(elem, &node_list);
223 nsIDOMNodeList_GetLength(node_list, &child_cnt);
225 for(i=0; i<child_cnt; i++) {
226 nsIDOMNodeList_Item(node_list, i, &child_node);
228 nsIDOMNode_GetNodeType(child_node, &node_type);
229 if(node_type == ELEMENT_NODE) {
230 nsIDOMElement *child_elem;
232 const PRUnichar *ctag;
234 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
236 nsAString_Init(&tag_str, NULL);
237 nsIDOMElement_GetTagName(child_elem, &tag_str);
238 nsAString_GetData(&tag_str, &ctag, NULL);
240 if(!strcmpiW(ctag, tag))
241 /* FIXME: remove node if there are no more attributes */
242 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
244 nsAString_Finish(&tag_str);
246 remove_child_attr(child_elem, tag, attr_str);
248 nsIDOMNode_Release(child_elem);
251 nsIDOMNode_Release(child_node);
254 nsIDOMNodeList_Release(node_list);
257 static void get_font_size(HTMLDocument *This, WCHAR *ret)
259 nsISelection *nsselection = get_ns_selection(This);
260 nsIDOMElement *elem = NULL;
261 nsIDOMNode *node = NULL, *tmp_node;
272 nsISelection_GetFocusNode(nsselection, &node);
273 nsISelection_Release(nsselection);
276 nsres = nsIDOMNode_GetNodeType(node, &node_type);
277 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
280 if(node_type == ELEMENT_NODE) {
281 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
283 nsAString_Init(&tag_str, NULL);
284 nsIDOMElement_GetTagName(elem, &tag_str);
285 nsAString_GetData(&tag_str, &tag, NULL);
287 if(!strcmpiW(tag, wszFont)) {
288 nsAString size_str, val_str;
291 TRACE("found font tag %p\n", elem);
293 nsAString_Init(&size_str, wszSize);
294 nsAString_Init(&val_str, NULL);
296 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
297 nsAString_GetData(&val_str, &val, NULL);
300 TRACE("found size %s\n", debugstr_w(val));
304 nsAString_Finish(&size_str);
305 nsAString_Finish(&val_str);
308 nsAString_Finish(&tag_str);
310 nsIDOMElement_Release(elem);
317 nsIDOMNode_GetParentNode(node, &node);
318 nsIDOMNode_Release(tmp_node);
322 nsIDOMNode_Release(node);
325 static void set_font_size(HTMLDocument *This, LPCWSTR size)
327 nsISelection *nsselection;
329 nsIDOMDocument *nsdoc;
332 PRInt32 range_cnt = 0;
338 nsselection = get_ns_selection(This);
343 nsISelection_GetRangeCount(nsselection, &range_cnt);
345 FIXME("range_cnt %d not supprted\n", range_cnt);
347 nsISelection_Release(nsselection);
352 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
356 nsAString_Init(&font_str, wszFont);
357 nsAString_Init(&size_str, wszSize);
358 nsAString_Init(&val_str, size);
360 nsIDOMDocument_CreateElement(nsdoc, &font_str, &elem);
361 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
363 nsISelection_GetRangeAt(nsselection, 0, &range);
364 nsISelection_GetIsCollapsed(nsselection, &collapsed);
365 nsISelection_RemoveAllRanges(nsselection);
367 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
370 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
372 /* Remove all size attrbutes from the range */
373 remove_child_attr(elem, wszFont, &size_str);
374 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
377 nsIDOMRange_Release(range);
378 nsIDOMElement_Release(elem);
380 nsAString_Finish(&font_str);
381 nsAString_Finish(&size_str);
382 nsAString_Finish(&val_str);
384 nsISelection_Release(nsselection);
385 nsIDOMDocument_Release(nsdoc);
388 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
393 nsIDOMKeyEvent_GetCtrlKey(event, &b);
397 nsIDOMKeyEvent_GetShiftKey(event, &b);
402 do_ns_editor_command(This->nscontainer, cmds[i]);
404 nsIDOMKeyEvent_PreventDefault(event);
407 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
409 nsIDOMKeyEvent *key_event;
412 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
414 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
418 static const char * const cmds[] = {
421 NSCMD_SELECTCHARPREVIOUS,
422 NSCMD_SELECTWORDPREVIOUS
426 handle_arrow_key(This, key_event, cmds);
430 static const char * const cmds[] = {
433 NSCMD_SELECTCHARNEXT,
438 handle_arrow_key(This, key_event, cmds);
442 static const char * const cmds[] = {
445 NSCMD_SELECTLINEPREVIOUS,
450 handle_arrow_key(This, key_event, cmds);
454 static const char * const cmds[] = {
457 NSCMD_SELECTLINENEXT,
462 handle_arrow_key(This, key_event, cmds);
465 case DOM_VK_DELETE: {
466 static const char * const cmds[] = {
467 NSCMD_DELETECHARFORWARD,
468 NSCMD_DELETEWORDFORWARD,
473 handle_arrow_key(This, key_event, cmds);
477 static const char * const cmds[] = {
480 NSCMD_SELECTBEGINLINE,
485 handle_arrow_key(This, key_event, cmds);
489 static const char * const cmds[] = {
497 handle_arrow_key(This, key_event, cmds);
502 nsIDOMKeyEvent_Release(key_event);
505 void handle_edit_load(HTMLDocument *This)
507 get_editor_controller(This->nscontainer);
510 static void set_ns_fontname(NSContainer *This, const char *fontname)
512 nsICommandParams *nsparam = create_nscommand_params();
514 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
515 do_ns_command(This, NSCMD_FONTFACE, nsparam);
516 nsICommandParams_Release(nsparam);
519 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
521 TRACE("(%p)->(%p %p)\n", This, in, out);
523 if(This->nscontainer)
524 do_ns_editor_command(This->nscontainer, NSCMD_DELETECHARFORWARD);
526 update_doc(This, UPDATE_UI);
530 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
532 TRACE("(%p)->(%p %p)\n", This, in, out);
534 if(!This->nscontainer) {
535 update_doc(This, UPDATE_UI);
543 if(V_VT(in) != VT_BSTR) {
544 FIXME("Unsupported vt=%d\n", V_VT(out));
548 TRACE("%s\n", debugstr_w(V_BSTR(in)));
550 len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, NULL, 0, NULL, NULL);
551 stra = mshtml_alloc(len);
552 WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, stra, -1, NULL, NULL);
554 set_ns_fontname(This->nscontainer, stra);
558 update_doc(This, UPDATE_UI);
562 nsICommandParams *nsparam;
568 nsparam = create_nscommand_params();
570 nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
574 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
575 nsICommandParams_Release(nsparam);
577 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
578 strw = mshtml_alloc(len*sizeof(WCHAR));
579 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, -1);
583 V_BSTR(out) = SysAllocString(strw);
590 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
592 TRACE("(%p)->(%p %p)\n", This, in, out);
595 if(V_VT(in) == VT_I4) {
596 nsICommandParams *nsparam = create_nscommand_params();
599 sprintf(color_str, "#%02x%02x%02x",
600 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
602 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
603 do_ns_command(This->nscontainer, NSCMD_FONTCOLOR, nsparam);
605 nsICommandParams_Release(nsparam);
607 FIXME("unsupported in vt=%d\n", V_VT(in));
610 update_doc(This, UPDATE_UI);
614 FIXME("unsupported out\n");
621 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
623 TRACE("(%p)->(%p %p)\n", This, in, out);
628 get_font_size(This, val);
630 V_I4(out) = strtolW(val, NULL, 10);
637 static const WCHAR format[] = {'%','d',0};
638 wsprintfW(size, format, V_I4(in));
639 set_font_size(This, size);
643 set_font_size(This, V_BSTR(in));
646 FIXME("unsupported vt %d\n", V_VT(in));
649 update_doc(This, UPDATE_UI);
655 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
658 FIXME("(%p)->(%p %p)\n", This, in, out);
662 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
664 TRACE("(%p)\n", This);
667 FIXME("unsupported args\n");
669 if(This->nscontainer)
670 do_ns_command(This->nscontainer, NSCMD_SELECTALL, NULL);
672 update_doc(This, UPDATE_UI);
676 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
678 TRACE("(%p)\n", This);
681 FIXME("unsupported args\n");
683 if(This->nscontainer)
684 do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
686 update_doc(This, UPDATE_UI);
690 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
692 TRACE("(%p)\n", This);
695 FIXME("unsupported args\n");
697 if(This->nscontainer)
698 do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
700 update_doc(This, UPDATE_UI);
704 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
707 case IDM_JUSTIFYCENTER:
708 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
709 cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
711 case IDM_JUSTIFYLEFT:
712 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
713 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
714 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
715 cmd->cmdf = OLECMDF_SUPPORTED;
717 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
719 case IDM_JUSTIFYRIGHT:
720 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
721 cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
728 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
730 TRACE("(%p)\n", This);
733 FIXME("unsupported args\n");
735 set_ns_align(This, NSALIGN_CENTER);
736 update_doc(This, UPDATE_UI);
740 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
742 TRACE("(%p)\n", This);
745 FIXME("unsupported args\n");
747 set_ns_align(This, NSALIGN_LEFT);
748 update_doc(This, UPDATE_UI);
752 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
754 TRACE("(%p)\n", This);
757 FIXME("unsupported args\n");
759 set_ns_align(This, NSALIGN_RIGHT);
760 update_doc(This, UPDATE_UI);
764 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
766 TRACE("(%p)\n", This);
769 FIXME("unsupported args\n");
771 if(This->nscontainer)
772 do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
774 update_doc(This, UPDATE_UI);
778 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
780 TRACE("(%p)\n", This);
783 FIXME("unsupported args\n");
785 if(This->nscontainer)
786 do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
788 update_doc(This, UPDATE_UI);
792 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
794 TRACE("(%p)\n", This);
797 FIXME("unsupported args\n");
799 if(This->nscontainer)
800 do_ns_command(This->nscontainer, NSCMD_OL, NULL);
802 update_doc(This, UPDATE_UI);
806 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
808 TRACE("(%p)\n", This);
811 FIXME("unsupported args\n");
813 if(This->nscontainer)
814 do_ns_command(This->nscontainer, NSCMD_UL, NULL);
816 update_doc(This, UPDATE_UI);
820 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
822 TRACE("(%p)\n", This);
825 FIXME("unsupported args\n");
827 if(This->nscontainer)
828 do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
830 update_doc(This, UPDATE_UI);
834 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
836 TRACE("(%p)\n", This);
839 FIXME("unsupported args\n");
841 if(This->nscontainer)
842 do_ns_command(This->nscontainer, 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->nscontainer)
926 do_ns_editor_command(This->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->nscontainer)
937 do_ns_editor_command(This->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->nscontainer)
948 do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
952 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
956 TRACE("CGID_MSHTML: IDM_DELETE\n");
957 cmd->cmdf = query_ns_edit_status(This, NULL);
960 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
961 cmd->cmdf = query_ns_edit_status(This, NULL);
964 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
965 cmd->cmdf = query_ns_edit_status(This, NULL);
968 TRACE("CGID_MSHTML: IDM_BOLD\n");
969 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
972 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
973 cmd->cmdf = query_ns_edit_status(This, NULL);
976 TRACE("CGID_MSHTML: IDM_ITALIC\n");
977 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
980 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
981 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
983 case IDM_HORIZONTALLINE:
984 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
985 cmd->cmdf = query_ns_edit_status(This, NULL);
988 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
989 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
991 case IDM_UNORDERLIST:
992 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
993 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
996 TRACE("CGID_MSHTML: IDM_INDENT\n");
997 cmd->cmdf = query_ns_edit_status(This, NULL);
1000 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1001 cmd->cmdf = query_ns_edit_status(This, NULL);
1004 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1005 cmd->cmdf = query_ns_edit_status(This, NULL);
1012 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1014 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1020 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1021 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1022 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1023 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1024 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1025 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1027 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1028 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1031 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1033 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1034 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1035 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1036 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1037 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1038 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1039 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1040 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1042 /* force the updating of the URL edit box */
1043 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1046 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1047 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1054 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1055 EndDialog(hwnd, wparam);
1057 case MAKEWPARAM(IDOK, BN_CLICKED):
1059 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1060 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1061 INT len = GetWindowTextLengthW(hwndURL);
1062 *url = SysAllocStringLen(NULL, len + 1);
1063 GetWindowTextW(hwndURL, *url, len + 1);
1064 EndDialog(hwnd, wparam);
1067 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1069 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1075 static const WCHAR wszSlashSlash[] = {'/','/'};
1077 /* get string of currently selected hyperlink type */
1078 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1079 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1080 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1081 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1083 if (!strcmpW(type, wszOther))
1086 /* get current URL */
1087 len = GetWindowTextLengthW(hwndURL);
1088 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1089 GetWindowTextW(hwndURL, url, len + 1);
1091 /* strip off old protocol */
1092 p = strchrW(url, ':');
1093 if (p && p[1] == '/' && p[2] == '/')
1096 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1098 /* add new protocol */
1101 memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1102 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1105 SetWindowTextW(hwndURL, url);
1107 HeapFree(GetProcessHeap(), 0, url);
1108 HeapFree(GetProcessHeap(), 0, type);
1114 EndDialog(hwnd, IDCANCEL);
1121 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1126 PRBool insert_link_at_caret;
1127 nsISelection *nsselection;
1129 FIXME("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1131 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1133 if (!in || V_VT(in) != VT_BSTR)
1135 WARN("invalid arg\n");
1136 return E_INVALIDARG;
1142 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1144 return OLECMDERR_E_CANCELED;
1147 nsselection = get_ns_selection(This);
1151 nsAString_Init(&ns_url, url);
1153 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1155 if (insert_link_at_caret)
1157 static const WCHAR wszA[] = {'a',0};
1158 static const WCHAR wszHref[] = {'h','r','e','f',0};
1159 nsIHTMLEditor *html_editor;
1160 nsIDOMDocument *nsdoc;
1161 nsIDOMNode *text_node;
1162 nsIDOMElement *anchor_elem;
1163 nsIDOMNode *unused_node;
1168 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
1169 if(NS_FAILED(nsres))
1172 nsAString_Init(&a_str, wszA);
1173 nsAString_Init(&href_str, wszHref);
1175 /* create an element for the link */
1176 nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
1177 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1179 nsAString_Finish(&href_str);
1180 nsAString_Finish(&a_str);
1182 /* create an element with text of URL */
1183 nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
1185 /* wrap the <a> tags around the text element */
1186 nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1187 nsIDOMNode_Release(text_node);
1188 nsIDOMNode_Release(unused_node);
1190 nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1193 /* add them to the document at the caret position */
1194 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
1195 nsIHTMLEditor_Release(html_editor);
1198 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1200 nsIDOMElement_Release(anchor_elem);
1201 nsIDOMDocument_Release(nsdoc);
1205 nsICommandParams *nsparam = create_nscommand_params();
1207 nsICommandParams_SetStringValue(nsparam, NSSTATE_ATTRIBUTE, &ns_url);
1208 do_ns_command(This->nscontainer, NSCMD_INSERTLINKNOUI, nsparam);
1209 nsICommandParams_Release(nsparam);
1212 nsAString_Finish(&ns_url);
1214 nsISelection_Release(nsselection);
1216 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1222 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1224 TRACE("(%p)->(%p)\n", This, cmd);
1226 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1230 const cmdtable_t editmode_cmds[] = {
1231 {IDM_DELETE, query_edit_status, exec_delete},
1232 {IDM_FONTNAME, query_edit_status, exec_fontname},
1233 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1234 {IDM_SELECTALL, query_selall_status , exec_selectall},
1235 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1236 {IDM_BOLD, query_edit_status, exec_bold},
1237 {IDM_ITALIC, query_edit_status, exec_italic},
1238 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1239 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1240 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1241 {IDM_FONT, NULL, exec_font},
1242 {IDM_UNDERLINE, query_edit_status, exec_underline},
1243 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1244 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1245 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1246 {IDM_INDENT, query_edit_status, exec_indent},
1247 {IDM_OUTDENT, query_edit_status, exec_outdent},
1248 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1249 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1253 void init_editor(HTMLDocument *This)
1255 update_doc(This, UPDATE_UI);
1257 if(!This->nscontainer)
1260 set_ns_fontname(This->nscontainer, "Times New Roman");
1263 HRESULT editor_is_dirty(HTMLDocument *This)
1267 if(!This->nscontainer || !This->nscontainer->editor)
1270 nsIEditor_GetDocumentModified(This->nscontainer->editor, &modified);
1272 return modified ? S_OK : S_FALSE;