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 

Temperature and humidity graph

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> Project Showcase
Author Message
psaval
Familiar Face


Joined: 20 Feb 2009
Posts: 7

PostPosted: Wed Apr 08, 2009 5:19 pm    Post subject: Temperature and humidity graph Reply with quote

Hi to all. I'm a spanish student working on his finall project. I'm a begginer programming, so don't be rude with me.

We have a dsPIC reading temperatures and humidity, saving it into a .txt. As this is hard to manage, I have the purpose of writing a gtk aplication that reads the file, takes 2 dates, and show the data contained in the file and it's between the 2 dates with a graph.
I have used a GTK assistant, a gtk_file_chooser, 2 gtk calendars and a gtk curve because of it's easyness- but the problem is that when i set the curve as a spline or linear, only interpolates 9 points, and that's not a good way to show the data :( So i think i will have to draw it by my own, but due my lack of experience and knowledgement, i can't even put a drawnable area. every single example i have seen does not compile under codeblocks (I hope someone can help with this)

But here is my example:

main.cpp

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
/***************************************************************************************/

/*INCLUDES*/

/***************************************************************************************/



#include <cairo/cairo.h>

#include <pango/pango.h>

#include <string.h>

#include <gtk/gtk.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

//#include "demo-common.h"

#include <gtk/gtkwindow.h>

//#include "func.c"



GtkWidget* do_assistant (GtkWidget *do_widget);



gchar *parametro_1, *parametro_2, *parametro_3=NULL;



/***************************************************************************************/

/*DEFINICION DE FUNCIONES*/

/***************************************************************************************/



static GtkWidget *window = NULL;



/***************************************************************************************/

/**/

/***************************************************************************************/







/***************************************************************************************/

/*FUNCION PRINCIPAL*/

/***************************************************************************************/

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

{

    unsigned int i=0;



    gtk_init(&argc, &argv);



if (argc>=3)

{

    for (i=0;argv[i];i++)

    {

        printf("argumento %u: %s\n",i,argv[i]);

    }

    parametro_1 = argv[1];

    parametro_2 = argv[2];

    parametro_3 = argv[3];

    printf("hay %d argumentos\n",argc);

    printf("path encontrado %s\n",parametro_3);



}

    window = do_assistant(window);



    gtk_widget_show_all(window);



    gtk_main();



    return 0;

}


func.cpp

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/***************************************************************************************/

/*INCLUDES*/

/***************************************************************************************/



#include <stdlib.h>

#include <string.h>

#include <gtk/gtk.h>

#include <gtk/gtkwindow.h>



struct reg_sensor {

    char nombremaquina[3];

    char fecha[12];

    char hora[12];

    double temp1;

    double temp2;

    double humedad1;

    double humedad2;

    double reservado1;

    double reservado2;

    double reservado3;

    double reservado4;



    double luminosidad1;

    double reservado5;

    double reservado6;

    double reservado7;

};



typedef struct reg_sensor registro_sensor;



unsigned int nregistro = 0;

unsigned int arraytemp[500000][3];

unsigned int fecha_inicial[3];

unsigned int fecha_final[3];

unsigned int fecha3[3];

gchar date_start[16];

gchar date_end[16];



registro_sensor *arrayRegistros=NULL;



/***************************************************************************************/

/*DEFINICION DE FUNCIONES*/

/***************************************************************************************/

gfloat vector1[30];



static void on_assistant_apply (GtkWidget *widget, gpointer data);

static void on_assistant_close_cancel (GtkWidget *widget, gpointer data);

static void on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer data);

static void on_entry_changed (GtkWidget *widget, gpointer data);



static void create_page1 (GtkWidget *assistant);

static void create_page2 (GtkWidget *assistant);

static void create_page3 (GtkWidget *assistant);

static void create_page4 (GtkWidget *assistant);

static void create_page5 (GtkWidget *assistant);



void seleccion(GtkCalendar *calendar,gpointer user_data);

void seleccion2(GtkCalendar *calendar,gpointer user_data);





