如何自动将 FLTK 的Fl_Input对象一个堆叠在另一个下面

How to automatically stack FLTK's Fl_Input-objects one below the other

本文关键字:一个 另一个 FLTK 何自动 Fl 对象 Input      更新时间:2023-10-16

我基于 FLTK 的 Fl_Input 类创建了一个定制对象,该对象绘制以筛选输入框并自动将其放置在声明的前一个下方。这是通过使用用于计算框的 y 坐标的"外部"(在"类实现的外部"意义上)计数器来实现的。

下面是该类的简化版本:

// Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
class Example : public Fl_Input {
public:
  Example(const char * str); // constructor
private:
  int ex_y(); // compute y-coordinate of current box based on the previous ones
};
#endif // EXAMPLE_H

下面是成员函数和构造函数:

// Example.cpp
#include "Example.h"
int inbox_y1;       // y-coord of first box; an example of “external” constants
int inbox_num;      // counter: number of input boxes declared; used to compute their y-coord
Example::Example(const char * str)
  : Fl_Input(200, ex_y(), 200, 25, str)
{
  ++inbox_num;
}
int Example::ex_y()
{
  return inbox_y1 + (25+25)*inbox_num;;
}

最后是主文件(我将对象的声明包装在自定义函数中):

// main.cpp
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Window.H>
#include <string>
#include <iostream>
#include "Example.h"
void place_inboxes()
{
  int inbox_y1 = 25; // not used by the following objects: the first one is attached to the upper window
  int inbox_num = 0; // the counter is not incremented by the following declarations
  Example * ex1 = new Example("First Input Box");
  std::cout << "First call: " << inbox_num << 'n';
  Example * ex2 = new Example("Second Input Box");
  std::cout << "Second call: " << inbox_num << 'n';
  Example * ex3 = new Example("Third Input Box");
  std::cout << "Third call: " << inbox_num << 'n';
}
int main()
{
  Fl_Window * win = new Fl_Window(600, 600, "Test of 'Example' class");
  {
    place_inboxes();
  }
  win->end();
  win->show();
  return Fl::run();
}

这成功编译和链接,但是(即使盒子确实堆叠在一起)结果也不是我期望的:

  1. 第一个框连接到窗口的上部,而它应该位于其下方,距离由("外部")常数inbox_y1定义。这让我认为Example型对象的声明没有考虑到这种常量。
  2. 在每个新对象声明之后,都会打印计数器的递增值,但实际上它不受影响。

问题:如何使其正常工作?(我的印象是我在声明各种源文件中的常量时弄得一团糟......

(可选)附带问题:定义一个既构建对象又放置对象的类是否被认为是不好的做法?我的意思是,让一个对象一次做"太多"的事情是一种不好的做法吗?(我也想写一个放置对象的函数,但我什至不知道从哪里开始。

结语:我目前使用的代码实际上运行良好,但它只有一个源文件(其中我称之为"外部"的常量是全局常量)。将其划分为多个源文件时出现问题。

方法 1:在类中使用静态。 这不是一个非常好的方法,因为它只知道自己,而不了解其他任何事情。 问题是,如果您有第二个使用 Example 的窗口,它从最后一个窗口停止的位置开始,因此您需要一个重置功能来将所有内容放回起点。 对代码的更改是

// Example.h
class Example : public Fl_Input {
public:
    Example(const char * str); // constructor
    static int inbox_y1, inbox_num;
    static void Reset();
...
// Example.cpp
int Example::inbox_y1, Example::inbox_num;
void Example::Reset()
{
    inbox_y1 = 25;
    inbox_num = 0;
}
...
// main.cpp
// call Example::Reset() before you call place_inboxes

方法二:问家长。 父母跟踪最后一个孩子在哪里。 这稍微好一点,因为父母控制了孩子的位置。 孩子需要做的就是问父母它在哪里。

// Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Window.H>
class ExParent: public Fl_Window
{
public:
   ExParent(int x, int y, const char* name);
   int child_x();
   int child_y();
   void next_child_y();
private:
   int cy;
};
class Example : public Fl_Input {
public:
   Example(ExParent* parent, const char * str); // constructor
};
#endif // EXAMPLE_H

// Example.cpp
#include "Example.h"
Example::Example(ExParent* parent, const char * str)
  : Fl_Input(parent->child_x(), parent->child_y(), 200, 25, str)
{
}
ExParent::ExParent(int x, int y, const char* name)
: Fl_Window(x, y, name)
, cy(25)
{
}
int ExParent::child_x() { return 200; }
int ExParent::child_y()
{
   return cy;
}
void ExParent::next_child_y()
{
   cy += 50;
}
int main()
{
  ExParent * win = new ExParent(600, 600, "Test of 'Example' class");
  Example * ex1 = new Example(win, "First Input Box");
  win->next_child_y();
  Example * ex2 = new Example(win, "Second Input Box");
  win->next_child_y();
  Example * ex3 = new Example(win, "Third Input Box");
  win->end();
  win->show();
  return Fl::run();
}
相关文章: