OSDN Git Service

Add thread search from all boards.
[ntch/develop.git] / src / ui / disp_string.c
1 /* Copyright 2013 Akira Ohta (akohta001@gmail.com)
2     This file is part of ntch.
3
4     The ntch is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8
9     The ntch is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with ntch.  If not, see <http://www.gnu.org/licenses/>.
16     
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <ncursesw/ncurses.h>
23 #include <locale.h>
24 #include <iconv.h>
25 #include <assert.h>
26
27 #include "ui/disp_string.h"
28
29
30
31 int nt_add_wch(WINDOW *w, const wchar_t wc, attr_t attr)
32 {
33         cchar_t c;
34
35         /*
36          * This will supress valgrind's verbose messages.
37          */
38         memset(&c, '\0', sizeof(cchar_t));
39
40         c.attr  =       attr;
41         c.chars[0]      =       wc;
42         c.chars[1]      =       L'\0';
43         return  wadd_wch(w, &c);
44 }
45
46
47 int nt_add_wstr(WINDOW *w, const wchar_t *wc, attr_t attr)
48 {
49         int ret;
50         assert(wc);
51         wattr_on(w, attr, NULL);
52         ret = waddwstr(w, wc);
53         wattr_off(w, attr, NULL);
54         return ret;
55 }
56
57 int nt_add_wnch(WINDOW *w, const wchar_t wc, attr_t attr, size_t colmns)
58 {
59         int i, delta;
60         cchar_t c;
61         /*
62          * This will supress valgrind's verbose messages.
63          */
64         memset(&c, '\0', sizeof(cchar_t));
65         c.attr = attr;
66         c.chars[0] = wc;
67         c.chars[1] = L'\0';
68         if(wc < 128)
69                 delta = 1;
70         else
71                 delta = 2;
72         colmns /= 2;
73         colmns *= 2;
74         for(i = 0; i < colmns; i+=delta){
75                 if(ERR == wadd_wch(w, &c))
76                         break;
77         }
78         return i / delta;
79 }
80
81 int nt_add_wnstr(WINDOW *w, const wchar_t *wc, attr_t attr, size_t colmns)
82 {
83         int i, col, len;
84         cchar_t c;
85         assert(wc);
86         /*
87          * This will supress valgrind's verbose messages.
88          */
89         memset(&c, '\0', sizeof(cchar_t));
90
91         col = 0;
92         c.attr = attr;
93         c.chars[1] = L'\0';
94         len = wcslen(wc);
95         for(i = 0; i < len && col < colmns; i++){
96                 if(wc[i] <= 128 || 
97                                 (wc[i] >= 0xff66 && wc[i] <= 0xff9d)){
98                         col++;
99                 }else{
100                         col += 2;
101                         if(col >= colmns)
102                                 break;
103                 }
104                 c.chars[0] = wc[i];
105                 if(ERR == wadd_wch(w, &c))
106                         break;
107         }
108         return i;
109 }
110
111 int nt_get_column_length(const wchar_t *wc)
112 {
113         int i, column, len;
114         wchar_t ch;
115
116         assert(wc);
117
118         len = wcslen(wc);
119         column = 0;
120         for(i = 0; i < len; i++){
121                 ch = wc[i];
122                 if(ch <= 128 || (ch >= 0xff66 && ch <= 0xff9d))
123                         column++;
124                 else
125                         column += 2;
126         }
127         return column;
128 }
129
130
131 int nt_get_wc_count_within_colmns(const wchar_t *wc, size_t colmns)
132 {
133         int len;
134         int col = 0;
135         int i;
136         wchar_t ch;
137         assert(wc);
138         len = wcslen(wc);
139         for(i = 0; i < len; i++){
140                 ch = wc[i];
141                 if(ch <= 128 || (ch >= 0xff66 && ch <= 0xff9d))
142                         col++;
143                 else
144                         col += 2;
145                 if(col > colmns)
146                         return i;
147         }
148         return i;
149 }