1 //
2 //	paramdialog.cc
3 //
4 
5 #include "wx/button.h"
6 #include "wx/dialog.h"
7 #include "wx/radiobut.h"
8 #include "wx/sizer.h"
9 #include "wx/spinctrl.h"
10 #include "wx/statline.h"
11 #include "wx/stattext.h"
12 #include "wx/string.h"
13 #include "wx/utils.h"
14 
15 #include "edge.h"
16 #include "lang.h"
17 #include "paramdialog.h"
18 #include "vertex.h"
19 
20 
BEGIN_EVENT_TABLE(ParamDialogIntInt,wxDialog)21 BEGIN_EVENT_TABLE(ParamDialogIntInt, wxDialog)
22     EVT_BUTTON(wxID_OK,		ParamDialogIntInt::OnOK)
23     EVT_BUTTON(wxID_CANCEL,	ParamDialogIntInt::OnCancel)
24 END_EVENT_TABLE()
25 
26 ParamDialogIntInt::ParamDialogIntInt (wxWindow *parent, const wxString &title,
27 	const wxString &prompt, long value1, long min1, long max1,
28 				long value2, long min2, long max2)
29                    			: wxDialog (parent, wxID_ANY, title)
30 {
31 	m_value1 = value1;
32 	m_max1 = max1;
33 	m_min1 = min1;
34 	m_value2 = value2;
35 	m_max2 = max2;
36 	m_min2 = min2;
37 
38 	wxBeginBusyCursor();
39 
40 	wxBoxSizer *topsizer = new wxBoxSizer (wxVERTICAL);
41 
42 	// 2) prompt and spin ctrl
43 	wxBoxSizer *inputsizer = new wxBoxSizer (wxHORIZONTAL);
44 
45 	// prompt, if any
46 	if (!prompt.IsEmpty ())
47         	inputsizer->Add (new wxStaticText (this, wxID_ANY, prompt),
48 					0, wxCENTER | wxLEFT | wxEXPAND, 10);
49 
50 	// spin ctrl
51 	wxString valStr1, valStr2;
52 	valStr1.Printf (wxT("%ld"), m_value1);
53 	valStr2.Printf (wxT("%ld"), m_value2);
54 	m_spinctrl1 = new wxSpinCtrl (this, wxID_ANY, valStr1);
55 	m_spinctrl2 = new wxSpinCtrl (this, wxID_ANY, valStr2);
56 	m_spinctrl1->SetRange (int (m_min1), int (m_max1));
57 	m_spinctrl2->SetRange (int (m_min2), int (m_max2));
58 	inputsizer->Add (m_spinctrl1, 1, wxCENTER | wxLEFT | wxRIGHT, 10);
59 	inputsizer->Add (m_spinctrl2, 1, wxCENTER | wxLEFT | wxRIGHT, 10);
60 
61 	// add both
62 	topsizer->Add (inputsizer, 1, wxEXPAND | wxALL, 5);
63 
64 	// 3) static line
65 	topsizer->Add (new wxStaticLine (this, wxID_ANY), 0,
66 						wxEXPAND | wxALL, 10);
67 
68 	// 4) buttons
69 	topsizer->Add (CreateButtonSizer (wxOK | wxCANCEL), 0,
70 							wxEXPAND | wxALL, 10);
71 
72 	SetSizer (topsizer);
73 	SetAutoLayout (true);
74 
75 	topsizer->SetSizeHints (this);
76 	topsizer->Fit (this);
77 
78 	Centre (wxBOTH);
79 
80 	m_spinctrl1->SetSelection (-1, -1);
81 	m_spinctrl1->SetFocus ();
82 
83 	wxEndBusyCursor ();
84 }
85 
OnOK(wxCommandEvent & event)86 void ParamDialogIntInt::OnOK (wxCommandEvent &event)
87 {
88 	m_value1 = m_spinctrl1->GetValue ();
89 	if ((m_value1 < m_min1) || (m_value1 > m_max1)) {
90 		// not a number or out of range
91 		m_value1 = -1;
92 		EndModal (wxID_CANCEL);
93 	}
94 	m_value2 = m_spinctrl2->GetValue ();
95 	if ((m_value2 < m_min2) || (m_value2 > m_max2)) {
96 		// not a number or out of range
97 		m_value2 = -1;
98 		EndModal (wxID_CANCEL);
99 	}
100 
101 	EndModal (wxID_OK);
102 }
103 
OnCancel(wxCommandEvent & event)104 void ParamDialogIntInt::OnCancel (wxCommandEvent &event)
105 {
106 	EndModal (wxID_CANCEL);
107 }
108 
109 ////////////////////////////////////////////////////////////
110 
111 enum {
112 	ID_SPIN = 1000,
113 
114 	ID_EDGE_RIGHT,
115 	ID_EDGE_NONE,
116 	ID_EDGE_LEFT
117 };
118 
BEGIN_EVENT_TABLE(ParamDialogEdge,wxDialog)119 BEGIN_EVENT_TABLE(ParamDialogEdge, wxDialog)
120     EVT_BUTTON(wxID_OK,		ParamDialogEdge::OnOK)
121     EVT_BUTTON(wxID_CANCEL,	ParamDialogEdge::OnCancel)
122     EVT_SPINCTRL(ID_SPIN,	ParamDialogEdge::OnSpin)
123     EVT_RADIOBUTTON(ID_EDGE_RIGHT,	ParamDialogEdge::OnEdgeDir)
124     EVT_RADIOBUTTON(ID_EDGE_NONE,	ParamDialogEdge::OnEdgeDir)
125     EVT_RADIOBUTTON(ID_EDGE_LEFT,	ParamDialogEdge::OnEdgeDir)
126 END_EVENT_TABLE()
127 
128 ParamDialogEdge::ParamDialogEdge (wxWindow *parent, const wxString &title,
129 							const Edge *e)
130                    		: wxDialog (parent, wxID_ANY, title)
131 {
132 	m_weight = e->weight;
133 	m_flow = e->flow;
134 	m_dir = e->directed ? 1 : 0;
135 
136 	dir_inverted = (e->w->label < e->v->label);
137 	if (dir_inverted)
138 		m_dir = -m_dir;
139 
140 	wxBeginBusyCursor();
141 
142 	wxBoxSizer *topsizer = new wxBoxSizer (wxVERTICAL);
143 
144 	// 2) prompt and spin ctrl
145 	wxBoxSizer *inputsizer0 = new wxBoxSizer (wxHORIZONTAL);
146 	wxBoxSizer *inputsizer1 = new wxBoxSizer (wxHORIZONTAL);
147 	wxBoxSizer *inputsizer2 = new wxBoxSizer (wxHORIZONTAL);
148 
149 	// prompts
150 	inputsizer0->Add (new wxStaticText (this, wxID_ANY,
151 				_("Edge weight:")),
152 				0, wxCENTER | wxLEFT, 10);
153 	inputsizer1->Add (new wxStaticText (this, wxID_ANY,
154 				_("Edge flow:")),
155 				0, wxCENTER | wxLEFT, 10);
156 
157 	// spin ctrls
158 	wxString valStr1;
159 	valStr1.Printf (wxT("%ld"), m_weight);
160 	m_spinctrl_weight = new wxSpinCtrl (this, ID_SPIN, valStr1);
161 	m_spinctrl_weight->SetRange (0, 10000);	// FIXME: arbitrary bounds!
162 	inputsizer0->Add (m_spinctrl_weight, 1, wxCENTER | wxLEFT | wxRIGHT, 5);
163 
164 	wxString valStr2;
165 	valStr2.Printf (wxT("%ld"), m_flow);
166 	m_spinctrl_flow = new wxSpinCtrl (this, ID_SPIN, valStr2);
167 	m_spinctrl_flow->SetRange (0, m_weight);
168 	inputsizer1->Add (m_spinctrl_flow, 1, wxCENTER | wxLEFT | wxRIGHT, 5);
169 
170 	// edge direction
171 	inputsizer2->Add (new wxStaticText (this, wxID_ANY,
172 				_("Edge direction:")),
173 				0, wxCENTER | wxLEFT | wxEXPAND, 10);
174 	wxBoxSizer *dirsizer = new wxBoxSizer (wxVERTICAL);
175 	wxString rightS = wxT("--->"), noneS = wxT("----"), leftS = wxT("<---");
176 	wxString leftV = e->v->label, rightV = e->w->label;
177 	if (dir_inverted) {
178 		wxString tmp = leftV;
179 		leftV = rightV;
180 		rightV = tmp;
181 	}
182 	rightS = leftV + rightS + rightV;
183 	noneS = leftV + noneS + rightV;
184 	leftS = leftV + leftS + rightV;
185 	wxRadioButton *rightB = new wxRadioButton (this, ID_EDGE_RIGHT, rightS,
186 			wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
187 	wxRadioButton *noneB = new wxRadioButton (this, ID_EDGE_NONE, noneS);
188 	wxRadioButton *leftB = new wxRadioButton (this, ID_EDGE_LEFT, leftS);
189 	dirsizer->Add (rightB);
190 	dirsizer->Add (noneB);
191 	dirsizer->Add (leftB);
192 	inputsizer2->Add (dirsizer, 0, wxCENTER, 10);
193 	if (m_dir == 0)
194 		noneB->SetValue (true);
195 	else {
196 		if (!dir_inverted)
197 			rightB->SetValue (true);
198 		else
199 			leftB->SetValue (true);
200 	}
201 
202 	// add both
203 	topsizer->Add (inputsizer0, 1, wxEXPAND | wxALL, 5);
204 	topsizer->Add (inputsizer1, 1, wxEXPAND | wxALL, 5);
205 	topsizer->Add (inputsizer2, 1, wxEXPAND | wxALL, 5);
206 
207 	// 3) static line
208 	topsizer->Add (new wxStaticLine (this, wxID_ANY), 0,
209 						wxEXPAND | wxALL, 10);
210 
211 	// 4) buttons
212 	topsizer->Add (CreateButtonSizer (wxOK | wxCANCEL), 0,
213 							wxEXPAND | wxALL, 10);
214 
215 	SetSizer (topsizer);
216 	SetAutoLayout (true);
217 
218 	topsizer->SetSizeHints (this);
219 	topsizer->Fit (this);
220 
221 	CentreOnParent ();
222 
223 	m_spinctrl_weight->SetSelection (-1, -1);
224 	m_spinctrl_weight->SetFocus ();
225 
226 	wxEndBusyCursor();
227 }
228 
OnOK(wxCommandEvent & event)229 void ParamDialogEdge::OnOK (wxCommandEvent &event)
230 {
231 	m_weight = m_spinctrl_weight->GetValue ();
232 	if ((m_weight < 0) || (m_weight > m_spinctrl_weight->GetMax ())) {
233 		// not a number or out of range
234 		m_weight = -1;
235 		EndModal (wxID_CANCEL);
236 	}
237 	m_flow = m_spinctrl_flow->GetValue ();
238 	if ((m_flow < 0) || (m_flow > m_spinctrl_flow->GetMax ())) {
239 		// not a number or out of range
240 		m_flow = -1;
241 		EndModal (wxID_CANCEL);
242 	}
243 	if (m_flow > m_weight) {
244 		// out of range
245 		m_flow = -1;
246 		EndModal (wxID_CANCEL);
247 	}
248 	if (dir_inverted)
249 		m_dir = -m_dir;
250 
251 	EndModal (wxID_OK);
252 }
253 
OnCancel(wxCommandEvent & event)254 void ParamDialogEdge::OnCancel (wxCommandEvent &event)
255 {
256 	EndModal (wxID_CANCEL);
257 }
258 
OnSpin(wxSpinEvent & event)259 void ParamDialogEdge::OnSpin (wxSpinEvent &event)
260 {
261 	m_weight = m_spinctrl_weight->GetValue ();
262 	if ((m_weight < 0) || (m_weight > m_spinctrl_weight->GetMax ())) {
263 		// not a number or out of range
264 		return;
265 	}
266 
267 	m_flow = m_spinctrl_flow->GetValue ();
268 	if (m_flow > m_weight)
269 		m_spinctrl_flow->SetValue (m_weight);	// clamp
270 
271 	m_spinctrl_flow->SetRange (0, m_weight);
272 	m_spinctrl_flow->Refresh ();
273 }
274 
OnEdgeDir(wxCommandEvent & event)275 void ParamDialogEdge::OnEdgeDir (wxCommandEvent &event)
276 {
277 	switch (event.GetId ()) {
278 		case ID_EDGE_RIGHT:
279 			m_dir = 1;
280 			break;
281 		case ID_EDGE_NONE:
282 			m_dir = 0;
283 			break;
284 		case ID_EDGE_LEFT:
285 			m_dir = -1;
286 			break;
287 	}
288 }
289