Mercurial > mercurial > hgweb_qlipbrd.cgi
view src/mainframe.cpp @ 1:e4aa0e7a07ad
Second release.
author | pyon@macmini |
---|---|
date | Sun, 13 Sep 2015 14:56:42 +0900 |
parents | cb3403ca39b1 |
children | fd4361d2cbbd |
line wrap: on
line source
// Filename: mainframe.cpp // Last Change: 2015-09-13 Sun 14:43:27. // #include <wx/filedlg.h> #include <wx/textfile.h> #include <wx/clipbrd.h> #include <wx/utils.h> #include <wx/msgdlg.h> #include "wx/numdlg.h" #include "mainframe.h" #include "adddialog.h" // resources #if !defined(__WXMSW__) && !defined(__WXPM__) #include "sample.xpm" #endif #define COL_NO 0 #define COL_TEXT 1 #define COL_OW 2 #define COL_MLT 3 #define COL_MAL 4 #define COL_DESC 5 #define COL_ID 6 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 ), m_counter( 0 ), m_period( 5 ), m_bottom( true ) { this->SetSizeHints( wxSize( 500, 450 ), wxDefaultSize ); this->SetBackgroundColour( wxColour( wxT("WHEAT") ) ); SetIcon( wxICON( sample ) ); // menu bar m_menubar = new wxMenuBar( 0 ); m_menuFile = new wxMenu(); wxMenuItem* m_menuItemAppdir = new wxMenuItem( m_menuFile, ID_MNAPPDIR, wxString( wxT("Open App Forlder") ), wxEmptyString, wxITEM_NORMAL ); m_menuFile->Append( m_menuItemAppdir ); wxMenuItem* m_menuItemInterval = new wxMenuItem( m_menuFile, ID_MNINTERVAL, wxString( wxT("Set Interval") ), wxEmptyString, wxITEM_NORMAL ); m_menuFile->Append( m_menuItemInterval ); 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_textCtrlRemain = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 40, -1 ), wxTE_READONLY|wxTE_CENTER ); bSizerShow->Add( m_textCtrlRemain, 0, wxRIGHT|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* bSizerMain = new wxBoxSizer( wxHORIZONTAL ); // list wxBoxSizer* bSizerList = new wxBoxSizer( wxVERTICAL ); m_staticTextCycle = new wxStaticText( this, wxID_ANY, wxT("Cycle list"), wxDefaultPosition, wxDefaultSize, 0 ); bSizerList->Add( m_staticTextCycle, 0, wxTOP|wxLEFT|wxALIGN_BOTTOM, 5 ); m_lisViewCycle = new wxListView( this, ID_LISTVIEW, wxDefaultPosition, wxSize( -1, 100 ), wxLC_REPORT|wxLC_SINGLE_SEL ); wxListItem itemCol; itemCol.SetText( wxT("No") ); m_lisViewCycle->InsertColumn( COL_NO, itemCol ); m_lisViewCycle->SetColumnWidth( COL_NO, 20 ); itemCol.SetText( wxT("text") ); m_lisViewCycle->InsertColumn( COL_TEXT, itemCol ); m_lisViewCycle->SetColumnWidth( COL_TEXT, 80 ); itemCol.SetText( wxT("o/w") ); m_lisViewCycle->InsertColumn( COL_OW, itemCol ); m_lisViewCycle->SetColumnWidth( COL_OW, 40 ); itemCol.SetText( wxT("live") ); m_lisViewCycle->InsertColumn( COL_MLT, itemCol ); m_lisViewCycle->SetColumnWidth( COL_MLT, 50 ); itemCol.SetText( wxT("active") ); m_lisViewCycle->InsertColumn( COL_MAL, itemCol ); m_lisViewCycle->SetColumnWidth( COL_MAL, 50 ); itemCol.SetText( wxT("desc") ); m_lisViewCycle->InsertColumn( COL_DESC, itemCol ); m_lisViewCycle->SetColumnWidth( COL_DESC, 80 ); itemCol.SetText( wxT("id") ); m_lisViewCycle->InsertColumn( COL_ID, itemCol ); m_lisViewCycle->SetColumnWidth( COL_ID, 30 ); //m_lisViewCycle->EnableAlternateRowColours( true ); bSizerList->Add( m_lisViewCycle, 1, wxALL|wxEXPAND, 5 ); m_staticTextHist = new wxStaticText( this, wxID_ANY, wxT("User input history"), wxDefaultPosition, wxDefaultSize, 0 ); bSizerList->Add( m_staticTextHist, 0, wxTOP|wxLEFT|wxALIGN_BOTTOM, 5 ); m_listBoxHist = new wxListBox( this, ID_LISTBOX, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE ); bSizerList->Add( m_listBoxHist, 1, wxALL|wxEXPAND, 5 ); bSizerMain->Add( bSizerList, 1, wxEXPAND, 5 ); // button wxBoxSizer* bSizerBtn = new wxBoxSizer( wxVERTICAL ); bSizerBtn->Add( 0, 20, 0, 0, 5 ); 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 ); bSizerMain->Add( bSizerBtn, 0, wxEXPAND, 5 ); bSizerTop->Add( bSizerMain, 1, wxEXPAND, 5 ); this->SetSizer( bSizerTop ); this->Layout(); this->Centre( wxBOTH ); } MainFrame::~MainFrame() { } // Event Table BEGIN_EVENT_TABLE( MainFrame, wxFrame ) EVT_MENU( ID_MNAPPDIR, MainFrame::OnMNAppdir ) EVT_MENU( ID_MNINTERVAL, MainFrame::OnMNInterval ) 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_LISTVIEW, MainFrame::OnLViewDClickItem ) EVT_LIST_ITEM_SELECTED( ID_LISTVIEW, MainFrame::OnLViewSelectItem ) EVT_LISTBOX_DCLICK( ID_LISTBOX, MainFrame::OnLBoxDClickItem ) 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::OnMNAppdir( wxCommandEvent& WXUNUSED(event) ) { wxString app_dir = wxGetCwd(); wxString execmd = wxT("explorer ") + app_dir; wxExecute( execmd ); } void MainFrame::OnMNInterval( wxCommandEvent& WXUNUSED(event) ) { long t = wxGetNumberFromUser( wxT("text changed by user is preserved specified period."), wxT("Input 1-60[sec]."), wxT("Setting"), 5, 1, 60, this, wxDefaultPosition ); if ( t != -1 ) // canceled m_period = (int)t; } 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_lisViewCycle->DeleteAllItems(); QH.clear(); for ( int i = 0, n = 0; i < file.GetLineCount(); i++ ) { if ( file[i].StartsWith( wxT("#") ) ) continue; wxArrayString s = wxSplit( file[i], ',', '\\' ); m_lisViewCycle->InsertItem( n, wxString::Format( wxT("%d"), n + 1 ) ); m_lisViewCycle->SetItem( n, COL_TEXT, s[0] ); m_lisViewCycle->SetItem( n, COL_OW, s[1] ); m_lisViewCycle->SetItem( n, COL_MLT, s[2] ); m_lisViewCycle->SetItem( n, COL_MAL, s[3] ); m_lisViewCycle->SetItem( n, COL_DESC, s[4] ); m_lisViewCycle->SetItem( n, COL_ID, s[5] ); n++; } file.Close(); m_current = 0; m_lisViewCycle->Select( m_current, true ); } 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_lisViewCycle->GetItemCount(); r++ ) { wxArrayString s; for ( int c = 0; c < m_lisViewCycle->GetColumnCount(); c++ ) { s.Add( m_lisViewCycle->GetItemText( r, c ) ); } file.AddLine( wxJoin( s, ',', '\\' ) ); } file.Write(); file.Close(); } void MainFrame::OnLViewDClickItem( wxListEvent& event ) { wxString s = event.GetText(); } void MainFrame::OnLViewSelectItem( wxListEvent& event ) { long i = event.GetIndex(); m_current = (int)i; } void MainFrame::OnLBoxDClickItem( wxCommandEvent& event ) { wxString s = event.GetString(); m_counter = m_period; if ( wxTheClipboard->Open() ) { wxTheClipboard->SetData( new wxTextDataObject( s ) ); wxTheClipboard->Close(); m_listBoxHist->Delete( event.GetSelection() ); m_listBoxHist->Insert( s, 0 ); m_textCtrlShow->SetValue( s ); m_textCtrlRemain->SetValue( wxString::Format( wxT("%d"), m_period ) ); } m_listBoxHist->Deselect( event.GetInt() ); m_listBoxHist->SetSelection( 0 ); } // [↑] void MainFrame::OnBtnUp( wxCommandEvent& WXUNUSED(event) ) { long item = m_lisViewCycle->GetFirstSelected(); if ( item == -1 || item == 0 ) return; SwapListItem( item, item - 1 ); ReNumberList(); } // [↓] void MainFrame::OnBtnDown( wxCommandEvent& WXUNUSED(event) ) { long item = m_lisViewCycle->GetFirstSelected(); if ( item == -1 || item == m_lisViewCycle->GetItemCount() - 1 ) return; SwapListItem( item, item + 1 ); ReNumberList(); } // [−] void MainFrame::OnBtnDel( wxCommandEvent& WXUNUSED(event) ) { long item = m_lisViewCycle->GetFirstSelected(); if ( item == -1 ) return; int id = ToInt( m_lisViewCycle->GetItemText( item, COL_ID ) ); QH.erase( id ); m_lisViewCycle->DeleteItem( item ); ReNumberList(); } // [+] void MainFrame::OnBtnAdd( wxCommandEvent& WXUNUSED(event) ) { int r; long item = m_lisViewCycle->GetFirstSelected(); if ( item == -1 ) r = 0; else r = item; AddDialog add_dlg( this, wxID_ANY, wxT("Add Item"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE ); if ( add_dlg.ShowModal() == wxID_OK ) { int id = CreateID(); QlipData* q = new QlipData; q->id = id; q->text = add_dlg.GetText(); q->overwrite = add_dlg.GetOverwrite(); q->max_active_time = add_dlg.GetMaxActiveTime(); q->max_live_time = q->overwrite ? add_dlg.GetMaxLiveTime() : 0; q->desc = add_dlg.GetDesc(); q->active_time = 0; q->live_time = 0; QH[id] = q; m_lisViewCycle->InsertItem( r, wxEmptyString ); m_lisViewCycle->SetItem( r, COL_TEXT, q->text ); m_lisViewCycle->SetItem( r, COL_OW, wxString::Format( wxT("%s"), q->overwrite ? wxT("true") : wxT("false") ) ); m_lisViewCycle->SetItem( r, COL_MAL, wxString::Format( wxT("-- / %d"), q->max_active_time ) ); m_lisViewCycle->SetItem( r, COL_MLT, wxString::Format( wxT("-- / %d"), q->max_live_time ) ); m_lisViewCycle->SetItem( r, COL_DESC, q->desc ); m_lisViewCycle->SetItem( r, COL_ID, wxString::Format( wxT("%d"), id ) ); if ( q->overwrite ) m_lisViewCycle->SetItemBackgroundColour( r, wxColour( wxT("MEDIUM GOLDENROD") ) ); else m_lisViewCycle->SetItemBackgroundColour( r, wxColour( wxT("WHITE") ) ); } ReNumberList(); } void MainFrame::OnTimer( wxTimerEvent& WXUNUSED(event) ) { wxString clipboard; if ( wxTheClipboard->Open() ) { //wxMessageBox( clipboard ); wxTextDataObject data; wxTheClipboard->GetData( data ); clipboard = data.GetText(); wxTheClipboard->Close(); m_textCtrlShow->SetValue( clipboard ); m_textCtrlRemain->SetValue( wxT("∞") ); UpdateHistory( clipboard ); } else { return; } long list_num = m_lisViewCycle->GetItemCount(); if ( list_num == 0 ) return; if ( m_lisViewCycle->GetSelectedItemCount() == 0 ) { m_lisViewCycle->Select( 0, true ); m_current = 0; } AddStackTime(); UpdateView(); int r = GetEmptyStack(); if ( r != -1 && m_bottom ) { m_timer.Stop(); int id = ToInt( m_lisViewCycle->GetItemText( r, COL_ID ) ); QH[id]->text = clipboard; m_lisViewCycle->SetItem( r, COL_TEXT, clipboard ); wxSleep( 2 ); m_timer.Start( -1 ); m_current = 0; m_lisViewCycle->Select( 0, true ); return; } // Main Cycle if ( list_num == 1 ) { if ( m_counter == m_period ) { m_counter = 0; if ( wxTheClipboard->Open() ) { wxString s = m_lisViewCycle->GetItemText( 0, COL_TEXT ); wxTheClipboard->SetData( new wxTextDataObject( s ) ); wxTheClipboard->Close(); m_textCtrlShow->SetValue( s ); m_textCtrlRemain->SetValue( wxT("∞") ); } } else { wxString t = wxString::Format( wxT("%d"), m_period - m_counter ); m_textCtrlShow->SetValue( clipboard ); m_textCtrlRemain->SetValue( t ); m_counter++; } m_lisViewCycle->Select( 0, true ); m_current = 0; m_bottom = true; } else { long item = m_lisViewCycle->GetFirstSelected(); m_current = (int)item; int id = ToInt( m_lisViewCycle->GetItemText( item, COL_ID ) ); QlipData* q = QH[id]; if ( q->overwrite && !q->IsAlive() ) return; if ( q->IsArrived() ) { m_lisViewCycle->Select( m_current, false ); if ( ++m_current == list_num ) m_bottom = true; else m_bottom = false; m_current = m_current % list_num; m_lisViewCycle->Select( m_current, true ); if ( wxTheClipboard->Open() ) { int id = ToInt( m_lisViewCycle->GetItemText( m_current, COL_ID ) ); QlipData* qn = QH[id]; wxTheClipboard->SetData( new wxTextDataObject( qn->text ) ); wxTheClipboard->Close(); m_textCtrlShow->SetValue( qn->text ); m_textCtrlRemain->SetValue( wxString::Format( wxT("%d"), qn->max_active_time ) ); } } else { int t = q->max_active_time - q->active_time; m_textCtrlRemain->SetValue( wxString::Format( wxT("%d"), t ) ); } } } void MainFrame::OnToggle( wxCommandEvent& WXUNUSED(event) ) { if ( m_toggleBtn->GetValue() ) { m_toggleBtn->SetLabel( wxT("OFF") ); EnableButtons( false ); m_timer.Start( 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 r = 0; r < m_lisViewCycle->GetItemCount(); r++ ) { m_lisViewCycle->SetItem( r, COL_NO, wxString::Format( wxT("%d"), r + 1 ) ); wxString s = m_lisViewCycle->GetItemText( r, COL_OW ); if ( s.IsSameAs( wxT("true") ) ) m_lisViewCycle->SetItemBackgroundColour( r, wxColour( wxT("MEDIUM GOLDENROD") ) ); else m_lisViewCycle->SetItemBackgroundColour( r, wxColour( wxT("WHITE") ) ); } } void MainFrame::SwapListItem( long item1, long item2 ) { for ( int c = 0; c < m_lisViewCycle->GetColumnCount(); c++ ) { wxString buf = m_lisViewCycle->GetItemText( item1, c ); m_lisViewCycle->SetItem( item1, c, m_lisViewCycle->GetItemText( item2, c ) ); m_lisViewCycle->SetItem( item2, c, buf ); } m_lisViewCycle->Select( item1, false ); m_lisViewCycle->Select( item2, true ); } int MainFrame::CreateID() { long max_id = 1; if ( m_lisViewCycle->GetItemCount() == 0 ) return max_id; wxArrayString id; for ( int r = 0; r < m_lisViewCycle->GetItemCount(); r++ ) { wxString buf = m_lisViewCycle->GetItemText( r, COL_ID ); id.Add( buf ); } id.Sort(); wxString max = id.Last(); max.ToLong( &max_id, 10 ); return ++max_id; } void MainFrame::AddStackTime() { QlipDataHash::iterator it; for ( it = QH.begin(); it != QH.end(); it++ ) { int id = it->first; QlipData* q = it->second; if ( q->overwrite ) { if ( ++q->live_time == q->max_live_time ) q->Kill(); } QH[id] = q; } } void MainFrame::UpdateView() { for ( int r = 0; r < m_lisViewCycle->GetItemCount(); r++ ) { int id = ToInt( m_lisViewCycle->GetItemText( r, COL_ID ) ); m_lisViewCycle->SetItem( r, COL_TEXT, QH[id]->text ); wxString at = wxString::Format( wxT("%d / %d"), QH[id]->active_time, QH[id]->max_active_time ); m_lisViewCycle->SetItem( r, COL_MAL, at ); wxString lt = wxT("-- / --"); if ( QH[id]->overwrite ) lt = wxString::Format( wxT("%d / %d"), QH[id]->live_time, QH[id]->max_live_time ); m_lisViewCycle->SetItem( r, COL_MLT, lt ); } } void MainFrame::UpdateHistory( wxString s ) { if ( s.IsEmpty() ) return; int row = m_listBoxHist->FindString( s, true ); if ( row != wxNOT_FOUND ) m_listBoxHist->Delete( row ); m_listBoxHist->Insert( s, 0 ); } int MainFrame::GetEmptyStack() { int row = -1; for ( int r = 0; r < m_lisViewCycle->GetItemCount(); r++ ) { int id = ToInt( m_lisViewCycle->GetItemText( r, COL_ID ) ); if ( QH[(int)id]->text.IsEmpty() ) row = r; } return row; } int MainFrame::ToInt( wxString s ) { long n = 0; s.ToLong( &n, 10 ); return n; }