Table of Contents
This chapter will introduce some of the most important aspects of maemomm coding. These will be demonstrated with simple working example code. However, this is just a taster, so you need to look at the other chapters for more substantial information.
Your existing knowledge of C++ will help you with maemomm as it would with any library. Unless we state otherwise, you can expect maemomm classes to behave like any other C++ class, and you can expect to use your existing C++ techniques with maemomm classes.
To begin our introduction to maemomm, we'll start with the simplest program possible. This program will create an window with a label to displaying a text.
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>
/* 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("Hildon-Widgetsmm example");
/* Create Window and set it to Program */
Hildon::Window window;
program.add_window(window);
/* Add example label to window */
Gtk::Label label("Hildon::Program example");
window.add(label);
/* Begin the main application */
window.show_all();
kit.run(window);
/* Exit */
return 0;
}
We will now explain each line of the example
#include <hildon-libsmm/program.h>
All maemomm programs must include certain maemomm headers; hildon-libsmm.h and
hildon-fmmm.h
include the entire maemomm kit. This is usually not a good idea, because
it includes a megabyte or so of headers, but for simple programs, it
suffices.
The next line:
Gtk::Main kit(argc, argv);
Creates a Gtk::Main object. This is needed in all maemomm
applications. The constructor for this object initializes maemomm, and checks the
arguments passed to your application on the command line, looking for
standard options such as -display. It takes these from the argument list, leaving anything it does not
recognize for your application to parse or ignore. This ensures
that all maemomm applications accept the same set of standard arguments.
Hildon::Program program;
This will create a new Hildon::Program element. Note that you only have one Hildon::Program in your whole application and that you always get a reference to the exsting if you try to create a second.
Glib::set_application_name("Hildon-Widgetsmm example");
Set the name of the application. Maemo displays this name at the top of the screen.
The next two lines of code create and display a window. This is quite simular to what you can do in GTK(mm).
Hildon::Window window;
program.add_window(window);
Gtk::Label label("Hildon::Program example");
window.add(label);
window.show_all();
Usually you will derive you own window class from Hildon::Window and add widgets to it in the constuctor. Note that you have to add all windows to your program.
The last line shows the window and enters the maemomm main processing loop, which will finish when the window is closed.
kit.run(window);
After putting the source code in simple.cc you can compile the above program with gcc using:
g++ simple.cc -o simple `pkg-config hildon-libsmm --cflags --libs`
Note that you must surround
the pkg-config invocation with backquotes.
Backquotes cause the shell to execute the command inside them, and to use
the command's output as part of the command line.
Although we have shown the compilation command for this simple example, you really should use the automake and autoconf tools, as described in "Autoconf, Automake, Libtool", by G. V. Vaughan et al. The examples used in this book are included in the maemomm-documentation package, with appropriate build files, so we won't show the build commands in future. You'll just need to find the appropriate directory and type make.
To simplify compilation, we use pkg-config, which
is present in all (properly installed) maemomm installations. This
program 'knows' what compiler switches are needed to compile programs
that use maemomm. The --cflags option causes
pkg-config to output a list of include directories for the
compiler to look in; the --libs option requests the
list of libraries for the compiler to link with and the directories to
find them in. Try running it from your shell-prompt to see the results on your system.