| 1 | //============================================================================ |
| 2 | // |
| 3 | // SSSS tt lll lll |
| 4 | // SS SS tt ll ll |
| 5 | // SS tttttt eeee ll ll aaaa |
| 6 | // SSSS tt ee ee ll ll aa |
| 7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
| 8 | // SS SS tt ee ll ll aa aa |
| 9 | // SSSS ttt eeeee llll llll aaaaa |
| 10 | // |
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
| 12 | // and the Stella Team |
| 13 | // |
| 14 | // See the file "License.txt" for information on usage and redistribution of |
| 15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 16 | //============================================================================ |
| 17 | |
| 18 | #ifndef PROMPT_WIDGET_HXX |
| 19 | #define PROMPT_WIDGET_HXX |
| 20 | |
| 21 | #include <cstdarg> |
| 22 | |
| 23 | class ScrollBarWidget; |
| 24 | class FilesystemNode; |
| 25 | |
| 26 | #include "Widget.hxx" |
| 27 | #include "Command.hxx" |
| 28 | #include "bspf.hxx" |
| 29 | |
| 30 | // TODO - remove this once we clean up the printf stuff |
| 31 | #if defined(BSPF_UNIX) || defined(BSPF_MACOS) |
| 32 | #define ATTRIBUTE_FMT_PRINTF __attribute__((__format__ (__printf__, 2, 0))) |
| 33 | #else |
| 34 | #define ATTRIBUTE_FMT_PRINTF |
| 35 | #endif |
| 36 | |
| 37 | class PromptWidget : public Widget, public CommandSender |
| 38 | { |
| 39 | public: |
| 40 | PromptWidget(GuiObject* boss, const GUI::Font& font, |
| 41 | int x, int y, int w, int h); |
| 42 | virtual ~PromptWidget() = default; |
| 43 | |
| 44 | public: |
| 45 | ATTRIBUTE_FMT_PRINTF int printf(const char* format, ...); |
| 46 | ATTRIBUTE_FMT_PRINTF int vprintf(const char* format, va_list argptr); |
| 47 | void print(const string& str); |
| 48 | void printPrompt(); |
| 49 | bool saveBuffer(const FilesystemNode& file); |
| 50 | |
| 51 | // Clear screen and erase all history |
| 52 | void clearScreen(); |
| 53 | |
| 54 | void addToHistory(const char *str); |
| 55 | |
| 56 | protected: |
| 57 | int& buffer(int idx) { return _buffer[idx % kBufferSize]; } |
| 58 | |
| 59 | void drawWidget(bool hilite) override; |
| 60 | void drawCaret(); |
| 61 | void putcharIntern(int c); |
| 62 | // void insertIntoPrompt(const char *str); |
| 63 | void updateScrollBuffer(); |
| 64 | void scrollToCurrent(); |
| 65 | |
| 66 | // Line editing |
| 67 | void specialKeys(StellaKey key); |
| 68 | void nextLine(); |
| 69 | void killChar(int direction); |
| 70 | void killLine(int direction); |
| 71 | void killLastWord(); |
| 72 | |
| 73 | // Clipboard |
| 74 | void textSelectAll(); |
| 75 | void textCut(); |
| 76 | void textCopy(); |
| 77 | void textPaste(); |
| 78 | |
| 79 | // History |
| 80 | void historyScroll(int direction); |
| 81 | |
| 82 | void handleMouseDown(int x, int y, MouseButton b, int clickCount) override; |
| 83 | void handleMouseWheel(int x, int y, int direction) override; |
| 84 | bool handleText(char text) override; |
| 85 | bool handleKeyDown(StellaKey key, StellaMod mod) override; |
| 86 | void handleCommand(CommandSender* sender, int cmd, int data, int id) override; |
| 87 | |
| 88 | // Account for the extra width of embedded scrollbar |
| 89 | int getWidth() const override; |
| 90 | |
| 91 | bool wantsFocus() const override { return true; } |
| 92 | void loadConfig() override; |
| 93 | |
| 94 | private: |
| 95 | // Get the longest prefix (initially 's') that is in every string in the list |
| 96 | string getCompletionPrefix(const StringList& completions); |
| 97 | |
| 98 | private: |
| 99 | enum { |
| 100 | kBufferSize = 32768, |
| 101 | kLineBufferSize = 256, |
| 102 | kHistorySize = 20 |
| 103 | }; |
| 104 | |
| 105 | int _buffer[kBufferSize]; |
| 106 | int _linesInBuffer; |
| 107 | |
| 108 | int _lineWidth; |
| 109 | int _linesPerPage; |
| 110 | |
| 111 | int _currentPos; |
| 112 | int _scrollLine; |
| 113 | int _firstLineInBuffer; |
| 114 | |
| 115 | int _promptStartPos; |
| 116 | int _promptEndPos; |
| 117 | |
| 118 | ScrollBarWidget* _scrollBar; |
| 119 | |
| 120 | char _history[kHistorySize][kLineBufferSize]; |
| 121 | int _historySize; |
| 122 | int _historyIndex; |
| 123 | int _historyLine; |
| 124 | |
| 125 | int _kConsoleCharWidth, _kConsoleCharHeight, _kConsoleLineHeight; |
| 126 | |
| 127 | bool _inverse; |
| 128 | bool _makeDirty; |
| 129 | bool _firstTime; |
| 130 | bool _exitedEarly; |
| 131 | |
| 132 | // int compareHistory(const char *histLine); |
| 133 | |
| 134 | private: |
| 135 | // Following constructors and assignment operators not supported |
| 136 | PromptWidget() = delete; |
| 137 | PromptWidget(const PromptWidget&) = delete; |
| 138 | PromptWidget(PromptWidget&&) = delete; |
| 139 | PromptWidget& operator=(const PromptWidget&) = delete; |
| 140 | PromptWidget& operator=(PromptWidget&&) = delete; |
| 141 | }; |
| 142 | |
| 143 | #endif |
| 144 | |