2 * Copyright (C) 2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2006 Jon Shier (jshier@iastate.edu)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reseved.
5 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 #include "WindowFeatures.h"
26 #include "FloatRect.h"
27 #include "PlatformString.h"
28 #include <wtf/Assertions.h>
29 #include <wtf/MathExtras.h>
30 #include <wtf/text/StringHash.h>
34 // Though isspace() considers \t and \v to be whitespace, Win IE doesn't when parsing window features.
35 static bool isWindowFeaturesSeparator(UChar c)
37 return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '=' || c == ',' || c == '\0';
40 WindowFeatures::WindowFeatures(const String& features)
49 The IE rule is: all features except for channelmode and fullscreen default to YES, but
50 if the user specifies a feature string, all features default to NO. (There is no public
51 standard that applies to this method.)
53 <http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/open_0.asp>
54 We always allow a window to be resized, which is consistent with Firefox.
57 if (features.length() == 0) {
58 menuBarVisible = true;
59 statusBarVisible = true;
60 toolBarVisible = true;
61 locationBarVisible = true;
62 scrollbarsVisible = true;
67 menuBarVisible = false;
68 statusBarVisible = false;
69 toolBarVisible = false;
70 locationBarVisible = false;
71 scrollbarsVisible = false;
74 // Tread lightly in this code -- it was specifically designed to mimic Win IE's parsing behavior.
76 int valueBegin, valueEnd;
79 int length = features.length();
80 String buffer = features.lower();
82 // skip to first non-separator, but don't skip past the end of the string
83 while (isWindowFeaturesSeparator(buffer[i])) {
90 // skip to first separator
91 while (!isWindowFeaturesSeparator(buffer[i]))
95 // skip to first '=', but don't skip past a ',' or the end of the string
96 while (buffer[i] != '=') {
97 if (buffer[i] == ',' || i >= length)
102 // skip to first non-separator, but don't skip past a ',' or the end of the string
103 while (isWindowFeaturesSeparator(buffer[i])) {
104 if (buffer[i] == ',' || i >= length)
110 // skip to first separator
111 while (!isWindowFeaturesSeparator(buffer[i]))
117 String keyString(buffer.substring(keyBegin, keyEnd - keyBegin));
118 String valueString(buffer.substring(valueBegin, valueEnd - valueBegin));
119 setWindowFeature(keyString, valueString);
123 void WindowFeatures::setWindowFeature(const String& keyString, const String& valueString)
127 // Listing a key with no value is shorthand for key=yes
128 if (valueString.isEmpty() || valueString == "yes")
131 value = valueString.toInt();
133 // We treat keyString of "resizable" here as an additional feature rather than setting resizeable to true.
134 // This is consistent with Firefox, but could also be handled at another level.
136 if (keyString == "left" || keyString == "screenx") {
139 } else if (keyString == "top" || keyString == "screeny") {
142 } else if (keyString == "width" || keyString == "innerwidth") {
145 } else if (keyString == "height" || keyString == "innerheight") {
148 } else if (keyString == "menubar")
149 menuBarVisible = value;
150 else if (keyString == "toolbar")
151 toolBarVisible = value;
152 else if (keyString == "location")
153 locationBarVisible = value;
154 else if (keyString == "status")
155 statusBarVisible = value;
156 else if (keyString == "fullscreen")
158 else if (keyString == "scrollbars")
159 scrollbarsVisible = value;
161 additionalFeatures.append(keyString);
164 WindowFeatures::WindowFeatures(const String& dialogFeaturesString, const FloatRect& screenAvailableRect)
167 , menuBarVisible(false)
168 , toolBarVisible(false)
169 , locationBarVisible(false)
173 DialogFeaturesMap features;
174 parseDialogFeatures(dialogFeaturesString, features);
176 const bool trusted = false;
178 // The following features from Microsoft's documentation are not implemented:
179 // - default font settings
180 // - width, height, left, and top specified in units other than "px"
181 // - edge (sunken or raised, default is raised)
182 // - dialogHide: trusted && boolFeature(features, "dialoghide"), makes dialog hide when you print
183 // - help: boolFeature(features, "help", true), makes help icon appear in dialog (what does it do on Windows?)
184 // - unadorned: trusted && boolFeature(features, "unadorned");
186 width = floatFeature(features, "dialogwidth", 100, screenAvailableRect.width(), 620); // default here came from frame size of dialog in MacIE
187 height = floatFeature(features, "dialogheight", 100, screenAvailableRect.height(), 450); // default here came from frame size of dialog in MacIE
189 x = floatFeature(features, "dialogleft", screenAvailableRect.x(), screenAvailableRect.maxX() - width, -1);
191 y = floatFeature(features, "dialogtop", screenAvailableRect.y(), screenAvailableRect.maxY() - height, -1);
194 if (boolFeature(features, "center", true)) {
196 x = screenAvailableRect.x() + (screenAvailableRect.width() - width) / 2;
200 y = screenAvailableRect.y() + (screenAvailableRect.height() - height) / 2;
205 resizable = boolFeature(features, "resizable");
206 scrollbarsVisible = boolFeature(features, "scroll", true);
207 statusBarVisible = boolFeature(features, "status", !trusted);
210 bool WindowFeatures::boolFeature(const DialogFeaturesMap& features, const char* key, bool defaultValue)
212 DialogFeaturesMap::const_iterator it = features.find(key);
213 if (it == features.end())
215 const String& value = it->second;
216 return value.isNull() || value == "1" || value == "yes" || value == "on";
219 float WindowFeatures::floatFeature(const DialogFeaturesMap& features, const char* key, float min, float max, float defaultValue)
221 DialogFeaturesMap::const_iterator it = features.find(key);
222 if (it == features.end())
224 // FIXME: The toDouble function does not offer a way to tell "0q" from string with no digits in it: Both
225 // return the number 0 and false for ok. But "0q" should yield the minimum rather than the default.
227 double parsedNumber = it->second.toDouble(&ok);
228 if ((parsedNumber == 0 && !ok) || isnan(parsedNumber))
230 if (parsedNumber < min || max <= min)
232 if (parsedNumber > max)
234 // FIXME: Seems strange to cast a double to int and then convert back to a float. Why is this a good idea?
235 return static_cast<int>(parsedNumber);
238 void WindowFeatures::parseDialogFeatures(const String& string, DialogFeaturesMap& map)
240 Vector<String> vector;
241 string.split(';', vector);
242 size_t size = vector.size();
243 for (size_t i = 0; i < size; ++i) {
244 const String& featureString = vector[i];
246 size_t separatorPosition = featureString.find('=');
247 size_t colonPosition = featureString.find(':');
248 if (separatorPosition != notFound && colonPosition != notFound)
249 continue; // ignore strings that have both = and :
250 if (separatorPosition == notFound)
251 separatorPosition = colonPosition;
253 String key = featureString.left(separatorPosition).stripWhiteSpace().lower();
255 // Null string for value indicates key without value.
257 if (separatorPosition != notFound) {
258 value = featureString.substring(separatorPosition + 1).stripWhiteSpace().lower();
259 value = value.left(value.find(' '));
266 } // namespace WebCore