]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/groupdialog.cpp
gtkgl module: drop
[xonotic/netradiant.git] / radiant / groupdialog.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 // Floating dialog that contains a notebook with at least Entities and Group tabs
24 // I merged the 2 MS Windows dialogs in a single class
25 //
26 // Leonardo Zide (leo@lokigames.com)
27 //
28
29 #include "groupdialog.h"
30 #include "globaldefs.h"
31
32 #include "debugging/debugging.h"
33
34 #include <vector>
35 #include <gtk/gtk.h>
36
37 #include "gtkutil/widget.h"
38 #include "gtkutil/accelerator.h"
39 #include "entityinspector.h"
40 #include "gtkmisc.h"
41 #include "multimon.h"
42 #include "console.h"
43 #include "commands.h"
44 #include "gtkutil/window.h"
45
46 #if defined(WORKAROUND_WINDOWS_GTK2_GLWIDGET) || defined(WORKAROUND_MACOS_GTK2_GLWIDGET)
47 #include "texwindow.h"
48 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET || WORKAROUND_MACOS_GTK2_GLWIDGET
49
50 class GroupDlg
51 {
52 public:
53 ui::Widget m_pNotebook{ui::null};
54 ui::Window m_window{ui::null};
55
56 GroupDlg();
57 void Create( ui::Window parent );
58
59 void Show(){
60         // workaround for strange gtk behaviour - modifying the contents of a window while it is not visible causes the window position to change without sending a configure_event
61         m_position_tracker.sync( m_window );
62         m_window.show();
63 }
64 void Hide(){
65         m_window.hide();
66 }
67
68 WindowPositionTracker m_position_tracker;
69 };
70
71 namespace
72 {
73 GroupDlg g_GroupDlg;
74
75 std::size_t g_current_page;
76 std::vector<Callback<void(const Callback<void(const char *)> &)>> g_pages;
77 }
78
79 static void workaround_macos_show_hide(){
80 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
81         if ( g_current_page == 2 )
82         {
83                 TextureBrowser_showGLWidget();
84         }
85         else
86         {
87                 TextureBrowser_hideGLWidget();
88         }
89 #endif // WORKAROUND_MACOS_GTK2_GLWIDGET
90 }
91
92 void GroupDialog_updatePageTitle( ui::Window window, std::size_t pageIndex ){
93         if ( pageIndex < g_pages.size() ) {
94                 g_pages[pageIndex]( PointerCaller<GtkWindow, void(const char*), gtk_window_set_title>( window ) );
95         }
96
97         workaround_macos_show_hide();
98 }
99
100 static gboolean switch_page( GtkNotebook *notebook, gpointer page, guint page_num, gpointer data ){
101         g_current_page = page_num;
102         GroupDialog_updatePageTitle( ui::Window::from(data), page_num );
103         return FALSE;
104 }
105
106 GroupDlg::GroupDlg() : m_window( ui::null ){
107         m_position_tracker.setPosition( c_default_window_pos );
108 }
109
110 void GroupDlg::Create( ui::Window parent ){
111         ASSERT_MESSAGE( !m_window, "dialog already created" );
112
113         auto window = ui::Window(create_persistent_floating_window( "Entities", parent ));
114
115         global_accel_connect_window( window );
116
117         window_connect_focus_in_clear_focus_widget( window );
118
119         m_window = window;
120
121 #if GDEF_OS_WINDOWS
122         if ( g_multimon_globals.m_bStartOnPrimMon ) {
123                 WindowPosition pos( m_position_tracker.getPosition() );
124                 PositionWindowOnPrimaryScreen( pos );
125                 m_position_tracker.setPosition( pos );
126         }
127 #endif
128         m_position_tracker.connect( window );
129
130         {
131                 ui::Widget notebook = ui::Widget::from(gtk_notebook_new());
132                 notebook.show();
133                 window.add(notebook);
134                 gtk_notebook_set_tab_pos( GTK_NOTEBOOK( notebook ), GTK_POS_BOTTOM );
135                 m_pNotebook = notebook;
136
137                 notebook.connect( "switch_page", G_CALLBACK( switch_page ), (gpointer) window );
138         }
139 }
140
141 ui::Widget GroupDialog_addPage( const char* tabLabel, ui::Widget widget, const Callback<void(const Callback<void(const char *)> &)>& title ){
142         ui::Widget w = ui::Label( tabLabel );
143         w.show();
144         auto page = ui::Widget::from(gtk_notebook_get_nth_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gtk_notebook_insert_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), widget, w, -1 ) ));
145         g_pages.push_back( title );
146
147         return page;
148 }
149
150 bool GroupDialog_isShown(){
151         return g_GroupDlg.m_window.visible();
152 }
153 void GroupDialog_setShown( bool shown ){
154         shown ? g_GroupDlg.Show() : g_GroupDlg.Hide();
155 }
156 void GroupDialog_ToggleShow(){
157         GroupDialog_setShown( !GroupDialog_isShown() );
158 }
159
160 void GroupDialog_constructWindow( ui::Window main_window ){
161         g_GroupDlg.Create( main_window );
162 }
163 void GroupDialog_destroyWindow(){
164         ASSERT_TRUE( g_GroupDlg.m_window );
165         destroy_floating_window( g_GroupDlg.m_window );
166         g_GroupDlg.m_window = ui::Window{ui::null};
167 }
168
169 ui::Window GroupDialog_getWindow(){
170         return ui::Window(g_GroupDlg.m_window);
171 }
172 void GroupDialog_show(){
173         g_GroupDlg.Show();
174 }
175
176 ui::Widget GroupDialog_getPage(){
177         return ui::Widget::from(gtk_notebook_get_nth_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) ) );
178 }
179
180 void GroupDialog_setPage( ui::Widget page ){
181         g_current_page = gtk_notebook_page_num( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), page );
182         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
183
184         workaround_macos_show_hide();
185 }
186
187 #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
188 void GroupDialog_cycle(){
189         g_current_page = ( g_current_page + 1 ) % g_pages.size();
190         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
191 }
192 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
193
194 void GroupDialog_showPage( ui::Widget page ){
195
196         if ( GroupDialog_getPage() == page ) {
197                 GroupDialog_ToggleShow();
198
199 #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
200                 /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
201                 /* this is very ugly: cycle to next tab then return to current tab immediately to force the refresh
202                  * this fixes the drawing of texture tab when window is restored and current tab is texture tab
203                  * this is called for nothing when windows is minimized and called for nothing when current tab
204                  * is not texture tab, hopefully it's a workaround that would disappear with gtk 3 */
205                 GroupDialog_cycle();
206                 GroupDialog_setPage( page );
207 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
208
209         }
210         else
211         {
212                 g_GroupDlg.m_window.show();
213                 GroupDialog_setPage( page );
214         }
215
216         workaround_macos_show_hide();
217 }
218
219 void GroupDialog_updatePageTitle( ui::Widget page ){
220         if ( GroupDialog_getPage() == page ) {
221                 GroupDialog_updatePageTitle( g_GroupDlg.m_window, g_current_page );
222         }
223 }
224
225 #include "preferencesystem.h"
226
227 void GroupDialog_Construct(){
228         GlobalPreferenceSystem().registerPreference( "EntityWnd", make_property<WindowPositionTracker_String>( g_GroupDlg.m_position_tracker ) );
229
230         GlobalCommands_insert( "ViewEntityInfo", makeCallbackF(GroupDialog_ToggleShow), Accelerator( 'N' ) );
231 }
232 void GroupDialog_Destroy(){
233 }