Bug 1073336 part 5 - Add AnimationPlayerCollection::PlayerUpdated; r=dbaron
[gecko.git] / widget / qt / nsLookAndFeel.cpp
blob1a8b24e52991d99e997a7b80c8e3641e9ab0f067
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* Copyright 2012 Mozilla Foundation and Mozilla contributors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <QGuiApplication>
18 #include <QFont>
19 #include <QScreen>
20 #include <QPalette>
21 #include <QStyle>
22 #include <QStyleFactory>
24 #include "nsLookAndFeel.h"
25 #include "nsStyleConsts.h"
26 #include "gfxFont.h"
27 #include "gfxFontConstants.h"
28 #include "mozilla/gfx/2D.h"
30 static const char16_t UNICODE_BULLET = 0x2022;
32 #define QCOLOR_TO_NS_RGB(c) \
33 ((nscolor)NS_RGB(c.red(),c.green(),c.blue()))
35 nsLookAndFeel::nsLookAndFeel()
36 : nsXPLookAndFeel()
40 nsLookAndFeel::~nsLookAndFeel()
44 nsresult
45 nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor)
47 nsresult rv = NS_OK;
49 #define BG_PRELIGHT_COLOR NS_RGB(0xee,0xee,0xee)
50 #define FG_PRELIGHT_COLOR NS_RGB(0x77,0x77,0x77)
51 #define RED_COLOR NS_RGB(0xff,0x00,0x00)
53 QPalette palette = QGuiApplication::palette();
55 switch (aID) {
56 // These colors don't seem to be used for anything anymore in Mozilla
57 // (except here at least TextSelectBackground and TextSelectForeground)
58 // The CSS2 colors below are used.
59 case eColorID_WindowBackground:
60 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
61 break;
62 case eColorID_WindowForeground:
63 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::WindowText));
64 break;
65 case eColorID_WidgetBackground:
66 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
67 break;
68 case eColorID_WidgetForeground:
69 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::WindowText));
70 break;
71 case eColorID_WidgetSelectBackground:
72 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
73 break;
74 case eColorID_WidgetSelectForeground:
75 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::WindowText));
76 break;
77 case eColorID_Widget3DHighlight:
78 aColor = NS_RGB(0xa0,0xa0,0xa0);
79 break;
80 case eColorID_Widget3DShadow:
81 aColor = NS_RGB(0x40,0x40,0x40);
82 break;
83 case eColorID_TextBackground:
84 // not used?
85 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
86 break;
87 case eColorID_TextForeground:
88 // not used?
89 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::WindowText));
90 break;
91 case eColorID_TextSelectBackground:
92 case eColorID_IMESelectedRawTextBackground:
93 case eColorID_IMESelectedConvertedTextBackground:
94 // still used
95 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Highlight));
96 break;
97 case eColorID_TextSelectForeground:
98 case eColorID_IMESelectedRawTextForeground:
99 case eColorID_IMESelectedConvertedTextForeground:
100 // still used
101 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::HighlightedText));
102 break;
103 case eColorID_IMERawInputBackground:
104 case eColorID_IMEConvertedTextBackground:
105 aColor = NS_TRANSPARENT;
106 break;
107 case eColorID_IMERawInputForeground:
108 case eColorID_IMEConvertedTextForeground:
109 aColor = NS_SAME_AS_FOREGROUND_COLOR;
110 break;
111 case eColorID_IMERawInputUnderline:
112 case eColorID_IMEConvertedTextUnderline:
113 aColor = NS_SAME_AS_FOREGROUND_COLOR;
114 break;
115 case eColorID_IMESelectedRawTextUnderline:
116 case eColorID_IMESelectedConvertedTextUnderline:
117 aColor = NS_TRANSPARENT;
118 break;
119 case eColorID_SpellCheckerUnderline:
120 aColor = RED_COLOR;
121 break;
123 // css2 http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
124 case eColorID_activeborder:
125 // active window border
126 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
127 break;
128 case eColorID_activecaption:
129 // active window caption background
130 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
131 break;
132 case eColorID_appworkspace:
133 // MDI background color
134 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
135 break;
136 case eColorID_background:
137 // desktop background
138 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
139 break;
140 case eColorID_captiontext:
141 // text in active window caption, size box, and scrollbar arrow box (!)
142 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
143 break;
144 case eColorID_graytext:
145 // disabled text in windows, menus, etc.
146 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Disabled, QPalette::Text));
147 break;
148 case eColorID_highlight:
149 // background of selected item
150 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Highlight));
151 break;
152 case eColorID_highlighttext:
153 // text of selected item
154 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::HighlightedText));
155 break;
156 case eColorID_inactiveborder:
157 // inactive window border
158 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Disabled, QPalette::Window));
159 break;
160 case eColorID_inactivecaption:
161 // inactive window caption
162 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Disabled, QPalette::Window));
163 break;
164 case eColorID_inactivecaptiontext:
165 // text in inactive window caption
166 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Disabled, QPalette::Text));
167 break;
168 case eColorID_infobackground:
169 // tooltip background color
170 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::ToolTipBase));
171 break;
172 case eColorID_infotext:
173 // tooltip text color
174 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::ToolTipText));
175 break;
176 case eColorID_menu:
177 // menu background
178 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
179 break;
180 case eColorID_menutext:
181 // menu text
182 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
183 break;
184 case eColorID_scrollbar:
185 // scrollbar gray area
186 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Mid));
187 break;
189 case eColorID_threedface:
190 case eColorID_buttonface:
191 // 3-D face color
192 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Button));
193 break;
195 case eColorID_buttontext:
196 // text on push buttons
197 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::ButtonText));
198 break;
200 case eColorID_buttonhighlight:
201 // 3-D highlighted edge color
202 case eColorID_threedhighlight:
203 // 3-D highlighted outer edge color
204 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Dark));
205 break;
207 case eColorID_threedlightshadow:
208 // 3-D highlighted inner edge color
209 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Light));
210 break;
212 case eColorID_buttonshadow:
213 // 3-D shadow edge color
214 case eColorID_threedshadow:
215 // 3-D shadow inner edge color
216 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Dark));
217 break;
219 case eColorID_threeddarkshadow:
220 // 3-D shadow outer edge color
221 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Shadow));
222 break;
224 case eColorID_window:
225 case eColorID_windowframe:
226 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
227 break;
229 case eColorID_windowtext:
230 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
231 break;
233 case eColorID__moz_eventreerow:
234 case eColorID__moz_field:
235 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Base));
236 break;
237 case eColorID__moz_fieldtext:
238 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
239 break;
240 case eColorID__moz_dialog:
241 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
242 break;
243 case eColorID__moz_dialogtext:
244 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::WindowText));
245 break;
246 case eColorID__moz_dragtargetzone:
247 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Window));
248 break;
249 case eColorID__moz_buttondefault:
250 // default button border color
251 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Button));
252 break;
253 case eColorID__moz_buttonhoverface:
254 aColor = BG_PRELIGHT_COLOR;
255 break;
256 case eColorID__moz_buttonhovertext:
257 aColor = FG_PRELIGHT_COLOR;
258 break;
259 case eColorID__moz_cellhighlight:
260 case eColorID__moz_html_cellhighlight:
261 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Highlight));
262 break;
263 case eColorID__moz_cellhighlighttext:
264 case eColorID__moz_html_cellhighlighttext:
265 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::HighlightedText));
266 break;
267 case eColorID__moz_menuhover:
268 aColor = BG_PRELIGHT_COLOR;
269 break;
270 case eColorID__moz_menuhovertext:
271 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
272 break;
273 case eColorID__moz_oddtreerow:
274 aColor = NS_TRANSPARENT;
275 break;
276 case eColorID__moz_nativehyperlinktext:
277 aColor = NS_SAME_AS_FOREGROUND_COLOR;
278 break;
279 case eColorID__moz_comboboxtext:
280 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
281 break;
282 case eColorID__moz_combobox:
283 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Base));
284 break;
285 case eColorID__moz_menubartext:
286 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
287 break;
288 case eColorID__moz_menubarhovertext:
289 aColor = QCOLOR_TO_NS_RGB(palette.color(QPalette::Normal, QPalette::Text));
290 break;
291 default:
292 /* default color is BLACK */
293 aColor = 0;
294 rv = NS_ERROR_FAILURE;
295 break;
298 return rv;
301 nsresult
302 nsLookAndFeel::GetIntImpl(IntID aID, int32_t &aResult)
304 nsresult rv = nsXPLookAndFeel::GetIntImpl(aID, aResult);
305 if (NS_SUCCEEDED(rv))
306 return rv;
308 rv = NS_OK;
310 switch (aID) {
311 case eIntID_CaretBlinkTime:
312 aResult = 500;
313 break;
315 case eIntID_CaretWidth:
316 aResult = 1;
317 break;
319 case eIntID_ShowCaretDuringSelection:
320 aResult = 0;
321 break;
323 case eIntID_SelectTextfieldsOnKeyFocus:
324 // Select textfield content when focused by kbd
325 // used by EventStateManager::sTextfieldSelectModel
326 aResult = 1;
327 break;
329 case eIntID_SubmenuDelay:
330 aResult = 200;
331 break;
333 case eIntID_TooltipDelay:
334 aResult = 500;
335 break;
337 case eIntID_MenusCanOverlapOSBar:
338 // we want XUL popups to be able to overlap the task bar.
339 aResult = 1;
340 break;
342 case eIntID_ScrollArrowStyle:
343 aResult = eScrollArrowStyle_Single;
344 break;
346 case eIntID_ScrollSliderStyle:
347 aResult = eScrollThumbStyle_Proportional;
348 break;
350 case eIntID_TouchEnabled:
351 aResult = 1;
352 break;
354 case eIntID_WindowsDefaultTheme:
355 case eIntID_WindowsThemeIdentifier:
356 case eIntID_OperatingSystemVersionIdentifier:
357 aResult = 0;
358 rv = NS_ERROR_NOT_IMPLEMENTED;
359 break;
361 case eIntID_IMERawInputUnderlineStyle:
362 case eIntID_IMEConvertedTextUnderlineStyle:
363 aResult = NS_STYLE_TEXT_DECORATION_STYLE_SOLID;
364 break;
366 case eIntID_IMESelectedRawTextUnderlineStyle:
367 case eIntID_IMESelectedConvertedTextUnderline:
368 aResult = NS_STYLE_TEXT_DECORATION_STYLE_NONE;
369 break;
371 case eIntID_SpellCheckerUnderlineStyle:
372 aResult = NS_STYLE_TEXT_DECORATION_STYLE_WAVY;
373 break;
375 case eIntID_ScrollbarButtonAutoRepeatBehavior:
376 aResult = 0;
377 break;
379 default:
380 aResult = 0;
381 rv = NS_ERROR_FAILURE;
384 return rv;
387 nsresult
388 nsLookAndFeel::GetFloatImpl(FloatID aID, float &aResult)
390 nsresult res = nsXPLookAndFeel::GetFloatImpl(aID, aResult);
391 if (NS_SUCCEEDED(res))
392 return res;
393 res = NS_OK;
395 switch (aID) {
396 case eFloatID_IMEUnderlineRelativeSize:
397 aResult = 1.0f;
398 break;
399 case eFloatID_SpellCheckerUnderlineRelativeSize:
400 aResult = 1.0f;
401 break;
402 default:
403 aResult = -1.0;
404 res = NS_ERROR_FAILURE;
406 return res;
409 /*virtual*/
410 bool
411 nsLookAndFeel::GetFontImpl(FontID aID, nsString& aFontName,
412 gfxFontStyle& aFontStyle,
413 float aDevPixPerCSSPixel)
415 QFont qFont = QGuiApplication::font();
417 NS_NAMED_LITERAL_STRING(quote, "\"");
418 nsString family((char16_t*)qFont.family().data());
419 aFontName = quote + family + quote;
421 aFontStyle.systemFont = true;
422 aFontStyle.style = qFont.style();
423 aFontStyle.weight = qFont.weight();
424 aFontStyle.stretch = qFont.stretch();
425 // use pixel size directly if it is set, otherwise compute from point size
426 if (qFont.pixelSize() != -1) {
427 aFontStyle.size = qFont.pixelSize();
428 } else {
429 aFontStyle.size = qFont.pointSizeF() * qApp->primaryScreen()->logicalDotsPerInch() / 72.0f;
432 return true;
435 /*virtual*/
436 bool
437 nsLookAndFeel::GetEchoPasswordImpl() {
438 return true;
441 /*virtual*/
442 uint32_t
443 nsLookAndFeel::GetPasswordMaskDelayImpl()
445 // Same value on Android framework
446 return 1500;
449 /* virtual */
450 char16_t
451 nsLookAndFeel::GetPasswordCharacterImpl()
453 return UNICODE_BULLET;