/* * pawsstackcountwindow.h - Author: Ondrej Hurt * * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org) * * * 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 (version 2 of the License) * 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #include #include "pawsstackcountwindow.h" #include "paws/pawsmanager.h" #include "paws/pawscrollbar.h" #include "paws/pawsbutton.h" #include "paws/pawstextbox.h" pawsStackCountWindow::pawsStackCountWindow() { maxLabel = NULL; editBox = NULL; scrollBar = NULL; okButton = NULL; cancelButton = NULL; action = NULL; SetMaxCount(0); lastValidText = ""; } void pawsStackCountWindow::SetMaxCount(int maxCount) { csString countStr; int div; this->maxCount = maxCount; if (scrollBar != NULL) scrollBar->SetMaxValue(maxCount); if (maxLabel != NULL) { countStr.Format("%d",maxCount); maxLabel->SetText("max = "+countStr); } maxDigits = 0; div = maxCount; while (div > 0) { maxDigits++; div /= 10; } } bool pawsStackCountWindow::PostSetup() { maxLabel = dynamic_cast (FindWidget("MaxLabel")); if (maxLabel == NULL) return false; editBox = dynamic_cast (FindWidget("EditBox")); if (editBox == NULL) return false; PawsManager::GetSingleton().SetCurrentFocusedWidget(editBox); scrollBar = dynamic_cast (FindWidget("ScrollBar")); if (scrollBar == NULL) return false; scrollBar->SetTickValue(1); okButton = dynamic_cast (FindWidget("OkButton")); if (okButton == NULL) return false; cancelButton = dynamic_cast (FindWidget("CancelButton")); if (cancelButton == NULL) return false; return true; } bool pawsStackCountWindow::OnScroll( int scrollDirection, pawsScrollBar* widget ) { lastValidText.Format("%d",(int)scrollBar->GetCurrentValue()); editBox->SetText(lastValidText); return true; } bool pawsStackCountWindow::OnButtonPressed( int button, int keyModifier, pawsWidget* widget ) { if (action == NULL) return false; if (widget == okButton) { if (TextIsValidForOutput(editBox->GetText())) CountWasEntered(atoi(editBox->GetText())); return true; } if (widget == cancelButton) { if (action != NULL) { action->Execute(-1); delete action; action = NULL; } PawsManager::GetSingleton().SetModalWidget(NULL); Hide(); return true; } return false; } bool pawsStackCountWindow::TextIsValidForEditing(const csString & text) { if ((int)text.Length() > maxDigits) return false; for (int i=0; i < (int)text.Length(); i++) if ((text.GetAt(i) < '0') || (text.GetAt(i) > '9')) return false; if ( (text.GetData() != NULL) && (atoi(text.GetData()) > maxCount) ) return false; return true; } bool pawsStackCountWindow::TextIsValidForOutput(const csString & text) { return TextIsValidForEditing(text) && (text.Length() > 0) && (atoi(text.GetData()) > 0); } bool pawsStackCountWindow::OnChange(pawsWidget * widget) { csString text; if (widget == editBox) { text = editBox->GetText(); if (TextIsValidForEditing(text)) { lastValidText = text; if (TextIsValidForOutput(text) && (text.Length() == maxDigits)) CountWasEntered(atoi(text.GetData())); } else editBox->SetText(lastValidText); } return true; } void pawsStackCountWindow::CountWasEntered(int count) { PawsManager::GetSingleton().SetModalWidget(NULL); Hide(); if (action != NULL) { action->Execute(count); delete action; action = NULL; } } void pawsStackCountWindow::ResetDialog() { editBox->SetText(""); scrollBar->SetCurrentValue(0); lastValidText = ""; PawsManager::GetSingleton().SetCurrentFocusedWidget(editBox); SetAppropriatePos(); } void pawsStackCountWindow::SetAppropriatePos() { psPoint mouse; int x, y; int width, height; width = screenFrame.Width(); height = screenFrame.Height(); mouse = PawsManager::GetSingleton().GetMouse()->GetPosition(); x = mouse.x - width / 2; y = mouse.y - height / 2; MoveTo(x, y); MakeFullyVisible(); } void pawsStackCountWindow::GetStackCount(iOnCountEnteredAction * action, int maxCount) { pawsStackCountWindow * scw; if (maxCount == 1) { action->Execute(1); delete action; } else { scw = dynamic_cast (PawsManager::GetSingleton().FindWidget("StackCountWindow")); CS_ASSERT(scw != NULL); scw->Show(); PawsManager::GetSingleton().SetModalWidget(scw); scw->SetMaxCount(maxCount); scw->ResetDialog(); scw->SetAction(action); } }