Toolbars

To create an application that has a toolbar, first create a normal Hildon::Window and then a normal Gtk::Toolbar. The main difference compared to normal GTK+/gtkmm usage is that toolbars can be common to all windows in an application or they can be used only in a particular Hildon::Window.

Hildon::Window Reference

Gtk::Toolbar Reference

The documentation for the standard gtkmm toolbar API can be found in the gtkmm tutorial.

The next example creates a window with a toolbar and adds some toolbar items.

Figure 4.17. Toolbar

Toolbar

Example

Source Code

File: examplewindow.h

#ifndef _MAEMOMM_EXAMPLEWINDOW_H
#define _MAEMOMM_EXAMPLEWINDOW_H

#include <hildonmm/window.h>
#include <gtkmm.h>

class ExampleWindow : public Hildon::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

private:
  // Signal handlers:
  void on_item_close();

  // Child widgets:
  Gtk::Toolbar toolbar_;
  Gtk::ToolButton tool_new_;
  Gtk::ToolButton tool_open_;
  Gtk::ToolButton tool_save_;
  Gtk::ToolButton tool_close_;
  Gtk::SeparatorToolItem tool_separator_;
  Gtk::ToolItem tool_combo_;
};

#endif /* _MAEMOMM_EXAMPLEWINDOW_H */

File: main.cc

#include <hildonmm.h>
#include <gtkmm.h>
#include "examplewindow.h"

int main(int argc, char *argv[])
{
  // Initialize gtkmm:
  Gtk::Main kit(&argc, &argv);
  Hildon::init();
  
  // Create Window and set it to Program.
  ExampleWindow window;
  Hildon::Program::get_instance()->add_window(window);
  
  // Begin the main application.
  kit.run(window);

  return 0;

}

File: examplewindow.cc

#include "examplewindow.h"
#include <gtkmm.h>
#include <iostream>

ExampleWindow::ExampleWindow() :
  tool_new_(Gtk::Stock::NEW),
  tool_open_(Gtk::Stock::OPEN),
  tool_save_(Gtk::Stock::SAVE),
  tool_close_(Gtk::Stock::CLOSE)
{
  set_title("Toolbar Example");

  // Create Combobox on tool item.
  Gtk::ComboBoxText* combo = Gtk::manage(new Gtk::ComboBoxText);
  combo->append_text("Entry 1");
  combo->append_text("Entry 2");
  combo->append_text("Entry 3");
  combo->set_active(1);
  tool_combo_.add(*combo);
  tool_combo_.set_expand();
    
  // Add toolbar items to toolbar.
  toolbar_.append(tool_new_);
  toolbar_.append(tool_separator_);
  toolbar_.append(tool_open_);
  toolbar_.append(tool_save_);
  toolbar_.append(tool_combo_);
  toolbar_.append(tool_close_);
  
  // Add toolbar.
  add_toolbar(toolbar_);
  toolbar_.show_all(); //Show the toolbar and all its child widgets.

  // Attach the callback functions to the activate signal.
  tool_close_.signal_clicked().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_item_close));
  
  // Make all child widgets visible
  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_item_close()
{
  hide();
}