GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Oct 21, 2014 2:11 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: How to draw a box around the widget?
PostPosted: Wed May 28, 2008 3:26 pm 
Offline
GTK+ Geek

Joined: Wed May 21, 2008 2:20 pm
Posts: 76
I'm desingning a custom widget and I would like to draw a box around the area it occupies in order to see if the size is allocated correctly. So, I need to draw outside the widget's client area. How to do this?

As far as I understand, gdk_cairo_create creates a cairo surface that is inside the widget's client area. How to create one that expands farther? Or maybe I don't need to use cairo for this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 7:51 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
couldn't you pack your custom widget into a GtkFrame?
http://library.gnome.org/devel/gtk/stable/GtkFrame.html
Code:
GtkWidget *frame = gtk_frame_new(NULL);
GtkWidget *wi = my_custom_widget_new();
gtk_container_add(GTK_CONTAINER(frame), wi);
gtk_widget_show(wi);
gtk_widget_show(frame);


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 12:16 pm 
Offline
GTK+ Geek

Joined: Wed May 21, 2008 2:20 pm
Posts: 76
dreblen wrote:
couldn't you pack your custom widget into a GtkFrame?
http://library.gnome.org/devel/gtk/stable/GtkFrame.html
Code:
GtkWidget *frame = gtk_frame_new(NULL);
GtkWidget *wi = my_custom_widget_new();
gtk_container_add(GTK_CONTAINER(frame), wi);
gtk_widget_show(wi);
gtk_widget_show(frame);


Will it react correctly if my widget would resize itself?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 5:14 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
yes, as this example shows:
Code:
#include <gtk/gtk.h>

gboolean timer(gpointer data)
{
   GtkWidget *wi;
   gint w, h;
   static gint count = 0;

   if(count <= 20)
      count++;
   else
      return FALSE;

   wi = GTK_WIDGET(data);
   gtk_widget_get_size_request(wi, &w, &h);
   gtk_widget_set_size_request(wi, w+count, h+count);

   return TRUE;
}

gboolean on_expose(GtkWidget *widget, GdkEventExpose *ev, gpointer data)
{
   cairo_t *cr;
   gint w, h;

   cr = gdk_cairo_create(widget->window);

   gdk_drawable_get_size(widget->window, &w, &h);

   cairo_set_line_width(cr, 5);
   cairo_move_to(cr, 0, 0);
   cairo_line_to(cr, w, 0);
   cairo_line_to(cr, w, h);
   cairo_line_to(cr, 0, h);
   cairo_line_to(cr, 0, 0);
   cairo_line_to(cr, w, h);
   cairo_move_to(cr, w, 0);
   cairo_line_to(cr, 0, h);
   cairo_stroke(cr);

   return FALSE;
}

int main(int argc, char **argv)
{
   GtkWidget *win;
   GtkWidget *frame;
   GtkWidget *d;

   gtk_init(&argc, &argv);

   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(G_OBJECT(win), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
   gtk_container_set_border_width(GTK_CONTAINER(win), 10);

   frame = gtk_frame_new(NULL);
   gtk_container_add(GTK_CONTAINER(win), frame);
   gtk_widget_show(frame);

   d = gtk_drawing_area_new();
   g_signal_connect(G_OBJECT(d), "expose_event", G_CALLBACK(on_expose), NULL);
   gtk_container_add(GTK_CONTAINER(frame), d);
   gtk_widget_show(d);

   g_timeout_add(500, timer, (gpointer)d);

   gtk_widget_show(win);
   gtk_main();

   return 0;
}

It gets kind of hard to see the frame because I had cairo draw a border as well


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 8:21 pm 
Offline
GTK+ Geek

Joined: Wed May 21, 2008 2:20 pm
Posts: 76
dreblen wrote:
yes, as this example shows:

It gets kind of hard to see the frame because I had cairo draw a border as well


Thanks, I tried and that worked.

P.S.: I wonder, why this forum doesn't send notifications of new messages to my email... :(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 9:06 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
Have you checked the 'Always notify me of replies:' pref in your profile options?
http://www.gtkforums.com/profile.php?mode=editprofile
It should be set to 'Yes'


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 4:33 pm 
Offline
GTK+ Geek

Joined: Wed May 21, 2008 2:20 pm
Posts: 76
dreblen wrote:
Have you checked the 'Always notify me of replies:' pref in your profile options?
http://www.gtkforums.com/profile.php?mode=editprofile
It should be set to 'Yes'


Yes, I did, I fiddled with all email-related settings there to no avail :(

Anyway, there's another question I have to GTK geek :)

How do I check if button_release_event happened inside my widget or outside?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 7:16 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
I don't know any simple way, but you can use this if you find no other alternative.
Just connect the "button_release_event" to check_inside_or_outside,
then have check_inside_or_outside do whatever depending on if it's inside or outside:
Code:
#include <gtk/gtk.h>

gboolean check_inside_or_outside(GtkWidget *wi, GdkEventButton *ev, gpointer data)
{
   GtkWidget *top;
   GdkDisplay *display;
   gint x, y;
   gint wi_x, wi_y;
   gint win_x, win_y;
   gint w, h;

   top = gtk_widget_get_toplevel(wi);

   display = gdk_display_get_default();
   gdk_window_get_origin(top->window, &win_x, &win_y);
   gdk_display_get_pointer(display, NULL, &x, &y, NULL);
   wi_x = wi->allocation.x + win_x;
   wi_y = wi->allocation.y + win_y;
   w = wi->allocation.width;
   h = wi->allocation.height;

   if((x >= wi_x && x < wi_x+w) && (y >= wi_y && y < wi_y+h))
      printf("Inside\n");
   else
      printf("Outside\n");

   return FALSE;
}

int main(int argc, char **argv)
{
   GtkWidget *win;
   GtkWidget *vbox;
   GtkWidget *button;

   gtk_init(&argc, &argv);

   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(G_OBJECT(win), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
   gtk_container_set_border_width(GTK_CONTAINER(win), 10);

   vbox = gtk_vbox_new(TRUE, 2);
   gtk_container_add(GTK_CONTAINER(win), vbox);
   gtk_widget_show(vbox);

   button = gtk_button_new_with_label("Press!");
   g_signal_connect(G_OBJECT(button), "button_release_event", G_CALLBACK(check_inside_or_outside), NULL);
   gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
   gtk_widget_show(button);

   button = gtk_button_new_with_label("Press!");
   g_signal_connect(G_OBJECT(button), "button_release_event", G_CALLBACK(check_inside_or_outside), NULL);
   gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
   gtk_widget_show(button);

   gtk_widget_show(win);

   gtk_main();
   return 0;
}


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group