static void curva_temp1();

static void curva_temp2();

static void curva_hume1();

static void curva_hume2();

static void curva_linear ();

static void curva_spline ();

static void curva_free ();



GtkWidget* do_assistant (GtkWidget *do_widget);



GtkWidget *assistant,*label1, *label2, *label3, *label4;

//GtkWidget*  lookup_widget              (GtkWidget       *widget,

//                                        const gchar     *widget_name);

static GtkWidget *entry  = NULL;

static GtkWidget *entry1 = NULL;

static GtkWidget *entry2 = NULL;



static GtkWidget *curva = NULL;



static gchar *path;

extern gchar *parametro_1, *parametro_2, *parametro_3;



void abrir_archivo();

void seleccionar_fecha_1();



void leer_archivo(const gchar *selected_filename);

//void ver_archivo(GtkWidget *pFileSelection, char *pFileContent);

void about();



/***************************************************************************************/

/*CREACION DEL ASISTENTE*/

/***************************************************************************************/



GtkWidget* do_assistant (GtkWidget *do_widget)

{

    entry = gtk_entry_new();

    gchar fetemp1[16];

    gchar fetemp2[16];

    gchar fetemp3[16];



    if (!assistant)

    {

        assistant = gtk_assistant_new ();

        gtk_window_set_default_size (GTK_WINDOW (assistant), 480, 300);



        gtk_window_set_position(GTK_WINDOW(assistant), GTK_WIN_POS_CENTER);



        //gtk_window_set_screen (GTK_WINDOW (assistant), gtk_widget_get_screen (do_widget));



        create_page1 (assistant);

        create_page2 (assistant);

        create_page3 (assistant);

        create_page4 (assistant);

        create_page5 (assistant);



        g_signal_connect (G_OBJECT (assistant), "cancel", G_CALLBACK (on_assistant_close_cancel), &assistant);

        g_signal_connect (G_OBJECT (assistant), "close", G_CALLBACK (on_assistant_close_cancel), &assistant);

        g_signal_connect (G_OBJECT (assistant), "apply", G_CALLBACK (on_assistant_apply), NULL);

        g_signal_connect (G_OBJECT (assistant), "prepare", G_CALLBACK (on_assistant_prepare), NULL);



        if (parametro_3 != NULL)

        {

            gtk_entry_set_text(GTK_ENTRY(entry), parametro_3);

            g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (on_entry_changed), assistant);

            }

    }

       if (parametro_1 != NULL)

       {

            gtk_entry_set_text(GTK_ENTRY(entry1), parametro_1);

            fetemp1[0]=parametro_1[0];

            fetemp1[1]=parametro_1[1];

            fetemp1[2]='\0';



            fecha_inicial[0]=atoi(fetemp1);

            printf("el dia inicial pasado por parametro es: %d\n",fecha_inicial[0]);



            fetemp2[0]=parametro_1[3];

            fetemp2[1]=parametro_1[4];

            fetemp2[2]='\0';

            fecha_inicial[1]=(atoi(fetemp2));

            printf("el mes inicial pasado por parametro es: %d",fecha_inicial[1]);



            fetemp3[0]=parametro_1[6];

            fetemp3[1]=parametro_1[7];

            fetemp3[2]=parametro_1[8];

            fetemp3[3]=parametro_1[9];

            fetemp3[4]='\0';

            fecha_inicial[2]=(atoi(fetemp3));

            printf("el anno inicial pasado por parametro es: %d\n",fecha_inicial[2]);

       }



       if (parametro_2 != NULL)

       {

            gtk_entry_set_text(GTK_ENTRY(entry2), parametro_2);

            fetemp1[0]=parametro_2[0];

            fetemp1[1]=parametro_2[1];

            fetemp1[2]='\0';



            fecha_final[0]=atoi(fetemp1);

            printf("el dia inicial pasado por parametro es: %d\n",fecha_final[0]);



            fetemp2[0]=parametro_2[3];

            fetemp2[1]=parametro_2[4];

            fetemp2[2]='\0';

            fecha_final[1]=(atoi(fetemp2));

            printf("el mes inicial pasado por parametro es: %d",fecha_final[1]);



            fetemp3[0]=parametro_2[6];

            fetemp3[1]=parametro_2[7];

            fetemp3[2]=parametro_2[8];

            fetemp3[3]=parametro_2[9];

            fetemp3[4]='\0';

            fecha_final[2]=(atoi(fetemp3));

            printf("el anno inicial pasado por parametro es: %d\n",fecha_final[2]);

       }



    if (!GTK_WIDGET_VISIBLE (assistant))

    {

        gtk_widget_show (assistant);

    }

    else

    {

        gtk_widget_destroy (assistant);

        assistant = NULL;

    }



    return assistant;

}



