textview.monkey2 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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 ) Virtual
  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. Endif
  669. Endif
  670. Case Key.Enter,Key.KeypadEnter
  671. ReplaceText( "~n" )
  672. 'auto indent!
  673. Local line:=CursorLine
  674. If line>0
  675. Local ptext:=_doc.GetLine( line-1 )
  676. Local indent:=ptext
  677. For Local i:=0 Until ptext.Length
  678. If ptext[i]<=32 Continue
  679. indent=ptext.Slice( 0,i )
  680. Exit
  681. Next
  682. If indent ReplaceText( indent )
  683. Endif
  684. Case Key.Left
  685. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  686. _cursor=Min( _anchor,_cursor )
  687. Else If _cursor
  688. _cursor-=1
  689. Endif
  690. UpdateCursor()
  691. Return True
  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. Return True
  700. Case Key.Home
  701. _cursor=_doc.StartOfLine( CursorLine )
  702. UpdateCursor()
  703. Return True
  704. Case Key.KeyEnd
  705. _cursor=_doc.EndOfLine( CursorLine )
  706. UpdateCursor()
  707. Return True
  708. Case Key.Up
  709. MoveLine( -1 )
  710. Return True
  711. Case Key.Down
  712. MoveLine( 1 )
  713. Return True
  714. Case Key.PageUp
  715. Local n:=VisibleRect.Height/_charh-1 'shouldn't really use cliprect here...
  716. MoveLine( -n )
  717. Return True
  718. Case Key.PageDown
  719. Local n:=VisibleRect.Height/_charh-1
  720. MoveLine( n )
  721. Return True
  722. End
  723. Return False
  724. End
  725. Method OnControlKeyDown:Bool( key:Key,modifiers:Modifier ) Virtual
  726. Select key
  727. Case Key.A
  728. SelectAll()
  729. Case Key.X
  730. Cut()
  731. Case Key.C
  732. Copy()
  733. Case Key.V
  734. Paste()
  735. Case Key.Z
  736. Undo()
  737. Case Key.Y
  738. Redo()
  739. Case Key.Home
  740. _cursor=0
  741. UpdateCursor()
  742. Return True
  743. Case Key.KeyEnd
  744. _cursor=_doc.TextLength
  745. UpdateCursor()
  746. Return True
  747. Case Key.Left
  748. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  749. _cursor=Min( _anchor,_cursor )
  750. Endif
  751. Local text:=Text
  752. Local term:=New Int[1]
  753. _cursor=FindWord( _cursor,term )
  754. While _cursor And text[_cursor-1]<=32 And text[_cursor-1]<>10
  755. _cursor-=1
  756. Wend
  757. _cursor=FindWord( Max( _cursor-1,0 ),term )
  758. UpdateCursor()
  759. Return True
  760. Case Key.Right
  761. If _anchor<>_cursor And Not (modifiers & Modifier.Shift)
  762. _cursor=Max( _anchor,_cursor )
  763. Endif
  764. 'next word...
  765. Local text:=Text
  766. Local term:=New Int[1]
  767. FindWord( _cursor,term )
  768. _cursor=term[0]
  769. While _cursor<text.Length And text[_cursor]<=32 And text[_cursor]<>10
  770. _cursor+=1
  771. Wend
  772. UpdateCursor()
  773. Return True
  774. End
  775. Return False
  776. End
  777. Method OnKeyChar( text:String ) Virtual
  778. If _undos.Length
  779. Local undo:=_undos.Top
  780. If Not undo.text And _cursor=undo.cursor
  781. ReplaceText( _anchor,_cursor,text )
  782. undo.cursor=_cursor
  783. Return
  784. Endif
  785. Endif
  786. ReplaceText( text )
  787. End
  788. Method OnKeyEvent( event:KeyEvent ) Override
  789. If _readOnly
  790. If _macosMode
  791. If (event.Modifiers & Modifier.Gui) And (event.Key=Key.C Or event.Key=Key.A)
  792. 'Copy, Select All
  793. Else
  794. Return
  795. Endif
  796. Else
  797. If (event.Modifiers & Modifier.Control) And (event.Key=Key.C Or event.Key=Key.A)
  798. 'Copy, Select All
  799. Else
  800. Return
  801. Endif
  802. Endif
  803. Endif
  804. Select event.Type
  805. Case EventType.KeyDown,EventType.KeyRepeat
  806. Local key:=event.Key
  807. Local modifiers:=event.Modifiers
  808. 'map keypad nav keys...
  809. If Not (modifiers & Modifier.NumLock)
  810. Select key
  811. Case Key.Keypad1 key=Key.KeyEnd
  812. Case Key.Keypad2 key=Key.Down
  813. Case Key.Keypad3 key=Key.PageDown
  814. Case Key.Keypad4 key=Key.Left
  815. Case Key.Keypad6 key=Key.Right
  816. Case Key.Keypad7 key=Key.Home
  817. Case Key.Keypad8 key=Key.Up
  818. Case Key.Keypad9 key=Key.PageUp
  819. Case Key.Keypad0 key=Key.Insert
  820. End
  821. Endif
  822. Local r:=False
  823. If _macosMode
  824. If modifiers & Modifier.Gui
  825. Select key
  826. Case Key.A,Key.X,Key.C,Key.V,Key.Z,Key.Y,Key.Left,Key.Right
  827. r=OnControlKeyDown( key,modifiers )
  828. End
  829. Else If modifiers & Modifier.Control
  830. Select key
  831. Case Key.A
  832. r=OnKeyDown( Key.Home,modifiers )
  833. Case Key.E
  834. r=OnKeyDown( Key.KeyEnd,modifiers )
  835. End
  836. Else
  837. Select key
  838. Case Key.Home,Key.KeyEnd
  839. r=OnControlKeyDown( key,modifiers )
  840. Default
  841. r=OnKeyDown( key,modifiers )
  842. End
  843. Endif
  844. Else
  845. If modifiers & Modifier.Control
  846. r=OnControlKeyDown( key,modifiers )
  847. Else
  848. r=OnKeyDown( key,modifiers )
  849. Endif
  850. Endif
  851. If r And Not (modifiers & Modifier.Shift) _anchor=_cursor
  852. Case EventType.KeyChar
  853. OnKeyChar( event.Text )
  854. End
  855. End
  856. Method OnContentMouseEvent( event:MouseEvent ) Override
  857. Select event.Type
  858. Case EventType.MouseDown
  859. Select event.Clicks
  860. Case 1
  861. _cursor=CharAtPoint( event.Location )
  862. If Not (event.Modifiers & Modifier.Shift) _anchor=_cursor
  863. _dragging=True
  864. MakeKeyView()
  865. UpdateCursor()
  866. Case 2
  867. Local term:=New Int[1]
  868. Local start:=FindWord( CharAtPoint( event.Location ),term )
  869. SelectText( start,term[0] )
  870. Case 3
  871. SelectLine( LineAtPoint( event.Location ) )
  872. End
  873. Return
  874. Case EventType.MouseUp
  875. _dragging=False
  876. Case EventType.MouseMove
  877. If _dragging
  878. _cursor=CharAtPoint( event.Location )
  879. UpdateCursor()
  880. Endif
  881. Case EventType.MouseWheel
  882. Return
  883. End
  884. event.Eat()
  885. End
  886. Private
  887. Struct Line
  888. Field rect:Recti
  889. Field maxWidth:Int
  890. End
  891. Class UndoOp
  892. Field text:String
  893. Field anchor:Int
  894. Field cursor:Int
  895. End
  896. Field _doc:TextDocument
  897. Field _lines:=New Stack<Line>
  898. Field _tabStop:Int=4
  899. Field _tabSpaces:String=" "
  900. Field _cursorColor:Color=New Color( 0,.5,1,1 )
  901. Field _selColor:Color=New Color( 1,1,1,.25 )
  902. Field _cursorType:CursorType
  903. Field _blinkRate:Float=0
  904. Field _blinkOn:Bool=True
  905. Field _blinkTimer:Timer
  906. #if __HOSTOS__="macos"
  907. Field _macosMode:Bool=True
  908. #else
  909. Field _macosMode:Bool=False
  910. #endif
  911. Field _textColors:Color[]
  912. Field _anchor:Int
  913. Field _cursor:Int
  914. Field _font:Font
  915. Field _charw:Int
  916. Field _charh:Int
  917. Field _tabw:Int
  918. Field _wordWrap:Bool=False
  919. Field _wrapw:Int=$7fffffff
  920. Field _cursorRect:Recti
  921. Field _vcursor:Vec2i
  922. Field _undos:=New Stack<UndoOp>
  923. Field _redos:=New Stack<UndoOp>
  924. Field _dragging:Bool
  925. Field _readOnly:Bool
  926. Method UpdateColors()
  927. CursorColor=App.Theme.GetColor( "textview-cursor" )
  928. SelectionColor=App.Theme.GetColor( "textview-selection" )
  929. Local colors:=New Color[8]
  930. For Local i:=0 Until 8
  931. colors[i]=App.Theme.GetColor( "textview-color"+i )
  932. Next
  933. TextColors=colors
  934. End
  935. Method CancelBlinkTimer()
  936. If Not _blinkTimer Return
  937. _blinkTimer.Cancel()
  938. _blinkTimer=Null
  939. _blinkOn=True
  940. End
  941. Method RestartBlinkTimer()
  942. CancelBlinkTimer()
  943. If Not _blinkRate Or App.KeyView<>Self Return
  944. _blinkTimer=New Timer( _blinkRate,Lambda()
  945. If App.KeyView<>Self Or Not _blinkRate
  946. CancelBlinkTimer()
  947. Return
  948. Endif
  949. _blinkOn=Not _blinkOn
  950. RequestRender()
  951. End )
  952. End
  953. Method UpdateCursor()
  954. Local rect:=CharRect( _cursor )
  955. EnsureVisible( rect )
  956. _vcursor=rect.Origin
  957. If rect<>_cursorRect
  958. RestartBlinkTimer()
  959. _cursorRect=rect
  960. CursorMoved()
  961. Endif
  962. RequestRender()
  963. End
  964. Method FindWord:Int( from:Int,term:Int[] )
  965. Local text:=Text
  966. If from<0
  967. term[0]=0
  968. Return 0
  969. Else If from>=text.Length
  970. term[0]=text.Length
  971. Return text.Length
  972. Else If text[from]=10
  973. term[0]=from+1
  974. Return from
  975. Endif
  976. Local start:=from,ends:=from+1
  977. If text[from]<=32
  978. While start And text[start-1]<=32 And text[start-1]<>10
  979. start-=1
  980. Wend
  981. While ends<text.Length And text[ends]<=32 And text[ends]<>10
  982. ends+=1
  983. Wend
  984. Else if IsIdent( text[start] )
  985. While start And IsIdent( text[start-1] )
  986. start-=1
  987. Wend
  988. While ends<text.Length And IsIdent( text[ends] )
  989. ends+=1
  990. Wend
  991. Else
  992. While start And text[start-1]>32 And Not IsIdent( text[start-1] )
  993. start-=1
  994. Wend
  995. While ends<text.Length And text[ends]>32 And Not IsIdent( text[ends] )
  996. ends+=1
  997. Wend
  998. Endif
  999. term[0]=ends
  1000. Return start
  1001. End
  1002. Method WordLength:Int( text:String,i0:Int,eol:Int )
  1003. Local i1:=i0
  1004. If text[i1]<=32
  1005. While i1<eol And text[i1]<=32
  1006. i1+=1
  1007. Wend
  1008. Else If IsIdent( text[i1] )
  1009. While i1<eol And IsIdent( text[i1] )
  1010. i1+=1
  1011. Wend
  1012. Else
  1013. While i1<eol And text[i1]>32 And Not IsIdent( text[i1] )
  1014. i1+=1
  1015. Wend
  1016. Endif
  1017. Return i1-i0
  1018. End
  1019. Method WordWidth:Int( text:String,i0:Int,eol:Int,x0:Int )
  1020. Local i1:=i0,x1:=x0
  1021. If text[i0]<=32
  1022. While i1<eol And text[i1]<=32
  1023. If text[i1]=9
  1024. x1=Int( (x1+_tabw)/_tabw ) * _tabw
  1025. Else
  1026. x1+=_charw
  1027. Endif
  1028. i1+=1
  1029. Wend
  1030. Else
  1031. If IsIdent( text[i1] )
  1032. While i1<eol And IsIdent( text[i1] )
  1033. i1+=1
  1034. Wend
  1035. Else
  1036. While i1<eol And text[i1]>32 And Not IsIdent( text[i1] )
  1037. i1+=1
  1038. Wend
  1039. Endif
  1040. x1+=_font.TextWidth( text.Slice( i0,i1 ) )
  1041. Endif
  1042. Return x1-x0
  1043. End
  1044. Method MeasureLine:Vec2i( line:Int )
  1045. Local text:=_doc.Text
  1046. Local i0:=_doc.StartOfLine( line )
  1047. Local eol:=_doc.EndOfLine( line )
  1048. Local x0:=0,y0:=_charh,maxw:=0
  1049. While i0<eol
  1050. Local w:=WordWidth( text,i0,eol,x0 )
  1051. If x0+w>_wrapw '-_charw
  1052. maxw=Max( maxw,x0 )
  1053. y0+=_charh
  1054. x0=0
  1055. Endif
  1056. x0+=w
  1057. i0+=WordLength( text,i0,eol )
  1058. Wend
  1059. maxw=Max( maxw,x0 )
  1060. Return New Vec2i( maxw,y0 )
  1061. End
  1062. Method MoveLine( delta:Int )
  1063. Local vcursor:=_vcursor
  1064. _vcursor.y+=delta * _charh
  1065. _cursor=CharAtPoint( _vcursor )
  1066. UpdateCursor()
  1067. _vcursor.x=vcursor.x
  1068. End
  1069. Method UpdateLines()
  1070. Local liney:=0,maxWidth:=0
  1071. For Local i:=0 Until _lines.Length
  1072. Local size:=MeasureLine( i )
  1073. maxWidth=Max( maxWidth,size.x )
  1074. _lines.Data[i].maxWidth=maxWidth
  1075. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1076. liney+=size.y
  1077. Next
  1078. UpdateCursor()
  1079. End
  1080. Method RenderLine( canvas:Canvas,line:Int )
  1081. Local text:=_doc.Text
  1082. Local colors:=_doc.Colors
  1083. Local i0:=_doc.StartOfLine( line )
  1084. Local eol:=_doc.EndOfLine( line )
  1085. Local x0:=0,y0:=_lines[line].rect.Top
  1086. While i0<eol
  1087. Local w:=WordWidth( text,i0,eol,x0 )
  1088. Local l:=WordLength( text,i0,eol )
  1089. If x0+w>_wrapw '-_charw
  1090. y0+=_charh
  1091. x0=0
  1092. Endif
  1093. If text[i0]<=32
  1094. x0+=w
  1095. i0+=l
  1096. Continue
  1097. Endif
  1098. Local i1:=i0+l
  1099. While i0<i1
  1100. Local start:=i0
  1101. Local color:=colors[start]
  1102. i0+=1
  1103. While i0<i1 And colors[i0]=color
  1104. i0+=1
  1105. Wend
  1106. If color<0 Or color>=_textColors.Length color=0
  1107. canvas.Color=_textColors[color]
  1108. Local str:=text.Slice( start,i0 )
  1109. canvas.DrawText( str,x0,y0 )
  1110. x0+=_font.TextWidth( str )
  1111. Wend
  1112. Wend
  1113. End
  1114. Method LinesModified( first:Int,removed:Int,inserted:Int )
  1115. ' Print "Lines modified: first="+first+", removed="+removed+", inserted="+inserted+", _charh="+_charh
  1116. ValidateStyle()
  1117. Local last:=first+inserted+1
  1118. Local dlines:=inserted-removed
  1119. If dlines>=0
  1120. _lines.Resize( _lines.Length+dlines )
  1121. Local i:=_lines.Length
  1122. While i>last
  1123. i-=1
  1124. _lines[i]=_lines[i-dlines]
  1125. Wend
  1126. Endif
  1127. Local liney:=0,maxWidth:=0
  1128. If first
  1129. liney=_lines[first-1].rect.Bottom
  1130. maxWidth=_lines[first-1].maxWidth
  1131. Endif
  1132. For Local i:=first Until last
  1133. Local size:=MeasureLine( i )
  1134. maxWidth=Max( maxWidth,size.x )
  1135. _lines.Data[i].maxWidth=maxWidth
  1136. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1137. liney+=size.y
  1138. Next
  1139. If dlines<0
  1140. Local i:=last
  1141. While i<_lines.Length+dlines
  1142. _lines[i]=_lines[i-dlines]
  1143. i+=1
  1144. Wend
  1145. _lines.Resize( _lines.Length+dlines )
  1146. Endif
  1147. For Local i:=last Until _lines.Length
  1148. Local size:=_lines[i].rect.Size
  1149. maxWidth=Max( maxWidth,size.x )
  1150. _lines.Data[i].maxWidth=maxWidth
  1151. _lines.Data[i].rect=New Recti( 0,liney,size.x,liney+size.y )
  1152. liney+=size.y
  1153. Next
  1154. If _cursor>_doc.TextLength
  1155. _cursor=_doc.TextLength
  1156. _anchor=_cursor
  1157. UpdateCursor()
  1158. RequestRender()
  1159. Else
  1160. _anchor=Min( _anchor,_doc.TextLength )
  1161. Endif
  1162. ' Print "Document width="+_lines.Top.maxWidth+", height="+_lines.Top.rect.Bottom
  1163. End
  1164. End