Re: TreeView::append_column_numeric() format?
- From: Carl Nygard <cjnygard fast net>
- To: Murray Cumming <murrayc murrayc com>
- Cc: gtkmm-list <gtkmm-list gnome org>
- Subject: Re: TreeView::append_column_numeric() format?
- Date: Tue, 07 Dec 2004 17:56:39 -0500
Apologies for spamming with code, but here's a concrete working example
from the gtklink/cast libs. The point of the classes is to update a
variable when the widget changes, or to update the widget to reflect a
new value.
The class uses a parameterized Translator object to do the translation,
effectively allowing any type of variable to be represented (so long as
it can be (de)string-ified).
I'm not sure how this fits specifically with the treemodel code, so I'll
take a look at that next.
But this is what I'm thinking about when I do all my hand-waving...
Regards,
Carl
On Mon, 2004-12-06 at 17:27, Murray Cumming wrote:
> On Mon, 2004-12-06 at 16:48 -0500, Carl Nygard wrote:
> > On Mon, 2004-12-06 at 13:52, Murray Cumming wrote:
> > > On Tue, 2004-11-23 at 10:23 +0100, Murray Cumming wrote:
> >
> > > > I think I'll allow a sprintf-style format string,
> > >
> > > I've done that now, in gtkmm 2.5. When parsing data in editable cells,
> > > it only understand decimal numbers, but I don't think that's a huge
> > > disadvantage.
> > >
> > > I'd welcome any criticism.
> >
> > The problem with relying on purely sprintf-style formatting is you're
> > missing the localization provided by the streams classes (unless *I'm*
> > missing something).
>
> Yes, the streams classes seem to be better at parsing times and dates. I
> use them in Glom. But
> 1. I think that localization with sprintf()-formatting works OK. It
> works for C, for instance. Do you have an example of what doesn't work.
> 2. We don't handle date and time types yet. That's another issue.
>
> > And if you want to parse the numbers, again streams would be the logical
> > choice in C++.
> >
> > That's why I was suggesting something akin to the lexical_cast<>
> > templates I have available. They are easily extensible to include input
> > as well as output, so would provide a natural bridge between text
> > representation and float/complex/etc. data values, and even extend to
> > other data types like dates, money, etc.
>
> Maybe you've shown me a simple example of how method would look, but I
> can't find it now.
>
> > I use something similar in the libsimple/libgtklink libraries (also
> > available, I believe) which provide simplified interface/link between
> > data vars and widgets. In particular, I have a translation class used
> > with the entry fields to provide translation to/from the Gtk::Text
> > widget.
// -*- c++ -*-
// $RCSfile:$
// fieldwidget.hxx
//
// Copyright (C) 2004 Spatial Software Solutions, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef GTKLINK_FIELDWIDGET
#define GTKLINK_FIELDWIDGET
////////////////////////////////////////////////////////////////////
//- Includes
#include <gtkmm/entry.h>
#ifndef SIMPLEWIDGET_GENERICWIDG
#include <simplewidget/genericwidg.hxx>
#endif // SIMPLEWIDGET_GENERICWIDG
#ifndef GTKLINK_TRANSLATOR
#include <gtklink/translator.hxx>
#endif // GTKLINK_TRANSLATOR
////////////////////////////////////////////////////////////////////
//- Class Definition
namespace GtkLink {
//: Gtk field widget for int data
template <typename T, typename Trans = Translator<T> >
class FieldWidget : public SimpleWidget::GenericWidg<T> {
public:
//- Types
typedef Gtk::Entry GtkWidgetType;
typedef typename SimpleWidget::GenericWidg<T>::ProxyType ProxyType;
//: Constructor
FieldWidget(const ProxyType& proxy, GtkWidgetType* widg,
const Trans& trans = Trans()):
SimpleWidget::GenericWidg<T>(proxy),
_entry(widg),
_translator(trans)
{
_entry->signal_focus_out_event().
connect(SigC::slot(*this,
&FieldWidget::on_focus_out_event));
}
void SetSensitive(bool flag) {
_entry->set_sensitive(flag);
}
void UpdateData() {
Set(_translator(_entry->get_text()));
}
void UpdateWidget() {
_entry->set_text(_translator(Get()));
}
protected:
//- Types
//- Constants
//- Variables
GtkWidgetType* _entry;
Trans _translator;
//- Routines
bool on_focus_out_event(GdkEventFocus* p){
UpdateData();
return true;
}
};
} // namespace
#endif // GTKLINK_INTFIELDWIDGET
// -*- c++ -*-
// $RCSfile:$
// translator.hxx
//
// Copyright (C) 2004 Spatial Software Solutions, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef GTKLINK_TRANSLATOR
#define GTKLINK_TRANSLATOR
////////////////////////////////////////////////////////////////////
//- Includes
#include <string>
#ifndef CAST_LEXICALCAST
#include <cast/lexical_cast.hxx>
#endif // CAST_LEXICALCAST
////////////////////////////////////////////////////////////////////
//- Class Definition
namespace GtkLink {
template <typename T>
struct Translator {
std::string operator()(const T& val) const {
return Cast::lexical_cast<T>(val);
}
T operator()(const std::string& str) const {
return Cast::string_extract<T>(str);
}
};
template <>
struct Translator<std::string> {
std::string operator()(const std::string& val) const {
return val;
}
};
} // namespace
#endif // GTKLINK_TRANSLATOR
// -*- c++ -*-
// $RCSfile:$
// lexical_cast.hxx
//
// Copyright (C) 2004 Spatial Software Solutions, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef CAST_LEXICALCAST
#define CAST_LEXICALCAST
////////////////////////////////////////////////////////////////////
// Includes
#include <string>
#include <strstream>
#include <iomanip>
#include <iostream>
////////////////////////////////////////////////////////////////////
// Class Definition
namespace Cast {
struct cast_policy_none {
void Init(std::ostream& os) const {}
void Cleanup(std::ostream& os) const {}
};
struct cast_policy_hex {
void Init(std::ostream& os) const { os << std::hex;}
void Cleanup(std::ostream& os) const { os << std::dec; }
};
struct cast_policy_quoted {
cast_policy_quoted(char q = '\'') : quote(q) {}
void Init(std::ostream& os) const { os << quote; }
void Cleanup(std::ostream& os) const { os << quote; }
char quote;
};
struct cast_policy_leadzero {
void Init(std::ostream& os) const {
os << std::setfill('0');
}
void Cleanup(std::ostream& os) const {}
};
template <int N>
struct cast_policy_width {
void Init(std::ostream& os) const {
os << std::setw(N);
}
void Cleanup(std::ostream& os) const {}
};
template<typename T, typename U>
struct cast_policy_bind {
cast_policy_bind(const T& t = T(), const U& u = U()) :
first(t), second(u)
{}
void Init(std::ostream& os) const {
first.Init(os);
second.Init(os);
}
void Cleanup(std::ostream& os) const {
second.Cleanup(os);
first.Cleanup(os);
}
T first;
U second;
};
template <typename T, typename U>
cast_policy_bind<T,U> policy_binder(const T& t, const U& u)
{ return cast_policy_bind<T,U>(t, u); }
template <typename T>
std::string lexical_cast(const T& obj)
{
std::ostrstream str;
str << obj << std::ends;
char* buf(str.str());
std::string result(buf);
delete[] buf;
return result;
}
template <typename T, typename P>
std::string lexical_cast(const T& obj, const P& policy = P())
{
std::ostrstream str;
policy.Init(str);
str << obj;
policy.Cleanup(str);
str << std::ends;
char* buf(str.str());
std::string result(buf);
delete[] buf;
return result;
}
std::string lexical_cast(const bool& obj);
template <typename P>
std::string lexical_cast(const bool& obj, const P& policy)
{
std::ostrstream str;
policy.Init(str);
str << (obj ? "YES" : "NO");
policy.Cleanup(str);
str << std::ends;
char* buf(str.str());
std::string result(buf);
delete[] buf;
return result;
}
template <typename T>
std::string upper_cast(const T& obj)
{
std::ostrstream str;
// str << uppercase << obj << std::ends;
str << obj << std::ends;
char* buf(str.str());
// Because uppercase doesn't seem to work
{
for(char* it = buf; *it; ++it){
if((*it <= 'z') && (*it >= 'a'))
*it += 'A' - 'a';
}
}
std::string result(buf);
delete[] buf;
return result;
}
template <typename T>
T string_extract(const std::string& str)
{
std::istrstream s(str.c_str(), str.length());
T result;
s >> result;
return result;
}
} // namespace
#endif // CAST_LEXICALCAST
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]