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