makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / mshtml / editor.c
blob0a1b4879bde5b44b9381965b4a7c30ae7d02bb92
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(GeckoBrowser *browser, VARIANT_BOOL dirty)
100 nsresult nsres;
102 if(browser->usermode != EDITMODE || !browser->editor)
103 return;
105 if(dirty) {
106 nsres = nsIEditor_IncrementModificationCount(browser->editor, 1);
107 if(NS_FAILED(nsres))
108 ERR("IncrementModificationCount failed: %08x\n", nsres);
109 }else {
110 nsres = nsIEditor_ResetModificationCount(browser->editor);
111 if(NS_FAILED(nsres))
112 ERR("ResetModificationCount failed: %08x\n", nsres);
116 static void do_ns_editor_command(GeckoBrowser *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(GeckoBrowser *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->window_proxy, 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(HTMLDocumentNode *doc, const char *nscmd)
149 nsICommandParams *nsparam;
150 cpp_bool b = FALSE;
152 if(doc->browser->usermode != EDITMODE || doc->basedoc.window->readystate < READYSTATE_INTERACTIVE)
153 return OLECMDF_SUPPORTED;
155 if(nscmd) {
156 nsparam = create_nscommand_params();
157 get_ns_command_state(doc->browser, 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(HTMLDocumentNode *doc, const char *align_str)
169 nsICommandParams *nsparam;
171 nsparam = create_nscommand_params();
172 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
174 do_ns_command(doc, NSCMD_ALIGN, nsparam);
176 nsICommandParams_Release(nsparam);
179 static DWORD query_align_status(HTMLDocumentNode *doc, const WCHAR *align)
181 DWORD ret = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
182 nsAString justify_str;
183 cpp_bool b;
184 nsresult nsres;
186 if(doc->browser->usermode != EDITMODE || doc->basedoc.window->readystate < READYSTATE_INTERACTIVE)
187 return OLECMDF_SUPPORTED;
189 nsAString_Init(&justify_str, align);
190 nsres = nsIDOMHTMLDocument_QueryCommandState(doc->nsdoc, &justify_str, &b);
191 nsAString_Finish(&justify_str);
192 if(NS_SUCCEEDED(nsres) && b)
193 ret |= OLECMDF_LATCHED;
195 return ret;
199 static nsISelection *get_ns_selection(HTMLDocumentNode *doc)
201 nsISelection *nsselection = NULL;
202 nsresult nsres;
204 nsres = nsIDOMWindow_GetSelection(doc->basedoc.window->nswindow, &nsselection);
205 if(NS_FAILED(nsres))
206 ERR("GetSelection failed %08x\n", nsres);
208 return nsselection;
212 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
214 cpp_bool has_children;
215 UINT32 child_cnt, i;
216 nsIDOMNode *child_node;
217 nsIDOMNodeList *node_list;
218 UINT16 node_type;
220 nsIDOMElement_HasChildNodes(elem, &has_children);
221 if(!has_children)
222 return;
224 nsIDOMElement_GetChildNodes(elem, &node_list);
225 nsIDOMNodeList_GetLength(node_list, &child_cnt);
227 for(i=0; i<child_cnt; i++) {
228 nsIDOMNodeList_Item(node_list, i, &child_node);
230 nsIDOMNode_GetNodeType(child_node, &node_type);
231 if(node_type == ELEMENT_NODE) {
232 nsIDOMElement *child_elem;
233 nsAString tag_str;
234 const PRUnichar *ctag;
236 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
238 nsAString_Init(&tag_str, NULL);
239 nsIDOMElement_GetTagName(child_elem, &tag_str);
240 nsAString_GetData(&tag_str, &ctag);
242 if(!wcsicmp(ctag, tag))
243 /* FIXME: remove node if there are no more attributes */
244 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
246 nsAString_Finish(&tag_str);
248 remove_child_attr(child_elem, tag, attr_str);
250 nsIDOMElement_Release(child_elem);
253 nsIDOMNode_Release(child_node);
256 nsIDOMNodeList_Release(node_list);
259 static void get_font_size(HTMLDocumentNode *doc, WCHAR *ret)
261 nsISelection *nsselection = get_ns_selection(doc);
262 nsIDOMElement *elem = NULL;
263 nsIDOMNode *node = NULL, *tmp_node;
264 nsAString tag_str;
265 LPCWSTR tag;
266 UINT16 node_type;
267 nsresult nsres;
269 *ret = 0;
271 if(!nsselection)
272 return;
274 nsISelection_GetFocusNode(nsselection, &node);
275 nsISelection_Release(nsselection);
277 while(node) {
278 nsres = nsIDOMNode_GetNodeType(node, &node_type);
279 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
280 break;
282 if(node_type == ELEMENT_NODE) {
283 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
285 nsAString_Init(&tag_str, NULL);
286 nsIDOMElement_GetTagName(elem, &tag_str);
287 nsAString_GetData(&tag_str, &tag);
289 if(!wcsicmp(tag, fontW)) {
290 nsAString val_str;
291 const PRUnichar *val;
293 TRACE("found font tag %p\n", elem);
295 get_elem_attr_value(elem, sizeW, &val_str, &val);
296 if(*val) {
297 TRACE("found size %s\n", debugstr_w(val));
298 lstrcpyW(ret, val);
301 nsAString_Finish(&val_str);
304 nsAString_Finish(&tag_str);
305 nsIDOMElement_Release(elem);
308 if(*ret)
309 break;
311 tmp_node = node;
312 nsIDOMNode_GetParentNode(node, &node);
313 nsIDOMNode_Release(tmp_node);
316 if(node)
317 nsIDOMNode_Release(node);
320 static void set_font_size(HTMLDocumentNode *doc, LPCWSTR size)
322 nsISelection *nsselection;
323 cpp_bool collapsed;
324 nsIDOMElement *elem;
325 nsIDOMRange *range;
326 LONG range_cnt = 0;
327 nsAString size_str;
328 nsAString val_str;
330 nsselection = get_ns_selection(doc);
331 if(!nsselection)
332 return;
334 nsISelection_GetRangeCount(nsselection, &range_cnt);
335 if(range_cnt != 1) {
336 FIXME("range_cnt %d not supprted\n", range_cnt);
337 if(!range_cnt) {
338 nsISelection_Release(nsselection);
339 return;
343 create_nselem(doc, fontW, &elem);
345 nsAString_InitDepend(&size_str, sizeW);
346 nsAString_InitDepend(&val_str, size);
348 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
349 nsAString_Finish(&val_str);
351 nsISelection_GetRangeAt(nsselection, 0, &range);
352 nsISelection_GetIsCollapsed(nsselection, &collapsed);
353 nsISelection_RemoveAllRanges(nsselection);
355 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
357 if(collapsed) {
358 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
359 }else {
360 /* Remove all size attributes from the range */
361 remove_child_attr(elem, fontW, &size_str);
362 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
365 nsISelection_Release(nsselection);
366 nsIDOMRange_Release(range);
367 nsIDOMElement_Release(elem);
369 nsAString_Finish(&size_str);
371 set_dirty(doc->browser, VARIANT_TRUE);
374 static void handle_arrow_key(HTMLDocumentNode *doc, nsIDOMEvent *event, nsIDOMKeyEvent *key_event, const char * const cmds[4])
376 int i=0;
377 cpp_bool b;
379 nsIDOMKeyEvent_GetCtrlKey(key_event, &b);
380 if(b)
381 i |= 1;
383 nsIDOMKeyEvent_GetShiftKey(key_event, &b);
384 if(b)
385 i |= 2;
387 if(cmds[i])
388 do_ns_editor_command(doc->browser, cmds[i]);
390 nsIDOMEvent_PreventDefault(event);
393 void handle_edit_event(HTMLDocumentNode *doc, nsIDOMEvent *event)
395 nsIDOMKeyEvent *key_event;
396 UINT32 code;
398 nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
400 nsIDOMKeyEvent_GetKeyCode(key_event, &code);
402 switch(code) {
403 case DOM_VK_LEFT: {
404 static const char * const cmds[] = {
405 NSCMD_CHARPREVIOUS,
406 NSCMD_WORDPREVIOUS,
407 NSCMD_SELECTCHARPREVIOUS,
408 NSCMD_SELECTWORDPREVIOUS
411 TRACE("left\n");
412 handle_arrow_key(doc, event, key_event, cmds);
413 break;
415 case DOM_VK_RIGHT: {
416 static const char * const cmds[] = {
417 NSCMD_CHARNEXT,
418 NSCMD_WORDNEXT,
419 NSCMD_SELECTCHARNEXT,
420 NSCMD_SELECTWORDNEXT
423 TRACE("right\n");
424 handle_arrow_key(doc, event, key_event, cmds);
425 break;
427 case DOM_VK_UP: {
428 static const char * const cmds[] = {
429 NSCMD_LINEPREVIOUS,
430 NSCMD_MOVEPAGEUP,
431 NSCMD_SELECTLINEPREVIOUS,
432 NSCMD_SELECTPAGEUP
435 TRACE("up\n");
436 handle_arrow_key(doc, event, key_event, cmds);
437 break;
439 case DOM_VK_DOWN: {
440 static const char * const cmds[] = {
441 NSCMD_LINENEXT,
442 NSCMD_MOVEPAGEDOWN,
443 NSCMD_SELECTLINENEXT,
444 NSCMD_SELECTPAGEDOWN
447 TRACE("down\n");
448 handle_arrow_key(doc, event, key_event, cmds);
449 break;
451 case DOM_VK_DELETE: {
452 static const char * const cmds[] = {
453 NSCMD_DELETECHARFORWARD,
454 NSCMD_DELETEWORDFORWARD,
455 NULL, NULL
458 TRACE("delete\n");
459 handle_arrow_key(doc, event, key_event, cmds);
460 break;
462 case DOM_VK_HOME: {
463 static const char * const cmds[] = {
464 NSCMD_BEGINLINE,
465 NSCMD_MOVETOP,
466 NSCMD_SELECTBEGINLINE,
467 NSCMD_SELECTTOP
470 TRACE("home\n");
471 handle_arrow_key(doc, event, key_event, cmds);
472 break;
474 case DOM_VK_END: {
475 static const char * const cmds[] = {
476 NSCMD_ENDLINE,
477 NSCMD_MOVEBOTTOM,
478 NSCMD_SELECTENDLINE,
479 NSCMD_SELECTBOTTOM
482 TRACE("end\n");
483 handle_arrow_key(doc, event, key_event, cmds);
484 break;
488 nsIDOMKeyEvent_Release(key_event);
491 static void set_ns_fontname(HTMLDocumentNode *doc, const char *fontname)
493 nsICommandParams *nsparam = create_nscommand_params();
495 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
496 do_ns_command(doc, NSCMD_FONTFACE, nsparam);
497 nsICommandParams_Release(nsparam);
500 static HRESULT exec_delete(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
502 TRACE("(%p)->(%p %p)\n", doc, in, out);
504 do_ns_editor_command(doc->browser, NSCMD_DELETECHARFORWARD);
506 update_doc(doc->browser->doc, UPDATE_UI);
507 return S_OK;
510 static HRESULT exec_fontname(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
512 TRACE("(%p)->(%p %p)\n", doc, in, out);
514 if(in) {
515 char *stra;
517 if(V_VT(in) != VT_BSTR) {
518 FIXME("Unsupported fontname %s\n", debugstr_variant(out));
519 return E_INVALIDARG;
522 TRACE("%s\n", debugstr_w(V_BSTR(in)));
524 stra = heap_strdupWtoA(V_BSTR(in));
525 set_ns_fontname(doc, stra);
526 heap_free(stra);
528 update_doc(doc->browser->doc, UPDATE_UI);
531 if(out) {
532 nsICommandParams *nsparam;
533 LPWSTR strw;
534 char *stra;
535 DWORD len;
536 nsresult nsres;
538 V_VT(out) = VT_BSTR;
539 V_BSTR(out) = NULL;
541 nsparam = create_nscommand_params();
543 nsres = get_ns_command_state(doc->browser, NSCMD_FONTFACE, nsparam);
544 if(NS_FAILED(nsres))
545 return S_OK;
547 nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
548 nsICommandParams_Release(nsparam);
550 len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
551 strw = heap_alloc(len*sizeof(WCHAR));
552 MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, len);
553 nsfree(stra);
555 V_BSTR(out) = SysAllocString(strw);
556 heap_free(strw);
559 return S_OK;
562 static HRESULT exec_forecolor(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
564 TRACE("(%p)->(%p %p)\n", doc, in, out);
566 if(in) {
567 if(V_VT(in) == VT_I4) {
568 nsICommandParams *nsparam = create_nscommand_params();
569 char color_str[10];
571 sprintf(color_str, "#%02x%02x%02x",
572 V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
574 nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
575 do_ns_command(doc, NSCMD_FONTCOLOR, nsparam);
577 nsICommandParams_Release(nsparam);
578 }else {
579 FIXME("unsupported forecolor %s\n", debugstr_variant(in));
582 update_doc(doc->browser->doc, UPDATE_UI);
585 if(out) {
586 FIXME("unsupported out\n");
587 return E_NOTIMPL;
590 return S_OK;
593 static HRESULT exec_fontsize(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
595 TRACE("(%p)->(%p %p)\n", doc, in, out);
597 if(out) {
598 WCHAR val[10] = {0};
600 get_font_size(doc, val);
601 V_VT(out) = VT_I4;
602 V_I4(out) = wcstol(val, NULL, 10);
605 if(in) {
606 switch(V_VT(in)) {
607 case VT_I4: {
608 WCHAR size[10];
609 static const WCHAR format[] = {'%','d',0};
610 wsprintfW(size, format, V_I4(in));
611 set_font_size(doc, size);
612 break;
614 case VT_BSTR:
615 set_font_size(doc, V_BSTR(in));
616 break;
617 default:
618 FIXME("unsupported fontsize %s\n", debugstr_variant(in));
621 update_doc(doc->browser->doc, UPDATE_UI);
624 return S_OK;
627 static HRESULT exec_font(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
630 FIXME("(%p)->(%p %p)\n", doc, in, out);
631 return E_NOTIMPL;
634 static HRESULT exec_bold(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
636 TRACE("(%p)\n", doc);
638 if(in || out)
639 FIXME("unsupported args\n");
641 do_ns_command(doc, NSCMD_BOLD, NULL);
642 update_doc(doc->browser->doc, UPDATE_UI);
643 return S_OK;
646 static HRESULT exec_italic(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
648 TRACE("(%p)\n", doc);
650 if(in || out)
651 FIXME("unsupported args\n");
653 do_ns_command(doc, NSCMD_ITALIC, NULL);
654 update_doc(doc->browser->doc, UPDATE_UI);
655 return S_OK;
658 static HRESULT query_justify(HTMLDocumentNode *doc, OLECMD *cmd)
660 static const PRUnichar justifycenterW[] = {'j','u','s','t','i','f','y','c','e','n','t','e','r',0};
661 static const PRUnichar justifyrightW[] = {'j','u','s','t','i','f','y','r','i','g','h','t',0};
663 switch(cmd->cmdID) {
664 case IDM_JUSTIFYCENTER:
665 TRACE("(%p) IDM_JUSTIFYCENTER\n", doc);
666 cmd->cmdf = query_align_status(doc, justifycenterW);
667 break;
668 case IDM_JUSTIFYLEFT:
669 TRACE("(%p) IDM_JUSTIFYLEFT\n", doc);
670 /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
671 if(doc->browser->usermode != EDITMODE || doc->basedoc.window->readystate < READYSTATE_INTERACTIVE)
672 cmd->cmdf = OLECMDF_SUPPORTED;
673 else
674 cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
675 break;
676 case IDM_JUSTIFYRIGHT:
677 TRACE("(%p) IDM_JUSTIFYRIGHT\n", doc);
678 cmd->cmdf = query_align_status(doc, justifyrightW);
679 break;
682 return S_OK;
685 static HRESULT exec_justifycenter(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
687 TRACE("(%p)\n", doc);
689 if(in || out)
690 FIXME("unsupported args\n");
692 set_ns_align(doc, NSALIGN_CENTER);
693 update_doc(doc->browser->doc, UPDATE_UI);
694 return S_OK;
697 static HRESULT exec_justifyleft(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
699 TRACE("(%p)\n", doc);
701 if(in || out)
702 FIXME("unsupported args\n");
704 set_ns_align(doc, NSALIGN_LEFT);
705 update_doc(doc->browser->doc, UPDATE_UI);
706 return S_OK;
709 static HRESULT exec_justifyright(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
711 TRACE("(%p)\n", doc);
713 if(in || out)
714 FIXME("unsupported args\n");
716 set_ns_align(doc, NSALIGN_RIGHT);
717 update_doc(doc->browser->doc, UPDATE_UI);
718 return S_OK;
721 static HRESULT exec_underline(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
723 TRACE("(%p)\n", doc);
725 if(in || out)
726 FIXME("unsupported args\n");
728 do_ns_command(doc, NSCMD_UNDERLINE, NULL);
729 update_doc(doc->browser->doc, UPDATE_UI);
730 return S_OK;
733 static HRESULT exec_horizontalline(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
735 TRACE("(%p)\n", doc);
737 if(in || out)
738 FIXME("unsupported args\n");
740 do_ns_command(doc, NSCMD_INSERTHR, NULL);
741 update_doc(doc->browser->doc, UPDATE_UI);
742 return S_OK;
745 static HRESULT exec_orderlist(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
747 TRACE("(%p)\n", doc);
749 if(in || out)
750 FIXME("unsupported args\n");
752 do_ns_command(doc, NSCMD_OL, NULL);
753 update_doc(doc->browser->doc, UPDATE_UI);
754 return S_OK;
757 static HRESULT exec_unorderlist(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
759 TRACE("(%p)\n", doc);
761 if(in || out)
762 FIXME("unsupported args\n");
764 do_ns_command(doc, NSCMD_UL, NULL);
765 update_doc(doc->browser->doc, UPDATE_UI);
766 return S_OK;
769 static HRESULT exec_indent(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
771 TRACE("(%p)\n", doc);
773 if(in || out)
774 FIXME("unsupported args\n");
776 do_ns_command(doc, NSCMD_INDENT, NULL);
777 update_doc(doc->browser->doc, UPDATE_UI);
778 return S_OK;
781 static HRESULT exec_outdent(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
783 TRACE("(%p)\n", doc);
785 if(in || out)
786 FIXME("unsupported args\n");
788 do_ns_command(doc, NSCMD_OUTDENT, NULL);
789 update_doc(doc->browser->doc, UPDATE_UI);
790 return S_OK;
793 static HRESULT exec_composesettings(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
795 WCHAR *ptr;
797 if(out || !in || V_VT(in) != VT_BSTR) {
798 WARN("invalid arg %s\n", debugstr_variant(in));
799 return E_INVALIDARG;
802 TRACE("(%p)->(%x %s)\n", doc, cmdexecopt, debugstr_w(V_BSTR(in)));
804 update_doc(doc->browser->doc, UPDATE_UI);
806 ptr = V_BSTR(in);
807 if(*ptr == '1')
808 exec_bold(doc, cmdexecopt, NULL, NULL);
809 ptr = wcschr(ptr, ',');
810 if(!ptr)
811 return S_OK;
813 if(*++ptr == '1')
814 exec_italic(doc, cmdexecopt, NULL, NULL);
815 ptr = wcschr(ptr, ',');
816 if(!ptr)
817 return S_OK;
819 if(*++ptr == '1')
820 exec_underline(doc, cmdexecopt, NULL, NULL);
821 ptr = wcschr(ptr, ',');
822 if(!ptr)
823 return S_OK;
825 if(is_digit(*++ptr)) {
826 VARIANT v;
828 V_VT(&v) = VT_I4;
829 V_I4(&v) = *ptr-'0';
831 exec_fontsize(doc, cmdexecopt, &v, NULL);
833 ptr = wcschr(ptr, ',');
834 if(!ptr)
835 return S_OK;
837 if(*++ptr != ',')
838 FIXME("set font color\n");
839 ptr = wcschr(ptr, ',');
840 if(!ptr)
841 return S_OK;
843 if(*++ptr != ',')
844 FIXME("set background color\n");
845 ptr = wcschr(ptr, ',');
846 if(!ptr)
847 return S_OK;
849 ptr++;
850 if(*ptr) {
851 VARIANT v;
853 V_VT(&v) = VT_BSTR;
854 V_BSTR(&v) = SysAllocString(ptr);
856 exec_fontname(doc, cmdexecopt, &v, NULL);
858 SysFreeString(V_BSTR(&v));
861 return S_OK;
864 HRESULT editor_exec_copy(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
866 update_doc(doc->browser->doc, UPDATE_UI);
867 do_ns_editor_command(doc->browser, NSCMD_COPY);
868 return S_OK;
871 HRESULT editor_exec_cut(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
873 update_doc(doc->browser->doc, UPDATE_UI);
874 do_ns_editor_command(doc->browser, NSCMD_CUT);
875 return S_OK;
878 HRESULT editor_exec_paste(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
880 update_doc(doc->browser->doc, UPDATE_UI);
881 do_ns_editor_command(doc->browser, NSCMD_PASTE);
882 return S_OK;
885 static HRESULT exec_setdirty(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
887 TRACE("(%p)->(%08x %p %p)\n", doc, cmdexecopt, in, out);
889 if(!in)
890 return S_OK;
892 if(V_VT(in) == VT_BOOL)
893 set_dirty(doc->browser, V_BOOL(in));
894 else
895 FIXME("unsupported arg %s\n", debugstr_variant(in));
897 return S_OK;
900 static HRESULT query_edit_status(HTMLDocumentNode *doc, OLECMD *cmd)
902 switch(cmd->cmdID) {
903 case IDM_DELETE:
904 TRACE("CGID_MSHTML: IDM_DELETE\n");
905 cmd->cmdf = query_ns_edit_status(doc, NULL);
906 break;
907 case IDM_FONTNAME:
908 TRACE("CGID_MSHTML: IDM_FONTNAME\n");
909 cmd->cmdf = query_ns_edit_status(doc, NULL);
910 break;
911 case IDM_FONTSIZE:
912 TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
913 cmd->cmdf = query_ns_edit_status(doc, NULL);
914 break;
915 case IDM_BOLD:
916 TRACE("CGID_MSHTML: IDM_BOLD\n");
917 cmd->cmdf = query_ns_edit_status(doc, NSCMD_BOLD);
918 break;
919 case IDM_FORECOLOR:
920 TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
921 cmd->cmdf = query_ns_edit_status(doc, NULL);
922 break;
923 case IDM_ITALIC:
924 TRACE("CGID_MSHTML: IDM_ITALIC\n");
925 cmd->cmdf = query_ns_edit_status(doc, NSCMD_ITALIC);
926 break;
927 case IDM_UNDERLINE:
928 TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
929 cmd->cmdf = query_ns_edit_status(doc, NSCMD_UNDERLINE);
930 break;
931 case IDM_HORIZONTALLINE:
932 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
933 cmd->cmdf = query_ns_edit_status(doc, NULL);
934 break;
935 case IDM_ORDERLIST:
936 TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
937 cmd->cmdf = query_ns_edit_status(doc, NSCMD_OL);
938 break;
939 case IDM_UNORDERLIST:
940 TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
941 cmd->cmdf = query_ns_edit_status(doc, NSCMD_UL);
942 break;
943 case IDM_INDENT:
944 TRACE("CGID_MSHTML: IDM_INDENT\n");
945 cmd->cmdf = query_ns_edit_status(doc, NULL);
946 break;
947 case IDM_OUTDENT:
948 TRACE("CGID_MSHTML: IDM_OUTDENT\n");
949 cmd->cmdf = query_ns_edit_status(doc, NULL);
950 break;
951 case IDM_HYPERLINK:
952 TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
953 cmd->cmdf = query_ns_edit_status(doc, NULL);
954 break;
957 return S_OK;
960 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
962 static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
963 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
964 static const WCHAR wszFtp[] = {'f','t','p',':',0};
965 static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
966 static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
967 static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
968 static const WCHAR wszNews[] = {'n','e','w','s',':',0};
970 switch (msg)
972 case WM_INITDIALOG:
974 INT def_idx;
975 HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
976 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
977 INT len;
979 SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
981 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
982 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
983 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
984 def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
985 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
986 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
987 SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
988 SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
990 /* force the updating of the URL edit box */
991 SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
993 SetFocus(hwndURL);
994 len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
995 SendMessageW(hwndURL, EM_SETSEL, 0, len);
997 return FALSE;
999 case WM_COMMAND:
1000 switch (wparam)
1002 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1003 EndDialog(hwnd, wparam);
1004 return TRUE;
1005 case MAKEWPARAM(IDOK, BN_CLICKED):
1007 BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1008 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1009 INT len = GetWindowTextLengthW(hwndURL);
1010 *url = SysAllocStringLen(NULL, len + 1);
1011 GetWindowTextW(hwndURL, *url, len + 1);
1012 EndDialog(hwnd, wparam);
1013 return TRUE;
1015 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1017 HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1018 INT item;
1019 INT len;
1020 LPWSTR type;
1021 LPWSTR url;
1022 LPWSTR p;
1023 static const WCHAR wszSlashSlash[] = {'/','/'};
1025 /* get string of currently selected hyperlink type */
1026 item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1027 len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1028 type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1029 SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1031 if (!wcscmp(type, wszOther))
1032 *type = '\0';
1034 /* get current URL */
1035 len = GetWindowTextLengthW(hwndURL);
1036 url = HeapAlloc(GetProcessHeap(), 0, (len + lstrlenW(type) + 3) * sizeof(WCHAR));
1037 GetWindowTextW(hwndURL, url, len + 1);
1039 /* strip off old protocol */
1040 p = wcschr(url, ':');
1041 if (p && p[1] == '/' && p[2] == '/')
1042 p += 3;
1043 if (!p) p = url;
1044 memmove(url + (*type != '\0' ? lstrlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1046 /* add new protocol */
1047 if (*type != '\0')
1049 memcpy(url, type, (lstrlenW(type) + 1) * sizeof(WCHAR));
1050 if (wcscmp(type, wszMailto) && wcscmp(type, wszNews))
1051 memcpy(url + lstrlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1054 SetWindowTextW(hwndURL, url);
1056 HeapFree(GetProcessHeap(), 0, url);
1057 HeapFree(GetProcessHeap(), 0, type);
1058 return TRUE;
1061 return FALSE;
1062 case WM_CLOSE:
1063 EndDialog(hwnd, IDCANCEL);
1064 return TRUE;
1065 default:
1066 return FALSE;
1070 static HRESULT exec_hyperlink(HTMLDocumentNode *doc, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1072 nsAString href_str, ns_url;
1073 nsIHTMLEditor *html_editor;
1074 nsIDOMElement *anchor_elem;
1075 cpp_bool insert_link_at_caret;
1076 nsISelection *nsselection;
1077 BSTR url = NULL;
1078 INT ret;
1079 HRESULT hres = E_FAIL;
1081 static const WCHAR aW[] = {'a',0};
1082 static const WCHAR hrefW[] = {'h','r','e','f',0};
1084 TRACE("%p, 0x%x, %p, %p\n", doc, cmdexecopt, in, out);
1086 if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1088 if (!in || V_VT(in) != VT_BSTR)
1090 WARN("invalid arg\n");
1091 return E_INVALIDARG;
1093 url = V_BSTR(in);
1095 else
1097 ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1098 if (ret != IDOK)
1099 return OLECMDERR_E_CANCELED;
1102 if(!doc->nsdoc) {
1103 WARN("NULL nsdoc\n");
1104 return E_UNEXPECTED;
1107 nsselection = get_ns_selection(doc);
1108 if (!nsselection)
1109 return E_FAIL;
1111 /* create an element for the link */
1112 create_nselem(doc, aW, &anchor_elem);
1114 nsAString_InitDepend(&href_str, hrefW);
1115 nsAString_InitDepend(&ns_url, url);
1116 nsIDOMElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1117 nsAString_Finish(&href_str);
1119 nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1121 /* create an element with text of URL */
1122 if (insert_link_at_caret) {
1123 nsIDOMNode *unused_node;
1124 nsIDOMText *text_node;
1126 nsIDOMHTMLDocument_CreateTextNode(doc->nsdoc, &ns_url, &text_node);
1128 /* wrap the <a> tags around the text element */
1129 nsIDOMElement_AppendChild(anchor_elem, (nsIDOMNode*)text_node, &unused_node);
1130 nsIDOMText_Release(text_node);
1131 nsIDOMNode_Release(unused_node);
1134 nsAString_Finish(&ns_url);
1136 nsIEditor_QueryInterface(doc->browser->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1137 if (html_editor) {
1138 nsresult nsres;
1140 if (insert_link_at_caret) {
1141 /* add them to the document at the caret position */
1142 nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
1143 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1144 }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1145 nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, anchor_elem);
1147 nsIHTMLEditor_Release(html_editor);
1148 hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1151 nsISelection_Release(nsselection);
1152 nsIDOMElement_Release(anchor_elem);
1154 if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1155 SysFreeString(url);
1157 TRACE("-- 0x%08x\n", hres);
1158 return hres;
1161 const cmdtable_t editmode_cmds[] = {
1162 {IDM_DELETE, query_edit_status, exec_delete},
1163 {IDM_FONTNAME, query_edit_status, exec_fontname},
1164 {IDM_FONTSIZE, query_edit_status, exec_fontsize},
1165 {IDM_FORECOLOR, query_edit_status, exec_forecolor},
1166 {IDM_BOLD, query_edit_status, exec_bold},
1167 {IDM_ITALIC, query_edit_status, exec_italic},
1168 {IDM_JUSTIFYCENTER, query_justify, exec_justifycenter},
1169 {IDM_JUSTIFYRIGHT, query_justify, exec_justifyright},
1170 {IDM_JUSTIFYLEFT, query_justify, exec_justifyleft},
1171 {IDM_FONT, NULL, exec_font},
1172 {IDM_UNDERLINE, query_edit_status, exec_underline},
1173 {IDM_HORIZONTALLINE, query_edit_status, exec_horizontalline},
1174 {IDM_ORDERLIST, query_edit_status, exec_orderlist},
1175 {IDM_UNORDERLIST, query_edit_status, exec_unorderlist},
1176 {IDM_INDENT, query_edit_status, exec_indent},
1177 {IDM_OUTDENT, query_edit_status, exec_outdent},
1178 {IDM_COMPOSESETTINGS, NULL, exec_composesettings},
1179 {IDM_HYPERLINK, query_edit_status, exec_hyperlink},
1180 {IDM_SETDIRTY, NULL, exec_setdirty},
1181 {0,NULL,NULL}
1184 void init_editor(HTMLDocumentNode *doc)
1186 update_doc(doc->browser->doc, UPDATE_UI);
1188 set_ns_fontname(doc, "Times New Roman");
1191 HRESULT browser_is_dirty(GeckoBrowser *browser)
1193 cpp_bool modified;
1195 if(browser->usermode != EDITMODE || !browser->editor)
1196 return S_FALSE;
1198 nsIEditor_GetDocumentModified(browser->editor, &modified);
1199 return modified ? S_OK : S_FALSE;
1202 HRESULT setup_edit_mode(HTMLDocumentObj *doc)
1204 IMoniker *mon;
1205 HRESULT hres;
1207 if(doc->nscontainer->usermode == EDITMODE)
1208 return S_OK;
1210 doc->nscontainer->usermode = EDITMODE;
1212 if(doc->basedoc.window->mon) {
1213 CLSID clsid = IID_NULL;
1214 hres = IMoniker_GetClassID(doc->basedoc.window->mon, &clsid);
1215 if(SUCCEEDED(hres)) {
1216 /* We should use IMoniker::Save here */
1217 FIXME("Use CLSID %s\n", debugstr_guid(&clsid));
1221 if(doc->frame)
1222 IOleInPlaceFrame_SetStatusText(doc->frame, NULL);
1224 doc->basedoc.window->readystate = READYSTATE_UNINITIALIZED;
1226 if(doc->client) {
1227 IOleCommandTarget *cmdtrg;
1229 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1230 if(SUCCEEDED(hres)) {
1231 VARIANT var;
1233 V_VT(&var) = VT_I4;
1234 V_I4(&var) = 0;
1235 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
1237 IOleCommandTarget_Release(cmdtrg);
1241 if(doc->hostui) {
1242 DOCHOSTUIINFO hostinfo;
1244 memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
1245 hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
1246 hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
1247 if(SUCCEEDED(hres))
1248 /* FIXME: use hostinfo */
1249 TRACE("hostinfo = {%u %08x %08x %s %s}\n",
1250 hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
1251 debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
1254 update_doc(doc, UPDATE_UI);
1256 if(doc->basedoc.window->mon) {
1257 /* FIXME: We should find nicer way to do this */
1258 remove_target_tasks(doc->task_magic);
1260 mon = doc->basedoc.window->mon;
1261 IMoniker_AddRef(mon);
1262 }else {
1263 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
1265 hres = CreateURLMoniker(NULL, about_blankW, &mon);
1266 if(FAILED(hres)) {
1267 FIXME("CreateURLMoniker failed: %08x\n", hres);
1268 return hres;
1272 hres = IPersistMoniker_Load(&doc->basedoc.IPersistMoniker_iface, TRUE, mon, NULL, 0);
1273 IMoniker_Release(mon);
1274 if(FAILED(hres))
1275 return hres;
1277 if(doc->ui_active) {
1278 if(doc->ip_window)
1279 call_set_active_object(doc->ip_window, NULL);
1280 if(doc->hostui)
1281 IDocHostUIHandler_HideUI(doc->hostui);
1284 if(doc->ui_active) {
1285 RECT rcBorderWidths;
1287 if(doc->hostui)
1288 IDocHostUIHandler_ShowUI(doc->hostui, DOCHOSTUITYPE_AUTHOR,
1289 &doc->basedoc.IOleInPlaceActiveObject_iface, &doc->basedoc.IOleCommandTarget_iface,
1290 doc->frame, doc->ip_window);
1292 if(doc->ip_window)
1293 call_set_active_object(doc->ip_window, &doc->basedoc.IOleInPlaceActiveObject_iface);
1295 SetRectEmpty(&rcBorderWidths);
1296 if(doc->frame)
1297 IOleInPlaceFrame_SetBorderSpace(doc->frame, &rcBorderWidths);
1300 return S_OK;