msvcirt: Add implementation of streambuf::unbuffered.
[wine.git] / dlls / mshtml / editor.c
blob81660949b5492788d84b9e047afc311b94d2d9e1
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"
29 #include "shlguid.h"
31 #include "wine/debug.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_SELECTBEGINLINE "cmd_selectBeginLine"
64 #define NSCMD_SELECTBOTTOM "cmd_selectBottom"
65 #define NSCMD_SELECTCHARNEXT "cmd_selectCharNext"
66 #define NSCMD_SELECTCHARPREVIOUS "cmd_selectCharPrevious"
67 #define NSCMD_SELECTENDLINE "cmd_selectEndLine"
68 #define NSCMD_SELECTLINENEXT "cmd_selectLineNext"
69 #define NSCMD_SELECTLINEPREVIOUS "cmd_selectLinePrevious"
70 #define NSCMD_SELECTPAGEDOWN "cmd_selectPageDown"
71 #define NSCMD_SELECTPAGEUP "cmd_selectPageUp"
72 #define NSCMD_SELECTTOP "cmd_selectTop"
73 #define NSCMD_SELECTWORDNEXT "cmd_selectWordNext"
74 #define NSCMD_SELECTWORDPREVIOUS "cmd_selectWordPrevious"
75 #define NSCMD_UL "cmd_ul"
76 #define NSCMD_UNDERLINE "cmd_underline"
77 #define NSCMD_WORDNEXT "cmd_wordNext"
78 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
80 #define NSSTATE_ATTRIBUTE "state_attribute"
81 #define NSSTATE_ALL "state_all"
83 #define NSALIGN_CENTER "center"
84 #define NSALIGN_LEFT "left"
85 #define NSALIGN_RIGHT "right"
87 #define DOM_VK_LEFT VK_LEFT
88 #define DOM_VK_UP VK_UP
89 #define DOM_VK_RIGHT VK_RIGHT
90 #define DOM_VK_DOWN VK_DOWN
91 #define DOM_VK_DELETE VK_DELETE
92 #define DOM_VK_HOME VK_HOME
93 #define DOM_VK_END VK_END
95 static const WCHAR fontW[] = {'f','o','n','t',0};
96 static const WCHAR sizeW[] = {'s','i','z','e',0};
98 void set_dirty(HTMLDocument *This, VARIANT_BOOL dirty)
100 nsresult nsres;
102 if(This->doc_obj->usermode != EDITMODE || !This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
103 return;
105 if(dirty) {
106 nsres = nsIEditor_IncrementModificationCount(This->doc_obj->nscontainer->editor, 1);
107 if(NS_FAILED(nsres))
108 ERR("IncrementModificationCount failed: %08x\n", nsres);
109 }else {
110 nsres = nsIEditor_ResetModificationCount(This->doc_obj->nscontainer->editor);
111 if(NS_FAILED(nsres))
112 ERR("ResetModificationCount failed: %08x\n", nsres);
116 static void do_ns_editor_command(NSContainer *This, const char *cmd)
118 nsresult nsres;
120 if(!This->editor_controller)
121 return;
123 nsres = nsIController_DoCommand(This->editor_controller, cmd);
124 if(NS_FAILED(nsres))
125 ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
128 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
130 nsICommandManager *cmdmgr;
131 nsresult nsres;
133 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsICommandManager, (void**)&cmdmgr);
134 if(NS_FAILED(nsres)) {
135 ERR("Could not get nsICommandManager: %08x\n", nsres);
136 return nsres;
139 nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, This->doc->basedoc.window->nswindow, nsparam);
140 if(NS_FAILED(nsres))
141 ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
143 nsICommandManager_Release(cmdmgr);
144 return nsres;
147 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
149 nsICommandParams *nsparam;
150 cpp_bool b = FALSE;
152 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
153 return OLECMDF_SUPPORTED;
155 if(This->doc_obj->nscontainer && nscmd) {
156 nsparam = create_nscommand_params();
157 get_ns_command_state(This->doc_obj->nscontainer, nscmd, nsparam);
159 nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
161 nsICommandParams_Release(nsparam);
164 return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
167 static void set_ns_align(HTMLDocument *This, const char *align_str)
169 nsICommandParams *nsparam;
171 if(!This->doc_obj->nscontainer)
172 return;
174 nsparam = create_nscommand_params();
175 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
177 do_ns_command(This, NSCMD_ALIGN, nsparam);
179 nsICommandParams_Release(nsparam);
182 static DWORD query_align_status(HTMLDocument *This, const WCHAR *align)
184 DWORD ret = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
185 nsAString justify_str;
186 cpp_bool b;
187 nsresult nsres;
189 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
190 return OLECMDF_SUPPORTED;
192 nsAString_Init(&justify_str, align);
193 nsres = nsIDOMHTMLDocument_QueryCommandState(This->doc_node->nsdoc, &justify_str, &b);
194 nsAString_Finish(&justify_str);
195 if(NS_SUCCEEDED(nsres) && b)
196 ret |= OLECMDF_LATCHED;
198 return ret;
202 static nsISelection *get_ns_selection(HTMLDocument *This)
204 nsISelection *nsselection = NULL;
205 nsresult nsres;
207 nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
208 if(NS_FAILED(nsres))
209 ERR("GetSelection failed %08x\n", nsres);
211 return nsselection;
215 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
217 cpp_bool has_children;
218 UINT32 child_cnt, i;
219 nsIDOMNode *child_node;
220 nsIDOMNodeList *node_list;
221 UINT16 node_type;
223 nsIDOMElement_HasChildNodes(elem, &has_children);
224 if(!has_children)
225 return;
227 nsIDOMElement_GetChildNodes(elem, &node_list);
228 nsIDOMNodeList_GetLength(node_list, &child_cnt);
230 for(i=0; i<child_cnt; i++) {
231 nsIDOMNodeList_Item(node_list, i, &child_node);
233 nsIDOMNode_GetNodeType(child_node, &node_type);
234 if(node_type == ELEMENT_NODE) {
235 nsIDOMElement *child_elem;
236 nsAString tag_str;
237 const PRUnichar *ctag;
239 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
241 nsAString_Init(&tag_str, NULL);
242 nsIDOMElement_GetTagName(child_elem, &tag_str);
243 nsAString_GetData(&tag_str, &ctag);
245 if(!strcmpiW(ctag, tag))
246 /* FIXME: remove node if there are no more attributes */
247 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
249 nsAString_Finish(&tag_str);
251 remove_child_attr(child_elem, tag, attr_str);
253 nsIDOMElement_Release(child_elem);
256 nsIDOMNode_Release(child_node);
259 nsIDOMNodeList_Release(node_list);
262 static void get_font_size(HTMLDocument *This, WCHAR *ret)
264 nsISelection *nsselection = get_ns_selection(This);
265 nsIDOMHTMLElement *elem = NULL;
266 nsIDOMNode *node = NULL, *tmp_node;
267 nsAString tag_str;
268 LPCWSTR tag;
269 UINT16 node_type;
270 nsresult nsres;
272 *ret = 0;
274 if(!nsselection)
275 return;
277 nsISelection_GetFocusNode(nsselection, &node);
278 nsISelection_Release(nsselection);
280 while(node) {
281 nsres = nsIDOMNode_GetNodeType(node, &node_type);
282 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
283 break;
285 if(node_type == ELEMENT_NODE) {
286 nsIDOMNode_QueryInterface(node, &IID_nsIDOMHTMLElement, (void**)&elem);
288 nsAString_Init(&tag_str, NULL);
289 nsIDOMHTMLElement_GetTagName(elem, &tag_str);
290 nsAString_GetData(&tag_str, &tag);
292 if(!strcmpiW(tag, fontW)) {
293 nsAString val_str;
294 const PRUnichar *val;
296 TRACE("found font tag %p\n", elem);
298 get_elem_attr_value(elem, sizeW, &val_str, &val);
299 if(*val) {
300 TRACE("found size %s\n", debugstr_w(val));
301 strcpyW(ret, val);
304 nsAString_Finish(&val_str);
307 nsAString_Finish(&tag_str);
308 nsIDOMHTMLElement_Release(elem);
311 if(*ret)
312 break;
314 tmp_node = node;
315 nsIDOMNode_GetParentNode(node, &node);
316 nsIDOMNode_Release(tmp_node);
319 if(node)
320 nsIDOMNode_Release(node);
323 static void set_font_size(HTMLDocument *This, LPCWSTR size)
325 nsISelection *nsselection;
326 cpp_bool collapsed;
327 nsIDOMHTMLElement *elem;
328 nsIDOMRange *range;
329 LONG range_cnt = 0;
330 nsAString size_str;
331 nsAString val_str;
333 if(!This->doc_node->nsdoc) {
334 WARN("NULL nsdoc\n");
335 return;
338 nsselection = get_ns_selection(This);
339 if(!nsselection)
340 return;
342 nsISelection_GetRangeCount(nsselection, &range_cnt);
343 if(range_cnt != 1) {
344 FIXME("range_cnt %d not supprted\n", range_cnt);
345 if(!range_cnt) {
346 nsISelection_Release(nsselection);
347 return;
351 create_nselem(This->doc_node, fontW, &elem);
353 nsAString_InitDepend(&size_str, sizeW);
354 nsAString_InitDepend(&val_str, size);
356 nsIDOMHTMLElement_SetAttribute(elem, &size_str, &val_str);
357 nsAString_Finish(&val_str);
359 nsISelection_GetRangeAt(nsselection, 0, &range);
360 nsISelection_GetIsCollapsed(nsselection, &collapsed);
361 nsISelection_RemoveAllRanges(nsselection);
363 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
365 if(collapsed) {
366 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
367 }else {
368 /* Remove all size attributes from the range */
369 remove_child_attr((nsIDOMElement*)elem, fontW, &size_str);
370 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
373 nsISelection_Release(nsselection);
374 nsIDOMRange_Release(range);
375 nsIDOMHTMLElement_Release(elem);
377 nsAString_Finish(&size_str);
379 set_dirty(This, VARIANT_TRUE);
382 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
384 int i=0;
385 cpp_bool b;
387 nsIDOMKeyEvent_GetCtrlKey(event, &b);
388 if(b)
389 i |= 1;
391 nsIDOMKeyEvent_GetShiftKey(event, &b);
392 if(b)
393 i |= 2;
395 if(cmds[i])
396 do_ns_editor_command(This->doc_obj->nscontainer, cmds[i]);
398 nsIDOMKeyEvent_PreventDefault(event);
401 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
403 nsIDOMKeyEvent *key_event;
404 UINT32 code;
406 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
408 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
410 switch(code) {
411 case DOM_VK_LEFT: {
412 static const char * const cmds[] = {
413 NSCMD_CHARPREVIOUS,
414 NSCMD_WORDPREVIOUS,
415 NSCMD_SELECTCHARPREVIOUS,
416 NSCMD_SELECTWORDPREVIOUS
419 TRACE("left\n");
420 handle_arrow_key(This, key_event, cmds);
421 break;
423 case DOM_VK_RIGHT: {
424 static const char * const cmds[] = {
425 NSCMD_CHARNEXT,
426 NSCMD_WORDNEXT,
427 NSCMD_SELECTCHARNEXT,
428 NSCMD_SELECTWORDNEXT
431 TRACE("right\n");
432 handle_arrow_key(This, key_event, cmds);
433 break;
435 case DOM_VK_UP: {
436 static const char * const cmds[] = {
437 NSCMD_LINEPREVIOUS,
438 NSCMD_MOVEPAGEUP,
439 NSCMD_SELECTLINEPREVIOUS,
440 NSCMD_SELECTPAGEUP
443 TRACE("up\n");
444 handle_arrow_key(This, key_event, cmds);
445 break;
447 case DOM_VK_DOWN: {
448 static const char * const cmds[] = {
449 NSCMD_LINENEXT,
450 NSCMD_MOVEPAGEDOWN,
451 NSCMD_SELECTLINENEXT,
452 NSCMD_SELECTPAGEDOWN
455 TRACE("down\n");
456 handle_arrow_key(This, key_event, cmds);
457 break;
459 case DOM_VK_DELETE: {
460 static const char * const cmds[] = {
461 NSCMD_DELETECHARFORWARD,
462 NSCMD_DELETEWORDFORWARD,
463 NULL, NULL
466 TRACE("delete\n");
467 handle_arrow_key(This, key_event, cmds);
468 break;
470 case DOM_VK_HOME: {
471 static const char * const cmds[] = {
472 NSCMD_BEGINLINE,
473 NSCMD_MOVETOP,
474 NSCMD_SELECTBEGINLINE,
475 NSCMD_SELECTTOP
478 TRACE("home\n");
479 handle_arrow_key(This, key_event, cmds);
480 break;
482 case DOM_VK_END: {
483 static const char * const cmds[] = {
484 NSCMD_ENDLINE,
485 NSCMD_MOVEBOTTOM,
486 NSCMD_SELECTENDLINE,
487 NSCMD_SELECTBOTTOM
490 TRACE("end\n");
491 handle_arrow_key(This, key_event, cmds);
492 break;
496 nsIDOMKeyEvent_Release(key_event);
499 void handle_edit_load(HTMLDocument *This)
501 get_editor_controller(This->doc_obj->nscontainer);
504 static void set_ns_fontname(HTMLDocument *This, const char *fontname)
506 nsICommandParams *nsparam = create_nscommand_params();
508 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
509 do_ns_command(This, NSCMD_FONTFACE, nsparam);
510 nsICommandParams_Release(nsparam);
513 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
515 TRACE("(%p)->(%p %p)\n", This, in, out);
517 if(This->doc_obj->nscontainer)
518 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_DELETECHARFORWARD);
520 update_doc(This, UPDATE_UI);
521 return S_OK;
524 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
526 TRACE("(%p)->(%p %p)\n", This, in, out);
528 if(!This->doc_obj->nscontainer) {
529 update_doc(This, UPDATE_UI);
530 return E_FAIL;
533 if(in) {
534 char *stra;
536 if(V_VT(in) != VT_BSTR) {
537 FIXME("Unsupported fontname %s\n", debugstr_variant(out));
538 return E_INVALIDARG;
541 TRACE("%s\n", debugstr_w(V_BSTR(in)));
543 stra = heap_strdupWtoA(V_BSTR(in));
544 set_ns_fontname(This, stra);
545 heap_free(stra);
547 update_doc(This, UPDATE_UI);
550 if(out) {
551 nsICommandParams *nsparam;
552 LPWSTR strw;
553 char *stra;
554 DWORD len;
555 nsresult nsres;
557 V_VT(out) = VT_BSTR;
558 V_BSTR(out) = NULL;
560 nsparam = create_nscommand_params();
562 nsres = get_ns_command_state(This->doc_obj->nscontainer, NSCMD_FONTFACE, nsparam);
563 if(NS_FAILED(nsres))
564 return S_OK;
566 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
567 nsICommandParams_Release(nsparam);
569 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
570 strw = heap_alloc(len*sizeof(WCHAR));
571 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, len);
572 nsfree(stra);
574 V_BSTR(out) = SysAllocString(strw);
575 heap_free(strw);
578 return S_OK;
581 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
583 TRACE("(%p)->(%p %p)\n", This, in, out);
585 if(in) {
586 if(V_VT(in) == VT_I4) {
587 nsICommandParams *nsparam = create_nscommand_params();
588 char color_str[10];
590 sprintf(color_str, "#%02x%02x%02x",
591 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
593 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
594 do_ns_command(This, NSCMD_FONTCOLOR, nsparam);
596 nsICommandParams_Release(nsparam);
597 }else {
598 FIXME("unsupported forecolor %s\n", debugstr_variant(in));
601 update_doc(This, UPDATE_UI);
604 if(out) {
605 FIXME("unsupported out\n");
606 return E_NOTIMPL;
609 return S_OK;
612 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
614 TRACE("(%p)->(%p %p)\n", This, in, out);
616 if(out) {
617 WCHAR val[10] = {0};
619 get_font_size(This, val);
620 V_VT(out) = VT_I4;
621 V_I4(out) = strtolW(val, NULL, 10);
624 if(in) {
625 switch(V_VT(in)) {
626 case VT_I4: {
627 WCHAR size[10];
628 static const WCHAR format[] = {'%','d',0};
629 wsprintfW(size, format, V_I4(in));
630 set_font_size(This, size);
631 break;
633 case VT_BSTR:
634 set_font_size(This, V_BSTR(in));
635 break;
636 default:
637 FIXME("unsupported fontsize %s\n", debugstr_variant(in));
640 update_doc(This, UPDATE_UI);
643 return S_OK;
646 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
649 FIXME("(%p)->(%p %p)\n", This, in, out);
650 return E_NOTIMPL;
653 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
655 TRACE("(%p)\n", This);
657 if(in || out)
658 FIXME("unsupported args\n");
660 if(This->doc_obj->nscontainer)
661 do_ns_command(This, NSCMD_BOLD, NULL);
663 update_doc(This, UPDATE_UI);
664 return S_OK;
667 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
669 TRACE("(%p)\n", This);
671 if(in || out)
672 FIXME("unsupported args\n");
674 if(This->doc_obj->nscontainer)
675 do_ns_command(This, NSCMD_ITALIC, NULL);
677 update_doc(This, UPDATE_UI);
678 return S_OK;
681 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
683 static const PRUnichar justifycenterW[] = {'j','u','s','t','i','f','y','c','e','n','t','e','r',0};
684 static const PRUnichar justifyrightW[] = {'j','u','s','t','i','f','y','r','i','g','h','t',0};
686 switch(cmd->cmdID) {
687 case IDM_JUSTIFYCENTER:
688 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
689 cmd->cmdf = query_align_status(This, justifycenterW);
690 break;
691 case IDM_JUSTIFYLEFT:
692 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
693 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
694 if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
695 cmd->cmdf = OLECMDF_SUPPORTED;
696 else
697 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
698 break;
699 case IDM_JUSTIFYRIGHT:
700 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
701 cmd->cmdf = query_align_status(This, justifyrightW);
702 break;
705 return S_OK;
708 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
710 TRACE("(%p)\n", This);
712 if(in || out)
713 FIXME("unsupported args\n");
715 set_ns_align(This, NSALIGN_CENTER);
716 update_doc(This, UPDATE_UI);
717 return S_OK;
720 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
722 TRACE("(%p)\n", This);
724 if(in || out)
725 FIXME("unsupported args\n");
727 set_ns_align(This, NSALIGN_LEFT);
728 update_doc(This, UPDATE_UI);
729 return S_OK;
732 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
734 TRACE("(%p)\n", This);
736 if(in || out)
737 FIXME("unsupported args\n");
739 set_ns_align(This, NSALIGN_RIGHT);
740 update_doc(This, UPDATE_UI);
741 return S_OK;
744 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
746 TRACE("(%p)\n", This);
748 if(in || out)
749 FIXME("unsupported args\n");
751 do_ns_command(This, NSCMD_UNDERLINE, NULL);
752 update_doc(This, UPDATE_UI);
753 return S_OK;
756 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
758 TRACE("(%p)\n", This);
760 if(in || out)
761 FIXME("unsupported args\n");
763 do_ns_command(This, NSCMD_INSERTHR, NULL);
764 update_doc(This, UPDATE_UI);
765 return S_OK;
768 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
770 TRACE("(%p)\n", This);
772 if(in || out)
773 FIXME("unsupported args\n");
775 do_ns_command(This, NSCMD_OL, NULL);
776 update_doc(This, UPDATE_UI);
777 return S_OK;
780 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
782 TRACE("(%p)\n", This);
784 if(in || out)
785 FIXME("unsupported args\n");
787 do_ns_command(This, NSCMD_UL, NULL);
788 update_doc(This, UPDATE_UI);
789 return S_OK;
792 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
794 TRACE("(%p)\n", This);
796 if(in || out)
797 FIXME("unsupported args\n");
799 do_ns_command(This, NSCMD_INDENT, NULL);
800 update_doc(This, UPDATE_UI);
801 return S_OK;
804 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
806 TRACE("(%p)\n", This);
808 if(in || out)
809 FIXME("unsupported args\n");
811 do_ns_command(This, NSCMD_OUTDENT, NULL);
812 update_doc(This, UPDATE_UI);
813 return S_OK;
816 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
818 WCHAR *ptr;
820 if(out || !in || V_VT(in) != VT_BSTR) {
821 WARN("invalid arg %s\n", debugstr_variant(in));
822 return E_INVALIDARG;
825 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
827 update_doc(This, UPDATE_UI);
829 ptr = V_BSTR(in);
830 if(*ptr == '1')
831 exec_bold(This, cmdexecopt, NULL, NULL);
832 ptr = strchrW(ptr, ',');
833 if(!ptr)
834 return S_OK;
836 if(*++ptr == '1')
837 exec_italic(This, cmdexecopt, NULL, NULL);
838 ptr = strchrW(ptr, ',');
839 if(!ptr)
840 return S_OK;
842 if(*++ptr == '1')
843 exec_underline(This, cmdexecopt, NULL, NULL);
844 ptr = strchrW(ptr, ',');
845 if(!ptr)
846 return S_OK;
848 if(isdigitW(*++ptr)) {
849 VARIANT v;
851 V_VT(&v) = VT_I4;
852 V_I4(&v) = *ptr-'0';
854 exec_fontsize(This, cmdexecopt, &v, NULL);
856 ptr = strchrW(ptr, ',');
857 if(!ptr)
858 return S_OK;
860 if(*++ptr != ',')
861 FIXME("set font color\n");
862 ptr = strchrW(ptr, ',');
863 if(!ptr)
864 return S_OK;
866 if(*++ptr != ',')
867 FIXME("set background color\n");
868 ptr = strchrW(ptr, ',');
869 if(!ptr)
870 return S_OK;
872 ptr++;
873 if(*ptr) {
874 VARIANT v;
876 V_VT(&v) = VT_BSTR;
877 V_BSTR(&v) = SysAllocString(ptr);
879 exec_fontname(This, cmdexecopt, &v, NULL);
881 SysFreeString(V_BSTR(&v));
884 return S_OK;
887 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
889 update_doc(This, UPDATE_UI);
891 if(!This->doc_obj->nscontainer)
892 return E_FAIL;
894 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_COPY);
895 return S_OK;
898 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
900 update_doc(This, UPDATE_UI);
902 if(!This->doc_obj->nscontainer)
903 return E_FAIL;
905 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_CUT);
906 return S_OK;
909 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
911 update_doc(This, UPDATE_UI);
913 if(!This->doc_obj->nscontainer)
914 return E_FAIL;
916 do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_PASTE);
917 return S_OK;
920 static HRESULT exec_setdirty(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
922 TRACE("(%p)->(%08x %p %p)\n", This, cmdexecopt, in, out);
924 if(!in)
925 return S_OK;
927 if(V_VT(in) == VT_BOOL)
928 set_dirty(This, V_BOOL(in));
929 else
930 FIXME("unsupported arg %s\n", debugstr_variant(in));
932 return S_OK;
935 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
937 switch(cmd->cmdID) {
938 case IDM_DELETE:
939 TRACE("CGID_MSHTML: IDM_DELETE\n");
940 cmd->cmdf = query_ns_edit_status(This, NULL);
941 break;
942 case IDM_FONTNAME:
943 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
944 cmd->cmdf = query_ns_edit_status(This, NULL);
945 break;
946 case IDM_FONTSIZE:
947 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
948 cmd->cmdf = query_ns_edit_status(This, NULL);
949 break;
950 case IDM_BOLD:
951 TRACE("CGID_MSHTML: IDM_BOLD\n");
952 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
953 break;
954 case IDM_FORECOLOR:
955 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
956 cmd->cmdf = query_ns_edit_status(This, NULL);
957 break;
958 case IDM_ITALIC:
959 TRACE("CGID_MSHTML: IDM_ITALIC\n");
960 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
961 break;
962 case IDM_UNDERLINE:
963 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
964 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
965 break;
966 case IDM_HORIZONTALLINE:
967 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
968 cmd->cmdf = query_ns_edit_status(This, NULL);
969 break;
970 case IDM_ORDERLIST:
971 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
972 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
973 break;
974 case IDM_UNORDERLIST:
975 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
976 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
977 break;
978 case IDM_INDENT:
979 TRACE("CGID_MSHTML: IDM_INDENT\n");
980 cmd->cmdf = query_ns_edit_status(This, NULL);
981 break;
982 case IDM_OUTDENT:
983 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
984 cmd->cmdf = query_ns_edit_status(This, NULL);
985 break;
986 case IDM_HYPERLINK:
987 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
988 cmd->cmdf = query_ns_edit_status(This, NULL);
989 break;
992 return S_OK;
995 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
997 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
998 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
999 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1000 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1001 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1002 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1003 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1005 switch (msg)
1007 case WM_INITDIALOG:
1009 INT def_idx;
1010 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1011 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1012 INT len;
1014 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1016 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1017 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1018 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1019 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1020 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1021 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1022 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1023 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1025 /* force the updating of the URL edit box */
1026 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1028 SetFocus(hwndURL);
1029 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1030 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1032 return FALSE;
1034 case WM_COMMAND:
1035 switch (wparam)
1037 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1038 EndDialog(hwnd, wparam);
1039 return TRUE;
1040 case MAKEWPARAM(IDOK, BN_CLICKED):
1042 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1043 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1044 INT len = GetWindowTextLengthW(hwndURL);
1045 *url = SysAllocStringLen(NULL, len + 1);
1046 GetWindowTextW(hwndURL, *url, len + 1);
1047 EndDialog(hwnd, wparam);
1048 return TRUE;
1050 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1052 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1053 INT item;
1054 INT len;
1055 LPWSTR type;
1056 LPWSTR url;
1057 LPWSTR p;
1058 static const WCHAR wszSlashSlash[] = {'/','/'};
1060 /* get string of currently selected hyperlink type */
1061 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1062 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1063 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1064 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1066 if (!strcmpW(type, wszOther))
1067 *type = '\0';
1069 /* get current URL */
1070 len = GetWindowTextLengthW(hwndURL);
1071 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1072 GetWindowTextW(hwndURL, url, len + 1);
1074 /* strip off old protocol */
1075 p = strchrW(url, ':');
1076 if (p && p[1] == '/' && p[2] == '/')
1077 p += 3;
1078 if (!p) p = url;
1079 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1081 /* add new protocol */
1082 if (*type != '\0')
1084 memcpy(url, type, (strlenW(type) + 1) * sizeof(WCHAR));
1085 if (strcmpW(type, wszMailto) && strcmpW(type, wszNews))
1086 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1089 SetWindowTextW(hwndURL, url);
1091 HeapFree(GetProcessHeap(), 0, url);
1092 HeapFree(GetProcessHeap(), 0, type);
1093 return TRUE;
1096 return FALSE;
1097 case WM_CLOSE:
1098 EndDialog(hwnd, IDCANCEL);
1099 return TRUE;
1100 default:
1101 return FALSE;
1105 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1107 nsAString href_str, ns_url;
1108 nsIHTMLEditor *html_editor;
1109 nsIDOMHTMLElement *anchor_elem;
1110 cpp_bool insert_link_at_caret;
1111 nsISelection *nsselection;
1112 BSTR url = NULL;
1113 INT ret;
1114 HRESULT hres = E_FAIL;
1116 static const WCHAR aW[] = {'a',0};
1117 static const WCHAR hrefW[] = {'h','r','e','f',0};
1119 TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1121 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1123 if (!in || V_VT(in) != VT_BSTR)
1125 WARN("invalid arg\n");
1126 return E_INVALIDARG;
1128 url = V_BSTR(in);
1130 else
1132 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1133 if (ret != IDOK)
1134 return OLECMDERR_E_CANCELED;
1137 if(!This->doc_node->nsdoc) {
1138 WARN("NULL nsdoc\n");
1139 return E_UNEXPECTED;
1142 nsselection = get_ns_selection(This);
1143 if (!nsselection)
1144 return E_FAIL;
1146 /* create an element for the link */
1147 create_nselem(This->doc_node, aW, &anchor_elem);
1149 nsAString_InitDepend(&href_str, hrefW);
1150 nsAString_InitDepend(&ns_url, url);
1151 nsIDOMHTMLElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1152 nsAString_Finish(&href_str);
1154 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1156 /* create an element with text of URL */
1157 if (insert_link_at_caret) {
1158 nsIDOMNode *unused_node;
1159 nsIDOMText *text_node;
1161 nsIDOMHTMLDocument_CreateTextNode(This->doc_node->nsdoc, &ns_url, &text_node);
1163 /* wrap the <a> tags around the text element */
1164 nsIDOMHTMLElement_AppendChild(anchor_elem, (nsIDOMNode*)text_node, &unused_node);
1165 nsIDOMText_Release(text_node);
1166 nsIDOMNode_Release(unused_node);
1169 nsAString_Finish(&ns_url);
1171 nsIEditor_QueryInterface(This->doc_obj->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1172 if (html_editor) {
1173 nsresult nsres;
1175 if (insert_link_at_caret) {
1176 /* add them to the document at the caret position */
1177 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, (nsIDOMElement*)anchor_elem, FALSE);
1178 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1179 }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1180 nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, (nsIDOMElement*)anchor_elem);
1182 nsIHTMLEditor_Release(html_editor);
1183 hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1186 nsISelection_Release(nsselection);
1187 nsIDOMHTMLElement_Release(anchor_elem);
1189 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1190 SysFreeString(url);
1192 TRACE("-- 0x%08x\n", hres);
1193 return hres;
1196 const cmdtable_t editmode_cmds[] = {
1197 {IDM_DELETE, query_edit_status, exec_delete},
1198 {IDM_FONTNAME, query_edit_status, exec_fontname},
1199 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1200 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1201 {IDM_BOLD, query_edit_status, exec_bold},
1202 {IDM_ITALIC, query_edit_status, exec_italic},
1203 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1204 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1205 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1206 {IDM_FONT, NULL, exec_font},
1207 {IDM_UNDERLINE, query_edit_status, exec_underline},
1208 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1209 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1210 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1211 {IDM_INDENT, query_edit_status, exec_indent},
1212 {IDM_OUTDENT, query_edit_status, exec_outdent},
1213 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1214 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1215 {IDM_SETDIRTY, NULL, exec_setdirty},
1216 {0,NULL,NULL}
1219 void init_editor(HTMLDocument *This)
1221 update_doc(This, UPDATE_UI);
1223 set_ns_fontname(This, "Times New Roman");
1226 HRESULT editor_is_dirty(HTMLDocument *This)
1228 cpp_bool modified;
1230 if(!This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
1231 return S_FALSE;
1233 nsIEditor_GetDocumentModified(This->doc_obj->nscontainer->editor, &modified);
1235 return modified ? S_OK : S_FALSE;
1238 HRESULT setup_edit_mode(HTMLDocumentObj *doc)
1240 IMoniker *mon;
1241 HRESULT hres;
1243 if(doc->usermode == EDITMODE)
1244 return S_OK;
1246 doc->usermode = EDITMODE;
1248 if(doc->basedoc.window->mon) {
1249 CLSID clsid = IID_NULL;
1250 hres = IMoniker_GetClassID(doc->basedoc.window->mon, &clsid);
1251 if(SUCCEEDED(hres)) {
1252 /* We should use IMoniker::Save here */
1253 FIXME("Use CLSID %s\n", debugstr_guid(&clsid));
1257 if(doc->frame)
1258 IOleInPlaceFrame_SetStatusText(doc->frame, NULL);
1260 doc->basedoc.window->readystate = READYSTATE_UNINITIALIZED;
1262 if(doc->client) {
1263 IOleCommandTarget *cmdtrg;
1265 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1266 if(SUCCEEDED(hres)) {
1267 VARIANT var;
1269 V_VT(&var) = VT_I4;
1270 V_I4(&var) = 0;
1271 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
1273 IOleCommandTarget_Release(cmdtrg);
1277 if(doc->hostui) {
1278 DOCHOSTUIINFO hostinfo;
1280 memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
1281 hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
1282 hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
1283 if(SUCCEEDED(hres))
1284 /* FIXME: use hostinfo */
1285 TRACE("hostinfo = {%u %08x %08x %s %s}\n",
1286 hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
1287 debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
1290 update_doc(&doc->basedoc, UPDATE_UI);
1292 if(doc->basedoc.window->mon) {
1293 /* FIXME: We should find nicer way to do this */
1294 remove_target_tasks(doc->basedoc.task_magic);
1296 mon = doc->basedoc.window->mon;
1297 IMoniker_AddRef(mon);
1298 }else {
1299 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
1301 hres = CreateURLMoniker(NULL, about_blankW, &mon);
1302 if(FAILED(hres)) {
1303 FIXME("CreateURLMoniker failed: %08x\n", hres);
1304 return hres;
1308 hres = IPersistMoniker_Load(&doc->basedoc.IPersistMoniker_iface, TRUE, mon, NULL, 0);
1309 IMoniker_Release(mon);
1310 if(FAILED(hres))
1311 return hres;
1313 if(doc->ui_active) {
1314 if(doc->ip_window)
1315 call_set_active_object(doc->ip_window, NULL);
1316 if(doc->hostui)
1317 IDocHostUIHandler_HideUI(doc->hostui);
1320 if(doc->ui_active) {
1321 RECT rcBorderWidths;
1323 if(doc->hostui)
1324 IDocHostUIHandler_ShowUI(doc->hostui, DOCHOSTUITYPE_AUTHOR,
1325 &doc->basedoc.IOleInPlaceActiveObject_iface, &doc->basedoc.IOleCommandTarget_iface,
1326 doc->frame, doc->ip_window);
1328 if(doc->ip_window)
1329 call_set_active_object(doc->ip_window, &doc->basedoc.IOleInPlaceActiveObject_iface);
1331 memset(&rcBorderWidths, 0, sizeof(rcBorderWidths));
1332 if(doc->frame)
1333 IOleInPlaceFrame_SetBorderSpace(doc->frame, &rcBorderWidths);
1336 return S_OK;