Commit Graph

42 Commits

Author SHA1 Message Date
Andreas Kling
fd5eb79d19 LibGUI: Make GMenu inherit from CObject
This is primarily to make it possible to pass a GMenu* where a CObject*
is expected.
2019-12-09 21:05:44 +01:00
Andreas Kling
6f4c380d95 AK: Use size_t for the length of strings
Using int was a mistake. This patch changes String, StringImpl,
StringView and StringBuilder to use size_t instead of int for lengths.
Obviously a lot of code needs to change as a result of this.
2019-12-09 17:51:21 +01:00
Sasan Hezarkhani
5c2ef01f7b LibGUI: Expose a function to clear GTextEditor selection 2019-12-03 12:51:27 +01:00
Andreas Kling
00a91bb02c LibGUI: Consolidate and simplify commands used for insertion/removal
This patch adds InsertTextCommand and RemoveTextCommand.
These two commands are used to ... insert and remove text :^)

The bulk of the logic is moved into GTextDocument, and we now use the
command's redo() virtual to perform the action. Or in other words, when
you type into the text editor, we create an InsertTextCommand, push it
onto the undo stack, and call redo() on it immediately. That's how the
text gets inserted.

This makes it quite easy to implement more commands, as there is no
distinction between a redo() and the initial application.
2019-11-30 16:54:05 +01:00
Andreas Kling
ee8773c586 LibGUI: Move the undo stack from GTextEditor to GTextDocument
Since the same document can be edited by multiple editors, we need to
keep the undo stack with the document for coherency.
2019-11-30 13:05:17 +01:00
Andreas Kling
fc14bdd442 LibGUI: Don't recompute line-wrapping over and over during set_text()
This makes the TextEditor start up fast for large files again.
2019-11-23 17:41:14 +01:00
Andreas Kling
c8e02e60a6 HackStudio+LibGUI: Implement matching curly brace highlighting
This works for C++ syntax highlighted text documents by caching the C++
token type in a new "arbitrary data" member of GTextDocumentSpan.

When the cursor is placed immediately before a '{' or immediately after
a '}', we highlight both of these brace buddies by changing their
corresponding spans to have a different background color.

..and spans can also now have a custom background color. :^)
2019-11-18 19:10:06 +01:00
ctfloyd
a3520bfdfd GTextEditor: Alt+Shift+S alphabetically sorts selected lines 2019-11-15 21:01:12 +01:00
Andreas Kling
ca538b6cee LibGUI: Add a simple GWidget class registry/factory
You can now register a GWidget subclass with REGISTER_GWIDGET(class)
and it will be available for factory construction through the new
GWidgetClassRegistration interface.

To obtain a GWidgetClassRegistration for a given class name, you call
GWidgetClassRegistration::find(class_name). You can also iterate over
all the registered classes using GWCR::for_each(callback).

This will be very useful for implementing a proper GUI designer, and
also in the future for things like script bindings.

NOTE: All of the registrations are done in GWidget.cpp at the moment
since I ran into trouble with the fricken linker pruning the global
constructors this mechanism relies on. :^)
2019-11-10 12:57:37 +01:00
Rhin
503fe37eaa TextEditor: Enable/disable undo & redo buttons based on availability (#740) 2019-11-09 08:50:39 +01:00
Andreas Kling
fa77a57257 GTextEditor: Allow moving the selected line(s) up/down in bulk
You can now press Ctrl+Shift+Up/Down in a GTextEditor and the currently
selected line(s) will all move together one step up/down.

If there is no selection, we move the line with the cursor on it. :^)
2019-11-08 20:14:42 +01:00
Andreas Kling
c16b1a515e GTextEditor: Add a way to flush any pending on_change notifications
Since on_change handlers can alter the text document we're working on,
we have to make sure they've been run before we try looking at spans.
This fixes some flakiness when a paint happened before HackStudio had
a chance to re-highlight some C++ while editing it.

