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 

[C]Creating a dynamic matrix with tables and Gtkentries....

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


Joined: 04 Apr 2008
Posts: 8

PostPosted: Fri Apr 04, 2008 10:24 am    Post subject: [C]Creating a dynamic matrix with tables and Gtkentries.... Reply with quote

Hello Everyone
I'm currently developping a linear system resolution application (Gauss,Cholesky,LU....) And I want to create a dynamic sizeable matrix to allow the user to enter datas.I'm working on DEVC++,GTK+2.0 and windows.
On the windows, there are three areas with labelled frames containing tables:
-An area for the matrix with the matrix table.
-An area for a vector with vector table.
-An area for the solution with the solution table.
My main problem is that above the size of 2(3,4 more precisely), the entries does not appear correctly in the vector and solution table.And when I enter 5, the application crash with a window error....Knowing I want the user to be able to enter infos for matrixes up to 10*10 size....

Well, I think an code should be more "talking":

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

void Creation_matrice(void)
{
     
     //Variables 
     int taille,i,j;
     GtkWidget *pChoix;
     GtkWidget *pCadre;
     GtkWidget *pSelect;
     GtkWidget *pHbox;
     GtkWidget *pMatrice;
     GtkWidget *pFrame[3];
     GtkWidget *pTable[3];
     GtkWidget *pEntryVec[taille];
     GtkWidget *pEntrySol[taille];
     GtkWidget *pEntryMat[taille*taille];
     
     
     //Creation de Frame
     pCadre = gtk_frame_new("Choix de la taille");
     
     //creation de la fenetre de dialogue de choix de la taille;
     pChoix=gtk_dialog_new();
     gtk_window_set_title(GTK_WINDOW(pChoix),"Taille de la matrice");
     gtk_window_set_transient_for(GTK_WINDOW(pChoix),GTK_WINDOW(pWindow));
     gtk_window_set_resizable(GTK_WINDOW(pChoix),FALSE);
     
     //Insertion de boutons dans la fenetre de dialogue
     gtk_dialog_add_buttons(GTK_DIALOG(pChoix),"OK",GTK_RESPONSE_OK,
                                               "Annuler",GTK_RESPONSE_CANCEL,
                                               NULL);
     //Creation de la zone de selection+insertion dans la frame
     pSelect = gtk_spin_button_new_with_range(1,10,1);
     gtk_container_add(GTK_CONTAINER(pCadre),pSelect);
     
     
     //Insertion du cadre dans la fenetre de dialogue de choix de taille
     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pChoix)->vbox),pCadre,TRUE,TRUE,0);
     
     //on limite les actions à cette fenêtre
     gtk_window_set_modal(GTK_WINDOW(pChoix),TRUE);
     
     //affichage de la fenetre
     gtk_widget_show_all(pChoix);
     
     //Récupération des réponses des boutons:
     if (gtk_dialog_run(GTK_DIALOG(pChoix))== GTK_RESPONSE_OK)
     {
        taille=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(pSelect));                                     
        gtk_widget_destroy(pChoix);
       
             
        //Creation de la fenetre+Insertion boutons
        pMatrice=gtk_dialog_new();
        gtk_window_set_title(GTK_WINDOW(pMatrice),g_locale_to_utf8("Insertion des données",-1,NULL,NULL,NULL));
        gtk_window_set_transient_for(GTK_WINDOW(pMatrice),GTK_WINDOW(pWindow));
        gtk_dialog_add_buttons(GTK_DIALOG(pMatrice),g_locale_to_utf8("Résolution",-1,NULL,NULL,NULL),GTK_RESPONSE_OK,
                                                                     "Nouveau",GTK_RESPONSE_APPLY,
                                                    g_locale_to_utf8("Arrêter",-1,NULL,NULL,NULL),GTK_RESPONSE_CANCEL
                                                    ,NULL);
     
        //Création de la Hbox + Insertion dans la Vbox de la boite de dialogue
        pHbox=gtk_hbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pMatrice)->vbox),pHbox,FALSE,TRUE,0);
     
     
        //Création des frames
        pFrame[0]=gtk_frame_new("Matrice");
        pFrame[1]=gtk_frame_new("Vecteur");
        pFrame[2]=gtk_frame_new("Solution");
       
        //Création des tables pour la matrice, le vecteur et la solution
        pTable[0]= gtk_table_new(taille,taille,TRUE);
        pTable[1]= gtk_table_new(taille,1,TRUE);
        pTable[2]= gtk_table_new(taille,1,TRUE);
       
        //Creation des zones d'entree des nombres+definition de leur taille
            /*Pour les vecteurs*/         
             for(i=0;i<taille;i++)
             {
              pEntryVec[i]=gtk_entry_new();
              pEntrySol[i]=gtk_entry_new();
                     
             }
            /*Pour la matrice*/
             for(i=0;i<taille*taille;i++)
             {
               pEntryMat[i]=gtk_entry_new();               
                                            }
        //Attachement des zones d'entrées aux différentes tables
            /*Pour la table Matricielle*/
           
           
            for(i=0;i<taille;i++)
            {
              for(j=0;j<taille;j++)
              {                   
                gtk_table_attach_defaults(GTK_TABLE(pTable[0]),pEntryMat[i*taille+j],i,i+1,j,j+1);
                                                                                                  }
                                                                                         
                                                                                                   }
             /*Pour la table Vectorielle*/
              for(i=0;i<taille;i++)
                {                   
                 gtk_table_attach_defaults(GTK_TABLE(pTable[1]),pEntryVec[i],0,1,i,i+1);
                                                                                         }
             /*Pour la table Solution*/ 
              for(i=0;i<taille;i++)
                {
                  gtk_table_attach_defaults(GTK_TABLE(pTable[2]),pEntrySol[i],0,1,i,i+1);
                                                                                         }
                                                                                                                                                               
        //Insertion des tables dans les frames
        for(i=0;i<3;i++)
        {
           gtk_container_add(GTK_CONTAINER(pFrame[i]),pTable[i]);             
                                                                 }
        //Insertion des frames dans la Hbox
        for(i=0;i<3;i++)
        {
         gtk_box_pack_start(GTK_BOX(pHbox),pFrame[i],FALSE,FALSE,15);
        }
       
       
       //limitaion des actions à cette fenêtre
       gtk_window_set_modal(GTK_WINDOW(pMatrice),TRUE);
   
       //Affichage fenêtre
       gtk_widget_show_all(pMatrice);
     }
     else
     {
       gtk_widget_destroy(pChoix); 
         }                                                         
}     



