- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

Showing posts with label Gnome. Show all posts

It's really itchy to have menu list cover half of your screen, they're way too biggggg!!!
I've googled to find the solution but it's like impossible @@!
Finally, I got the answer. This is how-to make it better look for you.

Choose a theme to use in order to edit that theme, example, I use 'Is Ora Smooth'.
Then I locate to its settings at: /usr/share/themes/Is Ora Smooth/gtk-2.0/gtkrc
Run gEdit or any text editor under root permission to edit this file.

Add this option into the file:

gtk-icon-sizes = "panel-menu=16,16"

I'm not sure if you want to make it smaller but the size is often like 16, 24 or 32.
Choose the one that best fits for you.
Logout and apply that theme, you'll see how beautiful it is!

Download Project

We do the same thing with Lesson 1 but this time we use form for better GUI.
This is the source code, self-explained fully-commented.



using System;
using Gtk;

public class Lesson_2
{

public static void Main() {

Application.Init();

// create a window
Window window = new Window("Lesson 2: Say hello with Gtk# Form");
window.Resize(400, 40);

// create a label
Label label = new Label("Hello Gtk# .NET");
// add label to the form
window.Add(label);
// show form and its widgets
window.ShowAll();

Application.Run();
}
}


Build and run, this is result:


Have fun!

Now, I will introduce you a very, very popular widget that is used most of the time, GtkMessageDialog.

As you use software, sometimes it will pops up a small dialog that contains message which can be in types of either about Information, or Warning, or Question, or Error.
Like this:

Under Windows, it's known as MessageBox, right? You might be familiar with this Message Box already.

Let's look over its documentation.
Reference GtkMessageDialog

First of all, in the inherited hierarchy, GtkMessageDialog is from GtkDialog, so it certainly contains all the properties and accessing methods of GtkDialog. Simply, it's a pre-decorated dialog.

Secondly, these are the properties that you need to consider when creating a MessageDialog:
[+] Title: inherited from GtkWindow
[+] Dialog Flags: either GTK_DIALOG_MODAL or GTK_DIALOG_DESTROY_WITH_PARENT
[+] Message Type: is one of the four: GTK_MESSAGE_INFO, GTK_MESSAGE_WARNING, GTK_MESSAGE_QUESTION, GTK_MESSAGE_ERROR
[+] Button Type: either GTK_BUTTONS_*{ NONE, OK, CLOSE, CANCEL, YES_NO, OK_CANCEL } or you can create and add your own button by gtk_dialog_add_buttons().
[+] Secondary Text: is the detail info what you want to describe.

[!] 'Til now, since I haven't introduced about GtkDialog, but just stick with what I mention below:
- use gtk_dialog_run() to run the message dialog.
- gtk_dialog_run() return a response signal, therefore, always try to handle the signal.
- always use gtk_widget_destroy() to close message dialog, to avoid memory leak.
Some reference you need to read about what I mentioned:
Reference gtk_dialog_run()
Reference GtkResponseType

So far, it's enough for you at the present.

Practice: write a Gtk+ application that contains 4 buttons that will emits different types of GtkMessageDialog.


Try to read documentation and what I've mentioned so far then make your code. Make it simple and clear.

Below is my source for reference:


/*
* @author [JaPh]
* @date 15 August, 2009
* @file GtkMessageDialog_01.c
* @site http://xjaphx.blogspot.com/
*/

#include <gtk/gtk.h>

/* declare a message box widget */
GtkWidget *msg_box;

/* construct a message box dialog */
void
MessageBox( GtkMessageType mType, GtkButtonsType bType, const gchar *title,
const gchar *msg, const gchar *details )
{
/* message box response code */
int res;
/* create a message box dialog */
msg_box = gtk_message_dialog_new( NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
mType, bType, msg );
/* set message box title */
gtk_window_set_title( GTK_WINDOW( msg_box ), title );
/* set detail text */
gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( msg_box ), details );
/* run the message box dialog */
res = gtk_dialog_run( GTK_DIALOG( msg_box ) );
/* check for dialog response and handle */
if( res == GTK_RESPONSE_OK )
gtk_widget_destroy( msg_box );
}

void
btn_info_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "[JaPh] Dialog: INFO" ,
"GtkMessageDialog: Info", "This is an example of INFO Dialog" );
}
void
btn_warn_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "[JaPh] Dialog: WARNING",
"GtkMessageDialog: Warning", "This is an example of WARNING Dialog" );
}
void
btn_ques_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK, "[JaPh] Dialog: QUESTION",
"GtkMessageDialog: Question", "This is an example of QUESTION Dialog" );
}
void
btn_erro_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "[JaPh] Dialog: ERROR",
"GtkMessageDialog: Error", "This is an example of QUESTION Dialog" );
}

