EditTextView.st
branchtext-view-selection-refactoring
changeset 4841 4fca96a61d42
parent 4674 935f7e07003b
child 4843 7cd827899263
equal deleted inserted replaced
4680:d9c981028573 4841:4fca96a61d42
  4557 
  4557 
  4558     (key == #CursorRight) ifTrue:[
  4558     (key == #CursorRight) ifTrue:[
  4559         (shifted and:[selectionStartLine isNil]) ifTrue:[
  4559         (shifted and:[selectionStartLine isNil]) ifTrue:[
  4560             selectionStartLine := selectionEndLine := clickStartLine := cursorLine.
  4560             selectionStartLine := selectionEndLine := clickStartLine := cursorLine.
  4561             selectionStartCol := selectionEndCol := clickStartCol := cursorCol.
  4561             selectionStartCol := selectionEndCol := clickStartCol := cursorCol.
       
  4562             cursorCol := cursorCol + 1.
  4562             expandingTop := false.
  4563             expandingTop := false.
  4563             self validateNewSelection.
  4564             self validateNewSelection.
  4564             self setPrimarySelection.
  4565             self setPrimarySelection.
  4565             self selectionChanged.
  4566             self selectionChanged.
  4566             self redrawLine:selectionStartLine.
  4567             self redrawLine:selectionStartLine.
  4567             ^ self.
  4568             ^ self.
  4568         ].
  4569         ].
  4569 
  4570 
  4570         selectionStartLine notNil ifTrue:[
  4571         self cursorMovementAllowed ifTrue:[
  4571             self cursorMovementAllowed ifTrue:[
  4572             shifted ifTrue:[
  4572                 "/
  4573                 self addToSelectionAfter:[ self cursorRight ]
  4573                 "/ treat the whole selection as cursor
  4574             ] ifFalse:[
  4574                 "/
       
  4575                 self setCursorLine:(selectionEndLine ? selectionStartLine).
       
  4576                 selectionEndCol == 0 ifTrue:[
       
  4577                     selectionEndCol := 1.
       
  4578                 ].
       
  4579                 self setCursorCol:selectionEndCol.
       
  4580                 shifted ifTrue:[
       
  4581                     self expandSelectionRight.
       
  4582                     ^ self
       
  4583                 ].
       
  4584                 self unselect; makeCursorVisible.
  4575                 self unselect; makeCursorVisible.
  4585                 cursorCol == 1 ifTrue:[^ self].
  4576                 self cursorRight.
  4586             ].
  4577             ].
  4587         ].
  4578         ].
  4588         self cursorRight.
       
  4589         ^ self
  4579         ^ self
  4590     ].
  4580     ].
  4591     (key == #CursorDown) ifTrue:[
  4581     (key == #CursorDown) ifTrue:[
  4592         (shifted and:[selectionStartLine isNil]) ifTrue:[
  4582         (shifted and:[selectionStartLine isNil]) ifTrue:[
  4593             selectionStartLine := clickStartLine := cursorLine. selectionEndLine := cursorLine + 1.
  4583             selectionStartLine := clickStartLine := cursorLine. selectionEndLine := cursorLine + 1.
  4594             selectionStartCol := clickStartCol := selectionEndCol := cursorCol.
  4584             selectionStartCol := clickStartCol := cursorCol.
       
  4585             selectionEndCol := cursorCol - 1.
  4595             selectionEndCol == 1 ifTrue:[
  4586             selectionEndCol == 1 ifTrue:[
  4596                 selectionEndCol := 0.
  4587                 selectionEndCol := 0.
  4597             ].
  4588             ].
       
  4589             self setCursorLine: cursorLine + 1.
  4598             self validateNewSelection.
  4590             self validateNewSelection.
  4599             self selectionChanged.
  4591             self selectionChanged.
  4600             self redrawLine:selectionStartLine.
  4592             self redrawLine:selectionStartLine.
  4601             expandingTop := false.
  4593             expandingTop := false.
  4602             self redrawLine:selectionEndLine.
  4594             self redrawLine:selectionEndLine.
  4606         selectionStartLine notNil ifTrue:[
  4598         selectionStartLine notNil ifTrue:[
  4607             self cursorMovementAllowed ifTrue:[
  4599             self cursorMovementAllowed ifTrue:[
  4608                 "/
  4600                 "/
  4609                 "/ treat the whole selection as cursor
  4601                 "/ treat the whole selection as cursor
  4610                 "/
  4602                 "/
  4611                 self setCursorLine:(selectionEndLine ? selectionStartLine).
  4603 "/                self setCursorLine:(selectionEndLine ? selectionStartLine).
  4612                 self setCursorCol:selectionStartCol.
  4604 "/                self setCursorCol:selectionStartCol.
  4613                 (cursorCol == 0 or:[selectionEndCol == 0]) ifTrue:[
  4605 "/                (cursorCol == 0 or:[selectionEndCol == 0]) ifTrue:[
  4614                     self setCursorCol:1.
  4606 "/                    self setCursorCol:1.
  4615                     self setCursorLine:(cursorLine - 1).
  4607 "/                    self setCursorLine:(cursorLine - 1).
  4616                 ].
  4608 "/                ].
  4617                 self makeCursorVisible.
  4609 "/                self makeCursorVisible.
  4618 
  4610 "/
  4619                 shifted ifTrue:[
  4611 "/                shifted ifTrue:[
  4620                     clickLine := cursorLine.
  4612 "/                    clickLine := cursorLine.
  4621                     clickCol := cursorCol.
  4613 "/                    clickCol := cursorCol.
  4622                     self expandSelectionDown.
  4614 "/                    self setCursorLine: cursorLine + 1.
  4623                     ^ self
  4615 "/                    self expandSelectionDown.
  4624                 ].
  4616 "/                    ^ self
  4625                 self unselect.
  4617 "/                ].
       
  4618 "/                self unselect.
  4626             ].
  4619             ].
  4627         ].
  4620         ].
  4628 
  4621         self cursorMovementAllowed ifTrue:[
  4629         n := 1 + (self sensor compressKeyPressEventsWithKey:#CursorDown).
  4622             n := 1 + (self sensor compressKeyPressEventsWithKey:#CursorDown).
  4630         self cursorDown:n.
  4623             shifted ifTrue:[
  4631         "/
  4624                 self addToSelectionAfter:[ self cursorDown:n ]
  4632         "/ flush keyboard to avoid runaway cursor
  4625             ] ifFalse:[
  4633         "/
  4626                 self unselect; makeCursorVisible.
  4634         self sensor flushKeyboardFor:self.
  4627                 self cursorDown:n.
       
  4628             ].  
       
  4629             "/
       
  4630             "/ flush keyboard to avoid runaway cursor
       
  4631             "/
       
  4632             self sensor flushKeyboardFor:self.
       
  4633         ].
  4635         ^ self
  4634         ^ self
  4636     ].
  4635     ].
  4637     (key == #CursorLeft or:[key == #CursorUp]) ifTrue:[
  4636     (key == #CursorLeft or:[key == #CursorUp]) ifTrue:[
  4638         (shifted and:[selectionStartLine isNil]) ifTrue:[
  4637         (shifted and:[selectionStartLine isNil]) ifTrue:[
  4639             expandingTop := true.
  4638             expandingTop := true.
  4640             key == #CursorLeft ifTrue:[
  4639             key == #CursorLeft ifTrue:[
  4641                 cursorCol > 1 ifTrue:[
  4640                 cursorCol > 1 ifTrue:[
  4642                     selectionStartLine := selectionEndLine := clickStartLine := cursorLine.
  4641                     selectionStartLine := selectionEndLine := clickStartLine := cursorLine.
  4643                     selectionEndCol := clickStartCol := cursorCol-1.
  4642                     selectionEndCol := clickStartCol := cursorCol-1.
  4644                     selectionStartCol := cursorCol-1.
  4643                     selectionStartCol := cursorCol-1.
       
  4644                     self setCursorCol: cursorCol-1.
  4645                     self validateNewSelection.
  4645                     self validateNewSelection.
  4646                     self selectionChanged.
  4646                     self selectionChanged.
  4647                     self redrawLine:selectionStartLine.
  4647                     self redrawLine:selectionStartLine.
  4648                     ^ self
  4648                     ^ self
  4649                 ]
  4649                 ]
  4650             ] ifFalse:[
  4650             ] ifFalse:[
  4651                 cursorLine > 1 ifTrue:[
  4651                 cursorLine > 1 ifTrue:[
  4652                     selectionEndLine := clickStartLine := cursorLine.
  4652                     selectionEndLine := clickStartLine := cursorLine.
  4653                     selectionEndCol := selectionStartCol := clickStartCol := cursorCol.
  4653                     selectionStartCol := clickStartCol := cursorCol.
       
  4654                     selectionEndCol := cursorCol - 1.
  4654                     selectionStartLine := cursorLine - 1.
  4655                     selectionStartLine := cursorLine - 1.
  4655                     selectionEndCol == 1 ifTrue:[
  4656                     selectionEndCol == 1 ifTrue:[
  4656                         selectionEndCol := 0.
  4657                         selectionEndCol := 0.
  4657                     ].
  4658                     ].
  4658                     self validateNewSelection.
  4659                     self validateNewSelection.
  4659                     self selectionChanged.
  4660                     self selectionChanged.
  4660                     self redrawFromLine:selectionStartLine to:cursorLine.
  4661                     self redrawFromLine:selectionStartLine to:cursorLine.
       
  4662                     self setCursorLine: cursorLine - 1.  
  4661                     ^ self
  4663                     ^ self
  4662                 ]
  4664                 ]
  4663             ]
  4665             ]
  4664         ].
  4666         ].
  4665 
  4667 
  4666         selectionStartLine notNil ifTrue:[
  4668         self hasSelection ifTrue:[
  4667             self cursorMovementAllowed ifTrue:[
  4669             self cursorMovementAllowed ifTrue:[
  4668                 "/
  4670                 "/
  4669                 "/ treat the whole selection as cursor
  4671                 "/ treat the whole selection as cursor
  4670                 "/
  4672                 "/
  4671                 self setCursorLine:selectionStartLine.
  4673 "/                self setCursorLine:selectionStartLine.
  4672                 self setCursorCol:selectionStartCol.
  4674 "/                self setCursorCol:selectionStartCol.
  4673                 (key == #CursorLeft) ifTrue:[
       
  4674                     self setCursorCol:(cursorCol+1).  "/ compensate for followup crsr-left
       
  4675                 ].
       
  4676                 self makeCursorVisible.
  4675                 self makeCursorVisible.
  4677 
  4676 
  4678                 shifted ifTrue:[
  4677 "/                shifted ifFalse:[
  4679                     (key == #CursorUp) ifTrue:[
  4678 "/                    self unselect.
  4680                         clickLine := cursorLine.
  4679 "/                ]
  4681                         self expandSelectionUp.
       
  4682                     ] ifFalse:[
       
  4683                         self expandSelectionLeft.
       
  4684                     ].
       
  4685                     ^ self
       
  4686                 ].
       
  4687                 self unselect.
       
  4688             ].
  4680             ].
  4689         ].
  4681         ].
  4690         (key == #CursorLeft) ifTrue:[
  4682         (key == #CursorLeft) ifTrue:[
  4691             self cursorLeft. ^self
  4683             shifted ifTrue:[
  4692         ].
  4684                 self addToSelectionAfter:[self cursorLeft].
  4693         (key == #CursorUp)        ifTrue:[
  4685             ] ifFalse:[
       
  4686                 self unselect; makeCursorVisible.
       
  4687                 self cursorLeft. 
       
  4688             ].
       
  4689             ^self
       
  4690         ].
       
  4691         (key == #CursorUp) ifTrue:[
  4694             n := 1 + (self sensor compressKeyPressEventsWithKey:#CursorUp).
  4692             n := 1 + (self sensor compressKeyPressEventsWithKey:#CursorUp).
  4695             self cursorUp:n.
  4693             shifted ifTrue:[
       
  4694                 self addToSelectionAfter: [ self cursorUp:n ]
       
  4695             ] ifFalse:[
       
  4696                 self unselect; makeCursorVisible.
       
  4697                 self cursorUp:n.
       
  4698             ].
  4696             "/
  4699             "/
  4697             "/ flush keyboard to avoid runaway cursor
  4700             "/ flush keyboard to avoid runaway cursor
  4698             "/
  4701             "/
  4699             self sensor flushKeyboardFor:self.
  4702             self sensor flushKeyboardFor:self.
  4700             ^ self
  4703             ^ self
  4701         ].
  4704         ].
  4702     ].
  4705     ].
  4703 
  4706 
  4704     "Modified: / 17-04-2012 / 21:01:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4707     "Modified: / 28-08-2013 / 01:08:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4705 !
  4708 !
  4706 
  4709 
  4707 doKeyPress:key x:x y:y
  4710 doKeyPress:key x:x y:y
  4708     "handle keyboard input"
  4711     "handle keyboard input"
  4709 
  4712 
  4882         shiftPressed ifTrue: [
  4885         shiftPressed ifTrue: [
  4883             "/ "Original St/X code - now use Ctrl modifier"
  4886             "/ "Original St/X code - now use Ctrl modifier"
  4884             "/ self unselect.
  4887             "/ self unselect.
  4885             "/ self cursorHome.
  4888             "/ self cursorHome.
  4886             "Jan's modification"
  4889             "Jan's modification"
  4887             "/ self addToSelectionAfter:[ self cursorToBeginOfLine ].
  4890             self addToSelectionAfter:[ self cursorToBeginOfLine ].
  4888             "/ Jan's modification modified by his own request ;-))
  4891             "/ Jan's modification modified by his own request ;-))
  4889             self selectFromBeginOfLine.
  4892             "/ JV: Well, does not work correctly. Better to fix addToSelectionAfter: 
       
  4893             "/ self selectFromBeginOfLine.
  4890         ] ifFalse: [
  4894         ] ifFalse: [
  4891             self unselect.
  4895             self unselect.
  4892             ctrlPressed ifTrue:[
  4896             ctrlPressed ifTrue:[
  4893                 self cursorHome.
  4897                 self cursorHome.
  4894             ] ifFalse:[
  4898             ] ifFalse:[
  4901         shiftPressed ifTrue:[
  4905         shiftPressed ifTrue:[
  4902             "/ "Original St/X code - now use Ctrl modifier"
  4906             "/ "Original St/X code - now use Ctrl modifier"
  4903             "/ self unselect.
  4907             "/ self unselect.
  4904             "/ self cursorToBottom
  4908             "/ self cursorToBottom
  4905             " Jan's modification"
  4909             " Jan's modification"
  4906             "/ self addToSelectionAfter:[ self cursorToEndOfLine ] .
  4910             self addToSelectionAfter:[ self cursorToEndOfLine ] .
  4907             "/ Jan's modification modified by his own request ;-))
  4911             "/ Jan's modification modified by his own request ;-))
  4908             self selectToEndOfLine.
  4912             "/ JV: Well, does not work correctly. Better to fix addToSelectionAfter:
       
  4913             "/  self selectToEndOfLine.
  4909         ] ifFalse:[
  4914         ] ifFalse:[
  4910             self unselect.
  4915             self unselect.
  4911             ctrlPressed ifTrue:[
  4916             ctrlPressed ifTrue:[
  4912                 self cursorToBottom
  4917                 self cursorToBottom
  4913             ] ifFalse:[
  4918             ] ifFalse:[
  5087 
  5092 
  5088     super keyPress:key x:x y:y
  5093     super keyPress:key x:x y:y
  5089 
  5094 
  5090     "Modified: / 06-02-1998 / 11:59:59 / stefan"
  5095     "Modified: / 06-02-1998 / 11:59:59 / stefan"
  5091     "Modified: / 14-07-2011 / 12:08:28 / cg"
  5096     "Modified: / 14-07-2011 / 12:08:28 / cg"
       
  5097     "Modified: / 26-08-2013 / 17:50:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  5092 !
  5098 !
  5093 
  5099 
  5094 executeKeyboardMacro:cmdMacro
  5100 executeKeyboardMacro:cmdMacro
  5095     Error handle:[:ex |
  5101     Error handle:[:ex |
  5096 	self warn:'Error in keyboard macro: ' , ex description.
  5102 	self warn:'Error in keyboard macro: ' , ex description.
  7311 ! !
  7317 ! !
  7312 
  7318 
  7313 !EditTextView methodsFor:'selections'!
  7319 !EditTextView methodsFor:'selections'!
  7314 
  7320 
  7315 addToSelectionAfter:aBlock
  7321 addToSelectionAfter:aBlock
       
  7322     "Extends the selection accroding to cursor position
       
  7323      after evaluation of `aBlock`. Used to implement
       
  7324      Shift-Home & Shift-End. "
       
  7325 
       
  7326     "/ Following comment is in czech and thus now very useful.
       
  7327     "/ However I (JV) keep it here as this is one of the very
       
  7328     "/ first fix/hack we made to Smalltalk/X ages ago, if not
       
  7329     "/ the very first one. Actually, this method and comment was 
       
  7330     "/ written by Martin Dvorak. Yeah, being bit nostalgic...
       
  7331 
  7316     "Pokud existuje selekce, upravi ji
  7332     "Pokud existuje selekce, upravi ji
  7317      podle aktualni pozice kurzoru a pozice
  7333      podle aktualni pozice kurzoru a pozice
  7318      po provedeni blocku.
  7334      po provedeni blocku.
  7319      Urceno k implementaci Shift-Home a Shift-End
  7335      Urceno k implementaci Shift-Home a Shift-End
  7320      Nejak nevim, jak to presneji popsat :-)"
  7336      Nejak nevim, jak to presneji popsat :-)"
  7321 
  7337 
  7322     |startLine startCol endLine endCol |
  7338     | startLine startCol endLine endCol curLine curCol cursorAtTheBeginning cursorAtTheEnd cursorWasShown |
       
  7339 
       
  7340     curLine := cursorLine.
       
  7341     curCol := cursorCol.
  7323 
  7342 
  7324     self hasSelection ifTrue: [
  7343     self hasSelection ifTrue: [
  7325 	startLine := selectionStartLine .
  7344         startLine := selectionStartLine .
  7326 	startCol := selectionStartCol .
  7345         startCol := selectionStartCol .
  7327 	endLine := selectionEndLine .
  7346         endLine := selectionEndLine .
  7328 	endCol := selectionEndCol .
  7347         endCol := selectionEndCol .
  7329     ] ifFalse: [
  7348     ] ifFalse:[
  7330 	startLine := endLine :=  cursorLine .
  7349         startLine := endLine := curLine.
  7331 	startCol := endCol := cursorCol .
  7350         startCol := curCol.
  7332     ].
  7351         endCol := curCol - 1.
  7333 
  7352     ].
  7334     "deselectim a provedu presun kurzoru..."
  7353 
  7335     self unselect .
  7354 
  7336     aBlock value .
  7355     cursorAtTheBeginning := (startLine == curLine) and:[startCol == curCol].
  7337 
  7356     cursorAtTheEnd := (endLine == curLine) and:[endCol == (curCol - 1)]. 
  7338     "funguje dost mizerne, jen na jednom radku..."
  7357 
  7339     (startCol - cursorCol) abs <= (endCol - cursorCol) abs
  7358     self assert: cursorAtTheBeginning | cursorAtTheEnd.
  7340 	ifTrue: [
  7359 
  7341 	    startCol := cursorCol.
  7360 
  7342 	] ifFalse: [
  7361     cursorWasShown := self hideCursor.
  7343 	    endCol := cursorCol - 1.
  7362     aBlock value.
  7344 	].
  7363 
       
  7364     cursorAtTheBeginning ifTrue:[
       
  7365         (cursorCol == (endCol + 1) and:[cursorLine == endLine]) ifTrue:[
       
  7366                 self unselect; showCursor.
       
  7367                 ^self
       
  7368         ].
       
  7369 
       
  7370         (cursorLine > endLine or:[cursorLine == endLine and:[cursorCol > endCol]]) ifTrue:[
       
  7371             startLine := endLine.
       
  7372             startCol := endCol + 1.
       
  7373             endLine := cursorLine.
       
  7374             endCol := cursorCol - 1
       
  7375         ] ifFalse:[
       
  7376             startLine := cursorLine.
       
  7377             startCol := cursorCol.
       
  7378         ]
       
  7379     ] ifFalse:[
       
  7380         cursorAtTheEnd ifTrue:[
       
  7381             (cursorCol == startCol and:[cursorLine == startLine]) ifTrue:[
       
  7382                 self unselect; showCursor.
       
  7383                 ^self
       
  7384             ].
       
  7385             (cursorLine < startLine or:[cursorLine == startLine and:[cursorCol < startCol]]) ifTrue:[
       
  7386                 endLine := startLine.
       
  7387                 endCol := startCol - 1.
       
  7388                 startLine := cursorLine.
       
  7389                 startCol := cursorCol
       
  7390             ] ifFalse:[
       
  7391                 endLine := cursorLine.
       
  7392                 endCol := cursorCol - 1.
       
  7393             ]
       
  7394         ].
       
  7395     ].
  7345     self selectFromLine:startLine col:startCol toLine: endLine col:endCol .
  7396     self selectFromLine:startLine col:startCol toLine: endLine col:endCol .
       
  7397     cursorWasShown ifTrue:[self showCursor].
       
  7398 
       
  7399     "Modified: / 28-08-2013 / 01:23:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7346 !
  7400 !
  7347 
  7401 
  7348 autoMoveCursorToEndOfSelection
  7402 autoMoveCursorToEndOfSelection
  7349     "return true, if the cursor should be automatically moved to the
  7403     "return true, if the cursor should be automatically moved to the
  7350      end of a selection.
  7404      end of a selection.
  7494 	typeOfSelection := nil
  7548 	typeOfSelection := nil
  7495     ]
  7549     ]
  7496 !
  7550 !
  7497 
  7551 
  7498 selectFromLine:startLine col:startCol toLine:endLine col:endCol
  7552 selectFromLine:startLine col:startCol toLine:endLine col:endCol
  7499     "when a range is selected, position the cursor behind the selection
       
  7500      for easier editing. Also typeOfSelection is nilled here."
       
  7501 
  7553 
  7502     super selectFromLine:startLine col:startCol toLine:endLine col:endCol.
  7554     super selectFromLine:startLine col:startCol toLine:endLine col:endCol.
  7503     (selectionEndLine notNil and:[self autoMoveCursorToEndOfSelection]) ifTrue:[
  7555     "JV: That's atctuall rubbish as then you loose the original positon
  7504 	self cursorLine:selectionEndLine col:(selectionEndCol + 1).
  7556      and if user wants to extend/shring the selection further, you don't know
  7505     ].
  7557      from which end to do it!!"
       
  7558 "/    (selectionEndLine notNil and:[self autoMoveCursorToEndOfSelection]) ifTrue:[
       
  7559 "/        self cursorLine:selectionEndLine col:(selectionEndCol + 1).
       
  7560 "/    ].
  7506     typeOfSelection := nil
  7561     typeOfSelection := nil
       
  7562 
       
  7563     "Modified: / 27-08-2013 / 13:28:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  7564     "Modified (comment): / 27-08-2013 / 15:58:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7507 !
  7565 !
  7508 
  7566 
  7509 selectToEndOfLine
  7567 selectToEndOfLine
  7510     "select the text from the current cursor position to the end of the current line"
  7568     "select the text from the current cursor position to the end of the current line"
  7511 
  7569