简体   繁体   中英

How to set a size of Fl_Text_Editor?

At this moment, I have a target to write a simple text editor using C++. Due to the fact that I need my textbox to be inside of my window, so when I change my window size, it has to change it size too. Here's my code:

int main(int argc, char **argv) {
  { window_main = new Fl_Double_Window(624, 644, "Text editor");
    window_main->labelfont(1);
    window_main->align(Fl_Align(FL_ALIGN_CLIP|FL_ALIGN_INSIDE));
    { File = new Fl_Menu_Button(0, 0, 69, 20, "File");
      File->labelfont(13);
      File->menu(menu_File);
    } // Fl_Menu_Button* File
    { File_text = new Fl_Text_Editor(5, 25, 0, 615);
      Fl_Group::current()->resizable(File_text);
    } // Fl_Text_Editor* File_text
    window_main->size_range(0, 0, 1280, 660);
    window_main->end();
  } // Fl_Double_Window* window_main
  File_text->resize(5, 25, Fl::w - 5, Fl::h - 25);
  window_main->show(argc, argv);
  return Fl::run();
}

But I didn't find the function in this class for resising Fl_Text_Editor class.

Could you help me, please?

The call to size_range() makes a window resizable as a side-effect (it only tells FLTK what the size range of the window is), you would still need to call the resizable() method.

I've modified your example to get a working piece of code:

#include <FL/Fl.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Menu_Item.H>
#include <FL/Fl_Text_Editor.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_File_Chooser.H>

Fl_Text_Buffer *buf;

void cb(Fl_Widget *w, void *data) {
  Fl_File_Chooser *chooser = new Fl_File_Chooser(
      ".", "*.{txt,cpp,c}", Fl_File_Chooser::SINGLE, "MyDialog");
  chooser->show();
  while (chooser->shown())
    Fl::wait();
  buf->loadfile(chooser->value());
}

int main(int argc, char **argv) {
  buf = new Fl_Text_Buffer;
  Fl_Menu_Item menu_File[] = { {"New", 0, cb, nullptr}, nullptr }; 

  Fl_Double_Window *window_main = new Fl_Double_Window(624, 644, "Text editor");
  window_main->labelfont(1);
  window_main->align(Fl_Align(FL_ALIGN_CLIP|FL_ALIGN_INSIDE));
  
  Fl_Menu_Button *File = new Fl_Menu_Button(0, 0, 69, 20, "File");
  File->labelfont(13);
  File->menu(menu_File);
  
  Fl_Text_Editor *File_text = new Fl_Text_Editor(5, 25, window_main->w() - 10, 615);
  File_text->buffer(buf);
  
  window_main->size_range(0, 0, 1280, 660);
  window_main->resizable(File_text); // <-- here
  window_main->end();
  window_main->show(argc, argv);
  
  return Fl::run();
}

Additionally I would suggest to the OP to set minimum values of size_range() rather than the maximum. Typically the window contents requires a minimum size but the user should be free to enlarge the window as much as they like and the screen size allows. Hence I would change the size_range() statement to:

  window_main->size_range(400, 300); // set minimum window size

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