int values were not drawing curve handles, yet the handles could still be selected...
[blender-staging.git] / source / blender / editors / space_graph / graph_draw.c
index f6915c4db0150cef8a8453cc9b872095b948a09b..382bb71a5926060e1050dddd0c0b61fd03076158 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) Blender Foundation
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/editors/space_graph/graph_draw.c
+ *  \ingroup spgraph
+ */
+
+
 #include <stdio.h>
 #include <math.h>
 #include <string.h>
 #include <float.h>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #ifndef _WIN32
 #include <unistd.h>
 #else
 #include <io.h>
 #endif
 
-#include "MEM_guardedalloc.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_arithb.h"
+#include "BLI_math.h"
+#include "BLI_utildefines.h"
 
 #include "DNA_anim_types.h"
-#include "DNA_action_types.h"
-#include "DNA_camera_types.h"
-#include "DNA_curve_types.h"
-#include "DNA_key_types.h"
-#include "DNA_lamp_types.h"
-#include "DNA_material_types.h"
-#include "DNA_meta_types.h"
 #include "DNA_object_types.h"
-#include "DNA_particle_types.h"
-#include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_space_types.h"
-#include "DNA_sequence_types.h"
-#include "DNA_userdef_types.h"
-#include "DNA_view2d_types.h"
 #include "DNA_windowmanager_types.h"
-#include "DNA_world_types.h"
 
-#include "BKE_animsys.h"
 #include "BKE_context.h"
 #include "BKE_curve.h"
-#include "BKE_depsgraph.h"
 #include "BKE_fcurve.h"
-#include "BKE_global.h"
-#include "BKE_key.h"
-#include "BKE_object.h"
-#include "BKE_screen.h"
-#include "BKE_utildefines.h"
+
 
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
 
 #include "ED_anim_api.h"
-#include "ED_util.h"
 
 #include "graph_intern.h"
 
 #include "UI_interface.h"
-#include "UI_interface_icons.h"
 #include "UI_resources.h"
 #include "UI_view2d.h"
 
-/* XXX */
-extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);
+/* *************************** */
+/* Utility Drawing Defines */
+
+/* determine the alpha value that should be used when 
+ * drawing components for some F-Curve (fcu)
+ *     - selected F-Curves should be more visible than partially visible ones
+ */
+#define drawFCurveFade(fcu) ( ((fcu)->flag & FCURVE_SELECTED)? 1.0f : 0.25f )
+
+/* set the color for some point from some value given packed into an int 
+ *     - intV: integer value containing color info packed into an int
+ *     - alpha: float value describing the 
+ */
+#define cpackA(intVC, alpha) \
+       { \
+               float _cpackCol[3]; \
+               cpack_to_rgb(intVC, &_cpackCol[0], &_cpackCol[1], &_cpackCol[2]); \
+               glColor4f(_cpackCol[0], _cpackCol[1], _cpackCol[2], alpha); \
+       }
 
 /* *************************** */
 /* F-Curve Modifier Drawing */
@@ -96,7 +93,7 @@ extern void gl_round_box(int mode, float minx, float miny, float maxx, float max
 /* Envelope -------------- */
 
 // TODO: draw a shaded poly showing the region of influence too!!!
-static void draw_fcurve_modifier_controls_envelope (FCurve *fcu, FModifier *fcm, View2D *v2d)
+static void draw_fcurve_modifier_controls_envelope (FModifier *fcm, View2D *v2d)
 {
        FMod_Envelope *env= (FMod_Envelope *)fcm->data;
        FCM_EnvelopeData *fed;
@@ -135,7 +132,7 @@ static void draw_fcurve_modifier_controls_envelope (FCurve *fcu, FModifier *fcm,
                        glVertex2f(fed->time, fed->max);
                }
        }
-       bglEnd();
+       bglEnd(); // GL_POINTS
        
        glPointSize(1.0f);
 }
@@ -146,7 +143,7 @@ static void draw_fcurve_modifier_controls_envelope (FCurve *fcu, FModifier *fcm,
 /* Points ---------------- */
 
 /* helper func - draw keyframe vertices only for an F-Curve */
-static void draw_fcurve_vertices_keyframes (FCurve *fcu, View2D *v2d, short edit, short sel)
+static void draw_fcurve_vertices_keyframes (FCurve *fcu, SpaceIpo *UNUSED(sipo), View2D *v2d, short edit, short sel)
 {
        BezTriple *bezt= fcu->bezt;
        const float fac= 0.05f * (v2d->cur.xmax - v2d->cur.xmin);
@@ -178,11 +175,14 @@ static void draw_fcurve_vertices_keyframes (FCurve *fcu, View2D *v2d, short edit
                }
        }
        
-       bglEnd();
+       bglEnd(); // GL_POINTS
 }
 
 
