xref: /reactos/dll/win32/mshtml/editor.c (revision 50cf16b3)
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include "mshtml_private.h"
20 
21 #define NSCMD_ALIGN        "cmd_align"
22 #define NSCMD_BEGINLINE    "cmd_beginLine"
23 #define NSCMD_BOLD         "cmd_bold"
24 #define NSCMD_CHARNEXT     "cmd_charNext"
25 #define NSCMD_CHARPREVIOUS "cmd_charPrevious"
26 #define NSCMD_COPY         "cmd_copy"
27 #define NSCMD_CUT          "cmd_cut"
28 #define NSCMD_DELETECHARFORWARD   "cmd_deleteCharForward"
29 #define NSCMD_DELETEWORDFORWARD   "cmd_deleteWordForward"
30 #define NSCMD_ENDLINE      "cmd_endLine"
31 #define NSCMD_FONTCOLOR    "cmd_fontColor"
32 #define NSCMD_FONTFACE     "cmd_fontFace"
33 #define NSCMD_INDENT       "cmd_indent"
34 #define NSCMD_INSERTHR     "cmd_insertHR"
35 #define NSCMD_INSERTLINKNOUI    "cmd_insertLinkNoUI"
36 #define NSCMD_ITALIC       "cmd_italic"
37 #define NSCMD_LINENEXT     "cmd_lineNext"
38 #define NSCMD_LINEPREVIOUS "cmd_linePrevious"
39 #define NSCMD_MOVEBOTTOM   "cmd_moveBottom"
40 #define NSCMD_MOVEPAGEDOWN "cmd_movePageDown"
41 #define NSCMD_MOVEPAGEUP   "cmd_movePageUp"
42 #define NSCMD_MOVETOP      "cmd_moveTop"
43 #define NSCMD_OL           "cmd_ol"
44 #define NSCMD_OUTDENT      "cmd_outdent"
45 #define NSCMD_PASTE        "cmd_paste"
46 #define NSCMD_SELECTBEGINLINE     "cmd_selectBeginLine"
47 #define NSCMD_SELECTBOTTOM        "cmd_selectBottom"
48 #define NSCMD_SELECTCHARNEXT      "cmd_selectCharNext"
49 #define NSCMD_SELECTCHARPREVIOUS  "cmd_selectCharPrevious"
50 #define NSCMD_SELECTENDLINE       "cmd_selectEndLine"
51 #define NSCMD_SELECTLINENEXT      "cmd_selectLineNext"
52 #define NSCMD_SELECTLINEPREVIOUS  "cmd_selectLinePrevious"
53 #define NSCMD_SELECTPAGEDOWN      "cmd_selectPageDown"
54 #define NSCMD_SELECTPAGEUP        "cmd_selectPageUp"
55 #define NSCMD_SELECTTOP           "cmd_selectTop"
56 #define NSCMD_SELECTWORDNEXT      "cmd_selectWordNext"
57 #define NSCMD_SELECTWORDPREVIOUS  "cmd_selectWordPrevious"
58 #define NSCMD_UL           "cmd_ul"
59 #define NSCMD_UNDERLINE    "cmd_underline"
60 #define NSCMD_WORDNEXT     "cmd_wordNext"
61 #define NSCMD_WORDPREVIOUS "cmd_wordPrevious"
62 
63 #define NSSTATE_ATTRIBUTE "state_attribute"
64 #define NSSTATE_ALL       "state_all"
65 
66 #define NSALIGN_CENTER "center"
67 #define NSALIGN_LEFT   "left"
68 #define NSALIGN_RIGHT  "right"
69 
70 #define DOM_VK_LEFT     VK_LEFT
71 #define DOM_VK_UP       VK_UP
72 #define DOM_VK_RIGHT    VK_RIGHT
73 #define DOM_VK_DOWN     VK_DOWN
74 #define DOM_VK_DELETE   VK_DELETE
75 #define DOM_VK_HOME     VK_HOME
76 #define DOM_VK_END      VK_END
77 
78 static const WCHAR fontW[] = {'f','o','n','t',0};
79 static const WCHAR sizeW[] = {'s','i','z','e',0};
80 
81 void set_dirty(HTMLDocument *This, VARIANT_BOOL dirty)
82 {
83     nsresult nsres;
84 
85     if(This->doc_obj->usermode != EDITMODE || !This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
86         return;
87 
88     if(dirty) {
89         nsres = nsIEditor_IncrementModificationCount(This->doc_obj->nscontainer->editor, 1);
90         if(NS_FAILED(nsres))
91             ERR("IncrementModificationCount failed: %08x\n", nsres);
92     }else {
93         nsres = nsIEditor_ResetModificationCount(This->doc_obj->nscontainer->editor);
94         if(NS_FAILED(nsres))
95             ERR("ResetModificationCount failed: %08x\n", nsres);
96     }
97 }
98 
99 static void do_ns_editor_command(NSContainer *This, const char *cmd)
100 {
101     nsresult nsres;
102 
103     if(!This->editor_controller)
104         return;
105 
106     nsres = nsIController_DoCommand(This->editor_controller, cmd);
107     if(NS_FAILED(nsres))
108         ERR("DoCommand(%s) failed: %08x\n", debugstr_a(cmd), nsres);
109 }
110 
111 static nsresult get_ns_command_state(NSContainer *This, const char *cmd, nsICommandParams *nsparam)
112 {
113     nsICommandManager *cmdmgr;
114     nsresult nsres;
115 
116     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsICommandManager, (void**)&cmdmgr);
117     if(NS_FAILED(nsres)) {
118         ERR("Could not get nsICommandManager: %08x\n", nsres);
119         return nsres;
120     }
121 
122     nsres = nsICommandManager_GetCommandState(cmdmgr, cmd, This->doc->basedoc.window->nswindow, nsparam);
123     if(NS_FAILED(nsres))
124         ERR("GetCommandState(%s) failed: %08x\n", debugstr_a(cmd), nsres);
125 
126     nsICommandManager_Release(cmdmgr);
127     return nsres;
128 }
129 
130 static DWORD query_ns_edit_status(HTMLDocument *This, const char *nscmd)
131 {
132     nsICommandParams *nsparam;
133     cpp_bool b = FALSE;
134 
135     if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
136         return OLECMDF_SUPPORTED;
137 
138     if(This->doc_obj->nscontainer && nscmd) {
139         nsparam = create_nscommand_params();
140         get_ns_command_state(This->doc_obj->nscontainer, nscmd, nsparam);
141 
142         nsICommandParams_GetBooleanValue(nsparam, NSSTATE_ALL, &b);
143 
144         nsICommandParams_Release(nsparam);
145     }
146 
147     return OLECMDF_SUPPORTED | OLECMDF_ENABLED | (b ? OLECMDF_LATCHED : 0);
148 }
149 
150 static void set_ns_align(HTMLDocument *This, const char *align_str)
151 {
152     nsICommandParams *nsparam;
153 
154     if(!This->doc_obj->nscontainer)
155         return;
156 
157     nsparam = create_nscommand_params();
158     nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, align_str);
159 
160     do_ns_command(This, NSCMD_ALIGN, nsparam);
161 
162     nsICommandParams_Release(nsparam);
163 }
164 
165 static DWORD query_align_status(HTMLDocument *This, const WCHAR *align)
166 {
167     DWORD ret = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
168     nsAString justify_str;
169     cpp_bool b;
170     nsresult nsres;
171 
172     if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
173         return OLECMDF_SUPPORTED;
174 
175     nsAString_Init(&justify_str, align);
176     nsres = nsIDOMHTMLDocument_QueryCommandState(This->doc_node->nsdoc, &justify_str, &b);
177     nsAString_Finish(&justify_str);
178     if(NS_SUCCEEDED(nsres) && b)
179         ret |= OLECMDF_LATCHED;
180 
181     return ret;
182 }
183 
184 
185 static nsISelection *get_ns_selection(HTMLDocument *This)
186 {
187     nsISelection *nsselection = NULL;
188     nsresult nsres;
189 
190     nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
191     if(NS_FAILED(nsres))
192         ERR("GetSelection failed %08x\n", nsres);
193 
194     return nsselection;
195 
196 }
197 
198 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
199 {
200     cpp_bool has_children;
201     UINT32 child_cnt, i;
202     nsIDOMNode *child_node;
203     nsIDOMNodeList *node_list;
204     UINT16 node_type;
205 
206     nsIDOMElement_HasChildNodes(elem, &has_children);
207     if(!has_children)
208         return;
209 
210     nsIDOMElement_GetChildNodes(elem, &node_list);
211     nsIDOMNodeList_GetLength(node_list, &child_cnt);
212 
213     for(i=0; i<child_cnt; i++) {
214         nsIDOMNodeList_Item(node_list, i, &child_node);
215 
216         nsIDOMNode_GetNodeType(child_node, &node_type);
217         if(node_type == ELEMENT_NODE) {
218             nsIDOMElement *child_elem;
219             nsAString tag_str;
220             const PRUnichar *ctag;
221 
222             nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
223 
224             nsAString_Init(&tag_str, NULL);
225             nsIDOMElement_GetTagName(child_elem, &tag_str);
226             nsAString_GetData(&tag_str, &ctag);
227 
228             if(!strcmpiW(ctag, tag))
229                 /* FIXME: remove node if there are no more attributes */
230                 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
231 
232             nsAString_Finish(&tag_str);
233 
234             remove_child_attr(child_elem, tag, attr_str);
235 
236             nsIDOMElement_Release(child_elem);
237         }
238 
239         nsIDOMNode_Release(child_node);
240     }
241 
242     nsIDOMNodeList_Release(node_list);
243 }
244 
245 static void get_font_size(HTMLDocument *This, WCHAR *ret)
246 {
247     nsISelection *nsselection = get_ns_selection(This);
248     nsIDOMHTMLElement *elem = NULL;
249     nsIDOMNode *node = NULL, *tmp_node;
250     nsAString tag_str;
251     LPCWSTR tag;
252     UINT16 node_type;
253     nsresult nsres;
254 
255     *ret = 0;
256 
257     if(!nsselection)
258         return;
259 
260     nsISelection_GetFocusNode(nsselection, &node);
261     nsISelection_Release(nsselection);
262 
263     while(node) {
264         nsres = nsIDOMNode_GetNodeType(node, &node_type);
265         if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
266             break;
267 
268         if(node_type == ELEMENT_NODE) {
269             nsIDOMNode_QueryInterface(node, &IID_nsIDOMHTMLElement, (void**)&elem);
270 
271             nsAString_Init(&tag_str, NULL);
272             nsIDOMHTMLElement_GetTagName(elem, &tag_str);
273             nsAString_GetData(&tag_str, &tag);
274 
275             if(!strcmpiW(tag, fontW)) {
276                 nsAString val_str;
277                 const PRUnichar *val;
278 
279                 TRACE("found font tag %p\n", elem);
280 
281                 get_elem_attr_value(elem, sizeW, &val_str, &val);
282                 if(*val) {
283                     TRACE("found size %s\n", debugstr_w(val));
284                     strcpyW(ret, val);
285                 }
286 
287                 nsAString_Finish(&val_str);
288             }
289 
290             nsAString_Finish(&tag_str);
291             nsIDOMHTMLElement_Release(elem);
292         }
293 
294         if(*ret)
295             break;
296 
297         tmp_node = node;
298         nsIDOMNode_GetParentNode(node, &node);
299         nsIDOMNode_Release(tmp_node);
300     }
301 
302     if(node)
303         nsIDOMNode_Release(node);
304 }
305 
306 static void set_font_size(HTMLDocument *This, LPCWSTR size)
307 {
308     nsISelection *nsselection;
309     cpp_bool collapsed;
310     nsIDOMHTMLElement *elem;
311     nsIDOMRange *range;
312     LONG range_cnt = 0;
313     nsAString size_str;
314     nsAString val_str;
315 
316     if(!This->doc_node->nsdoc) {
317         WARN("NULL nsdoc\n");
318         return;
319     }
320 
321     nsselection = get_ns_selection(This);
322     if(!nsselection)
323         return;
324 
325     nsISelection_GetRangeCount(nsselection, &range_cnt);
326     if(range_cnt != 1) {
327         FIXME("range_cnt %d not supprted\n", range_cnt);
328         if(!range_cnt) {
329             nsISelection_Release(nsselection);
330             return;
331         }
332     }
333 
334     create_nselem(This->doc_node, fontW, &elem);
335 
336     nsAString_InitDepend(&size_str, sizeW);
337     nsAString_InitDepend(&val_str, size);
338 
339     nsIDOMHTMLElement_SetAttribute(elem, &size_str, &val_str);
340     nsAString_Finish(&val_str);
341 
342     nsISelection_GetRangeAt(nsselection, 0, &range);
343     nsISelection_GetIsCollapsed(nsselection, &collapsed);
344     nsISelection_RemoveAllRanges(nsselection);
345 
346     nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
347 
348     if(collapsed) {
349         nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
350     }else {
351         /* Remove all size attributes from the range */
352         remove_child_attr((nsIDOMElement*)elem, fontW, &size_str);
353         nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
354     }
355 
356     nsISelection_Release(nsselection);
357     nsIDOMRange_Release(range);
358     nsIDOMHTMLElement_Release(elem);
359 
360     nsAString_Finish(&size_str);
361 
362     set_dirty(This, VARIANT_TRUE);
363 }
364 
365 static void handle_arrow_key(HTMLDocument *This, nsIDOMKeyEvent *event, const char * const cmds[4])
366 {
367     int i=0;
368     cpp_bool b;
369 
370     nsIDOMKeyEvent_GetCtrlKey(event, &b);
371     if(b)
372         i |= 1;
373 
374     nsIDOMKeyEvent_GetShiftKey(event, &b);
375     if(b)
376         i |= 2;
377 
378     if(cmds[i])
379         do_ns_editor_command(This->doc_obj->nscontainer, cmds[i]);
380 
381     nsIDOMKeyEvent_PreventDefault(event);
382 }
383 
384 void handle_edit_event(HTMLDocument *This, nsIDOMEvent *event)
385 {
386     nsIDOMKeyEvent *key_event;
387     UINT32 code;
388 
389     nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
390 
391     nsIDOMKeyEvent_GetKeyCode(key_event, &code);
392 
393     switch(code) {
394     case DOM_VK_LEFT: {
395         static const char * const cmds[] = {
396             NSCMD_CHARPREVIOUS,
397             NSCMD_WORDPREVIOUS,
398             NSCMD_SELECTCHARPREVIOUS,
399             NSCMD_SELECTWORDPREVIOUS
400         };
401 
402         TRACE("left\n");
403         handle_arrow_key(This, key_event, cmds);
404         break;
405     }
406     case DOM_VK_RIGHT: {
407         static const char * const cmds[] = {
408             NSCMD_CHARNEXT,
409             NSCMD_WORDNEXT,
410             NSCMD_SELECTCHARNEXT,
411             NSCMD_SELECTWORDNEXT
412         };
413 
414         TRACE("right\n");
415         handle_arrow_key(This, key_event, cmds);
416         break;
417     }
418     case DOM_VK_UP: {
419         static const char * const cmds[] = {
420             NSCMD_LINEPREVIOUS,
421             NSCMD_MOVEPAGEUP,
422             NSCMD_SELECTLINEPREVIOUS,
423             NSCMD_SELECTPAGEUP
424         };
425 
426         TRACE("up\n");
427         handle_arrow_key(This, key_event, cmds);
428         break;
429     }
430     case DOM_VK_DOWN: {
431         static const char * const cmds[] = {
432             NSCMD_LINENEXT,
433             NSCMD_MOVEPAGEDOWN,
434             NSCMD_SELECTLINENEXT,
435             NSCMD_SELECTPAGEDOWN
436         };
437 
438         TRACE("down\n");
439         handle_arrow_key(This, key_event, cmds);
440         break;
441     }
442     case DOM_VK_DELETE: {
443         static const char * const cmds[] = {
444             NSCMD_DELETECHARFORWARD,
445             NSCMD_DELETEWORDFORWARD,
446             NULL, NULL
447         };
448 
449         TRACE("delete\n");
450         handle_arrow_key(This, key_event, cmds);
451         break;
452     }
453     case DOM_VK_HOME: {
454         static const char * const cmds[] = {
455             NSCMD_BEGINLINE,
456             NSCMD_MOVETOP,
457             NSCMD_SELECTBEGINLINE,
458             NSCMD_SELECTTOP
459         };
460 
461         TRACE("home\n");
462         handle_arrow_key(This, key_event, cmds);
463         break;
464     }
465     case DOM_VK_END: {
466         static const char * const cmds[] = {
467             NSCMD_ENDLINE,
468             NSCMD_MOVEBOTTOM,
469             NSCMD_SELECTENDLINE,
470             NSCMD_SELECTBOTTOM
471         };
472 
473         TRACE("end\n");
474         handle_arrow_key(This, key_event, cmds);
475         break;
476     }
477     }
478 
479     nsIDOMKeyEvent_Release(key_event);
480 }
481 
482 void handle_edit_load(HTMLDocument *This)
483 {
484     get_editor_controller(This->doc_obj->nscontainer);
485 }
486 
487 static void set_ns_fontname(HTMLDocument *This, const char *fontname)
488 {
489     nsICommandParams *nsparam = create_nscommand_params();
490 
491     nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, fontname);
492     do_ns_command(This, NSCMD_FONTFACE, nsparam);
493     nsICommandParams_Release(nsparam);
494 }
495 
496 static HRESULT exec_delete(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
497 {
498     TRACE("(%p)->(%p %p)\n", This, in, out);
499 
500     if(This->doc_obj->nscontainer)
501         do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_DELETECHARFORWARD);
502 
503     update_doc(This, UPDATE_UI);
504     return S_OK;
505 }
506 
507 static HRESULT exec_fontname(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
508 {
509     TRACE("(%p)->(%p %p)\n", This, in, out);
510 
511     if(!This->doc_obj->nscontainer) {
512         update_doc(This, UPDATE_UI);
513         return E_FAIL;
514     }
515 
516     if(in) {
517         char *stra;
518 
519         if(V_VT(in) != VT_BSTR) {
520             FIXME("Unsupported fontname %s\n", debugstr_variant(out));
521             return E_INVALIDARG;
522         }
523 
524         TRACE("%s\n", debugstr_w(V_BSTR(in)));
525 
526         stra = heap_strdupWtoA(V_BSTR(in));
527         set_ns_fontname(This, stra);
528         heap_free(stra);
529 
530         update_doc(This, UPDATE_UI);
531     }
532 
533     if(out) {
534         nsICommandParams *nsparam;
535         LPWSTR strw;
536         char *stra;
537         DWORD len;
538         nsresult nsres;
539 
540         V_VT(out) = VT_BSTR;
541         V_BSTR(out) = NULL;
542 
543         nsparam = create_nscommand_params();
544 
545         nsres = get_ns_command_state(This->doc_obj->nscontainer, NSCMD_FONTFACE, nsparam);
546         if(NS_FAILED(nsres))
547             return S_OK;
548 
549         nsICommandParams_GetCStringValue(nsparam, NSSTATE_ATTRIBUTE, &stra);
550         nsICommandParams_Release(nsparam);
551 
552         len = MultiByteToWideChar(CP_ACP, 0, stra, -1, NULL, 0);
553         strw = heap_alloc(len*sizeof(WCHAR));
554         MultiByteToWideChar(CP_ACP, 0, stra, -1, strw, len);
555         nsfree(stra);
556 
557         V_BSTR(out) = SysAllocString(strw);
558         heap_free(strw);
559     }
560 
561     return S_OK;
562 }
563 
564 static HRESULT exec_forecolor(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
565 {
566     TRACE("(%p)->(%p %p)\n", This, in, out);
567 
568     if(in) {
569         if(V_VT(in) == VT_I4) {
570             nsICommandParams *nsparam = create_nscommand_params();
571             char color_str[10];
572 
573             sprintf(color_str, "#%02x%02x%02x",
574                     V_I4(in)&0xff, (V_I4(in)>>8)&0xff, (V_I4(in)>>16)&0xff);
575 
576             nsICommandParams_SetCStringValue(nsparam, NSSTATE_ATTRIBUTE, color_str);
577             do_ns_command(This, NSCMD_FONTCOLOR, nsparam);
578 
579             nsICommandParams_Release(nsparam);
580         }else {
581             FIXME("unsupported forecolor %s\n", debugstr_variant(in));
582         }
583 
584         update_doc(This, UPDATE_UI);
585     }
586 
587     if(out) {
588         FIXME("unsupported out\n");
589         return E_NOTIMPL;
590     }
591 
592     return S_OK;
593 }
594 
595 static HRESULT exec_fontsize(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
596 {
597     TRACE("(%p)->(%p %p)\n", This, in, out);
598 
599     if(out) {
600         WCHAR val[10] = {0};
601 
602         get_font_size(This, val);
603         V_VT(out) = VT_I4;
604         V_I4(out) = strtolW(val, NULL, 10);
605     }
606 
607     if(in) {
608         switch(V_VT(in)) {
609         case VT_I4: {
610             WCHAR size[10];
611             static const WCHAR format[] = {'%','d',0};
612             wsprintfW(size, format, V_I4(in));
613             set_font_size(This, size);
614             break;
615         }
616         case VT_BSTR:
617             set_font_size(This, V_BSTR(in));
618             break;
619         default:
620             FIXME("unsupported fontsize %s\n", debugstr_variant(in));
621         }
622 
623         update_doc(This, UPDATE_UI);
624     }
625 
626     return S_OK;
627 }
628 
629 static HRESULT exec_font(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
630 {
631 
632     FIXME("(%p)->(%p %p)\n", This, in, out);
633     return E_NOTIMPL;
634 }
635 
636 static HRESULT exec_bold(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
637 {
638     TRACE("(%p)\n", This);
639 
640     if(in || out)
641         FIXME("unsupported args\n");
642 
643     if(This->doc_obj->nscontainer)
644         do_ns_command(This, NSCMD_BOLD, NULL);
645 
646     update_doc(This, UPDATE_UI);
647     return S_OK;
648 }
649 
650 static HRESULT exec_italic(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
651 {
652     TRACE("(%p)\n", This);
653 
654     if(in || out)
655         FIXME("unsupported args\n");
656 
657     if(This->doc_obj->nscontainer)
658         do_ns_command(This, NSCMD_ITALIC, NULL);
659 
660     update_doc(This, UPDATE_UI);
661     return S_OK;
662 }
663 
664 static HRESULT query_justify(HTMLDocument *This, OLECMD *cmd)
665 {
666     static const PRUnichar justifycenterW[] = {'j','u','s','t','i','f','y','c','e','n','t','e','r',0};
667     static const PRUnichar justifyrightW[] = {'j','u','s','t','i','f','y','r','i','g','h','t',0};
668 
669     switch(cmd->cmdID) {
670     case IDM_JUSTIFYCENTER:
671         TRACE("(%p) IDM_JUSTIFYCENTER\n", This);
672         cmd->cmdf = query_align_status(This, justifycenterW);
673         break;
674     case IDM_JUSTIFYLEFT:
675         TRACE("(%p) IDM_JUSTIFYLEFT\n", This);
676         /* FIXME: We should set OLECMDF_LATCHED only if it's set explicitly. */
677         if(This->doc_obj->usermode != EDITMODE || This->window->readystate < READYSTATE_INTERACTIVE)
678             cmd->cmdf = OLECMDF_SUPPORTED;
679         else
680             cmd->cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
681         break;
682     case IDM_JUSTIFYRIGHT:
683         TRACE("(%p) IDM_JUSTIFYRIGHT\n", This);
684         cmd->cmdf = query_align_status(This, justifyrightW);
685         break;
686     }
687 
688     return S_OK;
689 }
690 
691 static HRESULT exec_justifycenter(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
692 {
693     TRACE("(%p)\n", This);
694 
695     if(in || out)
696         FIXME("unsupported args\n");
697 
698     set_ns_align(This, NSALIGN_CENTER);
699     update_doc(This, UPDATE_UI);
700     return S_OK;
701 }
702 
703 static HRESULT exec_justifyleft(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
704 {
705     TRACE("(%p)\n", This);
706 
707     if(in || out)
708         FIXME("unsupported args\n");
709 
710     set_ns_align(This, NSALIGN_LEFT);
711     update_doc(This, UPDATE_UI);
712     return S_OK;
713 }
714 
715 static HRESULT exec_justifyright(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
716 {
717     TRACE("(%p)\n", This);
718 
719     if(in || out)
720         FIXME("unsupported args\n");
721 
722     set_ns_align(This, NSALIGN_RIGHT);
723     update_doc(This, UPDATE_UI);
724     return S_OK;
725 }
726 
727 static HRESULT exec_underline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
728 {
729     TRACE("(%p)\n", This);
730 
731     if(in || out)
732         FIXME("unsupported args\n");
733 
734     do_ns_command(This, NSCMD_UNDERLINE, NULL);
735     update_doc(This, UPDATE_UI);
736     return S_OK;
737 }
738 
739 static HRESULT exec_horizontalline(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
740 {
741     TRACE("(%p)\n", This);
742 
743     if(in || out)
744         FIXME("unsupported args\n");
745 
746     do_ns_command(This, NSCMD_INSERTHR, NULL);
747     update_doc(This, UPDATE_UI);
748     return S_OK;
749 }
750 
751 static HRESULT exec_orderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
752 {
753     TRACE("(%p)\n", This);
754 
755     if(in || out)
756         FIXME("unsupported args\n");
757 
758     do_ns_command(This, NSCMD_OL, NULL);
759     update_doc(This, UPDATE_UI);
760     return S_OK;
761 }
762 
763 static HRESULT exec_unorderlist(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
764 {
765     TRACE("(%p)\n", This);
766 
767     if(in || out)
768         FIXME("unsupported args\n");
769 
770     do_ns_command(This, NSCMD_UL, NULL);
771     update_doc(This, UPDATE_UI);
772     return S_OK;
773 }
774 
775 static HRESULT exec_indent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
776 {
777     TRACE("(%p)\n", This);
778 
779     if(in || out)
780         FIXME("unsupported args\n");
781 
782     do_ns_command(This, NSCMD_INDENT, NULL);
783     update_doc(This, UPDATE_UI);
784     return S_OK;
785 }
786 
787 static HRESULT exec_outdent(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
788 {
789     TRACE("(%p)\n", This);
790 
791     if(in || out)
792         FIXME("unsupported args\n");
793 
794     do_ns_command(This, NSCMD_OUTDENT, NULL);
795     update_doc(This, UPDATE_UI);
796     return S_OK;
797 }
798 
799 static HRESULT exec_composesettings(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
800 {
801     WCHAR *ptr;
802 
803     if(out || !in || V_VT(in) != VT_BSTR) {
804         WARN("invalid arg %s\n", debugstr_variant(in));
805         return E_INVALIDARG;
806     }
807 
808     TRACE("(%p)->(%x %s)\n", This, cmdexecopt, debugstr_w(V_BSTR(in)));
809 
810     update_doc(This, UPDATE_UI);
811 
812     ptr = V_BSTR(in);
813     if(*ptr == '1')
814         exec_bold(This, cmdexecopt, NULL, NULL);
815     ptr = strchrW(ptr, ',');
816     if(!ptr)
817         return S_OK;
818 
819     if(*++ptr == '1')
820         exec_italic(This, cmdexecopt, NULL, NULL);
821     ptr = strchrW(ptr, ',');
822     if(!ptr)
823         return S_OK;
824 
825     if(*++ptr == '1')
826         exec_underline(This, cmdexecopt, NULL, NULL);
827     ptr = strchrW(ptr, ',');
828     if(!ptr)
829         return S_OK;
830 
831     if(isdigitW(*++ptr)) {
832         VARIANT v;
833 
834         V_VT(&v) = VT_I4;
835         V_I4(&v) = *ptr-'0';
836 
837         exec_fontsize(This, cmdexecopt, &v, NULL);
838     }
839     ptr = strchrW(ptr, ',');
840     if(!ptr)
841         return S_OK;
842 
843     if(*++ptr != ',')
844         FIXME("set font color\n");
845     ptr = strchrW(ptr, ',');
846     if(!ptr)
847         return S_OK;
848 
849     if(*++ptr != ',')
850         FIXME("set background color\n");
851     ptr = strchrW(ptr, ',');
852     if(!ptr)
853         return S_OK;
854 
855     ptr++;
856     if(*ptr) {
857         VARIANT v;
858 
859         V_VT(&v) = VT_BSTR;
860         V_BSTR(&v) = SysAllocString(ptr);
861 
862         exec_fontname(This, cmdexecopt, &v, NULL);
863 
864         SysFreeString(V_BSTR(&v));
865     }
866 
867     return S_OK;
868 }
869 
870 HRESULT editor_exec_copy(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
871 {
872     update_doc(This, UPDATE_UI);
873 
874     if(!This->doc_obj->nscontainer)
875         return E_FAIL;
876 
877     do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_COPY);
878     return S_OK;
879 }
880 
881 HRESULT editor_exec_cut(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
882 {
883     update_doc(This, UPDATE_UI);
884 
885     if(!This->doc_obj->nscontainer)
886         return E_FAIL;
887 
888     do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_CUT);
889     return S_OK;
890 }
891 
892 HRESULT editor_exec_paste(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
893 {
894     update_doc(This, UPDATE_UI);
895 
896     if(!This->doc_obj->nscontainer)
897         return E_FAIL;
898 
899     do_ns_editor_command(This->doc_obj->nscontainer, NSCMD_PASTE);
900     return S_OK;
901 }
902 
903 static HRESULT exec_setdirty(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
904 {
905     TRACE("(%p)->(%08x %p %p)\n", This, cmdexecopt, in, out);
906 
907     if(!in)
908         return S_OK;
909 
910     if(V_VT(in) == VT_BOOL)
911         set_dirty(This, V_BOOL(in));
912     else
913         FIXME("unsupported arg %s\n", debugstr_variant(in));
914 
915     return S_OK;
916 }
917 
918 static HRESULT query_edit_status(HTMLDocument *This, OLECMD *cmd)
919 {
920     switch(cmd->cmdID) {
921     case IDM_DELETE:
922         TRACE("CGID_MSHTML: IDM_DELETE\n");
923         cmd->cmdf = query_ns_edit_status(This, NULL);
924         break;
925     case IDM_FONTNAME:
926         TRACE("CGID_MSHTML: IDM_FONTNAME\n");
927         cmd->cmdf = query_ns_edit_status(This, NULL);
928         break;
929     case IDM_FONTSIZE:
930         TRACE("CGID_MSHTML: IDM_FONTSIZE\n");
931         cmd->cmdf = query_ns_edit_status(This, NULL);
932         break;
933     case IDM_BOLD:
934         TRACE("CGID_MSHTML: IDM_BOLD\n");
935         cmd->cmdf = query_ns_edit_status(This, NSCMD_BOLD);
936         break;
937     case IDM_FORECOLOR:
938         TRACE("CGID_MSHTML: IDM_FORECOLOR\n");
939         cmd->cmdf = query_ns_edit_status(This, NULL);
940         break;
941     case IDM_ITALIC:
942         TRACE("CGID_MSHTML: IDM_ITALIC\n");
943         cmd->cmdf = query_ns_edit_status(This, NSCMD_ITALIC);
944         break;
945     case IDM_UNDERLINE:
946         TRACE("CGID_MSHTML: IDM_UNDERLINE\n");
947         cmd->cmdf = query_ns_edit_status(This, NSCMD_UNDERLINE);
948         break;
949     case IDM_HORIZONTALLINE:
950         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
951         cmd->cmdf = query_ns_edit_status(This, NULL);
952         break;
953     case IDM_ORDERLIST:
954         TRACE("CGID_MSHTML: IDM_ORDERLIST\n");
955         cmd->cmdf = query_ns_edit_status(This, NSCMD_OL);
956         break;
957     case IDM_UNORDERLIST:
958         TRACE("CGID_MSHTML: IDM_HORIZONTALLINE\n");
959         cmd->cmdf = query_ns_edit_status(This, NSCMD_UL);
960         break;
961     case IDM_INDENT:
962         TRACE("CGID_MSHTML: IDM_INDENT\n");
963         cmd->cmdf = query_ns_edit_status(This, NULL);
964         break;
965     case IDM_OUTDENT:
966         TRACE("CGID_MSHTML: IDM_OUTDENT\n");
967         cmd->cmdf = query_ns_edit_status(This, NULL);
968         break;
969     case IDM_HYPERLINK:
970         TRACE("CGID_MSHTML: IDM_HYPERLINK\n");
971         cmd->cmdf = query_ns_edit_status(This, NULL);
972         break;
973     }
974 
975     return S_OK;
976 }
977 
978 static INT_PTR CALLBACK hyperlink_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
979 {
980     static const WCHAR wszOther[] = {'(','o','t','h','e','r',')',0};
981     static const WCHAR wszFile[] = {'f','i','l','e',':',0};
982     static const WCHAR wszFtp[] = {'f','t','p',':',0};
983     static const WCHAR wszHttp[] = {'h','t','t','p',':',0};
984     static const WCHAR wszHttps[] = {'h','t','t','p','s',':',0};
985     static const WCHAR wszMailto[] = {'m','a','i','l','t','o',':',0};
986     static const WCHAR wszNews[] = {'n','e','w','s',':',0};
987 
988     switch (msg)
989     {
990         case WM_INITDIALOG:
991         {
992             INT def_idx;
993             HWND hwndCB = GetDlgItem(hwnd, IDC_TYPE);
994             HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
995             INT len;
996 
997             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
998 
999             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszOther);
1000             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFile);
1001             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszFtp);
1002             def_idx = SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttp);
1003             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszHttps);
1004             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszMailto);
1005             SendMessageW(hwndCB, CB_INSERTSTRING, -1, (LPARAM)wszNews);
1006             SendMessageW(hwndCB, CB_SETCURSEL, def_idx, 0);
1007 
1008             /* force the updating of the URL edit box */
1009             SendMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE), (LPARAM)hwndCB);
1010 
1011             SetFocus(hwndURL);
1012             len = SendMessageW(hwndURL, WM_GETTEXTLENGTH, 0, 0);
1013             SendMessageW(hwndURL, EM_SETSEL, 0, len);
1014 
1015             return FALSE;
1016         }
1017         case WM_COMMAND:
1018             switch (wparam)
1019             {
1020                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
1021                     EndDialog(hwnd, wparam);
1022                     return TRUE;
1023                 case MAKEWPARAM(IDOK, BN_CLICKED):
1024                 {
1025                     BSTR *url = (BSTR *)GetWindowLongPtrW(hwnd, DWLP_USER);
1026                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1027                     INT len = GetWindowTextLengthW(hwndURL);
1028                     *url = SysAllocStringLen(NULL, len + 1);
1029                     GetWindowTextW(hwndURL, *url, len + 1);
1030                     EndDialog(hwnd, wparam);
1031                     return TRUE;
1032                 }
1033                 case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
1034                 {
1035                     HWND hwndURL = GetDlgItem(hwnd, IDC_URL);
1036                     INT item;
1037                     INT len;
1038                     LPWSTR type;
1039                     LPWSTR url;
1040                     LPWSTR p;
1041                     static const WCHAR wszSlashSlash[] = {'/','/'};
1042 
1043                     /* get string of currently selected hyperlink type */
1044                     item = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
1045                     len = SendMessageW((HWND)lparam, CB_GETLBTEXTLEN, item, 0);
1046                     type = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1047                     SendMessageW((HWND)lparam, CB_GETLBTEXT, item, (LPARAM)type);
1048 
1049                     if (!strcmpW(type, wszOther))
1050                         *type = '\0';
1051 
1052                     /* get current URL */
1053                     len = GetWindowTextLengthW(hwndURL);
1054                     url = HeapAlloc(GetProcessHeap(), 0, (len + strlenW(type) + 3) * sizeof(WCHAR));
1055                     GetWindowTextW(hwndURL, url, len + 1);
1056 
1057                     /* strip off old protocol */
1058                     p = strchrW(url, ':');
1059                     if (p && p[1] == '/' && p[2] == '/')
1060                         p += 3;
1061                     if (!p) p = url;
1062                     memmove(url + (*type != '\0' ? strlenW(type) + 2 : 0), p, (len + 1 - (p - url)) * sizeof(WCHAR));
1063 
1064                     /* add new protocol */
1065                     if (*type != '\0')
1066                     {
1067                         memcpy(url, type, (strlenW(type) + 1) * sizeof(WCHAR));
1068                         if (strcmpW(type, wszMailto) && strcmpW(type, wszNews))
1069                             memcpy(url + strlenW(type), wszSlashSlash, sizeof(wszSlashSlash));
1070                     }
1071 
1072                     SetWindowTextW(hwndURL, url);
1073 
1074                     HeapFree(GetProcessHeap(), 0, url);
1075                     HeapFree(GetProcessHeap(), 0, type);
1076                     return TRUE;
1077                 }
1078             }
1079             return FALSE;
1080         case WM_CLOSE:
1081             EndDialog(hwnd, IDCANCEL);
1082             return TRUE;
1083         default:
1084             return FALSE;
1085     }
1086 }
1087 
1088 static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in, VARIANT *out)
1089 {
1090     nsAString href_str, ns_url;
1091     nsIHTMLEditor *html_editor;
1092     nsIDOMHTMLElement *anchor_elem;
1093     cpp_bool insert_link_at_caret;
1094     nsISelection *nsselection;
1095     BSTR url = NULL;
1096     INT ret;
1097     HRESULT hres = E_FAIL;
1098 
1099     static const WCHAR aW[] = {'a',0};
1100     static const WCHAR hrefW[] = {'h','r','e','f',0};
1101 
1102     TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
1103 
1104     if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
1105     {
1106         if (!in || V_VT(in) != VT_BSTR)
1107         {
1108             WARN("invalid arg\n");
1109             return E_INVALIDARG;
1110         }
1111         url = V_BSTR(in);
1112     }
1113     else
1114     {
1115         ret = DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_HYPERLINK), NULL /* FIXME */, hyperlink_dlgproc, (LPARAM)&url);
1116         if (ret != IDOK)
1117             return OLECMDERR_E_CANCELED;
1118     }
1119 
1120     if(!This->doc_node->nsdoc) {
1121         WARN("NULL nsdoc\n");
1122         return E_UNEXPECTED;
1123     }
1124 
1125     nsselection = get_ns_selection(This);
1126     if (!nsselection)
1127         return E_FAIL;
1128 
1129     /* create an element for the link */
1130     create_nselem(This->doc_node, aW, &anchor_elem);
1131 
1132     nsAString_InitDepend(&href_str, hrefW);
1133     nsAString_InitDepend(&ns_url, url);
1134     nsIDOMHTMLElement_SetAttribute(anchor_elem, &href_str, &ns_url);
1135     nsAString_Finish(&href_str);
1136 
1137     nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
1138 
1139     /* create an element with text of URL */
1140     if (insert_link_at_caret) {
1141         nsIDOMNode *unused_node;
1142         nsIDOMText *text_node;
1143 
1144         nsIDOMHTMLDocument_CreateTextNode(This->doc_node->nsdoc, &ns_url, &text_node);
1145 
1146         /* wrap the <a> tags around the text element */
1147         nsIDOMHTMLElement_AppendChild(anchor_elem, (nsIDOMNode*)text_node, &unused_node);
1148         nsIDOMText_Release(text_node);
1149         nsIDOMNode_Release(unused_node);
1150     }
1151 
1152     nsAString_Finish(&ns_url);
1153 
1154     nsIEditor_QueryInterface(This->doc_obj->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
1155     if (html_editor) {
1156         nsresult nsres;
1157 
1158         if (insert_link_at_caret) {
1159             /* add them to the document at the caret position */
1160             nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, (nsIDOMElement*)anchor_elem, FALSE);
1161             nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
1162         }else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
1163             nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, (nsIDOMElement*)anchor_elem);
1164 
1165         nsIHTMLEditor_Release(html_editor);
1166         hres = NS_SUCCEEDED(nsres) ? S_OK : E_FAIL;
1167     }
1168 
1169     nsISelection_Release(nsselection);
1170     nsIDOMHTMLElement_Release(anchor_elem);
1171 
1172     if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
1173         SysFreeString(url);
1174 
1175     TRACE("-- 0x%08x\n", hres);
1176     return hres;
1177 }
1178 
1179 const cmdtable_t editmode_cmds[] = {
1180     {IDM_DELETE,          query_edit_status,    exec_delete},
1181     {IDM_FONTNAME,        query_edit_status,    exec_fontname},
1182     {IDM_FONTSIZE,        query_edit_status,    exec_fontsize},
1183     {IDM_FORECOLOR,       query_edit_status,    exec_forecolor},
1184     {IDM_BOLD,            query_edit_status,    exec_bold},
1185     {IDM_ITALIC,          query_edit_status,    exec_italic},
1186     {IDM_JUSTIFYCENTER,   query_justify,        exec_justifycenter},
1187     {IDM_JUSTIFYRIGHT,    query_justify,        exec_justifyright},
1188     {IDM_JUSTIFYLEFT,     query_justify,        exec_justifyleft},
1189     {IDM_FONT,            NULL,                 exec_font},
1190     {IDM_UNDERLINE,       query_edit_status,    exec_underline},
1191     {IDM_HORIZONTALLINE,  query_edit_status,    exec_horizontalline},
1192     {IDM_ORDERLIST,       query_edit_status,    exec_orderlist},
1193     {IDM_UNORDERLIST,     query_edit_status,    exec_unorderlist},
1194     {IDM_INDENT,          query_edit_status,    exec_indent},
1195     {IDM_OUTDENT,         query_edit_status,    exec_outdent},
1196     {IDM_COMPOSESETTINGS, NULL,                 exec_composesettings},
1197     {IDM_HYPERLINK,       query_edit_status,    exec_hyperlink},
1198     {IDM_SETDIRTY,        NULL,                 exec_setdirty},
1199     {0,NULL,NULL}
1200 };
1201 
1202 void init_editor(HTMLDocument *This)
1203 {
1204     update_doc(This, UPDATE_UI);
1205 
1206     set_ns_fontname(This, "Times New Roman");
1207 }
1208 
1209 HRESULT editor_is_dirty(HTMLDocument *This)
1210 {
1211     cpp_bool modified;
1212 
1213     if(!This->doc_obj->nscontainer || !This->doc_obj->nscontainer->editor)
1214         return S_FALSE;
1215 
1216     nsIEditor_GetDocumentModified(This->doc_obj->nscontainer->editor, &modified);
1217 
1218     return modified ? S_OK : S_FALSE;
1219 }
1220 
1221 HRESULT setup_edit_mode(HTMLDocumentObj *doc)
1222 {
1223     IMoniker *mon;
1224     HRESULT hres;
1225 
1226     if(doc->usermode == EDITMODE)
1227         return S_OK;
1228 
1229     doc->usermode = EDITMODE;
1230 
1231     if(doc->basedoc.window->mon) {
1232         CLSID clsid = IID_NULL;
1233         hres = IMoniker_GetClassID(doc->basedoc.window->mon, &clsid);
1234         if(SUCCEEDED(hres)) {
1235             /* We should use IMoniker::Save here */
1236             FIXME("Use CLSID %s\n", debugstr_guid(&clsid));
1237         }
1238     }
1239 
1240     if(doc->frame)
1241         IOleInPlaceFrame_SetStatusText(doc->frame, NULL);
1242 
1243     doc->basedoc.window->readystate = READYSTATE_UNINITIALIZED;
1244 
1245     if(doc->client) {
1246         IOleCommandTarget *cmdtrg;
1247 
1248         hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1249         if(SUCCEEDED(hres)) {
1250             VARIANT var;
1251 
1252             V_VT(&var) = VT_I4;
1253             V_I4(&var) = 0;
1254             IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
1255 
1256             IOleCommandTarget_Release(cmdtrg);
1257         }
1258     }
1259 
1260     if(doc->hostui) {
1261         DOCHOSTUIINFO hostinfo;
1262 
1263         memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
1264         hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
1265         hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
1266         if(SUCCEEDED(hres))
1267             /* FIXME: use hostinfo */
1268             TRACE("hostinfo = {%u %08x %08x %s %s}\n",
1269                     hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
1270                     debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
1271     }
1272 
1273     update_doc(&doc->basedoc, UPDATE_UI);
1274 
1275     if(doc->basedoc.window->mon) {
1276         /* FIXME: We should find nicer way to do this */
1277         remove_target_tasks(doc->basedoc.task_magic);
1278 
1279         mon = doc->basedoc.window->mon;
1280         IMoniker_AddRef(mon);
1281     }else {
1282         static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
1283 
1284         hres = CreateURLMoniker(NULL, about_blankW, &mon);
1285         if(FAILED(hres)) {
1286             FIXME("CreateURLMoniker failed: %08x\n", hres);
1287             return hres;
1288         }
1289     }
1290 
1291     hres = IPersistMoniker_Load(&doc->basedoc.IPersistMoniker_iface, TRUE, mon, NULL, 0);
1292     IMoniker_Release(mon);
1293     if(FAILED(hres))
1294         return hres;
1295 
1296     if(doc->ui_active) {
1297         if(doc->ip_window)
1298             call_set_active_object(doc->ip_window, NULL);
1299         if(doc->hostui)
1300             IDocHostUIHandler_HideUI(doc->hostui);
1301     }
1302 
1303     if(doc->ui_active) {
1304         RECT rcBorderWidths;
1305 
1306         if(doc->hostui)
1307             IDocHostUIHandler_ShowUI(doc->hostui, DOCHOSTUITYPE_AUTHOR,
1308                     &doc->basedoc.IOleInPlaceActiveObject_iface, &doc->basedoc.IOleCommandTarget_iface,
1309                     doc->frame, doc->ip_window);
1310 
1311         if(doc->ip_window)
1312             call_set_active_object(doc->ip_window, &doc->basedoc.IOleInPlaceActiveObject_iface);
1313 
1314         memset(&rcBorderWidths, 0, sizeof(rcBorderWidths));
1315         if(doc->frame)
1316             IOleInPlaceFrame_SetBorderSpace(doc->frame, &rcBorderWidths);
1317     }
1318 
1319     return S_OK;
1320 }
1321