/Rainman2/exception_dialog.cpp

http://modstudio2.googlecode.com/ · C++ · 194 lines · 137 code · 33 blank · 24 comment · 8 complexity · f15a5ee2c429fbbb76cc77c4024d8028 MD5 · raw file

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