added iff, lbm and bmp filetypes to the fileselect. added loading of bmp images,
[blender.git] / source / blender / src / drawimasel.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 #define OLD_IMASEL      0
34
35 #include <string.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifdef _WIN32
42 #include "BLI_winstuff.h"
43 #pragma warning (once : 4761)
44 #endif
45
46 #include "MEM_guardedalloc.h"
47 #include "BMF_Api.h"
48 #include "BLI_blenlib.h"
49 #include "IMB_imbuf_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "BKE_global.h"
53 #include "BIF_fsmenu.h"
54 #include "BIF_gl.h"
55 #include "BIF_resources.h"
56 #include "BIF_screen.h"
57 #include "BIF_interface.h"
58 #include "BIF_imasel.h"
59 #include "BIF_mywindow.h"
60 #include "BIF_space.h"
61 #include "BSE_drawimasel.h"
62 #include "BSE_filesel.h"
63
64 #include "interface.h"
65 #include "blendef.h"
66 #include "mydevice.h"
67
68 #define IMALINESIZE 16
69 /* well, who would have thought ... */
70 #define lrectwrite(a, b, c, d, rect)    {glRasterPos2i(a,  b);glDrawPixels((c)-(a)+1, (d)-(b)+1, GL_RGBA, GL_UNSIGNED_BYTE,  rect);}
71
72 /* GLOBALS */
73 extern char *fsmenu;
74
75 void rectwrite_imasel(int winxmin, int winymin, int winxmax, int winymax, int x1, int y1, int xim, int yim, float zoomx, float zoomy, unsigned int *rect)
76 {
77         int cx, cy, oldxim, x2, y2;
78         
79         oldxim= xim;
80                 
81         /* coordinaten hoe 't op scherm komt */
82         x2= x1+ zoomx*xim;
83         y2= y1+ zoomy*yim;
84
85         /* partiele clip */
86         if(x1<=winxmin) {
87                 /* recten bij OpenGL mogen niet links/onder van windowrand beginnen */
88                 cx= winxmin-x1+(int)zoomx;
89                 /* zorg ervoor dat de rect pixelnauwkeurig wordt neergezet */
90                 cx/= zoomx;
91                 cx++;
92                 x1+= zoomx*cx;
93                 xim-= cx;
94                 rect+= cx;
95         }
96         if(y1<=winymin) {
97                 cy= winymin-y1+(int)zoomy;
98                 cy/= zoomy;
99                 cy++;
100                 y1+= zoomy*cy;
101                 rect+= cy*oldxim;
102                 yim-= cy;
103         }
104         if(x2>=winxmax) {
105                 cx= x2-winxmax;
106                 cx/= zoomx;
107                 xim-= cx+3;
108         }
109         if(y2>=winymax) {
110                 cy= y2-winymax;
111                 cy/= zoomy;
112                 yim-= cy+3;
113         }
114         
115         if(xim<=0) return;
116         if(yim<=0) return;
117
118 //      mywinset(curarea->win);
119         glScissor(winxmin, winymin, winxmax-winxmin+1, winymax-winymin+1);
120         
121         glPixelStorei(GL_UNPACK_ROW_LENGTH,  oldxim);
122         
123         glPixelZoom(zoomx,  zoomy);
124
125         glRasterPos2i(x1, y1);
126         glDrawPixels(xim, yim, GL_RGBA, GL_UNSIGNED_BYTE,  rect);
127
128         glPixelZoom(1.0,  1.0);
129
130         glPixelStorei(GL_UNPACK_ROW_LENGTH,  0);
131 }
132
133 void viewgate(short sx, short sy, short ex, short ey) 
134 {
135         short wx, wy;
136         wx = curarea->winrct.xmin; wy = curarea->winrct.ymin;
137         glViewport(wx+sx, wy+sy, (wx+ex )-(wx+sx)+1, (wy+ey )-(wy+sy)+1); 
138         glScissor(wx+sx, wy+sy, (wx+ex )-(wx+sx)+1, (wy+ey )-(wy+sy)+1);
139         myortho2((float)sx+0.5 , (float)ex+0.5, (float)sy+0.5, (float)ey+0.5);
140 }       
141         
142 void areaview (void) 
143 {
144         short wx, wy;
145         wx = curarea->winrct.xmin; wy = curarea->winrct.ymin;
146         glViewport(wx,  wy, curarea->winx, curarea->winy); 
147         glScissor(wx,  wy, curarea->winx, curarea->winy); 
148         myortho2(-0.5, (float)(curarea->winx)-0.5, -0.5, (float)(curarea->winy)-0.5);
149
150 }
151                 
152 void calc_hilite(SpaceImaSel *simasel)
153 {
154         OneSelectableIma *ima;
155         ImaDir  *direntry;
156         short   mx, my;
157         int     i, area_event;
158         
159         if (simasel->hilite > -1) {
160                 direntry = simasel->firstdir; 
161                 while(direntry){
162                         direntry->hilite = 0;
163                         direntry = direntry->next;
164                 }       
165                 simasel->hilite = -1;
166         }
167         
168         if (simasel->totalima){
169                 simasel->hilite_ima =  0;
170                 ima = simasel->first_sel_ima;
171                 while (ima){
172                         ima->selectable = 0;
173                         ima = ima->next;
174                 }
175         }
176                 
177         area_event = 0;
178         mx = simasel->mx;
179         my = simasel->my;
180                 
181         if (simasel->desx > 0){
182                 if ( (mx > simasel->desx) && (mx < simasel->deex) && (my > simasel->desy) && (my < simasel->deey) ) area_event = IMS_INDIR;
183         }
184         if (simasel->fesx > 0){
185                 if ( (mx > simasel->fesx) && (mx < simasel->feex) && (my > simasel->fesy) && (my < simasel->feey) ) area_event = IMS_INFILE;
186         }       
187         
188         switch(area_event){
189         case IMS_INDIR:
190                 simasel->hilite = simasel->topdir + ((simasel->deey - my - 4) / IMALINESIZE);
191                 
192                 if (my >= simasel->deey)                                        simasel->hilite = -1;
193                 if (simasel->hilite >= simasel->totaldirs)  simasel->hilite = -1;
194         
195                 if (simasel->hilite > -1){
196                         direntry = simasel->firstdir; 
197                         for (i = simasel->hilite; i>0; i--){
198                                 direntry = direntry->next;
199                         }
200                         direntry->hilite = 1;
201                         
202                 }
203                 simasel->mouse_move_redraw = 1;
204                 break;
205         
206         case IMS_INFILE:
207                 if (simasel->totalima){
208                         ima = simasel->first_sel_ima;
209                         while (ima){
210                                 ima->selectable = 0;
211                                 
212                                 if (ima->draw_me) {
213                                         if ((mx > ima->sx) && (mx < ima->sx+76) && (my > ima->sy-16) && (my < ima->sy+76)) {
214                                                 ima->selectable = 1;
215                                                 simasel->hilite_ima = ima;
216                                                 simasel->mouse_move_redraw = 1;
217                                         }
218                                 }
219                                 
220                                 ima = ima->next;
221                         }       
222                 }
223                 break;
224         }
225 }
226
227
228 void make_sima_area(SpaceImaSel *simasel)
229 {
230         OneSelectableIma *ima;
231         short rh, dm, sc;
232         short boxperline, boxlines, boxlinesinview, boxlinesleft;
233
234 /*  ima slider  box */
235         simasel->fssx =  8; 
236         simasel->fssy = 8; 
237         simasel->fsex = 30; 
238         simasel->fsey = curarea->winy-64;
239 /*  ima entry's  box */
240         simasel->fesx = simasel->fsex + 8; 
241         simasel->fesy = simasel->fssy; 
242         simasel->feex = curarea->winx- 8;  
243         simasel->feey = curarea->winy-64;
244 /*  ima names */
245         simasel->dnsx = 38; 
246         simasel->dnsy = curarea->winy - 29; 
247         simasel->dnw  = curarea->winx - 8 - 38; 
248         simasel->dnh  = 21;
249         simasel->fnsx = simasel->fesx; 
250         simasel->fnsy = curarea->winy - 29 - 29; 
251         simasel->fnw  = curarea->winx - 8 - simasel->fnsx; 
252         simasel->fnh  = 21;
253         
254         if ((simasel->mode & 1)==1){
255         /*  dir slider  box */
256                 simasel->dssx =   8; 
257                 
258                 simasel->dsex =  30; 
259                 simasel->dsey = curarea->winy-64;
260         /*  dir entry's  box */
261                 simasel->desx =  38;
262                 simasel->desy =   8; 
263                 
264                 simasel->deex = 208; 
265                 simasel->deey = curarea->winy-64;
266                 simasel->dssy = simasel->desy; 
267                 if (simasel->deex > (curarea->winx -8) ) simasel->deex = curarea->winx - 8;
268                 if (simasel->deex <= simasel->desx ) simasel->dssx =  0;
269         /*  file slider & entry & name box ++ */
270                 simasel->fssx += 216; 
271                 simasel->fsex += 216;
272                 simasel->fesx += 216;
273                 simasel->fnsx += 216;
274                 simasel->fnw  -= 216;
275         }else{
276                 simasel->desx =  0;
277         }
278         
279         if ((simasel->mode & 2) == 2){  
280                 simasel->fesy += 32;
281                 simasel->infsx = simasel->fesx; simasel->infsy =  8;
282                 simasel->infex = simasel->feex; simasel->infey = 28;
283         }else{
284                 simasel->infsx = 0;
285         }
286         
287         simasel->dsdh = simasel->deey - simasel->desy - 4;
288         
289         if (simasel->dsdh  <= 16)  { simasel->desx  = 0; }
290         if ((simasel->feex-16)  <= simasel->fesx)  { simasel->fesx  = 0; }
291         if ((simasel->infex-16) <= simasel->infsx) { simasel->infsx = 0; }
292         
293         if ((simasel->deey  ) <= simasel->desy)    { simasel->desx  = 0; }
294         if ((simasel->feey  ) <= simasel->fesy)    { simasel->fesx  = 0; }
295         if ((simasel->infey )  > simasel->feey)    { simasel->infsx  = 0;}
296         
297         /* Dir Slider */
298         if (simasel->desx  != 0){
299                 simasel->dirsli = 0;
300                 
301                 simasel->dirsli_lines = (simasel->dsdh / IMALINESIZE);
302                 simasel->dirsli_h  = 0;
303                 
304                 if (simasel->topdir < 0) simasel->topdir = 0;
305                 if (simasel->topdir > (simasel->totaldirs - simasel->dirsli_lines) ) simasel->topdir = (simasel->totaldirs - simasel->dirsli_lines);
306                 
307                 if ( (simasel->totaldirs * IMALINESIZE) >= simasel->dsdh ){
308                         simasel->dirsli = 1;
309                         simasel->dirsli_sx = simasel->dssx+2;
310                         simasel->dirsli_ex = simasel->dsex-2;   
311                         
312                         simasel->dirsli_h   = (simasel->dsdh) * (float)simasel->dirsli_lines / (float)simasel->totaldirs;
313                         simasel->dirsli_ey  = simasel->dsey - 2;
314                         if (simasel->topdir) {
315                                 rh = (simasel->dsdh - simasel->dirsli_h);
316                                 simasel->dirsli_ey -= rh * (float)((float)simasel->topdir / (float)(simasel->totaldirs - simasel->dirsli_lines ));
317                         }
318                         
319                         if (simasel->dirsli_h < 4) simasel->dirsli_h = 4;
320                         
321                 }else{
322                         simasel->topdir = 0;
323                 }
324         }
325         
326         if (simasel->totalima){
327                 /* there are images */
328                 
329                 ima = simasel->first_sel_ima;
330                 
331                 
332                 boxperline      =   (simasel->feex - simasel->fesx) /  80;
333                 if (boxperline) boxlines = 1 + (simasel->totalima / boxperline); else boxlines = 1;
334                 boxlinesinview  =   (simasel->feey - simasel->fesy) / 100;
335                 boxlinesleft    =   boxlines - boxlinesinview;
336                 
337                 if (boxlinesleft > 0){
338                         /* slider needed */
339                         
340                         simasel->slider_height = boxlinesinview / (float)(boxlines+1);
341                         simasel->slider_space  = 1.0 - simasel->slider_height;
342                         
343                         simasel->imasli_sx = simasel->fssx+1; 
344                         simasel->imasli_ex = simasel->fsex-1; 
345                         simasel->fsdh      = simasel->fsey -  simasel->fssy - 4;
346                 
347                         simasel->imasli_h  = simasel->fsdh * simasel->slider_height;
348                         if (simasel->imasli_h < 6) simasel->imasli_h = 6;
349                         simasel->imasli_ey = simasel->fsey - 2 - (simasel->fsdh * simasel->slider_space * simasel->image_slider);
350                         
351                         simasel->imasli = 1;
352                 
353                 }else{
354                         simasel->image_slider = 0;
355                         simasel->imasli = 0;
356                 }
357                 
358                 sc = simasel->image_slider * (boxlinesleft * 100);
359                 
360                 simasel->curimax = simasel->fesx + 8;
361                 simasel->curimay = simasel->feey - 90 + sc;
362                 
363                 dm = 1;
364                 if (simasel->curimay-2  < simasel->fesy) dm = 0;
365                 // let first row of icons remain selectable
366                 if(OLD_IMASEL) if (simasel->curimay+80 > simasel->feey) dm = 0;
367                 if (simasel->curimax+72 > simasel->feex) dm = 0;
368                 
369                 simasel->total_selected = 0;
370                 while (ima){
371                         ima->draw_me = dm;
372                         
373                         if (ima->selected) simasel->total_selected++;
374         
375                         ima->sx = simasel->curimax;
376                         ima->sy = simasel->curimay+16;
377                         
378                         ima->ex = ima->sx + ima->dw;
379                         ima->ey = ima->sy + ima->dh;
380                         
381                         simasel->curimax += 80;
382                         if (simasel->curimax + 72 > simasel->feex){
383                                 
384                                 simasel->curimax  = simasel->fesx + 8;
385                                 simasel->curimay -= 100;
386                                 
387                                 dm = 1;
388                                 // let icons that fall off (top/bottom) be selectable
389                                 if(OLD_IMASEL) {
390                                         if (simasel->curimay+80 > simasel->feey) dm = 0;
391                                         if (simasel->curimay-8  < simasel->fesy) dm = 0;
392                                 }
393                                 
394                         }
395                         ima = ima->next;
396                 }
397         }
398 }
399
400 static void str_image_type(int ftype, char *name)
401 {
402         strcpy(name, "");
403         
404         if((ftype & JPG_MSK) == JPG_STD) strcat(name, "std ");
405         if((ftype & JPG_MSK) == JPG_VID) strcat(name, "video ");
406         if((ftype & JPG_MSK) == JPG_JST) strcat(name, "amiga ");
407         if((ftype & JPG_MSK) == JPG_MAX) strcat(name, "max ");
408         
409         if( ftype == AN_hamx)   { strcat(name, "hamx "); return; }
410         
411         if( ftype == IMAGIC )   { strcat(name, "sgi ");  return; }
412         if( ftype & JPG )       { strcat(name, "jpeg ");  }
413         if( ftype & TGA )       { strcat(name, "targa "); }
414         if( ftype & PNG )       { strcat(name, "png "); }
415         if( ftype & BMP )       { strcat(name, "bmp "); }
416         if( ftype & AMI )       { strcat(name, "iff ");   }
417 }
418
419 void draw_sima_area(SpaceImaSel *simasel)
420 {               
421         uiBlock *block;
422         OneSelectableIma *ima;
423         ImaDir      *direntry;
424         int   i, info;
425         short sx, sy, ex, ey, sc;
426         char naam[256], infostr[256];
427         
428         glClearColor(0.4375, 0.4375, 0.4375, 0.0);
429         glClear(GL_COLOR_BUFFER_BIT);
430         
431         sprintf(naam, "win %d", curarea->win);
432         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSSF, UI_HELV, curarea->win);
433         uiBlockSetCol(block, BUTBLUE);
434         
435         if (simasel->desx >  0){
436                 /*  DIR ENTRYS */
437                 cpack(C_DERK);
438                 glRecti(simasel->dssx,  simasel->dssy,  simasel->dsex,  simasel->dsey);
439                 glRecti(simasel->desx,  simasel->desy,  simasel->deex,  simasel->deey);
440         
441                 uiEmboss(simasel->dssx, simasel->dssy, simasel->dsex, simasel->dsey,1);
442                 uiEmboss(simasel->desx, simasel->desy, simasel->deex, simasel->deey,1);
443                 
444                 if (simasel->dirsli == 1) {
445                         sx = simasel->dirsli_sx+2;
446                         sy = simasel->dirsli_ey - simasel->dirsli_h+2;
447                         ex = simasel->dirsli_ex-2;
448                         ey = simasel->dirsli_ey-2;
449                         
450                         cpack(C_BACK);
451                         
452                         glRecti(sx,  sy,  ex,  ey);
453                         uiEmboss(sx, sy, ex,ey,0);
454                 }
455                 if (simasel->totaldirs) {
456                         sx = simasel->desx+8;
457                         sy = simasel->deey-IMALINESIZE;
458                         
459                         direntry = simasel->firstdir; 
460                         if (simasel->topdir){
461                                 for(i = simasel->topdir; i>0; i--){
462                                         direntry = direntry->next;
463                                 }
464                         }
465                         viewgate(simasel->desx, simasel->desy, simasel->deex-4, simasel->deey);
466                         
467                         i = simasel->dirsli_lines;
468                         if (i > simasel->totaldirs) i = simasel->totaldirs;
469                         for(;i > 0; i--){
470                                 strcpy(naam,  direntry->name);
471                                 
472                                 cpack(0xFFFFFF);
473                                 if (direntry->selected == 1){
474                                         cpack(0x7777CC);
475                                         glRecti(simasel->desx+2,  sy-4,  simasel->deex-4,  sy+IMALINESIZE-4);
476                                         cpack(0xFFFFFF);
477                                 }
478                                 if (direntry->hilite == 1){
479                                         cpack(0x999999);
480                                         glRecti(simasel->desx+2,  sy-4,  simasel->deex-4,  sy+IMALINESIZE-4);
481                                         cpack(0xFFFFFF);
482                                 }
483                                 
484                                 glRasterPos2i(sx,  sy);
485                                 BMF_DrawString(G.font, naam);
486                                 
487                                 direntry = direntry->next;
488                                 sy-=IMALINESIZE;
489                         }
490                         areaview();
491                         
492                 }
493                 
494                 /* status icons */
495                 
496                 sx = simasel->desx;
497                 sy = simasel->deey+6;
498                 
499                 glRasterPos2f(sx+16*0, sy);
500                 if (bitset(simasel->fase, IMS_FOUND_BIP)) {
501                         BIF_draw_icon(ICON_BPIBFOLDER_HLT);
502                 } else if (bitset(simasel->fase, IMS_WRITE_NO_BIP)) {
503                         BIF_draw_icon(ICON_BPIBFOLDER_DEHLT);
504                 } else {
505                         BIF_draw_icon(ICON_BPIBFOLDER_DEHLT);
506                 }
507
508                 glRasterPos2f(sx+16*1, sy);
509                 if (bitset(simasel->fase, IMS_KNOW_INF)) {
510                         BIF_draw_icon(ICON_FOLDER_HLT);
511                 } else {
512                         BIF_draw_icon(ICON_FOLDER_DEHLT);
513                 }
514                 
515                 glRasterPos2f(sx+16*2, sy);
516                 if (bitset(simasel->fase, IMS_KNOW_IMA)) {
517                         BIF_draw_icon(ICON_BLUEIMAGE_HLT);
518                 } else {
519                         BIF_draw_icon(ICON_BLUEIMAGE_DEHLT);
520                 }
521         }
522         
523         if (simasel->fesx >  0) {
524                 int extrabutsize;
525                 
526                 cpack(C_DARK);
527
528                 glRecti(simasel->fssx,  simasel->fssy,  simasel->fsex,  simasel->fsey);
529
530                 glRecti(simasel->fesx,  simasel->fesy,  simasel->feex,  simasel->feey);
531
532                 uiEmboss(simasel->fssx-1, simasel->fssy-1, simasel->fsex+1, simasel->fsey+1,1);
533                 uiEmboss(simasel->fesx-1, simasel->fesy-1, simasel->feex+1, simasel->feey+1,1);
534                 
535                 if (simasel->imasli == 1){
536                         sx = simasel->imasli_sx;
537                         sy = simasel->imasli_ey - simasel->imasli_h;
538                         ex = simasel->imasli_ex;
539                         ey = simasel->imasli_ey;
540                         
541                         cpack(C_BACK);
542
543                         glRecti(sx,  sy,  ex,  ey);
544                         uiEmboss(sx, sy, ex, ey, 1);
545                 }
546         
547                 info = 0;
548                 strcpy(infostr, "");
549                 if (simasel->totalima){
550                         viewgate(simasel->fesx, simasel->fesy, simasel->feex, simasel->feey);
551                 
552                         ima = simasel->first_sel_ima;
553                         
554                         while (ima){
555                                 sc = 0;
556                                 
557                                 sx = ima->sx- 6; sy = ima->sy-20 + sc; 
558                                 ex = ima->sx+71; ey = ima->sy+70 + sc;  
559                                 
560                                 if(ima->selected == 1){
561                                         cpack(0xCC6666);
562                                         glRecti(sx, sy,  ex, ey);
563                                 }
564                                 if(ima->selectable == 1){
565                                         if (ima->selected ) cpack(0xEE8888); else cpack(0x999999);
566                                         
567                                         if (((simasel->mode & 8) != 8) && (simasel->hilite_ima == ima)){
568                                                 glRecti(sx, sy,  ex, ey); uiEmboss(sx,sy, ex,ey, 1);
569                                         }
570                                         if (ima->disksize/1000 > 1000){ sprintf(infostr,  "%s  %.2fMb  x%i y%i  %i bits ",ima->file_name,(ima->disksize/1024)/1024.0, ima->orgx, ima->orgy, ima->orgd);
571                                         }else{ sprintf(infostr,  "%s  %dKb  x%i y%i  %i bits ",  ima->file_name,ima->disksize/1024,          ima->orgx, ima->orgy, ima->orgd);
572                                         }       
573                                         if (ima->anim == 1){ strcat (infostr, "movie"); }else{
574                                                 str_image_type(ima->ibuf_type, naam);
575                                                 strcat (infostr, naam);
576                                         }
577                                         info = 1;
578                                 }
579                                 
580                                 sx = ima->sx; sy = ima->sy + sc;
581                                 ex = ima->ex; ey = ima->ey + sc;
582                                 
583                                 if (ima->anim == 0) cpack(C_DARK); else cpack(C_DERK);
584                                 
585                                 glRecti(sx, sy,  ex, ey);
586                                 uiEmboss(sx-1,sy-1, ex+1,ey+1, 1);
587                                 
588                                 cpack(0);
589                                 strcpy(naam, ima->file_name);
590                                 naam[11] = 0;
591                         
592                                 glRasterPos2i(sx+32-BMF_GetStringWidth(G.fonts, naam) / 2  ,  sy-16);
593                                 BMF_DrawString(G.fonts, naam);
594                                 
595                                 if ((ima) && (ima->pict) && (ima->pict->rect)){
596                                         if ( (ey > simasel->fesy) && (sy < simasel->feey)){
597                                                 if(OLD_IMASEL) {
598                                                         lrectwrite(sx, sy, ex-1, ey-1, ima->pict->rect);
599                                                 } else
600                                                         rectwrite_imasel(simasel->fesx, simasel->fesy,
601                                                                 curarea->winrct.xmax, curarea->winrct.ymax - 64, //simasel->feey*1.5,
602                                                                 sx, sy, ima->pict->x, ima->pict->y, 1.0, 1.0, ima->pict->rect);
603                                         }
604                                 }
605                         
606                                 ima = ima->next;
607                         }
608                         
609                         if ((simasel->mode & 8) == 8) {  /* if loep */
610                                 
611                                 if (bitset(simasel->fase, IMS_KNOW_IMA) && (simasel->hilite_ima)) {
612                         
613                                         ima = simasel->hilite_ima;
614                                         glPixelZoom(2.0,  2.0);
615                                         
616                                         sx = ima->sx + (ima->ex - ima->sx)/2 - (ima->ex - ima->sx);
617                                         sy = ima->sy + (ima->ey - ima->sy)/2 - (ima->ey - ima->sy);
618                                         
619                                         ex = sx + 2*(ima->ex - ima->sx);
620                                         ey = sy + 2*(ima->ey - ima->sy);
621                                         
622                                         /* cpack(C_DERK); */
623                                         /* uiEmboss(sx-8,sy-8, ex+8,ey+8, 1); */
624                                         /* glRecti(sx-7, sy-7,  ex+7, ey+7); */
625                                         uiEmboss(sx-1,sy-1, ex+1,ey+1, 0);
626                                 
627                                         if(OLD_IMASEL) {
628                                                 lrectwrite(sx, sy, sx+ (ima->ex - ima->sx)-1, sy+ (ima->ey - ima->sy)-1, ima->pict->rect);
629                                         } else
630                                                 rectwrite_imasel(simasel->fesx, simasel->fesy,
631                                                         curarea->winrct.xmax, curarea->winrct.ymax - 64, //simasel->feey*1.5,
632                                                         sx, sy, ima->pict->x, ima->pict->y, 2.0, 2.0, ima->pict->rect);
633                                         
634                                         glPixelZoom(1.0,  1.0);
635                                 }
636                         }
637                         areaview();  /*  reset viewgate */
638                 }
639                 
640                 
641                 /* INFO */
642                 if (simasel->infsx > 0){
643                         cpack(C_DARK);
644
645                         glRecti(simasel->infsx,  simasel->infsy,  simasel->infex,  simasel->infey);
646                         uiEmboss(simasel->infsx, simasel->infsy, simasel->infex, simasel->infey,1);
647                 
648                         if ((info)&&(strlen(infostr) > 0)){
649                                 
650                                 sx = curarea->winrct.xmin;
651                                 sy = curarea->winrct.ymin;
652                                 
653                                 viewgate(simasel->infsx, simasel->infsy, simasel->infex, simasel->infey);
654                         
655                                 cpack(0xAAAAAA);
656                                 glRasterPos2i(simasel->infsx+4,  simasel->infsy+6);
657                                 BMF_DrawString(G.font, infostr);
658                                 
659                                 areaview();  /*  reset viewgate */
660         
661                         }
662                 }
663
664                 extrabutsize= (simasel->returnfunc)?60:0;
665                 if (simasel->dnw > extrabutsize+8) {
666                         simasel->dnw-= extrabutsize;
667                         uiDefBut(block, TEX, 1,"", simasel->dnsx, simasel->dnsy, simasel->dnw, simasel->dnh, simasel->dir,  0.0, (float)FILE_MAXFILE-1, 0, 0, "");
668                         if (extrabutsize)
669                                 uiDefBut(block, BUT, 5, "Load", simasel->dnsx+simasel->dnw, simasel->dnsy, extrabutsize, 21, NULL, 0.0, 0.0, 0, 0, "Load the selected image");
670                 }
671                 if (simasel->fnw > extrabutsize+8) {
672                         simasel->fnw-= extrabutsize;
673                         uiDefBut(block, TEX, 2,"", simasel->fnsx, simasel->fnsy, simasel->fnw, simasel->fnh, simasel->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
674                         if (extrabutsize)
675                                 uiDefBut(block, BUT, 6, "Cancel",       simasel->fnsx+simasel->fnw, simasel->fnsy, extrabutsize, 21, NULL, 0.0, 0.0, 0, 0, "Cancel image loading");
676                 }
677         }
678         
679         if (curarea->winx > 16) {
680                 char *menu= fsmenu_build_menu();
681
682                 uiDefBut(block, BUT, 13, "P", 8, (short)(curarea->winy-29), 20, 21, 0, 0, 0, 0, 0, "");
683                 uiDefButS(block, MENU, 3 , menu, 8, (short)(curarea->winy-58), 20, 21, &simasel->fileselmenuitem, 0, 0, 0, 0, "");
684
685                 MEM_freeN(menu);
686         }
687         
688         uiDrawBlock(block);
689 }
690
691 void select_ima_files(SpaceImaSel *simasel)
692 {
693         short set_reset;
694         short mval[2], oval[2];
695         
696         set_reset =  1 - (simasel->hilite_ima->selected);
697         
698         getmouseco_areawin(mval);
699         oval[0] = mval[0] + 1;
700         
701         while(get_mbut()&R_MOUSE) { 
702                 getmouseco_areawin(mval);
703                 if ((oval[0] != mval[0]) || (oval[1] != mval[1])){
704                         simasel->mx = mval[0];
705                         simasel->my = mval[1];
706                         
707                         calc_hilite(simasel);
708                         
709                         if (simasel->hilite_ima){
710                                 simasel->hilite_ima->selected = set_reset;
711                                 scrarea_do_windraw(curarea);
712                                 screen_swapbuffers();   
713                         }
714                         oval[0] = mval[0];
715                         oval[1] = mval[1];
716                 }
717         }
718 }
719
720 void move_imadir_sli(SpaceImaSel *simasel)
721 {
722         
723         short mval[2], lval[2], fh;
724         float rh;
725         
726         getmouseco_areawin(mval);
727         
728         if ((mval[0] > simasel->dirsli_sx) && 
729             (mval[0] < simasel->dirsli_ex) && 
730                 (mval[1] > simasel->dirsli_ey - simasel->dirsli_h) &&
731                 (mval[1] < simasel->dirsli_ey) ){
732                 
733                 /*  extactly in the slider  */
734                 fh = simasel->dirsli_ey - mval[1];
735                 lval[1]=1;
736                 while(get_mbut()&L_MOUSE) { 
737                         getmouseco_areawin(mval);
738                         if (mval[1] != lval[1]){
739                                 
740                                 rh = (float)(simasel->dsey - mval[1] - fh - simasel->dssy) / (simasel->dsdh - simasel->dirsli_h);
741                                 
742                                 simasel->topdir = 1 + rh * (simasel->totaldirs - simasel->dirsli_lines);
743                                 
744                                 scrarea_do_windraw(curarea);
745                                 uiEmboss(simasel->dirsli_sx, simasel->dirsli_ey - simasel->dirsli_h, 
746                                                    simasel->dirsli_ex, simasel->dirsli_ey,1);
747                             screen_swapbuffers();
748                                 lval[1] = mval[1];
749                         }
750                 }       
751         }else{
752                 if (mval[1] < simasel->dirsli_ey - simasel->dirsli_h)
753                         simasel->topdir += (simasel->dirsli_lines - 1); 
754                 else
755                         simasel->topdir -= (simasel->dirsli_lines - 1); 
756                         
757                 while(get_mbut()&L_MOUSE) {  }
758         }
759 }
760
761 void move_imafile_sli(SpaceImaSel *simasel)
762 {
763         short mval[2], cmy, omy = 0;
764         short ssl, sdh, ssv;
765         
766         getmouseco_areawin(mval);
767         cmy = mval[1];
768         
769         if ((mval[0] > simasel->imasli_sx) && 
770             (mval[0] < simasel->imasli_ex) && 
771                 (mval[1] > simasel->imasli_ey - simasel->imasli_h) &&
772                 (mval[1] < simasel->imasli_ey) ){
773                 
774                 ssv = simasel->fsey - simasel->imasli_ey - 2;
775                 
776                 while(get_mbut() & L_MOUSE) { 
777                         getmouseco_areawin(mval);
778                         if (mval[1] != omy){
779                                 sdh = simasel->fsdh - simasel->imasli_h;
780                                 ssl = cmy -  mval[1] + ssv;
781                                 
782                                 if (ssl < 0)   { ssl = 0; }
783                                 if (ssl > sdh) { ssl = sdh; }
784                                 
785                                 simasel->image_slider = ssl / (float)sdh;
786                                 
787                                 scrarea_do_windraw(curarea);
788                                 uiEmboss(simasel->imasli_sx, simasel->imasli_ey - simasel->imasli_h, 
789                                                    simasel->imasli_ex, simasel->imasli_ey, 1);
790                            
791                                 screen_swapbuffers();
792                                 omy = mval[1];
793                         }
794                 }
795         }else{
796                 while(get_mbut() & L_MOUSE) {  }
797         }
798 }
799
800 void ima_select_all(SpaceImaSel *simasel)
801 {
802         OneSelectableIma *ima;
803         int reselect = 0;
804         
805         ima = simasel->first_sel_ima;
806         if (!ima) return;
807         
808         while(ima){
809                 if (ima->selected == 1) reselect = 1;
810                 ima = ima->next;
811         }
812         ima = simasel->first_sel_ima;
813         if (reselect == 1){
814                 while(ima){
815                         ima->selected = 0;
816                         ima = ima->next;
817                 }
818         }else{
819                 while(ima){
820                         ima->selected = 1;
821                         ima = ima->next;
822                 }
823         }
824 }
825
826 void pibplay(SpaceImaSel *simasel)
827 {
828         OneSelectableIma *ima;
829         int sx= 8, sy= 8;
830         
831         ima = simasel->first_sel_ima;
832         if (!ima) return ;
833         
834         sx = curarea->winrct.xmin + 8; 
835         sy = curarea->winrct.ymin + 8;
836         
837         while(!(get_mbut()&L_MOUSE)){
838                 scrarea_do_windraw(curarea);     
839                 
840                 lrectwrite(sx, sy, sx+ima->dw-1, sy+ima->dh-1, ima->pict->rect);
841                 
842                 ima = ima->next;
843                 if (!ima) ima = simasel->first_sel_ima;
844                 screen_swapbuffers();   
845         }
846 }
847
848
849
850 /* ************** hoofdtekenfunktie ************** */
851
852 void drawimasel()       /* hoofdtekenfunktie */
853 {
854         SpaceImaSel *simasel;
855         simasel= curarea->spacedata.first;
856         
857         /* ortho: xmin xmax, ymin, ymax! */
858         myortho2(-0.5, (float)(curarea->winx)-0.5, -0.5, (float)(curarea->winy)-0.5);
859         
860         if (simasel->fase == 0){
861                 checkdir(simasel->dir);
862                 clear_ima_dir(simasel);
863         }
864
865         if (!bitset(simasel->fase, IMS_KNOW_DIR)){
866                 if(simasel->firstdir)   free_ima_dir(simasel->firstdir);
867                 if(simasel->firstfile)  free_ima_dir(simasel->firstfile);
868                 simasel->firstdir   = 0;
869                 simasel->firstfile  = 0;
870         
871                 if (get_ima_dir(simasel->dir, IMS_DIR,  &simasel->totaldirs,  &simasel->firstdir) < 0){
872                         /* error */
873                         strcpy(simasel->dir, simasel->dor);
874                         get_ima_dir(simasel->dir, IMS_DIR,  &simasel->totaldirs,  &simasel->firstdir);
875                 }
876                 
877                 if (get_ima_dir(simasel->dir, IMS_FILE, &simasel->totalfiles, &simasel->firstfile) < 0){
878                         /* error */
879                         strcpy(simasel->file, simasel->fole);
880                         get_ima_dir(simasel->dir, IMS_FILE, &simasel->totalfiles, &simasel->firstfile);
881                 }
882                 
883                 simasel->topdir  = 0;
884                 simasel->topfile = 0;
885                 simasel->fase |= IMS_KNOW_DIR;
886                 
887                 check_for_pib(simasel);
888                 
889                 strcpy(simasel->fole, simasel->file);
890                 strcpy(simasel->dor,  simasel->dir);
891         }
892                 
893         if (!bitset(simasel->fase, IMS_FOUND_BIP)){
894                 /* Make the first Bip file ever in this directory */
895                 if ( !bitset(simasel->fase, IMS_KNOW_INF)){
896                         if (!bitset(simasel->fase, IMS_DOTHE_INF)){
897                                 if(simasel->first_sel_ima)      free_sel_ima(simasel->first_sel_ima);
898                                 simasel->first_sel_ima   = 0;
899                                 simasel->fase |= IMS_DOTHE_INF;
900                                 addafterqueue(curarea->win, AFTERIMASELIMA, 1);
901                         }
902                 }
903         }else{
904                 if (!bitset(simasel->fase, IMS_KNOW_BIP)){
905                         addafterqueue(curarea->win, AFTERPIBREAD, 1);
906                 }
907         }
908         
909         make_sima_area(simasel);
910         calc_hilite(simasel);
911         draw_sima_area(simasel);
912         
913         curarea->win_swap= WIN_BACK_OK;
914 }
915