/* MAIN */
int
main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *btn_info, *btn_warn, *btn_ques, *btn_erro;
GtkWidget *btn_exit;

gtk_init( &argc, &argv );
/* construct main window */
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title( GTK_WINDOW( window ), "[JaPh]Lesson 11: GtkMessageDialog" );
gtk_widget_set_size_request( window, 200, 180 );
/* create vbox layout */
vbox = gtk_vbox_new( TRUE, 5 );
/* create buttons */
btn_info = gtk_button_new_with_mnemonic( "Show _Info" );
btn_warn = gtk_button_new_with_mnemonic( "Show _Warning" );
btn_ques = gtk_button_new_with_mnemonic( "Show _Question" );
btn_erro = gtk_button_new_with_mnemonic( "Show _Error" );
btn_exit = gtk_button_new_with_mnemonic( "E_xit" );
/* pack buttons to vbox from TOP to BOTTOM */
gtk_box_pack_start( GTK_BOX(vbox), btn_info, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_warn, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_ques, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_erro, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_exit, TRUE, TRUE, 0 );
/* add layout widget to main window */
gtk_container_add( GTK_CONTAINER(window), vbox );
/* button signal */
g_signal_connect( GTK_BUTTON( btn_info ), "clicked",
G_CALLBACK( btn_info_clicked ), NULL );
g_signal_connect( GTK_BUTTON( btn_warn ), "clicked",
G_CALLBACK( btn_warn_clicked ), NULL );
g_signal_connect( GTK_BUTTON( btn_ques ), "clicked",
G_CALLBACK( btn_ques_clicked ), NULL );
g_signal_connect( GTK_BUTTON( btn_erro ), "clicked",
G_CALLBACK( btn_erro_clicked ), NULL );

g_signal_connect( GTK_BUTTON( btn_exit), "clicked",
G_CALLBACK( gtk_main_quit ), NULL );
/* window signal */
g_signal_connect( window, "destroy", G_CALLBACK( gtk_main_quit ), NULL );
/* show window & widgets */
gtk_widget_show_all( window );

/* run Gtk+ app */
gtk_main();
return 0;
}

Remember to keep your practice always, don't forget!

Have fun!@

I have introduced about GtkVBox in the previous lesson; however, GtkHBox is basically the same as GtkVBox. The only difference is the horizontal arrangement of GtkHBox.
Reference GtkHBox

Things do the same.
So just try to mix GtkHBox and GtkVBox you can do a small application like this:Why not make it a practice?

Detail: an Gtk+ application, like a phone dial

When you done, you can check out my source below, it's pretty handy though...



/*
* @author [JaPh]
* @date August 12, 2009
* @file GtkHVBox_01.c
* @site http://xjaphx.blogspot.com/
*/

#include <gtk/gtk.h>
#include <string.h>

void
gtk_label_append_text( GtkLabel *label, char *s )
{
int len = strlen( gtk_label_get_text( label ));
gchar buf[128];
snprintf( buf, len + 2, "%s%s", gtk_label_get_text( label ), s );
gtk_label_set_text( label, buf );
}

void
btn1_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "1");
}
void
btn2_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "2");
}
void
btn3_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "3");
}
void
btn4_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "4");
}
void
btn5_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "5");
}
void
btn6_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "6");
}
void
btn7_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "7");
}
void
btn8_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "8");
}
void
btn9_clicked_event( GtkButton *btnN, GtkLabel *label )
{
gtk_label_append_text( label, "9");
}

void
btn_clear_clicked_event( GtkButton *btn_clear, GtkLabel *label )
{
gtk_label_set_text( label, " " );
}

int
main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *hbox1, *hbox2, *hbox3, *hbox4;
GtkWidget *btn1, *btn2, *btn3, *btn4,
*btn5, *btn6, *btn7, *btn8, *btn9;
GtkWidget *btn_clear, *btn_exit;

gtk_init( &argc, &argv );

/* window */
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title( GTK_WINDOW(window), "[JaPh]Lesson 10: GtkHBox & GtkVBox" );
gtk_widget_set_size_request( window, 300, 300 );

/* top level vbox */
vbox = gtk_vbox_new( TRUE, 0 ) ;

/* hbox1: btn1, btn2, btn3 */
hbox1 = gtk_hbox_new( TRUE, 0 );
btn1 = gtk_button_new_with_label("1");
btn2 = gtk_button_new_with_label("2");
btn3 = gtk_button_new_with_label("3");
gtk_box_pack_start( GTK_BOX( hbox1 ), btn1, TRUE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( hbox1 ), btn2, TRUE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( hbox1 ), btn3, TRUE, TRUE, 5 );

