Merge from 2.5 r20855 through r20966
[blender.git] / source / blender / editors / space_script / script_edit.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_space_types.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_userdef_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_screen.h"
43 #include "BKE_utildefines.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50
51 #include "ED_screen.h"
52 #include "ED_types.h"
53
54 #include "UI_interface.h"
55 #include "UI_resources.h"
56
57 #include "script_intern.h"      // own include
58
59
60 #include "BPY_extern.h" /* BPY_run_python_script */
61
62 static int run_pyfile_exec(bContext *C, wmOperator *op)
63 {
64         ARegion *ar= CTX_wm_region(C);
65         
66
67         char filename[512];
68         RNA_string_get(op->ptr, "filename", filename);
69 #ifndef DISABLE_PYTHON
70         if(BPY_run_python_script(C, filename, NULL, op->reports)) {
71                 ED_region_tag_redraw(ar);
72                 return OPERATOR_FINISHED;
73         }
74 #endif
75         return OPERATOR_CANCELLED; /* FAIL */
76 }
77
78 void SCRIPT_OT_python_file_run(wmOperatorType *ot)
79 {
80         /* identifiers */
81         ot->name= "Run python file";
82         ot->idname= "SCRIPT_OT_python_file_run";
83
84         /* api callbacks */
85         ot->exec= run_pyfile_exec;
86         ot->poll= ED_operator_areaactive;
87
88         RNA_def_string_file_path(ot->srna, "filename", "", 512, "Filename", "");
89 }
90
91 static int run_ui_scripts_exec(bContext *C, wmOperator *op)
92 {
93 #ifndef DISABLE_PYTHON
94         BPY_run_ui_scripts(C, 1); /* reload */
95 #endif
96         return OPERATOR_FINISHED;
97 }
98
99 static int run_ui_scripts_invoke(bContext *C, wmOperator *op, wmEvent *event)
100 {       
101         int ret= run_ui_scripts_exec(C, op);
102         
103         if(ret==OPERATOR_FINISHED)
104                 WM_event_add_notifier(C, NC_WINDOW, NULL);      
105         
106         return ret;
107 }
108
109
110 void SCRIPT_OT_python_run_ui_scripts(wmOperatorType *ot)
111 {
112         /* identifiers */
113         ot->name= "Reload Python Interface";
114         ot->idname= "SCRIPT_OT_python_run_ui_scripts";
115
116         /* api callbacks */
117         ot->exec= run_ui_scripts_exec;
118         ot->invoke= run_ui_scripts_invoke;
119         ot->poll= ED_operator_areaactive;
120 }