2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
28 #include "preferencesystem.h"
30 #include "gtkutil/widget.h"
31 #include "signal/signal.h"
36 #include "preferences.h"
40 Signal0 g_gridChange_callbacks;
42 void AddGridChangeCallback( const SignalHandler& handler ){
43 g_gridChange_callbacks.connectLast( handler );
47 void GridChangeNotify(){
48 g_gridChange_callbacks();
68 typedef const char* GridName;
69 // this must match the GridPower enumeration
70 const GridName g_gridnames[] = {
85 inline GridPower GridPower_forGridDefault( int gridDefault ){
86 return static_cast<GridPower>( gridDefault - 3 );
89 inline int GridDefault_forGridPower( GridPower gridPower ){
93 int g_grid_default = GridDefault_forGridPower( GRIDPOWER_8 );
95 int g_grid_power = GridPower_forGridDefault( g_grid_default );
97 bool g_grid_snap = true;
103 inline float GridSize_forGridPower( int gridPower ){
104 return pow( 2.0f, gridPower );
107 float g_gridsize = GridSize_forGridPower( g_grid_power );
109 float GetSnapGridSize(){
110 return g_grid_snap ? g_gridsize : 0;
118 void setGridPower( GridPower power );
124 GridMenuItem( const GridMenuItem& other ); // NOT COPYABLE
125 GridMenuItem& operator=( const GridMenuItem& other ); // NOT ASSIGNABLE
129 GridMenuItem( GridPower id ) : m_id( id ), m_item( ExportCaller( *this ) ){
134 setGridPower( m_id );
136 typedef MemberCaller<GridMenuItem, void(), &GridMenuItem::set> SetCaller;
137 void active( const ImportExportCallback<bool>::Import_t& importCallback ){
138 importCallback( g_grid_power == m_id );
140 typedef MemberCaller<GridMenuItem, void(const ImportExportCallback<bool>::Import_t&), &GridMenuItem::active> ExportCaller;
143 GridMenuItem g_gridMenu0125( GRIDPOWER_0125 );
144 GridMenuItem g_gridMenu025( GRIDPOWER_025 );
145 GridMenuItem g_gridMenu05( GRIDPOWER_05 );
146 GridMenuItem g_gridMenu1( GRIDPOWER_1 );
147 GridMenuItem g_gridMenu2( GRIDPOWER_2 );
148 GridMenuItem g_gridMenu4( GRIDPOWER_4 );
149 GridMenuItem g_gridMenu8( GRIDPOWER_8 );
150 GridMenuItem g_gridMenu16( GRIDPOWER_16 );
151 GridMenuItem g_gridMenu32( GRIDPOWER_32 );
152 GridMenuItem g_gridMenu64( GRIDPOWER_64 );
153 GridMenuItem g_gridMenu128( GRIDPOWER_128 );
154 GridMenuItem g_gridMenu256( GRIDPOWER_256 );
156 void setGridPower( GridPower power ){
158 g_gridsize = GridSize_forGridPower( power );
160 g_gridMenu0125.m_item.update();
161 g_gridMenu025.m_item.update();
162 g_gridMenu05.m_item.update();
163 g_gridMenu1.m_item.update();
164 g_gridMenu2.m_item.update();
165 g_gridMenu4.m_item.update();
166 g_gridMenu8.m_item.update();
167 g_gridMenu16.m_item.update();
168 g_gridMenu32.m_item.update();
169 g_gridMenu64.m_item.update();
170 g_gridMenu128.m_item.update();
171 g_gridMenu256.m_item.update();
177 if ( g_grid_power > GRIDPOWER_0125 ) {
178 setGridPower( static_cast<GridPower>( --g_grid_power ) );
184 if ( g_grid_power < GRIDPOWER_256 ) {
185 setGridPower( static_cast<GridPower>( ++g_grid_power ) );
189 void ToggleGridSnap(){
190 g_grid_snap = !g_grid_snap;
194 void Grid_registerCommands(){
195 GlobalCommands_insert( "GridDown", makeCallbackF(GridPrev), Accelerator( '[' ) );
196 GlobalCommands_insert( "GridUp", makeCallbackF(GridNext), Accelerator( ']' ) );
198 GlobalCommands_insert( "ToggleGridSnap", makeCallbackF(ToggleGridSnap) );
200 GlobalToggles_insert( "SetGrid0.125", GridMenuItem::SetCaller( g_gridMenu0125 ), ToggleItem::AddCallbackCaller( g_gridMenu0125.m_item ) );
201 GlobalToggles_insert( "SetGrid0.25", GridMenuItem::SetCaller( g_gridMenu025 ), ToggleItem::AddCallbackCaller( g_gridMenu025.m_item ) );
202 GlobalToggles_insert( "SetGrid0.5", GridMenuItem::SetCaller( g_gridMenu05 ), ToggleItem::AddCallbackCaller( g_gridMenu05.m_item ) );
203 GlobalToggles_insert( "SetGrid1", GridMenuItem::SetCaller( g_gridMenu1 ), ToggleItem::AddCallbackCaller( g_gridMenu1.m_item ), Accelerator( '1' ) );
204 GlobalToggles_insert( "SetGrid2", GridMenuItem::SetCaller( g_gridMenu2 ), ToggleItem::AddCallbackCaller( g_gridMenu2.m_item ), Accelerator( '2' ) );
205 GlobalToggles_insert( "SetGrid4", GridMenuItem::SetCaller( g_gridMenu4 ), ToggleItem::AddCallbackCaller( g_gridMenu4.m_item ), Accelerator( '3' ) );
206 GlobalToggles_insert( "SetGrid8", GridMenuItem::SetCaller( g_gridMenu8 ), ToggleItem::AddCallbackCaller( g_gridMenu8.m_item ), Accelerator( '4' ) );
207 GlobalToggles_insert( "SetGrid16", GridMenuItem::SetCaller( g_gridMenu16 ), ToggleItem::AddCallbackCaller( g_gridMenu16.m_item ), Accelerator( '5' ) );
208 GlobalToggles_insert( "SetGrid32", GridMenuItem::SetCaller( g_gridMenu32 ), ToggleItem::AddCallbackCaller( g_gridMenu32.m_item ), Accelerator( '6' ) );
209 GlobalToggles_insert( "SetGrid64", GridMenuItem::SetCaller( g_gridMenu64 ), ToggleItem::AddCallbackCaller( g_gridMenu64.m_item ), Accelerator( '7' ) );
210 GlobalToggles_insert( "SetGrid128", GridMenuItem::SetCaller( g_gridMenu128 ), ToggleItem::AddCallbackCaller( g_gridMenu128.m_item ), Accelerator( '8' ) );
211 GlobalToggles_insert( "SetGrid256", GridMenuItem::SetCaller( g_gridMenu256 ), ToggleItem::AddCallbackCaller( g_gridMenu256.m_item ), Accelerator( '9' ) );
215 void Grid_constructMenu( ui::Menu menu ){
216 create_check_menu_item_with_mnemonic( menu, "Grid0.125", "SetGrid0.125" );
217 create_check_menu_item_with_mnemonic( menu, "Grid0.25", "SetGrid0.25" );
218 create_check_menu_item_with_mnemonic( menu, "Grid0.5", "SetGrid0.5" );
219 create_check_menu_item_with_mnemonic( menu, "Grid1", "SetGrid1" );
220 create_check_menu_item_with_mnemonic( menu, "Grid2", "SetGrid2" );
221 create_check_menu_item_with_mnemonic( menu, "Grid4", "SetGrid4" );
222 create_check_menu_item_with_mnemonic( menu, "Grid8", "SetGrid8" );
223 create_check_menu_item_with_mnemonic( menu, "Grid16", "SetGrid16" );
224 create_check_menu_item_with_mnemonic( menu, "Grid32", "SetGrid32" );
225 create_check_menu_item_with_mnemonic( menu, "Grid64", "SetGrid64" );
226 create_check_menu_item_with_mnemonic( menu, "Grid128", "SetGrid128" );
227 create_check_menu_item_with_mnemonic( menu, "Grid256", "SetGrid256" );
230 void Grid_registerShortcuts(){
231 command_connect_accelerator( "ToggleGrid" );
232 command_connect_accelerator( "GridDown" );
233 command_connect_accelerator( "GridUp" );
234 command_connect_accelerator( "ToggleGridSnap" );
237 void Grid_constructPreferences( PreferencesPage& page ){
239 "Default grid spacing",
241 ARRAY_RANGE( g_gridnames )
244 void Grid_constructPage( PreferenceGroup& group ){
245 PreferencesPage page( group.createPage( "Grid", "Grid Settings" ) );
246 Grid_constructPreferences( page );
248 void Grid_registerPreferencesPage(){
249 PreferencesDialog_addSettingsPage( makeCallbackF(Grid_constructPage) );
252 void Grid_construct(){
253 Grid_registerPreferencesPage();
255 g_grid_default = GridDefault_forGridPower( GRIDPOWER_8 );
257 GlobalPreferenceSystem().registerPreference( "GridDefault", IntImportStringCaller( g_grid_default ), IntExportStringCaller( g_grid_default ) );
259 g_grid_power = GridPower_forGridDefault( g_grid_default );
260 g_gridsize = GridSize_forGridPower( g_grid_power );