/***************************************************************************************/

/*ULTIMA VENTANA DEL ASISTENTE*/

/***************************************************************************************/

static void on_assistant_apply (GtkWidget *widget, gpointer data)

{

    GtkWidget *dialog;



    dialog = gtk_message_dialog_new(GTK_WINDOW(assistant),

                                    GTK_DIALOG_MODAL,

                                    GTK_MESSAGE_INFO,

                                    GTK_BUTTONS_OK,

                                    "Asistente Completado.\n\nDibujar Gr\u00E1fico");



    gtk_window_set_title(GTK_WINDOW(dialog), "Finalizar");

    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);



    switch(gtk_dialog_run(GTK_DIALOG(dialog)))

    {

        case GTK_RESPONSE_OK: // Selección del botón "Si"

            g_print("Aceptar\n\n");

            gtk_widget_destroy(GTK_WIDGET(assistant));

            break;



        default: // Selección del botón cerrar "X"

            g_print("Cerrar\n\n");

            break;

    }



    gtk_widget_destroy(GTK_WIDGET(dialog));

}



/***************************************************************************************/

/*CERRAMOS EL ASISTENTE ANTES DE HORA*/

/***************************************************************************************/

static void on_assistant_close_cancel (GtkWidget *widget, gpointer data)

{

    GtkWidget *dialog;



    dialog = gtk_message_dialog_new(GTK_WINDOW(assistant),

                                    GTK_DIALOG_MODAL,

                                    GTK_MESSAGE_WARNING,

                                    GTK_BUTTONS_YES_NO,

                                    "\u00BFDesea salir de la aplicaci\u00F3n?");



    gtk_window_set_title(GTK_WINDOW(dialog), "Advertencia");

    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);



    switch(gtk_dialog_run(GTK_DIALOG(dialog)))

    {

        case GTK_RESPONSE_YES: // Selección del botón "Si"

            g_print("Si\n");

            gtk_widget_destroy(GTK_WIDGET(assistant));

            break;



        case GTK_RESPONSE_NO: // Selección del botón "No"

            g_print("No \n\n");

            break;



        default: // Selección del botón cerrar "X"

            g_print("Cerrar\n\n");

            break;

    }



    gtk_widget_destroy(GTK_WIDGET(dialog));

}





/***************************************************************************************/

/*TITULO DE LA VENTANA*/

/***************************************************************************************/

static void on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer data)

{

    gint current_page, n_pages;

    gchar *title;



    current_page = gtk_assistant_get_current_page (GTK_ASSISTANT (widget));

    n_pages = gtk_assistant_get_n_pages (GTK_ASSISTANT (widget));



    title = g_strdup_printf ("Asistente (%d de %d)", current_page + 1, n_pages);

    gtk_window_set_title (GTK_WINDOW (widget), title);

    g_free (title);

}





/***************************************************************************************/

/**/

/***************************************************************************************/

static void on_entry_changed (GtkWidget *widget, gpointer data)

