1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        steshell.h
3 // Purpose:     wxSTEditorShell
4 // Author:      John Labenski
5 // Modified by:
6 // Created:     11/05/2002
7 // Copyright:   (c) John Labenski
8 // Licence:     wxWidgets licence
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 /// @file steshell.h
12 /// @brief wxSTEditorShell, a command prompt made out of a wxSTEditor.
13 
14 #ifndef _STESHELL_H_
15 #define _STESHELL_H_
16 
17 #include "wx/stedit/stedefs.h"
18 
19 /// wxSTEditorShell extra window styles.
20 enum STE_ShellStyle_Type
21 {
22     STE_SHELL_SINGLELINE = wxCB_SIMPLE ///< Make the shell single line only.
23                                        ///< Pass this to the constructor as the style
24                                        ///< use Get/SetWindowStyleFlag() to access.
25 };
26 
27 /// Set what to do with the cursor for the function wxSTEShell::CaretOnPromptLine().
28 enum STE_CaretPos_Type
29 {
30     STE_CARET_MOVE_NONE     = 0, ///< don't move cursor
31     STE_CARET_MOVE_LASTLINE = 1, ///< move cursor to the beginning of last line
32     STE_CARET_MOVE_ENDTEXT  = 2, ///< move cursor to the end of text
33 };
34 
35 //-----------------------------------------------------------------------------
36 /// @class wxSTEditorShell
37 /// @brief A basic console type interactive shell widget, like a DOS prompt.
38 ///
39 /// The editor shows a caret marker in the STE_MARGIN_MARKER and is readonly
40 ///   whenever the cursor is not on the last line. When the user presses enter
41 ///   a wxEVT_STESHELL_ENTER event is generated, where the event.GetString()
42 ///   containins the text of the last line.
43 /// Since the control updates its readonly state internally you should
44 ///   surround modifications with BeginWriteable() and EndWriteable() so that
45 ///   while you're in the middle of changing things it won't get set readonly on you.
46 //-----------------------------------------------------------------------------
47 
48 class WXDLLIMPEXP_STEDIT wxSTEditorShell : public wxSTEditor
49 {
50 public :
wxSTEditorShell()51     wxSTEditorShell() : wxSTEditor() { Init(); }
52 
53     wxSTEditorShell( wxWindow *parent, wxWindowID id = wxID_ANY,
54                      const wxPoint& pos = wxDefaultPosition,
55                      const wxSize& size = wxDefaultSize,
56                      long style = 0,
57                      const wxString& name = wxT("wxSTEditorShell") )
58     {
59         Init();
60         Create(parent, id, pos, size, style, name);
61     }
62 
63     virtual ~wxSTEditorShell();
64 
65     bool Create( wxWindow *parent, wxWindowID id = wxID_ANY,
66                  const wxPoint& pos = wxDefaultPosition,
67                  const wxSize& size = wxDefaultSize,
68                  long style = 0,
69                  const wxString& name = wxT("wxSTEditorShell") );
70 
71     enum margin_markerType
72     {
73         PROMPT_MARGIN = STE_MARGIN_MARKER, ///< The margin number to use to show the prompt markers
74         PROMPT_MARKER = STE_MARKER__MAX    ///< The marker number displayed at the prompt
75     };
76 
77     /// The preferred method to append text to the shell.
78     /// If you wish to write to the shell in your own routine be sure to
79     ///   take note of the code in this function.
80     virtual void AppendText(const wxString &text);
81 
82     /// Set the text currently at the prompt, may be multiline.
83     virtual void SetPromptText(const wxString& text);
84 
85     /// Get the text currently on the "cursor" line. The text may be multiline
86     ///   if the user has pasted multiline text into the shell.
87     virtual wxString GetPromptText();
88 
89     /// Block internal updates to the readonly state, surround all cursor
90     ///   movements or programmatic writes with a begin and endwriteable and
91     ///   the readonly state will not be updated during your work.
92     /// For BeginWriteable(make_writeable=true) the editor will be made writeable,
93     ///   else left in the current state if make_writeable=false.
94     void BeginWriteable(bool make_writeable = true);
95     /// For EndWriteable(check_ro=true) the editor will be set back to readonly
96     ///   if appropriate or left as is if check_ro = false.
97     void EndWriteable(bool check_ro = true);
98 
99     /// Get the last line with a prompt on it. It may not be the actual last
100     ///  line for multiline entries.
101     int GetPromptLine();
102 
103     /// Is the caret on the last line with a prompt marker?
104     /// @param option Sets what to do with it if not already on the last line.
105     /// @returns True if it is on the last line after any changes due to option
106     bool CaretOnPromptLine(STE_CaretPos_Type option = STE_CARET_MOVE_NONE);
107 
108     /// Get the previous/next string last typed in, for up/dn arrows.
109     /// line is the current string at the cursor for comparison.
110     wxString GetNextHistoryLine(bool fowards, const wxString &line);
111     /// Get the current index into the prompt history
GetHistoryIndex()112     size_t GetHistoryIndex() const { return m_line_history_index; }
113     /// Get the total number of history items
GetHistoryCount()114     size_t GetHistoryCount() const { return m_lineHistoryArray.GetCount(); }
115     /// Get the maximum number of history lines to store, < 0 means unlimited.
GetMaxHistoryLines()116     int  GetMaxHistoryLines() const { return m_max_history_lines; }
117     /// Set the maximum number of history lines to store, < 0 means unlimited.
118     void SetMaxHistoryLines(int max_lines);
119     /// Add a line to the history lines, checks for duplicates.
120     /// If set_index_to_last put current history index at end.
121     void AddHistoryLine(const wxString& string, bool set_index_to_last);
122 
123     /// Get/Set the maximum number of lines to store.
124     ///   < 0 for no max number (don't let it get out of hand) default is 10000
125     ///   overflow_lines allows extra lines to be added beyond max_lines
126     ///     before deleting a chunk to get back down to max lines.
127     ///     If overflow_lines = 0 then only max_lines are allowed which can make
128     ///     continuously appending lines once you reach max_lines slow.
129     bool SetMaxLines(int max_lines, int overflow_lines = 2000);
GetMaxLines()130     int  GetMaxLines() const { return m_max_lines; }
GetOverflowLines()131     int  GetOverflowLines() const { return m_overflow_lines; }
132 
133     // implementation
134 
135     /// returns if the editor should be readonly (is on last line)
136     ///    if set then set the proper readonly state
137     virtual bool CheckReadOnly(bool set);
138 
139     /// returns if the last line needs a prompt (margin marker)
140     ///   if set then set the marker if needed
141     virtual bool CheckPrompt(bool set);
142 
143     void OnKeyDown(wxKeyEvent &event);
144     void OnSTCUpdateUI(wxStyledTextEvent &event);
145 
146 protected:
147     wxArrayString m_lineHistoryArray;
148     int m_line_history_index;
149     int m_max_history_lines;
150 
151     int m_max_lines;
152     int m_overflow_lines;
153 
154     int m_writeable_count;
155 
156 private:
157     void Init();
158     DECLARE_EVENT_TABLE();
159     DECLARE_DYNAMIC_CLASS(wxSTEditorShell);
160 };
161 
162 #endif  // _STESHELL_H_
163