1 /* 2 * Copyright 2001-2013, Haiku, Inc. 3 * Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net> 4 * Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai. 5 * 6 * Distributed under the terms of the MIT license. 7 * Authors: 8 * Stefano Ceccherini, stefano.ceccherini@gmail.com 9 * Kian Duffy, myob@users.sourceforge.net 10 * Simon South, simon@simonsouth.net 11 * Ingo Weinhold, ingo_weinhold@gmx.de 12 * Siarzhuk Zharski, zharik@gmx.li 13 */ 14 #ifndef TERMVIEW_H 15 #define TERMVIEW_H 16 17 18 #include <Autolock.h> 19 #include <HashMap.h> 20 #include <InterfaceDefs.h> 21 #include <Messenger.h> 22 #include <ObjectList.h> 23 #include <String.h> 24 #include <View.h> 25 26 #include "TermPos.h" 27 #include "TermViewHighlight.h" 28 29 30 class ActiveProcessInfo; 31 class BClipboard; 32 class BMessageRunner; 33 class BScrollBar; 34 class BScrollView; 35 class BString; 36 class BStringView; 37 class BasicTerminalBuffer; 38 class DefaultCharClassifier; 39 class InlineInput; 40 class ResizeWindow; 41 class ShellInfo; 42 class ShellParameters; 43 class TermBuffer; 44 class TerminalBuffer; 45 class Shell; 46 47 48 class TermView : public BView, private TermViewHighlighter { 49 public: 50 class Listener; 51 52 typedef TermViewHighlighter Highlighter; 53 typedef TermViewHighlight Highlight; 54 55 public: 56 TermView(BRect frame, 57 const ShellParameters& shellParameters, 58 int32 historySize); 59 TermView(int rows, int columns, 60 const ShellParameters& shellParameters, 61 int32 historySize); 62 TermView(BMessage* archive); 63 ~TermView(); 64 65 static BArchivable* Instantiate(BMessage* data); 66 virtual status_t Archive(BMessage* data, bool deep = true) const; 67 68 virtual void GetPreferredSize(float* _width, float* _height); 69 70 bool IsShellBusy() const; 71 bool GetActiveProcessInfo( 72 ActiveProcessInfo& _info) const; 73 bool GetShellInfo(ShellInfo& _info) const; 74 75 const char* TerminalName() const; 76 77 inline TerminalBuffer* TextBuffer() const { return fTextBuffer; } 78 79 void GetTermFont(BFont* font) const; 80 void SetTermFont(const BFont* font); 81 82 void GetFontSize(float* width, float* height); 83 int Rows() const; 84 int Columns() const; 85 BRect SetTermSize(int rows, int columns, 86 bool notifyShell); 87 void SetTermSize(BRect rect, 88 bool notifyShell = false); 89 void GetTermSizeFromRect(const BRect &rect, 90 int *rows, int *columns); 91 92 void SetTextColor(rgb_color fore, rgb_color back); 93 void SetCursorColor(rgb_color fore, rgb_color back); 94 void SetSelectColor(rgb_color fore, rgb_color back); 95 void SetTermColor(uint index, rgb_color color, 96 bool dynamic = false); 97 status_t GetTermColor(uint index, rgb_color* color); 98 99 int Encoding() const; 100 void SetEncoding(int encoding); 101 102 void SetScrollBar(BScrollBar* scrollBar); 103 BScrollBar* ScrollBar() const { return fScrollBar; }; 104 105 void SetKeymap(const key_map* keymap, 106 const char* chars); 107 void SetUseOptionAsMetaKey(bool enable); 108 109 void SetMouseClipboard(BClipboard *); 110 111 void MakeDebugSnapshots(); 112 void StartStopDebugCapture(); 113 114 void SwitchCursorBlinking(bool blinkingOn); 115 116 // edit functions 117 void Copy(BClipboard* clipboard); 118 void Paste(BClipboard* clipboard); 119 void SelectAll(); 120 void Clear(); 121 122 // Other 123 void GetFrameSize(float* width, float* height); 124 bool Find(const BString& str, bool forwardSearch, 125 bool matchCase, bool matchWord); 126 void GetSelection(BString& string); 127 128 bool CheckShellGone() const; 129 130 void InitiateDrag(); 131 132 void DisableResizeView(int32 disableCount = 1); 133 134 void SetListener(Listener* listener) 135 { fListener = listener; } 136 137 protected: 138 virtual void AttachedToWindow(); 139 virtual void DetachedFromWindow(); 140 virtual void Draw(BRect updateRect); 141 virtual void WindowActivated(bool active); 142 virtual void MakeFocus(bool focusState = true); 143 virtual void KeyDown(const char* bytes, int32 numBytes); 144 145 virtual void MouseDown(BPoint where); 146 virtual void MouseMoved(BPoint where, uint32 transit, 147 const BMessage* message); 148 virtual void MouseUp(BPoint where); 149 150 virtual void FrameResized(float width, float height); 151 virtual void MessageReceived(BMessage* message); 152 153 virtual void ScrollTo(BPoint where); 154 virtual void TargetedByScrollView(BScrollView *scrollView); 155 156 virtual status_t GetSupportedSuites(BMessage* message); 157 virtual BHandler* ResolveSpecifier(BMessage* message, int32 index, 158 BMessage* specifier, int32 form, 159 const char* property); 160 161 private: 162 class TextBufferSyncLocker; 163 friend class TextBufferSyncLocker; 164 165 class State; 166 class StandardBaseState; 167 class DefaultState; 168 class SelectState; 169 class HyperLinkState; 170 class HyperLinkMenuState; 171 172 friend class State; 173 friend class StandardBaseState; 174 friend class DefaultState; 175 friend class SelectState; 176 friend class HyperLinkState; 177 friend class HyperLinkMenuState; 178 179 typedef BObjectList<Highlight> HighlightList; 180 181 private: 182 // TermViewHighlighter 183 virtual rgb_color ForegroundColor(); 184 virtual rgb_color BackgroundColor(); 185 186 private: 187 // point and text offset conversion 188 inline int32 _LineAt(float y); 189 inline float _LineOffset(int32 index); 190 TermPos _ConvertToTerminal(const BPoint& point); 191 inline BPoint _ConvertFromTerminal(const TermPos& pos); 192 193 inline void _InvalidateTextRect(int32 x1, int32 y1, 194 int32 x2, int32 y2); 195 196 status_t _InitObject( 197 const ShellParameters& shellParameters); 198 199 status_t _AttachShell(Shell* shell); 200 void _DetachShell(); 201 202 void _Activate(); 203 void _Deactivate(); 204 205 void _DrawLinePart(float x1, float y1, uint32 attr, 206 char* buffer, int32 width, 207 Highlight* highlight, bool cursor, 208 BView* inView); 209 void _DrawCursor(); 210 void _InvalidateTextRange(TermPos start, 211 TermPos end); 212 213 bool _IsCursorVisible() const; 214 void _BlinkCursor(); 215 void _ActivateCursor(bool invalidate); 216 217 void _DoPrint(BRect updateRect); 218 void _UpdateScrollBarRange(); 219 void _SecondaryMouseButtonDropped(BMessage* message); 220 void _DoSecondaryMouseDropAction(BMessage* message); 221 void _DoFileDrop(entry_ref &ref); 222 223 void _SynchronizeWithTextBuffer( 224 int32 visibleDirtyTop, 225 int32 visibleDirtyBottom); 226 void _VisibleTextBufferChanged(); 227 228 void _WritePTY(const char* text, int32 numBytes); 229 230 // selection 231 float _MouseDistanceSinceLastClick(BPoint where); 232 void _Select(TermPos start, TermPos end, 233 bool inclusive, bool setInitialSelection); 234 void _ExtendSelection(TermPos, bool inclusive, 235 bool useInitialSelection); 236 void _Deselect(); 237 bool _HasSelection() const; 238 void _SelectWord(BPoint where, bool extend, 239 bool useInitialSelection); 240 void _SelectLine(BPoint where, bool extend, 241 bool useInitialSelection); 242 243 void _AddHighlight(Highlight* highlight); 244 void _RemoveHighlight(Highlight* highlight); 245 bool _ClearHighlight(Highlight* highlight); 246 247 Highlight* _CheckHighlightRegion(const TermPos& pos) const; 248 Highlight* _CheckHighlightRegion(int32 row, 249 int32 firstColumn, int32& lastColumn) const; 250 251 void _UpdateSIGWINCH(); 252 253 void _ScrollTo(float y, bool scrollGfx); 254 void _ScrollToRange(TermPos start, TermPos end); 255 256 void _SendMouseEvent(int32 button, int32 mode, 257 int32 x, int32 y, bool motion); 258 259 void _DrawInlineMethodString(); 260 void _HandleInputMethodChanged(BMessage* message); 261 void _HandleInputMethodLocationRequest(); 262 void _CancelInputMethod(); 263 264 void _UpdateModifiers(); 265 266 void _NextState(State* state); 267 268 private: 269 Listener* fListener; 270 Shell* fShell; 271 272 BMessageRunner* fWinchRunner; 273 BMessageRunner* fCursorBlinkRunner; 274 BMessageRunner* fAutoScrollRunner; 275 BMessageRunner* fResizeRunner; 276 BStringView* fResizeView; 277 DefaultCharClassifier* fCharClassifier; 278 279 // Font and Width 280 BFont fHalfFont; 281 BFont fBoldFont; 282 float fFontWidth; 283 int fFontHeight; 284 int fFontAscent; 285 struct escapement_delta fEscapement; 286 bool fEmulateBold; 287 bool fAllowBold; 288 289 // frame resized flag. 290 bool fFrameResized; 291 int32 fResizeViewDisableCount; 292 293 // Cursor Blinking, draw flag. 294 bigtime_t fLastActivityTime; 295 int32 fCursorState; 296 int fCursorStyle; 297 bool fCursorBlinking; 298 bool fCursorHidden; 299 300 // Cursor position. 301 TermPos fCursor; 302 303 // Terminal rows and columns. 304 int fColumns; 305 int fRows; 306 307 int fEncoding; 308 bool fActive; 309 310 // Object pointer. 311 TerminalBuffer* fTextBuffer; 312 BasicTerminalBuffer* fVisibleTextBuffer; 313 bool fVisibleTextBufferChanged; 314 BScrollBar* fScrollBar; 315 InlineInput* fInline; 316 317 // Color and Attribute. 318 rgb_color fTextForeColor; 319 rgb_color fTextBackColor; 320 rgb_color fCursorForeColor; 321 rgb_color fCursorBackColor; 322 rgb_color fSelectForeColor; 323 rgb_color fSelectBackColor; 324 325 // Scroll Region 326 float fScrollOffset; 327 int32 fScrBufSize; 328 // TODO: That's the history capacity -- only needed 329 // until the text buffer is created. 330 float fAutoScrollSpeed; 331 332 // redraw management 333 bigtime_t fLastSyncTime; 334 int32 fScrolledSinceLastSync; 335 BMessageRunner* fSyncRunner; 336 bool fConsiderClockedSync; 337 338 // selection 339 Highlight fSelection; 340 TermPos fInitialSelectionStart; 341 TermPos fInitialSelectionEnd; 342 BPoint fLastClickPoint; 343 344 HighlightList fHighlights; 345 346 // keyboard 347 const key_map* fKeymap; 348 const char* fKeymapChars; 349 HashMap<HashKey32<int32>, const int32(*)[128]> 350 fKeymapTableForModifiers; 351 bool fUseOptionAsMetaKey; 352 bool fInterpretMetaKey; 353 bool fMetaKeySendsEscape; 354 355 // mouse 356 int32 fMouseButtons; 357 int32 fModifiers; 358 TermPos fPrevPos; 359 bool fReportX10MouseEvent; 360 bool fReportNormalMouseEvent; 361 bool fReportButtonMouseEvent; 362 bool fReportAnyMouseEvent; 363 bool fEnableExtendedMouseCoordinates; 364 BClipboard* fMouseClipboard; 365 366 // states 367 DefaultState* fDefaultState; 368 SelectState* fSelectState; 369 HyperLinkState* fHyperLinkState; 370 HyperLinkMenuState* fHyperLinkMenuState; 371 State* fActiveState; 372 }; 373 374 375 class TermView::Listener { 376 public: 377 virtual ~Listener(); 378 379 // all hooks called in the window thread 380 virtual void NotifyTermViewQuit(TermView* view, 381 int32 reason); 382 virtual void SetTermViewTitle(TermView* view, 383 const char* title); 384 virtual void PreviousTermView(TermView* view); 385 virtual void NextTermView(TermView* view); 386 }; 387 388 389 #endif // TERMVIEW_H 390