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
);
509 if(This
->ui_active
) {
510 OLECHAR wszHTMLDocument
[30];
514 IOleInPlaceUIWindow_SetActiveObject(This
->ip_window
, NULL
, NULL
);
516 IDocHostUIHandler_HideUI(This
->hostui
);
519 IDocHostUIHandler_ShowUI(This
->hostui
, DOCHOSTUITYPE_AUTHOR
, ACTOBJ(This
), CMDTARGET(This
),
520 This
->frame
, This
->ip_window
);
522 LoadStringW(hInst
, IDS_HTMLDOCUMENT
, wszHTMLDocument
,
523 sizeof(wszHTMLDocument
)/sizeof(WCHAR
));
526 IOleInPlaceUIWindow_SetActiveObject(This
->ip_window
, ACTOBJ(This
), wszHTMLDocument
);
528 memset(&rcBorderWidths
, 0, sizeof(rcBorderWidths
));
529 IOleInPlaceFrame_SetBorderSpace(This
->frame
, &rcBorderWidths
);
533 static void set_ns_fontname(NSContainer
*This
, const char *fontname
)
535 nsICommandParams
*nsparam
= create_nscommand_params();
537 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, fontname
);
538 do_ns_command(This
, NSCMD_FONTFACE
, nsparam
);
539 nsICommandParams_Release(nsparam
);
542 static HRESULT
exec_delete(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
544 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
546 if(This
->nscontainer
)
547 do_ns_editor_command(This
->nscontainer
, NSCMD_DELETECHARFORWARD
);
549 update_doc(This
, UPDATE_UI
);
553 static HRESULT
exec_fontname(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
555 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
557 if(!This
->nscontainer
) {
558 update_doc(This
, UPDATE_UI
);
566 if(V_VT(in
) != VT_BSTR
) {
567 FIXME("Unsupported vt=%d\n", V_VT(out
));
571 TRACE("%s\n", debugstr_w(V_BSTR(in
)));
573 len
= WideCharToMultiByte(CP_ACP
, 0, V_BSTR(in
), -1, NULL
, 0, NULL
, NULL
);
574 stra
= mshtml_alloc(len
);
575 WideCharToMultiByte(CP_ACP
, 0, V_BSTR(in
), -1, stra
, -1, NULL
, NULL
);
577 set_ns_fontname(This
->nscontainer
, stra
);
581 update_doc(This
, UPDATE_UI
);
585 nsICommandParams
*nsparam
;
591 nsparam
= create_nscommand_params();
593 nsres
= get_ns_command_state(This
->nscontainer
, NSCMD_FONTFACE
, nsparam
);
597 nsICommandParams_GetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, &stra
);
598 nsICommandParams_Release(nsparam
);
600 len
= MultiByteToWideChar(CP_ACP
, 0, stra
, -1, NULL
, 0);
601 strw
= mshtml_alloc(len
*sizeof(WCHAR
));
602 MultiByteToWideChar(CP_ACP
, 0, stra
, -1, strw
, -1);
606 V_BSTR(out
) = SysAllocString(strw
);
613 static HRESULT
exec_forecolor(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
615 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
618 if(V_VT(in
) == VT_I4
) {
619 nsICommandParams
*nsparam
= create_nscommand_params();
622 sprintf(color_str
, "#%02x%02x%02x",
623 V_I4(in
)&0xff, (V_I4(in
)>>8)&0xff, (V_I4(in
)>>16)&0xff);
625 nsICommandParams_SetCStringValue(nsparam
, NSSTATE_ATTRIBUTE
, color_str
);
626 do_ns_command(This
->nscontainer
, NSCMD_FONTCOLOR
, nsparam
);
628 nsICommandParams_Release(nsparam
);
630 FIXME("unsupported in vt=%d\n", V_VT(in
));
633 update_doc(This
, UPDATE_UI
);
637 FIXME("unsupported out\n");
644 static HRESULT
exec_fontsize(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
646 TRACE("(%p)->(%p %p)\n", This
, in
, out
);
651 get_font_size(This
, val
);
653 V_I4(out
) = strtolW(val
, NULL
, 10);
660 static const WCHAR format
[] = {'%','d',0};
661 wsprintfW(size
, format
, V_I4(in
));
662 set_font_size(This
, size
);
666 set_font_size(This
, V_BSTR(in
));
669 FIXME("unsupported vt %d\n", V_VT(in
));
672 update_doc(This
, UPDATE_UI
);
678 static HRESULT
exec_font(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
681 FIXME("(%p)->(%p %p)\n", This
, in
, out
);
685 static HRESULT
exec_selectall(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
687 TRACE("(%p)\n", This
);
690 FIXME("unsupported args\n");
692 if(This
->nscontainer
)
693 do_ns_command(This
->nscontainer
, NSCMD_SELECTALL
, NULL
);
695 update_doc(This
, UPDATE_UI
);
699 static HRESULT
exec_bold(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
701 TRACE("(%p)\n", This
);
704 FIXME("unsupported args\n");
706 if(This
->nscontainer
)
707 do_ns_command(This
->nscontainer
, NSCMD_BOLD
, NULL
);
709 update_doc(This
, UPDATE_UI
);
713 static HRESULT
exec_italic(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
715 TRACE("(%p)\n", This
);
718 FIXME("unsupported args\n");
720 if(This
->nscontainer
)
721 do_ns_command(This
->nscontainer
, NSCMD_ITALIC
, NULL
);
723 update_doc(This
, UPDATE_UI
);
727 static HRESULT
query_justify(HTMLDocument
*This
, OLECMD
*cmd
)
730 case IDM_JUSTIFYCENTER
:
731 TRACE("(%p) IDM_JUSTIFYCENTER\n", This
);
732 cmd
->cmdf
= query_align_status(This
, NSALIGN_CENTER
);
734 case IDM_JUSTIFYLEFT
:
735 TRACE("(%p) IDM_JUSTIFYLEFT\n", This
);
736 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
737 if(This
->usermode
!= EDITMODE
|| This
->readystate
< READYSTATE_INTERACTIVE
)
738 cmd
->cmdf
= OLECMDF_SUPPORTED
;
740 cmd
->cmdf
= OLECMDF_SUPPORTED
| OLECMDF_ENABLED
;
742 case IDM_JUSTIFYRIGHT
:
743 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This
);
744 cmd
->cmdf
= query_align_status(This
, NSALIGN_RIGHT
);
751 static HRESULT
exec_justifycenter(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
753 TRACE("(%p)\n", This
);
756 FIXME("unsupported args\n");
758 set_ns_align(This
, NSALIGN_CENTER
);
759 update_doc(This
, UPDATE_UI
);
763 static HRESULT
exec_justifyleft(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
765 TRACE("(%p)\n", This
);
768 FIXME("unsupported args\n");
770 set_ns_align(This
, NSALIGN_LEFT
);
771 update_doc(This
, UPDATE_UI
);
775 static HRESULT
exec_justifyright(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
777 TRACE("(%p)\n", This
);
780 FIXME("unsupported args\n");
782 set_ns_align(This
, NSALIGN_RIGHT
);
783 update_doc(This
, UPDATE_UI
);
787 static HRESULT
exec_underline(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
789 TRACE("(%p)\n", This
);
792 FIXME("unsupported args\n");
794 if(This
->nscontainer
)
795 do_ns_command(This
->nscontainer
, NSCMD_UNDERLINE
, NULL
);
797 update_doc(This
, UPDATE_UI
);
801 static HRESULT
exec_horizontalline(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
803 TRACE("(%p)\n", This
);
806 FIXME("unsupported args\n");
808 if(This
->nscontainer
)
809 do_ns_command(This
->nscontainer
, NSCMD_INSERTHR
, NULL
);
811 update_doc(This
, UPDATE_UI
);
815 static HRESULT
exec_orderlist(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
817 TRACE("(%p)\n", This
);
820 FIXME("unsupported args\n");
822 if(This
->nscontainer
)
823 do_ns_command(This
->nscontainer
, NSCMD_OL
, NULL
);
825 update_doc(This
, UPDATE_UI
);
829 static HRESULT
exec_unorderlist(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
831 TRACE("(%p)\n", This
);
834 FIXME("unsupported args\n");
836 if(This
->nscontainer
)
837 do_ns_command(This
->nscontainer
, NSCMD_UL
, NULL
);
839 update_doc(This
, UPDATE_UI
);
843 static HRESULT
exec_indent(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
845 TRACE("(%p)\n", This
);
848 FIXME("unsupported args\n");
850 if(This
->nscontainer
)
851 do_ns_command(This
->nscontainer
, NSCMD_INDENT
, NULL
);
853 update_doc(This
, UPDATE_UI
);
857 static HRESULT
exec_outdent(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
859 TRACE("(%p)\n", This
);
862 FIXME("unsupported args\n");
864 if(This
->nscontainer
)
865 do_ns_command(This
->nscontainer
, NSCMD_OUTDENT
, NULL
);
867 update_doc(This
, UPDATE_UI
);
871 static HRESULT
exec_composesettings(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
875 if(out
|| !in
|| V_VT(in
) != VT_BSTR
) {
876 WARN("invalid arg\n");
880 TRACE("(%p)->(%x %s)\n", This
, cmdexecopt
, debugstr_w(V_BSTR(in
)));
882 update_doc(This
, UPDATE_UI
);
886 exec_bold(This
, cmdexecopt
, NULL
, NULL
);
887 ptr
= strchrW(ptr
, ',');
892 exec_italic(This
, cmdexecopt
, NULL
, NULL
);
893 ptr
= strchrW(ptr
, ',');
898 exec_underline(This
, cmdexecopt
, NULL
, NULL
);
899 ptr
= strchrW(ptr
, ',');
903 if(isdigitW(*++ptr
)) {
909 exec_fontsize(This
, cmdexecopt
, &v
, NULL
);
911 ptr
= strchrW(ptr
, ',');
916 FIXME("set font color\n");
917 ptr
= strchrW(ptr
, ',');
922 FIXME("set background color\n");
923 ptr
= strchrW(ptr
, ',');
932 V_BSTR(&v
) = SysAllocString(ptr
);
934 exec_fontname(This
, cmdexecopt
, &v
, NULL
);
936 SysFreeString(V_BSTR(&v
));
942 HRESULT
editor_exec_copy(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
944 update_doc(This
, UPDATE_UI
);
946 if(!This
->nscontainer
)
949 do_ns_editor_command(This
->nscontainer
, NSCMD_COPY
);
953 HRESULT
editor_exec_cut(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
955 update_doc(This
, UPDATE_UI
);
957 if(!This
->nscontainer
)
960 do_ns_editor_command(This
->nscontainer
, NSCMD_CUT
);
964 HRESULT
editor_exec_paste(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
966 update_doc(This
, UPDATE_UI
);
968 if(!This
->nscontainer
)
971 do_ns_editor_command(This
->nscontainer
, NSCMD_PASTE
);
975 static HRESULT
query_edit_status(HTMLDocument
*This
, OLECMD
*cmd
)
979 TRACE("CGID_MSHTML: IDM_DELETE\n");
980 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
983 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
984 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
987 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
988 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
991 TRACE("CGID_MSHTML: IDM_BOLD\n");
992 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_BOLD
);
995 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
996 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
999 TRACE("CGID_MSHTML: IDM_ITALIC\n");
1000 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_ITALIC
);
1003 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
1004 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_UNDERLINE
);
1006 case IDM_HORIZONTALLINE
:
1007 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1008 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1011 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1012 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_OL
);
1014 case IDM_UNORDERLIST
:
1015 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1016 cmd
->cmdf
= query_ns_edit_status(This
, NSCMD_UL
);
1019 TRACE("CGID_MSHTML: IDM_INDENT\n");
1020 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1023 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1024 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1027 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1028 cmd
->cmdf
= query_ns_edit_status(This
, NULL
);
1035 static INT_PTR CALLBACK
hyperlink_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
1037 static const WCHAR wszOther
[] = {'(','o','t','h','e','r',')',0};
1043 static const WCHAR wszFile
[] = {'f','i','l','e',':',0};
1044 static const WCHAR wszFtp
[] = {'f','t','p',':',0};
1045 static const WCHAR wszHttp
[] = {'h','t','t','p',':',0};
1046 static const WCHAR wszHttps
[] = {'h','t','t','p','s',':',0};
1047 static const WCHAR wszMailto
[] = {'m','a','i','l','t','o',':',0};
1048 static const WCHAR wszNews
[] = {'n','e','w','s',':',0};
1050 HWND hwndCB
= GetDlgItem(hwnd
, IDC_TYPE
);
1051 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1054 SetWindowLongPtrW(hwnd
, DWLP_USER
, lparam
);
1056 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszOther
);
1057 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszFile
);
1058 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszFtp
);
1059 def_idx
= SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszHttp
);
1060 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszHttps
);
1061 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszMailto
);
1062 SendMessageW(hwndCB
, CB_INSERTSTRING
, -1, (LPARAM
)wszNews
);
1063 SendMessageW(hwndCB
, CB_SETCURSEL
, def_idx
, 0);
1065 /* force the updating of the URL edit box */
1066 SendMessageW(hwnd
, WM_COMMAND
, MAKEWPARAM(IDC_TYPE
, CBN_SELCHANGE
), (LPARAM
)hwndCB
);
1069 len
= SendMessageW(hwndURL
, WM_GETTEXTLENGTH
, 0, 0);
1070 SendMessageW(hwndURL
, EM_SETSEL
, 0, len
);
1077 case MAKEWPARAM(IDCANCEL
, BN_CLICKED
):
1078 EndDialog(hwnd
, wparam
);
1080 case MAKEWPARAM(IDOK
, BN_CLICKED
):
1082 BSTR
*url
= (BSTR
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
1083 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1084 INT len
= GetWindowTextLengthW(hwndURL
);
1085 *url
= SysAllocStringLen(NULL
, len
+ 1);
1086 GetWindowTextW(hwndURL
, *url
, len
+ 1);
1087 EndDialog(hwnd
, wparam
);
1090 case MAKEWPARAM(IDC_TYPE
, CBN_SELCHANGE
):
1092 HWND hwndURL
= GetDlgItem(hwnd
, IDC_URL
);
1098 static const WCHAR wszSlashSlash
[] = {'/','/'};
1100 /* get string of currently selected hyperlink type */
1101 item
= SendMessageW((HWND
)lparam
, CB_GETCURSEL
, 0, 0);
1102 len
= SendMessageW((HWND
)lparam
, CB_GETLBTEXTLEN
, item
, 0);
1103 type
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1104 SendMessageW((HWND
)lparam
, CB_GETLBTEXT
, item
, (LPARAM
)type
);
1106 if (!strcmpW(type
, wszOther
))
1109 /* get current URL */
1110 len
= GetWindowTextLengthW(hwndURL
);
1111 url
= HeapAlloc(GetProcessHeap(), 0, (len
+ strlenW(type
) + 3) * sizeof(WCHAR
));
1112 GetWindowTextW(hwndURL
, url
, len
+ 1);
1114 /* strip off old protocol */
1115 p
= strchrW(url
, ':');
1116 if (p
&& p
[1] == '/' && p
[2] == '/')
1119 memmove(url
+ (*type
!= '\0' ? strlenW(type
) + 2 : 0), p
, (len
+ 1 - (p
- url
)) * sizeof(WCHAR
));
1121 /* add new protocol */
1124 memcpy(url
, type
, strlenW(type
) * sizeof(WCHAR
));
1125 memcpy(url
+ strlenW(type
), wszSlashSlash
, sizeof(wszSlashSlash
));
1128 SetWindowTextW(hwndURL
, url
);
1130 HeapFree(GetProcessHeap(), 0, url
);
1131 HeapFree(GetProcessHeap(), 0, type
);
1137 EndDialog(hwnd
, IDCANCEL
);
1144 static HRESULT
exec_hyperlink(HTMLDocument
*This
, DWORD cmdexecopt
, VARIANT
*in
, VARIANT
*out
)
1149 PRBool insert_link_at_caret
;
1150 nsISelection
*nsselection
;
1152 FIXME("%p, 0x%x, %p, %p\n", This
, cmdexecopt
, in
, out
);
1154 if (cmdexecopt
== OLECMDEXECOPT_DONTPROMPTUSER
)
1156 if (!in
|| V_VT(in
) != VT_BSTR
)
1158 WARN("invalid arg\n");
1159 return E_INVALIDARG
;
1165 ret
= DialogBoxParamW(hInst
, MAKEINTRESOURCEW(IDD_HYPERLINK
), NULL
/* FIXME */, hyperlink_dlgproc
, (LPARAM
)&url
);
1167 return OLECMDERR_E_CANCELED
;
1170 nsselection
= get_ns_selection(This
);
1174 nsAString_Init(&ns_url
, url
);
1176 nsISelection_GetIsCollapsed(nsselection
, &insert_link_at_caret
);
1178 if (insert_link_at_caret
)
1180 static const WCHAR wszA
[] = {'a',0};
1181 static const WCHAR wszHref
[] = {'h','r','e','f',0};
1182 nsIHTMLEditor
*html_editor
;
1183 nsIDOMDocument
*nsdoc
;
1184 nsIDOMNode
*text_node
;
1185 nsIDOMElement
*anchor_elem
;
1186 nsIDOMNode
*unused_node
;
1191 nsres
= nsIWebNavigation_GetDocument(This
->nscontainer
->navigation
, &nsdoc
);
1192 if(NS_FAILED(nsres
))
1195 nsAString_Init(&a_str
, wszA
);
1196 nsAString_Init(&href_str
, wszHref
);
1198 /* create an element for the link */
1199 nsIDOMDocument_CreateElement(nsdoc
, &a_str
, &anchor_elem
);
1200 nsIDOMElement_SetAttribute(anchor_elem
, &href_str
, &ns_url
);
1202 nsAString_Finish(&href_str
);
1203 nsAString_Finish(&a_str
);
1205 /* create an element with text of URL */
1206 nsIDOMDocument_CreateTextNode(nsdoc
, &ns_url
, (nsIDOMText
**)&text_node
);
1208 /* wrap the <a> tags around the text element */
1209 nsIDOMElement_AppendChild(anchor_elem
, text_node
, &unused_node
);
1210 nsIDOMNode_Release(text_node
);
1211 nsIDOMNode_Release(unused_node
);
1213 nsIEditor_QueryInterface(This
->nscontainer
->editor
, &IID_nsIHTMLEditor
, (void **)&html_editor
);
1216 /* add them to the document at the caret position */
1217 nsres
= nsIHTMLEditor_InsertElementAtSelection(html_editor
, anchor_elem
, FALSE
);
1218 nsIHTMLEditor_Release(html_editor
);
1221 nsISelection_SelectAllChildren(nsselection
, (nsIDOMNode
*)anchor_elem
);
1223 nsIDOMElement_Release(anchor_elem
);
1224 nsIDOMDocument_Release(nsdoc
);
1228 nsICommandParams
*nsparam
= create_nscommand_params();
1230 nsICommandParams_SetStringValue(nsparam
, NSSTATE_ATTRIBUTE
, &ns_url
);
1231 do_ns_command(This
->nscontainer
, NSCMD_INSERTLINKNOUI
, nsparam
);
1232 nsICommandParams_Release(nsparam
);
1235 nsAString_Finish(&ns_url
);
1237 nsISelection_Release(nsselection
);
1239 if (cmdexecopt
!= OLECMDEXECOPT_DONTPROMPTUSER
)
1245 static HRESULT
query_selall_status(HTMLDocument
*This
, OLECMD
*cmd
)
1247 TRACE("(%p)->(%p)\n", This
, cmd
);
1249 cmd
->cmdf
= OLECMDF_SUPPORTED
|OLECMDF_ENABLED
;
1253 const cmdtable_t editmode_cmds
[] = {
1254 {IDM_DELETE
, query_edit_status
, exec_delete
},
1255 {IDM_FONTNAME
, query_edit_status
, exec_fontname
},
1256 {IDM_FONTSIZE
, query_edit_status
, exec_fontsize
},
1257 {IDM_SELECTALL
, query_selall_status
, exec_selectall
},
1258 {IDM_FORECOLOR
, query_edit_status
, exec_forecolor
},
1259 {IDM_BOLD
, query_edit_status
, exec_bold
},
1260 {IDM_ITALIC
, query_edit_status
, exec_italic
},
1261 {IDM_JUSTIFYCENTER
, query_justify
, exec_justifycenter
},
1262 {IDM_JUSTIFYRIGHT
, query_justify
, exec_justifyright
},
1263 {IDM_JUSTIFYLEFT
, query_justify
, exec_justifyleft
},
1264 {IDM_FONT
, NULL
, exec_font
},
1265 {IDM_UNDERLINE
, query_edit_status
, exec_underline
},
1266 {IDM_HORIZONTALLINE
, query_edit_status
, exec_horizontalline
},
1267 {IDM_ORDERLIST
, query_edit_status
, exec_orderlist
},
1268 {IDM_UNORDERLIST
, query_edit_status
, exec_unorderlist
},
1269 {IDM_INDENT
, query_edit_status
, exec_indent
},
1270 {IDM_OUTDENT
, query_edit_status
, exec_outdent
},
1271 {IDM_COMPOSESETTINGS
, NULL
, exec_composesettings
},
1272 {IDM_HYPERLINK
, query_edit_status
, exec_hyperlink
},
1276 void init_editor(HTMLDocument
*This
)
1278 update_doc(This
, UPDATE_UI
);
1280 if(!This
->nscontainer
)
1283 set_ns_fontname(This
->nscontainer
, "Times New Roman");
1286 HRESULT
editor_is_dirty(HTMLDocument
*This
)
1290 if(!This
->nscontainer
|| !This
->nscontainer
->editor
)
1293 nsIEditor_GetDocumentModified(This
->nscontainer
->editor
, &modified
);
1295 return modified
? S_OK
: S_FALSE
;