1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/android/nsLookAndFeel.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,495 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/dom/ContentChild.h" 1.10 +#include "nsStyleConsts.h" 1.11 +#include "nsXULAppAPI.h" 1.12 +#include "nsLookAndFeel.h" 1.13 +#include "gfxFont.h" 1.14 +#include "gfxFontConstants.h" 1.15 +#include "mozilla/gfx/2D.h" 1.16 + 1.17 +using namespace mozilla; 1.18 +using mozilla::dom::ContentChild; 1.19 + 1.20 +bool nsLookAndFeel::mInitializedSystemColors = false; 1.21 +AndroidSystemColors nsLookAndFeel::mSystemColors; 1.22 + 1.23 +bool nsLookAndFeel::mInitializedShowPassword = false; 1.24 +bool nsLookAndFeel::mShowPassword = true; 1.25 + 1.26 +static const char16_t UNICODE_BULLET = 0x2022; 1.27 + 1.28 +nsLookAndFeel::nsLookAndFeel() 1.29 + : nsXPLookAndFeel() 1.30 +{ 1.31 +} 1.32 + 1.33 +nsLookAndFeel::~nsLookAndFeel() 1.34 +{ 1.35 +} 1.36 + 1.37 +#define BG_PRELIGHT_COLOR NS_RGB(0xee,0xee,0xee) 1.38 +#define FG_PRELIGHT_COLOR NS_RGB(0x77,0x77,0x77) 1.39 +#define BLACK_COLOR NS_RGB(0x00,0x00,0x00) 1.40 +#define DARK_GRAY_COLOR NS_RGB(0x40,0x40,0x40) 1.41 +#define GRAY_COLOR NS_RGB(0x80,0x80,0x80) 1.42 +#define LIGHT_GRAY_COLOR NS_RGB(0xa0,0xa0,0xa0) 1.43 +#define RED_COLOR NS_RGB(0xff,0x00,0x00) 1.44 + 1.45 +nsresult 1.46 +nsLookAndFeel::GetSystemColors() 1.47 +{ 1.48 + if (mInitializedSystemColors) 1.49 + return NS_OK; 1.50 + 1.51 + if (!AndroidBridge::Bridge()) 1.52 + return NS_ERROR_FAILURE; 1.53 + 1.54 + AndroidBridge::Bridge()->GetSystemColors(&mSystemColors); 1.55 + 1.56 + mInitializedSystemColors = true; 1.57 + 1.58 + return NS_OK; 1.59 +} 1.60 + 1.61 +nsresult 1.62 +nsLookAndFeel::CallRemoteGetSystemColors() 1.63 +{ 1.64 + // An array has to be used to get data from remote process 1.65 + InfallibleTArray<uint32_t> colors; 1.66 + uint32_t colorsCount = sizeof(AndroidSystemColors) / sizeof(nscolor); 1.67 + 1.68 + if (!ContentChild::GetSingleton()->SendGetSystemColors(colorsCount, &colors)) 1.69 + return NS_ERROR_FAILURE; 1.70 + 1.71 + NS_ASSERTION(colors.Length() == colorsCount, "System colors array is incomplete"); 1.72 + if (colors.Length() == 0) 1.73 + return NS_ERROR_FAILURE; 1.74 + 1.75 + if (colors.Length() < colorsCount) 1.76 + colorsCount = colors.Length(); 1.77 + 1.78 + // Array elements correspond to the members of mSystemColors structure, 1.79 + // so just copy the memory block 1.80 + memcpy(&mSystemColors, colors.Elements(), sizeof(nscolor) * colorsCount); 1.81 + 1.82 + mInitializedSystemColors = true; 1.83 + 1.84 + return NS_OK; 1.85 +} 1.86 + 1.87 +nsresult 1.88 +nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor) 1.89 +{ 1.90 + nsresult rv = NS_OK; 1.91 + 1.92 + if (!mInitializedSystemColors) { 1.93 + if (XRE_GetProcessType() == GeckoProcessType_Default) 1.94 + rv = GetSystemColors(); 1.95 + else 1.96 + rv = CallRemoteGetSystemColors(); 1.97 + NS_ENSURE_SUCCESS(rv, rv); 1.98 + } 1.99 + 1.100 + // XXX we'll want to use context.obtainStyledAttributes on the java side to 1.101 + // get all of these; see TextView.java for a good exmaple. 1.102 + 1.103 + switch (aID) { 1.104 + // These colors don't seem to be used for anything anymore in Mozilla 1.105 + // (except here at least TextSelectBackground and TextSelectForeground) 1.106 + // The CSS2 colors below are used. 1.107 + case eColorID_WindowBackground: 1.108 + aColor = NS_RGB(0xFF, 0xFF, 0xFF); 1.109 + break; 1.110 + case eColorID_WindowForeground: 1.111 + aColor = mSystemColors.textColorPrimary; 1.112 + break; 1.113 + case eColorID_WidgetBackground: 1.114 + aColor = mSystemColors.colorBackground; 1.115 + break; 1.116 + case eColorID_WidgetForeground: 1.117 + aColor = mSystemColors.colorForeground; 1.118 + break; 1.119 + case eColorID_WidgetSelectBackground: 1.120 + aColor = mSystemColors.textColorHighlight; 1.121 + break; 1.122 + case eColorID_WidgetSelectForeground: 1.123 + aColor = mSystemColors.textColorPrimaryInverse; 1.124 + break; 1.125 + case eColorID_Widget3DHighlight: 1.126 + aColor = LIGHT_GRAY_COLOR; 1.127 + break; 1.128 + case eColorID_Widget3DShadow: 1.129 + aColor = DARK_GRAY_COLOR; 1.130 + break; 1.131 + case eColorID_TextBackground: 1.132 + // not used? 1.133 + aColor = mSystemColors.colorBackground; 1.134 + break; 1.135 + case eColorID_TextForeground: 1.136 + // not used? 1.137 + aColor = mSystemColors.textColorPrimary; 1.138 + break; 1.139 + case eColorID_TextSelectBackground: 1.140 + case eColorID_IMESelectedRawTextBackground: 1.141 + case eColorID_IMESelectedConvertedTextBackground: 1.142 + // still used 1.143 + aColor = mSystemColors.textColorHighlight; 1.144 + break; 1.145 + case eColorID_TextSelectForeground: 1.146 + case eColorID_IMESelectedRawTextForeground: 1.147 + case eColorID_IMESelectedConvertedTextForeground: 1.148 + // still used 1.149 + aColor = mSystemColors.textColorPrimaryInverse; 1.150 + break; 1.151 + case eColorID_IMERawInputBackground: 1.152 + case eColorID_IMEConvertedTextBackground: 1.153 + aColor = NS_TRANSPARENT; 1.154 + break; 1.155 + case eColorID_IMERawInputForeground: 1.156 + case eColorID_IMEConvertedTextForeground: 1.157 + aColor = NS_SAME_AS_FOREGROUND_COLOR; 1.158 + break; 1.159 + case eColorID_IMERawInputUnderline: 1.160 + case eColorID_IMEConvertedTextUnderline: 1.161 + aColor = NS_SAME_AS_FOREGROUND_COLOR; 1.162 + break; 1.163 + case eColorID_IMESelectedRawTextUnderline: 1.164 + case eColorID_IMESelectedConvertedTextUnderline: 1.165 + aColor = NS_TRANSPARENT; 1.166 + break; 1.167 + case eColorID_SpellCheckerUnderline: 1.168 + aColor = RED_COLOR; 1.169 + break; 1.170 + 1.171 + // css2 http://www.w3.org/TR/REC-CSS2/ui.html#system-colors 1.172 + case eColorID_activeborder: 1.173 + // active window border 1.174 + aColor = mSystemColors.colorBackground; 1.175 + break; 1.176 + case eColorID_activecaption: 1.177 + // active window caption background 1.178 + aColor = mSystemColors.colorBackground; 1.179 + break; 1.180 + case eColorID_appworkspace: 1.181 + // MDI background color 1.182 + aColor = mSystemColors.colorBackground; 1.183 + break; 1.184 + case eColorID_background: 1.185 + // desktop background 1.186 + aColor = mSystemColors.colorBackground; 1.187 + break; 1.188 + case eColorID_captiontext: 1.189 + // text in active window caption, size box, and scrollbar arrow box (!) 1.190 + aColor = mSystemColors.colorForeground; 1.191 + break; 1.192 + case eColorID_graytext: 1.193 + // disabled text in windows, menus, etc. 1.194 + aColor = mSystemColors.textColorTertiary; 1.195 + break; 1.196 + case eColorID_highlight: 1.197 + // background of selected item 1.198 + aColor = mSystemColors.textColorHighlight; 1.199 + break; 1.200 + case eColorID_highlighttext: 1.201 + // text of selected item 1.202 + aColor = mSystemColors.textColorPrimaryInverse; 1.203 + break; 1.204 + case eColorID_inactiveborder: 1.205 + // inactive window border 1.206 + aColor = mSystemColors.colorBackground; 1.207 + break; 1.208 + case eColorID_inactivecaption: 1.209 + // inactive window caption 1.210 + aColor = mSystemColors.colorBackground; 1.211 + break; 1.212 + case eColorID_inactivecaptiontext: 1.213 + // text in inactive window caption 1.214 + aColor = mSystemColors.textColorTertiary; 1.215 + break; 1.216 + case eColorID_infobackground: 1.217 + // tooltip background color 1.218 + aColor = mSystemColors.colorBackground; 1.219 + break; 1.220 + case eColorID_infotext: 1.221 + // tooltip text color 1.222 + aColor = mSystemColors.colorForeground; 1.223 + break; 1.224 + case eColorID_menu: 1.225 + // menu background 1.226 + aColor = mSystemColors.colorBackground; 1.227 + break; 1.228 + case eColorID_menutext: 1.229 + // menu text 1.230 + aColor = mSystemColors.colorForeground; 1.231 + break; 1.232 + case eColorID_scrollbar: 1.233 + // scrollbar gray area 1.234 + aColor = mSystemColors.colorBackground; 1.235 + break; 1.236 + 1.237 + case eColorID_threedface: 1.238 + case eColorID_buttonface: 1.239 + // 3-D face color 1.240 + aColor = mSystemColors.colorBackground; 1.241 + break; 1.242 + 1.243 + case eColorID_buttontext: 1.244 + // text on push buttons 1.245 + aColor = mSystemColors.colorForeground; 1.246 + break; 1.247 + 1.248 + case eColorID_buttonhighlight: 1.249 + // 3-D highlighted edge color 1.250 + case eColorID_threedhighlight: 1.251 + // 3-D highlighted outer edge color 1.252 + aColor = LIGHT_GRAY_COLOR; 1.253 + break; 1.254 + 1.255 + case eColorID_threedlightshadow: 1.256 + // 3-D highlighted inner edge color 1.257 + aColor = mSystemColors.colorBackground; 1.258 + break; 1.259 + 1.260 + case eColorID_buttonshadow: 1.261 + // 3-D shadow edge color 1.262 + case eColorID_threedshadow: 1.263 + // 3-D shadow inner edge color 1.264 + aColor = GRAY_COLOR; 1.265 + break; 1.266 + 1.267 + case eColorID_threeddarkshadow: 1.268 + // 3-D shadow outer edge color 1.269 + aColor = BLACK_COLOR; 1.270 + break; 1.271 + 1.272 + case eColorID_window: 1.273 + case eColorID_windowframe: 1.274 + aColor = mSystemColors.colorBackground; 1.275 + break; 1.276 + 1.277 + case eColorID_windowtext: 1.278 + aColor = mSystemColors.textColorPrimary; 1.279 + break; 1.280 + 1.281 + case eColorID__moz_eventreerow: 1.282 + case eColorID__moz_field: 1.283 + aColor = mSystemColors.colorBackground; 1.284 + break; 1.285 + case eColorID__moz_fieldtext: 1.286 + aColor = mSystemColors.textColorPrimary; 1.287 + break; 1.288 + case eColorID__moz_dialog: 1.289 + aColor = mSystemColors.colorBackground; 1.290 + break; 1.291 + case eColorID__moz_dialogtext: 1.292 + aColor = mSystemColors.colorForeground; 1.293 + break; 1.294 + case eColorID__moz_dragtargetzone: 1.295 + aColor = mSystemColors.textColorHighlight; 1.296 + break; 1.297 + case eColorID__moz_buttondefault: 1.298 + // default button border color 1.299 + aColor = BLACK_COLOR; 1.300 + break; 1.301 + case eColorID__moz_buttonhoverface: 1.302 + aColor = BG_PRELIGHT_COLOR; 1.303 + break; 1.304 + case eColorID__moz_buttonhovertext: 1.305 + aColor = FG_PRELIGHT_COLOR; 1.306 + break; 1.307 + case eColorID__moz_cellhighlight: 1.308 + case eColorID__moz_html_cellhighlight: 1.309 + aColor = mSystemColors.textColorHighlight; 1.310 + break; 1.311 + case eColorID__moz_cellhighlighttext: 1.312 + case eColorID__moz_html_cellhighlighttext: 1.313 + aColor = mSystemColors.textColorPrimaryInverse; 1.314 + break; 1.315 + case eColorID__moz_menuhover: 1.316 + aColor = BG_PRELIGHT_COLOR; 1.317 + break; 1.318 + case eColorID__moz_menuhovertext: 1.319 + aColor = FG_PRELIGHT_COLOR; 1.320 + break; 1.321 + case eColorID__moz_oddtreerow: 1.322 + aColor = NS_TRANSPARENT; 1.323 + break; 1.324 + case eColorID__moz_nativehyperlinktext: 1.325 + aColor = NS_SAME_AS_FOREGROUND_COLOR; 1.326 + break; 1.327 + case eColorID__moz_comboboxtext: 1.328 + aColor = mSystemColors.colorForeground; 1.329 + break; 1.330 + case eColorID__moz_combobox: 1.331 + aColor = mSystemColors.colorBackground; 1.332 + break; 1.333 + case eColorID__moz_menubartext: 1.334 + aColor = mSystemColors.colorForeground; 1.335 + break; 1.336 + case eColorID__moz_menubarhovertext: 1.337 + aColor = FG_PRELIGHT_COLOR; 1.338 + break; 1.339 + default: 1.340 + /* default color is BLACK */ 1.341 + aColor = 0; 1.342 + rv = NS_ERROR_FAILURE; 1.343 + break; 1.344 + } 1.345 + 1.346 + return rv; 1.347 +} 1.348 + 1.349 + 1.350 +nsresult 1.351 +nsLookAndFeel::GetIntImpl(IntID aID, int32_t &aResult) 1.352 +{ 1.353 + nsresult rv = nsXPLookAndFeel::GetIntImpl(aID, aResult); 1.354 + if (NS_SUCCEEDED(rv)) 1.355 + return rv; 1.356 + 1.357 + rv = NS_OK; 1.358 + 1.359 + switch (aID) { 1.360 + case eIntID_CaretBlinkTime: 1.361 + aResult = 500; 1.362 + break; 1.363 + 1.364 + case eIntID_CaretWidth: 1.365 + aResult = 1; 1.366 + break; 1.367 + 1.368 + case eIntID_ShowCaretDuringSelection: 1.369 + aResult = 0; 1.370 + break; 1.371 + 1.372 + case eIntID_SelectTextfieldsOnKeyFocus: 1.373 + // Select textfield content when focused by kbd 1.374 + // used by EventStateManager::sTextfieldSelectModel 1.375 + aResult = 1; 1.376 + break; 1.377 + 1.378 + case eIntID_SubmenuDelay: 1.379 + aResult = 200; 1.380 + break; 1.381 + 1.382 + case eIntID_TooltipDelay: 1.383 + aResult = 500; 1.384 + break; 1.385 + 1.386 + case eIntID_MenusCanOverlapOSBar: 1.387 + // we want XUL popups to be able to overlap the task bar. 1.388 + aResult = 1; 1.389 + break; 1.390 + 1.391 + case eIntID_ScrollArrowStyle: 1.392 + aResult = eScrollArrowStyle_Single; 1.393 + break; 1.394 + 1.395 + case eIntID_ScrollSliderStyle: 1.396 + aResult = eScrollThumbStyle_Proportional; 1.397 + break; 1.398 + 1.399 + case eIntID_TouchEnabled: 1.400 + aResult = 1; 1.401 + break; 1.402 + 1.403 + case eIntID_ColorPickerAvailable: 1.404 + aResult = 1; 1.405 + break; 1.406 + 1.407 + case eIntID_WindowsDefaultTheme: 1.408 + case eIntID_WindowsThemeIdentifier: 1.409 + case eIntID_OperatingSystemVersionIdentifier: 1.410 + aResult = 0; 1.411 + rv = NS_ERROR_NOT_IMPLEMENTED; 1.412 + break; 1.413 + 1.414 + case eIntID_SpellCheckerUnderlineStyle: 1.415 + aResult = NS_STYLE_TEXT_DECORATION_STYLE_WAVY; 1.416 + break; 1.417 + 1.418 + case eIntID_ScrollbarButtonAutoRepeatBehavior: 1.419 + aResult = 0; 1.420 + break; 1.421 + 1.422 + default: 1.423 + aResult = 0; 1.424 + rv = NS_ERROR_FAILURE; 1.425 + } 1.426 + 1.427 + return rv; 1.428 +} 1.429 + 1.430 +nsresult 1.431 +nsLookAndFeel::GetFloatImpl(FloatID aID, float &aResult) 1.432 +{ 1.433 + nsresult rv = nsXPLookAndFeel::GetFloatImpl(aID, aResult); 1.434 + if (NS_SUCCEEDED(rv)) 1.435 + return rv; 1.436 + rv = NS_OK; 1.437 + 1.438 + switch (aID) { 1.439 + case eFloatID_IMEUnderlineRelativeSize: 1.440 + aResult = 1.0f; 1.441 + break; 1.442 + 1.443 + case eFloatID_SpellCheckerUnderlineRelativeSize: 1.444 + aResult = 1.0f; 1.445 + break; 1.446 + 1.447 + default: 1.448 + aResult = -1.0; 1.449 + rv = NS_ERROR_FAILURE; 1.450 + break; 1.451 + } 1.452 + return rv; 1.453 +} 1.454 + 1.455 +/*virtual*/ 1.456 +bool 1.457 +nsLookAndFeel::GetFontImpl(FontID aID, nsString& aFontName, 1.458 + gfxFontStyle& aFontStyle, 1.459 + float aDevPixPerCSSPixel) 1.460 +{ 1.461 + aFontName.AssignLiteral("\"Droid Sans\""); 1.462 + aFontStyle.style = NS_FONT_STYLE_NORMAL; 1.463 + aFontStyle.weight = NS_FONT_WEIGHT_NORMAL; 1.464 + aFontStyle.stretch = NS_FONT_STRETCH_NORMAL; 1.465 + aFontStyle.size = 9.0 * 96.0f / 72.0f * aDevPixPerCSSPixel; 1.466 + aFontStyle.systemFont = true; 1.467 + return true; 1.468 +} 1.469 + 1.470 +/*virtual*/ 1.471 +bool 1.472 +nsLookAndFeel::GetEchoPasswordImpl() 1.473 +{ 1.474 + if (!mInitializedShowPassword) { 1.475 + if (XRE_GetProcessType() == GeckoProcessType_Default) { 1.476 + mShowPassword = mozilla::widget::android::GeckoAppShell::GetShowPasswordSetting(); 1.477 + } else { 1.478 + ContentChild::GetSingleton()->SendGetShowPasswordSetting(&mShowPassword); 1.479 + } 1.480 + mInitializedShowPassword = true; 1.481 + } 1.482 + return mShowPassword; 1.483 +} 1.484 + 1.485 +uint32_t 1.486 +nsLookAndFeel::GetPasswordMaskDelayImpl() 1.487 +{ 1.488 + // This value is hard-coded in Android OS's PasswordTransformationMethod.java 1.489 + return 1500; 1.490 +} 1.491 + 1.492 +/* virtual */ 1.493 +char16_t 1.494 +nsLookAndFeel::GetPasswordCharacterImpl() 1.495 +{ 1.496 + // This value is hard-coded in Android OS's PasswordTransformationMethod.java 1.497 + return UNICODE_BULLET; 1.498 +}