]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/groupdialog.cpp
Merge branch 'TimePath/gtk++' 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
31 #include "debugging/debugging.h"
32
33 #include <vector>
34 #include <gtk/gtk.h>
35
36 #include "gtkutil/widget.h"
37 #include "gtkutil/accelerator.h"
38 #include "entityinspector.h"
39 #include "gtkmisc.h"
40 #include "multimon.h"
41 #include "console.h"
42 #include "commands.h"
43
44
45 #include "gtkutil/window.h"
46
47 class GroupDlg
48 {
49 public:
50 ui::Widget m_pNotebook;
51 ui::Window m_window;
52
53 GroupDlg();
54 void Create( ui::Window parent );
55
56 void Show(){
57         // 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
58         m_position_tracker.sync( m_window );
59         m_window.show();
60 }
61 void Hide(){
62         gtk_widget_hide( GTK_WIDGET( m_window ) );
63 }
64
65 WindowPositionTracker m_position_tracker;
66 };
67
68 namespace
69 {
70 GroupDlg g_GroupDlg;
71
72 std::size_t g_current_page;
73 std::vector<StringExportCallback> g_pages;
74 }
75
76 void GroupDialog_updatePageTitle( ui::Window window, std::size_t pageIndex ){
77         if ( pageIndex < g_pages.size() ) {
78                 g_pages[pageIndex]( PointerCaller1<GtkWindow, const char*, gtk_window_set_title>( window ) );
79         }
80 }
81
82 static gboolean switch_page( GtkNotebook *notebook, gpointer page, guint page_num, gpointer data ){
83         GroupDialog_updatePageTitle( ui::Window(GTK_WINDOW( data )), page_num );
84         g_current_page = page_num;
85
86         return FALSE;
87 }
88
89 GroupDlg::GroupDlg() : m_window( 0 ){
90         m_position_tracker.setPosition( c_default_window_pos );
91 }
92
93 void GroupDlg::Create( ui::Window parent ){
94         ASSERT_MESSAGE( !m_window, "dialog already created" );
95
96         auto window = ui::Window(create_persistent_floating_window( "Entities", parent ));
97
98         global_accel_connect_window( window );
99
100         window_connect_focus_in_clear_focus_widget( window );
101
102         m_window = window;
103
104 #ifdef WIN32
105         if ( g_multimon_globals.m_bStartOnPrimMon ) {
106                 WindowPosition pos( m_position_tracker.getPosition() );
107                 PositionWindowOnPrimaryScreen( pos );
108                 m_position_tracker.setPosition( pos );
109         }
110 #endif
111         m_position_tracker.connect( window );
112
113         {
114                 ui::Widget notebook = ui::Widget(gtk_notebook_new());
115                 notebook.show();
116                 window.add(notebook);
117                 gtk_notebook_set_tab_pos( GTK_NOTEBOOK( notebook ), GTK_POS_BOTTOM );
118                 m_pNotebook = notebook;
119
120                 notebook.connect( "switch_page", G_CALLBACK( switch_page ), (gpointer) window );
121         }
122 }
123
124
125 ui::Widget GroupDialog_addPage( const char* tabLabel, ui::Widget widget, const StringExportCallback& title ){
126         ui::Widget w = ui::Label( tabLabel );
127         w.show();
128         ui::Widget page = ui::Widget(gtk_notebook_get_nth_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gtk_notebook_insert_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), widget, w, -1 ) ));
129         g_pages.push_back( title );
130
131         return page;
132 }
133
134
135 bool GroupDialog_isShown(){
136         return widget_is_visible( g_GroupDlg.m_window );
137 }
138 void GroupDialog_setShown( bool shown ){
139         shown ? g_GroupDlg.Show() : g_GroupDlg.Hide();
140 }
141 void GroupDialog_ToggleShow(){
142         GroupDialog_setShown( !GroupDialog_isShown() );
143 }
144
145 void GroupDialog_constructWindow( ui::Window main_window ){
146         g_GroupDlg.Create( main_window );
147 }
148 void GroupDialog_destroyWindow(){
149         ASSERT_TRUE( g_GroupDlg.m_window );
150         destroy_floating_window( g_GroupDlg.m_window );
151         g_GroupDlg.m_window = ui::Window{ui::null};
152 }
153
154
155 ui::Window GroupDialog_getWindow(){
156         return ui::Window(g_GroupDlg.m_window);
157 }
158 void GroupDialog_show(){
159         g_GroupDlg.Show();
160 }
161
162 ui::Widget GroupDialog_getPage(){
163         return ui::Widget(gtk_notebook_get_nth_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) ));
164 }
165
166 void GroupDialog_setPage( ui::Widget page ){
167         g_current_page = gtk_notebook_page_num( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), page );
168         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
169 }
170
171 void GroupDialog_showPage( ui::Widget page ){
172         if ( GroupDialog_getPage() == page ) {
173                 GroupDialog_ToggleShow();
174         }
175         else
176         {
177                 g_GroupDlg.m_window.show();
178                 GroupDialog_setPage( page );
179         }
180 }
181
182 void GroupDialog_cycle(){
183         g_current_page = ( g_current_page + 1 ) % g_pages.size();
184         gtk_notebook_set_current_page( GTK_NOTEBOOK( g_GroupDlg.m_pNotebook ), gint( g_current_page ) );
185 }
186
187 void GroupDialog_updatePageTitle( ui::Widget page ){
188         if ( GroupDialog_getPage() == page ) {
189                 GroupDialog_updatePageTitle( g_GroupDlg.m_window, g_current_page );
190         }
191 }
192
193
194 #include "preferencesystem.h"
195
196 void GroupDialog_Construct(){
197         GlobalPreferenceSystem().registerPreference( "EntityWnd", WindowPositionTrackerImportStringCaller( g_GroupDlg.m_position_tracker ), WindowPositionTrackerExportStringCaller( g_GroupDlg.m_position_tracker ) );
198
199         GlobalCommands_insert( "ViewEntityInfo", FreeCaller<GroupDialog_ToggleShow>(), Accelerator( 'N' ) );
200 }
201 void GroupDialog_Destroy(){
202 }