我正在使用FLTK中的UI进行操作,而我的基本问题归结为:
我在主窗口上有一堆小部件(输入,输出,按钮小部件)。假设有16个项目(4行x 4列)。我需要使用箭头键在小部件之间导航。
主要是,如果焦点在(r4,c4)(右下角小部件)上:
向右箭头键应将焦点移至(r1,c1)(左上角小部件)。向左箭头键应将焦点移至(r4,c3)。向上箭头键应将焦点移至(r3,c4)。向下箭头键应将焦点移至(r1,c4)。
即根据我们的习惯,导航应该非常直观。
默认情况下,在使用箭头(或Tab)键时,焦点在小部件之间的创建顺序改变。向上和向右始终转到下一个窗口小部件(按创建顺序),而向下和向左始终转到上一个窗口小部件(按创建顺序)。
FLTK中有什么办法吗?还是我需要创建一个算法来做到这一点?
请查看分发测试程序中名为demo.cxx的程序。它似乎可以完成您想要的操作,而无需任何特殊编程。
这里是一个示例,如果您需要一个
#include <FL/FL.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <string>
#include <vector>
int ROWS = 4;
int COLS = 4;
int BUTTON_WID = 40;
int BUTTON_HGT = 40;
int BUTTON_SEP = 5;
// These must exist for the duration otherwise
// we get a load of random text as labels
std::vector<std::string> buttonLab;
Fl_Window* CreateGrid()
{
int gridwid = (BUTTON_WID + BUTTON_SEP) * COLS + BUTTON_SEP;
int gridhgt = (BUTTON_HGT + BUTTON_SEP) * ROWS + BUTTON_SEP;
Fl_Window* grid = new Fl_Window(gridwid, gridhgt, "GridTest");
grid->begin();
// Create the labels first - if the vector reallocates
// the fltk widgets lose the pointers
char tag[] = "@";
for (int rr = 0; rr < ROWS; ++rr)
{
for (int cc = 0; cc < COLS; ++cc)
{
++tag[0];
buttonLab.push_back(tag);
}
}
// Create the buttons
int ix = -1;
for (int ypos = BUTTON_SEP; ypos < gridhgt; ypos += (BUTTON_HGT + BUTTON_SEP))
{
for (int xpos = BUTTON_SEP; xpos < gridwid; xpos += (BUTTON_WID + BUTTON_SEP))
{
Fl_Button* button = new Fl_Button(xpos, ypos, BUTTON_WID, BUTTON_HGT, buttonLab[++ix].c_str());
}
}
grid->end();
return grid;
}
int main(int argc, char* argv[])
{
Fl_Window* grid = CreateGrid();
grid->show(argc, argv);
Fl::run();
return 0;
}
编辑:如果以随机顺序创建窗口小部件,则覆盖导航的最简单方法是提供自己的Fl_Group :: navigation函数。可以通过
来完成将所有小部件放在此新的组类中,您将可以控制导航
您想要的例程是Fl_Widget-> take_focus()。