annotate console/libgnt/gntutils.c @ 14578:60845d332ea6

[gaim-migrate @ 17302] Auto-completion can have non-alpha characters. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Mon, 18 Sep 2006 03:17:11 +0000
parents ca36763497d9
children 1d084d1ee6fa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14059
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
1 #include "gntutils.h"
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
2
14311
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
3 #include <stdlib.h>
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
4 #include <string.h>
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
5
14417
64e28f46c695 [gaim-migrate @ 17125]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14366
diff changeset
6 #include "config.h"
64e28f46c695 [gaim-migrate @ 17125]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14366
diff changeset
7
14059
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
8 void gnt_util_get_text_bound(const char *text, int *width, int *height)
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
9 {
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
10 const char *s = text, *last;
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
11 int count = 1, max = 0;
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
12 int len;
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
13
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
14 /* XXX: ew ... everyone look away */
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
15 last = s;
14060
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
16 if (s)
14059
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
17 {
14060
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
18 while (*s)
14059
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
19 {
14060
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
20 if (*s == '\n' || *s == '\r')
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
21 {
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
22 count++;
14420
c4a32405af68 [gaim-migrate @ 17128]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14419
diff changeset
23 len = gnt_util_onscreen_width(last, s);
14060
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
24 if (max < len)
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
25 max = len;
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
26 last = s + 1;
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
27 }
14420
c4a32405af68 [gaim-migrate @ 17128]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14419
diff changeset
28 s = g_utf8_next_char(s);
14059
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
29 }
14060
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
30
14420
c4a32405af68 [gaim-migrate @ 17128]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14419
diff changeset
31 len = gnt_util_onscreen_width(last, s);
14060
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
32 if (max < len)
81648c6aa918 [gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14059
diff changeset
33 max = len;
14059
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
34 }
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
35
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
36 if (height)
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
37 *height = count;
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
38 if (width)
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
39 *width = max + (count > 1);
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
40 }
2d1b41e3cf0d [gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
41
14311
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
42 int gnt_util_onscreen_width(const char *start, const char *end)
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
43 {
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
44 int width = 0;
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
45
14477
ca36763497d9 [gaim-migrate @ 17195]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14424
diff changeset
46 if (end == NULL)
ca36763497d9 [gaim-migrate @ 17195]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14424
diff changeset
47 end = start + strlen(start);
ca36763497d9 [gaim-migrate @ 17195]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14424
diff changeset
48
14311
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
49 while (start < end) {
14419
b50aa149e09d [gaim-migrate @ 17127]
Ethan Blanton <elb@pidgin.im>
parents: 14418
diff changeset
50 width += g_unichar_iswide(g_utf8_get_char(start)) ? 2 : 1;
b50aa149e09d [gaim-migrate @ 17127]
Ethan Blanton <elb@pidgin.im>
parents: 14418
diff changeset
51 start = g_utf8_next_char(start);
14311
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
52 }
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
53 return width;
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
54 }
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
55
14420
c4a32405af68 [gaim-migrate @ 17128]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14419
diff changeset
56 const char *gnt_util_onscreen_width_to_pointer(const char *string, int len, int *w)
14311
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
57 {
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
58 int size;
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
59 int width = 0;
14420
c4a32405af68 [gaim-migrate @ 17128]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14419
diff changeset
60 const char *str = string;
14311
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
61
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
62 while (width < len && *str) {
14419
b50aa149e09d [gaim-migrate @ 17127]
Ethan Blanton <elb@pidgin.im>
parents: 14418
diff changeset
63 size = g_unichar_iswide(g_utf8_get_char(str)) ? 2 : 1;
b50aa149e09d [gaim-migrate @ 17127]
Ethan Blanton <elb@pidgin.im>
parents: 14418
diff changeset
64 if (width + size > len)
b50aa149e09d [gaim-migrate @ 17127]
Ethan Blanton <elb@pidgin.im>
parents: 14418
diff changeset
65 break;
b50aa149e09d [gaim-migrate @ 17127]
Ethan Blanton <elb@pidgin.im>
parents: 14418
diff changeset
66 str = g_utf8_next_char(str);
b50aa149e09d [gaim-migrate @ 17127]
Ethan Blanton <elb@pidgin.im>
parents: 14418
diff changeset
67 width += size;
14311
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
68 }
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
69 if (w)
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
70 *w = width;
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
71 return str;
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
72 }
fda9dc44807d [gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14060
diff changeset
73
14423
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
74 char *gnt_util_onscreen_fit_string(const char *string, int maxw)
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
75 {
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
76 const char *start, *end;
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
77 GString *str;
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
78
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
79 if (maxw <= 0)
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
80 maxw = getmaxx(stdscr) - 4;
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
81
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
82 start = string;
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
83 str = g_string_new(NULL);
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
84
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
85 while (*start) {
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
86 if ((end = strchr(start, '\n')) != NULL ||
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
87 (end = strchr(start, '\r')) != NULL) {
14424
c374f45f4c94 [gaim-migrate @ 17132]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14423
diff changeset
88 if (gnt_util_onscreen_width(start, end) > maxw)
14423
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
89 end = NULL;
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
90 }
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
91 if (end == NULL)
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
92 end = gnt_util_onscreen_width_to_pointer(start, maxw, NULL);
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
93 str = g_string_append_len(str, start, end - start);
14424
c374f45f4c94 [gaim-migrate @ 17132]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14423
diff changeset
94 if (*end) {
c374f45f4c94 [gaim-migrate @ 17132]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14423
diff changeset
95 str = g_string_append_c(str, '\n');
c374f45f4c94 [gaim-migrate @ 17132]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14423
diff changeset
96 if (*end == '\n' || *end == '\r')
c374f45f4c94 [gaim-migrate @ 17132]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14423
diff changeset
97 end++;
c374f45f4c94 [gaim-migrate @ 17132]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14423
diff changeset
98 }
14423
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
99 start = end;
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
100 }
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
101 return g_string_free(str, FALSE);
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
102 }
cfd5bcc06a7e [gaim-migrate @ 17131]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 14420
diff changeset
103