📄 control.cxx
字号:
PIntegerEditBox::PIntegerEditBox(PInteractor * parent,
long min, long max, long val, long ndg, BYTE numBase)
: PNumberEditBox(parent)
{
SetMinimum(min);
SetMaximum(max);
SetNudge(ndg);
SetBase(numBase);
SetValue(val);
}
BOOL PIntegerEditBox::OnEndInput()
{
parent->OnControlNotify(*this, PEditBox::EndEdit);
long val = GetValue();
if (val < minimum || val > maximum) {
PSound::Beep();
return FALSE;
}
return TRUE;
}
void PIntegerEditBox::OnKeyInput(const PString & str)
{
char c = (char)toupper(str[0]);
char maxletter = '@';
char maxdigit = (char)(base+'0'-1);
if (maxdigit > '9') {
maxdigit = '9';
maxletter = (char)(base-10+'A'-1);
}
if (iscntrl(c) || c == '-' || c == '+' ||
(c >= '0' && c <= maxdigit) || (c >= '0' && c <= maxletter))
PNumberEditBox::OnKeyInput(str);
}
void PIntegerEditBox::TransferValue(int option)
{
if (valuePointer != NULL)
if (option == NotifyUpdate)
SetValue(*(long *)valuePointer);
else if (option == EndEdit)
*(long *)valuePointer = GetValue();
}
void PIntegerEditBox::AddNudge()
{
SetValue(GetValue()+nudge);
}
void PIntegerEditBox::SubtractNudge()
{
SetValue(GetValue()-nudge);
}
void PIntegerEditBox::SetValue(long val)
{
if (val < minimum)
val = minimum;
else if (val > maximum)
val = maximum;
PString str(PString::Signed, val, base);
SetText(str);
}
long PIntegerEditBox::GetValue() const
{
return GetText().AsInteger(base);
}
void PIntegerEditBox::SetNudge(long val)
{
PAssert(val > 0, PInvalidParameter);
nudge = val;
}
void PIntegerEditBox::SetBase(BYTE val)
{
PAssert(val >= 2 && val <= 36, PInvalidParameter);
base = val;
}
#endif
//////////////////////////////////////////////////////////////////////////////
// PFloatEditBox
#if defined(_PFLOATEDITBOX)
PFloatEditBox::PFloatEditBox(PInteractor * parent,
double min, double max, double val, double ndg, unsigned decs)
: PNumberEditBox(parent)
{
SetMinimum(min);
SetMaximum(max);
SetNudge(ndg);
SetDecimals(decs);
SetValue(val);
}
BOOL PFloatEditBox::OnEndInput()
{
parent->OnControlNotify(*this, PEditBox::EndEdit);
double val = GetValue();
if (val < minimum || val > maximum) {
PSound::Beep();
return FALSE;
}
return TRUE;
}
void PFloatEditBox::OnKeyInput(const PString & str)
{
char c = str[0];
if (iscntrl(c) || isdigit(c) ||
c == '-' || c == '+' || c == '.' || toupper(c) == 'E')
PNumberEditBox::OnKeyInput(str);
}
void PFloatEditBox::AddNudge()
{
SetValue(GetValue()+nudge);
}
void PFloatEditBox::SubtractNudge()
{
SetValue(GetValue()-nudge);
}
void PFloatEditBox::SetValue(double val)
{
if (val < minimum)
val = minimum;
else if (val > maximum)
val = maximum;
SetText(psprintf(psprintf("%%0.%dg", decimals), (double)val));
}
double PFloatEditBox::GetValue() const
{
return GetText().AsReal();
}
void PFloatEditBox::TransferValue(int option)
{
if (valuePointer != NULL)
if (option == NotifyUpdate)
SetValue(*(double *)valuePointer);
else if (option == EndEdit)
*(double *)valuePointer = GetValue();
}
#endif
//////////////////////////////////////////////////////////////////////////////
// PPushButton
#if defined(_PPUSHBUTTON)
PPushButton::PPushButton(PInteractor * parent, ButtonType deflt)
: PNamedControl(parent),
defaultButton(deflt == DefaultButton)
{
Construct();
}
PPushButton::PPushButton(PInteractor * parent,
const PString & name, ButtonType deflt, const PNotifier & notify)
: PNamedControl(parent, name, notify, NULL),
defaultButton(deflt == DefaultButton)
{
Construct();
}
PRect PPushButton::DrawButtonSurround(PCanvas & canvas, BOOL isSelected)
{
canvas.SetMappingRect(canvas.GetViewportRect());
PRect bounds = canvas.GetDrawingBounds();
canvas.SetPenFgColour(owner->GetButtonFgColour());
canvas.SetFillFgColour(owner->GetButtonBkColour());
canvas.DrawRoundRect(bounds, 2, 2);
PDim border = owner->GetBorderSize();
PDim border2 = border * 2;
bounds.Inflate(-(PORDINATE)border.Width(), -(PORDINATE)border.Height());
if (defaultButton) {
canvas.DrawRect(bounds);
bounds.Inflate(-(PORDINATE)border.Width(), -(PORDINATE)border.Height());
}
canvas.SetPenFgColour(owner->GetButtonShadowColour());
if (!isSelected) {
canvas.SetPenWidth(border.Height());
canvas.DrawLine(
bounds.Left(), bounds.Bottom()-border.Height(),
bounds.Right()-border.Width(), bounds.Bottom()-border.Height());
canvas.DrawLine(
bounds.Left()+border.Width(), bounds.Bottom()-border2.Height(),
bounds.Right()-border.Width(), bounds.Bottom()-border2.Height());
canvas.SetPenWidth(border.Width());
canvas.DrawLine(
bounds.Right()-border.Width(), bounds.Top(),
bounds.Right()-border.Width(), bounds.Bottom());
canvas.DrawLine(
bounds.Right()-border2.Width(), bounds.Top()+border.Height(),
bounds.Right()-border2.Width(), bounds.Bottom());
canvas.SetPenFgColour(owner->GetButtonLightingColour());
}
canvas.SetCurrentPosition(bounds.Left(), bounds.Bottom()-border2.Height());
canvas.SetPenWidth(border.Width());
canvas.DrawLine(bounds.Left(), bounds.Top());
canvas.SetPenWidth(border.Height());
canvas.DrawLine(bounds.Right()-border.Width(), bounds.Top());
bounds.Inflate(-(PORDINATE)border.Width()*3, -(PORDINATE)border.Height()*3);
if (isSelected)
bounds.Offset(border.Width(), border.Height());
if (IsEnabled())
canvas.SetTextFgColour(owner->GetButtonFgColour());
else
canvas.SetTextFgColour(owner->GetGrayTextColour());
canvas.SetTextBkColour(owner->GetButtonBkColour());
return bounds;
}
#endif
//////////////////////////////////////////////////////////////////////////////
// PTextButton
#if defined(_PTEXTBUTTON)
PTextButton::PTextButton(PInteractor * parent,
const PString & name, ButtonType deflt)
: PPushButton(parent, name, deflt, PNotifier())
{
Construct();
DefaultDimensions();
}
PTextButton::PTextButton(PInteractor * parent,
const PString & name, const PNotifier & notify, ButtonType deflt)
: PPushButton(parent, name, deflt, notify)
{
Construct();
DefaultDimensions();
}
void PTextButton::OnDrawFace(PCanvas & canvas,
const PRect & rect, BOOL hasFocus, BOOL)
{
canvas.DrawString(rect, GetName(), PCanvas::Centred|PCanvas::CentreVertical);
if (hasFocus) {
PDim dim = canvas.MeasureString(GetName()) + owner->GetBorderSize()*2;
PRect r(rect.Left()+(rect.Width()-dim.Width())/2,
rect.Top()+(rect.Height()-dim.Height())/2,
dim.Width(), dim.Height());
canvas.DrawFocusRect(r);
}
}
#endif
//////////////////////////////////////////////////////////////////////////////
// PImageButton
#if defined(_PIMAGEBUTTON)
PImageButton::PImageButton(PInteractor * parent, ButtonType deflt)
: PPushButton(parent, deflt)
{
Construct();
DefaultDimensions();
}
PImageButton::PImageButton(PInteractor *parent,
const PImgIcon & img, ButtonType deflt)
: PPushButton(parent, "", deflt, PNotifier()),
enabledImage(img),
disabledImage(img)
{
Construct();
DefaultDimensions();
}
PImageButton::PImageButton(PInteractor *parent,
const PImgIcon & img, const PNotifier & notify, ButtonType deflt)
: PPushButton(parent, "", deflt, notify),
enabledImage(img),
disabledImage(img)
{
Construct();
DefaultDimensions();
}
PImageButton::PImageButton(PInteractor *parent,
const PImgIcon & enabImg,
const PImgIcon & disabImg,
ButtonType deflt)
: PPushButton(parent, "", deflt, PNotifier()),
enabledImage(enabImg),
disabledImage(disabImg)
{
Construct();
DefaultDimensions();
}
PImageButton::PImageButton(PInteractor *parent,
const PImgIcon & enabImg,
const PImgIcon & disabImg,
const PNotifier & notify,
ButtonType deflt)
: PPushButton(parent, "", deflt, notify),
enabledImage(enabImg),
disabledImage(disabImg)
{
Construct();
DefaultDimensions();
}
void PImageButton::DefaultDimensions()
{
PDim edim = enabledImage.GetDimensions();
PDim ddim = disabledImage.GetDimensions();
PDim dim(PMAX(edim.Width(),ddim.Width()), PMAX(edim.Height(),ddim.Height()));
SetDimensions(dim+owner->GetBorderSize()*9, PixelCoords);
}
void PImageButton::OnDrawFace(PCanvas & canvas,
const PRect & rect, BOOL hasFocus, BOOL)
{
canvas.SetFillFgColour(owner->GetButtonFgColour());
canvas.SetFillBkColour(owner->GetButtonBkColour());
PImgIcon & image = IsEnabled() ? enabledImage : disabledImage;
PPoint pos = rect.Origin() + (rect.Dimensions() - image.GetDimensions())/2;
canvas.DrawImgIcon(pos, image);
if (hasFocus)
canvas.DrawFocusRect(rect);
}
#endif
//////////////////////////////////////////////////////////////////////////////
// PCheck3WayBox
#if defined(_PCHECK3WAYBOX)
PCheck3WayBox::PCheck3WayBox(PInteractor * parent, CheckValues)
: PNamedControl(parent)
{
Construct();
}
PCheck3WayBox::PCheck3WayBox(PInteractor * parent,
const PString & name, CheckValues value)
: PNamedControl(parent)
{
Construct();
SetName(name);
SetValue(value);
DefaultDimensions();
}
PCheck3WayBox::PCheck3WayBox(PInteractor * parent,
const PString & name, const PNotifier & notify, CheckValues value)
: PNamedControl(parent)
{
Construct();
SetName(name);
SetValue(value);
SetNotifier(notify);
DefaultDimensions();
}
void PCheck3WayBox::DefaultDimensions()
{
PDrawCanvas canvas(this);
PDim dim = canvas.MeasureString(GetName());
dim.AddWidth(16); // Allow 4 font width left of text
dim.AddHeight(4); // Allow 1/4 font height above and below text
SetDimensions(dim, LocalCoords);
}
void PCheck3WayBox::TransferValue(int option)
{
if (valuePointer != NULL)
if (option == NotifyUpdate)
SetValue(*(CheckValues *)valuePointer);
else
*(CheckValues *)valuePointer = GetValue();
}
#endif
//////////////////////////////////////////////////////////////////////////////
// PCheckBox
#if defined(_PCHECKBOX)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -