comparison src/mainframe.cpp @ 0:cb3403ca39b1

First release.
author pyon@macmini
date Sun, 30 Aug 2015 21:53:19 +0900
parents
children e4aa0e7a07ad
comparison
equal deleted inserted replaced
-1:000000000000 0:cb3403ca39b1
1 // Filename: mainframe.cpp
2 // Last Change: 2015-08-30 Sun 21:49:16.
3 //
4 #include <wx/filedlg.h>
5 #include <wx/textfile.h>
6 #include <wx/clipbrd.h>
7 #include <wx/utils.h>
8 #include <wx/msgdlg.h>
9
10 #include "mainframe.h"
11 #include "adddialog.h"
12
13 // resources
14 #if !defined(__WXMSW__) && !defined(__WXPM__)
15 #include "sample.xpm"
16 #endif
17
18 MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style )
19 : wxFrame( parent, id, title, pos, size, style ), m_timer( this, ID_TIMER )
20 {
21 this->SetSizeHints( wxDefaultSize, wxDefaultSize );
22 this->SetBackgroundColour( wxColour( wxT("WHEAT") ) );
23 SetIcon( wxICON( sample ) );
24
25 // menu bar
26 m_menubar = new wxMenuBar( 0 );
27 m_menuFile = new wxMenu();
28 wxMenuItem* m_menuItemAbout = new wxMenuItem( m_menuFile, ID_MNABOUT, wxString( wxT("About") ), wxEmptyString, wxITEM_NORMAL );
29 m_menuFile->Append( m_menuItemAbout );
30
31 wxMenuItem* m_menuItemExit = new wxMenuItem( m_menuFile, ID_MNEXIT, wxString( wxT("Exit") ), wxEmptyString, wxITEM_NORMAL );
32 m_menuFile->Append( m_menuItemExit );
33
34 m_menubar->Append( m_menuFile, wxT("File") );
35
36 m_menuPlugin = new wxMenu();
37 wxMenuItem* m_menuItemLoadPgin = new wxMenuItem( m_menuPlugin, ID_MNLDPGIN, wxString( wxT("Load") ), wxEmptyString, wxITEM_NORMAL );
38 m_menuPlugin->Append( m_menuItemLoadPgin );
39
40 wxMenuItem* m_menuItemSavePgin = new wxMenuItem( m_menuPlugin, ID_MNSVPGIN, wxString( wxT("Save as") ), wxEmptyString, wxITEM_NORMAL );
41 m_menuPlugin->Append( m_menuItemSavePgin );
42
43 m_menubar->Append( m_menuPlugin, wxT("Plugin") );
44
45 this->SetMenuBar( m_menubar );
46
47 // controls
48 wxBoxSizer* bSizerTop = new wxBoxSizer( wxVERTICAL );
49
50 //
51 wxBoxSizer* bSizerShow = new wxBoxSizer( wxHORIZONTAL );
52
53 m_textCtrlShow = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY );
54 bSizerShow->Add( m_textCtrlShow, 1, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
55
56 m_toggleBtn = new wxToggleButton( this, ID_TGL, wxT("ON"), wxDefaultPosition, wxSize( 60, -1 ), 0 );
57 bSizerShow->Add( m_toggleBtn, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
58
59 bSizerTop->Add( bSizerShow, 0, wxEXPAND, 5 );
60
61 //
62 wxBoxSizer* bSizerList = new wxBoxSizer( wxHORIZONTAL );
63
64 m_listView = new wxListView( this, ID_LIST, wxDefaultPosition, wxDefaultSize, wxLC_REPORT|wxLC_SINGLE_SEL );
65 bSizerList->Add( m_listView, 1, wxALL|wxEXPAND, 5 );
66 wxListItem itemCol;
67 itemCol.SetText( wxT("No") );
68 m_listView->InsertColumn( 0, itemCol );
69 m_listView->SetColumnWidth( 0, 40 );
70 itemCol.SetText( wxT("text") );
71 m_listView->InsertColumn( 1, itemCol );
72 m_listView->SetColumnWidth( 1, 80 );
73 itemCol.SetText( wxT("time") );
74 m_listView->InsertColumn( 2, itemCol );
75 m_listView->SetColumnWidth( 2, 40 );
76 itemCol.SetText( wxT("type") );
77 m_listView->InsertColumn( 3, itemCol );
78 m_listView->SetColumnWidth( 3, 40 );
79 itemCol.SetText( wxT("desc") );
80 m_listView->InsertColumn( 4, itemCol );
81 m_listView->SetColumnWidth( 4, 80 );
82 //m_listView->EnableAlternateRowColours( true );
83
84 wxBoxSizer* bSizerBtn = new wxBoxSizer( wxVERTICAL );
85
86 m_buttonUp = new wxButton( this, ID_BTNUP, wxT("↑"), wxDefaultPosition, wxSize( 40, -1 ), 0 );
87 bSizerBtn->Add( m_buttonUp, 0, wxALL, 5 );
88
89 m_buttonDown = new wxButton( this, ID_BTNDOWN, wxT("↓"), wxDefaultPosition, wxSize( 40, -1 ), 0 );
90 bSizerBtn->Add( m_buttonDown, 0, wxALL, 5 );
91
92 m_buttonDel = new wxButton( this, ID_BTNDEL, wxT("-"), wxDefaultPosition, wxSize( 40, -1 ), 0 );
93 bSizerBtn->Add( m_buttonDel, 0, wxALL, 5 );
94
95 m_buttonAdd = new wxButton( this, ID_BTNADD, wxT("+"), wxDefaultPosition, wxSize( 40, -1 ), 0 );
96 bSizerBtn->Add( m_buttonAdd, 0, wxALL, 5 );
97
98 bSizerBtn->Add( 0, 0, 1, wxEXPAND, 5 );
99
100 m_buttonExit = new wxButton( this, ID_EXIT, wxT("Exit"), wxDefaultPosition, wxSize( 60, -1 ), 0 );
101 bSizerBtn->Add( m_buttonExit, 0, wxALL, 5 );
102
103 bSizerList->Add( bSizerBtn, 0, wxEXPAND, 5 );
104 bSizerTop->Add( bSizerList, 1, wxEXPAND, 5 );
105
106 this->SetSizer( bSizerTop );
107 this->Layout();
108
109 this->Centre( wxBOTH );
110 }
111
112 MainFrame::~MainFrame()
113 {
114 }
115
116 // Event Table
117 BEGIN_EVENT_TABLE( MainFrame, wxFrame )
118 EVT_MENU( ID_MNABOUT, MainFrame::OnMNAbout )
119 EVT_MENU( ID_MNEXIT, MainFrame::OnMNExit )
120 EVT_MENU( ID_MNLDPGIN, MainFrame::OnMNLoad )
121 EVT_MENU( ID_MNSVPGIN, MainFrame::OnMNSaveAs )
122 EVT_LIST_ITEM_ACTIVATED( ID_LIST, MainFrame::OnDClickItem )
123 EVT_LIST_ITEM_SELECTED( ID_LIST, MainFrame::OnSelectItem )
124 EVT_TIMER( ID_TIMER, MainFrame::OnTimer )
125 EVT_BUTTON( ID_BTNUP, MainFrame::OnBtnUp )
126 EVT_BUTTON( ID_BTNDOWN, MainFrame::OnBtnDown )
127 EVT_BUTTON( ID_BTNDEL, MainFrame::OnBtnDel )
128 EVT_BUTTON( ID_BTNADD, MainFrame::OnBtnAdd )
129 EVT_BUTTON( ID_EXIT, MainFrame::OnBtnExit )
130 EVT_TOGGLEBUTTON( ID_TGL, MainFrame::OnToggle )
131 /*
132 EVT_IDLE( MainFrame::OnIdle )
133 EVT_CLOSE( MainFrame::OnClose )
134 */
135 END_EVENT_TABLE()
136
137 /* Event Handlers & Functions */
138 // Event Handlers
139 void MainFrame::OnMNAbout( wxCommandEvent& WXUNUSED(event) )
140 {
141 wxInfoMessageBox( this );
142 }
143
144 void MainFrame::OnMNExit( wxCommandEvent& WXUNUSED(event) )
145 {
146 Close();
147 }
148
149 void MainFrame::OnMNLoad( wxCommandEvent& WXUNUSED(event) )
150 {
151 wxString plugin_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("plugin");
152 wxFileDialog fd( this, wxT("Select Plug-in file"), plugin_dir, wxEmptyString, wxT("Plug-in files (*.qbrd)|*.qbrd"), wxFD_OPEN|wxFD_FILE_MUST_EXIST );
153 if ( fd.ShowModal() == wxID_CANCEL ) return; // the user changed idea...
154
155 wxTextFile file;
156 file.Open( fd.GetPath() );
157 m_listView->DeleteAllItems();
158 for ( int i = 0, n = 0; i < file.GetLineCount(); i++ ) {
159 if ( file[i].StartsWith( wxT("#") ) )
160 continue;
161 wxArrayString s = wxSplit( file[i], ',', '\\' );
162
163 m_listView->InsertItem( n, wxString::Format( wxT("%d"), n + 1 ) );
164 m_listView->SetItem( n, 1, s[0] );
165 m_listView->SetItem( n, 2, s[1] );
166 m_listView->SetItem( n, 3, s[2] );
167 n++;
168 }
169 file.Close();
170
171 m_current = 0;
172 m_last = m_listView->GetItemText( m_current, 1 );
173 m_listView->SetItemState( m_current, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
174 }
175
176 void MainFrame::OnMNSaveAs( wxCommandEvent& WXUNUSED(event) )
177 {
178 wxString plugin_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("plugin");
179 wxFileDialog fd( this, wxT("Save Plug-in file"), plugin_dir , wxEmptyString, wxT("Plug-in files (*.qbrd)|*.qbrd"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT );
180 if ( fd.ShowModal() == wxID_CANCEL ) return; // the user changed idea...
181
182 wxTextFile file( fd.GetPath() );
183 if ( file.Exists() ) {
184 file.Open();
185 file.Clear();
186 }
187 else {
188 file.Create();
189 }
190 for ( int r = 0; r < m_listView->GetItemCount(); r++ ) {
191 wxArrayString s;
192 for ( int c = 0; c < m_listView->GetColumnCount(); c++ ) {
193 s.Add( m_listView->GetItemText( r, c ) );
194 }
195 file.AddLine( wxJoin( s, ',', '\\' ) );
196 }
197 file.Write();
198 file.Close();
199 }
200
201 void MainFrame::OnDClickItem( wxListEvent& event )
202 {
203 long i = event.GetIndex();
204 }
205
206 void MainFrame::OnSelectItem( wxListEvent& event )
207 {
208 long i = event.GetIndex();
209 m_current = i;
210 }
211
212 // ↑
213 void MainFrame::OnBtnUp( wxCommandEvent& WXUNUSED(event) )
214 {
215 long item = -1;
216 item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
217 if ( item == -1 || item == 0 ) return;
218 SwapListItem( item, item - 1 );
219 ReNumberList();
220 }
221
222 // ↓
223 void MainFrame::OnBtnDown( wxCommandEvent& WXUNUSED(event) )
224 {
225 long item = -1;
226 item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
227 if ( item == -1 || item == m_listView->GetItemCount() - 1 ) return;
228 SwapListItem( item, item + 1 );
229 ReNumberList();
230 }
231
232 // −
233 void MainFrame::OnBtnDel( wxCommandEvent& WXUNUSED(event) )
234 {
235 long item = -1;
236 for ( ;; ) {
237 item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
238 if ( item == -1 ) break;
239 m_listView->DeleteItem( item );
240 ReNumberList();
241 }
242 }
243
244 // +
245 void MainFrame::OnBtnAdd( wxCommandEvent& WXUNUSED(event) )
246 {
247 long item = -1;
248 for ( ;; ) {
249 item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
250 if ( item == -1 ) break;
251 m_listView->InsertItem( item + 1, wxEmptyString );
252 ReNumberList();
253 }
254 /*
255 AddDialog adlg( this, wxID_ANY, wxT("a"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE );
256 if ( adlg.ShowModal() == wxID_OK ) {
257 }
258 */
259 }
260
261 void MainFrame::OnTimer( wxTimerEvent& WXUNUSED(event) )
262 {
263 if ( wxTheClipboard->Open() ) {
264
265 wxTextDataObject data;
266 wxTheClipboard->GetData( data );
267 wxString s = data.GetText();
268 if ( !m_last.IsSameAs( s ) ) { // the clipboard was changed by user
269 wxTheClipboard->Close();
270 m_textCtrlShow->SetValue( s );
271 for ( int r = 0; r < m_listView->GetItemCount(); r++ )
272 m_listView->SetItemState( r, 0, wxLIST_STATE_SELECTED );
273 m_counter = 5;
274 m_current = 0;
275 m_last = s;
276 m_timer.StartOnce( (int)m_counter * 1000 );
277 return;
278 }
279
280 wxString text = m_listView->GetItemText( m_current, 1 );
281 wxTheClipboard->SetData( new wxTextDataObject( text ) );
282 wxTheClipboard->Close();
283 m_textCtrlShow->SetValue( text );
284 m_listView->SetItemState( ( m_current + m_listView->GetItemCount() ) % m_listView->GetItemCount() - 1, 0, wxLIST_STATE_SELECTED );
285 m_listView->SetItemState( m_current, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
286 m_last = text;
287
288 wxString time = m_listView->GetItemText( m_current, 2 );
289 time.ToLong( &m_counter, 10 );
290 m_timer.StartOnce( (int)m_counter * 1000 );
291
292 if ( m_current == m_listView->GetItemCount() - 1 ) m_current = 0;
293 else m_current++;
294 }
295 }
296
297 void MainFrame::OnToggle( wxCommandEvent& WXUNUSED(event) )
298 {
299 if ( m_toggleBtn->GetValue() ) {
300 m_toggleBtn->SetLabel( wxT("OFF") );
301 EnableButtons( false );
302 if ( m_listView->GetItemCount() > 0 ) {
303 m_timer.StartOnce( 1000 );
304 }
305 }
306 else {
307 m_timer.Stop();
308 m_toggleBtn->SetLabel( wxT("ON") );
309 EnableButtons( true );
310 }
311 }
312
313 void MainFrame::OnBtnExit( wxCommandEvent& WXUNUSED(event) )
314 {
315 Close();
316 }
317
318 // Functions
319 void MainFrame::EnableButtons( bool enable )
320 {
321 if ( enable ) {
322 m_buttonUp->Enable( true );
323 m_buttonDown->Enable( true );
324 m_buttonDel->Enable( true );
325 m_buttonAdd->Enable( true );
326 }
327 else {
328 m_buttonUp->Enable( false );
329 m_buttonDown->Enable( false );
330 m_buttonDel->Enable( false );
331 m_buttonAdd->Enable( false );
332 }
333 }
334
335 void MainFrame::ReNumberList()
336 {
337 for ( int i = 0; i < m_listView->GetItemCount(); i++ ) {
338 m_listView->SetItem( i, 0, wxString::Format( wxT("%d"), i + 1 ) );
339 }
340 }
341
342 void MainFrame::SwapListItem( long item1, long item2 )
343 {
344 for ( int c = 0; c < m_listView->GetColumnCount(); c++ ) {
345 wxString buf = m_listView->GetItemText( item1, c );
346 m_listView->SetItem( item1, c, m_listView->GetItemText( item2, c ) );
347 m_listView->SetItem( item2, c, buf );
348 }
349 m_listView->SetItemState( item1, 0, wxLIST_STATE_SELECTED );
350 m_listView->SetItemState( item2, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
351 }
352