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
31 #include "wine/debug.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_SELECTBEGINLINE "cmd_selectBeginLine"
64 #define NSCMD_SELECTBOTTOM "cmd_selectBottom"
65 #define NSCMD_SELECTCHARNEXT "cmd_selectCharNext"
66 #define NSCMD_SELECTCHARPREVIOUS "cmd_selectCharPrevious"
67 #define NSCMD_SELECTENDLINE "cmd_selectEndLine"
68 #define NSCMD_SELECTLINENEXT "cmd_selectLineNext"
69 #define NSCMD_SELECTLINEPREVIOUS "cmd_selectLinePrevious"
70 #define NSCMD_SELECTPAGEDOWN "cmd_selectPageDown"
71 #define NSCMD_SELECTPAGEUP "cmd_selectPageUp"
72 #define NSCMD_SELECTTOP "cmd_selectTop"
73 #define NSCMD_SELECTWORDNEXT "cmd_selectWordNext"
74 #define NSCMD_SELECTWORDPREVIOUS "cmd_selectWordPrevious"
75 #define NSCMD_UL "cmd_ul"
76 #define NSCMD_UNDERLINE "cmd_underline"
77 #define NSCMD_WORDNEXT "cmd_wordNext"
78 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
80 #define NSSTATE_ATTRIBUTE "state_attribute"
81 #define NSSTATE_ALL "state_all"
83 #define NSALIGN_CENTER "center"
84 #define NSALIGN_LEFT "left"
85 #define NSALIGN_RIGHT "right"
87 #define DOM_VK_LEFT VK_LEFT
88 #define DOM_VK_UP VK_UP
89 #define DOM_VK_RIGHT VK_RIGHT
90 #define DOM_VK_DOWN VK_DOWN
91 #define DOM_VK_DELETE VK_DELETE
92 #define DOM_VK_HOME VK_HOME
93 #define DOM_VK_END VK_END
95 static const WCHAR fontW
[] = {'f','o','n','t',0};
96 static const WCHAR sizeW
[] = {'s','i','z','e',0};
98 void set_dirty(HTMLDocument
*This
, VARIANT_BOOL dirty
)
102 if(This
->doc_obj
->usermode
!= EDITMODE
|| !This
->doc_obj
->nscontainer
|| !This
->doc_obj
->nscontainer
->editor
)
106 nsres
= nsIEditor_IncrementModificationCount(This
->doc_obj
->nscontainer
->editor
, 1);
108 ERR("IncrementModificationCount failed: %08x\n", nsres
);
110 nsres
= nsIEditor_ResetModificationCount(This
->doc_obj
->nscontainer
->editor
);
112 ERR("ResetModificationCount failed: %08x\n", nsres
);
116 static void do_ns_editor_command(NSContainer
*This
, const char *cmd
)
120 if(!This
->editor_controller
)
123 nsres
= nsIController_DoCommand(This
->editor_controller
, cmd
);
125 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd
), nsres
);
128 static nsresult
get_ns_command_state(NSContainer
*This
, const char *cmd
, nsICommandParams
*nsparam
)
130 nsICommandManager
*cmdmgr
;
133 nsres
= get_nsinterface((nsISupports
*)This
->webbrowser
, &IID_nsICommandManager
, (void**)&cmdmgr
);
134 if(NS_FAILED(nsres
)) {
135 ERR("Could not get nsICommandManager: %08x\n", nsres
);
139 nsres
= nsICommandManager_GetCommandState(cmdmgr
, cmd
, This
->doc
->basedoc
.window
->nswindow
, nsparam
);
141 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd
), nsres
);
143 nsICommandManager_Release(cmdmgr
);
147 static DWORD
query_ns_edit_status(HTMLDocument
*This
, const char *nscmd
)
149 nsICommandParams
*nsparam
;
152 if(This
->doc_obj
->usermode
!= EDITMODE
|| This
->window
->readystate
< READYSTATE_INTERACTIVE
)
153 return OLECMDF_SUPPORTED
;
155 if(This
->doc_obj
->nscontainer
&& nscmd
) {
156 nsparam
= create_nscommand_params();
157 get_ns_command_state(This
->doc_obj
->nscontainer
, nscmd
, nsparam
);
159 nsICommandParams_GetBooleanValue(nsparam
, NSSTATE_ALL
, &b
);
161 nsICommandParams_Release(nsparam
);
164 return OLECMDF_SUPPORTED
| OLECMDF_ENABLED
| (b
? OLECMDF_LATCHED
: 0);
167 static void set_ns_align(HTMLDocument
*This
, const char *align_str
)
169 nsICommandParams
*nsparam
;
171 if(!This
->doc_obj
->nscontainer
)
174 nsparam
= create_nscommand_params();
175 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, align_str
);
177 do_ns_command(This
, NSCMD_ALIGN
, nsparam
);
179 nsICommandParams_Release(nsparam
);
182 static DWORD
query_align_status(HTMLDocument
*This
, const WCHAR
*align
)
184 DWORD ret
= OLECMDF_SUPPORTED
| OLECMDF_ENABLED
;
185 nsAString justify_str
;
189 if(This
->doc_obj
->usermode
!= EDITMODE
|| This
->window
->readystate
< READYSTATE_INTERACTIVE
)
190 return OLECMDF_SUPPORTED
;
192 nsAString_Init(&justify_str
, align
);
193 nsres
= nsIDOMHTMLDocument_QueryCommandState(This
->doc_node
->nsdoc
, &justify_str
, &b
);
194 nsAString_Finish(&justify_str
);
195 if(NS_SUCCEEDED(nsres
) && b
)
196 ret
|= OLECMDF_LATCHED
;
202 static nsISelection
*get_ns_selection(HTMLDocument
*This
)
204 nsISelection
*nsselection
= NULL
;
207 nsres
= nsIDOMWindow_GetSelection(This
->window
->nswindow
, &nsselection
);
209 ERR("GetSelection failed %08x\n", nsres
);
215 static void remove_child_attr(nsIDOMElement
*elem
, LPCWSTR tag
, nsAString
*attr_str
)
217 cpp_bool has_children
;
219 nsIDOMNode
*child_node
;
220 nsIDOMNodeList
*node_list
;
223 nsIDOMElement_HasChildNodes(elem
, &has_children
);
227 nsIDOMElement_GetChildNodes(elem
, &node_list
);
228 nsIDOMNodeList_GetLength(node_list
, &child_cnt
);
230 for(i
=0; i
<child_cnt
; i
++) {
231 nsIDOMNodeList_Item(node_list
, i
, &child_node
);
233 nsIDOMNode_GetNodeType(child_node
, &node_type
);
234 if(node_type
== ELEMENT_NODE
) {
235 nsIDOMElement
*child_elem
;
237 const PRUnichar
*ctag
;
239 nsIDOMNode_QueryInterface(child_node
, &IID_nsIDOMElement
, (void**)&child_elem
);
241 nsAString_Init(&tag_str
, NULL
);
242 nsIDOMElement_GetTagName(child_elem
, &tag_str
);
243 nsAString_GetData(&tag_str
, &ctag
);
245 if(!strcmpiW(ctag
, tag
))
246 /* FIXME: remove node if there are no more attributes */
247 nsIDOMElement_RemoveAttribute(child_elem
, attr_str
);
249 nsAString_Finish(&tag_str
);
251 remove_child_attr(child_elem
, tag
, attr_str
);
253 nsIDOMElement_Release(child_elem
);
256 nsIDOMNode_Release(child_node
);
259 nsIDOMNodeList_Release(node_list
);
262 static void get_font_size(HTMLDocument
*This
, WCHAR
*ret
)
264 nsISelection
*nsselection
= get_ns_selection(This
);
265 nsIDOMHTMLElement
*elem
= NULL
;
266 nsIDOMNode
*node
= NULL
, *tmp_node
;
277 nsISelection_GetFocusNode(nsselection
, &node
);
278 nsISelection_Release(nsselection
);
281 nsres
= nsIDOMNode_GetNodeType(node
, &node_type
);
282 if(NS_FAILED(nsres
) || node_type
== DOCUMENT_NODE
)
285 if(node_type
== ELEMENT_NODE
) {
286 nsIDOMNode_QueryInterface(node
, &IID_nsIDOMHTMLElement
, (void**)&elem
);
288 nsAString_Init(&tag_str
, NULL
);
289 nsIDOMHTMLElement_GetTagName(elem
, &tag_str
);
290 nsAString_GetData(&tag_str
, &tag
);
292 if(!strcmpiW(tag
, fontW
)) {
294 const PRUnichar
*val
;
296 TRACE("found font tag %p\n", elem
);
298 get_elem_attr_value(elem
, sizeW
, &val_str
, &val
);
300 TRACE("found size %s\n", debugstr_w(val
));
304 nsAString_Finish(&val_str
);
307 nsAString_Finish(&tag_str
);
308 nsIDOMHTMLElement_Release(elem
);
315 nsIDOMNode_GetParentNode(node
, &node
);
316 nsIDOMNode_Release(tmp_node
);
320 nsIDOMNode_Release(node
);
323 static void set_font_size(HTMLDocument
*This
, LPCWSTR size
)
325 nsISelection
*nsselection
;
327 nsIDOMHTMLElement
*elem
;
333 if(!This
->doc_node
->nsdoc
) {
334 WARN("NULL nsdoc\n");
338 nsselection
= get_ns_selection(This
);
342 nsISelection_GetRangeCount(nsselection
, &range_cnt
);
344 FIXME("range_cnt %d not supprted\n", range_cnt
);
346 nsISelection_Release(nsselection
);
351 create_nselem(This
->doc_node
, fontW
, &elem
);
353 nsAString_InitDepend(&size_str
, sizeW
);
354 nsAString_InitDepend(&val_str
, size
);
356 nsIDOMHTMLElement_SetAttribute(elem
, &size_str
, &val_str
);
357 nsAString_Finish(&val_str
);
359 nsISelection_GetRangeAt(nsselection
, 0, &range
);
360 nsISelection_GetIsCollapsed(nsselection
, &collapsed
);
361 nsISelection_RemoveAllRanges(nsselection
);
363 nsIDOMRange_SurroundContents(range
, (nsIDOMNode
*)elem
);
366 nsISelection_Collapse(nsselection
, (nsIDOMNode
*)elem
, 0);
368 /* Remove all size attributes from the range */
369 remove_child_attr((nsIDOMElement
*)elem
, fontW
, &size_str
);
370 nsISelection_SelectAllChildren(nsselection
, (nsIDOMNode
*)elem
);
373 nsISelection_Release(nsselection
);
374 nsIDOMRange_Release(range
);
375 nsIDOMHTMLElement_Release(elem
);
377 nsAString_Finish(&size_str
);
379 set_dirty(This
, VARIANT_TRUE
);
382 static void handle_arrow_key(HTMLDocument
*This
, nsIDOMKeyEvent
*event
, const char * const cmds
[4])
387 nsIDOMKeyEvent_GetCtrlKey(event
, &b
);
391 nsIDOMKeyEvent_GetShiftKey(event
, &b
);
396 do_ns_editor_command(This
->doc_obj
->nscontainer
, cmds
[i
]);
398 nsIDOMKeyEvent_PreventDefault(event
);
401 void handle_edit_event(HTMLDocument
*This
, nsIDOMEvent
*event
)
403 nsIDOMKeyEvent
*key_event
;
406 nsIDOMEvent_QueryInterface(event
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
408 nsIDOMKeyEvent_GetKeyCode(key_event
, &code
);
412 static const char * const cmds
[] = {
415 NSCMD_SELECTCHARPREVIOUS
,
416 NSCMD_SELECTWORDPREVIOUS
420 handle_arrow_key(This
, key_event
, cmds
);
424 static const char * const cmds
[] = {
427 NSCMD_SELECTCHARNEXT
,
432 handle_arrow_key(This
, key_event
, cmds
);
436 static const char * const cmds
[] = {
439 NSCMD_SELECTLINEPREVIOUS
,
444 handle_arrow_key(This
, key_event
, cmds
);
448 static const char * const cmds
[] = {
451 NSCMD_SELECTLINENEXT
,
456 handle_arrow_key(This
, key_event
, cmds
);
459 case DOM_VK_DELETE
: {
460 static const char * const cmds
[] = {
461 NSCMD_DELETECHARFORWARD
,
462 NSCMD_DELETEWORDFORWARD
,
467 handle_arrow_key(This
, key_event
, cmds
);
471 static const char * const cmds
[] = {
474 NSCMD_SELECTBEGINLINE
,
479 handle_arrow_key(This
, key_event
, cmds
);
483 static const char * const cmds
[] = {
491 handle_arrow_key(This
, key_event
, cmds
);
496 nsIDOMKeyEvent_Release(key_event
);
499 void handle_edit_load(HTMLDocument
*This
)
501 get_editor_controller(This
->doc_obj
->nscontainer
);
504 static void set_ns_fontname(HTMLDocument
*This
, const char *fontname
)
506 nsICommandParams
*nsparam
= create_nscommand_params();
508 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, fontname
);
509 do_ns_command(This
, NSCMD_FONTFACE
, nsparam
);
510 nsICommandParams_Release(nsparam
);
513 static HRESULT
exec_delete(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
515 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
517 if(This
->doc_obj
->nscontainer
)
518 do_ns_editor_command(This
->doc_obj
->nscontainer
, NSCMD_DELETECHARFORWARD
);
520 update_doc(This
, UPDATE_UI
);
524 static HRESULT
exec_fontname(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
526 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
528 if(!This
->doc_obj
->nscontainer
) {
529 update_doc(This
, UPDATE_UI
);
536 if(V_VT(in
) != VT_BSTR
) {
537 FIXME("Unsupported fontname %s\n", debugstr_variant(out
));
541 TRACE("%s\n", debugstr_w(V_BSTR(in
)));
543 stra
= heap_strdupWtoA(V_BSTR(in
));
544 set_ns_fontname(This
, stra
);
547 update_doc(This
, UPDATE_UI
);
551 nsICommandParams
*nsparam
;
560 nsparam
= create_nscommand_params();
562 nsres
= get_ns_command_state(This
->doc_obj
->nscontainer
, NSCMD_FONTFACE
, nsparam
);
566 nsICommandParams_GetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, &stra
);
567 nsICommandParams_Release(nsparam
);
569 len
= MultiByteToWideChar(CP_ACP
, 0, stra
, -1, NULL
, 0);
570 strw
= heap_alloc(len
*sizeof(WCHAR
));
571 MultiByteToWideChar(CP_ACP
, 0, stra
, -1, strw
, len
);
574 V_BSTR(out
) = SysAllocString(strw
);
581 static HRESULT
exec_forecolor(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
583 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
586 if(V_VT(in
) == VT_I4
) {
587 nsICommandParams
*nsparam
= create_nscommand_params();
590 sprintf(color_str
, "#%02x%02x%02x",
591 V_I4(in
)&0xff, (V_I4(in
)>>8)&0xff, (V_I4(in
)>>16)&0xff);
593 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, color_str
);
594 do_ns_command(This
, NSCMD_FONTCOLOR
, nsparam
);
596 nsICommandParams_Release(nsparam
);
598 FIXME("unsupported forecolor %s\n", debugstr_variant(in
));
601 update_doc(This
, UPDATE_UI
);
605 FIXME("unsupported out\n");
612 static HRESULT
exec_fontsize(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
614 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
619 get_font_size(This
, val
);
621 V_I4(out
) = strtolW(val
, NULL
, 10);
628 static const WCHAR format
[] = {'%','d',0};
629 wsprintfW(size
, format
, V_I4(in
));
630 set_font_size(This
, size
);
634 set_font_size(This
, V_BSTR(in
));
637 FIXME("unsupported fontsize %s\n", debugstr_variant(in
));
640 update_doc(This
, UPDATE_UI
);
646 static HRESULT
exec_font(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
649 FIXME("(%p)->(%p %p)\n", This
, in
, out
);
653 static HRESULT
exec_bold(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
655 TRACE("(%p)\n", This
);
658 FIXME("unsupported args\n");
660 if(This
->doc_obj
->nscontainer
)
661 do_ns_command(This
, NSCMD_BOLD
, NULL
);
663 update_doc(This
, UPDATE_UI
);
667 static HRESULT
exec_italic(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
669 TRACE("(%p)\n", This
);
672 FIXME("unsupported args\n");
674 if(This
->doc_obj
->nscontainer
)
675 do_ns_command(This
, NSCMD_ITALIC
, NULL
);
677 update_doc(This
, UPDATE_UI
);
681 static HRESULT
query_justify(HTMLDocument
*This
, OLECMD
*cmd
)
683 static const PRUnichar justifycenterW
[] = {'j','u','s','t','i','f','y','c','e','n','t','e','r',0};
684 static const PRUnichar justifyrightW
[] = {'j','u','s','t','i','f','y','r','i','g','h','t',0};
687 case IDM_JUSTIFYCENTER
:
688 TRACE("(%p) IDM_JUSTIFYCENTER\n", This
);
689 cmd
->cmdf
= query_align_status(This
, justifycenterW
);
691 case IDM_JUSTIFYLEFT
:
692 TRACE("(%p) IDM_JUSTIFYLEFT\n", This
);
693 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
694 if(This
->doc_obj
->usermode
!= EDITMODE
|| This
->window
->readystate
< READYSTATE_INTERACTIVE
)
695 cmd
->cmdf
= OLECMDF_SUPPORTED
;
697 cmd
->cmdf
= OLECMDF_SUPPORTED
| OLECMDF_ENABLED
;
699 case IDM_JUSTIFYRIGHT
:
700 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This
);
701 cmd
->cmdf
= query_align_status(This
, justifyrightW
);
708 static HRESULT
exec_justifycenter(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
710 TRACE("(%p)\n", This
);
713 FIXME("unsupported args\n");
715 set_ns_align(This
, NSALIGN_CENTER
);
716 update_doc(This
, UPDATE_UI
);
720 static HRESULT
exec_justifyleft(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
722 TRACE("(%p)\n", This
);
725 FIXME("unsupported args\n");
727 set_ns_align(This
, NSALIGN_LEFT
);
728 update_doc(This
, UPDATE_UI
);
732 static HRESULT
exec_justifyright(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
734 TRACE("(%p)\n", This
);
737 FIXME("unsupported args\n");
739 set_ns_align(This
, NSALIGN_RIGHT
);
740 update_doc(This
, UPDATE_UI
);
744 static HRESULT
exec_underline(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
746 TRACE("(%p)\n", This
);
749 FIXME("unsupported args\n");
751 do_ns_command(This
, NSCMD_UNDERLINE
, NULL
);
752 update_doc(This
, UPDATE_UI
);
756 static HRESULT
exec_horizontalline(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
758 TRACE("(%p)\n", This
);
761 FIXME("unsupported args\n");
763 do_ns_command(This
, NSCMD_INSERTHR
, NULL
);
764 update_doc(This
, UPDATE_UI
);
768 static HRESULT
exec_orderlist(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
770 TRACE("(%p)\n", This
);
773 FIXME("unsupported args\n");
775 do_ns_command(This
, NSCMD_OL
, NULL
);
776 update_doc(This
, UPDATE_UI
);
780 static HRESULT
exec_unorderlist(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
782 TRACE("(%p)\n", This
);
785 FIXME("unsupported args\n");
787 do_ns_command(This
, NSCMD_UL
, NULL
);
788 update_doc(This
, UPDATE_UI
);
792 static HRESULT
exec_indent(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
794 TRACE("(%p)\n", This
);
797 FIXME("unsupported args\n");
799 do_ns_command(This
, NSCMD_INDENT
, NULL
);
800 update_doc(This
, UPDATE_UI
);
804 static HRESULT
exec_outdent(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
806 TRACE("(%p)\n", This
);
809 FIXME("unsupported args\n");
811 do_ns_command(This
, NSCMD_OUTDENT
, NULL
);
812 update_doc(This
, UPDATE_UI
);
816 static HRESULT
exec_composesettings(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
820 if(out
|| !in
|| V_VT(in
) != VT_BSTR
) {
821 WARN("invalid arg %s\n", debugstr_variant(in
));
825 TRACE("(%p)->(%x %s)\n", This
, cmdexecopt
, debugstr_w(V_BSTR(in
)));
827 update_doc(This
, UPDATE_UI
);
831 exec_bold(This
, cmdexecopt
, NULL
, NULL
);
832 ptr
= strchrW(ptr
, ',');
837 exec_italic(This
, cmdexecopt
, NULL
, NULL
);
838 ptr
= strchrW(ptr
, ',');
843 exec_underline(This
, cmdexecopt
, NULL
, NULL
);
844 ptr
= strchrW(ptr
, ',');
848 if(isdigitW(*++ptr
)) {
854 exec_fontsize(This
, cmdexecopt
, &v
, NULL
);
856 ptr
= strchrW(ptr
, ',');
861 FIXME("set font color\n");
862 ptr
= strchrW(ptr
, ',');
867 FIXME("set background color\n");
868 ptr
= strchrW(ptr
, ',');
877 V_BSTR(&v
) = SysAllocString(ptr
);
879 exec_fontname(This
, cmdexecopt
, &v
, NULL
);
881 SysFreeString(V_BSTR(&v
));
887 HRESULT
editor_exec_copy(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
889 update_doc(This
, UPDATE_UI
);
891 if(!This
->doc_obj
->nscontainer
)
894 do_ns_editor_command(This
->doc_obj
->nscontainer
, NSCMD_COPY
);
898 HRESULT
editor_exec_cut(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
900 update_doc(This
, UPDATE_UI
);
902 if(!This
->doc_obj
->nscontainer
)
905 do_ns_editor_command(This
->doc_obj
->nscontainer
, NSCMD_CUT
);
909 HRESULT
editor_exec_paste(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
911 update_doc(This
, UPDATE_UI
);
913 if(!This
->doc_obj
->nscontainer
)
916 do_ns_editor_command(This
->doc_obj
->nscontainer
, NSCMD_PASTE
);
920 static HRESULT
exec_setdirty(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
922 TRACE("(%p)->(%08x %p %p)\n", This
, cmdexecopt
, in
, out
);
927 if(V_VT(in
) == VT_BOOL
)
928 set_dirty(This
, V_BOOL(in
));
930 FIXME("unsupported arg %s\n", debugstr_variant(in
));
935 static HRESULT
query_edit_status(HTMLDocument
*This
, OLECMD
*cmd
)
939 TRACE("CGID_MSHTML: IDM_DELETE\n");
940 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
943 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
944 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
947 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
948 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
951 TRACE("CGID_MSHTML: IDM_BOLD\n");
952 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_BOLD
);
955 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
956 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
959 TRACE("CGID_MSHTML: IDM_ITALIC\n");
960 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_ITALIC
);
963 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
964 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_UNDERLINE
);
966 case IDM_HORIZONTALLINE
:
967 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
968 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
971 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
972 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_OL
);
974 case IDM_UNORDERLIST
:
975 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
976 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_UL
);
979 TRACE("CGID_MSHTML: IDM_INDENT\n");
980 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
983 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
984 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
987 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
988 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
995 static INT_PTR CALLBACK
hyperlink_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
997 static const WCHAR wszOther
[] = {'(','o','t','h','e','r',')',0};
998 static const WCHAR wszFile
[] = {'f','i','l','e',':',0};
999 static const WCHAR wszFtp
[] = {'f','t','p',':',0};
1000 static const WCHAR wszHttp
[] = {'h','t','t','p',':',0};
1001 static const WCHAR wszHttps
[] = {'h','t','t','p','s',':',0};
1002 static const WCHAR wszMailto
[] = {'m','a','i','l','t','o',':',0};
1003 static const WCHAR wszNews
[] = {'n','e','w','s',':',0};
1010 HWND hwndCB
= GetDlgItem(hwnd
, IDC_TYPE
);
1011 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1014 SetWindowLongPtrW(hwnd
, DWLP_USER
, lparam
);
1016 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszOther
);
1017 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszFile
);
1018 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszFtp
);
1019 def_idx
= SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszHttp
);
1020 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszHttps
);
1021 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszMailto
);
1022 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszNews
);
1023 SendMessageW(hwndCB
, CB_SETCURSEL
, def_idx
, 0);
1025 /* force the updating of the URL edit box */
1026 SendMessageW(hwnd
, WM_COMMAND
, MAKEWPARAM(IDC_TYPE
, CBN_SELCHANGE
), (LPARAM
)hwndCB
);
1029 len
= SendMessageW(hwndURL
, WM_GETTEXTLENGTH
, 0, 0);
1030 SendMessageW(hwndURL
, EM_SETSEL
, 0, len
);
1037 case MAKEWPARAM(IDCANCEL
, BN_CLICKED
):
1038 EndDialog(hwnd
, wparam
);
1040 case MAKEWPARAM(IDOK
, BN_CLICKED
):
1042 BSTR
*url
= (BSTR
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
1043 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1044 INT len
= GetWindowTextLengthW(hwndURL
);
1045 *url
= SysAllocStringLen(NULL
, len
+ 1);
1046 GetWindowTextW(hwndURL
, *url
, len
+ 1);
1047 EndDialog(hwnd
, wparam
);
1050 case MAKEWPARAM(IDC_TYPE
, CBN_SELCHANGE
):
1052 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1058 static const WCHAR wszSlashSlash
[] = {'/','/'};
1060 /* get string of currently selected hyperlink type */
1061 item
= SendMessageW((HWND
)lparam
, CB_GETCURSEL
, 0, 0);
1062 len
= SendMessageW((HWND
)lparam
, CB_GETLBTEXTLEN
, item
, 0);
1063 type
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1064 SendMessageW((HWND
)lparam
, CB_GETLBTEXT
, item
, (LPARAM
)type
);
1066 if (!strcmpW(type
, wszOther
))
1069 /* get current URL */
1070 len
= GetWindowTextLengthW(hwndURL
);
1071 url
= HeapAlloc(GetProcessHeap(), 0, (len
+ strlenW(type
) + 3) * sizeof(WCHAR
));
1072 GetWindowTextW(hwndURL
, url
, len
+ 1);
1074 /* strip off old protocol */
1075 p
= strchrW(url
, ':');
1076 if (p
&& p
[1] == '/' && p
[2] == '/')
1079 memmove(url
+ (*type
!= '\0' ? strlenW(type
) + 2 : 0), p
, (len
+ 1 - (p
- url
)) * sizeof(WCHAR
));
1081 /* add new protocol */
1084 memcpy(url
, type
, (strlenW(type
) + 1) * sizeof(WCHAR
));
1085 if (strcmpW(type
, wszMailto
) && strcmpW(type
, wszNews
))
1086 memcpy(url
+ strlenW(type
), wszSlashSlash
, sizeof(wszSlashSlash
));
1089 SetWindowTextW(hwndURL
, url
);
1091 HeapFree(GetProcessHeap(), 0, url
);
1092 HeapFree(GetProcessHeap(), 0, type
);
1098 EndDialog(hwnd
, IDCANCEL
);
1105 static HRESULT
exec_hyperlink(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
1107 nsAString href_str
, ns_url
;
1108 nsIHTMLEditor
*html_editor
;
1109 nsIDOMHTMLElement
*anchor_elem
;
1110 cpp_bool insert_link_at_caret
;
1111 nsISelection
*nsselection
;
1114 HRESULT hres
= E_FAIL
;
1116 static const WCHAR aW
[] = {'a',0};
1117 static const WCHAR hrefW
[] = {'h','r','e','f',0};
1119 TRACE("%p, 0x%x, %p, %p\n", This
, cmdexecopt
, in
, out
);
1121 if (cmdexecopt
== OLECMDEXECOPT_DONTPROMPTUSER
)
1123 if (!in
|| V_VT(in
) != VT_BSTR
)
1125 WARN("invalid arg\n");
1126 return E_INVALIDARG
;
1132 ret
= DialogBoxParamW(hInst
, MAKEINTRESOURCEW(IDD_HYPERLINK
), NULL
/* FIXME */, hyperlink_dlgproc
, (LPARAM
)&url
);
1134 return OLECMDERR_E_CANCELED
;
1137 if(!This
->doc_node
->nsdoc
) {
1138 WARN("NULL nsdoc\n");
1139 return E_UNEXPECTED
;
1142 nsselection
= get_ns_selection(This
);
1146 /* create an element for the link */
1147 create_nselem(This
->doc_node
, aW
, &anchor_elem
);
1149 nsAString_InitDepend(&href_str
, hrefW
);
1150 nsAString_InitDepend(&ns_url
, url
);
1151 nsIDOMHTMLElement_SetAttribute(anchor_elem
, &href_str
, &ns_url
);
1152 nsAString_Finish(&href_str
);
1154 nsISelection_GetIsCollapsed(nsselection
, &insert_link_at_caret
);
1156 /* create an element with text of URL */
1157 if (insert_link_at_caret
) {
1158 nsIDOMNode
*unused_node
;
1159 nsIDOMText
*text_node
;
1161 nsIDOMHTMLDocument_CreateTextNode(This
->doc_node
->nsdoc
, &ns_url
, &text_node
);
1163 /* wrap the <a> tags around the text element */
1164 nsIDOMHTMLElement_AppendChild(anchor_elem
, (nsIDOMNode
*)text_node
, &unused_node
);
1165 nsIDOMText_Release(text_node
);
1166 nsIDOMNode_Release(unused_node
);
1169 nsAString_Finish(&ns_url
);
1171 nsIEditor_QueryInterface(This
->doc_obj
->nscontainer
->editor
, &IID_nsIHTMLEditor
, (void **)&html_editor
);
1175 if (insert_link_at_caret
) {
1176 /* add them to the document at the caret position */
1177 nsres
= nsIHTMLEditor_InsertElementAtSelection(html_editor
, (nsIDOMElement
*)anchor_elem
, FALSE
);
1178 nsISelection_SelectAllChildren(nsselection
, (nsIDOMNode
*)anchor_elem
);
1179 }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1180 nsres
= nsIHTMLEditor_InsertLinkAroundSelection(html_editor
, (nsIDOMElement
*)anchor_elem
);
1182 nsIHTMLEditor_Release(html_editor
);
1183 hres
= NS_SUCCEEDED(nsres
) ? S_OK
: E_FAIL
;
1186 nsISelection_Release(nsselection
);
1187 nsIDOMHTMLElement_Release(anchor_elem
);
1189 if (cmdexecopt
!= OLECMDEXECOPT_DONTPROMPTUSER
)
1192 TRACE("-- 0x%08x\n", hres
);
1196 const cmdtable_t editmode_cmds
[] = {
1197 {IDM_DELETE
, query_edit_status
, exec_delete
},
1198 {IDM_FONTNAME
, query_edit_status
, exec_fontname
},
1199 {IDM_FONTSIZE
, query_edit_status
, exec_fontsize
},
1200 {IDM_FORECOLOR
, query_edit_status
, exec_forecolor
},
1201 {IDM_BOLD
, query_edit_status
, exec_bold
},
1202 {IDM_ITALIC
, query_edit_status
, exec_italic
},
1203 {IDM_JUSTIFYCENTER
, query_justify
, exec_justifycenter
},
1204 {IDM_JUSTIFYRIGHT
, query_justify
, exec_justifyright
},
1205 {IDM_JUSTIFYLEFT
, query_justify
, exec_justifyleft
},
1206 {IDM_FONT
, NULL
, exec_font
},
1207 {IDM_UNDERLINE
, query_edit_status
, exec_underline
},
1208 {IDM_HORIZONTALLINE
, query_edit_status
, exec_horizontalline
},
1209 {IDM_ORDERLIST
, query_edit_status
, exec_orderlist
},
1210 {IDM_UNORDERLIST
, query_edit_status
, exec_unorderlist
},
1211 {IDM_INDENT
, query_edit_status
, exec_indent
},
1212 {IDM_OUTDENT
, query_edit_status
, exec_outdent
},
1213 {IDM_COMPOSESETTINGS
, NULL
, exec_composesettings
},
1214 {IDM_HYPERLINK
, query_edit_status
, exec_hyperlink
},
1215 {IDM_SETDIRTY
, NULL
, exec_setdirty
},
1219 void init_editor(HTMLDocument
*This
)
1221 update_doc(This
, UPDATE_UI
);
1223 set_ns_fontname(This
, "Times New Roman");
1226 HRESULT
editor_is_dirty(HTMLDocument
*This
)
1230 if(!This
->doc_obj
->nscontainer
|| !This
->doc_obj
->nscontainer
->editor
)
1233 nsIEditor_GetDocumentModified(This
->doc_obj
->nscontainer
->editor
, &modified
);
1235 return modified
? S_OK
: S_FALSE
;
1238 HRESULT
setup_edit_mode(HTMLDocumentObj
*doc
)
1243 if(doc
->usermode
== EDITMODE
)
1246 doc
->usermode
= EDITMODE
;
1248 if(doc
->basedoc
.window
->mon
) {
1249 CLSID clsid
= IID_NULL
;
1250 hres
= IMoniker_GetClassID(doc
->basedoc
.window
->mon
, &clsid
);
1251 if(SUCCEEDED(hres
)) {
1252 /* We should use IMoniker::Save here */
1253 FIXME("Use CLSID %s\n", debugstr_guid(&clsid
));
1258 IOleInPlaceFrame_SetStatusText(doc
->frame
, NULL
);
1260 doc
->basedoc
.window
->readystate
= READYSTATE_UNINITIALIZED
;
1263 IOleCommandTarget
*cmdtrg
;
1265 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&cmdtrg
);
1266 if(SUCCEEDED(hres
)) {
1271 IOleCommandTarget_Exec(cmdtrg
, &CGID_ShellDocView
, 37, 0, &var
, NULL
);
1273 IOleCommandTarget_Release(cmdtrg
);
1278 DOCHOSTUIINFO hostinfo
;
1280 memset(&hostinfo
, 0, sizeof(DOCHOSTUIINFO
));
1281 hostinfo
.cbSize
= sizeof(DOCHOSTUIINFO
);
1282 hres
= IDocHostUIHandler_GetHostInfo(doc
->hostui
, &hostinfo
);
1284 /* FIXME: use hostinfo */
1285 TRACE("hostinfo = {%u %08x %08x %s %s}\n",
1286 hostinfo
.cbSize
, hostinfo
.dwFlags
, hostinfo
.dwDoubleClick
,
1287 debugstr_w(hostinfo
.pchHostCss
), debugstr_w(hostinfo
.pchHostNS
));
1290 update_doc(&doc
->basedoc
, UPDATE_UI
);
1292 if(doc
->basedoc
.window
->mon
) {
1293 /* FIXME: We should find nicer way to do this */
1294 remove_target_tasks(doc
->basedoc
.task_magic
);
1296 mon
= doc
->basedoc
.window
->mon
;
1297 IMoniker_AddRef(mon
);
1299 static const WCHAR about_blankW
[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
1301 hres
= CreateURLMoniker(NULL
, about_blankW
, &mon
);
1303 FIXME("CreateURLMoniker failed: %08x\n", hres
);
1308 hres
= IPersistMoniker_Load(&doc
->basedoc
.IPersistMoniker_iface
, TRUE
, mon
, NULL
, 0);
1309 IMoniker_Release(mon
);
1313 if(doc
->ui_active
) {
1315 call_set_active_object(doc
->ip_window
, NULL
);
1317 IDocHostUIHandler_HideUI(doc
->hostui
);
1320 if(doc
->ui_active
) {
1321 RECT rcBorderWidths
;
1324 IDocHostUIHandler_ShowUI(doc
->hostui
, DOCHOSTUITYPE_AUTHOR
,
1325 &doc
->basedoc
.IOleInPlaceActiveObject_iface
, &doc
->basedoc
.IOleCommandTarget_iface
,
1326 doc
->frame
, doc
->ip_window
);
1329 call_set_active_object(doc
->ip_window
, &doc
->basedoc
.IOleInPlaceActiveObject_iface
);
1331 memset(&rcBorderWidths
, 0, sizeof(rcBorderWidths
));
1333 IOleInPlaceFrame_SetBorderSpace(doc
->frame
, &rcBorderWidths
);