]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/texwindow.cpp
Prevent implicit Widget construction
[xonotic/netradiant.git] / radiant / texwindow.cpp
1 /*
2    Copyright (C) 1999-2006 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 #include "texwindow.h"
29
30 #include <gtk/gtk.h>
31
32 #include "debugging/debugging.h"
33 #include "warnings.h"
34
35 #include "ifilesystem.h"
36 #include "iundo.h"
37 #include "igl.h"
38 #include "iarchive.h"
39 #include "moduleobserver.h"
40
41 #include <set>
42 #include <string>
43 #include <vector>
44
45 #include <uilib/uilib.h>
46
47 #include "signal/signal.h"
48 #include "math/vector.h"
49 #include "texturelib.h"
50 #include "string/string.h"
51 #include "shaderlib.h"
52 #include "os/file.h"
53 #include "os/path.h"
54 #include "stream/memstream.h"
55 #include "stream/textfilestream.h"
56 #include "stream/stringstream.h"
57 #include "cmdlib.h"
58 #include "texmanip.h"
59 #include "textures.h"
60 #include "convert.h"
61
62 #include "gtkutil/menu.h"
63 #include "gtkutil/nonmodal.h"
64 #include "gtkutil/cursor.h"
65 #include "gtkutil/widget.h"
66 #include "gtkutil/glwidget.h"
67 #include "gtkutil/messagebox.h"
68
69 #include "error.h"
70 #include "map.h"
71 #include "qgl.h"
72 #include "select.h"
73 #include "brush_primit.h"
74 #include "brushmanip.h"
75 #include "patchmanip.h"
76 #include "plugin.h"
77 #include "qe3.h"
78 #include "gtkdlgs.h"
79 #include "gtkmisc.h"
80 #include "mainframe.h"
81 #include "findtexturedialog.h"
82 #include "surfacedialog.h"
83 #include "patchdialog.h"
84 #include "groupdialog.h"
85 #include "preferences.h"
86 #include "shaders.h"
87 #include "commands.h"
88
89 #define NOTEX_BASENAME "notex"
90 #define SHADERNOTEX_BASENAME "shadernotex"
91
92 bool TextureBrowser_showWads(){
93         return !string_empty( g_pGameDescription->getKeyValue( "show_wads" ) );
94 }
95
96 void TextureBrowser_queueDraw( TextureBrowser& textureBrowser );
97
98 bool string_equal_start( const char* string, StringRange start ){
99         return string_equal_n( string, start.first, start.last - start.first );
100 }
101
102 typedef std::set<CopiedString> TextureGroups;
103
104 void TextureGroups_addWad( TextureGroups& groups, const char* archive ){
105         if ( extension_equal( path_get_extension( archive ), "wad" ) ) {
106 #if 1
107                 groups.insert( archive );
108 #else
109                 CopiedString archiveBaseName( path_get_filename_start( archive ), path_get_filename_base_end( archive ) );
110                 groups.insert( archiveBaseName );
111 #endif
112         }
113 }
114 typedef ReferenceCaller1<TextureGroups, const char*, TextureGroups_addWad> TextureGroupsAddWadCaller;
115
116 namespace
117 {
118 bool g_TextureBrowser_shaderlistOnly = false;
119 bool g_TextureBrowser_fixedSize = true;
120 bool g_TextureBrowser_filterMissing = false;
121 bool g_TextureBrowser_filterFallback = true;
122 bool g_TextureBrowser_enableAlpha = true;
123 }
124
125 CopiedString g_notex;
126 CopiedString g_shadernotex;
127 bool isMissing(const char* name);
128 bool isNotex(const char* name);
129
130 bool isMissing(const char* name){
131         if ( string_equal( g_notex.c_str(), name ) ) {
132                 return true;
133         }
134         if ( string_equal( g_shadernotex.c_str(), name ) ) {
135                 return true;
136         }
137         return false;
138 }
139
140 bool isNotex(const char* name){
141         if ( string_equal_suffix( name, "/" NOTEX_BASENAME ) ) {
142                 return true;
143         }
144         if ( string_equal_suffix( name, "/" SHADERNOTEX_BASENAME ) ) {
145                 return true;
146         }
147         return false;
148 }
149
150 void TextureGroups_addShader( TextureGroups& groups, const char* shaderName ){
151         const char* texture = path_make_relative( shaderName, "textures/" );
152
153         // hide notex / shadernotex images
154         if ( g_TextureBrowser_filterFallback ) {
155                 if ( isNotex( shaderName ) ) {
156                         return;
157                 }
158                 if ( isNotex( texture ) ) {
159                         return;
160                 }
161         }
162
163         if ( texture != shaderName ) {
164                 const char* last = path_remove_directory( texture );
165                 if ( !string_empty( last ) ) {
166                         groups.insert( CopiedString( StringRange( texture, --last ) ) );
167                 }
168         }
169 }
170 typedef ReferenceCaller1<TextureGroups, const char*, TextureGroups_addShader> TextureGroupsAddShaderCaller;
171
172 void TextureGroups_addDirectory( TextureGroups& groups, const char* directory ){
173         groups.insert( directory );
174 }
175 typedef ReferenceCaller1<TextureGroups, const char*, TextureGroups_addDirectory> TextureGroupsAddDirectoryCaller;
176
177 class DeferredAdjustment
178 {
179 gdouble m_value;
180 guint m_handler;
181 typedef void ( *ValueChangedFunction )( void* data, gdouble value );
182 ValueChangedFunction m_function;
183 void* m_data;
184
185 static gboolean deferred_value_changed( gpointer data ){
186         reinterpret_cast<DeferredAdjustment*>( data )->m_function(
187                 reinterpret_cast<DeferredAdjustment*>( data )->m_data,
188                 reinterpret_cast<DeferredAdjustment*>( data )->m_value
189                 );
190         reinterpret_cast<DeferredAdjustment*>( data )->m_handler = 0;
191         reinterpret_cast<DeferredAdjustment*>( data )->m_value = 0;
192         return FALSE;
193 }
194 public:
195 DeferredAdjustment( ValueChangedFunction function, void* data ) : m_value( 0 ), m_handler( 0 ), m_function( function ), m_data( data ){
196 }
197 void flush(){
198         if ( m_handler != 0 ) {
199                 g_source_remove( m_handler );
200                 deferred_value_changed( this );
201         }
202 }
203 void value_changed( gdouble value ){
204         m_value = value;
205         if ( m_handler == 0 ) {
206                 m_handler = g_idle_add( deferred_value_changed, this );
207         }
208 }
209 static void adjustment_value_changed( GtkAdjustment *adjustment, DeferredAdjustment* self ){
210         self->value_changed( gtk_adjustment_get_value(adjustment) );
211 }
212 };
213
214
215
216 class TextureBrowser;
217
218 typedef ReferenceCaller<TextureBrowser, TextureBrowser_queueDraw> TextureBrowserQueueDrawCaller;
219
220 void TextureBrowser_scrollChanged( void* data, gdouble value );
221
222
223 enum StartupShaders
224 {
225         STARTUPSHADERS_NONE = 0,
226         STARTUPSHADERS_COMMON,
227 };
228
229 void TextureBrowser_hideUnusedExport( const BoolImportCallback& importer );
230 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_hideUnusedExport> TextureBrowserHideUnusedExport;
231
232 void TextureBrowser_showShadersExport( const BoolImportCallback& importer );
233 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_showShadersExport> TextureBrowserShowShadersExport;
234
235 void TextureBrowser_showShaderlistOnly( const BoolImportCallback& importer );
236 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_showShaderlistOnly> TextureBrowserShowShaderlistOnlyExport;
237
238 void TextureBrowser_fixedSize( const BoolImportCallback& importer );
239 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_fixedSize> TextureBrowserFixedSizeExport;
240
241 void TextureBrowser_filterMissing( const BoolImportCallback& importer );
242 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_filterMissing> TextureBrowserFilterMissingExport;
243
244 void TextureBrowser_filterFallback( const BoolImportCallback& importer );
245 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_filterFallback> TextureBrowserFilterFallbackExport;
246
247 void TextureBrowser_enableAlpha( const BoolImportCallback& importer );
248 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_enableAlpha> TextureBrowserEnableAlphaExport;
249
250 class TextureBrowser
251 {
252 public:
253 int width, height;
254 int originy;
255 int m_nTotalHeight;
256
257 CopiedString shader;
258
259 ui::Window m_parent{ui::null};
260 ui::GLArea m_gl_widget{ui::null};
261 ui::Widget m_texture_scroll{ui::null};
262 ui::Widget m_treeViewTree{ui::null};
263 ui::Widget m_treeViewTags{ui::null};
264 ui::Frame m_tag_frame{ui::null};
265 ui::ListStore m_assigned_store{ui::null};
266 ui::ListStore m_available_store{ui::null};
267 ui::Widget m_assigned_tree{ui::null};
268 ui::Widget m_available_tree{ui::null};
269 ui::Widget m_scr_win_tree{ui::null};
270 ui::Widget m_scr_win_tags{ui::null};
271 ui::Widget m_tag_notebook{ui::null};
272 ui::Button m_search_button{ui::null};
273 ui::Widget m_shader_info_item{ui::null};
274
275 std::set<CopiedString> m_all_tags;
276 ui::ListStore m_all_tags_list{ui::null};
277 std::vector<CopiedString> m_copied_tags;
278 std::set<CopiedString> m_found_shaders;
279
280 ToggleItem m_hideunused_item;
281 ToggleItem m_hidenotex_item;
282 ToggleItem m_showshaders_item;
283 ToggleItem m_showshaderlistonly_item;
284 ToggleItem m_fixedsize_item;
285 ToggleItem m_filternotex_item;
286 ToggleItem m_enablealpha_item;
287
288 guint m_sizeHandler;
289 guint m_exposeHandler;
290
291 bool m_heightChanged;
292 bool m_originInvalid;
293
294 DeferredAdjustment m_scrollAdjustment;
295 FreezePointer m_freezePointer;
296
297 Vector3 color_textureback;
298 // the increment step we use against the wheel mouse
299 std::size_t m_mouseWheelScrollIncrement;
300 std::size_t m_textureScale;
301 // make the texture increments match the grid changes
302 bool m_showShaders;
303 bool m_showTextureScrollbar;
304 StartupShaders m_startupShaders;
305 // if true, the texture window will only display in-use shaders
306 // if false, all the shaders in memory are displayed
307 bool m_hideUnused;
308 bool m_rmbSelected;
309 bool m_searchedTags;
310 bool m_tags;
311 // The uniform size (in pixels) that textures are resized to when m_resizeTextures is true.
312 int m_uniformTextureSize;
313 // Return the display width of a texture in the texture browser
314 int getTextureWidth( qtexture_t* tex ){
315         int width;
316         if ( !g_TextureBrowser_fixedSize ) {
317                 // Don't use uniform size
318                 width = (int)( tex->width * ( (float)m_textureScale / 100 ) );
319         }
320         else if
321         ( tex->width >= tex->height ) {
322                 // Texture is square, or wider than it is tall
323                 width = m_uniformTextureSize;
324         }
325         else {
326                 // Otherwise, preserve the texture's aspect ratio
327                 width = (int)( m_uniformTextureSize * ( (float)tex->width / tex->height ) );
328         }
329         return width;
330 }
331 // Return the display height of a texture in the texture browser
332 int getTextureHeight( qtexture_t* tex ){
333         int height;
334         if ( !g_TextureBrowser_fixedSize ) {
335                 // Don't use uniform size
336                 height = (int)( tex->height * ( (float)m_textureScale / 100 ) );
337         }
338         else if ( tex->height >= tex->width ) {
339                 // Texture is square, or taller than it is wide
340                 height = m_uniformTextureSize;
341         }
342         else {
343                 // Otherwise, preserve the texture's aspect ratio
344                 height = (int)( m_uniformTextureSize * ( (float)tex->height / tex->width ) );
345         }
346         return height;
347 }
348
349 TextureBrowser() :
350         m_texture_scroll( 0 ),
351         m_hideunused_item( TextureBrowserHideUnusedExport() ),
352         m_hidenotex_item( TextureBrowserFilterFallbackExport() ),
353         m_showshaders_item( TextureBrowserShowShadersExport() ),
354         m_showshaderlistonly_item( TextureBrowserShowShaderlistOnlyExport() ),
355         m_fixedsize_item( TextureBrowserFixedSizeExport() ),
356         m_filternotex_item( TextureBrowserFilterMissingExport() ),
357         m_enablealpha_item( TextureBrowserEnableAlphaExport() ),
358         m_heightChanged( true ),
359         m_originInvalid( true ),
360         m_scrollAdjustment( TextureBrowser_scrollChanged, this ),
361         color_textureback( 0.25f, 0.25f, 0.25f ),
362         m_mouseWheelScrollIncrement( 64 ),
363         m_textureScale( 50 ),
364         m_showShaders( true ),
365         m_showTextureScrollbar( true ),
366         m_startupShaders( STARTUPSHADERS_NONE ),
367         m_hideUnused( false ),
368         m_rmbSelected( false ),
369         m_searchedTags( false ),
370         m_tags( false ),
371         m_uniformTextureSize( 96 ){
372 }
373 };
374
375 void ( *TextureBrowser_textureSelected )( const char* shader );
376
377
378 void TextureBrowser_updateScroll( TextureBrowser& textureBrowser );
379
380
381 const char* TextureBrowser_getComonShadersName(){
382         const char* value = g_pGameDescription->getKeyValue( "common_shaders_name" );
383         if ( !string_empty( value ) ) {
384                 return value;
385         }
386         return "Common";
387 }
388
389 const char* TextureBrowser_getComonShadersDir(){
390         const char* value = g_pGameDescription->getKeyValue( "common_shaders_dir" );
391         if ( !string_empty( value ) ) {
392                 return value;
393         }
394         return "common/";
395 }
396
397 inline int TextureBrowser_fontHeight( TextureBrowser& textureBrowser ){
398         return GlobalOpenGL().m_font->getPixelHeight();
399 }
400
401 const char* TextureBrowser_GetSelectedShader( TextureBrowser& textureBrowser ){
402         return textureBrowser.shader.c_str();
403 }
404
405 void TextureBrowser_SetStatus( TextureBrowser& textureBrowser, const char* name ){
406         IShader* shader = QERApp_Shader_ForName( name );
407         qtexture_t* q = shader->getTexture();
408         StringOutputStream strTex( 256 );
409         strTex << name << " W: " << Unsigned( q->width ) << " H: " << Unsigned( q->height );
410         shader->DecRef();
411         g_pParentWnd->SetStatusText( g_pParentWnd->m_texture_status, strTex.c_str() );
412 }
413
414 void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name );
415
416 void TextureBrowser_SetSelectedShader( TextureBrowser& textureBrowser, const char* shader ){
417         textureBrowser.shader = shader;
418         TextureBrowser_SetStatus( textureBrowser, shader );
419         TextureBrowser_Focus( textureBrowser, shader );
420
421         if ( FindTextureDialog_isOpen() ) {
422                 FindTextureDialog_selectTexture( shader );
423         }
424
425         // disable the menu item "shader info" if no shader was selected
426         IShader* ishader = QERApp_Shader_ForName( shader );
427         CopiedString filename = ishader->getShaderFileName();
428
429         if ( filename.empty() ) {
430                 if ( textureBrowser.m_shader_info_item != NULL ) {
431                         gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, FALSE );
432                 }
433         }
434         else {
435                 gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, TRUE );
436         }
437
438         ishader->DecRef();
439 }
440
441
442 CopiedString g_TextureBrowser_currentDirectory;
443
444 /*
445    ============================================================================
446
447    TEXTURE LAYOUT
448
449    TTimo: now based on a rundown through all the shaders
450    NOTE: we expect the Active shaders count doesn't change during a Texture_StartPos .. Texture_NextPos cycle
451    otherwise we may need to rely on a list instead of an array storage
452    ============================================================================
453  */
454
455 class TextureLayout
456 {
457 public:
458 // texture layout functions
459 // TTimo: now based on shaders
460 int current_x, current_y, current_row;
461 };
462
463 void Texture_StartPos( TextureLayout& layout ){
464         layout.current_x = 8;
465         layout.current_y = -8;
466         layout.current_row = 0;
467 }
468
469 void Texture_NextPos( TextureBrowser& textureBrowser, TextureLayout& layout, qtexture_t* current_texture, int *x, int *y ){
470         qtexture_t* q = current_texture;
471
472         int nWidth = textureBrowser.getTextureWidth( q );
473         int nHeight = textureBrowser.getTextureHeight( q );
474         if ( layout.current_x + nWidth > textureBrowser.width - 8 && layout.current_row ) { // go to the next row unless the texture is the first on the row
475                 layout.current_x = 8;
476                 layout.current_y -= layout.current_row + TextureBrowser_fontHeight( textureBrowser ) + 4;
477                 layout.current_row = 0;
478         }
479
480         *x = layout.current_x;
481         *y = layout.current_y;
482
483         // Is our texture larger than the row? If so, grow the
484         // row height to match it
485
486         if ( layout.current_row < nHeight ) {
487                 layout.current_row = nHeight;
488         }
489
490         // never go less than 96, or the names get all crunched up
491         layout.current_x += nWidth < 96 ? 96 : nWidth;
492         layout.current_x += 8;
493 }
494
495 bool TextureSearch_IsShown( const char* name ){
496         std::set<CopiedString>::iterator iter;
497
498         iter = GlobalTextureBrowser().m_found_shaders.find( name );
499
500         if ( iter == GlobalTextureBrowser().m_found_shaders.end() ) {
501                 return false;
502         }
503         else {
504                 return true;
505         }
506 }
507
508 // if texture_showinuse jump over non in-use textures
509 bool Texture_IsShown( IShader* shader, bool show_shaders, bool hideUnused ){
510         // filter missing shaders
511         // ugly: filter on built-in fallback name after substitution
512         if ( g_TextureBrowser_filterMissing ) {
513                 if ( isMissing( shader->getTexture()->name ) ) {
514                         return false;
515                 }
516         }
517         // filter the fallback (notex/shadernotex) for missing shaders or editor image
518         if ( g_TextureBrowser_filterFallback ) {
519                 if ( isNotex( shader->getName() ) ) {
520                         return false;
521                 }
522                 if ( isNotex( shader->getTexture()->name ) ) {
523                         return false;
524                 }
525         }
526
527         if ( g_TextureBrowser_currentDirectory == "Untagged" ) {
528                 std::set<CopiedString>::iterator iter;
529
530                 iter = GlobalTextureBrowser().m_found_shaders.find( shader->getName() );
531
532                 if ( iter == GlobalTextureBrowser().m_found_shaders.end() ) {
533                         return false;
534                 }
535                 else {
536                         return true;
537                 }
538         }
539
540         if ( !shader_equal_prefix( shader->getName(), "textures/" ) ) {
541                 return false;
542         }
543
544         if ( !show_shaders && !shader->IsDefault() ) {
545                 return false;
546         }
547
548         if ( hideUnused && !shader->IsInUse() ) {
549                 return false;
550         }
551
552         if ( GlobalTextureBrowser().m_searchedTags ) {
553                 if ( !TextureSearch_IsShown( shader->getName() ) ) {
554                         return false;
555                 }
556                 else {
557                         return true;
558                 }
559         }
560         else {
561                 if ( !shader_equal_prefix( shader_get_textureName( shader->getName() ), g_TextureBrowser_currentDirectory.c_str() ) ) {
562                         return false;
563                 }
564         }
565
566         return true;
567 }
568
569 void TextureBrowser_heightChanged( TextureBrowser& textureBrowser ){
570         textureBrowser.m_heightChanged = true;
571
572         TextureBrowser_updateScroll( textureBrowser );
573         TextureBrowser_queueDraw( textureBrowser );
574 }
575
576 void TextureBrowser_evaluateHeight( TextureBrowser& textureBrowser ){
577         if ( textureBrowser.m_heightChanged ) {
578                 textureBrowser.m_heightChanged = false;
579
580                 textureBrowser.m_nTotalHeight = 0;
581
582                 TextureLayout layout;
583                 Texture_StartPos( layout );
584                 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
585                 {
586                         IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
587
588                         if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
589                                 continue;
590                         }
591
592                         int x, y;
593                         Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
594                         textureBrowser.m_nTotalHeight = std::max( textureBrowser.m_nTotalHeight, abs( layout.current_y ) + TextureBrowser_fontHeight( textureBrowser ) + textureBrowser.getTextureHeight( shader->getTexture() ) + 4 );
595                 }
596         }
597 }
598
599 int TextureBrowser_TotalHeight( TextureBrowser& textureBrowser ){
600         TextureBrowser_evaluateHeight( textureBrowser );
601         return textureBrowser.m_nTotalHeight;
602 }
603
604 inline const int& min_int( const int& left, const int& right ){
605         return std::min( left, right );
606 }
607
608 void TextureBrowser_clampOriginY( TextureBrowser& textureBrowser ){
609         if ( textureBrowser.originy > 0 ) {
610                 textureBrowser.originy = 0;
611         }
612         int lower = min_int( textureBrowser.height - TextureBrowser_TotalHeight( textureBrowser ), 0 );
613         if ( textureBrowser.originy < lower ) {
614                 textureBrowser.originy = lower;
615         }
616 }
617
618 int TextureBrowser_getOriginY( TextureBrowser& textureBrowser ){
619         if ( textureBrowser.m_originInvalid ) {
620                 textureBrowser.m_originInvalid = false;
621                 TextureBrowser_clampOriginY( textureBrowser );
622                 TextureBrowser_updateScroll( textureBrowser );
623         }
624         return textureBrowser.originy;
625 }
626
627 void TextureBrowser_setOriginY( TextureBrowser& textureBrowser, int originy ){
628         textureBrowser.originy = originy;
629         TextureBrowser_clampOriginY( textureBrowser );
630         TextureBrowser_updateScroll( textureBrowser );
631         TextureBrowser_queueDraw( textureBrowser );
632 }
633
634
635 Signal0 g_activeShadersChangedCallbacks;
636
637 void TextureBrowser_addActiveShadersChangedCallback( const SignalHandler& handler ){
638         g_activeShadersChangedCallbacks.connectLast( handler );
639 }
640
641 void TextureBrowser_constructTreeStore();
642
643 class ShadersObserver : public ModuleObserver
644 {
645 Signal0 m_realiseCallbacks;
646 public:
647 void realise(){
648         m_realiseCallbacks();
649         TextureBrowser_constructTreeStore();
650 }
651 void unrealise(){
652 }
653 void insert( const SignalHandler& handler ){
654         m_realiseCallbacks.connectLast( handler );
655 }
656 };
657
658 namespace
659 {
660 ShadersObserver g_ShadersObserver;
661 }
662
663 void TextureBrowser_addShadersRealiseCallback( const SignalHandler& handler ){
664         g_ShadersObserver.insert( handler );
665 }
666
667 void TextureBrowser_activeShadersChanged( TextureBrowser& textureBrowser ){
668         TextureBrowser_heightChanged( textureBrowser );
669         textureBrowser.m_originInvalid = true;
670
671         g_activeShadersChangedCallbacks();
672 }
673
674 void TextureBrowser_importShowScrollbar( TextureBrowser& textureBrowser, bool value ){
675         textureBrowser.m_showTextureScrollbar = value;
676         if ( textureBrowser.m_texture_scroll != 0 ) {
677                 widget_set_visible( textureBrowser.m_texture_scroll, textureBrowser.m_showTextureScrollbar );
678                 TextureBrowser_updateScroll( textureBrowser );
679         }
680 }
681 typedef ReferenceCaller1<TextureBrowser, bool, TextureBrowser_importShowScrollbar> TextureBrowserImportShowScrollbarCaller;
682
683
684 /*
685    ==============
686    TextureBrowser_ShowDirectory
687    relies on texture_directory global for the directory to use
688    1) Load the shaders for the given directory
689    2) Scan the remaining texture, load them and assign them a default shader (the "noshader" shader)
690    NOTE: when writing a texture plugin, or some texture extensions, this function may need to be overriden, and made
691    available through the IShaders interface
692    NOTE: for texture window layout:
693    all shaders are stored with alphabetical order after load
694    previously loaded and displayed stuff is hidden, only in-use and newly loaded is shown
695    ( the GL textures are not flushed though)
696    ==============
697  */
698
699 bool endswith( const char *haystack, const char *needle ){
700         size_t lh = strlen( haystack );
701         size_t ln = strlen( needle );
702         if ( lh < ln ) {
703                 return false;
704         }
705         return !memcmp( haystack + ( lh - ln ), needle, ln );
706 }
707
708 bool texture_name_ignore( const char* name ){
709         StringOutputStream strTemp( string_length( name ) );
710         strTemp << LowerCase( name );
711
712         return
713                 endswith( strTemp.c_str(), ".specular" ) ||
714                 endswith( strTemp.c_str(), ".glow" ) ||
715                 endswith( strTemp.c_str(), ".bump" ) ||
716                 endswith( strTemp.c_str(), ".diffuse" ) ||
717                 endswith( strTemp.c_str(), ".blend" ) ||
718                 endswith( strTemp.c_str(), ".alpha" ) ||
719                 endswith( strTemp.c_str(), "_norm" ) ||
720                 endswith( strTemp.c_str(), "_bump" ) ||
721                 endswith( strTemp.c_str(), "_glow" ) ||
722                 endswith( strTemp.c_str(), "_gloss" ) ||
723                 endswith( strTemp.c_str(), "_pants" ) ||
724                 endswith( strTemp.c_str(), "_shirt" ) ||
725                 endswith( strTemp.c_str(), "_reflect" ) ||
726                 endswith( strTemp.c_str(), "_alpha" ) ||
727                 0;
728 }
729
730 class LoadShaderVisitor : public Archive::Visitor
731 {
732 public:
733 void visit( const char* name ){
734         IShader* shader = QERApp_Shader_ForName( CopiedString( StringRange( name, path_get_filename_base_end( name ) ) ).c_str() );
735         shader->DecRef();
736 }
737 };
738
739 void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused );
740
741 ui::Widget g_page_textures{ui::null};
742
743 void TextureBrowser_toggleShow(){
744         GroupDialog_showPage( g_page_textures );
745 }
746
747
748 void TextureBrowser_updateTitle(){
749         GroupDialog_updatePageTitle( g_page_textures );
750 }
751
752
753
754 class TextureCategoryLoadShader
755 {
756 const char* m_directory;
757 std::size_t& m_count;
758 public:
759 typedef const char* first_argument_type;
760
761 TextureCategoryLoadShader( const char* directory, std::size_t& count )
762         : m_directory( directory ), m_count( count ){
763         m_count = 0;
764 }
765 void operator()( const char* name ) const {
766         if ( shader_equal_prefix( name, "textures/" )
767                  && shader_equal_prefix( name + string_length( "textures/" ), m_directory ) ) {
768                 ++m_count;
769                 // request the shader, this will load the texture if needed
770                 // this Shader_ForName call is a kind of hack
771                 IShader *pFoo = QERApp_Shader_ForName( name );
772                 pFoo->DecRef();
773         }
774 }
775 };
776
777 void TextureDirectory_loadTexture( const char* directory, const char* texture ){
778         StringOutputStream name( 256 );
779         name << directory << StringRange( texture, path_get_filename_base_end( texture ) );
780
781         if ( texture_name_ignore( name.c_str() ) ) {
782                 return;
783         }
784
785         if ( !shader_valid( name.c_str() ) ) {
786                 globalOutputStream() << "Skipping invalid texture name: [" << name.c_str() << "]\n";
787                 return;
788         }
789
790         // if a texture is already in use to represent a shader, ignore it
791         IShader* shader = QERApp_Shader_ForName( name.c_str() );
792         shader->DecRef();
793 }
794 typedef ConstPointerCaller1<char, const char*, TextureDirectory_loadTexture> TextureDirectoryLoadTextureCaller;
795
796 class LoadTexturesByTypeVisitor : public ImageModules::Visitor
797 {
798 const char* m_dirstring;
799 public:
800 LoadTexturesByTypeVisitor( const char* dirstring )
801         : m_dirstring( dirstring ){
802 }
803 void visit( const char* minor, const _QERPlugImageTable& table ) const {
804         GlobalFileSystem().forEachFile( m_dirstring, minor, TextureDirectoryLoadTextureCaller( m_dirstring ) );
805 }
806 };
807
808 void TextureBrowser_ShowDirectory( TextureBrowser& textureBrowser, const char* directory ){
809         if ( TextureBrowser_showWads() ) {
810                 Archive* archive = GlobalFileSystem().getArchive( directory );
811                 ASSERT_NOTNULL( archive );
812                 LoadShaderVisitor visitor;
813                 archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eFiles, 0 ), "textures/" );
814         }
815         else
816         {
817                 g_TextureBrowser_currentDirectory = directory;
818                 TextureBrowser_heightChanged( textureBrowser );
819
820                 std::size_t shaders_count;
821                 GlobalShaderSystem().foreachShaderName( makeCallback1( TextureCategoryLoadShader( directory, shaders_count ) ) );
822                 globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
823
824                 if ( g_pGameDescription->mGameType != "doom3" ) {
825                         // load remaining texture files
826
827                         StringOutputStream dirstring( 64 );
828                         dirstring << "textures/" << directory;
829
830                         Radiant_getImageModules().foreachModule( LoadTexturesByTypeVisitor( dirstring.c_str() ) );
831                 }
832         }
833
834         // we'll display the newly loaded textures + all the ones already in use
835         TextureBrowser_SetHideUnused( textureBrowser, false );
836
837         TextureBrowser_updateTitle();
838 }
839
840 void TextureBrowser_ShowTagSearchResult( TextureBrowser& textureBrowser, const char* directory ){
841         g_TextureBrowser_currentDirectory = directory;
842         TextureBrowser_heightChanged( textureBrowser );
843
844         std::size_t shaders_count;
845         GlobalShaderSystem().foreachShaderName( makeCallback1( TextureCategoryLoadShader( directory, shaders_count ) ) );
846         globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
847
848         if ( g_pGameDescription->mGameType != "doom3" ) {
849                 // load remaining texture files
850                 StringOutputStream dirstring( 64 );
851                 dirstring << "textures/" << directory;
852
853                 {
854                         LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
855                         Radiant_getImageModules().foreachModule( visitor );
856                 }
857         }
858
859         // we'll display the newly loaded textures + all the ones already in use
860         TextureBrowser_SetHideUnused( textureBrowser, false );
861 }
862
863
864 bool TextureBrowser_hideUnused();
865
866 void TextureBrowser_hideUnusedExport( const BoolImportCallback& importer ){
867         importer( TextureBrowser_hideUnused() );
868 }
869 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_hideUnusedExport> TextureBrowserHideUnusedExport;
870
871 void TextureBrowser_showShadersExport( const BoolImportCallback& importer ){
872         importer( GlobalTextureBrowser().m_showShaders );
873 }
874 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_showShadersExport> TextureBrowserShowShadersExport;
875
876 void TextureBrowser_showShaderlistOnly( const BoolImportCallback& importer ){
877         importer( g_TextureBrowser_shaderlistOnly );
878 }
879 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_showShaderlistOnly> TextureBrowserShowShaderlistOnlyExport;
880
881 void TextureBrowser_fixedSize( const BoolImportCallback& importer ){
882         importer( g_TextureBrowser_fixedSize );
883 }
884 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_fixedSize> TextureBrowser_FixedSizeExport;
885
886 void TextureBrowser_filterMissing( const BoolImportCallback& importer ){
887         importer( g_TextureBrowser_filterMissing );
888 }
889 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_filterMissing> TextureBrowser_filterMissingExport;
890
891 void TextureBrowser_filterFallback( const BoolImportCallback& importer ){
892         importer( g_TextureBrowser_filterFallback );
893 }
894 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_filterFallback> TextureBrowser_filterFallbackExport;
895
896 void TextureBrowser_enableAlpha( const BoolImportCallback& importer ){
897         importer( g_TextureBrowser_enableAlpha );
898 }
899 typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_enableAlpha> TextureBrowser_enableAlphaExport;
900
901 void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused ){
902         if ( hideUnused ) {
903                 textureBrowser.m_hideUnused = true;
904         }
905         else
906         {
907                 textureBrowser.m_hideUnused = false;
908         }
909
910         textureBrowser.m_hideunused_item.update();
911
912         TextureBrowser_heightChanged( textureBrowser );
913         textureBrowser.m_originInvalid = true;
914 }
915
916 void TextureBrowser_ShowStartupShaders( TextureBrowser& textureBrowser ){
917         if ( textureBrowser.m_startupShaders == STARTUPSHADERS_COMMON ) {
918                 TextureBrowser_ShowDirectory( textureBrowser, TextureBrowser_getComonShadersDir() );
919         }
920 }
921
922
923 //++timo NOTE: this is a mix of Shader module stuff and texture explorer
924 // it might need to be split in parts or moved out .. dunno
925 // scroll origin so the specified texture is completely on screen
926 // if current texture is not displayed, nothing is changed
927 void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name ){
928         TextureLayout layout;
929         // scroll origin so the texture is completely on screen
930         Texture_StartPos( layout );
931
932         for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
933         {
934                 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
935
936                 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
937                         continue;
938                 }
939
940                 int x, y;
941                 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
942                 qtexture_t* q = shader->getTexture();
943                 if ( !q ) {
944                         break;
945                 }
946
947                 // we have found when texdef->name and the shader name match
948                 // NOTE: as everywhere else for our comparisons, we are not case sensitive
949                 if ( shader_equal( name, shader->getName() ) ) {
950                         int textureHeight = (int)( q->height * ( (float)textureBrowser.m_textureScale / 100 ) )
951                                                                 + 2 * TextureBrowser_fontHeight( textureBrowser );
952
953                         int originy = TextureBrowser_getOriginY( textureBrowser );
954                         if ( y > originy ) {
955                                 originy = y;
956                         }
957
958                         if ( y - textureHeight < originy - textureBrowser.height ) {
959                                 originy = ( y - textureHeight ) + textureBrowser.height;
960                         }
961
962                         TextureBrowser_setOriginY( textureBrowser, originy );
963                         return;
964                 }
965         }
966 }
967
968 IShader* Texture_At( TextureBrowser& textureBrowser, int mx, int my ){
969         my += TextureBrowser_getOriginY( textureBrowser ) - textureBrowser.height;
970
971         TextureLayout layout;
972         Texture_StartPos( layout );
973         for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
974         {
975                 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
976
977                 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
978                         continue;
979                 }
980
981                 int x, y;
982                 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
983                 qtexture_t  *q = shader->getTexture();
984                 if ( !q ) {
985                         break;
986                 }
987
988                 int nWidth = textureBrowser.getTextureWidth( q );
989                 int nHeight = textureBrowser.getTextureHeight( q );
990                 if ( mx > x && mx - x < nWidth
991                          && my < y && y - my < nHeight + TextureBrowser_fontHeight( textureBrowser ) ) {
992                         return shader;
993                 }
994         }
995
996         return 0;
997 }
998
999 /*
1000    ==============
1001    SelectTexture
1002
1003    By mouse click
1004    ==============
1005  */
1006 void SelectTexture( TextureBrowser& textureBrowser, int mx, int my, bool bShift ){
1007         IShader* shader = Texture_At( textureBrowser, mx, my );
1008         if ( shader != 0 ) {
1009                 if ( bShift ) {
1010                         if ( shader->IsDefault() ) {
1011                                 globalOutputStream() << "ERROR: " << shader->getName() << " is not a shader, it's a texture.\n";
1012                         }
1013                         else{
1014                                 ViewShader( shader->getShaderFileName(), shader->getName() );
1015                         }
1016                 }
1017                 else
1018                 {
1019                         TextureBrowser_SetSelectedShader( textureBrowser, shader->getName() );
1020                         TextureBrowser_textureSelected( shader->getName() );
1021
1022                         if ( !FindTextureDialog_isOpen() && !textureBrowser.m_rmbSelected ) {
1023                                 UndoableCommand undo( "textureNameSetSelected" );
1024                                 Select_SetShader( shader->getName() );
1025                         }
1026                 }
1027         }
1028 }
1029
1030 /*
1031    ============================================================================
1032
1033    MOUSE ACTIONS
1034
1035    ============================================================================
1036  */
1037
1038 void TextureBrowser_trackingDelta( int x, int y, unsigned int state, void* data ){
1039         TextureBrowser& textureBrowser = *reinterpret_cast<TextureBrowser*>( data );
1040         if ( y != 0 ) {
1041                 int scale = 1;
1042
1043                 if ( state & GDK_SHIFT_MASK ) {
1044                         scale = 4;
1045                 }
1046
1047                 int originy = TextureBrowser_getOriginY( textureBrowser );
1048                 originy += y * scale;
1049                 TextureBrowser_setOriginY( textureBrowser, originy );
1050         }
1051 }
1052
1053 void TextureBrowser_Tracking_MouseDown( TextureBrowser& textureBrowser ){
1054         textureBrowser.m_freezePointer.freeze_pointer( textureBrowser.m_parent, TextureBrowser_trackingDelta, &textureBrowser );
1055 }
1056
1057 void TextureBrowser_Tracking_MouseUp( TextureBrowser& textureBrowser ){
1058         textureBrowser.m_freezePointer.unfreeze_pointer( textureBrowser.m_parent );
1059 }
1060
1061 void TextureBrowser_Selection_MouseDown( TextureBrowser& textureBrowser, guint32 flags, int pointx, int pointy ){
1062         SelectTexture( textureBrowser, pointx, textureBrowser.height - 1 - pointy, ( flags & GDK_SHIFT_MASK ) != 0 );
1063 }
1064
1065 /*
1066    ============================================================================
1067
1068    DRAWING
1069
1070    ============================================================================
1071  */
1072
1073 /*
1074    ============
1075    Texture_Draw
1076    TTimo: relying on the shaders list to display the textures
1077    we must query all qtexture_t* to manage and display through the IShaders interface
1078    this allows a plugin to completely override the texture system
1079    ============
1080  */
1081 void Texture_Draw( TextureBrowser& textureBrowser ){
1082         int originy = TextureBrowser_getOriginY( textureBrowser );
1083
1084         glClearColor( textureBrowser.color_textureback[0],
1085                                   textureBrowser.color_textureback[1],
1086                                   textureBrowser.color_textureback[2],
1087                                   0 );
1088         glViewport( 0, 0, textureBrowser.width, textureBrowser.height );
1089         glMatrixMode( GL_PROJECTION );
1090         glLoadIdentity();
1091
1092         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1093         glDisable( GL_DEPTH_TEST );
1094         if ( g_TextureBrowser_enableAlpha ) {
1095                 glEnable( GL_BLEND );
1096                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1097         }
1098         else {
1099                 glDisable( GL_BLEND );
1100         }
1101         glOrtho( 0, textureBrowser.width, originy - textureBrowser.height, originy, -100, 100 );
1102         glEnable( GL_TEXTURE_2D );
1103
1104         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1105
1106         int last_y = 0, last_height = 0;
1107
1108         TextureLayout layout;
1109         Texture_StartPos( layout );
1110         for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
1111         {
1112                 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
1113
1114                 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
1115                         continue;
1116                 }
1117
1118                 int x, y;
1119                 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
1120                 qtexture_t *q = shader->getTexture();
1121                 if ( !q ) {
1122                         break;
1123                 }
1124
1125                 int nWidth = textureBrowser.getTextureWidth( q );
1126                 int nHeight = textureBrowser.getTextureHeight( q );
1127
1128                 if ( y != last_y ) {
1129                         last_y = y;
1130                         last_height = 0;
1131                 }
1132                 last_height = std::max( nHeight, last_height );
1133
1134                 // Is this texture visible?
1135                 if ( ( y - nHeight - TextureBrowser_fontHeight( textureBrowser ) < originy )
1136                          && ( y > originy - textureBrowser.height ) ) {
1137                         // borders rules:
1138                         // if it's the current texture, draw a thick red line, else:
1139                         // shaders have a white border, simple textures don't
1140                         // if !texture_showinuse: (some textures displayed may not be in use)
1141                         // draw an additional square around with 0.5 1 0.5 color
1142                         if ( shader_equal( TextureBrowser_GetSelectedShader( textureBrowser ), shader->getName() ) ) {
1143                                 glLineWidth( 3 );
1144                                 if ( textureBrowser.m_rmbSelected ) {
1145                                         glColor3f( 0,0,1 );
1146                                 }
1147                                 else {
1148                                         glColor3f( 1,0,0 );
1149                                 }
1150                                 glDisable( GL_TEXTURE_2D );
1151
1152                                 glBegin( GL_LINE_LOOP );
1153                                 glVertex2i( x - 4,y - TextureBrowser_fontHeight( textureBrowser ) + 4 );
1154                                 glVertex2i( x - 4,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight - 4 );
1155                                 glVertex2i( x + 4 + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight - 4 );
1156                                 glVertex2i( x + 4 + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) + 4 );
1157                                 glEnd();
1158
1159                                 glEnable( GL_TEXTURE_2D );
1160                                 glLineWidth( 1 );
1161                         }
1162                         else
1163                         {
1164                                 glLineWidth( 1 );
1165                                 // shader border:
1166                                 if ( !shader->IsDefault() ) {
1167                                         glColor3f( 1,1,1 );
1168                                         glDisable( GL_TEXTURE_2D );
1169
1170                                         glBegin( GL_LINE_LOOP );
1171                                         glVertex2i( x - 1,y + 1 - TextureBrowser_fontHeight( textureBrowser ) );
1172                                         glVertex2i( x - 1,y - nHeight - 1 - TextureBrowser_fontHeight( textureBrowser ) );
1173                                         glVertex2i( x + 1 + nWidth,y - nHeight - 1 - TextureBrowser_fontHeight( textureBrowser ) );
1174                                         glVertex2i( x + 1 + nWidth,y + 1 - TextureBrowser_fontHeight( textureBrowser ) );
1175                                         glEnd();
1176                                         glEnable( GL_TEXTURE_2D );
1177                                 }
1178
1179                                 // highlight in-use textures
1180                                 if ( !textureBrowser.m_hideUnused && shader->IsInUse() ) {
1181                                         glColor3f( 0.5,1,0.5 );
1182                                         glDisable( GL_TEXTURE_2D );
1183                                         glBegin( GL_LINE_LOOP );
1184                                         glVertex2i( x - 3,y + 3 - TextureBrowser_fontHeight( textureBrowser ) );
1185                                         glVertex2i( x - 3,y - nHeight - 3 - TextureBrowser_fontHeight( textureBrowser ) );
1186                                         glVertex2i( x + 3 + nWidth,y - nHeight - 3 - TextureBrowser_fontHeight( textureBrowser ) );
1187                                         glVertex2i( x + 3 + nWidth,y + 3 - TextureBrowser_fontHeight( textureBrowser ) );
1188                                         glEnd();
1189                                         glEnable( GL_TEXTURE_2D );
1190                                 }
1191                         }
1192
1193                         // draw checkerboard for transparent textures
1194                         if ( g_TextureBrowser_enableAlpha )
1195                         {
1196                                 glDisable( GL_TEXTURE_2D );
1197                                 glBegin( GL_QUADS );
1198                                 int font_height = TextureBrowser_fontHeight( textureBrowser );
1199                                 for ( int i = 0; i < nHeight; i += 8 )
1200                                         for ( int j = 0; j < nWidth; j += 8 )
1201                                         {
1202                                                 unsigned char color = (i + j) / 8 % 2 ? 0x66 : 0x99;
1203                                                 glColor3ub( color, color, color );
1204                                                 int left = j;
1205                                                 int right = std::min(j+8, nWidth);
1206                                                 int top = i;
1207                                                 int bottom = std::min(i+8, nHeight);
1208                                                 glVertex2i(x + right, y - nHeight - font_height + top);
1209                                                 glVertex2i(x + left,  y - nHeight - font_height + top);
1210                                                 glVertex2i(x + left,  y - nHeight - font_height + bottom);
1211                                                 glVertex2i(x + right, y - nHeight - font_height + bottom);
1212                                         }
1213                                 glEnd();
1214                                 glEnable( GL_TEXTURE_2D );
1215                         }
1216
1217                         // Draw the texture
1218                         glBindTexture( GL_TEXTURE_2D, q->texture_number );
1219                         GlobalOpenGL_debugAssertNoErrors();
1220                         glColor3f( 1,1,1 );
1221                         glBegin( GL_QUADS );
1222                         glTexCoord2i( 0,0 );
1223                         glVertex2i( x,y - TextureBrowser_fontHeight( textureBrowser ) );
1224                         glTexCoord2i( 1,0 );
1225                         glVertex2i( x + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) );
1226                         glTexCoord2i( 1,1 );
1227                         glVertex2i( x + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight );
1228                         glTexCoord2i( 0,1 );
1229                         glVertex2i( x,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight );
1230                         glEnd();
1231
1232                         // draw the texture name
1233                         glDisable( GL_TEXTURE_2D );
1234                         glColor3f( 1,1,1 );
1235
1236                         glRasterPos2i( x, y - TextureBrowser_fontHeight( textureBrowser ) + 5 );
1237
1238                         // don't draw the directory name
1239                         const char* name = shader->getName();
1240                         name += strlen( name );
1241                         while ( name != shader->getName() && *( name - 1 ) != '/' && *( name - 1 ) != '\\' )
1242                                 name--;
1243
1244                         GlobalOpenGL().drawString( name );
1245                         glEnable( GL_TEXTURE_2D );
1246                 }
1247
1248                 //int totalHeight = abs(y) + last_height + TextureBrowser_fontHeight(textureBrowser) + 4;
1249         }
1250
1251
1252         // reset the current texture
1253         glBindTexture( GL_TEXTURE_2D, 0 );
1254         //qglFinish();
1255 }
1256
1257 void TextureBrowser_queueDraw( TextureBrowser& textureBrowser ){
1258         if ( textureBrowser.m_gl_widget ) {
1259                 gtk_widget_queue_draw( textureBrowser.m_gl_widget );
1260         }
1261 }
1262
1263
1264 void TextureBrowser_setScale( TextureBrowser& textureBrowser, std::size_t scale ){
1265         textureBrowser.m_textureScale = scale;
1266
1267         TextureBrowser_queueDraw( textureBrowser );
1268 }
1269
1270 void TextureBrowser_setUniformSize( TextureBrowser& textureBrowser, std::size_t scale ){
1271         textureBrowser.m_uniformTextureSize = scale;
1272
1273         TextureBrowser_queueDraw( textureBrowser );
1274 }
1275
1276
1277 void TextureBrowser_MouseWheel( TextureBrowser& textureBrowser, bool bUp ){
1278         int originy = TextureBrowser_getOriginY( textureBrowser );
1279
1280         if ( bUp ) {
1281                 originy += int(textureBrowser.m_mouseWheelScrollIncrement);
1282         }
1283         else
1284         {
1285                 originy -= int(textureBrowser.m_mouseWheelScrollIncrement);
1286         }
1287
1288         TextureBrowser_setOriginY( textureBrowser, originy );
1289 }
1290
1291 XmlTagBuilder TagBuilder;
1292
1293 enum
1294 {
1295         TAG_COLUMN,
1296         N_COLUMNS
1297 };
1298
1299 void BuildStoreAssignedTags( ui::ListStore store, const char* shader, TextureBrowser* textureBrowser ){
1300         GtkTreeIter iter;
1301
1302         gtk_list_store_clear( store );
1303
1304         std::vector<CopiedString> assigned_tags;
1305         TagBuilder.GetShaderTags( shader, assigned_tags );
1306
1307         for ( size_t i = 0; i < assigned_tags.size(); i++ )
1308         {
1309                 gtk_list_store_append( store, &iter );
1310                 gtk_list_store_set( store, &iter, TAG_COLUMN, assigned_tags[i].c_str(), -1 );
1311         }
1312 }
1313
1314 void BuildStoreAvailableTags(   ui::ListStore storeAvailable,
1315                                                                 ui::ListStore storeAssigned,
1316                                                                 const std::set<CopiedString>& allTags,
1317                                                                 TextureBrowser* textureBrowser ){
1318         GtkTreeIter iterAssigned;
1319         GtkTreeIter iterAvailable;
1320         std::set<CopiedString>::const_iterator iterAll;
1321         gchar* tag_assigned;
1322
1323         gtk_list_store_clear( storeAvailable );
1324
1325         bool row = gtk_tree_model_get_iter_first( GTK_TREE_MODEL( storeAssigned ), &iterAssigned ) != 0;
1326
1327         if ( !row ) { // does the shader have tags assigned?
1328                 for ( iterAll = allTags.begin(); iterAll != allTags.end(); ++iterAll )
1329                 {
1330                         gtk_list_store_append( storeAvailable, &iterAvailable );
1331                         gtk_list_store_set( storeAvailable, &iterAvailable, TAG_COLUMN, ( *iterAll ).c_str(), -1 );
1332                 }
1333         }
1334         else
1335         {
1336                 while ( row ) // available tags = all tags - assigned tags
1337                 {
1338                         gtk_tree_model_get( GTK_TREE_MODEL( storeAssigned ), &iterAssigned, TAG_COLUMN, &tag_assigned, -1 );
1339
1340                         for ( iterAll = allTags.begin(); iterAll != allTags.end(); ++iterAll )
1341                         {
1342                                 if ( strcmp( (char*)tag_assigned, ( *iterAll ).c_str() ) != 0 ) {
1343                                         gtk_list_store_append( storeAvailable, &iterAvailable );
1344                                         gtk_list_store_set( storeAvailable, &iterAvailable, TAG_COLUMN, ( *iterAll ).c_str(), -1 );
1345                                 }
1346                                 else
1347                                 {
1348                                         row = gtk_tree_model_iter_next( GTK_TREE_MODEL( storeAssigned ), &iterAssigned ) != 0;
1349
1350                                         if ( row ) {
1351                                                 gtk_tree_model_get( GTK_TREE_MODEL( storeAssigned ), &iterAssigned, TAG_COLUMN, &tag_assigned, -1 );
1352                                         }
1353                                 }
1354                         }
1355                 }
1356         }
1357 }
1358
1359 gboolean TextureBrowser_button_press( ui::Widget widget, GdkEventButton* event, TextureBrowser* textureBrowser ){
1360         if ( event->type == GDK_BUTTON_PRESS ) {
1361                 if ( event->button == 3 ) {
1362                         if ( GlobalTextureBrowser().m_tags ) {
1363                                 textureBrowser->m_rmbSelected = true;
1364                                 TextureBrowser_Selection_MouseDown( *textureBrowser, event->state, static_cast<int>( event->x ), static_cast<int>( event->y ) );
1365
1366                                 BuildStoreAssignedTags( textureBrowser->m_assigned_store, textureBrowser->shader.c_str(), textureBrowser );
1367                                 BuildStoreAvailableTags( textureBrowser->m_available_store, textureBrowser->m_assigned_store, textureBrowser->m_all_tags, textureBrowser );
1368                                 textureBrowser->m_heightChanged = true;
1369                                 textureBrowser->m_tag_frame.show();
1370
1371                 ui::process();
1372
1373                                 TextureBrowser_Focus( *textureBrowser, textureBrowser->shader.c_str() );
1374                         }
1375                         else
1376                         {
1377                                 TextureBrowser_Tracking_MouseDown( *textureBrowser );
1378                         }
1379                 }
1380                 else if ( event->button == 1 ) {
1381                         TextureBrowser_Selection_MouseDown( *textureBrowser, event->state, static_cast<int>( event->x ), static_cast<int>( event->y ) );
1382
1383                         if ( GlobalTextureBrowser().m_tags ) {
1384                                 textureBrowser->m_rmbSelected = false;
1385                                 gtk_widget_hide( textureBrowser->m_tag_frame );
1386                         }
1387                 }
1388         }
1389         return FALSE;
1390 }
1391
1392 gboolean TextureBrowser_button_release( ui::Widget widget, GdkEventButton* event, TextureBrowser* textureBrowser ){
1393         if ( event->type == GDK_BUTTON_RELEASE ) {
1394                 if ( event->button == 3 ) {
1395                         if ( !GlobalTextureBrowser().m_tags ) {
1396                                 TextureBrowser_Tracking_MouseUp( *textureBrowser );
1397                         }
1398                 }
1399         }
1400         return FALSE;
1401 }
1402
1403 gboolean TextureBrowser_motion( ui::Widget widget, GdkEventMotion *event, TextureBrowser* textureBrowser ){
1404         return FALSE;
1405 }
1406
1407 gboolean TextureBrowser_scroll( ui::Widget widget, GdkEventScroll* event, TextureBrowser* textureBrowser ){
1408         if ( event->direction == GDK_SCROLL_UP ) {
1409                 TextureBrowser_MouseWheel( *textureBrowser, true );
1410         }
1411         else if ( event->direction == GDK_SCROLL_DOWN ) {
1412                 TextureBrowser_MouseWheel( *textureBrowser, false );
1413         }
1414         return FALSE;
1415 }
1416
1417 void TextureBrowser_scrollChanged( void* data, gdouble value ){
1418         //globalOutputStream() << "vertical scroll\n";
1419         TextureBrowser_setOriginY( *reinterpret_cast<TextureBrowser*>( data ), -(int)value );
1420 }
1421
1422 static void TextureBrowser_verticalScroll( GtkAdjustment *adjustment, TextureBrowser* textureBrowser ){
1423         textureBrowser->m_scrollAdjustment.value_changed( gtk_adjustment_get_value(adjustment) );
1424 }
1425
1426 void TextureBrowser_updateScroll( TextureBrowser& textureBrowser ){
1427         if ( textureBrowser.m_showTextureScrollbar ) {
1428                 int totalHeight = TextureBrowser_TotalHeight( textureBrowser );
1429
1430                 totalHeight = std::max( totalHeight, textureBrowser.height );
1431
1432                 GtkAdjustment *vadjustment = gtk_range_get_adjustment( GTK_RANGE( textureBrowser.m_texture_scroll ) );
1433
1434                 gtk_adjustment_set_value(vadjustment, -TextureBrowser_getOriginY( textureBrowser ));
1435                 gtk_adjustment_set_page_size(vadjustment, textureBrowser.height);
1436                 gtk_adjustment_set_page_increment(vadjustment, textureBrowser.height / 2);
1437                 gtk_adjustment_set_step_increment(vadjustment, 20);
1438                 gtk_adjustment_set_lower(vadjustment, 0);
1439                 gtk_adjustment_set_upper(vadjustment, totalHeight);
1440
1441                 g_signal_emit_by_name( G_OBJECT( vadjustment ), "changed" );
1442         }
1443 }
1444
1445 gboolean TextureBrowser_size_allocate( ui::Widget widget, GtkAllocation* allocation, TextureBrowser* textureBrowser ){
1446         textureBrowser->width = allocation->width;
1447         textureBrowser->height = allocation->height;
1448         TextureBrowser_heightChanged( *textureBrowser );
1449         textureBrowser->m_originInvalid = true;
1450         TextureBrowser_queueDraw( *textureBrowser );
1451         return FALSE;
1452 }
1453
1454 gboolean TextureBrowser_expose( ui::Widget widget, GdkEventExpose* event, TextureBrowser* textureBrowser ){
1455         if ( glwidget_make_current( textureBrowser->m_gl_widget ) != FALSE ) {
1456                 GlobalOpenGL_debugAssertNoErrors();
1457                 TextureBrowser_evaluateHeight( *textureBrowser );
1458                 Texture_Draw( *textureBrowser );
1459                 GlobalOpenGL_debugAssertNoErrors();
1460                 glwidget_swap_buffers( textureBrowser->m_gl_widget );
1461         }
1462         return FALSE;
1463 }
1464
1465
1466 TextureBrowser g_TextureBrowser;
1467
1468 TextureBrowser& GlobalTextureBrowser(){
1469         return g_TextureBrowser;
1470 }
1471
1472 bool TextureBrowser_hideUnused(){
1473         return g_TextureBrowser.m_hideUnused;
1474 }
1475
1476 void TextureBrowser_ToggleHideUnused(){
1477         if ( g_TextureBrowser.m_hideUnused ) {
1478                 TextureBrowser_SetHideUnused( g_TextureBrowser, false );
1479         }
1480         else
1481         {
1482                 TextureBrowser_SetHideUnused( g_TextureBrowser, true );
1483         }
1484 }
1485
1486 void TextureGroups_constructTreeModel( TextureGroups groups, GtkTreeStore* store ){
1487         // put the information from the old textures menu into a treeview
1488         GtkTreeIter iter, child;
1489
1490         TextureGroups::const_iterator i = groups.begin();
1491         while ( i != groups.end() )
1492         {
1493                 const char* dirName = ( *i ).c_str();
1494                 const char* firstUnderscore = strchr( dirName, '_' );
1495                 StringRange dirRoot( dirName, ( firstUnderscore == 0 ) ? dirName : firstUnderscore + 1 );
1496
1497                 TextureGroups::const_iterator next = i;
1498                 ++next;
1499                 if ( firstUnderscore != 0
1500                          && next != groups.end()
1501                          && string_equal_start( ( *next ).c_str(), dirRoot ) ) {
1502                         gtk_tree_store_append( store, &iter, NULL );
1503                         gtk_tree_store_set( store, &iter, 0, CopiedString( StringRange( dirName, firstUnderscore ) ).c_str(), -1 );
1504
1505                         // keep going...
1506                         while ( i != groups.end() && string_equal_start( ( *i ).c_str(), dirRoot ) )
1507                         {
1508                                 gtk_tree_store_append( store, &child, &iter );
1509                                 gtk_tree_store_set( store, &child, 0, ( *i ).c_str(), -1 );
1510                                 ++i;
1511                         }
1512                 }
1513                 else
1514                 {
1515                         gtk_tree_store_append( store, &iter, NULL );
1516                         gtk_tree_store_set( store, &iter, 0, dirName, -1 );
1517                         ++i;
1518                 }
1519         }
1520 }
1521
1522 TextureGroups TextureGroups_constructTreeView(){
1523         TextureGroups groups;
1524
1525         if ( TextureBrowser_showWads() ) {
1526                 GlobalFileSystem().forEachArchive( TextureGroupsAddWadCaller( groups ) );
1527         }
1528         else
1529         {
1530                 // scan texture dirs and pak files only if not restricting to shaderlist
1531                 if ( g_pGameDescription->mGameType != "doom3" && !g_TextureBrowser_shaderlistOnly ) {
1532                         GlobalFileSystem().forEachDirectory( "textures/", TextureGroupsAddDirectoryCaller( groups ) );
1533                 }
1534
1535                 GlobalShaderSystem().foreachShaderName( TextureGroupsAddShaderCaller( groups ) );
1536         }
1537
1538         return groups;
1539 }
1540
1541 void TextureBrowser_constructTreeStore(){
1542         TextureGroups groups = TextureGroups_constructTreeView();
1543         GtkTreeStore* store = gtk_tree_store_new( 1, G_TYPE_STRING );
1544         TextureGroups_constructTreeModel( groups, store );
1545         std::set<CopiedString>::iterator iter;
1546
1547         GtkTreeModel* model = GTK_TREE_MODEL( store );
1548
1549         gtk_tree_view_set_model( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTree ), model );
1550
1551         g_object_unref( G_OBJECT( store ) );
1552 }
1553
1554 void TextureBrowser_constructTreeStoreTags(){
1555         TextureGroups groups;
1556         GtkTreeStore* store = gtk_tree_store_new( 1, G_TYPE_STRING );
1557         GtkTreeModel* model = GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list );
1558
1559         gtk_tree_view_set_model( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ), model );
1560
1561         g_object_unref( G_OBJECT( store ) );
1562 }
1563
1564 void TreeView_onRowActivated( GtkTreeView* treeview, GtkTreePath* path, GtkTreeViewColumn* col, gpointer userdata ){
1565         GtkTreeIter iter;
1566
1567         GtkTreeModel* model = gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) );
1568
1569         if ( gtk_tree_model_get_iter( model, &iter, path ) ) {
1570                 gchar dirName[1024];
1571
1572                 gchar* buffer;
1573                 gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
1574                 strcpy( dirName, buffer );
1575                 g_free( buffer );
1576
1577                 g_TextureBrowser.m_searchedTags = false;
1578
1579                 if ( !TextureBrowser_showWads() ) {
1580                         strcat( dirName, "/" );
1581                 }
1582
1583                 ScopeDisableScreenUpdates disableScreenUpdates( dirName, "Loading Textures" );
1584                 TextureBrowser_ShowDirectory( GlobalTextureBrowser(), dirName );
1585                 TextureBrowser_queueDraw( GlobalTextureBrowser() );
1586         }
1587 }
1588
1589 void TextureBrowser_createTreeViewTree(){
1590         g_TextureBrowser.m_treeViewTree = ui::TreeView(ui::New);
1591         gtk_tree_view_set_enable_search( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTree ), FALSE );
1592
1593         gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTree ), FALSE );
1594         g_TextureBrowser.m_treeViewTree.connect( "row-activated", (GCallback) TreeView_onRowActivated, NULL );
1595
1596         auto renderer = ui::CellRendererText(ui::New);
1597         gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTree ), -1, "", renderer, "text", 0, NULL );
1598
1599         TextureBrowser_constructTreeStore();
1600 }
1601
1602 void TextureBrowser_addTag();
1603 void TextureBrowser_renameTag();
1604 void TextureBrowser_deleteTag();
1605
1606 void TextureBrowser_createContextMenu( ui::Widget treeview, GdkEventButton *event ){
1607         ui::Widget menu = ui::Menu(ui::New);
1608
1609         ui::Widget menuitem = ui::MenuItem( "Add tag" );
1610         menuitem.connect( "activate", (GCallback)TextureBrowser_addTag, treeview );
1611         gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1612
1613         menuitem = ui::MenuItem( "Rename tag" );
1614         menuitem.connect( "activate", (GCallback)TextureBrowser_renameTag, treeview );
1615         gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1616
1617         menuitem = ui::MenuItem( "Delete tag" );
1618         menuitem.connect( "activate", (GCallback)TextureBrowser_deleteTag, treeview );
1619         gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1620
1621         gtk_widget_show_all( menu );
1622
1623         gtk_menu_popup( GTK_MENU( menu ), NULL, NULL, NULL, NULL,
1624                                         ( event != NULL ) ? event->button : 0,
1625                                         gdk_event_get_time( (GdkEvent*)event ) );
1626 }
1627
1628 gboolean TreeViewTags_onButtonPressed( ui::Widget treeview, GdkEventButton *event ){
1629         if ( event->type == GDK_BUTTON_PRESS && event->button == 3 ) {
1630                 GtkTreePath *path;
1631                 GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( treeview ) );
1632
1633                 if ( gtk_tree_view_get_path_at_pos( GTK_TREE_VIEW( treeview ), event->x, event->y, &path, NULL, NULL, NULL ) ) {
1634                         gtk_tree_selection_unselect_all( selection );
1635                         gtk_tree_selection_select_path( selection, path );
1636                         gtk_tree_path_free( path );
1637                 }
1638
1639                 TextureBrowser_createContextMenu( treeview, event );
1640                 return TRUE;
1641         }
1642         return FALSE;
1643 }
1644
1645 void TextureBrowser_createTreeViewTags(){
1646         g_TextureBrowser.m_treeViewTags = ui::TreeView(ui::New);
1647         gtk_tree_view_set_enable_search( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ), FALSE );
1648
1649         g_TextureBrowser.m_treeViewTags.connect( "button-press-event", (GCallback)TreeViewTags_onButtonPressed, NULL );
1650
1651         gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ), FALSE );
1652
1653         auto renderer = ui::CellRendererText(ui::New);
1654         gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ), -1, "", renderer, "text", 0, NULL );
1655
1656         TextureBrowser_constructTreeStoreTags();
1657 }
1658
1659 ui::MenuItem TextureBrowser_constructViewMenu( ui::Menu menu ){
1660         ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "_View" ));
1661
1662         if ( g_Layout_enableDetachableMenus.m_value ) {
1663                 menu_tearoff( menu );
1664         }
1665
1666         create_check_menu_item_with_mnemonic( menu, "Hide _Unused", "ShowInUse" );
1667         if ( string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1668                 create_check_menu_item_with_mnemonic( menu, "Hide Image Missing", "FilterMissing" );
1669         }
1670
1671         // hide notex and shadernotex on texture browser: no one wants to apply them
1672         create_check_menu_item_with_mnemonic( menu, "Hide Fallback", "FilterFallback" );
1673
1674         menu_separator( menu );
1675
1676         create_menu_item_with_mnemonic( menu, "Show All", "ShowAllTextures" );
1677
1678         // we always want to show shaders but don't want a "Show Shaders" menu for doom3 and .wad file games
1679         if ( g_pGameDescription->mGameType == "doom3" || !string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1680                 g_TextureBrowser.m_showShaders = true;
1681         }
1682         else
1683         {
1684                 create_check_menu_item_with_mnemonic( menu, "Show shaders", "ToggleShowShaders" );
1685         }
1686
1687         if ( g_pGameDescription->mGameType != "doom3" && string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1688                 create_check_menu_item_with_mnemonic( menu, "Shaders Only", "ToggleShowShaderlistOnly" );
1689         }
1690         if ( g_TextureBrowser.m_tags ) {
1691                 create_menu_item_with_mnemonic( menu, "Show Untagged", "ShowUntagged" );
1692         }
1693
1694         menu_separator( menu );
1695         create_check_menu_item_with_mnemonic( menu, "Fixed Size", "FixedSize" );
1696         create_check_menu_item_with_mnemonic( menu, "Transparency", "EnableAlpha" );
1697
1698         if ( string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1699                 menu_separator( menu );
1700                 g_TextureBrowser.m_shader_info_item = ui::Widget(GTK_WIDGET( create_menu_item_with_mnemonic( menu, "Shader Info", "ShaderInfo" ) ));
1701                 gtk_widget_set_sensitive( g_TextureBrowser.m_shader_info_item, FALSE );
1702         }
1703
1704
1705         return textures_menu_item;
1706 }
1707
1708 ui::MenuItem TextureBrowser_constructToolsMenu( ui::Menu menu ){
1709         ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "_Tools" ));
1710
1711         if ( g_Layout_enableDetachableMenus.m_value ) {
1712                 menu_tearoff( menu );
1713         }
1714
1715         create_menu_item_with_mnemonic( menu, "Flush & Reload Shaders", "RefreshShaders" );
1716         create_menu_item_with_mnemonic( menu, "Find / Replace...", "FindReplaceTextures" );
1717
1718         return textures_menu_item;
1719 }
1720
1721 ui::MenuItem TextureBrowser_constructTagsMenu( ui::Menu menu ){
1722         ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "T_ags" ));
1723
1724         if ( g_Layout_enableDetachableMenus.m_value ) {
1725                 menu_tearoff( menu );
1726         }
1727
1728         create_menu_item_with_mnemonic( menu, "Add tag", "AddTag" );
1729         create_menu_item_with_mnemonic( menu, "Rename tag", "RenameTag" );
1730         create_menu_item_with_mnemonic( menu, "Delete tag", "DeleteTag" );
1731         menu_separator( menu );
1732         create_menu_item_with_mnemonic( menu, "Copy tags from selected", "CopyTag" );
1733         create_menu_item_with_mnemonic( menu, "Paste tags to selected", "PasteTag" );
1734
1735         return textures_menu_item;
1736 }
1737
1738 gboolean TextureBrowser_tagMoveHelper( GtkTreeModel* model, GtkTreePath* path, GtkTreeIter* iter, GSList** selected ){
1739         g_assert( selected != NULL );
1740
1741         GtkTreeRowReference* rowref = gtk_tree_row_reference_new( model, path );
1742         *selected = g_slist_append( *selected, rowref );
1743
1744         return FALSE;
1745 }
1746
1747 void TextureBrowser_assignTags(){
1748         GSList* selected = NULL;
1749         GSList* node;
1750         gchar* tag_assigned;
1751
1752         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_available_tree ) );
1753
1754         gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1755
1756         if ( selected != NULL ) {
1757                 for ( node = selected; node != NULL; node = node->next )
1758                 {
1759                         GtkTreePath* path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1760
1761                         if ( path ) {
1762                                 GtkTreeIter iter;
1763
1764                                 if ( gtk_tree_model_get_iter( GTK_TREE_MODEL( g_TextureBrowser.m_available_store ), &iter, path ) ) {
1765                                         gtk_tree_model_get( GTK_TREE_MODEL( g_TextureBrowser.m_available_store ), &iter, TAG_COLUMN, &tag_assigned, -1 );
1766                                         if ( !TagBuilder.CheckShaderTag( g_TextureBrowser.shader.c_str() ) ) {
1767                                                 // create a custom shader/texture entry
1768                                                 IShader* ishader = QERApp_Shader_ForName( g_TextureBrowser.shader.c_str() );
1769                                                 CopiedString filename = ishader->getShaderFileName();
1770
1771                                                 if ( filename.empty() ) {
1772                                                         // it's a texture
1773                                                         TagBuilder.AddShaderNode( g_TextureBrowser.shader.c_str(), CUSTOM, TEXTURE );
1774                                                 }
1775                                                 else {
1776                                                         // it's a shader
1777                                                         TagBuilder.AddShaderNode( g_TextureBrowser.shader.c_str(), CUSTOM, SHADER );
1778                                                 }
1779                                                 ishader->DecRef();
1780                                         }
1781                                         TagBuilder.AddShaderTag( g_TextureBrowser.shader.c_str(), (char*)tag_assigned, TAG );
1782
1783                                         gtk_list_store_remove( g_TextureBrowser.m_available_store, &iter );
1784                                         gtk_list_store_append( g_TextureBrowser.m_assigned_store, &iter );
1785                                         gtk_list_store_set( g_TextureBrowser.m_assigned_store, &iter, TAG_COLUMN, (char*)tag_assigned, -1 );
1786                                 }
1787                         }
1788                 }
1789
1790                 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
1791
1792                 // Save changes
1793                 TagBuilder.SaveXmlDoc();
1794         }
1795         g_slist_free( selected );
1796 }
1797
1798 void TextureBrowser_removeTags(){
1799         GSList* selected = NULL;
1800         GSList* node;
1801         gchar* tag;
1802
1803         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_assigned_tree ) );
1804
1805         gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1806
1807         if ( selected != NULL ) {
1808                 for ( node = selected; node != NULL; node = node->next )
1809                 {
1810                         GtkTreePath* path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1811
1812                         if ( path ) {
1813                                 GtkTreeIter iter;
1814
1815                                 if ( gtk_tree_model_get_iter( GTK_TREE_MODEL( g_TextureBrowser.m_assigned_store ), &iter, path ) ) {
1816                                         gtk_tree_model_get( GTK_TREE_MODEL( g_TextureBrowser.m_assigned_store ), &iter, TAG_COLUMN, &tag, -1 );
1817                                         TagBuilder.DeleteShaderTag( g_TextureBrowser.shader.c_str(), tag );
1818                                         gtk_list_store_remove( g_TextureBrowser.m_assigned_store, &iter );
1819                                 }
1820                         }
1821                 }
1822
1823                 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
1824
1825                 // Update the "available tags list"
1826                 BuildStoreAvailableTags( g_TextureBrowser.m_available_store, g_TextureBrowser.m_assigned_store, g_TextureBrowser.m_all_tags, &g_TextureBrowser );
1827
1828                 // Save changes
1829                 TagBuilder.SaveXmlDoc();
1830         }
1831         g_slist_free( selected );
1832 }
1833
1834 void TextureBrowser_buildTagList(){
1835         GtkTreeIter treeIter;
1836         gtk_list_store_clear( g_TextureBrowser.m_all_tags_list );
1837
1838         std::set<CopiedString>::iterator iter;
1839
1840         for ( iter = g_TextureBrowser.m_all_tags.begin(); iter != g_TextureBrowser.m_all_tags.end(); ++iter )
1841         {
1842                 gtk_list_store_append( g_TextureBrowser.m_all_tags_list, &treeIter );
1843                 gtk_list_store_set( g_TextureBrowser.m_all_tags_list, &treeIter, TAG_COLUMN, ( *iter ).c_str(), -1 );
1844         }
1845 }
1846
1847 void TextureBrowser_searchTags(){
1848         GSList* selected = NULL;
1849         GSList* node;
1850         gchar* tag;
1851         char buffer[256];
1852         char tags_searched[256];
1853
1854         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ) );
1855
1856         gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1857
1858         if ( selected != NULL ) {
1859                 strcpy( buffer, "/root/*/*[tag='" );
1860                 strcpy( tags_searched, "[TAGS] " );
1861
1862                 for ( node = selected; node != NULL; node = node->next )
1863                 {
1864                         GtkTreePath* path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1865
1866                         if ( path ) {
1867                                 GtkTreeIter iter;
1868
1869                                 if ( gtk_tree_model_get_iter( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iter, path ) ) {
1870                                         gtk_tree_model_get( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iter, TAG_COLUMN, &tag, -1 );
1871
1872                                         strcat( buffer, tag );
1873                                         strcat( tags_searched, tag );
1874                                         if ( node != g_slist_last( node ) ) {
1875                                                 strcat( buffer, "' and tag='" );
1876                                                 strcat( tags_searched, ", " );
1877                                         }
1878                                 }
1879                         }
1880                 }
1881
1882                 strcat( buffer, "']" );
1883
1884                 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
1885
1886                 g_TextureBrowser.m_found_shaders.clear(); // delete old list
1887                 TagBuilder.TagSearch( buffer, g_TextureBrowser.m_found_shaders );
1888
1889                 if ( !g_TextureBrowser.m_found_shaders.empty() ) { // found something
1890                         size_t shaders_found = g_TextureBrowser.m_found_shaders.size();
1891
1892                         globalOutputStream() << "Found " << (unsigned int)shaders_found << " textures and shaders with " << tags_searched << "\n";
1893                         ScopeDisableScreenUpdates disableScreenUpdates( "Searching...", "Loading Textures" );
1894
1895                         std::set<CopiedString>::iterator iter;
1896
1897                         for ( iter = g_TextureBrowser.m_found_shaders.begin(); iter != g_TextureBrowser.m_found_shaders.end(); iter++ )
1898                         {
1899                                 std::string path = ( *iter ).c_str();
1900                                 size_t pos = path.find_last_of( "/", path.size() );
1901                                 std::string name = path.substr( pos + 1, path.size() );
1902                                 path = path.substr( 0, pos + 1 );
1903                                 TextureDirectory_loadTexture( path.c_str(), name.c_str() );
1904                         }
1905                 }
1906                 g_TextureBrowser.m_searchedTags = true;
1907                 g_TextureBrowser_currentDirectory = tags_searched;
1908
1909                 g_TextureBrowser.m_nTotalHeight = 0;
1910                 TextureBrowser_setOriginY( g_TextureBrowser, 0 );
1911                 TextureBrowser_heightChanged( g_TextureBrowser );
1912                 TextureBrowser_updateTitle();
1913         }
1914         g_slist_free( selected );
1915 }
1916
1917 void TextureBrowser_toggleSearchButton(){
1918         gint page = gtk_notebook_get_current_page( GTK_NOTEBOOK( g_TextureBrowser.m_tag_notebook ) );
1919
1920         if ( page == 0 ) { // tag page
1921                 gtk_widget_show_all( g_TextureBrowser.m_search_button );
1922         }
1923         else {
1924                 gtk_widget_hide( g_TextureBrowser.m_search_button );
1925         }
1926 }
1927
1928 void TextureBrowser_constructTagNotebook(){
1929         g_TextureBrowser.m_tag_notebook = ui::Widget(gtk_notebook_new());
1930         ui::Widget labelTags = ui::Label( "Tags" );
1931         ui::Widget labelTextures = ui::Label( "Textures" );
1932
1933         gtk_notebook_append_page( GTK_NOTEBOOK( g_TextureBrowser.m_tag_notebook ), g_TextureBrowser.m_scr_win_tree, labelTextures );
1934         gtk_notebook_append_page( GTK_NOTEBOOK( g_TextureBrowser.m_tag_notebook ), g_TextureBrowser.m_scr_win_tags, labelTags );
1935
1936         g_TextureBrowser.m_tag_notebook.connect( "switch-page", G_CALLBACK( TextureBrowser_toggleSearchButton ), NULL );
1937
1938         gtk_widget_show_all( g_TextureBrowser.m_tag_notebook );
1939 }
1940
1941 void TextureBrowser_constructSearchButton(){
1942         ui::Widget image = ui::Widget(gtk_image_new_from_stock( GTK_STOCK_FIND, GTK_ICON_SIZE_SMALL_TOOLBAR ));
1943         g_TextureBrowser.m_search_button = ui::Button(ui::New);
1944         g_TextureBrowser.m_search_button.connect( "clicked", G_CALLBACK( TextureBrowser_searchTags ), NULL );
1945         gtk_widget_set_tooltip_text(g_TextureBrowser.m_search_button, "Search with selected tags");
1946         g_TextureBrowser.m_search_button.add(image);
1947 }
1948
1949 void TextureBrowser_checkTagFile(){
1950         const char SHADERTAG_FILE[] = "shadertags.xml";
1951         CopiedString default_filename, rc_filename;
1952         StringOutputStream stream( 256 );
1953
1954         stream << LocalRcPath_get();
1955         stream << SHADERTAG_FILE;
1956         rc_filename = stream.c_str();
1957
1958         if ( file_exists( rc_filename.c_str() ) ) {
1959                 g_TextureBrowser.m_tags = TagBuilder.OpenXmlDoc( rc_filename.c_str() );
1960
1961                 if ( g_TextureBrowser.m_tags ) {
1962                         globalOutputStream() << "Loading tag file " << rc_filename.c_str() << ".\n";
1963                 }
1964         }
1965         else
1966         {
1967                 // load default tagfile
1968                 stream.clear();
1969                 stream << g_pGameDescription->mGameToolsPath.c_str();
1970                 stream << SHADERTAG_FILE;
1971                 default_filename = stream.c_str();
1972
1973                 if ( file_exists( default_filename.c_str() ) ) {
1974                         g_TextureBrowser.m_tags = TagBuilder.OpenXmlDoc( default_filename.c_str(), rc_filename.c_str() );
1975
1976                         if ( g_TextureBrowser.m_tags ) {
1977                                 globalOutputStream() << "Loading default tag file " << default_filename.c_str() << ".\n";
1978                         }
1979                 }
1980                 else
1981                 {
1982                         globalErrorStream() << "Unable to find default tag file " << default_filename.c_str() << ". No tag support.\n";
1983                 }
1984         }
1985 }
1986
1987 void TextureBrowser_SetNotex(){
1988         StringOutputStream name( 256 );
1989         name << GlobalRadiant().getAppPath() << "bitmaps/" NOTEX_BASENAME ".png";
1990         g_notex = name.c_str();
1991
1992         name = StringOutputStream(256);
1993         name << GlobalRadiant().getAppPath() << "bitmaps/" SHADERNOTEX_BASENAME " .png";
1994         g_shadernotex = name.c_str();
1995 }
1996
1997 ui::Widget TextureBrowser_constructWindow( ui::Window toplevel ){
1998         // The gl_widget and the tag assignment frame should be packed into a GtkVPaned with the slider
1999         // position stored in local.pref. gtk_paned_get_position() and gtk_paned_set_position() don't
2000         // seem to work in gtk 2.4 and the arrow buttons don't handle GTK_FILL, so here's another thing
2001         // for the "once-the-gtk-libs-are-updated-TODO-list" :x
2002
2003         TextureBrowser_checkTagFile();
2004         TextureBrowser_SetNotex();
2005
2006         GlobalShaderSystem().setActiveShadersChangedNotify( ReferenceCaller<TextureBrowser, TextureBrowser_activeShadersChanged>( g_TextureBrowser ) );
2007
2008         g_TextureBrowser.m_parent = toplevel;
2009
2010         ui::Widget table = ui::Table( 3, 3, FALSE );
2011         ui::Widget frame_table{ui::null};
2012         ui::Widget vbox = ui::VBox( FALSE, 0 );
2013         gtk_table_attach( GTK_TABLE( table ), vbox, 0, 1, 1, 3, GTK_FILL, GTK_FILL, 0, 0 );
2014         vbox.show();
2015
2016         ui::Widget menu_bar{ui::null};
2017
2018         { // menu bar
2019                 menu_bar = ui::Widget(gtk_menu_bar_new());
2020                 auto menu_view = ui::Menu(ui::New);
2021                 auto view_item = TextureBrowser_constructViewMenu( menu_view );
2022                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( view_item ), menu_view );
2023                 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), view_item );
2024
2025                 auto menu_tools = ui::Menu(ui::New);
2026                 auto tools_item = TextureBrowser_constructToolsMenu( menu_tools );
2027                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( tools_item ), menu_tools );
2028                 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), tools_item );
2029
2030                 gtk_table_attach( GTK_TABLE( table ), menu_bar, 0, 3, 0, 1, GTK_FILL, GTK_SHRINK, 0, 0 );
2031                 menu_bar.show();
2032         }
2033         { // Texture TreeView
2034                 g_TextureBrowser.m_scr_win_tree = ui::ScrolledWindow(ui::New);
2035                 gtk_container_set_border_width( GTK_CONTAINER( g_TextureBrowser.m_scr_win_tree ), 0 );
2036
2037                 // vertical only scrolling for treeview
2038                 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( g_TextureBrowser.m_scr_win_tree ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2039
2040                 g_TextureBrowser.m_scr_win_tree.show();
2041
2042                 TextureBrowser_createTreeViewTree();
2043
2044                 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( g_TextureBrowser.m_scr_win_tree ), GTK_WIDGET( g_TextureBrowser.m_treeViewTree ) );
2045                 g_TextureBrowser.m_treeViewTree.show();
2046         }
2047         { // gl_widget scrollbar
2048                 ui::Widget w = ui::Widget(gtk_vscrollbar_new( ui::Adjustment( 0,0,0,1,1,0 ) ));
2049                 gtk_table_attach( GTK_TABLE( table ), w, 2, 3, 1, 2, GTK_SHRINK, GTK_FILL, 0, 0 );
2050                 w.show();
2051                 g_TextureBrowser.m_texture_scroll = w;
2052
2053                 auto vadjustment = ui::Adjustment(gtk_range_get_adjustment( GTK_RANGE( g_TextureBrowser.m_texture_scroll ) ));
2054                 vadjustment.connect( "value_changed", G_CALLBACK( TextureBrowser_verticalScroll ), &g_TextureBrowser );
2055
2056                 widget_set_visible( g_TextureBrowser.m_texture_scroll, g_TextureBrowser.m_showTextureScrollbar );
2057         }
2058         { // gl_widget
2059                 g_TextureBrowser.m_gl_widget = glwidget_new( FALSE );
2060                 g_object_ref( g_TextureBrowser.m_gl_widget._handle );
2061
2062                 gtk_widget_set_events( g_TextureBrowser.m_gl_widget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK );
2063                 gtk_widget_set_can_focus( g_TextureBrowser.m_gl_widget, true );
2064
2065                 gtk_table_attach_defaults( GTK_TABLE( table ), g_TextureBrowser.m_gl_widget, 1, 2, 1, 2 );
2066                 g_TextureBrowser.m_gl_widget.show();
2067
2068                 g_TextureBrowser.m_sizeHandler = g_TextureBrowser.m_gl_widget.connect( "size_allocate", G_CALLBACK( TextureBrowser_size_allocate ), &g_TextureBrowser );
2069                 g_TextureBrowser.m_exposeHandler = g_TextureBrowser.m_gl_widget.on_render( G_CALLBACK( TextureBrowser_expose ), &g_TextureBrowser );
2070
2071                 g_TextureBrowser.m_gl_widget.connect( "button_press_event", G_CALLBACK( TextureBrowser_button_press ), &g_TextureBrowser );
2072                 g_TextureBrowser.m_gl_widget.connect( "button_release_event", G_CALLBACK( TextureBrowser_button_release ), &g_TextureBrowser );
2073                 g_TextureBrowser.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( TextureBrowser_motion ), &g_TextureBrowser );
2074                 g_TextureBrowser.m_gl_widget.connect( "scroll_event", G_CALLBACK( TextureBrowser_scroll ), &g_TextureBrowser );
2075         }
2076
2077         // tag stuff
2078         if ( g_TextureBrowser.m_tags ) {
2079                 { // fill tag GtkListStore
2080                         g_TextureBrowser.m_all_tags_list = ui::ListStore(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2081                         GtkTreeSortable* sortable = GTK_TREE_SORTABLE( g_TextureBrowser.m_all_tags_list );
2082                         gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2083
2084                         TagBuilder.GetAllTags( g_TextureBrowser.m_all_tags );
2085                         TextureBrowser_buildTagList();
2086                 }
2087                 { // tag menu bar
2088                         auto menu_tags = ui::Menu(ui::New);
2089                         auto tags_item = TextureBrowser_constructTagsMenu( menu_tags );
2090                         gtk_menu_item_set_submenu( GTK_MENU_ITEM( tags_item ), menu_tags );
2091                         gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), tags_item );
2092                 }
2093                 { // Tag TreeView
2094                         g_TextureBrowser.m_scr_win_tags = ui::ScrolledWindow(ui::New);
2095                         gtk_container_set_border_width( GTK_CONTAINER( g_TextureBrowser.m_scr_win_tags ), 0 );
2096
2097                         // vertical only scrolling for treeview
2098                         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( g_TextureBrowser.m_scr_win_tags ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2099
2100                         TextureBrowser_createTreeViewTags();
2101
2102                         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ) );
2103                         gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2104
2105                         gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( g_TextureBrowser.m_scr_win_tags ), GTK_WIDGET( g_TextureBrowser.m_treeViewTags ) );
2106                         g_TextureBrowser.m_treeViewTags.show();
2107                 }
2108                 { // Texture/Tag notebook
2109                         TextureBrowser_constructTagNotebook();
2110                         gtk_box_pack_start( GTK_BOX( vbox ), g_TextureBrowser.m_tag_notebook, TRUE, TRUE, 0 );
2111                 }
2112                 { // Tag search button
2113                         TextureBrowser_constructSearchButton();
2114                         gtk_box_pack_end( GTK_BOX( vbox ), g_TextureBrowser.m_search_button, FALSE, FALSE, 0 );
2115                 }
2116                 { // Tag frame
2117                         frame_table = ui::Table( 3, 3, FALSE );
2118
2119                         g_TextureBrowser.m_tag_frame = ui::Frame( "Tag assignment" );
2120                         gtk_frame_set_label_align( GTK_FRAME( g_TextureBrowser.m_tag_frame ), 0.5, 0.5 );
2121                         gtk_frame_set_shadow_type( GTK_FRAME( g_TextureBrowser.m_tag_frame ), GTK_SHADOW_NONE );
2122
2123                         gtk_table_attach( GTK_TABLE( table ), g_TextureBrowser.m_tag_frame, 1, 3, 2, 3, GTK_FILL, GTK_SHRINK, 0, 0 );
2124
2125                         frame_table.show();
2126
2127                         g_TextureBrowser.m_tag_frame.add(frame_table);
2128                 }
2129                 { // assigned tag list
2130                         ui::Widget scrolled_win = ui::ScrolledWindow(ui::New);
2131                         gtk_container_set_border_width( GTK_CONTAINER( scrolled_win ), 0 );
2132                         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2133
2134                         g_TextureBrowser.m_assigned_store = ui::ListStore(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2135
2136                         GtkTreeSortable* sortable = GTK_TREE_SORTABLE( g_TextureBrowser.m_assigned_store );
2137                         gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2138
2139                         auto renderer = ui::CellRendererText(ui::New);
2140
2141                         g_TextureBrowser.m_assigned_tree = ui::TreeView(ui::TreeModel( GTK_TREE_MODEL( g_TextureBrowser.m_assigned_store ) ));
2142                         g_TextureBrowser.m_assigned_store.unref();
2143                         g_TextureBrowser.m_assigned_tree.connect( "row-activated", (GCallback) TextureBrowser_removeTags, NULL );
2144                         gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( g_TextureBrowser.m_assigned_tree ), FALSE );
2145
2146                         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_assigned_tree ) );
2147                         gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2148
2149                         GtkTreeViewColumn* column = ui::TreeViewColumn( "", renderer, {{"text", TAG_COLUMN}} );
2150                         gtk_tree_view_append_column( GTK_TREE_VIEW( g_TextureBrowser.m_assigned_tree ), column );
2151                         g_TextureBrowser.m_assigned_tree.show();
2152
2153                         scrolled_win.show();
2154                         gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_WIDGET( g_TextureBrowser.m_assigned_tree ) );
2155
2156                         gtk_table_attach( GTK_TABLE( frame_table ), scrolled_win, 0, 1, 1, 3, GTK_FILL, GTK_FILL, 0, 0 );
2157                 }
2158                 { // available tag list
2159                         ui::Widget scrolled_win = ui::ScrolledWindow(ui::New);
2160                         gtk_container_set_border_width( GTK_CONTAINER( scrolled_win ), 0 );
2161                         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2162
2163                         g_TextureBrowser.m_available_store = ui::ListStore(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2164                         GtkTreeSortable* sortable = GTK_TREE_SORTABLE( g_TextureBrowser.m_available_store );
2165                         gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2166
2167                         auto renderer = ui::CellRendererText(ui::New);
2168
2169                         g_TextureBrowser.m_available_tree = ui::TreeView(ui::TreeModel( GTK_TREE_MODEL( g_TextureBrowser.m_available_store ) ));
2170                         g_TextureBrowser.m_available_store.unref();
2171                         g_TextureBrowser.m_available_tree.connect( "row-activated", (GCallback) TextureBrowser_assignTags, NULL );
2172                         gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( g_TextureBrowser.m_available_tree ), FALSE );
2173
2174                         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_available_tree ) );
2175                         gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2176
2177                         GtkTreeViewColumn* column = ui::TreeViewColumn( "", renderer, {{"text", TAG_COLUMN}} );
2178                         gtk_tree_view_append_column( GTK_TREE_VIEW( g_TextureBrowser.m_available_tree ), column );
2179                         g_TextureBrowser.m_available_tree.show();
2180
2181                         scrolled_win.show();
2182                         gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_WIDGET( g_TextureBrowser.m_available_tree ) );
2183
2184                         gtk_table_attach( GTK_TABLE( frame_table ), scrolled_win, 2, 3, 1, 3, GTK_FILL, GTK_FILL, 0, 0 );
2185                 }
2186                 { // tag arrow buttons
2187                         auto m_btn_left = ui::Button(ui::New);
2188                         auto m_btn_right = ui::Button(ui::New);
2189                         auto m_arrow_left = ui::Widget(gtk_arrow_new( GTK_ARROW_LEFT, GTK_SHADOW_OUT ));
2190                         auto m_arrow_right = ui::Widget(gtk_arrow_new( GTK_ARROW_RIGHT, GTK_SHADOW_OUT ));
2191                         m_btn_left.add(m_arrow_left);
2192                         m_btn_right.add(m_arrow_right);
2193
2194                         // workaround. the size of the tag frame depends of the requested size of the arrow buttons.
2195                         gtk_widget_set_size_request( m_arrow_left, -1, 68 );
2196                         gtk_widget_set_size_request( m_arrow_right, -1, 68 );
2197
2198                         gtk_table_attach( GTK_TABLE( frame_table ), m_btn_left, 1, 2, 1, 2, GTK_SHRINK, GTK_EXPAND, 0, 0 );
2199                         gtk_table_attach( GTK_TABLE( frame_table ), m_btn_right, 1, 2, 2, 3, GTK_SHRINK, GTK_EXPAND, 0, 0 );
2200
2201                         m_btn_left.connect( "clicked", G_CALLBACK( TextureBrowser_assignTags ), NULL );
2202                         m_btn_right.connect( "clicked", G_CALLBACK( TextureBrowser_removeTags ), NULL );
2203
2204                         m_btn_left.show();
2205                         m_btn_right.show();
2206                         m_arrow_left.show();
2207                         m_arrow_right.show();
2208                 }
2209                 { // tag fram labels
2210                         ui::Widget m_lbl_assigned = ui::Label( "Assigned" );
2211                         ui::Widget m_lbl_unassigned = ui::Label( "Available" );
2212
2213                         gtk_table_attach( GTK_TABLE( frame_table ), m_lbl_assigned, 0, 1, 0, 1, GTK_EXPAND, GTK_SHRINK, 0, 0 );
2214                         gtk_table_attach( GTK_TABLE( frame_table ), m_lbl_unassigned, 2, 3, 0, 1, GTK_EXPAND, GTK_SHRINK, 0, 0 );
2215
2216                         m_lbl_assigned.show();
2217                         m_lbl_unassigned.show();
2218                 }
2219         }
2220         else { // no tag support, show the texture tree only
2221                 gtk_box_pack_start( GTK_BOX( vbox ), g_TextureBrowser.m_scr_win_tree, TRUE, TRUE, 0 );
2222         }
2223
2224         // TODO do we need this?
2225         //gtk_container_set_focus_chain(GTK_CONTAINER(hbox_table), NULL);
2226
2227         return table;
2228 }
2229
2230 void TextureBrowser_destroyWindow(){
2231         GlobalShaderSystem().setActiveShadersChangedNotify( Callback() );
2232
2233         g_signal_handler_disconnect( G_OBJECT( g_TextureBrowser.m_gl_widget ), g_TextureBrowser.m_sizeHandler );
2234         g_signal_handler_disconnect( G_OBJECT( g_TextureBrowser.m_gl_widget ), g_TextureBrowser.m_exposeHandler );
2235
2236         g_TextureBrowser.m_gl_widget.unref();
2237 }
2238
2239 const Vector3& TextureBrowser_getBackgroundColour( TextureBrowser& textureBrowser ){
2240         return textureBrowser.color_textureback;
2241 }
2242
2243 void TextureBrowser_setBackgroundColour( TextureBrowser& textureBrowser, const Vector3& colour ){
2244         textureBrowser.color_textureback = colour;
2245         TextureBrowser_queueDraw( textureBrowser );
2246 }
2247
2248 void TextureBrowser_selectionHelper( GtkTreeModel* model, GtkTreePath* path, GtkTreeIter* iter, GSList** selected ){
2249         g_assert( selected != NULL );
2250
2251         gchar* name;
2252         gtk_tree_model_get( model, iter, TAG_COLUMN, &name, -1 );
2253         *selected = g_slist_append( *selected, name );
2254 }
2255
2256 void TextureBrowser_shaderInfo(){
2257         const char* name = TextureBrowser_GetSelectedShader( g_TextureBrowser );
2258         IShader* shader = QERApp_Shader_ForName( name );
2259
2260         DoShaderInfoDlg( name, shader->getShaderFileName(), "Shader Info" );
2261
2262         shader->DecRef();
2263 }
2264
2265 void TextureBrowser_addTag(){
2266         CopiedString tag;
2267
2268         EMessageBoxReturn result = DoShaderTagDlg( &tag, "Add shader tag" );
2269
2270         if ( result == eIDOK && !tag.empty() ) {
2271                 GtkTreeIter iter, iter2;
2272                 g_TextureBrowser.m_all_tags.insert( tag.c_str() );
2273                 gtk_list_store_append( g_TextureBrowser.m_available_store, &iter );
2274                 gtk_list_store_set( g_TextureBrowser.m_available_store, &iter, TAG_COLUMN, tag.c_str(), -1 );
2275
2276                 // Select the currently added tag in the available list
2277                 GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_available_tree ) );
2278                 gtk_tree_selection_select_iter( selection, &iter );
2279
2280                 gtk_list_store_append( g_TextureBrowser.m_all_tags_list, &iter2 );
2281                 gtk_list_store_set( g_TextureBrowser.m_all_tags_list, &iter2, TAG_COLUMN, tag.c_str(), -1 );
2282         }
2283 }
2284
2285 void TextureBrowser_renameTag(){
2286         /* WORKAROUND: The tag treeview is set to GTK_SELECTION_MULTIPLE. Because
2287            gtk_tree_selection_get_selected() doesn't work with GTK_SELECTION_MULTIPLE,
2288            we need to count the number of selected rows first and use
2289            gtk_tree_selection_selected_foreach() then to go through the list of selected
2290            rows (which always containins a single row).
2291          */
2292
2293         GSList* selected = NULL;
2294
2295         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ) );
2296         gtk_tree_selection_selected_foreach( selection, GtkTreeSelectionForeachFunc( TextureBrowser_selectionHelper ), &selected );
2297
2298         if ( g_slist_length( selected ) == 1 ) { // we only rename a single tag
2299                 CopiedString newTag;
2300                 EMessageBoxReturn result = DoShaderTagDlg( &newTag, "Rename shader tag" );
2301
2302                 if ( result == eIDOK && !newTag.empty() ) {
2303                         GtkTreeIter iterList;
2304                         gchar* rowTag;
2305                         gchar* oldTag = (char*)selected->data;
2306
2307                         bool row = gtk_tree_model_get_iter_first( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iterList ) != 0;
2308
2309                         while ( row )
2310                         {
2311                                 gtk_tree_model_get( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iterList, TAG_COLUMN, &rowTag, -1 );
2312
2313                                 if ( strcmp( rowTag, oldTag ) == 0 ) {
2314                                         gtk_list_store_set( g_TextureBrowser.m_all_tags_list, &iterList, TAG_COLUMN, newTag.c_str(), -1 );
2315                                 }
2316                                 row = gtk_tree_model_iter_next( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iterList ) != 0;
2317                         }
2318
2319                         TagBuilder.RenameShaderTag( oldTag, newTag.c_str() );
2320
2321                         g_TextureBrowser.m_all_tags.erase( (CopiedString)oldTag );
2322                         g_TextureBrowser.m_all_tags.insert( newTag );
2323
2324                         BuildStoreAssignedTags( g_TextureBrowser.m_assigned_store, g_TextureBrowser.shader.c_str(), &g_TextureBrowser );
2325                         BuildStoreAvailableTags( g_TextureBrowser.m_available_store, g_TextureBrowser.m_assigned_store, g_TextureBrowser.m_all_tags, &g_TextureBrowser );
2326                 }
2327         }
2328         else
2329         {
2330                 ui::Widget(GTK_WIDGET( g_TextureBrowser.m_parent )).alert( "Select a single tag for renaming." );
2331         }
2332 }
2333
2334 void TextureBrowser_deleteTag(){
2335         GSList* selected = NULL;
2336
2337         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ) );
2338         gtk_tree_selection_selected_foreach( selection, GtkTreeSelectionForeachFunc( TextureBrowser_selectionHelper ), &selected );
2339
2340         if ( g_slist_length( selected ) == 1 ) { // we only delete a single tag
2341                 auto result = ui::Widget(GTK_WIDGET( g_TextureBrowser.m_parent )).alert( "Are you sure you want to delete the selected tag?", "Delete Tag", ui::alert_type::YESNO, ui::alert_icon::Question );
2342
2343                 if ( result == ui::alert_response::YES ) {
2344                         GtkTreeIter iterSelected;
2345                         gchar *rowTag;
2346
2347                         gchar* tagSelected = (char*)selected->data;
2348
2349                         bool row = gtk_tree_model_get_iter_first( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iterSelected ) != 0;
2350
2351                         while ( row )
2352                         {
2353                                 gtk_tree_model_get( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iterSelected, TAG_COLUMN, &rowTag, -1 );
2354
2355                                 if ( strcmp( rowTag, tagSelected ) == 0 ) {
2356                                         gtk_list_store_remove( g_TextureBrowser.m_all_tags_list, &iterSelected );
2357                                         break;
2358                                 }
2359                                 row = gtk_tree_model_iter_next( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iterSelected ) != 0;
2360                         }
2361
2362                         TagBuilder.DeleteTag( tagSelected );
2363                         g_TextureBrowser.m_all_tags.erase( (CopiedString)tagSelected );
2364
2365                         BuildStoreAssignedTags( g_TextureBrowser.m_assigned_store, g_TextureBrowser.shader.c_str(), &g_TextureBrowser );
2366                         BuildStoreAvailableTags( g_TextureBrowser.m_available_store, g_TextureBrowser.m_assigned_store, g_TextureBrowser.m_all_tags, &g_TextureBrowser );
2367                 }
2368         }
2369         else {
2370                 ui::Widget(GTK_WIDGET( g_TextureBrowser.m_parent )).alert( "Select a single tag for deletion." );
2371         }
2372 }
2373
2374 void TextureBrowser_copyTag(){
2375         g_TextureBrowser.m_copied_tags.clear();
2376         TagBuilder.GetShaderTags( g_TextureBrowser.shader.c_str(), g_TextureBrowser.m_copied_tags );
2377 }
2378
2379 void TextureBrowser_pasteTag(){
2380         IShader* ishader = QERApp_Shader_ForName( g_TextureBrowser.shader.c_str() );
2381         CopiedString shader = g_TextureBrowser.shader.c_str();
2382
2383         if ( !TagBuilder.CheckShaderTag( shader.c_str() ) ) {
2384                 CopiedString shaderFile = ishader->getShaderFileName();
2385                 if ( shaderFile.empty() ) {
2386                         // it's a texture
2387                         TagBuilder.AddShaderNode( shader.c_str(), CUSTOM, TEXTURE );
2388                 }
2389                 else
2390                 {
2391                         // it's a shader
2392                         TagBuilder.AddShaderNode( shader.c_str(), CUSTOM, SHADER );
2393                 }
2394
2395                 for ( size_t i = 0; i < g_TextureBrowser.m_copied_tags.size(); ++i )
2396                 {
2397                         TagBuilder.AddShaderTag( shader.c_str(), g_TextureBrowser.m_copied_tags[i].c_str(), TAG );
2398                 }
2399         }
2400         else
2401         {
2402                 for ( size_t i = 0; i < g_TextureBrowser.m_copied_tags.size(); ++i )
2403                 {
2404                         if ( !TagBuilder.CheckShaderTag( shader.c_str(), g_TextureBrowser.m_copied_tags[i].c_str() ) ) {
2405                                 // the tag doesn't exist - let's add it
2406                                 TagBuilder.AddShaderTag( shader.c_str(), g_TextureBrowser.m_copied_tags[i].c_str(), TAG );
2407                         }
2408                 }
2409         }
2410
2411         ishader->DecRef();
2412
2413         TagBuilder.SaveXmlDoc();
2414         BuildStoreAssignedTags( g_TextureBrowser.m_assigned_store, shader.c_str(), &g_TextureBrowser );
2415         BuildStoreAvailableTags( g_TextureBrowser.m_available_store, g_TextureBrowser.m_assigned_store, g_TextureBrowser.m_all_tags, &g_TextureBrowser );
2416 }
2417
2418 void TextureBrowser_RefreshShaders(){
2419         ScopeDisableScreenUpdates disableScreenUpdates( "Processing...", "Loading Shaders" );
2420         GlobalShaderSystem().refresh();
2421         UpdateAllWindows();
2422         GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(GlobalTextureBrowser().m_treeViewTree));
2423         GtkTreeModel* model = NULL;
2424         GtkTreeIter iter;
2425         if ( gtk_tree_selection_get_selected (selection, &model, &iter) )
2426         {
2427                 gchar dirName[1024];
2428
2429                 gchar* buffer;
2430                 gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
2431                 strcpy( dirName, buffer );
2432                 g_free( buffer );
2433                 if ( !TextureBrowser_showWads() ) {
2434                         strcat( dirName, "/" );
2435                 }
2436                 TextureBrowser_ShowDirectory( GlobalTextureBrowser(), dirName );
2437                 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2438         }
2439 }
2440
2441 void TextureBrowser_ToggleShowShaders(){
2442         g_TextureBrowser.m_showShaders ^= 1;
2443         g_TextureBrowser.m_showshaders_item.update();
2444         TextureBrowser_queueDraw( g_TextureBrowser );
2445 }
2446
2447 void TextureBrowser_ToggleShowShaderListOnly(){
2448         g_TextureBrowser_shaderlistOnly ^= 1;
2449         g_TextureBrowser.m_showshaderlistonly_item.update();
2450
2451         TextureBrowser_constructTreeStore();
2452 }
2453
2454 void TextureBrowser_showAll(){
2455         g_TextureBrowser_currentDirectory = "";
2456         g_TextureBrowser.m_searchedTags = false;
2457         TextureBrowser_heightChanged( g_TextureBrowser );
2458         TextureBrowser_updateTitle();
2459 }
2460
2461 void TextureBrowser_showUntagged(){
2462         auto result = ui::Widget(GTK_WIDGET( g_TextureBrowser.m_parent )).alert( "WARNING! This function might need a lot of memory and time. Are you sure you want to use it?", "Show Untagged", ui::alert_type::YESNO, ui::alert_icon::Warning );
2463
2464         if ( result == ui::alert_response::YES ) {
2465                 g_TextureBrowser.m_found_shaders.clear();
2466                 TagBuilder.GetUntagged( g_TextureBrowser.m_found_shaders );
2467                 std::set<CopiedString>::iterator iter;
2468
2469                 ScopeDisableScreenUpdates disableScreenUpdates( "Searching untagged textures...", "Loading Textures" );
2470
2471                 for ( iter = g_TextureBrowser.m_found_shaders.begin(); iter != g_TextureBrowser.m_found_shaders.end(); iter++ )
2472                 {
2473                         std::string path = ( *iter ).c_str();
2474                         size_t pos = path.find_last_of( "/", path.size() );
2475                         std::string name = path.substr( pos + 1, path.size() );
2476                         path = path.substr( 0, pos + 1 );
2477                         TextureDirectory_loadTexture( path.c_str(), name.c_str() );
2478                         globalErrorStream() << path.c_str() << name.c_str() << "\n";
2479                 }
2480
2481                 g_TextureBrowser_currentDirectory = "Untagged";
2482                 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2483                 TextureBrowser_heightChanged( g_TextureBrowser );
2484                 TextureBrowser_updateTitle();
2485         }
2486 }
2487
2488 void TextureBrowser_FixedSize(){
2489         g_TextureBrowser_fixedSize ^= 1;
2490         GlobalTextureBrowser().m_fixedsize_item.update();
2491         TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2492 }
2493
2494 void TextureBrowser_FilterMissing(){
2495         g_TextureBrowser_filterMissing ^= 1;
2496         GlobalTextureBrowser().m_filternotex_item.update();
2497         TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2498         TextureBrowser_RefreshShaders();
2499 }
2500
2501 void TextureBrowser_FilterFallback(){
2502         g_TextureBrowser_filterFallback ^= 1;
2503         GlobalTextureBrowser().m_hidenotex_item.update();
2504         TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2505         TextureBrowser_RefreshShaders();
2506 }
2507
2508 void TextureBrowser_EnableAlpha(){
2509         g_TextureBrowser_enableAlpha ^= 1;
2510         GlobalTextureBrowser().m_enablealpha_item.update();
2511         TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2512 }
2513
2514 void TextureBrowser_exportTitle( const StringImportCallback& importer ){
2515         StringOutputStream buffer( 64 );
2516         buffer << "Textures: ";
2517         if ( !string_empty( g_TextureBrowser_currentDirectory.c_str() ) ) {
2518                 buffer << g_TextureBrowser_currentDirectory.c_str();
2519         }
2520         else
2521         {
2522                 buffer << "all";
2523         }
2524         importer( buffer.c_str() );
2525 }
2526
2527
2528 void TextureScaleImport( TextureBrowser& textureBrowser, int value ){
2529         switch ( value )
2530         {
2531         case 0:
2532                 TextureBrowser_setScale( textureBrowser, 10 );
2533                 break;
2534         case 1:
2535                 TextureBrowser_setScale( textureBrowser, 25 );
2536                 break;
2537         case 2:
2538                 TextureBrowser_setScale( textureBrowser, 50 );
2539                 break;
2540         case 3:
2541                 TextureBrowser_setScale( textureBrowser, 100 );
2542                 break;
2543         case 4:
2544                 TextureBrowser_setScale( textureBrowser, 200 );
2545                 break;
2546         }
2547 }
2548 typedef ReferenceCaller1<TextureBrowser, int, TextureScaleImport> TextureScaleImportCaller;
2549
2550 void TextureScaleExport( TextureBrowser& textureBrowser, const IntImportCallback& importer ){
2551         switch ( textureBrowser.m_textureScale )
2552         {
2553         case 10:
2554                 importer( 0 );
2555                 break;
2556         case 25:
2557                 importer( 1 );
2558                 break;
2559         case 50:
2560                 importer( 2 );
2561                 break;
2562         case 100:
2563                 importer( 3 );
2564                 break;
2565         case 200:
2566                 importer( 4 );
2567                 break;
2568         }
2569 }
2570 typedef ReferenceCaller1<TextureBrowser, const IntImportCallback&, TextureScaleExport> TextureScaleExportCaller;
2571
2572
2573 void UniformTextureSizeImport( TextureBrowser& textureBrowser, int value ){
2574
2575         if ( value > 16 )
2576                 TextureBrowser_setUniformSize( textureBrowser, value );
2577 }
2578 typedef ReferenceCaller1<TextureBrowser, int, UniformTextureSizeImport> UniformTextureSizeImportCaller;
2579
2580 void TextureBrowser_constructPreferences( PreferencesPage& page ){
2581         page.appendCheckBox(
2582                 "", "Texture scrollbar",
2583                 TextureBrowserImportShowScrollbarCaller( GlobalTextureBrowser() ),
2584                 BoolExportCaller( GlobalTextureBrowser().m_showTextureScrollbar )
2585                 );
2586         {
2587                 const char* texture_scale[] = { "10%", "25%", "50%", "100%", "200%" };
2588                 page.appendCombo(
2589                         "Texture Thumbnail Scale",
2590                         STRING_ARRAY_RANGE( texture_scale ),
2591                         IntImportCallback( TextureScaleImportCaller( GlobalTextureBrowser() ) ),
2592                         IntExportCallback( TextureScaleExportCaller( GlobalTextureBrowser() ) )
2593                         );
2594         }
2595         page.appendSpinner(
2596                 "Texture Thumbnail Size",
2597                 GlobalTextureBrowser().m_uniformTextureSize,
2598                 GlobalTextureBrowser().m_uniformTextureSize,
2599                 16, 8192
2600         );
2601         page.appendEntry( "Mousewheel Increment", GlobalTextureBrowser().m_mouseWheelScrollIncrement );
2602         {
2603                 const char* startup_shaders[] = { "None", TextureBrowser_getComonShadersName() };
2604                 page.appendCombo( "Load Shaders at Startup", reinterpret_cast<int&>( GlobalTextureBrowser().m_startupShaders ), STRING_ARRAY_RANGE( startup_shaders ) );
2605         }
2606 }
2607 void TextureBrowser_constructPage( PreferenceGroup& group ){
2608         PreferencesPage page( group.createPage( "Texture Browser", "Texture Browser Preferences" ) );
2609         TextureBrowser_constructPreferences( page );
2610 }
2611 void TextureBrowser_registerPreferencesPage(){
2612         PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, TextureBrowser_constructPage>() );
2613 }
2614
2615
2616 #include "preferencesystem.h"
2617 #include "stringio.h"
2618
2619 typedef ReferenceCaller1<TextureBrowser, std::size_t, TextureBrowser_setScale> TextureBrowserSetScaleCaller;
2620
2621
2622
2623 void TextureClipboard_textureSelected( const char* shader );
2624
2625 void TextureBrowser_Construct(){
2626         GlobalCommands_insert( "ShaderInfo", FreeCaller<TextureBrowser_shaderInfo>() );
2627         GlobalCommands_insert( "ShowUntagged", FreeCaller<TextureBrowser_showUntagged>() );
2628         GlobalCommands_insert( "AddTag", FreeCaller<TextureBrowser_addTag>() );
2629         GlobalCommands_insert( "RenameTag", FreeCaller<TextureBrowser_renameTag>() );
2630         GlobalCommands_insert( "DeleteTag", FreeCaller<TextureBrowser_deleteTag>() );
2631         GlobalCommands_insert( "CopyTag", FreeCaller<TextureBrowser_copyTag>() );
2632         GlobalCommands_insert( "PasteTag", FreeCaller<TextureBrowser_pasteTag>() );
2633         GlobalCommands_insert( "RefreshShaders", FreeCaller<VFS_Refresh>() );
2634         GlobalToggles_insert( "ShowInUse", FreeCaller<TextureBrowser_ToggleHideUnused>(), ToggleItem::AddCallbackCaller( g_TextureBrowser.m_hideunused_item ), Accelerator( 'U' ) );
2635         GlobalCommands_insert( "ShowAllTextures", FreeCaller<TextureBrowser_showAll>(), Accelerator( 'A', (GdkModifierType)GDK_CONTROL_MASK ) );
2636         GlobalCommands_insert( "ToggleTextures", FreeCaller<TextureBrowser_toggleShow>(), Accelerator( 'T' ) );
2637         GlobalToggles_insert( "ToggleShowShaders", FreeCaller<TextureBrowser_ToggleShowShaders>(), ToggleItem::AddCallbackCaller( g_TextureBrowser.m_showshaders_item ) );
2638         GlobalToggles_insert( "ToggleShowShaderlistOnly", FreeCaller<TextureBrowser_ToggleShowShaderListOnly>(), ToggleItem::AddCallbackCaller( g_TextureBrowser.m_showshaderlistonly_item ) );
2639         GlobalToggles_insert( "FixedSize", FreeCaller<TextureBrowser_FixedSize>(), ToggleItem::AddCallbackCaller( g_TextureBrowser.m_fixedsize_item ) );
2640         GlobalToggles_insert( "FilterMissing", FreeCaller<TextureBrowser_FilterMissing>(), ToggleItem::AddCallbackCaller( g_TextureBrowser.m_filternotex_item ) );
2641         GlobalToggles_insert( "FilterFallback", FreeCaller<TextureBrowser_FilterFallback>(), ToggleItem::AddCallbackCaller( g_TextureBrowser.m_hidenotex_item ) );
2642         GlobalToggles_insert( "EnableAlpha", FreeCaller<TextureBrowser_EnableAlpha>(), ToggleItem::AddCallbackCaller( g_TextureBrowser.m_enablealpha_item ) );
2643
2644         GlobalPreferenceSystem().registerPreference( "TextureScale",
2645                                                                                                  makeSizeStringImportCallback( TextureBrowserSetScaleCaller( g_TextureBrowser ) ),
2646                                                                                                  SizeExportStringCaller( g_TextureBrowser.m_textureScale )
2647                                                                                                  );
2648         GlobalPreferenceSystem().registerPreference( "UniformTextureSize",
2649                                                                                                  makeIntStringImportCallback(UniformTextureSizeImportCaller(g_TextureBrowser)),
2650                                                                                                  IntExportStringCaller(g_TextureBrowser.m_uniformTextureSize) );
2651         GlobalPreferenceSystem().registerPreference( "TextureScrollbar",
2652                                                                                                  makeBoolStringImportCallback( TextureBrowserImportShowScrollbarCaller( g_TextureBrowser ) ),
2653                                                                                                  BoolExportStringCaller( GlobalTextureBrowser().m_showTextureScrollbar )
2654                                                                                                  );
2655         GlobalPreferenceSystem().registerPreference( "ShowShaders", BoolImportStringCaller( GlobalTextureBrowser().m_showShaders ), BoolExportStringCaller( GlobalTextureBrowser().m_showShaders ) );
2656         GlobalPreferenceSystem().registerPreference( "ShowShaderlistOnly", BoolImportStringCaller( g_TextureBrowser_shaderlistOnly ), BoolExportStringCaller( g_TextureBrowser_shaderlistOnly ) );
2657         GlobalPreferenceSystem().registerPreference( "FixedSize", BoolImportStringCaller( g_TextureBrowser_fixedSize ), BoolExportStringCaller( g_TextureBrowser_fixedSize ) );
2658         GlobalPreferenceSystem().registerPreference( "FilterMissing", BoolImportStringCaller( g_TextureBrowser_filterMissing ), BoolExportStringCaller( g_TextureBrowser_filterMissing ) );
2659         GlobalPreferenceSystem().registerPreference( "EnableAlpha", BoolImportStringCaller( g_TextureBrowser_enableAlpha ), BoolExportStringCaller( g_TextureBrowser_enableAlpha ) );
2660         GlobalPreferenceSystem().registerPreference( "LoadShaders", IntImportStringCaller( reinterpret_cast<int&>( GlobalTextureBrowser().m_startupShaders ) ), IntExportStringCaller( reinterpret_cast<int&>( GlobalTextureBrowser().m_startupShaders ) ) );
2661         GlobalPreferenceSystem().registerPreference( "WheelMouseInc", SizeImportStringCaller( GlobalTextureBrowser().m_mouseWheelScrollIncrement ), SizeExportStringCaller( GlobalTextureBrowser().m_mouseWheelScrollIncrement ) );
2662         GlobalPreferenceSystem().registerPreference( "SI_Colors0", Vector3ImportStringCaller( GlobalTextureBrowser().color_textureback ), Vector3ExportStringCaller( GlobalTextureBrowser().color_textureback ) );
2663
2664         g_TextureBrowser.shader = texdef_name_default();
2665
2666         Textures_setModeChangedNotify( ReferenceCaller<TextureBrowser, TextureBrowser_queueDraw>( g_TextureBrowser ) );
2667
2668         TextureBrowser_registerPreferencesPage();
2669
2670         GlobalShaderSystem().attach( g_ShadersObserver );
2671
2672         TextureBrowser_textureSelected = TextureClipboard_textureSelected;
2673 }
2674 void TextureBrowser_Destroy(){
2675         GlobalShaderSystem().detach( g_ShadersObserver );
2676
2677         Textures_setModeChangedNotify( Callback() );
2678 }