wined3d: Disable TEXTURE_RECTANGLE in SetupForBlit.
[wine.git] / dlls / mshtml / editor.c
blob5929603b67c03ed52eb19f2111f8798e25f6154a
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 wszFont[] = {'f','o','n','t',0};
97 static const WCHAR wszSize[] = {'s','i','z','e',0};
99 void set_dirty(HTMLDocument *This, VARIANT_BOOL dirty)
101 nsresult nsres;
103 if(This->usermode != EDITMODE || !This->nscontainer || !This->nscontainer->editor)
104 return;
106 if(dirty) {
107 nsres = nsIEditor_IncrementModificationCount(This->nscontainer->editor, 1);
108 if(NS_FAILED(nsres))
109 ERR("IncrementModificationCount failed: %08x\n", nsres);
110 }else {
111 nsres = nsIEditor_ResetModificationCount(This->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, NULL, 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->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
154 return OLECMDF_SUPPORTED;
156 if(This->nscontainer && nscmd) {
157 nsparam = create_nscommand_params();
158 get_ns_command_state(This->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->nscontainer)
173 return;
175 nsparam = create_nscommand_params();
176 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
178 do_ns_command(This->nscontainer, NSCMD_ALIGN, nsparam);
180 nsICommandParams_Release(nsparam);
183 static DWORD query_align_status(HTMLDocument *This, const char *align_str)
185 nsICommandParams *nsparam;
186 char *align = NULL;
188 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
189 return OLECMDF_SUPPORTED;
191 if(This->nscontainer) {
192 nsparam = create_nscommand_params();
193 get_ns_command_state(This->nscontainer, NSCMD_ALIGN, nsparam);
195 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &align);
197 nsICommandParams_Release(nsparam);
200 return OLECMDF_SUPPORTED | OLECMDF_ENABLED
201 | (align && !strcmp(align_str, align) ? OLECMDF_LATCHED : 0);
205 static nsISelection *get_ns_selection(HTMLDocument *This)
207 nsIDOMWindow *dom_window;
208 nsISelection *nsselection = NULL;
209 nsresult nsres;
211 if(!This->nscontainer)
212 return NULL;
214 nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
215 if(NS_FAILED(nsres))
216 return NULL;
218 nsIDOMWindow_GetSelection(dom_window, &nsselection);
219 nsIDOMWindow_Release(dom_window);
221 return nsselection;
225 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
227 PRBool has_children;
228 PRUint32 child_cnt, i;
229 nsIDOMNode *child_node;
230 nsIDOMNodeList *node_list;
231 PRUint16 node_type;
233 nsIDOMElement_HasChildNodes(elem, &has_children);
234 if(!has_children)
235 return;
237 nsIDOMElement_GetChildNodes(elem, &node_list);
238 nsIDOMNodeList_GetLength(node_list, &child_cnt);
240 for(i=0; i<child_cnt; i++) {
241 nsIDOMNodeList_Item(node_list, i, &child_node);
243 nsIDOMNode_GetNodeType(child_node, &node_type);
244 if(node_type == ELEMENT_NODE) {
245 nsIDOMElement *child_elem;
246 nsAString tag_str;
247 const PRUnichar *ctag;
249 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
251 nsAString_Init(&tag_str, NULL);
252 nsIDOMElement_GetTagName(child_elem, &tag_str);
253 nsAString_GetData(&tag_str, &ctag);
255 if(!strcmpiW(ctag, tag))
256 /* FIXME: remove node if there are no more attributes */
257 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
259 nsAString_Finish(&tag_str);
261 remove_child_attr(child_elem, tag, attr_str);
263 nsIDOMNode_Release(child_elem);
266 nsIDOMNode_Release(child_node);
269 nsIDOMNodeList_Release(node_list);
272 static void get_font_size(HTMLDocument *This, WCHAR *ret)
274 nsISelection *nsselection = get_ns_selection(This);
275 nsIDOMElement *elem = NULL;
276 nsIDOMNode *node = NULL, *tmp_node;
277 nsAString tag_str;
278 LPCWSTR tag;
279 PRUint16 node_type;
280 nsresult nsres;
282 *ret = 0;
284 if(!nsselection)
285 return;
287 nsISelection_GetFocusNode(nsselection, &node);
288 nsISelection_Release(nsselection);
290 while(node) {
291 nsres = nsIDOMNode_GetNodeType(node, &node_type);
292 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
293 break;
295 if(node_type == ELEMENT_NODE) {
296 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
298 nsAString_Init(&tag_str, NULL);
299 nsIDOMElement_GetTagName(elem, &tag_str);
300 nsAString_GetData(&tag_str, &tag);
302 if(!strcmpiW(tag, wszFont)) {
303 nsAString size_str, val_str;
304 LPCWSTR val;
306 TRACE("found font tag %p\n", elem);
308 nsAString_Init(&size_str, wszSize);
309 nsAString_Init(&val_str, NULL);
311 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
312 nsAString_GetData(&val_str, &val);
314 if(*val) {
315 TRACE("found size %s\n", debugstr_w(val));
316 strcpyW(ret, val);
319 nsAString_Finish(&size_str);
320 nsAString_Finish(&val_str);
323 nsAString_Finish(&tag_str);
325 nsIDOMElement_Release(elem);
328 if(*ret)
329 break;
331 tmp_node = node;
332 nsIDOMNode_GetParentNode(node, &node);
333 nsIDOMNode_Release(tmp_node);
336 if(node)
337 nsIDOMNode_Release(node);
340 static void set_font_size(HTMLDocument *This, LPCWSTR size)
342 nsISelection *nsselection;
343 PRBool collapsed;
344 nsIDOMDocument *nsdoc;
345 nsIDOMElement *elem;
346 nsIDOMRange *range;
347 PRInt32 range_cnt = 0;
348 nsAString font_str;
349 nsAString size_str;
350 nsAString val_str;
351 nsresult nsres;
353 nsselection = get_ns_selection(This);
355 if(!nsselection)
356 return;
358 nsISelection_GetRangeCount(nsselection, &range_cnt);
359 if(range_cnt != 1) {
360 FIXME("range_cnt %d not supprted\n", range_cnt);
361 if(!range_cnt) {
362 nsISelection_Release(nsselection);
363 return;
367 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
368 if(NS_FAILED(nsres))
369 return;
371 nsAString_Init(&font_str, wszFont);
372 nsAString_Init(&size_str, wszSize);
373 nsAString_Init(&val_str, size);
375 nsIDOMDocument_CreateElement(nsdoc, &font_str, &elem);
376 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
378 nsISelection_GetRangeAt(nsselection, 0, &range);
379 nsISelection_GetIsCollapsed(nsselection, &collapsed);
380 nsISelection_RemoveAllRanges(nsselection);
382 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
384 if(collapsed) {
385 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
386 }else {
387 /* Remove all size attributes from the range */
388 remove_child_attr(elem, wszFont, &size_str);
389 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
392 nsIDOMRange_Release(range);
393 nsIDOMElement_Release(elem);
395 nsAString_Finish(&font_str);
396 nsAString_Finish(&size_str);
397 nsAString_Finish(&val_str);
399 nsISelection_Release(nsselection);
400 nsIDOMDocument_Release(nsdoc);
402 set_dirty(This, VARIANT_TRUE);
405 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
407 int i=0;
408 PRBool b;
410 nsIDOMKeyEvent_GetCtrlKey(event, &b);
411 if(b)
412 i |= 1;
414 nsIDOMKeyEvent_GetShiftKey(event, &b);
415 if(b)
416 i |= 2;
418 if(cmds[i])
419 do_ns_editor_command(This->nscontainer, cmds[i]);
421 nsIDOMKeyEvent_PreventDefault(event);
424 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
426 nsIDOMKeyEvent *key_event;
427 PRUint32 code;
429 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
431 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
433 switch(code) {
434 case DOM_VK_LEFT: {
435 static const char * const cmds[] = {
436 NSCMD_CHARPREVIOUS,
437 NSCMD_WORDPREVIOUS,
438 NSCMD_SELECTCHARPREVIOUS,
439 NSCMD_SELECTWORDPREVIOUS
442 TRACE("left\n");
443 handle_arrow_key(This, key_event, cmds);
444 break;
446 case DOM_VK_RIGHT: {
447 static const char * const cmds[] = {
448 NSCMD_CHARNEXT,
449 NSCMD_WORDNEXT,
450 NSCMD_SELECTCHARNEXT,
451 NSCMD_SELECTWORDNEXT
454 TRACE("right\n");
455 handle_arrow_key(This, key_event, cmds);
456 break;
458 case DOM_VK_UP: {
459 static const char * const cmds[] = {
460 NSCMD_LINEPREVIOUS,
461 NSCMD_MOVEPAGEUP,
462 NSCMD_SELECTLINEPREVIOUS,
463 NSCMD_SELECTPAGEUP
466 TRACE("up\n");
467 handle_arrow_key(This, key_event, cmds);
468 break;
470 case DOM_VK_DOWN: {
471 static const char * const cmds[] = {
472 NSCMD_LINENEXT,
473 NSCMD_MOVEPAGEDOWN,
474 NSCMD_SELECTLINENEXT,
475 NSCMD_SELECTPAGEDOWN
478 TRACE("down\n");
479 handle_arrow_key(This, key_event, cmds);
480 break;
482 case DOM_VK_DELETE: {
483 static const char * const cmds[] = {
484 NSCMD_DELETECHARFORWARD,
485 NSCMD_DELETEWORDFORWARD,
486 NULL, NULL
489 TRACE("delete\n");
490 handle_arrow_key(This, key_event, cmds);
491 break;
493 case DOM_VK_HOME: {
494 static const char * const cmds[] = {
495 NSCMD_BEGINLINE,
496 NSCMD_MOVETOP,
497 NSCMD_SELECTBEGINLINE,
498 NSCMD_SELECTTOP
501 TRACE("home\n");
502 handle_arrow_key(This, key_event, cmds);
503 break;
505 case DOM_VK_END: {
506 static const char * const cmds[] = {
507 NSCMD_ENDLINE,
508 NSCMD_MOVEBOTTOM,
509 NSCMD_SELECTENDLINE,
510 NSCMD_SELECTBOTTOM
513 TRACE("end\n");
514 handle_arrow_key(This, key_event, cmds);
515 break;
519 nsIDOMKeyEvent_Release(key_event);
522 void handle_edit_load(HTMLDocument *This)
524 This->nscontainer->reset_focus = GetFocus();
525 get_editor_controller(This->nscontainer);
528 static void set_ns_fontname(NSContainer *This, const char *fontname)
530 nsICommandParams *nsparam = create_nscommand_params();
532 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
533 do_ns_command(This, NSCMD_FONTFACE, nsparam);
534 nsICommandParams_Release(nsparam);
537 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
539 TRACE("(%p)->(%p %p)\n", This, in, out);
541 if(This->nscontainer)
542 do_ns_editor_command(This->nscontainer, NSCMD_DELETECHARFORWARD);
544 update_doc(This, UPDATE_UI);
545 return S_OK;
548 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
550 TRACE("(%p)->(%p %p)\n", This, in, out);
552 if(!This->nscontainer) {
553 update_doc(This, UPDATE_UI);
554 return E_FAIL;
557 if(in) {
558 char *stra;
560 if(V_VT(in) != VT_BSTR) {
561 FIXME("Unsupported vt=%d\n", V_VT(out));
562 return E_INVALIDARG;
565 TRACE("%s\n", debugstr_w(V_BSTR(in)));
567 stra = heap_strdupWtoA(V_BSTR(in));
568 set_ns_fontname(This->nscontainer, stra);
569 heap_free(stra);
571 update_doc(This, UPDATE_UI);
574 if(out) {
575 nsICommandParams *nsparam;
576 LPWSTR strw;
577 char *stra;
578 DWORD len;
579 nsresult nsres;
581 V_VT(out) = VT_BSTR;
582 V_BSTR(out) = NULL;
584 nsparam = create_nscommand_params();
586 nsres = get_ns_command_state(This->nscontainer, NSCMD_FONTFACE, nsparam);
587 if(NS_FAILED(nsres))
588 return S_OK;
590 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
591 nsICommandParams_Release(nsparam);
593 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
594 strw = heap_alloc(len*sizeof(WCHAR));
595 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, -1);
596 nsfree(stra);
598 V_BSTR(out) = SysAllocString(strw);
599 heap_free(strw);
602 return S_OK;
605 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
607 TRACE("(%p)->(%p %p)\n", This, in, out);
609 if(in) {
610 if(V_VT(in) == VT_I4) {
611 nsICommandParams *nsparam = create_nscommand_params();
612 char color_str[10];
614 sprintf(color_str, "#%02x%02x%02x",
615 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
617 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
618 do_ns_command(This->nscontainer, NSCMD_FONTCOLOR, nsparam);
620 nsICommandParams_Release(nsparam);
621 }else {
622 FIXME("unsupported in vt=%d\n", V_VT(in));
625 update_doc(This, UPDATE_UI);
628 if(out) {
629 FIXME("unsupported out\n");
630 return E_NOTIMPL;
633 return S_OK;
636 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
638 TRACE("(%p)->(%p %p)\n", This, in, out);
640 if(out) {
641 WCHAR val[10] = {0};
643 get_font_size(This, val);
644 V_VT(out) = VT_I4;
645 V_I4(out) = strtolW(val, NULL, 10);
648 if(in) {
649 switch(V_VT(in)) {
650 case VT_I4: {
651 WCHAR size[10];
652 static const WCHAR format[] = {'%','d',0};
653 wsprintfW(size, format, V_I4(in));
654 set_font_size(This, size);
655 break;
657 case VT_BSTR:
658 set_font_size(This, V_BSTR(in));
659 break;
660 default:
661 FIXME("unsupported vt %d\n", V_VT(in));
664 update_doc(This, UPDATE_UI);
667 return S_OK;
670 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
673 FIXME("(%p)->(%p %p)\n", This, in, out);
674 return E_NOTIMPL;
677 static HRESULT exec_selectall(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
679 TRACE("(%p)\n", This);
681 if(in || out)
682 FIXME("unsupported args\n");
684 if(This->nscontainer)
685 do_ns_command(This->nscontainer, NSCMD_SELECTALL, NULL);
687 update_doc(This, UPDATE_UI);
688 return S_OK;
691 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
693 TRACE("(%p)\n", This);
695 if(in || out)
696 FIXME("unsupported args\n");
698 if(This->nscontainer)
699 do_ns_command(This->nscontainer, NSCMD_BOLD, NULL);
701 update_doc(This, UPDATE_UI);
702 return S_OK;
705 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
707 TRACE("(%p)\n", This);
709 if(in || out)
710 FIXME("unsupported args\n");
712 if(This->nscontainer)
713 do_ns_command(This->nscontainer, NSCMD_ITALIC, NULL);
715 update_doc(This, UPDATE_UI);
716 return S_OK;
719 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
721 switch(cmd->cmdID) {
722 case IDM_JUSTIFYCENTER:
723 TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
724 cmd->cmdf = query_align_status(This, NSALIGN_CENTER);
725 break;
726 case IDM_JUSTIFYLEFT:
727 TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
728 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
729 if(This->usermode != EDITMODE || This->readystate < READYSTATE_INTERACTIVE)
730 cmd->cmdf = OLECMDF_SUPPORTED;
731 else
732 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
733 break;
734 case IDM_JUSTIFYRIGHT:
735 TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
736 cmd->cmdf = query_align_status(This, NSALIGN_RIGHT);
737 break;
740 return S_OK;
743 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
745 TRACE("(%p)\n", This);
747 if(in || out)
748 FIXME("unsupported args\n");
750 set_ns_align(This, NSALIGN_CENTER);
751 update_doc(This, UPDATE_UI);
752 return S_OK;
755 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
757 TRACE("(%p)\n", This);
759 if(in || out)
760 FIXME("unsupported args\n");
762 set_ns_align(This, NSALIGN_LEFT);
763 update_doc(This, UPDATE_UI);
764 return S_OK;
767 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
769 TRACE("(%p)\n", This);
771 if(in || out)
772 FIXME("unsupported args\n");
774 set_ns_align(This, NSALIGN_RIGHT);
775 update_doc(This, UPDATE_UI);
776 return S_OK;
779 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
781 TRACE("(%p)\n", This);
783 if(in || out)
784 FIXME("unsupported args\n");
786 if(This->nscontainer)
787 do_ns_command(This->nscontainer, NSCMD_UNDERLINE, NULL);
789 update_doc(This, UPDATE_UI);
790 return S_OK;
793 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
795 TRACE("(%p)\n", This);
797 if(in || out)
798 FIXME("unsupported args\n");
800 if(This->nscontainer)
801 do_ns_command(This->nscontainer, NSCMD_INSERTHR, NULL);
803 update_doc(This, UPDATE_UI);
804 return S_OK;
807 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
809 TRACE("(%p)\n", This);
811 if(in || out)
812 FIXME("unsupported args\n");
814 if(This->nscontainer)
815 do_ns_command(This->nscontainer, NSCMD_OL, NULL);
817 update_doc(This, UPDATE_UI);
818 return S_OK;
821 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
823 TRACE("(%p)\n", This);
825 if(in || out)
826 FIXME("unsupported args\n");
828 if(This->nscontainer)
829 do_ns_command(This->nscontainer, NSCMD_UL, NULL);
831 update_doc(This, UPDATE_UI);
832 return S_OK;
835 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
837 TRACE("(%p)\n", This);
839 if(in || out)
840 FIXME("unsupported args\n");
842 if(This->nscontainer)
843 do_ns_command(This->nscontainer, NSCMD_INDENT, NULL);
845 update_doc(This, UPDATE_UI);
846 return S_OK;
849 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
851 TRACE("(%p)\n", This);
853 if(in || out)
854 FIXME("unsupported args\n");
856 if(This->nscontainer)
857 do_ns_command(This->nscontainer, NSCMD_OUTDENT, NULL);
859 update_doc(This, UPDATE_UI);
860 return S_OK;
863 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
865 WCHAR *ptr;
867 if(out || !in || V_VT(in) != VT_BSTR) {
868 WARN("invalid arg\n");
869 return E_INVALIDARG;
872 TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
874 update_doc(This, UPDATE_UI);
876 ptr = V_BSTR(in);
877 if(*ptr == '1')
878 exec_bold(This, cmdexecopt, NULL, NULL);
879 ptr = strchrW(ptr, ',');
880 if(!ptr)
881 return S_OK;
883 if(*++ptr == '1')
884 exec_italic(This, cmdexecopt, NULL, NULL);
885 ptr = strchrW(ptr, ',');
886 if(!ptr)
887 return S_OK;
889 if(*++ptr == '1')
890 exec_underline(This, cmdexecopt, NULL, NULL);
891 ptr = strchrW(ptr, ',');
892 if(!ptr)
893 return S_OK;
895 if(isdigitW(*++ptr)) {
896 VARIANT v;
898 V_VT(&v) = VT_I4;
899 V_I4(&v) = *ptr-'0';
901 exec_fontsize(This, cmdexecopt, &v, NULL);
903 ptr = strchrW(ptr, ',');
904 if(!ptr)
905 return S_OK;
907 if(*++ptr != ',')
908 FIXME("set font color\n");
909 ptr = strchrW(ptr, ',');
910 if(!ptr)
911 return S_OK;
913 if(*++ptr != ',')
914 FIXME("set background color\n");
915 ptr = strchrW(ptr, ',');
916 if(!ptr)
917 return S_OK;
919 ptr++;
920 if(*ptr) {
921 VARIANT v;
923 V_VT(&v) = VT_BSTR;
924 V_BSTR(&v) = SysAllocString(ptr);
926 exec_fontname(This, cmdexecopt, &v, NULL);
928 SysFreeString(V_BSTR(&v));
931 return S_OK;
934 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
936 update_doc(This, UPDATE_UI);
938 if(!This->nscontainer)
939 return E_FAIL;
941 do_ns_editor_command(This->nscontainer, NSCMD_COPY);
942 return S_OK;
945 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
947 update_doc(This, UPDATE_UI);
949 if(!This->nscontainer)
950 return E_FAIL;
952 do_ns_editor_command(This->nscontainer, NSCMD_CUT);
953 return S_OK;
956 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
958 update_doc(This, UPDATE_UI);
960 if(!This->nscontainer)
961 return E_FAIL;
963 do_ns_editor_command(This->nscontainer, NSCMD_PASTE);
964 return S_OK;
967 static HRESULT exec_setdirty(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
969 TRACE("(%p)->(%08x %p %p)\n", This, cmdexecopt, in, out);
971 if(!in)
972 return S_OK;
974 if(V_VT(in) == VT_BOOL)
975 set_dirty(This, V_BOOL(in));
976 else
977 FIXME("unsupported vt=%d\n", V_VT(in));
979 return S_OK;
982 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
984 switch(cmd->cmdID) {
985 case IDM_DELETE:
986 TRACE("CGID_MSHTML: IDM_DELETE\n");
987 cmd->cmdf = query_ns_edit_status(This, NULL);
988 break;
989 case IDM_FONTNAME:
990 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
991 cmd->cmdf = query_ns_edit_status(This, NULL);
992 break;
993 case IDM_FONTSIZE:
994 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
995 cmd->cmdf = query_ns_edit_status(This, NULL);
996 break;
997 case IDM_BOLD:
998 TRACE("CGID_MSHTML: IDM_BOLD\n");
999 cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
1000 break;
1001 case IDM_FORECOLOR:
1002 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
1003 cmd->cmdf = query_ns_edit_status(This, NULL);
1004 break;
1005 case IDM_ITALIC:
1006 TRACE("CGID_MSHTML: IDM_ITALIC\n");
1007 cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
1008 break;
1009 case IDM_UNDERLINE:
1010 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
1011 cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
1012 break;
1013 case IDM_HORIZONTALLINE:
1014 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1015 cmd->cmdf = query_ns_edit_status(This, NULL);
1016 break;
1017 case IDM_ORDERLIST:
1018 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
1019 cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
1020 break;
1021 case IDM_UNORDERLIST:
1022 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
1023 cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
1024 break;
1025 case IDM_INDENT:
1026 TRACE("CGID_MSHTML: IDM_INDENT\n");
1027 cmd->cmdf = query_ns_edit_status(This, NULL);
1028 break;
1029 case IDM_OUTDENT:
1030 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
1031 cmd->cmdf = query_ns_edit_status(This, NULL);
1032 break;
1033 case IDM_HYPERLINK:
1034 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
1035 cmd->cmdf = query_ns_edit_status(This, NULL);
1036 break;
1039 return S_OK;
1042 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1044 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
1046 switch (msg)
1048 case WM_INITDIALOG:
1050 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1051 static const WCHAR wszFtp[] = {'f','t','p',':',0};
1052 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
1053 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
1054 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
1055 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
1056 INT def_idx;
1057 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
1058 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1059 INT len;
1061 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
1063 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1064 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1065 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1066 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1067 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1068 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1069 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1070 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1072 /* force the updating of the URL edit box */
1073 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1075 SetFocus(hwndURL);
1076 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1077 SendMessageW(hwndURL, EM_SETSEL, 0, len);
1079 return FALSE;
1081 case WM_COMMAND:
1082 switch (wparam)
1084 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1085 EndDialog(hwnd, wparam);
1086 return TRUE;
1087 case MAKEWPARAM(IDOK, BN_CLICKED):
1089 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1090 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1091 INT len = GetWindowTextLengthW(hwndURL);
1092 *url = SysAllocStringLen(NULL, len + 1);
1093 GetWindowTextW(hwndURL, *url, len + 1);
1094 EndDialog(hwnd, wparam);
1095 return TRUE;
1097 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1099 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1100 INT item;
1101 INT len;
1102 LPWSTR type;
1103 LPWSTR url;
1104 LPWSTR p;
1105 static const WCHAR wszSlashSlash[] = {'/','/'};
1107 /* get string of currently selected hyperlink type */
1108 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1109 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1110 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1111 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1113 if (!strcmpW(type, wszOther))
1114 *type = '\0';
1116 /* get current URL */
1117 len = GetWindowTextLengthW(hwndURL);
1118 url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1119 GetWindowTextW(hwndURL, url, len + 1);
1121 /* strip off old protocol */
1122 p = strchrW(url, ':');
1123 if (p && p[1] == '/' && p[2] == '/')
1124 p += 3;
1125 if (!p) p = url;
1126 memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1128 /* add new protocol */
1129 if (*type != '\0')
1131 memcpy(url, type, strlenW(type) * sizeof(WCHAR));
1132 memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1135 SetWindowTextW(hwndURL, url);
1137 HeapFree(GetProcessHeap(), 0, url);
1138 HeapFree(GetProcessHeap(), 0, type);
1139 return TRUE;
1142 return FALSE;
1143 case WM_CLOSE:
1144 EndDialog(hwnd, IDCANCEL);
1145 return TRUE;
1146 default:
1147 return FALSE;
1151 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1153 BSTR url = NULL;
1154 INT ret;
1155 nsISelection *nsselection;
1156 nsIDOMDocument *nsdoc;
1157 nsresult nsres;
1158 HRESULT hres = E_FAIL;
1160 TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1162 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1164 if (!in || V_VT(in) != VT_BSTR)
1166 WARN("invalid arg\n");
1167 return E_INVALIDARG;
1169 url = V_BSTR(in);
1171 else
1173 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1174 if (ret != IDOK)
1175 return OLECMDERR_E_CANCELED;
1178 nsselection = get_ns_selection(This);
1179 if (!nsselection)
1180 return E_FAIL;
1182 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
1183 if(NS_SUCCEEDED(nsres))
1185 static const WCHAR wszA[] = {'a',0};
1186 static const WCHAR wszHref[] = {'h','r','e','f',0};
1187 nsIHTMLEditor *html_editor;
1188 nsIDOMNode *text_node;
1189 nsIDOMElement *anchor_elem;
1190 nsIDOMNode *unused_node;
1191 nsAString a_str;
1192 nsAString href_str;
1193 nsAString ns_url;
1194 PRBool insert_link_at_caret;
1196 nsAString_Init(&a_str, wszA);
1197 nsAString_Init(&href_str, wszHref);
1198 nsAString_Init(&ns_url, url);
1200 /* create an element for the link */
1201 nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
1202 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1204 nsAString_Finish(&href_str);
1205 nsAString_Finish(&a_str);
1207 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1209 /* create an element with text of URL */
1210 if (insert_link_at_caret)
1212 nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
1214 /* wrap the <a> tags around the text element */
1215 nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
1216 nsIDOMNode_Release(text_node);
1217 nsIDOMNode_Release(unused_node);
1220 nsAString_Finish(&ns_url);
1222 nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1223 if (html_editor)
1225 if (insert_link_at_caret)
1227 /* add them to the document at the caret position */
1228 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
1229 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1231 else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1232 nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, anchor_elem);
1233 nsIHTMLEditor_Release(html_editor);
1236 nsIDOMElement_Release(anchor_elem);
1237 nsIDOMDocument_Release(nsdoc);
1239 hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1242 nsISelection_Release(nsselection);
1244 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1245 SysFreeString(url);
1247 TRACE("-- 0x%08x\n", nsres);
1248 return hres;
1251 static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
1253 TRACE("(%p)->(%p)\n", This, cmd);
1255 cmd->cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
1256 return S_OK;
1259 const cmdtable_t editmode_cmds[] = {
1260 {IDM_DELETE, query_edit_status, exec_delete},
1261 {IDM_FONTNAME, query_edit_status, exec_fontname},
1262 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1263 {IDM_SELECTALL, query_selall_status , exec_selectall},
1264 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1265 {IDM_BOLD, query_edit_status, exec_bold},
1266 {IDM_ITALIC, query_edit_status, exec_italic},
1267 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1268 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1269 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1270 {IDM_FONT, NULL, exec_font},
1271 {IDM_UNDERLINE, query_edit_status, exec_underline},
1272 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1273 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1274 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1275 {IDM_INDENT, query_edit_status, exec_indent},
1276 {IDM_OUTDENT, query_edit_status, exec_outdent},
1277 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1278 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1279 {IDM_SETDIRTY, NULL, exec_setdirty},
1280 {0,NULL,NULL}
1283 void init_editor(HTMLDocument *This)
1285 update_doc(This, UPDATE_UI);
1287 if(!This->nscontainer)
1288 return;
1290 set_ns_fontname(This->nscontainer, "Times New Roman");
1293 HRESULT editor_is_dirty(HTMLDocument *This)
1295 PRBool modified;
1297 if(!This->nscontainer || !This->nscontainer->editor)
1298 return S_FALSE;
1300 nsIEditor_GetDocumentModified(This->nscontainer->editor, &modified);
1302 return modified ? S_OK : S_FALSE;