Re: now that we're on GtkDrawingArea... =)



Sorry about not answering. lost the messages.
here is a bit of the program, done with help of glade.

The program is to control 2 stepper motors with the paralell port, that will move a pen along x and y axis.
As the steppers move, the program also draws to a gtk drawing area widget.

robot.c
/* Variables from main()*/

GtkWidget *window1;
    GtkWidget *area;		
    static pthread_t hilo;
    grc *color=NULL;   /*a struct that has a pointer to an array. I messed up trying to do a pointer to an array of 5 pointers...  would it be **color[5]? or *(*color[5])? */

    GdkGCValues Color[5]; /* The "plotter" has 5 colors*/
    static GdkPixmap *mapa = NULL; /* i draw first to a pixmap*/
    guchar i;
    extern sem_t sema; /*messing with semaphores. its function is in another file, hence the extern.*/

    sem_init(&sema, 0, 0);

...
g_thread_init(NULL);
...
    gtk_init(&argc, &argv);
...
color=g_malloc(sizeof (grc));

    window1 = create_window1();
    gtk_widget_show(window1);
    area = gtk_object_get_data(GTK_OBJECT(window1), "dibujo"); 
    gtk_drawing_area_size(area, ANCHO, ALTO); 
    gtk_widget_set_events(area, GDK_EXPOSURE_MASK
			  | GDK_LEAVE_NOTIFY_MASK
			  | GDK_BUTTON_PRESS_MASK
			  | GDK_BUTTON_RELEASE_MASK
			  | GDK_POINTER_MOTION_MASK
			  | GDK_POINTER_MOTION_HINT_MASK);
    
    mapa =
	gdk_pixmap_new(window1->window, area->allocation.width,
		       area->allocation.height, -1); /*this generates the assertion if i change window1 for area, with the message ...Drawable!=NULL | depth!=-1, so area->window has NULL */

    for (i = 0; i < 5; i++) {  /*allocate colors*/
	//Color[i] = malloc(sizeof(GdkGCValues));
	Color[i].background.red = 65535;
	Color[i].background.green = 65535;
	Color[i].background.blue = 65535;
	Color[i].background.pixel = (256*Color[i].background.red+Color[i].background.green)*256+Color[i].background.blue;
	    Color[i].function=GDK_AND;
	    //Color[i].line_width=10;
    }
    Color[0].foreground.red = 0;
    Color[0].foreground.green = 0;
    Color[0].foreground.blue = 0;
    Color[0].foreground.pixel = (Color[0].foreground.red*256+Color[0].foreground.green)*256+Color[0].foreground.blue;
    gdk_colormap_alloc_color(gtk_widget_get_colormap(window1),&Color[0].foreground,TRUE,TRUE);

/*Do this 5 times for each color*/

...
/*Set the Graphics context*/
    for (i = 0; i <=4; i++) {
	color->gc[i] = gdk_gc_new_with_values(window1->window, &(Color[i]),GDK_GC_FOREGROUND|GDK_GC_BACKGROUND|GDK_GC_FUNCTION);
    }

    gtk_object_set_data(GTK_OBJECT(area), "mapa", mapa); /*store the pixmap pointer*/
    gtk_object_set_data(GTK_OBJECT(area), "gc", color); /* and the GdkGC vector pointer in the Area Widget*/
    gdk_draw_rectangle (mapa,
                      area->style->white_gc,
                      TRUE,
                      0, 0,
                      area->allocation.width,
                      area->allocation.height);
    pthread_create(&hilo, NULL, p, window1); /*Clean the drawable, like the scribble example. Any other way?*/
    gdk_threads_enter();
    gtk_main();
    gdk_threads_leave();
    return 0;
}
....

The part of the thread that draw the points.

rec.x = x-1;
	rec.y = y-1;
	rec.width = 3;
	rec.height = 3;
	gdk_threads_enter();
	gdk_draw_point(plano, cont->gc[col], x, y);
	gtk_widget_draw(area, &rec);
	gdk_flush();
	gdk_threads_leave();
	nanosleep(&nanos, &resto);

/* x and y are globals set when there is a release event on the drawing area. col is an index meaning the color.
gdk_flush, from something told in the mails files. Thank you =) 
(though i would love to see a mail interface to ask the server for past mails)*/

Also, the expose event generated an assertion too.

..........
from the interface generation:

....
gtk_box_pack_start (GTK_BOX (mainvbox), notebook1, TRUE, TRUE, 0);
....
gtk_container_add (GTK_CONTAINER (notebook1), hpaned1);
....
gtk_paned_pack1 (GTK_PANED (hpaned1), scrolledwindow1, FALSE, TRUE);
....
gtk_container_add (GTK_CONTAINER (scrolledwindow1), viewport1);


dibujo = gtk_drawing_area_new ();
  gtk_widget_ref (dibujo);
  gtk_object_set_data_full (GTK_OBJECT (window1), "dibujo", dibujo,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (dibujo);
  gtk_container_add (GTK_CONTAINER (viewport1), dibujo);
....

It works ok now, but it just looks ugly.
specially that extern.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]