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



David J. Singer wrote:
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...

Did you even try it? It can't be much simpler...

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).

OK. Here's the code modified to do what I suppose you want.

(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).


I fixed that as well. Though all this is right in the tutorial. I think you need to RTFM + perhaps some example code and then try a bit harder.

--
Christer Palm
#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();

#define MAX_NUM_LABELS 16

GtkWidget *window;
GtkWidget *darea;
GdkPixmap *pixmap = NULL;
GtkWidget *labels[MAX_NUM_LABELS];
GtkWidget *table;

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

   GtkWidget *vbox, *hbox;
   GtkWidget *toolbar, *text;
   GtkWidget *scrolled_window;
   GtkWidget *viewport;
   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);

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

   /* 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);

   viewport = gtk_viewport_new(NULL, NULL);
   gtk_container_add(GTK_CONTAINER(scrolled_window), viewport);

   /* Create a table  */
   table = gtk_table_new( MAX_NUM_LABELS, 2, FALSE );
   gtk_container_add(GTK_CONTAINER(viewport), table);
   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);
      labels[i] = text;
      gtk_entry_set_text(GTK_ENTRY(text), "Label");
      gtk_entry_set_width_chars(GTK_ENTRY(text), 10);
      gtk_table_attach(GTK_TABLE(table), text, 0, 1, i, i+1,
         (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 0, 0);
      /* Realize the widgets now so that they have an */
      /* allocation when the drawing area is configured */
      gtk_widget_realize(text);
   }

   /* Create drawing area */
   darea = gtk_drawing_area_new();
//   gtk_widget_set_usize(darea, Darea_Width , Darea_Height); /* DEBUG */
   gtk_table_attach(GTK_TABLE(table), darea, 1, 2, 0, MAX_NUM_LABELS,
      (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)GTK_FILL, 0, 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);

   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->allocation.width, darea->allocation.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 ytop, ybot, y, i, rc;

   /* Clear pixmap */
   gdk_draw_rectangle( pixmap, darea->style->white_gc,
                       TRUE, 0, 0, darea->allocation.width, darea->allocation.height );

   for (i=0; i < MAX_NUM_LABELS; i++) {
      if (gtk_widget_translate_coordinates(labels[i], darea, 0, 0, 0, &ytop) &&
          gtk_widget_translate_coordinates(labels[i], darea, 0, labels[i]->allocation.height, 0, &ybot)) {

         y = (ytop + ybot) / 2;
         gdk_draw_line( pixmap, darea->style->black_gc, 0, y, darea->allocation.width, y );
      }
   }
}





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