1 #include "windowattributespickle.h"
2 
3 #include "../helper/slconfig.h"
4 
5 #include <lslutils/misc.h>
6 #include <wx/settings.h>
7 
8 const unsigned int DEFSETT_MW_WIDTH = 880;
9 const unsigned int DEFSETT_MW_HEIGHT = 600;
10 const unsigned int DEFSETT_MW_TOP = 50;
11 const unsigned int DEFSETT_MW_LEFT = 50;
12 
WindowAttributesPickle(const wxString & name,wxTopLevelWindow * window,const wxSize & default_size)13 WindowAttributesPickle::WindowAttributesPickle( const wxString& name, wxTopLevelWindow* window, const wxSize& default_size  )
14 	: m_name( name ),
15 	  m_window( window ),
16 	  m_default_size( default_size )
17 {
18 	LoadAttributes();
19 }
20 
LoadAttributes()21 void WindowAttributesPickle::LoadAttributes()
22 {
23 	wxPoint pos = GetWindowPos( m_name, wxDefaultPosition );
24 	wxSize size = GetWindowSize( m_name, m_default_size );
25 	m_window->SetSize( pos.x , pos.y, size.GetWidth(), size.GetHeight() );
26 #ifndef __WXMAC__
27 	m_window->Maximize( GetWindowMaximized( m_name ) );
28 #endif
29 }
30 
~WindowAttributesPickle()31 WindowAttributesPickle::~WindowAttributesPickle()
32 {
33 	SaveAttributes();
34 }
35 
SaveAttributes()36 void WindowAttributesPickle::SaveAttributes()
37 {
38 	SetWindowSize( m_name, m_window->GetSize() );
39 	SetWindowPos( m_name, m_window->GetPosition() );
40 #ifndef __WXMAC__
41 	GetWindowMaximized( m_name, m_window->IsMaximized() );
42 #endif
43 }
44 
WindowHintsPickle(const wxString & name,wxTopLevelWindow * window,const wxSize & default_size)45 WindowHintsPickle::WindowHintsPickle( const wxString& name, wxTopLevelWindow* window, const wxSize& default_size  )
46 	: WindowAttributesPickle( name, window, default_size )
47 {
48 	// nop
49 }
50 
LoadAttributes()51 void WindowHintsPickle::LoadAttributes()
52 {
53 	WindowAttributesPickle::LoadAttributes();
54 	wxSize size = GetWindowSize( m_name, m_default_size );
55 	m_window->SetSizeHints( size );
56 }
57 
58 
59 // methods to interact with slconfig
GetWindowPos(const wxString & window,const wxPoint & def)60 wxPoint WindowAttributesPickle::GetWindowPos( const wxString& window, const wxPoint& def )
61 {
62 	wxPoint ret = def;
63 	ret.x = cfg().Read( _T( "/GUI/" ) + window + _T( "/left" ), ret.x );
64 	ret.y = cfg().Read( _T( "/GUI/" ) + window + _T( "/top" ), ret.y );
65 	return ret;
66 }
67 
SetWindowPos(const wxString & window,const wxPoint & pos)68 void WindowAttributesPickle::SetWindowPos( const wxString& window, const wxPoint& pos )
69 {
70 	SetWindowLeft( window, pos.x );
71 	SetWindowTop( window, pos.y );
72 }
73 
74 //some code duplication necessary to be able to simply use wx defaults
GetWindowSize(const wxString & window,const wxSize & def)75 wxSize  WindowAttributesPickle::GetWindowSize( const wxString& window, const wxSize& def )
76 {
77 	wxSize ret = def;
78 	ret.SetHeight( cfg().Read( _T( "/GUI/" ) + window + _T( "/height" ), ret.GetHeight() ) );
79 	ret.SetWidth( cfg().Read( _T( "/GUI/" ) + window + _T( "/width" ), ret.GetWidth() ) );
80 	return ret;
81 }
82 
SetWindowSize(const wxString & window,const wxSize & size)83 void WindowAttributesPickle::SetWindowSize( const wxString& window, const wxSize& size  )
84 {
85 	SetWindowWidth( window, size.GetWidth() );
86 	SetWindowHeight( window, size.GetHeight() );
87 }
88 
GetWindowMaximized(const wxString & window)89 bool WindowAttributesPickle::GetWindowMaximized( const wxString& window )
90 {
91 	return cfg().Read(_T( "/GUI/" ) + window + _T( "/maximized" ), 0l );
92 }
93 
GetWindowMaximized(const wxString & window,bool maximized)94 void WindowAttributesPickle::GetWindowMaximized( const wxString& window, bool maximized )
95 {
96 	cfg().Write(_T( "/GUI/" ) + window + _T( "/maximized" ), maximized );
97 }
98 
99 /************* SPRINGLOBBY WINDOW POS/SIZE   ******************/
100 //! @brief Get width of MainWindow.
GetWindowWidth(const wxString & window)101 int WindowAttributesPickle::GetWindowWidth( const wxString& window )
102 {
103 	return cfg().Read( _T( "/GUI/" ) + window + _T( "/width" ), DEFSETT_MW_WIDTH );
104 }
105 
106 
107 //! @brief Set width position of MainWindow
SetWindowWidth(const wxString & window,const int value)108 void WindowAttributesPickle::SetWindowWidth( const wxString& window, const int value )
109 {
110 	cfg().Write(
111 		_T( "/GUI/" ) + window + _T( "/width" ),
112 		LSL::Util::Clamp(  value,
113 						   wxSystemSettings::GetMetric( wxSYS_WINDOWMIN_X ),
114 						   wxSystemSettings::GetMetric( wxSYS_SCREEN_X )
115 						)
116 	);
117 }
118 
119 
120 //! @brief Get height of MainWindow.
GetWindowHeight(const wxString & window)121 int WindowAttributesPickle::GetWindowHeight( const wxString& window )
122 {
123 	return cfg().Read( _T( "/GUI/" ) + window + _T( "/height" ), DEFSETT_MW_HEIGHT );
124 }
125 
126 
127 //! @brief Set height position of MainWindow
SetWindowHeight(const wxString & window,const int value)128 void WindowAttributesPickle::SetWindowHeight( const wxString& window, const int value )
129 {
130 	cfg().Write(
131 		_T( "/GUI/" ) + window + _T( "/height" ),
132 		LSL::Util::Clamp(  value,
133 						   wxSystemSettings::GetMetric( wxSYS_WINDOWMIN_Y ),
134 						   wxSystemSettings::GetMetric( wxSYS_SCREEN_Y )
135 						)
136 	);
137 }
138 
139 
140 //! @brief Get top position of MainWindow.
GetWindowTop(const wxString & window)141 int WindowAttributesPickle::GetWindowTop( const wxString& window )
142 {
143 	return cfg().Read( _T( "/GUI/" ) + window + _T( "/top" ), DEFSETT_MW_TOP );
144 }
145 
146 
147 //! @brief Set top position of MainWindow
SetWindowTop(const wxString & window,const int value)148 void WindowAttributesPickle::SetWindowTop( const wxString& window, const int value )
149 {
150 	cfg().Write(
151 		_T( "/GUI/" ) + window + _T( "/top" ),
152 		LSL::Util::Clamp( value,
153 						  0,
154 						  wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ) - 20
155 						)
156 	);
157 }
158 
159 
160 //! @brief Get left position of MainWindow.
GetWindowLeft(const wxString & window)161 int WindowAttributesPickle::GetWindowLeft( const wxString& window )
162 {
163 	return cfg().Read( _T( "/GUI/" ) + window + _T( "/left" ), DEFSETT_MW_LEFT );
164 }
165 
166 //! @brief Set left position of MainWindow
SetWindowLeft(const wxString & window,const int value)167 void WindowAttributesPickle::SetWindowLeft( const wxString& window, const int value )
168 {
169 	cfg().Write(
170 		_T( "/GUI/" ) + window + _T( "/left" ),
171 		LSL::Util::Clamp( value,
172 						  0,
173 						  wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 20
174 						)
175 	);
176 }
177