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 void set_dirty(HTMLDocument
*This
, VARIANT_BOOL dirty
)
106 if(This
->usermode
!= EDITMODE
|| !This
->nscontainer
|| !This
->nscontainer
->editor
)
110 nsres
= nsIEditor_IncrementModificationCount(This
->nscontainer
->editor
, 1);
112 ERR("IncrementModificationCount failed: %08x\n", nsres
);
114 nsres
= nsIEditor_ResetModificationCount(This
->nscontainer
->editor
);
116 ERR("ResetModificationCount failed: %08x\n", nsres
);
120 static void do_ns_editor_command(NSContainer
*This
, const char *cmd
)
124 if(!This
->editor_controller
)
127 nsres
= nsIController_DoCommand(This
->editor_controller
, cmd
);
129 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd
), nsres
);
132 static nsresult
get_ns_command_state(NSContainer
*This
, const char *cmd
, nsICommandParams
*nsparam
)
134 nsICommandManager
*cmdmgr
;
137 nsres
= get_nsinterface((nsISupports
*)This
->webbrowser
, &IID_nsICommandManager
, (void**)&cmdmgr
);
138 if(NS_FAILED(nsres
)) {
139 ERR("Could not get nsICommandManager: %08x\n", nsres
);
143 nsres
= nsICommandManager_GetCommandState(cmdmgr
, cmd
, NULL
, nsparam
);
145 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd
), nsres
);
147 nsICommandManager_Release(cmdmgr
);
151 static DWORD
query_ns_edit_status(HTMLDocument
*This
, const char *nscmd
)
153 nsICommandParams
*nsparam
;
156 if(This
->usermode
!= EDITMODE
|| This
->readystate
< READYSTATE_INTERACTIVE
)
157 return OLECMDF_SUPPORTED
;
159 if(This
->nscontainer
&& nscmd
) {
160 nsparam
= create_nscommand_params();
161 get_ns_command_state(This
->nscontainer
, nscmd
, nsparam
);
163 nsICommandParams_GetBooleanValue(nsparam
, NSSTATE_ALL
, &b
);
165 nsICommandParams_Release(nsparam
);
168 return OLECMDF_SUPPORTED
| OLECMDF_ENABLED
| (b
? OLECMDF_LATCHED
: 0);
171 static void set_ns_align(HTMLDocument
*This
, const char *align_str
)
173 nsICommandParams
*nsparam
;
175 if(!This
->nscontainer
)
178 nsparam
= create_nscommand_params();
179 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, align_str
);
181 do_ns_command(This
->nscontainer
, NSCMD_ALIGN
, nsparam
);
183 nsICommandParams_Release(nsparam
);
186 static DWORD
query_align_status(HTMLDocument
*This
, const char *align_str
)
188 nsICommandParams
*nsparam
;
191 if(This
->usermode
!= EDITMODE
|| This
->readystate
< READYSTATE_INTERACTIVE
)
192 return OLECMDF_SUPPORTED
;
194 if(This
->nscontainer
) {
195 nsparam
= create_nscommand_params();
196 get_ns_command_state(This
->nscontainer
, NSCMD_ALIGN
, nsparam
);
198 nsICommandParams_GetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, &align
);
200 nsICommandParams_Release(nsparam
);
203 return OLECMDF_SUPPORTED
| OLECMDF_ENABLED
204 | (align
&& !strcmp(align_str
, align
) ? OLECMDF_LATCHED
: 0);
208 static nsISelection
*get_ns_selection(HTMLDocument
*This
)
210 nsIDOMWindow
*dom_window
;
211 nsISelection
*nsselection
= NULL
;
214 if(!This
->nscontainer
)
217 nsres
= nsIWebBrowser_GetContentDOMWindow(This
->nscontainer
->webbrowser
, &dom_window
);
221 nsIDOMWindow_GetSelection(dom_window
, &nsselection
);
222 nsIDOMWindow_Release(dom_window
);
228 static void remove_child_attr(nsIDOMElement
*elem
, LPCWSTR tag
, nsAString
*attr_str
)
231 PRUint32 child_cnt
, i
;
232 nsIDOMNode
*child_node
;
233 nsIDOMNodeList
*node_list
;
236 nsIDOMElement_HasChildNodes(elem
, &has_children
);
240 nsIDOMElement_GetChildNodes(elem
, &node_list
);
241 nsIDOMNodeList_GetLength(node_list
, &child_cnt
);
243 for(i
=0; i
<child_cnt
; i
++) {
244 nsIDOMNodeList_Item(node_list
, i
, &child_node
);
246 nsIDOMNode_GetNodeType(child_node
, &node_type
);
247 if(node_type
== ELEMENT_NODE
) {
248 nsIDOMElement
*child_elem
;
250 const PRUnichar
*ctag
;
252 nsIDOMNode_QueryInterface(child_node
, &IID_nsIDOMElement
, (void**)&child_elem
);
254 nsAString_Init(&tag_str
, NULL
);
255 nsIDOMElement_GetTagName(child_elem
, &tag_str
);
256 nsAString_GetData(&tag_str
, &ctag
);
258 if(!strcmpiW(ctag
, tag
))
259 /* FIXME: remove node if there are no more attributes */
260 nsIDOMElement_RemoveAttribute(child_elem
, attr_str
);
262 nsAString_Finish(&tag_str
);
264 remove_child_attr(child_elem
, tag
, attr_str
);
266 nsIDOMNode_Release(child_elem
);
269 nsIDOMNode_Release(child_node
);
272 nsIDOMNodeList_Release(node_list
);
275 static void get_font_size(HTMLDocument
*This
, WCHAR
*ret
)
277 nsISelection
*nsselection
= get_ns_selection(This
);
278 nsIDOMElement
*elem
= NULL
;
279 nsIDOMNode
*node
= NULL
, *tmp_node
;
290 nsISelection_GetFocusNode(nsselection
, &node
);
291 nsISelection_Release(nsselection
);
294 nsres
= nsIDOMNode_GetNodeType(node
, &node_type
);
295 if(NS_FAILED(nsres
) || node_type
== DOCUMENT_NODE
)
298 if(node_type
== ELEMENT_NODE
) {
299 nsIDOMNode_QueryInterface(node
, &IID_nsIDOMElement
, (void**)&elem
);
301 nsAString_Init(&tag_str
, NULL
);
302 nsIDOMElement_GetTagName(elem
, &tag_str
);
303 nsAString_GetData(&tag_str
, &tag
);
305 if(!strcmpiW(tag
, wszFont
)) {
306 nsAString size_str
, val_str
;
309 TRACE("found font tag %p\n", elem
);
311 nsAString_Init(&size_str
, wszSize
);
312 nsAString_Init(&val_str
, NULL
);
314 nsIDOMElement_GetAttribute(elem
, &size_str
, &val_str
);
315 nsAString_GetData(&val_str
, &val
);
318 TRACE("found size %s\n", debugstr_w(val
));
322 nsAString_Finish(&size_str
);
323 nsAString_Finish(&val_str
);
326 nsAString_Finish(&tag_str
);
328 nsIDOMElement_Release(elem
);
335 nsIDOMNode_GetParentNode(node
, &node
);
336 nsIDOMNode_Release(tmp_node
);
340 nsIDOMNode_Release(node
);
343 static void set_font_size(HTMLDocument
*This
, LPCWSTR size
)
345 nsISelection
*nsselection
;
347 nsIDOMDocument
*nsdoc
;
350 PRInt32 range_cnt
= 0;
356 nsselection
= get_ns_selection(This
);
361 nsISelection_GetRangeCount(nsselection
, &range_cnt
);
363 FIXME("range_cnt %d not supprted\n", range_cnt
);
365 nsISelection_Release(nsselection
);
370 nsres
= nsIWebNavigation_GetDocument(This
->nscontainer
->navigation
, &nsdoc
);
374 nsAString_Init(&font_str
, wszFont
);
375 nsAString_Init(&size_str
, wszSize
);
376 nsAString_Init(&val_str
, size
);
378 nsIDOMDocument_CreateElement(nsdoc
, &font_str
, &elem
);
379 nsIDOMElement_SetAttribute(elem
, &size_str
, &val_str
);
381 nsISelection_GetRangeAt(nsselection
, 0, &range
);
382 nsISelection_GetIsCollapsed(nsselection
, &collapsed
);
383 nsISelection_RemoveAllRanges(nsselection
);
385 nsIDOMRange_SurroundContents(range
, (nsIDOMNode
*)elem
);
388 nsISelection_Collapse(nsselection
, (nsIDOMNode
*)elem
, 0);
390 /* Remove all size attrbutes from the range */
391 remove_child_attr(elem
, wszFont
, &size_str
);
392 nsISelection_SelectAllChildren(nsselection
, (nsIDOMNode
*)elem
);
395 nsIDOMRange_Release(range
);
396 nsIDOMElement_Release(elem
);
398 nsAString_Finish(&font_str
);
399 nsAString_Finish(&size_str
);
400 nsAString_Finish(&val_str
);
402 nsISelection_Release(nsselection
);
403 nsIDOMDocument_Release(nsdoc
);
405 set_dirty(This
, VARIANT_TRUE
);
408 static void handle_arrow_key(HTMLDocument
*This
, nsIDOMKeyEvent
*event
, const char * const cmds
[4])
413 nsIDOMKeyEvent_GetCtrlKey(event
, &b
);
417 nsIDOMKeyEvent_GetShiftKey(event
, &b
);
422 do_ns_editor_command(This
->nscontainer
, cmds
[i
]);
424 nsIDOMKeyEvent_PreventDefault(event
);
427 void handle_edit_event(HTMLDocument
*This
, nsIDOMEvent
*event
)
429 nsIDOMKeyEvent
*key_event
;
432 nsIDOMEvent_QueryInterface(event
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
434 nsIDOMKeyEvent_GetKeyCode(key_event
, &code
);
438 static const char * const cmds
[] = {
441 NSCMD_SELECTCHARPREVIOUS
,
442 NSCMD_SELECTWORDPREVIOUS
446 handle_arrow_key(This
, key_event
, cmds
);
450 static const char * const cmds
[] = {
453 NSCMD_SELECTCHARNEXT
,
458 handle_arrow_key(This
, key_event
, cmds
);
462 static const char * const cmds
[] = {
465 NSCMD_SELECTLINEPREVIOUS
,
470 handle_arrow_key(This
, key_event
, cmds
);
474 static const char * const cmds
[] = {
477 NSCMD_SELECTLINENEXT
,
482 handle_arrow_key(This
, key_event
, cmds
);
485 case DOM_VK_DELETE
: {
486 static const char * const cmds
[] = {
487 NSCMD_DELETECHARFORWARD
,
488 NSCMD_DELETEWORDFORWARD
,
493 handle_arrow_key(This
, key_event
, cmds
);
497 static const char * const cmds
[] = {
500 NSCMD_SELECTBEGINLINE
,
505 handle_arrow_key(This
, key_event
, cmds
);
509 static const char * const cmds
[] = {
517 handle_arrow_key(This
, key_event
, cmds
);
522 nsIDOMKeyEvent_Release(key_event
);
525 void handle_edit_load(HTMLDocument
*This
)
527 This
->nscontainer
->reset_focus
= GetFocus();
528 get_editor_controller(This
->nscontainer
);
531 static void set_ns_fontname(NSContainer
*This
, const char *fontname
)
533 nsICommandParams
*nsparam
= create_nscommand_params();
535 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, fontname
);
536 do_ns_command(This
, NSCMD_FONTFACE
, nsparam
);
537 nsICommandParams_Release(nsparam
);
540 static HRESULT
exec_delete(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
542 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
544 if(This
->nscontainer
)
545 do_ns_editor_command(This
->nscontainer
, NSCMD_DELETECHARFORWARD
);
547 update_doc(This
, UPDATE_UI
);
551 static HRESULT
exec_fontname(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
553 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
555 if(!This
->nscontainer
) {
556 update_doc(This
, UPDATE_UI
);
563 if(V_VT(in
) != VT_BSTR
) {
564 FIXME("Unsupported vt=%d\n", V_VT(out
));
568 TRACE("%s\n", debugstr_w(V_BSTR(in
)));
570 stra
= heap_strdupWtoA(V_BSTR(in
));
571 set_ns_fontname(This
->nscontainer
, stra
);
574 update_doc(This
, UPDATE_UI
);
578 nsICommandParams
*nsparam
;
587 nsparam
= create_nscommand_params();
589 nsres
= get_ns_command_state(This
->nscontainer
, NSCMD_FONTFACE
, nsparam
);
593 nsICommandParams_GetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, &stra
);
594 nsICommandParams_Release(nsparam
);
596 len
= MultiByteToWideChar(CP_ACP
, 0, stra
, -1, NULL
, 0);
597 strw
= heap_alloc(len
*sizeof(WCHAR
));
598 MultiByteToWideChar(CP_ACP
, 0, stra
, -1, strw
, -1);
601 V_BSTR(out
) = SysAllocString(strw
);
608 static HRESULT
exec_forecolor(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
610 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
613 if(V_VT(in
) == VT_I4
) {
614 nsICommandParams
*nsparam
= create_nscommand_params();
617 sprintf(color_str
, "#%02x%02x%02x",
618 V_I4(in
)&0xff, (V_I4(in
)>>8)&0xff, (V_I4(in
)>>16)&0xff);
620 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, color_str
);
621 do_ns_command(This
->nscontainer
, NSCMD_FONTCOLOR
, nsparam
);
623 nsICommandParams_Release(nsparam
);
625 FIXME("unsupported in vt=%d\n", V_VT(in
));
628 update_doc(This
, UPDATE_UI
);
632 FIXME("unsupported out\n");
639 static HRESULT
exec_fontsize(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
641 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
646 get_font_size(This
, val
);
648 V_I4(out
) = strtolW(val
, NULL
, 10);
655 static const WCHAR format
[] = {'%','d',0};
656 wsprintfW(size
, format
, V_I4(in
));
657 set_font_size(This
, size
);
661 set_font_size(This
, V_BSTR(in
));
664 FIXME("unsupported vt %d\n", V_VT(in
));
667 update_doc(This
, UPDATE_UI
);
673 static HRESULT
exec_font(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
676 FIXME("(%p)->(%p %p)\n", This
, in
, out
);
680 static HRESULT
exec_selectall(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
682 TRACE("(%p)\n", This
);
685 FIXME("unsupported args\n");
687 if(This
->nscontainer
)
688 do_ns_command(This
->nscontainer
, NSCMD_SELECTALL
, NULL
);
690 update_doc(This
, UPDATE_UI
);
694 static HRESULT
exec_bold(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
696 TRACE("(%p)\n", This
);
699 FIXME("unsupported args\n");
701 if(This
->nscontainer
)
702 do_ns_command(This
->nscontainer
, NSCMD_BOLD
, NULL
);
704 update_doc(This
, UPDATE_UI
);
708 static HRESULT
exec_italic(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
710 TRACE("(%p)\n", This
);
713 FIXME("unsupported args\n");
715 if(This
->nscontainer
)
716 do_ns_command(This
->nscontainer
, NSCMD_ITALIC
, NULL
);
718 update_doc(This
, UPDATE_UI
);
722 static HRESULT
query_justify(HTMLDocument
*This
, OLECMD
*cmd
)
725 case IDM_JUSTIFYCENTER
:
726 TRACE("(%p) IDM_JUSTIFYCENTER\n", This
);
727 cmd
->cmdf
= query_align_status(This
, NSALIGN_CENTER
);
729 case IDM_JUSTIFYLEFT
:
730 TRACE("(%p) IDM_JUSTIFYLEFT\n", This
);
731 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
732 if(This
->usermode
!= EDITMODE
|| This
->readystate
< READYSTATE_INTERACTIVE
)
733 cmd
->cmdf
= OLECMDF_SUPPORTED
;
735 cmd
->cmdf
= OLECMDF_SUPPORTED
| OLECMDF_ENABLED
;
737 case IDM_JUSTIFYRIGHT
:
738 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This
);
739 cmd
->cmdf
= query_align_status(This
, NSALIGN_RIGHT
);
746 static HRESULT
exec_justifycenter(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
748 TRACE("(%p)\n", This
);
751 FIXME("unsupported args\n");
753 set_ns_align(This
, NSALIGN_CENTER
);
754 update_doc(This
, UPDATE_UI
);
758 static HRESULT
exec_justifyleft(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
760 TRACE("(%p)\n", This
);
763 FIXME("unsupported args\n");
765 set_ns_align(This
, NSALIGN_LEFT
);
766 update_doc(This
, UPDATE_UI
);
770 static HRESULT
exec_justifyright(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
772 TRACE("(%p)\n", This
);
775 FIXME("unsupported args\n");
777 set_ns_align(This
, NSALIGN_RIGHT
);
778 update_doc(This
, UPDATE_UI
);
782 static HRESULT
exec_underline(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
784 TRACE("(%p)\n", This
);
787 FIXME("unsupported args\n");
789 if(This
->nscontainer
)
790 do_ns_command(This
->nscontainer
, NSCMD_UNDERLINE
, NULL
);
792 update_doc(This
, UPDATE_UI
);
796 static HRESULT
exec_horizontalline(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
798 TRACE("(%p)\n", This
);
801 FIXME("unsupported args\n");
803 if(This
->nscontainer
)
804 do_ns_command(This
->nscontainer
, NSCMD_INSERTHR
, NULL
);
806 update_doc(This
, UPDATE_UI
);
810 static HRESULT
exec_orderlist(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
812 TRACE("(%p)\n", This
);
815 FIXME("unsupported args\n");
817 if(This
->nscontainer
)
818 do_ns_command(This
->nscontainer
, NSCMD_OL
, NULL
);
820 update_doc(This
, UPDATE_UI
);
824 static HRESULT
exec_unorderlist(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
826 TRACE("(%p)\n", This
);
829 FIXME("unsupported args\n");
831 if(This
->nscontainer
)
832 do_ns_command(This
->nscontainer
, NSCMD_UL
, NULL
);
834 update_doc(This
, UPDATE_UI
);
838 static HRESULT
exec_indent(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
840 TRACE("(%p)\n", This
);
843 FIXME("unsupported args\n");
845 if(This
->nscontainer
)
846 do_ns_command(This
->nscontainer
, NSCMD_INDENT
, NULL
);
848 update_doc(This
, UPDATE_UI
);
852 static HRESULT
exec_outdent(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
854 TRACE("(%p)\n", This
);
857 FIXME("unsupported args\n");
859 if(This
->nscontainer
)
860 do_ns_command(This
->nscontainer
, NSCMD_OUTDENT
, NULL
);
862 update_doc(This
, UPDATE_UI
);
866 static HRESULT
exec_composesettings(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
870 if(out
|| !in
|| V_VT(in
) != VT_BSTR
) {
871 WARN("invalid arg\n");
875 TRACE("(%p)->(%x %s)\n", This
, cmdexecopt
, debugstr_w(V_BSTR(in
)));
877 update_doc(This
, UPDATE_UI
);
881 exec_bold(This
, cmdexecopt
, NULL
, NULL
);
882 ptr
= strchrW(ptr
, ',');
887 exec_italic(This
, cmdexecopt
, NULL
, NULL
);
888 ptr
= strchrW(ptr
, ',');
893 exec_underline(This
, cmdexecopt
, NULL
, NULL
);
894 ptr
= strchrW(ptr
, ',');
898 if(isdigitW(*++ptr
)) {
904 exec_fontsize(This
, cmdexecopt
, &v
, NULL
);
906 ptr
= strchrW(ptr
, ',');
911 FIXME("set font color\n");
912 ptr
= strchrW(ptr
, ',');
917 FIXME("set background color\n");
918 ptr
= strchrW(ptr
, ',');
927 V_BSTR(&v
) = SysAllocString(ptr
);
929 exec_fontname(This
, cmdexecopt
, &v
, NULL
);
931 SysFreeString(V_BSTR(&v
));
937 HRESULT
editor_exec_copy(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
939 update_doc(This
, UPDATE_UI
);
941 if(!This
->nscontainer
)
944 do_ns_editor_command(This
->nscontainer
, NSCMD_COPY
);
948 HRESULT
editor_exec_cut(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
950 update_doc(This
, UPDATE_UI
);
952 if(!This
->nscontainer
)
955 do_ns_editor_command(This
->nscontainer
, NSCMD_CUT
);
959 HRESULT
editor_exec_paste(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
961 update_doc(This
, UPDATE_UI
);
963 if(!This
->nscontainer
)
966 do_ns_editor_command(This
->nscontainer
, NSCMD_PASTE
);
970 static HRESULT
exec_setdirty(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
972 TRACE("(%p)->(%08x %p %p)\n", This
, cmdexecopt
, in
, out
);
977 if(V_VT(in
) == VT_BOOL
)
978 set_dirty(This
, V_BOOL(in
));
980 FIXME("unsupported vt=%d\n", V_VT(in
));
985 static HRESULT
query_edit_status(HTMLDocument
*This
, OLECMD
*cmd
)
989 TRACE("CGID_MSHTML: IDM_DELETE\n");
990 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
993 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
994 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
997 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
998 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1001 TRACE("CGID_MSHTML: IDM_BOLD\n");
1002 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_BOLD
);
1005 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
1006 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1009 TRACE("CGID_MSHTML: IDM_ITALIC\n");
1010 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_ITALIC
);
1013 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
1014 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_UNDERLINE
);
1016 case IDM_HORIZONTALLINE
:
1017 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1018 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1021 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1022 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_OL
);
1024 case IDM_UNORDERLIST
:
1025 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1026 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_UL
);
1029 TRACE("CGID_MSHTML: IDM_INDENT\n");
1030 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1033 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1034 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1037 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1038 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1045 static INT_PTR CALLBACK
hyperlink_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
1047 static const WCHAR wszOther
[] = {'(','o','t','h','e','r',')',0};
1053 static const WCHAR wszFile
[] = {'f','i','l','e',':',0};
1054 static const WCHAR wszFtp
[] = {'f','t','p',':',0};
1055 static const WCHAR wszHttp
[] = {'h','t','t','p',':',0};
1056 static const WCHAR wszHttps
[] = {'h','t','t','p','s',':',0};
1057 static const WCHAR wszMailto
[] = {'m','a','i','l','t','o',':',0};
1058 static const WCHAR wszNews
[] = {'n','e','w','s',':',0};
1060 HWND hwndCB
= GetDlgItem(hwnd
, IDC_TYPE
);
1061 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1064 SetWindowLongPtrW(hwnd
, DWLP_USER
, lparam
);
1066 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszOther
);
1067 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszFile
);
1068 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszFtp
);
1069 def_idx
= SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszHttp
);
1070 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszHttps
);
1071 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszMailto
);
1072 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszNews
);
1073 SendMessageW(hwndCB
, CB_SETCURSEL
, def_idx
, 0);
1075 /* force the updating of the URL edit box */
1076 SendMessageW(hwnd
, WM_COMMAND
, MAKEWPARAM(IDC_TYPE
, CBN_SELCHANGE
), (LPARAM
)hwndCB
);
1079 len
= SendMessageW(hwndURL
, WM_GETTEXTLENGTH
, 0, 0);
1080 SendMessageW(hwndURL
, EM_SETSEL
, 0, len
);
1087 case MAKEWPARAM(IDCANCEL
, BN_CLICKED
):
1088 EndDialog(hwnd
, wparam
);
1090 case MAKEWPARAM(IDOK
, BN_CLICKED
):
1092 BSTR
*url
= (BSTR
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
1093 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1094 INT len
= GetWindowTextLengthW(hwndURL
);
1095 *url
= SysAllocStringLen(NULL
, len
+ 1);
1096 GetWindowTextW(hwndURL
, *url
, len
+ 1);
1097 EndDialog(hwnd
, wparam
);
1100 case MAKEWPARAM(IDC_TYPE
, CBN_SELCHANGE
):
1102 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1108 static const WCHAR wszSlashSlash
[] = {'/','/'};
1110 /* get string of currently selected hyperlink type */
1111 item
= SendMessageW((HWND
)lparam
, CB_GETCURSEL
, 0, 0);
1112 len
= SendMessageW((HWND
)lparam
, CB_GETLBTEXTLEN
, item
, 0);
1113 type
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1114 SendMessageW((HWND
)lparam
, CB_GETLBTEXT
, item
, (LPARAM
)type
);
1116 if (!strcmpW(type
, wszOther
))
1119 /* get current URL */
1120 len
= GetWindowTextLengthW(hwndURL
);
1121 url
= HeapAlloc(GetProcessHeap(), 0, (len
+ strlenW(type
) + 3) * sizeof(WCHAR
));
1122 GetWindowTextW(hwndURL
, url
, len
+ 1);
1124 /* strip off old protocol */
1125 p
= strchrW(url
, ':');
1126 if (p
&& p
[1] == '/' && p
[2] == '/')
1129 memmove(url
+ (*type
!= '\0' ? strlenW(type
) + 2 : 0), p
, (len
+ 1 - (p
- url
)) * sizeof(WCHAR
));
1131 /* add new protocol */
1134 memcpy(url
, type
, strlenW(type
) * sizeof(WCHAR
));
1135 memcpy(url
+ strlenW(type
), wszSlashSlash
, sizeof(wszSlashSlash
));
1138 SetWindowTextW(hwndURL
, url
);
1140 HeapFree(GetProcessHeap(), 0, url
);
1141 HeapFree(GetProcessHeap(), 0, type
);
1147 EndDialog(hwnd
, IDCANCEL
);
1154 static HRESULT
exec_hyperlink(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
1158 nsISelection
*nsselection
;
1159 nsIDOMDocument
*nsdoc
;
1161 HRESULT hres
= E_FAIL
;
1163 TRACE("%p, 0x%x, %p, %p\n", This
, cmdexecopt
, in
, out
);
1165 if (cmdexecopt
== OLECMDEXECOPT_DONTPROMPTUSER
)
1167 if (!in
|| V_VT(in
) != VT_BSTR
)
1169 WARN("invalid arg\n");
1170 return E_INVALIDARG
;
1176 ret
= DialogBoxParamW(hInst
, MAKEINTRESOURCEW(IDD_HYPERLINK
), NULL
/* FIXME */, hyperlink_dlgproc
, (LPARAM
)&url
);
1178 return OLECMDERR_E_CANCELED
;
1181 nsselection
= get_ns_selection(This
);
1185 nsres
= nsIWebNavigation_GetDocument(This
->nscontainer
->navigation
, &nsdoc
);
1186 if(NS_SUCCEEDED(nsres
))
1188 static const WCHAR wszA
[] = {'a',0};
1189 static const WCHAR wszHref
[] = {'h','r','e','f',0};
1190 nsIHTMLEditor
*html_editor
;
1191 nsIDOMNode
*text_node
;
1192 nsIDOMElement
*anchor_elem
;
1193 nsIDOMNode
*unused_node
;
1197 PRBool insert_link_at_caret
;
1199 nsAString_Init(&a_str
, wszA
);
1200 nsAString_Init(&href_str
, wszHref
);
1201 nsAString_Init(&ns_url
, url
);
1203 /* create an element for the link */
1204 nsIDOMDocument_CreateElement(nsdoc
, &a_str
, &anchor_elem
);
1205 nsIDOMElement_SetAttribute(anchor_elem
, &href_str
, &ns_url
);
1207 nsAString_Finish(&href_str
);
1208 nsAString_Finish(&a_str
);
1210 nsISelection_GetIsCollapsed(nsselection
, &insert_link_at_caret
);
1212 /* create an element with text of URL */
1213 if (insert_link_at_caret
)
1215 nsIDOMDocument_CreateTextNode(nsdoc
, &ns_url
, (nsIDOMText
**)&text_node
);
1217 /* wrap the <a> tags around the text element */
1218 nsIDOMElement_AppendChild(anchor_elem
, text_node
, &unused_node
);
1219 nsIDOMNode_Release(text_node
);
1220 nsIDOMNode_Release(unused_node
);
1223 nsAString_Finish(&ns_url
);
1225 nsIEditor_QueryInterface(This
->nscontainer
->editor
, &IID_nsIHTMLEditor
, (void **)&html_editor
);
1228 if (insert_link_at_caret
)
1230 /* add them to the document at the caret position */
1231 nsres
= nsIHTMLEditor_InsertElementAtSelection(html_editor
, anchor_elem
, FALSE
);
1232 nsISelection_SelectAllChildren(nsselection
, (nsIDOMNode
*)anchor_elem
);
1234 else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1235 nsres
= nsIHTMLEditor_InsertLinkAroundSelection(html_editor
, anchor_elem
);
1236 nsIHTMLEditor_Release(html_editor
);
1239 nsIDOMElement_Release(anchor_elem
);
1240 nsIDOMDocument_Release(nsdoc
);
1242 hres
= NS_SUCCEEDED(nsres
) ? S_OK
: E_FAIL
;
1245 nsISelection_Release(nsselection
);
1247 if (cmdexecopt
!= OLECMDEXECOPT_DONTPROMPTUSER
)
1250 TRACE("-- 0x%08x\n", nsres
);
1254 static HRESULT
query_selall_status(HTMLDocument
*This
, OLECMD
*cmd
)
1256 TRACE("(%p)->(%p)\n", This
, cmd
);
1258 cmd
->cmdf
= OLECMDF_SUPPORTED
|OLECMDF_ENABLED
;
1262 const cmdtable_t editmode_cmds
[] = {
1263 {IDM_DELETE
, query_edit_status
, exec_delete
},
1264 {IDM_FONTNAME
, query_edit_status
, exec_fontname
},
1265 {IDM_FONTSIZE
, query_edit_status
, exec_fontsize
},
1266 {IDM_SELECTALL
, query_selall_status
, exec_selectall
},
1267 {IDM_FORECOLOR
, query_edit_status
, exec_forecolor
},
1268 {IDM_BOLD
, query_edit_status
, exec_bold
},
1269 {IDM_ITALIC
, query_edit_status
, exec_italic
},
1270 {IDM_JUSTIFYCENTER
, query_justify
, exec_justifycenter
},
1271 {IDM_JUSTIFYRIGHT
, query_justify
, exec_justifyright
},
1272 {IDM_JUSTIFYLEFT
, query_justify
, exec_justifyleft
},
1273 {IDM_FONT
, NULL
, exec_font
},
1274 {IDM_UNDERLINE
, query_edit_status
, exec_underline
},
1275 {IDM_HORIZONTALLINE
, query_edit_status
, exec_horizontalline
},
1276 {IDM_ORDERLIST
, query_edit_status
, exec_orderlist
},
1277 {IDM_UNORDERLIST
, query_edit_status
, exec_unorderlist
},
1278 {IDM_INDENT
, query_edit_status
, exec_indent
},
1279 {IDM_OUTDENT
, query_edit_status
, exec_outdent
},
1280 {IDM_COMPOSESETTINGS
, NULL
, exec_composesettings
},
1281 {IDM_HYPERLINK
, query_edit_status
, exec_hyperlink
},
1282 {IDM_SETDIRTY
, NULL
, exec_setdirty
},
1286 void init_editor(HTMLDocument
*This
)
1288 update_doc(This
, UPDATE_UI
);
1290 if(!This
->nscontainer
)
1293 set_ns_fontname(This
->nscontainer
, "Times New Roman");
1296 HRESULT
editor_is_dirty(HTMLDocument
*This
)
1300 if(!This
->nscontainer
|| !This
->nscontainer
->editor
)
1303 nsIEditor_GetDocumentModified(This
->nscontainer
->editor
, &modified
);
1305 return modified
? S_OK
: S_FALSE
;