-/* helper func - draw handle vertex for an F-Curve as a round unfilled circle */
+/* helper func - draw handle vertex for an F-Curve as a round unfilled circle 
+ * NOTE: the caller MUST HAVE GL_LINE_SMOOTH & GL_BLEND ENABLED, otherwise, the controls don't 
+ * have a consistent appearance (due to off-pixel alignments)...
+ */
 static void draw_fcurve_handle_control (float x, float y, float xscale, float yscale, float hsize)
 {
        static GLuint displist=0;
@@ -206,23 +206,16 @@ static void draw_fcurve_handle_control (float x, float y, float xscale, float ys
        glTranslatef(x, y, 0.0f);
        glScalef(1.0f/xscale*hsize, 1.0f/yscale*hsize, 1.0f);
        
-       /* anti-aliased lines for more consistent appearance */
-       glEnable(GL_LINE_SMOOTH);
-       glEnable(GL_BLEND);
-       
        /* draw! */
        glCallList(displist);
        
-       glDisable(GL_LINE_SMOOTH);
-       glDisable(GL_BLEND);
-       
        /* restore view transform */
        glScalef(xscale/hsize, yscale/hsize, 1.0);
        glTranslatef(-x, -y, 0.0f);
 }
 
 /* helper func - draw handle vertices only for an F-Curve (if it is not protected) */
-static void draw_fcurve_vertices_handles (FCurve *fcu, View2D *v2d, short sel)
+static void draw_fcurve_vertices_handles (FCurve *fcu, SpaceIpo *sipo, View2D *v2d, short sel, short sel_handle_only)
 {
        BezTriple *bezt= fcu->bezt;
        BezTriple *prevbezt = NULL;
@@ -237,47 +230,57 @@ static void draw_fcurve_vertices_handles (FCurve *fcu, View2D *v2d, short sel)
        if (sel) UI_ThemeColor(TH_HANDLE_VERTEX_SELECT);
        else UI_ThemeColor(TH_HANDLE_VERTEX);
        
+       /* anti-aliased lines for more consistent appearance */
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
+       glEnable(GL_BLEND);
+       
        for (i=0; i < fcu->totvert; i++, prevbezt=bezt, bezt++) {
-               /* Draw the editmode handels for a bezier curve (others don't have handles) 
+               /* Draw the editmode handles for a bezier curve (others don't have handles) 
                 * if their selection status matches the selection status we're drawing for
                 *      - first handle only if previous beztriple was bezier-mode
                 *      - second handle only if current beztriple is bezier-mode
+                *
+                * Also, need to take into account whether the keyframe was selected
+                * if a Graph Editor option to only show handles of selected keys is on.
                 */
-               if ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) {
-                       if ((bezt->f1 & SELECT) == sel)/* && v2d->cur.xmin < bezt->vec[0][0] < v2d->cur.xmax)*/
-                               draw_fcurve_handle_control(bezt->vec[0][0], bezt->vec[0][1], xscale, yscale, hsize);
-               }
-               
-               if (bezt->ipo==BEZT_IPO_BEZ) {
-                       if ((bezt->f3 & SELECT) == sel)/* && v2d->cur.xmin < bezt->vec[2][0] < v2d->cur.xmax)*/
-                               draw_fcurve_handle_control(bezt->vec[2][0], bezt->vec[2][1], xscale, yscale, hsize);
+               if ( !sel_handle_only || BEZSELECTED(bezt) ) {
+                       if ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) {
+                               if ((bezt->f1 & SELECT) == sel)/* && v2d->cur.xmin < bezt->vec[0][0] < v2d->cur.xmax)*/
+                                       draw_fcurve_handle_control(bezt->vec[0][0], bezt->vec[0][1], xscale, yscale, hsize);
+                       }
+                       
+                       if (bezt->ipo==BEZT_IPO_BEZ) {
+                               if ((bezt->f3 & SELECT) == sel)/* && v2d->cur.xmin < bezt->vec[2][0] < v2d->cur.xmax)*/
+                                       draw_fcurve_handle_control(bezt->vec[2][0], bezt->vec[2][1], xscale, yscale, hsize);
+                       }
                }
        }
+       
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
+       glDisable(GL_BLEND);
 }
 
 /* helper func - set color to draw F-Curve data with */
-static void set_fcurve_vertex_color (SpaceIpo *sipo, FCurve *fcu, short sel)
+static void set_fcurve_vertex_color (FCurve *fcu, short sel)
 {
-#if 0
-               if (sipo->showkey) {
-                       if (sel) UI_ThemeColor(TH_TEXT_HI);
-                       else UI_ThemeColor(TH_TEXT);
-               } 
-#endif
-               if ((fcu->flag & FCURVE_PROTECTED)==0) {
-                       /* Curve's points are being edited */
-                       if (sel) UI_ThemeColor(TH_VERTEX_SELECT); 
-                       else UI_ThemeColor(TH_VERTEX);
-               } 
-               else {
-                       /* Curve's points cannot be edited */
-                       if (sel) UI_ThemeColor(TH_TEXT_HI);
-                       else UI_ThemeColor(TH_TEXT);
-               }
+       /* Fade the 'intensity' of the vertices based on the selection of the curves too */
+       int alphaOffset= (int)((drawFCurveFade(fcu) - 1.0f) * 255);
+       
+       /* Set color of curve vertex based on state of curve (i.e. 'Edit' Mode) */
+       if ((fcu->flag & FCURVE_PROTECTED)==0) {
+               /* Curve's points ARE BEING edited */
+               if (sel) UI_ThemeColorShadeAlpha(TH_VERTEX_SELECT, 0, alphaOffset); 
+               else UI_ThemeColorShadeAlpha(TH_VERTEX, 0, alphaOffset);
+       } 
+       else {
+               /* Curve's points CANNOT BE edited */
+               if (sel) UI_ThemeColorShadeAlpha(TH_TEXT_HI, 0, alphaOffset);
+               else UI_ThemeColorShadeAlpha(TH_TEXT, 0, alphaOffset);
+       }
 }
 
 
