Programming GNOME - Linux System Programming - Lecture Notes, Study notes of Linux skills

Topics covered in this lecture handout are: Programming, Gnome, GTK, KDE, Features, Data types, Dealing.

Typology: Study notes

2011/2012

Uploaded on 10/24/2012

alia_maru
alia_maru 🇮🇳

4.5

(41)

57 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming GNOME
using GTK+
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Programming GNOME - Linux System Programming - Lecture Notes and more Study notes Linux skills in PDF only on Docsity!

Programming GNOME

using GTK+

Introduction to GNOME

  • GNOME: GNU Network Object Model En- vironment, is a software project with a simple aim – to provide a user friendly, powerful user and development desktop.
  • GTK+ was choosen as GNOME’s toolkit because it is a well featured, OO, cross platform, language neutral toolkit. It sup- ports many languages(C, C++, PERL, Python, ADA, etc.)
  • KDE(K Desktop Environment) is based on a widget toolkit Qt written by Troll- Tech.
  • Toolkits like GTK+, Qt or Tk are collec- tions of widgets – GUI objects like but- tons, menus, dialog boxes, etc. and gen- eral support functions.

Programming with GTK+

  1. Creating a window

#include <gnome.h>

int main(int argc, char argv[]) { / declare a window object */ GtkWidget *app;

/* initialize and load functions

  • ‘‘example’’ is application name
  • ‘‘0.1’’ is the version number */ gnome_init("example","0.1", argc, argv);

/* create a top level window, which has

  • the application name and window title */ app=gnome_app_new("example","Window Title");

/* make this window visible */ gtk_widget_show(app);

/* controls in gtk_main() can is loaded,

  • i.e. mouse clicks, button press, etc. */ gtk_main(); return 0; }

Use the following command to compile:

gcc win1.c ‘gnome-config --cflags --libs gnomeui‘ -o win

  1. Dealing with events

#include <gnome.h>

/* define the event of button click,

  • it will print out a string */ static void button_clicked(GtkWidget *button, gpointer data) { char *string =data; g_print(string);

GTK_SIGNAL_FUNC(button_clicked), "button is clicked\n"); /* put the button onto the object of app */ gnome_app_set_contents(GNOME_APP(app), button); gtk_widget_show_all(app); gtk_main (); return(0); }

  1. Entry widgets

#include <gnome.h>

static void enter_pressed(GtkWidget *button, gpointer data) { GtkWidget *text_entry = data; char *string = gtk_entry_get_text(GTK_ENTRY(text_entry)); g_print(string); }

int main(int argc, char *argv[]) { GtkWidget *app; GtkWidget *text_entry; GtkWidget *label; GtkWidget *hbox;

gnome_init("example","0.1", argc, argv);

/create a top level window with an title/ app=gnome_app_new("example","Entry widget");

/* give the window a boarder / gtk_container_border_width(GTK_CONTAINER(app), 5); / create a container, not each object occupy

  • the same area, and the spacing between the
  • adjcent widgets is 0 */ hbox = gtk_hbox_new(FALSE, 0);

/* create a label with text on*/ label= gtk_label_new("Password:");

NULL);

/* event of inputing some text / gtk_signal_connect(GTK_OBJECT(text_entry), "activate", GTK_SIGNAL_FUNC(enter_pressed), text_entry); / place the container to the app object */ gnome_app_set_contents(GNOME_APP(app), hbox);

/* show all the widgets on the app */ gtk_widget_show_all(app); gtk_main(); return 0; }

  1. Buttons

#include <gnome.h>

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

GtkWidget *app; GtkWidget *button1, *button2; GtkWidget *radio1, *radio2; GtkWidget *radio3, *radio4; GtkWidget *vbox1, *vbox2; GtkWidget hbox; /empty list */ GSList *group = NULL;

gnome_init("example","0.1", argc, argv);

app=gnome_app_new("example","Music choices");

/* give the window a boarder */ gtk_container_border_width(GTK_CONTAINER(app), 20);

/* create containers */ vbox1 = gtk_vbox_new(FALSE, 0); vbox2 = gtk_vbox_new(FALSE, 0); hbox = gtk_hbox_new(FALSE, 0);

/create button1 as check button with label/

/* place the radio buttons onto the vbox2 */ gtk_box_pack_start(GTK_BOX(vbox2), radio1, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox2), radio2, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox2), radio3, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox2), radio4, FALSE, FALSE, 0);

/* place the vbox1 and vbox2 onto the hbox */ gtk_container_add(GTK_CONTAINER(hbox),vbox1); gtk_container_add(GTK_CONTAINER(hbox),vbox2);

/event of quiting the window/ gtk_signal_connect(GTK_OBJECT(app), "delete_event", GTK_SIGNAL_FUNC(gtk_main_quit), NULL); /* place the hbox onto the top level window */ gnome_app_set_contents(GNOME_APP(app), hbox);

/* show all the widgets in the window*/

gtk_widget_show_all(app); gtk_main (); return(0); }

  1. Menus and Toolbars

#include <gnome.h>

static void callback(GtkWidget button, gpointer data) { g_print("Item selected"); } / create the file item list */ GnomeUIInfo file_tree[]= { GNOMEUIINFO_MENU_NEW_ITEM("New", "create a new file", callback, NULL), GNOMEUIINFO_MENU_EXIT_ITEM(gtk_main_quit, NULL), GNOMEUIINFO_END

GtkWidget *app;

gnome_init("example", "0.1", argc, argv);

/create the top level window with title/ app=gnome_app_new("example", "Simple Menu and Tool bar");

/*create the menu on the top window / gnome_app_create_menus(GNOME_APP(app), menubar); /create the toolbar on the top window / gnome_app_create_toolbar(GNOME_APP(app), toolbar); /show all the widget on the top window */ gtk_widget_show_all(app); gtk_main(); return 0; }