GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Sep 03, 2014 4:39 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Hello, gtk intrgration with c
PostPosted: Thu Feb 28, 2008 5:25 pm 
Offline
Familiar Face

Joined: Tue Feb 26, 2008 2:15 am
Posts: 12
Hello,
I have recently learned C and now i want to do my project in c with gtk+
i am new to gtk+
i learned how to design gui in glade
then
where can i write c code or gtk+'s code
thanks in advance
Ujjwol (11)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 02, 2008 12:42 am 
Offline

Joined: Sun Jan 13, 2008 4:23 pm
Posts: 2
Location: Puerto Rico
(sources codes) Samples form the book Foundations of GTK+ Development (great book):
http://www.apress.com/book/downloadfile/3438

_________________
>>>>=========<<<<
====<<<<=>>>>====
>>>>=========<<<<
=>>>>=======<<<<=
==>>>>=====<<<<==
===>>>>===<<<<===
====>>>>=<<<<====
=====>>>^<<<=====


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 2:41 am 
Offline

Joined: Mon Mar 03, 2008 2:07 am
Posts: 2
I recently took the same path as you. I lhaven't programmed in C for about ten years and now I'm learning it again, I wanted to add a GUI to my apllications so I looked at GTK+. Then discovered glade,

My advice is to use libglade to generate the xml file. Then learn some GTK programming. There are a few examples to be found along with tutorials.

Here is an example program that analyzes a tiff image using the libtiff library. Lines 81 to 123 were pre-existing code from a non-GUI program. The rest is GTK+ GUI programming. The main() function sets up the window and has several "signal handlers" that call the "callback functions". The call back functions will generally be where you do your programming. For example I do most of my programming work not related to the GUI in the register_all function. Most of the rest is just signals and callbacks which are the basis of GTK programming.

Use Google to find some good tutorials that will go into more depth and step by step show you how to write a simple C program with libglade,
[code=]/**********************************/
/*
Compile with:
gcc -Wall -g -o tutorial main.c `pkg-config --cflags --libs gtk+-2.0 libglade-2.0` -ltiff
*/
/**********************************/
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <tiffio.h>


GSList *file_list=NULL;


void on_window1_destroy (GtkObject *object, gpointer user_data);
void on_select_files_butn (GtkButton *button, gpointer user_data);
void add_images_textview (GtkWidget *textview, gpointer user_data);
int register_all (GtkButton *button, gpointer user_data);




int
main (int argc, char *argv[])
{
GladeXML *gxml;
GtkWidget *window;
GtkWidget *text_view;
GtkWidget *message_view;

gtk_init (&argc, &argv);

gxml = glade_xml_new ("gui.glade", NULL, NULL);
window = glade_xml_get_widget (gxml, "window1");
text_view = glade_xml_get_widget (gxml, "textview1");
message_view = glade_xml_get_widget (gxml, "messages_textview");

glade_xml_signal_connect (gxml, "on_window1_destroy",
G_CALLBACK(on_window1_destroy));
glade_xml_signal_connect_data (gxml, "on_select_files_butn",
G_CALLBACK(on_select_files_butn), window);
glade_xml_signal_connect_data (gxml, "on_select_files_butn",
G_CALLBACK(add_images_textview), text_view);
glade_xml_signal_connect_data (gxml, "on_reg_butn",
G_CALLBACK(register_all), message_view);


g_object_unref (G_OBJECT (gxml));

gtk_widget_show (window);
gtk_main ();

return 0;
}


void
on_window1_destroy (GtkObject *object,
gpointer user_data)
{
gtk_main_quit();
}



int register_all (GtkButton *button,
gpointer user_data)
{
gchar *filename, message[200];
guint num_files, i;
GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (user_data));
uint32 w,h,brightness=0,high_val=0;
uint16 *row_buf,x,y,reg_x,reg_y;
int16 j,jj, radius=3;

num_files = g_slist_length(file_list);

for (i=0; i<num_files; i++)
{
filename = g_slist_nth_data (file_list, i);
TIFF* tif = TIFFOpen(filename, "r");
/*Read Image Header for width, height*/
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);

/*Allocate memory for the stack of images*/
row_buf = _TIFFmalloc(TIFFScanlineSize(tif));
uint32 **row_buf_pointers = (uint32**) malloc (sizeof(uint32)*h);

for(y=0;y<h;y++)
{
row_buf_pointers[y] = (uint32*) malloc(sizeof(uint32) * w * 3);
TIFFReadScanline(tif, row_buf, y,0);
for(x=0;x<(w*3);x++) row_buf_pointers[y][x] = (uint32) row_buf[x];
}
_TIFFfree(row_buf);
TIFFClose(tif);

for (y=(h/8); y<(7*h/8); y++)
{
for (x=(w/8); x<(7*w/8); x++)
{
brightness=0;
for (jj = -radius; jj<=radius; jj++)
{
uint32* row = row_buf_pointers[y+jj];

for (j = -radius; j <= radius; j++)
{
brightness+=row[(x+j)*3]+row[(x+j)*3+1]+row[(x+j)*3+2];
}
}
if (brightness>high_val)
{
high_val=brightness;
reg_x=x;
reg_y=y;
}
}
}

sprintf(message,"Registered %s: Anchor Point[%d,%d]\n",filename,reg_x,reg_y);
gtk_text_buffer_insert_at_cursor (buffer, message, -1);
gtk_text_view_set_buffer (GTK_TEXT_VIEW (user_data),buffer);


}
return 1;
}



void on_select_files_butn (GtkButton *button,
gpointer user_data)
{
GtkWidget *chooser;
GtkFileFilter *file_filter;

chooser = gtk_file_chooser_dialog_new ("Select Multiple Images...",
GTK_WINDOW (user_data),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
NULL);

gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER (chooser), TRUE);

file_filter = gtk_file_filter_new();
gtk_file_filter_add_pattern(file_filter, "*.png");
gtk_file_filter_add_pattern(file_filter, "*.PNG");
gtk_file_filter_add_pattern(file_filter, "*.tif");
gtk_file_filter_add_pattern(file_filter, "*.tiff");
gtk_file_filter_add_pattern(file_filter, "*.TIF");
gtk_file_filter_add_pattern(file_filter, "*.TIFF");
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser), file_filter);

if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK)
{
file_list = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
}

gtk_widget_destroy (chooser);
}


void
add_images_textview (GtkWidget *button,
gpointer user_data)
{
GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (user_data));
gchar *filename;
guint num_files, i;

num_files = g_slist_length(file_list);
printf("Number of files = %d\n", num_files);
for (i=0; i<num_files; i++)
{
filename = g_slist_nth_data (file_list, i);
gtk_text_buffer_insert_at_cursor (buffer, filename, -1);
gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
gtk_text_view_set_buffer (GTK_TEXT_VIEW (user_data),buffer);
}
}

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:51 am 
Offline
Familiar Face

Joined: Tue Feb 26, 2008 2:15 am
Posts: 12
THanks
and i will search many other tutuorials also...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: ldries46 and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group