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 

A text in a widget respond a event. [Solved]

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
candeia



Joined: 26 Sep 2008
Posts: 1

PostPosted: Fri Sep 26, 2008 1:42 am    Post subject: A text in a widget respond a event. [Solved] Reply with quote

I apologize my low English knowledge, but i post this question in a Brazilian forum without success.
I am building an application aiming databases query, using C + SQLite3 + GTK+ libglade.
The client's names and addresses are stored in database.
I solved the query's problem (prototype code down).
The query, eg (select * from table) in entry1, the result outs entry2.
Each query has multiples result rows, and the entry is insufficient to store them.
What is the appropriate widget to accomplishes the work:
Store multiple result rows and respond to events on text, eg (mouse over client's name) emitting signals for handling (evoke call back functions) for further query?

Thanks in advance.

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

#include <stdlib.h>
#include <stdio.h>
#include <glade/glade.h>
#include <gtk/gtk.h>
#include <sqlite3.h>
#define FILENAME "/home/ice/Desktop/teste.glade"

/* Compilar:
gcc -o teste main.c  -L/usr/local/lib -lsqlite3 `pkg-config --cflags --libs libglade-2.0`
*/
static GladeXML *xml;

int call    (void *Unused,
        int cols,
        char **values,
        char **headers);

void on_window1_destroy (GtkWidget *widget, gpointer user_data){
    gtk_main_quit();
}

char saida(char *d, char *e){
GtkWidget *exec, *i, *x;
 char *str;
 
 x = GTK_WIDGET(glade_xml_get_widget(xml, "entry2"));
 str = g_strdup_printf ("%s = %s",d,e);
 gtk_entry_set_text (GTK_ENTRY (x), str);
 g_free (str);
}


void on_button1_clicked (GtkWidget *button, gpointer user_data){
GtkWidget *i, *j;
  i = GTK_WIDGET(glade_xml_get_widget(xml,"entry1"));
  j = gtk_entry_get_text(GTK_ENTRY(i));
sqlite_function(j);
}


int sqlite_function (char *j){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;

rc = sqlite3_open("/home/ice/Desktop/cid10.db", &db);
if( rc ){
fprintf(stderr, "Nao pode abrir o DB: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_exec(db, j, call, NULL, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);

return 0;
}
int call (void *Unused, int cols, char **values, char **headers){
int i;
char *r, *s;


for(i=0; i<ncols; i++){

r = values[i];
s = headers[i];
}
saida(r,s);
return 0;
}



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

GtkWidget *window1;

GtkWidget *entry1;
GtkWidget *entry2;

     
    gtk_init(&argc, &argv);
    glade_init();
    xml = glade_xml_new(FILENAME, NULL, NULL);
       window1 = glade_xml_get_widget (xml, "window1");
    entry1 = glade_xml_get_widget (xml, "entry1");
    entry2 = glade_xml_get_widget (xml, "entry2");
        glade_xml_signal_connect (xml, "on_button1_clicked", G_CALLBACK(on_button1_clicked));
    glade_xml_signal_connect(xml, "on_window1_destroy", G_CALLBACK(on_window1_destroy));
            if (!xml) {
            g_warning("Algo de errado com a interface");
            return 1;
                }
        gtk_widget_show(window1);
    gtk_main();
        return 0;
}


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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.0.3 on Wed Sep 24 20:10:54 2008 by ice@basan-->
<glade-interface>
  <widget class="GtkWindow" id="window1">
    <signal name="destroy" handler="on_window1_destroy"/>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <child>
          <widget class="GtkEntry" id="entry1">
            <property name="visible">True</property>
          </widget>
        </child>
        <child>
          <widget class="GtkButton" id="button1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">button</property>
            <signal name="clicked" handler="on_button1_clicked"/>
          </widget>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <widget class="GtkEntry" id="entry2">
            <property name="visible">True</property>
          </widget>
          <packing>
            <property name="position">2</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>


The treeview widget is helpful. The code:

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
#include <stdlib.h>
#include <stdio.h>
#include <glade/glade.h>
#include <gtk/gtk.h>
#include <sqlite3.h>

#define FILENAME "/home/ice/Desktop/teste.glade"

/* Compilar:
gcc -o teste main.c  -L/usr/local/lib -lsqlite3 `pkg-config --cflags --libs libglade-2.0`
*/

static GladeXML *xml;
GtkListStore *store;
GtkTreeIter iter;

enum
{
   CID,
   DESCRICAO,
   COLUMNS
};


void setup_tree_view (GtkWidget*);

int callback_sqlite(void *NotUsed,
            int  ncols,
            char **values,
            char **headers);

void on_window1_destroy (GtkWidget *widget, gpointer user_data){
    gtk_main_quit();
    }
//Function to insert result rows from query in list treeview
char saida(int a, char *b){

gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
                  CID, a, DESCRICAO, b, -1);

}


void on_button1_clicked (GtkWidget *button, gpointer user_data){
GtkWidget *i;
const char *j;

  i = GTK_WIDGET(glade_xml_get_widget(xml,"entry1"));
  j = gtk_entry_get_text(GTK_ENTRY(i));
//Clear previous query results
 gtk_list_store_clear(store);
//Call query sqlite3 function
sqlite_function(j);

}


int sqlite_function (char *j){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;

rc = sqlite3_open("/home/ice/Desktop/cid10.db", &db);
    if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
    }

rc = sqlite3_exec(db, j, callback_sqlite, NULL, &zErrMsg);
    if( rc!=SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
    }
sqlite3_close(db);
return 0;
}
//SQLite callback function
int callback_sqlite (void *NotUsed, int ncols, char **values, char **headers){
int  r;
char *s;

r =(int) values[0]; //Values in first column of database table
s = values[1];//Values in second column of database table
saida(r,s);
return 0;
}

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

GtkWidget *window1;
GtkWidget *entry1;
GtkWidget *treeview1;

       
    gtk_init(&argc, &argv);
    glade_init();
    xml = glade_xml_new(FILENAME, NULL, NULL);
       window1 = glade_xml_get_widget (xml, "window1");
    entry1 = glade_xml_get_widget (xml, "entry1");
    treeview1 = glade_xml_get_widget (xml, "treeview1");
        glade_xml_signal_connect (xml, "on_button1_clicked", G_CALLBACK(on_button1_clicked));
    glade_xml_signal_connect(xml, "on_window1_destroy", G_CALLBACK(on_window1_destroy));
       
        if (!xml) {
            g_warning("Erro ao criar interface");
                return 1;
                }
                       
 treeview1 = GTK_WIDGET(glade_xml_get_widget(xml, "treeview1"));
store = gtk_list_store_new (COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview1), GTK_TREE_MODEL (store));
g_object_unref (store);

            setup_tree_view (treeview1);
                      gtk_widget_show(window1);
               
                gtk_main();

                    return 0;
}
void setup_tree_view (GtkWidget *treeview)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
 
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes
                         ("CID", renderer, "text", CID, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes
                         ("DESCRICAO", renderer, "text", DESCRICAO, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
}

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