-void draw_fcurve_vertices (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
+static void draw_fcurve_vertices (SpaceIpo *sipo, ARegion *ar, FCurve *fcu, short do_handles, short sel_handle_only)
 {
        View2D *v2d= &ar->v2d;
        
@@ -290,67 +293,104 @@ void draw_fcurve_vertices (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
        
        glPointSize(UI_GetThemeValuef(TH_VERTEX_SIZE));
        
-       /* draw the two handles first (if they're shown, and if curve is being edited) */
-       if ((fcu->flag & FCURVE_PROTECTED)==0 && (fcu->flag & FCURVE_INT_VALUES)==0 && (sipo->flag & SIPO_NOHANDLES)==0) {
-               set_fcurve_vertex_color(sipo, fcu, 0);
-               draw_fcurve_vertices_handles(fcu, v2d, 0);
+       /* draw the two handles first (if they're shown, the curve doesn't have just a single keyframe, and the curve is being edited) */
+       if (do_handles)
+       {
+               set_fcurve_vertex_color(fcu, 0);
+               draw_fcurve_vertices_handles(fcu, sipo, v2d, 0, sel_handle_only);
                
-               set_fcurve_vertex_color(sipo, fcu, 1);
-               draw_fcurve_vertices_handles(fcu, v2d, 1);
+               set_fcurve_vertex_color(fcu, 1);
+               draw_fcurve_vertices_handles(fcu, sipo, v2d, 1, sel_handle_only);
        }
                
        /* draw keyframes over the handles */
-       set_fcurve_vertex_color(sipo, fcu, 0);
-       draw_fcurve_vertices_keyframes(fcu, v2d, !(fcu->flag & FCURVE_PROTECTED), 0);
+       set_fcurve_vertex_color(fcu, 0);
+       draw_fcurve_vertices_keyframes(fcu, sipo, v2d, !(fcu->flag & FCURVE_PROTECTED), 0);
        
-       set_fcurve_vertex_color(sipo, fcu, 1);
-       draw_fcurve_vertices_keyframes(fcu, v2d, !(fcu->flag & FCURVE_PROTECTED), 1);
+       set_fcurve_vertex_color(fcu, 1);
+       draw_fcurve_vertices_keyframes(fcu, sipo, v2d, !(fcu->flag & FCURVE_PROTECTED), 1);
        
        glPointSize(1.0f);
 }
 
 /* Handles ---------------- */
 
-/* draw lines for F-Curve handles only (this is only done in EditMode) */
-static void draw_fcurve_handles (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
+static int draw_fcurve_handles_check(SpaceIpo *sipo, FCurve *fcu)
+{
+       /* don't draw handle lines if handles are not to be shown */
+       if (    (sipo->flag & SIPO_NOHANDLES) || /* handles shouldn't be shown anywhere */
+                       (fcu->flag & FCURVE_PROTECTED) || /* keyframes aren't editable */
+#if 0          /* handles can still be selected and handle types set, better draw - campbell */
+                       (fcu->flag & FCURVE_INT_VALUES) || /* editing the handles here will cause weird/incorrect interpolation issues */
+#endif
+                       ((fcu->grp) && (fcu->grp->flag & AGRP_PROTECTED)) || /* group that curve belongs to is not editable */
+                       (fcu->totvert <= 1) /* do not show handles if there is only 1 keyframe, otherwise they all clump together in an ugly ball */
+               ) 
+       {
+               return 0;
+       } 
+       else 
+       {
+               return 1;
+       }
+}
+
+/* draw lines for F-Curve handles only (this is only done in EditMode)
+ * note: draw_fcurve_handles_check must be checked before running this. */
+static void draw_fcurve_handles (SpaceIpo *sipo, FCurve *fcu)
 {
-       extern unsigned int nurbcol[];
-       unsigned int *col;
        int sel, b;
        
-       /* don't draw handle lines if handles are not shown */
-       if ((sipo->flag & SIPO_NOHANDLES) || (fcu->flag & FCURVE_PROTECTED) || (fcu->flag & FCURVE_INT_VALUES))
-               return;
+       /* a single call to GL_LINES here around these calls should be sufficient to still
+        * get separate line segments, but which aren't wrapped with GL_LINE_STRIP everytime we
+        * want a single line
+        */
+       glBegin(GL_LINES);
        
-       /* slightly hacky, but we want to draw unselected points before selected ones*/
+       /* slightly hacky, but we want to draw unselected points before selected ones 
+        * so that selected points are clearly visible
+        */
        for (sel= 0; sel < 2; sel++) {
                BezTriple *bezt=fcu->bezt, *prevbezt=NULL;
+               int basecol= (sel)? TH_HANDLE_SEL_FREE : TH_HANDLE_FREE;
                float *fp;
+               unsigned char col[4];
+               
+               /* if only selected keyframes have handles shown, skip the first round */
+               if ((sel == 0) && (sipo->flag & SIPO_SELVHANDLESONLY))
+                       continue;
                
-               if (sel) col= nurbcol+4;
-               else col= nurbcol;
-                       
                for (b= 0; b < fcu->totvert; b++, prevbezt=bezt, bezt++) {
+                       /* if only selected keyframes can get their handles shown, 
+                        * check that keyframe is selected
+                        */
+                       if (sipo->flag & SIPO_SELVHANDLESONLY) {
+                               if (BEZSELECTED(bezt) == 0)
+                                       continue;
+                       }
+                       
+                       /* draw handle with appropriate set of colors if selection is ok */
                        if ((bezt->f2 & SELECT)==sel) {
                                fp= bezt->vec[0];
-                               
+
                                /* only draw first handle if previous segment had handles */
                                if ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) 
                                {
-                                       cpack(col[(unsigned char)bezt->h1]);
-                                       glBegin(GL_LINE_STRIP); 
-                                               glVertex2fv(fp); glVertex2fv(fp+3); 
-                                       glEnd();
+                                       UI_GetThemeColor3ubv(basecol + bezt->h1, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
                                        
+                                       glVertex2fv(fp); glVertex2fv(fp+3); 
                                }
-                               
+
                                /* only draw second handle if this segment is bezier */
                                if (bezt->ipo == BEZT_IPO_BEZ) 
                                {
-                                       cpack(col[(unsigned char)bezt->h2]);
-                                       glBegin(GL_LINE_STRIP); 
-                                               glVertex2fv(fp+3); glVertex2fv(fp+6); 
-                                       glEnd();
+                                       UI_GetThemeColor3ubv(basecol + bezt->h2, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
+
+                                       glVertex2fv(fp+3); glVertex2fv(fp+6); 
                                }
                        }
                        else {
@@ -359,11 +399,11 @@ static void draw_fcurve_handles (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
                                         ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) ) 
                                {
                                        fp= bezt->vec[0];
-                                       cpack(col[(unsigned char)bezt->h1]);
-                                       
-                                       glBegin(GL_LINE_STRIP); 
-                                               glVertex2fv(fp); glVertex2fv(fp+3); 
-                                       glEnd();
+                                       UI_GetThemeColor3ubv(basecol + bezt->h1, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
+
+                                       glVertex2fv(fp); glVertex2fv(fp+3); 
                                }
                                
                                /* only draw second handle if this segment is bezier, and selection is ok */
@@ -371,20 +411,25 @@ static void draw_fcurve_handles (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
                                         (bezt->ipo == BEZT_IPO_BEZ) )
                                {
                                        fp= bezt->vec[1];
-                                       cpack(col[(unsigned char)bezt->h2]);
+                                       UI_GetThemeColor3ubv(basecol + bezt->h2, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
                                        
-                                       glBegin(GL_LINE_STRIP); 
-                                               glVertex2fv(fp); glVertex2fv(fp+3); 
-                                       glEnd();
+                                       glVertex2fv(fp); glVertex2fv(fp+3); 
                                }
                        }
                }
        }
+       
+       glEnd(); // GL_LINES 
 }
 
 /* Samples ---------------- */
 
-/* helper func - draw sample-range marker for an F-Curve as a cross */
+/* helper func - draw sample-range marker for an F-Curve as a cross 
+ * NOTE: the caller MUST HAVE GL_LINE_SMOOTH & GL_BLEND ENABLED, otherwise, the controls don't 
+ * have a consistent appearance (due to off-pixel alignments)...
+ */
 static void draw_fcurve_sample_control (float x, float y, float xscale, float yscale, float hsize)
 {
        static GLuint displist=0;
@@ -409,17 +454,9 @@ static void draw_fcurve_sample_control (float x, float y, float xscale, float ys
        glTranslatef(x, y, 0.0f);
        glScalef(1.0f/xscale*hsize, 1.0f/yscale*hsize, 1.0f);
        
-       /* anti-aliased lines for more consistent appearance */
-               // XXX needed here?
-       glEnable(GL_LINE_SMOOTH);
-       glEnable(GL_BLEND);
-       
        /* draw! */
        glCallList(displist);
        
-       glDisable(GL_BLEND);
-       glDisable(GL_LINE_SMOOTH);
-       
        /* restore view transform */
        glScalef(xscale/hsize, yscale/hsize, 1.0);
        glTranslatef(-x, -y, 0.0f);
@@ -445,29 +482,42 @@ static void draw_fcurve_samples (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
        
        /* draw */
        if (first && last) {
+               /* anti-aliased lines for more consistent appearance */
+               if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
+               glEnable(GL_BLEND);
+               
                draw_fcurve_sample_control(first->vec[0], first->vec[1], xscale, yscale, hsize);
                draw_fcurve_sample_control(last->vec[0], last->vec[1], xscale, yscale, hsize);
+               
+               glDisable(GL_BLEND);
+               if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
        }
 }
 
 /* Curve ---------------- */
 
-/* minimum pixels per gridstep 
- * XXX: defined in view2d.c - must keep these in sync or relocate to View2D header!
- */
-#define MINGRIDSTEP    35
-
 /* helper func - just draw the F-Curve by sampling the visible region (for drawing curves with modifiers) */
-static void draw_fcurve_curve (FCurve *fcu, SpaceIpo *sipo, View2D *v2d, View2DGrid *grid)
+static void draw_fcurve_curve (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d, View2DGrid *grid)
 {
        ChannelDriver *driver;
        float samplefreq, ctime;
        float stime, etime;
-       
+       float unitFac;
+       float dx, dy;
+
+       /* when opening a blend file on a different sized screen or while dragging the toolbar this can happen
+        * best just bail out in this case */
+       UI_view2d_grid_size(grid, &dx, &dy);
+       if(dx <= 0.0f)
+               return;
+
+
        /* disable any drivers temporarily */
        driver= fcu->driver;
        fcu->driver= NULL;
        
+       /* compute unit correction factor */
+       unitFac= ANIM_unit_mapping_get_factor(ac->scene, id, fcu, 0);
        
        /* Note about sampling frequency:
         *      Ideally, this is chosen such that we have 1-2 pixels = 1 segment
@@ -478,26 +528,28 @@ static void draw_fcurve_curve (FCurve *fcu, SpaceIpo *sipo, View2D *v2d, View2DG
         *      though it is impossible to predict this from the modifiers!
         *
         *      If the automatically determined sampling frequency is likely to cause an infinite
-        *      loop (i.e. too close to FLT_EPSILON), fall back to default of 0.001
+        *      loop (i.e. too close to 0), then clamp it to a determined "safe" value. The value
+        *      chosen here is just the coarsest value which still looks reasonable...
         */
-               /* grid->dx is the first float in View2DGrid struct, so just cast to float pointer, and use it
-                * It represents the number of 'frames' between gridlines, but we divide by MINGRIDSTEP to get pixels-steps
-                */
+               /* grid->dx represents the number of 'frames' between gridlines, but we divide by U.v2d_min_gridsize to get pixels-steps */
                // TODO: perhaps we should have 1.0 frames as upper limit so that curves don't get too distorted?
-       samplefreq= *((float *)grid) / MINGRIDSTEP;
-       if (IS_EQ(samplefreq, 0)) samplefreq= 0.001f;
+       samplefreq= dx / U.v2d_min_gridsize;
+       if (samplefreq < 0.00001f) samplefreq= 0.00001f;
        
        
        /* the start/end times are simply the horizontal extents of the 'cur' rect */
        stime= v2d->cur.xmin;
-       etime= v2d->cur.xmax;
+       etime= v2d->cur.xmax + samplefreq; /* + samplefreq here so that last item gets included... */
        
        
-       /* at each sampling interval, add a new vertex */
+       /* at each sampling interval, add a new vertex 
+        *      - apply the unit correction factor to the calculated values so that 
+        *        the displayed values appear correctly in the viewport
+        */
        glBegin(GL_LINE_STRIP);
        
        for (ctime= stime; ctime <= etime; ctime += samplefreq)
-               glVertex2f( ctime, evaluate_fcurve(fcu, ctime) );
+               glVertex2f( ctime, evaluate_fcurve(fcu, ctime)*unitFac );
        
        glEnd();
        
@@ -506,8 +558,7 @@ static void draw_fcurve_curve (FCurve *fcu, SpaceIpo *sipo, View2D *v2d, View2DG
 }
 
 /* helper func - draw a samples-based F-Curve */
-// TODO: add offset stuff...
-static void draw_fcurve_curve_samples (FCurve *fcu, View2D *v2d)
+static void draw_fcurve_curve_samples (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d)
 {
        FPoint *prevfpt= fcu->fpt;
        FPoint *fpt= prevfpt + 1;
@@ -516,6 +567,9 @@ static void draw_fcurve_curve_samples (FCurve *fcu, View2D *v2d)
        
        glBegin(GL_LINE_STRIP);
        
+       /* apply unit mapping */
+       ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, 0);
+       
        /* extrapolate to left? - left-side of view comes before first keyframe? */
        if (prevfpt->vec[0] > v2d->cur.xmin) {
                v[0]= v2d->cur.xmin;
@@ -574,11 +628,14 @@ static void draw_fcurve_curve_samples (FCurve *fcu, View2D *v2d)
                glVertex2fv(v);
        }
        
+       /* unapply unit mapping */
+       ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, ANIM_UNITCONV_RESTORE);
+       
        glEnd();
 }
 
 /* helper func - draw one repeat of an F-Curve */
-static void draw_fcurve_curve_bezts (FCurve *fcu, View2D *v2d, View2DGrid *grid)
+static void draw_fcurve_curve_bezts (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d)
 {
        BezTriple *prevbezt= fcu->bezt;
        BezTriple *bezt= prevbezt+1;
@@ -590,6 +647,9 @@ static void draw_fcurve_curve_bezts (FCurve *fcu, View2D *v2d, View2DGrid *grid)
        
        glBegin(GL_LINE_STRIP);
        
+       /* apply unit mapping */
+       ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, 0);
+       
        /* extrapolate to left? */
        if (prevbezt->vec[1][0] > v2d->cur.xmin) {
                /* left-side of view comes before first keyframe, so need to extend as not cyclic */
@@ -647,7 +707,7 @@ static void draw_fcurve_curve_bezts (FCurve *fcu, View2D *v2d, View2DGrid *grid)
                         */
                        
                        /* resol not depending on horizontal resolution anymore, drivers for example... */
-                       // XXX need to take into account the scale
+                       // TODO: would be nice to make this depend on the scale of the graph too...
                        if (fcu->driver) 
                                resol= 32;
                        else 
@@ -675,8 +735,8 @@ static void draw_fcurve_curve_bezts (FCurve *fcu, View2D *v2d, View2DGrid *grid)
                                
                                correct_bezpart(v1, v2, v3, v4);
                                
-                               forward_diff_bezier(v1[0], v2[0], v3[0], v4[0], data, resol, 3);
-                               forward_diff_bezier(v1[1], v2[1], v3[1], v4[1], data+1, resol, 3);
+                               forward_diff_bezier(v1[0], v2[0], v3[0], v4[0], data, resol, sizeof(float)*3);
+                               forward_diff_bezier(v1[1], v2[1], v3[1], v4[1], data+1, resol, sizeof(float)*3);
                                
                                for (fp= data; resol; resol--, fp+= 3)
                                        glVertex2fv(fp);
@@ -721,13 +781,18 @@ static void draw_fcurve_curve_bezts (FCurve *fcu, View2D *v2d, View2DGrid *grid)
                glVertex2fv(v1);
        }
        
+       /* unapply unit mapping */
+       ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, ANIM_UNITCONV_RESTORE);
+       
        glEnd();
 } 
 
 /* Public Curve-Drawing API  ---------------- */
 
-/* Draw the 'ghost' F-Curves (i.e. snapshots of the curve) */
-void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGrid *grid)
+/* Draw the 'ghost' F-Curves (i.e. snapshots of the curve) 
+ * NOTE: unit mapping has already been applied to the values, so do not try and apply again
+ */
+void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar)
 {
        FCurve *fcu;
        
@@ -736,7 +801,7 @@ void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, Vie
        glLineWidth(3.0f);
        
        /* anti-aliased lines for less jagged appearance */
-       glEnable(GL_LINE_SMOOTH);
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
        glEnable(GL_BLEND);
        
        /* the ghost curves are simply sampled F-Curves stored in sipo->ghostCurves */
@@ -748,14 +813,14 @@ void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, Vie
                glColor4f(fcu->color[0], fcu->color[1], fcu->color[2], 0.5f);
                
                /* simply draw the stored samples */
-               draw_fcurve_curve_samples(fcu, &ar->v2d);
+               draw_fcurve_curve_samples(ac, NULL, fcu, &ar->v2d);
        }
        
        /* restore settings */
        setlinestyle(0);
        glLineWidth(1.0f);
        
-       glDisable(GL_LINE_SMOOTH);
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
        glDisable(GL_BLEND);
 }
 
@@ -800,7 +865,7 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                                /* protected curves (non editable) are drawn with dotted lines */
                                setlinestyle(2);
                        }
-                       if (fcu->flag & FCURVE_MUTED) {
+                       if ( ((fcu->grp) && (fcu->grp->flag & AGRP_MUTED)) || (fcu->flag & FCURVE_MUTED) ) {
                                /* muted curves are drawn in a greyish hue */
                                // XXX should we have some variations?
                                UI_ThemeColorShade(TH_HEADER, 50);
@@ -809,11 +874,11 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                                /* set whatever color the curve has set 
                                 *      - unselected curves draw less opaque to help distinguish the selected ones
                                 */
-                               glColor4f(fcu->color[0], fcu->color[1], fcu->color[2], ((sel) ? 1.0f : 0.5f));
+                               glColor4f(fcu->color[0], fcu->color[1], fcu->color[2], drawFCurveFade(fcu));
                        }
                        
                        /* anti-aliased lines for less jagged appearance */
-                       glEnable(GL_LINE_SMOOTH);
+                       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
                        glEnable(GL_BLEND);
                        
                        /* draw F-Curve */
@@ -821,20 +886,20 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                                /* draw a curve affected by modifiers or only allowed to have integer values 
                                 * by sampling it at various small-intervals over the visible region 
                                 */
-                               draw_fcurve_curve(fcu, sipo, &ar->v2d, grid);
+                               draw_fcurve_curve(ac, ale->id, fcu, &ar->v2d, grid);
                        }
                        else if ( ((fcu->bezt) || (fcu->fpt)) && (fcu->totvert) ) { 
                                /* just draw curve based on defined data (i.e. no modifiers) */
                                if (fcu->bezt)
-                                       draw_fcurve_curve_bezts(fcu, &ar->v2d, grid);
+                                       draw_fcurve_curve_bezts(ac, ale->id, fcu, &ar->v2d);
                                else if (fcu->fpt)
-                                       draw_fcurve_curve_samples(fcu, &ar->v2d);
+                                       draw_fcurve_curve_samples(ac, ale->id, fcu, &ar->v2d);
                        }
                        
                        /* restore settings */
                        setlinestyle(0);
                        
-                       glDisable(GL_LINE_SMOOTH);
+                       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
                        glDisable(GL_BLEND);
                }
                
