msxml3: Basic support for startElement()/endElement().
[wine/multimedia.git] / dlls / mshtml / editor.c
blob20d7d8fdb47988726c736b535b26fabeaadc7c9c
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 <stdarg.h>
20 #include <stdio.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28 #include "mshtmcid.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 #include "mshtml_private.h"
34 #include "resource.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_SELECTALL "cmd_selectAll"
64 #define NSCMD_SELECTBEGINLINE "cmd_selectBeginLine"
65 #define NSCMD_SELECTBOTTOM "cmd_selectBottom"
66 #define NSCMD_SELECTCHARNEXT "cmd_selectCharNext"
67 #define NSCMD_SELECTCHARPREVIOUS "cmd_selectCharPrevious"
68 #define NSCMD_SELECTENDLINE "cmd_selectEndLine"
69 #define NSCMD_SELECTLINENEXT "cmd_selectLineNext"
70 #define NSCMD_SELECTLINEPREVIOUS "cmd_selectLinePrevious"
71 #define NSCMD_SELECTPAGEDOWN "cmd_selectPageDown"
72 #define NSCMD_SELECTPAGEUP "cmd_selectPageUp"
73 #define NSCMD_SELECTTOP "cmd_selectTop"
74 #define NSCMD_SELECTWORDNEXT "cmd_selectWordNext"
75 #define NSCMD_SELECTWORDPREVIOUS "cmd_selectWordPrevious"
76 #define NSCMD_UL "cmd_ul"
77 #define NSCMD_UNDERLINE "cmd_underline"
78 #define NSCMD_WORDNEXT "cmd_wordNext"
79 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
81 #define NSSTATE_ATTRIBUTE "state_attribute"
82 #define NSSTATE_ALL "state_all"
84 #define NSALIGN_CENTER "center"
85 #define NSALIGN_LEFT "left"
86 #define NSALIGN_RIGHT "right"
88 #define DOM_VK_LEFT VK_LEFT
89 #define DOM_VK_UP VK_UP
90 #define DOM_VK_RIGHT VK_RIGHT
91 #define DOM_VK_DOWN VK_DOWN
92 #define DOM_VK_DELETE VK_DELETE
93 #define DOM_VK_HOME VK_HOME
94 #define DOM_VK_END VK_END
96 static const WCHAR fontW[] = {'f','o','n','t',0};
97 static const WCHAR sizeW[] = {'s','i','z','e',0};
99 void set_dirty(HTMLDocument *This, VARIANT_BOOL dirty)
101 nsresult nsres;
103 if(This->doc_obj->usermode != EDITMODE || !This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
104 return;
106 if(dirty) {
107 nsres = nsIEditor_IncrementModificationCount(This->doc_obj->nscontainer->editor, 1);
108 if(NS_FAILED(nsres))
109 ERR("IncrementModificationCount failed: %08x\n", nsres);
110 }else {
111 nsres = nsIEditor_ResetModificationCount(This->doc_obj->nscontainer->editor);
112 if(NS_FAILED(nsres))
113 ERR("ResetModificationCount failed: %08x\n", nsres);
117 static void do_ns_editor_command(NSContainer *This, const char *cmd)
119 nsresult nsres;
121 if(!This->editor_controller)
122 return;
124 nsres = nsIController_DoCommand(This->editor_controller, cmd);
125 if(NS_FAILED(nsres))
126 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
129 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
131 nsICommandManager *cmdmgr;
132 nsresult nsres;
134 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsICommandManager, (void**)&cmdmgr);
135 if(NS_FAILED(nsres)) {
136 ERR("Could not get nsICommandManager: %08x\n", nsres);
137 return nsres;
140 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, This->doc->basedoc.window->nswindow, nsparam);
141 if(NS_FAILED(nsres))
142 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
144 nsICommandManager_Release(cmdmgr);
145 return nsres;
148 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
150 nsICommandParams *nsparam;
151 PRBool b = FALSE;
153 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
154 return OLECMDF_SUPPORTED;
156 if(This->doc_obj->nscontainer && nscmd) {
157 nsparam = create_nscommand_params();
158 get_ns_command_state(This->doc_obj->nscontainer, nscmd, nsparam);
160 nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
162 nsICommandParams_Release(nsparam);
165 return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
168 static void set_ns_align(HTMLDocument *This, const char *align_str)
170 nsICommandParams *nsparam;
172 if(!This->doc_obj->nscontainer)
173 return;
175 nsparam = create_nscommand_params();
176 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
178 do_ns_command(This, NSCMD_ALIGN, nsparam);
180 nsICommandParams_Release(nsparam);
183 static DWORD query_align_status(HTMLDocument *This, const WCHAR *align)
185 DWORD ret = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
186 nsIDOMNSHTMLDocument *nsdoc;
187 nsAString justify_str;
188 PRBool b;
189 nsresult nsres;
191 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
192 return OLECMDF_SUPPORTED;
195 nsres = nsIDOMHTMLDocument_QueryInterface(This->doc_node->nsdoc, &IID_nsIDOMNSHTMLDocument,
196 (void**)&nsdoc);
197 if(NS_FAILED(nsres)) {
198 ERR("Could not get nsIDOMNSHTMLDocument iface: %08x\n", nsres);
199 return 0;
202 nsAString_Init(&justify_str, align);
203 nsres = nsIDOMNSHTMLDocument_QueryCommandState(nsdoc, &justify_str, &b);
204 nsAString_Finish(&justify_str);
205 if(NS_SUCCEEDED(nsres) && b)
206 ret |= OLECMDF_LATCHED;
208 nsIDOMNSHTMLDocument_Release(nsdoc);
209 return ret;
213 static nsISelection *get_ns_selection(HTMLDocument *This)
215 nsISelection *nsselection = NULL;
216 nsresult nsres;
218 nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
219 if(NS_FAILED(nsres))
220 ERR("GetSelection failed %08x\n", nsres);
222 return nsselection;
226 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
228 PRBool has_children;
229 PRUint32 child_cnt, i;
230 nsIDOMNode *child_node;
231 nsIDOMNodeList *node_list;
232 PRUint16 node_type;
234 nsIDOMElement_HasChildNodes(elem, &has_children);
235 if(!has_children)
236 return;
238 nsIDOMElement_GetChildNodes(elem, &node_list);
239 nsIDOMNodeList_GetLength(node_list, &child_cnt);
241 for(i=0; i<child_cnt; i++) {
242 nsIDOMNodeList_Item(node_list, i, &child_node);
244 nsIDOMNode_GetNodeType(child_node, &node_type);
245 if(node_type == ELEMENT_NODE) {
246 nsIDOMElement *child_elem;
247 nsAString tag_str;
248 const PRUnichar *ctag;
250 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
252 nsAString_Init(&tag_str, NULL);
253 nsIDOMElement_GetTagName(child_elem, &tag_str);
254 nsAString_GetData(&tag_str, &ctag);
256 if(!strcmpiW(ctag, tag))
257 /* FIXME: remove node if there are no more attributes */
258 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
260 nsAString_Finish(&tag_str);
262 remove_child_attr(child_elem, tag, attr_str);
264 nsIDOMNode_Release(child_elem);
267 nsIDOMNode_Release(child_node);
270 nsIDOMNodeList_Release(node_list);
273 static void get_font_size(HTMLDocument *This, WCHAR *ret)
275 nsISelection *nsselection = get_ns_selection(This);
276 nsIDOMElement *elem = NULL;
277 nsIDOMNode *node = NULL, *tmp_node;
278 nsAString tag_str;
279 LPCWSTR tag;
280 PRUint16 node_type;
281 nsresult nsres;
283 *ret = 0;
285 if(!nsselection)
286 return;
288 nsISelection_GetFocusNode(nsselection, &node);
289 nsISelection_Release(nsselection);
291 while(node) {
292 nsres = nsIDOMNode_GetNodeType(node, &node_type);
293 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
294 break;
296 if(node_type == ELEMENT_NODE) {
297 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
299 nsAString_Init(&tag_str, NULL);
300 nsIDOMElement_GetTagName(elem, &tag_str);
301 nsAString_GetData(&tag_str, &tag);
303 if(!strcmpiW(tag, fontW)) {
304 nsAString size_str, val_str;
305 LPCWSTR val;
307 TRACE("found font tag %p\n", elem);
309 nsAString_InitDepend(&size_str, sizeW);
310 nsAString_Init(&val_str, NULL);
312 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
313 nsAString_GetData(&val_str, &val);
315 if(*val) {
316 TRACE("found size %s\n", debugstr_w(val));
317 strcpyW(ret, val);
320 nsAString_Finish(&size_str);
321 nsAString_Finish(&val_str);
324 nsAString_Finish(&tag_str);
326 nsIDOMElement_Release(elem);
329 if(*ret)
330 break;
332 tmp_node = node;
333 nsIDOMNode_GetParentNode(node, &node);
334 nsIDOMNode_Release(tmp_node);
337 if(node)
338 nsIDOMNode_Release(node);
341 static void set_font_size(HTMLDocument *This, LPCWSTR size)
343 nsISelection *nsselection;
344 PRBool collapsed;
345 nsIDOMHTMLElement *elem;
346 nsIDOMRange *range;
347 PRInt32 range_cnt = 0;
348 nsAString size_str;
349 nsAString val_str;
351 if(!This->doc_node->nsdoc) {
352 WARN("NULL nsdoc\n");
353 return;
356 nsselection = get_ns_selection(This);
357 if(!nsselection)
358 return;
360 nsISelection_GetRangeCount(nsselection, &range_cnt);
361 if(range_cnt != 1) {
362 FIXME("range_cnt %d not supprted\n", range_cnt);
363 if(!range_cnt) {
364 nsISelection_Release(nsselection);
365 return;
369 create_nselem(This->doc_node, fontW, &elem);
371 nsAString_InitDepend(&size_str, sizeW);
372 nsAString_InitDepend(&val_str, size);
374 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
375 nsAString_Finish(&val_str);
377 nsISelection_GetRangeAt(nsselection, 0, &range);
378 nsISelection_GetIsCollapsed(nsselection, &collapsed);
379 nsISelection_RemoveAllRanges(nsselection);
381 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
383 if(collapsed) {
384 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
385 }else {
386 /* Remove all size attributes from the range */
387 remove_child_attr((nsIDOMElement*)elem, fontW, &size_str);
388 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
391 nsISelection_Release(nsselection);
392 nsIDOMRange_Release(range);
393 nsIDOMElement_Release(elem);
395 nsAString_Finish(&size_str);
397 set_dirty(This, VARIANT_TRUE);
400 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
402 int i=0;
403 PRBool b;
405 nsIDOMKeyEvent_GetCtrlKey(event, &b);
406 if(b)
407 i |= 1;
409 nsIDOMKeyEvent_GetShiftKey(event, &b);
410 if(b)
411 i |= 2;
413 if(cmds[i])
414 do_ns_editor_command(This->doc_obj->nscontainer, cmds[i]);
416 nsIDOMKeyEvent_PreventDefault(event);
419 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
421 nsIDOMKeyEvent *key_event;
422 PRUint32 code;
424 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
426 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
428 switch(code) {
429 case DOM_VK_LEFT: {
430 static const char * const cmds[] = {
431 NSCMD_CHARPREVIOUS,
432 NSCMD_WORDPREVIOUS,
433 NSCMD_SELECTCHARPREVIOUS,
434 NSCMD_SELECTWORDPREVIOUS
437 TRACE("left\n");
438 handle_arrow_key(This, key_event, cmds);
439 break;
441 case DOM_VK_RIGHT: {
442 static const char * const cmds[] = {
443 NSCMD_CHARNEXT,
444 NSCMD_WORDNEXT,
445 NSCMD_SELECTCHARNEXT,
446 NSCMD_SELECTWORDNEXT
449 TRACE("right\n");
450 handle_arrow_key(This, key_event, cmds);
451 break;
453 case DOM_VK_UP: {
454 static const char * const cmds[] = {
455 NSCMD_LINEPREVIOUS,
456 NSCMD_MOVEPAGEUP,
457 NSCMD_SELECTLINEPREVIOUS,
458 NSCMD_SELECTPAGEUP
461 TRACE("up\n");
462 handle_arrow_key(This, key_event, cmds);
463 break;
465 case DOM_VK_DOWN: {
466 static const char * const cmds[] = {
467 NSCMD_LINENEXT,
468 NSCMD_MOVEPAGEDOWN,
469 NSCMD_SELECTLINENEXT,
470 NSCMD_SELECTPAGEDOWN
473 TRACE("down\n");
474 handle_arrow_key(This, key_event, cmds);
475 break;
477 case DOM_VK_DELETE: {
478 static const char * const cmds[] = {
479 NSCMD_DELETECHARFORWARD,
480 NSCMD_DELETEWORDFORWARD,
481 NULL, NULL
484 TRACE("delete\n");
485 handle_arrow_key(This, key_event, cmds);
486 break;
488 case DOM_VK_HOME: {
489 static const char * const cmds[] = {
490 NSCMD_BEGINLINE,
491 NSCMD_MOVETOP,
492 NSCMD_SELECTBEGINLINE,
493 NSCMD_SELECTTOP
496 TRACE("home\n");
497 handle_arrow_key(This, key_event, cmds);
498 break;
500 case DOM_VK_END: {
501 static const char * const cmds[] = {
502 NSCMD_ENDLINE,
503 NSCMD_MOVEBOTTOM,
504 NSCMD_SELECTENDLINE,
505 NSCMD_SELECTBOTTOM
508 TRACE("end\n");
509 handle_arrow_key(This, key_event, cmds);
510 break;
514 nsIDOMKeyEvent_Release(key_event);
517 void handle_edit_load(HTMLDocument *This)
519 get_editor_controller(This->doc_obj->nscontainer);
522 static void set_ns_fontname(HTMLDocument *This, const char *fontname)
524 nsICommandParams *nsparam = create_nscommand_params();
526 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
527 do_ns_command(This, NSCMD_FONTFACE, nsparam);
528 nsICommandParams_Release(nsparam);
531 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
533 TRACE("(%p)->(%p %p)\n", This, in, out);
535 if(This->doc_obj->nscontainer)
536 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_DELETECHARFORWARD);
538 update_doc(This, UPDATE_UI);
539 return S_OK;
542 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
544 TRACE("(%p)->(%p %p)\n", This, in, out);
546 if(!This->doc_obj->nscontainer) {
547 update_doc(This, UPDATE_UI);
548 return E_FAIL;
551 if(in) {
552 char *stra;
554 if(V_VT(in) != VT_BSTR) {
555 FIXME("Unsupported vt=%d\n", V_VT(out));
556 return E_INVALIDARG;
559 TRACE("%s\n", debugstr_w(V_BSTR(in)));
561 stra = heap_strdupWtoA(V_BSTR(in));
562 set_ns_fontname(This, stra);
563 heap_free(stra);
565 update_doc(This, UPDATE_UI);
568 if(out) {
569 nsICommandParams *nsparam;
570 LPWSTR strw;
571 char *stra;
572 DWORD len;
573 nsresult nsres;
575 V_VT(out) = VT_BSTR;
576 V_BSTR(out) = NULL;
578 nsparam = create_nscommand_params();
580 nsres = get_ns_command_state(This->doc_obj->nscontainer, NSCMD_FONTFACE, nsparam);
581 if(NS_FAILED(nsres))
582 return S_OK;
584 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
585 nsICommandParams_Release(nsparam);
587 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
588 strw = heap_alloc(len*sizeof(WCHAR));
589 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, len);
590 nsfree(stra);
592 V_BSTR(out) = SysAllocString(strw);
593 heap_free(strw);
596 return S_OK;
599 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
601 TRACE("(%p)->(%p %p)\n", This, in, out);
603 if(in) {
604 if(V_VT(in) == VT_I4) {
605 nsICommandParams *nsparam = create_nscommand_params();
606 char color_str[10];
608 sprintf(color_str, "#%02x%02x%02x",
609 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
611 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
612 do_ns_command(This, NSCMD_FONTCOLOR, nsparam);
614 nsICommandParams_Release(nsparam);
615 }else {
616 FIXME("unsupported in vt=%d\n", V_VT(in));
619 update_doc(This, UPDATE_UI);
622 if(out) {
623 FIXME("unsupported out\n");
624 return E_NOTIMPL;
627 return S_OK;
630 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
632 TRACE("(%p)->(%p %p)\n", This, in, out);
634 if(out) {
635 WCHAR val[10] = {0};
637 get_font_size(This, val);
638 V_VT(out) = VT_I4;
639 V_I4(out) = strtolW(val, NULL, 10);
642 if(in) {
643 switch(V_VT(in)) {
644 case VT_I4: {
645 WCHAR size[10];
646 static const WCHAR format[] = {'%','d',0};
647 wsprintfW(size, format, V_I4(in));
648 set_font_size(This, size);
649 break;
651 case VT_BSTR:
652 set_font_size(This, V_BSTR(in));
653 break;
654 default:
655 FIXME("unsupported vt %d\n", V_VT(in));
658 update_doc(This, UPDATE_UI);
661 return S_OK;
664 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
667 FIXME("(%p)->(%p %p)\n", This, in, out);
668 return E_NOTIMPL;
671 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
673 TRACE("(%p)\n", This);
675 if(in || out)
676 FIXME("unsupported args\n");
678 if(This->doc_obj->nscontainer)
679 do_ns_command(This, NSCMD_SELECTALL, NULL);
681 update_doc(This, UPDATE_UI);
682 return S_OK;
685 static HRESULT exec_bold(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->doc_obj->nscontainer)
693 do_ns_command(This, NSCMD_BOLD, NULL);
695 update_doc(This, UPDATE_UI);
696 return S_OK;
699 static HRESULT exec_italic(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->doc_obj->nscontainer)
707 do_ns_command(This, NSCMD_ITALIC, NULL);
709 update_doc(This, UPDATE_UI);
710 return S_OK;
713 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
715 static const PRUnichar justifycenterW[] = {'j','u','s','t','i','f','y','c','e','n','t','e','r',0};
716 static const PRUnichar justifyrightW[] = {'j','u','s','t','i','f','y','r','i','g','h','t',0};
718 switch(cmd->cmdID) {
719 case IDM_JUSTIFYCENTER:
720 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
721 cmd->cmdf = query_align_status(This, justifycenterW);
722 break;
723 case IDM_JUSTIFYLEFT:
724 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
725 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
726 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
727 cmd->cmdf = OLECMDF_SUPPORTED;
728 else
729 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
730 break;
731 case IDM_JUSTIFYRIGHT:
732 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
733 cmd->cmdf = query_align_status(This, justifyrightW);
734 break;
737 return S_OK;
740 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
742 TRACE("(%p)\n", This);
744 if(in || out)
745 FIXME("unsupported args\n");
747 set_ns_align(This, NSALIGN_CENTER);
748 update_doc(This, UPDATE_UI);
749 return S_OK;
752 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
754 TRACE("(%p)\n", This);
756 if(in || out)
757 FIXME("unsupported args\n");
759 set_ns_align(This, NSALIGN_LEFT);
760 update_doc(This, UPDATE_UI);
761 return S_OK;
764 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
766 TRACE("(%p)\n", This);
768 if(in || out)
769 FIXME("unsupported args\n");
771 set_ns_align(This, NSALIGN_RIGHT);
772 update_doc(This, UPDATE_UI);
773 return S_OK;
776 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
778 TRACE("(%p)\n", This);
780 if(in || out)
781 FIXME("unsupported args\n");
783 do_ns_command(This, NSCMD_UNDERLINE, NULL);
784 update_doc(This, UPDATE_UI);
785 return S_OK;
788 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
790 TRACE("(%p)\n", This);
792 if(in || out)
793 FIXME("unsupported args\n");
795 do_ns_command(This, NSCMD_INSERTHR, NULL);
796 update_doc(This, UPDATE_UI);
797 return S_OK;
800 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
802 TRACE("(%p)\n", This);
804 if(in || out)
805 FIXME("unsupported args\n");
807 do_ns_command(This, NSCMD_OL, NULL);
808 update_doc(This, UPDATE_UI);
809 return S_OK;
812 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
814 TRACE("(%p)\n", This);
816 if(in || out)
817 FIXME("unsupported args\n");
819 do_ns_command(This, NSCMD_UL, NULL);
820 update_doc(This, UPDATE_UI);
821 return S_OK;
824 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
826 TRACE("(%p)\n", This);
828 if(in || out)
829 FIXME("unsupported args\n");
831 do_ns_command(This, NSCMD_INDENT, NULL);
832 update_doc(This, UPDATE_UI);
833 return S_OK;
836 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
838 TRACE("(%p)\n", This);
840 if(in || out)
841 FIXME("unsupported args\n");
843 do_ns_command(This, NSCMD_OUTDENT, NULL);
844 update_doc(This, UPDATE_UI);
845 return S_OK;
848 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
850 WCHAR *ptr;
852 if(out || !in || V_VT(in) != VT_BSTR) {
853 WARN("invalid arg\n");
854 return E_INVALIDARG;
857 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
859 update_doc(This, UPDATE_UI);
861 ptr = V_BSTR(in);
862 if(*ptr == '1')
863 exec_bold(This, cmdexecopt, NULL, NULL);
864 ptr = strchrW(ptr, ',');
865 if(!ptr)
866 return S_OK;
868 if(*++ptr == '1')
869 exec_italic(This, cmdexecopt, NULL, NULL);
870 ptr = strchrW(ptr, ',');
871 if(!ptr)
872 return S_OK;
874 if(*++ptr == '1')
875 exec_underline(This, cmdexecopt, NULL, NULL);
876 ptr = strchrW(ptr, ',');
877 if(!ptr)
878 return S_OK;
880 if(isdigitW(*++ptr)) {
881 VARIANT v;
883 V_VT(&v) = VT_I4;
884 V_I4(&v) = *ptr-'0';
886 exec_fontsize(This, cmdexecopt, &v, NULL);
888 ptr = strchrW(ptr, ',');
889 if(!ptr)
890 return S_OK;
892 if(*++ptr != ',')
893 FIXME("set font color\n");
894 ptr = strchrW(ptr, ',');
895 if(!ptr)
896 return S_OK;
898 if(*++ptr != ',')
899 FIXME("set background color\n");
900 ptr = strchrW(ptr, ',');
901 if(!ptr)
902 return S_OK;
904 ptr++;
905 if(*ptr) {
906 VARIANT v;
908 V_VT(&v) = VT_BSTR;
909 V_BSTR(&v) = SysAllocString(ptr);
911 exec_fontname(This, cmdexecopt, &v, NULL);
913 SysFreeString(V_BSTR(&v));
916 return S_OK;
919 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
921 update_doc(This, UPDATE_UI);
923 if(!This->doc_obj->nscontainer)
924 return E_FAIL;
926 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_COPY);
927 return S_OK;
930 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
932 update_doc(This, UPDATE_UI);
934 if(!This->doc_obj->nscontainer)
935 return E_FAIL;
937 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_CUT);
938 return S_OK;
941 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
943 update_doc(This, UPDATE_UI);
945 if(!This->doc_obj->nscontainer)
946 return E_FAIL;
948 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_PASTE);
949 return S_OK;
952 static HRESULT exec_setdirty(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
954 TRACE("(%p)->(%08x %p %p)\n", This, cmdexecopt, in, out);
956 if(!in)
957 return S_OK;
959 if(V_VT(in) == VT_BOOL)
960 set_dirty(This, V_BOOL(in));
961 else
962 FIXME("unsupported vt=%d\n", V_VT(in));
964 return S_OK;
967 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
969 switch(cmd->cmdID) {
970 case IDM_DELETE:
971 TRACE("CGID_MSHTML: IDM_DELETE\n");
972 cmd->cmdf = query_ns_edit_status(This, NULL);
973 break;
974 case IDM_FONTNAME:
975 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
976 cmd->cmdf = query_ns_edit_status(This, NULL);
977 break;
978 case IDM_FONTSIZE:
979 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
980 cmd->cmdf = query_ns_edit_status(This, NULL);
981 break;
982 case IDM_BOLD:
983 TRACE("CGID_MSHTML: IDM_BOLD\n");
984 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
985 break;
986 case IDM_FORECOLOR:
987 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
988 cmd->cmdf = query_ns_edit_status(This, NULL);
989 break;
990 case IDM_ITALIC:
991 TRACE("CGID_MSHTML: IDM_ITALIC\n");
992 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
993 break;
994 case IDM_UNDERLINE:
995 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
996 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
997 break;
998 case IDM_HORIZONTALLINE:
999 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1000 cmd->cmdf = query_ns_edit_status(This, NULL);
1001 break;
1002 case IDM_ORDERLIST:
1003 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1004 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1005 break;
1006 case IDM_UNORDERLIST:
1007 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1008 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1009 break;
1010 case IDM_INDENT:
1011 TRACE("CGID_MSHTML: IDM_INDENT\n");
1012 cmd->cmdf = query_ns_edit_status(This, NULL);
1013 break;
1014 case IDM_OUTDENT:
1015 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1016 cmd->cmdf = query_ns_edit_status(This, NULL);
1017 break;
1018 case IDM_HYPERLINK:
1019 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1020 cmd->cmdf = query_ns_edit_status(This, NULL);
1021 break;
1024 return S_OK;
1027 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1029 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1031 switch (msg)
1033 case WM_INITDIALOG:
1035 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1036 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1037 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1038 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1039 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1040 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1041 INT def_idx;
1042 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1043 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1044 INT len;
1046 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1048 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1049 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1050 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1051 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1052 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1053 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1054 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1055 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1057 /* force the updating of the URL edit box */
1058 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1060 SetFocus(hwndURL);
1061 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1062 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1064 return FALSE;
1066 case WM_COMMAND:
1067 switch (wparam)
1069 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1070 EndDialog(hwnd, wparam);
1071 return TRUE;
1072 case MAKEWPARAM(IDOK, BN_CLICKED):
1074 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1075 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1076 INT len = GetWindowTextLengthW(hwndURL);
1077 *url = SysAllocStringLen(NULL, len + 1);
1078 GetWindowTextW(hwndURL, *url, len + 1);
1079 EndDialog(hwnd, wparam);
1080 return TRUE;
1082 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1084 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1085 INT item;
1086 INT len;
1087 LPWSTR type;
1088 LPWSTR url;
1089 LPWSTR p;
1090 static const WCHAR wszSlashSlash[] = {'/','/'};
1092 /* get string of currently selected hyperlink type */
1093 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1094 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1095 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1096 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1098 if (!strcmpW(type, wszOther))
1099 *type = '\0';
1101 /* get current URL */
1102 len = GetWindowTextLengthW(hwndURL);
1103 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1104 GetWindowTextW(hwndURL, url, len + 1);
1106 /* strip off old protocol */
1107 p = strchrW(url, ':');
1108 if (p && p[1] == '/' && p[2] == '/')
1109 p += 3;
1110 if (!p) p = url;
1111 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1113 /* add new protocol */
1114 if (*type != '\0')
1116 memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1117 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1120 SetWindowTextW(hwndURL, url);
1122 HeapFree(GetProcessHeap(), 0, url);
1123 HeapFree(GetProcessHeap(), 0, type);
1124 return TRUE;
1127 return FALSE;
1128 case WM_CLOSE:
1129 EndDialog(hwnd, IDCANCEL);
1130 return TRUE;
1131 default:
1132 return FALSE;
1136 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1138 nsAString href_str, ns_url;
1139 nsIHTMLEditor *html_editor;
1140 nsIDOMHTMLElement *anchor_elem;
1141 PRBool insert_link_at_caret;
1142 nsISelection *nsselection;
1143 BSTR url = NULL;
1144 INT ret;
1145 HRESULT hres = E_FAIL;
1147 static const WCHAR aW[] = {'a',0};
1148 static const WCHAR hrefW[] = {'h','r','e','f',0};
1150 TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1152 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1154 if (!in || V_VT(in) != VT_BSTR)
1156 WARN("invalid arg\n");
1157 return E_INVALIDARG;
1159 url = V_BSTR(in);
1161 else
1163 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1164 if (ret != IDOK)
1165 return OLECMDERR_E_CANCELED;
1168 if(!This->doc_node->nsdoc) {
1169 WARN("NULL nsdoc\n");
1170 return E_UNEXPECTED;
1173 nsselection = get_ns_selection(This);
1174 if (!nsselection)
1175 return E_FAIL;
1177 /* create an element for the link */
1178 create_nselem(This->doc_node, aW, &anchor_elem);
1180 nsAString_InitDepend(&href_str, hrefW);
1181 nsAString_InitDepend(&ns_url, url);
1182 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1183 nsAString_Finish(&href_str);
1185 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1187 /* create an element with text of URL */
1188 if (insert_link_at_caret) {
1189 nsIDOMNode *text_node, *unused_node;
1191 nsIDOMDocument_CreateTextNode(This->doc_node->nsdoc, &ns_url, (nsIDOMText **)&text_node);
1193 /* wrap the <a> tags around the text element */
1194 nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1195 nsIDOMNode_Release(text_node);
1196 nsIDOMNode_Release(unused_node);
1199 nsAString_Finish(&ns_url);
1201 nsIEditor_QueryInterface(This->doc_obj->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1202 if (html_editor) {
1203 nsresult nsres;
1205 if (insert_link_at_caret) {
1206 /* add them to the document at the caret position */
1207 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, (nsIDOMElement*)anchor_elem, FALSE);
1208 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1209 }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1210 nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, (nsIDOMElement*)anchor_elem);
1212 nsIHTMLEditor_Release(html_editor);
1213 hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1216 nsISelection_Release(nsselection);
1217 nsIDOMElement_Release(anchor_elem);
1219 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1220 SysFreeString(url);
1222 TRACE("-- 0x%08x\n", hres);
1223 return hres;
1226 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1228 TRACE("(%p)->(%p)\n", This, cmd);
1230 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1231 return S_OK;
1234 const cmdtable_t editmode_cmds[] = {
1235 {IDM_DELETE, query_edit_status, exec_delete},
1236 {IDM_FONTNAME, query_edit_status, exec_fontname},
1237 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1238 {IDM_SELECTALL, query_selall_status , exec_selectall},
1239 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1240 {IDM_BOLD, query_edit_status, exec_bold},
1241 {IDM_ITALIC, query_edit_status, exec_italic},
1242 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1243 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1244 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1245 {IDM_FONT, NULL, exec_font},
1246 {IDM_UNDERLINE, query_edit_status, exec_underline},
1247 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1248 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1249 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1250 {IDM_INDENT, query_edit_status, exec_indent},
1251 {IDM_OUTDENT, query_edit_status, exec_outdent},
1252 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1253 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1254 {IDM_SETDIRTY, NULL, exec_setdirty},
1255 {0,NULL,NULL}
1258 void init_editor(HTMLDocument *This)
1260 update_doc(This, UPDATE_UI);
1262 set_ns_fontname(This, "Times New Roman");
1265 HRESULT editor_is_dirty(HTMLDocument *This)
1267 PRBool modified;
1269 if(!This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
1270 return S_FALSE;
1272 nsIEditor_GetDocumentModified(This->doc_obj->nscontainer->editor, &modified);
1274 return modified ? S_OK : S_FALSE;