1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* Copyright 2012 Mozilla Foundation and Mozilla contributors
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #include "nsLookAndFeel.h"
18 #include "nsStyleConsts.h"
19 #include "gfxFont.h"
20 #include "gfxFontConstants.h"
21 #include "mozilla/gfx/2D.h"
22 #include "cutils/properties.h"
23 
24 static const char16_t UNICODE_BULLET = 0x2022;
25 
nsLookAndFeel()26 nsLookAndFeel::nsLookAndFeel()
27     : nsXPLookAndFeel()
28 {
29 }
30 
~nsLookAndFeel()31 nsLookAndFeel::~nsLookAndFeel()
32 {
33 }
34 
35 nsresult
NativeGetColor(ColorID aID,nscolor & aColor)36 nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor)
37 {
38     nsresult rv = NS_OK;
39 
40 #define BASE_ACTIVE_COLOR     NS_RGB(0xaa,0xaa,0xaa)
41 #define BASE_NORMAL_COLOR     NS_RGB(0xff,0xff,0xff)
42 #define BASE_SELECTED_COLOR   NS_RGB(0xaa,0xaa,0xaa)
43 #define BG_ACTIVE_COLOR       NS_RGB(0xff,0xff,0xff)
44 #define BG_INSENSITIVE_COLOR  NS_RGB(0xaa,0xaa,0xaa)
45 #define BG_NORMAL_COLOR       NS_RGB(0xff,0xff,0xff)
46 #define BG_PRELIGHT_COLOR     NS_RGB(0xee,0xee,0xee)
47 #define BG_SELECTED_COLOR     NS_RGB(0x99,0x99,0x99)
48 #define DARK_NORMAL_COLOR     NS_RGB(0x88,0x88,0x88)
49 #define FG_INSENSITIVE_COLOR  NS_RGB(0x44,0x44,0x44)
50 #define FG_NORMAL_COLOR       NS_RGB(0x00,0x00,0x00)
51 #define FG_PRELIGHT_COLOR     NS_RGB(0x77,0x77,0x77)
52 #define FG_SELECTED_COLOR     NS_RGB(0xaa,0xaa,0xaa)
53 #define LIGHT_NORMAL_COLOR    NS_RGB(0xaa,0xaa,0xaa)
54 #define TEXT_ACTIVE_COLOR     NS_RGB(0x99,0x99,0x99)
55 #define TEXT_NORMAL_COLOR     NS_RGB(0x00,0x00,0x00)
56 #define TEXT_SELECTED_COLOR   NS_RGB(0x00,0x00,0x00)
57 
58     switch (aID) {
59         // These colors don't seem to be used for anything anymore in Mozilla
60         // (except here at least TextSelectBackground and TextSelectForeground)
61         // The CSS2 colors below are used.
62     case eColorID_WindowBackground:
63         aColor = BASE_NORMAL_COLOR;
64         break;
65     case eColorID_WindowForeground:
66         aColor = TEXT_NORMAL_COLOR;
67         break;
68     case eColorID_WidgetBackground:
69         aColor = BG_NORMAL_COLOR;
70         break;
71     case eColorID_WidgetForeground:
72         aColor = FG_NORMAL_COLOR;
73         break;
74     case eColorID_WidgetSelectBackground:
75         aColor = BG_SELECTED_COLOR;
76         break;
77     case eColorID_WidgetSelectForeground:
78         aColor = FG_SELECTED_COLOR;
79         break;
80     case eColorID_Widget3DHighlight:
81         aColor = NS_RGB(0xa0,0xa0,0xa0);
82         break;
83     case eColorID_Widget3DShadow:
84         aColor = NS_RGB(0x40,0x40,0x40);
85         break;
86     case eColorID_TextBackground:
87         // not used?
88         aColor = BASE_NORMAL_COLOR;
89         break;
90     case eColorID_TextForeground:
91         // not used?
92         aColor = TEXT_NORMAL_COLOR;
93         break;
94     case eColorID_TextSelectBackground:
95         aColor = NS_RGBA(0x33,0xb5,0xe5,0x66);
96         break;
97     case eColorID_IMESelectedRawTextBackground:
98     case eColorID_IMESelectedConvertedTextBackground:
99         // still used
100         aColor = BASE_SELECTED_COLOR;
101         break;
102     case eColorID_TextSelectForegroundCustom:
103         aColor = NS_RGB(0x4d,0x4d,0x4d);
104         break;
105     case eColorID_TextSelectForeground:
106         aColor = NS_CHANGE_COLOR_IF_SAME_AS_BG;
107         break;
108     case eColorID_IMESelectedRawTextForeground:
109     case eColorID_IMESelectedConvertedTextForeground:
110         // still used
111         aColor = TEXT_SELECTED_COLOR;
112         break;
113     case eColorID_IMERawInputBackground:
114     case eColorID_IMEConvertedTextBackground:
115         aColor = NS_TRANSPARENT;
116         break;
117     case eColorID_IMERawInputForeground:
118     case eColorID_IMEConvertedTextForeground:
119         aColor = NS_SAME_AS_FOREGROUND_COLOR;
120         break;
121     case eColorID_IMERawInputUnderline:
122     case eColorID_IMEConvertedTextUnderline:
123         aColor = NS_SAME_AS_FOREGROUND_COLOR;
124         break;
125     case eColorID_IMESelectedRawTextUnderline:
126     case eColorID_IMESelectedConvertedTextUnderline:
127         aColor = NS_TRANSPARENT;
128         break;
129     case eColorID_SpellCheckerUnderline:
130       aColor = NS_RGB(0xff, 0, 0);
131       break;
132 
133         // css2  http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
134     case eColorID_activeborder:
135         // active window border
136         aColor = BG_NORMAL_COLOR;
137         break;
138     case eColorID_activecaption:
139         // active window caption background
140         aColor = BG_NORMAL_COLOR;
141         break;
142     case eColorID_appworkspace:
143         // MDI background color
144         aColor = BG_NORMAL_COLOR;
145         break;
146     case eColorID_background:
147         // desktop background
148         aColor = BG_NORMAL_COLOR;
149         break;
150     case eColorID_captiontext:
151         // text in active window caption, size box, and scrollbar arrow box (!)
152         aColor = FG_NORMAL_COLOR;
153         break;
154     case eColorID_graytext:
155         // disabled text in windows, menus, etc.
156         aColor = FG_INSENSITIVE_COLOR;
157         break;
158     case eColorID_highlight:
159         // background of selected item
160         aColor = BASE_SELECTED_COLOR;
161         break;
162     case eColorID_highlighttext:
163         // text of selected item
164         aColor = TEXT_SELECTED_COLOR;
165         break;
166     case eColorID_inactiveborder:
167         // inactive window border
168         aColor = BG_NORMAL_COLOR;
169         break;
170     case eColorID_inactivecaption:
171         // inactive window caption
172         aColor = BG_INSENSITIVE_COLOR;
173         break;
174     case eColorID_inactivecaptiontext:
175         // text in inactive window caption
176         aColor = FG_INSENSITIVE_COLOR;
177         break;
178     case eColorID_infobackground:
179         // tooltip background color
180         aColor = BG_NORMAL_COLOR;
181         break;
182     case eColorID_infotext:
183         // tooltip text color
184         aColor = TEXT_NORMAL_COLOR;
185         break;
186     case eColorID_menu:
187         // menu background
188         aColor = BG_NORMAL_COLOR;
189         break;
190     case eColorID_menutext:
191         // menu text
192         aColor = TEXT_NORMAL_COLOR;
193         break;
194     case eColorID_scrollbar:
195         // scrollbar gray area
196         aColor = BG_ACTIVE_COLOR;
197         break;
198 
199     case eColorID_threedface:
200     case eColorID_buttonface:
201         // 3-D face color
202         aColor = BG_NORMAL_COLOR;
203         break;
204 
205     case eColorID_buttontext:
206         // text on push buttons
207         aColor = TEXT_NORMAL_COLOR;
208         break;
209 
210     case eColorID_buttonhighlight:
211         // 3-D highlighted edge color
212     case eColorID_threedhighlight:
213         // 3-D highlighted outer edge color
214         aColor = LIGHT_NORMAL_COLOR;
215         break;
216 
217     case eColorID_threedlightshadow:
218         // 3-D highlighted inner edge color
219         aColor = BG_NORMAL_COLOR;
220         break;
221 
222     case eColorID_buttonshadow:
223         // 3-D shadow edge color
224     case eColorID_threedshadow:
225         // 3-D shadow inner edge color
226         aColor = DARK_NORMAL_COLOR;
227         break;
228 
229     case eColorID_threeddarkshadow:
230         // 3-D shadow outer edge color
231         aColor = NS_RGB(0,0,0);
232         break;
233 
234     case eColorID_window:
235     case eColorID_windowframe:
236         aColor = BG_NORMAL_COLOR;
237         break;
238 
239     case eColorID_windowtext:
240         aColor = FG_NORMAL_COLOR;
241         break;
242 
243     case eColorID__moz_eventreerow:
244     case eColorID__moz_field:
245         aColor = BASE_NORMAL_COLOR;
246         break;
247     case eColorID__moz_fieldtext:
248         aColor = TEXT_NORMAL_COLOR;
249         break;
250     case eColorID__moz_dialog:
251         aColor = BG_NORMAL_COLOR;
252         break;
253     case eColorID__moz_dialogtext:
254         aColor = FG_NORMAL_COLOR;
255         break;
256     case eColorID__moz_dragtargetzone:
257         aColor = BG_SELECTED_COLOR;
258         break;
259     case eColorID__moz_buttondefault:
260         // default button border color
261         aColor = NS_RGB(0,0,0);
262         break;
263     case eColorID__moz_buttonhoverface:
264         aColor = BG_PRELIGHT_COLOR;
265         break;
266     case eColorID__moz_buttonhovertext:
267         aColor = FG_PRELIGHT_COLOR;
268         break;
269     case eColorID__moz_cellhighlight:
270     case eColorID__moz_html_cellhighlight:
271         aColor = BASE_ACTIVE_COLOR;
272         break;
273     case eColorID__moz_cellhighlighttext:
274     case eColorID__moz_html_cellhighlighttext:
275         aColor = TEXT_ACTIVE_COLOR;
276         break;
277     case eColorID__moz_menuhover:
278         aColor = BG_PRELIGHT_COLOR;
279         break;
280     case eColorID__moz_menuhovertext:
281         aColor = FG_PRELIGHT_COLOR;
282         break;
283     case eColorID__moz_oddtreerow:
284         aColor = NS_TRANSPARENT;
285         break;
286     case eColorID__moz_nativehyperlinktext:
287         aColor = NS_SAME_AS_FOREGROUND_COLOR;
288         break;
289     case eColorID__moz_comboboxtext:
290         aColor = TEXT_NORMAL_COLOR;
291         break;
292     case eColorID__moz_combobox:
293         aColor = BG_NORMAL_COLOR;
294         break;
295     case eColorID__moz_menubartext:
296         aColor = TEXT_NORMAL_COLOR;
297         break;
298     case eColorID__moz_menubarhovertext:
299         aColor = FG_PRELIGHT_COLOR;
300         break;
301     default:
302         /* default color is BLACK */
303         aColor = 0;
304         rv = NS_ERROR_FAILURE;
305         break;
306     }
307 
308     return rv;
309 }
310 
311 nsresult
GetIntImpl(IntID aID,int32_t & aResult)312 nsLookAndFeel::GetIntImpl(IntID aID, int32_t &aResult)
313 {
314     nsresult rv = nsXPLookAndFeel::GetIntImpl(aID, aResult);
315     if (NS_SUCCEEDED(rv))
316         return rv;
317 
318     rv = NS_OK;
319 
320     switch (aID) {
321         case eIntID_CaretBlinkTime:
322             aResult = 500;
323             break;
324 
325         case eIntID_CaretWidth:
326             aResult = 1;
327             break;
328 
329         case eIntID_ShowCaretDuringSelection:
330             aResult = 0;
331             break;
332 
333         case eIntID_SelectTextfieldsOnKeyFocus:
334             // Select textfield content when focused by kbd
335             // used by EventStateManager::sTextfieldSelectModel
336             aResult = 1;
337             break;
338 
339         case eIntID_SubmenuDelay:
340             aResult = 200;
341             break;
342 
343         case eIntID_TooltipDelay:
344             aResult = 500;
345             break;
346 
347         case eIntID_MenusCanOverlapOSBar:
348             // we want XUL popups to be able to overlap the task bar.
349             aResult = 1;
350             break;
351 
352         case eIntID_ScrollArrowStyle:
353             aResult = eScrollArrowStyle_Single;
354             break;
355 
356         case eIntID_ScrollSliderStyle:
357             aResult = eScrollThumbStyle_Proportional;
358             break;
359 
360         case eIntID_TouchEnabled:
361             aResult = 1;
362             break;
363 
364         case eIntID_WindowsDefaultTheme:
365         case eIntID_WindowsThemeIdentifier:
366         case eIntID_OperatingSystemVersionIdentifier:
367             aResult = 0;
368             rv = NS_ERROR_NOT_IMPLEMENTED;
369             break;
370 
371         case eIntID_IMERawInputUnderlineStyle:
372         case eIntID_IMEConvertedTextUnderlineStyle:
373             aResult = NS_STYLE_TEXT_DECORATION_STYLE_SOLID;
374             break;
375 
376         case eIntID_IMESelectedRawTextUnderlineStyle:
377         case eIntID_IMESelectedConvertedTextUnderline:
378             aResult = NS_STYLE_TEXT_DECORATION_STYLE_NONE;
379             break;
380 
381         case eIntID_SpellCheckerUnderlineStyle:
382             aResult = NS_STYLE_TEXT_DECORATION_STYLE_WAVY;
383             break;
384 
385         case eIntID_ScrollbarButtonAutoRepeatBehavior:
386             aResult = 0;
387             break;
388 
389         case eIntID_PhysicalHomeButton: {
390             char propValue[PROPERTY_VALUE_MAX];
391             property_get("ro.moz.has_home_button", propValue, "1");
392             aResult = atoi(propValue);
393             break;
394         }
395 
396         case eIntID_ContextMenuOffsetVertical:
397         case eIntID_ContextMenuOffsetHorizontal:
398             aResult = 2;
399             break;
400 
401         default:
402             aResult = 0;
403             rv = NS_ERROR_FAILURE;
404     }
405 
406     return rv;
407 }
408 
409 nsresult
GetFloatImpl(FloatID aID,float & aResult)410 nsLookAndFeel::GetFloatImpl(FloatID aID, float &aResult)
411 {
412   nsresult res = nsXPLookAndFeel::GetFloatImpl(aID, aResult);
413   if (NS_SUCCEEDED(res))
414     return res;
415   res = NS_OK;
416 
417   switch (aID) {
418     case eFloatID_IMEUnderlineRelativeSize:
419         aResult = 1.0f;
420         break;
421     case eFloatID_SpellCheckerUnderlineRelativeSize:
422         aResult = 1.0f;
423         break;
424     default:
425         aResult = -1.0;
426         res = NS_ERROR_FAILURE;
427     }
428   return res;
429 }
430 
431 /*virtual*/
432 bool
GetFontImpl(FontID aID,nsString & aFontName,gfxFontStyle & aFontStyle,float aDevPixPerCSSPixel)433 nsLookAndFeel::GetFontImpl(FontID aID, nsString& aFontName,
434                            gfxFontStyle& aFontStyle,
435                            float aDevPixPerCSSPixel)
436 {
437     aFontName.AssignLiteral("\"Fira Sans\"");
438     aFontStyle.style = NS_FONT_STYLE_NORMAL;
439     aFontStyle.weight = NS_FONT_WEIGHT_NORMAL;
440     aFontStyle.stretch = NS_FONT_STRETCH_NORMAL;
441     aFontStyle.size = 9.0 * 96.0f / 72.0f;
442     aFontStyle.systemFont = true;
443     return true;
444 }
445 
446 /*virtual*/
447 bool
GetEchoPasswordImpl()448 nsLookAndFeel::GetEchoPasswordImpl() {
449     return true;
450 }
451 
452 /*virtual*/
453 uint32_t
GetPasswordMaskDelayImpl()454 nsLookAndFeel::GetPasswordMaskDelayImpl()
455 {
456     // Same value on Android framework
457     return 1500;
458 }
459 
460 /* virtual */
461 char16_t
GetPasswordCharacterImpl()462 nsLookAndFeel::GetPasswordCharacterImpl()
463 {
464     return UNICODE_BULLET;
465 }
466