libt3widget
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 <t3window/window.h>
20 
21 #include <t3widget/widget_api.h>
22 #include <t3widget/ptr.h>
23 #include <t3widget/signals.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  T value_or_default(T dflt) { return initialized ? value : dflt; }
44 };
45 
46 typedef optional<int> optint;
48 T3_WIDGET_API extern const optint None;
49 
50 struct T3_WIDGET_API text_coordinate_t {
51  text_coordinate_t(void) {}
52  text_coordinate_t(int _line, int _pos) : line(_line), pos(_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 || 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  bool operator<=(const text_coordinate_t &other) const { return line < other.line || (line == other.line && pos <= other.pos); }
59  int line;
60  int pos;
61 };
62 
63 #define T3_WIDGET_SIGNAL(_name, ...) \
64 protected: \
65  signals::signal<__VA_ARGS__> _name; \
66 public: \
67  signals::connection connect_##_name(const signals::slot<__VA_ARGS__> &_slot) { return _name.connect(_slot); }
68 
69 #define _T3_WIDGET_ENUM(_name, ...) \
70 class T3_WIDGET_API _name { \
71  public: \
72  enum _values { \
73  __VA_ARGS__ \
74  }; \
75  _name(void) {} \
76  _name(_values _value_arg) : _value(_value_arg) {} \
77  _values operator =(_values _value_arg) { _value = _value_arg; return _value; } \
78  operator int (void) const { return (int) _value; } \
79  bool operator == (_values _value_arg) const { return _value == _value_arg; } \
80  private: \
81  _values _value; \
82 }
83 
84 _T3_WIDGET_ENUM(selection_mode_t,
85  NONE,
86  SHIFT,
87  MARK,
88  ALL
89 );
90 
91 _T3_WIDGET_ENUM(find_flags_t,
92  BACKWARD = (1<<0),
93  ICASE = (1<<1),
94  REGEX = (1<<2),
95  WRAP = (1<<3),
96  TRANSFROM_BACKSLASH = (1<<4),
97  WHOLE_WORD = (1<<5) | (1<<6),
98  ANCHOR_WORD_LEFT = (1<<5),
99  ANCHOR_WORD_RIGHT = (1<<6),
100  VALID = (1<<7),
101  REPLACEMENT_VALID = (1<<8),
102 );
103 
104 _T3_WIDGET_ENUM(find_action_t,
105  FIND,
106  SKIP,
107  REPLACE,
108  REPLACE_ALL,
109  REPLACE_IN_SELECTION
110 );
111 
113 _T3_WIDGET_ENUM(attribute_t,
114  NON_PRINT,
115  TEXT_SELECTION_CURSOR,
116  TEXT_SELECTION_CURSOR2,
117  BAD_DRAW,
118  TEXT_CURSOR,
119  TEXT,
120  TEXT_SELECTED,
121  HOTKEY_HIGHLIGHT,
122  DIALOG,
123  DIALOG_SELECTED,
124  BUTTON_SELECTED,
125  SCROLLBAR,
126  MENUBAR,
127  MENUBAR_SELECTED,
128  BACKGROUND,
129  SHADOW,
130  META_TEXT
131 );
140 //FIXME: list other attributes
141 
142 _T3_WIDGET_ENUM(rewrap_type_t,
143  REWRAP_ALL,
144  REWRAP_LINE,
145  REWRAP_LINE_LOCAL,
146  INSERT_LINES,
147  DELETE_LINES
148 );
149 
150 _T3_WIDGET_ENUM(wrap_type_t,
151  NONE,
152  WORD,
153  CHARACTER
154 );
155 
156 #undef _T3_WIDGET_ENUM
157 
158 
159 typedef cleanup_func_ptr<t3_window_t, t3_win_del>::t cleanup_t3_window_ptr;
160 
161 
162 T3_WIDGET_API ssize_t nosig_write(int fd, const char *buffer, size_t bytes);
163 T3_WIDGET_API ssize_t nosig_read(int fd, char *buffer, size_t bytes);
164 
165 T3_WIDGET_API std::string get_working_directory(void);
166 T3_WIDGET_API std::string get_directory(const char *directory);
167 T3_WIDGET_API void sanitize_dir(std::string *directory);
168 T3_WIDGET_API bool is_dir(const std::string *current_dir, const char *name);
169 
170 T3_WIDGET_API void convert_lang_codeset(const char *str, size_t len, std::string *result, bool from);
171 T3_WIDGET_API void convert_lang_codeset(const char *str, std::string *result, bool from);
172 T3_WIDGET_API void convert_lang_codeset(const std::string *str, std::string *result, bool from);
173 
174 }; // namespace
175 #endif
The t3_widget namespace is contains all classes, functions and global variables in the libt3widget li...
Definition: autocompleter.cc:18
const optint None
Standard uninitialized optint value.
Definition: util.cc:53
Definition: util.h:50
Class defining values with a separate validity check.
Definition: util.h:29