AvalonEdit: Copy takes forever for large highlighted text files - regex

Actually this was asked on http://community.sharpdevelop.net/forums/p/21949/56153.aspx#56153 but with no answer yet - so I try it here.
I'm using Avalon Edit (ICSharpCode.AvalonEdit.dll 4.4.2) in a WPF 4.0 application.
I have loaded a text file (~7 MBytes) into the editor. When I apply syntax highlighting and then coping (Control-A and Control-C) the whole text it takes forever (without highlighting it's done in a second)
When I break into debugger I get the following callstack (shortened):
System.Text.RegularExpressions.RegexInterpreter.Go()
System.Text.RegularExpressions.RegexRunner.Scan(regex, text, textbeg, textend, textstart, prevlen, quick, timeout)
System.Text.RegularExpressions.Regex.Run(quick, prevlen, input, beginning, length, startat)
System.Text.RegularExpressions.Regex.Match(input, beginning, length)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightNonSpans(until)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightLineInternal(line)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightLineAndUpdateTreeList(line, lineNumber)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightLine(lineNumber)
ICSharpCode.AvalonEdit.Highlighting.HtmlClipboard.CreateHtmlFragment(document, highlighter, segment, options)
ICSharpCode.AvalonEdit.Editing.Selection.CreateHtmlFragment(options)
ICSharpCode.AvalonEdit.Editing.Selection.CreateDataObject(textArea)
ICSharpCode.AvalonEdit.Editing.EditingCommandHandler.CopySelectedText(textArea)
ICSharpCode.AvalonEdit.Editing.EditingCommandHandler.OnCopy(target, args)
It seems the editor creates html-based content for the clipboard and uses RegularExpressions which takes forever (~30 seconds).
Question: Does anyone know a possibility to disable the syntax highlighting for the copy action so that only pure text is copied to the clipboard.

I got the answer from DanielGrunwald on SharpDevelop that I want to share:
In avalonedit 4.X it's not possible to disable html copy to clipboard. But in 5.X you can do that.
With:
AvalonEdit.TextEditor TextView
write the following to register the callback for the before-copy event:
DataObject.AddSettingDataHandler(TextView, onTextViewSettingDataHandler);
to register a user handler that is called before cliboard copying is processed. In that handler cancel the html format (e.g. depending on the document size). Example:
static public void onTextViewSettingDataHandler(object sender, DataObjectSettingDataEventArgs e)
{
var textView = sender as TextEditor;
if (textView != null && e.Format == DataFormats.Html && textView.Text.Count() > MaxDocByteSizeForHtmlCopy)
{
e.CancelCommand();
}
}
With that code you can prevent that hanger, but of course formatting is not preserved when the content is pasted (e.g. into Word).

Related

WinRT/C++ TextBox doesn't accept any character input in ContentDialog

I have a desktop program with a WinRT/C++ library (and Microsoft.UI.Xaml.Controls 2.8 prerelease), which in the application will bring up a ContentDialog with a TextBox as its content. However, unfortunately the TextBox does not accept any character input from the keyboard, even though the TextBox is in a focused state. For the experiment so far, the TextBox can only accept input from the Copy and Paste methods. So, is there any workaround or manipulation that can be done to solve this problem?
Code:
TextBox tbox{};
ContentDialog dialog{};
dialog.Title(box_value(L"Title"));
dialog.Content(tbox);
dialog.PrimaryButtonText(L"Create");
dialog.CloseButtonText(L"Cancel");
dialog.XamlRoot(XamlRoot());
auto result = co_await dialog.ShowAsync();
if (result == ContentDialogResult::Primary)
{
// Create new data from tbox.Text()
}

Performantly appending (rich) text into QTextEdit or QTextBrowser in Qt

QTextEdit can be appended text to simply using append(). However, if the document is rich text, every time you append to the document, it is apparently reparsed. This seems like a bit of a trap in Qt.
If you're using the edit box as a log window and appending text in fast successions as a result of external signals, the appending can easily hang your app with no intermediate appends shown until each of the appends have completed.
How do I append rich text to a QTextEdit without it slowing down the entire UI?
If you want each append to actually show quickly & separately (instead of waiting until they've all been appended before they are shown), you need to access the internal QTextDocument:
void fastAppend(QString message,QTextEdit *editWidget)
{
const bool atBottom = editWidget->verticalScrollBar()->value() == editWidget->verticalScrollBar()->maximum();
QTextDocument* doc = editWidget->document();
QTextCursor cursor(doc);
cursor.movePosition(QTextCursor::End);
cursor.beginEditBlock();
cursor.insertBlock();
cursor.insertHtml(message);
cursor.endEditBlock();
//scroll scrollarea to bottom if it was at bottom when we started
//(we don't want to force scrolling to bottom if user is looking at a
//higher position)
if (atBottom) {
scrollLogToBottom(editWidget);
}
}
void scrollLogToBottom(QTextEdit *editWidget)
{
QScrollBar* bar = editWidget->verticalScrollBar();
bar->setValue(bar->maximum());
}
The scrolling to bottom is optional, but in logging use it's a reasonable default for UI behaviour.
Also, if your app is doing lots of other processing at the same time, appending this at the end of fastAppend, will prioritize actually getting the message displayed asap:
//show the message in output right away by triggering event loop
QCoreApplication::processEvents();
This actually seems a kind of trap in Qt. I would know why there isn't a fastAppend method directly in QTextEdit? Or are there caveats to this solution?
(My company actually paid KDAB for this advice, but this seems so silly that I thought this should be more common knowledge.)

How to disable Edit control's focus on Dialog first launch?

Hello everybody reading this. Thanks in advance for your time.
One thing before question: I DO NOT use neither MFC nor Windows Forms, just WinApi in C++.
Well, I am making a polynomial calculator in Visual C++. I added a Dialog to it, which was created in resources (.rc file) using drag'n'drop method. I suppose there would be no such a problem if i created my Dialog with CreateWindowEx (but I don't want to).
My Dialog has a few of Edit Controls. Everything is fine except that when the Dialog is launched, one of Edit controls takes focus to be ready to take keyboard input.
I have included management of EN_KILLFOCUS (Edit sends it to parent when loses focus due to selecting another control).
Here I read from control to wstring (string of wide characters - _UNICODE is defined), use some kind of parser to verify this wstring and remove bad characters, and then put correct string into the same edit control. It works fine, but here is the source of my problem:
When there was no input, parser returns string "0" (not the NULL, string is just set to "0"), as if control had focus and then lost it even before I clicked anything in Dialog.
Due to that, and something else (this is what I have to figure out), at the Dialog launch parser puts this string "0" to edit.
I want to make my edit not be able to take input from keyboard until i click one of the Edits (including this one).
If it is not possible, I want to clear the whole text at the beginning of dialog (being able to take input is not a problem, I just want to prevent parser from entering string "0" at the beginning)
My code:
In DlgProc I have:
//up here is switch to manage all controls
case MyEditID: // here is ID of one of my edits from resources
switch (HIWORD(wParam))
{
case EN_KILLFOCUS: // edit lost focus - another control selected
if (LOWORD(wParam)==MyEditID) //necessary to determine if
// one of allowed Edits sent this message
// because I have also other Edits
{
GetDlgItemText(hPanel, LOWORD(wParam), MyTempWcharArray, 100);
MyTempString.assign(MyTempWcharArray);
w1 = polynomial(MyTempWcharArray); // parser takes the string
// and removes bad chars in constructor
// polynomial is my class - you don't have to care of it
// w1 is declared before as object of polynomial class
MyTempString = w1.ConversionToString();
SetDlgItemText(hDialog, LOWORD(wParam), sw1);
}
break;
}
break;
does it matter what integer number is set to Edit's ID?
I know SetFocus(), and WM_SETFOCUS message. In this case I just can't get this working.
If i haven't included something important to make you see my point please let me know. I'm sorry I'm just a newbie in WinAPI world.
EDIT:
For those with a similar problem: Do not do this:
I made an workaround with global variable ProcessKillFocus set to false indicating that instructions in message management should not be processed, except that at the end (just before break;) I am changing it to true, so next time and later it will be processed:
case EN_KILLFOCUS:
if (ProcessKillFocus && LOWORD(wParam)==MyEditID)
{
// first time global ProcessKillFocus is false so all this is skipped
// 2nd time and later do all the stuff
}
ProcessKillFocus = true;
break;
Huge thanx to Sheyros Adikari for making my question easy to understand!!!
Huge thanx to patriiice for simple answer on a huge messing question!!!
ANSWER:
BTW: patriiice, I tried this:
case WM_INITDIALOG:
SetFocus(GetDlgItem(hDialog, Desired_Control_ID));
return (INT_PTR)FALSE;
break;
IT JUST WORKS!!!
You have to return FALSE to WM_INITDIALOG message and set the correct focus by yourself.

Preventing newline when pressing enter in WxWidgets text control

I am writing an application with WxWidgets, and have run into an issue with a multiline text control (wxTextControl). It is the input field in a chat window, and it needs to be multi line in case the user types a longer message that needs to wrap. I want the send event, e.g. the action that is taken when the Send button is pressed, to be executed when the user presses enter in the control. I have this working using the wxEVT_COMMAND_TEXT_ENTER event, with the wxTE_PROCESS_ENTER style enabled. However, the problem is that while the send command does get executed, a new line character \n is also appended to the text (this happens after the send command and after I have cleared the text, resulting in an empty field except for a new line). I tried to avoid this by trapping both char and key down events, but for some reason they are not firing.
I simply want to avoid the new line being shown at all. Does anyone have any tips?
I am developing on Windows, but the application is meant to run on all platforms supported by WxWidgets.
You might track wxEVT_COMMAND_TEXT_UPDATED and clear the editor when a new-line is entered (it assumes you are also clearing the editor when Enter is pressed).
BEGIN_EVENT_TABLE(Test,wxFrame)
EVT_TEXT_ENTER(EditorID, Test::OnEnter)
EVT_TEXT(EditorId, Test::OnText)
END_EVENT_TABLE()
void Test::OnEnter(wxCommandEvent&)
{
Send(editor->GetValue());
editor->Clear();
}
void Test::OnText(wxCommandEvent& event)
{
if (event.GetString() == wxT("\n")) { //seems to work, not much info in documentation?
editor->Clear();
}
}

Qt 4.x: how to implement drag-and-drop onto the desktop or into a folder?

I've written a little file-transfer application written in C++ using Qt 4.x ... it logs into a server, shows the user a list of files available on the server, and lets the user upload or download files.
This all works fine; you can even drag a file in from the desktop (or from an open folder), and when you drop the file icon into the server-files-list-view, the dropped file gets uploaded to the server.
Now I have a request for the opposite action as well... my users would like to be able to drag a file out of the server-files-list-view and onto the desktop, or into an open folder window, and have that file get downloaded into that location.
That seems like a reasonable request, but I don't know how to implement it. Is there a way for a Qt application to find out the directory corresponding to where "drop event" occurred, when the icon was dropped onto the desktop or into an open folder window? Ideally this would be a Qt-based platform-neutral mechanism, but if that doesn't exist, then platform-specific mechanisms for MacOS/X and Windows (XP or higher) would suffice.
Any ideas?
Look at QMimeData and its documentation, it has a virtual function
virtual QVariant retrieveData ( const QString & mimetype, QVariant::Type type ) const
this means to do you drag to the outside you implement this functions accordingly
class DeferredMimeData : public QMimeData
{
DeferredMimeData(QString downloadFilename) : m_filename(downloadFilename)
virtual QVariant retrieveData (const QString & mimetype, QVariant::Type type) const
{
if (mimetype matches expected && type matches expected)
{
perform download with m_filename
}
}
}
The delayed encoding examples shows this principle.
You will probably also have to override hasFormat and formats to provide the appropriate types, application/octet-stream probably being the one that might get you the most play, you will probably have to read up on how windows specifically handles drag and drop using mime types.
I don't know how you will supply the file name under which the file is saved, but you will probably have to get into the windows side of things. Looking at the source of QWindowsMime might also help. There might me a multiple step process where you will get requests for text/uri-list data for the filenames and then application/octet-stream for the data.
Hope this helps
I think you are going about that in the wrong way.
You don't care where the drop goes to, you just know it was dropped. In the DropEvent, download the file to a temporary location and then set the mime data to be what was downloaded. Granted this may end up with a second copy to the hard drive, it will be cross platform from the start. You may be able to optimize it afterward with platform specific calls.
Take a look at Dropsite example to see how the mime data works from other sources...
Edit:
It looks like the double copy method is the standard if you don't write a shell extension (for windows at least). Filezilla and 7zip both do that, they return a "text/uri-list" mime type with a temp location. This way explorer copies the data from the temp location to the real one. Your application can do the same, create a temp file (or just the name) with no data. Using the delayed encoding example, create the data on the drop. This whole operation is very platform specific it appears. On Linux (running KDE) I can drag and drop just based on mime type. It appears that windows is not as flexible.
You just implement the drag part. You don't need to know where the drop happen because the application receiving it will handle it and decide where to store your file.
You create a QMimeData, not sure which type but from what i saw here and here, maybe "application/octet-stream" with your file as data in a QByteArray or "text/uri-list" with url to your file.
You create a QDrag, and use its setMimeData() method and exec() (not sure about which QT::DropAction to choose).
Here is an example (disclamer : i did it with color not files) :
void DraggedWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
start_pos = event->pos();
}
void DraggedWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
int distance = (event->pos() - start_pos).manhattanLength();
if (distance >= QApplication::startDragDistance())
{
/* Drag */
QMimeData *mime_data = new QMimeData;
mime_data->setData( ... );
QDrag *drag = new QDrag(this);
drag->setMimeData(mime_data);
drag->exec(Qt::CopyAction);
}
}
}
Sorry it's quite incomplete, i hope it helps a little.