| 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 | #include "modelbrowser.h" |
| 23 | |
| 24 | #include <set> |
| 25 | #include <map> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include <gtk/gtk.h> |
| 29 | |
| 30 | #include "ifilesystem.h" |
| 31 | #include "ifiletypes.h" |
| 32 | #include "imodel.h" |
| 33 | |
| 34 | #include "os/path.h" |
| 35 | #include "string/string.h" |
| 36 | #include "stream/stringstream.h" |
| 37 | #include "generic/callback.h" |
| 38 | |
| 39 | #include "gtkutil/toolbar.h" |
| 40 | #include "gtkutil/dialog.h" |
| 41 | #include "entity.h" |
| 42 | #include "entityinspector.h" |
| 43 | #include "mainframe.h" |
| 44 | #include "qe3.h" |
| 45 | |
| 46 | namespace |
| 47 | { |
| 48 | |
| 49 | // Tree store columns for the directory tree |
| 50 | enum |
| 51 | { |
| 52 | DIR_NAME_COLUMN, |
| 53 | DIR_FULLPATH_COLUMN, |
| 54 | DIR_N_COLUMNS |
| 55 | }; |
| 56 | |
| 57 | // List store columns for the model file list |
| 58 | enum |
| 59 | { |
| 60 | MODEL_NAME_COLUMN, |
| 61 | MODEL_FULLPATH_COLUMN, |
| 62 | MODEL_N_COLUMNS |
| 63 | }; |
| 64 | |
| 65 | // In-memory filesystem tree built from VFS |
| 66 | class ModelFS |
| 67 | { |
| 68 | public: |
| 69 | CopiedString m_name; |
| 70 | |
| 71 | ModelFS() = default; |
| 72 | ModelFS( const char* name ) : m_name( name ) {} |
| 73 | |
| 74 | bool operator<( const ModelFS& other ) const { |
| 75 | return string_less_nocase( m_name.c_str(), other.m_name.c_str() ); |
| 76 | } |
| 77 | |
| 78 | mutable std::set<ModelFS> m_folders; |
| 79 | mutable std::set<CopiedString> m_files; |
| 80 | |
| 81 | void insert( const char* filepath ) const { |
| 82 | const char* slash = strchr( filepath, '/' ); |
| 83 | if ( slash == nullptr ) { |
| 84 | m_files.insert( CopiedString( filepath ) ); |
| 85 | } |
| 86 | else { |
| 87 | CopiedString dirName( StringRange( filepath, slash ) ); |
| 88 | auto result = m_folders.insert( ModelFS( dirName.c_str() ) ); |
| 89 | result.first->insert( slash + 1 ); |
| 90 | } |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | class ModelBrowser |
| 95 | { |
| 96 | public: |
| 97 | ui::Window m_parent{ui::null}; |
| 98 | ui::TreeView m_treeView{ui::null}; |
| 99 | ui::TreeView m_fileList{ui::null}; |
| 100 | |
| 101 | ModelFS m_modelFS; |
| 102 | CopiedString m_currentFolderPath; |
| 103 | CopiedString m_selectedModel; |
| 104 | |
| 105 | std::set<CopiedString> m_modelExtensions; |
| 106 | }; |
| 107 | |
| 108 | ModelBrowser g_ModelBrowser; |
| 109 | |
| 110 | // Collect known model extensions from the file type registry |
| 111 | void ModelBrowser_getModelExtensions(){ |
| 112 | g_ModelBrowser.m_modelExtensions.clear(); |
| 113 | |
| 114 | class TypeListAccumulator : public IFileTypeList |
| 115 | { |
| 116 | public: |
| 117 | std::set<CopiedString>& m_extensions; |
| 118 | TypeListAccumulator( std::set<CopiedString>& ext ) : m_extensions( ext ) {} |
| 119 | void addType( const char* moduleName, filetype_t type ) override { |
| 120 | // moduleName is the file extension |
| 121 | m_extensions.insert( CopiedString( moduleName ) ); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | TypeListAccumulator typelist( g_ModelBrowser.m_modelExtensions ); |
| 126 | GlobalFiletypes().getTypeList( ModelLoader::Name(), &typelist, true, false, false ); |
| 127 | } |
| 128 | |
| 129 | // VFS callback to collect model files |
| 130 | void ModelBrowser_addModelFile( const char* name ){ |
| 131 | g_ModelBrowser.m_modelFS.insert( name ); |
| 132 | } |
| 133 | typedef FreeCaller<void(const char*), ModelBrowser_addModelFile> ModelBrowserAddModelFileCaller; |
| 134 | |
| 135 | // Scan VFS for model files |
| 136 | void ModelBrowser_buildTree(){ |
| 137 | g_ModelBrowser.m_modelFS.m_folders.clear(); |
| 138 | g_ModelBrowser.m_modelFS.m_files.clear(); |
| 139 | |
| 140 | for ( const CopiedString& ext : g_ModelBrowser.m_modelExtensions ) { |
| 141 | GlobalFileSystem().forEachFile( "models/", ext.c_str(), ModelBrowserAddModelFileCaller(), 99 ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Recursively build the directory tree GtkTreeStore from ModelFS |
| 146 | void ModelBrowser_buildTreeStore_r( const ModelFS& fs, GtkTreeStore* store, GtkTreeIter* parent, const char* parentPath ){ |
| 147 | for ( const ModelFS& child : fs.m_folders ) { |
| 148 | GtkTreeIter iter; |
| 149 | gtk_tree_store_append( store, &iter, parent ); |
| 150 | |
| 151 | StringOutputStream fullpath( 256 ); |
| 152 | fullpath << parentPath << child.m_name.c_str() << "/"; |
| 153 | |
| 154 | gtk_tree_store_set( store, &iter, |
| 155 | DIR_NAME_COLUMN, child.m_name.c_str(), |
| 156 | DIR_FULLPATH_COLUMN, fullpath.c_str(), |
| 157 | -1 ); |
| 158 | |
| 159 | ModelBrowser_buildTreeStore_r( child, store, &iter, fullpath.c_str() ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Find a ModelFS node by path |
| 164 | const ModelFS* ModelFS_findPath( const ModelFS& root, const char* path ){ |
| 165 | if ( string_empty( path ) ) { |
| 166 | return &root; |
| 167 | } |
| 168 | |
| 169 | const char* slash = strchr( path, '/' ); |
| 170 | if ( slash == nullptr ) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | CopiedString dirName( StringRange( path, slash ) ); |
| 175 | for ( const ModelFS& child : root.m_folders ) { |
| 176 | if ( string_equal_nocase( child.m_name.c_str(), dirName.c_str() ) ) { |
| 177 | return ModelFS_findPath( child, slash + 1 ); |
| 178 | } |
| 179 | } |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | // Populate the file list for a given directory path |
| 184 | void ModelBrowser_populateFileList( const char* dirPath ){ |
| 185 | g_ModelBrowser.m_currentFolderPath = dirPath; |
| 186 | g_ModelBrowser.m_selectedModel = ""; |
| 187 | |
| 188 | auto store = ui::ListStore::from( gtk_list_store_new( MODEL_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 189 | gtk_list_store_clear( store ); |
| 190 | |
| 191 | // Find the ModelFS node for this path |
| 192 | // dirPath is relative like "models/weapons/" - we need to strip "models/" prefix since our tree starts there |
| 193 | const ModelFS* node = ModelFS_findPath( g_ModelBrowser.m_modelFS, dirPath ); |
| 194 | if ( node != nullptr ) { |
| 195 | for ( const CopiedString& filename : node->m_files ) { |
| 196 | GtkTreeIter iter; |
| 197 | gtk_list_store_append( store, &iter ); |
| 198 | |
| 199 | StringOutputStream fullpath( 256 ); |
| 200 | fullpath << dirPath << filename.c_str(); |
| 201 | |
| 202 | gtk_list_store_set( store, &iter, |
| 203 | MODEL_NAME_COLUMN, filename.c_str(), |
| 204 | MODEL_FULLPATH_COLUMN, fullpath.c_str(), |
| 205 | -1 ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | gtk_tree_view_set_model( g_ModelBrowser.m_fileList, store ); |
| 210 | g_object_unref( G_OBJECT( store ) ); |
| 211 | } |
| 212 | |
| 213 | // Build the full directory tree widget model |
| 214 | void ModelBrowser_constructTreeStore(){ |
| 215 | auto store = ui::TreeStore::from( gtk_tree_store_new( DIR_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 216 | |
| 217 | // The root "models/" entry |
| 218 | GtkTreeIter rootIter; |
| 219 | gtk_tree_store_append( store, &rootIter, nullptr ); |
| 220 | gtk_tree_store_set( store, &rootIter, |
| 221 | DIR_NAME_COLUMN, "models", |
| 222 | DIR_FULLPATH_COLUMN, "", |
| 223 | -1 ); |
| 224 | |
| 225 | ModelBrowser_buildTreeStore_r( g_ModelBrowser.m_modelFS, store, &rootIter, "" ); |
| 226 | |
| 227 | gtk_tree_view_set_model( g_ModelBrowser.m_treeView, store ); |
| 228 | g_object_unref( G_OBJECT( store ) ); |
| 229 | |
| 230 | // Expand the root node |
| 231 | auto path = ui::TreePath( "0" ); |
| 232 | gtk_tree_view_expand_row( g_ModelBrowser.m_treeView, path, FALSE ); |
| 233 | gtk_tree_path_free( path ); |
| 234 | } |
| 235 | |
| 236 | // Rebuild the entire model browser tree |
| 237 | void ModelBrowser_refresh(){ |
| 238 | ModelBrowser_getModelExtensions(); |
| 239 | ModelBrowser_buildTree(); |
| 240 | ModelBrowser_constructTreeStore(); |
| 241 | |
| 242 | // Clear the file list |
| 243 | auto store = ui::ListStore::from( gtk_list_store_new( MODEL_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 244 | gtk_tree_view_set_model( g_ModelBrowser.m_fileList, store ); |
| 245 | g_object_unref( G_OBJECT( store ) ); |
| 246 | } |
| 247 | typedef FreeCaller<void(), ModelBrowser_refresh> ModelBrowserRefreshCaller; |
| 248 | |
| 249 | // Callback: directory tree row activated |
| 250 | void ModelBrowser_TreeView_onRowActivated( ui::TreeView treeView, ui::TreePath path, ui::TreeViewColumn col, gpointer userdata ){ |
| 251 | GtkTreeIter iter; |
| 252 | auto model = gtk_tree_view_get_model( treeView ); |
| 253 | |
| 254 | if ( gtk_tree_model_get_iter( model, &iter, path ) ) { |
| 255 | gchar* fullpath; |
| 256 | gtk_tree_model_get( model, &iter, DIR_FULLPATH_COLUMN, &fullpath, -1 ); |
| 257 | |
| 258 | // Build the VFS directory path: the stored path already has no "models/" prefix since |
| 259 | // the VFS forEachFile was called with "models/" as basedir. |
| 260 | // But we stored the paths relative to the VFS root in our ModelFS, so reconstruct. |
| 261 | StringOutputStream dirpath( 256 ); |
| 262 | if ( string_empty( fullpath ) ) { |
| 263 | // root "models" node: show all files directly under models/ |
| 264 | // Our ModelFS root contains the tree as scanned under "models/" |
| 265 | // so the files at root level are directly in models/ |
| 266 | } |
| 267 | dirpath << fullpath; |
| 268 | |
| 269 | ModelBrowser_populateFileList( dirpath.c_str() ); |
| 270 | |
| 271 | g_free( fullpath ); |
| 272 | |
| 273 | // Deactivate focus so keyboard shortcuts still work |
| 274 | gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( treeView ) ) ), nullptr ); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Callback: file list selection changed |
| 279 | void ModelBrowser_FileList_onSelectionChanged( GtkTreeSelection* selection, gpointer userdata ){ |
| 280 | GtkTreeModel* model; |
| 281 | GtkTreeIter iter; |
| 282 | |
| 283 | if ( gtk_tree_selection_get_selected( selection, &model, &iter ) ) { |
| 284 | gchar* fullpath; |
| 285 | gtk_tree_model_get( model, &iter, MODEL_FULLPATH_COLUMN, &fullpath, -1 ); |
| 286 | |
| 287 | // Prepend "models/" since VFS expects the full relative path |
| 288 | StringOutputStream vfspath( 256 ); |
| 289 | vfspath << "models/" << fullpath; |
| 290 | g_ModelBrowser.m_selectedModel = vfspath.c_str(); |
| 291 | |
| 292 | g_free( fullpath ); |
| 293 | } |
| 294 | else { |
| 295 | g_ModelBrowser.m_selectedModel = ""; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Callback: file list row activated (double click) - apply model to selected entities |
| 300 | void ModelBrowser_FileList_onRowActivated( ui::TreeView treeView, ui::TreePath path, ui::TreeViewColumn col, gpointer userdata ){ |
| 301 | GtkTreeIter iter; |
| 302 | auto model = gtk_tree_view_get_model( treeView ); |
| 303 | |
| 304 | if ( gtk_tree_model_get_iter( model, &iter, path ) ) { |
| 305 | gchar* fullpath; |
| 306 | gtk_tree_model_get( model, &iter, MODEL_FULLPATH_COLUMN, &fullpath, -1 ); |
| 307 | |
| 308 | StringOutputStream vfspath( 256 ); |
| 309 | vfspath << "models/" << fullpath; |
| 310 | |
| 311 | // Set the model key on all selected entities |
| 312 | Scene_EntitySetKeyValue_Selected( "model", vfspath.c_str() ); |
| 313 | |
| 314 | g_free( fullpath ); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | } // anonymous namespace |
| 319 | |
| 320 | const char* ModelBrowser_getSelectedModel(){ |
| 321 | if ( g_ModelBrowser.m_selectedModel.empty() ) { |
| 322 | return nullptr; |
| 323 | } |
| 324 | return g_ModelBrowser.m_selectedModel.c_str(); |
| 325 | } |
| 326 | |
| 327 | ui::Widget ModelBrowser_constructWindow( ui::Window toplevel ){ |
| 328 | g_ModelBrowser.m_parent = toplevel; |
| 329 | |
| 330 | auto hbox = ui::HPaned( ui::New ); |
| 331 | hbox.show(); |
| 332 | |
| 333 | // Left side: directory tree in scrolled window |
| 334 | { |
| 335 | auto scr = ui::ScrolledWindow( ui::New ); |
| 336 | gtk_container_set_border_width( GTK_CONTAINER( scr ), 0 ); |
| 337 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); |
| 338 | scr.show(); |
| 339 | |
| 340 | auto store = ui::TreeStore::from( gtk_tree_store_new( DIR_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 341 | g_ModelBrowser.m_treeView = ui::TreeView( ui::TreeModel::from( store._handle ) ); |
| 342 | g_object_unref( G_OBJECT( store ) ); |
| 343 | |
| 344 | gtk_tree_view_set_enable_search( g_ModelBrowser.m_treeView, FALSE ); |
| 345 | gtk_tree_view_set_headers_visible( g_ModelBrowser.m_treeView, FALSE ); |
| 346 | g_ModelBrowser.m_treeView.connect( "row-activated", G_CALLBACK( ModelBrowser_TreeView_onRowActivated ), nullptr ); |
| 347 | |
| 348 | auto renderer = ui::CellRendererText( ui::New ); |
| 349 | gtk_tree_view_insert_column_with_attributes( g_ModelBrowser.m_treeView, -1, "", renderer, "text", DIR_NAME_COLUMN, NULL ); |
| 350 | |
| 351 | gtk_container_add( GTK_CONTAINER( scr ), GTK_WIDGET( g_ModelBrowser.m_treeView ) ); |
| 352 | g_ModelBrowser.m_treeView.show(); |
| 353 | |
| 354 | gtk_paned_pack1( GTK_PANED( hbox ), GTK_WIDGET( scr ), TRUE, TRUE ); |
| 355 | } |
| 356 | |
| 357 | // Right side: vbox with toolbar and file list |
| 358 | { |
| 359 | auto vbox = ui::VBox( FALSE, 0 ); |
| 360 | vbox.show(); |
| 361 | |
| 362 | // Toolbar |
| 363 | { |
| 364 | auto toolbar = ui::Toolbar::from( gtk_toolbar_new() ); |
| 365 | gtk_toolbar_set_style( toolbar, GTK_TOOLBAR_ICONS ); |
| 366 | gtk_toolbar_set_show_arrow( toolbar, FALSE ); |
| 367 | |
| 368 | toolbar_append_button( toolbar, "Reload Model Tree", "refresh_models.png", ModelBrowserRefreshCaller() ); |
| 369 | |
| 370 | toolbar.show(); |
| 371 | vbox.pack_start( toolbar, FALSE, FALSE, 0 ); |
| 372 | } |
| 373 | |
| 374 | // File list in scrolled window |
| 375 | { |
| 376 | auto scr = ui::ScrolledWindow( ui::New ); |
| 377 | gtk_container_set_border_width( GTK_CONTAINER( scr ), 0 ); |
| 378 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); |
| 379 | scr.show(); |
| 380 | |
| 381 | auto store = ui::ListStore::from( gtk_list_store_new( MODEL_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 382 | g_ModelBrowser.m_fileList = ui::TreeView( ui::TreeModel::from( store._handle ) ); |
| 383 | g_object_unref( G_OBJECT( store ) ); |
| 384 | |
| 385 | gtk_tree_view_set_enable_search( g_ModelBrowser.m_fileList, TRUE ); |
| 386 | gtk_tree_view_set_headers_visible( g_ModelBrowser.m_fileList, FALSE ); |
| 387 | |
| 388 | g_ModelBrowser.m_fileList.connect( "row-activated", G_CALLBACK( ModelBrowser_FileList_onRowActivated ), nullptr ); |
| 389 | |
| 390 | auto selection = gtk_tree_view_get_selection( g_ModelBrowser.m_fileList ); |
| 391 | g_signal_connect( G_OBJECT( selection ), "changed", G_CALLBACK( ModelBrowser_FileList_onSelectionChanged ), nullptr ); |
| 392 | |
| 393 | auto renderer = ui::CellRendererText( ui::New ); |
| 394 | gtk_tree_view_insert_column_with_attributes( g_ModelBrowser.m_fileList, -1, "Model", renderer, "text", MODEL_NAME_COLUMN, NULL ); |
| 395 | |
| 396 | gtk_container_add( GTK_CONTAINER( scr ), GTK_WIDGET( g_ModelBrowser.m_fileList ) ); |
| 397 | g_ModelBrowser.m_fileList.show(); |
| 398 | |
| 399 | vbox.pack_start( scr, TRUE, TRUE, 0 ); |
| 400 | } |
| 401 | |
| 402 | gtk_paned_pack2( GTK_PANED( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE ); |
| 403 | } |
| 404 | |
| 405 | // Set reasonable default pane position |
| 406 | gtk_paned_set_position( GTK_PANED( hbox ), 200 ); |
| 407 | |
| 408 | // Initial tree population |
| 409 | ModelBrowser_refresh(); |
| 410 | |
| 411 | return hbox; |
| 412 | } |
| 413 | |
| 414 | void ModelBrowser_destroyWindow(){ |
| 415 | g_ModelBrowser.m_treeView = ui::TreeView{ui::null}; |
| 416 | g_ModelBrowser.m_fileList = ui::TreeView{ui::null}; |
| 417 | } |
| 418 | |
| 419 | void ModelBrowser_Construct(){ |
| 420 | } |
| 421 | |
| 422 | void ModelBrowser_Destroy(){ |
| 423 | } |
| 424 | |
| 425 | // Static storage for the chooser dialog's selected model path |
| 426 | static CopiedString s_chooserSelectedModel; |
| 427 | |
| 428 | // Modal model chooser dialog |
| 429 | const char* ModelBrowser_showChooser( ui::Widget parent ){ |
| 430 | ModalDialog dialog; |
| 431 | auto window = create_modal_dialog_window( parent.window(), "Choose Model", dialog, 600, 400 ); |
| 432 | |
| 433 | auto vbox = create_dialog_vbox( 4, 4 ); |
| 434 | window.add( vbox ); |
| 435 | |
| 436 | auto hpaned = ui::HPaned( ui::New ); |
| 437 | hpaned.show(); |
| 438 | vbox.pack_start( hpaned, TRUE, TRUE, 0 ); |
| 439 | |
| 440 | // Ensure we have extension/tree data |
| 441 | if ( g_ModelBrowser.m_modelExtensions.empty() ) { |
| 442 | ModelBrowser_getModelExtensions(); |
| 443 | ModelBrowser_buildTree(); |
| 444 | } |
| 445 | |
| 446 | // Directory tree |
| 447 | ui::TreeView dirTreeView{ui::null}; |
| 448 | { |
| 449 | auto scr = ui::ScrolledWindow( ui::New ); |
| 450 | gtk_container_set_border_width( GTK_CONTAINER( scr ), 0 ); |
| 451 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); |
| 452 | scr.show(); |
| 453 | |
| 454 | auto store = ui::TreeStore::from( gtk_tree_store_new( DIR_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 455 | |
| 456 | GtkTreeIter rootIter; |
| 457 | gtk_tree_store_append( store, &rootIter, nullptr ); |
| 458 | gtk_tree_store_set( store, &rootIter, |
| 459 | DIR_NAME_COLUMN, "models", |
| 460 | DIR_FULLPATH_COLUMN, "", |
| 461 | -1 ); |
| 462 | ModelBrowser_buildTreeStore_r( g_ModelBrowser.m_modelFS, store, &rootIter, "" ); |
| 463 | |
| 464 | dirTreeView = ui::TreeView( ui::TreeModel::from( store._handle ) ); |
| 465 | g_object_unref( G_OBJECT( store ) ); |
| 466 | |
| 467 | gtk_tree_view_set_enable_search( dirTreeView, FALSE ); |
| 468 | gtk_tree_view_set_headers_visible( dirTreeView, FALSE ); |
| 469 | |
| 470 | auto renderer = ui::CellRendererText( ui::New ); |
| 471 | gtk_tree_view_insert_column_with_attributes( dirTreeView, -1, "", renderer, "text", DIR_NAME_COLUMN, NULL ); |
| 472 | |
| 473 | gtk_container_add( GTK_CONTAINER( scr ), GTK_WIDGET( dirTreeView ) ); |
| 474 | dirTreeView.show(); |
| 475 | |
| 476 | // Expand root |
| 477 | auto path = ui::TreePath( "0" ); |
| 478 | gtk_tree_view_expand_row( dirTreeView, path, FALSE ); |
| 479 | gtk_tree_path_free( path ); |
| 480 | |
| 481 | gtk_paned_pack1( GTK_PANED( hpaned ), GTK_WIDGET( scr ), TRUE, TRUE ); |
| 482 | } |
| 483 | |
| 484 | // File list |
| 485 | ui::TreeView fileListView{ui::null}; |
| 486 | { |
| 487 | auto scr = ui::ScrolledWindow( ui::New ); |
| 488 | gtk_container_set_border_width( GTK_CONTAINER( scr ), 0 ); |
| 489 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); |
| 490 | scr.show(); |
| 491 | |
| 492 | auto store = ui::ListStore::from( gtk_list_store_new( MODEL_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 493 | fileListView = ui::TreeView( ui::TreeModel::from( store._handle ) ); |
| 494 | g_object_unref( G_OBJECT( store ) ); |
| 495 | |
| 496 | gtk_tree_view_set_enable_search( fileListView, TRUE ); |
| 497 | gtk_tree_view_set_headers_visible( fileListView, FALSE ); |
| 498 | |
| 499 | auto renderer = ui::CellRendererText( ui::New ); |
| 500 | gtk_tree_view_insert_column_with_attributes( fileListView, -1, "Model", renderer, "text", MODEL_NAME_COLUMN, NULL ); |
| 501 | |
| 502 | gtk_container_add( GTK_CONTAINER( scr ), GTK_WIDGET( fileListView ) ); |
| 503 | fileListView.show(); |
| 504 | |
| 505 | gtk_paned_pack2( GTK_PANED( hpaned ), GTK_WIDGET( scr ), TRUE, TRUE ); |
| 506 | } |
| 507 | |
| 508 | gtk_paned_set_position( GTK_PANED( hpaned ), 200 ); |
| 509 | |
| 510 | // Wire up directory tree row activation to populate file list |
| 511 | struct ChooserData { |
| 512 | ui::TreeView fileListView; |
| 513 | }; |
| 514 | auto chooserData = new ChooserData{ fileListView }; |
| 515 | |
| 516 | dirTreeView.connect( "row-activated", G_CALLBACK( +[]( GtkTreeView* tv, GtkTreePath* path, GtkTreeViewColumn* col, gpointer data ) { |
| 517 | auto cd = static_cast<ChooserData*>( data ); |
| 518 | GtkTreeIter iter; |
| 519 | auto model = gtk_tree_view_get_model( GTK_TREE_VIEW( tv ) ); |
| 520 | |
| 521 | if ( gtk_tree_model_get_iter( model, &iter, path ) ) { |
| 522 | gchar* fullpath; |
| 523 | gtk_tree_model_get( model, &iter, DIR_FULLPATH_COLUMN, &fullpath, -1 ); |
| 524 | |
| 525 | auto store = ui::ListStore::from( gtk_list_store_new( MODEL_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ) ); |
| 526 | |
| 527 | const ModelFS* node = ModelFS_findPath( g_ModelBrowser.m_modelFS, fullpath ); |
| 528 | if ( node != nullptr ) { |
| 529 | for ( const CopiedString& filename : node->m_files ) { |
| 530 | GtkTreeIter listIter; |
| 531 | gtk_list_store_append( store, &listIter ); |
| 532 | |
| 533 | StringOutputStream fp( 256 ); |
| 534 | fp << fullpath << filename.c_str(); |
| 535 | |
| 536 | gtk_list_store_set( store, &listIter, |
| 537 | MODEL_NAME_COLUMN, filename.c_str(), |
| 538 | MODEL_FULLPATH_COLUMN, fp.c_str(), |
| 539 | -1 ); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | gtk_tree_view_set_model( cd->fileListView, store ); |
| 544 | g_object_unref( G_OBJECT( store ) ); |
| 545 | g_free( fullpath ); |
| 546 | } |
| 547 | } ), chooserData ); |
| 548 | |
| 549 | // Buttons |
| 550 | { |
| 551 | auto buttonBox = create_dialog_hbox( 4 ); |
| 552 | vbox.pack_end( buttonBox, FALSE, FALSE, 0 ); |
| 553 | |
| 554 | ModalDialogButton okDialogButton( dialog, eIDOK ); |
| 555 | auto okButton = create_modal_dialog_button( "OK", okDialogButton ); |
| 556 | buttonBox.pack_end( okButton, FALSE, FALSE, 0 ); |
| 557 | ModalDialogButton cancelDialogButton( dialog, eIDCANCEL ); |
| 558 | auto cancelButton = create_modal_dialog_button( "Cancel", cancelDialogButton ); |
| 559 | buttonBox.pack_end( cancelButton, FALSE, FALSE, 0 ); |
| 560 | } |
| 561 | |
| 562 | // Also accept double-click on file list as OK |
| 563 | struct ChooserDialogData { |
| 564 | ModalDialog* dialog; |
| 565 | ui::TreeView fileListView; |
| 566 | }; |
| 567 | auto chooserDialogData = new ChooserDialogData{ &dialog, fileListView }; |
| 568 | |
| 569 | fileListView.connect( "row-activated", G_CALLBACK( +[]( GtkTreeView* tv, GtkTreePath* path, GtkTreeViewColumn* col, gpointer data ) { |
| 570 | auto dd = static_cast<ChooserDialogData*>( data ); |
| 571 | dd->dialog->ret = eIDOK; |
| 572 | dd->dialog->loop = false; |
| 573 | } ), chooserDialogData ); |
| 574 | |
| 575 | EMessageBoxReturn ret = modal_dialog_show( window, dialog ); |
| 576 | |
| 577 | const char* result = nullptr; |
| 578 | if ( ret == eIDOK ) { |
| 579 | // Get selected file from the file list |
| 580 | auto selection = gtk_tree_view_get_selection( fileListView ); |
| 581 | GtkTreeModel* treeModel; |
| 582 | GtkTreeIter iter; |
| 583 | if ( gtk_tree_selection_get_selected( selection, &treeModel, &iter ) ) { |
| 584 | gchar* fullpath; |
| 585 | gtk_tree_model_get( treeModel, &iter, MODEL_FULLPATH_COLUMN, &fullpath, -1 ); |
| 586 | |
| 587 | StringOutputStream vfspath( 256 ); |
| 588 | vfspath << "models/" << fullpath; |
| 589 | s_chooserSelectedModel = vfspath.c_str(); |
| 590 | result = s_chooserSelectedModel.c_str(); |
| 591 | |
| 592 | g_free( fullpath ); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | delete chooserData; |
| 597 | delete chooserDialogData; |
| 598 | window.destroy(); |
| 599 | |
| 600 | return result; |
| 601 | } |