FileBrowser: Editable Bookmarks.
[blender-staging.git] / source / blender / makesrna / intern / rna_space.c
index 382ed358e120d236468b350285e8ea271a5024b3..fc2b2d72ac9f8156f240f3546283e0beb040a793 100644 (file)
@@ -56,6 +56,8 @@
 #include "RE_engine.h"
 #include "RE_pipeline.h"
 
+#include "ED_fileselect.h"
+
 #include "RNA_enum_types.h"
 
 
@@ -204,6 +206,7 @@ static EnumPropertyItem buttons_texture_context_items[] = {
 #include "BKE_icons.h"
 
 #include "ED_buttons.h"
+#include "ED_fileselect.h"
 #include "ED_image.h"
 #include "ED_node.h"
 #include "ED_screen.h"
@@ -1368,6 +1371,271 @@ static void rna_SpaceClipEditor_view_type_update(Main *UNUSED(bmain), Scene *UNU
        ED_area_tag_refresh(sa);
 }
 
+/* File browser. */
+
+static void rna_FileBrowser_FSMenuEntry_path_get(PointerRNA *ptr, char *value)
+{
+       char *path = ED_fsmenu_entry_get_path(ptr->data);
+
+       strcpy(value, path ? path : "");
+}
+
+static int rna_FileBrowser_FSMenuEntry_path_length(PointerRNA *ptr)
+{
+       char *path = ED_fsmenu_entry_get_path(ptr->data);
+
+       return (int)(path ? strlen(path) : 0);
+}
+
+static void rna_FileBrowser_FSMenuEntry_path_set(PointerRNA *ptr, const char *value)
+{
+       FSMenuEntry *fsm = ptr->data;
+
+       /* Note: this will write to file immediately.
+        * Not nice (and to be fixed ultimately), but acceptable in this case for now. */
+       ED_fsmenu_entry_set_path(fsm, value);
+}
+
+static void rna_FileBrowser_FSMenuEntry_name_get(PointerRNA *ptr, char *value)
+{
+       strcpy(value, ED_fsmenu_entry_get_name(ptr->data));
+}
+
+static int rna_FileBrowser_FSMenuEntry_name_length(PointerRNA *ptr)
+{
+       return (int)strlen(ED_fsmenu_entry_get_name(ptr->data));
+}
+
+static void rna_FileBrowser_FSMenuEntry_name_set(PointerRNA *ptr, const char *value)
+{
+       FSMenuEntry *fsm = ptr->data;
+
+       /* Note: this will write to file immediately.
+        * Not nice (and to be fixed ultimately), but acceptable in this case for now. */
+       ED_fsmenu_entry_set_name(fsm, value);
+}
+
+static int rna_FileBrowser_FSMenuEntry_name_get_editable(PointerRNA *ptr)
+{
+       FSMenuEntry *fsm = ptr->data;
+
+       return fsm->save;
+}
+
+static void rna_FileBrowser_FSMenu_next(CollectionPropertyIterator *iter)
+{
+       ListBaseIterator *internal = &iter->internal.listbase;
+
+       if (internal->skip) {
+               do {
+                       internal->link = (Link *)(((FSMenuEntry *)(internal->link))->next);
+                       iter->valid = (internal->link != NULL);
+               } while (iter->valid && internal->skip(iter, internal->link));
+       }
+       else {
+               internal->link = (Link *)(((FSMenuEntry *)(internal->link))->next);
+               iter->valid = (internal->link != NULL);
+       }
+}
+
+static void rna_FileBrowser_FSMenu_begin(CollectionPropertyIterator *iter, FSMenuCategory category)
+{
+       ListBaseIterator *internal = &iter->internal.listbase;
+
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+       struct FSMenuEntry *fsmentry = ED_fsmenu_get_category(fsmenu, category);
+
+       internal->link = (fsmentry) ? (Link *)fsmentry : NULL;
+       internal->skip = NULL;
+
+       iter->valid = (internal->link != NULL);
+}
+
+static PointerRNA rna_FileBrowser_FSMenu_get(CollectionPropertyIterator *iter)
+{
+       ListBaseIterator *internal = &iter->internal.listbase;
+       PointerRNA r_ptr;
+
+       RNA_pointer_create(NULL, &RNA_FileBrowserFSMenuEntry, internal->link, &r_ptr);
+
+       return r_ptr;
+}
+
+static void rna_FileBrowser_FSMenu_end(CollectionPropertyIterator *UNUSED(iter))
+{
+}
+
+static void rna_FileBrowser_FSMenuSystem_data_begin(CollectionPropertyIterator *iter, PointerRNA *UNUSED(ptr))
+{
+       rna_FileBrowser_FSMenu_begin(iter, FS_CATEGORY_SYSTEM);
+}
+
+static int rna_FileBrowser_FSMenuSystem_data_length(PointerRNA *UNUSED(ptr))
+{
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+
+       return ED_fsmenu_get_nentries(fsmenu, FS_CATEGORY_SYSTEM);
+}
+
+static void rna_FileBrowser_FSMenuSystemBookmark_data_begin(CollectionPropertyIterator *iter, PointerRNA *UNUSED(ptr))
+{
+       rna_FileBrowser_FSMenu_begin(iter, FS_CATEGORY_SYSTEM_BOOKMARKS);
+}
+
+static int rna_FileBrowser_FSMenuSystemBookmark_data_length(PointerRNA *UNUSED(ptr))
+{
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+
+       return ED_fsmenu_get_nentries(fsmenu, FS_CATEGORY_SYSTEM_BOOKMARKS);
+}
+
+static void rna_FileBrowser_FSMenuBookmark_data_begin(CollectionPropertyIterator *iter, PointerRNA *UNUSED(ptr))
+{
+       rna_FileBrowser_FSMenu_begin(iter, FS_CATEGORY_BOOKMARKS);
+}
+
+static int rna_FileBrowser_FSMenuBookmark_data_length(PointerRNA *UNUSED(ptr))
+{
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+
+       return ED_fsmenu_get_nentries(fsmenu, FS_CATEGORY_BOOKMARKS);
+}
+
+static void rna_FileBrowser_FSMenuRecent_data_begin(CollectionPropertyIterator *iter, PointerRNA *UNUSED(ptr))
+{
+       rna_FileBrowser_FSMenu_begin(iter, FS_CATEGORY_RECENT);
+}
+
+static int rna_FileBrowser_FSMenuRecent_data_length(PointerRNA *UNUSED(ptr))
+{
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+
+       return ED_fsmenu_get_nentries(fsmenu, FS_CATEGORY_RECENT);
+}
+
+static int rna_FileBrowser_FSMenu_active_get(PointerRNA *ptr, const FSMenuCategory category)
+{
+       SpaceFile *sf = ptr->data;
+       int actnr = -1;
+
+       switch (category) {
+               case FS_CATEGORY_SYSTEM:
+                       actnr = sf->systemnr;
+                       break;
+               case FS_CATEGORY_SYSTEM_BOOKMARKS:
+                       actnr = sf->system_bookmarknr;
+                       break;
+               case FS_CATEGORY_BOOKMARKS:
+                       actnr = sf->bookmarknr;
+                       break;
+               case FS_CATEGORY_RECENT:
+                       actnr = sf->recentnr;
+                       break;
+       }
+
+       return actnr;
+}
+
+static void rna_FileBrowser_FSMenu_active_set(PointerRNA *ptr, int value, const FSMenuCategory category)
+{
+       SpaceFile *sf = ptr->data;
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+       FSMenuEntry *fsm = ED_fsmenu_get_entry(fsmenu, category, value);
+
+       if (fsm && sf->params) {
+               switch (category) {
+                       case FS_CATEGORY_SYSTEM:
+                               sf->systemnr = value;
+                               break;
+                       case FS_CATEGORY_SYSTEM_BOOKMARKS:
+                               sf->system_bookmarknr = value;
+                               break;
+                       case FS_CATEGORY_BOOKMARKS:
+                               sf->bookmarknr = value;
+                               break;
+                       case FS_CATEGORY_RECENT:
+                               sf->recentnr = value;
+                               break;
+               }
+
+               BLI_strncpy(sf->params->dir, fsm->path, sizeof(sf->params->dir));
+       }
+}
+
+static void rna_FileBrowser_FSMenu_active_range(
+        PointerRNA *UNUSED(ptr), int *min, int *max, int *softmin, int *softmax, const FSMenuCategory category)
+{
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+
+       *min = *softmin = -1;
+       *max = *softmax = ED_fsmenu_get_nentries(fsmenu, category) - 1;
+}
+
+static void rna_FileBrowser_FSMenu_active_update(struct bContext *C, PointerRNA *UNUSED(ptr))
+{
+       ED_file_change_dir(C, true);
+}
+
+static int rna_FileBrowser_FSMenuSystem_active_get(PointerRNA *ptr)
+{
+       return rna_FileBrowser_FSMenu_active_get(ptr, FS_CATEGORY_SYSTEM);
+}
+
+static void rna_FileBrowser_FSMenuSystem_active_set(PointerRNA *ptr, int value)
+{
+       rna_FileBrowser_FSMenu_active_set(ptr, value, FS_CATEGORY_SYSTEM);
+}
+
+static void rna_FileBrowser_FSMenuSystem_active_range(PointerRNA *ptr, int *min, int *max, int *softmin, int *softmax)
+{
+       rna_FileBrowser_FSMenu_active_range(ptr, min, max, softmin, softmax, FS_CATEGORY_SYSTEM);
+}
+
+static int rna_FileBrowser_FSMenuSystemBookmark_active_get(PointerRNA *ptr)
+{
+       return rna_FileBrowser_FSMenu_active_get(ptr, FS_CATEGORY_SYSTEM_BOOKMARKS);
+}
+
+static void rna_FileBrowser_FSMenuSystemBookmark_active_set(PointerRNA *ptr, int value)
+{
+       rna_FileBrowser_FSMenu_active_set(ptr, value, FS_CATEGORY_SYSTEM_BOOKMARKS);
+}
+
+static void rna_FileBrowser_FSMenuSystemBookmark_active_range(PointerRNA *ptr, int *min, int *max, int *softmin, int *softmax)
+{
+       rna_FileBrowser_FSMenu_active_range(ptr, min, max, softmin, softmax, FS_CATEGORY_SYSTEM_BOOKMARKS);
+}
+
+static int rna_FileBrowser_FSMenuBookmark_active_get(PointerRNA *ptr)
+{
+       return rna_FileBrowser_FSMenu_active_get(ptr, FS_CATEGORY_BOOKMARKS);
+}
+
+static void rna_FileBrowser_FSMenuBookmark_active_set(PointerRNA *ptr, int value)
+{
+       rna_FileBrowser_FSMenu_active_set(ptr, value, FS_CATEGORY_BOOKMARKS);
+}
+
+static void rna_FileBrowser_FSMenuBookmark_active_range(PointerRNA *ptr, int *min, int *max, int *softmin, int *softmax)
+{
+       rna_FileBrowser_FSMenu_active_range(ptr, min, max, softmin, softmax, FS_CATEGORY_BOOKMARKS);
+}
+
+static int rna_FileBrowser_FSMenuRecent_active_get(PointerRNA *ptr)
+{
+       return rna_FileBrowser_FSMenu_active_get(ptr, FS_CATEGORY_RECENT);
+}
+
+static void rna_FileBrowser_FSMenuRecent_active_set(PointerRNA *ptr, int value)
+{
+       rna_FileBrowser_FSMenu_active_set(ptr, value, FS_CATEGORY_RECENT);
+}
+
+static void rna_FileBrowser_FSMenuRecent_active_range(PointerRNA *ptr, int *min, int *max, int *softmin, int *softmax)
+{
+       rna_FileBrowser_FSMenu_active_range(ptr, min, max, softmin, softmax, FS_CATEGORY_RECENT);
+}
+
 #else
 
 static EnumPropertyItem dt_uv_items[] = {
@@ -3332,6 +3600,37 @@ static void rna_def_fileselect_params(BlenderRNA *brna)
        RNA_def_property_update(prop, NC_SPACE | ND_SPACE_FILE_LIST, NULL);
 }
 
+static void rna_def_filemenu_entry(BlenderRNA *brna)
+{
+       StructRNA *srna;
+       PropertyRNA *prop;
+
+       srna = RNA_def_struct(brna, "FileBrowserFSMenuEntry", NULL);
+       RNA_def_struct_sdna(srna, "FSMenuEntry");
+       RNA_def_struct_ui_text(srna, "File Select Parameters", "File Select Parameters");
+
+       prop = RNA_def_property(srna, "path", PROP_STRING, PROP_FILEPATH);
+       RNA_def_property_string_sdna(prop, NULL, "path");
+       RNA_def_property_string_funcs(prop, "rna_FileBrowser_FSMenuEntry_path_get",
+                                           "rna_FileBrowser_FSMenuEntry_path_length",
+                                           "rna_FileBrowser_FSMenuEntry_path_set");
+       RNA_def_property_ui_text(prop, "Path", "");
+
+       prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
+       RNA_def_property_string_sdna(prop, NULL, "name");
+       RNA_def_property_string_funcs(prop, "rna_FileBrowser_FSMenuEntry_name_get",
+                                           "rna_FileBrowser_FSMenuEntry_name_length",
+                                           "rna_FileBrowser_FSMenuEntry_name_set");
+       RNA_def_property_editable_func(prop, "rna_FileBrowser_FSMenuEntry_name_get_editable");
+       RNA_def_property_ui_text(prop, "Name", "");
+       RNA_def_struct_name_property(srna, prop);
+
+       prop = RNA_def_property(srna, "use_save", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "save", 1);
+       RNA_def_property_ui_text(prop, "Save", "Whether this path is saved in bookmarks, or generated from OS");
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+}
+
 static void rna_def_space_filebrowser(BlenderRNA *brna)
 {
        StructRNA *srna;
@@ -3354,6 +3653,67 @@ static void rna_def_space_filebrowser(BlenderRNA *brna)
        prop = RNA_def_property(srna, "operator", PROP_POINTER, PROP_NONE);
        RNA_def_property_pointer_sdna(prop, NULL, "op");
        RNA_def_property_ui_text(prop, "Active Operator", "");
+
+       /* bookmarks, recent files etc. */
+       prop = RNA_def_collection(srna, "system_folders", "FileBrowserFSMenuEntry", "System Folders",
+                                 "System's folders (usually root, available hard drives, etc)");
+       RNA_def_property_collection_funcs(prop, "rna_FileBrowser_FSMenuSystem_data_begin", "rna_FileBrowser_FSMenu_next",
+                                         "rna_FileBrowser_FSMenu_end", "rna_FileBrowser_FSMenu_get",
+                                         "rna_FileBrowser_FSMenuSystem_data_length", NULL, NULL, NULL);
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+
+       prop = RNA_def_int(srna, "system_folders_active", -1, -1, INT_MAX, "Active System Folder",
+                          "Index of active system folder (-1 if none)", -1, INT_MAX);
+       RNA_def_property_int_sdna(prop, NULL, "systemnr");
+       RNA_def_property_int_funcs(prop, "rna_FileBrowser_FSMenuSystem_active_get",
+                                  "rna_FileBrowser_FSMenuSystem_active_set", "rna_FileBrowser_FSMenuSystem_active_range");
+       RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
+       RNA_def_property_update(prop, NC_SPACE | ND_SPACE_FILE_PARAMS, "rna_FileBrowser_FSMenu_active_update");
+
+       prop = RNA_def_collection(srna, "system_bookmarks", "FileBrowserFSMenuEntry", "System Bookmarks",
+                                 "System's bookmarks");
+       RNA_def_property_collection_funcs(prop, "rna_FileBrowser_FSMenuSystemBookmark_data_begin", "rna_FileBrowser_FSMenu_next",
+                                         "rna_FileBrowser_FSMenu_end", "rna_FileBrowser_FSMenu_get",
+                                         "rna_FileBrowser_FSMenuSystemBookmark_data_length", NULL, NULL, NULL);
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+
+       prop = RNA_def_int(srna, "system_bookmarks_active", -1, -1, INT_MAX, "Active System Bookmark",
+                          "Index of active system bookmark (-1 if none)", -1, INT_MAX);
+       RNA_def_property_int_sdna(prop, NULL, "system_bookmarknr");
+       RNA_def_property_int_funcs(prop, "rna_FileBrowser_FSMenuSystemBookmark_active_get",
+                                  "rna_FileBrowser_FSMenuSystemBookmark_active_set", "rna_FileBrowser_FSMenuSystemBookmark_active_range");
+       RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
+       RNA_def_property_update(prop, NC_SPACE | ND_SPACE_FILE_PARAMS, "rna_FileBrowser_FSMenu_active_update");
+
+       prop = RNA_def_collection(srna, "bookmarks", "FileBrowserFSMenuEntry", "Bookmarks",
+                                 "User's bookmarks");
+       RNA_def_property_collection_funcs(prop, "rna_FileBrowser_FSMenuBookmark_data_begin", "rna_FileBrowser_FSMenu_next",
+                                         "rna_FileBrowser_FSMenu_end", "rna_FileBrowser_FSMenu_get",
+                                         "rna_FileBrowser_FSMenuBookmark_data_length", NULL, NULL, NULL);
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+
+       prop = RNA_def_int(srna, "bookmarks_active", -1, -1, INT_MAX, "Active Bookmark",
+                          "Index of active bookmark (-1 if none)", -1, INT_MAX);
+       RNA_def_property_int_sdna(prop, NULL, "bookmarknr");
+       RNA_def_property_int_funcs(prop, "rna_FileBrowser_FSMenuBookmark_active_get",
+                                  "rna_FileBrowser_FSMenuBookmark_active_set", "rna_FileBrowser_FSMenuBookmark_active_range");
+       RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
+       RNA_def_property_update(prop, NC_SPACE | ND_SPACE_FILE_PARAMS, "rna_FileBrowser_FSMenu_active_update");
+
+       prop = RNA_def_collection(srna, "recent_folders", "FileBrowserFSMenuEntry", "Recent Folders",
+                                 "");
+       RNA_def_property_collection_funcs(prop, "rna_FileBrowser_FSMenuRecent_data_begin", "rna_FileBrowser_FSMenu_next",
+                                         "rna_FileBrowser_FSMenu_end", "rna_FileBrowser_FSMenu_get",
+                                         "rna_FileBrowser_FSMenuRecent_data_length", NULL, NULL, NULL);
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+
+       prop = RNA_def_int(srna, "recent_folders_active", -1, -1, INT_MAX, "Active Recent Folder",
+                          "Index of active recent folder (-1 if none)", -1, INT_MAX);
+       RNA_def_property_int_sdna(prop, NULL, "recentnr");
+       RNA_def_property_int_funcs(prop, "rna_FileBrowser_FSMenuRecent_active_get",
+                                  "rna_FileBrowser_FSMenuRecent_active_set", "rna_FileBrowser_FSMenuRecent_active_range");
+       RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
+       RNA_def_property_update(prop, NC_SPACE | ND_SPACE_FILE_PARAMS, "rna_FileBrowser_FSMenu_active_update");
 }
 
 static void rna_def_space_info(BlenderRNA *brna)
@@ -3957,6 +4317,7 @@ void RNA_def_space(BlenderRNA *brna)
        rna_def_space_sequencer(brna);
        rna_def_space_text(brna);
        rna_def_fileselect_params(brna);
+       rna_def_filemenu_entry(brna);
        rna_def_space_filebrowser(brna);
        rna_def_space_outliner(brna);
        rna_def_background_image(brna);