Helppppppp :°°°(°° [GTK] - Programmation
Marsh Posté le 21-11-2001 à 20:00:53
Si ça peut t'aider un truc bien sale qui utilise imlib (mais à priori tu n'en as pas besoin ou alors utilise gdkPixbuf).
L'idée est qui faut utiliser les primitive du gdk ;pour tracer un point regarde du côté des primitives GDKs
gdk_draw_point (pixmap, context, x,y);
le context contient la couleur qui faut allouer auparavant:
gdk_colormap_alloc_color( gdk_colormap_get_system(),colors,FALSE,TRUE);
gdk_gc_set_foreground (context, colors);
Bonne chance
#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk_imlib.h>
int neuro_number ;
gchar *name_coord_file ;
gchar *name_neuro_file ;
void trace( GtkWidget *widget);
void save_image_file ( GtkWidget *widget, gpointer *dummy);
void save_image( GtkWidget *widget, GtkFileSelection *fs) ;
gfloat absf(gfloat value);
static GdkPixmap *pixmap = NULL;
gfloat absf(gfloat value) {
if (value > 0 ) return value;
else return -value;
}
void callback( GtkWidget *widget,
gpointer data )
{
g_print ("Hello again - %s was pressed\n", (char *) data);
}
void callback_save_image( GtkWidget *widget,
gpointer data_pixmap)
{
GdkImlibImage *image;
/* gint width,
height;
gdk_drawable_get_size(GDK_DRAWABLE(pixmap),
&width,
&height);
image = gdk_imlib_create_image_from_drawable(pixmap
,NULL
,0
,0
,width
,height);*/
}
/* Create a new backing pixmap of the appropriate size */
static gint configure_event( GtkWidget *widget,
GdkEventConfigure *event )
{
if (pixmap)
gdk_pixmap_unref(pixmap);
pixmap = gdk_pixmap_new(widget->window,
widget->allocation.width,
widget->allocation.height,
-1);
gdk_draw_rectangle (pixmap,
widget->style->white_gc,
TRUE,
0, 0,
widget->allocation.width,
widget->allocation.height);
return TRUE;
}
/* Redraw the screen from the backing pixmap */
static gint expose_event( GtkWidget *widget,
GdkEventExpose *event )
{
gdk_draw_pixmap(widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
pixmap,
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
/* trace(widget);*/
return FALSE;
}
void callback_trace(GtkWidget *widget,
gpointer data)
{
trace(*(GtkWidget **) data);
}
/* this function open a file descriptor */
void save_image_file ( GtkWidget *widget,
gpointer *dummy)
{
GtkWidget *file_selector;
/* Create a new_file_selector */
file_selector = gtk_file_selection_new ("Save file as" );
gtk_signal_connect (GTK_OBJECT (file_selector), "destroy",
(GtkSignalFunc) gtk_widget_destroy, &file_selector);
/* Connect the ok_button to file_ok_sel function */
gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
"clicked", (GtkSignalFunc) save_image, (gpointer) file_selector);
/* Connect the cancel_button to destroy the widget */
gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION
(file_selector)->cancel_button),
"clicked", (GtkSignalFunc) gtk_widget_destroy,
GTK_OBJECT (file_selector));
/* Lets set the filename, as if this were a save dialog, and we are giving
a default filename */
/* gtk_file_selection_set_filename (GTK_FILE_SELECTION(file_selector),
descriptor_default);*/
gtk_widget_show(file_selector);
}
void save_image( GtkWidget *widget, GtkFileSelection *fs ) {
gint width,
height;
GdkImlibSaveInfo param ;
gchar *image_name;
image_name = (char *) gtk_file_selection_get_filename(GTK_FILE_SELECTION (fs) );
param.quality = 256; /* Highest quality for jpeg */
param.scaling = 1024; /* ps file*/
param.xjustification = 512; /* ps file*/
param.yjustification = 1024; /* ps file*/
param.page_size = PAGE_SIZE_A4; /* ps file*/
param.color = '1'; /* ps file*/
/* gdk_drawable_getsize(GDKDRAWABLE(pixmap), &width, &height); */
printf("\n\nLe nom est : %s \n\n",image_name);
gdk_imlib_save_image( gdk_imlib_create_image_from_drawable(pixmap
,NULL
,0
,0
,800 /* GTK_WIDGET(pixmap)->allocation.width */ /* width*/
,600 /* GTK_WIDGET(pixmap)->allocation.height */ /*height*/),
image_name,
¶m);
gtk_widget_destroy( (GtkWidget *) gtk_widget_get_toplevel(widget) );
}
void trace( GtkWidget *widget)
{
FILE *coord_file;
FILE *neuro_file;
gint i;
gint init_colors_number;
gint neuro;
gint compteur_lignes;
gint border = 10;
gint taille_buffer = 1024;
gfloat x_max,x_min,x_tampon, y_min, y_max, y_tampon;
gfloat lambda;
gint max_neuro,temp_neuro;
int widget_height,
widget_width;
char buffer[1024];
GdkColor *colors;
GdkGC *context;
context = malloc(sizeof(GdkGC));
colors = malloc(sizeof(GdkColor));
neuro_file = fopen(name_neuro_file,"r" );
if (neuro_file == NULL) {
g_print("The neuro file could not be open\n" );
exit(1);
}
fgets(buffer,taille_buffer,neuro_file);
max_neuro = atoi( (char *) strtok(buffer," " ));
compteur_lignes = 0;
while (!feof(neuro_file)) {
fgets(buffer,taille_buffer,neuro_file);
temp_neuro = atoi( (char *) strtok(buffer," " ));
if ( temp_neuro > max_neuro ) max_neuro = temp_neuro ;
compteur_lignes ++;
}
neuro_number = max_neuro ;
fclose(neuro_file);
/* we print both pillars in the room*/
gdk_draw_rectangle( pixmap,
widget->style->black_gc,
TRUE,
325,
600 - 250,
80,
80);
gdk_draw_rectangle( pixmap,
widget->style->black_gc,
TRUE,
720,
600 - 275,
80,
80);
/* g_print( " taille_fichier: %d\n x_min: %e\n y_min: %e\n x_max: %e\n y_max: %e\n",compteur_lignes,x_min,y_min,x_max,y_max);*/
widget_height = widget->allocation.height;
widget_width = widget->allocation.width;
/* We print the points */
coord_file = fopen(name_coord_file,"r" );
neuro_file = fopen(name_neuro_file,"r" );
if (neuro_file == NULL || coord_file == NULL ) {
g_print("The data file could not be open" );
}
while(!feof(coord_file) && !feof(neuro_file) ) {
/* We get the coordinates*/
fgets(buffer,taille_buffer,coord_file);
x_tampon = atof( (char *) strtok(buffer,";" ));
y_tampon = atof( (char *) strtok(NULL,";" ));
x_tampon = x_tampon *widget_width/8000 ; /* We scale the data to the size of the drawing window */
y_tampon = y_tampon *widget_height/6000 ;
/*We get the number of the neuron */
fgets(buffer,taille_buffer,neuro_file);
neuro = atoi( (char *) strtok(buffer," " ));
/* We print the small squares*/
context = gdk_gc_new(gtk_widget_get_parent_window(widget));
lambda = ( float) ( (float) neuro / neuro_number ) ;
(*colors).blue = ( int ) ( 65300 * lambda * absf( 0.5 - lambda ) * 2 ) ;
(*colors).red = ( int ) ( 65300 * 4 * (1-lambda) * lambda );
(*colors).green = ( int )( 65300 * ( 1-lambda ) * absf( 0.5 - lambda ) * 2 );
(*colors).pixel = 50 ;
printf("poids %d : %e %d %d %d \n",neuro, lambda ,(int ) ( 65300 * lambda * absf( 1/2 - lambda )*2 ) , ( int ) ( 65300 * 4 * (1-lambda) * lambda ) , ( int )( 65300 * ( 1-lambda ) * absf( 1/2 - lambda )*2) );
gdk_colormap_alloc_color( gdk_colormap_get_system(),colors,FALSE,TRUE);
gdk_gc_set_foreground (context, colors);
/* gdk_draw_point (pixmap,
context,
x_tampon,
y_tampon);*/
gdk_draw_rectangle( pixmap,
context,
TRUE,
x_tampon-1,
600 - y_tampon + 1,
3,
3);
}
free(context);
free(colors);
fclose(coord_file);
fclose(neuro_file);
gtk_widget_queue_draw ( widget );
}
void quit( GtkWidget *widget)
{
gtk_main_quit();
}
/* another callback */
gint delete_event( GtkWidget *widget,
GdkEvent *event,
GtkWidget data )
{
gtk_main_quit();
return(FALSE);
}
gint main( gint argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *box1;
GtkWidget *box2;
GtkWidget *drawing_area;
GtkWidget *frame;
gdouble i;
char test[] = "putain de merde";
/* We gave the files name NORMALLY THESE ARE ARGUMENTS*/
if (argc < 3 ) {
printf("\n\nUsage: plotroom coord_file_location neuro_file_location\n\n" );
exit(1) ;
}
name_coord_file = (gchar *) (argv[1]); /* "/home/myuki/envoi/data_final.txt";*/
name_neuro_file = (gchar *) (argv[2]); /*"/home/myuki/programs/divers/neural/data/result.txt";*/
/**** Never forget this !!!! ****/
gtk_init (&argc, &argv);
gdk_imlib_init();
/**** Main window ****/
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Super Display By " );
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (delete_event), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/**** The general box ****/
box2 = gtk_hbox_new(FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box2);
/**** Frame area ****/
frame = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(frame),GTK_SHADOW_ETCHED_OUT);
gtk_box_pack_start (GTK_BOX(box2), frame, TRUE, TRUE, 0);
/**** Button Box ****/
box1 = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(box2),box1,TRUE,TRUE,0);
/**** Display Button ****/
button = gtk_button_new_with_label ("Dipslay" );
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (callback_trace), (gpointer) &drawing_area);
gtk_box_pack_start(GTK_BOX(box1), button, FALSE, FALSE, 0);
gtk_widget_show(button);
/**** Save Button ****/
button = gtk_button_new_with_label ("Save As" );
gtk_signal_connect(GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC ( save_image_file ), (gpointer) NULL );
gtk_box_pack_start(GTK_BOX(box1), button, FALSE, FALSE, 0);
gtk_widget_show(button);
/**** Exit Button ****/
button = gtk_button_new_with_label ("Quit" );
gtk_box_pack_end(GTK_BOX(box1), button, FALSE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (quit), NULL );
gtk_widget_show(button);
/**** Display area ****/
drawing_area = gtk_drawing_area_new ();
gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 800, 600);
gtk_container_add (GTK_CONTAINER (frame),drawing_area);
gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
(GtkSignalFunc) expose_event, NULL);
gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
(GtkSignalFunc) configure_event, NULL);
/* Event signals */
gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
/* Show the final components */
gtk_widget_show (drawing_area);
gtk_widget_show(frame);
gtk_widget_show(box2);
gtk_widget_show(box1);
gtk_widget_show (window);
gtk_main ();
return(0);
}
[edtdd]--Message édité par zeltron--[/edtdd]
Marsh Posté le 22-11-2001 à 10:01:41
zeltron a écrit a écrit : Si ça peut t'aider un truc bien sale qui utilise imlib (mais à priori tu n'en as pas besoin ou alors utilise gdkPixbuf). L'idée est qui faut utiliser les primitive du gdk ;pour tracer un point regarde du côté des primitives GDKs gdk_draw_point (pixmap, context, x,y); |
oki vais i aller voir de ce cote mais bon kan meme si on est obliger de faire a comme ca je penssait ken GTK on pouvais le faire mais ce ki est
tu ma donne des chemins ou chercher alors on est repartit
Marsh Posté le 21-11-2001 à 15:11:34
je cherche une tuto un exemple une idee
pour creer une drawingarea et i afficher un pixel de couleur
le tout en GTK
la tuto officiel (sur www.gtk.org) ne traite pas de ce chapitre le plan est la mais kan on vas a la rub il est marque en anglais k'il cherchent du peuple pour faire cettte partie de la tuto :°°°(°°
si vous savez ou je pourrais trouver de l'info la dessus car ca me fait grave chier de faire un mix X11 GTK pour ke ca marche