Chapter 5. Widgets

Table of Contents

This sections explains the additional widgets provided by the Maemo framework. Most widgets of GTKmm are of course also availible.

Menus

Menus in maemomm use GTKmm menu functions with the exception that the menu is attached to the Hildon::Program/Window and appears in the title bar. A Gtk::Menu can be created and attached to the Hildon::Program to be used as a common menu for all Hildon::Windows that don't have their own menu. Another way is to use a Gtk::Menu in a Hildon::Window. In this way every application window can have a different menu. GTKmm menus can be made both manually and using Gtk::UIManager. The Gtk::UIManager is a new action-based method to create menus and toolbars using an XML description. This tutorial introduces only manual creation, you can read more about Gtk::UIManager at the GTKmm tutorial.

The next example creates a menu with a submenu. This submenu contains radio menu items and check menu item, all of which can be toggled. The last item in the menu (Close) is attached to a callback fuctions so that the application can be closed also from the menu.

/* In constuctor of window */
set_menu(main);

This is the only line really different from Gtkmm. Sets the menu of the window to a menu created before.

Example

This is the first example that shows how to derive your own window class from Hildon::Window:

examplewindow.h
//$Id: examplewindow.h,v 1.1 2003/11/03 09:29:56 murrayc Exp $ -*- c++ -*-

/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <hildon-libsmm/window.h>

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

protected:
  //Signal handlers:
  virtual void on_close();

  Gtk::Menu main;
  Gtk::Menu sub_others;
  
  Gtk::MenuItem item_others;
  Gtk::RadioMenuItem::Group group;
  Gtk::RadioMenuItem item_radio1;
  Gtk::RadioMenuItem item_radio2;
    
  Gtk::CheckMenuItem item_check;
  Gtk::CheckMenuItem item_close;
  Gtk::SeparatorMenuItem item_separator;
};

#endif //GTKMM_EXAMPLEWINDOW_H

examplewindow.cc
//$Id: examplewindow.cc,v 1.5 2004/03/25 14:35:34 murrayc Exp $ -*- c++ -*-

/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

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

ExampleWindow::ExampleWindow() :
	item_others("Others"),
    item_radio1(group, "Radio1"),
    item_radio2(group, "Radio2"),  
    item_check("Check"),
    item_close("Close")
{    
    /* Add menu items to right menus */
    main.append(item_others);
   	sub_others.append(item_radio1);
    sub_others.append(item_radio2);
   	sub_others.append(item_separator);
    sub_others.append(item_check);
    main.append(item_close);
	main.show_all();

    /* Add others submenu to the "Others" item */
    item_others.set_submenu(sub_others);
    set_menu(main);

    /* Attach the callback functions to the activate signal */
	item_close.signal_activate().connect( sigc::mem_fun(*this, &ExampleWindow::on_close) );
	
    /* Make all menu widgets visible */
    show_all();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_close()
{
	Gtk::Main::quit();
}


main.cc
//$Id: main.cc,v 1.1 2003/11/03 09:29:56 murrayc Exp $ -*- c++ -*-

/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <hildon-libsmm/program.h>
#include <gtkmm/main.h>

#include "examplewindow.h"

/* This is the C++ version of the example in the tutorial */

int main(int argc, char *argv[])
{
    /* Initialize the GTKmm. */
    Gtk::Main kit(&argc, &argv);

    /* Create the hildon program and setup the title */
   	Hildon::Program program;
    
   Glib::set_application_name("Menu example");

    /* Create Window and set it to Program */
    ExampleWindow window;
	program.add_window(window);

	/* Add example label to window */
    Gtk::Label label("Hildon::Menu example");
	window.add(label);

    /* Begin the main application */
    window.show_all();
    kit.run(window);
	
    /* Exit */
    return 0;

}

The documentation for the standard GTKmm menu handling can be found in the GTKmm tutorial.