more conversion to ustring. added more members

This commit is contained in:
Dana Jansens 2003-01-13 03:51:48 +00:00
parent 2aff07a250
commit 4947902d26
6 changed files with 178 additions and 115 deletions

View file

@ -94,9 +94,14 @@ void Font::drawString(XftDraw *d, int x, int y, const Color &color,
c.color.alpha = _tint | _tint << 8; // transparent shadow c.color.alpha = _tint | _tint << 8; // transparent shadow
c.pixel = BlackPixel(Display::display, _screen_num); c.pixel = BlackPixel(Display::display, _screen_num);
XftDrawStringUtf8(d, &c, _xftfont, x + _offset, if (string.utf8())
_xftfont->ascent + y + _offset, XftDrawStringUtf8(d, &c, _xftfont, x + _offset,
(FcChar8*)string.c_str(), string.size()); _xftfont->ascent + y + _offset,
(FcChar8*)string.c_str(), string.size());
else
XftDrawString8(d, &c, _xftfont, x + _offset,
_xftfont->ascent + y + _offset,
(FcChar8*)string.c_str(), string.size());
} }
XftColor c; XftColor c;
@ -106,8 +111,12 @@ void Font::drawString(XftDraw *d, int x, int y, const Color &color,
c.pixel = color.pixel(); c.pixel = color.pixel();
c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y, if (string.utf8())
(FcChar8*)string.c_str(), string.size()); XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y,
(FcChar8*)string.c_str(), string.size());
else
XftDrawString8(d, &c, _xftfont, x, _xftfont->ascent + y,
(FcChar8*)string.c_str(), string.size());
return; return;
} }
@ -117,8 +126,12 @@ unsigned int Font::measureString(const ustring &string) const
{ {
XGlyphInfo info; XGlyphInfo info;
XftTextExtentsUtf8(Display::display, _xftfont, if (string.utf8())
(FcChar8*)string.c_str(), string.size(), &info); XftTextExtentsUtf8(Display::display, _xftfont,
(FcChar8*)string.c_str(), string.size(), &info);
else
XftTextExtents8(Display::display, _xftfont,
(FcChar8*)string.c_str(), string.size(), &info);
return info.xOff + (_shadow ? _offset : 0); return info.xOff + (_shadow ? _offset : 0);
} }

View file

