/***************************************************************************
 * Name:      PlottWareControlMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    Fandly Gergo (fandlygergo@gmail.hu)
 * Created:   2017-11-25
 * Copyright: Copyright (C) 2017 Fandly Gergo (systemtest.tk)
 * License: 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **************************************************************************/

#include "PlottWareControlMain.h"
#include <wx/msgdlg.h>
#include <string>
#include "version.h"
#include "SettingDialog.h"
#include "imageViewer.h"
#include "GcodeEditorDialog.h"

//(*InternalHeaders(PlottWareControlFrame)
#include <wx/bitmap.h>
#include <wx/icon.h>
#include <wx/settings.h>
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(PlottWareControlFrame)
const long PlottWareControlFrame::ID_BUTTON1 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT1 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON2 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON3 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON4 = wxNewId();
const long PlottWareControlFrame::ID_PANEL2 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT2 = wxNewId();
const long PlottWareControlFrame::ID_LISTBOX1 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON5 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON6 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON9 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON23 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON24 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT25 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT26 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT27 = wxNewId();
const long PlottWareControlFrame::ID_TEXTCTRL2 = wxNewId();
const long PlottWareControlFrame::ID_SPINCTRL1 = wxNewId();
const long PlottWareControlFrame::ID_SPINCTRL2 = wxNewId();
const long PlottWareControlFrame::ID_SPINCTRL3 = wxNewId();
const long PlottWareControlFrame::ID_SPINCTRL4 = wxNewId();
const long PlottWareControlFrame::ID_SPINCTRL5 = wxNewId();
const long PlottWareControlFrame::ID_SPINCTRL6 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT4 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT5 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT3 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT7 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT9 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT8 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT10 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT6 = wxNewId();
const long PlottWareControlFrame::ID_PANEL3 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT12 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT11 = wxNewId();
const long PlottWareControlFrame::ID_RADIOBUTTON1 = wxNewId();
const long PlottWareControlFrame::ID_RADIOBUTTON2 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT13 = wxNewId();
const long PlottWareControlFrame::ID_SLIDER1 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT14 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT15 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT16 = wxNewId();
const long PlottWareControlFrame::ID_CHOICE1 = wxNewId();
const long PlottWareControlFrame::ID_CHOICE2 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON25 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON8 = wxNewId();
const long PlottWareControlFrame::ID_PANEL4 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT17 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON10 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON12 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON13 = wxNewId();
const long PlottWareControlFrame::ID_PANEL5 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT18 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT19 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT20 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT21 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON7 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON16 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON17 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON18 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON19 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON20 = wxNewId();
const long PlottWareControlFrame::ID_CHOICE3 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT22 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON21 = wxNewId();
const long PlottWareControlFrame::ID_SPINCTRL7 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT24 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT23 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT28 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT29 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT30 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT32 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT33 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT31 = wxNewId();
const long PlottWareControlFrame::ID_TEXTCTRL1 = wxNewId();
const long PlottWareControlFrame::ID_LISTBOX2 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT34 = wxNewId();
const long PlottWareControlFrame::ID_PANEL1 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON11 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON14 = wxNewId();
const long PlottWareControlFrame::ID_BUTTON15 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT35 = wxNewId();
const long PlottWareControlFrame::ID_GAUGE1 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT36 = wxNewId();
const long PlottWareControlFrame::ID_STATICTEXT37 = wxNewId();
const long PlottWareControlFrame::ID_PANEL6 = wxNewId();
const long PlottWareControlFrame::ID_NOTEBOOK2 = wxNewId();
const long PlottWareControlFrame::MenuSettingItem = wxNewId();
const long PlottWareControlFrame::MenuQuitItem = wxNewId();
const long PlottWareControlFrame::idMenuAbout = wxNewId();
const long PlottWareControlFrame::ID_STATUSBAR1 = wxNewId();
const long PlottWareControlFrame::ID_MESSAGEDIALOG1 = wxNewId();
const long PlottWareControlFrame::ID_TIMER1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(PlottWareControlFrame,wxFrame)
    //(*EventTable(PlottWareControlFrame)
    //*)
END_EVENT_TABLE()