The design where clients of GTextEditor perform syntax highlighting in
the "arbitrary code execution" on_change callback is not very good.
We should find a way to move highlighting closer to the editor.
2019-11-08 19:51:10 +01:00
rhin123
96c1e36ed3 TextEditor: Added redo functionality & proper stack manipulation
Added redo functionality & added m_undo_stack_index that moves back &
forth on the stack depending on the undo's & redo's.
2019-11-07 22:29:59 +01:00
rhin123
9e608885d1 TextEditor: Added undo functionality
Created a stack where a vector of undo actions are stored.
2019-11-03 20:21:14 +01:00
Andreas Kling
1282b778e7 LibGUI: Move GTextPosition and GTextRange LogStream<< to their headers 2019-11-03 09:57:14 +01:00
Andreas Kling
b8bf998b61 LibGUI: Move text search functions from GTextEditor to GTextDocument
Also add a find_all() that retuns a Vector<GTextRange> and simply does
a find_next() loop, returning all the matching ranges.
2019-11-01 21:31:06 +01:00
Andreas Kling
a4709502d1 GTextEditor: Make text_position_at() protected 2019-10-30 20:28:44 +01:00
Andreas Kling
9b13a3905b LibGUI: Support multiple GTextEditors editing the same GTextDocument
With this patch, you can now assign the same GTextDocument to multiple
GTextEditor widgets via GTextEditor::set_document().

The editors have independent cursors and selection, but all changes
are shared, and immediately propagate to all editors.

This is very unoptimized and will do lots of unnecessary invalidation,
especially line re-wrapping and repainting over and over again.
2019-10-27 19:36:59 +01:00
Andreas Kling
f96c683543 LibGUI: Move visual line metadata from GTextDocument to GTextEditor
This patch decouples GTextDocument and GTextDocumentLine from the line
wrapping functionality of GTextEditor.

This should basically make it possible to have multiple GTextEditors
editing the same GTextDocument. Of course, that will require a bit more
work since there's no paint invalidation yet.
2019-10-27 18:00:07 +01:00
Andreas Kling
f1c6193d6d LibGUI: Move GTextDocument out of GTextEditor
The idea here is to decouple the document from the editor widget so you
could have multiple editors being views onto the same document.

This doesn't work yet, since the document and editor are coupled in
various ways still (including a per-line back-pointer to the editor.)
2019-10-27 16:44:16 +01:00
Andreas Kling
de2b25e2c1 LibGUI: Move GTextRange and GTextPosition to their own header files 2019-10-27 11:23:53 +01:00
Andreas Kling
bc2026d26d LibGUI: Make GTextEditor::Span have a range instead of two positions
A GTextRange is really just two GTextPositions (start and end) anyway.
This way we can say nice things like "if (range.contains(position))"
2019-10-26 15:33:19 +02:00
Andreas Kling
4fa8acf6ea GTextEditor: The Home key should jump to the first non-space character
Press Home twice to get to column 0. This feels way more natural.
2019-10-26 14:02:39 +02:00
Andreas Kling
59107a7cfe GTextEditor: Allow setting a custom font for each span 2019-10-26 00:13:07 +02:00
Andreas Kling
0d53d74d5f GTextEditor: Add a "span" mechanism for having custom-style text ranges
It's now possible to give GTextEditor a vector of Span objects.
Spans currently tell the editor which color to use for each character
in the span. This can be used to implement syntax highlighting :^)
2019-10-25 21:07:02 +02:00
Andreas Kling
0a0dfeee8b LibGUI: Make GTextEditor::set_cursor() public
Also clamp the cursor value to the possible range instead of asserting
when trying to set a cursor past the end of the document.
2019-10-21 19:01:27 +02:00
Andreas Kling
93851c3832 LibGUI: Convert GTextBox, GTextEditor and GResizeCorner to ObjectPtr 2019-09-21 15:46:47 +02:00
Andreas Kling
906582d8df GTextEditor: Fix wrong width calculations with line-wrapping enabled
There were various little mistakes in the width calculations used by
the line-wrapping layout code.

