]> Dogcows Code - chaz/openbox/commitdiff
otk using ustring for shit that draws. expended its inferface yet some more.
authorDana Jansens <danakj@orodu.net>
Mon, 13 Jan 2003 04:46:50 +0000 (04:46 +0000)
committerDana Jansens <danakj@orodu.net>
Mon, 13 Jan 2003 04:46:50 +0000 (04:46 +0000)
otk/focuslabel.cc
otk/focuslabel.hh
otk/label.cc
otk/label.hh
otk/ustring.cc
otk/ustring.hh

index 397a2cac8928b0e113b37fb2b159c8a47980e644..79c1507f43ae7ad25caeb34346d2b6857a34dfeb 100644 (file)
@@ -41,7 +41,7 @@ void FocusLabel::update(void)
                           : style()->getTextUnfocus());
     unsigned int sidemargin = style()->getBevelWidth() * 2;
 
-    std::string t = _text; // the actual text to draw
+    ustring t = _text; // the actual text to draw
     int x = sidemargin;    // x coord for the text
 
     // find a string that will fit inside the area for text
index 54a7f06f078ba177e6291c6fab17db95b26ba6d3..ea9f9b66e74f3561223798a5bc6b80d0e84af26c 100644 (file)
@@ -14,8 +14,8 @@ public:
   FocusLabel(Widget *parent);
   ~FocusLabel();
 
-  inline const std::string &getText(void) const { return _text; }
-  void setText(const std::string &text) { _text = text; _dirty = true; }
+  inline const ustring &getText(void) const { return _text; }
+  void setText(const ustring &text) { _text = text; _dirty = true; }
 
   void update(void);
 
@@ -25,7 +25,7 @@ private:
   //! Object used by Xft to render to the drawable
   XftDraw *_xftdraw;
   //! Text displayed in the label
-  std::string _text;
+  ustring _text;
 };
 
 }
index 6b6a8e358424406a34a2ca95678f501cbfeeb1ad..05ed6cfa13c9e3975d643828cd018deb311e9210 100644 (file)
@@ -35,7 +35,7 @@ void Label::update(void)
     const Font *ft = style()->getFont();
     unsigned int sidemargin = style()->getBevelWidth() * 2;
 
-    std::string t = _text; // the actual text to draw
+    ustring t = _text; // the actual text to draw
     int x = sidemargin;    // x coord for the text
 
     // find a string that will fit inside the area for text
index 0366c262c7195285abc1b6acc5090c331b061d65..ad938f102e70f7ef72460062cfa1536099853fdf 100644 (file)
@@ -14,8 +14,8 @@ public:
   Label(Widget *parent);
   ~Label();
 
-  inline const std::string &getText(void) const { return _text; }
-  void setText(const std::string &text) { _text = text; _dirty = true; }
+  inline const ustring &getText(void) const { return _text; }
+  void setText(const ustring &text) { _text = text; _dirty = true; }
 
   void update(void);
 
@@ -25,7 +25,7 @@ private:
   //! Object used by Xft to render to the drawable
   XftDraw *_xftdraw;
   //! Text displayed in the label
-  std::string _text;
+  ustring _text;
 };
 
 }
index a04c08cff69496e91c5f547cd6423372d03649e7..8f3cdfc452fae6bfdc2c4af2f716da9c769ff9d0 100644 (file)
@@ -12,6 +12,63 @@ extern "C" {
 
 namespace otk {
 
+// helper functions
+
+static ustring::size_type utf8_find_offset(const char *str, const char *pos)
+{
+  ustring::size_type offset = 0;
+
+  while (str < pos) {
+    str += g_utf8_skip[*str];
+    offset += g_utf8_skip[*str];
+  }
+
+  return offset;
+}
+
+// First overload: stop on '\0' character.
+ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset)
+{
+  if(offset == ustring::npos)
+    return ustring::npos;
+
+  const char* p = str;
+
+  for(; offset != 0; --offset)
+  {
+    if(*p == '\0')
+      return ustring::npos;
+
+    p += g_utf8_skip[*p];
+  }
+
+  return (p - str);
+}
+
+// Second overload: stop when reaching maxlen.
+ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset,
+                                   ustring::size_type maxlen)
+{
+  if(offset == ustring::npos)
+    return ustring::npos;
+
+  const char *const pend = str + maxlen;
+  const char* p = str;
+
+  for(; offset != 0; --offset)
+  {
+    if(p >= pend)
+      return ustring::npos;
+
+    p += g_utf8_skip[*p];
+  }
+
+  return (p - str);
+}
+
+
+// ustring methods
+
 ustring::ustring()
 {
 }
@@ -61,23 +118,11 @@ ustring& ustring::operator+=(char c)
   return *this;
 }
 