PlottWareControlFrame::PlottWareControlFrame(wxWindow* parent,wxWindowID id)
{
    settings=new Settings();
    project=NULL;

    //(*Initialize(PlottWareControlFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, _("PlottWareControl"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    SetClientSize(wxSize(473,543));
    SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    {
    	wxIcon FrameIcon;
    	FrameIcon.CopyFromBitmap(wxBitmap(wxImage(_T(".\\res\\icon.bmp"))));
    	SetIcon(FrameIcon);
    }
    MenuChanger = new wxNotebook(this, ID_NOTEBOOK2, wxPoint(680,72), wxSize(475,536), 0, _T("ID_NOTEBOOK2"));
    MenuChangerProjectPage = new wxPanel(MenuChanger, ID_PANEL2, wxDefaultPosition, wxSize(467,421), wxTAB_TRAVERSAL, _T("ID_PANEL2"));
    MenuChangerProjectPage->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    ProjNewButton = new wxButton(MenuChangerProjectPage, ID_BUTTON1, _("New"), wxPoint(16,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    ProjNewButton->SetToolTip(_("Create new project"));
    StaticText1 = new wxStaticText(MenuChangerProjectPage, ID_STATICTEXT1, _("Project"), wxPoint(16,16), wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    wxFont StaticText1Font(14,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    StaticText1->SetFont(StaticText1Font);
    ProjOpenButton = new wxButton(MenuChangerProjectPage, ID_BUTTON2, _("Open"), wxPoint(104,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
    ProjOpenButton->SetToolTip(_("Open an existing project"));
    ProjSaveButton = new wxButton(MenuChangerProjectPage, ID_BUTTON3, _("Save"), wxPoint(192,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
    ProjSaveButton->Disable();
    ProjSaveButton->SetToolTip(_("Save current project"));
    ProjCloseButton = new wxButton(MenuChangerProjectPage, ID_BUTTON4, _("Close"), wxPoint(280,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON4"));
    ProjCloseButton->Disable();
    ProjCloseButton->SetToolTip(_("Close current project"));
    MenuChangerImagesPage = new wxPanel(MenuChanger, ID_PANEL3, wxDefaultPosition, wxSize(467,479), wxTAB_TRAVERSAL, _T("ID_PANEL3"));
    MenuChangerImagesPage->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    StaticText2 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT2, _("Images"), wxPoint(16,16), wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    wxFont StaticText2Font(14,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    StaticText2->SetFont(StaticText2Font);
    ImagesImageListBox = new wxListBox(MenuChangerImagesPage, ID_LISTBOX1, wxPoint(24,56), wxSize(120,168), 0, 0, 0, wxDefaultValidator, _T("ID_LISTBOX1"));
    ImagesAddButton = new wxButton(MenuChangerImagesPage, ID_BUTTON5, _("Add image"), wxPoint(160,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON5"));
    ImagesAddButton->Disable();
    ImagesAddButton->SetToolTip(_("Load new image"));
    ImagesDeleteButton = new wxButton(MenuChangerImagesPage, ID_BUTTON6, _("Delete"), wxPoint(160,88), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON6"));
    ImagesDeleteButton->Disable();
    ImagesDeleteButton->SetToolTip(_("Delete selected image"));
    ImagesPreviewButton = new wxButton(MenuChangerImagesPage, ID_BUTTON9, _("Combine"), wxPoint(16,464), wxSize(432,23), 0, wxDefaultValidator, _T("ID_BUTTON9"));
    ImagesPreviewButton->Disable();
    ImagesPreviewButton->SetToolTip(_("Preview images on canvas"));
    ImagesImageListMoveUpButton = new wxButton(MenuChangerImagesPage, ID_BUTTON23, _("/\\"), wxPoint(160,168), wxSize(24,23), 0, wxDefaultValidator, _T("ID_BUTTON23"));
    ImagesImageListMoveUpButton->Disable();
    ImagesImageListMoveDownButton = new wxButton(MenuChangerImagesPage, ID_BUTTON24, _("\\/"), wxPoint(160,200), wxSize(24,23), 0, wxDefaultValidator, _T("ID_BUTTON24"));
    ImagesImageListMoveDownButton->Disable();
    StaticText24 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT25, _("Size:"), wxPoint(24,248), wxDefaultSize, 0, _T("ID_STATICTEXT25"));
    ImagesSizeLabel = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT26, _("0x0"), wxPoint(88,248), wxSize(120,13), 0, _T("ID_STATICTEXT26"));
    StaticText25 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT27, _("Scale:"), wxPoint(24,272), wxDefaultSize, 0, _T("ID_STATICTEXT27"));
    ImagesScaleText = new wxTextCtrl(MenuChangerImagesPage, ID_TEXTCTRL2, _("0"), wxPoint(88,264), wxSize(120,21), 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    ImagesScaleText->Disable();
    ImagesPositionXSpinner = new wxSpinCtrl(MenuChangerImagesPage, ID_SPINCTRL1, _T("0"), wxPoint(88,288), wxDefaultSize, 0, 0, 999999999, 0, _T("ID_SPINCTRL1"));
    ImagesPositionXSpinner->SetValue(_T("0"));
    ImagesPositionXSpinner->Disable();
    ImagesPositionXSpinner->SetToolTip(_("X coordinate of image"));
    ImagesPositionYSpinner = new wxSpinCtrl(MenuChangerImagesPage, ID_SPINCTRL2, _T("0"), wxPoint(88,312), wxDefaultSize, 0, 0, 999999999, 0, _T("ID_SPINCTRL2"));
    ImagesPositionYSpinner->SetValue(_T("0"));
    ImagesPositionYSpinner->Disable();
    ImagesPositionYSpinner->SetToolTip(_("X coordinate of image"));
    ImagesCropX1Spinner = new wxSpinCtrl(MenuChangerImagesPage, ID_SPINCTRL3, _T("0"), wxPoint(88,336), wxDefaultSize, 0, 0, 999999999, 0, _T("ID_SPINCTRL3"));
    ImagesCropX1Spinner->SetValue(_T("0"));
    ImagesCropX1Spinner->Disable();
    ImagesCropX1Spinner->SetToolTip(_("X coordinate of top-left crop point"));
    ImagesCropY1Spinner = new wxSpinCtrl(MenuChangerImagesPage, ID_SPINCTRL4, _T("0"), wxPoint(88,360), wxDefaultSize, 0, 0, 999999999, 0, _T("ID_SPINCTRL4"));
    ImagesCropY1Spinner->SetValue(_T("0"));
    ImagesCropY1Spinner->Disable();
    ImagesCropY1Spinner->SetToolTip(_("Y coordinate of top-left crop point"));
    ImagesCropX2Spinner = new wxSpinCtrl(MenuChangerImagesPage, ID_SPINCTRL5, _T("0"), wxPoint(88,384), wxDefaultSize, 0, 0, 999999999, 0, _T("ID_SPINCTRL5"));
    ImagesCropX2Spinner->SetValue(_T("0"));
    ImagesCropX2Spinner->Disable();
    ImagesCropX2Spinner->SetToolTip(_("X coordinate of bottom-right crop point"));
    ImagesCropY2Spinner = new wxSpinCtrl(MenuChangerImagesPage, ID_SPINCTRL6, _T("0"), wxPoint(88,408), wxDefaultSize, 0, 0, 999999999, 0, _T("ID_SPINCTRL6"));
    ImagesCropY2Spinner->SetValue(_T("0"));
    ImagesCropY2Spinner->Disable();
    ImagesCropY2Spinner->SetToolTip(_("Y coordinate of bottom-right crop point"));
    StaticText4 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT4, _("X"), wxPoint(72,296), wxSize(8,13), 0, _T("ID_STATICTEXT4"));
    StaticText5 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT5, _("Y"), wxPoint(72,320), wxDefaultSize, 0, _T("ID_STATICTEXT5"));
    StaticText3 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT3, _("Position:"), wxPoint(24,296), wxDefaultSize, 0, _T("ID_STATICTEXT3"));
    StaticText7 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT7, _("X1"), wxPoint(72,344), wxDefaultSize, 0, _T("ID_STATICTEXT7"));
    StaticText9 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT9, _("Y1"), wxPoint(72,368), wxDefaultSize, 0, _T("ID_STATICTEXT9"));
    StaticText8 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT8, _("X2"), wxPoint(72,392), wxDefaultSize, 0, _T("ID_STATICTEXT8"));
    StaticText10 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT10, _("Y2"), wxPoint(72,416), wxDefaultSize, 0, _T("ID_STATICTEXT10"));
    StaticText6 = new wxStaticText(MenuChangerImagesPage, ID_STATICTEXT6, _("Crop:"), wxPoint(24,344), wxDefaultSize, 0, _T("ID_STATICTEXT6"));
    MenuChangerProcessingPage = new wxPanel(MenuChanger, ID_PANEL4, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL4"));
    MenuChangerProcessingPage->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    StaticText12 = new wxStaticText(MenuChangerProcessingPage, ID_STATICTEXT12, _("Retrival mode:"), wxPoint(16,56), wxDefaultSize, 0, _T("ID_STATICTEXT12"));
    StaticText11 = new wxStaticText(MenuChangerProcessingPage, ID_STATICTEXT11, _("Processing"), wxPoint(16,16), wxDefaultSize, 0, _T("ID_STATICTEXT11"));
    wxFont StaticText11Font(14,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    StaticText11->SetFont(StaticText11Font);
    ProcessingRetrAllRadio = new wxRadioButton(MenuChangerProcessingPage, ID_RADIOBUTTON1, _("All"), wxPoint(144,56), wxDefaultSize, wxRB_GROUP, wxDefaultValidator, _T("ID_RADIOBUTTON1"));
    ProcessingRetrAllRadio->SetValue(true);
    ProcessingRetrAllRadio->Disable();
    ProcessingRetrAllRadio->SetToolTip(_("Retrive all borders"));
    ProcessingRetrExtRadio = new wxRadioButton(MenuChangerProcessingPage, ID_RADIOBUTTON2, _("External"), wxPoint(192,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON2"));
    ProcessingRetrExtRadio->Disable();
    ProcessingRetrExtRadio->SetToolTip(_("Retrive only the external border"));
    StaticText13 = new wxStaticText(MenuChangerProcessingPage, ID_STATICTEXT13, _("Canny threshold:"), wxPoint(16,88), wxDefaultSize, 0, _T("ID_STATICTEXT13"));
    ProcessingCannyThreshSlider = new wxSlider(MenuChangerProcessingPage, ID_SLIDER1, 100, 0, 255, wxPoint(144,80), wxDefaultSize, 0, wxDefaultValidator, _T("ID_SLIDER1"));
    ProcessingCannyThreshSlider->Disable();
    ProcessingCannyThreshSlider->SetToolTip(_("Threshold for canny edge detection"));
    StaticText14 = new wxStaticText(MenuChangerProcessingPage, ID_STATICTEXT14, _("Coloring:"), wxPoint(16,136), wxDefaultSize, 0, _T("ID_STATICTEXT14"));
    StaticText15 = new wxStaticText(MenuChangerProcessingPage, ID_STATICTEXT15, _("Edge:"), wxPoint(144,136), wxSize(32,13), 0, _T("ID_STATICTEXT15"));
    StaticText16 = new wxStaticText(MenuChangerProcessingPage, ID_STATICTEXT16, _("Color:"), wxPoint(144,160), wxDefaultSize, 0, _T("ID_STATICTEXT16"));
    ProcessingColorLineSelect = new wxChoice(MenuChangerProcessingPage, ID_CHOICE1, wxPoint(184,128), wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
    ProcessingColorLineSelect->Disable();
    ProcessingColorLineSelect->SetToolTip(_("Edge to be colored"));
    ProcessingColorColorSelect = new wxChoice(MenuChangerProcessingPage, ID_CHOICE2, wxPoint(184,152), wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE2"));
    ProcessingColorColorSelect->Append(_("Leave out"));
    ProcessingColorColorSelect->Disable();
    ProcessingColorColorSelect->SetToolTip(_("Used color"));
    ProcessingColorForAllButton = new wxButton(MenuChangerProcessingPage, ID_BUTTON25, _("Set for all"), wxPoint(320,152), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON25"));
    ProcessingColorForAllButton->Disable();
    ProcessingPreviewButton = new wxButton(MenuChangerProcessingPage, ID_BUTTON8, _("Process"), wxPoint(16,208), wxSize(440,23), 0, wxDefaultValidator, _T("ID_BUTTON8"));
    ProcessingPreviewButton->Disable();
    ProcessingPreviewButton->SetToolTip(_("Preview the processed image"));
    MenuChangerGcodePage = new wxPanel(MenuChanger, ID_PANEL5, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL5"));
    MenuChangerGcodePage->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    StaticText17 = new wxStaticText(MenuChangerGcodePage, ID_STATICTEXT17, _("G code"), wxPoint(16,16), wxDefaultSize, 0, _T("ID_STATICTEXT17"));
    wxFont StaticText17Font(14,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    StaticText17->SetFont(StaticText17Font);
    GcodeGenerateButton = new wxButton(MenuChangerGcodePage, ID_BUTTON10, _("Generate"), wxPoint(16,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON10"));
    GcodeGenerateButton->Disable();
    GcodeGenerateButton->SetToolTip(_("Generate G code"));
    GcodeEditButton = new wxButton(MenuChangerGcodePage, ID_BUTTON12, _("Edit"), wxPoint(104,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON12"));
    GcodeEditButton->Disable();
    GcodeEditButton->SetToolTip(_("Edit generate G code"));
    GcodeExportButton = new wxButton(MenuChangerGcodePage, ID_BUTTON13, _("Export"), wxPoint(192,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON13"));
    GcodeExportButton->Disable();
    GcodeExportButton->SetToolTip(_("Export G code to file"));
    MenuChangerPrintingPage = new wxPanel(MenuChanger, ID_PANEL6, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL6"));
    MenuChangerPrintingPage->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    StaticText18 = new wxStaticText(MenuChangerPrintingPage, ID_STATICTEXT18, _("Printing"), wxPoint(16,16), wxDefaultSize, 0, _T("ID_STATICTEXT18"));
    wxFont StaticText18Font(14,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    StaticText18->SetFont(StaticText18Font);
    Panel1 = new wxPanel(MenuChangerPrintingPage, ID_PANEL1, wxPoint(8,120), wxSize(440,280), wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    Panel1->SetBackgroundColour(wxColour(224,224,224));
    StaticText19 = new wxStaticText(Panel1, ID_STATICTEXT19, _("Printer"), wxPoint(8,8), wxDefaultSize, 0, _T("ID_STATICTEXT19"));
    wxFont StaticText19Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    StaticText19->SetFont(StaticText19Font);
    StaticText20 = new wxStaticText(Panel1, ID_STATICTEXT20, _("Status:"), wxPoint(88,8), wxDefaultSize, 0, _T("ID_STATICTEXT20"));
    PrintingPrinterStatusLabel = new wxStaticText(Panel1, ID_STATICTEXT21, _("Offline"), wxPoint(128,8), wxSize(48,13), 0, _T("ID_STATICTEXT21"));
    PrintingPrinterConnectButton = new wxButton(Panel1, ID_BUTTON7, _("Connect"), wxPoint(8,32), wxSize(72,23), 0, wxDefaultValidator, _T("ID_BUTTON7"));
    PrintingPrinterConnectButton->SetToolTip(_("Connect/Disconnect to the printer"));
    PrintingPrinterUpButton = new wxButton(Panel1, ID_BUTTON16, _("/\\"), wxPoint(208,40), wxSize(20,20), 0, wxDefaultValidator, _T("ID_BUTTON16"));
    PrintingPrinterUpButton->Disable();
    PrintingPrinterDownButton = new wxButton(Panel1, ID_BUTTON17, _("\\/"), wxPoint(208,72), wxSize(20,20), 0, wxDefaultValidator, _T("ID_BUTTON17"));
    PrintingPrinterDownButton->Disable();
    PrintingPrinterLeftButton = new wxButton(Panel1, ID_BUTTON18, _("<"), wxPoint(184,56), wxSize(20,20), 0, wxDefaultValidator, _T("ID_BUTTON18"));
    PrintingPrinterLeftButton->Disable();
    wxFont PrintingPrinterLeftButtonFont(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    PrintingPrinterLeftButton->SetFont(PrintingPrinterLeftButtonFont);
    PrintingPrinterRightButton = new wxButton(Panel1, ID_BUTTON19, _(">"), wxPoint(232,56), wxSize(20,20), 0, wxDefaultValidator, _T("ID_BUTTON19"));
    PrintingPrinterRightButton->Disable();
    wxFont PrintingPrinterRightButtonFont(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    PrintingPrinterRightButton->SetFont(PrintingPrinterRightButtonFont);
    PrintingPrinterHomeButton = new wxButton(Panel1, ID_BUTTON20, _("Home"), wxPoint(320,8), wxSize(50,23), 0, wxDefaultValidator, _T("ID_BUTTON20"));
    PrintingPrinterHomeButton->Disable();
    PrintingPrinterHomeButton->SetToolTip(_("Home axises"));
    PrintingPrinterHeadSelect = new wxChoice(Panel1, ID_CHOICE3, wxPoint(320,32), wxSize(112,21), 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE3"));
    PrintingPrinterHeadSelect->SetSelection( PrintingPrinterHeadSelect->Append(_("No tool")) );
    PrintingPrinterHeadSelect->Disable();
    PrintingPrinterHeadSelect->SetToolTip(_("Select used drawing head"));
    StaticText21 = new wxStaticText(Panel1, ID_STATICTEXT22, _("Head:"), wxPoint(280,40), wxDefaultSize, 0, _T("ID_STATICTEXT22"));
    PrintingPrinterPowerButton = new wxButton(Panel1, ID_BUTTON21, _("Power"), wxPoint(384,8), wxSize(50,23), 0, wxDefaultValidator, _T("ID_BUTTON21"));
    PrintingPrinterPowerButton->Disable();
    PrintingPrinterPowerButton->SetToolTip(_("Cycle power supply"));
    PrintingPrinterMoveSpinner = new wxSpinCtrl(Panel1, ID_SPINCTRL7, _T("25"), wxPoint(320,64), wxSize(48,21), 0, 0, 100, 25, _T("ID_SPINCTRL7"));
    PrintingPrinterMoveSpinner->SetValue(_T("25"));
    PrintingPrinterMoveSpinner->Disable();
    PrintingPrinterMoveSpinner->SetToolTip(_("Amount in mm to move when button pressed"));
    StaticText23 = new wxStaticText(Panel1, ID_STATICTEXT24, _("Move:"), wxPoint(280,72), wxDefaultSize, 0, _T("ID_STATICTEXT24"));
    StaticText22 = new wxStaticText(Panel1, ID_STATICTEXT23, _("Power:"), wxPoint(88,32), wxDefaultSize, 0, _T("ID_STATICTEXT23"));
    PrintingPrinterPowerLabel = new wxStaticText(Panel1, ID_STATICTEXT28, wxEmptyString, wxPoint(128,32), wxSize(48,13), 0, _T("ID_STATICTEXT28"));
    StaticText26 = new wxStaticText(Panel1, ID_STATICTEXT29, _("X:"), wxPoint(8,64), wxDefaultSize, 0, _T("ID_STATICTEXT29"));
    StaticText27 = new wxStaticText(Panel1, ID_STATICTEXT30, _("Y:"), wxPoint(8,80), wxDefaultSize, 0, _T("ID_STATICTEXT30"));
    PrintingPrinterXLabel = new wxStaticText(Panel1, ID_STATICTEXT32, wxEmptyString, wxPoint(24,64), wxSize(48,13), 0, _T("ID_STATICTEXT32"));
    PrintingPrinterYLabel = new wxStaticText(Panel1, ID_STATICTEXT33, wxEmptyString, wxPoint(24,80), wxSize(48,13), 0, _T("ID_STATICTEXT33"));
    StaticText28 = new wxStaticText(Panel1, ID_STATICTEXT31, _("Console:"), wxPoint(8,120), wxDefaultSize, 0, _T("ID_STATICTEXT31"));
    PrintingPrinterConsole = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxPoint(64,112), wxSize(360,21), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    PrintingPrinterConsole->Disable();
    PrintingPrinterLog = new wxListBox(Panel1, ID_LISTBOX2, wxPoint(8,160), wxSize(424,104), 0, 0, 0, wxDefaultValidator, _T("ID_LISTBOX2"));
    StaticText29 = new wxStaticText(Panel1, ID_STATICTEXT34, _("Log:"), wxPoint(8,144), wxDefaultSize, 0, _T("ID_STATICTEXT34"));
    PrintingPrintButton = new wxButton(MenuChangerPrintingPage, ID_BUTTON11, _("Print"), wxPoint(16,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON11"));
    PrintingPrintButton->Disable();
    PrintingPrintButton->SetToolTip(_("Start printing"));
    PrintingStopButton = new wxButton(MenuChangerPrintingPage, ID_BUTTON14, _("Stop"), wxPoint(104,56), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON14"));
    PrintingStopButton->Disable();
    PrintingStopButton->SetToolTip(_("Stop printing"));
    PrintingEmergencyButton = new wxButton(MenuChangerPrintingPage, ID_BUTTON15, _("Emergency stop"), wxPoint(320,16), wxSize(128,56), 0, wxDefaultValidator, _T("ID_BUTTON15"));
    PrintingEmergencyButton->Disable();
    PrintingEmergencyButton->SetBackgroundColour(wxColour(248,31,90));
    wxFont PrintingEmergencyButtonFont(10,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    PrintingEmergencyButton->SetFont(PrintingEmergencyButtonFont);
    PrintingEmergencyButton->SetToolTip(_("Reset printer immediately"));
    PrintingStatusLabel = new wxStaticText(MenuChangerPrintingPage, ID_STATICTEXT35, _("Print status:"), wxPoint(24,440), wxDefaultSize, 0, _T("ID_STATICTEXT35"));
    PrintingStatusLabel->Hide();
    PrintingStatusGauge = new wxGauge(MenuChangerPrintingPage, ID_GAUGE1, 1, wxPoint(104,432), wxSize(336,36), 0, wxDefaultValidator, _T("ID_GAUGE1"));
    PrintingStatusGauge->Hide();
    PrintingElapsedTimeLabelLabel = new wxStaticText(MenuChangerPrintingPage, ID_STATICTEXT36, _("Elapsed time:"), wxPoint(24,488), wxSize(72,13), 0, _T("ID_STATICTEXT36"));
    PrintingElapsedTimeLabelLabel->Hide();
    PrintingElapsedTimeLabel = new wxStaticText(MenuChangerPrintingPage, ID_STATICTEXT37, _("00:00:00"), wxPoint(104,488), wxSize(336,13), 0, _T("ID_STATICTEXT37"));
    PrintingElapsedTimeLabel->Hide();
    MenuChanger->AddPage(MenuChangerProjectPage, _("Project"), false);
    MenuChanger->AddPage(MenuChangerImagesPage, _("Images"), false);
    MenuChanger->AddPage(MenuChangerProcessingPage, _("Processing"), false);
    MenuChanger->AddPage(MenuChangerGcodePage, _("G code"), false);
    MenuChanger->AddPage(MenuChangerPrintingPage, _("Printing"), false);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItemSettings = new wxMenuItem(Menu1, MenuSettingItem, _("Settings\tCtrl-s"), _("Change settings"), wxITEM_NORMAL);
    Menu1->Append(MenuItemSettings);
    MenuItem1 = new wxMenuItem(Menu1, MenuQuitItem, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[2] = { 375, 100 };
    int __wxStatusBarStyles_1[2] = { wxSB_NORMAL, wxSB_NORMAL };
    StatusBar1->SetFieldsCount(2,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(2,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    FileDialog1 = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, wxFileSelectorDefaultWildcardStr, wxFD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    MessageDialog1 = new wxMessageDialog(this, wxEmptyString, _("Message"), wxOK|wxCANCEL, wxDefaultPosition);
    PrintingElapsedTimeCheckerTimer.SetOwner(this, ID_TIMER1);

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnProjNewButtonClick);
    Connect(ID_BUTTON4,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnProjCloseButtonClick);
    Connect(ID_LISTBOX1,wxEVT_COMMAND_LISTBOX_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesImageListBoxSelect);
    Connect(ID_BUTTON5,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesAddButtonClick);
    Connect(ID_BUTTON6,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesDeleteButtonClick);
    Connect(ID_BUTTON9,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesPreviewButtonClick);
    Connect(ID_BUTTON23,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesImageListMoveUpButtonClick);
    Connect(ID_BUTTON24,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesImageListMoveDownButtonClick);
    Connect(ID_TEXTCTRL2,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesScaleTextText);
    Connect(ID_SPINCTRL1,wxEVT_COMMAND_SPINCTRL_UPDATED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesPositionXSpinnerChange);
    Connect(ID_SPINCTRL2,wxEVT_COMMAND_SPINCTRL_UPDATED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesPositionYSpinnerChange);
    Connect(ID_SPINCTRL3,wxEVT_COMMAND_SPINCTRL_UPDATED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesCropX1SpinnerChange);
    Connect(ID_SPINCTRL4,wxEVT_COMMAND_SPINCTRL_UPDATED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesCropY1SpinnerChange);
    Connect(ID_SPINCTRL5,wxEVT_COMMAND_SPINCTRL_UPDATED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesCropX2SpinnerChange);
    Connect(ID_SPINCTRL6,wxEVT_COMMAND_SPINCTRL_UPDATED,(wxObjectEventFunction)&PlottWareControlFrame::OnImagesCropY2SpinnerChange);
    Connect(ID_RADIOBUTTON1,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnProcessingRetrAllRadioSelect);
    Connect(ID_RADIOBUTTON2,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnProcessingRetrExtRadioSelect);
    Connect(ID_SLIDER1,wxEVT_SCROLL_TOP|wxEVT_SCROLL_BOTTOM|wxEVT_SCROLL_LINEUP|wxEVT_SCROLL_LINEDOWN|wxEVT_SCROLL_PAGEUP|wxEVT_SCROLL_PAGEDOWN|wxEVT_SCROLL_THUMBTRACK|wxEVT_SCROLL_THUMBRELEASE|wxEVT_SCROLL_CHANGED,(wxObjectEventFunction)&PlottWareControlFrame::OnProcessingCannyThreshSliderCmdScroll);
    Connect(ID_CHOICE1,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnProcessingColorLineSelectSelect);
    Connect(ID_CHOICE2,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnProcessingColorColorSelectSelect);
    Connect(ID_BUTTON25,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnProcessingColorForAllButtonClick);
    Connect(ID_BUTTON8,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnProcessingPreviewButtonClick);
    Connect(ID_BUTTON10,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnGcodeGenerateButtonClick);
    Connect(ID_BUTTON12,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnGcodeEditButtonClick);
    Connect(ID_BUTTON13,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnGcodeExportButtonClick);
    Connect(ID_BUTTON7,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterConnectButtonClick);
    Connect(ID_BUTTON16,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterUpButtonClick);
    Connect(ID_BUTTON17,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterDownButtonClick);
    Connect(ID_BUTTON18,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterLeftButtonClick);
    Connect(ID_BUTTON19,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterRightButtonClick);
    Connect(ID_BUTTON20,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterHomeButtonClick);
    Connect(ID_CHOICE3,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterHeadSelectSelect);
    Connect(ID_BUTTON21,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterPowerButtonClick);
    Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrinterConsoleTextEnter);
    Connect(ID_BUTTON11,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingPrintButtonClick);
    Connect(ID_BUTTON14,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingStopButtonClick);
    Connect(ID_BUTTON15,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingEmergencyButtonClick);
    Connect(MenuSettingItem,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnMenuItemSettingsSelected);
    Connect(MenuQuitItem,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PlottWareControlFrame::OnAbout);
    Connect(ID_TIMER1,wxEVT_TIMER,(wxObjectEventFunction)&PlottWareControlFrame::OnPrintingElapsedTimeCheckerTimerTrigger);
    //*)

    for(int i=0; i<getSettings()->getPrinter()->getPens().size(); i++){
        ProcessingColorColorSelect->Append(getSettings()->getPrinter()->getPens().at(i));
    }
}

Settings* PlottWareControlFrame::getSettings(){
    return settings;
}

Project* PlottWareControlFrame::getProject(){
    return project;
}

void PlottWareControlFrame::projectControlEnable(bool to){
    ProjSaveButton->Enable(to);
    ProjCloseButton->Enable(to);
    ImagesImageListBox->Enable(to);
    ImagesAddButton->Enable(to);
    ImagesDeleteButton->Enable(to);
    ImagesImageListMoveUpButton->Enable(to);
    ImagesImageListMoveDownButton->Enable(to);
    ImagesScaleText->Enable(to);
    ImagesPositionXSpinner->Enable(to);
    ImagesPositionYSpinner->Enable(to);
    ImagesCropX1Spinner->Enable(to);
    ImagesCropY1Spinner->Enable(to);
    ImagesCropX2Spinner->Enable(to);
    ImagesCropY2Spinner->Enable(to);
    ProcessingRetrAllRadio->Enable(to);
    ProcessingRetrExtRadio->Enable(to);
    ProcessingCannyThreshSlider->Enable(to);
    ProcessingColorLineSelect->Enable(to);
    ProcessingColorColorSelect->Enable(to);
    ProcessingColorForAllButton->Enable(to);
    ProcessingPreviewButton->Enable(to);
    ImagesPreviewButton->Enable(to);
    GcodeGenerateButton->Enable(to);
    GcodeEditButton->Enable(to);
    GcodeExportButton->Enable(to);
}

void PlottWareControlFrame::printerControlEnable(bool to){
    PrintingEmergencyButton->Enable(to);
    PrintingPrinterHomeButton->Enable(to);
    PrintingPrinterPowerButton->Enable(to);
    PrintingPrinterHeadSelect->Enable(to);
    PrintingPrinterMoveSpinner->Enable(to);
    PrintingPrinterUpButton->Enable(to);
    PrintingPrinterDownButton->Enable(to);
    PrintingPrinterRightButton->Enable(to);
    PrintingPrinterLeftButton->Enable(to);
    PrintingPrinterConsole->Enable(to);
}

void PlottWareControlFrame::printingControlShow(bool to){
    PrintingStatusLabel->Show(to);
    PrintingStatusGauge->Show(to);
    PrintingElapsedTimeLabelLabel->Show(to);
    PrintingElapsedTimeLabel->Show(to);
}

PlottWareControlFrame::~PlottWareControlFrame()
{
    //(*Destroy(PlottWareControlFrame)
    //*)
}

void PlottWareControlFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void PlottWareControlFrame::OnAbout(wxCommandEvent& event)
{
    std::string msg="PlottWareControl\nVersion: ";
    msg+=Version::FULLVERSION_STRING;
    msg+="\nBuild: ";
    msg+=std::to_string(Version::BUILDS_COUNT);
    msg+="\n\nWhen printing 10 pixe/mm (254 dpi)\n\nCopyright: Fandly Gergo Zoltan\n(fandlygergo@gmail.hu, systemtest.tk)\n\nLicense: GNU GPLv3 (www.gnu.org/licenses/gpl-3.0.en.html)";
    wxMessageBox(msg, _("About"));
}

void PlottWareControlFrame::OnMenuItemSettingsSelected(wxCommandEvent& event)
{
    SettingDialog *dialog=new SettingDialog(this, wxID_ANY, this);
    dialog->Show(true);
}

void PlottWareControlFrame::OnProjNewButtonClick(wxCommandEvent& event)
{
    wxFileDialog newFileDialog(this, _("Select the location of new project"), "", "", "PlottWareControl project files (*.pwc)|*.pwc", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
    if(newFileDialog.ShowModal()==wxID_CANCEL){
        event.Skip();
        return;
    }
    project=new Project(getSettings(), std::string(newFileDialog.GetPath().mb_str()));
    if(!getProject()->newProj()){
        wxMessageDialog(this, _("Error: Could not write file"), _("Error"), wxOK|wxCENTER|wxICON_ERROR).ShowModal();
        event.Skip();
        delete project;
        project=NULL;
        return;
    }
    projectControlEnable(true);
}

void PlottWareControlFrame::OnProjCloseButtonClick(wxCommandEvent& event)
{
    getProject()->close();
    delete project;
    project=NULL;
    projectControlEnable(false);
}

void PlottWareControlFrame::OnImagesAddButtonClick(wxCommandEvent& event)
{
    wxFileDialog loadImageDialog(this, _("Open an image file"), "%userprofile%\pictures", "", "Image files (*.jpg;*jpeg;*.bpm;*.png)|*.jpg;*.jpeg;*.bmp;*.png;*.JPG;*.JPEG;*.BPM;*.PNG", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
    if(loadImageDialog.ShowModal()==wxID_CANCEL){
        event.Skip();
        return;
    }
    Image img=Image(std::string(loadImageDialog.GetPath().mb_str()));
    if(!img.load()){
        wxMessageDialog(this, _("Could not open or find the image"), _("Error"), wxOK|wxCENTER|wxICON_ERROR).ShowModal();
        event.Skip();
        return;
    }
    getProject()->getImages()->push_back(img);
    ImagesImageListBox->Append(loadImageDialog.GetFilename());
    if(getSettings()->isDebug()){
        ImageViewer* iv=new ImageViewer(getProject()->getImages()->back().getMat(), "DEBUG:OnImagesAddBUttonClick> "+img.getFilename());
        iv->show();
    }
    return;
}

void PlottWareControlFrame::OnImagesDeleteButtonClick(wxCommandEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    ImagesImageListBox->Delete(sel);
    getProject()->getImages()->erase(getProject()->getImages()->begin()+sel);
}

void PlottWareControlFrame::OnImagesImageListBoxSelect(wxCommandEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    ImagesSizeLabel->SetLabel(std::to_string(getProject()->getImages()->at(sel).getMat()->cols)+"x"+std::to_string(getProject()->getImages()->at(sel).getMat()->rows));
    ImagesScaleText->SetValue(std::to_string(getProject()->getImages()->at(sel).getScale()));
    ImagesPositionXSpinner->SetValue(getProject()->getImages()->at(sel).getPosition().getX());
    ImagesPositionYSpinner->SetValue(getProject()->getImages()->at(sel).getPosition().getY());
    ImagesCropX1Spinner->SetValue(getProject()->getImages()->at(sel).getCrop1().getX());
    ImagesCropY1Spinner->SetValue(getProject()->getImages()->at(sel).getCrop1().getY());
    ImagesCropX2Spinner->SetValue(getProject()->getImages()->at(sel).getCrop2().getX());
    ImagesCropY2Spinner->SetValue(getProject()->getImages()->at(sel).getCrop2().getY());
}

void PlottWareControlFrame::OnImagesPositionXSpinnerChange(wxSpinEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    getProject()->getImages()->at(sel).setPosition(Point(ImagesPositionXSpinner->GetValue(), ImagesPositionYSpinner->GetValue()));
}

void PlottWareControlFrame::OnImagesPositionYSpinnerChange(wxSpinEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    getProject()->getImages()->at(sel).setPosition(Point(ImagesPositionXSpinner->GetValue(), ImagesPositionYSpinner->GetValue()));
}

void PlottWareControlFrame::OnImagesCropX1SpinnerChange(wxSpinEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    getProject()->getImages()->at(sel).setCrop(Point(ImagesCropX1Spinner->GetValue(), ImagesCropY1Spinner->GetValue()), Point(ImagesCropX2Spinner->GetValue(), ImagesCropY2Spinner->GetValue()));
}

void PlottWareControlFrame::OnImagesCropY1SpinnerChange(wxSpinEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    getProject()->getImages()->at(sel).setCrop(Point(ImagesCropX1Spinner->GetValue(), ImagesCropY1Spinner->GetValue()), Point(ImagesCropX2Spinner->GetValue(), ImagesCropY2Spinner->GetValue()));
}

void PlottWareControlFrame::OnImagesCropX2SpinnerChange(wxSpinEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    getProject()->getImages()->at(sel).setCrop(Point(ImagesCropX1Spinner->GetValue(), ImagesCropY1Spinner->GetValue()), Point(ImagesCropX2Spinner->GetValue(), ImagesCropY2Spinner->GetValue()));
}

void PlottWareControlFrame::OnImagesCropY2SpinnerChange(wxSpinEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    getProject()->getImages()->at(sel).setCrop(Point(ImagesCropX1Spinner->GetValue(), ImagesCropY1Spinner->GetValue()), Point(ImagesCropX2Spinner->GetValue(), ImagesCropY2Spinner->GetValue()));
}

void PlottWareControlFrame::OnImagesPreviewButtonClick(wxCommandEvent& event)
{
    getProject()->combine();
}

void PlottWareControlFrame::OnImagesImageListMoveUpButtonClick(wxCommandEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    if(sel>0){
        std::swap(getProject()->getImages()->at(sel), getProject()->getImages()->at(sel-1));
        wxString cp=ImagesImageListBox->GetString(sel-1);
        ImagesImageListBox->SetString(sel-1, ImagesImageListBox->GetString(sel));
        ImagesImageListBox->SetString(sel, cp);
        ImagesImageListBox->SetSelection(sel-1);
    }
}

void PlottWareControlFrame::OnImagesImageListMoveDownButtonClick(wxCommandEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();
    if(sel<getProject()->getImages()->size()-1){
        std::swap(getProject()->getImages()->at(sel), getProject()->getImages()->at(sel+1));
        wxString cp=ImagesImageListBox->GetString(sel+1);
        ImagesImageListBox->SetString(sel+1, ImagesImageListBox->GetString(sel));
        ImagesImageListBox->SetString(sel, cp);
        ImagesImageListBox->SetSelection(sel+1);
    }
}

void PlottWareControlFrame::OnImagesScaleTextText(wxCommandEvent& event)
{
    if(ImagesImageListBox->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }
    int sel=ImagesImageListBox->GetSelection();

    double n=std::stod(std::string(ImagesScaleText->GetValue().mb_str()));
    if(n<=0){
        ImagesScaleText->SetBackgroundColour(wxColour("red"));
        event.Skip();
        return;
    }
    else{
        ImagesScaleText->SetBackgroundColour(wxColour("white"));
        getProject()->getImages()->at(sel).setScale(n);
    }
}

void PlottWareControlFrame::OnProcessingRetrAllRadioSelect(wxCommandEvent& event)
{
    if(ProcessingRetrAllRadio->GetValue()){
        getProject()->setRetrMode(CV_RETR_LIST);
    }
    else{
        getProject()->setRetrMode(CV_RETR_EXTERNAL);
    }
}

void PlottWareControlFrame::OnProcessingRetrExtRadioSelect(wxCommandEvent& event)
{
    if(ProcessingRetrAllRadio->GetValue()){
        getProject()->setRetrMode(CV_RETR_LIST);
    }
    else{
        getProject()->setRetrMode(CV_RETR_EXTERNAL);
    }
}

void PlottWareControlFrame::OnProcessingCannyThreshSliderCmdScroll(wxScrollEvent& event)
{
    getProject()->setCannyThresh(ProcessingCannyThreshSlider->GetValue());
}

void PlottWareControlFrame::OnProcessingPreviewButtonClick(wxCommandEvent& event)
{
    if(!getProject()->process()){
        wxMessageDialog(this, _("Please combine the pictures first!"), _("Error"), wxOK|wxCENTER|wxICON_EXCLAMATION).ShowModal();
    }

    //add edges to drop down
    ProcessingColorLineSelect->Clear();
    for(int i=0; i<getProject()->getColoring()->size(); i++){
        ProcessingColorLineSelect->Append(std::to_string(i));
    }

    //add colors if this did't happen till now
    ProcessingColorColorSelect->Clear();
    ProcessingColorColorSelect->Append("Leave out");
    for(int i=0; i<getSettings()->getPrinter()->getPens().size(); i++){
        ProcessingColorColorSelect->Append(getSettings()->getPrinter()->getPens().at(i));
    }

    //remove selection
    ProcessingColorColorSelect->SetSelection(wxNOT_FOUND);
}

void PlottWareControlFrame::OnProcessingColorLineSelectSelect(wxCommandEvent& event)
{
    //select the set
    ProcessingColorColorSelect->SetSelection(getProject()->getColoring()->at(ProcessingColorLineSelect->GetSelection()));

    //show the ominous contour
    getProject()->printEdge(ProcessingColorLineSelect->GetSelection());
}

void PlottWareControlFrame::OnProcessingColorColorSelectSelect(wxCommandEvent& event)
{
    //check if there's selected anything
    if(ProcessingColorLineSelect->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }

    getProject()->getColoring()->at(ProcessingColorLineSelect->GetSelection())=ProcessingColorColorSelect->GetSelection();
}

void PlottWareControlFrame::OnProcessingColorForAllButtonClick(wxCommandEvent& event)
{
    if(ProcessingColorColorSelect->GetSelection()==wxNOT_FOUND){
        event.Skip();
        return;
    }

    for(int i=0; i<getProject()->getColoring()->size(); i++){
        getProject()->getColoring()->at(i)=ProcessingColorColorSelect->GetSelection();
    }
}

void PlottWareControlFrame::OnGcodeGenerateButtonClick(wxCommandEvent& event)
{
    if(!getProject()->generateGcode()){
        wxMessageDialog(this, _("Plese process the canvas first!"), _("Error"), wxOK|wxCENTER|wxICON_EXCLAMATION).ShowModal();
    }
}

void PlottWareControlFrame::OnGcodeEditButtonClick(wxCommandEvent& event)
{
    GcodeEditorDialog* dialog=new GcodeEditorDialog(this, wxID_ANY, getProject()->getGcode());
    dialog->Show();
}

void PlottWareControlFrame::OnGcodeExportButtonClick(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Export G code"), "", "", "G code files (*.g;*.gcode)|*.g;*.gcode", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
    if(saveFileDialog.ShowModal()==wxID_CANCEL){
        return;
    }
    if(!getProject()->exportGcode(std::string(saveFileDialog.GetPath().mb_str()))){
        wxMessageDialog(this, _("Can not create or write the selected file"), _("Error"), wxOK|wxCENTER|wxICON_ERROR).ShowModal();
        return;
    }
    wxMessageDialog(this, _("G code successfully exported"), _("Info"), wxOK|wxCENTER|wxICON_INFORMATION).ShowModal();
}

void PlottWareControlFrame::OnPrintingPrinterConnectButtonClick(wxCommandEvent& event)
{
    if(!getSettings()->getPrinter()->isConnected()){
        if(!getSettings()->getPrinter()->connect()){
            wxMessageDialog(this, _("Can not connect to printer. Please check your settings!"), _("Warning"), wxOK|wxCENTER|wxICON_WARNING).ShowModal();
            return;
        }
        printerControlEnable(true);
        PrintingPrinterStatusLabel->SetLabel(_("Online"));
        PrintingPrinterPowerLabel->SetLabel(getSettings()->getPrinter()->getPowerState()?"On":"Off");
        PrintingPrinterConnectButton->SetLabel(_("Disconnect"));
        if(getSettings()->getPrinter()->getPosition().getX()>-1){
            PrintingPrinterXLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getX()));
        }
        else{
            PrintingPrinterXLabel->SetLabel("?");
        }
        if(getSettings()->getPrinter()->getPosition().getY()>-1){
            PrintingPrinterYLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getY()));
        }
        else{
            PrintingPrinterYLabel->SetLabel("?");
        }
    }
    else{
        getSettings()->getPrinter()->disconnect();
        printerControlEnable(false);
        PrintingPrinterStatusLabel->SetLabel(_("Offline"));
        PrintingPrinterPowerLabel->SetLabel("");
        PrintingPrinterConnectButton->SetLabel(_("Connect"));
        PrintingPrinterXLabel->SetLabel("");
        PrintingPrinterYLabel->SetLabel("");
    }
}

void PlottWareControlFrame::OnPrintingPrinterHomeButtonClick(wxCommandEvent& event)
{
    getSettings()->getPrinter()->send("G28\n");
    PrintingPrinterXLabel->SetLabel("0");
    PrintingPrinterYLabel->SetLabel("0");
}

void PlottWareControlFrame::OnPrintingPrinterPowerButtonClick(wxCommandEvent& event)
{
    if(getSettings()->getPrinter()->getPowerState()){
        getSettings()->getPrinter()->send("M81\n"); //turn off
    }
    else{
        getSettings()->getPrinter()->send("M80"); //turn on
    }
}

void PlottWareControlFrame::OnPrintingPrinterHeadSelectSelect(wxCommandEvent& event)
{
    getSettings()->getPrinter()->send("T"+std::to_string(PrintingPrinterHeadSelect->GetSelection()));
}

void PlottWareControlFrame::OnPrintingPrinterUpButtonClick(wxCommandEvent& event)
{
    getSettings()->getPrinter()->send("G91\nG1 Y"+std::to_string(PrintingPrinterMoveSpinner->GetValue())+"G90");
    PrintingPrinterXLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getX()));
    PrintingPrinterYLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getY()));
}

void PlottWareControlFrame::OnPrintingPrinterDownButtonClick(wxCommandEvent& event)
{
    getSettings()->getPrinter()->send("G91\nG1 Y"+std::to_string(-PrintingPrinterMoveSpinner->GetValue())+"G90");
    PrintingPrinterXLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getX()));
    PrintingPrinterYLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getY()));
}

void PlottWareControlFrame::OnPrintingPrinterRightButtonClick(wxCommandEvent& event)
{
    getSettings()->getPrinter()->send("G91\nG1 X"+std::to_string(PrintingPrinterMoveSpinner->GetValue())+"G90");
    PrintingPrinterXLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getX()));
    PrintingPrinterYLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getY()));
}

void PlottWareControlFrame::OnPrintingPrinterLeftButtonClick(wxCommandEvent& event)
{
    getSettings()->getPrinter()->send("G91\nG1 X"+std::to_string(-PrintingPrinterMoveSpinner->GetValue())+"G90");
    PrintingPrinterXLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getX()));
    PrintingPrinterYLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getY()));
}

void PlottWareControlFrame::OnPrintingPrinterConsoleTextEnter(wxCommandEvent& event)
{
    getSettings()->getPrinter()->send(std::string(PrintingPrinterConsole->GetValue().mb_str()));
    std::string r=getSettings()->getPrinter()->recv();
    PrintingPrinterLog->AppendAndEnsureVisible(r);
    PrintingPrinterXLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getX()));
    PrintingPrinterYLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getY()));
}

void PlottWareControlFrame::OnPrintingPrintButtonClick(wxCommandEvent& event)
{
    if(getProject()->getGcode()->empty()){
        wxMessageDialog(this, _("Please generate some G code first!"), _("Error"), wxCENTER|wxOK|wxICON_ERROR).ShowModal();
        return;
    }
    //get line count (for status)
    int lineNumber=std::count(getProject()->getGcode()->begin(), getProject()->getGcode()->end(), '\n');

    //prepare UI
    PrintingStatusGauge->SetRange(lineNumber);
    PrintingStatusGauge->SetValue(0);
    PrintingElapsedTimeLabel->SetLabel("00:00:00");
    printingControlShow(true);

    //start UI refresher
    PrintingStopWatch.Start();
    PrintingElapsedTimeCheckerTimer.Start(1000);

    //set counter
    getProject()->printStatus=0;

    //start printing
    //print to log
    PrintingPrinterLog->AppendAndEnsureVisible("Printing started");

    //print!!!
    getProject()->printStop=false;
    std::istringstream print(*getProject()->getGcode());
    for(std::string line; std::getline(print, line);){
        //stop if we need so
        if(getProject()->printStop){
            break;
        }

        //log
        PrintingPrinterLog->AppendAndEnsureVisible("Printing line #"+std::to_string(getProject()->printStatus)+" of "+std::to_string(lineNumber));

        if(!getSettings()->getPrinter()->send(line+"\n")){ //if can't send there's some problem
            break;
        }
        std::string r=getSettings()->getPrinter()->recv();
        getProject()->printStatus++;
        if(r!="OK"){
            if(wxMessageDialog(this, _("Something went wrong. The printer responded with:")+r+_("\nWould you like to continue with the printing?"), _("Error"), wxCENTER|wxYES_NO|wxICON_ERROR).ShowModal()==wxID_YES){
                continue;
            }
            else{
                break;
            }
        }

        //update position
        PrintingPrinterXLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getX()));
        PrintingPrinterYLabel->SetLabel(std::to_string(getSettings()->getPrinter()->getPosition().getY()));
    }

    //finish printing
    wxMessageDialog(this, _("Printing finished!"), _("Info"), wxCENTER|wxOK|wxICON_INFORMATION).ShowModal();

    //print to log
    PrintingPrinterLog->AppendAndEnsureVisible("Printing finished");

    //stop UI refresher
    PrintingStopWatch.Pause();
    PrintingElapsedTimeCheckerTimer.Stop();
}

void PlottWareControlFrame::OnPrintingElapsedTimeCheckerTimerTrigger(wxTimerEvent& event)
{
    //update gauge
    PrintingStatusGauge->SetValue(getProject()->printStatus);

    //update elapsed time
    long sec=PrintingStopWatch.Time()/1000;
    char time[15];
    sprintf(time, "%02i:%02i:%02i", sec/3600, sec%3600/60, sec%60);
    PrintingElapsedTimeLabel->SetLabel(std::string(time));
}

void PlottWareControlFrame::OnPrintingStopButtonClick(wxCommandEvent& event)
{
    getProject()->printStop=true;
}

void PlottWareControlFrame::OnPrintingEmergencyButtonClick(wxCommandEvent& event)
{
    //stop printing
    getProject()->printStop=true;
    //send emergency reset code
    getSettings()->getPrinter()->send("M112\n");
    //disconnect from printer
    getSettings()->getPrinter()->disconnect();

    //update UI
    printerControlEnable(false);
    PrintingPrinterStatusLabel->SetLabel(_("Offline"));
    PrintingPrinterPowerLabel->SetLabel("");
    PrintingPrinterConnectButton->SetLabel(_("Connect"));
    PrintingPrinterXLabel->SetLabel("");
    PrintingPrinterYLabel->SetLabel("");
}
