1 //
2 // matrixdialog.cc
3 //
4
5 #include "wx/button.h"
6 #include "wx/dialog.h"
7 #include "wx/grid.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/window.h"
14
15 #include <sstream>
16 #include <vector>
17 #include "graph.h"
18 #include "lang.h"
19 #include "matrix.h"
20 #include "matrixdialog.h"
21 #include "vertex.h"
22
23
MatrixGrid(wxWindow * parent)24 MatrixGrid::MatrixGrid (wxWindow *parent)
25 : wxGrid (parent, wxID_ANY)
26 {
27 }
28
SetScrollbar(int orient,int pos,int thumb,int range,bool refresh)29 void MatrixGrid::SetScrollbar (int orient, int pos, int thumb, int range, bool refresh)
30 {
31 int m_scrollstyle = 0;
32 if (m_scrollstyle & orient)
33 wxGrid::SetScrollbar (orient, pos, thumb, range, refresh);
34 else
35 wxGrid::SetScrollbar (orient, 0, 0, 0);
36 }
37
38 enum {
39 ID_SPINCTRL = 1000
40 };
41
BEGIN_EVENT_TABLE(MatrixDialog,wxDialog)42 BEGIN_EVENT_TABLE(MatrixDialog, wxDialog)
43 EVT_SPINCTRL(ID_SPINCTRL, MatrixDialog::OnChangeExponent)
44 END_EVENT_TABLE()
45
46 MatrixDialog::MatrixDialog (wxWindow *parent, const wxString &title,
47 const Matrix &mat, const Graph &g)
48 : wxDialog (parent, wxID_ANY, title)
49 {
50 unsigned int i;
51
52 wxBeginBusyCursor ();
53
54 // Create identity matrix (A^0)
55 Matrix id = mat - mat; // zero matrix
56 for (i = 0; i < id.rows (); ++i)
57 id(i, i) = 1;
58
59 // TODO: Check that mat.rows () == mat.columns ()
60 matrices.push_back (id);
61 matrices.push_back (mat);
62
63 wxBoxSizer *topsizer = new wxBoxSizer (wxVERTICAL);
64
65 // Create grid
66 m_grid = new MatrixGrid (this);
67 m_grid->CreateGrid (mat.rows (), mat.columns ());
68 m_grid->DisableCellEditControl ();
69 for (i = 0; i < mat.rows (); ++i) {
70 m_grid->SetColLabelValue (i, g[i]->label);
71 m_grid->SetRowLabelValue (i, g[i]->label);
72 }
73
74 topsizer->Add (m_grid, 1, wxCENTER | wxALL | wxEXPAND, 10);
75
76 // label + spin ctrl below grid
77 wxBoxSizer *expsizer = new wxBoxSizer (wxHORIZONTAL);
78 expsizer->Add (new wxStaticText (this, wxID_ANY, _("Exponent:")),
79 0, wxCENTER | wxLEFT | wxEXPAND, 10);
80 m_spinctrl = new wxSpinCtrl (this, ID_SPINCTRL, wxT("1"));
81 m_spinctrl->SetRange (0, MatrixDialog::max_exponent);
82 expsizer->Add (m_spinctrl, 0, wxCENTER | wxRIGHT, 10);
83 topsizer->Add (expsizer);
84
85 // static line
86 topsizer->Add (new wxStaticLine (this, wxID_ANY), 0,
87 wxEXPAND | wxALL, 10);
88
89 // OK button
90 topsizer->Add (CreateStdDialogButtonSizer (wxOK), 0,
91 wxEXPAND | wxALL, 10);
92
93
94 SetSizer (topsizer);
95 SetAutoLayout (true);
96
97 Centre (wxBOTH);
98
99 wxSpinEvent event;
100 OnChangeExponent (event);
101
102 wxEndBusyCursor ();
103 }
104
OnChangeExponent(wxSpinEvent & event)105 void MatrixDialog::OnChangeExponent (wxSpinEvent &event)
106 {
107 int exp = m_spinctrl->GetValue (), sz = matrices.size ();
108 unsigned int i, j;
109
110 // Compute any powers we don't already have cached
111 while (sz <= exp) {
112 matrices.push_back (matrices[sz - 1] * matrices[1]);
113 sz = matrices.size ();
114 }
115
116 Matrix &mat = matrices[exp];
117
118 // Add contents
119 m_grid->BeginBatch ();
120 for (j = 0; j < mat.rows (); ++j)
121 for (i = 0; i < mat.columns (); ++i) {
122 wxString str = wxString::Format (wxT("%d"), mat (i, j));
123 m_grid->SetCellValue (j, i, str);
124 }
125 m_grid->EndBatch ();
126 m_grid->AutoSize ();
127
128 m_grid->SetScrollbar (wxHORIZONTAL | wxVERTICAL, 0, 0, 0);
129 Fit ();
130 }
131