revert between 56095 -> 55830 in arch
[AROS.git] / workbench / classes / zune / texteditor / mcc / Search.c
blob4bda565a5ba9f59a96bb62b3a3c0bcddc4e278c6
1 /***************************************************************************
3 TextEditor.mcc - Textediting MUI Custom Class
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2014 TextEditor.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 TextEditor class Support Site: http://www.sf.net/projects/texteditor-mcc
19 $Id$
21 ***************************************************************************/
22 #include <string.h>
24 #include <clib/alib_protos.h>
25 #include <proto/utility.h>
27 #include "private.h"
28 #include "Debug.h"
30 /// SimpleMarkText()
31 static void SimpleMarkText(struct InstData *data, LONG startx, struct line_node *startline, LONG stopx, struct line_node *stopline)
33 ENTER();
35 if(Enabled(data))
37 data->blockinfo.enabled = FALSE;
38 MarkText(data, data->blockinfo.startx, data->blockinfo.startline, data->blockinfo.stopx, data->blockinfo.stopline);
40 // else
42 SetCursor(data, data->CPos_X, data->actualline, FALSE);
45 data->blockinfo.startline = startline;
46 data->blockinfo.startx = startx;
47 data->blockinfo.stopline = data->actualline = stopline;
48 data->blockinfo.stopx = data->CPos_X = stopx;
49 data->blockinfo.enabled = TRUE;
51 ScrollIntoDisplay(data);
52 MarkText(data, startx, startline, stopx, stopline);
54 LEAVE();
57 ///
59 static LONG Native_strncmp (STRPTR str1, STRPTR str2, LONG len) { return strncmp(str1, str2, len); }
60 static LONG Utility_strnicmp (STRPTR str1, STRPTR str2, LONG len) { return Strnicmp(str1, str2, len); }
62 /// mSearch()
63 IPTR mSearch(UNUSED struct IClass *cl, Object *obj, struct MUIP_TextEditor_Search *msg)
65 struct InstData *data = INST_DATA(cl, obj);
66 STRPTR str = msg->SearchString;
67 LONG len = strlen(str), step = 0;
69 ENTER();
71 if(len > 0 && len <= 120)
73 BYTE map[256];
74 LONG (*StrCmp)(STRPTR, STRPTR, LONG);
75 LONG cursor;
76 struct line_node *line;
78 // if the FromTop flag is set we start the search right from the top
79 if(isFlagSet(msg->Flags, MUIF_TextEditor_Search_FromTop))
81 cursor = 0;
82 line = GetFirstLine(&data->linelist);
84 else
86 cursor = data->CPos_X;
87 line = data->actualline;
90 memset(map, len, 256);
92 // if a casesensitive search is requested we use a different
93 // compare function.
94 if(isFlagSet(msg->Flags, MUIF_TextEditor_Search_CaseSensitive))
96 StrCmp = Native_strncmp;
98 while(*str)
99 map[(int)*str++] = step--;
101 else
103 StrCmp = Utility_strnicmp;
104 while(*str)
106 map[ToLower(*str)] = step;
107 map[ToUpper(*str++)] = step--;
111 if(isFlagSet(msg->Flags, MUIF_TextEditor_Search_Backwards))
113 //D(DBF_STARTUP, "MUIF_TextEditor_Search_Backwards search=%s\n", msg->SearchString);
114 if(Enabled(data))
115 cursor -= len;
117 while(line != NULL)
119 LONG lenTmp = len;
120 STRPTR contents = line->line.Contents + cursor - lenTmp+1;
121 STRPTR lower = line->line.Contents;
123 while(contents >= lower)
125 //D(DBF_STARTUP, "MUIF_TextEditor_Search_Backwards previous=%ld, contents=%s\n",line, contents);
126 if(!StrCmp(contents, msg->SearchString, len))
128 LONG startx = contents - line->line.Contents;
130 //D(DBF_STARTUP, "MUIF_TextEditor_Search_Backwards found\n");
132 SimpleMarkText(data, startx, line, startx+len, line);
134 RETURN(TRUE);
135 return TRUE;
137 contents -= 1;
138 lenTmp += 1;
141 line = GetPrevLine(line);
143 if(line != NULL)
144 cursor = line->line.Length;
147 else
149 while(line)
151 LONG skip;
152 STRPTR contents = line->line.Contents + cursor + len-1;
153 STRPTR upper = line->line.Contents + line->line.Length;
155 while(contents < upper)
157 skip = map[(int)(*contents)];
158 contents += skip;
160 if(skip <= 0)
162 if(!StrCmp(contents, msg->SearchString, len))
164 LONG startx = contents - line->line.Contents;
166 SimpleMarkText(data, startx, line, startx+len, line);
168 RETURN(TRUE);
169 return TRUE;
171 contents += len;
175 cursor = 0;
177 line = GetNextLine(line);
183 RETURN(FALSE);
184 return FALSE;
188 /// mReplace()
189 IPTR mReplace(UNUSED struct IClass *cl, Object *obj, struct MUIP_TextEditor_Replace *msg)
191 struct InstData *data = INST_DATA(cl, obj);
192 IPTR res = FALSE;
194 ENTER();
196 if(Enabled(data))
198 Key_Clear(data);
199 DoMethod(obj, MUIM_TextEditor_InsertText, msg->NewString, MUIV_TextEditor_InsertText_Cursor);
200 res = TRUE;
203 RETURN(res);
204 return res;