{

    GtkAssistant *assistant = GTK_ASSISTANT (data);

    GtkWidget *current_page;

    gint page_number;

    const gchar *text;



//    page_number = gtk_assistant_get_current_page (assistant);

    page_number = 1;

    current_page = gtk_assistant_get_nth_page (assistant, page_number);



    text = gtk_entry_get_text (GTK_ENTRY (widget));



    if (text && text[0])

    {

        gtk_assistant_set_page_complete (assistant, current_page, TRUE);

    }

    else

    {

        gtk_assistant_set_page_complete (assistant, current_page, FALSE);

    }

}





/***************************************************************************************/

/**/

/***************************************************************************************/

static void create_page1 (GtkWidget *assistant)

{

    GtkWidget *label;

    GdkPixbuf *pixbuf;



    label = gtk_label_new ("Este asistente le ayudar\u00E1 a mostrar gr\u00E1ficamente\nlas estad\u00EDsticas obtenidas con sus sensores.\n\nPulse adelante para continuar.");



    gtk_widget_show (label);

    gtk_assistant_append_page (GTK_ASSISTANT (assistant), label);

    gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), label, TRUE);

    gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), label, "P\u00E1gina 1: Bienvenido");

    gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), label, GTK_ASSISTANT_PAGE_INTRO);



    pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);

    gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), label, pixbuf);

    g_object_unref (pixbuf);

}





/***************************************************************************************/

/**/

/***************************************************************************************/

static void create_page2 (GtkWidget *assistant)

{

    GtkWidget *box, *label, *button, *table;

    GdkPixbuf *pixbuf;



  box = gtk_hbox_new (FALSE, 12);



  table = gtk_table_new (4, 4, FALSE);

  //gtk_table_set_col_spacing (GTK_TABLE (table), 10, 10);

  gtk_table_set_row_spacings (GTK_TABLE (table), 40);

  gtk_container_add (GTK_CONTAINER (box), table);

  gtk_container_border_width(GTK_CONTAINER (table), 24);



  label = gtk_label_new ("Seleccione el archivo:");



  g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (on_entry_changed), assistant);



  button = gtk_button_new_with_label ("Examinar...");

  g_signal_connect (G_OBJECT(button), "clicked", G_CALLBACK(abrir_archivo), (gpointer) assistant);



  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);

  gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 2, 3);

  gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 2, 3);



    gtk_widget_show_all (box);

    gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);

    gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "P\u00E1gina 2: Selecciona el fichero");

    gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_CONTENT);



    pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_DIALOG, NULL);

    gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), box, pixbuf);

    g_object_unref (pixbuf);

}





/***************************************************************************************/

/**/

/***************************************************************************************/

static void create_page3 (GtkWidget *assistant)

{



  GtkWidget *box, *calendar1, *calendar2, *table,*label1, *label2;

  GdkPixbuf *pixbuf;



  box = gtk_hbox_new (FALSE, 12);



  table = gtk_table_new (4, 4, FALSE);

  gtk_table_set_row_spacings (GTK_TABLE (table), 40);

  gtk_container_add (GTK_CONTAINER (box), table);

  gtk_container_border_width(GTK_CONTAINER (table), 24);



  label1 = gtk_label_new ("Seleccione la fecha inicial");

  label2 = gtk_label_new ("Seleccione la fecha final");

  entry1 = gtk_entry_new ();

  entry2 = gtk_entry_new ();

  g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (on_entry_changed), assistant);



  calendar1 = gtk_calendar_new ();

  gtk_widget_set_size_request (calendar1, 241, 186);

  gtk_calendar_display_options (GTK_CALENDAR (calendar1),

                                (GtkCalendarDisplayOptions) (GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES) );

  g_signal_connect (G_OBJECT (calendar1), "day_selected", G_CALLBACK (seleccion), assistant);



    calendar2 = gtk_calendar_new ();

    gtk_widget_set_size_request (calendar2, 241, 186);

    gtk_calendar_display_options (GTK_CALENDAR (calendar2),

                                (GtkCalendarDisplayOptions) (GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES) );

    g_signal_connect ((gpointer) calendar2, "day_selected",

                    G_CALLBACK (seleccion2),   NULL);



  gtk_table_attach_defaults (GTK_TABLE (table), label1, 0, 1, 0, 1);

  gtk_table_attach_defaults (GTK_TABLE (table), label2, 1, 2, 0, 1);

  gtk_table_attach_defaults (GTK_TABLE (table), entry1, 0, 1, 1, 2);

  gtk_table_attach_defaults (GTK_TABLE (table), entry2, 1, 2, 1, 2);

  gtk_table_attach_defaults (GTK_TABLE (table), calendar1, 0, 1, 2, 3);

   gtk_table_attach_defaults (GTK_TABLE (table), calendar2, 1, 2, 2, 3);



  gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);

  gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);

  gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "P\u00E1gina 3: Selecciona los datos");

  gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_CONTENT);



  pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG, NULL);

  gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), box, pixbuf);

  g_object_unref (pixbuf);

}







