bb7c285d51eabd45fe7fbb3ae297acd0324915b7
kent
  Tue Apr 19 15:09:50 2011 -0700
Refactoring in hopes of making auto-scale work as you'd hope with multiwigs.  That still isn't there, this is just preliminaries.
diff --git src/hg/hgTracks/bedGraph.c src/hg/hgTracks/bedGraph.c
index 2ce83c3..038c494 100644
--- src/hg/hgTracks/bedGraph.c
+++ src/hg/hgTracks/bedGraph.c
@@ -122,44 +122,47 @@
 
 sqlFreeResult(&sr);
 hFreeConn(&conn);
 
 slReverse(&bgList);
 tg->items = bgList;
 }	/*	bedGraphLoadItems()	*/
 
 static void bedGraphFreeItems(struct track *tg) {
 #if defined(DEBUG)
 snprintf(dbgMsg, DBGMSGSZ, "I haven't seen bedGraphFreeItems ever called ?");
 wigDebugPrint("bedGraphFreeItems");
 #endif
 }
 
-static void bedGraphDrawItems(struct track *tg, int seqStart, int seqEnd,
-	struct hvGfx *hvg, int xOff, int yOff, int width,
-	MgFont *font, Color color, enum trackVisibility vis)
+struct preDrawContainer *bedGraphLoadPreDraw(struct track *tg, int seqStart, int seqEnd, int width)
+/* Do bits that load the predraw buffer tg->preDraw and also preDrawSize and preDrawZero. */
 {
 struct bedGraphItem *wi;
 double pixelsPerBase = scaleForPixels(width);
 double basesPerPixel = 1.0;
 int i;				/* an integer loop counter	*/
 if (pixelsPerBase > 0.0)
     basesPerPixel = 1.0 / pixelsPerBase;
 
-/*	walk through all the data and prepare the preDraw array	*/
-int preDrawZero, preDrawSize;
-struct preDrawElement *preDraw = initPreDraw(width, &preDrawSize, &preDrawZero);
+/* Allocate predraw and save it and related info in the track. */
+struct preDrawContainer *pre = tg->preDrawContainer = initPreDrawContainer(width);
+struct preDrawElement *preDraw = pre->preDraw;	/* to accumulate everything in prep for draw */
+int preDrawZero = pre->preDrawZero;		/* location in preDraw where screen starts */
+int preDrawSize = pre->preDrawSize;		/* size of preDraw array */
+
+/*	walk through all the data fill in the preDraw array	*/
 for (wi = tg->items; wi != NULL; wi = wi->next)
     {
     double dataValue = wi->dataValue;	/* the data value to graph */
 
     /*	Ready to draw, what do we know:
     *	the feature being processed:
     *	chrom coords:  [wi->start : wi->end)
     *	its data value: dataValue = wi->dataValue
     *
     *	The drawing window, in pixels:
     *	xOff = left margin, yOff = top margin, h = height of drawing window
     *	drawing window in chrom coords: seqStart, seqEnd
     *	'basesPerPixel' is known, 'pixelsPerBase' is known
     */
     /*	let's check end point screen coordinates.  If they are
@@ -196,45 +199,45 @@
 	 *	should always be true unless the data was
 	 *	prepared incorrectly.
 	 */
 	if ((xCoord >= 0) && (xCoord < preDrawSize))
 	    {
 	    ++preDraw[xCoord].count;
 	    if (dataValue > preDraw[xCoord].max)
 		preDraw[xCoord].max = dataValue;
 	    if (dataValue < preDraw[xCoord].min)
 		preDraw[xCoord].min = dataValue;
 	    preDraw[xCoord].sumData += dataValue;
 	    preDraw[xCoord].sumSquares += dataValue * dataValue;
 	    }
 	}
     }	/*	for (wi = tg->items; wi != NULL; wi = wi->next)	*/
+return pre;
+}
 
-/*	now we are ready to draw.  Each element in the preDraw[] array
- *	cooresponds to a single pixel on the screen
- */
-
-struct preDrawContainer *preDrawContainer;
-AllocVar(preDrawContainer);
-preDrawContainer->preDraw = preDraw;
+static void bedGraphDrawItems(struct track *tg, int seqStart, int seqEnd,
+	struct hvGfx *hvg, int xOff, int yOff, int width,
+	MgFont *font, Color color, enum trackVisibility vis)
+{
+struct preDrawContainer *pre = bedGraphLoadPreDraw(tg, seqStart, seqEnd, width);
+if (pre != NULL)
+    {
 wigDrawPredraw(tg, seqStart, seqEnd, hvg, xOff, yOff, width, font, color, vis,
-	       preDrawContainer, preDrawZero, preDrawSize, &tg->graphUpperLimit, &tg->graphLowerLimit);
-
-freeMem(preDrawContainer);
-freeMem(preDraw);
-}	/*	bedGraphDrawItems()	*/
-
+		   pre, pre->preDrawZero, pre->preDrawSize, 
+		   &tg->graphUpperLimit, &tg->graphLowerLimit);
+    }
+}
 
 /*
  *	WARNING ! - track->visibility is merely the default value
  *	from the trackDb entry at this time.  It will be set after this
  *	 by hgTracks from its cart UI setting.  When called in
  *	 TotalHeight it will then be the requested visibility.
  */
 void bedGraphMethods(struct track *track, struct trackDb *tdb, 
 	int wordCount, char *words[])
 {
 struct wigCartOptions *wigCart = wigCartOptionsNew(cart, tdb, wordCount, words);
 wigCart->bedGraph = TRUE;	/*	signal to left labels	*/
 switch (wordCount)
     {
 	case 2: