Sunday, February 13, 2011

GTK+, Hello World

It is not really a hello world application, but since it is my first experience with GTK+ I've decided to use that "familiar" expresion in the title of this post. Here you have the code:

main.c

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


/*
* Function launching the View
*/

void
initUI(int argc, char** argv);

/*
* Close window signal handler
*/

void
destroy(GtkWidget * widget,gpointer data);

/*
* Login Callback
*/

void
login(GtkWidget * widget,gpointer data);

/*
* Application's entry point
*/


int
main(int argc, char**argv){
g_print("INIT APPLICATION\n");
initUI(argc,argv);
return
0;
}


void
initUI(int argc, char** argv){

// Initialize GTK
gtk_init(&argc,&argv);

// Create the main window Widget
GtkWidget * mainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);

// Set the window title, default size and position (Centered)
gtk_window_set_title(GTK_WINDOW(mainWindow),"Login");
gtk_window_set_default_size(GTK_WINDOW(mainWindow),250,100);
gtk_window_set_position(GTK_WINDOW(mainWindow),GTK_WIN_POS_CENTER);

/* When resize is off some of the properties are interpreted in weird ways */

//gtk_window_set_resizable(GTK_WINDOW(mainWindow),FALSE);

// Set the destroy signal handler, that will be called when the
// window is closed
g_signal_connect(G_OBJECT(mainWindow),"destroy",G_CALLBACK(destroy),NULL);

// Now is time to add some panels to layout the content in the window
GtkWidget * topVPanel = gtk_vbox_new(FALSE,10);
GtkWidget * buttonsHPanel = gtk_hbox_new(FALSE,0);
GtkWidget * topHPanel = gtk_hbox_new(FALSE,20);
GtkWidget * labelsVPanel = gtk_vbox_new(FALSE,0);
GtkWidget * textsVPanel = gtk_vbox_new(FALSE,0);

// Now let's link the panels to each other
gtk_box_pack_start(GTK_BOX(topVPanel),topHPanel, FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(topVPanel),buttonsHPanel,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(topHPanel),labelsVPanel,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(topHPanel),textsVPanel,FALSE,FALSE,0);

// Declare and add the buttons
GtkWidget * loginButton = gtk_button_new();
GtkWidget * cancelButton = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(loginButton),"Login");
gtk_button_set_label(GTK_BUTTON(cancelButton),"Cancel");

// Link the login button with the login signal handler
g_signal_connect(G_OBJECT(loginButton),"clicked",G_CALLBACK(login),NULL);
g_signal_connect(G_OBJECT(cancelButton),"clicked",G_CALLBACK(destroy),NULL);
gtk_box_pack_end(GTK_BOX(buttonsHPanel),cancelButton,FALSE,FALSE,0);
gtk_box_pack_end(GTK_BOX(buttonsHPanel),loginButton,FALSE,FALSE,0);

// Declare and add the labels
GtkWidget * userLabel = gtk_label_new("User");
GtkWidget * passLabel = gtk_label_new("Password");
gtk_label_set_width_chars(GTK_LABEL(userLabel),10);
gtk_label_set_width_chars(GTK_LABEL(passLabel),10);
gtk_misc_set_alignment(GTK_MISC(userLabel),0,0);
gtk_misc_set_alignment(GTK_MISC(passLabel),0,0);
gtk_box_pack_start(GTK_BOX(labelsVPanel),userLabel,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(labelsVPanel),passLabel,TRUE,TRUE,0);


// Declare and add the text boxes
GtkWidget * userText = gtk_entry_new();
GtkWidget * passText = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(textsVPanel),userText,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(textsVPanel),passText,TRUE,TRUE,0);

// Add the top box to the window
gtk_container_add(GTK_CONTAINER(mainWindow),topVPanel);

gtk_widget_show(topVPanel);
gtk_widget_show(buttonsHPanel);
gtk_widget_show(topHPanel);
gtk_widget_show(labelsVPanel);
gtk_widget_show(textsVPanel);
gtk_widget_show(loginButton);
gtk_widget_show(cancelButton);
gtk_widget_show(userLabel);
gtk_widget_show(passLabel);
gtk_widget_show(userText);
gtk_widget_show(passText);
gtk_widget_show(mainWindow);
//Init the GTK loop
gtk_main();
}



void
destroy(GtkWidget * widget,gpointer data){
gtk_main_quit();
}


void
login(GtkWidget * widget,gpointer data){
// Create the main window Widget
GtkWidget * messageWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget * label = gtk_label_new("Trying to log in the system");

// Set the window title
gtk_window_set_title(GTK_WINDOW(messageWindow),"MESSAGE");
gtk_window_set_default_size(GTK_WINDOW(messageWindow),200,50);
gtk_window_set_modal(GTK_WINDOW(messageWindow),TRUE);
gtk_window_set_position(GTK_WINDOW(messageWindow),GTK_WIN_POS_CENTER);

//gtk_window_set_resizable(GTK_WINDOW(messageWindow),FALSE);
gtk_container_add(GTK_CONTAINER(messageWindow),label);
gtk_widget_show(label);
gtk_widget_show(messageWindow);
}

Compile with:

>>gcc -o a.out main.c `pkg-config --libs --cflags gtk+-2.0`

Sunday, February 06, 2011

Online rock radio (Air Mackay)

Hi folks,

check this out:

http://www.shoutcast.com/Internet-Radio/air%20mackay

fantastic 70's and 80's rock.

Enjoy!!!