How to store syntax-highlighted text? - c++

I am creating a simple text editor with syntax highlighting from scratch as a school project. Right now, I use QStringList for storing the text data but later I'll be adding the syntax highlighting functionality and I don't know how should I store the data.
One option would be not to save the color information at all and continuously getting it from the code parser. But this would be probably very inefficient.
Maybe better idea is that there would be some list of structs (containing the color and the string) for every line which would store the color for every word in the text. But I'm not so sure how fast will this be either.
What is the best way to store these data?
Thank you

I'll suggest two methods.
Method 1: Store text with attributes
Change your data structure to a container of structures. The structure would contain a text string and a style variable:
struct Text_With_Properties
{
std::string text;
Properties text_properties;
};
This may take up a lot more space and may not be the most efficient method.
Method 2: Parse for Style Changes
Many editors will display the text in normal font, then make another pass parsing for style changes. For example, when a C++ keyword is found during the 2nd pass, the editor would change its style.
This method does not require any more space for the data, but does require more processing time.

This question is probably too broad. But you could probably use HTML markup. It would give you an easy way to test, as well, as you could open up the output in a browser.

Related

Is it possible to add text instead of replacing the entire text for a static text control on MFC?

I know the classic way to show text on a static text control.
GetDlgItem(IDC_STATIC_YOURTEXT)->SetWindowText("What you want to show");
But if my text is gradually added, doesn't have to replace the entire old text. Replacing entire would be low performance if the text is long. Is there any way to add text gradually.
No, there is no such way. If you want to add to a text, keep your own variable, add to it and then call SetWindowText with your variable again when it changes.
If that is too slow, maybe a static text control is not the best way to achieve your goal.

Static wxRichTextControl?

I want to create an Help page for my wxWidgets application, which consists of only text of various fonts, sizes and colors. I tried achieving that with wxStaticText's and sizers but I was having trouble so I switched to a wxRichTextCtrl, which I like to use. The problem is, the text should of course be non-modifiable. I know of the existance of the wxRE_READONLY flag, but it doesn't hide the caret that appears when I click on the text.
Isn't there some kind of wxRichStaticTextCtrl that works exactly like a wxRichTextCtrl, but is readonly by default and doesn't have this problem? I tried looking online, but to no avail.
The best solution for "rich" static text is wxHtmlWindow: this allows you to just define your help contents in terms of simple HTML which is usually more than enough.

Seeking Qt5 WYSIGWG editor design advice

I am planning a new desktop application. It will provide a WYSIWYG editor for HL7
files (OSS Project). HL7 is a structured file format often used by hospitals for
exchanging data between systems.
The basic structure of the format comprises of records which are \r
delimited. A record can have N fields which are | delimited. A field itself
can be sub-devided into components ^ and sub-components & and
fields can be repeated ~ (similar to an array). Every message type has a
different number of fields/components and sub-components. Empty fields at the
end of a record can be omitted.
Example of a simple record:
OBX|14|NM|0050–5^Calcium||8.9|mg/dl|8.4–10.4||||F
I have already implemented an efficient parser which turns a whole file into a
hirarchial data structure in C. I want to implement an application that
allows editing these files like in a text editor. I want to keep the underlaying
hirarchial datastructure at any point, so it is easy to validate structure and
content of every field, upon user changes, quickly.
Also note worthy is, that I already implemented a viewer where the document
strcuture is displayed in a QTreeView. The structure is using a QAbstractItemModel.
Now to my actual question: how would I approach the problem of having a text editor
with a structured data model in Qt. I have done some research:
there is a QDomDocument, it seems to be made to work with xml data. I could convert my structure into XML but can the dom object be used with a text editor ?
Is there a way to bind a tree like document model to QTextEdit or QPlainTextEdit?
is it possible to bind a QAbstractItemModel to a document in QTextEdit or QPlainTextEdit?
What would be the best approach to tackle the problem of having a textual representation of a tree that gets updated once text and/or stricture is changed by editing in the text field?
Performance and cross platform capability is important, so this project will be implemented in C++.
Thanks for any advice and examples appreciated if you have any.
-S

Is there a window setting for keeping a selected passage of text even after the text has been changed/refreshed?

I have a CEdit-Box containing some text, which is being refreshed by a thread every ~0.25 seconds. The problem is that everytime the text is being refreshed, a possible selection of text is being erased.
I found 2 ways to avoid this so far:
My implementation right now (1) :
Use quite a passage of logic in order to assure that the text is actually really changing, and not just refreshing itself with the exact same string. This is kind of avoiding the problem, but it feels very clunky to be honest.
Another idea (2) :
Every cycle, before the text is being refreshed, we need to grab the selection we currently have, store it, and try to reconstruct it after our text has been refreshed. However, I don't know how this would turn out if the new text doesn't contain our old string at all. I guess the functionality is implemented in WTL, but I don't think this is a very good approach.
Is there any other way? Something like a control setting which would do something like this?

QTextEdit: typing in HTML/richttext

I have a QTextEdit and want the user to be able to type rich text which will then automatically be (correctly) shown in the widget (so: formatted).
It works fine when setting the text programmatically (using setText()), but not when manually typed. See picture below.. "Input" is set using setText, the following line is manually typed. I would like this line to automatically be formatted a
What's the (easiest) way to do this? The only way I can think about it to manually catch key events and explicitly set the text as HTML.. But I'm sure there's a better way.
Manual typed html gets escaped, the < will become a < etc . .
You wouldn't be able to edit it if that would not be the case, for obvious reasons.
You could try adding a [render] button or something like that to render the entered text to html. Trying to render on keypress is very dangerous because it makes it terribly inconvenient and counter-intuitive to type something and then have it magically change the output. Also un-finished markup will probably throw a stick in your wheel.
Also pasting from a rich text source (for example a webpage) keeps the formatting.
As "the JinX" already said it will not be so intuitive if you try to capture every key event and then try to change the text to render in HTML.
Though you can use some special key sequences, say "shift+return key" to change the text of current line/entire textedit to to html formatted one.
This is just a suggestion.
In this case more than implementation it is also about what a user will expect.
Changing the text of 1 line/entire textedit from plain to HTML would be easy to achieve as well.