textview.monkey2 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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 '-_charw
  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. Exit
  379. Endif
  380. x0+=w
  381. i0+=l
  382. Wend
  383. Local w:=_charw
  384. If i0<eol And text[i0]>32 w=_font.TextWidth( text.Slice( i0,i0+1 ) )
  385. Return New Recti( x0,y0,x0+w,y0+_charh )
  386. End
  387. #rem monkeydoc Returns the index of the character nearest to a given point.
  388. #end
  389. Method CharAtPoint:Int( p:Vec2i )
  390. Local line:=LineAtPoint( p )
  391. Local text:=_doc.Text
  392. Local i0:=_doc.StartOfLine( line )
  393. Local eol:=_doc.EndOfLine( line )
  394. Local x0:=0,y0:=_lines[line].rect.Top+_charh
  395. While i0<eol
  396. Local w:=WordWidth( text,i0,eol,x0 )
  397. If x0+w>_wrapw '-_charw
  398. If p.y<y0 Exit
  399. y0+=_charh
  400. x0=0
  401. Endif
  402. Local l:=WordLength( text,i0,eol )
  403. If p.x<x0+w And p.y<y0
  404. For Local i:=0 Until l
  405. x0+=WordWidth( text,i0,i0+1,x0 )
  406. If p.x<x0 Exit
  407. i0+=1
  408. Next
  409. Exit
  410. Endif
  411. x0+=w
  412. i0+=l
  413. Wend
  414. Return i0
  415. End
  416. #rem monkedoc Returns the index of the line nearest to a given point.
  417. #end
  418. Method LineAtPoint:Int( p:Vec2i )
  419. If p.y<=0 Return 0
  420. If p.y>=_lines.Top.rect.Top Return _lines.Length-1
  421. Local min:=0,max:=_lines.Length-1
  422. Repeat
  423. Local line:=(min+max)/2
  424. If p.y>=_lines[line].rect.Bottom
  425. min=line+1
  426. Else If max-min>1
  427. max=line
  428. Else
  429. Return min
  430. Endif
  431. Forever
  432. Return 0
  433. End
  434. #rem monkeydoc Gets the bounding rect for a line.
  435. #end
  436. Method LineRect:Recti( line:Int )
  437. If line>=0 And line<_lines.Length Return _lines[line].rect
  438. Return New Recti
  439. End
  440. #rem monkeydoc Clears all text.
  441. #end
  442. Method Clear()
  443. SelectAll()
  444. ReplaceText( "" )
  445. End
  446. #rem monkeydoc Move cursor to line.
  447. #end
  448. Method GotoLine( line:Int )
  449. _anchor=_doc.StartOfLine( line )
  450. _cursor=_anchor
  451. UpdateCursor()
  452. End
  453. #rem monkeydoc Selects a line.
  454. #end
  455. Method SelectLine( line:Int )
  456. SelectText( _doc.StartOfLine( line ),_doc.EndOfLine( line ) )
  457. End
  458. #rem monkeydoc Selects text in a range.
  459. #end
  460. Method SelectText( anchor:Int,cursor:Int )
  461. _anchor=Clamp( anchor,0,_doc.TextLength )
  462. _cursor=Clamp( cursor,0,_doc.TextLength )
  463. UpdateCursor()
  464. End
  465. #rem monkeydoc Appends text.
  466. #end
  467. Method AppendText( text:String )
  468. SelectText( _doc.TextLength,_doc.TextLength )
  469. ReplaceText( text )
  470. End
  471. #rem monkeydoc Replaces current selection.
  472. #end
  473. Method ReplaceText( text:String )
  474. Local undo:=New UndoOp
  475. undo.text=_doc.Text.Slice( Min( _anchor,_cursor ),Max( _anchor,_cursor ) )
  476. undo.anchor=Min( _anchor,_cursor )
  477. undo.cursor=undo.anchor+text.Length
  478. _undos.Push( undo )
  479. ReplaceText( _anchor,_cursor,text )
  480. End
  481. 'non-undoable
  482. #rem monkeydoc @hidden
  483. #end
  484. Method ReplaceText( anchor:Int,cursor:Int,text:String )
  485. _redos.Clear()
  486. _doc.ReplaceText( anchor,cursor,text )
  487. _cursor=Min( anchor,cursor )+text.Length
  488. _anchor=_cursor
  489. UpdateCursor()
  490. End
  491. #rem monkeydoc Performs an undo.
  492. #end
  493. Method Undo()
  494. If _readOnly Return
  495. If _undos.Empty Return
  496. Local undo:=_undos.Pop()
  497. Local text:=undo.text
  498. Local anchor:=undo.anchor
  499. Local cursor:=undo.cursor
  500. undo.text=_doc.Text.Slice( anchor,cursor )
  501. undo.cursor=anchor+text.Length
  502. _redos.Push( undo )
  503. _doc.ReplaceText( anchor,cursor,text )
  504. _cursor=anchor+text.Length
  505. _anchor=_cursor
  506. UpdateCursor()
  507. End
  508. #rem monkeydoc Performs a redo.
  509. #end
  510. Method Redo()
  511. If _readOnly Return
  512. If _redos.Empty Return
  513. Local undo:=_redos.Pop()
  514. Local text:=undo.text
  515. Local anchor:=undo.anchor
  516. Local cursor:=undo.cursor
  517. undo.text=_doc.Text.Slice( anchor,cursor )
  518. undo.cursor=anchor+text.Length
  519. _undos.Push( undo )
  520. _doc.ReplaceText( anchor,cursor,text )
  521. _cursor=anchor+text.Length
  522. _anchor=_cursor
  523. UpdateCursor()
  524. End
  525. #rem monkeydoc Selects all text.
  526. #end
  527. Method SelectAll()
  528. SelectText( 0,_doc.TextLength )
  529. End
  530. #rem monkeydoc Performs a cut.
  531. #end
  532. Method Cut()
  533. If _readOnly Return
  534. Copy()
  535. ReplaceText( "" )
  536. End
  537. #rem monkeydoc Performs a copy.
  538. #end
  539. Method Copy()
  540. Local min:=Min( _anchor,_cursor )
  541. Local max:=Max( _anchor,_cursor )
  542. Local text:=_doc.Text.Slice( min,max )
  543. App.ClipboardText=text
  544. End
  545. #rem monkeydoc Performs a paste.
  546. #end
  547. Method Paste()
  548. If _readOnly Return
  549. If App.ClipboardTextEmpty Return
  550. Local text:String=App.ClipboardText
  551. text=text.Replace( "~r~n","~n" )
  552. text=text.Replace( "~r","~n" )
  553. If text ReplaceText( text )
  554. End
  555. Protected
  556. Method OnThemeChanged() Override
  557. UpdateColors()
  558. End
  559. Method OnValidateStyle() Override
  560. Local style:=RenderStyle
  561. _font=style.Font
  562. _charw=_font.TextWidth( "X" )
  563. _charh=_font.Height
  564. _tabw=_charw*_tabStop
  565. UpdateLines()
  566. End
  567. Method OnMeasureContent:Vec2i() Override
  568. If _wordWrap Return New Vec2i( 0,0 )
  569. If _wrapw<>$7fffffff
  570. _wrapw=$7fffffff
  571. UpdateLines()
  572. Endif
  573. Return New Vec2i( _lines.Top.maxWidth+_charw,_lines.Top.rect.Bottom )
  574. End
  575. Method OnMeasureContent2:Vec2i( size:Vec2i ) Override
  576. If Not _wordWrap Return New Vec2i( 0,0 )
  577. If _wrapw<>size.x
  578. _wrapw=size.x
  579. UpdateLines()
  580. Endif
  581. Return New Vec2i( _lines.Top.maxWidth,_lines.Top.rect.Bottom )
  582. End
  583. Method OnRenderContent( canvas:Canvas ) Override
  584. If App.KeyView=Self And Not _blinkTimer RestartBlinkTimer()
  585. Local clip:=VisibleRect
  586. Local firstLine:=LineAtPoint( New Vec2i( 0,clip.Top ) )
  587. Local lastLine:=LineAtPoint( New Vec2i( 0,clip.Bottom-1 ) )+1
  588. If _cursor<>_anchor
  589. Local min:=CharRect( Min( _anchor,_cursor ) )
  590. Local max:=CharRect( Max( _anchor,_cursor ) )
  591. canvas.Color=_selColor
  592. If min.Y=max.Y
  593. canvas.DrawRect( min.Left,min.Top,max.Left-min.Left,min.Height )
  594. Else
  595. canvas.DrawRect( min.Left,min.Top,(clip.Right-min.Left),min.Height )
  596. canvas.DrawRect( 0,min.Bottom,clip.Right,max.Top-min.Bottom )
  597. canvas.DrawRect( 0,max.Top,max.Left,max.Height )
  598. Endif
  599. Else If Not _readOnly And App.KeyView=Self And _blinkOn
  600. canvas.Color=_cursorColor
  601. Select _cursorType
  602. Case CursorType.Block
  603. canvas.DrawRect( _cursorRect )
  604. Case CursorType.IBeam
  605. canvas.DrawRect( _cursorRect.X,_cursorRect.Y,1,_cursorRect.Height )
  606. canvas.DrawRect( _cursorRect.X-2,_cursorRect.Y,5,1 )
  607. canvas.DrawRect( _cursorRect.X-2,_cursorRect.Y+_cursorRect.Height-1,5,1 )
  608. Default
  609. canvas.DrawRect( _cursorRect.X,_cursorRect.Y,1,_cursorRect.Height )
  610. End
  611. Endif
  612. _textColors[0]=RenderStyle.TextColor
  613. For Local line:=firstLine Until lastLine
  614. RenderLine( canvas,line )
  615. Next
  616. End
  617. Method OnKeyDown:Bool( key:Key,modifiers:Modifier=Null )
  618. Select key
  619. Case Key.Backspace
  620. If _anchor=_cursor And _cursor>0 SelectText( _cursor-1,_cursor )
  621. ReplaceText( "" )
  622. Case Key.KeyDelete
  623. If _anchor=_cursor And _cursor<_doc.Text.Length SelectText( _cursor,_cursor+1 )
  624. ReplaceText( "" )
  625. Case Key.Tab
  626. Local min:=_doc.FindLine( Min( _cursor,_anchor ) )
  627. Local max:=_doc.FindLine( Max( _cursor,_anchor ) )
  628. If min=max
  629. ReplaceText( "~t" )
  630. Else
  631. 'block tab/untab
  632. Local lines:=New StringStack
  633. For Local i:=min Until max
  634. lines.Push( _doc.GetLine( i ) )
  635. Next
  636. Local go:=True
  637. If modifiers & Modifier.Shift
  638. For Local i:=0 Until lines.Length
  639. If Not lines[i].Trim()
  640. lines[i]+="~n"
  641. Continue
  642. Endif
  643. If lines[i][0]=9
  644. lines[i]=lines[i].Slice( 1 )+"~n"
  645. Continue
  646. Endif
  647. go=False
  648. Exit
  649. Next
  650. Else
  651. For Local i:=0 Until lines.Length
  652. lines[i]="~t"+lines[i]+"~n"
  653. Next
  654. Endif
  655. If go
  656. SelectText( _doc.StartOfLine( min ),_doc.StartOfLine( max ) )
  657. ReplaceText( lines.Join( "" ) )
  658. SelectText( _doc.StartOfLine( min ),_doc.StartOfLine( max ) )
  659. Return False
  660. Endif
  661. Endif
  662. Case Key.Enter,Key.KeypadEnter
  663. ReplaceText( "~n" )
  664. 'auto indent!
  665. Local line:=CursorLine
  666. If line>0
  667. Local ptext:=_doc.GetLine( line-1 )
  668. Local indent:=ptext
  669. For Local i:=0 Until ptext.Length
  670. If ptext[i]<=32 Continue
  671. indent=ptext.Slice( 0,i )
  672. Exit
  673. Next
  674. If indent ReplaceText( indent )
  675. Endif
  676. Case Key.Left
  677. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  678. _cursor=Min( _anchor,_cursor )
  679. Else If _cursor
  680. _cursor-=1
  681. Endif
  682. UpdateCursor()
  683. Case Key.Right
  684. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  685. _cursor=Max( _anchor,_cursor )
  686. Else If _cursor<_doc.Text.Length
  687. _cursor+=1
  688. Endif
  689. UpdateCursor()
  690. Case Key.Home
  691. _cursor=_doc.StartOfLine( CursorLine )
  692. UpdateCursor()
  693. Case Key.KeyEnd
  694. _cursor=_doc.EndOfLine( CursorLine )
  695. UpdateCursor()
  696. Case Key.Up
  697. MoveLine( -1 )
  698. Case Key.Down
  699. MoveLine( 1 )
  700. Case Key.PageUp
  701. Local n:=VisibleRect.Height/_charh-1 'shouldn't really use cliprect here...
  702. MoveLine( -n )
  703. Case Key.PageDown
  704. Local n:=VisibleRect.Height/_charh-1
  705. MoveLine( n )
  706. Default
  707. Return False
  708. End
  709. Return True
  710. End
  711. Method OnControlKeyDown:bool( key:Key,modifiers:Modifier=Null ) Virtual
  712. Select key
  713. Case Key.A
  714. SelectAll()
  715. Case Key.X
  716. Cut()
  717. Case Key.C
  718. Copy()
  719. Case Key.V
  720. Paste()
  721. Case Key.Z
  722. Undo()
  723. Case Key.Y
  724. Redo()
  725. Case Key.Home
  726. _cursor=0
  727. UpdateCursor()
  728. Return True
  729. Case Key.KeyEnd
  730. _cursor=_doc.TextLength
  731. UpdateCursor()
  732. Return True
  733. Case Key.Left
  734. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  735. _cursor=Min( _anchor,_cursor )
  736. Endif
  737. Local term:=New Int[1]
  738. Local start:=FindWord( _cursor,term )
  739. _cursor=Max( start-1,0 )
  740. Local text:=Text
  741. While _cursor And text[_cursor-1]<=32 And text[_cursor-1]<>10
  742. _cursor-=1
  743. Wend
  744. UpdateCursor()
  745. Return True
  746. Case Key.Right
  747. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  748. _cursor=Max( _anchor,_cursor )
  749. Endif
  750. Local term:=New Int[1]
  751. Local start:=FindWord( _cursor,term )
  752. _cursor=term[0]
  753. Local text:=Text
  754. While _cursor<text.Length And text[_cursor]<=32 And text[_cursor]<>10
  755. _cursor+=1
  756. Wend
  757. UpdateCursor()
  758. Return True
  759. End
  760. Return False
  761. End
  762. Method OnKeyEvent( event:KeyEvent ) Override
  763. If _readOnly
  764. If _macosMode
  765. If (event.Modifiers & Modifier.Gui) And (event.Key=Key.C Or event.Key=Key.A)
  766. 'Copy, Select All
  767. Else
  768. Return
  769. Endif
  770. Else
  771. If (event.Modifiers & Modifier.Control) And (event.Key=Key.C Or event.Key=Key.A)
  772. 'Copy, Select All
  773. Else
  774. Return
  775. Endif
  776. Endif
  777. Endif
  778. Select event.Type
  779. Case EventType.KeyDown,EventType.KeyRepeat
  780. If _macosMode
  781. If event.Modifiers & Modifier.Gui
  782. Select event.Key
  783. Case Key.Home,Key.KeyEnd
  784. Default
  785. If Not OnControlKeyDown( event.Key,event.Modifiers ) Return
  786. End
  787. Else If event.Modifiers & Modifier.Control
  788. Select event.Key
  789. Case Key.A
  790. OnKeyDown( Key.Home )
  791. Case Key.E
  792. OnKeyDown( Key.KeyEnd )
  793. End
  794. Else
  795. Select event.Key
  796. Case Key.Home
  797. OnControlKeyDown( Key.Home )
  798. Case Key.KeyEnd
  799. OnControlKeyDown( Key.KeyEnd )
  800. Default
  801. If Not OnKeyDown( event.Key,event.Modifiers ) Return
  802. End
  803. Endif
  804. Else
  805. If event.Modifiers & Modifier.Control
  806. If Not OnControlKeyDown( event.Key,event.Modifiers ) Return
  807. Else
  808. If Not OnKeyDown( event.Key,event.Modifiers ) Return
  809. Endif
  810. Endif
  811. If Not (event.Modifiers & Modifier.Shift) _anchor=_cursor
  812. Case EventType.KeyChar
  813. If _undos.Length
  814. Local undo:=_undos.Top
  815. If Not undo.text And _cursor=undo.cursor
  816. ReplaceText( _anchor,_cursor,event.Text )
  817. undo.cursor=_cursor
  818. Return
  819. Endif
  820. Endif
  821. ReplaceText( event.Text )
  822. End
  823. End
  824. Method OnContentMouseEvent( event:MouseEvent ) Override
  825. Select event.Type
  826. Case EventType.MouseDown
  827. Select event.Clicks
  828. Case 1
  829. _cursor=CharAtPoint( event.Location )
  830. If Not (event.Modifiers & Modifier.Shift) _anchor=_cursor
  831. _dragging=True
  832. MakeKeyView()
  833. UpdateCursor()
  834. Case 2
  835. Local term:=New Int[1]
  836. Local start:=FindWord( CharAtPoint( event.Location ),term )
  837. SelectText( start,term[0] )
  838. Case 3
  839. SelectLine( LineAtPoint( event.Location ) )
  840. End
  841. Return
  842. #rem
  843. Case EventType.MouseClick
  844. _cursor=CharAtPoint( event.Location )
  845. If Not (event.Modifiers & Modifier.Shift) _anchor=_cursor
  846. _dragging=True
  847. MakeKeyView()
  848. UpdateCursor()
  849. Case EventType.MouseDoubleClick
  850. Local term:=New Int[1]
  851. Local start:=FindWord( CharAtPoint( event.Location ),term )
  852. SelectText( start,term[0] )
  853. #end
  854. Case EventType.MouseUp
  855. _dragging=False
  856. Case EventType.MouseMove
  857. If _dragging
  858. _cursor=CharAtPoint( event.Location )
  859. UpdateCursor()
  860. Endif
  861. Case EventType.MouseWheel
  862. Return
  863. End
  864. event.Eat()
  865. End
  866. Private
  867. Struct Line
  868. Field rect:Recti
  869. Field maxWidth:Int
  870. End
  871. Class UndoOp
  872. Field text:String
  873. Field anchor:Int
  874. Field cursor:Int
  875. End
  876. Field _doc:TextDocument
  877. Field _lines:=New Stack<Line>
  878. Field _tabStop:Int=4
  879. Field _tabSpaces:String=" "
  880. Field _cursorColor:Color=New Color( 0,.5,1,1 )
  881. Field _selColor:Color=New Color( 1,1,1,.25 )
  882. Field _cursorType:CursorType
  883. Field _blinkRate:Float=0
  884. Field _blinkOn:Bool=True
  885. Field _blinkTimer:Timer
  886. #if __HOSTOS__="macos"
  887. Field _macosMode:Bool=True
  888. #else
  889. Field _macosMode:Bool=False
  890. #endif
  891. Field _textColors:Color[]
  892. Field _anchor:Int
  893. Field _cursor:Int
  894. Field _font:Font
  895. Field _charw:Int
  896. Field _charh:Int
  897. Field _tabw:Int
  898. Field _wordWrap:Bool=False
  899. Field _wrapw:Int=$7fffffff
  900. Field _cursorRect:Recti
  901. Field _vcursor:Vec2i
  902. Field _undos:=New Stack<UndoOp>
  903. Field _redos:=New Stack<UndoOp>
  904. Field _dragging:Bool
  905. Field _readOnly:Bool
  906. Method UpdateColors()
  907. CursorColor=App.Theme.GetColor( "textview-cursor" )
  908. SelectionColor=App.Theme.GetColor( "textview-selection" )
  909. Local colors:=New Color[8]
  910. For Local i:=0 Until 8
  911. colors[i]=App.Theme.GetColor( "textview-color"+i )
  912. Next
  913. TextColors=colors
  914. End
  915. Method CancelBlinkTimer()
  916. If Not _blinkTimer Return
  917. _blinkTimer.Cancel()
  918. _blinkTimer=Null
  919. _blinkOn=True
  920. End
  921. Method RestartBlinkTimer()
  922. CancelBlinkTimer()
  923. If Not _blinkRate Or App.KeyView<>Self Return
  924. _blinkTimer=New Timer( _blinkRate,Lambda()
  925. If App.KeyView<>Self Or Not _blinkRate
  926. CancelBlinkTimer()
  927. Return
  928. Endif
  929. _blinkOn=Not _blinkOn
  930. RequestRender()
  931. End )
  932. End
  933. Method UpdateCursor()
  934. Local rect:=CharRect( _cursor )
  935. EnsureVisible( rect )
  936. _vcursor=rect.Origin
  937. If rect<>_cursorRect
  938. RestartBlinkTimer()
  939. _cursorRect=rect
  940. CursorMoved()
  941. Endif
  942. RequestRender()
  943. End
  944. Method FindWord:Int( from:Int,term:Int[] )
  945. Local text:=Text
  946. If from<0
  947. term[0]=0
  948. Return 0
  949. Else If from>=text.Length
  950. term[0]=text.Length
  951. Return text.Length
  952. Else If text[from]=10
  953. term[0]=from+1
  954. Return from
  955. Endif
  956. Local start:=from,ends:=from+1
  957. If text[from]<=32
  958. While start And text[start-1]<=32 And text[start-1]<>10
  959. start-=1
  960. Wend
  961. While ends<text.Length And text[ends]<=32 And text[ends]<>10
  962. ends+=1
  963. Wend
  964. Else if IsIdent( text[start] )
  965. While start And IsIdent( text[start-1] )
  966. start-=1
  967. Wend
  968. While ends<text.Length And IsIdent( text[ends] )
  969. ends+=1
  970. Wend
  971. Else
  972. While start And text[start-1]>32 And Not IsIdent( text[start-1] )
  973. start-=1
  974. Wend
  975. While ends<text.Length And text[ends]>32 And Not IsIdent( text[ends] )
  976. ends+=1
  977. Wend
  978. Endif
  979. term[0]=ends
  980. Return start
  981. End
  982. Method WordLength:Int( text:String,i0:Int,eol:Int )
  983. Local i1:=i0
  984. If text[i1]<=32
  985. While i1<eol And text[i1]<=32
  986. i1+=1
  987. Wend
  988. Else If IsIdent( text[i1] )
  989. While i1<eol And IsIdent( text[i1] )
  990. i1+=1
  991. Wend
  992. Else
  993. While i1<eol And text[i1]>32 And Not IsIdent( text[i1] )
  994. i1+=1
  995. Wend
  996. Endif
  997. Return i1-i0
  998. End
  999. Method WordWidth:Int( text:String,i0:Int,eol:Int,x0:Int )
  1000. Local i1:=i0,x1:=x0
  1001. If text[i0]<=32
  1002. While i1<eol And text[i1]<=32
  1003. If text[i1]=9
  1004. x1=Int( (x1+_tabw)/_tabw ) * _tabw
  1005. Else
  1006. x1+=_charw
  1007. Endif
  1008. i1+=1
  1009. Wend
  1010. Else
  1011. If IsIdent( text[i1] )
  1012. While i1<eol And IsIdent( text[i1] )
  1013. i1+=1
  1014. Wend
  1015. Else
  1016. While i1<eol And text[i1]>32 And Not IsIdent( text[i1] )
  1017. i1+=1
  1018. Wend
  1019. Endif
  1020. x1+=_font.TextWidth( text.Slice( i0,i1 ) )
  1021. Endif
  1022. Return x1-x0
  1023. End
  1024. Method MeasureLine:Vec2i( line:Int )
  1025. Local text:=_doc.Text
  1026. Local i0:=_doc.StartOfLine( line )
  1027. Local eol:=_doc.EndOfLine( line )
  1028. Local x0:=0,y0:=_charh,maxw:=0
  1029. While i0<eol
  1030. Local w:=WordWidth( text,i0,eol,x0 )
  1031. If x0+w>_wrapw '-_charw
  1032. maxw=Max( maxw,x0 )
  1033. y0+=_charh
  1034. x0=0
  1035. Endif
  1036. x0+=w
  1037. i0+=WordLength( text,i0,eol )
  1038. Wend
  1039. maxw=Max( maxw,x0 )
  1040. Return New Vec2i( maxw,y0 )
  1041. End
  1042. Method MoveLine( delta:Int )
  1043. Local vcursor:=_vcursor
  1044. _vcursor.y+=delta * _charh
  1045. _cursor=CharAtPoint( _vcursor )
  1046. UpdateCursor()
  1047. _vcursor.x=vcursor.x
  1048. End
  1049. Method UpdateLines()
  1050. Local liney:=0,maxWidth:=0
  1051. For Local i:=0 Until _lines.Length
  1052. Local size:=MeasureLine( i )
  1053. maxWidth=Max( maxWidth,size.x )
  1054. _lines.Data[i].maxWidth=maxWidth
  1055. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1056. liney+=size.y
  1057. Next
  1058. UpdateCursor()
  1059. End
  1060. Method RenderLine( canvas:Canvas,line:Int )
  1061. Local text:=_doc.Text
  1062. Local colors:=_doc.Colors
  1063. Local i0:=_doc.StartOfLine( line )
  1064. Local eol:=_doc.EndOfLine( line )
  1065. Local x0:=0,y0:=_lines[line].rect.Top
  1066. While i0<eol
  1067. Local w:=WordWidth( text,i0,eol,x0 )
  1068. Local l:=WordLength( text,i0,eol )
  1069. If x0+w>_wrapw '-_charw
  1070. y0+=_charh
  1071. x0=0
  1072. Endif
  1073. If text[i0]<=32
  1074. x0+=w
  1075. i0+=l
  1076. Continue
  1077. Endif
  1078. Local i1:=i0+l
  1079. While i0<i1
  1080. Local start:=i0
  1081. Local color:=colors[start]
  1082. i0+=1
  1083. While i0<i1 And colors[i0]=color
  1084. i0+=1
  1085. Wend
  1086. If color<0 Or color>=_textColors.Length color=0
  1087. canvas.Color=_textColors[color]
  1088. Local str:=text.Slice( start,i0 )
  1089. canvas.DrawText( str,x0,y0 )
  1090. x0+=_font.TextWidth( str )
  1091. Wend
  1092. Wend
  1093. End
  1094. Method LinesModified( first:Int,removed:Int,inserted:Int )
  1095. ' Print "Lines modified: first="+first+", removed="+removed+", inserted="+inserted+", _charh="+_charh
  1096. ValidateStyle()
  1097. Local last:=first+inserted+1
  1098. Local dlines:=inserted-removed
  1099. If dlines>=0
  1100. _lines.Resize( _lines.Length+dlines )
  1101. Local i:=_lines.Length
  1102. While i>last
  1103. i-=1
  1104. _lines[i]=_lines[i-dlines]
  1105. Wend
  1106. Endif
  1107. Local liney:=0,maxWidth:=0
  1108. If first
  1109. liney=_lines[first-1].rect.Bottom
  1110. maxWidth=_lines[first-1].maxWidth
  1111. Endif
  1112. For Local i:=first Until last
  1113. Local size:=MeasureLine( i )
  1114. maxWidth=Max( maxWidth,size.x )
  1115. _lines.Data[i].maxWidth=maxWidth
  1116. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1117. liney+=size.y
  1118. Next
  1119. If dlines<0
  1120. Local i:=last
  1121. While i<_lines.Length+dlines
  1122. _lines[i]=_lines[i-dlines]
  1123. i+=1
  1124. Wend
  1125. _lines.Resize( _lines.Length+dlines )
  1126. Endif
  1127. For Local i:=last Until _lines.Length
  1128. Local size:=_lines[i].rect.Size
  1129. maxWidth=Max( maxWidth,size.x )
  1130. _lines.Data[i].maxWidth=maxWidth
  1131. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1132. liney+=size.y
  1133. Next
  1134. If _cursor>_doc.TextLength
  1135. _cursor=_doc.TextLength
  1136. _anchor=_cursor
  1137. UpdateCursor()
  1138. RequestRender()
  1139. Else
  1140. _anchor=Min( _anchor,_doc.TextLength )
  1141. Endif
  1142. ' Print "Document width="+_lines.Top.maxWidth+", height="+_lines.Top.rect.Bottom
  1143. End
  1144. End