comctl32/imagelist: Use proper color format for merged image lists.
[wine] / dlls / riched20 / string.c
1 /*
2  * RichEdit - string operations
3  *
4  * Copyright 2004 by Krzysztof Foltman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 static int ME_GetOptimalBuffer(int nLen)
26 {
27   /* FIXME: This seems wasteful for tabs and end of lines strings,
28    *        since they have a small fixed length. */
29   return ((sizeof(WCHAR) * nLen) + 128) & ~63;
30 }
31
32 /* Create a buffer (uninitialized string) of size nMaxChars */
33 static ME_String *ME_MakeStringB(int nMaxChars)
34 {
35   ME_String *s = ALLOC_OBJ(ME_String);
36
37   s->nLen = nMaxChars;
38   s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
39   s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
40   s->szData[s->nLen] = 0;
41   return s;
42 }
43
44 ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
45 {
46   ME_String *s = ME_MakeStringB(nMaxChars);
47   /* Native allows NULL chars */
48   memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
49   return s;
50 }
51
52 /* Make a string by repeating a char nMaxChars times */
53 ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
54 {
55   int i;
56   ME_String *s = ME_MakeStringB(nMaxChars);
57   for (i = 0; i < nMaxChars; i++)
58     s->szData[i] = cRepeat;
59   return s;
60 }
61
62 ME_String *ME_StrDup(const ME_String *s)
63 {
64   return ME_MakeStringN(s->szData, s->nLen);
65 }
66
67 void ME_DestroyString(ME_String *s)
68 {
69   if (!s) return;
70   FREE_OBJ(s->szData);
71   FREE_OBJ(s);
72 }
73
74 BOOL ME_InsertString(ME_String *s, int ofs, const WCHAR *insert, int len)
75 {
76     DWORD new_len = s->nLen + len + 1;
77     assert( ofs <= s->nLen );
78
79     if( new_len > s->nBuffer )
80     {
81         s->nBuffer = ME_GetOptimalBuffer( new_len );
82         s->szData = heap_realloc( s->szData, s->nBuffer * sizeof(WCHAR) );
83         if (!s->szData) return FALSE;
84     }
85
86     memmove( s->szData + ofs + len, s->szData + ofs, (s->nLen - ofs + 1) * sizeof(WCHAR) );
87     memcpy( s->szData + ofs, insert, len * sizeof(WCHAR) );
88     s->nLen += len;
89
90     return TRUE;
91 }
92
93 BOOL ME_AppendString(ME_String *s, const WCHAR *append, int len)
94 {
95     return ME_InsertString( s, s->nLen, append, len );
96 }
97
98 ME_String *ME_VSplitString(ME_String *orig, int charidx)
99 {
100   ME_String *s;
101
102   /*if (charidx<0) charidx = 0;
103   if (charidx>orig->nLen) charidx = orig->nLen;
104   */
105   assert(charidx>=0);
106   assert(charidx<=orig->nLen);
107
108   s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
109   orig->nLen = charidx;
110   orig->szData[charidx] = '\0';
111   return s;
112 }
113
114 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
115 {
116   int end_ofs = nVChar + nChars;
117
118   assert(nChars >= 0);
119   assert(nVChar >= 0);
120   assert(end_ofs <= s->nLen);
121
122   memmove(s->szData + nVChar, s->szData + end_ofs,
123           (s->nLen - end_ofs + 1) * sizeof(WCHAR));
124   s->nLen -= nChars;
125 }
126
127 static int
128 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
129 {
130   /* FIXME: Native also knows about punctuation */
131   TRACE("s==%s, start==%d, len==%d, code==%d\n",
132         debugstr_wn(s, len), start, len, code);
133   /* convert number of bytes to number of characters. */
134   len /= sizeof(WCHAR);
135   switch (code)
136   {
137     case WB_ISDELIMITER:
138       return ME_IsWSpace(s[start]);
139     case WB_LEFT:
140     case WB_MOVEWORDLEFT:
141       while (start && ME_IsWSpace(s[start - 1]))
142         start--;
143       while (start && !ME_IsWSpace(s[start - 1]))
144         start--;
145       return start;
146     case WB_RIGHT:
147     case WB_MOVEWORDRIGHT:
148       while (start < len && !ME_IsWSpace(s[start]))
149         start++;
150       while (start < len && ME_IsWSpace(s[start]))
151         start++;
152       return start;
153   }
154   return 0;
155 }
156
157
158 int
159 ME_CallWordBreakProc(ME_TextEditor *editor, WCHAR *str, INT len, INT start, INT code)
160 {
161   if (!editor->pfnWordBreak) {
162     return ME_WordBreakProc(str, start, len * sizeof(WCHAR), code);
163   } else if (!editor->bEmulateVersion10) {
164     /* MSDN lied about the third parameter for EditWordBreakProc being the number
165      * of characters, it is actually the number of bytes of the string. */
166     return editor->pfnWordBreak(str, start, len * sizeof(WCHAR), code);
167   } else {
168     int result;
169     int buffer_size = WideCharToMultiByte(CP_ACP, 0, str, len,
170                                           NULL, 0, NULL, NULL);
171     char *buffer = heap_alloc(buffer_size);
172     WideCharToMultiByte(CP_ACP, 0, str, len,
173                         buffer, buffer_size, NULL, NULL);
174     result = editor->pfnWordBreak((WCHAR*)buffer, start, buffer_size, code);
175     heap_free(buffer);
176     return result;
177   }
178 }
179
180 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
181 {
182   assert(psz != NULL);
183
184   if (unicode)
185     return psz;
186   else {
187     WCHAR *tmp;
188     int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
189     if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
190       MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
191     return tmp;
192   }
193 }
194
195 void ME_EndToUnicode(BOOL unicode, LPVOID psz)
196 {
197   if (!unicode)
198     FREE_OBJ(psz);
199 }