5
$\begingroup$

Edits are marked in bold:

How do I make an InputField that behaves just like the following HTML Textarea example.

enter image description here

Specific Features that are ideal:

  1. Text box, unless manually resized, has fixed height with scrollbar (The scrollbar must shrink to content size)
  2. Ctrl+A selects all content
  3. Enter key works correctly e.g:

    The InputField auto scrolls to bottom as user inserts new line

    When a new line is entered somewhere next to an existing new line inside the string, the coursor should follow correctly (now it leaves an empty line unless we use LeftArrow)

  4. The text wrapping works appropriately

I looked through the docs unable to find an example that follows those specifications.

This question is quiet close, but I can't seem to keep the content autoscrolled when the user enters a new line and the text wrapping isn't working correctly.

Also related: How to anchor a Pane's scroll position to the bottom?

$\endgroup$
  • $\begingroup$ concerning the Enter key see : mathematica.stackexchange.com/q/6457/5467 $\endgroup$ – andre314 Jun 15 '13 at 8:08
  • $\begingroup$ On my system (OS X), I can start a new line in your input field by entering Ctrl-J. $\endgroup$ – m_goldberg Jun 15 '13 at 8:23
  • $\begingroup$ @LeonidShifrin The linked question works quiet wells except it doesn't have scrollbar support that I am aware of. Are you aware of way to possible fix this? $\endgroup$ – William Jun 15 '13 at 16:49
  • $\begingroup$ @LeonidShifrin I have modified it to support the scrollbar, but I have amended the specifications slightly. Ideally the scrollbar should autoscroll to the bottom when a user enters a new line. $\endgroup$ – William Jun 15 '13 at 17:10
  • 1
    $\begingroup$ @Liam I have been thinking about it, for my own purposes, but did not implement yet. Similar scrolling issues have been discussed before here and in the question of which that one is a duplicate. $\endgroup$ – Leonid Shifrin Jun 15 '13 at 19:03
7
$\begingroup$

Perhaps this slightly modified version of your code is closer to what you want. Remember, Ctrl+J, not Enter, to start a new line.

DynamicModule[{data = "some\ndata"}, 
  Column[{
    Framed[
      Pane[InputField[Dynamic@data, String, 
        ContinuousAction -> True,
        Appearance -> None, 
        FieldSize -> {Automatic, {1, Infinity}}], 
        ImageSize -> {300, 100}, 
        ImageMargins -> 0, 
        AppearanceElements -> {},
        FrameMargins -> {{5, 5}, {0, 0}},
        Scrollbars -> {False, True}],
        Background -> White,
        FrameMargins -> 0,
        FrameStyle -> Gray],
   Dynamic@data}]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Crtl+J does not work for me (Win7), but it showed me how to add tags to current cell what I find useful. :) $\endgroup$ – Kuba Jun 15 '13 at 10:07
  • $\begingroup$ @Kuba. Perhaps someone using Window can tell us what keystroke will work there. $\endgroup$ – m_goldberg Jun 15 '13 at 10:48
  • $\begingroup$ I have noticed that if you type in text until the end of the line, that it wraps quiet funny. The text doesn't doesn't appear to fill up the first line before going onto the next. Additionally I noticed if I start pressing the space bar the text wraps a lot sooner then where the scrollbar is. After some trail and error it seems FieldSize -> {30, {1, Infinity}}],FrameMargins -> {{0, 0}, {0, 0}} seems to fix the 2nd issue, but I'm not so sure about the text wrapping issue. $\endgroup$ – William Jun 15 '13 at 16:28
  • $\begingroup$ @Kuba I have edited the code to use EventHandler to get around the Enter key issue. pastebin.com/n6eDPzaX I am not going to make this answer correct yet to see if someone comes up with a better answer(well shorter), more importantly the text is wrapping is wrong making the editor quiet difficult is use. $\endgroup$ – William Jun 15 '13 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.