2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2009-06-07 13:36:12 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-06-07 13:36:12 +00:00
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2009 Blender Foundation.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
2011-02-27 20:29:51 +00:00
|
|
|
/** \file blender/editors/space_buttons/buttons_ops.c
|
|
|
|
|
* \ingroup spbuttons
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2009-06-07 13:36:12 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2010-06-25 12:04:04 +00:00
|
|
|
#include "DNA_userdef_types.h"
|
2009-06-07 13:36:12 +00:00
|
|
|
|
2013-03-04 18:36:37 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2010-07-04 10:02:30 +00:00
|
|
|
#include "BLI_fileops.h"
|
2010-09-20 21:11:38 +00:00
|
|
|
#include "BLI_path_util.h"
|
|
|
|
|
#include "BLI_string.h"
|
2010-07-04 10:02:30 +00:00
|
|
|
|
2013-02-15 14:30:36 +00:00
|
|
|
#include "BLF_translation.h"
|
|
|
|
|
|
2009-06-07 13:36:12 +00:00
|
|
|
#include "BKE_context.h"
|
2010-09-20 21:11:38 +00:00
|
|
|
#include "BKE_global.h"
|
2010-10-18 06:41:16 +00:00
|
|
|
#include "BKE_main.h"
|
2012-02-02 05:55:01 +00:00
|
|
|
#include "BKE_report.h"
|
2009-06-07 13:36:12 +00:00
|
|
|
|
|
|
|
|
#include "WM_api.h"
|
|
|
|
|
#include "WM_types.h"
|
|
|
|
|
|
2010-02-22 06:05:35 +00:00
|
|
|
#include "ED_screen.h"
|
2011-05-23 10:14:07 +00:00
|
|
|
#include "ED_util.h"
|
2010-02-22 06:05:35 +00:00
|
|
|
|
2009-07-14 20:27:28 +00:00
|
|
|
#include "RNA_access.h"
|
|
|
|
|
|
2009-07-23 20:40:51 +00:00
|
|
|
#include "UI_interface.h"
|
2011-02-27 18:03:19 +00:00
|
|
|
#include "UI_resources.h"
|
2009-07-23 20:40:51 +00:00
|
|
|
|
2012-05-08 15:30:00 +00:00
|
|
|
#include "buttons_intern.h" /* own include */
|
2009-06-07 13:36:12 +00:00
|
|
|
|
2009-07-23 20:40:51 +00:00
|
|
|
/********************** toolbox operator *********************/
|
|
|
|
|
|
2013-03-13 09:03:46 +00:00
|
|
|
static int toolbox_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
|
2009-07-23 20:40:51 +00:00
|
|
|
{
|
2012-05-08 15:30:00 +00:00
|
|
|
bScreen *sc = CTX_wm_screen(C);
|
|
|
|
|
SpaceButs *sbuts = CTX_wm_space_buts(C);
|
2009-07-23 20:40:51 +00:00
|
|
|
PointerRNA ptr;
|
|
|
|
|
uiPopupMenu *pup;
|
|
|
|
|
uiLayout *layout;
|
|
|
|
|
|
2009-08-18 12:58:51 +00:00
|
|
|
RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
|
2009-07-23 20:40:51 +00:00
|
|
|
|
2014-11-09 21:20:40 +01:00
|
|
|
pup = UI_popup_menu_begin(C, IFACE_("Align"), ICON_NONE);
|
|
|
|
|
layout = UI_popup_menu_layout(pup);
|
2009-07-23 20:40:51 +00:00
|
|
|
uiItemsEnumR(layout, &ptr, "align");
|
2014-11-09 21:20:40 +01:00
|
|
|
UI_popup_menu_end(C, pup);
|
2009-07-23 20:40:51 +00:00
|
|
|
|
2014-10-28 17:51:06 +01:00
|
|
|
return OPERATOR_INTERFACE;
|
2009-07-23 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BUTTONS_OT_toolbox(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->name = "Toolbox";
|
|
|
|
|
ot->description = "Display button panel toolbox";
|
|
|
|
|
ot->idname = "BUTTONS_OT_toolbox";
|
2009-07-23 20:40:51 +00:00
|
|
|
|
|
|
|
|
/* api callbacks */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->invoke = toolbox_invoke;
|
|
|
|
|
ot->poll = ED_operator_buttons_active;
|
2009-07-23 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-28 18:51:06 +00:00
|
|
|
/********************** filebrowse operator *********************/
|
|
|
|
|
|
|
|
|
|
typedef struct FileBrowseOp {
|
|
|
|
|
PointerRNA ptr;
|
|
|
|
|
PropertyRNA *prop;
|
2015-06-09 17:17:22 +10:00
|
|
|
bool is_undo;
|
2009-07-28 18:51:06 +00:00
|
|
|
} FileBrowseOp;
|
|
|
|
|
|
|
|
|
|
static int file_browse_exec(bContext *C, wmOperator *op)
|
|
|
|
|
{
|
2012-05-08 15:30:00 +00:00
|
|
|
FileBrowseOp *fbo = op->customdata;
|
2010-09-20 21:11:38 +00:00
|
|
|
ID *id;
|
2011-11-07 15:55:03 +00:00
|
|
|
char *str, path[FILE_MAX];
|
2012-05-08 15:30:00 +00:00
|
|
|
const char *path_prop = RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
|
2009-07-28 18:51:06 +00:00
|
|
|
|
2012-05-08 15:30:00 +00:00
|
|
|
if (RNA_struct_property_is_set(op->ptr, path_prop) == 0 || fbo == NULL)
|
2009-09-04 08:49:11 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
|
2012-05-08 15:30:00 +00:00
|
|
|
str = RNA_string_get_alloc(op->ptr, path_prop, NULL, 0);
|
2010-09-20 21:11:38 +00:00
|
|
|
|
|
|
|
|
/* add slash for directories, important for some properties */
|
2012-03-24 06:38:07 +00:00
|
|
|
if (RNA_property_subtype(fbo->prop) == PROP_DIRPATH) {
|
2014-01-28 03:52:21 +11:00
|
|
|
const bool is_relative = RNA_boolean_get(op->ptr, "relative_path");
|
2010-09-20 21:11:38 +00:00
|
|
|
id = fbo->ptr.id.data;
|
|
|
|
|
|
|
|
|
|
BLI_strncpy(path, str, FILE_MAX);
|
2011-11-07 15:55:03 +00:00
|
|
|
BLI_path_abs(path, id ? ID_BLEND_PATH(G.main, id) : G.main->name);
|
2010-12-14 16:23:15 +00:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (BLI_is_dir(path)) {
|
2013-03-29 01:34:04 +00:00
|
|
|
/* do this first so '//' isnt converted to '//\' on windows */
|
|
|
|
|
BLI_add_slash(path);
|
2013-01-18 08:44:55 +00:00
|
|
|
if (is_relative) {
|
|
|
|
|
BLI_strncpy(path, str, FILE_MAX);
|
|
|
|
|
BLI_path_rel(path, G.main->name);
|
|
|
|
|
str = MEM_reallocN(str, strlen(path) + 2);
|
|
|
|
|
BLI_strncpy(str, path, FILE_MAX);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
str = MEM_reallocN(str, strlen(str) + 2);
|
|
|
|
|
}
|
2010-09-20 21:11:38 +00:00
|
|
|
}
|
2013-03-05 06:26:10 +00:00
|
|
|
else {
|
|
|
|
|
char * const lslash = (char *)BLI_last_slash(str);
|
|
|
|
|
if (lslash) lslash[1] = '\0';
|
|
|
|
|
}
|
2010-09-20 21:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-28 18:51:06 +00:00
|
|
|
RNA_property_string_set(&fbo->ptr, fbo->prop, str);
|
|
|
|
|
RNA_property_update(C, &fbo->ptr, fbo->prop);
|
|
|
|
|
MEM_freeN(str);
|
|
|
|
|
|
2015-06-09 17:17:22 +10:00
|
|
|
if (fbo->is_undo) {
|
|
|
|
|
const char *undostr = RNA_property_identifier(fbo->prop);
|
|
|
|
|
ED_undo_push(C, undostr);
|
|
|
|
|
}
|
2011-05-23 10:14:07 +00:00
|
|
|
|
|
|
|
|
/* special, annoying exception, filesel on redo panel [#26618] */
|
|
|
|
|
{
|
2012-05-08 15:30:00 +00:00
|
|
|
wmOperator *redo_op = WM_operator_last_redo(C);
|
2012-03-24 06:38:07 +00:00
|
|
|
if (redo_op) {
|
|
|
|
|
if (fbo->ptr.data == redo_op->ptr->data) {
|
2011-05-23 10:14:07 +00:00
|
|
|
ED_undo_operator_repeat(C, redo_op);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-28 18:51:06 +00:00
|
|
|
MEM_freeN(op->customdata);
|
2011-05-23 10:14:07 +00:00
|
|
|
|
2009-07-28 18:51:06 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-30 23:08:53 +00:00
|
|
|
static void file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
|
2009-07-28 18:51:06 +00:00
|
|
|
{
|
|
|
|
|
MEM_freeN(op->customdata);
|
2012-05-08 15:30:00 +00:00
|
|
|
op->customdata = NULL;
|
2009-07-28 18:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-13 09:03:46 +00:00
|
|
|
static int file_browse_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
2009-07-28 18:51:06 +00:00
|
|
|
{
|
|
|
|
|
PointerRNA ptr;
|
|
|
|
|
PropertyRNA *prop;
|
2015-06-09 17:17:22 +10:00
|
|
|
bool is_undo;
|
2009-07-28 18:51:06 +00:00
|
|
|
FileBrowseOp *fbo;
|
|
|
|
|
char *str;
|
|
|
|
|
|
2012-02-02 05:55:01 +00:00
|
|
|
if (CTX_wm_space_file(C)) {
|
2012-10-26 17:32:50 +00:00
|
|
|
BKE_report(op->reports, RPT_ERROR, "Cannot activate a file selector, one already open");
|
2012-02-02 05:55:01 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 17:17:22 +10:00
|
|
|
UI_context_active_but_prop_get_filebrowser(C, &ptr, &prop, &is_undo);
|
2009-07-28 18:51:06 +00:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (!prop)
|
2009-07-28 18:51:06 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
|
2012-05-08 15:30:00 +00:00
|
|
|
str = RNA_property_string_get_alloc(&ptr, prop, NULL, 0, NULL);
|
2009-07-28 18:51:06 +00:00
|
|
|
|
2010-07-04 10:02:30 +00:00
|
|
|
/* useful yet irritating feature, Shift+Click to open the file
|
|
|
|
|
* Alt+Click to browse a folder in the OS's browser */
|
2012-03-24 06:38:07 +00:00
|
|
|
if (event->shift || event->alt) {
|
2014-11-16 11:09:51 +01:00
|
|
|
wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true);
|
2010-07-04 10:02:30 +00:00
|
|
|
PointerRNA props_ptr;
|
2010-06-25 12:04:04 +00:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (event->alt) {
|
2013-03-04 19:27:51 +00:00
|
|
|
char *lslash = (char *)BLI_last_slash(str);
|
2012-03-24 06:38:07 +00:00
|
|
|
if (lslash)
|
2012-05-08 15:30:00 +00:00
|
|
|
*lslash = '\0';
|
2010-07-04 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-11-16 11:09:51 +01:00
|
|
|
WM_operator_properties_create_ptr(&props_ptr, ot);
|
2010-07-04 10:02:30 +00:00
|
|
|
RNA_string_set(&props_ptr, "filepath", str);
|
2014-11-16 11:09:51 +01:00
|
|
|
WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &props_ptr);
|
2010-07-04 10:02:30 +00:00
|
|
|
WM_operator_properties_free(&props_ptr);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(str);
|
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2015-02-17 13:09:21 +11:00
|
|
|
PropertyRNA *prop_relpath;
|
2012-05-08 15:30:00 +00:00
|
|
|
const char *path_prop = RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
|
|
|
|
|
fbo = MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
|
|
|
|
|
fbo->ptr = ptr;
|
|
|
|
|
fbo->prop = prop;
|
2015-06-09 17:17:22 +10:00
|
|
|
fbo->is_undo = is_undo;
|
2012-05-08 15:30:00 +00:00
|
|
|
op->customdata = fbo;
|
2010-07-04 10:02:30 +00:00
|
|
|
|
2011-06-16 15:28:39 +00:00
|
|
|
RNA_string_set(op->ptr, path_prop, str);
|
2010-07-04 10:02:30 +00:00
|
|
|
MEM_freeN(str);
|
|
|
|
|
|
2011-10-12 00:15:19 +00:00
|
|
|
/* normally ED_fileselect_get_params would handle this but we need to because of stupid
|
|
|
|
|
* user-prefs exception - campbell */
|
2015-02-17 13:09:21 +11:00
|
|
|
if ((prop_relpath = RNA_struct_find_property(op->ptr, "relative_path"))) {
|
|
|
|
|
if (!RNA_property_is_set(op->ptr, prop_relpath)) {
|
2012-10-14 13:08:19 +00:00
|
|
|
/* annoying exception!, if were dealing with the user prefs, default relative to be off */
|
2015-02-17 13:09:21 +11:00
|
|
|
RNA_property_boolean_set(op->ptr, prop_relpath, U.flag & USER_RELPATHS && (ptr.data != &U));
|
2010-11-05 06:33:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-07-04 10:02:30 +00:00
|
|
|
WM_event_add_fileselect(C, op);
|
|
|
|
|
|
|
|
|
|
return OPERATOR_RUNNING_MODAL;
|
|
|
|
|
}
|
2009-07-28 18:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BUTTONS_OT_file_browse(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->name = "Accept";
|
|
|
|
|
ot->description = "Open a file browser, Hold Shift to open the file, Alt to browse containing directory";
|
|
|
|
|
ot->idname = "BUTTONS_OT_file_browse";
|
2009-07-28 18:51:06 +00:00
|
|
|
|
|
|
|
|
/* api callbacks */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->invoke = file_browse_invoke;
|
|
|
|
|
ot->exec = file_browse_exec;
|
|
|
|
|
ot->cancel = file_browse_cancel;
|
2009-07-28 18:51:06 +00:00
|
|
|
|
2015-06-09 17:17:22 +10:00
|
|
|
/* conditional undo based on button flag */
|
|
|
|
|
ot->flag = 0;
|
2014-07-07 15:25:32 +06:00
|
|
|
|
2009-07-28 18:51:06 +00:00
|
|
|
/* properties */
|
2012-08-04 12:54:27 +00:00
|
|
|
WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE,
|
|
|
|
|
WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
|
2009-07-28 18:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-16 15:28:39 +00:00
|
|
|
/* second operator, only difference from BUTTONS_OT_file_browse is WM_FILESEL_DIRECTORY */
|
|
|
|
|
void BUTTONS_OT_directory_browse(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->name = "Accept";
|
|
|
|
|
ot->description = "Open a directory browser, Hold Shift to open the file, Alt to browse containing directory";
|
|
|
|
|
ot->idname = "BUTTONS_OT_directory_browse";
|
2011-06-16 15:28:39 +00:00
|
|
|
|
|
|
|
|
/* api callbacks */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->invoke = file_browse_invoke;
|
|
|
|
|
ot->exec = file_browse_exec;
|
|
|
|
|
ot->cancel = file_browse_cancel;
|
2011-06-16 15:28:39 +00:00
|
|
|
|
2015-06-09 17:17:22 +10:00
|
|
|
/* conditional undo based on button flag */
|
|
|
|
|
ot->flag = 0;
|
2014-07-07 15:25:32 +06:00
|
|
|
|
2011-06-16 15:28:39 +00:00
|
|
|
/* properties */
|
2012-08-04 12:54:27 +00:00
|
|
|
WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE,
|
|
|
|
|
WM_FILESEL_DIRECTORY | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
|
2011-06-16 15:28:39 +00:00
|
|
|
}
|