Newer
Older
powermon_manager_sw / gui / gui_string_dialog.cpp
@Razvan Turiac Razvan Turiac on 8 Jul 3 KB Initial import
/* Copyright (C) 2020 - 2024, Thornwave Labs Inc
 * Written by Razvan Turiac <razvan.turiac@thornwave.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the “Software”), to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * Attribution shall be given to Thornwave Labs Inc. and shall be made visible to the final user. 
 * 
 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <gui_string_dialog.h>


GuiEnterString::GuiEnterString(wxWindow *parent, wxWindowID id, const wxString &title, bool password, bool confirm): wxDialog(parent, id, title), 
																		mMaxStringLength(-1), mPassword(password), mConfirm(confirm)
{
	const size_t BORDER_SIZE = FromDIP(6);

	//main window sizer
	wxBoxSizer *main_sizer = new wxBoxSizer(wxVERTICAL);
	SetSizer(main_sizer);

	mStringText = new wxTextCtrl(this, ID_STRING, wxT(""), wxDefaultPosition, wxDefaultSize, mPassword ? wxTE_PASSWORD : 0);
	mStringLabel = new wxStaticText(this, wxID_ANY, wxT(""));

	if (mConfirm)
	{
		mConfirmStringText = new wxTextCtrl(this, ID_CONFIRM_STRING, wxT(""), wxDefaultPosition, wxDefaultSize, mPassword ? wxTE_PASSWORD : 0);
		mConfirmLabel = new wxStaticText(this, wxID_ANY, wxT(""));
	}
	
	main_sizer->AddSpacer(10);
	main_sizer->Add(mStringLabel, 0, wxEXPAND);
	main_sizer->Add(mStringText, 0, wxALL | wxEXPAND, BORDER_SIZE);
	main_sizer->AddSpacer(10);
	
	if (mConfirm)
	{
		main_sizer->Add(mConfirmLabel, 0, wxEXPAND);
		main_sizer->Add(mConfirmStringText, 0, wxALL | wxEXPAND, BORDER_SIZE);
		main_sizer->AddSpacer(10);
	}

	mErrorLabel = new wxStaticText(this, wxID_ANY, wxT(""));
	mErrorLabel->SetForegroundColour(*wxRED);
	main_sizer->Add(mErrorLabel, 0, wxALL | wxEXPAND, BORDER_SIZE);

	wxBoxSizer *h_sizer = new wxBoxSizer(wxHORIZONTAL);
	main_sizer->Add(h_sizer, 1, wxALL | wxEXPAND, BORDER_SIZE);

	wxButton* button = new wxButton(this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, FromDIP(wxSize(150, 40)));
	h_sizer->Add(button, 0, wxALL | wxCENTER, BORDER_SIZE);

	mContinueButton = new wxButton(this, wxID_OK, wxT("Continue"), wxDefaultPosition, FromDIP(wxSize(150, 40)));
	h_sizer->Add(mContinueButton, 0, wxALL | wxCENTER, BORDER_SIZE);

	main_sizer->SetSizeHints(this);
	Centre();

	Bind(wxEVT_COMMAND_TEXT_UPDATED, &GuiEnterString::OnTextCtrlUpdated, this, ID_STRING);

	if (mConfirm)
		Bind(wxEVT_COMMAND_TEXT_UPDATED, &GuiEnterString::OnTextCtrlUpdated, this, ID_CONFIRM_STRING);
}


GuiEnterString::~GuiEnterString()
{
}


void GuiEnterString::OnTextCtrlUpdated(wxCommandEvent &event)
{
	wxString pass1 = mStringText->GetValue();
	bool enable = true;

	if (pass1.size() > mMaxStringLength)
	{
		mErrorLabel->SetLabel(wxT("string is too long"));
		enable = false;
	}
	else
	{
		if (mConfirm)
		{
			wxString pass2 = mConfirmStringText->GetValue();

			if (pass1 == pass2)
			{
				mErrorLabel->SetLabel(wxT(""));
			}
			else
			{
				mErrorLabel->SetLabel(wxT("passwords do not match"));
				enable = false;
			}
		}
		else
		{
			mErrorLabel->SetLabel(wxT(""));
		}
	}

	mErrorLabel->Refresh();
	mContinueButton->Enable(enable);
}