mshtml: Added Exec(IDM_FONTSIZE) implementation.
[wine/multimedia.git] / dlls / mshtml / editor.c
blob64c5daf5c1c62e24b7007f68aa1241cf872decae
1 /*
2 * Copyright 2006 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 "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "mshtml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 static const WCHAR wszFont[] = {'f','o','n','t',0};
40 static const WCHAR wszSize[] = {'s','i','z','e',0};
42 static nsISelection *get_ns_selection(HTMLDocument *This)
44 nsIDOMWindow *dom_window;
45 nsISelection *nsselection = NULL;
46 nsresult nsres;
48 if(!This->nscontainer)
49 return NULL;
51 nsres = nsIWebBrowser_GetContentDOMWindow(This->nscontainer->webbrowser, &dom_window);
52 if(NS_FAILED(nsres))
53 return NULL;
55 nsIDOMWindow_GetSelection(dom_window, &nsselection);
56 nsIDOMWindow_Release(dom_window);
58 return nsselection;
62 static void remove_child_attr(nsIDOMElement *elem, LPCWSTR tag, nsAString *attr_str)
64 PRBool has_children;
65 PRUint32 child_cnt, i;
66 nsIDOMNode *child_node;
67 nsIDOMNodeList *node_list;
68 PRUint16 node_type;
70 nsIDOMElement_HasChildNodes(elem, &has_children);
71 if(!has_children)
72 return;
74 nsIDOMElement_GetChildNodes(elem, &node_list);
75 nsIDOMNodeList_GetLength(node_list, &child_cnt);
77 for(i=0; i<child_cnt; i++) {
78 nsIDOMNodeList_Item(node_list, i, &child_node);
80 nsIDOMNode_GetNodeType(child_node, &node_type);
81 if(node_type == ELEMENT_NODE) {
82 nsIDOMElement *child_elem;
83 nsAString tag_str;
84 const PRUnichar *ctag;
86 nsIDOMNode_QueryInterface(child_node, &IID_nsIDOMElement, (void**)&child_elem);
88 nsAString_Init(&tag_str, NULL);
89 nsIDOMElement_GetTagName(child_elem, &tag_str);
90 nsAString_GetData(&tag_str, &ctag, NULL);
92 if(!strcmpiW(ctag, tag))
93 /* FIXME: remove node if there are no more attributes */
94 nsIDOMElement_RemoveAttribute(child_elem, attr_str);
96 nsAString_Finish(&tag_str);
98 remove_child_attr(child_elem, tag, attr_str);
100 nsIDOMNode_Release(child_elem);
103 nsIDOMNode_Release(child_node);
106 nsIDOMNodeList_Release(node_list);
109 void get_font_size(HTMLDocument *This, WCHAR *ret)
111 nsISelection *nsselection = get_ns_selection(This);
112 nsIDOMElement *elem = NULL;
113 nsIDOMNode *node = NULL, *tmp_node;
114 nsAString tag_str;
115 LPCWSTR tag;
116 PRUint16 node_type;
117 nsresult nsres;
119 *ret = 0;
121 if(!nsselection)
122 return;
124 nsISelection_GetFocusNode(nsselection, &node);
125 nsISelection_Release(nsselection);
127 while(node) {
128 nsres = nsIDOMNode_GetNodeType(node, &node_type);
129 if(NS_FAILED(nsres) || node_type == DOCUMENT_NODE)
130 break;
132 if(node_type == ELEMENT_NODE) {
133 nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
135 nsAString_Init(&tag_str, NULL);
136 nsIDOMElement_GetTagName(elem, &tag_str);
137 nsAString_GetData(&tag_str, &tag, NULL);
139 if(!strcmpiW(tag, wszFont)) {
140 nsAString size_str, val_str;
141 LPCWSTR val;
143 TRACE("found font tag %p", elem);
145 nsAString_Init(&size_str, wszSize);
146 nsAString_Init(&val_str, NULL);
148 nsIDOMElement_GetAttribute(elem, &size_str, &val_str);
149 nsAString_GetData(&val_str, &val, NULL);
151 if(*val) {
152 TRACE("found size %s\n", debugstr_w(val));
153 strcpyW(ret, val);
156 nsAString_Finish(&size_str);
157 nsAString_Finish(&val_str);
160 nsAString_Finish(&tag_str);
162 nsIDOMElement_Release(elem);
165 if(*ret)
166 break;
168 tmp_node = node;
169 nsIDOMNode_GetParentNode(node, &node);
170 nsIDOMNode_Release(tmp_node);
173 if(node)
174 nsIDOMNode_Release(node);
177 void set_font_size(HTMLDocument *This, LPCWSTR size)
179 nsISelection *nsselection;
180 PRBool collapsed;
181 nsIDOMDocument *nsdoc;
182 nsIDOMElement *elem;
183 nsIDOMRange *range;
184 PRInt32 range_cnt = 0;
185 nsAString font_str;
186 nsAString size_str;
187 nsAString val_str;
188 nsresult nsres;
190 nsselection = get_ns_selection(This);
192 if(!nsselection)
193 return;
195 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
196 if(NS_FAILED(nsres))
197 return;
199 nsAString_Init(&font_str, wszFont);
200 nsAString_Init(&size_str, wszSize);
201 nsAString_Init(&val_str, size);
203 nsISelection_GetRangeCount(nsselection, &range_cnt);
204 if(range_cnt != 1)
205 FIXME("range_cnt %d not supprted\n", range_cnt);
207 nsIDOMDocument_CreateElement(nsdoc, &font_str, &elem);
208 nsIDOMElement_SetAttribute(elem, &size_str, &val_str);
210 nsISelection_GetRangeAt(nsselection, 0, &range);
211 nsISelection_GetIsCollapsed(nsselection, &collapsed);
212 nsISelection_RemoveAllRanges(nsselection);
214 nsIDOMRange_SurroundContents(range, (nsIDOMNode*)elem);
216 if(collapsed) {
217 nsISelection_Collapse(nsselection, (nsIDOMNode*)elem, 0);
218 }else {
219 /* Remove all size attrbutes from the range */
220 remove_child_attr(elem, wszFont, &size_str);
221 nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)elem);
224 nsIDOMRange_Release(range);
225 nsIDOMElement_Release(elem);
227 nsAString_Finish(&font_str);
228 nsAString_Finish(&size_str);
229 nsAString_Finish(&val_str);
231 nsISelection_Release(nsselection);
232 nsIDOMDocument_Release(nsdoc);