Toolbars

To create an application that has a toolbar, 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 next example creates a window with a toolbar and adds some toolbar items.

Figure 5.2. Toolbar

Toolbar

Example

Source Code

File: examplewindow.h

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

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

class ExampleWindow : public Hildon::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();
  
protected:
  //Signal handlers:
  virtual void on_item_close();

  Gtk::Toolbar m_toolbar;
  Gtk::ToolButton m_tool_close;
  Gtk::ToolButton m_tool_open;
  Gtk::ToolButton m_tool_new;
  Gtk::ToolButton m_tool_save;
  Gtk::SeparatorToolItem m_tool_separator;
  Gtk::ToolItem m_tool_combo;
};

#endif //GTKMM_EXAMPLEWINDOW_H

File: examplewindow.cc

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

ExampleWindow::ExampleWindow() :
	m_tool_new(Gtk::Stock::NEW),
	m_tool_open(Gtk::Stock::OPEN),
	m_tool_save(Gtk::Stock::SAVE),
	m_tool_close(Gtk::Stock::CLOSE)
{
  // 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);
  m_tool_combo.add(*combo);
  m_tool_combo.set_expand();
    
  // Add toolbar items to toolbar
  m_toolbar.append(m_tool_new);
  m_toolbar.append(m_tool_separator);
  m_toolbar.append(m_tool_open);
  m_toolbar.append(m_tool_save);
  m_toolbar.append(m_tool_combo);
  m_toolbar.append(m_tool_close);
  
  // Add toolbar
  add_toolbar(m_toolbar);
  m_toolbar.show_all(); //Show the toolbar and all its child widgets.

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

  //TODO: This should not be necessary, but the toolbar items are not shown if we don't call show_all().
  //Strangely, it must be show_all(), and show() has no effect,
  //which is odd because show_all_children() + show() should == show_all().
  //This may be a bug in how HildonWindow implements show_all,
  //or maybe it's impossible for us to use the show_all vfunc override from show_all_children().
  //More likely, there are implementation container widgets in HildonWindow that should always be shown by default,
  //or always shown when a toolbar is present.
  //Anyway, if that's the case then let's just document it as necessary.
  //See bug #875.
  show_all();
}

ExampleWindow::~ExampleWindow()
{
}

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

File: main.cc

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

int main(int argc, char *argv[])
{
  // Initialize gtkmm:
  Gtk::Main kit(&argc, &argv);
  Hildon::init();

  osso_context_t* osso_context = osso_initialize("example", "0.0.1", TRUE /* deprecated parameter */, 0 /* Use default Glib main loop context */);
  if(!osso_context)
    std::cerr << "osso_initialize() failed." << std::endl;
  
  Glib::set_application_name("Hildon-Widgetsmm: Toolbar example");
	
  // Create Window and set it to Program
  ExampleWindow window;
  Hildon::Program::get_instance()->add_window(window);
  
  // Add example label to window
  Gtk::Label label("Hildon::Toolbar example");
  window.add(label);
  label.show();
  
  // Begin the main application
  kit.run(window);
  
  osso_deinitialize(osso_context);

  return 0;

}

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