/***************************************************************************************/

/**/

/***************************************************************************************/

static void create_page4_prepare (GtkWidget *assistant);



static void create_page4 (GtkWidget *assistant)

{

    GtkWidget *box, *table, *boton1, *boton2, *boton3, *boton4, *boton5, *boton6, *boton7;

    GdkPixbuf *pixbuf;



    box = gtk_vbox_new (FALSE, 12);



    table = gtk_table_new (6, 6, FALSE);

    gtk_table_set_row_spacings (GTK_TABLE (table), 40);

    gtk_container_add (GTK_CONTAINER (box), table);

    gtk_container_border_width(GTK_CONTAINER (table), 24);



    curva = gtk_curve_new();

    gtk_curve_set_range (GTK_CURVE(curva), -2, 2, -2, 2);



    label1 = gtk_label_new ("valor maximo: ");

    label2 = gtk_label_new ("valor minimo: ");

    label3 = gtk_label_new ("Fecha inicial: ");

    label4 = gtk_label_new ("Fecha final: ");



    //gtk_label_set_text (GTK_LABEL (label1), "valor maximo 2:");



    boton1 =   gtk_button_new_with_label ("Temperaturas 1");

    g_signal_connect (G_OBJECT(boton1), "clicked", G_CALLBACK(curva_temp1), (gpointer) assistant);

    boton2 =   gtk_button_new_with_label ("Temperaturas 2");

    g_signal_connect (G_OBJECT(boton2), "clicked", G_CALLBACK(curva_temp2), (gpointer) assistant);

    boton3 =   gtk_button_new_with_label ("Humedad 1");

    g_signal_connect (G_OBJECT(boton3), "clicked", G_CALLBACK(curva_hume1), (gpointer) assistant);

    boton4 =   gtk_button_new_with_label ("Humedad 2");

    g_signal_connect (G_OBJECT(boton4), "clicked", G_CALLBACK(curva_hume2), (gpointer) assistant);



    boton5 =   gtk_button_new_with_label ("linear");

    g_signal_connect (G_OBJECT(boton5), "clicked", G_CALLBACK(curva_linear), (gpointer) assistant);

    boton6 =   gtk_button_new_with_label ("spline");

    g_signal_connect (G_OBJECT(boton6), "clicked", G_CALLBACK(curva_spline), (gpointer) assistant);

    boton7 =   gtk_button_new_with_label ("free");

    g_signal_connect (G_OBJECT(boton7), "clicked", G_CALLBACK(curva_free), (gpointer) assistant);



    gtk_table_attach_defaults (GTK_TABLE (table), curva, 1, 5, 0, 4);



    gtk_table_attach_defaults (GTK_TABLE (table), label1, 0, 1, 0, 1);

    gtk_table_attach_defaults (GTK_TABLE (table), label2, 0, 1, 3, 4);

    gtk_table_attach_defaults (GTK_TABLE (table), label3, 1, 2, 4, 5);

    gtk_table_attach_defaults (GTK_TABLE (table), label4, 4, 5, 4, 5);



    gtk_table_attach_defaults (GTK_TABLE (table), boton1, 1, 2, 5, 6);

    gtk_table_attach_defaults (GTK_TABLE (table), boton2, 2, 3, 5, 6);

    gtk_table_attach_defaults (GTK_TABLE (table), boton3, 3, 4, 5, 6);

    gtk_table_attach_defaults (GTK_TABLE (table), boton4, 4, 5, 5, 6);



    gtk_table_attach_defaults (GTK_TABLE (table), boton5, 1, 2, 6, 7);

    gtk_table_attach_defaults (GTK_TABLE (table), boton6, 2, 3, 6, 7);

    gtk_table_attach_defaults (GTK_TABLE (table), boton7, 3, 4, 6, 7);





    /*static void curva_linear ();

static void curva_spline ();

static void curva_interpolated ();

static void curva_free ();

*/



    g_signal_connect (G_OBJECT(assistant), "prepare", G_CALLBACK(create_page4_prepare), (gpointer) assistant);







/*

checkbutton = gtk_check_button_new_with_label ("This is optional data, you may continue even if you do not check this");

gtk_box_pack_start (GTK_BOX (box), checkbutton, FALSE, FALSE, 0);

*/



    gtk_widget_show_all (box);

    gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);

    gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);

    gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "P\u00E1gina 4: Selecciona fechas");

    gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_CONTENT);



    pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);

    gtk_assistan
