Added rename/delete popups to the file and imageselect windows.
[blender.git] / source / blender / src / editimasel.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdlib.h>  
34 #include <string.h>
35 #include <stdio.h>
36 #include <math.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifdef WIN32
43 #include "BLI_winstuff.h"
44 #else
45 #include <sys/times.h>
46 #endif
47
48 #include "PIL_time.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52 #include "BLI_editVert.h"
53
54 #include "DNA_screen_types.h"
55 #include "DNA_space_types.h"
56 #include "DNA_userdef_types.h"
57
58 #include "BKE_global.h"
59
60 #include "BIF_fsmenu.h"
61 #include "BIF_screen.h"
62 #include "BIF_interface.h"
63 #include "BIF_imasel.h"
64 #include "BIF_mywindow.h"
65 #include "BIF_toolbox.h"
66
67 #include "BSE_filesel.h"
68 #include "BSE_drawimasel.h"
69
70 #include "BDR_editcurve.h"
71
72 #include "blendef.h"
73 #include "mydevice.h"
74 #include "interface.h"
75
76 #define XIC 20
77 #define YIC 21
78
79 /* GLOBALS */
80 extern char *fsmenu;
81
82
83 static void draw_icon_imasel(void)
84 {
85         scrarea_queue_winredraw(curarea);
86 }
87
88 void winqreadimaselspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
89 {
90         unsigned short event= evt->event;
91         short val= evt->val;
92         char ascii= evt->ascii;
93         SpaceImaSel *simasel;
94         
95         short mval[2];
96         short area_event;
97         short queredraw = 0;
98         int ret = 0;
99         char  name[256];
100         char *selname;
101         static double prevtime=0;
102         
103         
104         if(val==0) return;
105         simasel= curarea->spacedata.first;
106         
107         area_event = 0;
108         getmouseco_areawin(mval);
109         simasel->mx= mval[0];
110         simasel->my= mval[1];
111         
112         if (simasel->desx > 0){
113                 if ( (mval[0] > simasel->dssx) && (mval[0] < simasel->dsex) && (mval[1] > simasel->dssy) && (mval[1] < simasel->dsey) ) area_event = IMS_INDIRSLI;
114                 if ( (mval[0] > simasel->desx) && (mval[0] < simasel->deex) && (mval[1] > simasel->desy) && (mval[1] < simasel->deey) ) area_event = IMS_INDIR;
115         }
116         if (simasel->fesx > 0){
117                 if ( (mval[0] > simasel->fssx) && (mval[0] < simasel->fsex) && (mval[1] > simasel->fssy) && (mval[1] < simasel->fsey) ) area_event = IMS_INFILESLI;
118                 if ( (mval[0] > simasel->fesx) && (mval[0] < simasel->feex) && (mval[1] > simasel->fesy) && (mval[1] < simasel->feey) ) area_event = IMS_INFILE;
119         }       
120         
121         if( event!=RETKEY && event!=PADENTER)
122                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
123
124         switch(event) {
125         case AFTERPIBREAD:      
126                 get_pib_file(simasel);
127                 queredraw = 1;
128                 break;
129                         
130         case AFTERIMASELIMA:
131                 if (bitset(simasel->fase, IMS_DOTHE_INF)){
132                         get_file_info(simasel);
133                         
134                         if (!bitset(simasel->fase, IMS_KNOW_INF)){
135                                 addafterqueue(curarea->win, AFTERIMASELIMA, 1);
136                                 
137                         }else{
138                                 simasel->subfase = 0;
139                                 simasel->imafase = 0;
140                                 simasel->fase |= IMS_DOTHE_IMA;
141                                 addafterqueue(curarea->win, AFTERIMASELGET, 1);
142                         }
143                 }
144                 break;
145         case AFTERIMASELGET:
146                 if (bitset(simasel->fase, IMS_DOTHE_IMA)){
147                         get_next_image(simasel);
148                         if (simasel->ima_redraw > 0){
149                                 double newtime = PIL_check_seconds_timer();
150                                 if ((newtime - prevtime) > 0.03) { 
151                                         simasel->ima_redraw = 0;
152                                         queredraw = 1;
153                                         prevtime = newtime;
154                                 }
155                                 
156                         }
157                         if (!bitset(simasel->fase, IMS_KNOW_IMA)){
158                                 addafterqueue(curarea->win, AFTERIMASELGET, 1);
159                         }else{
160                                 simasel->ima_redraw = 0;
161                                 simasel->subfase    = 0;
162                                 simasel->imafase    = 0;
163                                 addqueue(curarea->win, AFTERIMAWRITE, 1);
164                                 queredraw = 1;
165                         }
166                 }
167                 break;
168         case  AFTERIMAWRITE:
169                 if (bitset(simasel->fase, IMS_KNOW_IMA)){
170                         write_new_pib(simasel);
171                         queredraw = 1;
172                 }
173                 break;  
174         
175         case RIGHTMOUSE:
176                 if ((area_event == IMS_INFILE) && (simasel->hilite_ima)){
177                         select_ima_files(simasel);
178                         queredraw = 1;
179                 }
180                 break;
181         case UI_BUT_EVENT:
182                 
183                 /* bug: blender's interface kit also returns a '4'... what is it! */
184                 
185                 switch(val) {
186                 case 13:        /*  'P' */
187                         imadir_parent(simasel);
188                         queredraw = 1;
189                         
190                 case 1: /* dir entry */
191                         checkdir(simasel->dir);
192                         clear_ima_dir(simasel);
193                         queredraw = 1;
194                         break;
195                 
196                 case 3: /* fsmenu */
197                         selname= fsmenu_get_entry(simasel->fileselmenuitem-1);
198                         if (selname) {
199                                 strcpy(simasel->dir, selname);
200                                 checkdir(simasel->dir);
201                                 clear_ima_dir(simasel);
202                             queredraw = 1;
203                         }
204                         break;
205
206                 case 5:
207                         if (simasel->returnfunc) {
208                                 char name[256];
209                                 strcpy(name, simasel->dir);
210                                 strcat(name, simasel->file);
211                                 simasel->returnfunc(name);
212                                 filesel_prevspace();
213                         }
214                         break;
215                 case 6:
216                         filesel_prevspace();
217                         break;
218                                         
219                 }
220                 break;
221                 
222         case LEFTMOUSE:
223         case MIDDLEMOUSE:
224         
225                 /* No button pressed */
226                 switch (area_event){
227                 case IMS_INDIRSLI:
228                         move_imadir_sli(simasel);
229                         queredraw = 1;
230                         break;
231                 case IMS_INFILESLI:
232                         move_imafile_sli(simasel);
233                         queredraw = 1;
234                         break;
235                 case IMS_INDIR:
236                         if (simasel->hilite > -1){
237                                 change_imadir(simasel);
238                                 queredraw = 1;
239                         }
240                         break;
241                 case IMS_INFILE:
242                         if (simasel->hilite_ima){
243                                 strcpy(simasel->fole, simasel->hilite_ima->file_name);
244                                 strcpy(simasel->file, simasel->hilite_ima->file_name);
245                                 
246                                 if (event == LEFTMOUSE) addqueue(curarea->win, IMALEFTMOUSE, 1);        
247                                 
248                                 if ((event == MIDDLEMOUSE)&&(simasel->returnfunc)){
249                                         strcpy(name, simasel->dir);
250                                         strcat(name, simasel->file);
251                                         
252                                         if(simasel->mode & IMS_STRINGCODE) BLI_makestringcode(G.sce, name);
253                                         
254                                         simasel->returnfunc(name);
255                                         filesel_prevspace();
256                                 }
257                                 queredraw = 1;
258                         }
259                         break;
260                 }
261                 break;
262         
263         case MOUSEX:
264         case MOUSEY:
265                 getmouseco_areawin(mval);       /* lokaal screen */
266                 calc_hilite(simasel);
267                 if (simasel->mouse_move_redraw ){
268                         simasel->mouse_move_redraw = 0;
269                         queredraw = 1;
270                 }
271                 break;
272                 
273         case WHEELUPMOUSE:
274         case WHEELDOWNMOUSE:
275                 switch(area_event){
276                 case IMS_INDIRSLI:
277                 case IMS_INDIR:
278                         if (simasel->dirsli){
279                                 if (event == WHEELUPMOUSE)      simasel->topdir -= U.wheellinescroll;
280                                 if (event == WHEELDOWNMOUSE)    simasel->topdir += U.wheellinescroll;   
281                                 queredraw = 1;
282                         }
283                         break;
284                 case IMS_INFILESLI:
285                 case IMS_INFILE:
286                         if(simasel->imasli){
287                                 if (event == WHEELUPMOUSE)      simasel->image_slider -= 0.2 * simasel->slider_height;
288                                 if (event == WHEELDOWNMOUSE)    simasel->image_slider += 0.2 * simasel->slider_height;
289                                 
290                                 if(simasel->image_slider < 0.0) simasel->image_slider = 0.0;
291                                 if(simasel->image_slider > 1.0) simasel->image_slider = 1.0;
292                                 queredraw = 1;
293                         }       
294                         break;
295                 }
296                 break;
297
298         case PAGEUPKEY:
299         case PAGEDOWNKEY:
300                 switch(area_event){
301                 case IMS_INDIRSLI:
302                 case IMS_INDIR:
303                         if (simasel->dirsli){
304                                 if (event == PAGEUPKEY)   simasel->topdir -= (simasel->dirsli_lines - 1);
305                                 if (event == PAGEDOWNKEY) simasel->topdir += (simasel->dirsli_lines - 1);       
306                                 queredraw = 1;
307                         }
308                         break;
309                 case IMS_INFILESLI:
310                 case IMS_INFILE:
311                         if(simasel->imasli){
312                                 if (event == PAGEUPKEY)   simasel->image_slider -= simasel->slider_height;
313                                 if (event == PAGEDOWNKEY) simasel->image_slider += simasel->slider_height;
314                                 
315                                 if(simasel->image_slider < 0.0)  simasel->image_slider = 0.0;
316                                 if(simasel->image_slider > 1.0)  simasel->image_slider = 1.0;
317                                 queredraw = 1;
318                         }       
319                         break;
320                 }
321                 break;
322         
323         case HOMEKEY:
324                 simasel->image_slider = 0.0;
325                 queredraw = 1;
326                 break;
327
328         case ENDKEY:
329                 simasel->image_slider = 1.0;
330                 queredraw = 1;
331                 break;
332                         
333         case AKEY:
334                 if (G.qual == 0){
335                         ima_select_all(simasel);
336                         queredraw = 1;
337                 }
338                 break;
339
340         case IKEY:
341                 if ((G.qual == 0)&&(simasel->file)){
342                         sprintf(name, "$IMAGEEDITOR %s%s", simasel->dir, simasel->file);
343                         system(name);
344                         queredraw = 1;
345                 }
346
347                 break;
348         
349         case PKEY:
350                 if(G.qual & LR_SHIFTKEY) {
351                         extern char bprogname[];        /* usiblender.c */
352                         
353                         sprintf(name, "%s -a \"%s%s\"", bprogname, simasel->dir, simasel->file);
354                         system(name);
355                 }
356                 if(G.qual & LR_CTRLKEY) {
357                         if(bitset(simasel->fase, IMS_KNOW_IMA)) pibplay(simasel);
358                 }
359                 if (G.qual == 0){
360                         imadir_parent(simasel);
361                         checkdir(simasel->dir);
362                         clear_ima_dir(simasel);
363                         queredraw = 1;
364                 }
365                 break;
366                 
367         case RKEY:
368         case XKEY:
369                 if (simasel->hilite_ima){
370                         strcpy(name, simasel->dir);
371                         strcat(name, simasel->hilite_ima->file_name);
372
373                         if( okee("Remove %s", name) ) {
374                                 ret = BLI_delete(name, 0, 0);
375                                 if (ret) {
376                                         error("Command failed, see console");
377                                 } else {
378                                         clear_ima_dir(simasel);
379                                         queredraw = 1;
380                                 }
381                         }
382                 }
383                 break;
384
385         case PADPLUSKEY:
386         case EQUALKEY:
387                 BLI_newname(simasel->file, +1);
388                 queredraw = 1;
389                 break;
390                 
391         case PADMINUS:
392         case MINUSKEY:
393                 BLI_newname(simasel->file, -1);
394                 queredraw = 1;
395                 break;
396                 
397         case BACKSLASHKEY:
398         case SLASHKEY:
399 #ifdef WIN32
400                 strcpy(simasel->dir, "\\");
401 #else
402                 strcpy(simasel->dir, "/");
403 #endif
404                 clear_ima_dir(simasel);
405                 simasel->image_slider = 0.0;
406                 queredraw = 1;
407                 break;
408                 
409         case PERIODKEY:
410                 clear_ima_dir(simasel);
411                 queredraw = 1;
412                 break;
413         
414         case ESCKEY:
415                 filesel_prevspace();
416                 break;
417
418         case PADENTER:
419         case RETKEY:
420                 if (simasel->returnfunc){
421                         strcpy(name, simasel->dir);
422                         strcat(name, simasel->file);
423                         simasel->returnfunc(name);
424                         filesel_prevspace();
425                 }
426                 break;
427         }
428         
429                 
430         if (queredraw) scrarea_queue_winredraw(curarea);
431 }
432
433
434 void clever_numbuts_imasel()
435 {
436         SpaceImaSel *simasel;
437         static char orgname[FILE_MAXDIR+FILE_MAXFILE+12];
438         static char filename[FILE_MAXDIR+FILE_MAXFILE+12];
439         static char newname[FILE_MAXDIR+FILE_MAXFILE+12];
440         int len;
441         
442         simasel= curarea->spacedata.first;
443         len = 110;
444
445         if (simasel->hilite_ima){
446                 BLI_make_file_string(G.sce, orgname, simasel->dir, simasel->hilite_ima->file_name);
447                 strcpy(filename, simasel->hilite_ima->file_name);
448
449                 add_numbut(0, TEX, "", 0, len, filename, "Rename Image");
450                 if( do_clever_numbuts("Rename Image", 1, REDRAW) ) {
451                         BLI_make_file_string(G.sce, newname, simasel->dir, filename);
452
453                         if( strcmp(orgname, newname) != 0 ) {
454                                 BLI_rename(orgname, newname);
455
456                                 clear_ima_dir(simasel);
457                         }
458                 }
459
460                 scrarea_queue_winredraw(curarea);
461         }
462 }