Mercurial > mercurial > hgweb_qlipbrd.cgi
diff src/mainframe.cpp @ 0:cb3403ca39b1
First release.
author | pyon@macmini |
---|---|
date | Sun, 30 Aug 2015 21:53:19 +0900 |
parents | |
children | e4aa0e7a07ad |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mainframe.cpp Sun Aug 30 21:53:19 2015 +0900 @@ -0,0 +1,352 @@ +// Filename: mainframe.cpp +// Last Change: 2015-08-30 Sun 21:49:16. +// +#include <wx/filedlg.h> +#include <wx/textfile.h> +#include <wx/clipbrd.h> +#include <wx/utils.h> +#include <wx/msgdlg.h> + +#include "mainframe.h" +#include "adddialog.h" + +// resources +#if !defined(__WXMSW__) && !defined(__WXPM__) + #include "sample.xpm" +#endif + +MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) + : wxFrame( parent, id, title, pos, size, style ), m_timer( this, ID_TIMER ) +{ + this->SetSizeHints( wxDefaultSize, wxDefaultSize ); + this->SetBackgroundColour( wxColour( wxT("WHEAT") ) ); + SetIcon( wxICON( sample ) ); + + // menu bar + m_menubar = new wxMenuBar( 0 ); + m_menuFile = new wxMenu(); + wxMenuItem* m_menuItemAbout = new wxMenuItem( m_menuFile, ID_MNABOUT, wxString( wxT("About") ), wxEmptyString, wxITEM_NORMAL ); + m_menuFile->Append( m_menuItemAbout ); + + wxMenuItem* m_menuItemExit = new wxMenuItem( m_menuFile, ID_MNEXIT, wxString( wxT("Exit") ), wxEmptyString, wxITEM_NORMAL ); + m_menuFile->Append( m_menuItemExit ); + + m_menubar->Append( m_menuFile, wxT("File") ); + + m_menuPlugin = new wxMenu(); + wxMenuItem* m_menuItemLoadPgin = new wxMenuItem( m_menuPlugin, ID_MNLDPGIN, wxString( wxT("Load") ), wxEmptyString, wxITEM_NORMAL ); + m_menuPlugin->Append( m_menuItemLoadPgin ); + + wxMenuItem* m_menuItemSavePgin = new wxMenuItem( m_menuPlugin, ID_MNSVPGIN, wxString( wxT("Save as") ), wxEmptyString, wxITEM_NORMAL ); + m_menuPlugin->Append( m_menuItemSavePgin ); + + m_menubar->Append( m_menuPlugin, wxT("Plugin") ); + + this->SetMenuBar( m_menubar ); + + // controls + wxBoxSizer* bSizerTop = new wxBoxSizer( wxVERTICAL ); + + // + wxBoxSizer* bSizerShow = new wxBoxSizer( wxHORIZONTAL ); + + m_textCtrlShow = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY ); + bSizerShow->Add( m_textCtrlShow, 1, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); + + m_toggleBtn = new wxToggleButton( this, ID_TGL, wxT("ON"), wxDefaultPosition, wxSize( 60, -1 ), 0 ); + bSizerShow->Add( m_toggleBtn, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); + + bSizerTop->Add( bSizerShow, 0, wxEXPAND, 5 ); + + // + wxBoxSizer* bSizerList = new wxBoxSizer( wxHORIZONTAL ); + + m_listView = new wxListView( this, ID_LIST, wxDefaultPosition, wxDefaultSize, wxLC_REPORT|wxLC_SINGLE_SEL ); + bSizerList->Add( m_listView, 1, wxALL|wxEXPAND, 5 ); + wxListItem itemCol; + itemCol.SetText( wxT("No") ); + m_listView->InsertColumn( 0, itemCol ); + m_listView->SetColumnWidth( 0, 40 ); + itemCol.SetText( wxT("text") ); + m_listView->InsertColumn( 1, itemCol ); + m_listView->SetColumnWidth( 1, 80 ); + itemCol.SetText( wxT("time") ); + m_listView->InsertColumn( 2, itemCol ); + m_listView->SetColumnWidth( 2, 40 ); + itemCol.SetText( wxT("type") ); + m_listView->InsertColumn( 3, itemCol ); + m_listView->SetColumnWidth( 3, 40 ); + itemCol.SetText( wxT("desc") ); + m_listView->InsertColumn( 4, itemCol ); + m_listView->SetColumnWidth( 4, 80 ); + //m_listView->EnableAlternateRowColours( true ); + + wxBoxSizer* bSizerBtn = new wxBoxSizer( wxVERTICAL ); + + m_buttonUp = new wxButton( this, ID_BTNUP, wxT("↑"), wxDefaultPosition, wxSize( 40, -1 ), 0 ); + bSizerBtn->Add( m_buttonUp, 0, wxALL, 5 ); + + m_buttonDown = new wxButton( this, ID_BTNDOWN, wxT("↓"), wxDefaultPosition, wxSize( 40, -1 ), 0 ); + bSizerBtn->Add( m_buttonDown, 0, wxALL, 5 ); + + m_buttonDel = new wxButton( this, ID_BTNDEL, wxT("-"), wxDefaultPosition, wxSize( 40, -1 ), 0 ); + bSizerBtn->Add( m_buttonDel, 0, wxALL, 5 ); + + m_buttonAdd = new wxButton( this, ID_BTNADD, wxT("+"), wxDefaultPosition, wxSize( 40, -1 ), 0 ); + bSizerBtn->Add( m_buttonAdd, 0, wxALL, 5 ); + + bSizerBtn->Add( 0, 0, 1, wxEXPAND, 5 ); + + m_buttonExit = new wxButton( this, ID_EXIT, wxT("Exit"), wxDefaultPosition, wxSize( 60, -1 ), 0 ); + bSizerBtn->Add( m_buttonExit, 0, wxALL, 5 ); + + bSizerList->Add( bSizerBtn, 0, wxEXPAND, 5 ); + bSizerTop->Add( bSizerList, 1, wxEXPAND, 5 ); + + this->SetSizer( bSizerTop ); + this->Layout(); + + this->Centre( wxBOTH ); +} + +MainFrame::~MainFrame() +{ +} + +// Event Table +BEGIN_EVENT_TABLE( MainFrame, wxFrame ) + EVT_MENU( ID_MNABOUT, MainFrame::OnMNAbout ) + EVT_MENU( ID_MNEXIT, MainFrame::OnMNExit ) + EVT_MENU( ID_MNLDPGIN, MainFrame::OnMNLoad ) + EVT_MENU( ID_MNSVPGIN, MainFrame::OnMNSaveAs ) + EVT_LIST_ITEM_ACTIVATED( ID_LIST, MainFrame::OnDClickItem ) + EVT_LIST_ITEM_SELECTED( ID_LIST, MainFrame::OnSelectItem ) + EVT_TIMER( ID_TIMER, MainFrame::OnTimer ) + EVT_BUTTON( ID_BTNUP, MainFrame::OnBtnUp ) + EVT_BUTTON( ID_BTNDOWN, MainFrame::OnBtnDown ) + EVT_BUTTON( ID_BTNDEL, MainFrame::OnBtnDel ) + EVT_BUTTON( ID_BTNADD, MainFrame::OnBtnAdd ) + EVT_BUTTON( ID_EXIT, MainFrame::OnBtnExit ) + EVT_TOGGLEBUTTON( ID_TGL, MainFrame::OnToggle ) + /* + EVT_IDLE( MainFrame::OnIdle ) + EVT_CLOSE( MainFrame::OnClose ) + */ +END_EVENT_TABLE() + +/* Event Handlers & Functions */ +// Event Handlers +void MainFrame::OnMNAbout( wxCommandEvent& WXUNUSED(event) ) +{ + wxInfoMessageBox( this ); +} + +void MainFrame::OnMNExit( wxCommandEvent& WXUNUSED(event) ) +{ + Close(); +} + +void MainFrame::OnMNLoad( wxCommandEvent& WXUNUSED(event) ) +{ + wxString plugin_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("plugin"); + wxFileDialog fd( this, wxT("Select Plug-in file"), plugin_dir, wxEmptyString, wxT("Plug-in files (*.qbrd)|*.qbrd"), wxFD_OPEN|wxFD_FILE_MUST_EXIST ); + if ( fd.ShowModal() == wxID_CANCEL ) return; // the user changed idea... + + wxTextFile file; + file.Open( fd.GetPath() ); + m_listView->DeleteAllItems(); + for ( int i = 0, n = 0; i < file.GetLineCount(); i++ ) { + if ( file[i].StartsWith( wxT("#") ) ) + continue; + wxArrayString s = wxSplit( file[i], ',', '\\' ); + + m_listView->InsertItem( n, wxString::Format( wxT("%d"), n + 1 ) ); + m_listView->SetItem( n, 1, s[0] ); + m_listView->SetItem( n, 2, s[1] ); + m_listView->SetItem( n, 3, s[2] ); + n++; + } + file.Close(); + + m_current = 0; + m_last = m_listView->GetItemText( m_current, 1 ); + m_listView->SetItemState( m_current, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); +} + +void MainFrame::OnMNSaveAs( wxCommandEvent& WXUNUSED(event) ) +{ + wxString plugin_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("plugin"); + wxFileDialog fd( this, wxT("Save Plug-in file"), plugin_dir , wxEmptyString, wxT("Plug-in files (*.qbrd)|*.qbrd"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT ); + if ( fd.ShowModal() == wxID_CANCEL ) return; // the user changed idea... + + wxTextFile file( fd.GetPath() ); + if ( file.Exists() ) { + file.Open(); + file.Clear(); + } + else { + file.Create(); + } + for ( int r = 0; r < m_listView->GetItemCount(); r++ ) { + wxArrayString s; + for ( int c = 0; c < m_listView->GetColumnCount(); c++ ) { + s.Add( m_listView->GetItemText( r, c ) ); + } + file.AddLine( wxJoin( s, ',', '\\' ) ); + } + file.Write(); + file.Close(); +} + +void MainFrame::OnDClickItem( wxListEvent& event ) +{ + long i = event.GetIndex(); +} + +void MainFrame::OnSelectItem( wxListEvent& event ) +{ + long i = event.GetIndex(); + m_current = i; +} + +// ↑ +void MainFrame::OnBtnUp( wxCommandEvent& WXUNUSED(event) ) +{ + long item = -1; + item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); + if ( item == -1 || item == 0 ) return; + SwapListItem( item, item - 1 ); + ReNumberList(); +} + +// ↓ +void MainFrame::OnBtnDown( wxCommandEvent& WXUNUSED(event) ) +{ + long item = -1; + item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); + if ( item == -1 || item == m_listView->GetItemCount() - 1 ) return; + SwapListItem( item, item + 1 ); + ReNumberList(); +} + +// − +void MainFrame::OnBtnDel( wxCommandEvent& WXUNUSED(event) ) +{ + long item = -1; + for ( ;; ) { + item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); + if ( item == -1 ) break; + m_listView->DeleteItem( item ); + ReNumberList(); + } +} + +// + +void MainFrame::OnBtnAdd( wxCommandEvent& WXUNUSED(event) ) +{ + long item = -1; + for ( ;; ) { + item = m_listView->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); + if ( item == -1 ) break; + m_listView->InsertItem( item + 1, wxEmptyString ); + ReNumberList(); + } + /* + AddDialog adlg( this, wxID_ANY, wxT("a"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE ); + if ( adlg.ShowModal() == wxID_OK ) { + } + */ +} + +void MainFrame::OnTimer( wxTimerEvent& WXUNUSED(event) ) +{ + if ( wxTheClipboard->Open() ) { + + wxTextDataObject data; + wxTheClipboard->GetData( data ); + wxString s = data.GetText(); + if ( !m_last.IsSameAs( s ) ) { // the clipboard was changed by user + wxTheClipboard->Close(); + m_textCtrlShow->SetValue( s ); + for ( int r = 0; r < m_listView->GetItemCount(); r++ ) + m_listView->SetItemState( r, 0, wxLIST_STATE_SELECTED ); + m_counter = 5; + m_current = 0; + m_last = s; + m_timer.StartOnce( (int)m_counter * 1000 ); + return; + } + + wxString text = m_listView->GetItemText( m_current, 1 ); + wxTheClipboard->SetData( new wxTextDataObject( text ) ); + wxTheClipboard->Close(); + m_textCtrlShow->SetValue( text ); + m_listView->SetItemState( ( m_current + m_listView->GetItemCount() ) % m_listView->GetItemCount() - 1, 0, wxLIST_STATE_SELECTED ); + m_listView->SetItemState( m_current, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); + m_last = text; + + wxString time = m_listView->GetItemText( m_current, 2 ); + time.ToLong( &m_counter, 10 ); + m_timer.StartOnce( (int)m_counter * 1000 ); + + if ( m_current == m_listView->GetItemCount() - 1 ) m_current = 0; + else m_current++; + } +} + +void MainFrame::OnToggle( wxCommandEvent& WXUNUSED(event) ) +{ + if ( m_toggleBtn->GetValue() ) { + m_toggleBtn->SetLabel( wxT("OFF") ); + EnableButtons( false ); + if ( m_listView->GetItemCount() > 0 ) { + m_timer.StartOnce( 1000 ); + } + } + else { + m_timer.Stop(); + m_toggleBtn->SetLabel( wxT("ON") ); + EnableButtons( true ); + } +} + +void MainFrame::OnBtnExit( wxCommandEvent& WXUNUSED(event) ) +{ + Close(); +} + +// Functions +void MainFrame::EnableButtons( bool enable ) +{ + if ( enable ) { + m_buttonUp->Enable( true ); + m_buttonDown->Enable( true ); + m_buttonDel->Enable( true ); + m_buttonAdd->Enable( true ); + } + else { + m_buttonUp->Enable( false ); + m_buttonDown->Enable( false ); + m_buttonDel->Enable( false ); + m_buttonAdd->Enable( false ); + } +} + +void MainFrame::ReNumberList() +{ + for ( int i = 0; i < m_listView->GetItemCount(); i++ ) { + m_listView->SetItem( i, 0, wxString::Format( wxT("%d"), i + 1 ) ); + } +} + +void MainFrame::SwapListItem( long item1, long item2 ) +{ + for ( int c = 0; c < m_listView->GetColumnCount(); c++ ) { + wxString buf = m_listView->GetItemText( item1, c ); + m_listView->SetItem( item1, c, m_listView->GetItemText( item2, c ) ); + m_listView->SetItem( item2, c, buf ); + } + m_listView->SetItemState( item1, 0, wxLIST_STATE_SELECTED ); + m_listView->SetItemState( item2, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); +} +