public String getSuggestionWrapping(String suggestion, Font font, bool entityMode)
{
int gap = 30;
if (entityMode)
{
gap = 100;
}
String[] words = suggestion.Split(' ');
String result = "";
int width = getRenderTextWidth(suggestion, listBox1, font);
if (width > listBox1.Width - gap)
{
ArrayList wordIndexCollection = new ArrayList();
// The check sequence define what kind of substring pattern we wish to have
// So by having the following value {0, words.length, 1, 2}
// It will check the following substring accordingly:
// Example: "This is an example of substring sequence check."
// "This ... "
// "This ... check."
// "This is ... check."
// "This is an ... check."
// This is an absolute lame and stupid check.
int[] firstCheckSequence = { 0, words.Count()-1, 1, 2 };
for(int i=0; i<firstCheckSequence.Count(); i++){
wordIndexCollection.Add(firstCheckSequence[i]);
result = generateSubstring(words, wordIndexCollection);
width = getRenderTextWidth(result, listBox1, font);
// check if the width is wider
if (width > listBox1.Width - gap)
{
if (i == 0)
{
// If the first word is longer than the width list, we need to substring by characters
while(getRenderTextWidth(
generateSubstring(words, wordIndexCollection),
listBox1, font) > listBox1.Width - gap){
words[0] = words[0].Remove(words[0].Length - 1, 1);
}
result = generateSubstring(words, wordIndexCollection);
}
break;
}
}
// After having the initial pattern check, we will try to take as many words into
// the substring as possible based on the length
// cStart and cEnd define the start and end indexes to perform this operation.
int cStart = 3, cEnd = words.Count()-2;
while (width < listBox1.Width - gap)
{
String cStartWord = words[cStart];
String cEndWord = words[cEnd];
String tempResult;
if (cStartWord.Length < cEndWord.Length)
{
wordIndexCollection.Add(cStart);
cStart++;
}
else
{
wordIndexCollection.Add(cEnd);
cEnd--;
}
tempResult = generateSubstring(words, wordIndexCollection);
width = getRenderTextWidth(tempResult, listBox1, font);
// check if the width is wider
if (width > listBox1.Width - gap)
{
break;
}
else
{
result = tempResult;
}
}
}
else{
result = suggestion;
}
return result;
}