Renamed the FILE_OPEN option for the file browser to FILE_OPENFILE to cleanup compile...
[blender.git] / source / blender / editors / space_info / info_ops.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_packedFile_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_userdef_types.h"
37 #include "DNA_windowmanager_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_bpath.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_image.h"
47 #include "BKE_main.h"
48 #include "BKE_packedFile.h"
49 #include "BKE_report.h"
50 #include "BKE_screen.h"
51
52 #include "ED_screen.h"
53 #include "ED_types.h"
54 #include "ED_util.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "UI_interface.h"
63 #include "UI_resources.h"
64
65 #include "IMB_imbuf_types.h"
66
67 #include "RNA_access.h"
68 #include "RNA_define.h"
69
70 #include "WM_types.h"
71
72 #include "info_intern.h"
73
74 /********************* pack all operator *********************/
75
76 static int pack_all_exec(bContext *C, wmOperator *op)
77 {
78         Main *bmain= CTX_data_main(C);
79
80         packAll(bmain, op->reports);
81         G.fileflags |= G_AUTOPACK;
82
83         return OPERATOR_FINISHED;
84 }
85
86 static int pack_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
87 {
88         Main *bmain= CTX_data_main(C);
89         Image *ima;
90         ImBuf *ibuf;
91
92         // first check for dirty images
93         for(ima=bmain->image.first; ima; ima=ima->id.next) {
94                 if(ima->ibufs.first) { /* XXX FIX */
95                         ibuf= BKE_image_get_ibuf(ima, NULL);
96                         
97                         if(ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
98                                 break;
99                 }
100         }
101
102         if(ima) {
103                 uiPupMenuOkee(C, "FILE_OT_pack_all", "Some images are painted on. These changes will be lost. Continue?");
104                 return OPERATOR_CANCELLED;
105         }
106
107         return pack_all_exec(C, op);
108 }
109
110 void FILE_OT_pack_all(wmOperatorType *ot)
111 {
112         /* identifiers */
113         ot->name= "Pack All";
114         ot->idname= "FILE_OT_pack_all";
115         
116         /* api callbacks */
117         ot->exec= pack_all_exec;
118         ot->invoke= pack_all_invoke;
119
120         /* flags */
121         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
122 }
123
124 /********************* unpack all operator *********************/
125
126 static const EnumPropertyItem unpack_all_method_items[] = {
127         {PF_USE_LOCAL, "USE_LOCAL", 0, "Use files in current directory (create when necessary)", ""},
128         {PF_WRITE_LOCAL, "WRITE_LOCAL", 0, "Write files to current directory (overwrite existing files)", ""},
129         {PF_USE_ORIGINAL, "USE_ORIGINAL", 0, "Use files in original location (create when necessary)", ""},
130         {PF_WRITE_ORIGINAL, "WRITE_ORIGINAL", 0, "Write files to original location (overwrite existing files)", ""},
131         {PF_KEEP, "KEEP", 0, "Disable AutoPack, keep all packed files", ""},
132         {PF_ASK, "ASK", 0, "Ask for each file", ""},
133         {0, NULL, 0, NULL, NULL}};
134
135 static int unpack_all_exec(bContext *C, wmOperator *op)
136 {
137         Main *bmain= CTX_data_main(C);
138         int method= RNA_enum_get(op->ptr, "method");
139
140         if(method != PF_KEEP) unpackAll(bmain, op->reports, method); /* XXX PF_ASK can't work here */
141         G.fileflags &= ~G_AUTOPACK;
142
143         return OPERATOR_FINISHED;
144 }
145
146 static int unpack_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
147 {
148         Main *bmain= CTX_data_main(C);
149         uiPopupMenu *pup;
150         uiLayout *layout;
151         char title[128];
152         int count = 0;
153         
154         count = countPackedFiles(bmain);
155         
156         if(!count) {
157                 BKE_report(op->reports, RPT_WARNING, "No packed files. Autopack disabled.");
158                 G.fileflags &= ~G_AUTOPACK;
159                 return OPERATOR_CANCELLED;
160         }
161
162         if(count == 1)
163                 sprintf(title, "Unpack 1 file");
164         else
165                 sprintf(title, "Unpack %d files", count);
166         
167         pup= uiPupMenuBegin(C, title, 0);
168         layout= uiPupMenuLayout(pup);
169
170         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
171         uiItemsEnumO(layout, "FILE_OT_unpack_all", "method");
172
173         uiPupMenuEnd(C, pup);
174
175         return OPERATOR_CANCELLED;
176 }
177
178 void FILE_OT_unpack_all(wmOperatorType *ot)
179 {
180         /* identifiers */
181         ot->name= "Unpack All";
182         ot->idname= "FILE_OT_unpack_all";
183         
184         /* api callbacks */
185         ot->exec= unpack_all_exec;
186         ot->invoke= unpack_all_invoke;
187
188         /* flags */
189         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
190
191         /* properties */
192         RNA_def_enum(ot->srna, "method", unpack_all_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
193 }
194
195 /********************* make paths relative operator *********************/
196
197 static int make_paths_relative_exec(bContext *C, wmOperator *op)
198 {
199         if(!G.relbase_valid) {
200                 BKE_report(op->reports, RPT_WARNING, "Can't set relative paths with an unsaved blend file.");
201                 return OPERATOR_CANCELLED;
202         }
203
204         makeFilesRelative(G.sce, op->reports);
205
206         return OPERATOR_FINISHED;
207 }
208
209 void FILE_OT_make_paths_relative(wmOperatorType *ot)
210 {
211         /* identifiers */
212         ot->name= "Make All Paths Relative";
213         ot->idname= "FILE_OT_make_paths_relative";
214         
215         /* api callbacks */
216         ot->exec= make_paths_relative_exec;
217
218         /* flags */
219         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
220 }
221
222 /********************* make paths absolute operator *********************/
223
224 static int make_paths_absolute_exec(bContext *C, wmOperator *op)
225 {
226         if(!G.relbase_valid) {
227                 BKE_report(op->reports, RPT_WARNING, "Can't set absolute paths with an unsaved blend file.");
228                 return OPERATOR_CANCELLED;
229         }
230
231         makeFilesAbsolute(G.sce, op->reports);
232         return OPERATOR_FINISHED;
233 }
234
235 void FILE_OT_make_paths_absolute(wmOperatorType *ot)
236 {
237         /* identifiers */
238         ot->name= "Make All Paths Absolute";
239         ot->idname= "FILE_OT_make_paths_absolute";
240         
241         /* api callbacks */
242         ot->exec= make_paths_absolute_exec;
243
244         /* flags */
245         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
246 }
247
248 /********************* report missing files operator *********************/
249
250 static int report_missing_files_exec(bContext *C, wmOperator *op)
251 {
252         char txtname[24]; /* text block name */
253
254         txtname[0] = '\0';
255         
256         /* run the missing file check */
257         checkMissingFiles(G.sce, op->reports);
258         
259         return OPERATOR_FINISHED;
260 }
261
262 void FILE_OT_report_missing_files(wmOperatorType *ot)
263 {
264         /* identifiers */
265         ot->name= "Report Missing Files...";
266         ot->idname= "FILE_OT_report_missing_files";
267         
268         /* api callbacks */
269         ot->exec= report_missing_files_exec;
270
271         /* flags */
272         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
273 }
274
275 /********************* find missing files operator *********************/
276
277 static int find_missing_files_exec(bContext *C, wmOperator *op)
278 {
279         char *path;
280         
281         path= RNA_string_get_alloc(op->ptr, "path", NULL, 0);
282         findMissingFiles(path, G.sce);
283         MEM_freeN(path);
284
285         return OPERATOR_FINISHED;
286 }
287
288 static int find_missing_files_invoke(bContext *C, wmOperator *op, wmEvent *event)
289 {
290         /* XXX file open button text "Find Missing Files" */
291         WM_event_add_fileselect(C, op); 
292         return OPERATOR_RUNNING_MODAL;
293 }
294
295 void FILE_OT_find_missing_files(wmOperatorType *ot)
296 {
297         /* identifiers */
298         ot->name= "Find Missing Files...";
299         ot->idname= "FILE_OT_find_missing_files";
300         
301         /* api callbacks */
302         ot->exec= find_missing_files_exec;
303         ot->invoke= find_missing_files_invoke;
304
305         /* flags */
306         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
307
308         /* properties */
309         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE);
310 }