2.5: File browse button in ui layouts now works, e.g. for render
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Tue, 28 Jul 2009 18:51:06 +0000 (18:51 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Tue, 28 Jul 2009 18:51:06 +0000 (18:51 +0000)
output path or fluidsim path.

source/blender/editors/include/UI_interface.h
source/blender/editors/interface/interface_anim.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_layout.c
source/blender/editors/space_buttons/buttons_intern.h
source/blender/editors/space_buttons/buttons_ops.c
source/blender/editors/space_buttons/space_buttons.c

index d818f298de7a27a97a2de41ffba8c5519cf24a58..70fbad3216daed773a0f51709ce32531fbfbcaeb 100644 (file)
@@ -142,6 +142,7 @@ typedef struct uiLayout uiLayout;
 #define UI_BUT_ANIMATED_KEY    (1<<21)
 #define UI_BUT_DRIVEN          (1<<22)
 #define UI_BUT_INACTIVE                (1<<23)
+#define UI_BUT_LAST_ACTIVE     (1<<24)
 
 #define UI_PANEL_WIDTH                 340
 #define UI_COMPACT_PANEL_WIDTH 160
@@ -674,9 +675,9 @@ void uiItemMenuF(uiLayout *layout, char *name, int icon, uiMenuCreateFunc func);
 void uiItemMenuEnumO(uiLayout *layout, char *name, int icon, char *opname, char *propname);
 void uiItemMenuEnumR(uiLayout *layout, char *name, int icon, struct PointerRNA *ptr, char *propname);
 
-/* Animation */
-
+/* Helpers for Operators */
 void uiAnimContextProperty(const struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index);
+void uiFileBrowseContextProperty(const struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop);
 
 /* Styled text draw */
 void uiStyleFontSet(struct uiFontStyle *fs);
index 4a2ef50a31b5af0bb1f6103419c9a094752ddcc4..d2e9236fcff8900a352e26f9ce9faf327c71c8da 100644 (file)
@@ -165,6 +165,10 @@ void uiAnimContextProperty(const bContext *C, struct PointerRNA *ptr, struct Pro
        uiBlock *block;
        uiBut *but;
 
+       memset(ptr, 0, sizeof(*ptr));
+       *prop= NULL;
+       *index= 0;
+
        if(ar) {
                for(block=ar->uiblocks.first; block; block=block->next) {
                        for(but=block->buttons.first; but; but= but->next) {
index 50877439e89a03025aee5bd5123c217053f93add..93293a6f352cdc8e46c4466c71c7d17118efd921 100644 (file)
@@ -3687,6 +3687,7 @@ static void button_activate_init(bContext *C, ARegion *ar, uiBut *but, uiButtonA
 static void button_activate_exit(bContext *C, uiHandleButtonData *data, uiBut *but, int mousemove)
 {
        uiBlock *block= but->block;
+       uiBut *bt;
 
        /* ensure we are in the exit state */
        if(data->state != BUTTON_STATE_EXIT)
@@ -3712,7 +3713,14 @@ static void button_activate_exit(bContext *C, uiHandleButtonData *data, uiBut *b
        if(!data->cancel)
                ui_apply_autokey_undo(C, but);
 
-       /* disable tooltips until mousemove */
+       /* disable tooltips until mousemove + last active flag */
+       for(block=data->region->uiblocks.first; block; block=block->next) {
+               for(bt=block->buttons.first; bt; bt=bt->next)
+                       bt->flag &= ~UI_BUT_LAST_ACTIVE;
+
+               block->tooltipdisabled= 1;
+       }
+
        ui_blocks_set_tooltips(data->region, 0);
 
        /* clean up */
@@ -3728,6 +3736,7 @@ static void button_activate_exit(bContext *C, uiHandleButtonData *data, uiBut *b
        MEM_freeN(but->active);
        but->active= NULL;
        but->flag &= ~(UI_ACTIVE|UI_SELECT);
+       but->flag |= UI_BUT_LAST_ACTIVE;
        ui_check_but(but);
 
        /* adds empty mousemove in queue for re-init handler, in case mouse is
index 2705cde27c9d4a131b9854760ca7a03e9f0c5134..6932ad350b001ad38bac3838a61c7e279ee2490c 100644 (file)
@@ -486,7 +486,9 @@ static uiBut *ui_item_with_label(uiLayout *layout, uiBlock *block, char *name, i
        if(subtype == PROP_FILEPATH || subtype == PROP_DIRPATH) {
                uiBlockSetCurLayout(block, uiLayoutRow(sub, 1));
                uiDefAutoButR(block, ptr, prop, index, "", icon, x, y, w-UI_UNIT_X, h);
-               but= uiDefIconBut(block, BUT, 0, ICON_FILESEL, x, y, UI_UNIT_X, h, NULL, 0.0f, 0.0f, 0.0f, 0.0f, "DUMMY file select button"); /* XXX */
+
+               /* BUTTONS_OT_file_browse calls uiFileBrowseContextProperty */
+               but= uiDefIconButO(block, BUT, "BUTTONS_OT_file_browse", WM_OP_INVOKE_DEFAULT, ICON_FILESEL, x, y, UI_UNIT_X, h, "Browse for file or directory.");
        }
        else
                but= uiDefAutoButR(block, ptr, prop, index, "", icon, x, y, w, h);
@@ -495,6 +497,34 @@ static uiBut *ui_item_with_label(uiLayout *layout, uiBlock *block, char *name, i
        return but;
 }
 
+void uiFileBrowseContextProperty(const bContext *C, PointerRNA *ptr, PropertyRNA **prop)
+{
+       ARegion *ar= CTX_wm_region(C);
+       uiBlock *block;
+       uiBut *but, *prevbut;
+
+       memset(ptr, 0, sizeof(*ptr));
+       *prop= NULL;
+
+       if(!ar)
+               return;
+
+       for(block=ar->uiblocks.first; block; block=block->next) {
+               for(but=block->buttons.first; but; but= but->next) {
+                       prevbut= but->prev;
+
+                       /* find the button before the active one */
+                       if((but->flag & UI_BUT_LAST_ACTIVE) && prevbut && prevbut->rnapoin.id.data) {
+                               if(RNA_property_type(prevbut->rnaprop) == PROP_STRING) {
+                                       *ptr= prevbut->rnapoin;
+                                       *prop= prevbut->rnaprop;
+                                       return;
+                               }
+                       }
+               }
+       }
+}
+
 /********************* Button Items *************************/
 
 /* disabled item */
index c505c7fdb18ffecf9463247aabfa39d2216dc253..8ed17ab1fa88df3ea780afb52aa6e29e5a8fd71c 100644 (file)
@@ -89,6 +89,7 @@ void PARTICLE_OT_target_move_down(struct wmOperatorType *ot);
 void SCENE_OT_render_layer_add(struct wmOperatorType *ot);
 void SCENE_OT_render_layer_remove(struct wmOperatorType *ot);
 
+void BUTTONS_OT_file_browse(struct wmOperatorType *ot);
 void BUTTONS_OT_toolbox(struct wmOperatorType *ot);
 
 #endif /* ED_BUTTONS_INTERN_H */
index b1ffc7249f4b45ba8868f579c77af551845af55c..e52efe8508dfb89c959e7c8c073014fee5e17774 100644 (file)
@@ -72,6 +72,7 @@
 #include "RNA_define.h"
 
 #include "UI_interface.h"
+#include "UI_resources.h"
 
 #include "buttons_intern.h"    // own include
 
@@ -935,3 +936,73 @@ void BUTTONS_OT_toolbox(wmOperatorType *ot)
        ot->invoke= toolbox_invoke;
 }
 
+/********************** filebrowse operator *********************/
+
+typedef struct FileBrowseOp {
+       PointerRNA ptr;
+       PropertyRNA *prop;
+} FileBrowseOp;
+
+static int file_browse_exec(bContext *C, wmOperator *op)
+{
+       FileBrowseOp *fbo= op->customdata;
+       char *str;
+       
+       str= RNA_string_get_alloc(op->ptr, "filename", 0, 0);
+       RNA_property_string_set(&fbo->ptr, fbo->prop, str);
+       RNA_property_update(C, &fbo->ptr, fbo->prop);
+       MEM_freeN(str);
+
+       MEM_freeN(op->customdata);
+       return OPERATOR_FINISHED;
+}
+
+static int file_browse_cancel(bContext *C, wmOperator *op)
+{
+       MEM_freeN(op->customdata);
+       op->customdata= NULL;
+
+       return OPERATOR_CANCELLED;
+}
+
+static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       PointerRNA ptr;
+       PropertyRNA *prop;
+       FileBrowseOp *fbo;
+       char *str;
+
+       uiFileBrowseContextProperty(C, &ptr, &prop);
+
+       if(!prop)
+               return OPERATOR_CANCELLED;
+       
+       fbo= MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
+       fbo->ptr= ptr;
+       fbo->prop= prop;
+       op->customdata= fbo;
+
+       str= RNA_property_string_get_alloc(&ptr, prop, 0, 0);
+       RNA_string_set(op->ptr, "filename", str);
+       MEM_freeN(str);
+
+       WM_event_add_fileselect(C, op); 
+       
+       return OPERATOR_RUNNING_MODAL;
+}
+
+void BUTTONS_OT_file_browse(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "File Browse";
+       ot->idname= "BUTTONS_OT_file_browse";
+       
+       /* api callbacks */
+       ot->invoke= file_browse_invoke;
+       ot->exec= file_browse_exec;
+       ot->cancel= file_browse_cancel;
+
+       /* properties */
+       WM_operator_properties_filesel(ot, 0);
+}
+
index 0195ba4b18df4b8432e94a7d054607d5afecb9b9..0f57152cef6a2a90f60361a779efff6fadd13766 100644 (file)
@@ -209,6 +209,7 @@ void buttons_operatortypes(void)
        WM_operatortype_append(SCENE_OT_render_layer_remove);
 
        WM_operatortype_append(BUTTONS_OT_toolbox);
+       WM_operatortype_append(BUTTONS_OT_file_browse);
 }
 
 void buttons_keymap(struct wmWindowManager *wm)