With this patch, we should no longer see the horizontal scrollbar get
enabled with line-wrapping enabled. I will hide the scrollbar in a
separate patch.
2019-09-01 20:04:25 +02:00
Andreas Kling
3e2e086011 LibGUI: Add a way for GWidget subclasses to learn that the font changed
Use this in GTextEditor to update the vertical scrolling step size so
we always scroll one-line-at-a-time.
2019-09-01 12:26:35 +02:00
Andreas Kling
a6be213287 GTextEditor: Add add_custom_context_menu_action()
This allows embedders to add their own custom GAction set to a text
editor's context menu.
2019-08-25 21:33:08 +02:00
Andreas Kling
fa20dcafb5 GTextEditor: Simplify computation of visual selection start/end
Add Line::visual_line_containing(int column) to easily convert a column
number into a visual line index.
2019-08-25 14:04:46 +02:00
Andreas Kling
5aac652b4b GTextEditor: Relayout when the line-wrapping setting is changed 2019-08-25 12:23:14 +02:00
Andreas Kling
9752e683f6 GTextEditor: Start working on a line-wrapping feature
This is not finished, but since the feature is controlled by a runtime
flag, the broken implementation should not affect users of this widget
too much (in theory :^).)
2019-08-25 11:24:23 +02:00
Andreas Kling
23b70d5c59 GTextEditor: Clean up some of the rect computations
Moving some rect computations to separate functions to make it easier
to reuse them.
2019-08-25 07:17:09 +02:00
Andrew Weller
e75e33eb46 TextEditor: Replaced 'Find' button with 'Prev' and 'Next' buttons. 2019-08-24 21:57:42 +02:00
Andreas Kling
0c72371ad9 GTextEditor: Implement a simple text search API
- GTextRange find(const StringView& needle, const GTextPosition& start)

This function searches for the needle in the haystack (the full text)
and returns a GTextRange for the closest match after "start".
If the needle is not found, it returns an invalid GTextRange.
If no "start" position is provided, the search begins at the head of
the text document. :^)
2019-08-21 21:23:17 +02:00
Andreas Kling
d5431a6df8 GTextEditor: Rename is_automatic_indentation_enabled() API
The previous name was is_automatic_indentation() which sounds weird.
2019-08-21 19:33:54 +02:00
Andreas Kling
9e5c5627d5 GTextEditor: Give Line objects a back-reference to the GTextEditor
This will allow us to do more complicated things in Line without having
to pass the editor around all the time.
2019-08-21 19:32:39 +02:00
Andreas Kling
a599317624 LibCore: Introduce a C_OBJECT macro.
This macro goes at the top of every CObject-derived class like so:

class SomeClass : public CObject {
    C_OBJECT(SomeClass)
public:
    ...

At the moment, all it does is create an override for the class_name() getter
but in the future this will be used to automatically insert member functions
into these classes.
2019-07-25 19:49:28 +02:00
Andreas Kling
2196f17c10 LibGUI: Convert Vector<OwnPtr> to NonnullOwnPtrVector.
This is turning out really nice so far. :^)
2019-07-24 09:13:06 +02:00
Andreas Kling
1c0669f010 LibDraw: Introduce (formerly known as SharedGraphics.)
Instead of LibGUI and WindowServer building their own copies of the drawing
and graphics code, let's it in a separate LibDraw library.

This avoids building the code twice, and will encourage better separation
of concerns. :^)
2019-07-18 10:18:16 +02:00
Andreas Kling
04b9dc2d30 Libraries: Create top level directory for libraries.
Things were getting a little crowded in the project root, so this patch
moves the Lib*/ directories into Libraries/.
2019-07-04 16:16:50 +02:00