| 1 | /* |
|---|
| 2 | * Copyright (c) 2007-2008 Mat Booth |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU Library General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program. If not, write to: |
|---|
| 16 | * The Free Software Foundation, Inc., |
|---|
| 17 | * 51 Franklin Street, Fifth Floor |
|---|
| 18 | * Boston, MA 02110-1301, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include <wx/confbase.h> |
|---|
| 22 | #include "M3CodePanel.h" |
|---|
| 23 | |
|---|
| 24 | BEGIN_EVENT_TABLE(M3CodePanel, wxPanel) |
|---|
| 25 | //controls |
|---|
| 26 | EVT_TEXT(ID_CODE, M3CodePanel::OnCodeChanged) |
|---|
| 27 | EVT_TEXT(ID_NORMALISED, M3CodePanel::OnCodeChanged) |
|---|
| 28 | END_EVENT_TABLE() |
|---|
| 29 | |
|---|
| 30 | M3CodePanel::M3CodePanel(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) |
|---|
| 31 | : wxPanel(parent, id, pos, size, style) |
|---|
| 32 | { |
|---|
| 33 | //create controls |
|---|
| 34 | wxPanel *topPanel = new wxPanel(this, -1); |
|---|
| 35 | wxPanel *bottomPanel = new wxPanel(this, -1); |
|---|
| 36 | |
|---|
| 37 | wxStaticText *codeLabel = new wxStaticText(topPanel, -1, wxT("Malbolge Code:")); |
|---|
| 38 | wxStaticText *normalisedLabel = new wxStaticText(bottomPanel, -1, wxT("Normalised Malbolge Code:")); |
|---|
| 39 | m_Code = new M3CodeCtrl(topPanel, ID_CODE); |
|---|
| 40 | m_Normalised = new M3CodeCtrl(bottomPanel, ID_NORMALISED); |
|---|
| 41 | |
|---|
| 42 | //let code controls know about each other |
|---|
| 43 | m_Code->SetOtherCodeControl(m_Normalised); |
|---|
| 44 | m_Normalised->SetOtherCodeControl(m_Code); |
|---|
| 45 | |
|---|
| 46 | //top layout |
|---|
| 47 | wxFlexGridSizer *topSizer = new wxFlexGridSizer(2, 1, 0, 0); |
|---|
| 48 | topSizer->AddGrowableCol(0); |
|---|
| 49 | topSizer->AddGrowableRow(1); |
|---|
| 50 | topSizer->Add(codeLabel, 0, wxALL, 5); |
|---|
| 51 | topSizer->Add(m_Code, 0, wxEXPAND, 0); |
|---|
| 52 | topPanel->SetSizer(topSizer); |
|---|
| 53 | topPanel->SetAutoLayout(true); |
|---|
| 54 | |
|---|
| 55 | //bottom layout |
|---|
| 56 | wxFlexGridSizer *bottomSizer = new wxFlexGridSizer(2, 1, 0, 0); |
|---|
| 57 | bottomSizer->AddGrowableCol(0); |
|---|
| 58 | bottomSizer->AddGrowableRow(1); |
|---|
| 59 | bottomSizer->Add(normalisedLabel, 0, wxALL, 5); |
|---|
| 60 | bottomSizer->Add(m_Normalised, 0, wxEXPAND, 0); |
|---|
| 61 | bottomPanel->SetSizer(bottomSizer); |
|---|
| 62 | bottomPanel->SetAutoLayout(true); |
|---|
| 63 | |
|---|
| 64 | //main layout |
|---|
| 65 | wxFlexGridSizer *mainSizer = new wxFlexGridSizer(2, 1, 0, 0); |
|---|
| 66 | mainSizer->AddGrowableCol(0); |
|---|
| 67 | mainSizer->AddGrowableRow(0); |
|---|
| 68 | mainSizer->AddGrowableRow(1); |
|---|
| 69 | mainSizer->Add(topPanel, 0, wxEXPAND, 0); |
|---|
| 70 | mainSizer->Add(bottomPanel, 0, wxEXPAND, 0); |
|---|
| 71 | SetSizer(mainSizer); |
|---|
| 72 | SetAutoLayout(true); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | M3CodePanel::~M3CodePanel() |
|---|
| 76 | { |
|---|
| 77 | //save before the window is destroyed |
|---|
| 78 | SaveConf(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | bool M3CodePanel::OpenMalbolgeFile(const wxString& filename) |
|---|
| 82 | { |
|---|
| 83 | return m_Code->LoadFile(filename); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | bool M3CodePanel::SaveMalbolgeFile(const wxString& filename) |
|---|
| 87 | { |
|---|
| 88 | return m_Code->SaveFile(filename); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void M3CodePanel::CloseMalbolgeFile() |
|---|
| 92 | { |
|---|
| 93 | m_Code->Clear(); |
|---|
| 94 | m_Normalised->Clear(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void M3CodePanel::LoadConf() |
|---|
| 98 | { |
|---|
| 99 | //save config path |
|---|
| 100 | wxConfigBase* conf = wxConfigBase::Get(); |
|---|
| 101 | wxString oldPath = conf->GetPath(); |
|---|
| 102 | |
|---|
| 103 | //load |
|---|
| 104 | |
|---|
| 105 | //restore config path |
|---|
| 106 | conf->SetPath(oldPath); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void M3CodePanel::SaveConf() |
|---|
| 110 | { |
|---|
| 111 | //save config path |
|---|
| 112 | wxConfigBase* conf = wxConfigBase::Get(); |
|---|
| 113 | wxString oldPath = conf->GetPath(); |
|---|
| 114 | |
|---|
| 115 | //save |
|---|
| 116 | |
|---|
| 117 | //restore config path |
|---|
| 118 | conf->SetPath(oldPath); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void M3CodePanel::OnCodeChanged(wxCommandEvent& e) |
|---|
| 122 | { |
|---|
| 123 | switch(e.GetId()) |
|---|
| 124 | { |
|---|
| 125 | case ID_CODE: |
|---|
| 126 | m_Normalised->Normalise(); |
|---|
| 127 | break; |
|---|
| 128 | case ID_NORMALISED: |
|---|
| 129 | m_Code->Abnormalise(); |
|---|
| 130 | break; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | //let parent objects know about code change |
|---|
| 134 | e.Skip(); |
|---|
| 135 | } |
|---|