eBlogzilla

Saturday, October 4, 2008

How to add images to menuitem in button.

Lets assume that we have button with menupopup:
<button type="menu" label="Main">
<menupopup>
<menuitem id="miHomeId" label="Home Page"/>
<menuitem id="miAboutId" label="About"/>
<menuitem id="miContactId" label="Contact"/>
</menupopup>
</button>
It will look something like this:



And we need to add different icons to each menuitem. The first idea is to use list-style-image attribute.
#miHomeId {
list-style-image: url("chrome://toolbar/skin/Home.PNG");
}
It is really strange, but it doesn't work. I've tried to find some examples where this problem is solved. But I have found nothing. So, I've found one interesting workaround:
<button type="menu" label="Main">
<menupopup>
<menuitem id="miHomeId">
<img src="chrome://toolbar/skin/Home.PNG" id="imgHomeId"/>
<label value="Home Page"/>
</menuitem>
<menuitem id="miAboutId">
<img src="chrome://toolbar/skin/About.PNG" id="imgAboutId"/>
<label value="About"/>
</menuitem>
<menuitem id="miContactId">
<img src="chrome://toolbar/skin/Contact.PNG" id="imgContactId"/>
<label value="Contact"/>
</menuitem>
</menupopup>
</button>
And you just need to specify your images in CSS:
#imgHomeId {
list-style-image: url("chrome://toolbar/skin/Home.PNG");
}
#imgAboutId {
list-style-image: url("chrome://toolbar/skin/About.PNG");
}
#imgContactId {
list-style-image: url("chrome://toolbar/skin/Contact.PNG");
}
Here is the result:

3 comments:

StingBlah said...

hey! I really liked this post, as I am trying to learn how to make a firefox extension.

I have experience in BASIC and Python, but none whatsoever in JAVA or C++. What would you suggest I learn first in order to create an extension? How long do you think this process will take? I have around 1 hour/day to spend studying the languages...

devunion said...

You need to learn JavaScript, XUL, CSS and HTML. Most of things can be implemented without knowledge of C++.

Your learning time depends on your experience in programming at all. For experienced programmer it will be enough one day of studying. For less experienced person it will take more time.

Anyway, welcome to FF developers community. I hope that my blog will help you.

Nils Maier said...

Better use class="menuitem-iconic" (which will bind the item to another XBL binding).
Then you may style using list-style-image and even -moz-image-region. Or advanced selectors like menuitem#someid[disabled]

See: menuitem - MDC. There are other special classes for other XUL elements as well (or better for other platform XBL bindings).