• Advertisement
Sign in to follow this  

Syntax Highlighting (How?)

This topic is 4241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing an IDE for a programming language I'm writing (I'll probably use it for scripting AI in games or writing simple tools). Of course the IDE includes a text editor, which I would like to have syntax highlighting. The language resembles C/C#/Java so I would like it to color certain keywords like "for", "if", and "int". Do I need to make a RichEdit control? A little help here would be greatly appreciated. :) Edit: I'm coding in native C++, using only the Win32 API.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
One thing that you might think about doing is writing a module for Visual Studio (search your msdn for babel something or other, if you have it). I did that for my own language and it worked out fairly well. By properly formatting your output from your compiler (if that is what you are using) you can get good error messages just like with C++. Then with custom build events you can easily develop in your own language in Visual Studio.

Otherwise, you can use a rich edit as you said. I think you will find some things about this here at gamedev if you search (I remember something about syntax highlighting coming up). One thing you might think of doing is lexing the text occasionally and coloring lexemes (which is kind of how you do in for visual studio).

Sorry if that isn't much to go on.

Share this post


Link to post
Share on other sites
I'm not interesting in writing a module for Visual Studio, I'm writing a stand-alone IDE. Actually I already have the whole IDE finished, including project files, text editors, output windows and everything - except syntax highlighting. It's not exactly necessary, though.

How do I create multi-colored Rich Edit's?

Share this post


Link to post
Share on other sites
The idea behind highlighting in a RichEdit is to programatically move the cursor to select the keywords, change the color for the selection, and restore the user's selection.

Then, you can use lexical hightlight just fine by checking the changed lines for keywords and updating colors. Syntax highlighting involves parsing, and as such it's a little bit harder.

Share this post


Link to post
Share on other sites
You could look at Scintilla. It is a edit control that does syntax highlighting. You pass in all the keywords and it colours them for you. I used it once in a php editor.

Share this post


Link to post
Share on other sites
rate++ for scintilla. It's also bound to the wxWidgets library (great cross-platform gui library) that has both python and lua bindings. It's called something different in there though, something like wxRichTextControl or something

Share this post


Link to post
Share on other sites
Actually I'm looking for a small solution, parsing is no problem (all you have to do is look for keywords while excluding strings and comments, right?). I'm curious though - how do you change the color of some text in a Rich Edit?

And I'm also curious, what does "rich edit" mean? Is it like an improved normal Win32 edit control?

Edit: (hehe..."edit...") I just found out that a RichEdit control is an edit box that displays RTF data. Now I just need to find out how to use it. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
The idea behind highlighting in a RichEdit is to programatically move the cursor to select the keywords, change the color for the selection, and restore the user's selection.


Hahahahaha, you said programatically. Sorry, I couldn't help it ;-)

Share this post


Link to post
Share on other sites
Look at scintilla. It has source code. The best way of doing things is usually to look at how other people do it. Then either copy it (giving credit where due) or reimplenting it if you think you can do it better / does not serve your needs.

To change colour of rich edit text. Taken from MSDN

// The pointer to my rich edit control.
extern CRichEditCtrl* pmyRichEditCtrl;
CHARFORMAT cf;

// Modify the word format so that the selected word is
// displayed in bold and not striked out.
cf.dwMask = CFM_STRIKEOUT|CFM_BOLD;
cf.dwEffects = CFE_BOLD;
pmyRichEditCtrl->SetWordCharFormat(cf);


Share this post


Link to post
Share on other sites
I wrote the PHP IDE that I use at work myself, including extensive syntax highlighting. You can read some of my comments on how I did it in this thread.

Share this post


Link to post
Share on other sites
Quote:
// The pointer to my rich edit control.
extern CRichEditCtrl* pmyRichEditCtrl;
CHARFORMAT cf;

// Modify the word format so that the selected word is
// displayed in bold and not striked out.
cf.dwMask = CFM_STRIKEOUT|CFM_BOLD;
cf.dwEffects = CFE_BOLD;
pmyRichEditCtrl->SetWordCharFormat(cf);

That seems like what I need to do...so I have to select the text (from within the program) and then set the color of that text? Hmmm...that seems a little foreign to me. Oh well, how do I select text then?

Whoa, wait...I just noticed this: "CRichEditCtrl". Is that MFC? Sorry, bud, I don't do MFC. I like small/fast programs.

I tried creating a RichEdit using CreateWindowEx(...) but it didn't show up. I can't post the code because I don't have it anymore (sorry...why would I keep code that doesn't work? I just replaced it with a normal edit until I can figure this out).

I can't seem to find any good Rich Edit tutorials online using simple Win32 - They all use MFC! Grr...

Share this post


Link to post
Share on other sites
Quote:
Sorry, bud, I don't do MFC. I like small/fast programs.

Sorry about the MFC code. I work with it everyday so forget other people don't use it. [sidenote] Small is not necessarily fast [/sidenote]

Use EM_SETCHARFORMAT instead.

To create a richedit view call LoadLibraryA("RICHED20.DLL") in winmain.
Then call CreateWindowEx with classname set to RICHEDIT_CLASS
you need to #include <richedit.h> to get access to RICHEDIT_CLASS
Don't forget to FreeLibrary(hRichEditInstance) before you exit.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://catch22.net/tuts/editor04.asp is a start

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement