Cleanup: Rename callback flags from library_query to `IDWALK_CB_...`
[blender-staging.git] / source / blender / windowmanager / intern / wm_operators.c
index bbe6c76aaa2694f3b3404feb4be00ad51f11c50e..a3f403e0bb34690e9b6b55bc9521127a4dc93759 100644 (file)
 #include "DNA_scene_types.h"
 #include "DNA_userdef_types.h"
 #include "DNA_windowmanager_types.h"
-#include "DNA_mesh_types.h" /* only for USE_BMESH_SAVE_AS_COMPAT */
 
 #include "BLT_translation.h"
 
 #include "PIL_time.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_bitmap.h"
 #include "BLI_dial.h"
 #include "BLI_dynstr.h" /*for WM_operator_pystring */
-#include "BLI_linklist.h"
-#include "BLI_linklist_stack.h"
 #include "BLI_math.h"
-#include "BLI_memarena.h"
 #include "BLI_utildefines.h"
 #include "BLI_ghash.h"
 
 #include "BLO_readfile.h"
 
 #include "BKE_appdir.h"
-#include "BKE_autoexec.h"
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_brush.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_scene.h"
 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
 #include "BKE_unit.h"
-#include "BKE_utildefines.h"
 
 #include "BKE_idcode.h"
 
 #include "BIF_glutil.h" /* for paint cursor */
 #include "BLF_api.h"
 
-#include "IMB_colormanagement.h"
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
 
 #include "ED_view3d.h"
 
 #include "GPU_basic_shader.h"
-#include "GPU_material.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
@@ -1077,7 +1068,7 @@ int WM_operator_smooth_viewtx_get(const wmOperator *op)
 }
 
 /* invoke callback, uses enum property named "type" */
-int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
+int WM_menu_invoke_ex(bContext *C, wmOperator *op, int opcontext)
 {
        PropertyRNA *prop = op->type->prop;
        uiPopupMenu *pup;
@@ -1099,8 +1090,8 @@ int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
                pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
                layout = UI_popup_menu_layout(pup);
                /* set this so the default execution context is the same as submenus */
-               uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_REGION_WIN);
-               uiItemsFullEnumO(layout, op->type->idname, RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
+               uiLayoutSetOperatorContext(layout, opcontext);
+               uiItemsFullEnumO(layout, op->type->idname, RNA_property_identifier(prop), op->ptr->data, opcontext, 0);
                UI_popup_menu_end(C, pup);
                return OPERATOR_INTERFACE;
        }
@@ -1108,6 +1099,11 @@ int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
        return OPERATOR_CANCELLED;
 }
 
+int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
+{
+       return WM_menu_invoke_ex(C, op, WM_OP_INVOKE_REGION_WIN);
+}
+
 
 /* generic enum search invoke popup */
 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
@@ -1373,12 +1369,13 @@ typedef struct wmOpPopUp {
 /* Only invoked by OK button in popups created with wm_block_dialog_create() */
 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
 {
-       wmWindowManager *wm = CTX_wm_manager(C);
-       wmWindow *win = CTX_wm_window(C);
-
        wmOpPopUp *data = arg1;
        uiBlock *block = arg2;
 
+       /* Explicitly set UI_RETURN_OK flag, otherwise the menu might be cancelled
+        * in case WM_operator_call_ex exits/reloads the current file (T49199). */
+       UI_popup_menu_retval_set(block, UI_RETURN_OK, true);
+
        WM_operator_call_ex(C, data->op, true);
 
        /* let execute handle freeing it */
@@ -1388,14 +1385,24 @@ static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
        /* in this case, wm_operator_ui_popup_cancel wont run */
        MEM_freeN(data);
 
+       /* get context data *after* WM_operator_call_ex which might have closed the current file and changed context */
+       wmWindowManager *wm = CTX_wm_manager(C);
+       wmWindow *win = CTX_wm_window(C);
+
        /* check window before 'block->handle' incase the
-        * popup execution closed the window and freed the block. see T44688. */
+        * popup execution closed the window and freed the block. see T44688.
+        */
+       /* Post 2.78 TODO: Check if this fix and others related to T44688 are still
+        * needed or can be improved now that requesting context data has been corrected
+        * (see above). We're close to release so not a good time for experiments.
+        * -- Julian
+        */
        if (BLI_findindex(&wm->windows, win) != -1) {
                UI_popup_block_close(C, win, block);
        }
 }
 
-static void dialog_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
+static void popup_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
 {
        wmOperator *op = op_ptr;
        if (op->type->check) {
@@ -1427,7 +1434,7 @@ static uiBlock *wm_block_dialog_create(bContext *C, ARegion *ar, void *userData)
 
        layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, 0, style);
        
-       UI_block_func_set(block, dialog_check_cb, op, NULL);
+       UI_block_func_set(block, popup_check_cb, op, NULL);
 
        uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
        
@@ -1467,9 +1474,13 @@ static uiBlock *wm_operator_ui_create(bContext *C, ARegion *ar, void *userData)
 
        layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, 0, style);
 
+       UI_block_func_set(block, popup_check_cb, op, NULL);
+
        /* since ui is defined the auto-layout args are not used */
        uiLayoutOperatorButs(C, layout, op, NULL, 'V', 0);
 
+       UI_block_func_set(block, NULL, NULL, NULL);
+
        UI_block_bounds_set_popup(block, 4, 0, 0);
 
        return block;
@@ -1835,7 +1846,7 @@ static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(ar
                     BLENDER_VERSION / 100, BLENDER_VERSION % 100);
        uiItemStringO(col, IFACE_("Release Log"), ICON_URL, "WM_OT_url_open", "url", url);
        uiItemStringO(col, IFACE_("Manual"), ICON_URL, "WM_OT_url_open", "url",
-                     "http://www.blender.org/manual");
+                     "https://docs.blender.org/manual/en/dev/");
        uiItemStringO(col, IFACE_("Blender Website"), ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org");
        if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "release")) {
                BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d"
@@ -2074,1025 +2085,6 @@ static void WM_OT_window_duplicate(wmOperatorType *ot)
        ot->poll = wm_operator_winactive_normal;
 }
 
-static void WM_OT_save_homefile(wmOperatorType *ot)
-{
-       ot->name = "Save Startup File";
-       ot->idname = "WM_OT_save_homefile";
-       ot->description = "Make the current file the default .blend file, includes preferences";
-               
-       ot->invoke = WM_operator_confirm;
-       ot->exec = wm_homefile_write_exec;
-}
-
-static int wm_userpref_autoexec_add_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
-{
-       bPathCompare *path_cmp = MEM_callocN(sizeof(bPathCompare), "bPathCompare");
-       BLI_addtail(&U.autoexec_paths, path_cmp);
-       return OPERATOR_FINISHED;
-}
-
-static void WM_OT_userpref_autoexec_path_add(wmOperatorType *ot)
-{
-       ot->name = "Add Autoexec Path";
-       ot->idname = "WM_OT_userpref_autoexec_path_add";
-       ot->description = "Add path to exclude from autoexecution";
-
-       ot->exec = wm_userpref_autoexec_add_exec;
-
-       ot->flag = OPTYPE_INTERNAL;
-}
-
-static int wm_userpref_autoexec_remove_exec(bContext *UNUSED(C), wmOperator *op)
-{
-       const int index = RNA_int_get(op->ptr, "index");
-       bPathCompare *path_cmp = BLI_findlink(&U.autoexec_paths, index);
-       if (path_cmp) {
-               BLI_freelinkN(&U.autoexec_paths, path_cmp);
-       }
-       return OPERATOR_FINISHED;
-}
-
-static void WM_OT_userpref_autoexec_path_remove(wmOperatorType *ot)
-{
-       ot->name = "Remove Autoexec Path";
-       ot->idname = "WM_OT_userpref_autoexec_path_remove";
-       ot->description = "Remove path to exclude from autoexecution";
-
-       ot->exec = wm_userpref_autoexec_remove_exec;
-
-       ot->flag = OPTYPE_INTERNAL;
-
-       RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, 1000);
-}
-
-static void WM_OT_save_userpref(wmOperatorType *ot)
-{
-       ot->name = "Save User Settings";
-       ot->idname = "WM_OT_save_userpref";
-       ot->description = "Save user preferences separately, overrides startup file preferences";
-       
-       ot->invoke = WM_operator_confirm;
-       ot->exec = wm_userpref_write_exec;
-}
-
-static void WM_OT_read_history(wmOperatorType *ot)
-{
-       ot->name = "Reload History File";
-       ot->idname = "WM_OT_read_history";
-       ot->description = "Reloads history and bookmarks";
-
-       ot->invoke = WM_operator_confirm;
-       ot->exec = wm_history_file_read_exec;
-
-       /* this operator is only used for loading settings from a previous blender install */
-       ot->flag = OPTYPE_INTERNAL;
-}
-
-static void WM_OT_read_homefile(wmOperatorType *ot)
-{
-       PropertyRNA *prop;
-       ot->name = "Reload Start-Up File";
-       ot->idname = "WM_OT_read_homefile";
-       ot->description = "Open the default file (doesn't save the current file)";
-       
-       ot->invoke = WM_operator_confirm;
-       ot->exec = wm_homefile_read_exec;
-
-       prop = RNA_def_string_file_path(ot->srna, "filepath", NULL,
-                                       FILE_MAX, "File Path", 
-                                       "Path to an alternative start-up file");
-       RNA_def_property_flag(prop, PROP_HIDDEN);
-
-       /* So scripts can use an alternative start-up file without the UI */
-       prop = RNA_def_boolean(ot->srna, "load_ui", true, "Load UI",
-                              "Load user interface setup from the .blend file");
-       RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
-
-       /* omit poll to run in background mode */
-}
-
-static void WM_OT_read_factory_settings(wmOperatorType *ot)
-{
-       ot->name = "Load Factory Settings";
-       ot->idname = "WM_OT_read_factory_settings";
-       ot->description = "Load default file and user preferences";
-       
-       ot->invoke = WM_operator_confirm;
-       ot->exec = wm_homefile_read_exec;
-       /* omit poll to run in background mode */
-}
-
-/* *************** open file **************** */
-
-/**
- * Wrap #WM_file_read, shared by file reading operators.
- */
-static bool wm_file_read_opwrap(bContext *C, const char *filepath, ReportList *reports,
-                                const bool autoexec_init)
-{
-       bool success;
-
-       /* XXX wm in context is not set correctly after WM_file_read -> crash */
-       /* do it before for now, but is this correct with multiple windows? */
-       WM_event_add_notifier(C, NC_WINDOW, NULL);
-
-       if (autoexec_init) {
-               WM_file_autoexec_init(filepath);
-       }
-
-       success = WM_file_read(C, filepath, reports);
-
-       return success;
-}
-
-/* currently fits in a pointer */
-struct FileRuntime {
-       bool is_untrusted;
-};
-
-static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
-{
-       const char *openname = G.main->name;
-
-       if (CTX_wm_window(C) == NULL) {
-               /* in rare cases this could happen, when trying to invoke in background
-                * mode on load for example. Don't use poll for this because exec()
-                * can still run without a window */
-               BKE_report(op->reports, RPT_ERROR, "Context window not set");
-               return OPERATOR_CANCELLED;
-       }
-
-       /* if possible, get the name of the most recently used .blend file */
-       if (G.recent_files.first) {
-               struct RecentFile *recent = G.recent_files.first;
-               openname = recent->filepath;
-       }
-
-       RNA_string_set(op->ptr, "filepath", openname);
-       wm_open_init_load_ui(op, true);
-       wm_open_init_use_scripts(op, true);
-       op->customdata = NULL;
-
-       WM_event_add_fileselect(C, op);
-
-       return OPERATOR_RUNNING_MODAL;
-}
-
-static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
-{
-       char filepath[FILE_MAX];
-       bool success;
-
-       RNA_string_get(op->ptr, "filepath", filepath);
-
-       /* re-use last loaded setting so we can reload a file without changing */
-       wm_open_init_load_ui(op, false);
-       wm_open_init_use_scripts(op, false);
-
-       if (RNA_boolean_get(op->ptr, "load_ui"))
-               G.fileflags &= ~G_FILE_NO_UI;
-       else
-               G.fileflags |= G_FILE_NO_UI;
-               
-       if (RNA_boolean_get(op->ptr, "use_scripts"))
-               G.f |= G_SCRIPT_AUTOEXEC;
-       else
-               G.f &= ~G_SCRIPT_AUTOEXEC;
-       
-       success = wm_file_read_opwrap(C, filepath, op->reports, !(G.f & G_SCRIPT_AUTOEXEC));
-
-       /* for file open also popup for warnings, not only errors */
-       BKE_report_print_level_set(op->reports, RPT_WARNING);
-
-       if (success) {
-               return OPERATOR_FINISHED;
-       }
-       else {
-               return OPERATOR_CANCELLED;
-       }
-}
-
-static bool wm_open_mainfile_check(bContext *UNUSED(C), wmOperator *op)
-{
-       struct FileRuntime *file_info = (struct FileRuntime *)&op->customdata;
-       PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_scripts");
-       bool is_untrusted = false;
-       char path[FILE_MAX];
-       char *lslash;
-
-       RNA_string_get(op->ptr, "filepath", path);
-
-       /* get the dir */
-       lslash = (char *)BLI_last_slash(path);
-       if (lslash) *(lslash + 1) = '\0';
-
-       if ((U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0) {
-               if (BKE_autoexec_match(path) == true) {
-                       RNA_property_boolean_set(op->ptr, prop, false);
-                       is_untrusted = true;
-               }
-       }
-
-       if (file_info) {
-               file_info->is_untrusted = is_untrusted;
-       }
-
-       return is_untrusted;
-}
-
-static void wm_open_mainfile_ui(bContext *UNUSED(C), wmOperator *op)
-{
-       struct FileRuntime *file_info = (struct FileRuntime *)&op->customdata;
-       uiLayout *layout = op->layout;
-       uiLayout *col = op->layout;
-       const char *autoexec_text;
-
-       uiItemR(layout, op->ptr, "load_ui", 0, NULL, ICON_NONE);
-
-       col = uiLayoutColumn(layout, false);
-       if (file_info->is_untrusted) {
-               autoexec_text = IFACE_("Trusted Source [Untrusted Path]");
-               uiLayoutSetActive(col, false);
-               uiLayoutSetEnabled(col, false);
-       }
-       else {
-               autoexec_text = IFACE_("Trusted Source");
-       }
-
-       uiItemR(col, op->ptr, "use_scripts", 0, autoexec_text, ICON_NONE);
-}
-
-static void WM_OT_open_mainfile(wmOperatorType *ot)
-{
-       ot->name = "Open Blender File";
-       ot->idname = "WM_OT_open_mainfile";
-       ot->description = "Open a Blender file";
-
-       ot->invoke = wm_open_mainfile_invoke;
-       ot->exec = wm_open_mainfile_exec;
-       ot->check = wm_open_mainfile_check;
-       ot->ui = wm_open_mainfile_ui;
-       /* omit window poll so this can work in background mode */
-
-       WM_operator_properties_filesel(
-               ot, FILE_TYPE_FOLDER | FILE_TYPE_BLENDER, FILE_BLENDER, FILE_OPENFILE,
-               WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
-
-       RNA_def_boolean(ot->srna, "load_ui", true, "Load UI", "Load user interface setup in the .blend file");
-       RNA_def_boolean(ot->srna, "use_scripts", true, "Trusted Source",
-                       "Allow .blend file to execute scripts automatically, default available from system preferences");
-}
-
-
-/* *************** revert file **************** */
-
-static int wm_revert_mainfile_exec(bContext *C, wmOperator *op)
-{
-       bool success;
-
-       wm_open_init_use_scripts(op, false);
-
-       if (RNA_boolean_get(op->ptr, "use_scripts"))
-               G.f |= G_SCRIPT_AUTOEXEC;
-       else
-               G.f &= ~G_SCRIPT_AUTOEXEC;
-
-       success = wm_file_read_opwrap(C, G.main->name, op->reports, !(G.f & G_SCRIPT_AUTOEXEC));
-
-       if (success) {
-               return OPERATOR_FINISHED;
-       }
-       else {
-               return OPERATOR_CANCELLED;
-       }
-}
-
-static int wm_revert_mainfile_poll(bContext *UNUSED(C))
-{
-       return G.relbase_valid;
-}
-
-static void WM_OT_revert_mainfile(wmOperatorType *ot)
-{
-       ot->name = "Revert";
-       ot->idname = "WM_OT_revert_mainfile";
-       ot->description = "Reload the saved file";
-       ot->invoke = WM_operator_confirm;
-
-       RNA_def_boolean(ot->srna, "use_scripts", true, "Trusted Source",
-                       "Allow .blend file to execute scripts automatically, default available from system preferences");
-
-       ot->exec = wm_revert_mainfile_exec;
-       ot->poll = wm_revert_mainfile_poll;
-}
-
-/* **************** link/append *************** */
-
-static int wm_link_append_poll(bContext *C)
-{
-       if (WM_operator_winactive(C)) {
-               /* linking changes active object which is pretty useful in general,
-                * but which totally confuses edit mode (i.e. it becoming not so obvious
-                * to leave from edit mode and invalid tools in toolbar might be displayed)
-                * so disable link/append when in edit mode (sergey) */
-               if (CTX_data_edit_object(C))
-                       return 0;
-
-               return 1;
-       }
-
-       return 0;
-}
-
-static int wm_link_append_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
-{
-       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
-               return WM_operator_call_notest(C, op);
-       }
-       else {
-               /* XXX TODO solve where to get last linked library from */
-               if (G.lib[0] != '\0') {
-                       RNA_string_set(op->ptr, "filepath", G.lib);
-               }
-               else if (G.relbase_valid) {
-                       char path[FILE_MAX];
-                       BLI_strncpy(path, G.main->name, sizeof(G.main->name));
-                       BLI_parent_dir(path);
-                       RNA_string_set(op->ptr, "filepath", path);
-               }
-               WM_event_add_fileselect(C, op);
-               return OPERATOR_RUNNING_MODAL;
-       }
-}
-
-static short wm_link_append_flag(wmOperator *op)
-{
-       PropertyRNA *prop;
-       short flag = 0;
-
-       if (RNA_boolean_get(op->ptr, "autoselect"))
-               flag |= FILE_AUTOSELECT;
-       if (RNA_boolean_get(op->ptr, "active_layer"))
-               flag |= FILE_ACTIVELAY;
-       if ((prop = RNA_struct_find_property(op->ptr, "relative_path")) && RNA_property_boolean_get(op->ptr, prop))
-               flag |= FILE_RELPATH;
-       if (RNA_boolean_get(op->ptr, "link"))
-               flag |= FILE_LINK;
-       if (RNA_boolean_get(op->ptr, "instance_groups"))
-               flag |= FILE_GROUP_INSTANCE;
-
-       return flag;
-}
-
-typedef struct WMLinkAppendDataItem {
-       char *name;
-       BLI_bitmap *libraries;  /* All libs (from WMLinkAppendData.libraries) to try to load this ID from. */
-       short idcode;
-
-       ID *new_id;
-       void *customdata;
-} WMLinkAppendDataItem;
-
-typedef struct WMLinkAppendData {
-       LinkNodePair libraries;
-       LinkNodePair items;
-       int num_libraries;
-       int num_items;
-       short flag;
-
-       /* Internal 'private' data */
-       MemArena *memarena;
-} WMLinkAppendData;
-
-static WMLinkAppendData *wm_link_append_data_new(const int flag)
-{
-       MemArena *ma = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
-       WMLinkAppendData *lapp_data = BLI_memarena_calloc(ma, sizeof(*lapp_data));
-
-       lapp_data->flag = flag;
-       lapp_data->memarena = ma;
-
-       return lapp_data;
-}
-
-static void wm_link_append_data_free(WMLinkAppendData *lapp_data)
-{
-       BLI_memarena_free(lapp_data->memarena);
-}
-
-/* WARNING! *Never* call wm_link_append_data_library_add() after having added some items! */
-
-static void wm_link_append_data_library_add(WMLinkAppendData *lapp_data, const char *libname)
-{
-       size_t len = strlen(libname) + 1;
-       char *libpath = BLI_memarena_alloc(lapp_data->memarena, len);
-
-       BLI_strncpy(libpath, libname, len);
-       BLI_linklist_append_arena(&lapp_data->libraries, libpath, lapp_data->memarena);
-       lapp_data->num_libraries++;
-}
-
-static WMLinkAppendDataItem *wm_link_append_data_item_add(
-        WMLinkAppendData *lapp_data, const char *idname, const short idcode, void *customdata)
-{
-       WMLinkAppendDataItem *item = BLI_memarena_alloc(lapp_data->memarena, sizeof(*item));
-       size_t len = strlen(idname) + 1;
-
-       item->name = BLI_memarena_alloc(lapp_data->memarena, len);
-       BLI_strncpy(item->name, idname, len);
-       item->idcode = idcode;
-       item->libraries = BLI_BITMAP_NEW_MEMARENA(lapp_data->memarena, lapp_data->num_libraries);
-
-       item->new_id = NULL;
-       item->customdata = customdata;
-
-       BLI_linklist_append_arena(&lapp_data->items, item, lapp_data->memarena);
-       lapp_data->num_items++;
-
-       return item;
-}
-
-static void wm_link_do(
-        WMLinkAppendData *lapp_data, ReportList *reports, Main *bmain, Scene *scene, View3D *v3d)
-{
-       Main *mainl;
-       BlendHandle *bh;
-       Library *lib;
-
-       const int flag = lapp_data->flag;
-
-       LinkNode *liblink, *itemlink;
-       int lib_idx, item_idx;
-
-       BLI_assert(lapp_data->num_items && lapp_data->num_libraries);
-
-       for (lib_idx = 0, liblink = lapp_data->libraries.list; liblink; lib_idx++, liblink = liblink->next) {
-               char *libname = liblink->link;
-
-               bh = BLO_blendhandle_from_file(libname, reports);
-
-               if (bh == NULL) {
-                       /* Unlikely since we just browsed it, but possible
-                        * Error reports will have been made by BLO_blendhandle_from_file() */
-                       continue;
-               }
-
-               /* here appending/linking starts */
-               mainl = BLO_library_link_begin(bmain, &bh, libname);
-               lib = mainl->curlib;
-               BLI_assert(lib);
-               UNUSED_VARS_NDEBUG(lib);
-
-               if (mainl->versionfile < 250) {
-                       BKE_reportf(reports, RPT_WARNING,
-                                   "Linking or appending from a very old .blend file format (%d.%d), no animation conversion will "
-                                   "be done! You may want to re-save your lib file with current Blender",
-                                   mainl->versionfile, mainl->subversionfile);
-               }
-
-               /* For each lib file, we try to link all items belonging to that lib,
-                * and tag those successful to not try to load them again with the other libs. */
-               for (item_idx = 0, itemlink = lapp_data->items.list; itemlink; item_idx++, itemlink = itemlink->next) {
-                       WMLinkAppendDataItem *item = itemlink->link;
-                       ID *new_id;
-
-                       if (!BLI_BITMAP_TEST(item->libraries, lib_idx)) {
-                               continue;
-                       }
-
-                       new_id = BLO_library_link_named_part_ex(mainl, &bh, item->idcode, item->name, flag, scene, v3d);
-                       if (new_id) {
-                               /* If the link is sucessful, clear item's libs 'todo' flags.
-                                * This avoids trying to link same item with other libraries to come. */
-                               BLI_BITMAP_SET_ALL(item->libraries, false, lapp_data->num_libraries);
-                               item->new_id = new_id;
-                       }
-               }
-
-               BLO_library_link_end(mainl, &bh, flag, scene, v3d);
-               BLO_blendhandle_close(bh);
-       }
-}
-
-static int wm_link_append_exec(bContext *C, wmOperator *op)
-{
-       Main *bmain = CTX_data_main(C);
-       Scene *scene = CTX_data_scene(C);
-       PropertyRNA *prop;
-       WMLinkAppendData *lapp_data;
-       char path[FILE_MAX_LIBEXTRA], root[FILE_MAXDIR], libname[FILE_MAX], relname[FILE_MAX];
-       char *group, *name;
-       int totfiles = 0;
-       short flag;
-
-       RNA_string_get(op->ptr, "filename", relname);
-       RNA_string_get(op->ptr, "directory", root);
-
-       BLI_join_dirfile(path, sizeof(path), root, relname);
-
-       /* test if we have a valid data */
-       if (!BLO_library_path_explode(path, libname, &group, &name)) {
-               BKE_reportf(op->reports, RPT_ERROR, "'%s': not a library", path);
-               return OPERATOR_CANCELLED;
-       }
-       else if (!group) {
-               BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", path);
-               return OPERATOR_CANCELLED;
-       }
-       else if (BLI_path_cmp(bmain->name, libname) == 0) {
-               BKE_reportf(op->reports, RPT_ERROR, "'%s': cannot use current file as library", path);
-               return OPERATOR_CANCELLED;
-       }
-
-       /* check if something is indicated for append/link */
-       prop = RNA_struct_find_property(op->ptr, "files");
-       if (prop) {
-               totfiles = RNA_property_collection_length(op->ptr, prop);
-               if (totfiles == 0) {
-                       if (!name) {
-                               BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", path);
-                               return OPERATOR_CANCELLED;
-                       }
-               }
-       }
-       else if (!name) {
-               BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", path);
-               return OPERATOR_CANCELLED;
-       }
-
-       flag = wm_link_append_flag(op);
-
-       /* sanity checks for flag */
-       if (scene && scene->id.lib) {
-               BKE_reportf(op->reports, RPT_WARNING,
-                           "Scene '%s' is linked, instantiation of objects & groups is disabled", scene->id.name + 2);
-               flag &= ~FILE_GROUP_INSTANCE;
-               scene = NULL;
-       }
-
-       /* from here down, no error returns */
-
-       if (scene && RNA_boolean_get(op->ptr, "autoselect")) {
-               BKE_scene_base_deselect_all(scene);
-       }
-       
-       /* tag everything, all untagged data can be made local
-        * its also generally useful to know what is new
-        *
-        * take extra care BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false) is called after! */
-       BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);
-
-       /* We define our working data...
-        * Note that here, each item 'uses' one library, and only one. */
-       lapp_data = wm_link_append_data_new(flag);
-       if (totfiles != 0) {
-               GHash *libraries = BLI_ghash_new(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, __func__);
-               int lib_idx = 0;
-
-               RNA_BEGIN (op->ptr, itemptr, "files")
-               {
-                       RNA_string_get(&itemptr, "name", relname);
-
-                       BLI_join_dirfile(path, sizeof(path), root, relname);
-
-                       if (BLO_library_path_explode(path, libname, &group, &name)) {
-                               if (!group || !name) {
-                                       continue;
-                               }
-
-                               if (!BLI_ghash_haskey(libraries, libname)) {
-                                       BLI_ghash_insert(libraries, BLI_strdup(libname), SET_INT_IN_POINTER(lib_idx));
-                                       lib_idx++;
-                                       wm_link_append_data_library_add(lapp_data, libname);
-                               }
-                       }
-               }
-               RNA_END;
-
-               RNA_BEGIN (op->ptr, itemptr, "files")
-               {
-                       RNA_string_get(&itemptr, "name", relname);
-
-                       BLI_join_dirfile(path, sizeof(path), root, relname);
-
-                       if (BLO_library_path_explode(path, libname, &group, &name)) {
-                               WMLinkAppendDataItem *item;
-                               if (!group || !name) {
-                                       printf("skipping %s\n", path);
-                                       continue;
-                               }
-
-                               lib_idx = GET_INT_FROM_POINTER(BLI_ghash_lookup(libraries, libname));
-
-                               item = wm_link_append_data_item_add(lapp_data, name, BKE_idcode_from_name(group), NULL);
-                               BLI_BITMAP_ENABLE(item->libraries, lib_idx);
-                       }
-               }
-               RNA_END;
-
-               BLI_ghash_free(libraries, MEM_freeN, NULL);
-       }
-       else {
-               WMLinkAppendDataItem *item;
-
-               wm_link_append_data_library_add(lapp_data, libname);
-               item = wm_link_append_data_item_add(lapp_data, name, BKE_idcode_from_name(group), NULL);
-               BLI_BITMAP_ENABLE(item->libraries, 0);
-       }
-
-       /* XXX We'd need re-entrant locking on Main for this to work... */
-       /* BKE_main_lock(bmain); */
-
-       wm_link_do(lapp_data, op->reports, bmain, scene, CTX_wm_view3d(C));
-
-       /* BKE_main_unlock(bmain); */
-
-       wm_link_append_data_free(lapp_data);
-
-       /* mark all library linked objects to be updated */
-       BKE_main_lib_objects_recalc_all(bmain);
-       IMB_colormanagement_check_file_config(bmain);
-
-       /* append, rather than linking */
-       if ((flag & FILE_LINK) == 0) {
-               bool set_fake = RNA_boolean_get(op->ptr, "set_fake");
-               BKE_library_make_local(bmain, NULL, true, set_fake);
-       }
-
-       /* important we unset, otherwise these object wont
-        * link into other scenes from this blend file */
-       BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);
-
-       /* recreate dependency graph to include new objects */
-       DAG_scene_relations_rebuild(bmain, scene);
-       
-       /* free gpu materials, some materials depend on existing objects, such as lamps so freeing correctly refreshes */
-       GPU_materials_free();
-
-       /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
-       BLI_strncpy(G.lib, root, FILE_MAX);
-
-       WM_event_add_notifier(C, NC_WINDOW, NULL);
-
-       return OPERATOR_FINISHED;
-}
-
-static void wm_link_append_properties_common(wmOperatorType *ot, bool is_link)
-{
-       PropertyRNA *prop;
-
-       /* better not save _any_ settings for this operator */
-       /* properties */
-       prop = RNA_def_boolean(ot->srna, "link", is_link,
-                              "Link", "Link the objects or datablocks rather than appending");
-       RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
-       prop = RNA_def_boolean(ot->srna, "autoselect", true,
-                              "Select", "Select new objects");
-       RNA_def_property_flag(prop, PROP_SKIP_SAVE);
-       prop = RNA_def_boolean(ot->srna, "active_layer", true,
-                              "Active Layer", "Put new objects on the active layer");
-       RNA_def_property_flag(prop, PROP_SKIP_SAVE);
-       prop = RNA_def_boolean(ot->srna, "instance_groups", is_link,
-                              "Instance Groups", "Create Dupli-Group instances for each group");
-       RNA_def_property_flag(prop, PROP_SKIP_SAVE);
-}
-
-static void WM_OT_link(wmOperatorType *ot)
-{
-       ot->name = "Link from Library";
-       ot->idname = "WM_OT_link";
-       ot->description = "Link from a Library .blend file";
-       
-       ot->invoke = wm_link_append_invoke;
-       ot->exec = wm_link_append_exec;
-       ot->poll = wm_link_append_poll;
-       
-       ot->flag |= OPTYPE_UNDO;
-
-       WM_operator_properties_filesel(
-               ot, FILE_TYPE_FOLDER | FILE_TYPE_BLENDER | FILE_TYPE_BLENDERLIB, FILE_LOADLIB, FILE_OPENFILE,
-               WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME | WM_FILESEL_RELPATH | WM_FILESEL_FILES,
-               FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
-       
-       wm_link_append_properties_common(ot, true);
-}
-
-static void WM_OT_append(wmOperatorType *ot)
-{
-       ot->name = "Append from Library";
-       ot->idname = "WM_OT_append";
-       ot->description = "Append from a Library .blend file";
-
-       ot->invoke = wm_link_append_invoke;
-       ot->exec = wm_link_append_exec;
-       ot->poll = wm_link_append_poll;
-
-       ot->flag |= OPTYPE_UNDO;
-
-       WM_operator_properties_filesel(
-               ot, FILE_TYPE_FOLDER | FILE_TYPE_BLENDER | FILE_TYPE_BLENDERLIB, FILE_LOADLIB, FILE_OPENFILE,
-               WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME | WM_FILESEL_FILES,
-               FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
-
-       wm_link_append_properties_common(ot, false);
-       RNA_def_boolean(ot->srna, "set_fake", false, "Fake User", "Set Fake User for appended items (except Objects and Groups)");
-}
-
-/* *************** recover last session **************** */
-
-void WM_recover_last_session(bContext *C, ReportList *reports)
-{
-       char filepath[FILE_MAX];
-       
-       BLI_make_file_string("/", filepath, BKE_tempdir_base(), BLENDER_QUIT_FILE);
-       /* if reports==NULL, it's called directly without operator, we add a quick check here */
-       if (reports || BLI_exists(filepath)) {
-               G.fileflags |= G_FILE_RECOVER;
-               
-               wm_file_read_opwrap(C, filepath, reports, true);
-       
-               G.fileflags &= ~G_FILE_RECOVER;
-               
-               /* XXX bad global... fixme */
-               if (G.main->name[0])
-                       G.file_loaded = 1;      /* prevents splash to show */
-               else {
-                       G.relbase_valid = 0;
-                       G.save_over = 0;    /* start with save preference untitled.blend */
-               }
-
-       }
-}
-
-static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
-{
-       WM_recover_last_session(C, op->reports);
-       return OPERATOR_FINISHED;
-}
-
-static void WM_OT_recover_last_session(wmOperatorType *ot)
-{
-       ot->name = "Recover Last Session";
-       ot->idname = "WM_OT_recover_last_session";
-       ot->description = "Open the last closed file (\"" BLENDER_QUIT_FILE "\")";
-       ot->invoke = WM_operator_confirm;
-       
-       ot->exec = wm_recover_last_session_exec;
-}
-
-/* *************** recover auto save **************** */
-
-static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
-{
-       char filepath[FILE_MAX];
-       bool success;
-
-       RNA_string_get(op->ptr, "filepath", filepath);
-
-       G.fileflags |= G_FILE_RECOVER;
-
-       success = wm_file_read_opwrap(C, filepath, op->reports, true);
-
-       G.fileflags &= ~G_FILE_RECOVER;
-       
-       if (success) {
-               return OPERATOR_FINISHED;
-       }
-       else {
-               return OPERATOR_CANCELLED;
-       }
-}
-
-static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
-{
-       char filename[FILE_MAX];
-
-       wm_autosave_location(filename);
-       RNA_string_set(op->ptr, "filepath", filename);
-       WM_event_add_fileselect(C, op);
-
-       return OPERATOR_RUNNING_MODAL;
-}
-
-static void WM_OT_recover_auto_save(wmOperatorType *ot)
-{
-       ot->name = "Recover Auto Save";
-       ot->idname = "WM_OT_recover_auto_save";
-       ot->description = "Open an automatically saved file to recover it";
-       
-       ot->exec = wm_recover_auto_save_exec;
-       ot->invoke = wm_recover_auto_save_invoke;
-
-       WM_operator_properties_filesel(
-               ot, FILE_TYPE_BLENDER, FILE_BLENDER, FILE_OPENFILE,
-               WM_FILESEL_FILEPATH, FILE_LONGDISPLAY, FILE_SORT_TIME);
-}
-
-/* *************** save file as **************** */
-
-static void wm_filepath_default(char *filepath)
-{
-       if (G.save_over == false) {
-               BLI_ensure_filename(filepath, FILE_MAX, "untitled.blend");
-       }
-}
-
-static void save_set_compress(wmOperator *op)
-{
-       PropertyRNA *prop;
-
-       prop = RNA_struct_find_property(op->ptr, "compress");
-       if (!RNA_property_is_set(op->ptr, prop)) {
-               if (G.save_over) {  /* keep flag for existing file */
-                       RNA_property_boolean_set(op->ptr, prop, (G.fileflags & G_FILE_COMPRESS) != 0);
-               }
-               else {  /* use userdef for new file */
-                       RNA_property_boolean_set(op->ptr, prop, (U.flag & USER_FILECOMPRESS) != 0);
-               }
-       }
-}
-
-static void save_set_filepath(wmOperator *op)
-{
-       PropertyRNA *prop;
-       char name[FILE_MAX];
-
-       prop = RNA_struct_find_property(op->ptr, "filepath");
-       if (!RNA_property_is_set(op->ptr, prop)) {
-               /* if not saved before, get the name of the most recently used .blend file */
-               if (G.main->name[0] == 0 && G.recent_files.first) {
-                       struct RecentFile *recent = G.recent_files.first;
-                       BLI_strncpy(name, recent->filepath, FILE_MAX);
-               }
-               else {
-                       BLI_strncpy(name, G.main->name, FILE_MAX);
-               }
-
-               wm_filepath_default(name);
-               RNA_property_string_set(op->ptr, prop, name);
-       }
-}
-
-static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
-{
-
-       save_set_compress(op);
-       save_set_filepath(op);
-       
-       WM_event_add_fileselect(C, op);
-
-       return OPERATOR_RUNNING_MODAL;
-}
-
-/* function used for WM_OT_save_mainfile too */
-static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
-{
-       char path[FILE_MAX];
-       int fileflags;
-
-       save_set_compress(op);
-       
-       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
-               RNA_string_get(op->ptr, "filepath", path);
-       }
-       else {
-               BLI_strncpy(path, G.main->name, FILE_MAX);
-               wm_filepath_default(path);
-       }
-       
-       fileflags = G.fileflags & ~G_FILE_USERPREFS;
-
-       /* set compression flag */
-       BKE_BIT_TEST_SET(fileflags, RNA_boolean_get(op->ptr, "compress"),
-                        G_FILE_COMPRESS);
-       BKE_BIT_TEST_SET(fileflags, RNA_boolean_get(op->ptr, "relative_remap"),
-                        G_FILE_RELATIVE_REMAP);
-       BKE_BIT_TEST_SET(fileflags,
-                        (RNA_struct_property_is_set(op->ptr, "copy") &&
-                         RNA_boolean_get(op->ptr, "copy")),
-                        G_FILE_SAVE_COPY);
-
-#ifdef USE_BMESH_SAVE_AS_COMPAT
-       BKE_BIT_TEST_SET(fileflags,
-                        (RNA_struct_find_property(op->ptr, "use_mesh_compat") &&
-                         RNA_boolean_get(op->ptr, "use_mesh_compat")),
-                        G_FILE_MESH_COMPAT);
-#else
-#  error "don't remove by accident"
-#endif
-
-       if (wm_file_write(C, path, fileflags, op->reports) != 0)
-               return OPERATOR_CANCELLED;
-
-       WM_event_add_notifier(C, NC_WM | ND_FILESAVE, NULL);
-
-       return OPERATOR_FINISHED;
-}
-
-/* function used for WM_OT_save_mainfile too */
-static bool blend_save_check(bContext *UNUSED(C), wmOperator *op)
-{
-       char filepath[FILE_MAX];
-       RNA_string_get(op->ptr, "filepath", filepath);
-       if (!BLO_has_bfile_extension(filepath)) {
-               /* some users would prefer BLI_replace_extension(),
-                * we keep getting nitpicking bug reports about this - campbell */
-               BLI_ensure_extension(filepath, FILE_MAX, ".blend");
-               RNA_string_set(op->ptr, "filepath", filepath);
-               return true;
-       }
-       return false;
-}
-
-static void WM_OT_save_as_mainfile(wmOperatorType *ot)
-{
-       PropertyRNA *prop;
-
-       ot->name = "Save As Blender File";
-       ot->idname = "WM_OT_save_as_mainfile";
-       ot->description = "Save the current file in the desired location";
-       
-       ot->invoke = wm_save_as_mainfile_invoke;
-       ot->exec = wm_save_as_mainfile_exec;
-       ot->check = blend_save_check;
-       /* omit window poll so this can work in background mode */
-
-       WM_operator_properties_filesel(
-               ot, FILE_TYPE_FOLDER | FILE_TYPE_BLENDER, FILE_BLENDER, FILE_SAVE,
-               WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
-       RNA_def_boolean(ot->srna, "compress", false, "Compress", "Write compressed .blend file");
-       RNA_def_boolean(ot->srna, "relative_remap", true, "Remap Relative",
-                       "Remap relative paths when saving in a different directory");
-       prop = RNA_def_boolean(ot->srna, "copy", false, "Save Copy",
-                       "Save a copy of the actual working state but does not make saved file active");
-       RNA_def_property_flag(prop, PROP_SKIP_SAVE);
-#ifdef USE_BMESH_SAVE_AS_COMPAT
-       RNA_def_boolean(ot->srna, "use_mesh_compat", false, "Legacy Mesh Format",
-                       "Save using legacy mesh format (no ngons) - WARNING: only saves tris and quads, other ngons will "
-                       "be lost (no implicit triangulation)");
-#endif
-}
-
-/* *************** save file directly ******** */
-
-static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
-{
-       int ret;
-       
-       /* cancel if no active window */
-       if (CTX_wm_window(C) == NULL)
-               return OPERATOR_CANCELLED;
-
-       save_set_compress(op);
-       save_set_filepath(op);
-
-       /* if we're saving for the first time and prefer relative paths - any existing paths will be absolute,
-        * enable the option to remap paths to avoid confusion [#37240] */
-       if ((G.relbase_valid == false) && (U.flag & USER_RELPATHS)) {
-               PropertyRNA *prop = RNA_struct_find_property(op->ptr, "relative_remap");
-               if (!RNA_property_is_set(op->ptr, prop)) {
-                       RNA_property_boolean_set(op->ptr, prop, true);
-               }
-       }
-
-       if (G.save_over) {
-               char path[FILE_MAX];
-
-               RNA_string_get(op->ptr, "filepath", path);
-               if (BLI_exists(path)) {
-                       ret = WM_operator_confirm_message_ex(C, op, IFACE_("Save Over?"), ICON_QUESTION, path);
-               }
-               else {
-                       ret = wm_save_as_mainfile_exec(C, op);
-               }
-       }
-       else {
-               WM_event_add_fileselect(C, op);
-               ret = OPERATOR_RUNNING_MODAL;
-       }
-       
-       return ret;
-}
-
-static void WM_OT_save_mainfile(wmOperatorType *ot)
-{
-       ot->name = "Save Blender File";
-       ot->idname = "WM_OT_save_mainfile";
-       ot->description = "Save the current Blender file";
-       
-       ot->invoke = wm_save_mainfile_invoke;
-       ot->exec = wm_save_as_mainfile_exec;
-       ot->check = blend_save_check;
-       /* omit window poll so this can work in background mode */
-       
-       WM_operator_properties_filesel(
-               ot, FILE_TYPE_FOLDER | FILE_TYPE_BLENDER, FILE_BLENDER, FILE_SAVE,
-               WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
-       RNA_def_boolean(ot->srna, "compress", false, "Compress", "Write compressed .blend file");
-       RNA_def_boolean(ot->srna, "relative_remap", false, "Remap Relative",
-                       "Remap relative paths when saving in a different directory");
-}
-
 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
 {
        ot->name = "Toggle Window Fullscreen";
@@ -3307,6 +2299,11 @@ int WM_border_select_modal(bContext *C, wmOperator *op, const wmEvent *event)
                }
 
        }
+#ifdef WITH_INPUT_NDOF
+       else if (event->type == NDOF_MOTION) {
+               return OPERATOR_PASS_THROUGH;
+       }
+#endif
 //     /* Allow view navigation??? */
 //     else {
 //             return OPERATOR_PASS_THROUGH;
@@ -3421,6 +2418,11 @@ int WM_gesture_circle_modal(bContext *C, wmOperator *op, const wmEvent *event)
                                return OPERATOR_FINISHED; /* use finish or we don't get an undo */
                }
        }
+#ifdef WITH_INPUT_NDOF
+       else if (event->type == NDOF_MOTION) {
+               return OPERATOR_PASS_THROUGH;
+       }
+#endif
        /* Allow view navigation??? */
        /* note, this gives issues: 1) other modal ops run on top (border select), 2) middlemouse is used now 3) tablet/trackpad? */
 //     else {
@@ -3870,10 +2872,9 @@ void WM_OT_straightline_gesture(wmOperatorType *ot)
 
 /* *********************** radial control ****************** */
 
-#define WM_RADIAL_CONTROL_DISPLAY_SIZE (200 * U.pixelsize)
-#define WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE (35 * U.pixelsize)
+#define WM_RADIAL_CONTROL_DISPLAY_SIZE (200 * UI_DPI_FAC)
+#define WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE (35 * UI_DPI_FAC)
 #define WM_RADIAL_CONTROL_DISPLAY_WIDTH (WM_RADIAL_CONTROL_DISPLAY_SIZE - WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE)
-#define WM_RADIAL_CONTROL_HEADER_LENGTH 180
 #define WM_RADIAL_MAX_STR 10
 
 typedef struct {
@@ -3899,7 +2900,7 @@ typedef struct {
 static void radial_control_update_header(wmOperator *op, bContext *C)
 {
        RadialControl *rc = op->customdata;
-       char msg[WM_RADIAL_CONTROL_HEADER_LENGTH];
+       char msg[UI_MAX_DRAW_STR];
        ScrArea *sa = CTX_wm_area(C);
        Scene *scene = CTX_data_scene(C);
        
@@ -3907,34 +2908,33 @@ static void radial_control_update_header(wmOperator *op, bContext *C)
                if (hasNumInput(&rc->num_input)) {
                        char num_str[NUM_STR_REP_LEN];
                        outputNumInput(&rc->num_input, num_str, &scene->unit);
-                       BLI_snprintf(msg, WM_RADIAL_CONTROL_HEADER_LENGTH, "%s: %s", RNA_property_ui_name(rc->prop), num_str);
-                       ED_area_headerprint(sa, msg);
+                       BLI_snprintf(msg, sizeof(msg), "%s: %s", RNA_property_ui_name(rc->prop), num_str);
                }
                else {
                        const char *ui_name = RNA_property_ui_name(rc->prop);
                        switch (rc->subtype) {
                                case PROP_NONE:
                                case PROP_DISTANCE:
-                                       BLI_snprintf(msg, WM_RADIAL_CONTROL_HEADER_LENGTH, "%s: %0.4f", ui_name, rc->current_value);
+                                       BLI_snprintf(msg, sizeof(msg), "%s: %0.4f", ui_name, rc->current_value);
                                        break;
                                case PROP_PIXEL:
-                                       BLI_snprintf(msg, WM_RADIAL_CONTROL_HEADER_LENGTH, "%s: %d", ui_name, (int)rc->current_value); /* XXX: round to nearest? */
+                                       BLI_snprintf(msg, sizeof(msg), "%s: %d", ui_name, (int)rc->current_value); /* XXX: round to nearest? */
                                        break;
                                case PROP_PERCENTAGE:
-                                       BLI_snprintf(msg, WM_RADIAL_CONTROL_HEADER_LENGTH, "%s: %3.1f%%", ui_name, rc->current_value);
+                                       BLI_snprintf(msg, sizeof(msg), "%s: %3.1f%%", ui_name, rc->current_value);
                                        break;
                                case PROP_FACTOR:
-                                       BLI_snprintf(msg, WM_RADIAL_CONTROL_HEADER_LENGTH, "%s: %1.3f", ui_name, rc->current_value);
+                                       BLI_snprintf(msg, sizeof(msg), "%s: %1.3f", ui_name, rc->current_value);
                                        break;
                                case PROP_ANGLE:
-                                       BLI_snprintf(msg, WM_RADIAL_CONTROL_HEADER_LENGTH, "%s: %3.2f", ui_name, RAD2DEGF(rc->current_value));
+                                       BLI_snprintf(msg, sizeof(msg), "%s: %3.2f", ui_name, RAD2DEGF(rc->current_value));
                                        break;
                                default:
-                                       BLI_snprintf(msg, WM_RADIAL_CONTROL_HEADER_LENGTH, "%s", ui_name); /* XXX: No value? */
+                                       BLI_snprintf(msg, sizeof(msg), "%s", ui_name); /* XXX: No value? */
                                        break;
                        }
-                       ED_area_headerprint(sa, msg);
                }
+               ED_area_headerprint(sa, msg);
        }
 }
 
@@ -3950,7 +2950,7 @@ static void radial_control_set_initial_mouse(RadialControl *rc, const wmEvent *e
                case PROP_NONE:
                case PROP_DISTANCE:
                case PROP_PIXEL:
-                       d[0] = rc->initial_value * U.pixelsize;
+                       d[0] = rc->initial_value;
                        break;
                case PROP_PERCENTAGE:
                        d[0] = (rc->initial_value) / 100.0f * WM_RADIAL_CONTROL_DISPLAY_WIDTH + WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
@@ -4076,8 +3076,8 @@ static void radial_control_paint_cursor(bContext *C, int x, int y, void *customd
                case PROP_NONE:
                case PROP_DISTANCE:
                case PROP_PIXEL:
-                       r1 = rc->current_value * U.pixelsize;
-                       r2 = rc->initial_value * U.pixelsize;
+                       r1 = rc->current_value;
+                       r2 = rc->initial_value;
                        tex_radius = r1;
                        alpha = 0.75;
                        break;
@@ -4150,9 +3150,9 @@ static void radial_control_paint_cursor(bContext *C, int x, int y, void *customd
        if (rmin > 0.0f)
                glutil_draw_lined_arc(0.0, (float)(M_PI * 2.0), rmin, 40);
 
-       BLF_size(fontid, 1.5 * fstyle_points, 1.0f / U.dpi);
+       BLF_size(fontid, 1.5 * fstyle_points * U.pixelsize, U.dpi);
        BLF_enable(fontid, BLF_SHADOW);
-       BLF_shadow(fontid, 3, 0.0f, 0.0f, 0.0f, 0.5f);
+       BLF_shadow(fontid, 3, (const float[4]){0.0f, 0.0f, 0.0f, 0.5f});
        BLF_shadow_offset(fontid, 1, -1);
 
        /* draw value */
@@ -4559,7 +3559,6 @@ static int radial_control_modal(bContext *C, wmOperator *op, const wmEvent *even
                                                case PROP_PIXEL:
                                                        new_value = dist;
                                                        if (snap) new_value = ((int)new_value + 5) / 10 * 10;
-                                                       new_value /= U.pixelsize;
                                                        break;
                                                case PROP_PERCENTAGE:
                                                        new_value = ((dist - WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE) / WM_RADIAL_CONTROL_DISPLAY_WIDTH) * 100.0f;
@@ -4894,12 +3893,10 @@ static void WM_OT_dependency_relations(wmOperatorType *ot)
 
 /* *************************** Mat/tex/etc. previews generation ************* */
 
-typedef struct PreviewsIDEnsureStack {
+typedef struct PreviewsIDEnsureData {
        bContext *C;
        Scene *scene;
-
-       BLI_LINKSTACK_DECLARE(id_stack, ID *);
-} PreviewsIDEnsureStack;
+} PreviewsIDEnsureData;
 
 static void previews_id_ensure(bContext *C, Scene *scene, ID *id)
 {
@@ -4907,23 +3904,25 @@ static void previews_id_ensure(bContext *C, Scene *scene, ID *id)
 
        /* Only preview non-library datablocks, lib ones do not pertain to this .blend file!
         * Same goes for ID with no user. */
-       if (!id->lib && (id->us != 0)) {
+       if (!ID_IS_LINKED_DATABLOCK(id) && (id->us != 0)) {
                UI_id_icon_render(C, scene, id, false, false);
                UI_id_icon_render(C, scene, id, true, false);
        }
 }
 
-static int previews_id_ensure_callback(void *todo_v, ID *UNUSED(idself), ID **idptr, int UNUSED(cd_flag))
+static int previews_id_ensure_callback(void *userdata, ID *UNUSED(self_id), ID **idptr, int cd_flag)
 {
-       PreviewsIDEnsureStack *todo = todo_v;
+       if (cd_flag & IDWALK_CB_PRIVATE) {
+               return IDWALK_RET_NOP;
+       }
+
+       PreviewsIDEnsureData *data = userdata;
        ID *id = *idptr;
 
        if (id && (id->tag & LIB_TAG_DOIT)) {
-               if (ELEM(GS(id->name), ID_MA, ID_TE, ID_IM, ID_WO, ID_LA)) {
-                       previews_id_ensure(todo->C, todo->scene, id);
-               }
-               id->tag &= ~LIB_TAG_DOIT;  /* Tag the ID as done in any case. */
-               BLI_LINKSTACK_PUSH(todo->id_stack, id);
+               BLI_assert(ELEM(GS(id->name), ID_MA, ID_TE, ID_IM, ID_WO, ID_LA));
+               previews_id_ensure(data->C, data->scene, id);
+               id->tag &= ~LIB_TAG_DOIT;
        }
 
        return IDWALK_RET_NOP;
@@ -4933,35 +3932,33 @@ static int previews_ensure_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Main *bmain = CTX_data_main(C);
        ListBase *lb[] = {&bmain->mat, &bmain->tex, &bmain->image, &bmain->world, &bmain->lamp, NULL};
-       PreviewsIDEnsureStack preview_id_stack;
+       PreviewsIDEnsureData preview_id_data;
        Scene *scene;
        ID *id;
        int i;
 
        /* We use LIB_TAG_DOIT to check whether we have already handled a given ID or not. */
-       BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, true);
-
-       BLI_LINKSTACK_INIT(preview_id_stack.id_stack);
+       BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
+       for (i = 0; lb[i]; i++) {
+               BKE_main_id_tag_listbase(lb[i], LIB_TAG_DOIT, true);
+       }
 
+       preview_id_data.C = C;
        for (scene = bmain->scene.first; scene; scene = scene->id.next) {
-               preview_id_stack.scene = scene;
-               preview_id_stack.C = C;
+               preview_id_data.scene = scene;
                id = (ID *)scene;
 
-               do {
-                       /* This will loop over all IDs linked by current one, render icons for them if needed,
-                        * and add them to 'todo' preview_id_stack. */
-                       BKE_library_foreach_ID_link(id, previews_id_ensure_callback, &preview_id_stack, IDWALK_READONLY);
-               } while ((id = BLI_LINKSTACK_POP(preview_id_stack.id_stack)));
+               BKE_library_foreach_ID_link(NULL, id, previews_id_ensure_callback, &preview_id_data, IDWALK_RECURSE);
        }
 
-       BLI_LINKSTACK_FREE(preview_id_stack.id_stack);
-
        /* Check a last time for ID not used (fake users only, in theory), and
         * do our best for those, using current scene... */
        for (i = 0; lb[i]; i++) {
                for (id = lb[i]->first; id; id = id->next) {
-                       previews_id_ensure(C, NULL, id);
+                       if (id->tag & LIB_TAG_DOIT) {
+                               previews_id_ensure(C, NULL, id);
+                               id->tag &= ~LIB_TAG_DOIT;
+                       }
                }
        }
 
@@ -4970,9 +3967,9 @@ static int previews_ensure_exec(bContext *C, wmOperator *UNUSED(op))
 
 static void WM_OT_previews_ensure(wmOperatorType *ot)
 {
-       ot->name = "Refresh DataBlock Previews";
+       ot->name = "Refresh Data-Block Previews";
        ot->idname = "WM_OT_previews_ensure";
-       ot->description = "Ensure datablock previews are available and up-to-date "
+       ot->description = "Ensure data-block previews are available and up-to-date "
                          "(to be saved in .blend file, only for some types like materials, textures, etc.)";
 
        ot->exec = previews_ensure_exec;
@@ -5029,9 +4026,9 @@ static int previews_clear_exec(bContext *C, wmOperator *op)
 
 static void WM_OT_previews_clear(wmOperatorType *ot)
 {
-       ot->name = "Clear DataBlock Previews";
+       ot->name = "Clear Data-Block Previews";
        ot->idname = "WM_OT_previews_clear";
-       ot->description = "Clear datablock previews (only for some types like objects, materials, textures, etc.)";
+       ot->description = "Clear data-block previews (only for some types like objects, materials, textures, etc.)";
 
        ot->exec = previews_clear_exec;
        ot->invoke = WM_menu_invoke;
@@ -5039,7 +4036,7 @@ static void WM_OT_previews_clear(wmOperatorType *ot)
        ot->prop = RNA_def_enum_flag(ot->srna, "id_type", preview_id_type_items,
                                     FILTER_ID_SCE | FILTER_ID_OB | FILTER_ID_GR |
                                     FILTER_ID_MA | FILTER_ID_LA | FILTER_ID_WO | FILTER_ID_TE | FILTER_ID_IM,
-                                    "DataBlock Type", "Which datablock previews to clear");
+                                    "Data-Block Type", "Which data-block previews to clear");
 }
 
 /* *************************** Doc from UI ************* */
@@ -5154,6 +4151,8 @@ void wm_operatortype_init(void)
        WM_operatortype_append(WM_OT_revert_mainfile);
        WM_operatortype_append(WM_OT_link);
        WM_operatortype_append(WM_OT_append);
+       WM_operatortype_append(WM_OT_lib_relocate);
+       WM_operatortype_append(WM_OT_lib_reload);
        WM_operatortype_append(WM_OT_recover_last_session);
        WM_operatortype_append(WM_OT_recover_auto_save);
        WM_operatortype_append(WM_OT_save_as_mainfile);
@@ -5233,7 +4232,8 @@ static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
        WM_modalkeymap_assign(keymap, "MASK_OT_select_circle");
        WM_modalkeymap_assign(keymap, "NODE_OT_select_circle");
        WM_modalkeymap_assign(keymap, "GPENCIL_OT_select_circle");
-       WM_modalkeymap_assign(keymap, "GRAPH_OT_select_circle");        
+       WM_modalkeymap_assign(keymap, "GRAPH_OT_select_circle");
+       WM_modalkeymap_assign(keymap, "ACTION_OT_select_circle");
 
 }
 
@@ -5330,6 +4330,7 @@ static void gesture_border_modal_keymap(wmKeyConfig *keyconf)
        WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_border");
        WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border"); /* XXX TODO: zoom border should perhaps map rightmouse to zoom out instead of in+cancel */
        WM_modalkeymap_assign(keymap, "IMAGE_OT_render_border");
+       WM_modalkeymap_assign(keymap, "IMAGE_OT_view_zoom_border");
        WM_modalkeymap_assign(keymap, "GPENCIL_OT_select_border");
 }
 
@@ -5364,6 +4365,7 @@ static void gesture_zoom_border_modal_keymap(wmKeyConfig *keyconf)
        /* assign map to operators */
        WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
        WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border");
+       WM_modalkeymap_assign(keymap, "IMAGE_OT_view_zoom_border");
 }
 
 /* default keymap for windows and screens, only call once per WM */
@@ -5371,7 +4373,6 @@ void wm_window_keymap(wmKeyConfig *keyconf)
 {
        wmKeyMap *keymap = WM_keymap_find(keyconf, "Window", 0, 0);
        wmKeyMapItem *kmi;
-       const char *data_path;
        
        /* note, this doesn't replace existing keymap items */
        WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL | KM_ALT, 0);
@@ -5409,7 +4410,9 @@ void wm_window_keymap(wmKeyConfig *keyconf)
 
        /* menus that can be accessed anywhere in blender */
        WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
+#ifdef WITH_INPUT_NDOF
        WM_keymap_add_menu(keymap, "USERPREF_MT_ndof_settings", NDOF_BUTTON_MENU, KM_PRESS, 0, 0);
+#endif
 
        /* Space switching */
        kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F2KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was DXF export */
@@ -5456,8 +4459,9 @@ void wm_window_keymap(wmKeyConfig *keyconf)
        RNA_string_set(kmi->ptr, "data_path", "area.type");
        RNA_string_set(kmi->ptr, "value", "DOPESHEET_EDITOR");
        
+#ifdef WITH_INPUT_NDOF
        /* ndof speed */
-       data_path = "user_preferences.inputs.ndof_sensitivity";
+       const char *data_path = "user_preferences.inputs.ndof_sensitivity";
        kmi = WM_keymap_add_item(keymap, "WM_OT_context_scale_float", NDOF_BUTTON_PLUS, KM_PRESS, 0, 0);
        RNA_string_set(kmi->ptr, "data_path", data_path);
        RNA_float_set(kmi->ptr, "value", 1.1f);
@@ -5473,9 +4477,7 @@ void wm_window_keymap(wmKeyConfig *keyconf)
        kmi = WM_keymap_add_item(keymap, "WM_OT_context_scale_float", NDOF_BUTTON_MINUS, KM_PRESS, KM_SHIFT, 0);
        RNA_string_set(kmi->ptr, "data_path", data_path);
        RNA_float_set(kmi->ptr, "value", 1.0f / 1.5f);
-       data_path = NULL;
-       (void)data_path;
-
+#endif /* WITH_INPUT_NDOF */
 
        gesture_circle_modal_keymap(keyconf);
        gesture_border_modal_keymap(keyconf);
@@ -5491,7 +4493,7 @@ static EnumPropertyItem *rna_id_itemf(bContext *UNUSED(C), PointerRNA *UNUSED(pt
        int i = 0;
 
        for (; id; id = id->next) {
-               if (local == false || id->lib == NULL) {
+               if (local == false || !ID_IS_LINKED_DATABLOCK(id)) {
                        item_tmp.identifier = item_tmp.name = id->name + 2;
                        item_tmp.value = i++;
                        RNA_enum_item_add(&item, &totitem, &item_tmp);
@@ -5560,4 +4562,3 @@ EnumPropertyItem *RNA_mask_local_itemf(bContext *C, PointerRNA *ptr, PropertyRNA
 {
        return rna_id_itemf(C, ptr, r_free, C ? (ID *)CTX_data_main(C)->mask.first : NULL, true);
 }
-