]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/groupdialog.cpp
Merge branch 'NateEag-master-patch-12920' into 'master'
[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 #define GARUX_GTK_WORKAROUND
63 #ifndef GARUX_GTK_WORKAROUND
64         /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
65         GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( m_window ), "glwidget" ) );
66         if ( glwidget ){
67                 //if ( widget_is_visible( glwidget ) )
68                         //globalOutputStream() << "glwidget have been already visible :0\n"; /* is not hidden aswell, according to this */
69                 gtk_widget_hide( glwidget );
70                 gtk_widget_show( glwidget );
71         }
72 #endif
73         m_window.show();
74 }
75 void Hide(){
76         m_window.hide();
77 }
78
79 WindowPositionTracker m_position_tracker;
80 };
81
82 namespace
83 {
84 GroupDlg g_GroupDlg;
85
86 std::size_t g_current_page;
87 std::vector<Callback<void(const Callback<void(const char *)> &)>> g_pages;
88 }
89
90 static void workaround_macos_show_hide(){
91 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
92         if ( g_current_page == 2 )
93         {
94                 TextureBrowser_showGLWidget();
95         }
96         else
97         {
98                 TextureBrowser_hideGLWidget();
99         }
100 #endif // WORKAROUND_MACOS_GTK2_GLWIDGET
101 }
102
103 void GroupDialog_updatePageTitle( ui::Window window, std::size_t pageIndex ){
104         if ( pageIndex < g_pages.size() ) {
105                 g_pages[pageIndex]( PointerCaller<GtkWindow, void(const char*), gtk_window_set_title>( window ) );
106         }
107
108         workaround_macos_show_hide();
109 }
110
111 static gboolean switch_page( GtkNotebook *notebook, gpointer page, guint page_num, gpointer data ){
112         g_current_page = page_num;
113         GroupDialog_updatePageTitle( ui::Window::from(data), page_num );
114         return FALSE;
115 }
116
117 GroupDlg::GroupDlg() : m_window( ui::null ){
118         m_position_tracker.setPosition( c_default_window_pos );
119 }
120
121 void GroupDlg::Create( ui::Window parent ){
122         ASSERT_MESSAGE( !m_window, "dialog already created" );
123
124         auto window = ui::Window(create_persistent_floating_window( "Entities", parent ));
125
126         global_accel_connect_window( window );
127
128         window_connect_focus_in_clear_focus_widget( window );
129
130         m_window = window;
131
132 #if GDEF_OS_WINDOWS
133         if ( g_multimon_globals.m_bStartOnPrimMon ) {
134                 WindowPosition pos( m_position_tracker.getPosition() );
135                 PositionWindowOnPrimaryScreen( pos );
136                 m_position_tracker.setPosition( pos );
137         }
138 #endif
139         m_position_tracker.connect( window );
140
141         {
142                 ui::Widget notebook = ui::Widget::from(gtk_notebook_new());
143                 notebook.show();
144                 window.add(notebook);
145                 gtk_notebook_set_tab_pos( GTK_NOTEBOOK( notebook ), GTK_POS_BOTTOM );
146                 m_pNotebook = notebook;
147
148                 notebook.connect( "switch_page", G_CALLBACK( switch_page ), (gpointer) window );
149         }
150 }
151
152 ui::Widget GroupDialog_addPage( const char* tabLabel, ui::Widget widget, const Callback<void(const Callback<void(const char *)> &)>& title ){
153         ui::Widget w = ui::Label( tabLabel );
154         w.show();
155         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 ) ));
156         g_pages.push_back( title );
157
158         return page;
159 }
160
161 bool GroupDialog_isShown(){
162         return g_GroupDlg.m_window.visible();
163 }
164 void GroupDialog_setShown( bool shown ){
165         shown ? g_GroupDlg.Show() : g_GroupDlg.Hide();
166 }
167 void GroupDialog_ToggleShow(){
168         GroupDialog_setShown( !GroupDialog_isShown() );
169 }
170
171 void GroupDialog_constructWindow( ui::Window main_window ){
172         g_GroupDlg.Create( main_window );
173 }
174 void GroupDialog_destroyWindow(){
175         ASSERT_TRUE( g_GroupDlg.m_window );
176         destroy_floating_window( g_GroupDlg.m_window );
177         g_GroupDlg.m_window = ui::Window{ui::null};
178 }
179
180 ui::Window GroupDialog_getWindow(){
181         return ui::Window(g_GroupDlg.m_window);
182 }
183 void GroupDialog_show(){
184         g_GroupDlg.Show();
185 }
186
187 ui::Widget GroupDialog_getPage(){
188         return ui::Widget::from(gtk_notebook_get_nth_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) ) );
189 }
190
191 void GroupDialog_setPage( ui::Widget page ){
192         g_current_page = gtk_notebook_page_num( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), page );
193         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
194
195         workaround_macos_show_hide();
196 }
197
198 #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
199 void GroupDialog_cycle(){
200         g_current_page = ( g_current_page + 1 ) % g_pages.size();
201         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
202 }
203 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
204
205 void GroupDialog_showPage( ui::Widget page ){
206
207         if ( GroupDialog_getPage() == page ) {
208                 GroupDialog_ToggleShow();
209
210 #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
211                 /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
212                 /* this is very ugly: cycle to next tab then return to current tab immediately to force the refresh
213                  * this fixes the drawing of texture tab when window is restored and current tab is texture tab
214                  * this is called for nothing when windows is minimized and called for nothing when current tab
215                  * is not texture tab, hopefully it's a workaround that would disappear with gtk 3 */
216                 GroupDialog_cycle();
217                 GroupDialog_setPage( page );
218 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
219
220         }
221         else
222         {
223                 g_GroupDlg.m_window.show();
224                 GroupDialog_setPage( page );
225         }
226
227         workaround_macos_show_hide();
228 }
229
230 void GroupDialog_updatePageTitle( ui::Widget page ){
231         if ( GroupDialog_getPage() == page ) {
232                 GroupDialog_updatePageTitle( g_GroupDlg.m_window, g_current_page );
233         }
234 }
235
236 #include "preferencesystem.h"
237
238 void GroupDialog_Construct(){
239         GlobalPreferenceSystem().registerPreference( "EntityWnd", make_property<WindowPositionTracker_String>( g_GroupDlg.m_position_tracker ) );
240
241         GlobalCommands_insert( "ViewEntityInfo", makeCallbackF(GroupDialog_ToggleShow), Accelerator( 'N' ) );
242 }
243 void GroupDialog_Destroy(){
244 }