[cut at 20 KB]


Any sugestions about the curve???[/code]
Back to top
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 270
Location: INDIA

PostPosted: Thu Apr 09, 2009 4:03 am    Post subject: Reply with quote

Hi

can i have complete code.
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Thu Apr 09, 2009 2:16 pm    Post subject: Reply with quote

Hello.

GtkCurve widget is considered too specialized to be included in Gtk+ and is not maintained properly (it waits to be added to some 3rd party package).

I'm not sure why would this widget interpolate only first 9 points, but I didn't manage to get it working on my machine at all, so I cannot investigate it further.

And to assist you with writing your display widget, I created simple application that will show you how to create your own graphs using cairo library. I didn't comment it, since I'm really short with spare time, but if you have any questions, post back.

Usage instructons: Clicking on a drawing area will place points there. Shift clicking on a point will remove it. And that's it.

Code: (C)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <gtk/gtk.h>

enum
{
    SPLINE_NONE = 0,
    SPLINE_LINE,
    SPLINE_CATMULL_ROM,
    NO_TYPES
};

typedef struct _Data Data;
struct _Data
{
    GArray    *points;
    gboolean   visible[NO_TYPES];
    gdouble    spline_factor;
    GtkWidget *draw;
};

static void
cb_value( GtkSpinButton *button,
          Data          *data )
{
    data->spline_factor = gtk_spin_button_get_value( button );
    gtk_widget_queue_draw( data->draw );
}

static void
cb_toggled( GtkToggleButton *button,
            Data            *data )
{
    gint index =
        GPOINTER_TO_INT( g_object_get_data( G_OBJECT( button ), "type" ) );

    data->visible[index] = gtk_toggle_button_get_active( button );

    gtk_widget_queue_draw( data->draw );
}

static void
draw_points( cairo_t *cr,
             gdouble *coords,
             gint     size )
{
    gint i;

    cairo_set_source_rgb( cr, 1, 0, 0 );
    for( i = 0; i < size; i += 2 )
    {
        cairo_move_to( cr, coords[i], coords[i + 1] );
        cairo_arc( cr, coords[i], coords[i + 1], 2, 0, 2 * G_PI );
    }
    cairo_stroke( cr );
}

static void
draw_line( cairo_t *cr,
           gdouble *coords,
           gint     size )
{
    gint i;

    cairo_set_source_rgb( cr, 0, 0, 1 );
    cairo_move_to( cr, coords[0], coords[1] );
    for( i = 2; i < size; i += 2 )
    {
        cairo_line_to( cr, coords[i], coords[i + 1] );
    }
    cairo_stroke( cr );
}

