GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 01, 2014 7:49 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Drawing cairo with other widgets
PostPosted: Tue Feb 28, 2012 8:19 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
My question is :

I want to draw something in the middle of a gtk window (programming in c gtk 2.24) with menubars and toolbars over it.
When i check this example on zetcode i try to add menubars and toolbars to the window but they overidde the other widgets.What i want to do is specify an area in the middle where its going to be drawn without messing around with the other widgets.To me it looks like the issue is cr = gdk_cairo_create(widget->window); where window should be replaced with an invisible widget but i dont have a clue since im new to cairo.

Code:
#include <cairo.h>
#include <gtk/gtk.h>

static gboolean
on_expose_event(GtkWidget *widget,
    GdkEventExpose *event,
    gpointer data)
{
  cairo_t *cr;
  cr = gdk_cairo_create(widget->window);

  gint i;
  for ( i = 1; i <= 10; i++) {
      cairo_set_source_rgba(cr, 0, 0, 1, i*0.1);
      cairo_rectangle(cr, 50*i, 20, 40, 40);
      cairo_fill(cr); 
  }

  cairo_destroy(cr);
  return FALSE;
}


int
main (int argc, char *argv[])
{
  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  g_signal_connect(G_OBJECT(window), "expose-event",
      G_CALLBACK(on_expose_event), NULL);
  g_signal_connect(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 590, 90);
  gtk_window_set_title(GTK_WINDOW(window), "transparency");

  gtk_widget_set_app_paintable(window, TRUE);
  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

Any suggestions?


Top
 Profile  
 
 Post subject: Re: Drawing cairo with other widgets
PostPosted: Thu Mar 01, 2012 10:15 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
No you want to leave widget->window as it is because its the window component of the widget structure that you want to get for a cairo context.
What you will find better is, rather than sending the whole window as the widget to be used by on_expose_event, create a Gtk Drawing Area in your window and then connect it to the on_expose_event handler. You then need to set a clip region so that drawing is confined to the window of the Drawing area (thus not messing with your other widgets).
If you want you image centred then you will need to know where this centre is. If you are centring about the drawing area alone then you can get this as (widget->allocation.width)/2,(widget->allocation.height)/2. If you want to centre it about the entire window you will need to know the dimensions of the main window as well as the coordinates of the origin of the canvas.


Top
 Profile  
 
 Post subject: Re: Drawing cairo with other widgets
PostPosted: Thu Mar 08, 2012 9:10 am 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
Thanks!Didnt know the drawing area was meant for that purpose... :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group