Re: Aligning widgets adjacent to a drawing area...



On Wednesday 21 July 2004 7:30 pm, Christer Palm wrote:
>
> gtk_widget_translate_coordinates() with values from each of the text
> labels allocation's and the DrawingArea as the destination should do the
> job, I guess.

Hmmm. I've had a look at that and I can't see how it would work for my 
problem...  

I've taken the liberty of attaching some code which I've been using to 
try and figure out how to do what I want.  (it's not pretty, but it's 
fine to prove the concept).  

(As a bonus question, how do I stop the text items in the table from 
"spacing out" when the window is maximized?  I've played with the 
pack box options, but to no avail).

--------8<-----snip----------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

void gui_create();
gint gui_darea_configure(GtkWidget *widget, GdkEventConfigure *event);
gboolean gui_darea_expose(GtkWidget *widget, GdkEventExpose *event);
void gui_redraw();

GtkWidget *window;
GtkWidget *darea;
GdkPixmap *pixmap = NULL;
int Darea_Width = 600;
int Darea_Height = 300;

#define MAX_NUM_LABELS 16

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

   GtkWidget *vbox, *hbox;
   GtkWidget *toolbar, *table, *text;
   GtkWidget *scrolled_window;
   int i;

   gtk_init(&argc, &argv);

   /* Create the main window */
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title(GTK_WINDOW(window), "Example");
   gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);

   /* Create a vbox */
   vbox = gtk_vbox_new(FALSE, 0);

   /* Create a scollable window and add to the vbox */
   scrolled_window = gtk_scrolled_window_new(NULL, NULL);
   gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 2);
   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 2);

   /* Create an hbox */
   hbox = gtk_hbox_new(FALSE, 0);
   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window), hbox);

   /* Create a table  */
   table = gtk_table_new( MAX_NUM_LABELS, 3, FALSE );
   gtk_box_pack_start( GTK_BOX(hbox), table, FALSE, FALSE, 0 );
   gtk_table_set_row_spacings( GTK_TABLE(table), 2 );
   gtk_table_set_col_spacings( GTK_TABLE(table), 2 );
   gtk_container_border_width( GTK_CONTAINER(table), 2 );

   /* Add labels */
   for ( i = 0; i < MAX_NUM_LABELS; i++ ) {
      text = gtk_entry_new_with_max_length(20);
      gtk_entry_set_text(GTK_ENTRY(text), "Label");
      gtk_widget_set_usize(text, 80, 20);
      gtk_table_attach_defaults(GTK_TABLE(table), text, 1, 2, i, i+1 );
   }

   /* Create drawing area */
   darea = gtk_drawing_area_new();
   gtk_widget_set_usize(darea, Darea_Width , Darea_Height); /* DEBUG */
   gtk_box_pack_start( GTK_BOX(hbox), darea, FALSE, FALSE, 0 );

   /* Drawing area callbacks */
   g_signal_connect(G_OBJECT(darea),"configure_event",
                    G_CALLBACK(gui_darea_configure), NULL);
   g_signal_connect(G_OBJECT(darea), "expose_event",
                       G_CALLBACK(gui_darea_expose), NULL);

   /* Add the vbox to the main window */
   gtk_container_add(GTK_CONTAINER(window), vbox);

   gtk_widget_show_all(window);
   gtk_main();

}

gint gui_darea_configure(GtkWidget *widget, GdkEventConfigure *event)
{
   if (pixmap)
      g_object_unref(pixmap);

   /* Create backing pixmap */
   pixmap = gdk_pixmap_new( darea->window, Darea_Width, Darea_Height, -1 );

   gui_redraw();

   return TRUE;

}

gboolean gui_darea_expose(GtkWidget *widget, GdkEventExpose *event)
{

   /* Redraw backing pixmap region modified by event */
   gdk_draw_drawable( widget->window,
                      widget->style->black_gc,
                      pixmap,
                      event->area.x, event->area.y,
                      event->area.x, event->area.y,
                      event->area.width, event->area.height );

   return TRUE;

}

void gui_redraw( void )
{

   int x, y;

   /* Clear pixmap */
   gdk_draw_rectangle( pixmap, darea->style->white_gc,
                       TRUE, 0, 0, Darea_Width, Darea_Height );

   /**************************************************************************/
   /* THE PROBLEM: How to ensure that each line is drawn such that it lines  */
   /* up with the associated text box on the left, allowing for user-defined */
   /* Font sizes, widget decorations or what have you...                     */
   /**************************************************************************/
   for (y=0; y<Darea_Height; y+=20)
         gdk_draw_line( pixmap, darea->style->black_gc, 0, y, Darea_Width, y );

}


-- 
David J. Singer
doc deadvirgins org uk
"Time flies like an arrow, fruit flies like a banana"




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