static void
draw_catmull_rom( cairo_t *cr,
                  gdouble *coords,
                  gint     size,
                  double   spline_factor )
{
    gint i;
    gdouble ctrl_points[4];

    /* Initial calculations */
   
ctrl_points[2] = ( coords[2] - coords[0] ) / spline_factor;
    ctrl_points[3] = ( coords[3] - coords[1] ) / spline_factor;

    cairo_set_source_rgb( cr, 0, 1, 0 );
    cairo_move_to( cr, coords[0], coords[1] );
    for( i = 2; i < size - 2; i += 2 )
    {
        ctrl_points[0] = ctrl_points[2];
        ctrl_points[1] = ctrl_points[3];
        ctrl_points[2] = ( coords[i + 2] - coords[i - 2] ) / spline_factor;
        ctrl_points[3] = ( coords[i + 3] - coords[i - 1] ) / spline_factor;

        cairo_curve_to( cr, coords[i - 2] + ctrl_points[0],
                            coords[i - 1] + ctrl_points[1],
                            coords[i]     - ctrl_points[2],
                            coords[i + 1] - ctrl_points[3],
                            coords[i],
                            coords[i + 1] );
    }

    /* Draw the last segment */
   
ctrl_points[0] = ctrl_points[2];
    ctrl_points[1] = ctrl_points[3];
    ctrl_points[2] = ( coords[i] - coords[i - 2] ) / spline_factor;
    ctrl_points[3] = ( coords[i + 1] - coords[i - 1] ) / spline_factor;

    cairo_curve_to( cr, coords[i - 2] + ctrl_points[0],
                        coords[i - 1] + ctrl_points[1],
                        coords[i]     - ctrl_points[2],
                        coords[i + 1] - ctrl_points[3],
                        coords[i],
                        coords[i + 1] );

    cairo_stroke( cr );
}

static gboolean
cb_expose( GtkWidget      *draw,
           GdkEventExpose *expose,
           Data           *data )
{
    cairo_t  *cr;
    gdouble  *coords;
    gint      i;

    if( data->points->len == 0 )
        return( FALSE );

    /* Draw spline */
   
cr = gdk_cairo_create( draw->window );
    cairo_set_line_width( cr, 5 );

    coords = (gdouble *)data->points->data;

    /* Special case - if only one point is present, draw it no matter what
     * spline type is set. */
   
if( data->points->len == 2 && data->visible[SPLINE_NONE] )
    {
        draw_points( cr, coords, data->points->len );
        cairo_destroy( cr );
        return( FALSE );
    }

    if( data->visible[SPLINE_LINE] )
        draw_line( cr, coords, data->points->len );

    if( data->visible[SPLINE_CATMULL_ROM] )
        draw_catmull_rom( cr, coords, data->points->len, data->spline_factor );

    if( data->visible[SPLINE_NONE] )
        draw_points( cr, coords, data->points->len );
   
    cairo_destroy( cr );

    return( FALSE );
}

static gboolean
cb_press( GtkWidget      *draw,
          GdkEventButton *event,
          Data           *data )
{
    if( event->state & GDK_SHIFT_MASK )
    {
        gint     i;
        gdouble *coords = (gdouble *)data->points->data;

        for( i = 0; i < data->points->len - 1; i += 2 )
        {
            if( event->x > ( coords[i] - 5 ) &&
                event->x < ( coords[i] + 5 ) )
            {
                if( event->y > ( coords[i + 1] - 5 ) &&
                    event->y < ( coords[i + 1] + 5 ) )
                {
                    g_print( "Delete point at (%f, %f)\n", event->x, event->y );
                    g_array_remove_range( data->points, i, 2 );
                    break;
                }
            }
        }
    }
    else
   
{
        gdouble point[] = { event->x, event->y };

        g_print( "Create point at (%f, %f)\n", event->x, event->y );
        g_array_append_vals( data->points, (gconstpointer)point, 2 );
    }

    gtk_widget_queue_draw( draw );

    return( TRUE );
}

