如何自动将 FLTK 的 Fl_Input-objects 一个堆叠在另一个下方

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

我创建了一个基于 FLTK 的 Fl_Input-class 的自定义对象,它绘制屏幕输入框并自动将其放置在 下方 上一个 已声明的框。这是通过使用 "external"(在 "external to the class implementation" 的意义上)计数器来实现的,该计数器用于计算框的 y 坐标。

这是 class 的简化版本:

// 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. 第一个盒子附在 window 的上部,而它应该在 "external" 常量 inbox_y1 定义的距离下方。这让我认为 Example 类型对象的声明没有考虑到这样的常量。
  2. 在每个新对象声明之后,计数器的增量值被打印出来,但实际上它不受影响。

问题: 我怎样才能让它正常工作? (我的印象是我在各种源文件中声明常量时弄得一团糟...)

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

最后说明: 我目前使用的代码实际上运行良好,但它有 一个单一的 源文件(我调用的常量 "external" 是全局常量)。将其分成多个源文件时出现问题。

方法一:在class中使用静力学。这不是一个很好的方法,因为它只知道自己而不知道其他任何东西。问题是如果你有第二个使用 Example 的 window,它从最后一个停止的位置开始,所以你需要一个重置函数来把所有东西放回开始。您的代码所做的更改是

// 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

方法二:询问parent。 parent 跟踪最后一个 child 的位置。这稍微好一点,因为 parent 控制 children 的位置。 child 需要做的就是询问 parent 它在哪里。

// 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();
}