Main Page | Class List | File List | Class Members | Related Pages

dlggoto.cpp

00001 /***************************************************************************
00002                            dlggoto.cpp
00003                            -------------------
00004     begin                : 2004-06-05 20:08
00005     copyright            : (C) 2004-2005 by Michael Menne
00006     email                : menne@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  This program is free software; you can redistribute it and/or
00011  modify it under the terms of the GNU General Public License
00012  as published by the Free Software Foundation; either version 2
00013  of the License, or (at your option) any later version.
00014 
00015  This program is distributed in the hope that it will be useful,
00016  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  GNU General Public License for more details.
00019 
00020  You should have received a copy of the GNU General Public License
00021  along with this program; if not, write to the Free Software
00022  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023  ***************************************************************************/
00024 
00025 #include "dlggoto.h"
00026 #include "app.h"
00027 #include "map.h"
00028 #include "profilemanager.h"
00029 #include "town.h"
00030 
00031 #include <wx/notebook.h>
00032 #include <wx/panel.h>
00033 #include <wx/valtext.h> // wxValidator
00034 
00035 namespace WDS
00036 {
00037 
00038 BEGIN_EVENT_TABLE(CDlgGoTo, wxDialog)
00039     EVT_BUTTON( wxID_OK, CDlgGoTo::OnGoTo )
00040     EVT_BUTTON( wxID_CANCEL, CDlgGoTo::OnClose )
00041     EVT_BUTTON( IDC_REMOVE_BOOKMARKS, CDlgGoTo::OnDeleteBookmarks )
00042     EVT_BUTTON( IDC_REMOVE_RECENTS, CDlgGoTo::OnDeleteRecents )
00043     EVT_TEXT  ( IDC_EDT_X, CDlgGoTo::OnPaste )
00044     EVT_LIST_ITEM_ACTIVATED( IDC_LIST_BOOKMARKS, CDlgGoTo::OnActivateBookmarkItem )
00045     EVT_LIST_ITEM_ACTIVATED( IDC_LIST_RECENTS, CDlgGoTo::OnActivateRecentItem )
00046     EVT_LIST_ITEM_SELECTED( IDC_LIST_BOOKMARKS, CDlgGoTo::OnSelectedBookmarkItem )
00047     EVT_LIST_ITEM_SELECTED( IDC_LIST_RECENTS, CDlgGoTo::OnSelectedRecentItem )
00048 END_EVENT_TABLE()
00049 
00050 /*----------------------------------------------------------------------------/
00051     Constructor / Destructor
00052 -----------------------------------------------------------------------------*/
00053 
00054 void CDlgGoTo::OnPaste( wxCommandEvent& event )
00055 {
00056     if( !m_pEdtPosX )
00057         return;
00058 
00059     // Testen ob jemand x:y eingeben hat
00060     wxString strTmp = m_pEdtPosX->GetValue();
00061     if( strTmp.Find( ':' ) != -1 )
00062     {
00063         strTmp.Trim( true  );
00064         strTmp.Trim( false );
00065 
00066         // Den String aufspalten und die Teile in die Texstfelder eintragen
00067         m_pEdtPosX->SetValue( strTmp.BeforeFirst( ':' ) );
00068         m_pEdtPosY->SetValue( strTmp.AfterFirst ( ':' ) );
00069 
00070         m_pEdtPosX->SetSelection( -1, -1 );
00071         m_pEdtPosY->SetFocus();
00072     }
00073 }
00074 
00075 CDlgGoTo::CDlgGoTo( wxWindow *pParent )
00076     : wxDialog( pParent, -1, wxString("GoTo - WDSMap"), wxDefaultPosition,
00077     wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
00078     m_pEdtPosX(0L), m_pEdtPosY(0L)
00079 {
00080     wxBoxSizer* sTop       = new wxBoxSizer( wxVERTICAL );
00081     wxNotebook* sTabs      = new wxNotebook( this, -1 );
00082     wxBoxSizer* sBookmarks = new wxBoxSizer( wxVERTICAL );
00083     wxBoxSizer* sRecents   = new wxBoxSizer( wxVERTICAL );
00084     wxBoxSizer* sGoToClose = new wxBoxSizer( wxHORIZONTAL );
00085     wxPanel* pBookmarks    = new wxPanel( sTabs );
00086     wxPanel* pRecents      = new wxPanel( sTabs );
00087 
00088     //BEGIN fill tab Bookmarks
00089     m_pLstBookmarks = new wxListCtrl( pBookmarks, IDC_LIST_BOOKMARKS, wxDefaultPosition, wxDefaultSize,
00090                                         wxLC_REPORT | wxLC_VRULES | wxLC_SINGLE_SEL );
00091     m_pLstBookmarks->InsertColumn( 0, "Position", wxLIST_FORMAT_LEFT, 80 );
00092     m_pLstBookmarks->InsertColumn( 1, "Stadt - [Gilde] Besitzer", wxLIST_FORMAT_LEFT, 200 );
00093     m_pLstBookmarks->InsertColumn( 3, "Beschreibung", wxLIST_FORMAT_LEFT, 150 );
00094 
00095     wxButton* pBtnRemoveBookmarks = new wxButton( pBookmarks, IDC_REMOVE_BOOKMARKS, "Auswahl löschen" );
00096 
00097     sBookmarks->Add( m_pLstBookmarks, 1, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10 );
00098     sBookmarks->Add( pBtnRemoveBookmarks, 0, wxALIGN_RIGHT | wxALL, 10 );
00099     //END fill tab Bookmarks
00100 
00101     //BEGIN fill tab Recents
00102     m_pLstRecents = new wxListCtrl( pRecents, IDC_LIST_RECENTS, wxDefaultPosition, wxDefaultSize,
00103                                     wxLC_REPORT | wxLC_VRULES | wxLC_SINGLE_SEL );
00104     m_pLstRecents->InsertColumn( 0, "Position", wxLIST_FORMAT_LEFT, 80 );
00105     m_pLstRecents->InsertColumn( 1, "Stadt - [Gilde] Besitzer", wxLIST_FORMAT_LEFT, 250 );
00106 
00107     wxButton* pBtnRemoveRecents = new wxButton( pRecents, IDC_REMOVE_RECENTS, "Auswahl löschen" );
00108 
00109     sRecents->Add( m_pLstRecents, 1, wxEXPAND | wxALL, 10 );
00110     sRecents->Add( pBtnRemoveRecents, 0, wxALIGN_RIGHT | wxLEFT | wxRIGHT | wxBOTTOM, 10 );
00111     //END fill tab Recents
00112 
00113     //BEGIN Position, X, Y, GoTo, Dummy, Close
00114     wxStaticText* pTxtPosition  = new wxStaticText( this, -1, "Position (x:y)" );
00115     m_pEdtPosX = new wxTextCtrl( this, IDC_EDT_X, "1", wxDefaultPosition, wxSize( 40, -1 ) );
00116     m_pEdtPosY = new wxTextCtrl( this, IDC_EDT_Y, "1", wxDefaultPosition, wxSize( 40, -1 ) );
00117 
00118     wxButton* pBtnGoTo  = new wxButton( this, wxID_OK, "GO!", wxDefaultPosition, wxSize( 40, -1 ) );
00119     wxStaticText* pTxtDummy  = new wxStaticText( this, -1, "" );
00120     wxButton* pBtnClose = new wxButton( this, wxID_CANCEL, "Schließen" );
00121 
00122     sGoToClose->Add( pTxtPosition, 0, wxALIGN_CENTER_VERTICAL | wxTOP | wxRIGHT, 5 );
00123     sGoToClose->Add( m_pEdtPosX, 0, wxALIGN_CENTER_VERTICAL | wxTOP | wxRIGHT, 5 );
00124     sGoToClose->Add( m_pEdtPosY, 0, wxALIGN_CENTER_VERTICAL | wxTOP | wxRIGHT, 5 );
00125     sGoToClose->Add( pBtnGoTo  , 0, wxALIGN_CENTER_VERTICAL | wxTOP | wxRIGHT, 5 );
00126     sGoToClose->Add( pTxtDummy , 1, wxEXPAND, 0 );
00127     sGoToClose->Add( pBtnClose , 0, wxALIGN_CENTER_VERTICAL | wxTOP, 5 );
00128     //END Position, X, Y, GoTo, Dummy, Close
00129 
00130     sTop->Add( sTabs, 1, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
00131     sTop->Add( sGoToClose, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10);
00132 
00133     sTabs->AddPage( pBookmarks, wxString("Bookmarks") );
00134     sTabs->AddPage( pRecents, wxString("Recents") );
00135 
00136     pBookmarks->SetSizer( sBookmarks );
00137     pRecents  ->SetSizer( sRecents );
00138 
00139     SetAutoLayout( true );
00140     SetSizer( sTop );
00141 
00142     sTop->SetSizeHints( this );
00143     SetClientSize( 500, 300 );
00144 
00145     m_pEdtPosX->SetFocus();
00146     pBtnGoTo->SetDefault( );
00147 
00148     // Variablen intialisieren
00149     m_x = 0;
00150     m_y = 0;
00151 
00152     FillListBoxes( );
00153 }
00154 
00155 CDlgGoTo::~CDlgGoTo()
00156 {
00157 }
00158 
00159 /*----------------------------------------------------------------------------/
00160     Member Functions
00161 -----------------------------------------------------------------------------*/
00162 
00167 void CDlgGoTo::FillListBoxes( )
00168 {
00169     m_pLstBookmarks->DeleteAllItems();
00170     m_pLstRecents  ->DeleteAllItems();
00171 
00172     CProfile* profile = CProfileManager::GetCurrentProfile();
00173     CMap* pMap = wxGetApp().GetMap();
00174 
00175     if ( profile && pMap ) {
00176 
00177         GoToArray bookmarks = profile->GetBookmarks();
00178         GoToArray recents   = profile->GetRecentGoTos();
00179 
00180         int index;
00181         GoToArray::iterator it;
00182         wxListItem item;
00183 
00184         // fill bookmarks
00185         for ( index = 0, it = bookmarks.begin();
00186               it != bookmarks.end();
00187               it++, index++ )
00188         {
00189             CTown* pTown = pMap->GetTownAt( it->x, it->y );
00190             wxString infos = "-";
00191             if ( pTown ) {
00192                 infos = pTown->GetName() + " - " + pTown->GetGuild() + " " + pTown->GetOwner();
00193             }
00194 
00195             m_pLstBookmarks->InsertItem ( index, wxString::Format("%d:%d", it->x, it->y) );
00196             m_pLstBookmarks->SetItemData( index, index );
00197 
00198             m_pLstBookmarks->SetItem( index, 1, infos );
00199             m_pLstBookmarks->SetItem( index, 2, it->comment );
00200 
00201             if ( index & 1 ) {
00202                 item.SetBackgroundColour( wxColor( 238, 246, 255) );    // Hellblau
00203             } else {
00204                 item.SetBackgroundColour( wxColor( 255, 255, 255) );    // Weiss
00205             }
00206 
00207             item.m_itemId = index;
00208             m_pLstBookmarks->SetItem( item );
00209         }
00210 
00211         // fill recents
00212         for ( index = 0, it = recents.begin();
00213               it != recents.end(); it++, index++ )
00214         {
00215             CTown* pTown = pMap->GetTownAt( it->x, it->y );
00216             wxString infos = "-";
00217             if ( pTown ) {
00218                 infos = pTown->GetName() + " - " + pTown->GetGuild() + " " + pTown->GetOwner();
00219             }
00220 
00221             m_pLstRecents->InsertItem ( 0, wxString::Format("%d:%d", it->x, it->y) );
00222             m_pLstRecents->SetItemData( 0, index );
00223 
00224             m_pLstRecents->SetItem( 0, 1, infos );
00225 
00226             if ( index & 1 ) {
00227                 item.SetBackgroundColour( wxColor( 238, 246, 255) );    // Hellblau
00228             } else {
00229                 item.SetBackgroundColour( wxColor( 255, 255, 255) );    // Weiss
00230             }
00231 
00232             item.m_itemId = 0;
00233             m_pLstRecents->SetItem( item );
00234         }
00235     }
00236 }
00237 
00238 void CDlgGoTo::OnDeleteBookmarks( wxCommandEvent &event )
00239 {
00240     long result = m_pLstBookmarks->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
00241 
00242     if( result == -1 ) {
00243         return;
00244     }
00245 
00246     int nIndex = m_pLstBookmarks->GetItemData( result );
00247 
00248     CProfile* profile = CProfileManager::GetCurrentProfile( );
00249     GoToArray& rgoto = profile->GetBookmarks( );
00250 
00251     GoToArray::iterator it = rgoto.begin() + nIndex;
00252 
00253     if ( it != rgoto.end() ) {
00254         rgoto.erase( it );
00255     }
00256 
00257     FillListBoxes( );
00258 }
00259 
00260 void CDlgGoTo::OnDeleteRecents( wxCommandEvent &event )
00261 {
00262     long result = m_pLstRecents->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
00263 
00264     if( result == -1 ) {
00265         return;
00266     }
00267 
00268     int nIndex = m_pLstRecents->GetItemData( result );
00269 
00270     CProfile* profile = CProfileManager::GetCurrentProfile( );
00271     GoToArray& rgoto = profile->GetRecentGoTos( );
00272 
00273     GoToArray::iterator it = rgoto.begin() + nIndex;
00274 
00275     if ( it != rgoto.end() ) {
00276         rgoto.erase( it );
00277     }
00278 
00279     FillListBoxes( );
00280 }
00281 
00282 void CDlgGoTo::OnSelectedBookmarkItem( wxCommandEvent &event )
00283 {
00284     long result = m_pLstBookmarks->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
00285 
00286     if( result == -1 ) {
00287         return;
00288     }
00289 
00290     int nIndex = m_pLstBookmarks->GetItemData( result );
00291 
00292     CProfile* profile = CProfileManager::GetCurrentProfile( );
00293     GoToArray& rgoto = profile->GetBookmarks( );
00294 
00295     GoToArray::iterator it = rgoto.begin() + nIndex;
00296 
00297     if ( it != rgoto.end() ) {
00298         m_pEdtPosX->SetValue( wxString::Format("%d", it->x) );
00299         m_pEdtPosY->SetValue( wxString::Format("%d", it->y) );
00300     }
00301 }
00302 
00303 void CDlgGoTo::OnSelectedRecentItem( wxCommandEvent &event )
00304 {
00305     long result = m_pLstRecents->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
00306 
00307     if( result == -1 ) {
00308         return;
00309     }
00310 
00311     int nIndex = m_pLstRecents->GetItemData( result );
00312 
00313     CProfile* profile = CProfileManager::GetCurrentProfile( );
00314     GoToArray& rgoto = profile->GetRecentGoTos( );
00315 
00316     GoToArray::iterator it = rgoto.begin() + nIndex;
00317 
00318     if ( it != rgoto.end() ) {
00319         m_pEdtPosX->SetValue( wxString::Format("%d", it->x) );
00320         m_pEdtPosY->SetValue( wxString::Format("%d", it->y) );
00321     }
00322 }
00323 
00324 void CDlgGoTo::OnActivateBookmarkItem( wxListEvent& event )
00325 {
00326     CProfile* profile = CProfileManager::GetCurrentProfile();
00327     if ( !profile )
00328         return;
00329 
00330     GoToArray& rgoto = profile->GetBookmarks();
00331 
00332     int nIndex = event.GetData();
00333     if ( nIndex < 0 || nIndex >= int(rgoto.size()) )
00334         return;
00335 
00336     CGoToEntry entry = rgoto[nIndex];
00337 
00338     m_x = entry.x;
00339     m_y = entry.y;
00340 
00341     EndModal( wxID_OK );
00342 }
00343 
00344 void CDlgGoTo::OnActivateRecentItem( wxListEvent& event )
00345 {
00346     CProfile* profile = CProfileManager::GetCurrentProfile();
00347     if ( !profile )
00348         return;
00349 
00350     GoToArray& rgoto = profile->GetRecentGoTos();
00351 
00352     int nIndex = event.GetData();
00353     if ( nIndex < 0 || nIndex >= int(rgoto.size()) )
00354         return;
00355 
00356     CGoToEntry entry = rgoto[nIndex];
00357 
00358     m_x = entry.x;
00359     m_y = entry.y;
00360 
00361     EndModal( wxID_OK );
00362 }
00363 
00364 void CDlgGoTo::OnGoTo ( wxCommandEvent &event )
00365 {
00366     long x, y;
00367 
00368     // Testen ob jemand x:y eingeben hat
00369     /*wxString strTmp = m_pEdtPosX->GetValue();
00370     if( strTmp.Find( ':' ) != -1 )
00371     {
00372     strTmp.Trim( true  );
00373     strTmp.Trim( false );
00374 
00375         // Den String aufspalten und die Teile in die Texstfelder eintragen
00376     m_pEdtPosX->SetValue( strTmp.BeforeFirst( ':' ) );
00377     m_pEdtPosY->SetValue( strTmp.AfterFirst ( ':' ) );
00378 }*/
00379 
00380     // Text aus Editbox holen und versuchen in eine Zahl umzuwandeln
00381     wxString strX = m_pEdtPosX->GetValue();
00382     if( !strX.ToLong( &x ) )
00383     {
00384         wxMessageBox( wxString::Format("'%s' ist keine Zahl!", strX.c_str() )); // Keine Zahl!
00385         return;
00386     }
00387 
00388     // Gleiches fuer die Y Koordinate
00389     wxString strY = m_pEdtPosY->GetValue();
00390     if( !strY.ToLong( &y ) )
00391     {
00392         wxMessageBox( wxString::Format("'%s' ist keine Zahl!", strY.c_str() )); // Keine Zahl!
00393         return;
00394     }
00395 
00396     // Zuweisen
00397     m_x = x;
00398     m_y = y;
00399 
00400     event.Skip( );
00401 }
00402 
00403 void CDlgGoTo::OnClose( wxCommandEvent &event )
00404 {
00405     event.Skip( );
00406 }
00407 
00408 }
00409 
00410 // kate: space-indent off; tab-width 4; replace-tabs off;

Generated on Sun Jan 16 18:20:26 2005 for WDSMap by  doxygen 1.3.9.1