@ -217,17 +217,18 @@ void Property::set(Window win, Atoms atom, Atoms type,
* Set an string property value on a window. * Set an string property value on a window.
*/ */
void Property::set(Window win, Atoms atom, StringType type, void Property::set(Window win, Atoms atom, StringType type,
const std::string &value) const const ustring &value) const
{ {
assert(atom >= 0 && atom < NUM_ATOMS); assert(atom >= 0 && atom < NUM_ATOMS);
assert(type >= 0 && type < NUM_STRING_TYPE); assert(type >= 0 && type < NUM_STRING_TYPE);
Atom t; Atom t;
switch (type) { switch (type) {
case ascii: t = _atoms[Atom_String]; break; case ascii: t = _atoms[Atom_String]; assert(!value.utf8()); break;
case utf8: t = _atoms[Atom_Utf8]; break; case utf8: t = _atoms[Atom_Utf8]; assert(value.utf8()); break;
default: assert(False); return; // unhandled StringType default: assert(False); return; // unhandled StringType
} }
set(win, _atoms[atom], t, set(win, _atoms[atom], t,
reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())), reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
8, value.size() + 1, False); // add 1 to the size to include the null 8, value.size() + 1, False); // add 1 to the size to include the null
@ -244,18 +245,22 @@ void Property::set(Window win, Atoms atom, StringType type,
assert(type >= 0 && type < NUM_STRING_TYPE); assert(type >= 0 && type < NUM_STRING_TYPE);
Atom t; Atom t;
bool u; // utf8 encoded?
switch (type) { switch (type) {
case ascii: t = _atoms[Atom_String]; break; case ascii: t = _atoms[Atom_String]; u = false; break;
case utf8: t = _atoms[Atom_Utf8]; break; case utf8: t = _atoms[Atom_Utf8]; u = true; break;
default: assert(False); return; // unhandled StringType default: assert(False); return; // unhandled StringType
} }
std::string value; ustring value;
StringVect::const_iterator it = strings.begin(); StringVect::const_iterator it = strings.begin();
const StringVect::const_iterator end = strings.end(); const StringVect::const_iterator end = strings.end();
for (; it != end; ++it) for (; it != end; ++it) {
value += *it + '\0'; assert(it->utf8() == u); // the ustring is encoded correctly?
value += *it;
value += '\0';
}
set(win, _atoms[atom], t, set(win, _atoms[atom], t,
reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())), reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
@ -363,10 +368,11 @@ bool Property::get(Window win, Atoms atom, Atoms type,
* Gets an string property's value from a window. * Gets an string property's value from a window.
*/ */
bool Property::get(Window win, Atoms atom, StringType type, bool Property::get(Window win, Atoms atom, StringType type,
std::string *value) const ustring *value) const
{ {
unsigned long n = 1; unsigned long n = 1;
StringVect s; StringVect s;
if (get(win, atom, type, &n, &s)) { if (get(win, atom, type, &n, &s)) {
*value = s[0]; *value = s[0];
return True; return True;
@ -384,9 +390,10 @@ bool Property::get(Window win, Atoms atom, StringType type,
assert(*nelements > 0); assert(*nelements > 0);
Atom t; Atom t;
bool u; // utf8 encoded?
switch (type) { switch (type) {
case ascii: t = _atoms[Atom_String]; break; case ascii: t = _atoms[Atom_String]; u = false; break;
case utf8: t = _atoms[Atom_Utf8]; break; case utf8: t = _atoms[Atom_Utf8]; u = true; break;
default: assert(False); return False; // unhandled StringType default: assert(False); return False; // unhandled StringType
} }
@ -404,6 +411,7 @@ bool Property::get(Window win, Atoms atom, StringType type,
std::string::const_iterator tmp = it; // current string.begin() std::string::const_iterator tmp = it; // current string.begin()
it = std::find(tmp, end, '\0'); // look for null between tmp and end it = std::find(tmp, end, '\0'); // look for null between tmp and end
strings->push_back(std::string(tmp, it)); // s[tmp:it) strings->push_back(std::string(tmp, it)); // s[tmp:it)
if (!u) strings->back().setUtf8(false);
++num; ++num;
if (it == end) break; if (it == end) break;
++it; ++it;

View file

@ -6,6 +6,9 @@
@brief Provides access to window properties @brief Provides access to window properties
*/ */
#include "ustring.hh"
#include "screeninfo.hh"
extern "C" { extern "C" {
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
@ -14,9 +17,6 @@ extern "C" {
} }
#include <vector> #include <vector>
#include <string>
#include "screeninfo.hh"
namespace otk { namespace otk {
@ -176,7 +176,7 @@ private:
public: public:
//! A list of strings //! A list of strings
typedef std::vector<std::string> StringVect; typedef std::vector<ustring> StringVect;
//! Constructs a new Atom object //! Constructs a new Atom object
/*! /*!
@ -220,7 +220,7 @@ public:
@param value The string to set the property to @param value The string to set the property to
*/ */
void set(Window win, Atoms atom, StringType type, void set(Window win, Atoms atom, StringType type,
const std::string &value) const; const ustring &value) const;
//! Sets a string-array property on a window to a new value //! Sets a string-array property on a window to a new value
/*! /*!
@param win The window id of the window on which to set the property's value @param win The window id of the window on which to set the property's value
@ -284,7 +284,7 @@ public:
@return true if retrieval of the specified property with the specified @return true if retrieval of the specified property with the specified
type was successful; otherwise, false type was successful; otherwise, false
*/ */
bool get(Window win, Atoms atom, StringType type, std::string *value) const; bool get(Window win, Atoms atom, StringType type, ustring *value) const;
//! Gets strings from the value of a property on a window //! Gets strings from the value of a property on a window
/*! /*!
@param win The window id of the window to get the property value from @param win The window id of the window to get the property value from

View file

@ -21,27 +21,47 @@ ustring::~ustring()
} }
ustring::ustring(const ustring& other) ustring::ustring(const ustring& other)
: _string(other._string) : _string(other._string), _utf8(other._utf8)
{ {
} }
ustring& ustring::operator=(const ustring& other) ustring& ustring::operator=(const ustring& other)
{ {
_string = other._string; _string = other._string;
_utf8 = other._utf8;
return *this; return *this;
} }
ustring::ustring(const std::string& src) ustring::ustring(const std::string& src)
: _string(src) : _string(src), _utf8(true)
{ {
} }
ustring::ustring(const char* src) ustring::ustring(const char* src)
: _string(src) : _string(src), _utf8(true)
{ {
} }
static ustring::size_type find_offset(const char *str, const char *pos) ustring& ustring::operator+=(const ustring& src)
{
assert(_utf8 == src._utf8);
_string += src._string;
return *this;
}
ustring& ustring::operator+=(const char* src)
{
_string += src;
return *this;
}
ustring& ustring::operator+=(char c)
{
_string += c;
return *this;
}
static ustring::size_type find_utf8_offset(const char *str, const char *pos)
{ {
ustring::size_type offset = 0; ustring::size_type offset = 0;
@ -55,14 +75,11 @@ static ustring::size_type find_offset(const char *str, const char *pos)
ustring::size_type ustring::size() const ustring::size_type ustring::size() const
{ {
const char *const pdata = _string.data(); if (_utf8) {
return find_offset(pdata, pdata + _string.size()); const char *const pdata = _string.data();
} return find_utf8_offset(pdata, pdata + _string.size());
} else
ustring::size_type ustring::length() const return _string.size();
{
const char *const pdata = _string.data();
return find_offset(pdata, pdata + _string.size());
} }
ustring::size_type ustring::bytes() const ustring::size_type ustring::bytes() const
@ -91,4 +108,14 @@ const char* ustring::c_str() const
return _string.c_str(); return _string.c_str();
} }
bool ustring::utf8() const
{
return _utf8;
}
void ustring::setUtf8(bool utf8)
{
_utf8 = utf8;
}
} }

View file

@ -106,8 +106,9 @@ private:
#endif // DOXYGEN_IGNORE #endif // DOXYGEN_IGNORE
//! This class provides a simple wrapper to a std::string that is encoded as //! This class provides a simple wrapper to a std::string that can be encoded
//! UTF-8. //! as UTF-8. The ustring::utf() member specifies if the given string is UTF-8
//! encoded. ustrings default to specifying UTF-8 encoding.
/*! /*!
This class does <b>not</b> handle extended 8-bit ASCII charsets like This class does <b>not</b> handle extended 8-bit ASCII charsets like
ISO-8859-1. ISO-8859-1.
@ -120,7 +121,8 @@ private:
*/ */
class ustring { class ustring {
std::string _string; std::string _string;
bool _utf8;
public: public:
typedef std::string::size_type size_type; typedef std::string::size_type size_type;
typedef std::string::difference_type difference_type; typedef std::string::difference_type difference_type;
@ -144,10 +146,15 @@ public:
ustring(const std::string& src); ustring(const std::string& src);
ustring::ustring(const char* src); ustring::ustring(const char* src);
// append to the string
ustring& operator+=(const ustring& src);
ustring& operator+=(const char* src);
ustring& operator+=(char c);
// sizes // sizes
ustring::size_type size() const; ustring::size_type size() const;
ustring::size_type length() const;
ustring::size_type bytes() const; ustring::size_type bytes() const;
ustring::size_type capacity() const; ustring::size_type capacity() const;
ustring::size_type max_size() const; ustring::size_type max_size() const;
@ -156,7 +163,11 @@ public:
const char* data() const; const char* data() const;
const char* c_str() const; const char* c_str() const;
// encoding
bool utf8() const;
void setUtf8(bool utf8);
}; };
} }

View file

@ -677,36 +677,37 @@ SWIG_InstallConstants(PyObject *d, swig_const_info constants[]) {
#define SWIGTYPE_p_XCirculateEvent swig_types[29] #define SWIGTYPE_p_XCirculateEvent swig_types[29]
#define SWIGTYPE_p_XRectangle swig_types[30] #define SWIGTYPE_p_XRectangle swig_types[30]
#define SWIGTYPE_p_std__string swig_types[31] #define SWIGTYPE_p_std__string swig_types[31]
#define SWIGTYPE_p_XCrossingEvent swig_types[32] #define SWIGTYPE_p_ustring swig_types[32]
#define SWIGTYPE_p_Display swig_types[33] #define SWIGTYPE_p_XCrossingEvent swig_types[33]
#define SWIGTYPE_p_otk__Display swig_types[34] #define SWIGTYPE_p_Display swig_types[34]
#define SWIGTYPE_p_XMappingEvent swig_types[35] #define SWIGTYPE_p_otk__Display swig_types[35]
#define SWIGTYPE_p_otk__Style swig_types[36] #define SWIGTYPE_p_XMappingEvent swig_types[36]
#define SWIGTYPE_p_otk__EventHandler swig_types[37] #define SWIGTYPE_p_otk__Style swig_types[37]
#define SWIGTYPE_p_XReparentEvent swig_types[38] #define SWIGTYPE_p_otk__EventHandler swig_types[38]
#define SWIGTYPE_p_otk__EventDispatcher swig_types[39] #define SWIGTYPE_p_XReparentEvent swig_types[39]
#define SWIGTYPE_p_otk__GCCache swig_types[40] #define SWIGTYPE_p_otk__EventDispatcher swig_types[40]
#define SWIGTYPE_p_ob__Bindings swig_types[41] #define SWIGTYPE_p_otk__GCCache swig_types[41]
#define SWIGTYPE_p_ob__Openbox swig_types[42] #define SWIGTYPE_p_ob__Bindings swig_types[42]
#define SWIGTYPE_p_ob__Actions swig_types[43] #define SWIGTYPE_p_ob__Openbox swig_types[43]
#define SWIGTYPE_p_XEvent swig_types[44] #define SWIGTYPE_p_ob__Actions swig_types[44]
#define SWIGTYPE_p_otk__Property swig_types[45] #define SWIGTYPE_p_XEvent swig_types[45]
#define SWIGTYPE_p_PyObject swig_types[46] #define SWIGTYPE_p_otk__Property swig_types[46]
#define SWIGTYPE_p_otk__ScreenInfo swig_types[47] #define SWIGTYPE_p_PyObject swig_types[47]
#define SWIGTYPE_p_ob__EventData swig_types[48] #define SWIGTYPE_p_otk__ScreenInfo swig_types[48]
#define SWIGTYPE_p_XCreateWindowEvent swig_types[49] #define SWIGTYPE_p_ob__EventData swig_types[49]
#define SWIGTYPE_p_XDestroyWindowEvent swig_types[50] #define SWIGTYPE_p_XCreateWindowEvent swig_types[50]
#define SWIGTYPE_p_otk__Property__StringVect swig_types[51] #define SWIGTYPE_p_XDestroyWindowEvent swig_types[51]
#define SWIGTYPE_p_ob__WidgetBase swig_types[52] #define SWIGTYPE_p_otk__Property__StringVect swig_types[52]
#define SWIGTYPE_p_XKeyEvent swig_types[53] #define SWIGTYPE_p_ob__WidgetBase swig_types[53]
#define SWIGTYPE_p_otk__Strut swig_types[54] #define SWIGTYPE_p_XKeyEvent swig_types[54]
#define SWIGTYPE_p_unsigned_long swig_types[55] #define SWIGTYPE_p_otk__Strut swig_types[55]
#define SWIGTYPE_p_p_unsigned_long swig_types[56] #define SWIGTYPE_p_unsigned_long swig_types[56]
#define SWIGTYPE_p_XMotionEvent swig_types[57] #define SWIGTYPE_p_p_unsigned_long swig_types[57]
#define SWIGTYPE_p_XButtonEvent swig_types[58] #define SWIGTYPE_p_XMotionEvent swig_types[58]
#define SWIGTYPE_p_XSelectionEvent swig_types[59] #define SWIGTYPE_p_XButtonEvent swig_types[59]
#define SWIGTYPE_p_otk__TimerQueueManager swig_types[60] #define SWIGTYPE_p_XSelectionEvent swig_types[60]
static swig_type_info *swig_types[62]; #define SWIGTYPE_p_otk__TimerQueueManager swig_types[61]
static swig_type_info *swig_types[63];
/* -------- TYPES TABLE (END) -------- */ /* -------- TYPES TABLE (END) -------- */
@ -1505,8 +1506,7 @@ static PyObject *_wrap_Property_set__SWIG_2(PyObject *self, PyObject *args) {
Window arg2 ; Window arg2 ;
int arg3 ; int arg3 ;
int arg4 ; int arg4 ;
std::string *arg5 = 0 ; ustring *arg5 = 0 ;
std::string temp5 ;
PyObject * obj0 = 0 ; PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ; PyObject * obj1 = 0 ;
PyObject * obj4 = 0 ; PyObject * obj4 = 0 ;
@ -1515,15 +1515,11 @@ static PyObject *_wrap_Property_set__SWIG_2(PyObject *self, PyObject *args) {
if ((SWIG_ConvertPtr(obj0,(void **) &arg1, SWIGTYPE_p_otk__Property,SWIG_POINTER_EXCEPTION | 0 )) == -1) SWIG_fail; if ((SWIG_ConvertPtr(obj0,(void **) &arg1, SWIGTYPE_p_otk__Property,SWIG_POINTER_EXCEPTION | 0 )) == -1) SWIG_fail;
arg2 = (Window) PyInt_AsLong(obj1); arg2 = (Window) PyInt_AsLong(obj1);
if (PyErr_Occurred()) SWIG_fail; if (PyErr_Occurred()) SWIG_fail;
{ if ((SWIG_ConvertPtr(obj4,(void **) &arg5, SWIGTYPE_p_ustring,SWIG_POINTER_EXCEPTION | 0 )) == -1) SWIG_fail;
if (PyString_Check(obj4)) { if (arg5 == NULL) {
temp5 = std::string(PyString_AsString(obj4)); PyErr_SetString(PyExc_TypeError,"null reference"); SWIG_fail;
arg5 = &temp5;
}else {
SWIG_exception(SWIG_TypeError, "string expected");
}
} }
((otk::Property const *)arg1)->set(arg2,(otk::Property::Atoms )arg3,(otk::Property::StringType )arg4,(std::string const &)*arg5); ((otk::Property const *)arg1)->set(arg2,(otk::Property::Atoms )arg3,(otk::Property::StringType )arg4,(ustring const &)*arg5);
Py_INCREF(Py_None); resultobj = Py_None; Py_INCREF(Py_None); resultobj = Py_None;
return resultobj; return resultobj;
@ -1569,6 +1565,47 @@ static PyObject *_wrap_Property_set(PyObject *self, PyObject *args) {
for (ii = 0; (ii < argc) && (ii < 6); ii++) { for (ii = 0; (ii < argc) && (ii < 6); ii++) {
argv[ii] = PyTuple_GetItem(args,ii); argv[ii] = PyTuple_GetItem(args,ii);
} }
if (argc == 5) {
int _v;
{
void *ptr;
if (SWIG_ConvertPtr(argv[0], (void **) &ptr, SWIGTYPE_p_otk__Property, 0) == -1) {
_v = 0;
PyErr_Clear();
}else {
_v = 1;
}
}
if (_v) {
{
_v = (PyInt_Check(argv[1]) || PyLong_Check(argv[1])) ? 1 : 0;
}
if (_v) {
{
_v = (PyInt_Check(argv[2]) || PyLong_Check(argv[2])) ? 1 : 0;
}
if (_v) {
{
_v = (PyInt_Check(argv[3]) || PyLong_Check(argv[3])) ? 1 : 0;
}
if (_v) {
{
void *ptr;
if (SWIG_ConvertPtr(argv[4], (void **) &ptr, SWIGTYPE_p_ustring, 0) == -1) {
_v = 0;
PyErr_Clear();
}else {
_v = 1;
}
}
if (_v) {
return _wrap_Property_set__SWIG_2(self,args);
}
}
}
}
}
}
if (argc == 5) { if (argc == 5) {
int _v; int _v;
{ {
@ -1645,41 +1682,6 @@ static PyObject *_wrap_Property_set(PyObject *self, PyObject *args) {
} }
} }
} }
if (argc == 5) {
int _v;
{
void *ptr;
if (SWIG_ConvertPtr(argv[0], (void **) &ptr, SWIGTYPE_p_otk__Property, 0) == -1) {
_v = 0;
PyErr_Clear();
}else {
_v = 1;
}
}
if (_v) {
{
_v = (PyInt_Check(argv[1]) || PyLong_Check(argv[1])) ? 1 : 0;
}
if (_v) {
{
_v = (PyInt_Check(argv[2]) || PyLong_Check(argv[2])) ? 1 : 0;
}
if (_v) {
{
_v = (PyInt_Check(argv[3]) || PyLong_Check(argv[3])) ? 1 : 0;
}
if (_v) {
{
_v = PyString_Check(argv[4]) ? 1 : 0;
}
if (_v) {
return _wrap_Property_set__SWIG_2(self,args);
}
}
}
}
}
}
if (argc == 6) { if (argc == 6) {
int _v; int _v;
{ {
@ -8251,6 +8253,7 @@ static swig_type_info _swigt__p_XConfigureEvent[] = {{"_p_XConfigureEvent", 0, "
static swig_type_info _swigt__p_XCirculateEvent[] = {{"_p_XCirculateEvent", 0, "XCirculateEvent *", 0},{"_p_XCirculateEvent"},{0}}; static swig_type_info _swigt__p_XCirculateEvent[] = {{"_p_XCirculateEvent", 0, "XCirculateEvent *", 0},{"_p_XCirculateEvent"},{0}};
static swig_type_info _swigt__p_XRectangle[] = {{"_p_XRectangle", 0, "XRectangle *", 0},{"_p_XRectangle"},{0}}; static swig_type_info _swigt__p_XRectangle[] = {{"_p_XRectangle", 0, "XRectangle *", 0},{"_p_XRectangle"},{0}};
static swig_type_info _swigt__p_std__string[] = {{"_p_std__string", 0, "std::string *", 0},{"_p_std__string"},{0}}; static swig_type_info _swigt__p_std__string[] = {{"_p_std__string", 0, "std::string *", 0},{"_p_std__string"},{0}};
static swig_type_info _swigt__p_ustring[] = {{"_p_ustring", 0, "ustring *", 0},{"_p_ustring"},{0}};
static swig_type_info _swigt__p_XCrossingEvent[] = {{"_p_XCrossingEvent", 0, "XCrossingEvent *", 0},{"_p_XCrossingEvent"},{0}}; static swig_type_info _swigt__p_XCrossingEvent[] = {{"_p_XCrossingEvent", 0, "XCrossingEvent *", 0},{"_p_XCrossingEvent"},{0}};
static swig_type_info _swigt__p_Display[] = {{"_p_Display", 0, "Display *", 0},{"_p_Display"},{0}}; static swig_type_info _swigt__p_Display[] = {{"_p_Display", 0, "Display *", 0},{"_p_Display"},{0}};
static swig_type_info _swigt__p_otk__Display[] = {{"_p_otk__Display", 0, "otk::Display *", 0},{"_p_otk__Display"},{0}}; static swig_type_info _swigt__p_otk__Display[] = {{"_p_otk__Display", 0, "otk::Display *", 0},{"_p_otk__Display"},{0}};
@ -8314,6 +8317,7 @@ _swigt__p_XConfigureEvent,
_swigt__p_XCirculateEvent, _swigt__p_XCirculateEvent,
_swigt__p_XRectangle, _swigt__p_XRectangle,
_swigt__p_std__string, _swigt__p_std__string,
_swigt__p_ustring,
_swigt__p_XCrossingEvent, _swigt__p_XCrossingEvent,
_swigt__p_Display, _swigt__p_Display,
_swigt__p_otk__Display, _swigt__p_otk__Display,