/* hbox2: btn4, btn5, btn6 */
hbox2 = gtk_hbox_new( TRUE, 0 );
btn4 = gtk_button_new_with_label("4");
btn5 = gtk_button_new_with_label("5");
btn6 = gtk_button_new_with_label("6");
gtk_box_pack_start( GTK_BOX( hbox2 ), btn4, TRUE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( hbox2 ), btn5, TRUE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( hbox2 ), btn6, TRUE, TRUE, 5 );

/* hbox3: btn7, btn8, btn9 */
hbox3 = gtk_hbox_new( TRUE, 0 );
btn7 = gtk_button_new_with_label("7");
btn8 = gtk_button_new_with_label("8");
btn9 = gtk_button_new_with_label("9");
gtk_box_pack_start( GTK_BOX( hbox3 ), btn7, TRUE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( hbox3 ), btn8, TRUE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( hbox3 ), btn9, TRUE, TRUE, 5 );

/* hbox4: btn_clear, btn_exit */
hbox4 = gtk_hbox_new( TRUE, 0 );
btn_clear = gtk_button_new_with_label("Clear");
btn_exit = gtk_button_new_with_label("Exit");
gtk_box_pack_start( GTK_BOX( hbox4 ), btn_clear, TRUE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( hbox4 ), btn_exit, TRUE, TRUE, 5 );

/* vbox: hbox1, hbox2, hbox3, hbox4 */
label = gtk_label_new("Click any number button");
gtk_box_pack_start( GTK_BOX( vbox ), label, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX( vbox ), hbox1, TRUE, TRUE , 5 );
gtk_box_pack_start( GTK_BOX( vbox ), hbox2, TRUE, TRUE , 5 );
gtk_box_pack_start( GTK_BOX( vbox ), hbox3, TRUE, TRUE , 5 );
gtk_box_pack_start( GTK_BOX( vbox ), hbox4, TRUE, TRUE , 5 );

/* add to window */
gtk_container_add( GTK_CONTAINER( window ), vbox );

/* signal connect */
/* window */
g_signal_connect( window, "destroy",
G_CALLBACK( gtk_main_quit ), NULL );

/* btn1->9 */
g_signal_connect( GTK_BUTTON( btn1 ), "clicked",
G_CALLBACK( btn1_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn2 ), "clicked",
G_CALLBACK( btn2_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn3 ), "clicked",
G_CALLBACK( btn3_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn4 ), "clicked",
G_CALLBACK( btn4_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn5 ), "clicked",
G_CALLBACK( btn5_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn6 ), "clicked",
G_CALLBACK( btn6_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn7 ), "clicked",
G_CALLBACK( btn7_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn8 ), "clicked",
G_CALLBACK( btn8_clicked_event ), GTK_LABEL( label ) );

g_signal_connect( GTK_BUTTON( btn9 ), "clicked",
G_CALLBACK( btn9_clicked_event ), GTK_LABEL( label ) );


/* btn_clear */
g_signal_connect( GTK_BUTTON( btn_clear ), "clicked",
G_CALLBACK( btn_clear_clicked_event ), GTK_LABEL( label ) );

/* btn_exit */
g_signal_connect( GTK_BUTTON(btn_exit), "clicked",
G_CALLBACK( gtk_main_quit ), NULL );

gtk_widget_show_all( window );

gtk_main();

return 0;
}

Let’s work out together with Gtk+ 2.0.

Well, you need to setup your environment with Gtk+, already support by Gnome if using Gnome/Linux.

Let’s do this: “Hello World”


#include 

void
destroy( void )
{
gtk_main_quit( );
}

int
main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;

gtk_init( &argc, &argv );

window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_container_border_width( GTK_CONTAINER( window ), 10 );

g_signal_connect( GTK_OBJECT(window ), "destroy", G_CALLBACK( destroy ), NULL );

button = gtk_button_new_with_label( "Hello World" );

g_signal_connect( GTK_OBJECT( button ), "clicked", G_CALLBACK( hello ), NULL );
g_signal_connect_object( GTK_OBJECT( button ), "clicked", G_CALLBACK( gtk_widget_destroy ), GTK_OBJECT( window ) );

gtk_container_add( GTK_CONTAINER ( window ), button );

gtk_widget_show( button );
gtk_widget_show( window );

gtk_main ();

return 0;
}

There you have the source, well, named it “hello.c”

To make it work, let’s compile

japh@server ~] $ gcc -Wall -g -o hello hello.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)

Then just run ….

Have fun….


[Note: source code updated - 13 August 2009]