EditTextView.st
changeset 474 f73ed1f5cb75
parent 473 e810b5bd6bad
child 477 e7c29649e8ae
equal deleted inserted replaced
473:e810b5bd6bad 474:f73ed1f5cb75
   315     "set the action to be performed on accept"
   315     "set the action to be performed on accept"
   316 
   316 
   317     acceptAction := aBlock
   317     acceptAction := aBlock
   318 !
   318 !
   319 
   319 
       
   320 autoIndent:aBoolean
       
   321     autoIndent := aBoolean
       
   322 
       
   323     "Created: 5.3.1996 / 14:37:50 / cg"
       
   324 !
       
   325 
   320 exceptionBlock:aBlock
   326 exceptionBlock:aBlock
   321     "define the action to be triggered when user tries to modify
   327     "define the action to be triggered when user tries to modify
   322      readonly text"
   328      readonly text"
   323 
   329 
   324     exceptionBlock := aBlock
   330     exceptionBlock := aBlock
   357      This is the absolute col; NOT the visible col"
   363      This is the absolute col; NOT the visible col"
   358 
   364 
   359     ^ cursorCol
   365     ^ cursorCol
   360 !
   366 !
   361 
   367 
   362 cursorForegroundColor:color1 backgroundColor:color2
       
   363     "set both cursor foreground and cursor background colors"
       
   364 
       
   365     |wasOn|
       
   366 
       
   367     wasOn := self hideCursor.
       
   368     cursorFgColor := color1 on:device.
       
   369     cursorBgColor := color2 on:device.
       
   370     wasOn ifTrue:[self showCursor]
       
   371 !
       
   372 
       
   373 cursorLine
   368 cursorLine
   374     "return the cursors line (1..). 
   369     "return the cursors line (1..). 
   375      This is the absolute line; NOT the visible line"
   370      This is the absolute line; NOT the visible line"
   376 
   371 
   377     ^ cursorLine
   372     ^ cursorLine
   422 
   417 
   423 readOnly
   418 readOnly
   424     "make the text readonly"
   419     "make the text readonly"
   425 
   420 
   426     readOnly := true
   421     readOnly := true
       
   422 ! !
       
   423 
       
   424 !EditTextView methodsFor:'accessing-look'!
       
   425 
       
   426 cursorForegroundColor:color1 backgroundColor:color2
       
   427     "set both cursor foreground and cursor background colors"
       
   428 
       
   429     |wasOn|
       
   430 
       
   431     wasOn := self hideCursor.
       
   432     cursorFgColor := color1 on:device.
       
   433     cursorBgColor := color2 on:device.
       
   434     wasOn ifTrue:[self showCursor]
   427 ! !
   435 ! !
   428 
   436 
   429 !EditTextView methodsFor:'change & update '!
   437 !EditTextView methodsFor:'change & update '!
   430 
   438 
   431 accept
   439 accept
  2024                           #BeginOfLine #EndOfLine #NextWord
  2032                           #BeginOfLine #EndOfLine #NextWord
  2025                           #GotoLine #Delete #BeginOfText #EndOfText
  2033                           #GotoLine #Delete #BeginOfText #EndOfText
  2026                           #SelectLine #ExpandSelectionByLine #DeleteLine
  2034                           #SelectLine #ExpandSelectionByLine #DeleteLine
  2027                           #InsertLine)>
  2035                           #InsertLine)>
  2028 
  2036 
  2029     |sensor n fKeyMacros shifted|
  2037     |sensor n fKeyMacros shifted i|
  2030 
  2038 
  2031     sensor := self sensor.
  2039     sensor := self sensor.
  2032     shifted := sensor shiftDown.
  2040     shifted := sensor shiftDown.
  2033 
  2041 
  2034     (key isMemberOf:Character) ifTrue:[
  2042     (key isMemberOf:Character) ifTrue:[
  2245                 self unselect; makeCursorVisible.
  2253                 self unselect; makeCursorVisible.
  2246             ] ifFalse:[
  2254             ] ifFalse:[
  2247                 self copyAndDeleteSelection.            
  2255                 self copyAndDeleteSelection.            
  2248             ].
  2256             ].
  2249             self insertCharAtCursor:(Character cr). 
  2257             self insertCharAtCursor:(Character cr). 
       
  2258             autoIndent == true ifTrue:[
       
  2259                 i := self leftIndentForLine:cursorLine.
       
  2260                 self indentFromLine:cursorLine toLine:cursorLine.
       
  2261                 self cursorCol:(i max:1)
       
  2262             ].
  2250         ].
  2263         ].
  2251         ^self
  2264         ^self
  2252     ].
  2265     ].
  2253 
  2266 
  2254     (key == #Tab) ifTrue:[
  2267     (key == #Tab) ifTrue:[
  2337         self makeCursorVisible.
  2350         self makeCursorVisible.
  2338         self unselect. self insertLine:nil before:cursorLine. ^self
  2351         self unselect. self insertLine:nil before:cursorLine. ^self
  2339     ].
  2352     ].
  2340     super keyPress:key x:x y:y
  2353     super keyPress:key x:x y:y
  2341 
  2354 
  2342     "Modified: 2.3.1996 / 00:04:24 / cg"
  2355     "Modified: 5.3.1996 / 15:00:25 / cg"
  2343 !
  2356 !
  2344 
  2357 
  2345 mapped
  2358 mapped
  2346     "view was made visible"
  2359     "view was made visible"
  2347 
  2360 
  2410 indentFromLine:start toLine:end
  2423 indentFromLine:start toLine:end
  2411     "indent a line-range - this is don by searching for the 
  2424     "indent a line-range - this is don by searching for the 
  2412      last non-empty line before start, and change the indent
  2425      last non-empty line before start, and change the indent
  2413      of the line based on that indent."
  2426      of the line based on that indent."
  2414 
  2427 
  2415     |leftStart lnr delta d line spaces|
  2428     |leftStart delta d line spaces|
  2416 
  2429 
  2417     "find a line to base indent on..."
  2430     leftStart := self leftIndentForLine:start.
  2418     leftStart := 0.
       
  2419     lnr := start.
       
  2420     [(leftStart == 0) and:[lnr ~~ 1]] whileTrue:[
       
  2421 	lnr := lnr - 1.
       
  2422 	leftStart := self leftIndentOfLine:lnr
       
  2423     ].
       
  2424 
       
  2425     (leftStart == 0) ifTrue:[^ self].
  2431     (leftStart == 0) ifTrue:[^ self].
  2426 
  2432 
  2427     delta := leftStart - (self leftIndentOfLine:start).
  2433     delta := leftStart - (self leftIndentOfLine:start).
  2428     (delta == 0) ifTrue:[^ self].
  2434     (delta == 0) ifTrue:[^ self].
  2429     (delta > 0) ifTrue:[
  2435     (delta > 0) ifTrue:[
  2430 	spaces := String new:delta
  2436         spaces := String new:delta
  2431     ].
  2437     ].
  2432     start to:end do:[:lineNr |
  2438     start to:end do:[:lineNr |
  2433 	line := self listAt:lineNr.
  2439         line := self listAt:lineNr.
  2434 	line notNil ifTrue:[
  2440         line notNil ifTrue:[
  2435 	    line isBlank ifTrue:[
  2441             line isBlank ifTrue:[
  2436 		list at:lineNr put:nil
  2442                 list at:lineNr put:nil
  2437 	    ] ifFalse:[
  2443             ] ifFalse:[
  2438 		(delta > 0) ifTrue:[
  2444                 (delta > 0) ifTrue:[
  2439 		    line := spaces , line.
  2445                     line := spaces , line.
  2440 		    widthOfWidestLine notNil ifTrue:[
  2446                     widthOfWidestLine notNil ifTrue:[
  2441 			widthOfWidestLine := widthOfWidestLine max:(self widthOfLineString:line).
  2447                         widthOfWidestLine := widthOfWidestLine max:(self widthOfLineString:line).
  2442 		    ]
  2448                     ]
  2443 		] ifFalse:[
  2449                 ] ifFalse:[
  2444 		    "check if deletion is ok"
  2450                     "check if deletion is ok"
  2445 		    d := delta negated + 1.
  2451                     d := delta negated + 1.
  2446 
  2452 
  2447 		    line size > d ifTrue:[
  2453                     line size > d ifTrue:[
  2448 			(line copyTo:(d - 1)) withoutSeparators isEmpty ifTrue:[
  2454                         (line copyTo:(d - 1)) withoutSeparators isEmpty ifTrue:[
  2449 			    line := line copyFrom:d
  2455                             line := line copyFrom:d
  2450 			]
  2456                         ]
  2451 		    ].
  2457                     ].
  2452 		    widthOfWidestLine := nil
  2458                     widthOfWidestLine := nil
  2453 		].
  2459                 ].
  2454 		list at:lineNr put:line.
  2460                 list at:lineNr put:line.
  2455 		self textChanged.
  2461                 self textChanged.
  2456 	    ]
  2462             ]
  2457 	]
  2463         ]
  2458     ].
  2464     ].
  2459     self redrawFromLine:start to:end
  2465     self redrawFromLine:start to:end
       
  2466 
       
  2467     "Modified: 5.3.1996 / 14:59:18 / cg"
       
  2468 !
       
  2469 
       
  2470 leftIndentForLine:lineNr
       
  2471     "find an appropriate indent for a line.
       
  2472      this is done by searching for the last non-empty line before it
       
  2473      and returning its indent."
       
  2474 
       
  2475     |leftStart lnr|
       
  2476 
       
  2477     leftStart := 0.
       
  2478     lnr := lineNr.
       
  2479     [(leftStart == 0) and:[lnr ~~ 1]] whileTrue:[
       
  2480         lnr := lnr - 1.
       
  2481         leftStart := self leftIndentOfLine:lnr
       
  2482     ].
       
  2483     ^ leftStart
       
  2484 
       
  2485     "Created: 5.3.1996 / 14:58:53 / cg"
  2460 ! !
  2486 ! !
  2461 
  2487 
  2462 !EditTextView methodsFor:'initialization'!
  2488 !EditTextView methodsFor:'initialization'!
  2463 
  2489 
  2464 initEvents
  2490 initEvents
  2497     cursorCol := 1.
  2523     cursorCol := 1.
  2498     modifiedChannel := false asValue.
  2524     modifiedChannel := false asValue.
  2499     showMatchingParenthesis := false.
  2525     showMatchingParenthesis := false.
  2500     hasKeyboardFocus := false. "/ true.
  2526     hasKeyboardFocus := false. "/ true.
  2501     tabMeansNextField := false.
  2527     tabMeansNextField := false.
       
  2528     autoIndent := false.
       
  2529 
       
  2530     "Modified: 5.3.1996 / 14:36:48 / cg"
  2502 !
  2531 !
  2503 
  2532 
  2504 realize
  2533 realize
  2505     "make the view visible"
  2534     "make the view visible"
  2506 
  2535 
  2625                     'goto ...'
  2654                     'goto ...'
  2626                     '-'
  2655                     '-'
  2627                     'font ...'
  2656                     'font ...'
  2628                     '-'
  2657                     '-'
  2629                     'indent'
  2658                     'indent'
       
  2659                     'autoIndent \c'
  2630                     '-'
  2660                     '-'
  2631                     'save as ...'
  2661                     'save as ...'
  2632                     'print'
  2662                     'print'
  2633                 ).
  2663                 ).
  2634 
  2664 
  2637                      gotoLine
  2667                      gotoLine
  2638                      nil
  2668                      nil
  2639                      changeFont
  2669                      changeFont
  2640                      nil
  2670                      nil
  2641                      indent
  2671                      indent
       
  2672                      autoIndent:
  2642                      nil
  2673                      nil
  2643                      save
  2674                      save
  2644                      print
  2675                      print
  2645                     ).
  2676                     ).
  2646 
  2677 
  2660                 selectors:selectors
  2691                 selectors:selectors
  2661                 accelerators:shortKeys
  2692                 accelerators:shortKeys
  2662                 receiver:model.
  2693                 receiver:model.
  2663 
  2694 
  2664     m subMenuAt:#others put:sub.
  2695     m subMenuAt:#others put:sub.
       
  2696     sub checkToggleAt:#autoIndent: put:autoIndent.
  2665 
  2697 
  2666     readOnly ifTrue:[
  2698     readOnly ifTrue:[
  2667         m disable:#paste
  2699         m disable:#paste
  2668     ].
  2700     ].
  2669     self hasSelection not ifTrue:[
  2701     self hasSelection not ifTrue:[
  2673         m disable:#cut.
  2705         m disable:#cut.
  2674         sub disable:#indent.
  2706         sub disable:#indent.
  2675     ].
  2707     ].
  2676     ^ m.
  2708     ^ m.
  2677 
  2709 
  2678     "Modified: 28.2.1996 / 19:00:12 / cg"
  2710     "Modified: 5.3.1996 / 14:43:41 / cg"
  2679 !
  2711 !
  2680 
  2712 
  2681 paste
  2713 paste
  2682     "paste copybuffer; if there is a selection, unselect first.
  2714     "paste copybuffer; if there is a selection, unselect first.
  2683      Then paste at cursor position."
  2715      Then paste at cursor position."
  3278 ! !
  3310 ! !
  3279 
  3311 
  3280 !EditTextView class methodsFor:'documentation'!
  3312 !EditTextView class methodsFor:'documentation'!
  3281 
  3313 
  3282 version
  3314 version
  3283     ^ '$Header: /cvs/stx/stx/libwidg/EditTextView.st,v 1.53 1996-03-05 15:01:00 cg Exp $'
  3315     ^ '$Header: /cvs/stx/stx/libwidg/EditTextView.st,v 1.54 1996-03-05 15:54:12 cg Exp $'
  3284 ! !
  3316 ! !