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