| 1 | package Tk::WorkspaceText; | ||
| 2 | # Temp version for CPAN | ||
| 3 | $VERSION=0.57; | ||
| 4 | my $RCSRevKey = '$Revision: 0.58 $'; | ||
| 5 | $RCSRevKey =~ /Revision: (.*?) /; | ||
| 6 | $VERSION=$1; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | use Tk qw(Ev); | ||
| 10 | |||
| 11 | Construct Tk::Widget 'WorkspaceText'; | ||
| 12 | use base qw(Tk::TextUndo Tk::Toplevel); | ||
| 13 | # | ||
| 14 | # Bound in Tk::Text.pm | ||
| 15 | # | ||
| 16 | sub InsertKeypress { | ||
| 17 | my ($w,$char)=@_; | ||
| 18 | $w -> SUPER::InsertKeypress($char); | ||
| 19 | $w -> {modified} = '1' if (defined $char && $char ne ''); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub Insert { | ||
| 23 | my $w = shift; | ||
| 24 | my $string = shift; | ||
| 25 | $w -> SUPER::Insert($string); | ||
| 26 | $w -> {modified} = '1'; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub Delete { | ||
| 30 | my ($w) = @_; | ||
| 31 | $w -> SUPER::Delete; | ||
| 32 | $w -> {modified} = '1'; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub Backspace { | ||
| 36 | my ($w) = @_; | ||
| 37 | $w -> SUPER::Backspace; | ||
| 38 | $w -> {modified} = '1'; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub clipboardColumnCut { | ||
| 42 | my ($w) = @_; | ||
| 43 | $w-> Column_Copy_or_Cut(1); | ||
| 44 | $w -> {modified} = '1'; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub clipboardColumnPaste { | ||
| 48 | my ($w) = @_; | ||
| 49 | $w -> SUPER::clipboardColumnPaste; | ||
| 50 | $w -> {modified} = '1'; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub ClassInit { | ||
| 54 | my ($class,$mw) = @_; | ||
| 55 | $class->SUPER::ClassInit($mw); | ||
| 56 | $Tk::prevPos = undef; | ||
| 57 | $mw -> bind($class, '<Tab>', 'fixed_tabs'); | ||
| 58 | $mw -> bind($class, '<Alt-h>', 'selectPara'); | ||
| 59 | $mw -> bind($class, '<Alt-l>', 'paragraphFill'); | ||
| 60 | $mw -> bind($class, '<Control-v>', ['SetCursor',Ev('ScrollPages',1)]); | ||
| 61 | $mw -> bind($class, '<Control-r>', ['SetCursor',Ev('ScrollPages',-1)]); | ||
| 62 | $mw -> bind($class, '<Control-period>', 'center'); | ||
| 63 | return $class; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub InitObject { | ||
| 67 | my ($w, $args) = @_; | ||
| 68 | $w -> {modified} = ''; | ||
| 69 | $w -> {fixedtabs} = '8'; | ||
| 70 | $w -> {fillcolumn} = '65'; | ||
| 71 | $w -> bind ('<Tab>', sub{ $w -> fixed_tabs }); | ||
| 72 | return $w; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub Populate { | ||
| 76 | my ($w, $args) = @_; | ||
| 77 | $w -> SUPER::Populate($args); | ||
| 78 | return $w; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub paragraphFill { | ||
| 82 | my ($w) = @_; | ||
| 83 | my ($tmpline, $breakpos); | ||
| 84 | my ($insertrow,$insertcol) = split /\./, $w -> index ('insert'); | ||
| 85 | $previndex = $w -> prevPara ($w -> index ('insert')); | ||
| 86 | $nextindex = $w -> nextPara ($w -> index ('insert')); | ||
| 87 | my $t = $w -> get ("$previndex linestart", "$nextindex - 1 line"); | ||
| 88 | $t =~ s/\n/ /smg; | ||
| 89 | $w -> delete ("$previndex linestart", "$nextindex - 1 line"); | ||
| 90 | $w -> markSet ('insert', "$previndex linestart"); | ||
| 91 | while (length ($t) >= $w -> {fillcolumn}) { | ||
| 92 | $tmpline = substr ($t, 0, $w -> {fillcolumn}); | ||
| 93 | $breakpos = rindex ($tmpline, ' '); | ||
| 94 | $tmpline = substr ($t, 0, $breakpos); | ||
| 95 | # Remove the extra space unless it's an indented line | ||
| 96 | $tmpline =~ s/^ // unless ($tmpline =~ /^(\t|\s\s+)/) ; | ||
| 97 | $w -> insert('insert', "$tmpline\n"); | ||
| 98 | $t = substr ($t, $breakpos); | ||
| 99 | } | ||
| 100 | $t =~ s/^ //; | ||
| 101 | # Trim the extra space from the end of the last line. | ||
| 102 | chop $t; | ||
| 103 | $w -> insert('insert', "$t\n"); | ||
| 104 | $w -> markSet ('insert', "$insertrow.$insertcol"); | ||
| 105 | $w -> {modified} = '1'; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub selectPara { | ||
| 109 | my ($w) = @_; | ||
| 110 | my ($insertrow,$insertcol) = split /\./, $w -> index ('insert'); | ||
| 111 | $previndex = $w -> prevPara ($w -> index ('insert')); | ||
| 112 | $nextindex = $w -> nextPara ($w -> index ('insert')); | ||
| 113 | $w -> tagAdd ('sel', "$previndex linestart", "$nextindex linestart"); | ||
| 114 | } | ||
| 115 | |||
| 116 | # Determines if insertion cursor is on the first line of | ||
| 117 | # a paragraph before calling standard routine. | ||
| 118 | sub prevPara { | ||
| 119 | my ($w,$args) = @_; | ||
| 120 | my $i = $w -> index ('insert'); | ||
| 121 | my $t = $w -> get ( "$i - 1 line" ); | ||
| 122 | if ($t eq "\n") { | ||
| 123 | return $w -> index ('insert'); | ||
| 124 | } | ||
| 125 | return $w -> SUPER::PrevPara ($args); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub fillcolumn { | ||
| 129 | my $self = shift; | ||
| 130 | if (@_) { $self -> {fillcolumn} = shift } | ||
| 131 | return $self -> {fillcolumn} | ||
| 132 | } | ||
| 133 | |||
| 134 | sub setFixedTabs { | ||
| 135 | my ($w,$args) = @_; | ||
| 136 | $w -> {fixedtabs} = $args; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub fixed_tabs { | ||
| 140 | my ($w, $args) = @_; | ||
| 141 | my $tabsize = $w -> {fixedtabs}; | ||
| 142 | my ($row,$col) = split /\./, $w -> index ('insert'); | ||
| 143 | my $ntabs = int ($col/$tabsize); | ||
| 144 | my $nextab = ($ntabs + 1) * $tabsize; | ||
| 145 | $w -> insert( $w -> index ('insert'), | ||
| 146 | ' ' x ($nextab - $col)); | ||
| 147 | $w -> markSet( 'insert', "$row.$nextab"); | ||
| 148 | $w -> break; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub nextPara { | ||
| 152 | my ($w,$args) = @_; | ||
| 153 | return $w -> SUPER::NextPara ($args); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub center { | ||
| 157 | my ($w,$args) = @_; | ||
| 158 | my ($row,$col) = split /\./, $w->index('insert'); | ||
| 159 | my ($endrow,$endcol) = split /\./, $w->index('end'); | ||
| 160 | my $start = ($w->yview)[0]; | ||
| 161 | my $end = ($w->yview)[1]; | ||
| 162 | my $linefraction = 1/$endrow; | ||
| 163 | my $firstline = int $start/$linefraction; | ||
| 164 | my $lastline = int $end/$linefraction; | ||
| 165 | my $visible = $end - $start; | ||
| 166 | my $visiblelines = int $visible/$linefraction; | ||
| 167 | my $dotline = int $visiblelines/2; | ||
| 168 | my $cursorline = $row - $firstline; | ||
| 169 | $w -> yviewScroll(($cursorline - $dotline), 'units' ); | ||
| 170 | $w -> break; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub modified { | ||
| 174 | my $self = shift; | ||
| 175 | if (@_) { $self -> {modified} = shift } | ||
| 176 | return $self -> {modified} | ||
| 177 | } | ||
| 178 | |||
| 179 | 1; | ||
| 180 | |||
| 181 | =head1 NAME | ||
| 182 | |||
| 183 | WorkspaceText.pm - Text editor widget. | ||
| 184 | |||
| 185 | =head1 SYNOPSIS | ||
| 186 | |||
| 187 | use Tk::WorkspaceText; | ||
| 188 | |||
| 189 | my $w = new Tk::WorkspaceText (<options>); | ||
| 190 | |||
| 191 | =head1 DESCRIPTION | ||
| 192 | |||
| 193 | The Tk::WorkspaceText widget is derived from the Tk::Text | ||
| 194 | and Tk::TextUndo widgets. For information about standard | ||
| 195 | options, refer to the widgets' documentation. | ||
| 196 | |||
| 197 | Tk::WorkspaceText widgets use fixed tabs, and support paragraph | ||
| 198 | filling and selection, a "text modified" flag, recentering, and | ||
| 199 | additional key bindings. | ||
| 200 | |||
| 201 | Paragraphs are defined as contiguous lines of text separated | ||
| 202 | by blank lines - lines that consist only of a newline ("\n"). | ||
| 203 | |||
| 204 | =head1 WIDGET METHODS | ||
| 205 | |||
| 206 | The Tk::WorkspaceText widget supports the B<configure> | ||
| 207 | and B<cget> methods described in the L<Tk::options> | ||
| 208 | documentation. The Tk::WorkspaceText widget also inherits | ||
| 209 | all the methods of the Tk::Widget, Tk::Text, and | ||
| 210 | Tk::TextUndo widget classes. | ||
| 211 | |||
| 212 | In addition, Tk::WorkspaceText widgets recognize these | ||
| 213 | methods: | ||
| 214 | |||
| 215 | =over 4 | ||
| 216 | |||
| 217 | =item I<$text>-E<gt>B<paragraphFill> | ||
| 218 | |||
| 219 | Fill the paragraph that contains the insertion point. Paragraphs | ||
| 220 | are defined as a series of contiguous lines of text separated by | ||
| 221 | blank lines. Text is filled against the left margin, and wraps | ||
| 222 | before the right margin, which can be set with the B<wrapcolumn> | ||
| 223 | method, below. | ||
| 224 | |||
| 225 | =item I<$text>-E<gt>B<selectPara> | ||
| 226 | |||
| 227 | Select the entire paragraph. As with paragraphFill, paragraphs are | ||
| 228 | defined as a set of contiguous lines separated by blank lines. | ||
| 229 | |||
| 230 | =item I<$text>-E<gt>B<prevPara> | ||
| 231 | |||
| 232 | Set the insertion cursor to the first line of a paragraph. Does | ||
| 233 | nothing if the insertion cursor is already on the first line of | ||
| 234 | the paragraph. | ||
| 235 | |||
| 236 | =item I<$text>-E<gt>B<setFixedTabs> | ||
| 237 | |||
| 238 | Set fixed tab stops to the value of the numeric argument. Tab | ||
| 239 | stops occur every 8 columns by default. | ||
| 240 | |||
| 241 | =item I<$text>-E<gt>B<center> | ||
| 242 | |||
| 243 | Center the line that contains the insertion cursor in the window. | ||
| 244 | |||
| 245 | =item I<$text>-E<gt>B<modified> | ||
| 246 | |||
| 247 | With argument, sets the modified flag to the argument. With no | ||
| 248 | argument, returns the value of the modified flag. By default, the | ||
| 249 | value of the modified flag is '1' if text was modified, '' if | ||
| 250 | unmodified. | ||
| 251 | |||
| 252 | =item I<$text>-E<gt>B<fillcolumn> | ||
| 253 | |||
| 254 | With argument, set the right margin column for filling | ||
| 255 | paragraphs. Returns the value of the right margin. | ||
| 256 | The default is column 65. | ||
| 257 | |||
| 258 | =back | ||
| 259 | |||
| 260 | =head1 KEY BINDINGS | ||
| 261 | |||
| 262 | Tk::WorkspaceText widgets support the keybindings of the Tk::Text and | ||
| 263 | Tk::TextUndo widgets, in addition to its own bindings. For further | ||
| 264 | information, please refer to the Tk::Text, Tk::TextUndo, and | ||
| 265 | Tk::bind man pages. | ||
| 266 | |||
| 267 | Alt-H Select Paragraph | ||
| 268 | Alt-L Fill Paragraph | ||
| 269 | |||
| 270 | Right, Ctrl-F Forward Character | ||
| 271 | Left, Ctrl-B Backward Character | ||
| 272 | Up, Ctrl-P Up One Line | ||
| 273 | Down, Ctrl-N Down One Line | ||
| 274 | Shift-Right Forward Character Extend Selection | ||
| 275 | Shift-Left Backward Character Extend Selection | ||
| 276 | Shift-Up Up One Line, Extend Selection | ||
| 277 | Shift-Down Down One Line, Extend Selection | ||
| 278 | Ctrl-Right, Meta-F Forward Word | ||
| 279 | Ctrl-Left, Meta-B Backward Word | ||
| 280 | Ctrl-Up Up One Paragraph | ||
| 281 | Ctrl-Down Down One Paragraph | ||
| 282 | PgUp Scroll View Up One Screen | ||
| 283 | PgDn Scroll View Down One Screen | ||
| 284 | Ctrl-PgUp Scroll View Right | ||
| 285 | Ctrl-PgDn Scroll View Left | ||
| 286 | Home, Ctrl-A Beginning of Line | ||
| 287 | End, Ctrl-E End of Line | ||
| 288 | Ctrl-Home, Meta-< Beginning of Text | ||
| 289 | Ctrl-End, Meta-> End of Text | ||
| 290 | Ctrl-/ Select All | ||
| 291 | Ctrl-\ Clear Selection | ||
| 292 | F16, Copy, Meta-W Copy Selection to Clipboard | ||
| 293 | F20, Cut, Ctrl-W Copy Selection to Clipboard and Delete | ||
| 294 | F18, Paste, Ctrl-Y Paste Clipboard Text at Insertion Point | ||
| 295 | Delete, Ctrl-D Delete Character to Right, or Selection | ||
| 296 | Backspace, Ctrl-H Delete Character to Left, or Selection | ||
| 297 | Meta-D Delete Word to Right | ||
| 298 | Meta-Backspace, Meta-Delete | ||
| 299 | Delete Word to Left | ||
| 300 | Ctrl-K Delete from Cursor to End of Line | ||
| 301 | Ctrl-O Open a Blank Line | ||
| 302 | Ctrl-X Clear Selection | ||
| 303 | Ctrl-T Reverse Order of Characters on Either Side | ||
| 304 | of the Cursor | ||
| 305 | Ctrl-. Center the line the insertion point is on | ||
| 306 | in the window. | ||
| 307 | |||
| 308 | Mouse Button 1: | ||
| 309 | Single Click: Set Insertion Cursor at Mouse Pointer | ||
| 310 | Double Click: Select Word Under the Mouse Pointer and Position | ||
| 311 | Cursor at the Beginning of the Word | ||
| 312 | Triple Click: Select Line Under the Mouse Pointer and Position | ||
| 313 | Cursor at the Beginning of the Line | ||
| 314 | Drag: Define Selection from Insertion Cursor | ||
| 315 | Shift-Drag: Extend Selection | ||
| 316 | Double Click, Shift-Drag: Extend Selection by Whole Words | ||
| 317 | Triple Click, Shift-Drag: Extend Selection by Whole Lines | ||
| 318 | Ctrl: Position Insertion Cursor without Affecting Selection | ||
| 319 | |||
| 320 | Mouse Button 2: | ||
| 321 | Click: Copy Selection into Text at the Mouse Pointer | ||
| 322 | Drag:Shift View | ||
| 323 | |||
| 324 | Mouse Button 3: | ||
| 325 | Pop Up Menu Bar | ||
| 326 | |||
| 327 | Meta Escape | ||
| 328 | |||
| 329 | =head1 CREDITS | ||
| 330 | |||
| 331 | Tk::WorkspaceText by rkiesling@mainmatter.com (Robert Kiesling) | ||
| 332 | |||
| 333 | Perl/Tk by Nick Ing-Simmons. | ||
| 334 | Perl by Larry Wall and many others. | ||
| 335 | |||
| 336 | =head1 REVISION | ||
| 337 | |||
| 338 | $Id: WorkspaceText.pm,v 0.58 2001/09/15 02:22:54 kiesling Exp $ | ||
| 339 | |||
| 340 | =head1 SEE ALSO: | ||
| 341 | |||
| 342 | Tk::Workspace(3), Tk::overview(3), Tk::ColorEditor(3), perl(1) manual | ||
| 343 | pages. | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | |||
| 348 | |||
| 349 |