PCL
CodeEditor.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/CodeEditor.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_CodeEditor_h
53 #define __PCL_CodeEditor_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Flags.h>
60 
61 #ifndef __PCL_BUILDING_PIXINSIGHT_APPLICATION
62 
63 #include <pcl/AutoPointer.h>
64 #include <pcl/Control.h>
65 
66 #endif // !__PCL_BUILDING_PIXINSIGHT_APPLICATION
67 
68 namespace pcl
69 {
70 
71 // ----------------------------------------------------------------------------
72 
87 namespace FindMode
88 {
89  enum mask_type
90  {
91  RegExp = 0x00000001, // Search with a regular expression
92  Backward = 0x00000010, // Search toward the beginning of text
93  CaseSensitive = 0x00000020, // Case-sensitive search
94  WholeWords = 0x00000040, // Search for whole words
95  SelectionOnly = 0x00000100, // Replace within the current selection
96  Incremental = 0x00000200, // Perform an incremental find operation
97  Default = CaseSensitive|WholeWords
98  };
99 }
100 
105 
106 // ----------------------------------------------------------------------------
107 
108 #ifndef __PCL_BUILDING_PIXINSIGHT_APPLICATION
109 
110 // ----------------------------------------------------------------------------
111 
118 class PCL_CLASS CodeEditor : public Control
119 {
120 public:
121 
129 
133  ~CodeEditor() override
134  {
135  }
136 
146 
151  String FilePath() const;
152 
176  void SetFilePath( const String& path );
177 
181  String Text() const;
182 
186  void SetText( const String& text );
187 
195  IsoString EncodedText( const IsoString& encoding = "UTF-8" ) const;
196 
205  {
206  return EncodedText( "UTF-8" );
207  }
208 
217  {
218  return EncodedText( "ISO-8859-1" );
219  }
220 
230  void SetEncodedText( const IsoString& text, const IsoString& encoding = "UTF-8" );
231 
239  void SetTextUTF8( const IsoString& text )
240  {
241  SetEncodedText( text, "UTF-8" );
242  }
243 
251  void SetTextISO88591( const IsoString& text )
252  {
253  SetEncodedText( text, "ISO-8859-1" );
254  }
255 
260  void ClearText();
261 
268  bool IsReadOnly() const;
269 
279  void SetReadOnly( bool readOnly = true );
280 
298  bool Save( const String& filePath, const IsoString& encoding = "UTF-8" );
299 
308  bool SaveUTF8( const String& filePath )
309  {
310  return Save( filePath, "UTF-8" );
311  }
312 
321  bool SaveISO88591( const String& filePath )
322  {
323  return Save( filePath, "ISO-8859-1" );
324  }
325 
338  bool Load( const String& filePath, const IsoString& encoding = "UTF-8" );
339 
348  bool LoadUTF8( const String& filePath )
349  {
350  return Load( filePath, "UTF-8" );
351  }
352 
361  bool LoadISO88591( const String& filePath )
362  {
363  return Load( filePath, "ISO-8859-1" );
364  }
365 
370  int NumberOfLines() const;
371 
376  int NumberOfCharacters() const;
377 
388 
392  int CursorLine() const
393  {
394  return CursorPosition().y;
395  }
396 
400  int CursorColumn() const
401  {
402  return CursorPosition().x;
403  }
404 
410  void SetCursorPosition( int line, int column );
411 
417  void SetCursorPosition( const Point& pos )
418  {
419  SetCursorPosition( pos.y, pos.x );
420  }
421 
431  bool IsInsertMode() const;
432 
439  void SetInsertMode( bool insert = true );
440 
447  void SetReplaceMode( bool replace = true )
448  {
449  SetInsertMode( !replace );
450  }
451 
462  bool IsBlockSelectionMode() const;
463 
471  void SetBlockSelectionMode( bool blockMode = true );
472 
480  void SetLineSelectionMode( bool lineMode = true )
481  {
482  SetBlockSelectionMode( !lineMode );
483  }
484 
497  bool IsDynamicWordWrapMode() const;
498 
504  void SetDynamicWordWrapMode( bool wrapMode = true );
505 
509  int UndoSteps() const;
510 
514  int RedoSteps() const;
515 
519  bool HasSelection() const;
520 
531  Rect Selection() const;
532 
541  void SetSelection( int fromLine, int fromCol, int toLine, int toCol );
542 
550  void SetSelection( const Rect& r )
551  {
552  SetSelection( r.y0, r.x0, r.y1, r.x1 );
553  }
554 
560 
567  void InsertText( const String& text );
568 
573  void Undo();
574 
579  void Redo();
580 
585  void Cut();
586 
591  void Copy();
592 
598  void Paste();
599 
604  void Delete();
605 
609  void SelectAll();
610 
615  void Unselect();
616 
626 
642  int HighlightAllMatches( const String& toFind, FindModes mode );
643 
648  void ClearMatches();
649 
662  bool Find( const String& toFind, FindModes mode );
663 
675  bool ReplaceSelection( const String& replaceWith );
676 
694  int ReplaceAll( const String& toFind, const String& replaceWith, FindModes mode );
695 
710  using editor_event_handler = void (Control::*)( CodeEditor& sender );
711 
726  using cursor_event_handler = void (Control::*)( CodeEditor& sender, int line, int col );
727 
750  using selection_event_handler = void (Control::*)( CodeEditor& sender, int fromLine, int fromCol, int toLine, int toCol );
751 
764  using state_event_handler = void (Control::*)( CodeEditor& sender, bool state );
765 
779  void OnTextUpdated( editor_event_handler handler, Control& receiver );
780 
796 
812 
829 
846 
862 
863 private:
864 
865  struct EventHandlers
866  {
867  editor_event_handler onTextUpdated = nullptr;
868  cursor_event_handler onCursorPositionUpdated = nullptr;
869  selection_event_handler onSelectionUpdated = nullptr;
870  state_event_handler onOverwriteModeUpdated = nullptr;
871  state_event_handler onSelectionModeUpdated = nullptr;
872  state_event_handler onDynamicWordWrapModeUpdated = nullptr;
873 
874  EventHandlers() = default;
875  EventHandlers( const EventHandlers& ) = default;
876  EventHandlers& operator =( const EventHandlers& ) = default;
877  };
878 
879  AutoPointer<EventHandlers> m_handlers;
880  Control m_lineNumbers;
881 
882  friend class CodeEditorEventDispatcher;
883 };
884 
885 // ----------------------------------------------------------------------------
886 
887 #endif // __PCL_BUILDING_PIXINSIGHT_APPLICATION
888 
889 } // pcl
890 
891 #endif // __PCL_CodeEditor_h
892 
893 // ----------------------------------------------------------------------------
894 // EOF pcl/CodeEditor.h - Released 2024-06-18T15:48:54Z
Client-side interface to a PixInsight CodeEditor control.
Definition: CodeEditor.h:119
void SetReadOnly(bool readOnly=true)
void SetTextUTF8(const IsoString &text)
Definition: CodeEditor.h:239
void SetTextISO88591(const IsoString &text)
Definition: CodeEditor.h:251
void SetDynamicWordWrapMode(bool wrapMode=true)
bool Load(const String &filePath, const IsoString &encoding="UTF-8")
int NumberOfLines() const
int CursorLine() const
Definition: CodeEditor.h:392
int UndoSteps() const
String FilePath() const
void InsertText(const String &text)
bool IsDynamicWordWrapMode() const
bool LoadUTF8(const String &filePath)
Definition: CodeEditor.h:348
void SetSelection(const Rect &r)
Definition: CodeEditor.h:550
IsoString EncodedText(const IsoString &encoding="UTF-8") const
void SetLineSelectionMode(bool lineMode=true)
Definition: CodeEditor.h:480
void SetInsertMode(bool insert=true)
bool LoadISO88591(const String &filePath)
Definition: CodeEditor.h:361
bool SaveUTF8(const String &filePath)
Definition: CodeEditor.h:308
int HighlightAllMatches(const String &toFind, FindModes mode)
int ReplaceAll(const String &toFind, const String &replaceWith, FindModes mode)
bool HasSelection() const
bool Find(const String &toFind, FindModes mode)
Point CursorPosition() const
Control & LineNumbersControl()
bool IsInsertMode() const
void SetFilePath(const String &path)
Rect Selection() const
void SetCursorPosition(int line, int column)
bool Save(const String &filePath, const IsoString &encoding="UTF-8")
void SetSelection(int fromLine, int fromCol, int toLine, int toCol)
void SetBlockSelectionMode(bool blockMode=true)
void SetEncodedText(const IsoString &text, const IsoString &encoding="UTF-8")
int NumberOfCharacters() const
IsoString TextUTF8() const
Definition: CodeEditor.h:204
int CursorColumn() const
Definition: CodeEditor.h:400
bool ReplaceSelection(const String &replaceWith)
bool IsBlockSelectionMode() const
String SelectedText() const
IsoString TextISO88591() const
Definition: CodeEditor.h:216
bool GotoMatchedParenthesis()
String Text() const
bool IsReadOnly() const
void SetCursorPosition(const Point &pos)
Definition: CodeEditor.h:417
void SetText(const String &text)
void SetReplaceMode(bool replace=true)
Definition: CodeEditor.h:447
int RedoSteps() const
CodeEditor(Control &parent=Control::Null())
~CodeEditor() override
Definition: CodeEditor.h:133
bool SaveISO88591(const String &filePath)
Definition: CodeEditor.h:321
Client-side interface to a PixInsight Control object.
Definition: Control.h:126
static Control & Null()
A type-safe collection of enumerated flags.
Definition: Flags.h:85
A generic point in the two-dimensional space.
Definition: Point.h:100
component x
Abscissa (horizontal, or X-axis coordinate).
Definition: Point.h:111
component y
Ordinate (vertical, or Y-axis coordinate).
Definition: Point.h:112
A generic rectangle in the two-dimensional space.
Definition: Rectangle.h:314
component x1
Horizontal coordinate of the lower right corner.
Definition: Rectangle.h:334
component y1
Vertical coordinate of the lower right corner.
Definition: Rectangle.h:335
component y0
Vertical coordinate of the upper left corner.
Definition: Rectangle.h:333
component x0
Horizontal coordinate of the upper left corner.
Definition: Rectangle.h:332
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5425
Unicode (UTF-16) string.
Definition: String.h:8113
void(Control::*)(CodeEditor &sender, int fromLine, int fromCol, int toLine, int toCol) selection_event_handler
Definition: CodeEditor.h:750
void OnDynamicWordWrapModeUpdated(state_event_handler handler, Control &receiver)
void OnSelectionModeUpdated(state_event_handler handler, Control &receiver)
void(Control::*)(CodeEditor &sender, int line, int col) cursor_event_handler
Definition: CodeEditor.h:726
void OnCursorPositionUpdated(cursor_event_handler handler, Control &receiver)
void(Control::*)(CodeEditor &sender, bool state) state_event_handler
Definition: CodeEditor.h:764
void OnOverwriteModeUpdated(state_event_handler handler, Control &receiver)
void(Control::*)(CodeEditor &sender) editor_event_handler
Definition: CodeEditor.h:710
void OnTextUpdated(editor_event_handler handler, Control &receiver)
void OnSelectionUpdated(selection_event_handler handler, Control &receiver)
PCL root namespace.
Definition: AbstractImage.h:77