int
main( int    argc,
      char **argv )
{
    GtkWidget *window;
    GtkWidget *hbox;
    GtkWidget *vbox, *vbox_main;
    GtkWidget *frame;
    GtkWidget *button;
    GtkWidget *draw;
    Data       data;


    data.points = g_array_sized_new( FALSE, FALSE, sizeof( gdouble ), 10 );
    data.spline_factor = 5;
    data.visible[0] = TRUE;
    data.visible[1] = TRUE;
    data.visible[2] = FALSE;

    gtk_init( &argc, &argv );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size( GTK_WINDOW( window ), 400, 300 );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    hbox = gtk_hbox_new( FALSE, 6 );
    gtk_container_add( GTK_CONTAINER( window ), hbox );

    frame = gtk_frame_new( "Drawing area" );
    gtk_box_pack_start( GTK_BOX( hbox ), frame, TRUE, TRUE, 0 );

    draw = gtk_drawing_area_new();
    data.draw = draw;
    gtk_widget_add_events( draw, GDK_BUTTON_PRESS_MASK );
    g_signal_connect( G_OBJECT( draw ), "expose-event",
                      G_CALLBACK( cb_expose ), &data );
    g_signal_connect( G_OBJECT( draw ), "button-press-event",
                      G_CALLBACK( cb_press ), &data );
    gtk_container_add( GTK_CONTAINER( frame ), draw );

    vbox_main = gtk_vbox_new( FALSE, 6 );
    gtk_box_pack_start( GTK_BOX( hbox ), vbox_main, FALSE, FALSE, 0 );

    /* Interpolation type */
   
frame = gtk_frame_new( "Interpolation" );
    gtk_box_pack_start( GTK_BOX( vbox_main ), frame, FALSE, FALSE, 0 );

    vbox = gtk_vbox_new( TRUE, 6 );
    gtk_container_add( GTK_CONTAINER( frame ), vbox );

    button = gtk_check_button_new_with_label( "Points" );
    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
    g_object_set_data( G_OBJECT( button ), "type",
                       GINT_TO_POINTER( SPLINE_NONE ) );
    g_signal_connect( G_OBJECT( button ), "toggled",
                      G_CALLBACK( cb_toggled ), &data );
    gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );

    button = gtk_check_button_new_with_label( "Lines" );
    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
    g_object_set_data( G_OBJECT( button ), "type",
                       GINT_TO_POINTER( SPLINE_LINE ) );
    g_signal_connect( G_OBJECT( button ), "toggled",
                      G_CALLBACK( cb_toggled ), &data );
    gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );

    button = gtk_check_button_new_with_label( "Cardinal spline" );
    g_object_set_data( G_OBJECT( button ), "type",
                       GINT_TO_POINTER( SPLINE_CATMULL_ROM ) );
    g_signal_connect( G_OBJECT( button ), "toggled",
                      G_CALLBACK( cb_toggled ), &data );
    gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );

    /* Spline factor */
   
frame = gtk_frame_new( "Cardinal spline factor" );
    gtk_box_pack_start( GTK_BOX( vbox_main ), frame, FALSE, FALSE, 0 );

    button = gtk_spin_button_new_with_range( 0.1, 100, 0.1 );
    gtk_spin_button_set_numeric( GTK_SPIN_BUTTON( button ), TRUE );
    gtk_spin_button_set_value( GTK_SPIN_BUTTON( button ), 5. );
    g_signal_connect( G_OBJECT( button ), "value-changed",
                      G_CALLBACK( cb_value ), &data );
    gtk_container_add( GTK_CONTAINER( frame ), button );


    gtk_widget_show_all( window );

    gtk_main();

    g_array_free( data.points, TRUE );

    return( 0 );
}
Back to top
psaval
Familiar Face


Joined: 20 Feb 2009
Posts: 7

PostPosted: Fri Apr 17, 2009 7:23 pm    Post subject: Reply with quote

If you want my newest code, just email me to psaval@hotmail.com

And thanks for your code, is impressive, i will make good use of it. Thanks again, i'm gratefull.

P.D.: Sorry for my english, but i'm spanish
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> Project Showcase 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