- UNUSED macro wasn't throwing an error with GCC if a var become used.
[blender.git] / source / blender / editors / space_graph / graph_buttons.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation, Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "DNA_anim_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_math.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_editVert.h"
43 #include "BLI_rand.h"
44
45 #include "BKE_context.h"
46 #include "BKE_depsgraph.h"
47 #include "BKE_fcurve.h"
48 #include "BKE_main.h"
49 #include "BKE_screen.h"
50
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "RNA_access.h"
56
57 #include "ED_anim_api.h"
58 #include "ED_keyframing.h"
59 #include "ED_screen.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63
64 #include "graph_intern.h"       // own include
65
66 /* XXX */
67
68 /* temporary definition for limits of float number buttons (FLT_MAX tends to infinity with old system) */
69 #define UI_FLT_MAX      10000.0f
70
71
72 /* ******************* graph editor space & buttons ************** */
73
74 #define B_NOP           1
75 #define B_REDR          2
76
77 /* -------------- */
78
79 static void do_graph_region_buttons(bContext *UNUSED(C), void *UNUSED(arg), int event)
80 {
81         //Scene *scene= CTX_data_scene(C);
82         
83         switch(event) {
84
85         }
86         
87         /* default for now */
88         //WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
89 }
90
91 /* -------------- */
92
93 static int graph_panel_context(const bContext *C, bAnimListElem **ale, FCurve **fcu)
94 {
95         bAnimContext ac;
96         bAnimListElem *elem= NULL;
97         
98         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
99          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
100          */
101         if (ANIM_animdata_get_context(C, &ac) == 0) 
102                 return 0;
103         
104         /* try to find 'active' F-Curve */
105         elem= get_active_fcurve_channel(&ac);
106         if(elem == NULL) 
107                 return 0;
108         
109         if(fcu)
110                 *fcu= (FCurve*)elem->data;
111         if(ale)
112                 *ale= elem;
113         else
114                 MEM_freeN(elem);
115         
116         return 1;
117 }
118
119 static int graph_panel_poll(const bContext *C, PanelType *UNUSED(pt))
120 {
121         return graph_panel_context(C, NULL, NULL);
122 }
123
124 /* -------------- */
125
126 /* Graph Editor View Settings */
127 static void graph_panel_view(const bContext *C, Panel *pa)
128 {
129         bScreen *sc= CTX_wm_screen(C);
130         SpaceIpo *sipo= CTX_wm_space_graph(C);
131         Scene *scene= CTX_data_scene(C);
132         PointerRNA spaceptr, sceneptr;
133         uiLayout *col, *subcol, *row;
134         
135         /* get RNA pointers for use when creating the UI elements */
136         RNA_id_pointer_create(&scene->id, &sceneptr);
137         RNA_pointer_create(&sc->id, &RNA_SpaceGraphEditor, sipo, &spaceptr);
138
139         /* 2D-Cursor */
140         col= uiLayoutColumn(pa->layout, 0);
141                 uiItemR(col, &spaceptr, "show_cursor", 0, NULL, 0);
142                 
143                 subcol= uiLayoutColumn(col, 1);
144                 uiLayoutSetActive(subcol, RNA_boolean_get(&spaceptr, "show_cursor")); 
145                         uiItemO(subcol, "Cursor from Selection", 0, "GRAPH_OT_frame_jump");
146                 
147                 subcol= uiLayoutColumn(col, 1);
148                 uiLayoutSetActive(subcol, RNA_boolean_get(&spaceptr, "show_cursor")); 
149                         row= uiLayoutSplit(subcol, 0.7, 1);
150                                 uiItemR(row, &sceneptr, "frame_current", 0, "Cursor X", 0);
151                                 uiItemEnumO(row, "GRAPH_OT_snap", "To Keys", 0, "type", GRAPHKEYS_SNAP_CFRA);
152                         row= uiLayoutSplit(subcol, 0.7, 1);
153                                 uiItemR(row, &spaceptr, "cursor_position_y", 0, "Cursor Y", 0);
154                                 uiItemEnumO(row, "GRAPH_OT_snap", "To Keys", 0, "type", GRAPHKEYS_SNAP_VALUE);
155 }
156
157 /* ******************* active F-Curve ************** */
158
159 static void graph_panel_properties(const bContext *C, Panel *pa)
160 {
161         bAnimListElem *ale;
162         FCurve *fcu;
163         PointerRNA fcu_ptr;
164         uiLayout *layout = pa->layout;
165         uiLayout *col, *row, *subrow;
166         uiBlock *block;
167         char name[256];
168         int icon = 0;
169
170         if (!graph_panel_context(C, &ale, &fcu))
171                 return;
172         
173         block= uiLayoutGetBlock(layout);
174         uiBlockSetHandleFunc(block, do_graph_region_buttons, NULL);
175         
176         /* F-Curve pointer */
177         RNA_pointer_create(ale->id, &RNA_FCurve, fcu, &fcu_ptr);
178         
179         /* user-friendly 'name' for F-Curve */
180         // TODO: only show the path if this is invalid?
181         col= uiLayoutColumn(layout, 0);
182                 icon= getname_anim_fcurve(name, ale->id, fcu);
183                 uiItemL(col, name, icon);
184                 
185         /* RNA-Path Editing - only really should be enabled when things aren't working */
186         col= uiLayoutColumn(layout, 1);
187                 uiLayoutSetEnabled(col, (fcu->flag & FCURVE_DISABLED)!=0); 
188                 uiItemR(col, &fcu_ptr, "data_path", 0, "", ICON_RNA);
189                 uiItemR(col, &fcu_ptr, "array_index", 0, NULL, 0);
190                 
191         /* color settings */
192         col= uiLayoutColumn(layout, 1);
193                 uiItemL(col, "Display Color:", 0);
194                 
195                 row= uiLayoutRow(col, 1);
196                         uiItemR(row, &fcu_ptr, "color_mode", 0, "", 0);
197                         
198                         subrow= uiLayoutRow(row, 1);
199                                 uiLayoutSetEnabled(subrow, (fcu->color_mode==FCURVE_COLOR_CUSTOM));
200                                 uiItemR(subrow, &fcu_ptr, "color", 0, "", 0);
201         
202         MEM_freeN(ale);
203 }
204
205 /* ******************* active Keyframe ************** */
206
207 /* get 'active' keyframe for panel editing */
208 static short get_active_fcurve_keyframe_edit(FCurve *fcu, BezTriple **bezt, BezTriple **prevbezt)
209 {
210         BezTriple *b;
211         int i;
212         
213         /* zero the pointers */
214         *bezt = *prevbezt = NULL;
215         
216         /* sanity checks */
217         if ((fcu->bezt == NULL) || (fcu->totvert == 0))
218                 return 0;
219                 
220         /* find first selected keyframe for now, and call it the active one 
221          *      - this is a reasonable assumption, given that whenever anyone 
222          *        wants to edit numerically, there is likely to only be 1 vert selected
223          */
224         for (i=0, b=fcu->bezt; i < fcu->totvert; i++, b++) {
225                 if (BEZSELECTED(b)) {
226                         /* found 
227                          *      - 'previous' is either the one before, of the keyframe itself (which is still fine)
228                          *              XXX: we can just make this null instead if needed
229                          */
230                         *prevbezt = (i > 0) ? b-1 : b;
231                         *bezt = b;
232                         
233                         return 1;
234                 }
235         }
236         
237         /* not found */
238         return 0;
239 }
240
241 static void graph_panel_key_properties(const bContext *C, Panel *pa)
242 {
243         bAnimListElem *ale;
244         FCurve *fcu;
245         BezTriple *bezt, *prevbezt;
246         
247         uiLayout *layout = pa->layout;
248         uiLayout *col;
249         uiBlock *block;
250
251         if (!graph_panel_context(C, &ale, &fcu))
252                 return;
253         
254         block = uiLayoutGetBlock(layout);
255         uiBlockSetHandleFunc(block, do_graph_region_buttons, NULL);
256         
257         /* only show this info if there are keyframes to edit */
258         if (get_active_fcurve_keyframe_edit(fcu, &bezt, &prevbezt)) {
259                 PointerRNA bezt_ptr;
260                 
261                 /* RNA pointer to keyframe, to allow editing */
262                 RNA_pointer_create(ale->id, &RNA_Keyframe, bezt, &bezt_ptr);
263                 
264                 /* interpolation */
265                 col= uiLayoutColumn(layout, 0);
266                         uiItemR(col, &bezt_ptr, "interpolation", 0, NULL, 0);
267                         
268                 /* numerical coordinate editing */
269                 col= uiLayoutColumn(layout, 1);
270                         /* keyframe itself */
271                         uiItemR(col, &bezt_ptr, "co", 0, "Key", 0);
272                         
273                         /* previous handle - only if previous was Bezier interpolation */
274                         if ((prevbezt) && (prevbezt->ipo == BEZT_IPO_BEZ))
275                                 uiItemR(col, &bezt_ptr, "handle_left", 0, NULL, 0);
276                         
277                         /* next handle - only if current is Bezier interpolation */
278                         if (bezt->ipo == BEZT_IPO_BEZ)
279                                 uiItemR(col, &bezt_ptr, "handle_right", 0, NULL, 0);
280         }
281         else
282                 uiItemL(layout, "No active keyframe on F-Curve", 0);
283         
284         MEM_freeN(ale);
285 }
286
287 /* ******************* drivers ******************************** */
288
289 #define B_IPO_DEPCHANGE         10
290
291 static void do_graph_region_driver_buttons(bContext *C, void *UNUSED(arg), int event)
292 {
293         Main *bmain= CTX_data_main(C);
294         Scene *scene= CTX_data_scene(C);
295         
296         switch (event) {
297                 case B_IPO_DEPCHANGE:
298                 {
299                         /* rebuild depsgraph for the new deps */
300                         DAG_scene_sort(bmain, scene);
301                         
302                         /* force an update of depsgraph */
303                         DAG_ids_flush_update(bmain, 0);
304                 }
305                         break;
306         }
307         
308         /* default for now */
309         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene); // XXX could use better notifier
310 }
311
312 /* callback to remove the active driver */
313 static void driver_remove_cb (bContext *UNUSED(C), void *ale_v, void *UNUSED(arg))
314 {
315         bAnimListElem *ale= (bAnimListElem *)ale_v;
316         ID *id= ale->id;
317         FCurve *fcu= ale->data;
318         
319         /* try to get F-Curve that driver lives on, and ID block which has this AnimData */
320         if (ELEM(NULL, id, fcu))
321                 return;
322         
323         /* call API method to remove this driver  */    
324         ANIM_remove_driver(id, fcu->rna_path, fcu->array_index, 0);
325 }
326
327 /* callback to add a target variable to the active driver */
328 static void driver_add_var_cb (bContext *UNUSED(C), void *driver_v, void *UNUSED(arg))
329 {
330         ChannelDriver *driver= (ChannelDriver *)driver_v;
331         
332         /* add a new variable */
333         driver_add_new_variable(driver);
334 }
335
336 /* callback to remove target variable from active driver */
337 static void driver_delete_var_cb (bContext *UNUSED(C), void *driver_v, void *dvar_v)
338 {
339         ChannelDriver *driver= (ChannelDriver *)driver_v;
340         DriverVar *dvar= (DriverVar *)dvar_v;
341         
342         /* remove the active variable */
343         driver_free_variable(driver, dvar);
344 }
345
346 /* callback to reset the driver's flags */
347 static void driver_update_flags_cb (bContext *UNUSED(C), void *fcu_v, void *UNUSED(arg))
348 {
349         FCurve *fcu= (FCurve *)fcu_v;
350         ChannelDriver *driver= fcu->driver;
351         
352         /* clear invalid flags */
353         fcu->flag &= ~FCURVE_DISABLED; // XXX?
354         driver->flag &= ~DRIVER_FLAG_INVALID;
355 }
356
357 /* drivers panel poll */
358 static int graph_panel_drivers_poll(const bContext *C, PanelType *UNUSED(pt))
359 {
360         SpaceIpo *sipo= CTX_wm_space_graph(C);
361
362         if(sipo->mode != SIPO_MODE_DRIVERS)
363                 return 0;
364
365         return graph_panel_context(C, NULL, NULL);
366 }
367
368 /* settings for 'single property' driver variable type */
369 static void graph_panel_driverVar__singleProp(uiLayout *layout, ID *id, DriverVar *dvar)
370 {
371         DriverTarget *dtar= &dvar->targets[0];
372         PointerRNA dtar_ptr;
373         uiLayout *row, *col;
374         uiBlock *block;
375         
376         /* initialise RNA pointer to the target */
377         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
378         
379         /* Target ID */
380         row= uiLayoutRow(layout, 0);
381                 uiTemplateAnyID(row, &dtar_ptr, "id", "id_type", "Prop:");
382         
383         /* Target Property */
384         // TODO: make this less technical...
385         if (dtar->id) {
386                 PointerRNA root_ptr;
387                 
388                 /* get pointer for resolving the property selected */
389                 RNA_id_pointer_create(dtar->id, &root_ptr);
390                 
391                 col= uiLayoutColumn(layout, 1);
392                 block= uiLayoutGetBlock(col);
393                         /* rna path */
394                         uiTemplatePathBuilder(col, &dtar_ptr, "data_path", &root_ptr, "Path");
395         }
396 }
397
398 /* settings for 'rotation difference' driver variable type */
399 static void graph_panel_driverVar__rotDiff(uiLayout *layout, ID *id, DriverVar *dvar)
400 {
401         DriverTarget *dtar= &dvar->targets[0];
402         DriverTarget *dtar2= &dvar->targets[1];
403         Object *ob1 = (Object *)dtar->id;
404         Object *ob2 = (Object *)dtar2->id;
405         PointerRNA dtar_ptr, dtar2_ptr;
406         uiLayout *col;
407         
408         /* initialise RNA pointer to the target */
409         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
410         RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); 
411         
412         /* Bone 1 */
413         col= uiLayoutColumn(layout, 1);
414                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Bone 1:");
415                 
416                 if (dtar->id && ob1->pose) {
417                         PointerRNA tar_ptr;
418                         
419                         RNA_pointer_create(dtar->id, &RNA_Pose, ob1->pose, &tar_ptr);
420                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
421                 }
422         
423         col= uiLayoutColumn(layout, 1);
424                 uiTemplateAnyID(col, &dtar2_ptr, "id", "id_type", "Bone 2:");
425                 
426                 if (dtar2->id && ob2->pose) {
427                         PointerRNA tar_ptr;
428                         
429                         RNA_pointer_create(dtar2->id, &RNA_Pose, ob2->pose, &tar_ptr);
430                         uiItemPointerR(col, &dtar2_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
431                 }
432 }
433
434 /* settings for 'location difference' driver variable type */
435 static void graph_panel_driverVar__locDiff(uiLayout *layout, ID *id, DriverVar *dvar)
436 {
437         DriverTarget *dtar= &dvar->targets[0];
438         DriverTarget *dtar2= &dvar->targets[1];
439         Object *ob1 = (Object *)dtar->id;
440         Object *ob2 = (Object *)dtar2->id;
441         PointerRNA dtar_ptr, dtar2_ptr;
442         uiLayout *col;
443         
444         /* initialise RNA pointer to the target */
445         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
446         RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); 
447         
448         /* Bone 1 */
449         col= uiLayoutColumn(layout, 1);
450                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Ob/Bone 1:");
451                 
452                 if (dtar->id && ob1->pose) {
453                         PointerRNA tar_ptr;
454                         
455                         RNA_pointer_create(dtar->id, &RNA_Pose, ob1->pose, &tar_ptr);
456                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
457                 }
458                 
459                 uiItemR(col, &dtar_ptr, "use_local_space_transform", 0, NULL, 0);
460         
461         col= uiLayoutColumn(layout, 1);
462                 uiTemplateAnyID(col, &dtar2_ptr, "id", "id_type", "Ob/Bone 2:");
463                 
464                 if (dtar2->id && ob2->pose) {
465                         PointerRNA tar_ptr;
466                         
467                         RNA_pointer_create(dtar2->id, &RNA_Pose, ob2->pose, &tar_ptr);
468                         uiItemPointerR(col, &dtar2_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
469                 }
470                 
471                 uiItemR(col, &dtar2_ptr, "use_local_space_transform", 0, NULL, 0);
472 }
473
474 /* settings for 'transform channel' driver variable type */
475 static void graph_panel_driverVar__transChan(uiLayout *layout, ID *id, DriverVar *dvar)
476 {
477         DriverTarget *dtar= &dvar->targets[0];
478         Object *ob = (Object *)dtar->id;
479         PointerRNA dtar_ptr;
480         uiLayout *col, *row;
481         
482         /* initialise RNA pointer to the target */
483         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
484         
485         /* properties */
486         col= uiLayoutColumn(layout, 1);
487                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Ob/Bone:");
488                 
489                 if (dtar->id && ob->pose) {
490                         PointerRNA tar_ptr;
491                         
492                         RNA_pointer_create(dtar->id, &RNA_Pose, ob->pose, &tar_ptr);
493                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
494                 }
495                 
496                 row= uiLayoutRow(layout, 1);
497                         uiItemR(row, &dtar_ptr, "transform_type", 0, "", 0);
498                         uiItemR(row, &dtar_ptr, "use_local_space_transform", 0, NULL, 0);
499 }
500
501 /* driver settings for active F-Curve (only for 'Drivers' mode) */
502 static void graph_panel_drivers(const bContext *C, Panel *pa)
503 {
504         bAnimListElem *ale;
505         FCurve *fcu;
506         ChannelDriver *driver;
507         DriverVar *dvar;
508         
509         PointerRNA driver_ptr;
510         uiLayout *col;
511         uiBlock *block;
512         uiBut *but;
513         
514         /* Get settings from context */
515         if (!graph_panel_context(C, &ale, &fcu))
516                 return;
517         driver= fcu->driver;
518         
519         /* set event handler for panel */
520         block= uiLayoutGetBlock(pa->layout); // xxx?
521         uiBlockSetHandleFunc(block, do_graph_region_driver_buttons, NULL);
522         
523         /* general actions - management */
524         col= uiLayoutColumn(pa->layout, 0);
525         block= uiLayoutGetBlock(col);
526                 but= uiDefBut(block, BUT, B_IPO_DEPCHANGE, "Update Dependencies", 0, 0, 10*UI_UNIT_X, 22, NULL, 0.0, 0.0, 0, 0, "Force updates of dependencies");
527                 uiButSetFunc(but, driver_update_flags_cb, fcu, NULL);
528                 
529                 but= uiDefBut(block, BUT, B_IPO_DEPCHANGE, "Remove Driver", 0, 0, 10*UI_UNIT_X, 18, NULL, 0.0, 0.0, 0, 0, "Remove this driver");
530                 uiButSetNFunc(but, driver_remove_cb, MEM_dupallocN(ale), NULL);
531                 
532         /* driver-level settings - type, expressions, and errors */
533         RNA_pointer_create(ale->id, &RNA_Driver, driver, &driver_ptr);
534         
535         col= uiLayoutColumn(pa->layout, 1);
536         block= uiLayoutGetBlock(col);
537                 uiItemR(col, &driver_ptr, "type", 0, NULL, 0);
538                 
539                 /* show expression box if doing scripted drivers, and/or error messages when invalid drivers exist */
540                 if (driver->type == DRIVER_TYPE_PYTHON) {
541                         /* expression */
542                         uiItemR(col, &driver_ptr, "expression", 0, "Expr", 0);
543                         
544                         /* errors? */
545                         if (driver->flag & DRIVER_FLAG_INVALID)
546                                 uiItemL(col, "ERROR: invalid Python expression", ICON_ERROR);
547                 }
548                 else {
549                         /* errors? */
550                         if (driver->flag & DRIVER_FLAG_INVALID)
551                                 uiItemL(col, "ERROR: invalid target channel(s)", ICON_ERROR);
552                 }
553                 
554         col= uiLayoutColumn(pa->layout, 1);
555                 /* debug setting */
556                 uiItemR(col, &driver_ptr, "show_debug_info", 0, NULL, 0);
557                 
558                 /* value of driver */
559                 if (driver->flag & DRIVER_FLAG_SHOWDEBUG) {
560                         uiLayout *row= uiLayoutRow(col, 1);
561                         char valBuf[32];
562                         
563                         uiItemL(row, "Driver Value:", 0);
564                         
565                         sprintf(valBuf, "%.3f", driver->curval);
566                         uiItemL(row, valBuf, 0);
567                 }
568         
569         /* add driver variables */
570         col= uiLayoutColumn(pa->layout, 0);
571         block= uiLayoutGetBlock(col);
572                 but= uiDefBut(block, BUT, B_IPO_DEPCHANGE, "Add Variable", 0, 0, 10*UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "Add a new target variable for this Driver");
573                 uiButSetFunc(but, driver_add_var_cb, driver, NULL);
574         
575         /* loop over targets, drawing them */
576         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
577                 PointerRNA dvar_ptr;
578                 uiLayout *box, *row;
579                 
580                 /* sub-layout column for this variable's settings */
581                 col= uiLayoutColumn(pa->layout, 1);
582                 
583                 /* header panel */
584                 box= uiLayoutBox(col);
585                         /* first row context info for driver */
586                         RNA_pointer_create(ale->id, &RNA_DriverVariable, dvar, &dvar_ptr);
587                         
588                         row= uiLayoutRow(box, 0);
589                         block= uiLayoutGetBlock(row);
590                                 /* variable name */
591                                 uiItemR(row, &dvar_ptr, "name", 0, "", 0);
592                                 
593                                 /* remove button */
594                                 uiBlockSetEmboss(block, UI_EMBOSSN);
595                                         but= uiDefIconBut(block, BUT, B_IPO_DEPCHANGE, ICON_X, 290, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "Delete target variable.");
596                                         uiButSetFunc(but, driver_delete_var_cb, driver, dvar);
597                                 uiBlockSetEmboss(block, UI_EMBOSS);
598                         
599                         /* variable type */
600                         row= uiLayoutRow(box, 0);
601                                 uiItemR(row, &dvar_ptr, "type", 0, "", 0);
602                                 
603                 /* variable type settings */
604                 box= uiLayoutBox(col);
605                         /* controls to draw depends on the type of variable */
606                         switch (dvar->type) {
607                                 case DVAR_TYPE_SINGLE_PROP:     /* single property */
608                                         graph_panel_driverVar__singleProp(box, ale->id, dvar);
609                                         break;
610                                 case DVAR_TYPE_ROT_DIFF: /* rotational difference */
611                                         graph_panel_driverVar__rotDiff(box, ale->id, dvar);
612                                         break;
613                                 case DVAR_TYPE_LOC_DIFF: /* location difference */
614                                         graph_panel_driverVar__locDiff(box, ale->id, dvar);
615                                         break;
616                                 case DVAR_TYPE_TRANSFORM_CHAN: /* transform channel */
617                                         graph_panel_driverVar__transChan(box, ale->id, dvar);
618                                         break;
619                         }
620                         
621                 /* value of variable */
622                 if (driver->flag & DRIVER_FLAG_SHOWDEBUG) {
623                         uiLayout *row;
624                         char valBuf[32];
625                         
626                         box= uiLayoutBox(col);
627                         row= uiLayoutRow(box, 1);
628                                 uiItemL(row, "Value:", 0);
629                                 
630                                 sprintf(valBuf, "%.3f", dvar->curval);
631                                 uiItemL(row, valBuf, 0);
632                 }
633         }
634         
635         /* cleanup */
636         MEM_freeN(ale);
637 }
638
639 /* ******************* f-modifiers ******************************** */
640 /* all the drawing code is in editors/animation/fmodifier_ui.c */
641
642 #define B_FMODIFIER_REDRAW              20
643
644 static void do_graph_region_modifier_buttons(bContext *C, void *UNUSED(arg), int event)
645 {
646         switch (event) {
647                 case B_REDR:
648                 case B_FMODIFIER_REDRAW: // XXX this should send depsgraph updates too
649                         WM_event_add_notifier(C, NC_ANIMATION, NULL); // XXX need a notifier specially for F-Modifiers
650                         break;
651         }
652 }
653
654 static void graph_panel_modifiers(const bContext *C, Panel *pa) 
655 {
656         bAnimListElem *ale;
657         FCurve *fcu;
658         FModifier *fcm;
659         uiLayout *col, *row;
660         uiBlock *block;
661         
662         if (!graph_panel_context(C, &ale, &fcu))
663                 return;
664         
665         block= uiLayoutGetBlock(pa->layout);
666         uiBlockSetHandleFunc(block, do_graph_region_modifier_buttons, NULL);
667         
668         /* 'add modifier' button at top of panel */
669         {
670                 row= uiLayoutRow(pa->layout, 0);
671                 block= uiLayoutGetBlock(row);
672                 
673                 // XXX for now, this will be a operator button which calls a 'add modifier' operator
674                 uiDefButO(block, BUT, "GRAPH_OT_fmodifier_add", WM_OP_INVOKE_REGION_WIN, "Add Modifier", 10, 0, 150, 20, "Adds a new F-Curve Modifier for the active F-Curve");
675                 
676                 /* copy/paste (as sub-row)*/
677                 row= uiLayoutRow(row, 1);
678                         uiItemO(row, "", ICON_COPYDOWN, "GRAPH_OT_fmodifier_copy");
679                         uiItemO(row, "", ICON_PASTEDOWN, "GRAPH_OT_fmodifier_paste");
680         }
681         
682         /* draw each modifier */
683         for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
684                 col= uiLayoutColumn(pa->layout, 1);
685                 
686                 ANIM_uiTemplate_fmodifier_draw(col, ale->id, &fcu->modifiers, fcm);
687         }
688
689         MEM_freeN(ale);
690 }
691
692 /* ******************* general ******************************** */
693
694 void graph_buttons_register(ARegionType *art)
695 {
696         PanelType *pt;
697
698         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel view");
699         strcpy(pt->idname, "GRAPH_PT_view");
700         strcpy(pt->label, "View Properties");
701         pt->draw= graph_panel_view;
702         pt->flag |= PNL_DEFAULT_CLOSED;
703         BLI_addtail(&art->paneltypes, pt);
704         
705         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel properties");
706         strcpy(pt->idname, "GRAPH_PT_properties");
707         strcpy(pt->label, "Active F-Curve");
708         pt->draw= graph_panel_properties;
709         pt->poll= graph_panel_poll;
710         BLI_addtail(&art->paneltypes, pt);
711         
712         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel properties");
713         strcpy(pt->idname, "GRAPH_PT_key_properties");
714         strcpy(pt->label, "Active Keyframe");
715         pt->draw= graph_panel_key_properties;
716         pt->poll= graph_panel_poll;
717         BLI_addtail(&art->paneltypes, pt);
718
719
720         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel drivers");
721         strcpy(pt->idname, "GRAPH_PT_drivers");
722         strcpy(pt->label, "Drivers");
723         pt->draw= graph_panel_drivers;
724         pt->poll= graph_panel_drivers_poll;
725         BLI_addtail(&art->paneltypes, pt);
726
727         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel modifiers");
728         strcpy(pt->idname, "GRAPH_PT_modifiers");
729         strcpy(pt->label, "Modifiers");
730         pt->draw= graph_panel_modifiers;
731         pt->poll= graph_panel_poll;
732         BLI_addtail(&art->paneltypes, pt);
733 }
734
735 static int graph_properties(bContext *C, wmOperator *UNUSED(op))
736 {
737         ScrArea *sa= CTX_wm_area(C);
738         ARegion *ar= graph_has_buttons_region(sa);
739         
740         if(ar)
741                 ED_region_toggle_hidden(C, ar);
742
743         return OPERATOR_FINISHED;
744 }
745
746 void GRAPH_OT_properties(wmOperatorType *ot)
747 {
748         ot->name= "Properties";
749         ot->idname= "GRAPH_OT_properties";
750         
751         ot->exec= graph_properties;
752         ot->poll= ED_operator_ipo_active; // xxx
753         
754         /* flags */
755         ot->flag= 0;
756 }