-static ustring::size_type find_utf8_offset(const char *str, const char *pos)
-{
-  ustring::size_type offset = 0;
-
-  while (str < pos) {
-    str += g_utf8_skip[*str];
-    offset += g_utf8_skip[*str];
-  }
-
-  return offset;
-}
-
 ustring::size_type ustring::size() const
 {
   if (_utf8) {
     const char *const pdata = _string.data();
-    return find_utf8_offset(pdata, pdata + _string.size());
+    return utf8_find_offset(pdata, pdata + _string.size());
   } else
     return _string.size();
 }
@@ -97,6 +142,39 @@ ustring::size_type ustring::max_size() const
   return _string.max_size();
 }
 
+void ustring::clear()
+{
+  _string.erase();
+}
+
+ustring& ustring::erase(ustring::size_type i, ustring::size_type n)
+{
+  if (_utf8) {
+    // find a proper offset
+    size_type utf_i = utf8_byte_offset(_string.c_str(), i);
+    if (utf_i != npos) {
+      // if the offset is not npos, find a proper length for 'n'
+      size_type utf_n = utf8_byte_offset(_string.data() + utf_i, n,
+                                        _string.size() - utf_i);
+      _string.erase(utf_i, utf_n);
+    }
+  } else
+    _string.erase(i, n);
+
+  return *this;
+}
+
+void ustring::resize(ustring::size_type n, char c)
+{
+  if (_utf8) {
+    const size_type size_now = size();
+    if(n < size_now)
+      erase(n, npos);
+    else if(n > size_now)
+      _string.append(n - size_now, c);
+  } else
+    _string.resize(n, c);
+}
 
 const char* ustring::data() const
 {
index 02111a3379e89dde26e1a90b444226d6a6435cda..ef94440cff746770ebbf0a68fe296c3ede7b4a79 100644 (file)
@@ -7,6 +7,7 @@
 */
 
 extern "C" {
+/*
 #ifdef HAVE_STDINT_H
 #  include <stdint.h>
 #else
@@ -14,17 +15,20 @@ extern "C" {
 #    include <sys/types.h>
 #  endif
 #endif
+*/
 }
 
 #include <string>
 
 namespace otk {
 
+/*
 #ifdef HAVE_STDINT_H
 typedef uint32_t unichar;
 #else
 typedef u_int32_t unichar;
 #endif
+*/
 
 #ifndef DOXYGEN_IGNORE
 
@@ -40,6 +44,8 @@ const char g_utf8_skip[256] = {
   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
 };
 
+#endif // DOXYGEN_IGNORE
+
 //! The iterator type for ustring
 /*!
   Note this is not a random access iterator but a bidirectional one, since all
@@ -51,20 +57,22 @@ const char g_utf8_skip[256] = {
   write operation would invalidate all other iterators pointing into the same
   string.
 */
+/*
 template <class T>
 class ustring_Iterator
 {
 public:
   typedef std::bidirectional_iterator_tag   iterator_category;
-  typedef unichar                           value_type;
+  //typedef unichar                           value_type;
   typedef std::string::difference_type      difference_type;
-  typedef value_type                        reference;
+  //typedef value_type                        reference;
   typedef void                              pointer;
 
   inline ustring_Iterator() {}
   inline ustring_Iterator(const ustring_Iterator<std::string::iterator>&
                          other) : _pos(other.base()) {}
 
+
   inline value_type operator*() const {
     // get a unicode character from the iterator's position
 
@@ -88,6 +96,7 @@ public:
     return result;
   }
 
+  
   inline ustring_Iterator<T> &     operator++() {
     pos_ += g_utf8_skip[static_cast<unsigned char>(*pos_)];
     return *this;
@@ -103,8 +112,7 @@ public:
 private:
   T _pos;
 };
-
-#endif // DOXYGEN_IGNORE
+*/
 
 //! This class provides a simple wrapper to a std::string that can be encoded
 //! as UTF-8. The ustring::utf() member specifies if the given string is UTF-8
@@ -127,12 +135,12 @@ public:
   typedef std::string::size_type                        size_type;
   typedef std::string::difference_type                  difference_type;
 
-  typedef unichar                                       value_type;
-  typedef unichar &                                     reference;
-  typedef const unichar &                               const_reference;
+  //typedef unichar                                       value_type;
+  //typedef unichar &                                     reference;
+  //typedef const unichar &                               const_reference;
 
-  typedef ustring_Iterator<std::string::iterator>       iterator;
-  typedef ustring_Iterator<std::string::const_iterator> const_iterator;
+  //typedef ustring_Iterator<std::string::iterator>       iterator;
+  //typedef ustring_Iterator<std::string::const_iterator> const_iterator;
 
   static const size_type npos = std::string::npos;
 
@@ -159,6 +167,15 @@ public:
   ustring::size_type capacity() const;
   ustring::size_type max_size() const;
 
+  // erase substrings
+
+  void clear();
+  ustring& erase(size_type i, size_type n=npos);
+
+  // change the string's size
+
+  void resize(size_type n, char c='\0');
+  
   // internal data
 
   const char* data()  const;
This page took 0.034223 seconds and 4 git commands to generate.