Backed out changeset 3fe07c50c854 (bug 946316) for bustage. a=backout
[gecko.git] / widget / os2 / nsLookAndFeel.cpp
blob6532a8e100f3a9c2d13a3b8a6dee4d6fb1e0218a
1 /* -*- Mode: C++; tab-width: 2; 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 #define INCL_WIN
7 #define INCL_WINWINDOWMGR
8 #define INCL_WINSHELLDATA
9 #define INCL_DOSNLS
10 #define INCL_DOSERRORS
11 #include <os2.h>
12 #include <stdlib.h>
14 #include "nsLookAndFeel.h"
15 #include "nsFont.h"
16 #include "nsSize.h"
17 #include "nsStyleConsts.h"
19 static bool bIsDBCS;
20 static bool bIsDBCSSet = false;
22 nsLookAndFeel::nsLookAndFeel() : nsXPLookAndFeel()
26 nsLookAndFeel::~nsLookAndFeel()
30 nsresult
31 nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor)
33 nsresult res = NS_OK;
35 int idx;
36 switch (aID) {
37 case eColorID_WindowBackground:
38 idx = SYSCLR_WINDOW;
39 break;
40 case eColorID_WindowForeground:
41 idx = SYSCLR_WINDOWTEXT;
42 break;
43 case eColorID_WidgetBackground:
44 idx = SYSCLR_BUTTONMIDDLE;
45 break;
46 case eColorID_WidgetForeground:
47 idx = SYSCLR_WINDOWTEXT;
48 break;
49 case eColorID_WidgetSelectBackground:
50 idx = SYSCLR_HILITEBACKGROUND;
51 break;
52 case eColorID_WidgetSelectForeground:
53 idx = SYSCLR_HILITEFOREGROUND;
54 break;
55 case eColorID_Widget3DHighlight:
56 idx = SYSCLR_BUTTONLIGHT;
57 break;
58 case eColorID_Widget3DShadow:
59 idx = SYSCLR_BUTTONDARK;
60 break;
61 case eColorID_TextBackground:
62 idx = SYSCLR_WINDOW;
63 break;
64 case eColorID_TextForeground:
65 idx = SYSCLR_WINDOWTEXT;
66 break;
67 case eColorID_TextSelectBackground:
68 case eColorID_IMESelectedRawTextBackground:
69 case eColorID_IMESelectedConvertedTextBackground:
70 idx = SYSCLR_HILITEBACKGROUND;
71 break;
72 case eColorID_TextSelectForeground:
73 case eColorID_IMESelectedRawTextForeground:
74 case eColorID_IMESelectedConvertedTextForeground:
75 idx = SYSCLR_HILITEFOREGROUND;
76 break;
77 case eColorID_IMERawInputBackground:
78 case eColorID_IMEConvertedTextBackground:
79 aColor = NS_TRANSPARENT;
80 return NS_OK;
81 case eColorID_IMERawInputForeground:
82 case eColorID_IMEConvertedTextForeground:
83 aColor = NS_SAME_AS_FOREGROUND_COLOR;
84 return NS_OK;
85 case eColorID_IMERawInputUnderline:
86 case eColorID_IMEConvertedTextUnderline:
87 aColor = NS_SAME_AS_FOREGROUND_COLOR;
88 return NS_OK;
89 case eColorID_IMESelectedRawTextUnderline:
90 case eColorID_IMESelectedConvertedTextUnderline:
91 aColor = NS_TRANSPARENT;
92 return NS_OK;
93 case eColorID_SpellCheckerUnderline:
94 aColor = NS_RGB(0xff, 0, 0);
95 return NS_OK;
97 // New CSS 2 Color definitions
98 case eColorID_activeborder:
99 idx = SYSCLR_ACTIVEBORDER;
100 break;
101 case eColorID_activecaption:
102 idx = SYSCLR_ACTIVETITLETEXT;
103 break;
104 case eColorID_appworkspace:
105 idx = SYSCLR_APPWORKSPACE;
106 break;
107 case eColorID_background:
108 idx = SYSCLR_BACKGROUND;
109 break;
110 case eColorID_buttonface:
111 case eColorID__moz_buttonhoverface:
112 idx = SYSCLR_BUTTONMIDDLE;
113 break;
114 case eColorID_buttonhighlight:
115 idx = SYSCLR_BUTTONLIGHT;
116 break;
117 case eColorID_buttonshadow:
118 idx = SYSCLR_BUTTONDARK;
119 break;
120 case eColorID_buttontext:
121 case eColorID__moz_buttonhovertext:
122 idx = SYSCLR_MENUTEXT;
123 break;
124 case eColorID_captiontext:
125 idx = SYSCLR_WINDOWTEXT;
126 break;
127 case eColorID_graytext:
128 idx = SYSCLR_MENUDISABLEDTEXT;
129 break;
130 case eColorID_highlight:
131 case eColorID__moz_html_cellhighlight:
132 idx = SYSCLR_HILITEBACKGROUND;
133 break;
134 case eColorID_highlighttext:
135 case eColorID__moz_html_cellhighlighttext:
136 idx = SYSCLR_HILITEFOREGROUND;
137 break;
138 case eColorID_inactiveborder:
139 idx = SYSCLR_INACTIVEBORDER;
140 break;
141 case eColorID_inactivecaption:
142 idx = SYSCLR_INACTIVETITLE;
143 break;
144 case eColorID_inactivecaptiontext:
145 idx = SYSCLR_INACTIVETITLETEXT;
146 break;
147 case eColorID_infobackground:
148 aColor = NS_RGB( 255, 255, 228);
149 return res;
150 case eColorID_infotext:
151 idx = SYSCLR_WINDOWTEXT;
152 break;
153 case eColorID_menu:
154 idx = SYSCLR_MENU;
155 break;
156 case eColorID_menutext:
157 case eColorID__moz_menubartext:
158 idx = SYSCLR_MENUTEXT;
159 break;
160 case eColorID_scrollbar:
161 idx = SYSCLR_SCROLLBAR;
162 break;
163 case eColorID_threeddarkshadow:
164 idx = SYSCLR_BUTTONDARK;
165 break;
166 case eColorID_threedface:
167 idx = SYSCLR_BUTTONMIDDLE;
168 break;
169 case eColorID_threedhighlight:
170 idx = SYSCLR_BUTTONLIGHT;
171 break;
172 case eColorID_threedlightshadow:
173 idx = SYSCLR_BUTTONMIDDLE;
174 break;
175 case eColorID_threedshadow:
176 idx = SYSCLR_BUTTONDARK;
177 break;
178 case eColorID_window:
179 idx = SYSCLR_WINDOW;
180 break;
181 case eColorID_windowframe:
182 idx = SYSCLR_WINDOWFRAME;
183 break;
184 case eColorID_windowtext:
185 idx = SYSCLR_WINDOWTEXT;
186 break;
187 case eColorID__moz_eventreerow:
188 case eColorID__moz_oddtreerow:
189 case eColorID__moz_field:
190 case eColorID__moz_combobox:
191 idx = SYSCLR_ENTRYFIELD;
192 break;
193 case eColorID__moz_fieldtext:
194 case eColorID__moz_comboboxtext:
195 idx = SYSCLR_WINDOWTEXT;
196 break;
197 case eColorID__moz_dialog:
198 case eColorID__moz_cellhighlight:
199 idx = SYSCLR_DIALOGBACKGROUND;
200 break;
201 case eColorID__moz_dialogtext:
202 case eColorID__moz_cellhighlighttext:
203 idx = SYSCLR_WINDOWTEXT;
204 break;
205 case eColorID__moz_buttondefault:
206 idx = SYSCLR_BUTTONDEFAULT;
207 break;
208 case eColorID__moz_menuhover:
209 if (WinQuerySysColor(HWND_DESKTOP, SYSCLR_MENUHILITEBGND, 0) ==
210 WinQuerySysColor(HWND_DESKTOP, SYSCLR_MENU, 0)) {
211 // if this happens, we would paint menu selections unreadable
212 // (we are most likely on Warp3), so let's fake a dark grey
213 // background for the selected menu item
214 aColor = NS_RGB( 132, 130, 132);
215 return res;
216 } else {
217 idx = SYSCLR_MENUHILITEBGND;
219 break;
220 case eColorID__moz_menuhovertext:
221 case eColorID__moz_menubarhovertext:
222 if (WinQuerySysColor(HWND_DESKTOP, SYSCLR_MENUHILITEBGND, 0) ==
223 WinQuerySysColor(HWND_DESKTOP, SYSCLR_MENU, 0)) {
224 // white text to be readable on dark grey
225 aColor = NS_RGB( 255, 255, 255);
226 return res;
227 } else {
228 idx = SYSCLR_MENUHILITE;
230 break;
231 case eColorID__moz_nativehyperlinktext:
232 aColor = NS_RGB( 0, 0, 255);
233 return res;
234 default:
235 idx = SYSCLR_WINDOW;
236 break;
239 long lColor = WinQuerySysColor( HWND_DESKTOP, idx, 0);
241 int iRed = (lColor & RGB_RED) >> 16;
242 int iGreen = (lColor & RGB_GREEN) >> 8;
243 int iBlue = (lColor & RGB_BLUE);
245 aColor = NS_RGB( iRed, iGreen, iBlue);
247 return res;
250 nsresult
251 nsLookAndFeel::GetIntImpl(IntID aID, int32_t &aResult)
253 nsresult res = nsXPLookAndFeel::GetIntImpl(aID, aResult);
254 if (NS_SUCCEEDED(res))
255 return res;
256 res = NS_OK;
258 switch (aID) {
259 case eIntID_CaretBlinkTime:
260 aResult = WinQuerySysValue( HWND_DESKTOP, SV_CURSORRATE);
261 break;
262 case eIntID_CaretWidth:
263 aResult = 1;
264 break;
265 case eIntID_ShowCaretDuringSelection:
266 aResult = 0;
267 break;
268 case eIntID_SelectTextfieldsOnKeyFocus:
269 // Do not select textfield content when focused by kbd
270 // used by nsEventStateManager::sTextfieldSelectModel
271 aResult = 0;
272 break;
273 case eIntID_SubmenuDelay:
274 aResult = 300;
275 break;
276 case eIntID_TooltipDelay:
277 aResult = 500;
278 break;
279 case eIntID_MenusCanOverlapOSBar:
280 // we want XUL popups to be able to overlap the task bar.
281 aResult = 1;
282 break;
283 case eIntID_ScrollArrowStyle:
284 aResult = eScrollArrowStyle_Single;
285 break;
286 case eIntID_ScrollSliderStyle:
287 aResult = eScrollThumbStyle_Proportional;
288 break;
289 case eIntID_TreeOpenDelay:
290 aResult = 1000;
291 break;
292 case eIntID_TreeCloseDelay:
293 aResult = 0;
294 break;
295 case eIntID_TreeLazyScrollDelay:
296 aResult = 150;
297 break;
298 case eIntID_TreeScrollDelay:
299 aResult = 100;
300 break;
301 case eIntID_TreeScrollLinesMax:
302 aResult = 3;
303 break;
304 case eIntID_DWMCompositor:
305 case eIntID_WindowsClassic:
306 case eIntID_WindowsDefaultTheme:
307 case eIntID_TouchEnabled:
308 case eIntID_WindowsThemeIdentifier:
309 case eIntID_OperatingSystemVersionIdentifier:
310 aResult = 0;
311 res = NS_ERROR_NOT_IMPLEMENTED;
312 break;
313 case eIntID_MacGraphiteTheme:
314 case eIntID_MacLionTheme:
315 aResult = 0;
316 res = NS_ERROR_NOT_IMPLEMENTED;
317 break;
318 case eIntID_IMERawInputUnderlineStyle:
319 case eIntID_IMEConvertedTextUnderlineStyle:
320 aResult = NS_STYLE_TEXT_DECORATION_STYLE_SOLID;
321 break;
322 case eIntID_IMESelectedRawTextUnderlineStyle:
323 case eIntID_IMESelectedConvertedTextUnderline:
324 aResult = NS_STYLE_TEXT_DECORATION_STYLE_NONE;
325 break;
326 case eIntID_SpellCheckerUnderlineStyle:
327 aResult = NS_STYLE_TEXT_DECORATION_STYLE_WAVY;
328 break;
329 case eIntID_ScrollbarButtonAutoRepeatBehavior:
330 aResult = 0;
331 break;
332 case eIntID_SwipeAnimationEnabled:
333 aResult = 0;
334 break;
336 default:
337 aResult = 0;
338 res = NS_ERROR_FAILURE;
340 return res;
343 nsresult
344 nsLookAndFeel::GetFloatImpl(FloatID aID, float &aResult)
346 nsresult res = nsXPLookAndFeel::GetFloatImpl(aID, aResult);
347 if (NS_SUCCEEDED(res))
348 return res;
349 res = NS_OK;
351 switch (aID) {
352 case eFloatID_IMEUnderlineRelativeSize:
353 aResult = 1.0f;
354 break;
355 case eFloatID_SpellCheckerUnderlineRelativeSize:
356 aResult = 1.0f;
357 break;
358 default:
359 aResult = -1.0;
360 res = NS_ERROR_FAILURE;
362 return res;
365 /* Helper function to determine if we are running on DBCS */
366 static bool
367 IsDBCS()
369 if (!bIsDBCSSet) {
370 APIRET rc;
371 COUNTRYCODE ctrycodeInfo = {0};
372 CHAR achDBCSInfo[12] = {0}; // DBCS environmental vector
373 ctrycodeInfo.country = 0; // current country
374 ctrycodeInfo.codepage = 0; // current codepage
376 rc = DosQueryDBCSEnv(sizeof(achDBCSInfo), &ctrycodeInfo, achDBCSInfo);
377 if (rc == NO_ERROR) {
378 // DBCS countries will have at least one nonzero byte in the
379 // first four bytes of the DBCS environmental vector
380 bIsDBCS = (achDBCSInfo[0] != 0 || achDBCSInfo[1] != 0 ||
381 achDBCSInfo[2] != 0 || achDBCSInfo[3] != 0);
382 } else {
383 bIsDBCS = false;
385 bIsDBCSSet = true;
387 return bIsDBCS;
390 /* Helper function to query font from INI file */
391 static void
392 QueryFontFromINI(char* fontType, char* fontName, ULONG ulLength)
394 ULONG ulMaxNameL = ulLength;
396 // We must use PrfQueryProfileData here, because some users have
397 // binary font data in their INI files.
398 if (PrfQueryProfileData(HINI_USER,
399 (PCSZ)"PM_SystemFonts",
400 (PCSZ)fontType,
401 fontName, &ulMaxNameL)) {
402 // PrfQueryProfileData does not nul-terminate for us.
403 fontName[ulMaxNameL] = '\0';
404 } else {
405 // If there was no entry in the INI, default to something sensible.
406 // Make sure to use a DBCS-capable font in a DBCS-capable environment.
407 if (IsDBCS()) {
408 strcpy(fontName, "9.WarpSans Combined");
409 } else {
410 strcpy(fontName, "9.WarpSans");
416 * Query the font used for various CSS properties (aID) from the system.
417 * For OS/2, only very few fonts are defined in the system, so most of the IDs
418 * resolve to the same system font.
419 * The font queried will give back a string like
420 * 9.WarpSans Bold
421 * 12.Times New Roman Bold Italic
422 * 10.Times New Roman.Strikeout.Underline
423 * 20.Bitstream Vera Sans Mono Obli
424 * (always restricted to 32 chars, at least before the second dot)
425 * We use the value before the dot as the font size (in pt, and convert it to
426 * px using the screen resolution) and then try to use the rest of the string
427 * to determine the font style from it.
429 bool
430 nsLookAndFeel::GetFont(FontID aID, nsString& aFontName,
431 gfxFontStyle& aFontStyle)
433 char szFontNameSize[MAXNAMEL];
435 switch (aID)
437 case eFont_Icon:
438 QueryFontFromINI("IconText", szFontNameSize, MAXNAMEL);
439 break;
441 case eFont_Menu:
442 QueryFontFromINI("Menus", szFontNameSize, MAXNAMEL);
443 break;
445 case eFont_Caption:
446 case eFont_MessageBox:
447 case eFont_SmallCaption:
448 case eFont_StatusBar:
449 case eFont_Tooltips:
450 case eFont_Widget:
452 case eFont_Window: // css3
453 case eFont_Document:
454 case eFont_Workspace:
455 case eFont_Desktop:
456 case eFont_Info:
457 case eFont_Dialog:
458 case eFont_Button:
459 case eFont_PullDownMenu:
460 case eFont_List:
461 case eFont_Field:
462 QueryFontFromINI("WindowText", szFontNameSize, MAXNAMEL);
463 break;
465 default:
466 NS_WARNING("None of the listed font types, using WarpSans");
467 if (!IsDBCS()) {
468 strcpy(szFontNameSize, "9.WarpSans");
469 } else {
470 strcpy(szFontNameSize, "9.WarpSans Combined");
474 char *szFacename = strchr(szFontNameSize, '.');
475 if (!szFacename || (*(szFacename++) == '\0'))
476 return false;
478 // local DPI for size will be taken into account below
479 aFontStyle.size = atof(szFontNameSize);
481 // determine DPI resolution of screen device to compare compute
482 // font size in pixels
483 HPS ps = WinGetScreenPS(HWND_DESKTOP);
484 HDC dc = GpiQueryDevice(ps);
485 // effective vertical resolution in DPI
486 LONG vertScreenRes = 120; // assume 120 dpi as default
487 DevQueryCaps(dc, CAPS_VERTICAL_FONT_RES, 1, &vertScreenRes);
488 WinReleasePS(ps);
490 // now scale to make pixels from points (1 pt = 1/72in)
491 aFontStyle.size *= vertScreenRes / 72.0;
493 NS_ConvertUTF8toUTF16 fontFace(szFacename);
494 int pos = 0;
496 // this is a system font in any case
497 aFontStyle.systemFont = true;
499 // bold fonts should have " Bold" in their names, at least we hope that they
500 // do, otherwise it's bad luck
501 NS_NAMED_LITERAL_CSTRING(spcBold, " Bold");
502 if ((pos = fontFace.Find(spcBold.get(), false, 0, -1)) > -1) {
503 aFontStyle.weight = NS_FONT_WEIGHT_BOLD;
504 // strip the attribute, now that we have set it in the gfxFontStyle
505 fontFace.Cut(pos, spcBold.Length());
506 } else {
507 aFontStyle.weight = NS_FONT_WEIGHT_NORMAL;
510 // FIXME: Set aFontStyle.stretch correctly!
511 aFontStyle.stretch = NS_FONT_STRETCH_NORMAL;
513 // similar hopes for italic and oblique fonts...
514 NS_NAMED_LITERAL_CSTRING(spcItalic, " Italic");
515 NS_NAMED_LITERAL_CSTRING(spcOblique, " Oblique");
516 NS_NAMED_LITERAL_CSTRING(spcObli, " Obli");
517 if ((pos = fontFace.Find(spcItalic.get(), false, 0, -1)) > -1) {
518 aFontStyle.style = NS_FONT_STYLE_ITALIC;
519 fontFace.Cut(pos, spcItalic.Length());
520 } else if ((pos = fontFace.Find(spcOblique.get(), false, 0, -1)) > -1) {
521 // oblique fonts are rare on OS/2 and not specially supported by
522 // the GPI system, but at least we are trying...
523 aFontStyle.style = NS_FONT_STYLE_OBLIQUE;
524 fontFace.Cut(pos, spcOblique.Length());
525 } else if ((pos = fontFace.Find(spcObli.get(), false, 0, -1)) > -1) {
526 // especially oblique often gets cut by the 32 char limit to "Obli",
527 // so search for that, too (anything shorter would be ambiguous)
528 aFontStyle.style = NS_FONT_STYLE_OBLIQUE;
529 // In this case, assume that this is the last property in the line
530 // and cut off everything else, too
531 // This is needed in case it was really Obliq or Obliqu...
532 fontFace.Cut(pos, fontFace.Length());
533 } else {
534 aFontStyle.style = NS_FONT_STYLE_NORMAL;
537 // just throw away any modifiers that are separated by dots (which are either
538 // .Strikeout, .Underline, or .Outline, none of which have a corresponding
539 // gfxFont property)
540 if ((pos = fontFace.Find(".", false, 0, -1)) > -1) {
541 fontFace.Cut(pos, fontFace.Length());
544 // seems like we need quotes around the font name
545 NS_NAMED_LITERAL_STRING(quote, "\"");
546 aFontName = quote + fontFace + quote;
548 return true;