@@ -842,26 +907,39 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                 *      - if the option to only show controls if the F-Curve is selected is enabled, we must obey this
                 */
                if (!(sipo->flag & SIPO_SELCUVERTSONLY) || (fcu->flag & FCURVE_SELECTED)) {
-                       if (fcurve_needs_draw_fmodifier_controls(fcu, fcm)) {
+                       if (fcurve_are_keyframes_usable(fcu) == 0) {
                                /* only draw controls if this is the active modifier */
                                if ((fcu->flag & FCURVE_ACTIVE) && (fcm)) {
                                        switch (fcm->type) {
                                                case FMODIFIER_TYPE_ENVELOPE: /* envelope */
-                                                       draw_fcurve_modifier_controls_envelope(fcu, fcm, &ar->v2d);
+                                                       draw_fcurve_modifier_controls_envelope(fcm, &ar->v2d);
                                                        break;
                                        }
                                }
                        }
                        else if ( ((fcu->bezt) || (fcu->fpt)) && (fcu->totvert) ) { 
+                               /* apply unit mapping */
+                               ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, fcu, 0);
+                               
                                if (fcu->bezt) {
-                                       /* only draw handles/vertices on keyframes */
-                                       draw_fcurve_handles(sipo, ar, fcu);
-                                       draw_fcurve_vertices(sipo, ar, fcu);
+                                       int do_handles = draw_fcurve_handles_check(sipo, fcu);
+                                       
+                                       if (do_handles) {
+                                               /* only draw handles/vertices on keyframes */
+                                               glEnable(GL_BLEND);
+                                               draw_fcurve_handles(sipo, fcu);
+                                               glDisable(GL_BLEND);
+                                       }
+                                       
+                                       draw_fcurve_vertices(sipo, ar, fcu, do_handles, (sipo->flag & SIPO_SELVHANDLESONLY));
                                }
                                else {
-                                       /* samples: should we only draw two indicators at either end as indicators? */
+                                       /* samples: only draw two indicators at either end as indicators */
                                        draw_fcurve_samples(sipo, ar, fcu);
                                }
+                               
+                               /* unapply unit mapping */
+                               ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, fcu, ANIM_UNITCONV_RESTORE);
                        }
                }
                
