Thursday, August 30, 2012

Keep it simple stupid

I love WPF. I write love songs about it. It also happens to be my main area of expertise when it comes to .NET framework but there are certain things about WPF that drove me nuts when they WPF-tized a former winforms control.

RichTextBox is one of them.

I needed a very simple textbox that can programmatically highlight a portion of my text given an index and a length. Now in Winforms, I can simply call the Select function on the text and change the selection color of it.
rtb.Select(startindex, endindex);  
rtb.SelectionColor = Color.Red;

Boom, two lines. Job done. How simple and elegant is that? You would think that you can get away doing the same thing in WPF but instead, they have to f$*% it up so hard that they make me write this monstrosity of a code:

 private static TextPointer GetTextPointAt(TextPointer from, int pos)  
     TextPointer ret = from;  
     int i = 0;  
     while ((i < pos) && (ret != null))  
       if ((ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text) || (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None))  
       if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)  
         return ret;  
       ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);  
     return ret;  
 internal string Select(RichTextBox rtb, int offset, int length, Color color)  
     // Get text selection:  
     TextSelection textRange = rtb.Selection;  
     // Get text starting point:  
     TextPointer start = rtb.Document.ContentStart;  
     // Get begin and end requested:  
     TextPointer startPos = GetTextPointAt(start, offset);  
     TextPointer endPos = GetTextPointAt(start, offset + length);  
     // New selection of text:  
     textRange.Select(startPos, endPos);  
     // Apply property to the selection:  
     textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));  
     // Return selection text:  
     return rtb.Selection.Text;  

WHY SHOULD I HAVE TO GO THROUGH SO MUCH TROUBLE FOR SOMETHING SEEMINGLY SIMPLE? Wtf is the reason that I have to deal with entirely new paradigms like Textpointers for such a simple feature? Oh M$FT wanted to make the control more robust and powerful? Well what the hell is the problem with making a new control then? Call it Even-More-Rich-Text-Box I don't f**king care. And if I really wanted a powerful texteditor, I'd be using Avalonedit anyways. Leave my Richtextbox alone.

Sure, there is an option to include a element host and just use the Winforms version of it, but the richtextbox I was including was inside a WPF host control running as a Winforms application. Going Winforms-WPF-Winforms (is this even possible?) would be the last retarded thing I do before I cut my throat. And no, I didn't have a choice to build the application in WPF to begin with.

Look, my point is this. It's great that Microsoft was trying to provide the users with more features, but I don't want to have to use a Swiss army knife to carve a chicken. Textbox lacks the feature I need, other powerful editors like AvalonEdit and ScintillaNET is an overkill. RichTextBox is just the right tool I need, except they made it much more complicated than needed for typical use.

I love WPF, I really do. But sometimes, ridiculous upgrades on features or radical changes in paradigms really frustrates me. And I'm sure I'm not the only developer who faces this kind of a problem.

Keep it simple, Stupid.

No comments:

Post a Comment