private static int FindFirstEmoticon(string text, int startIndex, out string emoticonFound)
-
{
-
InitEmoticons();
-
emoticonFound = string.Empty;
-
int minIndex = -1;
-
foreach (string e in m_Emoticons.Keys) {
-
int index = text.IndexOf(e, startIndex);
-
if (index >= 0) {
-
if (minIndex < 0 || index < minIndex) {
-
minIndex = index;
-
emoticonFound = e;
-
}
-
}
-
}
-
return minIndex;
-
} // FindFirstEmoticon
-
-
public static void ParseText(FrameworkElement element)
-
{
-
InitEmoticons();
-
TextBlock textBlock = null;
-
RichTextBox textBox = element as RichTextBox;
-
if (textBox == null)
-
textBlock = element as TextBlock;
-
-
if (textBox == null && textBlock == null)
-
return;
-
-
if (textBox != null){
-
FlowDocument doc = textBox.Document;
-
for (int blockIndex=0; blockIndex < doc.Blocks.Count; blockIndex++){
-
Block b = doc.Blocks.ElementAt(blockIndex);
-
Paragraph p = b as Paragraph;
-
if (p != null) {
-
ProcessInlines(textBox, p.Inlines);
-
}
-
}
-
}else{
-
ProcessInlines(null, textBlock.Inlines);
-
}
-
} // ParseText
-
-
private static void ProcessInlines(RichTextBox textBox, InlineCollection inlines)
-
{
-
for (int inlineIndex=0; inlineIndex < inlines.Count; inlineIndex++){
-
Inline i = inlines.ElementAt(inlineIndex);
-
if (i is Run) {
-
Run r = i as Run;
-
string text = r.Text;
-
string emoticonFound = string.Empty;
-
int index = FindFirstEmoticon(text, 0, out emoticonFound);
-
if (index >= 0) {
-
TextPointer tp = i.ContentStart;
-
bool reposition = false;
-
while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonFound))
-
tp = tp.GetNextInsertionPosition(LogicalDirection.Forward);
-
TextPointer end = tp;
-
for (int j=0; j<emoticonFound.Length; j++)
-
end = end.GetNextInsertionPosition(LogicalDirection.Forward);
-
TextRange tr = new TextRange(tp, end);
-
if (textBox != null)
-
reposition = textBox.CaretPosition.CompareTo(tr.End) == 0;
-
tr.Text = string.Empty;
-
-
string imageFile = m_Emoticons[emoticonFound];
-
Image image = new Image();
-
BitmapImage bimg = new BitmapImage();
-
bimg.BeginInit();
-
bimg.UriSource = new Uri(imageFile, UriKind.Relative);
-
bimg.EndInit();
-
image.Source = bimg;
-
image.Width = 24;
-
-
InlineUIContainer iui = new InlineUIContainer(image, tp);
-
iui.BaselineAlignment = BaselineAlignment.TextBottom;
-
-
if (textBox != null && reposition)
-
textBox.CaretPosition = tp.GetNextInsertionPosition(LogicalDirection.Forward);
-
}
-
}
-
}
-
} // ProcessInlines