views:

316

answers:

2

Is there any way to detect which windows XP theme is in use?

I suspect that there is no specific api call you can make, but you may be able to figure it out by checking something on some DOM element, ie feature detection.

Another question: does the classic theme even exist on windows vista or windows 7?

edit - this is my solution:

function isXpTheme() {
  var rgb;
  var map = { "rgb(212,208,200)" : false,
              "rgb(236,233,216)" : true };
  var $elem = $("<button>");
  $elem.css("backgroundColor", "ButtonFace");
  $("body").append($elem);
  var elem = $elem.get(0);
  if (document.defaultView && document.defaultView.getComputedStyle) {
    s = document.defaultView.getComputedStyle(elem, "");
    rgb = s && s.getPropertyValue("background-color");
  } else if (elem.currentStyle) {
    rgb = (function (el) { // get a rgb based color on IE
    var oRG =document.body.createTextRange();
    oRG.moveToElementText(el);
    var iClr=oRG.queryCommandValue("BackColor");
      return "rgb("+(iClr & 0xFF)+","+((iClr & 0xFF00)>>8)+","+
                  ((iClr & 0xFF0000)>>16)+")";
    })(elem);
  } else if (elem.style["backgroundColor"]) {
    rgb = elem.style["backgroundColor"];
  } else  {
    rgb = null;
  }
  $elem.remove();
  rgb = rgb.replace(/[ ]+/g,"")
  if(rgb){;
    return map[rgb];
  }
}

Next step is to figure out what this function returns on non-xp machines and/or figure out how to detect windows boxes. I have tested this in windows XP only, so vista and windows 7 might give different color values, it should be easy to add though.

Here is a demo page of this in action:

http://programmingdrunk.com/current-projects/isXpTheme/

+5  A: 

Interesting question. The only thing that comes to mind is checking the size of a default button. It is styled differently in both themes, and I suppose it has a different size. This could be half-way reliable if you give the button a fixed text size.

I'll start the XP virtual machine and check whether the sizes actually differ.

Update: They do differ.

Google "I'm feeling lucky" button

  • in classic skin: 99 x 23.75 (sic!) pixels
  • in XP skin: 97 x 21.75 pixels

A second, less reliable approach that comes to mind is giving an element a CSS system colour and then parsing the resulting computed colour. In classic mode, the ButtonFace property will have a specific shade of grey and I think a different one in the default skin. Again, would have to be tested.

Update: they differ, too.

ButtonFace CSS system colour

  • in Windows classic skin: #D4D0C8
  • in XP skin: #ECE9D8

Obviously, both approaches will break if the user did any customization to colours and/or font sizes. The font size approach is the more reliable IMO, as there are fewer people playing around with that.

You would, of course, have to have comparison tables for all Windows generations, as presumably, the values for classic and default skin will differ.

Pekka
I think the less reliable way may be more reliable, since the button may already be styled by css, so when you create it, you will not get the default size. Ill try both ways though.
mkoryak
@mkoryak I added my test results.
Pekka
@mkoryak if you force a `font-size` on a specific button and reset all the other attributes (height, padding, etc.) you should be able to get reliable results I think. You'd have to reset a lot to be sure, though.
Pekka
can you add the code that you used to get the computed color. i am looking for that but not finding much that is cross browser. Thanks
mkoryak
@mkoryak I didn't - I used Firebug. But hang on, I think I know a SO question that'll help.
Pekka
@mkoryak Here is a `getComputedStyle` function that functions well cross browser and should work: http://stackoverflow.com/questions/1955048/get-computed-font-size-for-dom-element-in-js
Pekka
Ah, i see. unfortunatly that method doesnt work in IE. it returns "buttonFace" not a useable value. see for yourself: http://programmingdrunk.com/current-projects/dropdownReplacement/
mkoryak
@mkoryak crap! Is it the same when you try `style.backgroundColor` directly? Hmm, it probably is.... Too bad! :(
Pekka
yeah it is too bad. i guess ill try to button method, and first reset all size related css styles on it. Thanks for your help anyway, you put me on the right track
mkoryak
@mkoryak you're welcome. I'd be interested to hear whether it worked out for you, updates welcome.
Pekka
@Pekka - updated question with solution
mkoryak
A: 

just to give a starting point look for IsThemeActive()

luca
err, thats looks like C++ I was talking about javascript here.
mkoryak