scrolled window with viewport exampe redux



Thanks in advance for taking the time to read this.  Here is a very short
code to produce a scrolled window with a child that IS NOT a table.  You
see almost every example on the web uses tables, and they seem to work.
But if you want to have a drawable area in a scrolled window (imagine if
you wanted to zoom in on an picture), it doesn't seem to work.

The scrollbars are always as big as the mainwindow, and can hence never
move!  How can I make this scrolled window a scrolling window?

HERE IS THE CODE:

static GdkPixmap *PixMap = NULL; /* Backing pixmap */
int InnerWindow_x=100, InnerWindow_y=100;

static gint exposeInnerWindow( 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;
}

static gint configureInnerWindow( 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->black_gc,
		      TRUE,
		      0, 0,
		      widget->allocation.width,
		      widget->allocation.height);
  gtk_widget_draw(widget,NULL);
  return TRUE;
}

void quit ()
{
  gtk_exit (0);
}

int main( int   argc,
	  char *argv[] )
{
  GtkWidget *MainWindow, *vbox;
  GtkWidget *scrolledWindow, *InnerWindow;
  GtkObject *viewerHAdjustment, *viewerVAdjustment;
  GtkWidget *wordListWindow, *wordList;
  GtkObject *wordListHAdjustment, *wordListVAdjustment;
  GtkWidget *button;

  gtk_init (&argc, &argv);

  MainWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_name (MainWindow, "CASPER");

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (MainWindow), vbox);
  gtk_widget_show (vbox);

  /* setup scrolled window */
  viewerHAdjustment=gtk_adjustment_new(0,0,100,1.0,1.0,1.0);
  viewerVAdjustment=gtk_adjustment_new(0,0,100,1.0,1.0,1.0);

scrolledWindow=gtk_scrolled_window_new(GTK_ADJUSTMENT(viewerHAdjustment),GTK_ADJUSTMENT(viewerVAdjustment));

gtk_widget_set_usize(GTK_WIDGET(scrolledWindow),InnerWindow_x,InnerWindow_y);
  gtk_box_pack_start(GTK_BOX(vbox),scrolledWindow,TRUE,TRUE,0);
  gtk_widget_show(scrolledWindow);

  /* Create the child of the scrolled window */
  InnerWindow = gtk_drawing_area_new ();

gtk_widget_set_usize(GTK_WIDGET(scrolledWindow),2*InnerWindow_x,2*InnerWindow_y);

gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledWindow),
InnerWindow);
  gtk_widget_show (InnerWindow);

  /* Signals used to handle backing pixmap of the inner drawing area */
  gtk_signal_connect (GTK_OBJECT (InnerWindow), "expose_event",
		      (GtkSignalFunc) exposeInnerWindow, NULL);
  gtk_signal_connect (GTK_OBJECT(InnerWindow),"configure_event",
		      (GtkSignalFunc) configureInnerWindow, NULL);
  gtk_widget_set_events (InnerWindow, GDK_EXPOSURE_MASK
			 | GDK_LEAVE_NOTIFY_MASK
			 | GDK_BUTTON_PRESS_MASK
			 | GDK_POINTER_MOTION_MASK
			 | GDK_POINTER_MOTION_HINT_MASK);

gtk_signal_connect (GTK_OBJECT (MainWindow), "destroy",
		      GTK_SIGNAL_FUNC (quit), NULL);

  /* ...and a quit button */
  button = gtk_button_new_with_label ("Quit");
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			     GTK_SIGNAL_FUNC (gtk_widget_destroy),
			     GTK_OBJECT (MainWindow));
  gtk_widget_show (button);
  gtk_widget_show (MainWindow);
  gtk_main ();

  return 0;
}
-dave

dhendrix socrates berkeley edu|
         eigendave hotmail com|



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