Re: [gtk-list] draw_line



Sune Fischer wrote:
> 
> Hi
> 
> I need to know how to draw a line between to points, and that funktion
> is I belive
>      gdk_draw_line(...)
> but nothing happens.
> I have tried to hacke one of the examples that came with gtk, it draws
> a rectangle but uses a function to "realize" the rectangle.
>       gdk_draw_rectangle (...)
>       gtk_widget_draw (..., &rectangle)
> 
> I don't know what to do to "realize" this:
> gdk_draw_line (pixmap, drawing_area->style->black_gc, 0, 0, 56, 78);
> 
> I hope someone can help me with this problem?
> 

Maybe that helps. I used gdk_draw_line in my function 'draw_axis'

Chris



-- 
====================================================================
Ch. B. Westermann

University of Bern         
Physikalisches Institut      
Sidlerstrasse 5            phone: +41(0)31 6314417
3012 Bern                    fax: +41(0)31 6314405
Switzerland                email: christian.westermann@phim.unibe.ch
 			   http://phimcasymir.unibe.ch/casymir/cas_SS.html
#include <gtk/gtk.h>

static GdkPixmap *pixmap = NULL;
static GtkWidget *window;

void delete_event(GtkWidget *widget,gpointer data)
{
  gtk_main_quit();
}


/*draw two axis*/
static void draw_axis(GtkWidget *widget)
{
	GdkRectangle update_rect;
	gint w = widget->allocation.width;
	gint h = widget->allocation.height;
	
	update_rect.x=0;
	update_rect.y=0;
	update_rect.width=w;
	update_rect.height=h;
	
	gdk_draw_line(pixmap,
					  widget->style->black_gc,
					  10,h-10,
					  w-10,h-10);
	gdk_draw_line(pixmap,
					  widget->style->black_gc,
					  10,h-10,
					  10,10);
	gtk_widget_draw(widget,&update_rect);
}

/*create new backing pixmap of 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);
	draw_axis(widget);
	
	return TRUE;
}
							 
/*redraw screen from 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);
	return FALSE;
}


/*create main window*/
static void create_main_window(void)
{
	GtkWidget *main_vbox;
	GtkWidget *drawing_area;
	
	/*create main window, add signals*/
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_name(window,"Test Window");
	gtk_widget_set_uposition(window,600,300);
	
	gtk_signal_connect(GTK_OBJECT(window),"destroy",
							 GTK_SIGNAL_FUNC(gtk_main_quit),NULL);
	gtk_signal_connect(GTK_OBJECT(window),"delete-event",
							 GTK_SIGNAL_FUNC(delete_event),NULL);
	
	gtk_container_border_width(GTK_CONTAINER(window),3);
	
	/*create main vbox (menu,main window),add it to the window and show it*/
	main_vbox = gtk_vbox_new(FALSE,1);
	gtk_container_border_width(GTK_CONTAINER(main_vbox),1);
	gtk_container_add(GTK_CONTAINER(window),main_vbox);
	gtk_widget_show(main_vbox);	
		
	/*create drawing-area*/
	drawing_area = gtk_drawing_area_new();
	gtk_drawing_area_size(GTK_DRAWING_AREA(drawing_area),600,300);
	gtk_box_pack_start(GTK_BOX(main_vbox),drawing_area,TRUE,TRUE,0);
		
	/*signals for pixmap handling*/
	gtk_signal_connect(GTK_OBJECT(drawing_area),"expose-event",
							 GTK_SIGNAL_FUNC(expose_event),NULL);
	gtk_signal_connect(GTK_OBJECT(drawing_area),"configure-event",
							 GTK_SIGNAL_FUNC(configure_event),NULL);
	
	gtk_widget_show(drawing_area);
	
	gtk_widget_show(window);
}
		

int main(int argc,char *argv[])
{
	gtk_init(&argc,&argv);
	create_main_window();
	gtk_main();
	return 0;
}


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