Making a textbox from scratch?

Started by
14 comments, last by mutex 15 years ago
I've tried to do this many times, with different approaches, but I usually can't make one that works very well. The selection algorithm is messy, it can only use fixed-length fonts (Courier New), etc. Is there any good tutorial on coding one? I haven't had much luck with Google. I use Visual Basic 6 for basic windows programs that I need done quickly, and Dev-C++ for anything that uses DirectX or any program that I need to run efficiently (Visual C++ takes up alot of RAM, and I generally prefer the Dev-C++ interface and compiler set). The reason I can't just use a default textbox is because I need control over colors (making a combination chat program and IDE) and the reason I can't just use a RTF Textbox is because coloring them generally takes a long, complicated algorithm of string parsing and adding in RTF codes. The best alternative would just be to write my own, custom textbox to fit my needs... but how? Like I said, all my attempts were rather messy. I've come to find that a GDI-drawn Backbuffered, fixed-length font, character-by-character textbox using early ascii characters (like char 1, char 2, etc) for formatting works rather well, but now I have to worry about scrolling and selection and non-fixed-length fonts. Basically all I need is a simple tutorial on coding a simple text entry field complete with selection. Thanks to anyone who has bothered to read all this and respond.
It's a sofa! It's a camel! No! It's Super Llama!
Advertisement
Quote:
The best alternative would just be to write my own, custom textbox to fit my needs...

No, the best alternative is to leverage as much existing text box functionality as possible, adding your enhancements on top. This is because, as you've noted, getting all the subtle (and even not so subtle) details of text box operation correct is difficult.

I've written implementations from scratch before, and it's not fun. There's no real good resource enumerating the things you need to do -- no tutorials. Mostly you need to pay very close attention to the behavior of some existing textbox and go through a lot of trial and error. You can expect to spend a lot of time reading about text layout metrics and font APIs, for example.

You were vague about your desired language. Are you going to use VB6 or C++?
Quote:Original post by Super Llama
The reason I can't just use a default textbox is because I need control over colors (making a combination chat program and IDE) and the reason I can't just use a RTF Textbox is because coloring them generally takes a long, complicated algorithm of string parsing and adding in RTF codes.
I can guarantee you that a long algorithm of string parsing and adding in RTF codes will be nothing compared to the pain involved in reimplementing a completely standard textbox. Kerning, multiline selection rules, multiword selection rules, shortcut keys, custom shortcut keys... to say nothing of your users who are using alternative adaptive input devices (you know, the cripples) and will be thoroughly screwed over by a widget that tells Windows it isn't a textbox, yet acts like one.

If I were in your situation, I would spend my time looking for information about techniques and algorithms that could simplify the string parsing and RTFizing process.
Also, have you considered leveraging Scintilla ?
First of all, Don't use Dev-C++.

Second...why VB6??? I use it every day at work because I have to, and for that reason only. Whenever I'm writing a tool or a personal project, I use C# and .NET. Believe me when I say that the tools and Class Library are better in every conceivable way. If you prefer VB syntax, then VB.NET has many of the same advantages.

As for your textbox problem...if you use the RichTextBox class in .NET there's a SelectionColor property that sets the color for currently selected text. In fact the VB6 version has the same property, if you're using that.
The topic isn't about what IDE I use or why. Personally, I think of .NET as asking Microsoft to do the hard stuff for you, and most of the stuff Microsoft does these days is as buggy as Vista.

I've used RTF Textboxes before, and its not about WRITING the long algorithm, its about the inefficiency of it all. The computer has to process this giant list of words and decide what color they are, that makes it pretty slow.

I'm an efficiency-obsessed control freak when it comes to programming, that's why I don't like .NET stuff. It has limitations, and uses something that someone else made. I even use x86 assembly when I have to. The only external libraries I settle for are the WinAPI, DirectX, and the VB6 controls. I've used VB.NET before, and I just keep coming back to VB6. I've used C# before, and I just keep coming back to unmanaged C++. These two languages are time-tested and trustworthy, and I know them like the back of my llama foot.

But this is off-topic, back to Textboxes!

First of all, when I get to the GUI portion of my Game Engine, I'll have to write my own textbox anyway just to get it to work inside the D3D Device (unless I can bitblt one onto a texture or something).

I'll try using the RichTextbox again, maybe I can optimize my old processing code to get it to work right.

btw, I'm reading that thing about why not to use Dev-C++, and Code::Blocks sounds great. I'm downloading it now, and I'm glad I don't have to use Visual C++ Express. (I just don't like 21st century Microsoft stuff...) thanks for the link

Also, the SelectionColor property requires you to select each word and set the color, which would result in the box scrolling all over the place every frame...

I also can't reskin default textboxes that easily. Chat programs usually have cool-looking skins for buttons and stuff, which is so much easier if you have your own drawing code for a textbox. I guess I could just keep using every programmer's best friend: BitBlt. I can put a normal textbox offscreen, and blit the part that I want into my own skin, possibly even modifying the look of the result during the blit. In fact, if I use a black background with a white foreground for the textbox, I can use additive or subtractive blitting to achieve interesting effects on a skin.

[Edited by - Super Llama on March 23, 2009 12:08:14 PM]
It's a sofa! It's a camel! No! It's Super Llama!
Quote:Original post by Super Llama
The reason I can't just use a default textbox is because I need control over colors (making a combination chat program and IDE) and the reason I can't just use a RTF Textbox is because coloring them generally takes a long, complicated algorithm of string parsing and adding in RTF codes.

I've used RTF Textboxes before, and its not about WRITING the long algorithm, its about the inefficiency of it all. The computer has to process this giant list of words and decide what color they are, that makes it pretty slow.
You've got to be kidding me! We're talking about a GUI element here. Whether it takes one millisecond or ten, it's neither here nor there.

RTF processing is easy. I've done plenty of it, and quite recently too. Also, the testers were testing the performance with pasting multi-megabyte bitmaps into them and I got no complaints whatsoever about speed.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Wow, my algorithm must have been really bad then XD

I'll try again, and see what I can come up with. Thanks for re-encouraging me on the use of Richtext.

Code::Blocks installing, looks like I've finally found something that I like better than Dev-C++! And its not even Micro$oft :D

[Edited by - Super Llama on March 23, 2009 1:38:43 PM]
It's a sofa! It's a camel! No! It's Super Llama!
I'm still not sure if you're working with VB6 or C++. If it's VB6, you can find some excellent resources on using subclassing with the default textboxes to allow a heavily customized look. Some examples can be found here: http://www.xtremevbtalk.com/showthread.php?t=228243

I'm sure similar techniques exist with C++ but I'm not sure where to find tutorials on them. I definitely agree with everyone else that building on top of the default controls is the way to go.
I've found a way to use BitBlt with SRCPAINT to draw just the text out of a Richtext or Textbox control onto a graphic, except that the textbox doesn't own its on hDC and doesn't draw at all when invisible and cannot be set to AutoRedraw.
It's a sofa! It's a camel! No! It's Super Llama!

This topic is closed to new replies.

Advertisement