Creating toolbar buttons

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 23:02, 21 February 2005
Asqueella (Talk | contribs)
(looks like this doesn't contain major mistakes...)
<-- Previous diff
Revision as of 23:09, 21 February 2005
Asqueella (Talk | contribs)
(More information - link to the thread about adding a toolbar button)
Next diff -->
Line 104: Line 104:
==More information== ==More information==
-*[ How to adjust toolbarbutton's label position]+* references: [ <code><toolbarbutton></code>], [ <code><toolbaritem></code>].
-* references: [ <code><toolbarbutton></code>], [ <code><toolbaritem></code>].+* [ How to adjust toolbarbutton's label position]
 +* [ A forum thread] about adding an item to the toolbar (instead of just adding it to palette) right after an extension is installed. Note that doing this is not recommended.
[[Category:Development]] [[Category:Example code]] [[Category:Development]] [[Category:Example code]]

Revision as of 23:09, 21 February 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

This article explains the steps that need to be done in order to add a toolbar button to toolkit applications (Firefox, Thunderbird, Nvu etc.) using the overlaying mechanism (often utilized by extensions). Intended audience is XUL developers who have basic knowledge of XUL and CSS.


Creating an overlay

In order to add a toolbar button, you should create an overlay to the window that contains the toolbar you wish to enhance. Explaining overlays is not in the scope of this tutorial - you can read about overlays at XulPlanet and learn from existing extensions.

You can see the list of common URLs at the bottom of this page.

Note: Some people overlay chrome://messenger/content/mailWindowOverlay.xul. That should cause the button to appear on all windows that mailWindowOverlay.xul is applied to (i.e. Main window and View Message window). Need to check that.

Adding the toolbar button

Toolkit applications have customizable toolbars. Therefore, the common practice for extensions is to add their toolbar buttons to the toolbar palette rather than adding them directly to the toolbar. The latter is possible but is not recommended and is harder to implement.

Adding a button to the toolbar palette is very easy. Just add this code to your overlay:

<toolbarpalette id="BrowserToolbarPalette">
  <toolbarbutton id="myextension-button" class="toolbarbutton-1"
    label="&toolbarbutton.label;" tooltiptext="&toolbarbutton.tooltip;"
  • The id of palette (BrowserToolbarPalette in the example) is different for different windows. See below for the list of common palette ids.
  • class="toolbarbutton-1" makes the toolbar button appear correctly in Icons and Text mode; it also adjusts padding.
  • Put the command to be executed when the button is clicked in oncommand attribute. If you need to handle middle-click, add an onclick handler and check event.button in it.

To add more buttons, put more <toolbarbutton> elements inside the <toolbarpalette> element. Wrap other elements in <toolbaritem>.

Styling the button

Most toolbar buttons have an icon. To attach an image to the button we use standart Mozilla skinning facilities. If you're unfamiliar with it, I highly recommend you to read the skinning section of Jonah Bishop's excellent Toolbar Tutorial. It talks about slightly different thing — creating a whole custom toolbar — but it has a great explanation of the techniques we'll use.

Icon sizes

Toolbar buttons can have two different sizes — big and small. This means you'll need to provide two icons for each of your toolbar buttons. Here's the table of small and big icon sizes (in pixels) in various applications for your convenience.

Application Big icon size Small icon size
Firefox 1.0 (Winstripe) 24x24 16x16
Thunderbird 1.0 (Qute) 24x24 16x16
Nvu (Orbit?)  ?  ?

The stylesheet

To set the image for your toolbar button, use the following CSS:

/*  skin/toolbar-button.css  */

#myextension-button {
  list-style-image: url("chrome://myextension/skin/btn_large.png");

toolbar[iconsize="small"] #myextension-button {
  list-style-image: url("chrome://myextension/skin/btn_small.png");

Applying the stylesheet

Remember to attach this stylesheet to both the overlay file and customizeToolbar.xul window. To attach it to the overlay, put this PI at the top of it:

<?xml-stylesheet href="chrome://myextension/skin/toolbar-button.css" type="text/css"?>

To attach it to customizeToolbar.xul, you may use skin/contents.rdf, e.g.:

<?xml version="1.0"?>
<RDF xmlns=""

  <Seq about="urn:mozilla:skin:root">
    <li resource="urn:mozilla:skin:classic/1.0"/>

  <Description about="urn:mozilla:skin:classic/1.0">
      <Seq about="urn:mozilla:skin:classic/1.0:packages">
        <li resource="urn:mozilla:skin:classic/1.0:myextension"/>

  <Seq about="urn:mozilla:stylesheets">
    <li resource="chrome://global/content/customizeToolbar.xul"/>

  <Seq about="chrome://global/content/customizeToolbar.xul">

Common mistakes

  • Malformed or not applied stylesheet - the whole set of default buttons is painted instead your own icon on the toolbar or in the Customize Toolbars window.

Example extension

Coming soon...

The list of commonly overlayed windows with toolbars

URL Application and affected window(s) Palette id
chrome://browser/content/browser.xul Firefox - Main window BrowserToolbarPalette
chrome://messenger/content/messenger.xul Thunderbird - Main window MailToolbarPalette
chrome://messenger/content/messengercompose/messengercompose.xul Thunderbird - Compose window MsgComposeToolbarPalette
chrome://editor/content/editor.xul Nvu - Main window NvuToolbarPalette

More information