MozillaZine

Creating toolbar buttons

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 10:37, 22 August 2005
Asqueella (Talk | contribs)
(Applying the stylesheet - chrome.manifest)
<-- Previous diff
Revision as of 10:38, 22 August 2005
Asqueella (Talk | contribs)
(Adding the toolbar button)
Next diff -->
Line 20: Line 20:
</toolbarpalette></pre> </toolbarpalette></pre>
-* The <code>id</code> of palette (<code>BrowserToolbarPalette</code> in the example) is different for different windows. See below for the list of common palette ids.+* The <code>id</code> of palette (<code>BrowserToolbarPalette</code> in the example) is different for different windows. See [[#The list of commonly overlayed windows with toolbars | below]] for the list of common palette ids.
* <code>class="toolbarbutton-1"</code> makes the toolbar button appear correctly in Icons and Text mode; it also adjusts padding. * <code>class="toolbarbutton-1"</code> 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 <code>oncommand</code> attribute. If you need to handle middle-click, add an <code>onclick</code> handler and check <code>event.button</code> in it. * Put the command to be executed when the button is clicked in <code>oncommand</code> attribute. If you need to handle middle-click, add an <code>onclick</code> handler and check <code>event.button</code> in it.

Revision as of 10:38, 22 August 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.

Contents

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;"
    oncommand="MyExtension.onToolbarButtonCommand(event);"/>
</toolbarpalette>
  • 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 standard 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="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:chrome="http://www.mozilla.org/rdf/chrome#">

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

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

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

  <Seq about="chrome://global/content/customizeToolbar.xul">
    <li>chrome://myextension/skin/toolbar-button.css</li>
  </Seq>
</RDF>

Extensions for Firefox/Thunderbird 1.5 should have something like this in chrome.manifest instead:

skin	myextension	classic/1.0	skin/
style	chrome://global/content/customizeToolbar.xul	chrome://myextension/skin/toolbar-button.css

Common mistakes

  • Malformed or not applied stylesheet - the whole set of default buttons is painted instead of your own icon on the toolbar or in the Customize Toolbars window.
    • Solution: check if your stylesheet is correct, check if your contents.rdf (or chrome.manifest) is correct, make sure you didn't forget to apply the stylesheet to customizeToolbar.xul.

Example extension

redButton Error Note: The link to this page works but the XPI install is missing (404).

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