mshtml: Pass HTMLDOMNode pointer to its destructor.
[wine.git] / dlls / mshtml / editor.c
blob239bba552a9250eb90184871b2e3d0d57df23804
1 /*
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
19 #include "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31 #include "mshtmcid.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "mshtml_private.h"
37 #include "resource.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)
104 nsresult nsres;
106 if(!This->editor_controller)
107 return;
109 nsres = nsIController_DoCommand(This->editor_controller, cmd);
110 if(NS_FAILED(nsres))
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;
117 nsresult nsres;
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);
122 return nsres;
125 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, NULL, nsparam);
126 if(NS_FAILED(nsres))
127 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
129 nsICommandManager_Release(cmdmgr);
130 return nsres;
133 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
135 nsICommandParams *nsparam;
136 PRBool b = FALSE;
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)
158 return;
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;
171 char *align = NULL;
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;
194 nsresult nsres;
196 if(!This->nscontainer)
197 return NULL;
199 nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
200 if(NS_FAILED(nsres))
201 return NULL;
203 nsIDOMWindow_GetSelection(dom_window, &nsselection);
204 nsIDOMWindow_Release(dom_window);
206 return nsselection;
210 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
212 PRBool has_children;
213 PRUint32 child_cnt, i;
214 nsIDOMNode *child_node;
215 nsIDOMNodeList *node_list;
216 PRUint16 node_type;
218 nsIDOMElement_HasChildNodes(elem, &has_children);
219 if(!has_children)
220 return;
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;
231 nsAString tag_str;
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;
262 nsAString tag_str;
263 LPCWSTR tag;
264 PRUint16 node_type;
265 nsresult nsres;
267 *ret = 0;
269 if(!nsselection)
270 return;
272 nsISelection_GetFocusNode(nsselection, &node);
273 nsISelection_Release(nsselection);
275 while(node) {
276 nsres = nsIDOMNode_GetNodeType(node, &node_type);
277 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
278 break;
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;
289 LPCWSTR val;
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);
299 if(*val) {
300 TRACE("found size %s\n", debugstr_w(val));
301 strcpyW(ret, val);
304 nsAString_Finish(&size_str);
305 nsAString_Finish(&val_str);
308 nsAString_Finish(&tag_str);
310 nsIDOMElement_Release(elem);
313 if(*ret)
314 break;
316 tmp_node = node;
317 nsIDOMNode_GetParentNode(node, &node);
318 nsIDOMNode_Release(tmp_node);
321 if(node)
322 nsIDOMNode_Release(node);
325 static void set_font_size(HTMLDocument *This, LPCWSTR size)
327 nsISelection *nsselection;
328 PRBool collapsed;
329 nsIDOMDocument *nsdoc;
330 nsIDOMElement *elem;
331 nsIDOMRange *range;
332 PRInt32 range_cnt = 0;
333 nsAString font_str;
334 nsAString size_str;
335 nsAString val_str;
336 nsresult nsres;
338 nsselection = get_ns_selection(This);
340 if(!nsselection)
341 return;
343 nsISelection_GetRangeCount(nsselection, &range_cnt);
344 if(range_cnt != 1) {
345 FIXME("range_cnt %d not supprted\n", range_cnt);
346 if(!range_cnt) {
347 nsISelection_Release(nsselection);
348 return;
352 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
353 if(NS_FAILED(nsres))
354 return;
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);
369 if(collapsed) {
370 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
371 }else {
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])
390 int i=0;
391 PRBool b;
393 nsIDOMKeyEvent_GetCtrlKey(event, &b);
394 if(b)
395 i |= 1;
397 nsIDOMKeyEvent_GetShiftKey(event, &b);
398 if(b)
399 i |= 2;
401 if(cmds[i])
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;
410 PRUint32 code;
412 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
414 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
416 switch(code) {
417 case DOM_VK_LEFT: {
418 static const char * const cmds[] = {
419 NSCMD_CHARPREVIOUS,
420 NSCMD_WORDPREVIOUS,
421 NSCMD_SELECTCHARPREVIOUS,
422 NSCMD_SELECTWORDPREVIOUS
425 TRACE("left\n");
426 handle_arrow_key(This, key_event, cmds);
427 break;
429 case DOM_VK_RIGHT: {
430 static const char * const cmds[] = {
431 NSCMD_CHARNEXT,
432 NSCMD_WORDNEXT,
433 NSCMD_SELECTCHARNEXT,
434 NSCMD_SELECTWORDNEXT
437 TRACE("right\n");
438 handle_arrow_key(This, key_event, cmds);
439 break;
441 case DOM_VK_UP: {
442 static const char * const cmds[] = {
443 NSCMD_LINEPREVIOUS,
444 NSCMD_MOVEPAGEUP,
445 NSCMD_SELECTLINEPREVIOUS,
446 NSCMD_SELECTPAGEUP
449 TRACE("up\n");
450 handle_arrow_key(This, key_event, cmds);
451 break;
453 case DOM_VK_DOWN: {
454 static const char * const cmds[] = {
455 NSCMD_LINENEXT,
456 NSCMD_MOVEPAGEDOWN,
457 NSCMD_SELECTLINENEXT,
458 NSCMD_SELECTPAGEDOWN
461 TRACE("down\n");
462 handle_arrow_key(This, key_event, cmds);
463 break;
465 case DOM_VK_DELETE: {
466 static const char * const cmds[] = {
467 NSCMD_DELETECHARFORWARD,
468 NSCMD_DELETEWORDFORWARD,
469 NULL, NULL
472 TRACE("delete\n");
473 handle_arrow_key(This, key_event, cmds);
474 break;
476 case DOM_VK_HOME: {
477 static const char * const cmds[] = {
478 NSCMD_BEGINLINE,
479 NSCMD_MOVETOP,
480 NSCMD_SELECTBEGINLINE,
481 NSCMD_SELECTTOP
484 TRACE("home\n");
485 handle_arrow_key(This, key_event, cmds);
486 break;
488 case DOM_VK_END: {
489 static const char * const cmds[] = {
490 NSCMD_ENDLINE,
491 NSCMD_MOVEBOTTOM,
492 NSCMD_SELECTENDLINE,
493 NSCMD_SELECTBOTTOM
496 TRACE("end\n");
497 handle_arrow_key(This, key_event, cmds);
498 break;
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];
511 RECT rcBorderWidths;
513 if(This->ip_window)
514 IOleInPlaceUIWindow_SetActiveObject(This->ip_window, NULL, NULL);
515 if(This->hostui)
516 IDocHostUIHandler_HideUI(This->hostui);
518 if(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));
525 if(This->ip_window)
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);
550 return S_OK;
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);
559 return E_FAIL;
562 if(in) {
563 char *stra;
564 DWORD len;
566 if(V_VT(in) != VT_BSTR) {
567 FIXME("Unsupported vt=%d\n", V_VT(out));
568 return E_INVALIDARG;
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);
579 mshtml_free(stra);
581 update_doc(This, UPDATE_UI);
584 if(out) {
585 nsICommandParams *nsparam;
586 LPWSTR strw;
587 char *stra;
588 DWORD len;
589 nsresult nsres;
591 nsparam = create_nscommand_params();
593 nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
594 if(NS_FAILED(nsres))
595 return S_OK;
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);
603 nsfree(stra);
605 V_VT(out) = VT_BSTR;
606 V_BSTR(out) = SysAllocString(strw);
607 mshtml_free(strw);
610 return S_OK;
613 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
615 TRACE("(%p)->(%p %p)\n", This, in, out);
617 if(in) {
618 if(V_VT(in) == VT_I4) {
619 nsICommandParams *nsparam = create_nscommand_params();
620 char color_str[10];
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);
629 }else {
630 FIXME("unsupported in vt=%d\n", V_VT(in));
633 update_doc(This, UPDATE_UI);
636 if(out) {
637 FIXME("unsupported out\n");
638 return E_NOTIMPL;
641 return S_OK;
644 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
646 TRACE("(%p)->(%p %p)\n", This, in, out);
648 if(out) {
649 WCHAR val[10] = {0};
651 get_font_size(This, val);
652 V_VT(out) = VT_I4;
653 V_I4(out) = strtolW(val, NULL, 10);
656 if(in) {
657 switch(V_VT(in)) {
658 case VT_I4: {
659 WCHAR size[10];
660 static const WCHAR format[] = {'%','d',0};
661 wsprintfW(size, format, V_I4(in));
662 set_font_size(This, size);
663 break;
665 case VT_BSTR:
666 set_font_size(This, V_BSTR(in));
667 break;
668 default:
669 FIXME("unsupported vt %d\n", V_VT(in));
672 update_doc(This, UPDATE_UI);
675 return S_OK;
678 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
681 FIXME("(%p)->(%p %p)\n", This, in, out);
682 return E_NOTIMPL;
685 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
687 TRACE("(%p)\n", This);
689 if(in || out)
690 FIXME("unsupported args\n");
692 if(This->nscontainer)
693 do_ns_command(This->nscontainer, NSCMD_SELECTALL, NULL);
695 update_doc(This, UPDATE_UI);
696 return S_OK;
699 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
701 TRACE("(%p)\n", This);
703 if(in || out)
704 FIXME("unsupported args\n");
706 if(This->nscontainer)
707 do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
709 update_doc(This, UPDATE_UI);
710 return S_OK;
713 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
715 TRACE("(%p)\n", This);
717 if(in || out)
718 FIXME("unsupported args\n");
720 if(This->nscontainer)
721 do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
723 update_doc(This, UPDATE_UI);
724 return S_OK;
727 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
729 switch(cmd->cmdID) {
730 case IDM_JUSTIFYCENTER:
731 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
732 cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
733 break;
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;
739 else
740 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
741 break;
742 case IDM_JUSTIFYRIGHT:
743 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
744 cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
745 break;
748 return S_OK;
751 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
753 TRACE("(%p)\n", This);
755 if(in || out)
756 FIXME("unsupported args\n");
758 set_ns_align(This, NSALIGN_CENTER);
759 update_doc(This, UPDATE_UI);
760 return S_OK;
763 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
765 TRACE("(%p)\n", This);
767 if(in || out)
768 FIXME("unsupported args\n");
770 set_ns_align(This, NSALIGN_LEFT);
771 update_doc(This, UPDATE_UI);
772 return S_OK;
775 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
777 TRACE("(%p)\n", This);
779 if(in || out)
780 FIXME("unsupported args\n");
782 set_ns_align(This, NSALIGN_RIGHT);
783 update_doc(This, UPDATE_UI);
784 return S_OK;
787 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
789 TRACE("(%p)\n", This);
791 if(in || out)
792 FIXME("unsupported args\n");
794 if(This->nscontainer)
795 do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
797 update_doc(This, UPDATE_UI);
798 return S_OK;
801 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
803 TRACE("(%p)\n", This);
805 if(in || out)
806 FIXME("unsupported args\n");
808 if(This->nscontainer)
809 do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
811 update_doc(This, UPDATE_UI);
812 return S_OK;
815 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
817 TRACE("(%p)\n", This);
819 if(in || out)
820 FIXME("unsupported args\n");
822 if(This->nscontainer)
823 do_ns_command(This->nscontainer, NSCMD_OL, NULL);
825 update_doc(This, UPDATE_UI);
826 return S_OK;
829 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
831 TRACE("(%p)\n", This);
833 if(in || out)
834 FIXME("unsupported args\n");
836 if(This->nscontainer)
837 do_ns_command(This->nscontainer, NSCMD_UL, NULL);
839 update_doc(This, UPDATE_UI);
840 return S_OK;
843 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
845 TRACE("(%p)\n", This);
847 if(in || out)
848 FIXME("unsupported args\n");
850 if(This->nscontainer)
851 do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
853 update_doc(This, UPDATE_UI);
854 return S_OK;
857 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
859 TRACE("(%p)\n", This);
861 if(in || out)
862 FIXME("unsupported args\n");
864 if(This->nscontainer)
865 do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
867 update_doc(This, UPDATE_UI);
868 return S_OK;
871 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
873 WCHAR *ptr;
875 if(out || !in || V_VT(in) != VT_BSTR) {
876 WARN("invalid arg\n");
877 return E_INVALIDARG;
880 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
882 update_doc(This, UPDATE_UI);
884 ptr = V_BSTR(in);
885 if(*ptr == '1')
886 exec_bold(This, cmdexecopt, NULL, NULL);
887 ptr = strchrW(ptr, ',');
888 if(!ptr)
889 return S_OK;
891 if(*++ptr == '1')
892 exec_italic(This, cmdexecopt, NULL, NULL);
893 ptr = strchrW(ptr, ',');
894 if(!ptr)
895 return S_OK;
897 if(*++ptr == '1')
898 exec_underline(This, cmdexecopt, NULL, NULL);
899 ptr = strchrW(ptr, ',');
900 if(!ptr)
901 return S_OK;
903 if(isdigitW(*++ptr)) {
904 VARIANT v;
906 V_VT(&v) = VT_I4;
907 V_I4(&v) = *ptr-'0';
909 exec_fontsize(This, cmdexecopt, &v, NULL);
911 ptr = strchrW(ptr, ',');
912 if(!ptr)
913 return S_OK;
915 if(*++ptr != ',')
916 FIXME("set font color\n");
917 ptr = strchrW(ptr, ',');
918 if(!ptr)
919 return S_OK;
921 if(*++ptr != ',')
922 FIXME("set background color\n");
923 ptr = strchrW(ptr, ',');
924 if(!ptr)
925 return S_OK;
927 ptr++;
928 if(*ptr) {
929 VARIANT v;
931 V_VT(&v) = VT_BSTR;
932 V_BSTR(&v) = SysAllocString(ptr);
934 exec_fontname(This, cmdexecopt, &v, NULL);
936 SysFreeString(V_BSTR(&v));
939 return S_OK;
942 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
944 update_doc(This, UPDATE_UI);
946 if(!This->nscontainer)
947 return E_FAIL;
949 do_ns_editor_command(This->nscontainer, NSCMD_COPY);
950 return S_OK;
953 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
955 update_doc(This, UPDATE_UI);
957 if(!This->nscontainer)
958 return E_FAIL;
960 do_ns_editor_command(This->nscontainer, NSCMD_CUT);
961 return S_OK;
964 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
966 update_doc(This, UPDATE_UI);
968 if(!This->nscontainer)
969 return E_FAIL;
971 do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
972 return S_OK;
975 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
977 switch(cmd->cmdID) {
978 case IDM_DELETE:
979 TRACE("CGID_MSHTML: IDM_DELETE\n");
980 cmd->cmdf = query_ns_edit_status(This, NULL);
981 break;
982 case IDM_FONTNAME:
983 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
984 cmd->cmdf = query_ns_edit_status(This, NULL);
985 break;
986 case IDM_FONTSIZE:
987 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
988 cmd->cmdf = query_ns_edit_status(This, NULL);
989 break;
990 case IDM_BOLD:
991 TRACE("CGID_MSHTML: IDM_BOLD\n");
992 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
993 break;
994 case IDM_FORECOLOR:
995 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
996 cmd->cmdf = query_ns_edit_status(This, NULL);
997 break;
998 case IDM_ITALIC:
999 TRACE("CGID_MSHTML: IDM_ITALIC\n");
1000 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
1001 break;
1002 case IDM_UNDERLINE:
1003 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
1004 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
1005 break;
1006 case IDM_HORIZONTALLINE:
1007 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1008 cmd->cmdf = query_ns_edit_status(This, NULL);
1009 break;
1010 case IDM_ORDERLIST:
1011 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1012 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1013 break;
1014 case IDM_UNORDERLIST:
1015 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1016 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1017 break;
1018 case IDM_INDENT:
1019 TRACE("CGID_MSHTML: IDM_INDENT\n");
1020 cmd->cmdf = query_ns_edit_status(This, NULL);
1021 break;
1022 case IDM_OUTDENT:
1023 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1024 cmd->cmdf = query_ns_edit_status(This, NULL);
1025 break;
1026 case IDM_HYPERLINK:
1027 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1028 cmd->cmdf = query_ns_edit_status(This, NULL);
1029 break;
1032 return S_OK;
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};
1039 switch (msg)
1041 case WM_INITDIALOG:
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};
1049 INT def_idx;
1050 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1051 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1052 INT len;
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);
1068 SetFocus(hwndURL);
1069 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1070 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1072 return FALSE;
1074 case WM_COMMAND:
1075 switch (wparam)
1077 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1078 EndDialog(hwnd, wparam);
1079 return TRUE;
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);
1088 return TRUE;
1090 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1092 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1093 INT item;
1094 INT len;
1095 LPWSTR type;
1096 LPWSTR url;
1097 LPWSTR p;
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))
1107 *type = '\0';
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] == '/')
1117 p += 3;
1118 if (!p) p = url;
1119 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1121 /* add new protocol */
1122 if (*type != '\0')
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);
1132 return TRUE;
1135 return FALSE;
1136 case WM_CLOSE:
1137 EndDialog(hwnd, IDCANCEL);
1138 return TRUE;
1139 default:
1140 return FALSE;
1144 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1146 BSTR url = NULL;
1147 INT ret;
1148 nsAString ns_url;
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;
1161 url = V_BSTR(in);
1163 else
1165 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1166 if (ret != IDOK)
1167 return OLECMDERR_E_CANCELED;
1170 nsselection = get_ns_selection(This);
1171 if (!nsselection)
1172 return E_FAIL;
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;
1187 nsAString a_str;
1188 nsAString href_str;
1189 nsresult nsres;
1191 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
1192 if(NS_FAILED(nsres))
1193 return E_FAIL;
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);
1214 if (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);
1226 else
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)
1240 SysFreeString(url);
1242 return S_OK;
1245 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1247 TRACE("(%p)->(%p)\n", This, cmd);
1249 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1250 return S_OK;
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},
1273 {0,NULL,NULL}
1276 void init_editor(HTMLDocument *This)
1278 update_doc(This, UPDATE_UI);
1280 if(!This->nscontainer)
1281 return;
1283 set_ns_fontname(This->nscontainer, "Times New Roman");
1286 HRESULT editor_is_dirty(HTMLDocument *This)
1288 PRBool modified;
1290 if(!This->nscontainer || !This->nscontainer->editor)
1291 return S_FALSE;
1293 nsIEditor_GetDocumentModified(This->nscontainer->editor, &modified);
1295 return modified ? S_OK : S_FALSE;