And an image of my "bug":


Could you help me finding the issue please?And if this is not the good solution, is there another? Thanks in advance.
Back to top
Redlion
Familiar Face


Joined: 04 Apr 2008
Posts: 8

PostPosted: Sun Apr 06, 2008 1:32 pm    Post subject: Reply with quote

Hum...Well.I decided to change my way to present the matrices and I gonna use a GtkTreeView in order to show it.The main problem now is that I only can edit the first row of the treeview.When I try to edit the other rows, nothing is changed in the selected row and moreover,It changes the edited value of the first row.Here's the code.Can someone help me please?

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



#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>


void cell_edited(GtkCellRendererText *cell,
                 const gchar         *path_string,
                 const gchar         *new_text,
                 GtkTreeView *treeView        );

int main(int argc, char *argv[])
{
  //Initialisation GTK
  gtk_init(&argc, &argv);
 
  //Définition des variables
  gint i=0,Taille=0;
  GtkWidget *pWindow=NULL;
  GtkWidget *pLV=NULL;
  GtkWidget *pScroll=NULL;
  GtkListStore *pStore=NULL;
  GtkCellRenderer *pRender=NULL;
  GtkTreeViewColumn *pColumn=NULL;
  GType *types=NULL;
 
     Taille=3;
 
  //Création et définition des attributs de la fenêtre principale
    pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(pWindow),GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(pWindow),"Test");
    gtk_window_set_default_size(GTK_WINDOW(pWindow),400,300);
 
  //Creation du modèle et du type pour les colonnes
     types = g_new0(GType,Taille);
     for(i=0;i<Taille;i++)
     {types[i]=G_TYPE_FLOAT;}
     pStore = gtk_list_store_newv(Taille,types);
     g_free (types);
   
   
  //Insertion des éléments
    for(i=0;i<Taille;i++)
     {
        GtkTreeIter pIter;
        /*Creation de la nouvelle ligne*/
          gtk_list_store_prepend(pStore, &pIter);
        /*Insertion des données*/
          gtk_list_store_set(pStore,&pIter,i,0);
                                                          }
                             
  //Creation de la vue
    pLV= gtk_tree_view_new_with_model(GTK_TREE_MODEL(pStore));
 
  //Creation des colonnes
    for(i=0;i<Taille;i++)
    {
      pRender= gtk_cell_renderer_text_new();
      g_object_set (GTK_CELL_RENDERER (pRender), "editable",TRUE,"editable-set",TRUE,NULL);
      g_signal_connect(G_OBJECT(pRender),"edited", G_CALLBACK(cell_edited),(gpointer)pLV);                 
      pColumn=gtk_tree_view_column_new_with_attributes("",pRender,"text",i,NULL);
      gtk_tree_view_append_column(GTK_TREE_VIEW(pLV),pColumn);                   
                                                                                          }
                                                                                                                                                                                                                                                                     
                                                                                         
  //Creation de la barre de défilement+Gestion de ses attributs                                                                                     
    pScroll=gtk_scrolled_window_new(NULL,NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(pScroll),GTK_POLICY_ALWAYS,GTK_POLICY_ALWAYS);
    gtk_container_add(GTK_CONTAINER(pScroll),pLV);
    gtk_container_add(GTK_CONTAINER(pWindow),pScroll);
 
  //Fermeture du programme
    g_signal_connect(G_OBJECT(pWindow),"destroy", G_CALLBACK(gtk_main_quit),NULL);
 
  //Affichage de la fenêtre
    gtk_widget_show_all(pWindow);
    gtk_main();
   
   return EXIT_SUCCESS;
}

void cell_edited(GtkCellRendererText *cell,
                 const gchar         *path_string,
                 const gchar         *new_text,
                 GtkTreeView *treeView            )
                 
{
   GtkTreeIter Iter;
   GtkTreeModel *Model;
   gfloat num=atof(new_text);
   Model=gtk_tree_view_get_model(treeView);
   guint column_number = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(cell), "column"));
   gtk_tree_model_get_iter_from_string(Model,&Iter,path_string);
   gtk_list_store_set(GTK_LIST_STORE(Model),&Iter,column_number,num);             
                 
}                                       
                                         

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