【问题标题】:How to automatically put newline at text's end in FLTK Multiline_Input?如何自动把换行符在FLTK Multiline_Input文本的结束?
【发布时间】:2020-11-02 08:13:40
【问题描述】:

我正在使用 FLTK 1.3.5,我想使用 Multiline_Input 对象。我想知道是否有一种方法可以在插入的文本到达输入字段的末尾时自动换行,而不是手动执行(例如,检查上传的图像)。此外,换行符应该放在单词的末尾。我在 SO 和网络上搜索了这里,但我找不到任何有用的东西。

这是用于生成上述图像的代码。

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Multiline_Input.H>


int main(int argc, char **argv) {

    Fl_Window *G_win = 0;
    G_win = new Fl_Window(200,200,"Test multi input");

    Fl_Multiline_Input* in;
    in = new Fl_Multiline_Input(50,50,120,100," Test:");

    G_win->end();
    G_win->show(argc, argv);
    
    return Fl::run();
}

【问题讨论】:

    标签: c++ input string-formatting fltk


    【解决方案1】:

    根据 Basile 的建议,我对事件处理进行了更多调查,然后想出了一个解决方案。

    我从原始Fl_Multiline_Input 派生了一个类,它基本上控制Fl_Multiline_input-&gt;value() 中的char[] 不超过几何体给出的最大宽度(考虑到当前的Fl_font)。也许这不是最好的解决方案,但是,嘿,它有效!当然,更高效/优雅的策略更受欢迎。

    代码在下面。

    #include "mymulti.hpp"
    
    /*
       This is needed for converting a std::string 
       to a char[] (required by Fl_Input). Again, 
       maybe more performant alternative are out there...
    */
    char* str2char(std::string S){
        char *out;
        out =  (char*) malloc ((S.size()+1)*sizeof(char));
        
        strcpy(out,&S[0]);
        out[S.size()] = '\0';
        
        return out;
    }
    
    mymulti::mymulti(int x, int y, int h, int l):Fl_Multiline_Input(x, y, h,l)
    {}
    
    int mymulti::handle(int event)
    {
        switch (event) {
            // When a key is pressed and the widget is waiting for input
            case FL_KEYBOARD:
                return handle_key(event, Fl::event_key());
            default:
                return Fl_Multiline_Input::handle(event);
            };
    }
    
    int mymulti::handle_key(int event, int key) {
    
        
        if (key==FL_BackSpace)
            // Allowing Fl_Multiline_Input to handle the delection of characters
            return Fl_Multiline_Input::handle(event);
        else{
            // Converting to std::string just because manipulation is easer
            std::string s(this->value());
            std::string s2;
            /* 
               Search for the last newline char: 
               the subsequent substring must be shorter 
               than the width of the input field
            */
            std::size_t found = s.find_last_of("\n");
            if (found<std::string::npos)
                s2= s.substr (found);
            else
                s2 = s;
            
            /* 
               Mean length of a char (lower case) 
               under the current fl_font. One can add 
               uppercase letters and/or numbers and/or 
               other symbols.
            */
            double lc = fl_width("abcdefghijklmnopqrstuvwxyz")/26;
            // Maximum length for the substring
            int string_length = this->w()/lc -1;
            // if the substring is longer than the max allowed length, then add a newline
            if (s2.length()>string_length)
                s+="\n";
            // Update the input field    
            this->value(str2char(s));
    
            return Fl_Multiline_Input::handle(event);   
            }
    }
    

    【讨论】:

      【解决方案2】:

      使用 FLTK,您可以随时捕捉键盘 events,例如使用(或重新定义)键盘事件

      人类打字速度不是很快,而计算机速度很快。您的键盘事件处理程序可以在每次击键时更改多行内容

      我们计划在 RefPerSys 项目中这样做(2020 年夏季)。

      【讨论】:

      • 我确实考虑过检查每个键盘输入的字符串长度,例如length(string)&gt;input_filed? string+=\n: ; ,但我想知道是否已经在 FLTK 中编码了某些内容。我会尝试你建议的方法。
      猜你喜欢
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 2018-09-13
      • 1970-01-01
      相关资源
      最近更新 更多