GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Hello, gtk intrgration with c

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Ujjwol
Familiar Face


Joined: 26 Feb 2008
Posts: 12

PostPosted: Thu Feb 28, 2008 5:25 pm    Post subject: Hello, gtk intrgration with c Reply with quote

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)
Back to top
elektroglow



Joined: 13 Jan 2008
Posts: 2
Location: Puerto Rico

PostPosted: Sun Mar 02, 2008 12:42 am    Post subject: Reply with quote

(sources codes) Samples form the book Foundations of GTK+ Development (great book):
http://www.apress.com/book/downloadfile/3438
Back to top
JBullard



Joined: 03 Mar 2008
Posts: 2

PostPosted: Mon Mar 03, 2008 2:41 am    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**********************************/
/*
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);
    }
}

Back to top
Ujjwol
Familiar Face


Joined: 26 Feb 2008
Posts: 12

PostPosted: Tue Mar 11, 2008 1:51 am    Post subject: Reply with quote

THanks
and i will search many other tutuorials also...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP