]> de.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
45
46 #include "gtkutil/window.h"
47
48 class GroupDlg
49 {
50 public:
51 ui::Widget m_pNotebook{ui::null};
52 ui::Window m_window{ui::null};
53
54 GroupDlg();
55 void Create( ui::Window parent );
56
57 void Show(){
58         // 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
59         m_position_tracker.sync( m_window );
60         m_window.show();
61 }
62 void Hide(){
63         m_window.hide();
64 }
65
66 WindowPositionTracker m_position_tracker;
67 };
68
69 namespace
70 {
71 GroupDlg g_GroupDlg;
72
73 std::size_t g_current_page;
74 std::vector<Callback<void(const Callback<void(const char *)> &)>> g_pages;
75 }
76
77 void GroupDialog_updatePageTitle( ui::Window window, std::size_t pageIndex ){
78         if ( pageIndex < g_pages.size() ) {
79                 g_pages[pageIndex]( PointerCaller<GtkWindow, void(const char*), gtk_window_set_title>( window ) );
80         }
81 }
82
83 static gboolean switch_page( GtkNotebook *notebook, gpointer page, guint page_num, gpointer data ){
84         GroupDialog_updatePageTitle( ui::Window::from(data), page_num );
85         g_current_page = page_num;
86
87         return FALSE;
88 }
89
90 GroupDlg::GroupDlg() : m_window( ui::null ){
91         m_position_tracker.setPosition( c_default_window_pos );
92 }
93
94 void GroupDlg::Create( ui::Window parent ){
95         ASSERT_MESSAGE( !m_window, "dialog already created" );
96
97         auto window = ui::Window(create_persistent_floating_window( "Entities", parent ));
98
99         global_accel_connect_window( window );
100
101         window_connect_focus_in_clear_focus_widget( window );
102
103         m_window = window;
104
105 #if GDEF_OS_WINDOWS
106         if ( g_multimon_globals.m_bStartOnPrimMon ) {
107                 WindowPosition pos( m_position_tracker.getPosition() );
108                 PositionWindowOnPrimaryScreen( pos );
109                 m_position_tracker.setPosition( pos );
110         }
111 #endif
112         m_position_tracker.connect( window );
113
114         {
115                 ui::Widget notebook = ui::Widget::from(gtk_notebook_new());
116                 notebook.show();
117                 window.add(notebook);
118                 gtk_notebook_set_tab_pos( GTK_NOTEBOOK( notebook ), GTK_POS_BOTTOM );
119                 m_pNotebook = notebook;
120
121                 notebook.connect( "switch_page", G_CALLBACK( switch_page ), (gpointer) window );
122         }
123 }
124
125
126 ui::Widget GroupDialog_addPage( const char* tabLabel, ui::Widget widget, const Callback<void(const Callback<void(const char *)> &)>& title ){
127         ui::Widget w = ui::Label( tabLabel );
128         w.show();
129         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 ) ));
130         g_pages.push_back( title );
131
132         return page;
133 }
134
135
136 bool GroupDialog_isShown(){
137         return g_GroupDlg.m_window.visible();
138 }
139 void GroupDialog_setShown( bool shown ){
140         shown ? g_GroupDlg.Show() : g_GroupDlg.Hide();
141 }
142 void GroupDialog_ToggleShow(){
143         GroupDialog_setShown( !GroupDialog_isShown() );
144 }
145
146 void GroupDialog_constructWindow( ui::Window main_window ){
147         g_GroupDlg.Create( main_window );
148 }
149 void GroupDialog_destroyWindow(){
150         ASSERT_TRUE( g_GroupDlg.m_window );
151         destroy_floating_window( g_GroupDlg.m_window );
152         g_GroupDlg.m_window = ui::Window{ui::null};
153 }
154
155
156 ui::Window GroupDialog_getWindow(){
157         return ui::Window(g_GroupDlg.m_window);
158 }
159 void GroupDialog_show(){
160         g_GroupDlg.Show();
161 }
162
163 ui::Widget GroupDialog_getPage(){
164         return ui::Widget::from(gtk_notebook_get_nth_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) ));
165 }
166
167 void GroupDialog_setPage( ui::Widget page ){
168         g_current_page = gtk_notebook_page_num( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), page );
169         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
170 }
171
172 #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
173 void GroupDialog_cycle();
174 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
175
176 void GroupDialog_showPage( ui::Widget page ){
177         if ( GroupDialog_getPage() == page ) {
178                 GroupDialog_ToggleShow();
179
180 #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
181                 /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
182                 /* this is very ugly: cycle to next tab then return to current tab immediately to force the refresh
183                  * this fixes the drawing of texture tab when window is restored and current tab is texture tab
184                  * this is called for nothing when windows is minimized and called for nothing when current tab
185                  * is not texture tab, hopefully it's a workaround that would disappear with gtk 3 */
186                 GroupDialog_cycle();
187                 GroupDialog_setPage( page );
188 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
189
190         }
191         else
192         {
193                 g_GroupDlg.m_window.show();
194                 GroupDialog_setPage( page );
195         }
196 }
197
198 void GroupDialog_cycle(){
199         g_current_page = ( g_current_page + 1 ) % g_pages.size();
200         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
201 }
202
203 void GroupDialog_updatePageTitle( ui::Widget page ){
204         if ( GroupDialog_getPage() == page ) {
205                 GroupDialog_updatePageTitle( g_GroupDlg.m_window, g_current_page );
206         }
207 }
208
209
210 #include "preferencesystem.h"
211
212 void GroupDialog_Construct(){
213         GlobalPreferenceSystem().registerPreference( "EntityWnd", make_property<WindowPositionTracker_String>( g_GroupDlg.m_position_tracker ) );
214
215         GlobalCommands_insert( "ViewEntityInfo", makeCallbackF(GroupDialog_ToggleShow), Accelerator( 'N' ) );
216 }
217 void GroupDialog_Destroy(){
218 }