use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / space_graph / graph_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation, Joshua Leung
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_graph/graph_buttons.c
28  *  \ingroup spgraph
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <float.h>
36
37 #include "DNA_anim_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_math.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_editVert.h"
46 #include "BLI_rand.h"
47 #include "BLI_utildefines.h"
48
49 #include "BLF_translation.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 #include "BKE_unit.h"
57
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "RNA_access.h"
63
64 #include "ED_anim_api.h"
65 #include "ED_keyframing.h"
66 #include "ED_screen.h"
67
68 #include "UI_interface.h"
69 #include "UI_resources.h"
70
71 #include "graph_intern.h"       // own include
72
73 /* XXX */
74
75 /* temporary definition for limits of float number buttons (FLT_MAX tends to infinity with old system) */
76 #define UI_FLT_MAX      10000.0f
77
78
79 /* ******************* graph editor space & buttons ************** */
80
81 #define B_REDR 1
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, *sub, *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                 sub= uiLayoutColumn(col, 1);
150                 uiLayoutSetActive(sub, RNA_boolean_get(&spaceptr, "show_cursor")); 
151                         uiItemO(sub, "Cursor from Selection", ICON_NONE, "GRAPH_OT_frame_jump");
152                 
153                 sub= uiLayoutColumn(col, 1);
154                 uiLayoutSetActive(sub, RNA_boolean_get(&spaceptr, "show_cursor")); 
155                         row= uiLayoutSplit(sub, 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(sub, 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, *sub;
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                         sub= uiLayoutRow(row, 1);
205                                 uiLayoutSetEnabled(sub, (fcu->color_mode==FCURVE_COLOR_CUSTOM));
206                                 uiItemR(sub, &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 /* update callback for active keyframe properties - base updates stuff */
248 static void graphedit_activekey_update_cb(bContext *C, void *fcu_ptr, void *UNUSED(bezt_ptr))
249 {
250         SpaceIpo *sipo= CTX_wm_space_graph(C);
251         const short use_handle = !(sipo->flag & SIPO_NOHANDLES);
252         FCurve *fcu = (FCurve *)fcu_ptr;
253         
254         /* make sure F-Curve and its handles are still valid after this editing */
255         sort_time_fcurve(fcu);
256         testhandles_fcurve(fcu, use_handle);
257 }
258
259 /* update callback for active keyframe properties - handle-editing wrapper */
260 static void graphedit_activekey_handles_cb(bContext *C, void *fcu_ptr, void *bezt_ptr)
261 {
262         BezTriple *bezt = (BezTriple *)bezt_ptr;
263         
264         /* since editing the handles, make sure they're set to types which are receptive to editing 
265          * see transform_conversions.c :: createTransGraphEditData(), last step in second loop
266          */
267         if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) && ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
268                 /* by changing to aligned handles, these can now be moved... */
269                 bezt->h1= HD_ALIGN;
270                 bezt->h2= HD_ALIGN;
271         }
272         
273         /* now call standard updates */
274         graphedit_activekey_update_cb(C, fcu_ptr, bezt_ptr);
275 }
276
277 static void graph_panel_key_properties(const bContext *C, Panel *pa)
278 {
279         bAnimListElem *ale;
280         FCurve *fcu;
281         BezTriple *bezt, *prevbezt;
282         
283         uiLayout *layout = pa->layout;
284         uiLayout *col;
285         uiBlock *block;
286
287         if (!graph_panel_context(C, &ale, &fcu))
288                 return;
289         
290         block = uiLayoutGetBlock(layout);
291         uiBlockSetHandleFunc(block, do_graph_region_buttons, NULL);
292         
293         /* only show this info if there are keyframes to edit */
294         if (get_active_fcurve_keyframe_edit(fcu, &bezt, &prevbezt)) {
295                 PointerRNA bezt_ptr, id_ptr, fcu_prop_ptr;
296                 PropertyRNA *fcu_prop = NULL;
297                 uiBut *but;
298                 int unit = B_UNIT_NONE;
299                 
300                 /* RNA pointer to keyframe, to allow editing */
301                 RNA_pointer_create(ale->id, &RNA_Keyframe, bezt, &bezt_ptr);
302                 
303                 /* get property that F-Curve affects, for some unit-conversion magic */
304                 RNA_id_pointer_create(ale->id, &id_ptr);
305                 if (RNA_path_resolve(&id_ptr, fcu->rna_path, &fcu_prop_ptr, &fcu_prop) && fcu_prop) {
306                         /* determine the unit for this property */
307                         unit = RNA_SUBTYPE_UNIT(RNA_property_subtype(fcu_prop));
308                 }               
309                 
310                 /* interpolation */
311                 col= uiLayoutColumn(layout, 0);
312                         uiItemR(col, &bezt_ptr, "interpolation", 0, NULL, ICON_NONE);
313                         
314                 /* numerical coordinate editing 
315                  *      - we use the button-versions of the calls so that we can attach special update handlers
316                  *        and unit conversion magic that cannot be achieved using a purely RNA-approach
317                  */
318                 // XXX: 
319                 col= uiLayoutColumn(layout, 1);
320                         /* keyframe itself */
321                         {
322                                 uiItemL(col, "Key:", ICON_NONE);
323                                 
324                                 but = uiDefButR(block, NUM, B_REDR, "Frame", 0, 0, UI_UNIT_X, UI_UNIT_Y, &bezt_ptr, "co", 0, 0, 0, -1, -1, NULL);
325                                 uiButSetFunc(but, graphedit_activekey_update_cb, fcu, bezt);
326                                 
327                                 but = uiDefButR(block, NUM, B_REDR, "Value", 0, 0, UI_UNIT_X, UI_UNIT_Y, &bezt_ptr, "co", 1, 0, 0, -1, -1, NULL);
328                                 uiButSetFunc(but, graphedit_activekey_update_cb, fcu, bezt);
329                                 uiButSetUnitType(but, unit);
330                         }
331                         
332                         /* previous handle - only if previous was Bezier interpolation */
333                         if ((prevbezt) && (prevbezt->ipo == BEZT_IPO_BEZ)) {
334                                 uiItemL(col, "Left Handle:", ICON_NONE);
335                                 
336                                 but = uiDefButR(block, NUM, B_REDR, "X", 0, 0, UI_UNIT_X, UI_UNIT_Y, &bezt_ptr, "handle_left", 0, 0, 0, -1, -1, NULL);
337                                 uiButSetFunc(but, graphedit_activekey_handles_cb, fcu, bezt);           
338                                 
339                                 but = uiDefButR(block, NUM, B_REDR, "Y", 0, 0, UI_UNIT_X, UI_UNIT_Y, &bezt_ptr, "handle_left", 1, 0, 0, -1, -1, NULL);
340                                 uiButSetFunc(but, graphedit_activekey_handles_cb, fcu, bezt);
341                                 uiButSetUnitType(but, unit);
342                         }
343                         
344                         /* next handle - only if current is Bezier interpolation */
345                         if (bezt->ipo == BEZT_IPO_BEZ) {
346                                 uiItemL(col, "Right Handle:", ICON_NONE);
347                                 
348                                 but = uiDefButR(block, NUM, B_REDR, "X", 0, 0, UI_UNIT_X, UI_UNIT_Y, &bezt_ptr, "handle_right", 0, 0, 0, -1, -1, NULL);
349                                 uiButSetFunc(but, graphedit_activekey_handles_cb, fcu, bezt);
350                                 
351                                 but = uiDefButR(block, NUM, B_REDR, "Y", 0, 0, UI_UNIT_X, UI_UNIT_Y, &bezt_ptr, "handle_right", 1, 0, 0, -1, -1, NULL);
352                                 uiButSetFunc(but, graphedit_activekey_handles_cb, fcu, bezt);
353                                 uiButSetUnitType(but, unit);
354                         }
355         }
356         else {
357                 if ((fcu->bezt == NULL) && (fcu->modifiers.first)) {
358                         /* modifiers only - so no keyframes to be active */
359                         uiItemL(layout, "F-Curve only has F-Modifiers", ICON_NONE);
360                         uiItemL(layout, "See Modifiers panel below", ICON_INFO);
361                 }
362                 else if (fcu->fpt) {
363                         /* samples only */
364                         uiItemL(layout, "F-Curve doesn't have any keyframes as it only contains sampled points", ICON_NONE);
365                 }
366                 else
367                         uiItemL(layout, "No active keyframe on F-Curve", ICON_NONE);
368         }
369         
370         MEM_freeN(ale);
371 }
372
373 /* ******************* drivers ******************************** */
374
375 #define B_IPO_DEPCHANGE         10
376
377 static void do_graph_region_driver_buttons(bContext *C, void *UNUSED(arg), int event)
378 {
379         Main *bmain= CTX_data_main(C);
380         Scene *scene= CTX_data_scene(C);
381         
382         switch (event) {
383                 case B_IPO_DEPCHANGE:
384                 {
385                         /* rebuild depsgraph for the new deps */
386                         DAG_scene_sort(bmain, scene);
387                         
388                         /* force an update of depsgraph */
389                         DAG_ids_flush_update(bmain, 0);
390                 }
391                         break;
392         }
393         
394         /* default for now */
395         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene); // XXX could use better notifier
396 }
397
398 /* callback to remove the active driver */
399 static void driver_remove_cb (bContext *C, void *ale_v, void *UNUSED(arg))
400 {
401         bAnimListElem *ale= (bAnimListElem *)ale_v;
402         ID *id= ale->id;
403         FCurve *fcu= ale->data;
404         ReportList *reports = CTX_wm_reports(C);
405         
406         /* try to get F-Curve that driver lives on, and ID block which has this AnimData */
407         if (ELEM(NULL, id, fcu))
408                 return;
409         
410         /* call API method to remove this driver  */    
411         ANIM_remove_driver(reports, id, fcu->rna_path, fcu->array_index, 0);
412 }
413
414 /* callback to add a target variable to the active driver */
415 static void driver_add_var_cb (bContext *UNUSED(C), void *driver_v, void *UNUSED(arg))
416 {
417         ChannelDriver *driver= (ChannelDriver *)driver_v;
418         
419         /* add a new variable */
420         driver_add_new_variable(driver);
421 }
422
423 /* callback to remove target variable from active driver */
424 static void driver_delete_var_cb (bContext *UNUSED(C), void *driver_v, void *dvar_v)
425 {
426         ChannelDriver *driver= (ChannelDriver *)driver_v;
427         DriverVar *dvar= (DriverVar *)dvar_v;
428         
429         /* remove the active variable */
430         driver_free_variable(driver, dvar);
431 }
432
433 /* callback to reset the driver's flags */
434 static void driver_update_flags_cb (bContext *UNUSED(C), void *fcu_v, void *UNUSED(arg))
435 {
436         FCurve *fcu= (FCurve *)fcu_v;
437         ChannelDriver *driver= fcu->driver;
438         
439         /* clear invalid flags */
440         fcu->flag &= ~FCURVE_DISABLED; // XXX?
441         driver->flag &= ~DRIVER_FLAG_INVALID;
442 }
443
444 /* drivers panel poll */
445 static int graph_panel_drivers_poll(const bContext *C, PanelType *UNUSED(pt))
446 {
447         SpaceIpo *sipo= CTX_wm_space_graph(C);
448
449         if(sipo->mode != SIPO_MODE_DRIVERS)
450                 return 0;
451
452         return graph_panel_context(C, NULL, NULL);
453 }
454
455 /* settings for 'single property' driver variable type */
456 static void graph_panel_driverVar__singleProp(uiLayout *layout, ID *id, DriverVar *dvar)
457 {
458         DriverTarget *dtar= &dvar->targets[0];
459         PointerRNA dtar_ptr;
460         uiLayout *row, *col;
461         
462         /* initialise RNA pointer to the target */
463         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
464         
465         /* Target ID */
466         row= uiLayoutRow(layout, 0);
467                 uiTemplateAnyID(row, &dtar_ptr, "id", "id_type", "Prop:");
468         
469         /* Target Property */
470         // TODO: make this less technical...
471         if (dtar->id) {
472                 PointerRNA root_ptr;
473                 
474                 /* get pointer for resolving the property selected */
475                 RNA_id_pointer_create(dtar->id, &root_ptr);
476                 
477                 col= uiLayoutColumn(layout, 1);
478                 /* rna path */
479                 uiTemplatePathBuilder(col, &dtar_ptr, "data_path", &root_ptr, "Path");
480         }
481 }
482
483 /* settings for 'rotation difference' driver variable type */
484 static void graph_panel_driverVar__rotDiff(uiLayout *layout, ID *id, DriverVar *dvar)
485 {
486         DriverTarget *dtar= &dvar->targets[0];
487         DriverTarget *dtar2= &dvar->targets[1];
488         Object *ob1 = (Object *)dtar->id;
489         Object *ob2 = (Object *)dtar2->id;
490         PointerRNA dtar_ptr, dtar2_ptr;
491         uiLayout *col;
492         
493         /* initialise RNA pointer to the target */
494         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
495         RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); 
496         
497         /* Bone 1 */
498         col= uiLayoutColumn(layout, 1);
499                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Bone 1:");
500                 
501                 if (dtar->id && ob1->pose) {
502                         PointerRNA tar_ptr;
503                         
504                         RNA_pointer_create(dtar->id, &RNA_Pose, ob1->pose, &tar_ptr);
505                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
506                 }
507         
508         col= uiLayoutColumn(layout, 1);
509                 uiTemplateAnyID(col, &dtar2_ptr, "id", "id_type", "Bone 2:");
510                 
511                 if (dtar2->id && ob2->pose) {
512                         PointerRNA tar_ptr;
513                         
514                         RNA_pointer_create(dtar2->id, &RNA_Pose, ob2->pose, &tar_ptr);
515                         uiItemPointerR(col, &dtar2_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
516                 }
517 }
518
519 /* settings for 'location difference' driver variable type */
520 static void graph_panel_driverVar__locDiff(uiLayout *layout, ID *id, DriverVar *dvar)
521 {
522         DriverTarget *dtar= &dvar->targets[0];
523         DriverTarget *dtar2= &dvar->targets[1];
524         Object *ob1 = (Object *)dtar->id;
525         Object *ob2 = (Object *)dtar2->id;
526         PointerRNA dtar_ptr, dtar2_ptr;
527         uiLayout *col;
528         
529         /* initialise RNA pointer to the target */
530         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
531         RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); 
532         
533         /* Bone 1 */
534         col= uiLayoutColumn(layout, 1);
535                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Ob/Bone 1:");
536                 
537                 if (dtar->id && ob1->pose) {
538                         PointerRNA tar_ptr;
539                         
540                         RNA_pointer_create(dtar->id, &RNA_Pose, ob1->pose, &tar_ptr);
541                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
542                 }
543                 
544                 uiItemR(col, &dtar_ptr, "transform_space", 0, NULL, ICON_NONE);
545         
546         col= uiLayoutColumn(layout, 1);
547                 uiTemplateAnyID(col, &dtar2_ptr, "id", "id_type", "Ob/Bone 2:");
548                 
549                 if (dtar2->id && ob2->pose) {
550                         PointerRNA tar_ptr;
551                         
552                         RNA_pointer_create(dtar2->id, &RNA_Pose, ob2->pose, &tar_ptr);
553                         uiItemPointerR(col, &dtar2_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
554                 }
555                 
556                 uiItemR(col, &dtar2_ptr, "transform_space", 0, NULL, ICON_NONE);
557 }
558
559 /* settings for 'transform channel' driver variable type */
560 static void graph_panel_driverVar__transChan(uiLayout *layout, ID *id, DriverVar *dvar)
561 {
562         DriverTarget *dtar= &dvar->targets[0];
563         Object *ob = (Object *)dtar->id;
564         PointerRNA dtar_ptr;
565         uiLayout *col, *sub;
566         
567         /* initialise RNA pointer to the target */
568         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
569         
570         /* properties */
571         col= uiLayoutColumn(layout, 1);
572                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Ob/Bone:");
573                 
574                 if (dtar->id && ob->pose) {
575                         PointerRNA tar_ptr;
576                         
577                         RNA_pointer_create(dtar->id, &RNA_Pose, ob->pose, &tar_ptr);
578                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
579                 }
580                 
581                 sub= uiLayoutColumn(layout, 1);
582                         uiItemR(sub, &dtar_ptr, "transform_type", 0, NULL, ICON_NONE);
583                         uiItemR(sub, &dtar_ptr, "transform_space", 0, "Space", ICON_NONE);
584 }
585
586 /* driver settings for active F-Curve (only for 'Drivers' mode) */
587 static void graph_panel_drivers(const bContext *C, Panel *pa)
588 {
589         bAnimListElem *ale;
590         FCurve *fcu;
591         ChannelDriver *driver;
592         DriverVar *dvar;
593         
594         PointerRNA driver_ptr;
595         uiLayout *col;
596         uiBlock *block;
597         uiBut *but;
598         
599         /* Get settings from context */
600         if (!graph_panel_context(C, &ale, &fcu))
601                 return;
602         driver= fcu->driver;
603         
604         /* set event handler for panel */
605         block= uiLayoutGetBlock(pa->layout); // xxx?
606         uiBlockSetHandleFunc(block, do_graph_region_driver_buttons, NULL);
607         
608         /* general actions - management */
609         col= uiLayoutColumn(pa->layout, 0);
610         block= uiLayoutGetBlock(col);
611                 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");
612                 uiButSetFunc(but, driver_update_flags_cb, fcu, NULL);
613                 
614                 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");
615                 uiButSetNFunc(but, driver_remove_cb, MEM_dupallocN(ale), NULL);
616                 
617         /* driver-level settings - type, expressions, and errors */
618         RNA_pointer_create(ale->id, &RNA_Driver, driver, &driver_ptr);
619         
620         col= uiLayoutColumn(pa->layout, 1);
621         block= uiLayoutGetBlock(col);
622                 uiItemR(col, &driver_ptr, "type", 0, NULL, ICON_NONE);
623                 
624                 /* show expression box if doing scripted drivers, and/or error messages when invalid drivers exist */
625                 if (driver->type == DRIVER_TYPE_PYTHON) {
626                         /* expression */
627                         uiItemR(col, &driver_ptr, "expression", 0, "Expr", ICON_NONE);
628                         
629                         /* errors? */
630                         if (driver->flag & DRIVER_FLAG_INVALID)
631                                 uiItemL(col, "ERROR: invalid Python expression", ICON_ERROR);
632                 }
633                 else {
634                         /* errors? */
635                         if (driver->flag & DRIVER_FLAG_INVALID)
636                                 uiItemL(col, "ERROR: invalid target channel(s)", ICON_ERROR);
637                 }
638                 
639         col= uiLayoutColumn(pa->layout, 1);
640                 /* debug setting */
641                 uiItemR(col, &driver_ptr, "show_debug_info", 0, NULL, ICON_NONE);
642                 
643                 /* value of driver */
644                 if (driver->flag & DRIVER_FLAG_SHOWDEBUG) {
645                         uiLayout *row= uiLayoutRow(col, 1);
646                         char valBuf[32];
647                         
648                         uiItemL(row, "Driver Value:", ICON_NONE);
649                         
650                         BLI_snprintf(valBuf, sizeof(valBuf), "%.3f", driver->curval);
651                         uiItemL(row, valBuf, ICON_NONE);
652                 }
653         
654         /* add driver variables */
655         col= uiLayoutColumn(pa->layout, 0);
656         block= uiLayoutGetBlock(col);
657                 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");
658                 uiButSetFunc(but, driver_add_var_cb, driver, NULL);
659         
660         /* loop over targets, drawing them */
661         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
662                 PointerRNA dvar_ptr;
663                 uiLayout *box, *row;
664                 
665                 /* sub-layout column for this variable's settings */
666                 col= uiLayoutColumn(pa->layout, 1);
667                 
668                 /* header panel */
669                 box= uiLayoutBox(col);
670                         /* first row context info for driver */
671                         RNA_pointer_create(ale->id, &RNA_DriverVariable, dvar, &dvar_ptr);
672                         
673                         row= uiLayoutRow(box, 0);
674                         block= uiLayoutGetBlock(row);
675                                 /* variable name */
676                                 uiItemR(row, &dvar_ptr, "name", 0, "", ICON_NONE);
677                                 
678                                 /* remove button */
679                                 uiBlockSetEmboss(block, UI_EMBOSSN);
680                                         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");
681                                         uiButSetFunc(but, driver_delete_var_cb, driver, dvar);
682                                 uiBlockSetEmboss(block, UI_EMBOSS);
683                         
684                         /* variable type */
685                         row= uiLayoutRow(box, 0);
686                                 uiItemR(row, &dvar_ptr, "type", 0, "", ICON_NONE);
687                                 
688                 /* variable type settings */
689                 box= uiLayoutBox(col);
690                         /* controls to draw depends on the type of variable */
691                         switch (dvar->type) {
692                                 case DVAR_TYPE_SINGLE_PROP:     /* single property */
693                                         graph_panel_driverVar__singleProp(box, ale->id, dvar);
694                                         break;
695                                 case DVAR_TYPE_ROT_DIFF: /* rotational difference */
696                                         graph_panel_driverVar__rotDiff(box, ale->id, dvar);
697                                         break;
698                                 case DVAR_TYPE_LOC_DIFF: /* location difference */
699                                         graph_panel_driverVar__locDiff(box, ale->id, dvar);
700                                         break;
701                                 case DVAR_TYPE_TRANSFORM_CHAN: /* transform channel */
702                                         graph_panel_driverVar__transChan(box, ale->id, dvar);
703                                         break;
704                         }
705
706                         /* value of variable */
707                         if (driver->flag & DRIVER_FLAG_SHOWDEBUG) {
708                                 char valBuf[32];
709
710                                 box= uiLayoutBox(col);
711                                 row= uiLayoutRow(box, 1);
712                                 uiItemL(row, "Value:", ICON_NONE);
713                                 
714                                 BLI_snprintf(valBuf, sizeof(valBuf), "%.3f", dvar->curval);
715                                 uiItemL(row, valBuf, ICON_NONE);
716                         }
717         }
718         
719         /* cleanup */
720         MEM_freeN(ale);
721 }
722
723 /* ******************* f-modifiers ******************************** */
724 /* all the drawing code is in editors/animation/fmodifier_ui.c */
725
726 #define B_FMODIFIER_REDRAW              20
727
728 static void do_graph_region_modifier_buttons(bContext *C, void *UNUSED(arg), int event)
729 {
730         switch (event) {
731                 case B_FMODIFIER_REDRAW: // XXX this should send depsgraph updates too
732                         WM_event_add_notifier(C, NC_ANIMATION, NULL); // XXX need a notifier specially for F-Modifiers
733                         break;
734         }
735 }
736
737 static void graph_panel_modifiers(const bContext *C, Panel *pa) 
738 {
739         bAnimListElem *ale;
740         FCurve *fcu;
741         FModifier *fcm;
742         uiLayout *col, *row;
743         uiBlock *block;
744         
745         if (!graph_panel_context(C, &ale, &fcu))
746                 return;
747         
748         block= uiLayoutGetBlock(pa->layout);
749         uiBlockSetHandleFunc(block, do_graph_region_modifier_buttons, NULL);
750         
751         /* 'add modifier' button at top of panel */
752         {
753                 row= uiLayoutRow(pa->layout, 0);
754                 block= uiLayoutGetBlock(row);
755                 
756                 // XXX for now, this will be a operator button which calls a 'add modifier' operator
757                 uiDefButO(block, BUT, "GRAPH_OT_fmodifier_add", WM_OP_INVOKE_REGION_WIN, IFACE_("Add Modifier"), 10, 0, 150, 20,
758                                 TIP_("Adds a new F-Curve Modifier for the active F-Curve"));
759                 
760                 /* copy/paste (as sub-row)*/
761                 row= uiLayoutRow(row, 1);
762                         uiItemO(row, "", ICON_COPYDOWN, "GRAPH_OT_fmodifier_copy");
763                         uiItemO(row, "", ICON_PASTEDOWN, "GRAPH_OT_fmodifier_paste");
764         }
765         
766         /* draw each modifier */
767         for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
768                 col= uiLayoutColumn(pa->layout, 1);
769                 
770                 ANIM_uiTemplate_fmodifier_draw(col, ale->id, &fcu->modifiers, fcm);
771         }
772
773         MEM_freeN(ale);
774 }
775
776 /* ******************* general ******************************** */
777
778 void graph_buttons_register(ARegionType *art)
779 {
780         PanelType *pt;
781
782         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel view");
783         strcpy(pt->idname, "GRAPH_PT_view");
784         strcpy(pt->label, "View Properties");
785         pt->draw= graph_panel_view;
786         pt->flag |= PNL_DEFAULT_CLOSED;
787         BLI_addtail(&art->paneltypes, pt);
788         
789         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel properties");
790         strcpy(pt->idname, "GRAPH_PT_properties");
791         strcpy(pt->label, "Active F-Curve");
792         pt->draw= graph_panel_properties;
793         pt->poll= graph_panel_poll;
794         BLI_addtail(&art->paneltypes, pt);
795         
796         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel properties");
797         strcpy(pt->idname, "GRAPH_PT_key_properties");
798         strcpy(pt->label, "Active Keyframe");
799         pt->draw= graph_panel_key_properties;
800         pt->poll= graph_panel_poll;
801         BLI_addtail(&art->paneltypes, pt);
802
803
804         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel drivers");
805         strcpy(pt->idname, "GRAPH_PT_drivers");
806         strcpy(pt->label, "Drivers");
807         pt->draw= graph_panel_drivers;
808         pt->poll= graph_panel_drivers_poll;
809         BLI_addtail(&art->paneltypes, pt);
810
811         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel modifiers");
812         strcpy(pt->idname, "GRAPH_PT_modifiers");
813         strcpy(pt->label, "Modifiers");
814         pt->draw= graph_panel_modifiers;
815         pt->poll= graph_panel_poll;
816         BLI_addtail(&art->paneltypes, pt);
817 }
818
819 static int graph_properties(bContext *C, wmOperator *UNUSED(op))
820 {
821         ScrArea *sa= CTX_wm_area(C);
822         ARegion *ar= graph_has_buttons_region(sa);
823         
824         if(ar)
825                 ED_region_toggle_hidden(C, ar);
826
827         return OPERATOR_FINISHED;
828 }
829
830 void GRAPH_OT_properties(wmOperatorType *ot)
831 {
832         ot->name= "Properties";
833         ot->idname= "GRAPH_OT_properties";
834         ot->description= "Toggle display properties panel";
835         
836         ot->exec= graph_properties;
837         ot->poll= ED_operator_graphedit_active;
838
839         /* flags */
840         ot->flag= 0;
841 }