How to create a GUI(Graphical User Interface) using C programming Language.. (part 3)

17 08 2011

Hi, this is the third part of the How to create a GUI(Graphical User Interface) using C programming Language.. (part 2) post as I promised. In today’s post I’m going to tell you how to create and run Simple hello world GUI program.

And it includes how display a message when click a button.


Ok now open a new glade project and draw a GUI like above image. You have three components one label and two buttons.

Then set properties like this.

  • For the main Window

General–> Name= mainWindow
General–>Resizable= No
Common–>Height request = 400
Common–>Width request=200

  • For Display Label

General–>Name=displayLabel

General–>Label=Display

  • For Display Button

General–>Name=displayButton

General–>Label=Display

Signals–>Clicked=on_displayButton_clicked

  • For Exit Button

General–>Name=exitButton

General–>Label=Exit

Signals–>Clicked=on_exitButton_clicked

Then save it as hello.glade in libglade format.

Now open CodeBlock’s Gtk+ project. When you open a new it will automatically generates some codes, so erase and clear main.c .Then copy paste this code into your main.c .

Then copy your hello.glade file into CodeBlock project folder.

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

/*
Author : Gihan De Silva
gihansblog.com
*/

GladeXML *xml;
GtkWidget *widget;
GtkWidget *display;

G_MODULE_EXPORT void on_displayButton_clicked(GtkButton *button,gpointer *data)
{
/* Find the Glade XML tree containing widget. */
xml = glade_get_widget_tree(GTK_WIDGET( widget ));

/* Pull the widgets out of the tree */
display= glade_xml_get_widget(xml, “displayLabel”);

gtk_label_set_text(GTK_LABEL(display),”Hello World!\n gihansblog.com”);
}

G_MODULE_EXPORT void on_exitButton_clicked(GtkButton *button,gpointer *data)
{
gtk_main_quit();
}

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

gtk_init(&argc, &argv);

/*import glade file*/
xml = glade_xml_new(“hello.glade”, NULL, NULL);

/* get a widget (useful if you want to change something) */
widget = glade_xml_get_widget(xml, “mainWindow”);

/* connect signal handlers */
glade_xml_signal_autoconnect(xml);

/*show widget*/
gtk_widget_show (widget);

gtk_main();

return 0;
}

Now run the project. If everything ok it will look like this and will function well :D.

The program in CodeBlocks…

Main Code Explained…

First we should initialise gtk in our code.

gtk_init(&argc, &argv);

Then we have to import our .glade file into our program and convert it into xml file format.

xml = glade_xml_new(“hello.glade”, NULL, NULL);

Now signals of widgets should be functioning with this line.

glade_xml_signal_autoconnect(xml);

And with this line, it will show the GUI at run time.

gtk_widget_show (widget);

Then call gtk main method

gtk_main();

Exit Button Code Explained…

In the exit button we call
gtk_main_quit();

to quit fro the program.

Display Button Code Explained…

Then find the Glade XML tree containing widget.
 xml = glade_get_widget_tree(GTK_WIDGET( widget ));

Now pull the label widgets out of the tree
 display= glade_xml_get_widget(xml, “displayLabel”);

Display the message on the label
gtk_label_set_text(GTK_LABEL(display),”Hello World!\n gihansblog.com”);

Ok that’s all for today :D. If you want, you can DOWNLOAD my CodeBlock project here!. In next post I will show you how to add a Text Entry widget to your application.

Thank you

Gihan De Silva





Moving to a new domain name… gihansblog.com

28 07 2011

Hi, A little Announcement for all the readers of Gihan’s Blog. That is gihansblog.wordpress.com moving to gihansblog.com :D. Now it’s been around two months that I’ve started this blog. There are lot of people who give me strength to carry on this blog by commenting, discussing and saying ‘Hi’.And I appreciate all those things.

I got this domian name form GoDaddy.com. And I forwarded it into my old wordpress domain name through their forwarding facilities.  And anyone interesting on more info about getting a domain, masking ,forwarding or anything regarding this , I’d like to share my experience. 😀

Now you can find my Blog on http://gihansblog.com/ . 😀

Thank you

Gihan Malan De Silva @ gihansblog.com/





Java 7 Released… (watch the video)

19 07 2011

Good and very Important News for all. That is Java 7 had released on 7th July 2011. Ceremony speech was given by Adam Messinger, vice president of development Fusion Middleware. According to dream team of Java , in the new version of Java has a big different comparing than the other versions of Java. And this is the first release of Java with Oracle company.And they has made it as MOVING JAVA FORWARD!

With Oracle, Java’s future  has goes with  the  new technology, the community, the Java Community Process (JCP), and the entire ecosystem focused on moving Java forward.

And they has categorised new Technical Breakout of Java 7

  1. Making Heads and Tails of Project Coin, Small Language Changes in JDK 7
  2. Divide and Conquer Parallelism with the Fork/Join Framework
  3. The New File System API in JDK 7
  4. A Renaissance VM: One Platform, Many Languages

Java 7 Released… from Gihan on Vimeo.

Just watch it. It’s worth to watch…

Thanks

Gihan Malan De Silva