no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / widget / uikit / nsLookAndFeel.mm
blob29eb52a234e14160f61909656f1834a39aa8c4e3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #import <UIKit/UIColor.h>
7 #import <UIKit/UIInterface.h>
9 #include "nsLookAndFeel.h"
11 #include "mozilla/FontPropertyTypes.h"
12 #include "nsStyleConsts.h"
13 #include "gfxFont.h"
14 #include "gfxFontConstants.h"
16 nsLookAndFeel::nsLookAndFeel() : mInitialized(false) {}
18 nsLookAndFeel::~nsLookAndFeel() {}
20 static nscolor GetColorFromUIColor(UIColor* aColor) {
21   CGColorRef cgColor = [aColor CGColor];
22   CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(cgColor));
23   const CGFloat* components = CGColorGetComponents(cgColor);
24   if (model == kCGColorSpaceModelRGB) {
25     return NS_RGB((unsigned int)(components[0] * 255.0),
26                   (unsigned int)(components[1] * 255.0),
27                   (unsigned int)(components[2] * 255.0));
28   } else if (model == kCGColorSpaceModelMonochrome) {
29     unsigned int val = (unsigned int)(components[0] * 255.0);
30     return NS_RGBA(val, val, val, (unsigned int)(components[1] * 255.0));
31   }
32   MOZ_ASSERT_UNREACHABLE("Unhandled color space!");
33   return 0;
36 void nsLookAndFeel::NativeInit() { EnsureInit(); }
38 void nsLookAndFeel::RefreshImpl() {
39   nsXPLookAndFeel::RefreshImpl();
41   mInitialized = false;
44 nsresult nsLookAndFeel::NativeGetColor(ColorID, ColorScheme, nscolor& aResult) {
45   EnsureInit();
47   nsresult res = NS_OK;
49   switch (aID) {
50     case ColorID::Highlight:
51       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
52       break;
53     case ColorID::MozMenuhover:
54       aResult = NS_RGB(0xee, 0xee, 0xee);
55       break;
56     case ColorID::Highlighttext:
57     case ColorID::MozMenuhovertext:
58       aResult = mColorTextSelectForeground;
59       break;
60     case ColorID::IMESelectedRawTextBackground:
61     case ColorID::IMESelectedConvertedTextBackground:
62     case ColorID::IMERawInputBackground:
63     case ColorID::IMEConvertedTextBackground:
64       aResult = NS_TRANSPARENT;
65       break;
66     case ColorID::IMESelectedRawTextForeground:
67     case ColorID::IMESelectedConvertedTextForeground:
68     case ColorID::IMERawInputForeground:
69     case ColorID::IMEConvertedTextForeground:
70       aResult = NS_SAME_AS_FOREGROUND_COLOR;
71       break;
72     case ColorID::IMERawInputUnderline:
73     case ColorID::IMEConvertedTextUnderline:
74       aResult = NS_40PERCENT_FOREGROUND_COLOR;
75       break;
76     case ColorID::IMESelectedRawTextUnderline:
77     case ColorID::IMESelectedConvertedTextUnderline:
78       aResult = NS_SAME_AS_FOREGROUND_COLOR;
79       break;
80     case ColorID::SpellCheckerUnderline:
81       aResult = NS_RGB(0xff, 0, 0);
82       break;
84     //
85     // css2 system colors http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
86     //
87     case ColorID::Buttontext:
88     case ColorID::MozButtonhovertext:
89     case ColorID::Captiontext:
90     case ColorID::Menutext:
91     case ColorID::Infotext:
92     case ColorID::Windowtext:
93       aResult = mColorDarkText;
94       break;
95     case ColorID::Activecaption:
96       aResult = NS_RGB(0xff, 0xff, 0xff);
97       break;
98     case ColorID::Activeborder:
99       aResult = NS_RGB(0x00, 0x00, 0x00);
100       break;
101     case ColorID::Appworkspace:
102       aResult = NS_RGB(0xFF, 0xFF, 0xFF);
103       break;
104     case ColorID::Background:
105       aResult = NS_RGB(0x63, 0x63, 0xCE);
106       break;
107     case ColorID::Buttonface:
108     case ColorID::MozButtonhoverface:
109       aResult = NS_RGB(0xF0, 0xF0, 0xF0);
110       break;
111     case ColorID::Buttonhighlight:
112       aResult = NS_RGB(0xFF, 0xFF, 0xFF);
113       break;
114     case ColorID::Buttonshadow:
115       aResult = NS_RGB(0xDC, 0xDC, 0xDC);
116       break;
117     case ColorID::Graytext:
118       aResult = NS_RGB(0x44, 0x44, 0x44);
119       break;
120     case ColorID::Inactiveborder:
121       aResult = NS_RGB(0xff, 0xff, 0xff);
122       break;
123     case ColorID::Inactivecaption:
124       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
125       break;
126     case ColorID::Inactivecaptiontext:
127       aResult = NS_RGB(0x45, 0x45, 0x45);
128       break;
129     case ColorID::Scrollbar:
130       aResult = NS_RGB(0, 0, 0);  // XXX
131       break;
132     case ColorID::Threeddarkshadow:
133       aResult = NS_RGB(0xDC, 0xDC, 0xDC);
134       break;
135     case ColorID::Threedshadow:
136       aResult = NS_RGB(0xE0, 0xE0, 0xE0);
137       break;
138     case ColorID::Threedface:
139       aResult = NS_RGB(0xF0, 0xF0, 0xF0);
140       break;
141     case ColorID::Threedhighlight:
142       aResult = NS_RGB(0xff, 0xff, 0xff);
143       break;
144     case ColorID::Threedlightshadow:
145       aResult = NS_RGB(0xDA, 0xDA, 0xDA);
146       break;
147     case ColorID::Menu:
148       aResult = NS_RGB(0xff, 0xff, 0xff);
149       break;
150     case ColorID::Infobackground:
151       aResult = NS_RGB(0xFF, 0xFF, 0xC7);
152       break;
153     case ColorID::Windowframe:
154       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
155       break;
156     case ColorID::Window:
157     case ColorID::MozField:
158     case ColorID::MozCombobox:
159       aResult = NS_RGB(0xff, 0xff, 0xff);
160       break;
161     case ColorID::MozFieldtext:
162     case ColorID::MozComboboxtext:
163       aResult = mColorDarkText;
164       break;
165     case ColorID::MozDialog:
166       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
167       break;
168     case ColorID::MozDialogtext:
169     case ColorID::MozCellhighlighttext:
170     case ColorID::Selecteditemtext:
171     case ColorID::MozColheadertext:
172     case ColorID::MozColheaderhovertext:
173       aResult = mColorDarkText;
174       break;
175     case ColorID::MozMacFocusring:
176       aResult = NS_RGB(0x3F, 0x98, 0xDD);
177       break;
178     case ColorID::MozMacMenutextdisable:
179       aResult = NS_RGB(0x88, 0x88, 0x88);
180       break;
181     case ColorID::MozMacMenutextselect:
182       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
183       break;
184     case ColorID::MozMacDisabledtoolbartext:
185       aResult = NS_RGB(0x3F, 0x3F, 0x3F);
186       break;
187     case ColorID::MozCellhighlight:
188     case ColorID::Selecteditem:
189       // For inactive list selection
190       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
191       break;
192     case ColorID::MozEventreerow:
193       // Background color of even list rows.
194       aResult = NS_RGB(0xff, 0xff, 0xff);
195       break;
196     case ColorID::MozOddtreerow:
197       // Background color of odd list rows.
198       aResult = NS_TRANSPARENT;
199       break;
200     case ColorID::MozNativehyperlinktext:
201       // There appears to be no available system defined color. HARDCODING to
202       // the appropriate color.
203       aResult = NS_RGB(0x14, 0x4F, 0xAE);
204       break;
205     case ColorID::MozNativevisitedhyperlinktext:
206       // Safari defaults to the MacOS color implementation for visited links,
207       // which in turn uses systemPurpleColor, so we do the same here.
208       aResult = GetColorFromUIColor([UIColor systemPurpleColor]);
209       break;
210     default:
211       NS_WARNING("Someone asked nsILookAndFeel for a color I don't know about");
212       aResult = NS_RGB(0xff, 0xff, 0xff);
213       res = NS_ERROR_FAILURE;
214       break;
215   }
217   return res;
220 NS_IMETHODIMP
221 nsLookAndFeel::NativeGetInt(IntID aID, int32_t& aResult) {
222   nsresult res = NS_OK;
224   switch (aID) {
225     case IntID::ScrollButtonLeftMouseButtonAction:
226       aResult = 0;
227       break;
228     case IntID::ScrollButtonMiddleMouseButtonAction:
229     case IntID::ScrollButtonRightMouseButtonAction:
230       aResult = 3;
231       break;
232     case IntID::CaretBlinkTime:
233       aResult = 567;
234       break;
235     case IntID::CaretWidth:
236       aResult = 1;
237       break;
238     case IntID::ShowCaretDuringSelection:
239       aResult = 0;
240       break;
241     case IntID::SelectTextfieldsOnKeyFocus:
242       // Select textfield content when focused by kbd
243       // used by nsEventStateManager::sTextfieldSelectModel
244       aResult = 1;
245       break;
246     case IntID::SubmenuDelay:
247       aResult = 200;
248       break;
249     case IntID::MenusCanOverlapOSBar:
250       // xul popups are not allowed to overlap the menubar.
251       aResult = 0;
252       break;
253     case IntID::SkipNavigatingDisabledMenuItem:
254       aResult = 1;
255       break;
256     case IntID::DragThresholdX:
257     case IntID::DragThresholdY:
258       aResult = 4;
259       break;
260     case IntID::ScrollArrowStyle:
261       aResult = eScrollArrow_None;
262       break;
263     case IntID::TreeOpenDelay:
264       aResult = 1000;
265       break;
266     case IntID::TreeCloseDelay:
267       aResult = 1000;
268       break;
269     case IntID::TreeLazyScrollDelay:
270       aResult = 150;
271       break;
272     case IntID::TreeScrollDelay:
273       aResult = 100;
274       break;
275     case IntID::TreeScrollLinesMax:
276       aResult = 3;
277       break;
278     case IntID::TabFocusModel:
279       aResult = 1;  // default to just textboxes
280       break;
281     case IntID::ScrollToClick:
282       aResult = 0;
283       break;
284     case IntID::ChosenMenuItemsShouldBlink:
285       aResult = 1;
286       break;
287     case IntID::IMERawInputUnderlineStyle:
288     case IntID::IMEConvertedTextUnderlineStyle:
289     case IntID::IMESelectedRawTextUnderlineStyle:
290     case IntID::IMESelectedConvertedTextUnderline:
291       aResult = static_cast<int32_t>(StyleTextDecorationStyle::Solid);
292       break;
293     case IntID::SpellCheckerUnderlineStyle:
294       aResult = static_cast<int32_t>(StyleTextDecorationStyle::Dotted);
295       break;
296     case IntID::ContextMenuOffsetVertical:
297     case IntID::ContextMenuOffsetHorizontal:
298       aResult = 2;
299       break;
300     default:
301       aResult = 0;
302       res = NS_ERROR_FAILURE;
303   }
304   return res;
307 NS_IMETHODIMP
308 nsLookAndFeel::NativeGetFloat(FloatID aID, float& aResult) {
309   nsresult res = NS_OK;
311   switch (aID) {
312     case FloatID::IMEUnderlineRelativeSize:
313       aResult = 2.0f;
314       break;
315     case FloatID::SpellCheckerUnderlineRelativeSize:
316       aResult = 2.0f;
317       break;
318     default:
319       aResult = -1.0;
320       res = NS_ERROR_FAILURE;
321   }
323   return res;
326 bool nsLookAndFeel::NativeGetFont(FontID aID, nsString& aFontName,
327                                   gfxFontStyle& aFontStyle) {
328   // hack for now
329   if (aID == FontID::Window || aID == FontID::Document) {
330     aFontStyle.style = FontSlantStyle::Normal();
331     aFontStyle.weight = FontWeight::Normal();
332     aFontStyle.stretch = FontStretch::Normal();
333     aFontStyle.size = 14;
334     aFontStyle.systemFont = true;
336     aFontName.AssignLiteral("sans-serif");
337     return true;
338   }
340   // TODO: implement more here?
341   return false;
344 void nsLookAndFeel::EnsureInit() {
345   if (mInitialized) {
346     return;
347   }
348   mInitialized = true;
350   nscolor color;
351   GetColor(ColorID::TextSelectBackground, color);
352   if (color == 0x000000) {
353     mColorTextSelectForeground = NS_RGB(0xff, 0xff, 0xff);
354   } else {
355     mColorTextSelectForeground = NS_SAME_AS_FOREGROUND_COLOR;
356   }
358   mColorDarkText = GetColorFromUIColor([UIColor darkTextColor]);
360   RecordTelemetry();