Mercurial > pidgin
annotate finch/libgnt/gntutils.c @ 16150:218818f96b93
Change a g_slist_append() to a g_slist_prepend() in one place, and
change some other stuff to use a hashtable instead of a linked list,
which should make one aspect of smiley theme destroying O(something fast)
instead of O(n). Which really doesn't matter in the grand scheme
of things.
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 15 Apr 2007 18:38:40 +0000 |
parents | 2c81ebc7bf0b |
children | 5f204f55af09 |
rev | line source |
---|---|
15817 | 1 #include "gntutils.h" |
15979
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
2 #include "gnttree.h" |
15817 | 3 |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 | |
7 #include "config.h" | |
8 | |
9 void gnt_util_get_text_bound(const char *text, int *width, int *height) | |
10 { | |
11 const char *s = text, *last; | |
12 int count = 1, max = 0; | |
13 int len; | |
14 | |
15 /* XXX: ew ... everyone look away */ | |
16 last = s; | |
17 if (s) | |
18 { | |
19 while (*s) | |
20 { | |
21 if (*s == '\n' || *s == '\r') | |
22 { | |
23 count++; | |
24 len = gnt_util_onscreen_width(last, s); | |
25 if (max < len) | |
26 max = len; | |
27 last = s + 1; | |
28 } | |
29 s = g_utf8_next_char(s); | |
30 } | |
31 | |
32 len = gnt_util_onscreen_width(last, s); | |
33 if (max < len) | |
34 max = len; | |
35 } | |
36 | |
37 if (height) | |
38 *height = count; | |
39 if (width) | |
40 *width = max + (count > 1); | |
41 } | |
42 | |
43 int gnt_util_onscreen_width(const char *start, const char *end) | |
44 { | |
45 int width = 0; | |
46 | |
47 if (end == NULL) | |
48 end = start + strlen(start); | |
49 | |
50 while (start < end) { | |
51 width += g_unichar_iswide(g_utf8_get_char(start)) ? 2 : 1; | |
52 start = g_utf8_next_char(start); | |
53 } | |
54 return width; | |
55 } | |
56 | |
57 const char *gnt_util_onscreen_width_to_pointer(const char *string, int len, int *w) | |
58 { | |
59 int size; | |
60 int width = 0; | |
61 const char *str = string; | |
62 | |
63 if (len <= 0) { | |
64 len = gnt_util_onscreen_width(string, NULL); | |
65 } | |
66 | |
67 while (width < len && *str) { | |
68 size = g_unichar_iswide(g_utf8_get_char(str)) ? 2 : 1; | |
69 if (width + size > len) | |
70 break; | |
71 str = g_utf8_next_char(str); | |
72 width += size; | |
73 } | |
74 if (w) | |
75 *w = width; | |
76 return str; | |
77 } | |
78 | |
79 char *gnt_util_onscreen_fit_string(const char *string, int maxw) | |
80 { | |
81 const char *start, *end; | |
82 GString *str; | |
83 | |
84 if (maxw <= 0) | |
85 maxw = getmaxx(stdscr) - 4; | |
86 | |
87 start = string; | |
88 str = g_string_new(NULL); | |
89 | |
90 while (*start) { | |
91 if ((end = strchr(start, '\n')) != NULL || | |
92 (end = strchr(start, '\r')) != NULL) { | |
93 if (gnt_util_onscreen_width(start, end) > maxw) | |
94 end = NULL; | |
95 } | |
96 if (end == NULL) | |
97 end = gnt_util_onscreen_width_to_pointer(start, maxw, NULL); | |
98 str = g_string_append_len(str, start, end - start); | |
99 if (*end) { | |
100 str = g_string_append_c(str, '\n'); | |
101 if (*end == '\n' || *end == '\r') | |
102 end++; | |
103 } | |
104 start = end; | |
105 } | |
106 return g_string_free(str, FALSE); | |
107 } | |
108 | |
109 struct duplicate_fns | |
110 { | |
111 GDupFunc key_dup; | |
112 GDupFunc value_dup; | |
113 GHashTable *table; | |
114 }; | |
115 | |
116 static void | |
117 duplicate_values(gpointer key, gpointer value, gpointer data) | |
118 { | |
119 struct duplicate_fns *fns = data; | |
120 g_hash_table_insert(fns->table, fns->key_dup ? fns->key_dup(key) : key, | |
121 fns->value_dup ? fns->value_dup(value) : value); | |
122 } | |
123 | |
124 GHashTable *g_hash_table_duplicate(GHashTable *src, GHashFunc hash, | |
125 GEqualFunc equal, GDestroyNotify key_d, GDestroyNotify value_d, | |
126 GDupFunc key_dup, GDupFunc value_dup) | |
127 { | |
128 GHashTable *dest = g_hash_table_new_full(hash, equal, key_d, value_d); | |
129 struct duplicate_fns fns = {key_dup, value_dup, dest}; | |
130 g_hash_table_foreach(src, duplicate_values, &fns); | |
131 return dest; | |
132 } | |
133 | |
134 gboolean gnt_boolean_handled_accumulator(GSignalInvocationHint *ihint, | |
135 GValue *return_accu, | |
136 const GValue *handler_return, | |
137 gpointer dummy) | |
138 { | |
139 gboolean continue_emission; | |
140 gboolean signal_handled; | |
141 | |
142 signal_handled = g_value_get_boolean (handler_return); | |
143 g_value_set_boolean (return_accu, signal_handled); | |
144 continue_emission = !signal_handled; | |
145 | |
146 return continue_emission; | |
147 } | |
148 | |
15979
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
149 typedef struct { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
150 GHashTable *hash; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
151 GntTree *tree; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
152 } BindingView; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
153 |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
154 static void |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
155 add_binding(gpointer key, gpointer value, gpointer data) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
156 { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
157 BindingView *bv = data; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
158 GntBindableActionParam *act = value; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
159 const char *name = g_hash_table_lookup(bv->hash, act->action); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
160 if (name && *name) { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
161 const char *k = gnt_key_lookup(key); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
162 if (!k) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
163 k = key; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
164 gnt_tree_add_row_after(bv->tree, (gpointer)k, |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
165 gnt_tree_create_row(bv->tree, k, name), NULL, NULL); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
166 } |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
167 } |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
168 |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
169 static void |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
170 add_action(gpointer key, gpointer value, gpointer data) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
171 { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
172 BindingView *bv = data; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
173 g_hash_table_insert(bv->hash, value, key); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
174 } |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
175 |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
176 GntWidget *gnt_widget_bindings_view(GntWidget *widget) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
177 { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
178 GntBindable *bind = GNT_BINDABLE(widget); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
179 GntWidget *tree = gnt_tree_new_with_columns(2); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
180 GntBindableClass *klass = GNT_BINDABLE_CLASS(GNT_BINDABLE_GET_CLASS(bind)); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
181 GHashTable *hash = g_hash_table_new(g_direct_hash, g_direct_equal); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
182 BindingView bv = {hash, GNT_TREE(tree)}; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
183 |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
184 gnt_tree_set_compare_func(bv.tree, (GCompareFunc)g_utf8_collate); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
185 g_hash_table_foreach(klass->actions, add_action, &bv); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
186 g_hash_table_foreach(klass->bindings, add_binding, &bv); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
187 gnt_tree_adjust_columns(bv.tree); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
188 g_hash_table_destroy(hash); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
189 |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
190 return tree; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
191 } |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
192 |