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_command(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
104 nsICommandManager *cmdmgr;
105 nsIInterfaceRequestor *iface_req;
108 TRACE("(%p)\n", This);
110 nsres = nsIWebBrowser_QueryInterface(This->webbrowser,
111 &IID_nsIInterfaceRequestor, (void**)&iface_req);
112 if(NS_FAILED(nsres)) {
113 ERR("Could not get nsIInterfaceRequestor: %08x\n", nsres);
117 nsres = nsIInterfaceRequestor_GetInterface(iface_req, &IID_nsICommandManager,
119 nsIInterfaceRequestor_Release(iface_req);
120 if(NS_FAILED(nsres)) {
121 ERR("Could not get nsICommandManager: %08x\n", nsres);
125 nsres = nsICommandManager_DoCommand(cmdmgr, cmd, nsparam, NULL);
127 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
129 nsICommandManager_Release(cmdmgr);
132 static void do_ns_editor_command(NSContainer *This, const char *cmd)
136 if(!This->editor_controller)
139 nsres = nsIController_DoCommand(This->editor_controller, cmd);
141 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
144 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
146 nsICommandManager *cmdmgr;
147 nsIInterfaceRequestor *iface_req;
150 nsres = nsIWebBrowser_QueryInterface(This->webbrowser,
151 &IID_nsIInterfaceRequestor, (void**)&iface_req);
152 if(NS_FAILED(nsres)) {
153 ERR("Could not get nsIInterfaceRequestor: %08x\n", nsres);
157 nsres = nsIInterfaceRequestor_GetInterface(iface_req, &IID_nsICommandManager,
159 nsIInterfaceRequestor_Release(iface_req);
160 if(NS_FAILED(nsres)) {
161 ERR("Could not get nsICommandManager: %08x\n", nsres);
165 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, NULL, nsparam);
167 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
169 nsICommandManager_Release(cmdmgr);
173 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
175 nsICommandParams *nsparam;
178 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
179 return OLECMDF_SUPPORTED;
181 if(This->nscontainer && nscmd) {
182 nsparam = create_nscommand_params();
183 get_ns_command_state(This->nscontainer, nscmd, nsparam);
185 nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
187 nsICommandParams_Release(nsparam);
190 return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
193 static void set_ns_align(HTMLDocument *This, const char *align_str)
195 nsICommandParams *nsparam;
197 if(!This->nscontainer)
200 nsparam = create_nscommand_params();
201 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
203 do_ns_command(This->nscontainer, NSCMD_ALIGN, nsparam);
205 nsICommandParams_Release(nsparam);
208 static DWORD query_align_status(HTMLDocument *This, const char *align_str)
210 nsICommandParams *nsparam;
213 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
214 return OLECMDF_SUPPORTED;
216 if(This->nscontainer) {
217 nsparam = create_nscommand_params();
218 get_ns_command_state(This->nscontainer, NSCMD_ALIGN, nsparam);
220 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &align);
222 nsICommandParams_Release(nsparam);
225 return OLECMDF_SUPPORTED | OLECMDF_ENABLED
226 | (align && !strcmp(align_str, align) ? OLECMDF_LATCHED : 0);
230 static nsISelection *get_ns_selection(HTMLDocument *This)
232 nsIDOMWindow *dom_window;
233 nsISelection *nsselection = NULL;
236 if(!This->nscontainer)
239 nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
243 nsIDOMWindow_GetSelection(dom_window, &nsselection);
244 nsIDOMWindow_Release(dom_window);
250 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
253 PRUint32 child_cnt, i;
254 nsIDOMNode *child_node;
255 nsIDOMNodeList *node_list;
258 nsIDOMElement_HasChildNodes(elem, &has_children);
262 nsIDOMElement_GetChildNodes(elem, &node_list);
263 nsIDOMNodeList_GetLength(node_list, &child_cnt);
265 for(i=0; i<child_cnt; i++) {
266 nsIDOMNodeList_Item(node_list, i, &child_node);
268 nsIDOMNode_GetNodeType(child_node, &node_type);
269 if(node_type == ELEMENT_NODE) {
270 nsIDOMElement *child_elem;
272 const PRUnichar *ctag;
274 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
276 nsAString_Init(&tag_str, NULL);
277 nsIDOMElement_GetTagName(child_elem, &tag_str);
278 nsAString_GetData(&tag_str, &ctag, NULL);
280 if(!strcmpiW(ctag, tag))
281 /* FIXME: remove node if there are no more attributes */
282 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
284 nsAString_Finish(&tag_str);
286 remove_child_attr(child_elem, tag, attr_str);
288 nsIDOMNode_Release(child_elem);
291 nsIDOMNode_Release(child_node);
294 nsIDOMNodeList_Release(node_list);
297 static void get_font_size(HTMLDocument *This, WCHAR *ret)
299 nsISelection *nsselection = get_ns_selection(This);
300 nsIDOMElement *elem = NULL;
301 nsIDOMNode *node = NULL, *tmp_node;
312 nsISelection_GetFocusNode(nsselection, &node);
313 nsISelection_Release(nsselection);
316 nsres = nsIDOMNode_GetNodeType(node, &node_type);
317 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
320 if(node_type == ELEMENT_NODE) {
321 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
323 nsAString_Init(&tag_str, NULL);
324 nsIDOMElement_GetTagName(elem, &tag_str);
325 nsAString_GetData(&tag_str, &tag, NULL);
327 if(!strcmpiW(tag, wszFont)) {
328 nsAString size_str, val_str;
331 TRACE("found font tag %p\n", elem);
333 nsAString_Init(&size_str, wszSize);
334 nsAString_Init(&val_str, NULL);
336 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
337 nsAString_GetData(&val_str, &val, NULL);
340 TRACE("found size %s\n", debugstr_w(val));
344 nsAString_Finish(&size_str);
345 nsAString_Finish(&val_str);
348 nsAString_Finish(&tag_str);
350 nsIDOMElement_Release(elem);
357 nsIDOMNode_GetParentNode(node, &node);
358 nsIDOMNode_Release(tmp_node);
362 nsIDOMNode_Release(node);
365 static void set_font_size(HTMLDocument *This, LPCWSTR size)
367 nsISelection *nsselection;
369 nsIDOMDocument *nsdoc;
372 PRInt32 range_cnt = 0;
378 nsselection = get_ns_selection(This);
383 nsISelection_GetRangeCount(nsselection, &range_cnt);
385 FIXME("range_cnt %d not supprted\n", range_cnt);
387 nsISelection_Release(nsselection);
392 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
396 nsAString_Init(&font_str, wszFont);
397 nsAString_Init(&size_str, wszSize);
398 nsAString_Init(&val_str, size);
400 nsIDOMDocument_CreateElement(nsdoc, &font_str, &elem);
401 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
403 nsISelection_GetRangeAt(nsselection, 0, &range);
404 nsISelection_GetIsCollapsed(nsselection, &collapsed);
405 nsISelection_RemoveAllRanges(nsselection);
407 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
410 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
412 /* Remove all size attrbutes from the range */
413 remove_child_attr(elem, wszFont, &size_str);
414 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
417 nsIDOMRange_Release(range);
418 nsIDOMElement_Release(elem);
420 nsAString_Finish(&font_str);
421 nsAString_Finish(&size_str);
422 nsAString_Finish(&val_str);
424 nsISelection_Release(nsselection);
425 nsIDOMDocument_Release(nsdoc);
428 static BOOL is_visible_text_node(nsIDOMNode *node)
430 nsIDOMCharacterData *char_data;
435 nsIDOMNode_QueryInterface(node, &IID_nsIDOMCharacterData, (void**)&char_data);
437 nsIDOMCharacterData_GetLength(char_data, &len);
439 nsAString_Init(&data_str, NULL);
440 nsIDOMCharacterData_GetData(char_data, &data_str);
441 nsAString_GetData(&data_str, &data, NULL);
445 for(ptr=data+1; ptr && isspaceW(*ptr); ptr++)
449 nsAString_Finish(&data_str);
451 nsIDOMCharacterData_Release(char_data);
456 static nsIDOMNode *get_child_text_node(nsIDOMNode *node, BOOL first)
458 nsIDOMNode *iter, *iter2;
461 nsIDOMNode_GetFirstChild(node, &iter);
463 nsIDOMNode_GetLastChild(node, &iter);
468 nsIDOMNode_GetNodeType(iter, &node_type);
471 if(is_visible_text_node(iter))
474 iter2 = get_child_text_node(iter, first);
476 nsIDOMNode_Release(iter);
482 nsIDOMNode_GetNextSibling(iter, &iter2);
484 nsIDOMNode_GetPreviousSibling(iter, &iter2);
486 nsIDOMNode_Release(iter);
493 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
498 nsIDOMKeyEvent_GetCtrlKey(event, &b);
502 nsIDOMKeyEvent_GetShiftKey(event, &b);
507 do_ns_editor_command(This->nscontainer, cmds[i]);
509 nsIDOMKeyEvent_PreventDefault(event);
512 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
514 nsIDOMKeyEvent *key_event;
517 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
519 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
523 static const char * const cmds[] = {
526 NSCMD_SELECTCHARPREVIOUS,
527 NSCMD_SELECTWORDPREVIOUS
531 handle_arrow_key(This, key_event, cmds);
535 static const char * const cmds[] = {
538 NSCMD_SELECTCHARNEXT,
543 handle_arrow_key(This, key_event, cmds);
547 static const char * const cmds[] = {
550 NSCMD_SELECTLINEPREVIOUS,
555 handle_arrow_key(This, key_event, cmds);
559 static const char * const cmds[] = {
562 NSCMD_SELECTLINENEXT,
567 handle_arrow_key(This, key_event, cmds);
570 case DOM_VK_DELETE: {
571 static const char * const cmds[] = {
572 NSCMD_DELETECHARFORWARD,
573 NSCMD_DELETEWORDFORWARD,
578 handle_arrow_key(This, key_event, cmds);
582 static const char * const cmds[] = {
585 NSCMD_SELECTBEGINLINE,
590 handle_arrow_key(This, key_event, cmds);
594 static const char * const cmds[] = {
602 handle_arrow_key(This, key_event, cmds);
607 nsIDOMKeyEvent_Release(key_event);
610 void handle_edit_load(HTMLDocument *This)
612 get_editor_controller(This->nscontainer);
614 if(This->ui_active) {
615 OLECHAR wszHTMLDocument[30];
619 IOleInPlaceUIWindow_SetActiveObject(This->ip_window, NULL, NULL);
621 IDocHostUIHandler_HideUI(This->hostui);
624 IDocHostUIHandler_ShowUI(This->hostui, DOCHOSTUITYPE_AUTHOR, ACTOBJ(This), CMDTARGET(This),
625 This->frame, This->ip_window);
627 LoadStringW(hInst, IDS_HTMLDOCUMENT, wszHTMLDocument,
628 sizeof(wszHTMLDocument)/sizeof(WCHAR));
631 IOleInPlaceUIWindow_SetActiveObject(This->ip_window, ACTOBJ(This), wszHTMLDocument);
633 memset(&rcBorderWidths, 0, sizeof(rcBorderWidths));
634 IOleInPlaceFrame_SetBorderSpace(This->frame, &rcBorderWidths);
638 static void set_ns_fontname(NSContainer *This, const char *fontname)
640 nsICommandParams *nsparam = create_nscommand_params();
642 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
643 do_ns_command(This, NSCMD_FONTFACE, nsparam);
644 nsICommandParams_Release(nsparam);
647 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
649 TRACE("(%p)->(%p %p)\n", This, in, out);
651 if(This->nscontainer)
652 do_ns_editor_command(This->nscontainer, NSCMD_DELETECHARFORWARD);
654 update_doc(This, UPDATE_UI);
658 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
660 TRACE("(%p)->(%p %p)\n", This, in, out);
662 if(!This->nscontainer) {
663 update_doc(This, UPDATE_UI);
671 if(V_VT(in) != VT_BSTR) {
672 FIXME("Unsupported vt=%d\n", V_VT(out));
676 TRACE("%s\n", debugstr_w(V_BSTR(in)));
678 len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, NULL, 0, NULL, NULL);
679 stra = mshtml_alloc(len);
680 WideCharToMultiByte(CP_ACP, 0, V_BSTR(in), -1, stra, -1, NULL, NULL);
682 set_ns_fontname(This->nscontainer, stra);
686 update_doc(This, UPDATE_UI);
690 nsICommandParams *nsparam;
696 nsparam = create_nscommand_params();
698 nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
702 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
703 nsICommandParams_Release(nsparam);
705 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
706 strw = mshtml_alloc(len*sizeof(WCHAR));
707 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, -1);
711 V_BSTR(out) = SysAllocString(strw);
718 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
720 TRACE("(%p)->(%p %p)\n", This, in, out);
723 if(V_VT(in) == VT_I4) {
724 nsICommandParams *nsparam = create_nscommand_params();
727 sprintf(color_str, "#%02x%02x%02x",
728 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
730 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
731 do_ns_command(This->nscontainer, NSCMD_FONTCOLOR, nsparam);
733 nsICommandParams_Release(nsparam);
735 FIXME("unsupported in vt=%d\n", V_VT(in));
738 update_doc(This, UPDATE_UI);
742 FIXME("unsupported out\n");
749 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
751 TRACE("(%p)->(%p %p)\n", This, in, out);
756 get_font_size(This, val);
758 V_I4(out) = strtolW(val, NULL, 10);
765 static const WCHAR format[] = {'%','d',0};
766 wsprintfW(size, format, V_I4(in));
767 set_font_size(This, size);
771 set_font_size(This, V_BSTR(in));
774 FIXME("unsupported vt %d\n", V_VT(in));
777 update_doc(This, UPDATE_UI);
783 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
786 FIXME("(%p)->(%p %p)\n", This, in, out);
790 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
792 TRACE("(%p)\n", This);
795 FIXME("unsupported args\n");
797 if(This->nscontainer)
798 do_ns_command(This->nscontainer, NSCMD_SELECTALL, NULL);
800 update_doc(This, UPDATE_UI);
804 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
806 TRACE("(%p)\n", This);
809 FIXME("unsupported args\n");
811 if(This->nscontainer)
812 do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
814 update_doc(This, UPDATE_UI);
818 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
820 TRACE("(%p)\n", This);
823 FIXME("unsupported args\n");
825 if(This->nscontainer)
826 do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
828 update_doc(This, UPDATE_UI);
832 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
835 case IDM_JUSTIFYCENTER:
836 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
837 cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
839 case IDM_JUSTIFYLEFT:
840 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
841 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
842 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
843 cmd->cmdf = OLECMDF_SUPPORTED;
845 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
847 case IDM_JUSTIFYRIGHT:
848 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
849 cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
856 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
858 TRACE("(%p)\n", This);
861 FIXME("unsupported args\n");
863 set_ns_align(This, NSALIGN_CENTER);
864 update_doc(This, UPDATE_UI);
868 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
870 TRACE("(%p)\n", This);
873 FIXME("unsupported args\n");
875 set_ns_align(This, NSALIGN_LEFT);
876 update_doc(This, UPDATE_UI);
880 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
882 TRACE("(%p)\n", This);
885 FIXME("unsupported args\n");
887 set_ns_align(This, NSALIGN_RIGHT);
888 update_doc(This, UPDATE_UI);
892 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
894 TRACE("(%p)\n", This);
897 FIXME("unsupported args\n");
899 if(This->nscontainer)
900 do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
902 update_doc(This, UPDATE_UI);
906 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
908 TRACE("(%p)\n", This);
911 FIXME("unsupported args\n");
913 if(This->nscontainer)
914 do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
916 update_doc(This, UPDATE_UI);
920 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
922 TRACE("(%p)\n", This);
925 FIXME("unsupported args\n");
927 if(This->nscontainer)
928 do_ns_command(This->nscontainer, NSCMD_OL, NULL);
930 update_doc(This, UPDATE_UI);
934 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
936 TRACE("(%p)\n", This);
939 FIXME("unsupported args\n");
941 if(This->nscontainer)
942 do_ns_command(This->nscontainer, NSCMD_UL, NULL);
944 update_doc(This, UPDATE_UI);
948 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
950 TRACE("(%p)\n", This);
953 FIXME("unsupported args\n");
955 if(This->nscontainer)
956 do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
958 update_doc(This, UPDATE_UI);
962 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
964 TRACE("(%p)\n", This);
967 FIXME("unsupported args\n");
969 if(This->nscontainer)
970 do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
972 update_doc(This, UPDATE_UI);
976 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
980 if(out || !in || V_VT(in) != VT_BSTR) {
981 WARN("invalid arg\n");
985 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
987 update_doc(This, UPDATE_UI);
991 exec_bold(This, cmdexecopt, NULL, NULL);
992 ptr = strchrW(ptr, ',');
997 exec_italic(This, cmdexecopt, NULL, NULL);
998 ptr = strchrW(ptr, ',');
1003 exec_underline(This, cmdexecopt, NULL, NULL);
1004 ptr = strchrW(ptr, ',');
1008 if(isdigitW(*++ptr)) {
1012 V_I4(&v) = *ptr-'0';
1014 exec_fontsize(This, cmdexecopt, &v, NULL);
1016 ptr = strchrW(ptr, ',');
1021 FIXME("set font color\n");
1022 ptr = strchrW(ptr, ',');
1027 FIXME("set background color\n");
1028 ptr = strchrW(ptr, ',');
1037 V_BSTR(&v) = SysAllocString(ptr);
1039 exec_fontname(This, cmdexecopt, &v, NULL);
1041 SysFreeString(V_BSTR(&v));
1047 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1049 update_doc(This, UPDATE_UI);
1051 if(!This->nscontainer)
1054 do_ns_editor_command(This->nscontainer, NSCMD_COPY);
1058 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1060 update_doc(This, UPDATE_UI);
1062 if(!This->nscontainer)
1065 do_ns_editor_command(This->nscontainer, NSCMD_CUT);
1069 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1071 update_doc(This, UPDATE_UI);
1073 if(!This->nscontainer)
1076 do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
1080 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
1082 switch(cmd->cmdID) {
1084 TRACE("CGID_MSHTML: IDM_DELETE\n");
1085 cmd->cmdf = query_ns_edit_status(This, NULL);
1088 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
1089 cmd->cmdf = query_ns_edit_status(This, NULL);
1092 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
1093 cmd->cmdf = query_ns_edit_status(This, NULL);
1096 TRACE("CGID_MSHTML: IDM_BOLD\n");
1097 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
1100 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
1101 cmd->cmdf = query_ns_edit_status(This, NULL);
1104 TRACE("CGID_MSHTML: IDM_ITALIC\n");
1105 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
1108 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
1109 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
1111 case IDM_HORIZONTALLINE:
1112 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1113 cmd->cmdf = query_ns_edit_status(This, NULL);
1116 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1117 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1119 case IDM_UNORDERLIST:
1120 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1121 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1124 TRACE("CGID_MSHTML: IDM_INDENT\n");
1125 cmd->cmdf = query_ns_edit_status(This, NULL);
1128 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1129 cmd->cmdf = query_ns_edit_status(This, NULL);
1132 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1133 cmd->cmdf = query_ns_edit_status(This, NULL);
1140 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1142 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1148 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1149 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1150 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1151 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1152 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1153 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1155 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1156 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1159 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1161 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1162 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1163 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1164 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1165 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1166 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1167 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1168 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1170 /* force the updating of the URL edit box */
1171 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1174 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1175 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1182 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1183 EndDialog(hwnd, wparam);
1185 case MAKEWPARAM(IDOK, BN_CLICKED):
1187 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1188 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1189 INT len = GetWindowTextLengthW(hwndURL);
1190 *url = SysAllocStringLen(NULL, len + 1);
1191 GetWindowTextW(hwndURL, *url, len + 1);
1192 EndDialog(hwnd, wparam);
1195 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1197 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1203 static const WCHAR wszSlashSlash[] = {'/','/'};
1205 /* get string of currently selected hyperlink type */
1206 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1207 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1208 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1209 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1211 if (!strcmpW(type, wszOther))
1214 /* get current URL */
1215 len = GetWindowTextLengthW(hwndURL);
1216 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1217 GetWindowTextW(hwndURL, url, len + 1);
1219 /* strip off old protocol */
1220 p = strchrW(url, ':');
1221 if (p && p[1] == '/' && p[2] == '/')
1224 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1226 /* add new protocol */
1229 memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1230 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1233 SetWindowTextW(hwndURL, url);
1235 HeapFree(GetProcessHeap(), 0, url);
1236 HeapFree(GetProcessHeap(), 0, type);
1242 EndDialog(hwnd, IDCANCEL);
1249 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1254 PRBool insert_link_at_caret;
1255 nsISelection *nsselection;
1257 FIXME("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1259 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1261 if (!in || V_VT(in) != VT_BSTR)
1263 WARN("invalid arg\n");
1264 return E_INVALIDARG;
1270 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1272 return OLECMDERR_E_CANCELED;
1275 nsselection = get_ns_selection(This);
1279 nsAString_Init(&ns_url, url);
1281 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1283 if (insert_link_at_caret)
1285 static const WCHAR wszA[] = {'a',0};
1286 static const WCHAR wszHref[] = {'h','r','e','f',0};
1287 nsIHTMLEditor *html_editor;
1288 nsIDOMDocument *nsdoc;
1289 nsIDOMNode *text_node;
1290 nsIDOMElement *anchor_elem;
1291 nsIDOMNode *unused_node;
1296 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
1297 if(NS_FAILED(nsres))
1300 nsAString_Init(&a_str, wszA);
1301 nsAString_Init(&href_str, wszHref);
1303 /* create an element for the link */
1304 nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
1305 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1307 nsAString_Finish(&href_str);
1308 nsAString_Finish(&a_str);
1310 /* create an element with text of URL */
1311 nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
1313 /* wrap the <a> tags around the text element */
1314 nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1315 nsIDOMNode_Release(text_node);
1316 nsIDOMNode_Release(unused_node);
1318 nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1321 /* add them to the document at the caret position */
1322 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
1323 nsIHTMLEditor_Release(html_editor);
1326 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1328 nsIDOMElement_Release(anchor_elem);
1329 nsIDOMDocument_Release(nsdoc);
1333 nsICommandParams *nsparam = create_nscommand_params();
1335 nsICommandParams_SetStringValue(nsparam, NSSTATE_ATTRIBUTE, &ns_url);
1336 do_ns_command(This->nscontainer, NSCMD_INSERTLINKNOUI, nsparam);
1337 nsICommandParams_Release(nsparam);
1340 nsAString_Finish(&ns_url);
1342 nsISelection_Release(nsselection);
1344 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1350 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1352 TRACE("(%p)->(%p)\n", This, cmd);
1354 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1358 const cmdtable_t editmode_cmds[] = {
1359 {IDM_DELETE, query_edit_status, exec_delete},
1360 {IDM_FONTNAME, query_edit_status, exec_fontname},
1361 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1362 {IDM_SELECTALL, query_selall_status , exec_selectall},
1363 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1364 {IDM_BOLD, query_edit_status, exec_bold},
1365 {IDM_ITALIC, query_edit_status, exec_italic},
1366 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1367 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1368 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1369 {IDM_FONT, NULL, exec_font},
1370 {IDM_UNDERLINE, query_edit_status, exec_underline},
1371 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1372 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1373 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1374 {IDM_INDENT, query_edit_status, exec_indent},
1375 {IDM_OUTDENT, query_edit_status, exec_outdent},
1376 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1377 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1381 void init_editor(HTMLDocument *This)
1383 update_doc(This, UPDATE_UI);
1385 if(!This->nscontainer)
1388 set_ns_fontname(This->nscontainer, "Times New Roman");
1391 HRESULT editor_is_dirty(HTMLDocument *This)
1395 if(!This->nscontainer || !This->nscontainer->editor)
1398 nsIEditor_GetDocumentModified(This->nscontainer->editor, &modified);
1400 return modified ? S_OK : S_FALSE;