GtkApplicationInNetBeans

GTK+ Application in NetBeans 6.0



NetBeans IDE

The NetBeans IDE (Integrated Development Environment) is one of the top Java IDEs. NetBeans IDE was first a student project at Charles University in Prague. The project was bought by Sun in 1999. In 2000, Sun open-sourced Netbeans. Since then, NetBeans grows in usability, stability and features. Thousands of developers around the world use Netbeans. Netbeans was created as a Java IDE. It seems that NetBeans IDE is moving towards a more versatile IDE. Except for the three java platforms, Mobile, Desktop and Enterprise, NetBeans provides tools for creating C/C++ and Ruby projects.

Netbeans is written in Java with a Swing GUI. This combination is ideal for a multiplatform, multilanguage IDE.

System requirements

To comfortly use NetBeans IDE 6, we need enough memory. Using Netbeans is OK, unless we have little memory. 512 MB is the minimum requirement. We must not use other memory hungry applications, when we run NetBeans. If we use more than 100% of our memory the system will do a lot of swapping. The performance of NetBeans will then degrade.

For Ubuntu Linux, minimal system requirements are: 800 MHz processor, 512 MB memory, 650 MB free disk space. Recommended system requirements are: 2.6 GHz processor, 1 GB memory and 850 free disk space.

The IDE

The following screenshot shows the Netbeans IDE with the traditional metal look and feel.


http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/netbeans_GtkApplicationInNetBeans.png
Figure: Netbeans IDE


The Netbeans IDE is divided into several parts, called windows. These are probably the most common windows:


  1. Projects window
  2. Source editor window
  3. Output window


In the projects window, we create and manage our software projects. In the source editor window, we edit our source,

metafile, makefile and other editable files. The Netbeans IDE uses the output window for showing various messages, 

typically build and run messages.

Project groups

The Netbeans IDE 6.0 can be used to create projects in Java, Ruby and C/C++. It is a good idea to separate those projects into specific project groups, if we have more than one language pack installed.

To manage project groups, we right click on the projects window, and select Project groups option. Here we can create, rename or remove project groups. Here we can also select the active visible project group.

http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/projectgroups_GtkApplicationInNetBeans.png

Figure: Project groups



Creating a C/C++ project

There are several ways, how we can create a C/C++ project. From the File menu, we select New Project, or use the Ctrl+Shift+N shortcut.

We can also create the project by using the context menu: Right-click on the project window and select New Project from the popup window.
The fastest way is to click on the new project button on the edit toolbar.



http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/edittoolbar_GtkApplicationInNetBeans.png

Figure: The edit toolbar

Next we do two steps. Choose a type of a project and then provide a name and location for the project. From the Categories box, we select C/C++, from the Projects box, C/C++ Application.

http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/chooseproject_GtkApplicationInNetBeans.png

Figure: Choose project

In the next dialog window, we provide a name and location for our C/C++ project. There is also a check box which is checked by default. It means, that by default the new project is set to be the main project. We can have several projects opened in Netbeans. If we click on the build, debug or run button,

this is applied to the current main project in the IDE. We can set, unset a main project from the File menu, or by right 

clicking on the project icon in the projects window.
http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/namelocation_GtkApplicationInNetBeans.png
Figure: Name and location

Simple GTK+ application

In the next example, we will create a simple GTK+ application in the C language.

In order to create even a simple GTK+ application, we must use quite a few include files and libraries.

Fortunately, there is one handy tool called pkg-config, which will greatly help us setting up project settings.

The pgk-config returns metadata about installed libraries. Simply put, if we want to use a specific library, it will provide us with the necessary dependent libraries and include files. The pkg-config program retrieves information about packages from special metadata files.

$ pwd
/usr/lib/pkgconfig
$ cat cairo.pc
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: cairo
Description: Multi-platform 2D graphics library
Version: 1.4.2

Requires.private: freetype2 >= 8.0.2 fontconfig libpng12 xrender >= 0.6 x11 
Libs: -L${libdir} -lcairo
Libs.private: -lz -lm
Cflags: -I${includedir}/cairo

Here is the metadata file for the cairo library, a multiplatform 2D graphics library. In order to use the cairo library, we need freetype2, fontconfig, libpng12, xrender and x11 libraries.

$ pkg-config --cflags gtk+-2.0
-I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 
-I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 
-I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng12

By providing the --cflags option, we get all necessary include files for a GTK+ 2.0 application.

$ pkg-config --libs gtk+-2.0
-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 
-lfontconfig -lXext -lXrender -lXinerama -lXi -lXrandr 
-lXcursor -lXfixes -lpango-1.0 -lcairo -lX11 -lgobject-2.0 
-lgmodule-2.0 -ldl -lglib-2.0 

Similarly, we get all libraries with the --libs option. Notice that we might not need all libs. The math library (-lm) is not needed, unless we do some mathematical computations.

Now we will continue by creating a source file for our GTK+ application. Create a new file: From Categories, select C Files, from File Types select Empty C File. Provide a name and location for the file.

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

/* 
 * simple gtk application 
 * author jan bodnar
 * date february 17, 2008
 */

int main(int argc, char** argv) {

    GtkWidget *window;
    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
    gtk_window_set_title(GTK_WINDOW(window), "gtkapp");
    gtk_widget_show(window);

    g_signal_connect(window, "destroy",
		    G_CALLBACK (gtk_main_quit), NULL);
 
    gtk_main();

    return (EXIT_SUCCESS);
}

We create a small window in GTK+. Before we can build and run the application, we must set up our project settings. We must specify the include files and libraries, that are needed to build the application.

To provide necessary include directories, right-click on the project icon and select Properties. From the Categories box, select C/C++, C Compiler, General node. Click on the button near Include Directories.

Add necessary paths to the include directories. From the pkg-config tool, we got the list of the directories. 



http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/includedirs_GtkApplicationInNetBeans.png

Figure: Include directories

To provide the libraries, we select Linker, Libraries node. For our appliation, we need to add only the /usr/lib/libgtk-x11-2.0.so dynamic library.

Finally, we press F6 to build and run the project.

http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/includedirs_GtkApplicationInNetBeans.png

Figure: GTK+ application

Final word

NetBeans IDE provides excellent tools for C/C++ development that makes the programming more efficient.

UNIX/Linux provides a plethora of development tools for C/C++ projects. 

Some developers prefer the traditional Vi/Emacs/command line tools. Some prefer modern IDEs.

When creating large projects, IDEs like NetBeans gain momentum. NetBeans provides lots of features like code completion, code folding, syntax highlighting, code templates, integrated compiler and debugger. Just to name a few. Especially, it is much easier for a human to cope with multiple files when using a GUI, than with command line tools or traditional non-GUI editors. Finally, choosing the tool for programming has always been a matter of subjective preference. I recommend at least trying this excellent application.

http://wiki.netbeans.org/attach/GtkApplicationInNetBeans/codecompletion_GtkApplicationInNetBeans.png


Figure: Code completion in action

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo