Fix for undo... it didn't do the UV coords (tface) nor the vertexpaint
[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
53 #include "DNA_screen_types.h"
54 #include "DNA_space_types.h"
55 #include "DNA_userdef_types.h"
56
57 #include "BKE_global.h"
58
59 #include "BIF_fsmenu.h"
60 #include "BIF_screen.h"
61 #include "BIF_interface.h"
62 #include "BIF_imasel.h"
63 #include "BIF_mywindow.h"
64 #include "BIF_toolbox.h"
65
66 #include "BSE_filesel.h"
67 #include "BSE_drawimasel.h"
68
69 #include "BDR_editcurve.h"
70
71 #include "blendef.h"
72 #include "mydevice.h"
73
74 #define XIC 20
75 #define YIC 21
76
77 /* GLOBALS */
78 extern char *fsmenu;
79
80 void winqreadimaselspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
81 {
82         unsigned short event= evt->event;
83         short val= evt->val;
84         SpaceImaSel *simasel;
85         
86         short mval[2];
87         short area_event;
88         short queredraw = 0;
89         int ret = 0;
90         char  name[256];
91         char *selname;
92         static double prevtime=0;
93         
94         
95         if(val==0) return;
96         simasel= curarea->spacedata.first;
97         
98         area_event = 0;
99         getmouseco_areawin(mval);
100         simasel->mx= mval[0];
101         simasel->my= mval[1];
102         
103         if (simasel->desx > 0){
104                 if ( (mval[0] > simasel->dssx) && (mval[0] < simasel->dsex) && (mval[1] > simasel->dssy) && (mval[1] < simasel->dsey) ) area_event = IMS_INDIRSLI;
105                 if ( (mval[0] > simasel->desx) && (mval[0] < simasel->deex) && (mval[1] > simasel->desy) && (mval[1] < simasel->deey) ) area_event = IMS_INDIR;
106         }
107         if (simasel->fesx > 0){
108                 if ( (mval[0] > simasel->fssx) && (mval[0] < simasel->fsex) && (mval[1] > simasel->fssy) && (mval[1] < simasel->fsey) ) area_event = IMS_INFILESLI;
109                 if ( (mval[0] > simasel->fesx) && (mval[0] < simasel->feex) && (mval[1] > simasel->fesy) && (mval[1] < simasel->feey) ) area_event = IMS_INFILE;
110         }       
111         
112         if( event!=RETKEY && event!=PADENTER)
113                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
114
115         switch(event) {
116         case AFTERPIBREAD:      
117                 get_pib_file(simasel);
118                 queredraw = 1;
119                 break;
120                         
121         case AFTERIMASELIMA:
122                 if (bitset(simasel->fase, IMS_DOTHE_INF)){
123                         get_file_info(simasel);
124                         
125                         if (!bitset(simasel->fase, IMS_KNOW_INF)){
126                                 addafterqueue(curarea->win, AFTERIMASELIMA, 1);
127                                 
128                         }else{
129                                 simasel->subfase = 0;
130                                 simasel->imafase = 0;
131                                 simasel->fase |= IMS_DOTHE_IMA;
132                                 addafterqueue(curarea->win, AFTERIMASELGET, 1);
133                         }
134                 }
135                 break;
136         case AFTERIMASELGET:
137                 if (bitset(simasel->fase, IMS_DOTHE_IMA)){
138                         get_next_image(simasel);
139                         if (simasel->ima_redraw > 0){
140                                 double newtime = PIL_check_seconds_timer();
141                                 if ((newtime - prevtime) > 0.03) { 
142                                         simasel->ima_redraw = 0;
143                                         queredraw = 1;
144                                         prevtime = newtime;
145                                 }
146                                 
147                         }
148                         if (!bitset(simasel->fase, IMS_KNOW_IMA)){
149                                 addafterqueue(curarea->win, AFTERIMASELGET, 1);
150                         }else{
151                                 simasel->ima_redraw = 0;
152                                 simasel->subfase    = 0;
153                                 simasel->imafase    = 0;
154                                 addqueue(curarea->win, AFTERIMAWRITE, 1);
155                                 queredraw = 1;
156                         }
157                 }
158                 break;
159         case  AFTERIMAWRITE:
160                 if (bitset(simasel->fase, IMS_KNOW_IMA)){
161                         write_new_pib(simasel);
162                         queredraw = 1;
163                 }
164                 break;  
165         
166         case RIGHTMOUSE:
167                 if ((area_event == IMS_INFILE) && (simasel->hilite_ima)){
168                         select_ima_files(simasel);
169                         queredraw = 1;
170                 }
171                 break;
172         case UI_BUT_EVENT:
173                 
174                 /* bug: blender's interface kit also returns a '4'... what is it! */
175                 
176                 switch(val) {
177                 case 13:        /*  'P' */
178                         imadir_parent(simasel);
179                         queredraw = 1;
180                         
181                 case 1: /* dir entry */
182                         checkdir(simasel->dir);
183                         clear_ima_dir(simasel);
184                         queredraw = 1;
185                         break;
186                 
187                 case 3: /* fsmenu */
188                         selname= fsmenu_get_entry(simasel->fileselmenuitem-1);
189                         if (selname) {
190                                 strcpy(simasel->dir, selname);
191                                 checkdir(simasel->dir);
192                                 clear_ima_dir(simasel);
193                             queredraw = 1;
194                         }
195                         break;
196
197                 case 5:
198                         if (simasel->returnfunc) {
199                                 char name[256];
200                                 strcpy(name, simasel->dir);
201                                 strcat(name, simasel->file);
202                                 simasel->returnfunc(name);
203                                 filesel_prevspace();
204                         }
205                         break;
206                 case 6:
207                         filesel_prevspace();
208                         break;
209                                         
210                 }
211                 break;
212                 
213         case LEFTMOUSE:
214         case MIDDLEMOUSE:
215         
216                 /* No button pressed */
217                 switch (area_event){
218                 case IMS_INDIRSLI:
219                         move_imadir_sli(simasel);
220                         queredraw = 1;
221                         break;
222                 case IMS_INFILESLI:
223                         move_imafile_sli(simasel);
224                         queredraw = 1;
225                         break;
226                 case IMS_INDIR:
227                         if (simasel->hilite > -1){
228                                 change_imadir(simasel);
229                                 queredraw = 1;
230                         }
231                         break;
232                 case IMS_INFILE:
233                         if (simasel->hilite_ima){
234                                 strcpy(simasel->fole, simasel->hilite_ima->file_name);
235                                 strcpy(simasel->file, simasel->hilite_ima->file_name);
236                                 
237                                 if (event == LEFTMOUSE) addqueue(curarea->win, IMALEFTMOUSE, 1);        
238                                 
239                                 if ((event == MIDDLEMOUSE)&&(simasel->returnfunc)){
240                                         strcpy(name, simasel->dir);
241                                         strcat(name, simasel->file);
242                                         
243                                         if(simasel->mode & IMS_STRINGCODE) BLI_makestringcode(G.sce, name);
244                                         
245                                         simasel->returnfunc(name);
246                                         filesel_prevspace();
247                                 }
248                                 queredraw = 1;
249                         }
250                         break;
251                 }
252                 break;
253         
254         case MOUSEX:
255         case MOUSEY:
256                 getmouseco_areawin(mval);       /* local screen coordinates */
257                 calc_hilite(simasel);
258                 if (simasel->mouse_move_redraw ){
259                         simasel->mouse_move_redraw = 0;
260                         queredraw = 1;
261                 }
262                 break;
263                 
264         case WHEELUPMOUSE:
265         case WHEELDOWNMOUSE:
266                 switch(area_event){
267                 case IMS_INDIRSLI:
268                 case IMS_INDIR:
269                         if (simasel->dirsli){
270                                 if (event == WHEELUPMOUSE)      simasel->topdir -= U.wheellinescroll;
271                                 if (event == WHEELDOWNMOUSE)    simasel->topdir += U.wheellinescroll;   
272                                 queredraw = 1;
273                         }
274                         break;
275                 case IMS_INFILESLI:
276                 case IMS_INFILE:
277                         if(simasel->imasli){
278                                 if (event == WHEELUPMOUSE)      simasel->image_slider -= 0.2 * simasel->slider_height;
279                                 if (event == WHEELDOWNMOUSE)    simasel->image_slider += 0.2 * simasel->slider_height;
280                                 
281                                 if(simasel->image_slider < 0.0) simasel->image_slider = 0.0;
282                                 if(simasel->image_slider > 1.0) simasel->image_slider = 1.0;
283                                 queredraw = 1;
284                         }       
285                         break;
286                 }
287                 break;
288
289         case PAGEUPKEY:
290         case PAGEDOWNKEY:
291                 switch(area_event){
292                 case IMS_INDIRSLI:
293                 case IMS_INDIR:
294                         if (simasel->dirsli){
295                                 if (event == PAGEUPKEY)   simasel->topdir -= (simasel->dirsli_lines - 1);
296                                 if (event == PAGEDOWNKEY) simasel->topdir += (simasel->dirsli_lines - 1);       
297                                 queredraw = 1;
298                         }
299                         break;
300                 case IMS_INFILESLI:
301                 case IMS_INFILE:
302                         if(simasel->imasli){
303                                 if (event == PAGEUPKEY)   simasel->image_slider -= simasel->slider_height;
304                                 if (event == PAGEDOWNKEY) simasel->image_slider += simasel->slider_height;
305                                 
306                                 if(simasel->image_slider < 0.0)  simasel->image_slider = 0.0;
307                                 if(simasel->image_slider > 1.0)  simasel->image_slider = 1.0;
308                                 queredraw = 1;
309                         }       
310                         break;
311                 }
312                 break;
313         
314         case HOMEKEY:
315                 simasel->image_slider = 0.0;
316                 queredraw = 1;
317                 break;
318
319         case ENDKEY:
320                 simasel->image_slider = 1.0;
321                 queredraw = 1;
322                 break;
323                         
324         case AKEY:
325                 if (G.qual == 0){
326                         ima_select_all(simasel);
327                         queredraw = 1;
328                 }
329                 break;
330
331         case IKEY:
332                 if ((G.qual == 0)&&(simasel->file)){
333                         sprintf(name, "$IMAGEEDITOR %s%s", simasel->dir, simasel->file);
334                         system(name);
335                         queredraw = 1;
336                 }
337
338                 break;
339         
340         case PKEY:
341                 if(G.qual & LR_SHIFTKEY) {
342                         extern char bprogname[];        /* usiblender.c */
343                         
344                         sprintf(name, "%s -a \"%s%s\"", bprogname, simasel->dir, simasel->file);
345                         system(name);
346                 }
347                 if(G.qual & LR_CTRLKEY) {
348                         if(bitset(simasel->fase, IMS_KNOW_IMA)) pibplay(simasel);
349                 }
350                 if (G.qual == 0){
351                         imadir_parent(simasel);
352                         checkdir(simasel->dir);
353                         clear_ima_dir(simasel);
354                         queredraw = 1;
355                 }
356                 break;
357                 
358         case RKEY:
359         case XKEY:
360                 if (simasel->hilite_ima){
361                         strcpy(name, simasel->dir);
362                         strcat(name, simasel->hilite_ima->file_name);
363
364                         if( okee("Remove %s", name) ) {
365                                 ret = BLI_delete(name, 0, 0);
366                                 if (ret) {
367                                         error("Command failed, see console");
368                                 } else {
369                                         clear_ima_dir(simasel);
370                                         queredraw = 1;
371                                 }
372                         }
373                 }
374                 break;
375
376         case PADPLUSKEY:
377         case EQUALKEY:
378                 BLI_newname(simasel->file, +1);
379                 queredraw = 1;
380                 break;
381                 
382         case PADMINUS:
383         case MINUSKEY:
384                 BLI_newname(simasel->file, -1);
385                 queredraw = 1;
386                 break;
387                 
388         case BACKSLASHKEY:
389         case SLASHKEY:
390 #ifdef WIN32
391                 strcpy(simasel->dir, "\\");
392 #else
393                 strcpy(simasel->dir, "/");
394 #endif
395                 clear_ima_dir(simasel);
396                 simasel->image_slider = 0.0;
397                 queredraw = 1;
398                 break;
399                 
400         case PERIODKEY:
401                 clear_ima_dir(simasel);
402                 queredraw = 1;
403                 break;
404         
405         case ESCKEY:
406                 filesel_prevspace();
407                 break;
408
409         case PADENTER:
410         case RETKEY:
411                 if (simasel->returnfunc){
412                         strcpy(name, simasel->dir);
413                         strcat(name, simasel->file);
414                         simasel->returnfunc(name);
415                         filesel_prevspace();
416                 }
417                 break;
418         }
419         
420                 
421         if (queredraw) scrarea_queue_winredraw(curarea);
422 }
423
424
425 void clever_numbuts_imasel()
426 {
427         SpaceImaSel *simasel;
428         static char orgname[FILE_MAXDIR+FILE_MAXFILE+12];
429         static char filename[FILE_MAXDIR+FILE_MAXFILE+12];
430         static char newname[FILE_MAXDIR+FILE_MAXFILE+12];
431         int len;
432         
433         simasel= curarea->spacedata.first;
434         len = 110;
435
436         if (simasel->hilite_ima){
437                 BLI_make_file_string(G.sce, orgname, simasel->dir, simasel->hilite_ima->file_name);
438                 strcpy(filename, simasel->hilite_ima->file_name);
439
440                 add_numbut(0, TEX, "", 0, len, filename, "Rename Image");
441                 if( do_clever_numbuts("Rename Image", 1, REDRAW) ) {
442                         BLI_make_file_string(G.sce, newname, simasel->dir, filename);
443
444                         if( strcmp(orgname, newname) != 0 ) {
445                                 BLI_rename(orgname, newname);
446
447                                 clear_ima_dir(simasel);
448                         }
449                 }
450
451                 scrarea_queue_winredraw(curarea);
452         }
453 }