> Aside from this initial validation. do the Fl_Input types require
> manual validation for the amount of characters entered and/or range
> for ints or floats? or again is there a native feature for this?
Don't forget that you're working within an Object Oriented framework
so you can always create a subclass and do the extra work there.
For what it's worth, which isn't that much, here's a class that I
made a few years ago that checks an int value against a range. My
coding conventions have changed somewhat since then too. The class
should be standalone, but I haven't compiled it recently...
Cheers
D.
/* ValidIntInput.h =============================== */
#ifndef ValidIntInput_h
#define ValidIntInput_h 1
#include <FL/Fl_Int_Input.H>
class ValidIntInput : public Fl_Int_Input
{
public:
ValidIntInput(int X, int Y, int W, int H, const char* T=0);
bool IsValid();
void SetMinimum(int minimum, bool includeMinimum=true);
void SetMaximum(int maximum, bool includeMaximum=true);
void SetValue(const int i);
int GetValue();
private:
void OutOfRangeAlert(const char* t);
bool m_includeMinimum;
bool m_includeMaximum;
int m_minimum;
int m_maximum;
};
#endif // !ValidIntInput_h
/* ValidIntInput.cxx ============================= */
#include "ValidIntInput.h"
#include <FL/fl_ask.H>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
ValidIntInput::ValidIntInput(int X, int Y, int W, int H, const char* T)
: Fl_Int_Input(X, Y, W, H, T)
{
m_minimum = INT_MIN;
m_includeMinimum = false;
m_maximum = INT_MAX;
m_includeMaximum = false;
}
static char* trimString(char* s, char* t)
{
while (*s != '\0' && isspace(*s))
{
s++;
}
strcpy(s, t);
size_t len = strlen(t);
while (len > 0 && isspace(t[len-1]))
{
len -= 1;
t[len] = '\0';
}
return(t);
}
static void Format(char* s, int i)
{
char buffer[128];
sprintf(buffer, "%d", i);
strcpy(s, buffer);
}
void
ValidIntInput::OutOfRangeAlert(const char* s)
{
// first we [over]estimate the size of the buffer needed
char* format = "Error:\n\"%s\" is not an integer in the range [%s %s x
%s %s]";
char minString[128];
Format(minString, m_minimum);
char maxString[128];
Format(maxString, m_maximum);
const char* lte = (m_includeMinimum == true) ? "<=" : "<";
const char* gte = (m_includeMaximum == true) ? "<=" : "<";
size_t length = strlen(format) + strlen(s) +
strlen(minString) + strlen(lte) +
strlen(gte) + strlen(maxString);
char* buffer = (char*)malloc((length+1)*sizeof(char));
assert(buffer != 0);
sprintf(buffer, format, s, minString, lte, gte, maxString);
fl_alert(buffer);
free((void*)buffer);
}
bool
ValidIntInput::IsValid()
{
bool result = true;
char* s = strdup(value()); assert(s != 0);
char* t = strdup(value()); assert(t != 0);
trimString(s, t);
size_t length;
int newValue;
if (sscanf(t, "%d%n", &newValue, &length) != 1 ||
length != strlen(t))
{
result = false;
}
if (m_includeMinimum)
{
if (newValue < m_minimum)
{
result = false;
}
} else
{
if (newValue <= m_minimum)
{
result = false;
}
}
if (m_includeMaximum)
{
if (newValue > m_maximum)
{
result = false;
}
} else
{
if (newValue >= m_maximum)
{
result = false;
}
}
if (result == false)
{
OutOfRangeAlert(t);
}
free(t);
free(s);
return(result);
}
void
ValidIntInput::SetMinimum(int minimum, bool includeMinimum)
{
m_minimum = minimum;
m_includeMinimum = includeMinimum;
}
void
ValidIntInput::SetMaximum(int maximum, bool includeMaximum)
{
m_maximum = maximum;
m_includeMaximum = includeMaximum;
}
void
ValidIntInput::SetValue(const int i)
{
if (strcmp(value(), "") == 0 || i != GetValue())
{
char buffer[128];
Format(buffer, i);
value(buffer);
}
}
int
ValidIntInput::GetValue()
{
const char* p = value();
int newValue = atoi(p);
return(newValue);
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk