haskellgtk2hs

Line numbering from text using gtk2hs


I'm working on a program that contains a text editor on it and will be used for programming (which means that the font is monospaced and has the same size in the entire text). As such, it would be useful if it were possible to see the number of each line (paragraph, not display line) at the side of the TextView widget.

In another question (GTK+ line numbering for Text View), it was said that the SourceView widget does the job, but this widget doesn't seem to exist in gtk2hs, so I'm stuck with TextView.

Does the library offer a simple solution to this problem, or do I have to do it the hard way?


Solution

  • The minimalist code for using a SourceView with numbered lines with Haskell and Gtk2Hs is :

    import Graphics.UI.Gtk
    import Graphics.UI.Gtk.SourceView
    
    main :: IO ()
    main= do
      initGUI
      window <- windowNew
      set window [  windowTitle := "SourceView"
                  , windowDefaultWidth := 100
                  , windowDefaultHeight := 100
                  , windowResizable :=True ]
    
      sview <- sourceViewNew
      sourceViewSetShowLineNumbers sview True
    
      containerAdd window sview
      onDestroy window mainQuit
      widgetShowAll window 
      mainGUI