libt3widget
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
util.h
1 /* Copyright (C) 2011-2013 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifndef T3_WIDGET_UTIL_H
15 #define T3_WIDGET_UTIL_H
16 #include <cstdlib>
17 #include <string>
18 #include <unistd.h>
19 #include <sigc++/sigc++.h>
20 #include <t3window/window.h>
21 
22 #include <t3widget/widget_api.h>
23 #include <t3widget/ptr.h>
24 
25 namespace t3_widget {
26 
28 template <class T>
29 class T3_WIDGET_API optional {
30  private:
31  T value;
32  bool initialized;
34  public:
35  optional(void) : initialized(false) {}
36  optional(T _value) : value(_value), initialized(true) {}
37  bool is_valid(void) const { return initialized; }
38  void unset(void) { initialized = false; }
39  operator T (void) const { if (!initialized) throw(0); return (T) value; }
40  T operator()(void) const { if (!initialized) throw(0); return (T) value; }
41  optional & operator=(const optional &other) { initialized = other.initialized; value = other.value; return *this; }
42  optional & operator=(const T other) { initialized = true; value = other; return *this; }
43 };
44 
45 typedef optional<int> optint;
47 T3_WIDGET_API extern const optint None;
48 
49 struct T3_WIDGET_API text_coordinate_t {
50  text_coordinate_t(void) {}
51  text_coordinate_t(int _line, int _pos) : line(_line), pos(_pos) {}
52  bool operator==(const text_coordinate_t &other) const { return line == other.line && pos == other.pos; }
53  bool operator!=(const text_coordinate_t &other) const { return line != other.line || pos != other.pos; }
54  bool operator>(const text_coordinate_t &other) const { return line > other.line || (line == other.line && pos > other.pos); }
55  bool operator>=(const text_coordinate_t &other) const { return line > other.line || (line == other.line && pos >= other.pos); }
56  bool operator<(const text_coordinate_t &other) const { return line < other.line || (line == other.line && pos < other.pos); }
57  bool operator<=(const text_coordinate_t &other) const { return line < other.line || (line == other.line && pos <= other.pos); }
58  int line;
59  int pos;
60 };
61 
62 #define T3_WIDGET_SIGNAL(_name, ...) \
63 protected: \
64  sigc::signal<__VA_ARGS__> _name; \
65 public: \
66  sigc::connection connect_##_name(const sigc::slot<__VA_ARGS__> &_slot) { return _name.connect(_slot); }
67 
68 #define _T3_WIDGET_ENUM(_name, ...) \
69 class T3_WIDGET_API _name { \
70  public: \
71  enum _values { \
72  __VA_ARGS__ \
73  }; \
74  _name(void) {} \
75  _name(_values _value_arg) : _value(_value_arg) {} \
76  _values operator =(_values _value_arg) { _value = _value_arg; return _value; } \
77  operator int (void) const { return (int) _value; } \
78  bool operator == (_values _value_arg) const { return _value == _value_arg; } \
79  private: \
80  _values _value; \
81 }
82 
83 _T3_WIDGET_ENUM(selection_mode_t,
84  NONE,
85  SHIFT,
86  MARK,
87  ALL
88 );
89 
90 _T3_WIDGET_ENUM(find_flags_t,
91  BACKWARD = (1<<0),
92  ICASE = (1<<1),
93  REGEX = (1<<2),
94  WRAP = (1<<3),
95  TRANSFROM_BACKSLASH = (1<<4),
96  WHOLE_WORD = (1<<5) | (1<<6),
97  ANCHOR_WORD_LEFT = (1<<5),
98  ANCHOR_WORD_RIGHT = (1<<6),
99  VALID = (1<<7),
100  REPLACEMENT_VALID = (1<<8),
101 );
102 
103 _T3_WIDGET_ENUM(find_action_t,
104  FIND,
105  SKIP,
106  REPLACE,
107  REPLACE_ALL,
108  REPLACE_IN_SELECTION
109 );
110 
112 _T3_WIDGET_ENUM(attribute_t,
113  NON_PRINT,
114  TEXT_SELECTION_CURSOR,
115  TEXT_SELECTION_CURSOR2,
116  BAD_DRAW,
117  TEXT_CURSOR,
118  TEXT,
119  TEXT_SELECTED,
120  HOTKEY_HIGHLIGHT,
121  DIALOG,
122  DIALOG_SELECTED,
123  BUTTON_SELECTED,
124  SCROLLBAR,
125  MENUBAR,
126  MENUBAR_SELECTED,
127  BACKGROUND,
128  SHADOW,
129  META_TEXT
130 );
139 //FIXME: list other attributes
140 
141 _T3_WIDGET_ENUM(rewrap_type_t,
142  REWRAP_ALL,
143  REWRAP_LINE,
144  REWRAP_LINE_LOCAL,
145  INSERT_LINES,
146  DELETE_LINES
147 );
148 
149 _T3_WIDGET_ENUM(wrap_type_t,
150  NONE,
151  WORD,
152  CHARACTER
153 );
154 
155 #undef _T3_WIDGET_ENUM
156 
157 
158 typedef cleanup_func_ptr<t3_window_t, t3_win_del>::t cleanup_t3_window_ptr;
159 
160 
161 T3_WIDGET_API ssize_t nosig_write(int fd, const char *buffer, size_t bytes);
162 T3_WIDGET_API ssize_t nosig_read(int fd, char *buffer, size_t bytes);
163 
164 T3_WIDGET_API std::string get_working_directory(void);
165 T3_WIDGET_API std::string get_directory(const char *directory);
166 T3_WIDGET_API void sanitize_dir(std::string *directory);
167 T3_WIDGET_API bool is_dir(const std::string *current_dir, const char *name);
168 
169 T3_WIDGET_API void convert_lang_codeset(const char *str, size_t len, std::string *result, bool from);
170 T3_WIDGET_API void convert_lang_codeset(const char *str, std::string *result, bool from);
171 T3_WIDGET_API void convert_lang_codeset(const std::string *str, std::string *result, bool from);
172 
173 }; // namespace
174 #endif
const optint None
Standard uninitialized optint value.
Definition: util.cc:53
Definition: util.h:49
Class defining values with a separate validity check.
Definition: util.h:29