Skip to main content
GameDev.net gamedev.net
🔒 Locked

Making a textbox from scratch?

Started by Super Llama Mar 23, 2009 at 11:25 AM 14 replies 10.7k views
Original Post
Super Llama
Super Llama
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!
jpetrie
jpetrie
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++?
Sneftel
Sneftel
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.
MJP
MJP
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.
Super Llama
Super Llama
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!
iMalc
iMalc
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.
Super Llama
Super Llama
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!
Malevolence
Malevolence
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.
Super Llama
Super Llama
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!
NotAYakk
NotAYakk
Writing a text editor is a full time, many-month, job. That can be extended to an arbitrary amount of work depending on how finicky you get.

Writing a little hack one-line hardcoded fixed-width font single-style (or simple-style) single-language (English!) ascii text display in-game widget isn't hard.

Text processing isn't easy. Render regions aren't pretty, kerning and leading isn't pretty, selection isn't pretty. RtoL and other 'exotic' language feature support isn't pretty. Embedded direction changes isn't pretty. Unicode support can be decidedly unpretty. Italic/Bold heuristics aren't pretty. Underline isn't pretty. Wordwrap isn't pretty in English, and is downright ugly if you have the entire problem domain to work with. Mapping mouse locations to insert locations isn't pretty. Getting the right click and keyboard semantics that the user expects isn't pretty. Justification isn't pretty. Alpha isn't pretty. Getting rights to distribute a font isn't pretty. Picking a default font isn't pretty. Dealing with pathological existing fonts that "work in Microsoft" to work isn't pretty. Dealing with IMEs isn't pretty. Dealing with cut/copy/paste formatting isn't pretty. Accents, decorations, ligatures and multi-character glyphs aren't pretty.

Writing a general purpose text editor isn't pretty.
visitor
visitor
And once all this non-prettiness is done, do you have any reason to believe that your widget will be any faster?

Have you profiled your code to find out what exactly is making it slow?
phresnel
phresnel
Quote:
Original post by ToohrVyk
Also, have you considered leveraging Scintilla ?


Let me emphasize this. It is fast and reliable and highly configurable. Code::Blocks, Notepad++, Scite and geany are based on it (and for years now I nearly always prefer scintilla based editors over editors not based on it) (btw, I use it too).

Plugins exist to use it in QT and there is also a wxWidgets binding. It is a little bit painy to build it properly and stripped off all pre-definitions (if not needed), but that's nothing compared to writing it all yourself.

Just some features:
* code folding
* context sensitive syntax highlighting
* copy+paste current selection (not limitied to copying whole lines)
* text zoom (which is very nice if you're on a coding marathon and your eyes begin to tire)
* approximately 2 billion (really) predefined programming languages
* small, compact, robust
* configurable via configuration files
* matching brace highlighting
* supports block insertion (though I never really used it)
* more

Now, if your editor does not support at least 7 of those features, I won't use it [smile]
Ryann
Ryann
Hello,

I was implementing a textbox control for my GUI library for XNA. So far I have implemented just single-line textbox allowing selection, horizontal text scrolling, copy, paste, moving by words, words selection etc. In this regard it works almost identical to windows textbox, except mouse selection is not done yet and multiline functionality is missing. Althou it's not complete yet I can tell you it was a lot of work, but not impossible to be done.

If you want, you can look at the result in my Neoforce Controls library at the address http://www.tomshane.cz

If you are interested in any techniques I used and you think it could be useful to you as well, I gladly help you.

-Ryann
Ryann
ChaosEngine
ChaosEngine
Quote:
Original post by Super Llama
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.


Have you any actual arguments to back that up? .Net is not "as buggy as Vista", it's actually getting better and better. C# in particular has tonnes of awesome new language features, you can use python on it and WPF could probably implement your coloured-y textbox with writing a line of code.

Nevermind, you have fun with your ill-informed bigotry. I'll be over here being productive.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
mutex
mutex
Don't forget accessibility support for screen-readers. I can tell you from first-hand experience that that is more painful than a lot of the other control must-haves mentioned in this thread such as theming, high DPI, and RTL support.

Unless you have an affinity for writing text controls (and there's nothing wrong with that), save yourself time and headaches by using a preexisting control.

And regarding efficiency, unless you've used a profiler and have hard numbers to back up your claims of inefficiency, you're really just making random guesses.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.