@@ -877,19 +955,15 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
 /* ************************************************************************* */
 /* Channel List */
 
-// XXX quite a few of these need to be kept in sync with their counterparts in Action Editor
-// as they're the same. We have 2 separate copies of this for now to make it easier to develop
-// the diffences between the two editors, but one day these should be merged!
-
 /* left hand part */
-void graph_draw_channel_names(bAnimContext *ac, SpaceIpo *sipo, ARegion *ar) 
+void graph_draw_channel_names(bContext *C, bAnimContext *ac, ARegion *ar) 
 {
        ListBase anim_data = {NULL, NULL};
        bAnimListElem *ale;
        int filter;
        
        View2D *v2d= &ar->v2d;
-       float x= 0.0f, y= 0.0f, height;
+       float y= 0.0f, height;
        int items, i=0;
        
        /* build list of channels to draw */
@@ -903,537 +977,57 @@ void graph_draw_channel_names(bAnimContext *ac, SpaceIpo *sipo, ARegion *ar)
         *        start of list offset, and the second is as a correction for the scrollers.
         */
        height= (float)((items*ACHANNEL_STEP) + (ACHANNEL_HEIGHT*2));
-       
-#if 0
-       if (height > (v2d->mask.ymax - v2d->mask.ymin)) {
-               /* don't use totrect set, as the width stays the same 
-                * (NOTE: this is ok here, the configuration is pretty straightforward) 
-                */
-               v2d->tot.ymin= (float)(-height);
-       }
-       
-       /* XXX I would call the below line! (ton) */
-#endif
        UI_view2d_totRect_set(v2d, ar->winx, height);
        
        /* loop through channels, and set up drawing depending on their type  */        
-       y= (float)ACHANNEL_FIRST;
-       
-       for (ale= anim_data.first, i=0; ale; ale= ale->next, i++) {
-               const float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
-               const float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
+       {       /* first pass: just the standard GL-drawing for backdrop + text */
+               y= (float)ACHANNEL_FIRST;
                
-               /* check if visible */
-               if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
-                        IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
-               {
-                       bActionGroup *grp = NULL;
-                       short indent= 0, offset= 0, sel= 0, group= 0;
-                       int expand= -1, protect = -1, special= -1, mute = -1;
-                       char name[128];
+               for (ale= anim_data.first, i=0; ale; ale= ale->next, i++) {
+                       const float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
+                       const float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
                        
-                       /* determine what needs to be drawn */
-                       switch (ale->type) {
-                               case ANIMTYPE_SCENE: /* scene */
-                               {
-                                       Scene *sce= (Scene *)ale->data;
-                                       
-                                       group= 4;
-                                       indent= 0;
-                                       
-                                       special= ICON_SCENE_DATA;
-                                       
-                                       /* only show expand if there are any channels */
-                                       if (EXPANDED_SCEC(sce))
-                                               expand= ICON_TRIA_DOWN;
-                                       else
-                                               expand= ICON_TRIA_RIGHT;
-                                       
-                                       sel = SEL_SCEC(sce);
-                                       strcpy(name, sce->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_OBJECT: /* object */
-                               {
-                                       Base *base= (Base *)ale->data;
-                                       Object *ob= base->object;
-                                       
-                                       group= 4;
-                                       indent= 0;
-                                       
-                                       /* icon depends on object-type */
-                                       if (ob->type == OB_ARMATURE)
-                                               special= ICON_ARMATURE_DATA;
-                                       else    
-                                               special= ICON_OBJECT_DATA;
-                                               
-                                       /* only show expand if there are any channels */
-                                       if (EXPANDED_OBJC(ob))
-                                               expand= ICON_TRIA_DOWN;
-                                       else
-                                               expand= ICON_TRIA_RIGHT;
-                                       
-                                       sel = SEL_OBJC(base);
-                                       strcpy(name, ob->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_FILLACTD: /* action widget */
-                               {
-                                       bAction *act= (bAction *)ale->data;
-                                       
-                                       group = 4;
-                                       indent= 1;
-                                       special= ICON_ACTION;
-                                       
-                                       if (EXPANDED_ACTC(act))
-                                               expand= ICON_TRIA_DOWN;
-                                       else
-                                               expand= ICON_TRIA_RIGHT;
-                                       
-                                       sel = SEL_ACTC(act);
-                                       strcpy(name, act->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_FILLDRIVERS: /* drivers widget */
-                               {
-                                       AnimData *adt= (AnimData *)ale->data;
-                                       
-                                       group = 4;
-                                       indent= 1;
-                                       special= ICON_ANIM_DATA;
-                                       
-                                       if (EXPANDED_DRVD(adt))
-                                               expand= ICON_TRIA_DOWN;
-                                       else
-                                               expand= ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, "Drivers");
-                               }
-                                       break;
-                               case ANIMTYPE_FILLMATD: /* object materials (dopesheet) expand widget */
-                               {
-                                       Object *ob = (Object *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_MATERIAL_DATA;
-                                       
-                                       if (FILTER_MAT_OBJC(ob))
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                               
-                                       strcpy(name, "Materials");
-                               }
-                                       break;
-                               case ANIMTYPE_FILLPARTD: /* object particles (dopesheet) expand widget */
-                               {
-                                       Object *ob = (Object *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_PARTICLE_DATA;
-                                       
-                                       if (FILTER_PART_OBJC(ob))
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                               
-                                       strcpy(name, "Particles");
-                               }
-                                       break;
-                               
-                               
-                               case ANIMTYPE_DSMAT: /* single material (dopesheet) expand widget */
-                               {
-                                       Material *ma = (Material *)ale->data;
-                                       
-                                       group = 0;
-                                       indent = 0;
-                                       special = ICON_MATERIAL_DATA;
-                                       offset = 21;
-                                       
-                                       if (FILTER_MAT_OBJD(ma))
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, ma->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_DSLAM: /* lamp (dopesheet) expand widget */
-                               {
-                                       Lamp *la = (Lamp *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_LAMP_DATA;
-                                       
-                                       if (FILTER_LAM_OBJD(la))
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, la->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_DSCAM: /* camera (dopesheet) expand widget */
-                               {
-                                       Camera *ca = (Camera *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_CAMERA_DATA;
-                                       
-                                       if (FILTER_CAM_OBJD(ca))
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, ca->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_DSCUR: /* curve (dopesheet) expand widget */
-                               {
-                                       Curve *cu = (Curve *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_CURVE_DATA;
-                                       
-                                       if (FILTER_CUR_OBJD(cu))
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, cu->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_DSSKEY: /* shapekeys (dopesheet) expand widget */
-                               {
-                                       Key *key= (Key *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_SHAPEKEY_DATA;
-                                       
-                                       if (FILTER_SKE_OBJD(key))       
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                               
-                                       //sel = SEL_OBJC(base);
-                                       strcpy(name, "Shape Keys");
-                               }
-                                       break;
-                               case ANIMTYPE_DSWOR: /* world (dopesheet) expand widget */
-                               {
-                                       World *wo= (World *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_WORLD_DATA;
-                                       
-                                       if (FILTER_WOR_SCED(wo))        
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, wo->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_DSPART: /* particle (dopesheet) expand widget */
-                               {
-                                       ParticleSettings *part= (ParticleSettings*)ale->data;
-                                       
-                                       group = 0;
-                                       indent = 0;
-                                       special = ICON_PARTICLE_DATA;
-                                       offset = 21;
-                                       
-                                       if (FILTER_PART_OBJD(part))     
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, part->id.name+2);
-                               }
-                                       break;
-                               case ANIMTYPE_DSMBALL: /* metaball (dopesheet) expand widget */
-                               {
-                                       MetaBall *mb = (MetaBall *)ale->data;
-                                       
-                                       group = 4;
-                                       indent = 1;
-                                       special = ICON_META_DATA;
-                                       
-                                       if (FILTER_MBALL_OBJD(mb))
-                                               expand = ICON_TRIA_DOWN;
-                                       else
-                                               expand = ICON_TRIA_RIGHT;
-                                       
-                                       strcpy(name, mb->id.name+2);
-                               }
-                                       break;
-                               
-                               
-                               case ANIMTYPE_GROUP: /* action group */
-                               {
-                                       bActionGroup *agrp= (bActionGroup *)ale->data;
-                                       
-                                       group= 2;
-                                       indent= 0;
-                                       special= -1;
-                                       
-                                       if (ale->id) {
-                                               /* special exception for materials */
-                                               if (GS(ale->id->name) == ID_MA) 
-                                                       offset= 25;
-                                               else
-                                                       offset= 14;
-                                       }
-                                       else
-                                               offset= 0;
-                                       
-                                       /* only show expand if there are any channels */
-                                       if (agrp->channels.first) {
-                                               if (EXPANDED_AGRP(agrp))
-                                                       expand = ICON_TRIA_DOWN;
-                                               else
-                                                       expand = ICON_TRIA_RIGHT;
-                                       }
-                                       
-                                       /* for now, 'special' (i.e. in front of name) is used to show visibility status */
-                                       if (agrp->flag & AGRP_NOTVISIBLE)
-                                               special= ICON_CHECKBOX_DEHLT;
-                                       else
-                                               special= ICON_CHECKBOX_HLT;
-                                       
-                                       if (agrp->flag & AGRP_MUTED)
-                                               mute = ICON_MUTE_IPO_ON;
-                                       else    
-                                               mute = ICON_MUTE_IPO_OFF;
-                                       
-                                       if (EDITABLE_AGRP(agrp))
-                                               protect = ICON_UNLOCKED;
-                                       else
-                                               protect = ICON_LOCKED;
-                                               
-                                       sel = SEL_AGRP(agrp);
-                                       strcpy(name, agrp->name);
-                               }
-                                       break;
-                               case ANIMTYPE_FCURVE: /* F-Curve channel */
-                               {
-                                       FCurve *fcu = (FCurve *)ale->data;
-                                       
-                                       indent = 0;
-                                       
-                                       group= (fcu->grp) ? 1 : 0;
-                                       grp= fcu->grp;
-                                       
-                                       if (ale->id) {
-                                               /* special exception for materials and particles */
-                                               if (ELEM(GS(ale->id->name),ID_MA,ID_PA)) {
-                                                       offset= 21;
-                                                       indent= 1;
-                                               }
-                                               else
-                                                       offset= 14;
-                                       }
-                                       else
-                                               offset= 0;
-                                       
-                                       /* for now, 'special' (i.e. in front of name) is used to show visibility status */
-                                       if (fcu->flag & FCURVE_VISIBLE)
-                                               special= ICON_CHECKBOX_HLT;
-                                       else
-                                               special= ICON_CHECKBOX_DEHLT;
-                                       
-                                       if (fcu->flag & FCURVE_MUTED)
-                                               mute = ICON_MUTE_IPO_ON;
-                                       else    
-                                               mute = ICON_MUTE_IPO_OFF;
-                                               
-                                       if (fcu->bezt) {
-                                               if (EDITABLE_FCU(fcu))
-                                                       protect = ICON_UNLOCKED;
-                                               else
-                                                       protect = ICON_LOCKED;
-                                       }
-                                       else
-                                               protect = ICON_ZOOMOUT; // XXX editability is irrelevant here, but this icon is temp...
-                                       
-                                       sel = SEL_FCU(fcu);
-                                       
-                                       getname_anim_fcurve(name, ale->id, fcu);
-                               }
-                                       break;
-                                       
-                               case ANIMTYPE_SHAPEKEY: /* shapekey channel */
-                               {
-                                       KeyBlock *kb = (KeyBlock *)ale->data;
-                                       
-                                       indent = 0;
-                                       special = -1;
-                                       
-                                       offset= (ale->id) ? 21 : 0;
-                                       
-                                       if (kb->name[0] == '\0')
-                                               sprintf(name, "Key %d", ale->index);
-                                       else
-                                               strcpy(name, kb->name);
-                               }
-                                       break;
-                       }       
-                       
-                       /* now, start drawing based on this information */
-                       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-                       glEnable(GL_BLEND);
-                       
-                       /* draw backing strip behind channel name */
-                       if (group == 4) {
-                               /* only used in dopesheet... */
-                               if (ELEM(ale->type, ANIMTYPE_SCENE, ANIMTYPE_OBJECT)) {
-                                       /* object channel - darker */
-                                       UI_ThemeColor(TH_DOPESHEET_CHANNELOB);
-                                       uiSetRoundBox((expand == ICON_TRIA_DOWN)? (1):(1|8));
-                                       gl_round_box(GL_POLYGON, x+offset,  yminc, (float)ACHANNEL_NAMEWIDTH, ymaxc, 8);
-                               }
-                               else {
-                                       /* sub-object folders - lighter */
-                                       UI_ThemeColor(TH_DOPESHEET_CHANNELSUBOB);
-                                       
-                                       offset += 7 * indent;
-                                       glBegin(GL_QUADS);
-                                               glVertex2f(x+offset, yminc);
-                                               glVertex2f(x+offset, ymaxc);
-                                               glVertex2f((float)ACHANNEL_NAMEWIDTH, ymaxc);
-                                               glVertex2f((float)ACHANNEL_NAMEWIDTH, yminc);
-                                       glEnd();
-                                       
-                                       /* clear group value, otherwise we cause errors... */
-                                       group = 0;
-                               }
+                       /* check if visible */
+                       if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
+                                IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
+                       {
+                               /* draw all channels using standard channel-drawing API */
+                               ANIM_channel_draw(ac, ale, yminc, ymaxc);
                        }
-                       else if (group == 3) {
-                               /* only for gp-data channels */
-                               UI_ThemeColorShade(TH_GROUP, 20);
-                               uiSetRoundBox((expand == ICON_TRIA_DOWN)? (1):(1|8));
-                               gl_round_box(GL_POLYGON, x+offset,  yminc, (float)ACHANNEL_NAMEWIDTH, ymaxc, 8);
-                       }
-                       else if (group == 2) {
-                               /* only for action group channels */
-                               if (ale->flag & AGRP_ACTIVE)
-                                       UI_ThemeColorShade(TH_GROUP_ACTIVE, 10);
-                               else
-                                       UI_ThemeColorShade(TH_GROUP, 20);
-                               uiSetRoundBox((expand == ICON_TRIA_DOWN)? (1):(1|8));
-                               gl_round_box(GL_POLYGON, x+offset,  yminc, (float)ACHANNEL_NAMEWIDTH, ymaxc, 8);
-                       }
-                       else {
-                               short shadefac= ((indent==0)?20: (indent==1)?-20: -40);
-                               
-                               indent += group;
-                               offset += 7 * indent;
-                               
-                               /* draw channel backdrop */
-                               UI_ThemeColorShade(TH_HEADER, shadefac);
-                               
-                               glBegin(GL_QUADS);
-                                       glVertex2f(x+offset, yminc);
-                                       glVertex2f(x+offset, ymaxc);
-                                       glVertex2f((float)ACHANNEL_NAMEWIDTH, ymaxc);
-                                       glVertex2f((float)ACHANNEL_NAMEWIDTH, yminc);
-                               glEnd();
-                               
-                               /* most of the time, only F-Curves are going to be drawn here */
-                               if (ale->type == ANIMTYPE_FCURVE) {
-                                       /* F-Curve channels need to have a special 'color code' box drawn, which is colored with whatever 
-                                        * color the curve has stored 
-                                        */
-                                       FCurve *fcu= (FCurve *)ale->data;
-                                       glColor3fv(fcu->color);
-                                       
-                                       // NOTE: only enable the following line for the fading-out gradient
-                                       //glShadeModel(GL_SMOOTH);
-                                       
-                                       glBegin(GL_QUADS);
-                                               /* solid color for the area around the checkbox */
-                                               glVertex2f(x+offset, yminc);
-                                               glVertex2f(x+offset, ymaxc);
-                                               glVertex2f(x+offset+18, ymaxc);
-                                               glVertex2f(x+offset+18, yminc);
-                                               
-#if 0 // fading out gradient
-                                               /* fading out gradient for the rest of the box */
-                                               glVertex2f(x+offset+18, yminc);
-                                               glVertex2f(x+offset+18, ymaxc);
-                                               
-                                               UI_ThemeColorShade(TH_HEADER, shadefac); // XXX does this cause any problems on some cards?
-                                               
-                                               glVertex2f(x+offset+20, ymaxc);
-                                               glVertex2f(x+offset+20, yminc);
-#endif // fading out gradient
-                                       glEnd();
-                                       
-                                       // NOTE: only enable the following line for the fading-out gradient
-                                       //glShadeModel(GL_FLAT);
-                               }
-                       }
-                       
-                       /* draw expand/collapse triangle */
-                       if (expand > 0) {
-                               UI_icon_draw(x+offset, yminc, expand);
-                               offset += 17;
-                       }
-                       
-                       /* draw special icon indicating certain data-types */
-                       if (special > -1) {
-                               if (ELEM(group, 3, 4)) {
-                                       /* for gpdatablock channels */
-                                       UI_icon_draw(x+offset, yminc, special);
-                                       offset += 17;
-                               }
-                               else {
-                                       /* for normal channels */
-                                       UI_icon_draw(x+offset, yminc, special);
-                                       offset += 17;
-                               }
-                       }
-                       glDisable(GL_BLEND);
-                       
-                       /* draw name */
-                       if (sel)
-                               UI_ThemeColor(TH_TEXT_HI);
-                       else
-                               UI_ThemeColor(TH_TEXT);
-                       offset += 3;
-                       UI_DrawString(x+offset, y-4, name);
                        
-                       /* reset offset - for RHS of panel */
-                       offset = 0;
-                       
-                       /* set blending again, as text drawing may clear it */
-                       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-                       glEnable(GL_BLEND);
+                       /* adjust y-position for next one */
+                       y -= ACHANNEL_STEP;
+               }
+       }
+       {       /* second pass: widgets */
+               uiBlock *block= uiBeginBlock(C, ar, "graph channel buttons", UI_EMBOSS);
+               
+               y= (float)ACHANNEL_FIRST;
+               
+               /* set blending again, as may not be set in previous step */
+               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+               glEnable(GL_BLEND);
+               
+               for (ale= anim_data.first, i=0; ale; ale= ale->next, i++) {
+                       const float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
+                       const float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
                        
-                       /* draw protect 'lock' */
-                       if (protect > -1) {
-                               offset = 16;
-                               UI_icon_draw((float)ACHANNEL_NAMEWIDTH-offset, yminc, protect);
+                       /* check if visible */
+                       if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
+                                IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
+                       {
+                               /* draw all channels using standard channel-drawing API */
+                               ANIM_channel_draw_widgets(ac, ale, block, yminc, ymaxc);
                        }
                        
-                       /* draw mute 'eye' */
-                       if (mute > -1) {
-                               offset += 16;
-                               UI_icon_draw((float)(ACHANNEL_NAMEWIDTH-offset), yminc, mute);
-                       }
-                       glDisable(GL_BLEND);
+                       /* adjust y-position for next one */
+                       y -= ACHANNEL_STEP;
                }
                
-               /* adjust y-position for next one */
-               y -= ACHANNEL_STEP;
+               uiEndBlock(C, block);
+               uiDrawBlock(C, block);
+               
+               glDisable(GL_BLEND);
        }
        
        /* free tempolary channels */