简体   繁体   中英

FLTK: How to enable scrollbar in deactivated Fl_Text_Editor

In some cases, we would like to make the text in an Fl_Text_Editor uneditable (for example, when the user loads a read-only file. Clearly, an Fl_Text_Display could also be used in these cases)

Currently, we achieve this by calling deactivate() on the Fl_Text_Editor widget and this properly disables the widget and it becomes grayed out. However, this also seems to disable the scrollbars and the user is not able to scroll down the widget to read the remainder of the text.

I tried to override Fl_Text_Editor's deactivate function and reactive its scrollbar afterwards, but this didn't work. Apparently a widget is active when all of its parents are also active..

Any thoughts on how I can still use the scrollbars of a deactivated Fl_Text_Editor would be greatly appreciated.

Since the main difference between Fl_Text_Editor and Fl_Text_Display is the handle function, the easiest thing to do is to create your own class which decides which handle to use. Try adding the following to your code

class TextEditor: public Fl_Text_Editor
{
   bool m_readonly;
   Fl_Color grey;
   Fl_Color normal;
public:
   TextEditor(int x, int y, int w, int h, const char* l = 0)
      :Fl_Text_Editor(x, y, w, h, l)
      , m_readonly(false)
   {
      grey = FL_BACKGROUND_COLOR;
      normal = FL_BACKGROUND2_COLOR;
   }

   int handle(int e)
   {
      int rv = 0;
      if (m_readonly)
         rv = Fl_Text_Display::handle(e);
      else
         rv = Fl_Text_Editor::handle(e);
      return rv;
   }

   void Readonly(bool in_set)
   {
      m_readonly = in_set;
      color(m_readonly? grey: normal);
   }
};

Use TextEditor instead of Fl_Text_Editor. If you wish to deactivate, just call Readonly(true);

Setting the colour is just an added extra to make the display look non-writable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM