textview.monkey2 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. Namespace mojox
  2. Alias TextHighlighter:Int( text:String,colors:Byte[],sol:Int,eol:Int,state:Int )
  3. #rem monkeydoc The TextDocument class.
  4. #end
  5. Class TextDocument
  6. #rem monkeydoc Invoked after text has changed.
  7. #end
  8. Field TextChanged:Void()
  9. #rem monkeydoc Invoked after lines have been modified.
  10. #end
  11. Field LinesModified:Void( first:Int,removed:Int,inserted:Int )
  12. #rem monkeydoc Creates a new text document.
  13. #end
  14. Method New()
  15. _lines.Push( New Line )
  16. End
  17. #rem monkeydoc Document text.
  18. #end
  19. Property Text:String()
  20. Return _text
  21. Setter( text:String )
  22. text=text.Replace( "~r~n","~n" )
  23. text=text.Replace( "~r","~n" )
  24. ReplaceText( 0,_text.Length,text )
  25. End
  26. #rem monkeydoc Length of doucment text.
  27. #end
  28. Property TextLength:Int()
  29. Return _text.Length
  30. End
  31. #rem monkeydoc Number of lines in document.
  32. #end
  33. Property NumLines:Int()
  34. Return _lines.Length
  35. End
  36. #rem monkeydoc @hidden
  37. #end
  38. Property Colors:Byte[]()
  39. Return _colors.Data
  40. End
  41. #rem monkeydoc @hidden
  42. #end
  43. Property TextHighlighter:TextHighlighter()
  44. Return _highlighter
  45. Setter( textHighlighter:TextHighlighter )
  46. _highlighter=textHighlighter
  47. End
  48. #rem monkeydoc @hidden
  49. #end
  50. Method LineState:Int( line:Int )
  51. If line>=0 And line<_lines.Length Return _lines[line].state
  52. Return -1
  53. End
  54. #rem monkeydoc Gets the index of the first character on a line.
  55. #end
  56. Method StartOfLine:Int( line:Int )
  57. If line<=0 Return 0
  58. If line<_lines.Length Return _lines[line-1].eol+1
  59. Return _text.Length
  60. End
  61. #rem monkeydoc Gets the index of the last character on a line.
  62. #end
  63. Method EndOfLine:Int( line:Int )
  64. If line<0 Return 0
  65. If line<_lines.Length Return _lines[line].eol
  66. Return _text.Length
  67. End
  68. #rem monkeydoc Finds the line containing a character.
  69. #end
  70. Method FindLine:Int( index:Int )
  71. If index<=0 Return 0
  72. If index>=_text.Length Return _lines.Length-1
  73. Local min:=0,max:=_lines.Length-1
  74. Repeat
  75. Local line:=(min+max)/2
  76. If index>_lines[line].eol
  77. min=line+1
  78. Else If max-min<2
  79. Return min
  80. Else
  81. max=line
  82. Endif
  83. Forever
  84. Return 0
  85. End
  86. #rem monkeydoc Gets line text.
  87. #end
  88. Method GetLine:String( line:Int )
  89. Return _text.Slice( StartOfLine( line ),EndOfLine( line ) )
  90. End
  91. #rem monkeydoc Appends text to the end of the document.
  92. #end
  93. Method AppendText( text:String )
  94. ReplaceText( _text.Length,_text.Length,text )
  95. End
  96. #rem monkeydoc Replaces text in the document.
  97. #end
  98. Method ReplaceText( anchor:Int,cursor:Int,text:String )
  99. Local min:=Min( anchor,cursor )
  100. Local max:=Max( anchor,cursor )
  101. Local eols1:=0,eols2:=0
  102. For Local i:=min Until max
  103. If _text[i]=10 eols1+=1
  104. Next
  105. For Local i:=0 Until text.Length
  106. If text[i]=10 eols2+=1
  107. Next
  108. Local dlines:=eols2-eols1
  109. Local dchars:=text.Length-(max-min)
  110. Local line0:=FindLine( min )
  111. Local line:=line0
  112. Local eol:=StartOfLine( line )-1
  113. ' Print "eols1="+eols1+", eols2="+eols2+", dlines="+dlines+", dchars="+dchars+" text="+text.Length
  114. 'Move data!
  115. '
  116. Local oldlen:=_text.Length
  117. _text=_text.Slice( 0,min )+text+_text.Slice( max )
  118. _colors.Resize( _text.Length )
  119. Local p:=_colors.Data.Data
  120. libc.memmove( p + min + text.Length, p + max , oldlen-max )
  121. libc.memset( p + min , 0 , text.Length )
  122. 'Update lines
  123. '
  124. If dlines>=0
  125. _lines.Resize( _lines.Length+dlines )
  126. Local i:=_lines.Length
  127. While i>line+eols2+1
  128. i-=1
  129. _lines.Data[i].eol=_lines[i-dlines].eol+dchars
  130. _lines.Data[i].state=_lines[i-dlines].state
  131. Wend
  132. Endif
  133. For Local i:=0 Until eols2+1
  134. eol=_text.Find( "~n",eol+1 )
  135. If eol=-1 eol=_text.Length
  136. _lines.Data[line+i].eol=eol
  137. _lines.Data[line+i].state=-1
  138. Next
  139. If dlines<0
  140. Local i:=line+eols2+1
  141. While i<_lines.Length+dlines
  142. _lines.Data[i].eol=_lines[i-dlines].eol+dchars
  143. _lines.Data[i].state=_lines[i-dlines].state
  144. i+=1
  145. Wend
  146. _lines.Resize( _lines.Length+dlines )
  147. Endif
  148. If _highlighter<>Null
  149. 'update highlighting
  150. '
  151. Local state:=-1
  152. If line state=_lines[line-1].state
  153. For Local i:=0 Until eols2+1
  154. state=_highlighter( _text,_colors.Data,StartOfLine( line ),EndOfLine( line ),state )
  155. _lines.Data[line].state=state
  156. line+=1
  157. Next
  158. While line<_lines.Length 'And state<>_lines[line].state
  159. state=_highlighter( _text,_colors.Data,StartOfLine( line ),EndOfLine( line ),state )
  160. _lines.Data[line].state=state
  161. line+=1
  162. End
  163. Endif
  164. LinesModified( line0,eols1,eols2 )
  165. TextChanged()
  166. End
  167. Private
  168. Struct Line
  169. Field eol:Int
  170. Field state:Int
  171. End
  172. Field _text:String
  173. Field _lines:=New Stack<Line>
  174. Field _colors:=New Stack<Byte>
  175. Field _highlighter:TextHighlighter
  176. End
  177. #rem monkeydoc Cursor type for textviews.
  178. Supported cursor types are:
  179. | CursorType | Description
  180. |:--------------|:-----------
  181. | Block | Block cursor
  182. | Line | Single pixel vertical line
  183. | IBeam | IBeam cursor
  184. #end
  185. Enum CursorType
  186. Block
  187. Line
  188. IBeam
  189. End
  190. #rem monkeydoc The TextView class.
  191. #end
  192. Class TextView Extends ScrollableView
  193. #rem monkeydoc Invoked when cursor moves.
  194. #end
  195. Field CursorMoved:Void()
  196. #rem monkeydoc Creates a new text view.
  197. #end
  198. Method New()
  199. Style=GetStyle( "TextView" )
  200. ContentView.Style=GetStyle( "TextViewContent" )
  201. _lines.Push( New Line )
  202. UpdateColors()
  203. Document=New TextDocument
  204. End
  205. Method New( text:String )
  206. Self.New()
  207. Document.Text=text
  208. End
  209. Method New( doc:TextDocument )
  210. Self.New()
  211. Document=doc
  212. End
  213. #rem monkeydoc Text document.
  214. #end
  215. Property Document:TextDocument()
  216. Return _doc
  217. Setter( doc:TextDocument )
  218. If _doc _doc.LinesModified-=LinesModified
  219. _doc=doc
  220. _doc.LinesModified+=LinesModified
  221. _cursor=Clamp( _cursor,0,_doc.TextLength )
  222. _anchor=_cursor
  223. UpdateCursor()
  224. End
  225. Public
  226. #rem monkeydoc Text colors.
  227. #end
  228. Property TextColors:Color[]()
  229. Return _textColors
  230. Setter( textColors:Color[] )
  231. _textColors=textColors
  232. End
  233. #rem monkeydoc Cursor color.
  234. #end
  235. Property CursorColor:Color()
  236. Return _cursorColor
  237. Setter( cursorColor:Color )
  238. _cursorColor=cursorColor
  239. End
  240. #rem monkeydoc Selection color.
  241. #end
  242. Property SelectionColor:Color()
  243. Return _selColor
  244. Setter( selectionColor:Color )
  245. _selColor=selectionColor
  246. End
  247. #rem monkeydoc Cursor type.
  248. #end
  249. Property CursorType:CursorType()
  250. Return _cursorType
  251. Setter( type:CursorType )
  252. _cursorType=type
  253. End
  254. #rem monkeydoc @deprecated Use [[CursorType]].
  255. #end
  256. Property BlockCursor:Bool()
  257. Return _cursorType=CursorType.Block
  258. Setter( block:Bool )
  259. _cursorType=block ? CursorType.Block Else CursorType.Line
  260. End
  261. #rem monkeydoc Cursor blink rate.
  262. Set to 0 for non-blinking cursor.
  263. #end
  264. Property CursorBlinkRate:Float()
  265. Return _blinkRate
  266. Setter( blinkRate:Float )
  267. _blinkRate=blinkRate
  268. RequestRender()
  269. End
  270. #rem monkeydoc Text.
  271. #end
  272. Property Text:String()
  273. Return _doc.Text
  274. Setter( text:String )
  275. _doc.Text=text
  276. End
  277. #rem monkeydoc Read only flag.
  278. #end
  279. Property ReadOnly:Bool()
  280. Return _readOnly
  281. Setter( readOnly:Bool )
  282. _readOnly=readOnly
  283. End
  284. #rem monkeydoc Tabstop.
  285. #end
  286. Property TabStop:Int()
  287. Return _tabStop
  288. Setter( tabStop:Int )
  289. _tabStop=tabStop
  290. InvalidateStyle()
  291. End
  292. #rem monkeydoc WordWrap flag.
  293. #end
  294. Property WordWrap:Bool()
  295. Return _wordWrap
  296. Setter( wordWrap:Bool )
  297. _wordWrap=wordWrap
  298. InvalidateStyle()
  299. End
  300. #rem monkeydoc Cursor character index.
  301. #end
  302. Property Cursor:Int()
  303. Return _cursor
  304. End
  305. #rem monkeydoc Anchor character index.
  306. #end
  307. Property Anchor:Int()
  308. Return _anchor
  309. End
  310. #rem monkeydoc Line the cursor is on.
  311. #end
  312. Property CursorLine:Int()
  313. Return _doc.FindLine( _cursor )
  314. End
  315. #rem monkeydoc Cursor rect.
  316. #end
  317. Property CursorRect:Recti()
  318. Return _cursorRect
  319. End
  320. #rem monkeydoc Approximate character width.
  321. #end
  322. Property CharWidth:Int()
  323. Return _charw
  324. End
  325. #rem monkeydoc Approximate character height.
  326. #end
  327. Property CharHeight:Int()
  328. Return _charh
  329. End
  330. #rem monkeydoc Approximate line height.
  331. Deprecated! Use [[LineRect]] instead to properly deal with word wrap.
  332. #end
  333. Property LineHeight:Int()
  334. Return _charh
  335. End
  336. #rem monkeydoc True if undo available.
  337. #end
  338. Property CanUndo:Bool()
  339. Return Not _readOnly And Not _undos.Empty
  340. End
  341. #rem monkeydoc True if redo available.
  342. #end
  343. Property CanRedo:Bool()
  344. Return Not _readOnly And Not _redos.Empty
  345. End
  346. #rem monkeydoc True if cut available.
  347. #end
  348. Property CanCut:Bool()
  349. Return Not _readOnly And _anchor<>_cursor
  350. End
  351. #rem monkeydoc True if copy available.
  352. #end
  353. Property CanCopy:Bool()
  354. Return _anchor<>_cursor
  355. End
  356. #rem monkeydoc True if paste available.
  357. #end
  358. Property CanPaste:Bool()
  359. Return Not _readOnly And Not App.ClipboardTextEmpty
  360. End
  361. #rem monkeydoc Returns the rect containing a character at a given index.
  362. #end
  363. Method CharRect:Recti( index:Int )
  364. Local line:=_doc.FindLine( index )
  365. Local text:=_doc.Text
  366. Local i0:=_doc.StartOfLine( line )
  367. Local eol:=_doc.EndOfLine( line )
  368. Local x0:=0,y0:=_lines[line].rect.Top
  369. While i0<eol
  370. Local w:=WordWidth( text,i0,eol,x0 )
  371. If x0+w>_wrapw
  372. y0+=_charh
  373. x0=0
  374. Endif
  375. Local l:=WordLength( text,i0,eol )
  376. If index<i0+l
  377. x0+=WordWidth( text,i0,index,x0 )
  378. i0=index
  379. Exit
  380. Endif
  381. x0+=w
  382. i0+=l
  383. Wend
  384. Local w:=_charw
  385. If i0<eol And text[i0]>32 w=_font.TextWidth( text.Slice( i0,i0+1 ) )
  386. Return New Recti( x0,y0,x0+w,y0+_charh )
  387. End
  388. #rem monkeydoc Returns the index of the character nearest to a given point.
  389. #end
  390. Method CharAtPoint:Int( p:Vec2i )
  391. Local line:=LineAtPoint( p )
  392. Local text:=_doc.Text
  393. Local i0:=_doc.StartOfLine( line )
  394. Local eol:=_doc.EndOfLine( line )
  395. Local x0:=0,y0:=_lines[line].rect.Top+_charh
  396. While i0<eol
  397. Local w:=WordWidth( text,i0,eol,x0 )
  398. If x0+w>_wrapw
  399. If p.y<y0 Exit
  400. y0+=_charh
  401. x0=0
  402. Endif
  403. Local l:=WordLength( text,i0,eol )
  404. If p.x<x0+w And p.y<y0
  405. For Local i:=0 Until l
  406. x0+=WordWidth( text,i0,i0+1,x0 )
  407. If p.x<x0 Exit
  408. i0+=1
  409. Next
  410. Exit
  411. Endif
  412. x0+=w
  413. i0+=l
  414. Wend
  415. Return i0
  416. End
  417. #rem monkedoc Returns the index of the line nearest to a given point.
  418. #end
  419. Method LineAtPoint:Int( p:Vec2i )
  420. If p.y<=0 Return 0
  421. If p.y>=_lines.Top.rect.Top Return _lines.Length-1
  422. Local min:=0,max:=_lines.Length-1
  423. Repeat
  424. Local line:=(min+max)/2
  425. If p.y>=_lines[line].rect.Bottom
  426. min=line+1
  427. Else If max-min>1
  428. max=line
  429. Else
  430. Return min
  431. Endif
  432. Forever
  433. Return 0
  434. End
  435. #rem monkeydoc Gets the bounding rect for a line.
  436. #end
  437. Method LineRect:Recti( line:Int )
  438. If line>=0 And line<_lines.Length Return _lines[line].rect
  439. Return New Recti
  440. End
  441. #rem monkeydoc Clears all text.
  442. #end
  443. Method Clear()
  444. SelectAll()
  445. ReplaceText( "" )
  446. End
  447. #rem monkeydoc Move cursor to line.
  448. #end
  449. Method GotoLine( line:Int )
  450. _anchor=_doc.StartOfLine( line )
  451. _cursor=_anchor
  452. UpdateCursor()
  453. End
  454. #rem monkeydoc Selects a line.
  455. #end
  456. Method SelectLine( line:Int )
  457. SelectText( _doc.StartOfLine( line ),_doc.EndOfLine( line ) )
  458. End
  459. #rem monkeydoc Selects text in a range.
  460. #end
  461. Method SelectText( anchor:Int,cursor:Int )
  462. _anchor=Clamp( anchor,0,_doc.TextLength )
  463. _cursor=Clamp( cursor,0,_doc.TextLength )
  464. UpdateCursor()
  465. End
  466. #rem monkeydoc Appends text.
  467. #end
  468. Method AppendText( text:String )
  469. SelectText( _doc.TextLength,_doc.TextLength )
  470. ReplaceText( text )
  471. End
  472. #rem monkeydoc Replaces current selection.
  473. #end
  474. Method ReplaceText( text:String )
  475. Local undo:=New UndoOp
  476. undo.text=_doc.Text.Slice( Min( _anchor,_cursor ),Max( _anchor,_cursor ) )
  477. undo.anchor=Min( _anchor,_cursor )
  478. undo.cursor=undo.anchor+text.Length
  479. _undos.Push( undo )
  480. ReplaceText( _anchor,_cursor,text )
  481. End
  482. 'non-undoable
  483. #rem monkeydoc @hidden
  484. #end
  485. Method ReplaceText( anchor:Int,cursor:Int,text:String )
  486. _redos.Clear()
  487. _doc.ReplaceText( anchor,cursor,text )
  488. _cursor=Min( anchor,cursor )+text.Length
  489. _anchor=_cursor
  490. UpdateCursor()
  491. End
  492. #rem monkeydoc Performs an undo.
  493. #end
  494. Method Undo()
  495. If _readOnly Return
  496. If _undos.Empty Return
  497. Local undo:=_undos.Pop()
  498. Local text:=undo.text
  499. Local anchor:=undo.anchor
  500. Local cursor:=undo.cursor
  501. undo.text=_doc.Text.Slice( anchor,cursor )
  502. undo.cursor=anchor+text.Length
  503. _redos.Push( undo )
  504. _doc.ReplaceText( anchor,cursor,text )
  505. _cursor=anchor+text.Length
  506. _anchor=_cursor
  507. UpdateCursor()
  508. End
  509. #rem monkeydoc Performs a redo.
  510. #end
  511. Method Redo()
  512. If _readOnly Return
  513. If _redos.Empty Return
  514. Local undo:=_redos.Pop()
  515. Local text:=undo.text
  516. Local anchor:=undo.anchor
  517. Local cursor:=undo.cursor
  518. undo.text=_doc.Text.Slice( anchor,cursor )
  519. undo.cursor=anchor+text.Length
  520. _undos.Push( undo )
  521. _doc.ReplaceText( anchor,cursor,text )
  522. _cursor=anchor+text.Length
  523. _anchor=_cursor
  524. UpdateCursor()
  525. End
  526. #rem monkeydoc Selects all text.
  527. #end
  528. Method SelectAll()
  529. SelectText( 0,_doc.TextLength )
  530. End
  531. #rem monkeydoc Performs a cut.
  532. #end
  533. Method Cut()
  534. If _readOnly Return
  535. Copy()
  536. ReplaceText( "" )
  537. End
  538. #rem monkeydoc Performs a copy.
  539. #end
  540. Method Copy()
  541. Local min:=Min( _anchor,_cursor )
  542. Local max:=Max( _anchor,_cursor )
  543. Local text:=_doc.Text.Slice( min,max )
  544. App.ClipboardText=text
  545. End
  546. #rem monkeydoc Performs a paste.
  547. #end
  548. Method Paste()
  549. If _readOnly Return
  550. If App.ClipboardTextEmpty Return
  551. Local text:String=App.ClipboardText
  552. text=text.Replace( "~r~n","~n" )
  553. text=text.Replace( "~r","~n" )
  554. If text ReplaceText( text )
  555. End
  556. Protected
  557. Method OnThemeChanged() Override
  558. UpdateColors()
  559. End
  560. Method OnValidateStyle() Override
  561. Local style:=RenderStyle
  562. _font=style.Font
  563. _charw=_font.TextWidth( "X" )
  564. _charh=_font.Height
  565. _tabw=_charw*_tabStop
  566. UpdateLines()
  567. End
  568. Method OnMeasureContent:Vec2i() Override
  569. If _wordWrap Return New Vec2i( 0,0 )
  570. If _wrapw<>$7fffffff
  571. _wrapw=$7fffffff
  572. UpdateLines()
  573. Endif
  574. Return New Vec2i( _lines.Top.maxWidth+_charw,_lines.Top.rect.Bottom )
  575. End
  576. Method OnMeasureContent2:Vec2i( size:Vec2i ) Override
  577. If Not _wordWrap Return New Vec2i( 0,0 )
  578. If _wrapw<>size.x
  579. _wrapw=size.x
  580. UpdateLines()
  581. Endif
  582. Return New Vec2i( _lines.Top.maxWidth,_lines.Top.rect.Bottom )
  583. End
  584. Method OnRenderContent( canvas:Canvas ) Override
  585. If App.KeyView=Self And Not _blinkTimer RestartBlinkTimer()
  586. Local clip:=VisibleRect
  587. Local firstLine:=LineAtPoint( New Vec2i( 0,clip.Top ) )
  588. Local lastLine:=LineAtPoint( New Vec2i( 0,clip.Bottom-1 ) )+1
  589. If _cursor<>_anchor
  590. Local min:=CharRect( Min( _anchor,_cursor ) )
  591. Local max:=CharRect( Max( _anchor,_cursor ) )
  592. canvas.Color=_selColor
  593. If min.Y=max.Y
  594. canvas.DrawRect( min.Left,min.Top,max.Left-min.Left,min.Height )
  595. Else
  596. canvas.DrawRect( min.Left,min.Top,(clip.Right-min.Left),min.Height )
  597. canvas.DrawRect( 0,min.Bottom,clip.Right,max.Top-min.Bottom )
  598. canvas.DrawRect( 0,max.Top,max.Left,max.Height )
  599. Endif
  600. Endif
  601. If Not _readOnly And App.KeyView=Self And _blinkOn
  602. canvas.Color=_cursorColor
  603. Select _cursorType
  604. Case CursorType.Block
  605. canvas.DrawRect( _cursorRect )
  606. Case CursorType.IBeam
  607. canvas.DrawRect( _cursorRect.X,_cursorRect.Y,1,_cursorRect.Height )
  608. canvas.DrawRect( _cursorRect.X-2,_cursorRect.Y,5,1 )
  609. canvas.DrawRect( _cursorRect.X-2,_cursorRect.Y+_cursorRect.Height-1,5,1 )
  610. Default
  611. canvas.DrawRect( Max( _cursorRect.X-1,0 ),_cursorRect.Y,2,_cursorRect.Height )
  612. End
  613. Endif
  614. _textColors[0]=RenderStyle.TextColor
  615. For Local line:=firstLine Until lastLine
  616. RenderLine( canvas,line )
  617. Next
  618. End
  619. Method OnKeyDown:Bool( key:Key,modifiers:Modifier=Null )
  620. Select key
  621. Case Key.Backspace
  622. If _anchor=_cursor And _cursor>0 SelectText( _cursor-1,_cursor )
  623. ReplaceText( "" )
  624. Case Key.KeyDelete
  625. If _anchor=_cursor And _cursor<_doc.Text.Length SelectText( _cursor,_cursor+1 )
  626. ReplaceText( "" )
  627. Case Key.Tab
  628. Local cmin:=Min( _cursor,_anchor )
  629. Local cmax:=Max( _cursor,_anchor )
  630. Local min:=_doc.FindLine( cmin )
  631. Local max:=_doc.FindLine( cmax )
  632. If min=max
  633. ReplaceText( "~t" )
  634. Else
  635. 'select all lines...
  636. cmin=_doc.StartOfLine( min )
  637. If _doc.StartOfLine( max )<>cmax
  638. max+=1
  639. cmax=_doc.StartOfLine( max )
  640. Endif
  641. SelectText( cmin,cmax )
  642. Local lines:=New StringStack
  643. For Local i:=min Until max
  644. lines.Push( _doc.GetLine( i ) )
  645. Next
  646. Local go:=True
  647. If modifiers & Modifier.Shift
  648. For Local i:=0 Until lines.Length
  649. If Not lines[i].Trim()
  650. lines[i]+="~n"
  651. Continue
  652. Endif
  653. If lines[i][0]=9
  654. lines[i]=lines[i].Slice( 1 )+"~n"
  655. Continue
  656. Endif
  657. go=False
  658. Exit
  659. Next
  660. Else
  661. For Local i:=0 Until lines.Length
  662. lines[i]="~t"+lines[i]+"~n"
  663. Next
  664. Endif
  665. If go
  666. ReplaceText( lines.Join( "" ) )
  667. SelectText( _doc.StartOfLine( min ),_doc.StartOfLine( max ) )
  668. Return False
  669. Endif
  670. Endif
  671. Case Key.Enter,Key.KeypadEnter
  672. ReplaceText( "~n" )
  673. 'auto indent!
  674. Local line:=CursorLine
  675. If line>0
  676. Local ptext:=_doc.GetLine( line-1 )
  677. Local indent:=ptext
  678. For Local i:=0 Until ptext.Length
  679. If ptext[i]<=32 Continue
  680. indent=ptext.Slice( 0,i )
  681. Exit
  682. Next
  683. If indent ReplaceText( indent )
  684. Endif
  685. Case Key.Left
  686. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  687. _cursor=Min( _anchor,_cursor )
  688. Else If _cursor
  689. _cursor-=1
  690. Endif
  691. UpdateCursor()
  692. Case Key.Right
  693. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  694. _cursor=Max( _anchor,_cursor )
  695. Else If _cursor<_doc.Text.Length
  696. _cursor+=1
  697. Endif
  698. UpdateCursor()
  699. Case Key.Home
  700. _cursor=_doc.StartOfLine( CursorLine )
  701. UpdateCursor()
  702. Case Key.KeyEnd
  703. _cursor=_doc.EndOfLine( CursorLine )
  704. UpdateCursor()
  705. Case Key.Up
  706. MoveLine( -1 )
  707. Case Key.Down
  708. MoveLine( 1 )
  709. Case Key.PageUp
  710. Local n:=VisibleRect.Height/_charh-1 'shouldn't really use cliprect here...
  711. MoveLine( -n )
  712. Case Key.PageDown
  713. Local n:=VisibleRect.Height/_charh-1
  714. MoveLine( n )
  715. Default
  716. Return False
  717. End
  718. Return True
  719. End
  720. Method OnControlKeyDown:bool( key:Key,modifiers:Modifier=Null ) Virtual
  721. Select key
  722. Case Key.A
  723. SelectAll()
  724. Case Key.X
  725. Cut()
  726. Case Key.C
  727. Copy()
  728. Case Key.V
  729. Paste()
  730. Case Key.Z
  731. Undo()
  732. Case Key.Y
  733. Redo()
  734. Case Key.Home
  735. _cursor=0
  736. UpdateCursor()
  737. Return True
  738. Case Key.KeyEnd
  739. _cursor=_doc.TextLength
  740. UpdateCursor()
  741. Return True
  742. Case Key.Left
  743. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  744. _cursor=Min( _anchor,_cursor )
  745. Endif
  746. Local text:=Text
  747. Local term:=New Int[1]
  748. _cursor=FindWord( _cursor,term )
  749. While _cursor And text[_cursor-1]<=32 And text[_cursor-1]<>10
  750. _cursor-=1
  751. Wend
  752. _cursor=FindWord( Max( _cursor-1,0 ),term )
  753. UpdateCursor()
  754. Return True
  755. Case Key.Right
  756. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  757. _cursor=Max( _anchor,_cursor )
  758. Endif
  759. 'next word...
  760. Local text:=Text
  761. Local term:=New Int[1]
  762. FindWord( _cursor,term )
  763. _cursor=term[0]
  764. While _cursor<text.Length And text[_cursor]<=32 And text[_cursor]<>10
  765. _cursor+=1
  766. Wend
  767. UpdateCursor()
  768. Return True
  769. End
  770. Return False
  771. End
  772. Method OnKeyEvent( event:KeyEvent ) Override
  773. If _readOnly
  774. If _macosMode
  775. If (event.Modifiers & Modifier.Gui) And (event.Key=Key.C Or event.Key=Key.A)
  776. 'Copy, Select All
  777. Else
  778. Return
  779. Endif
  780. Else
  781. If (event.Modifiers & Modifier.Control) And (event.Key=Key.C Or event.Key=Key.A)
  782. 'Copy, Select All
  783. Else
  784. Return
  785. Endif
  786. Endif
  787. Endif
  788. Select event.Type
  789. Case EventType.KeyDown,EventType.KeyRepeat
  790. If _macosMode
  791. If event.Modifiers & Modifier.Gui
  792. Select event.Key
  793. Case Key.Home,Key.KeyEnd
  794. Default
  795. If Not OnControlKeyDown( event.Key,event.Modifiers ) Return
  796. End
  797. Else If event.Modifiers & Modifier.Control
  798. Select event.Key
  799. Case Key.A
  800. OnKeyDown( Key.Home )
  801. Case Key.E
  802. OnKeyDown( Key.KeyEnd )
  803. End
  804. Else
  805. Select event.Key
  806. Case Key.Home
  807. OnControlKeyDown( Key.Home )
  808. Case Key.KeyEnd
  809. OnControlKeyDown( Key.KeyEnd )
  810. Default
  811. If Not OnKeyDown( event.Key,event.Modifiers ) Return
  812. End
  813. Endif
  814. Else
  815. If event.Modifiers & Modifier.Control
  816. If Not OnControlKeyDown( event.Key,event.Modifiers ) Return
  817. Else
  818. If Not OnKeyDown( event.Key,event.Modifiers ) Return
  819. Endif
  820. Endif
  821. If Not (event.Modifiers & Modifier.Shift) _anchor=_cursor
  822. Case EventType.KeyChar
  823. If _undos.Length
  824. Local undo:=_undos.Top
  825. If Not undo.text And _cursor=undo.cursor
  826. ReplaceText( _anchor,_cursor,event.Text )
  827. undo.cursor=_cursor
  828. Return
  829. Endif
  830. Endif
  831. ReplaceText( event.Text )
  832. End
  833. End
  834. Method OnContentMouseEvent( event:MouseEvent ) Override
  835. Select event.Type
  836. Case EventType.MouseDown
  837. Select event.Clicks
  838. Case 1
  839. _cursor=CharAtPoint( event.Location )
  840. If Not (event.Modifiers & Modifier.Shift) _anchor=_cursor
  841. _dragging=True
  842. MakeKeyView()
  843. UpdateCursor()
  844. Case 2
  845. Local term:=New Int[1]
  846. Local start:=FindWord( CharAtPoint( event.Location ),term )
  847. SelectText( start,term[0] )
  848. Case 3
  849. SelectLine( LineAtPoint( event.Location ) )
  850. End
  851. Return
  852. #rem
  853. Case EventType.MouseClick
  854. _cursor=CharAtPoint( event.Location )
  855. If Not (event.Modifiers & Modifier.Shift) _anchor=_cursor
  856. _dragging=True
  857. MakeKeyView()
  858. UpdateCursor()
  859. Case EventType.MouseDoubleClick
  860. Local term:=New Int[1]
  861. Local start:=FindWord( CharAtPoint( event.Location ),term )
  862. SelectText( start,term[0] )
  863. #end
  864. Case EventType.MouseUp
  865. _dragging=False
  866. Case EventType.MouseMove
  867. If _dragging
  868. _cursor=CharAtPoint( event.Location )
  869. UpdateCursor()
  870. Endif
  871. Case EventType.MouseWheel
  872. Return
  873. End
  874. event.Eat()
  875. End
  876. Private
  877. Struct Line
  878. Field rect:Recti
  879. Field maxWidth:Int
  880. End
  881. Class UndoOp
  882. Field text:String
  883. Field anchor:Int
  884. Field cursor:Int
  885. End
  886. Field _doc:TextDocument
  887. Field _lines:=New Stack<Line>
  888. Field _tabStop:Int=4
  889. Field _tabSpaces:String=" "
  890. Field _cursorColor:Color=New Color( 0,.5,1,1 )
  891. Field _selColor:Color=New Color( 1,1,1,.25 )
  892. Field _cursorType:CursorType
  893. Field _blinkRate:Float=0
  894. Field _blinkOn:Bool=True
  895. Field _blinkTimer:Timer
  896. #if __HOSTOS__="macos"
  897. Field _macosMode:Bool=True
  898. #else
  899. Field _macosMode:Bool=False
  900. #endif
  901. Field _textColors:Color[]
  902. Field _anchor:Int
  903. Field _cursor:Int
  904. Field _font:Font
  905. Field _charw:Int
  906. Field _charh:Int
  907. Field _tabw:Int
  908. Field _wordWrap:Bool=False
  909. Field _wrapw:Int=$7fffffff
  910. Field _cursorRect:Recti
  911. Field _vcursor:Vec2i
  912. Field _undos:=New Stack<UndoOp>
  913. Field _redos:=New Stack<UndoOp>
  914. Field _dragging:Bool
  915. Field _readOnly:Bool
  916. Method UpdateColors()
  917. CursorColor=App.Theme.GetColor( "textview-cursor" )
  918. SelectionColor=App.Theme.GetColor( "textview-selection" )
  919. Local colors:=New Color[8]
  920. For Local i:=0 Until 8
  921. colors[i]=App.Theme.GetColor( "textview-color"+i )
  922. Next
  923. TextColors=colors
  924. End
  925. Method CancelBlinkTimer()
  926. If Not _blinkTimer Return
  927. _blinkTimer.Cancel()
  928. _blinkTimer=Null
  929. _blinkOn=True
  930. End
  931. Method RestartBlinkTimer()
  932. CancelBlinkTimer()
  933. If Not _blinkRate Or App.KeyView<>Self Return
  934. _blinkTimer=New Timer( _blinkRate,Lambda()
  935. If App.KeyView<>Self Or Not _blinkRate
  936. CancelBlinkTimer()
  937. Return
  938. Endif
  939. _blinkOn=Not _blinkOn
  940. RequestRender()
  941. End )
  942. End
  943. Method UpdateCursor()
  944. Local rect:=CharRect( _cursor )
  945. EnsureVisible( rect )
  946. _vcursor=rect.Origin
  947. If rect<>_cursorRect
  948. RestartBlinkTimer()
  949. _cursorRect=rect
  950. CursorMoved()
  951. Endif
  952. RequestRender()
  953. End
  954. Method FindWord:Int( from:Int,term:Int[] )
  955. Local text:=Text
  956. If from<0
  957. term[0]=0
  958. Return 0
  959. Else If from>=text.Length
  960. term[0]=text.Length
  961. Return text.Length
  962. Else If text[from]=10
  963. term[0]=from+1
  964. Return from
  965. Endif
  966. Local start:=from,ends:=from+1
  967. If text[from]<=32
  968. While start And text[start-1]<=32 And text[start-1]<>10
  969. start-=1
  970. Wend
  971. While ends<text.Length And text[ends]<=32 And text[ends]<>10
  972. ends+=1
  973. Wend
  974. Else if IsIdent( text[start] )
  975. While start And IsIdent( text[start-1] )
  976. start-=1
  977. Wend
  978. While ends<text.Length And IsIdent( text[ends] )
  979. ends+=1
  980. Wend
  981. Else
  982. While start And text[start-1]>32 And Not IsIdent( text[start-1] )
  983. start-=1
  984. Wend
  985. While ends<text.Length And text[ends]>32 And Not IsIdent( text[ends] )
  986. ends+=1
  987. Wend
  988. Endif
  989. term[0]=ends
  990. Return start
  991. End
  992. Method WordLength:Int( text:String,i0:Int,eol:Int )
  993. Local i1:=i0
  994. If text[i1]<=32
  995. While i1<eol And text[i1]<=32
  996. i1+=1
  997. Wend
  998. Else If IsIdent( text[i1] )
  999. While i1<eol And IsIdent( text[i1] )
  1000. i1+=1
  1001. Wend
  1002. Else
  1003. While i1<eol And text[i1]>32 And Not IsIdent( text[i1] )
  1004. i1+=1
  1005. Wend
  1006. Endif
  1007. Return i1-i0
  1008. End
  1009. Method WordWidth:Int( text:String,i0:Int,eol:Int,x0:Int )
  1010. Local i1:=i0,x1:=x0
  1011. If text[i0]<=32
  1012. While i1<eol And text[i1]<=32
  1013. If text[i1]=9
  1014. x1=Int( (x1+_tabw)/_tabw ) * _tabw
  1015. Else
  1016. x1+=_charw
  1017. Endif
  1018. i1+=1
  1019. Wend
  1020. Else
  1021. If IsIdent( text[i1] )
  1022. While i1<eol And IsIdent( text[i1] )
  1023. i1+=1
  1024. Wend
  1025. Else
  1026. While i1<eol And text[i1]>32 And Not IsIdent( text[i1] )
  1027. i1+=1
  1028. Wend
  1029. Endif
  1030. x1+=_font.TextWidth( text.Slice( i0,i1 ) )
  1031. Endif
  1032. Return x1-x0
  1033. End
  1034. Method MeasureLine:Vec2i( line:Int )
  1035. Local text:=_doc.Text
  1036. Local i0:=_doc.StartOfLine( line )
  1037. Local eol:=_doc.EndOfLine( line )
  1038. Local x0:=0,y0:=_charh,maxw:=0
  1039. While i0<eol
  1040. Local w:=WordWidth( text,i0,eol,x0 )
  1041. If x0+w>_wrapw '-_charw
  1042. maxw=Max( maxw,x0 )
  1043. y0+=_charh
  1044. x0=0
  1045. Endif
  1046. x0+=w
  1047. i0+=WordLength( text,i0,eol )
  1048. Wend
  1049. maxw=Max( maxw,x0 )
  1050. Return New Vec2i( maxw,y0 )
  1051. End
  1052. Method MoveLine( delta:Int )
  1053. Local vcursor:=_vcursor
  1054. _vcursor.y+=delta * _charh
  1055. _cursor=CharAtPoint( _vcursor )
  1056. UpdateCursor()
  1057. _vcursor.x=vcursor.x
  1058. End
  1059. Method UpdateLines()
  1060. Local liney:=0,maxWidth:=0
  1061. For Local i:=0 Until _lines.Length
  1062. Local size:=MeasureLine( i )
  1063. maxWidth=Max( maxWidth,size.x )
  1064. _lines.Data[i].maxWidth=maxWidth
  1065. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1066. liney+=size.y
  1067. Next
  1068. UpdateCursor()
  1069. End
  1070. Method RenderLine( canvas:Canvas,line:Int )
  1071. Local text:=_doc.Text
  1072. Local colors:=_doc.Colors
  1073. Local i0:=_doc.StartOfLine( line )
  1074. Local eol:=_doc.EndOfLine( line )
  1075. Local x0:=0,y0:=_lines[line].rect.Top
  1076. While i0<eol
  1077. Local w:=WordWidth( text,i0,eol,x0 )
  1078. Local l:=WordLength( text,i0,eol )
  1079. If x0+w>_wrapw '-_charw
  1080. y0+=_charh
  1081. x0=0
  1082. Endif
  1083. If text[i0]<=32
  1084. x0+=w
  1085. i0+=l
  1086. Continue
  1087. Endif
  1088. Local i1:=i0+l
  1089. While i0<i1
  1090. Local start:=i0
  1091. Local color:=colors[start]
  1092. i0+=1
  1093. While i0<i1 And colors[i0]=color
  1094. i0+=1
  1095. Wend
  1096. If color<0 Or color>=_textColors.Length color=0
  1097. canvas.Color=_textColors[color]
  1098. Local str:=text.Slice( start,i0 )
  1099. canvas.DrawText( str,x0,y0 )
  1100. x0+=_font.TextWidth( str )
  1101. Wend
  1102. Wend
  1103. End
  1104. Method LinesModified( first:Int,removed:Int,inserted:Int )
  1105. ' Print "Lines modified: first="+first+", removed="+removed+", inserted="+inserted+", _charh="+_charh
  1106. ValidateStyle()
  1107. Local last:=first+inserted+1
  1108. Local dlines:=inserted-removed
  1109. If dlines>=0
  1110. _lines.Resize( _lines.Length+dlines )
  1111. Local i:=_lines.Length
  1112. While i>last
  1113. i-=1
  1114. _lines[i]=_lines[i-dlines]
  1115. Wend
  1116. Endif
  1117. Local liney:=0,maxWidth:=0
  1118. If first
  1119. liney=_lines[first-1].rect.Bottom
  1120. maxWidth=_lines[first-1].maxWidth
  1121. Endif
  1122. For Local i:=first Until last
  1123. Local size:=MeasureLine( i )
  1124. maxWidth=Max( maxWidth,size.x )
  1125. _lines.Data[i].maxWidth=maxWidth
  1126. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1127. liney+=size.y
  1128. Next
  1129. If dlines<0
  1130. Local i:=last
  1131. While i<_lines.Length+dlines
  1132. _lines[i]=_lines[i-dlines]
  1133. i+=1
  1134. Wend
  1135. _lines.Resize( _lines.Length+dlines )
  1136. Endif
  1137. For Local i:=last Until _lines.Length
  1138. Local size:=_lines[i].rect.Size
  1139. maxWidth=Max( maxWidth,size.x )
  1140. _lines.Data[i].maxWidth=maxWidth
  1141. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1142. liney+=size.y
  1143. Next
  1144. If _cursor>_doc.TextLength
  1145. _cursor=_doc.TextLength
  1146. _anchor=_cursor
  1147. UpdateCursor()
  1148. RequestRender()
  1149. Else
  1150. _anchor=Min( _anchor,_doc.TextLength )
  1151. Endif
  1152. ' Print "Document width="+_lines.Top.maxWidth+", height="+_lines.Top.rect.Bottom
  1153. End
  1154. End