PageRenderTime 130ms CodeModel.GetById 77ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

/Rainman2/exception_dialog.cpp

http://modstudio2.googlecode.com/
C++ | 194 lines | 137 code | 33 blank | 24 comment | 8 complexity | f15a5ee2c429fbbb76cc77c4024d8028 MD5 | raw file
  1/*
  2Copyright (c) 2008 Peter "Corsix" Cawley
  3
  4Permission is hereby granted, free of charge, to any person
  5obtaining a copy of this software and associated documentation
  6files (the "Software"), to deal in the Software without
  7restriction, including without limitation the rights to use,
  8copy, modify, merge, publish, distribute, sublicense, and/or sell
  9copies of the Software, and to permit persons to whom the
 10Software is furnished to do so, subject to the following
 11conditions:
 12
 13The above copyright notice and this permission notice shall be
 14included in all copies or substantial portions of the Software.
 15
 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 18OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 21WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 23OTHER DEALINGS IN THE SOFTWARE.
 24*/
 25#include "exception_dialog.h"
 26#ifdef RAINMAN2_USE_WX
 27#include <wx/artprov.h>
 28#include <wx/clipbrd.h>
 29
 30BEGIN_EVENT_TABLE(RainExceptionDialog, wxDialog)
 31  EVT_BUTTON(wxID_OK, RainExceptionDialog::onOK)
 32  EVT_BUTTON(wxID_MORE, RainExceptionDialog::onDetails)
 33  EVT_BUTTON(wxID_SAVE, RainExceptionDialog::onSave)
 34  EVT_LIST_ITEM_SELECTED(wxID_ANY, RainExceptionDialog::onListSelect)
 35END_EVENT_TABLE()
 36
 37RainExceptionDialog::RainExceptionDialog(wxWindow *parent, RainException *pException)
 38  : wxDialog(parent, wxID_ANY, wxT("Error"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
 39  , m_pDetailList(0), m_bShowingDetails(false), m_pDetailsBtn(0), m_pStaticLine(0), m_pSaveBtn(0)
 40{
 41  for(RainException *e = pException; e; e = e->getPrevious())
 42  {
 43    m_aMessages.Add(e->getMessage());
 44    m_aFiles.Add(e->getFile());
 45    m_aLines.Add(e->getLine());
 46  }
 47
 48  wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
 49  wxBoxSizer *sizerButtons = new wxBoxSizer(wxVERTICAL);
 50  wxBoxSizer *sizerAll = new wxBoxSizer(wxHORIZONTAL);
 51
 52  wxButton *btnOk = new wxButton(this, wxID_OK);
 53  sizerButtons->Add(btnOk, 0, wxCENTRE | wxBOTTOM, 5);
 54  m_pDetailsBtn = new wxButton(this, wxID_MORE, wxT("&Details >>"));
 55  sizerButtons->Add(m_pDetailsBtn, 0,wxCENTRE | wxTOP, 4);
 56
 57  wxBitmap bitmap = wxArtProvider::GetBitmap(wxART_ERROR, wxART_MESSAGE_BOX);
 58  sizerAll->Add(new wxStaticBitmap(this, wxID_ANY, bitmap), 0, wxALIGN_CENTRE_VERTICAL);
 59
 60  sizerAll->Add(CreateTextSizer(pException->getMessage()), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, 10);
 61  sizerAll->Add(sizerButtons, 0, wxALIGN_RIGHT | wxLEFT, 10);
 62  sizerTop->Add(sizerAll, 0, wxALL | wxEXPAND, 10);
 63
 64  SetSizer(sizerTop);
 65
 66  wxSize size = sizerTop->Fit(this);
 67  m_maxHeight = size.y;
 68  SetSizeHints(size.x, size.y, m_maxWidth, m_maxHeight);
 69
 70  btnOk->SetFocus();
 71  Centre();
 72}
 73
 74void RainExceptionDialog::_addExtraControls()
 75{
 76  m_pSaveBtn = new wxButton(this, wxID_SAVE, wxT("Copy to Clipboard"));
 77  m_pStaticLine = new wxStaticLine(this, wxID_ANY);
 78
 79  m_pDetailList = new wxListCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER | wxLC_REPORT | wxLC_SINGLE_SEL);
 80
 81  m_pDetailList->InsertColumn(0, wxT("Message"));
 82  m_pDetailList->InsertColumn(1, wxT("File"));
 83  m_pDetailList->InsertColumn(2, wxT("Ln."));
 84
 85  size_t count = m_aMessages.GetCount();
 86  for(size_t n = 0; n < count; ++n)
 87  {
 88    m_pDetailList->InsertItem(n, m_aMessages[n]);
 89    m_pDetailList->SetItem(n, 1, m_aFiles[n]);
 90    m_pDetailList->SetItem(n, 2, wxString::Format(wxT("%li"), m_aLines[n]));
 91  }
 92
 93  m_pDetailList->SetColumnWidth(0, wxLIST_AUTOSIZE);
 94  m_pDetailList->SetColumnWidth(1, wxLIST_AUTOSIZE);
 95  m_pDetailList->SetColumnWidth(2, wxLIST_AUTOSIZE);
 96
 97  int height = GetCharHeight()*(count + 4);
 98  int heightMax = wxGetDisplaySize().y - GetPosition().y - 2*GetMinHeight();
 99
100  heightMax *= 9;
101  heightMax /= 10;
102
103  m_pDetailList->SetSize(wxDefaultCoord, wxMin(height, heightMax));
104}
105
106
107void RainExceptionDialog::onListSelect(wxListEvent& e)
108{
109  m_pDetailList->SetItemState(e.GetIndex(), 0, wxLIST_STATE_SELECTED);
110}
111
112void RainExceptionDialog::onOK(wxCommandEvent& WXUNUSED(e))
113{
114  EndModal(wxID_OK);
115}
116
117void RainExceptionDialog::onSave(wxCommandEvent& WXUNUSED(e))
118{
119  if(wxTheClipboard->Open())
120  {
121    wxString sMessage;
122
123    size_t count = m_aMessages.GetCount();
124    for(size_t n = 0; n < count; ++n)
125    {
126      sMessage << m_aFiles[n] << wxT(" line ") << m_aLines[n] << wxT(": ") << m_aMessages[n] << wxT("\n");
127    }
128
129    wxTheClipboard->SetData( new wxTextDataObject(sMessage) );
130    wxTheClipboard->Close();
131    wxMessageBox(wxT("Error copied to clipboard"), wxT("Copy to Clipboard"), wxOK | wxICON_INFORMATION, this);
132  }
133  else
134  {
135    wxMessageBox(wxT("Cannot access clipboard"), wxT("Copy to Clipboard"), wxOK | wxICON_ERROR, this);
136  }
137}
138
139void RainExceptionDialog::onDetails(wxCommandEvent& WXUNUSED(e))
140{
141  wxSizer *sizer = GetSizer();
142
143  if(m_bShowingDetails)
144  {
145    m_pDetailsBtn->SetLabel(wxT("&Details >>"));
146    sizer->Detach(m_pDetailList);
147    sizer->Detach(m_pStaticLine);
148    sizer->Detach(m_pSaveBtn);
149  }
150  else
151  {
152    m_pDetailsBtn->SetLabel(wxT("<< &Details"));
153    if(!m_pDetailList)
154    {
155      _addExtraControls();
156    }
157
158    sizer->Add(m_pStaticLine, 0, wxEXPAND | (wxALL & ~wxTOP), 10);
159    sizer->Add(m_pDetailList, 1, wxEXPAND | (wxALL & ~wxTOP), 10);
160    sizer->Add(m_pSaveBtn, 0, wxALIGN_RIGHT | (wxALL & ~wxTOP), 10);
161  }
162
163  m_bShowingDetails = !m_bShowingDetails;
164
165  m_minHeight = m_maxHeight = -1;
166
167  wxSize sizeTotal = GetSize(), sizeClient = GetClientSize();
168  wxSize size = sizer->GetMinSize();
169  size.x += sizeTotal.x - sizeClient.x;
170  size.y += sizeTotal.y - sizeClient.y;
171
172  if(m_bShowingDetails)
173  {
174    size.x *= 2;
175    size.y += m_aMessages.GetCount() * 10;
176  }
177  else
178  {
179    m_maxHeight = size.y;
180  }
181
182  SetSizeHints(size.x, size.y, m_maxWidth, m_maxHeight);
183  SetSize(size.x, size.y);
184}
185
186void RainExceptionDialog::show(RainException* pE, bool bDeleteException)
187{
188  RainExceptionDialog oDialog(wxGetActiveWindow(), pE);
189  oDialog.ShowModal();
190  if(bDeleteException)
191    delete pE;
192}
193
194#endif