Creating toolbar buttons: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (recategorizing)
Line 87: Line 87:


==Example extension==
==Example extension==
''Coming soon...''
[installE({redButton: {URL: "http://ihoss.not-a-blog.com/extensions/redbutton1_0.xpi", IconURL: "http://ihoss.not-a-blog.com/images/large.gif"}}); redButton]


==The list of commonly overlayed windows with toolbars==
==The list of commonly overlayed windows with toolbars==

Revision as of 12:08, 3 May 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;"
    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 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="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>

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

[installE({redButton: {URL: "http://ihoss.not-a-blog.com/extensions/redbutton1_0.xpi", IconURL: "http://ihoss.not-a-blog.com/images/large.gif"}}); redButton]

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