]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/texwindow.cpp
* applied patch by StefanV (from mailinglist) that fixes an error in config.py (broke...
[xonotic/netradiant.git] / radiant / texwindow.cpp
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 //
23 // Texture Window
24 //
25 // Leonardo Zide (leo@lokigames.com)
26 //
27
28 /*!\todo
29 Clean up texture menu.
30 - Remove all global variables and use some objects instead.
31 - Create an interface for a plugin to add texture menu items.
32 - Make sure the interface is not dependent on gtk.
33 */
34
35 #ifdef _WIN32
36 //#include <gdk/win32/gdkwin32.h>
37 #include <gdk/gdkwin32.h>
38 #endif
39 #if defined (__linux__) || defined (__APPLE__)
40 #include <gdk/gdkx.h>
41 #include <dirent.h>
42 #endif
43 #include <gtk/gtk.h>
44 #include <assert.h>
45 #include <sys/stat.h>
46 #include "stdafx.h"
47 #include "texwindow.h"
48 #include "str.h"
49 #include "missing.h"
50 #include "texmanip.h"
51
52 #define TYP_MIPTEX      68
53 static unsigned tex_palette[256];
54
55 #define FONT_HEIGHT     10
56
57 //int           texture_mode = GL_NEAREST;
58 //int           texture_mode = GL_NEAREST_MIPMAP_NEAREST;
59 //int           texture_mode = GL_NEAREST_MIPMAP_LINEAR;
60 //int           texture_mode = GL_LINEAR;
61 //int           texture_mode = GL_LINEAR_MIPMAP_NEAREST;
62 int             texture_mode = GL_LINEAR_MIPMAP_LINEAR;
63
64 int g_nTextureOffset = 0;
65
66 // current active texture directory
67 //++timo FIXME: I'm not sure this is used anymore
68 char            texture_directory[128];
69 // if true, the texture window will only display in-use shaders
70 // if false, all the shaders in memory are displayed
71 qboolean g_bShowAllShaders;
72
73 bool g_bFilterEnabled = false;
74 CString g_strFilter;
75
76 // texture layout functions
77 // TTimo: now based on shaders
78 int     nActiveShadersCount;
79 int     nCurrentShader;
80 IShader*  pCurrentShader;
81 qtexture_t  *current_texture = NULL;
82 int     current_x, current_y, current_row;
83
84 // globals for textures
85 int     texture_nummenus;
86 char    texture_menunames[MAX_TEXTUREDIRS][128];
87
88 // the list of scripts/*.shader files we need to work with
89 // those are listed in shaderlist file
90 // FIXME TTimo I get the feeling that those would need to move to the shaders module
91 //   for now it's still more simple to just keep it here
92 GSList *l_shaderfiles = NULL;
93
94 void SelectTexture (int mx, int my, bool bShift, bool bFitScale=false);
95
96 void  Texture_MouseDown (int x, int y, int buttons);
97 void  Texture_MouseMoved (int x, int y, int buttons);
98
99 CPtrArray g_lstSkinCache;
100
101 // TTimo: modifed to add a qtexture_t, Texture_LoadSkin loads using the shader API / QERApp_TryTexture_ForName
102 // m_strName is a copy of qtex->name
103 struct SkinInfo
104 {
105   CString m_strName;
106   int m_nTextureBind;
107   qtexture_t *m_qtex;
108   SkinInfo(const char *pName, int n, qtexture_t *qtex)
109   {
110     m_strName = pName;
111     m_nTextureBind = n;
112     m_qtex = qtex;
113   };
114   SkinInfo(){};
115 };
116
117 // =============================================================================
118 // global functions
119
120 // gets active texture extension
121 //
122 // FIXME: fix this to be generic from project file
123 //
124 int GetTextureExtensionCount()
125 {
126   // hardcoded hack for png support
127   if (g_pGameDescription->mGameFile == "sof2.game")
128     return 3;
129   else
130     return 2;
131 }
132
133 const char* GetTextureExtension(int nIndex)
134 {
135   switch(nIndex)
136   {
137     case 0:
138       return "tga";
139       break;
140     case 1:
141       return "jpg";
142       break;
143     case 2:
144       return "png";
145       break;
146     default:
147       return NULL;
148   }
149 }
150
151 /*
152 ==============
153 Texture_InitPalette
154 ==============
155 */
156 void Texture_InitPalette (byte *pal)
157 {
158   int   r,g,b;
159   int   i;
160   int   inf;
161   byte  gammatable[256];
162   float gamma;
163
164   gamma = g_qeglobals.d_savedinfo.fGamma;
165
166   if (gamma == 1.0)
167   {
168     for (i=0 ; i<256 ; i++)
169       gammatable[i] = i;
170   } else
171   {
172     for (i=0 ; i<256 ; i++)
173     {
174       inf = (int)( 255.0f * pow( ( i + 0.5f ) / 255.5f , gamma ) + 0.5f );
175       if (inf < 0)
176         inf = 0;
177       if (inf > 255)
178         inf = 255;
179       gammatable[i] = inf;
180     }
181   }
182
183   for (i=0 ; i<256 ; i++)
184   {
185     r = gammatable[pal[0]];
186     g = gammatable[pal[1]];
187     b = gammatable[pal[2]];
188     pal += 3;
189
190     //v = (r<<24) + (g<<16) + (b<<8) + 255;
191     //v = BigLong (v);
192
193     //tex_palette[i] = v;
194     tex_palette[i*3+0] = r;
195     tex_palette[i*3+1] = g;
196     tex_palette[i*3+2] = b;
197   }
198 }
199
200 void SetTexParameters (void)
201 {
202   qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture_mode );
203
204   switch ( texture_mode )
205   {
206   case GL_NEAREST:
207   case GL_NEAREST_MIPMAP_NEAREST:
208   case GL_NEAREST_MIPMAP_LINEAR:
209     qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
210     break;
211   case GL_LINEAR:
212   case GL_LINEAR_MIPMAP_NEAREST:
213   case GL_LINEAR_MIPMAP_LINEAR:
214     qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
215     break;
216   }
217 }
218
219 /*
220 ============
221 Texture_SetMode
222 ============
223 */
224 void Texture_SetMode(int iMenu)
225 {
226   int iMode;
227   qboolean texturing = true;
228   gpointer item = NULL;
229
230   switch (iMenu)
231   {
232   case ID_VIEW_NEAREST:
233     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_nearest");
234     iMode = GL_NEAREST;
235     break;
236   case ID_VIEW_NEARESTMIPMAP:
237     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_nearestmipmap");
238     iMode = GL_NEAREST_MIPMAP_NEAREST;
239     break;
240   case ID_VIEW_LINEAR:
241     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_linear");
242     iMode = GL_LINEAR;
243     break;
244   case ID_VIEW_BILINEAR:
245     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_bilinear");
246     iMode = GL_NEAREST_MIPMAP_LINEAR;
247     break;
248   case ID_VIEW_BILINEARMIPMAP:
249     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_bilinearmipmap");
250     iMode = GL_LINEAR_MIPMAP_NEAREST;
251     break;
252   case ID_VIEW_TRILINEAR:
253     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_trilinear");
254     iMode = GL_LINEAR_MIPMAP_LINEAR;
255     break;
256   case ID_TEXTURES_WIREFRAME:
257     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_wireframe");
258     iMode = -1;
259     texturing = false;
260     break;
261   case ID_TEXTURES_FLATSHADE:
262     item = g_object_get_data (G_OBJECT (g_pParentWnd->m_pWidget), "menu_view_flatshade");
263     iMode = -1;
264     texturing = false;
265     break;
266   default:
267     return;
268   }
269
270   g_qeglobals.d_savedinfo.iTexMenu = iMenu;
271   // NOTE: texture_mode is a GLenum used directly in glTexParameter
272   if(iMode!=-1) texture_mode = iMode;
273
274   g_bIgnoreCommands++;
275   if (item != NULL)
276     gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
277   g_bIgnoreCommands--;
278
279   if (texturing)
280     SetTexParameters ();
281
282   if ( !texturing && iMenu == ID_TEXTURES_WIREFRAME)
283   {
284     g_pParentWnd->GetCamWnd()->Camera()->draw_mode = cd_wire;
285     Map_BuildBrushData();
286     Sys_UpdateWindows (W_ALL);
287     return;
288   } else if ( !texturing && iMenu == ID_TEXTURES_FLATSHADE)
289   {
290     g_pParentWnd->GetCamWnd()->Camera()->draw_mode = cd_solid;
291     Map_BuildBrushData();
292     Sys_UpdateWindows (W_ALL);
293     return;
294   }
295
296   for (qtexture_t *q = g_qeglobals.d_qtextures; q; q = q->next)
297   {
298     qglBindTexture (GL_TEXTURE_2D, q->texture_number);
299     SetTexParameters ();
300   }
301
302   // select the default texture
303   qglBindTexture( GL_TEXTURE_2D, 0 );
304
305   qglFinish();
306
307   if (g_pParentWnd->GetCamWnd()->Camera()->draw_mode != cd_texture)
308   {
309     g_pParentWnd->GetCamWnd()->Camera()->draw_mode = cd_texture;
310     Map_BuildBrushData();
311   }
312
313   Sys_UpdateWindows (W_ALL);
314 }
315
316 /*!
317 gamma correction stuff
318 took out of QERApp_LoadTextureRGBA for clarity
319 */
320 byte g_gammatable[256];
321 void ResampleGamma(float fGamma)
322 {
323   int i,inf;
324   if (fGamma == 1.0)
325   {
326     for (i = 0; i < 256; i++)
327       g_gammatable[i] = i;
328   } else
329   {
330     for (i = 0; i < 256; i++)
331     {
332       inf = (int)( 255.0f * pow( (i + 0.5f) / 255.5f , fGamma ) + 0.5f );
333       if (inf < 0)
334         inf = 0;
335       if (inf > 255)
336         inf = 255;
337       g_gammatable[i] = inf;
338     }
339   }
340 }
341
342 /*!
343 this function does the actual processing of raw RGBA data into a GL texture
344 it will also generate the mipmaps
345 it looks like pPixels nWidth nHeight are the only relevant parameters
346 */
347 qtexture_t *QERApp_LoadTextureRGBA(unsigned char* pPixels, int nWidth, int nHeight)
348 {
349   static float fGamma = -1;
350   float total[3];
351   byte  *outpixels = NULL;
352   int   i, j, resampled, width2, height2, width3, height3;
353   int   max_tex_size = 0, mip = 0;
354   int   nCount = nWidth * nHeight;
355
356   if (fGamma != g_qeglobals.d_savedinfo.fGamma)
357   {
358     fGamma = g_qeglobals.d_savedinfo.fGamma;
359     ResampleGamma(fGamma);
360   }
361
362   qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);
363   if (!max_tex_size)
364     max_tex_size = 1024;
365
366   qtexture_t *q = (qtexture_t*)g_malloc(sizeof(*q));
367   q->width = nWidth;
368   q->height = nHeight;
369
370   total[0] = total[1] = total[2] = 0.0f;
371
372   // resample texture gamma according to user settings
373   for (i = 0; i < (nCount * 4); i += 4)
374   {
375     for (j = 0; j < 3; j++)
376     {
377       total[j] += (pPixels + i)[j];
378       byte b = (pPixels + i)[j];
379       (pPixels + i)[j] = g_gammatable[b];
380     }
381   }
382
383   q->color[0] = total[0] / (nCount * 255);
384   q->color[1] = total[1] / (nCount * 255);
385   q->color[2] = total[2] / (nCount * 255);
386
387   qglGenTextures (1, &q->texture_number);
388
389   qglBindTexture( GL_TEXTURE_2D, q->texture_number );
390
391   SetTexParameters();
392
393   width2 = 1; while (width2 < nWidth) width2 <<= 1;
394   height2 = 1; while (height2 < nHeight) height2 <<= 1;
395
396   width3 = width2;
397   height3 = height2;
398   while (width3 > max_tex_size) width3 >>= 1;
399   while (height3 > max_tex_size) height3 >>= 1;
400   if (width3 < 1) width3 = 1;
401   if (height3 < 1) height3 = 1;
402
403   if (!(width2 == nWidth && height2 == nHeight)) {
404     resampled = 1;
405     outpixels = (byte *)malloc(width2 * height2 * 4);
406     R_ResampleTexture(pPixels, nWidth, nHeight, outpixels, width2, height2, 4);
407   } else {
408     resampled = 0;
409     outpixels = pPixels;
410   }
411
412   while (width2 > width3 || height2 > height3)
413   {
414     GL_MipReduce(outpixels, outpixels, width2, height2, width3, height3);
415
416     if (width2 > width3)
417       width2 >>= 1;
418     if (height2 > height3)
419       height2 >>= 1;
420   }
421
422   qglTexImage2D(GL_TEXTURE_2D, mip++, g_qeglobals.texture_components, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, outpixels);
423   while (width2 > 1 || height2 > 1)
424   {
425     GL_MipReduce(outpixels, outpixels, width2, height2, 1, 1);
426
427     if (width2 > 1)
428       width2 >>= 1;
429     if (height2 > 1)
430       height2 >>= 1;
431
432     qglTexImage2D(GL_TEXTURE_2D, mip++, g_qeglobals.texture_components, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, outpixels);
433   }
434
435   qglBindTexture(GL_TEXTURE_2D, 0);
436   if (resampled)
437     free(outpixels);
438
439   return q;
440 }
441
442 /*
443 ==================
444 DumpUnreferencedShaders
445 usefull function: dumps the list of .shader files that are not referenced to the console
446 ==================
447 */
448 void DumpUnreferencedShaders()
449 {
450   GSList *lst, *sh, *files;
451   bool bFound = false;
452
453   files = vfsGetFileList ("scripts", "shader");
454   for (lst = files; lst; lst = lst->next)
455   {
456     bool listed = false;
457
458     for (sh = l_shaderfiles; sh != NULL; sh = g_slist_next (sh))
459       if (!strcmp ((char*)sh->data, (char*)lst->data))
460       {
461         listed = true;
462         break;
463       }
464
465     if (!listed)
466     {
467       if (!bFound)
468       {
469         bFound = true;
470         Sys_FPrintf (SYS_WRN, "Following shader files are not referenced in shaderlist.txt:\n");
471       }
472       Sys_FPrintf (SYS_WRN, "%s\n", (char*)lst->data);
473     }
474   }
475
476   vfsClearFileDirList (&files);
477 }
478
479 /*
480 ==================
481 BuildShaderList
482 build a CStringList of shader names
483 ==================
484 */
485 void BuildShaderList()
486 {
487   int count;
488   char filename[1024];
489   char *pBuff;
490   char dirstring[NAME_MAX];
491   int nLen;
492   if (l_shaderfiles!=NULL)
493   {
494     g_slist_free(l_shaderfiles);
495     l_shaderfiles = NULL;
496   }
497
498   if (g_pGameDescription->mGameFile != "hl.game")
499   {
500     strcpy(filename, g_pGameDescription->mShaderlist.GetBuffer());
501     count = vfsGetFileCount(filename, 0 );
502     if (count==0)
503     {
504       Sys_FPrintf(SYS_ERR, "Couldn't find '%s'\n", g_pGameDescription->mShaderlist.GetBuffer());
505       return;
506     }
507     // NOTE TTimo we use vfsGetFullPath solely to get the full path of the shader list we are gonna load
508     //   but we actually send the relative path to vfsLoadFile
509     //   so let's hope there is no disparity between the two functions
510     if (!vfsGetFullPath(filename, 0, 0))
511     {
512       Sys_FPrintf(SYS_ERR, "Couldn't find full path for '%s'\n", g_pGameDescription->mShaderlist.GetBuffer());
513       return;
514     }
515     Sys_Printf("Parsing shader files from %s\n", vfsGetFullPath(filename, 0, 0));
516     nLen = vfsLoadFile (filename, reinterpret_cast<void**>(&pBuff), 0);
517     if (nLen > 0)
518     {
519       StartTokenParsing(pBuff);
520       nLen = 0;
521       while (GetToken(true))
522       {
523         GSList *tmp;
524         bool found = false;
525
526         // each token should be a shader filename
527         sprintf(dirstring, "%s.shader", token);
528
529         for (tmp = l_shaderfiles; tmp != NULL; tmp = tmp->next)
530         {
531           if (!strcmp (dirstring, (char*)tmp->data))
532           {
533             found = true;
534             Sys_FPrintf(SYS_WRN, "duplicate entry \"%s\" in shaderlist.txt\n", (char*)tmp->data);
535             break;
536           }
537         }
538
539         if (!found)
540         {
541           l_shaderfiles = g_slist_append (l_shaderfiles, strdup (dirstring));
542           nLen++;
543         }
544       }
545       g_free(pBuff);
546     }
547   }
548 }
549
550 /*
551 ==================
552 FillTextureMenu
553
554 ==================
555 */
556 void ClearGSList (GSList* lst)
557 {
558   GSList *p = lst;
559   while (p)
560   {
561     free (p->data);
562     p = g_slist_remove (p, p->data);
563   }
564 }
565
566 void FillTextureMenu (GSList** pArray)
567 {
568   GtkWidget *menu, *sep, *item; // point to the Textures GtkMenu and to the last separator
569   GList *lst;
570   GSList *texdirs = NULL;
571   GSList *texdirs_tmp = NULL;
572   GSList *p;
573   char dirRoot[NAME_MAX];
574
575   // delete everything
576   menu = GTK_WIDGET (g_object_get_data (G_OBJECT (g_qeglobals_gui.d_main_window), "menu_textures"));
577   sep = GTK_WIDGET (g_object_get_data (G_OBJECT (g_qeglobals_gui.d_main_window), "menu_textures_separator"));
578   lst = g_list_find (gtk_container_children (GTK_CONTAINER (menu)), sep);
579   while (lst->next)
580   {
581     // these delete functions are recursive, it's gonna free all submenus
582     gtk_widget_destroy (GTK_WIDGET (lst->next->data));
583     // lst is no longer relevant, need to get it again
584     lst = g_list_find (gtk_container_children (GTK_CONTAINER (menu)), sep);
585   }
586
587   texture_nummenus = 0;
588
589   // add everything
590   if (!g_qeglobals.d_project_entity)
591     return;
592
593   // scan texture dirs and pak files only if not restricting to shaderlist
594   if (!g_PrefsDlg.m_bTexturesShaderlistOnly)
595   {
596     texdirs_tmp = vfsGetDirList ("textures/");
597     for (p=texdirs_tmp; p; p=g_slist_next(p))
598     {
599       // Hydra: erm, this didn't used to do anything except leak memory...
600       // For Halflife support this is required to work however.
601       // g_slist_append(texdirs, p->data);
602       texdirs = g_slist_append(texdirs, strdup((char *)p->data));
603     }
604     vfsClearFileDirList (&texdirs_tmp);
605   }
606
607   // scan the shaders in shaderlist.txt
608   BuildShaderList ();
609   PreloadShaders ();
610   DumpUnreferencedShaders ();
611   while (l_shaderfiles != NULL)
612   {
613     char shaderfile[PATH_MAX];
614     gboolean found = FALSE;
615
616     ExtractFileName ((char*)l_shaderfiles->data, shaderfile);
617     StripExtension (shaderfile);
618     g_strdown (shaderfile);
619
620     for (GSList *tmp = texdirs; tmp; tmp = g_slist_next (tmp))
621       if (!strcasecmp ((char*)tmp->data, shaderfile))
622       {
623               found = TRUE;
624               break;
625       }
626
627     if (!found)
628       texdirs = g_slist_prepend (texdirs, strdup (shaderfile));
629
630     free (l_shaderfiles->data);
631     l_shaderfiles = g_slist_remove (l_shaderfiles, l_shaderfiles->data);
632   }
633
634   // sort the list
635   texdirs = g_slist_sort (texdirs, (GCompareFunc)strcmp);
636
637   GSList *temp = texdirs;
638   while (temp)
639   {
640     char* ptr = strchr ((char*)temp->data, '_');
641
642     // do we shrink the menus?
643     if (ptr != NULL)
644     {
645       // extract the root
646       strcpy (dirRoot, (char*)temp->data);
647       dirRoot[ptr - (char*)temp->data + 1] = 0;
648
649       // we shrink only if we have at least two things to shrink :-)
650       if (temp->next && (strstr ((char*)temp->next->data, dirRoot) == (char*)temp->next->data))
651       {
652               GtkWidget *pSubMenu = gtk_menu_new ();
653         GtkWidget *pSubMenuRef = pSubMenu;
654               // keep going...
655               do
656               {
657                 item = gtk_menu_item_new_with_label ((char*)temp->data);
658                 gtk_widget_show (item);
659           CheckMenuSplitting (pSubMenu);
660                 gtk_container_add (GTK_CONTAINER (pSubMenu), item);
661                 gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (HandleCommand),
662                                     GINT_TO_POINTER (CMD_TEXTUREWAD+texture_nummenus));
663
664                 strcpy (texture_menunames[texture_nummenus], (char*)temp->data);
665                 strcat (texture_menunames[texture_nummenus], "/");
666                 if (pArray)
667                   *pArray = g_slist_append (*pArray, strdup ((char*)temp->data));
668                 if (++texture_nummenus == MAX_TEXTUREDIRS)
669                 {
670                   Sys_Printf("WARNING: max texture directories count has been reached!\n");
671                   // push submenu and get out
672                   item = gtk_menu_item_new_with_label (dirRoot);
673                   gtk_widget_show (item);
674                   gtk_container_add (GTK_CONTAINER (menu), item);
675                   gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), pSubMenu);
676                   ClearGSList (texdirs);
677                   return;
678                 }
679                 temp = temp->next;
680               }
681         while (temp && (strstr((char*)temp->data, dirRoot)==temp->data));
682
683         ptr = strchr (dirRoot, '_');
684         *ptr = 0;
685               item = gtk_menu_item_new_with_label (dirRoot);
686               gtk_widget_show (item);
687               CheckMenuSplitting (menu);
688               gtk_container_add (GTK_CONTAINER (menu), item);
689         gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), pSubMenuRef);
690               continue;
691       }
692     }
693
694     item = gtk_menu_item_new_with_label ((char*)temp->data);
695     gtk_widget_show (item);
696     CheckMenuSplitting (menu);
697     gtk_container_add (GTK_CONTAINER (menu), item);
698     gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (HandleCommand),
699                         GINT_TO_POINTER (CMD_TEXTUREWAD+texture_nummenus));
700
701     strcpy (texture_menunames[texture_nummenus], (char*)temp->data);
702     strcat (texture_menunames[texture_nummenus], "/");
703     if (pArray)
704       *pArray = g_slist_append (*pArray, strdup ((char*)temp->data));
705     if (++texture_nummenus == MAX_TEXTUREDIRS)
706     {
707       Sys_Printf("WARNING: max texture directories count has been reached!\n");
708       ClearGSList (texdirs);
709       return;
710     }
711
712     temp = temp->next;
713   }
714   ClearGSList (texdirs);
715 }
716
717 /*
718 ==============
719 Texture_ShowDirectory
720 relies on texture_directory global for the directory to use
721 called by
722   void Texture_ShowDirectory (int menunum, bool bLinked)
723   void  Texture_ShowDirectory (char* pPath, bool bLinked)
724 1) Load the shaders for the given directory
725 2) Scan the remaining texture, load them and assign them a default shader (the "noshader" shader)
726 NOTE: when writing a texture plugin, or some texture extensions, this function may need to be overriden, and made
727   available through the IShaders interface
728 NOTE: for texture window layout:
729   all shaders are stored with alphabetical order after load
730   previously loaded and displayed stuff is hidden, only in-use and newly loaded is shown
731   ( the GL textures are not flushed though)
732 ==============
733 */
734 void Texture_ShowDirectory ()
735 {
736   char  name[1024];
737   char  dirstring[1024];
738   CString strTemp;
739   int shaders_count = 0;
740   int textures_count = 0;
741   GSList *files = NULL, *temp;
742
743   g_bScreenUpdates = false;
744
745   // refresh the in-use textures: that will clear the IsDisplayed flag on unused stuff
746   // and leave it on in-use so they'll still be displayed
747   Texture_ShowInuse();
748   // and textures loaded in the following lines will be displayed as well...
749   // NOTE: shaders that are not in use but have been loaded previously are still in memory. But they don't get displayed.
750
751   g_qeglobals.d_texturewin.originy = 0;
752   // load texture_directory.shader
753   // NOTE: because of above call to Texture_ClearInuse, g_ActiveShaders will have the newly loaded shaders only
754   // we'll use that later to check if textures have a shader associated or not
755   // NOTE: all shaders loaded through QERApp_LoadShadersFromDir will get their InUse flag to True, we'll need a call to Texture_ShowInUse for later cleanup/adjustment
756   // NOTE: QERApp_LoadShadersFromDir has two criterions for loading a shader:
757   //   the shaderfile is texture_directory (like "museum" will load everything in museum.shader)
758   //   the shader name contains texture_directory (like "base_floor" will load museum.shader::base_floor/concfloor_rain)
759   shaders_count = QERApp_LoadShadersFromDir(texture_directory);
760   // load remaining texture files
761   // if a texture is already in use to represent a shader, ignore it
762
763   // need this function "GSList *lst SynapseServer::GetMinorList(char *major_name);"
764
765   sprintf (dirstring, "textures/%s", texture_directory);
766   g_ImageManager.BeginExtensionsScan();
767   const char* ext;
768   while((ext=g_ImageManager.GetNextExtension()) != NULL)
769   {
770     files = g_slist_concat(files, vfsGetFileList (dirstring, ext));
771   }
772
773   for (temp = files; temp; temp = temp->next)
774   {
775     sprintf(name, "%s%s", texture_directory, (char*)temp->data);
776
777       StripExtension (name);
778       strTemp = name;
779       strTemp.MakeLower();
780
781     if (strTemp.Find(".specular") >= 0 ||
782          strTemp.Find(".glow") >= 0 ||
783          strTemp.Find(".bump") >= 0 ||
784          strTemp.Find(".diffuse") >= 0 ||
785          strTemp.Find(".blend") >= 0 ||
786                strTemp.Find(".alpha") >= 0)
787          continue;
788
789     // avoid ever loading a texture name with spaces
790     if (strTemp.Find(" ") >= 0)
791     {
792       Sys_FPrintf(SYS_WRN, "WARNING: Skipping texture name with spaces [%s]\n", strTemp.GetBuffer());
793       continue;
794     }
795
796     // build a texture name that fits the conventions for qtexture_t::name
797     char stdName[1024];
798     sprintf( stdName, "textures/%s", name );
799     // check if this texture doesn't have a shader
800     if (!QERApp_ActiveShader_ForTextureName( stdName ))
801     {
802       QERApp_CreateShader_ForTextureName (stdName);
803       textures_count++;
804     }
805   }
806
807   Sys_Printf("Loaded %d shaders and created default shader for %d orphan textures.\n",
808              shaders_count, textures_count );
809
810   vfsClearFileDirList (&files);
811
812   // sort for displaying
813   QERApp_SortActiveShaders();
814
815   sprintf (name, "Textures: %s", texture_directory);
816   gtk_window_set_title (GTK_WINDOW (g_qeglobals_gui.d_entity), name);
817
818   // select the first texture in the list
819   if (!g_qeglobals.d_texturewin.texdef.GetName()[0])
820     SelectTexture (16, g_qeglobals.d_texturewin.height -16, false);
821
822   g_bScreenUpdates = true;
823
824   Sys_UpdateWindows (W_TEXTURE);
825 }
826
827 /*
828 ==============
829 Texture_ShowDirectory
830 1) Load the shaders for the given directory
831 2) Scan the remaining texture, load them and assign them a default shader (the "noshader" shader)
832 NOTE: when writing a texture plugin, or some texture extensions, this function may need to be overriden, and made
833   available through the IShaders interface
834 ==============
835 */
836 void Texture_ShowDirectory (int menunum)
837 {
838   strcpy (texture_directory, texture_menunames[menunum-CMD_TEXTUREWAD]);
839   Texture_ShowDirectory();
840 }
841
842 // scroll origin so the current texture is completely on screen
843 // if current texture is not displayed, nothing is changed
844 void Texture_ResetPosition()
845 {
846   qtexture_t  *q;
847   int  x,y;
848
849   //this shouldn't ever happen, we startup with notex
850   if (!g_qeglobals.d_texturewin.texdef.GetName()[0]) {
851     return;
852   }
853
854   // otherwise position with current texture shown
855   // this used to be in Texture_SetTexture
856   Texture_StartPos ();
857   while (1)
858   {
859     // NOTE: return value is == pCurrentShader and pCurrentShader->getTexture == current_texture
860     Texture_NextPos (&x, &y);
861     q = current_texture;
862     // if the current texture never found (because // 'show shaders' is off,
863     // for example), do nothing
864     if (!q)
865       break;
866
867     int nHeight = (int)(q->height * ((float)g_PrefsDlg.m_nTextureScale / 100));
868     // we have found when texdef->name and the shader name match
869     // NOTE: as everywhere else for our comparisons, we are not case sensitive
870     if (!strcmpi( g_qeglobals.d_texturewin.texdef.GetName(), pCurrentShader->getName() ))
871     {
872       // take care of calls before initialized
873       if ( !g_qeglobals.d_texturewin.height) {
874         g_qeglobals.d_texturewin.originy = 0;
875         break;
876       }
877       // if the bottom of our selected texture will fit with origin 0, use that
878       // to prevent scrolling uglyness (stuff scrolled off screen when
879       // everything would fit)
880       if ( -(y -nHeight-2*FONT_HEIGHT) <  g_qeglobals.d_texturewin.height) {
881         g_qeglobals.d_texturewin.originy = 0;
882         break;
883       }
884       // if current is off the top of the window, move it to the top
885       if (y > g_qeglobals.d_texturewin.originy)
886       {
887         g_qeglobals.d_texturewin.originy = y;
888         break;
889       }
890
891       // if current is off the bottom, put it on the bottom
892       if (y-nHeight-2*FONT_HEIGHT < g_qeglobals.d_texturewin.originy-g_qeglobals.d_texturewin.height)
893       {
894         g_qeglobals.d_texturewin.originy = y-nHeight-2*FONT_HEIGHT+g_qeglobals.d_texturewin.height;
895         break;
896       }
897       // if we made it here, it should already be in view
898       break;
899     }
900   }
901   Sys_UpdateWindows (W_TEXTURE);
902 }
903
904 /*
905 ==============
906 Texture_ShowAll
907 will set the IsDisplayed flag on all the active shaders, so we see everything that's currently in memory
908 ==============
909 */
910 void Texture_ShowAll()
911 {
912   char name[1024];
913
914 #ifdef _DEBUG
915   if (g_bShowAllShaders)
916     Sys_Printf("WARNING: already showing all shaders\n");
917 #endif
918   QERApp_ActiveShaders_SetDisplayed(true);
919   g_bShowAllShaders = true;
920   // put some information in the texture window title?
921   sprintf (name, "Textures: in use");
922   gtk_window_set_title (GTK_WINDOW (g_qeglobals_gui.d_entity), name);
923   Sys_UpdateWindows (W_TEXTURE);
924 }
925
926 /*
927 ==============
928 Texture_ShowInuse
929 clear all IsDisplayed flags
930 scan the map, set IsInUse (will set IsDisplayed on the way)
931 NOTE: don't sort the textures, don't update the windows (it's used in several contexts, not always necessary to do either)
932 ==============
933 */
934 void WINAPI Texture_ShowInuse (void)
935 {
936   face_t  *f;
937   brush_t *b;
938   char  name[1024];
939
940   g_qeglobals.d_texturewin.originy = 0;
941
942   // purge
943   QERApp_ActiveShaders_SetDisplayed(false);
944   // scan and only display in-use stuff
945   Sys_Status("Selecting active textures", 0);
946
947   for (b=active_brushes.next ; b != NULL && b != &active_brushes ; b=b->next)
948   {
949     if (b->patchBrush)
950     {
951       b->pPatch->pShader->SetInUse(true);
952     } else
953     {
954       for (f=b->brush_faces ; f ; f=f->next)
955       {
956         f->pShader->SetInUse(true);
957       }
958     }
959   }
960   for (b=selected_brushes.next ; b != NULL && b != &selected_brushes ; b=b->next)
961   {
962     if (b->patchBrush)
963     {
964       b->pPatch->pShader->SetInUse(true);
965     } else
966     {
967       for (f=b->brush_faces ; f ; f=f->next)
968       {
969         f->pShader->SetInUse(true);
970       }
971     }
972   }
973
974   // we are no longer showing everything
975   g_bShowAllShaders = false;
976   // put some information in the texture window title?
977   sprintf (name, "Textures: in use");
978   gtk_window_set_title (GTK_WINDOW (g_qeglobals_gui.d_entity), name);
979
980
981   // select the first texture in the list
982   if (!g_qeglobals.d_texturewin.texdef.GetName()[0])
983   {
984     SelectTexture (16, g_qeglobals.d_texturewin.height -16, false);
985   }
986 }
987
988 void Texture_ShowStartupShaders()
989 {
990   if (g_PrefsDlg.m_nShader == PrefsDlg::SHADER_COMMON)
991   {
992     // RIANT
993     // HACK FOR JK2 SUPPORT
994     if (g_pGameDescription->mGameFile == "jk2.game" || g_pGameDescription->mGameFile == "ja.game")
995     {
996       strcpy (texture_directory, "system/");
997     }
998     // RIANT
999     // HACK FOR SOF2 SUPPORT
1000     else if (g_pGameDescription->mGameFile == "sof2.game")
1001     {
1002       strcpy (texture_directory, "tools/");
1003     }
1004     else strcpy (texture_directory, "common/");
1005     Texture_ShowDirectory ();
1006   }
1007
1008   if (g_PrefsDlg.m_nShader == PrefsDlg::SHADER_ALL) {
1009     int    count;
1010     char   filename[1024];
1011     char   *pBuff;
1012     char   dirstring[NAME_MAX];
1013     int    nLen;
1014     GSList *shaderfiles = NULL;
1015
1016     strcpy(filename, g_pGameDescription->mShaderlist.GetBuffer());
1017     count = vfsGetFileCount(filename, 0);
1018     if (count == 0)
1019     {
1020       Sys_FPrintf(SYS_ERR, "Couldn't find '%s'\n", g_pGameDescription->mShaderlist.GetBuffer());
1021       return;
1022     }
1023
1024     if (!vfsGetFullPath(filename, 0, 0))
1025     {
1026       Sys_FPrintf(SYS_ERR, "Couldn't find full path for '%s'\n", g_pGameDescription->mShaderlist.GetBuffer());
1027       return;
1028     }
1029
1030     Sys_Printf("Parsing shader files from %s\n", vfsGetFullPath(filename, 0, 0));
1031     nLen = vfsLoadFile (filename, reinterpret_cast<void**>(&pBuff), 0);
1032     if (nLen > 0)
1033     {
1034       StartTokenParsing(pBuff);
1035       nLen = 0;
1036       while (GetToken(true))
1037       {
1038         GSList *tmp;
1039         bool found = false;
1040
1041         // each token should be a shader filename
1042         sprintf(dirstring, "%s.shader", token);
1043
1044         for (tmp = shaderfiles; tmp != NULL; tmp = tmp->next)
1045         {
1046           if (!strcmp (dirstring, (char*)tmp->data))
1047           {
1048             found = true;
1049             Sys_FPrintf(SYS_WRN, "duplicate entry \"%s\" in shaderlist.txt\n", (char*)tmp->data);
1050             break;
1051           }
1052         }
1053
1054         if (!found)
1055         {
1056           shaderfiles = g_slist_append (l_shaderfiles, strdup (dirstring));
1057           strcpy (texture_directory, dirstring);
1058           Texture_ShowDirectory ();
1059           nLen++;
1060         }
1061       }
1062       g_free(pBuff);
1063     }
1064   }
1065 }
1066
1067 /*
1068 ============================================================================
1069
1070 TEXTURE LAYOUT
1071
1072 TTimo: now based on a rundown through all the shaders
1073 nActiveShadersCount: number of shader that have a qtexture_t and may be displayed in the tex window
1074 nCurrentShader: index of active shader that has the current_texture
1075 pCurrentShader: IShader* for current shader
1076 NOTE: we expect the Active shaders count doesn't change during a Texture_StartPos .. Texture_NextPos cycle
1077   otherwise we may need to rely on a list instead of an array storage
1078 ============================================================================
1079 */
1080
1081 void Texture_StartPos (void)
1082 {
1083   //++timo TODO: check use of current_texture and current_row?
1084   current_x = 8;
1085   current_y = -8;
1086   current_row = 0;
1087   nActiveShadersCount = QERApp_GetActiveShaderCount();
1088   nCurrentShader = -1;
1089   current_texture = NULL;
1090   pCurrentShader = NULL;
1091 }
1092
1093 // if texture_showinuse jump over non in-use textures
1094 // it's not very clear what should be done here and what in Texture_Draw .. maybe merging the two would do good
1095 IShader* Texture_NextPos (int *x, int *y)
1096 {
1097   qtexture_t* q;
1098   while (1)
1099   {
1100     if (nCurrentShader >= nActiveShadersCount - 1)
1101     {
1102       // no more shaders
1103       current_texture = NULL;
1104       pCurrentShader = NULL;
1105       return NULL;
1106     }
1107     nCurrentShader++;
1108     pCurrentShader = QERApp_ActiveShader_ForIndex(nCurrentShader);
1109     if (pCurrentShader == NULL)
1110     {
1111       Sys_Printf("ERROR: unexpected pCurrentShader == NULL in Texture_NextPos\n");
1112       return NULL;
1113     }
1114     current_texture = pCurrentShader->getTexture();
1115     q = current_texture;
1116
1117     if (!q)
1118     {
1119       Sys_Printf("WARNING: found an IShader without qtexture_t in Texture_NextPos\n");
1120       return NULL;
1121     }
1122
1123     /*
1124     Never show anything other than "textures/" path,
1125     This is for q1/q2/q3 .map format, which expects "textures/" path on everything we apply
1126     */
1127     if(strncmp(pCurrentShader->getName(), "textures/", 9) != 0)
1128       continue;
1129
1130     // don't show shaders?
1131     if (!(g_PrefsDlg.m_bShowShaders || pCurrentShader->IsDefault()))
1132       continue;
1133
1134     if (g_PrefsDlg.m_bTextureWindow)
1135     {
1136       // some basic filtering
1137       if (!g_pParentWnd->GetTexWnd()->CheckFilter( pCurrentShader->getName() ))
1138         continue;
1139     }
1140
1141     //++timo FIXME: texture_showinuse is useless? with the menu and reload we just refresh the IsDisplayed flag
1142     // but the IsInUse is only relevant to draw the green outline
1143     if (pCurrentShader->IsDisplayed())
1144       break;
1145
1146     continue;
1147   }
1148
1149   int nWidth = (int)(q->width * ((float)g_PrefsDlg.m_nTextureScale / 100));
1150   int nHeight = (int)(q->height * ((float)g_PrefsDlg.m_nTextureScale / 100));
1151   if (current_x + nWidth > g_qeglobals.d_texturewin.width-8 && current_row)
1152   { // go to the next row unless the texture is the first on the row
1153     current_x = 8;
1154     current_y -= current_row + FONT_HEIGHT + 4;
1155     current_row = 0;
1156   }
1157
1158   *x = current_x;
1159   *y = current_y;
1160
1161   // Is our texture larger than the row? If so, grow the
1162   // row height to match it
1163
1164   if (current_row < nHeight)
1165     current_row = nHeight;
1166
1167   // never go less than 64, or the names get all crunched up
1168   current_x += nWidth < 64 ? 64 : nWidth;
1169   current_x += 8;
1170
1171   return pCurrentShader;
1172 }
1173
1174 /*
1175 ============================================================================
1176
1177   MOUSE ACTIONS
1178
1179 ============================================================================
1180 */
1181
1182 static  int textures_cursorx, textures_cursory;
1183
1184 /*
1185 ============
1186 Texture_SetTexture
1187
1188 brushprimit_texdef must be understood as a qtexture_t with width=2 height=2 ( the default one )
1189 ============
1190 */
1191
1192 //++timo NOTE: this is a mix of Shader module stuff and texture explorer
1193 // it might need to be split in parts or moved out .. dunno
1194 void WINAPI Texture_SetTexture (texdef_t *texdef, brushprimit_texdef_t *brushprimit_texdef, bool bFitScale, IPluginTexdef *pTexdef, bool bSetSelection )
1195 {
1196   if (texdef->GetName()[0] == '(')
1197   {
1198     Sys_Status("Can't select an entity texture", 0);
1199     return;
1200   }
1201   g_qeglobals.d_texturewin.texdef = *texdef;
1202   g_qeglobals.d_texturewin.texdef.flags &= ~SURF_KEEP;
1203   g_qeglobals.d_texturewin.texdef.contents &= ~CONTENTS_KEEP;
1204   // store the shader pointer
1205   // NOTE: maybe passing the shader pointer would help?
1206   g_qeglobals.d_texturewin.pShader->DecRef();
1207   g_qeglobals.d_texturewin.pShader = QERApp_Shader_ForName(texdef->GetName());
1208   g_qeglobals.d_texturewin.pShader->IncRef();
1209   // set this shader as in use
1210   g_qeglobals.d_texturewin.pShader->SetInUse( true );
1211   // store the texture coordinates for new brush primitive mode
1212   // be sure that all the callers are using the default 2x2 texture
1213   if (g_qeglobals.m_bBrushPrimitMode)
1214   {
1215     g_qeglobals.d_texturewin.brushprimit_texdef = *brushprimit_texdef;
1216   }
1217
1218   g_dlgFind.updateTextures(texdef->GetName());
1219   if (!g_dlgFind.isOpen() && bSetSelection)
1220   {
1221     Select_SetTexture(texdef,brushprimit_texdef,bFitScale);
1222   }
1223
1224   //plugins: send a message telling that the selected texture may have changed
1225   DispatchRadiantMsg( RADIANT_TEXTURE );
1226
1227   // scroll origin so the texture is completely on screen
1228   // takes texdef from g_qeglobals.d_texturewin.texdef, set above
1229   Texture_ResetPosition();
1230 }
1231
1232 void ViewShader(const char *pFile, const char *pName)
1233 {
1234   //  ask the vfs to build the full path to the file
1235   // (i.e. the first one found)
1236   char *fullName = vfsGetFullPath(pFile,0,0);
1237   if (fullName == NULL)
1238   {
1239     Sys_FPrintf (SYS_ERR, "Couldn't get a full path to the shader file: %s\n", pFile);
1240     return;
1241   }
1242
1243   char* pBuff = NULL;
1244   int nSize = vfsLoadFullPathFile(fullName, reinterpret_cast<void**>(&pBuff));
1245   if (nSize <= 0)
1246   {
1247     Sys_FPrintf (SYS_ERR, "Failed to load shader file %s\n", fullName);
1248     return;
1249   }
1250   // look for the shader declaration
1251   int nStart;
1252   CString strFind = pName;
1253   CString strLook = pBuff;
1254   strLook.MakeLower();
1255   strFind.MakeLower();
1256   // offset used when jumping over commented out definitions
1257   int nOffset = 0;
1258   while (true)
1259   {
1260     nStart = strLook.Find(strFind, nOffset);
1261     if (nStart == -1)
1262       break;
1263     // we have found something, maybe it's a commented out shader name?
1264     char *strCheck = new char[strLook.GetLength()+1];
1265     strcpy( strCheck, strLook.GetBuffer() );
1266     strCheck[nStart] = 0;
1267     char *pCheck = strrchr( strCheck, '\n' );
1268     // if there's a commentary sign in-between we'll continue
1269     if (pCheck && strstr( pCheck, "//" ))
1270     {
1271       delete[] strCheck;
1272       nOffset = nStart + 1;
1273       continue;
1274     }
1275     delete[] strCheck;
1276     nOffset = nStart;
1277     break;
1278   }
1279   // now close the file
1280   g_free(pBuff);
1281
1282   DoTextEditor (fullName, nOffset);
1283 }
1284
1285 /*
1286 ==============
1287 SelectTexture
1288
1289   By mouse click
1290 ==============
1291 */
1292 void SelectTexture (int mx, int my, bool bShift, bool bFitScale)
1293 {
1294   int   x, y;
1295   qtexture_t  *q;
1296   texdef_t  tex;
1297   brushprimit_texdef_t brushprimit_tex;
1298
1299   my += g_qeglobals.d_texturewin.originy-g_qeglobals.d_texturewin.height;
1300
1301   Texture_StartPos ();
1302   while (1)
1303   {
1304     // NOTE: return value is == pCurrentShader and pCurrentShader->getTexture == current_texture
1305     Texture_NextPos (&x, &y);
1306     q = current_texture;
1307     if (!q)
1308       break;
1309     int nWidth = (int)(q->width * ((float)g_PrefsDlg.m_nTextureScale / 100));
1310     int nHeight = (int)(q->height * ((float)g_PrefsDlg.m_nTextureScale / 100));
1311     if (mx > x && mx - x < nWidth
1312       && my < y && y - my < nHeight + FONT_HEIGHT)
1313     {
1314       if (bShift)
1315       {
1316         if (pCurrentShader->IsDefault())
1317           Sys_Printf("ERROR: %s is not a shader, it's a texture.\n", pCurrentShader->getName() );
1318         else
1319           ViewShader( pCurrentShader->getShaderFileName(), pCurrentShader->getName() );
1320       }
1321       else
1322       {
1323         memset (&tex, 0, sizeof(tex));
1324         memset (&brushprimit_tex, 0, sizeof(brushprimit_tex));
1325         if (g_qeglobals.m_bBrushPrimitMode)
1326         {
1327           // brushprimit fitted to a 2x2 texture
1328           brushprimit_tex.coords[0][0] = 1.0f;
1329           brushprimit_tex.coords[1][1] = 1.0f;
1330         }
1331         else
1332         {
1333           tex.scale[0] = g_pGameDescription->mTextureDefaultScale;
1334           tex.scale[1] = g_pGameDescription->mTextureDefaultScale;
1335         }
1336         tex.flags = pCurrentShader->getFlags();
1337         // TTimo - shader code cleanup
1338         // texdef.name is the name of the shader, not the name of the actual texture file
1339         tex.SetName(pCurrentShader->getName());
1340         // NOTE WARNING: Texture_SetTexture uses Texture_NextPos stuff to move the window position on to the texture
1341         // if there's some kind of fuckup in Texture_SetTexture you may end up with different pCurrentShader or even pCurrentShader == NULL
1342         // so we just consider pCurrentShader and current_texture are not valid after this point
1343         IShader *pAuxShader = pCurrentShader;
1344         Texture_SetTexture ( &tex, &brushprimit_tex, bFitScale, NULL); // Nurail
1345         CString strTex;
1346         CString strName;
1347         // if shader, print shader name, otherwise texture name
1348         //++timo FIXME: maybe CShader needs some properties between color / default / actual shader
1349 #ifdef _DEBUG
1350         // this one is never supposed to be set as current one
1351         if (pAuxShader->IsColor())
1352           Sys_Printf("ERROR: unexpected pCurrentShader->IsColor() in SelectTexture\n");
1353 #endif
1354         // NOTE: IsColor is false, IsDefault the only remaining property
1355         if (pAuxShader->IsDefault())
1356         {
1357           strName = q->name;
1358           // remove the "textures/" if needed
1359           if (strName.Find("textures/")!=-1)
1360             strName = strName.Mid(9);
1361         }
1362         else
1363         {
1364           strName = pAuxShader->getName();
1365         }
1366         strTex.Format("%s W: %i H: %i", strName.GetBuffer(), q->width, q->height);
1367         g_pParentWnd->SetStatusText(3, strTex);
1368       }
1369       return;
1370     }
1371   }
1372
1373   Sys_Status("Did not select a texture", 0);
1374 }
1375
1376 /*
1377 ==============
1378 Texture_MouseDown
1379 ==============
1380 */
1381 void Texture_MouseDown (int x, int y, int buttons)
1382 {
1383   Sys_GetCursorPos (&textures_cursorx, &textures_cursory);
1384
1385   // lbutton = select texture
1386   if (buttons == MK_LBUTTON || buttons == (MK_LBUTTON | MK_SHIFT) || buttons == (MK_LBUTTON | MK_CONTROL))
1387   {
1388     SelectTexture (x, g_qeglobals.d_texturewin.height - 1 - y, buttons & MK_SHIFT, buttons & MK_CONTROL);
1389     UpdateSurfaceDialog();
1390     UpdatePatchInspector();
1391   }
1392 }
1393
1394 /*
1395 ==============
1396 Texture_MouseMoved
1397 ==============
1398 */
1399
1400 void Texture_MouseMoved (int x, int y, int buttons)
1401 {
1402   int scale = 1;
1403
1404   if ( buttons & MK_SHIFT )
1405     scale = 4;
1406
1407   // rbutton = drag texture origin
1408   if (buttons & MK_RBUTTON)
1409   {
1410     Sys_GetCursorPos (&x, &y);
1411     if ( y != textures_cursory)
1412     {
1413       g_qeglobals.d_texturewin.originy += ( y-textures_cursory) * scale;
1414       if (g_qeglobals.d_texturewin.originy > 0)
1415         g_qeglobals.d_texturewin.originy = 0;
1416       Sys_SetCursorPos (textures_cursorx, textures_cursory);
1417
1418       // (g_PrefsDlg.m_bTextureScrollbar && g_qeglobals_gui.d_texture_scroll != NULL)
1419       // fixes broken texture scrolling when scrollbar is disabled
1420       GtkAdjustment *vadjustment = gtk_range_get_adjustment (GTK_RANGE (g_qeglobals_gui.d_texture_scroll));
1421       gtk_adjustment_set_value (vadjustment, abs(g_qeglobals.d_texturewin.originy));
1422       //
1423     }
1424     return;
1425   }
1426 }
1427
1428 /*
1429 ============================================================================
1430
1431 DRAWING
1432
1433 ============================================================================
1434 */
1435
1436 int imax(int iFloor, int i) { if (i>iFloor) return iFloor;return i;}
1437
1438 /*
1439 ============
1440 Texture_Draw
1441 TTimo: relying on the shaders list to display the textures
1442 we must query all qtexture_t* to manage and display through the IShaders interface
1443 this allows a plugin to completely override the texture system
1444 ============
1445 */
1446 void Texture_Draw (int width, int height)
1447 {
1448   int x, y, last_y = 0, last_height = 0, nWidth, nHeight;
1449   qtexture_t *q;
1450   char *name;
1451
1452   qglClearColor (g_qeglobals.d_savedinfo.colors[COLOR_TEXTUREBACK][0],
1453                  g_qeglobals.d_savedinfo.colors[COLOR_TEXTUREBACK][1],
1454                  g_qeglobals.d_savedinfo.colors[COLOR_TEXTUREBACK][2], 0);
1455   qglViewport (0,0,width,height);
1456   qglMatrixMode(GL_PROJECTION);
1457   qglLoadIdentity ();
1458
1459   qglClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1460   qglDisable (GL_DEPTH_TEST);
1461   qglDisable(GL_BLEND);
1462   qglOrtho (0, width, g_qeglobals.d_texturewin.originy-height, g_qeglobals.d_texturewin.originy, -100, 100);
1463   qglEnable (GL_TEXTURE_2D);
1464
1465   qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1466   g_qeglobals.d_texturewin.width = width;
1467   g_qeglobals.d_texturewin.height = height;
1468
1469   Texture_StartPos();
1470   for (;;)
1471   {
1472     // NOTE: return value is == pCurrentShader and pCurrentShader->getTexture == current_texture
1473     Texture_NextPos (&x, &y);
1474     q = current_texture;
1475     if (!q)
1476       break;
1477
1478     nWidth = (int)(q->width * ((float)g_PrefsDlg.m_nTextureScale / 100));
1479     nHeight = (int)(q->height * ((float)g_PrefsDlg.m_nTextureScale / 100));
1480
1481     if (y != last_y)
1482     {
1483       last_y = y;
1484       last_height = 0;
1485     }
1486     last_height = MAX (nHeight, last_height);
1487
1488     // Is this texture visible?
1489     if ((y-nHeight-FONT_HEIGHT < g_qeglobals.d_texturewin.originy)
1490         && (y > g_qeglobals.d_texturewin.originy - height))
1491     {
1492       // borders rules:
1493       // if it's the current texture, draw a thick red line, else:
1494       // shaders have a white border, simple textures don't
1495       // if !texture_showinuse: (some textures displayed may not be in use)
1496       // draw an additional square around with 0.5 1 0.5 color
1497       if (!strcmpi(g_qeglobals.d_texturewin.texdef.GetName(), pCurrentShader->getName()))
1498       {
1499               qglLineWidth (3);
1500               qglColor3f (1,0,0);
1501               qglDisable (GL_TEXTURE_2D);
1502
1503               qglBegin (GL_LINE_LOOP);
1504               qglVertex2f (x-4,y-FONT_HEIGHT+4);
1505               qglVertex2f (x-4,y-FONT_HEIGHT-nHeight-4);
1506               qglVertex2f (x+4+nWidth,y-FONT_HEIGHT-nHeight-4);
1507               qglVertex2f (x+4+nWidth,y-FONT_HEIGHT+4);
1508               qglEnd ();
1509
1510               qglEnable (GL_TEXTURE_2D);
1511               qglLineWidth (1);
1512       }
1513       else
1514       {
1515               qglLineWidth (1);
1516               // shader border:
1517               if (!pCurrentShader->IsDefault())
1518               {
1519                 qglColor3f (1,1,1);
1520                 qglDisable (GL_TEXTURE_2D);
1521
1522                 qglBegin (GL_LINE_LOOP);
1523                 qglVertex2f (x-1,y+1-FONT_HEIGHT);
1524                 qglVertex2f (x-1,y-nHeight-1-FONT_HEIGHT);
1525                 qglVertex2f (x+1+nWidth,y-nHeight-1-FONT_HEIGHT);
1526                 qglVertex2f (x+1+nWidth,y+1-FONT_HEIGHT);
1527                 qglEnd ();
1528                 qglEnable (GL_TEXTURE_2D);
1529               }
1530
1531               // highlight in-use textures
1532               if (pCurrentShader->IsInUse())
1533               {
1534                 qglColor3f (0.5,1,0.5);
1535                 qglDisable (GL_TEXTURE_2D);
1536                 qglBegin (GL_LINE_LOOP);
1537                 qglVertex2f (x-3,y+3-FONT_HEIGHT);
1538                 qglVertex2f (x-3,y-nHeight-3-FONT_HEIGHT);
1539                 qglVertex2f (x+3+nWidth,y-nHeight-3-FONT_HEIGHT);
1540                 qglVertex2f (x+3+nWidth,y+3-FONT_HEIGHT);
1541                 qglEnd ();
1542                 qglEnable (GL_TEXTURE_2D);
1543               }
1544       }
1545
1546       // Draw the texture
1547       qglBindTexture (GL_TEXTURE_2D, q->texture_number);
1548       QE_CheckOpenGLForErrors();
1549       qglColor3f (1,1,1);
1550       qglBegin (GL_QUADS);
1551       qglTexCoord2f (0,0);
1552       qglVertex2f (x,y-FONT_HEIGHT);
1553       qglTexCoord2f (1,0);
1554       qglVertex2f (x+nWidth,y-FONT_HEIGHT);
1555       qglTexCoord2f (1,1);
1556       qglVertex2f (x+nWidth,y-FONT_HEIGHT-nHeight);
1557       qglTexCoord2f (0,1);
1558       qglVertex2f (x,y-FONT_HEIGHT-nHeight);
1559       qglEnd ();
1560
1561       // draw the texture name
1562       qglDisable (GL_TEXTURE_2D);
1563       qglColor3f (1,1,1);
1564
1565       qglRasterPos2f (x, y-FONT_HEIGHT+2);
1566
1567       // don't draw the directory name
1568       name = (char*)pCurrentShader->getName();
1569       name += strlen(name);
1570       while(name != (char*)pCurrentShader->getName() && *(name-1) != '/' && *(name-1) != '\\')
1571         name--;
1572
1573       gtk_glwidget_print_string(name);
1574       qglEnable (GL_TEXTURE_2D);
1575     }
1576   }
1577
1578   g_qeglobals.d_texturewin.m_nTotalHeight = abs(y) + last_height + FONT_HEIGHT + 4;
1579
1580   // reset the current texture
1581   qglBindTexture(GL_TEXTURE_2D, 0);
1582   qglFinish();
1583 }
1584
1585 //++timo seems we only know hard inits now..
1586 //void Texture_Init (bool bHardInit)
1587 void Texture_Init()
1588 {
1589   g_qeglobals.d_qtextures = NULL;
1590   // initialize the qtexture map
1591   if (g_qeglobals.d_qtexmap)
1592   {
1593     Sys_FPrintf(SYS_ERR, "TODO: delete g_qeglobals.d_qtexmap in Texture_Init\n");
1594   }
1595   g_qeglobals.d_qtexmap = g_hash_table_new (g_str_hash, g_str_equal);
1596   // initialize .. in some cases if no default texture / project loaded it crashes
1597   memset( &g_qeglobals.d_texturewin.texdef, 0, sizeof(g_qeglobals.d_texturewin.texdef) );
1598   g_qeglobals.d_texturewin.texdef.SetName(SHADER_NOT_FOUND);
1599   g_qeglobals.d_texturewin.pShader = QERApp_Shader_ForName(SHADER_NOT_FOUND);
1600 }
1601
1602 // FIXME TTimo this needs to move to the shader module along with l_shaderlist move
1603 // preload shader files that have been listed in shaderlist.txt
1604 void PreloadShaders()
1605 {
1606   GSList *lst = l_shaderfiles;
1607   Str shadername;
1608   while (lst)
1609   {
1610     shadername = g_pGameDescription->mShaderPath;
1611     shadername += (char*)lst->data;
1612     QERApp_LoadShaderFile(shadername.GetBuffer());
1613     lst = lst->next;
1614   }
1615 }
1616
1617 // TTimo: modified to expect the reletive path to the skin as input
1618 // will look into pak files if necessary
1619 // uses the shader code to load the texture Try_Texture_ForName
1620 // modified SkinInfo accordingly to store the qtexture_t and shader name (reletive version)
1621 // the .md3 have bundled filetype extension, but they don't fit with the actual data
1622 //   ex: models/mapobjects/gargoyle.tga doesn't exist, but models/mapobjects/gargoyle.jpg can be used instead
1623 //   so we remove the extension before load attempt
1624 int WINAPI Texture_LoadSkin(char *pName, int *pnWidth, int *pnHeight)
1625 {
1626   //  byte *pic = NULL;
1627   //  byte *pic32 = NULL;
1628   int nTex = -1;
1629   qtexture_t *qtex;
1630   SkinInfo *pInfo;
1631   const char *pCleanName;
1632
1633   int nSize = g_lstSkinCache.GetSize();
1634   pCleanName = QERApp_CleanTextureName( pName, false );
1635   for (int i = 0; i < nSize; i++)
1636   {
1637     SkinInfo *pInfo = reinterpret_cast<SkinInfo*>(g_lstSkinCache.GetAt(i));
1638     if (pInfo)
1639     {
1640       if (stricmp(pCleanName, pInfo->m_strName) == 0)
1641       {
1642         return pInfo->m_nTextureBind;
1643       }
1644     }
1645   }
1646
1647   // if the load is successfull, we get back a qtexture_t
1648   // we don't need to free it, it's in g_qeglobals.d_qtextures
1649   // NOTE: we need to free the SkinInfo though..
1650   qtex = QERApp_Try_Texture_ForName( pCleanName );
1651   if (qtex)
1652   {
1653     nTex = qtex->texture_number;
1654     pInfo = new SkinInfo(qtex->name, nTex, qtex);
1655   } else
1656   {
1657     pInfo = new SkinInfo(pCleanName, -1, NULL);
1658   }
1659   g_lstSkinCache.Add(pInfo);
1660
1661   return nTex;
1662 }
1663
1664 bool TexWnd::CheckFilter( const char* name )
1665 {
1666   const char* buf = gtk_entry_get_text (GTK_ENTRY (m_pFilter));
1667   if (strstr( name, buf ) != 0)
1668     return true;
1669   return false;
1670 }
1671
1672 // =============================================================================
1673 // static functions
1674
1675 static void vertical_scroll (GtkWidget *widget, gpointer data)
1676 {
1677   ((TexWnd*)data)->OnVScroll ();
1678 }
1679
1680 static void filter_changed (GtkWidget *widget, gpointer data)
1681 {
1682   CString str;
1683   str = gtk_entry_get_text (GTK_ENTRY (widget));
1684   ((TexWnd*)data)->UpdateFilter (str);
1685 }
1686
1687 // =============================================================================
1688 // TexWnd class
1689
1690 TexWnd::TexWnd()
1691 : GLWindow (FALSE)
1692 {
1693   m_pFilter = NULL;
1694   m_bNeedRange = true;
1695 }
1696
1697 TexWnd::~TexWnd()
1698 {
1699 }
1700
1701 void TexWnd::OnCreate ()
1702 {
1703   if (!MakeCurrent ())
1704     Error ("glMakeCurrent in TexWnd::OnCreate failed");
1705
1706   g_qeglobals_gui.d_texture = m_pWidget;
1707   g_nTextureOffset = 0;
1708
1709   GtkAdjustment *vadjustment = gtk_range_get_adjustment (GTK_RANGE (g_qeglobals_gui.d_texture_scroll));
1710   gtk_signal_connect (GTK_OBJECT (vadjustment), "value_changed", GTK_SIGNAL_FUNC (vertical_scroll), this);
1711
1712   if (g_PrefsDlg.m_bTextureScrollbar)
1713     gtk_widget_show (g_qeglobals_gui.d_texture_scroll);
1714   else
1715     gtk_widget_hide (g_qeglobals_gui.d_texture_scroll);
1716   m_bNeedRange = true;
1717
1718   gtk_signal_connect (GTK_OBJECT (m_pFilter), "changed", GTK_SIGNAL_FUNC (filter_changed), this);
1719   if (g_PrefsDlg.m_bTextureWindow)
1720     gtk_widget_show (m_pFilter);
1721 }
1722
1723 void TexWnd::UpdateFilter(const char* pFilter)
1724 {
1725   g_bFilterEnabled = false;
1726   if (pFilter)
1727   {
1728     g_strFilter = pFilter;
1729     if (g_strFilter.GetLength() > 0)
1730       g_bFilterEnabled = true;
1731     QERApp_SortActiveShaders();
1732   }
1733   Sys_UpdateWindows (W_TEXTURE);
1734 }
1735
1736 void TexWnd::OnSize (int cx, int cy)
1737 {
1738   m_bNeedRange = true;
1739 }
1740
1741 void TexWnd::OnExpose ()
1742 {
1743   int nOld = g_qeglobals.d_texturewin.m_nTotalHeight;
1744   if (!MakeCurrent ())
1745   {
1746     Sys_Printf("ERROR: glXMakeCurrent failed..\n ");
1747     Sys_Printf("Please restart Radiant if the Texture view is not working\n");
1748   } else
1749   {
1750     QE_CheckOpenGLForErrors();
1751     Texture_Draw (m_pWidget->allocation.width, m_pWidget->allocation.height - g_nTextureOffset);
1752     QE_CheckOpenGLForErrors();
1753     SwapBuffers ();
1754   }
1755   if (g_PrefsDlg.m_bTextureScrollbar && (m_bNeedRange || g_qeglobals.d_texturewin.m_nTotalHeight != nOld))
1756   {
1757     GtkAdjustment *vadjustment = gtk_range_get_adjustment (GTK_RANGE (g_qeglobals_gui.d_texture_scroll));
1758
1759     vadjustment->value = -g_qeglobals.d_texturewin.originy;
1760     vadjustment->page_size = m_pWidget->allocation.height;
1761     vadjustment->page_increment = m_pWidget->allocation.height/2;
1762     vadjustment->step_increment = 20;
1763     vadjustment->lower = 0;
1764     vadjustment->upper = g_qeglobals.d_texturewin.m_nTotalHeight;
1765
1766     gtk_signal_emit_by_name (GTK_OBJECT (vadjustment), "changed");
1767
1768     m_bNeedRange = false;
1769   }
1770 }
1771
1772 void TexWnd::OnLButtonDown (guint32 flags, int pointx, int pointy)
1773 {
1774   SetCapture ();
1775   Texture_MouseDown (pointx, pointy - g_nTextureOffset, flags);
1776 }
1777
1778 void TexWnd::OnRButtonDown (guint32 flags, int pointx, int pointy)
1779 {
1780   SetCapture ();
1781   Texture_MouseDown (pointx, pointy - g_nTextureOffset, flags);
1782 }
1783
1784 void TexWnd::OnMButtonDown (guint32 flags, int pointx, int pointy)
1785 {
1786   SetCapture ();
1787   Texture_MouseDown (pointx, pointy - g_nTextureOffset, flags);
1788 }
1789
1790 void TexWnd::OnLButtonUp (guint32 flags, int pointx, int pointy)
1791 {
1792   ReleaseCapture ();
1793   DragDropTexture (flags, pointx, pointy);
1794 }
1795
1796 void TexWnd::OnRButtonUp (guint32 flags, int pointx, int pointy)
1797 {
1798   ReleaseCapture ();
1799 }
1800
1801 void TexWnd::OnMButtonUp (guint32 flags, int pointx, int pointy)
1802 {
1803   ReleaseCapture ();
1804 }
1805
1806 void TexWnd::OnMouseMove (guint32 flags, int pointx, int pointy)
1807 {
1808   Texture_MouseMoved (pointx, pointy - g_nTextureOffset, flags);
1809   // if scrollbar is hidden, we don't seem to get an update
1810   if( !g_PrefsDlg.m_bTextureScrollbar )
1811     RedrawWindow ();
1812 }
1813
1814 void TexWnd::OnVScroll ()
1815 {
1816   GtkAdjustment *vadjustment = gtk_range_get_adjustment (GTK_RANGE (g_qeglobals_gui.d_texture_scroll));
1817
1818   g_qeglobals.d_texturewin.originy = - (int)vadjustment->value;
1819   RedrawWindow ();
1820 }
1821
1822 void TexWnd::UpdatePrefs()
1823 {
1824   if (g_PrefsDlg.m_bTextureWindow)
1825     gtk_widget_show (m_pFilter);
1826   else
1827     gtk_widget_hide (m_pFilter);
1828
1829   if (g_PrefsDlg.m_bTextureScrollbar)
1830     gtk_widget_show (g_qeglobals_gui.d_texture_scroll);
1831   else
1832     gtk_widget_hide (g_qeglobals_gui.d_texture_scroll);
1833   m_bNeedRange = true;
1834   RedrawWindow ();
1835 }
1836
1837 void TexWnd::FocusEdit()
1838 {
1839   if (GTK_WIDGET_VISIBLE (m_pFilter))
1840     gtk_window_set_focus (GTK_WINDOW (g_pParentWnd->m_pWidget), m_pFilter);
1841 }
1842
1843 void TexWnd::OnMouseWheel(bool bUp)
1844 {
1845   if (bUp)
1846   {
1847     if(g_qeglobals.d_texturewin.originy < 0) {
1848       g_qeglobals.d_texturewin.originy += g_PrefsDlg.m_nWheelInc;
1849       // clamp so we don't get jiggle if moved by less than scrollwheel increment
1850       if(g_qeglobals.d_texturewin.originy > 0) {
1851         g_qeglobals.d_texturewin.originy = 0;
1852       }
1853     }
1854   }
1855   else
1856   {
1857     if(g_qeglobals.d_texturewin.originy > (-g_qeglobals.d_texturewin.m_nTotalHeight + g_qeglobals.d_texturewin.height))
1858       g_qeglobals.d_texturewin.originy -= g_PrefsDlg.m_nWheelInc;
1859   }
1860   GtkAdjustment *vadjustment = gtk_range_get_adjustment (GTK_RANGE (g_qeglobals_gui.d_texture_scroll));
1861   gtk_adjustment_set_value (vadjustment, abs(g_qeglobals.d_texturewin.originy));
1862
1863   RedrawWindow();
1864 }
1865
1866 void TexWnd::DragDropTexture (guint32 flags, int pointx, int pointy)
1867 {
1868   // This gets called from leftmouse up event. We see if the mouseup is above
1869   // the camwindow. If this is the case do a trace for a surface. If we hit a
1870   // surface, texture it with the current texture.
1871
1872   int     m_ptXcheck, m_ptYcheck;
1873   int     m_ptX, m_ptY;
1874   GtkWidget *widget;
1875   gint x, y;
1876   vec3_t  dir;
1877   float   f, r, u;
1878   int     i;
1879
1880   // we only want to catch a plain mouseevent
1881   if (flags)
1882     return;
1883
1884   // see if we are above the camwindow
1885   Sys_GetCursorPos (&m_ptX, &m_ptY);
1886
1887   if (g_pParentWnd->CurrentStyle() == MainFrame::eFloating)
1888     widget = g_pParentWnd->GetCamWnd()->m_pParent;
1889   else
1890     widget = g_pParentWnd->GetCamWnd()->GetWidget();
1891
1892   get_window_pos (widget, &x, &y);
1893
1894   if (m_ptX < x || m_ptY < y ||
1895       m_ptX > x + widget->allocation.width ||
1896       m_ptY > y + widget->allocation.height)
1897     return;
1898
1899   // check if the camwindow isn't being partially hidden by another window at this point
1900   m_ptXcheck = m_ptX;
1901   m_ptYcheck = m_ptY;
1902
1903   if( g_pParentWnd->GetCamWnd()->GetWidget()->window != gdk_window_at_pointer( &m_ptXcheck, &m_ptYcheck ) )
1904     return;
1905
1906   // calc ray direction
1907   x = m_ptX - x;
1908   y = g_pParentWnd->GetCamWnd()->Camera()->height - 1 - (m_ptY - y);
1909   u = (float)( y - ( g_pParentWnd->GetCamWnd()->Camera()->height * .5f ) ) / ( g_pParentWnd->GetCamWnd()->Camera()->height * .5f );
1910   r = (float)( x - ( g_pParentWnd->GetCamWnd()->Camera()->width * .5f ) ) / ( g_pParentWnd->GetCamWnd()->Camera()->width * .5f );
1911   f = 1;
1912
1913   for (i=0 ; i<3 ; i++)
1914     dir[i] = g_pParentWnd->GetCamWnd()->Camera()->vpn[i] * f +
1915         g_pParentWnd->GetCamWnd()->Camera()->vright[i] * r +
1916         g_pParentWnd->GetCamWnd()->Camera()->vup[i] * u;
1917   VectorNormalize (dir, dir);
1918
1919   // do a trace for a surface
1920         trace_t t;
1921
1922         t = Test_Ray (g_pParentWnd->GetCamWnd()->Camera()->origin, dir, SF_SINGLEFACE);
1923
1924   if (t.brush)
1925   {
1926     texdef_t  tex;
1927     brushprimit_texdef_t brushprimit_tex;
1928
1929     memset (&tex, 0, sizeof(tex));
1930     memset (&brushprimit_tex, 0, sizeof(brushprimit_tex));
1931     if (g_qeglobals.m_bBrushPrimitMode)
1932     {
1933       // brushprimit fitted to a 2x2 texture
1934       brushprimit_tex.coords[0][0] = 1.0f;
1935       brushprimit_tex.coords[1][1] = 1.0f;
1936     } else
1937     {
1938       tex.scale[0] = g_pGameDescription->mTextureDefaultScale;
1939       tex.scale[1] = g_pGameDescription->mTextureDefaultScale;
1940     }
1941     tex.flags = g_qeglobals.d_texturewin.texdef.flags;
1942     tex.value = g_qeglobals.d_texturewin.texdef.value;
1943     tex.contents = g_qeglobals.d_texturewin.texdef.contents;
1944     // TTimo - shader code cleanup
1945     // texdef.name is the name of the shader, not the name of the actual texture file
1946     tex.SetName(g_qeglobals.d_texturewin.texdef.GetName());
1947
1948     Undo_Start("set face textures");
1949           Undo_AddBrush(t.brush);
1950           SetFaceTexdef (t.face, &tex, &brushprimit_tex, false, NULL );
1951           Brush_Build(t.brush, false);
1952           Undo_EndBrush(t.brush);
1953           Undo_End();
1954
1955     Sys_UpdateWindows (W_CAMERA);
1956     g_pParentWnd->OnTimer ();
1957   }
1958 }