-
Notifications
You must be signed in to change notification settings - Fork 1
/
wxwidgets_table_demoMain.cpp
145 lines (127 loc) · 5 KB
/
wxwidgets_table_demoMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************
* Name: wxwidgets_table_demoMain.cpp
* Purpose: Code for Application Frame
* Author: tsn (talipovsn@gmail.com)
* Created: 2020-02-26
* Copyright: tsn (github.com/tsnsoft)
* License:
**************************************************************/
#include "wxwidgets_table_demoMain.h"
#include <wx/msgdlg.h>
#include <iostream>
#include <sstream>
#include <cmath>
//(*InternalHeaders(wxwidgets_table_demoDialog)
#include <wx/intl.h>
#include <wx/string.h>
//*)
//helper functions
enum wxbuildinfoformat
{
short_f, long_f
};
wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);
if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif
#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}
//(*IdInit(wxwidgets_table_demoDialog)
const long wxwidgets_table_demoDialog::ID_GRID1 = wxNewId();
const long wxwidgets_table_demoDialog::ID_BUTTON1 = wxNewId();
const long wxwidgets_table_demoDialog::ID_BUTTON2 = wxNewId();
const long wxwidgets_table_demoDialog::ID_STATICTEXT1 = wxNewId();
//*)
BEGIN_EVENT_TABLE(wxwidgets_table_demoDialog,wxDialog)
//(*EventTable(wxwidgets_table_demoDialog)
//*)
END_EVENT_TABLE()
wxwidgets_table_demoDialog::wxwidgets_table_demoDialog(wxWindow* parent,wxWindowID id)
{
//(*Initialize(wxwidgets_table_demoDialog)
wxBoxSizer* BoxSizer2;
Create(parent, id, _("Ïðèìåð ðàáîòû ñ òàáëèöàìè wxGrid wxWidgets"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));
BoxSizer1 = new wxBoxSizer(wxVERTICAL);
Grid1 = new wxGrid(this, ID_GRID1, wxDefaultPosition, wxDefaultSize, 0, _T("ID_GRID1"));
Grid1->CreateGrid(7,5);
Grid1->EnableEditing(true);
Grid1->EnableGridLines(true);
Grid1->SetDefaultCellFont( Grid1->GetFont() );
Grid1->SetDefaultCellTextColour( Grid1->GetForegroundColour() );
BoxSizer1->Add(Grid1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
Button1 = new wxButton(this, ID_BUTTON1, _("Ñëó÷àéíûå"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
BoxSizer2->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Button2 = new wxButton(this, ID_BUTTON2, _("Ñóììà"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
BoxSizer2->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Ñóììà = \?"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
BoxSizer2->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer1->Add(BoxSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
SetSizer(BoxSizer1);
BoxSizer1->Fit(this);
BoxSizer1->SetSizeHints(this);
Center();
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxwidgets_table_demoDialog::OnButton1Click);
Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxwidgets_table_demoDialog::OnButton2Click);
//*)
}
wxwidgets_table_demoDialog::~wxwidgets_table_demoDialog()
{
//(*Destroy(wxwidgets_table_demoDialog)
//*)
}
// Ñëó÷àéíûå ÷èñëà â òàáëèöó
void wxwidgets_table_demoDialog::OnButton1Click(wxCommandEvent& event)
{
int rc = Grid1 -> GetNumberRows(); // Îïðåäåëÿåì êîëè÷åñòâî ñòðîê â òàáëèöå
int cc = Grid1 -> GetNumberCols(); // Îïðåäåëÿåì êîëè÷åñòâî ñòîëáöîâ â òàáëèöå
// Ïåðåáèðàåì âñå ñòðîêè òàáëèöû
for(int i=0; i<rc; i++)
{
// Ïåðåáèðàåì âñå ñòîëáöû òàáëèöû
for(int j=0; j<cc; j++)
{
int r = std::rand() % 41 - 20; // Ãåíåðèðóåì ñëó÷àéíîå ÷èñëî
// Ïðåîáðàçóåì ÷èñëî â ñòðîêó
std::ostringstream foo;
foo << r;
Grid1 -> SetCellValue(i,j, foo.str()); // Çàíåñåì â ÿ÷åéêó òàáëèöû íóæíîå çíà÷åíèå
}
}
// Ïðîãðàììíî íàæìåì íà êíîïêó ðàñ÷åòà ñóììû
OnButton2Click(event);
}
// Ñóììà ÷èñåë â òàáëèöå
void wxwidgets_table_demoDialog::OnButton2Click(wxCommandEvent& event)
{
int rc = Grid1 -> GetNumberRows(); // Îïðåäåëÿåì êîëè÷åñòâî ñòðîê â òàáëèöå
int cc = Grid1 -> GetNumberCols(); // Îïðåäåëÿåì êîëè÷åñòâî ñòîëáöîâ â òàáëèöå
int sum = 0;
wxString ss;
// Ïåðåáèðàåì âñå ñòðîêè òàáëèöû
for(int i=0; i<rc; i++)
{
// Ïåðåáèðàåì âñå ñòîëáöû òàáëèöû
for(int j=0; j<cc; j++)
{
ss = Grid1 -> GetCellValue(i,j); // Ñ÷èòûâàåì òåêñòîâîå çíà÷åíèå ÿ÷åéêè òàáëèöû
sum += strtol(ss, NULL, 0); // Íàêàïëèâàåì â ïåðåìåííîé çíà÷åíèå ñóììû, ïðåîáðàçîâàâ â ÷èñëî çíà÷åíèå ÿ÷åéêè
}
}
// Ïðåîáðàçóåì ÷èñëî â ñòðîêó
std::ostringstream foo;
foo << sum;
StaticText1->SetLabel("Ñóììà = " + foo.str()); // Âûäàäèì â êîìïîíåíò çíà÷åíèå ñóììû
}