drawing area of increasing width in a scrolled window



I have a drawing area in a scrolled window.  The width of the drawing
area can increase a few times during its life span.  When the drawing
area is getting wider, I know how to shrink the width of the slider in
the trough of the horizontal scroll bar but I don't know how to get
the drawing area to be displayed correctly when the slider is dragged
to the new area: it's just black in the new area.

Following is some Common Lisp code to demonstrate the problem.
Sorry about not using C to write the sample code.  Could someone
tell me what I'm missing?

Best wishes,

Chisheng Huang

ps.  I do use GTK+ Reference Manual and occasionally poke into GTK+
     source code.  So replies contain C code is perfect, too.

pps. I'm using GTK+-1.2.10.

;; NOTE: comments start with semicolons.
;;
;; create a top level window that contains a scrolled window, which
;; contains a drawing area.  the width of the top level window is
;; 256.  the drawing area is 512 wide and 256 high.  this drawing area
;; has a backing pixmap of the same size.  we draw vertical black bars
;; in the backing pixmap and use an expose event handler to copy the
;; exposed area into the drawing area.  initially, we draw 4 vertical
;; bars evenly across the width of the drawing area.  dragging the slider
;; around can show these 4 vertical vars.  then, we double the width of
;; the drawing area and draw 8 vertical bars spaced evenly.  dragging
;; the slider around will only show 4 vertical bars in the left half
;; of drawing area and the right half of the drawing area is all black.
;; how can i make the right half of drawing area display 4 vertical
;; bars correctly?
;; 
(setf window (gtk:window-new :toplevel))
(setf (gtk:widget-width window) 256)

;; create a scrolled window and stuff it into the top level window.
(setf scrolled-window (gtk:scrolled-window-new))
(gtk:container-add window scrolled-window)
;; never show vertical scroll bar
(setf (gtk:scrolled-window-vscrollbar-policy scrolled-window) :never)
;; automatically show horizontal scroll bar
(setf (gtk:scrolled-window-hscrollbar-policy scrolled-window) :automatic)

;; create a 512x256 drawing area and stuff it into the scrolled window.
(setf drawing-area (gtk:drawing-area-new))
(gtk:scrolled-window-add-with-viewport scrolled-window drawing-area)
(setf (gtk:widget-width drawing-area) 512)
(setf (gtk:widget-height drawing-area) 256)
;; make the drawing area listen to exposure events
(setf (gtk:widget-events drawing-area)
      (append (gtk:widget-events drawing-area) '(:exposure)))
;; connect an expose event handler.
(gtk:signal-connect
 drawing-area
 :expose-event
 #'(lambda (event)
     (multiple-value-bind (x y w h)
	 (gdk:event-area event)
       (gdk:draw-pixmap (gtk:widget-window drawing-area)
			(gtk:style-get-gc (gtk:widget-style drawing-area)
					  :foreground
					  :normal)
			backing-pixmap
			x y
			x y
			w h))))
;; realize drawing-area's gdk window so that we can create a gdk pixmap
(gtk:widget-realize drawing-area)
;; create a gdk pixmap of size 512x256 as the backing pixmap.
(setf backing-pixmap
      (make-pixmap 512 256
		   :window (gtk:widget-window drawing-area)))
;; clear the backing pixmap with normal background color.
(gdk:draw-rectangle backing-pixmap
		    (gtk:style-get-gc (gtk:widget-style drawing-area)
				      :background
				      :normal)
		    t
		    0 0 512 256)
;; draw 4 vertical bars filled with normal foreground color.  these
;; 4 vertical bars are evenly spaced over the width of the drawing area.
(dolist (x '(0 128 256 384))
  (gdk:draw-rectangle backing-pixmap
		      (gtk:style-get-gc (gtk:widget-style drawing-area)
					:foreground
					:normal)
		      t
		      x 0 64 256))
;; now we show the top level window and all of its kids.
(gtk:widget-show-all window)
;; everything works fine so far.  there are 4 vertical black bars
;; and the horizontal scroll bar is working, too.

;;------------------------------------------------------------------
;; now we double the width of backing-pixmap and drawing area to
;; 1024 and keep the height the same.

;; unreference the old backing pixmap.
(gdk:pixmap-unref backing-pixmap)
;; create a new backing pixmap of 1024x256.
(setf backing-pixmap
      (make-pixmap 1024 256
		   :window (gtk:widget-window drawing-area)))
;; clear the new backing pixmap with normal background color.
(gdk:draw-rectangle backing-pixmap
		    (gtk:style-get-gc (gtk:widget-style drawing-area)
				      :background
				      :normal)
		    t
		    0 0 1024 256)

;; draw 8 vertical bars filled with normal foreground color.  these
;; 8 vertical bars are evenly spaced over the width of the drawing area.
(dolist (x '(0 128 256 384 512 640 768 896))
  (gdk:draw-rectangle backing-pixmap
		      (gtk:style-get-gc (gtk:widget-style drawing-area)
					:foreground
					:normal)
		      t
		      x 0 64 256))

;; now we double the width of drawing area
(gdk:window-resize (gtk:widget-window drawing-area) 1024 256)
;; tell the horizontal adjustment of scrolled-window that 1024 is the
;; new upper bound.
(setf (gtk:adjustment-upper (gtk:scrolled-window-hadjustment scrolled-window))
      1024)
;; tell the horizontal adjustment of scrolled-window to emit a "changed"
;; signal
(gtk:adjustment-changed (gtk:scrolled-window-hadjustment scrolled-window))
;; the width of the slider in the trough of the scroll bar is reduced by
;; half after we called gtk:adjustment-changed and is 1/4 of the width
;; of the trough of the scroll bar, which is correct.  but, as we scroll
;; toward right, we only see 4 black bars in the left half of drawing
;; area and the right half of drawing area is all black.




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