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

Re: How to draw a simple line?



Eric Harlow wrote:
> 
> Go here:
> 
> http://www.bcpl.net/~eharlow/book
> 
> Take the code for chapter 10 and 12.  It should give
> you what you want.

First of all I would like to thank Eric for his magnificent book! 
Without his book I would never started GTK+ programming.  I was looking
for something to program GUI in Linux and I visited my local
bookstore... I saw the book and by buying it i took my first steps into
the GTK world!
Keep the good work going guys!

Next thing is that i looked at the code from chapter 10 and based me on
it to do the drawing in my program.  I haven't figured out how things
really work but i managed to get a line on my drawing_area.  I have two
basic questions:

1) I have packed my drawing area in a frame. The distance between the
inside of the frame is rather small and that looks quite ugly.  Is there
a way to add some border to the inside of a frame or to the outside of a
GtkDrawingArea ?  The reference doc didn't give me any standard
solutions:-(

2) I have based my program on the clock example.  In that example the
function 

/* --- Repaint every second --- */
 gtk_timeout_add (1000, mot_paint_xy_graph, drawing_area);

is called to update the drawing area every second.  I only want to draw
my lines at startup and at the expose_event.  How do i do this ?

This is my code:

/*
 * drawing.c
 */
 
#include <gtk/gtk.h>
#include "drawing.h"

/* --- Backing pixmap for drawing area  --- */
static GdkPixmap *pixmap = NULL;

gint mot_paint_xy_graph (gpointer data)
{
 GtkWidget*    drawing_area = (GtkWidget *) data;
 GdkRectangle  update_rect;

 gdk_draw_line (pixmap, drawing_area->style->white_gc,
                   1, 1,
                   100,
                   100);
 update_rect.x = 0;
 update_rect.y = 0;
 update_rect.width = drawing_area->allocation.width;
 update_rect.height = drawing_area->allocation.height;

 /* --- And then draw it (calls expose event) --- */
 gtk_widget_draw (drawing_area, &update_rect);

 return (TRUE);
}


/* 
 * configure_event
 *
 * Create a new backing pixmap of the appropriate size 
 * Of course, this is called whenever the window is 
 * resized.  We have to free up things we allocated.
 */
static gint configure_event (GtkWidget *widget, GdkEventConfigure
*event)
{
    g_print ("configure_event\n");
    /* --- Free background if we created it --- */
    if (pixmap) {
        gdk_pixmap_unref (pixmap);
    } 

    /* --- Create a new pixmap with new size --- */
    pixmap = gdk_pixmap_new (widget->window,
                widget->allocation.width,
                widget->allocation.height,
                -1);

    return TRUE;
}

/*
 * expose_event
 *
 * When the window is exposed to the viewer or 
 * the gdk_widget_draw routine is called, this 
 * routine is called.  Copies the background pixmap
 * to the window.
 */
gint expose_event (GtkWidget *widget, GdkEventExpose *event)
{
    g_print ("expose_event\n");

    /* --- Copy pixmap to the window --- */
    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);

    return FALSE;
}

 
GtkWidget *mot_new_drawing_area(void)
{
 GtkWidget *drawing_area;
 GtkWidget *frame;
 
 frame = gtk_frame_new("Intensity visualisation");
 gtk_container_set_border_width(GTK_CONTAINER(frame), 10);
 
 drawing_area=gtk_drawing_area_new();
 gtk_drawing_area_size(GTK_DRAWING_AREA(drawing_area), 400, 300);
 gtk_widget_show(drawing_area);
 
 /* --- Signals used to handle backing pixmap --- */
 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);
		
 /* --- Repaint every second --- */
 gtk_timeout_add (1000, mot_paint_xy_graph, drawing_area);
 
 gtk_container_add(GTK_CONTAINER(frame), drawing_area);
 
 return frame;
}


As you see I copied much of the code from Eric Harlow.  I haven't
figured out how things really work... can someone give me some hints on
reducing the code?  I guess I'm doing a lot of unneccesary work here
just to draw my lines... what things can I leave out without affecting
my drawing?

Tnx!
Bart

-- 
Bart Vandewoestyne		http://hello.to/MC303		
Hugo Verrieststraat 48		Bart.Vandewoestyne@skynet.be
8550 ZWEVEGEM			ICQ# 21275340
BELGIUM - EUROPE		nick: MC303
Phone: +32(0)56/75.48.11
-------------------------------------------------------------
"If we knew what it was we were doing, it would not
 be called research, would it?"
				-- Albert Einstein --




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