如何使用X11、motif、DrawingArea和C++来设置DrawingArea的(X,Y)坐标为(0,0)?

3

我正在尝试使用绘图区域,但事情并不像我预期的那样工作。

#include <Xm/Xm.h>
#include <Xm/DrawingA.h>

main(int argc, char *argv[])
{
  Widget shell, workArea, box1;
  XtAppContext app;
  shell = XtVaAppInitialize(&app, "gp", NULL, 0, &argc, argv, NULL, XmNwidth, 500, XmNheight, 500, NULL);
  XtRealizeWidget(shell);

  workArea = XtCreateWidget("wa",xmDrawingAreaWidgetClass, shell, NULL, 0);
  XtVaSetValues(workArea, XmNbackground, 30000, NULL);

  box1 = XtCreateWidget("b1", xmDrawingAreaWidgetClass, workArea, NULL, 0);
  XtVaSetValues(box1, XmNx, 0, XmNy, 0, XmNwidth, 400, XmNheight, 400, NULL);

  XtManageChild(workArea);
  XtManageChild(box1);
  //XtAppMainLoop(app);
  XEvent event;
  Dimension x,y,w,h;
  while(1)
  {
    XtAppNextEvent(app, &event);
    if (event.type == EnterNotify)
    {
      XtVaGetValues(box1, XmNx, &x, XmNy, &y, XmNwidth, &w, XmNheight, &h, NULL);
      printf("(x,y,w,h) == (%d,%d,%d,%d)\n", x, y, w, h);
    }
    if (event.type == LeaveNotify)
    {
      XtVaSetValues(box1, XmNx, 0, XmNy, 0, XmNwidth, 400, XmNheight, 400, NULL);
      printf("tried to set (x,y,w,h) = (0,0,400,400)\n");
    }
    XtDispatchEvent(&event);
  }
}

当我用指针进入窗口并离开窗口时,会输出以下内容:
(x,y,w,h) == (10,10,400,400)
(x,y,w,h) == (10,10,400,400)
tried to set (x,y,w,h) = (0,0,400,400)
tried to set (x,y,w,h) = (0,0,400,400)
(x,y,w,h) == (10,10,400,400)
(x,y,w,h) == (10,10,400,400)
tried to set (x,y,w,h) = (0,0,400,400)
tried to set (x,y,w,h) = (0,0,400,400)

为什么XtVaSetValues不能把box1设置为(X,Y)=(0,0)?我应该如何在窗口中将绘图区域放在(0,0)的位置?

我已经找到了答案,但是由于声望不够,无法提供答案:

XtManageChild(box1);
XtUnmanageChild(box1);
XtVaSetValues(box1, XmNx, 0, XmNy, 0, XmNwidth, 400, XmNheight, 400, NULL);
XtMapWidget(box1);

看起来调用 XtManageChild() 函数时会调用父对象的 change_managed 过程: http://linux.die.net/man/3/xtmanagechild - Gavin Palmer
现在你应该有足够的声望来提供答案了。 - ypnos
1个回答

1

看起来调用XtManageChild()的是父窗口的change_managed过程:

xtmanpage

为了将(x,y)设置为(0,0),我必须确保小部件未被管理:

XtManageChild(box1); // must be called once
XtUnmanageChild(box1); // unmanage to allow (0,0)
XtVaSetValues(box1, NmNx, 0, XmNy, 0, XmNwidth, 400, XmNheight, 400, NULL);
XtMapWidget(box1); // show the widget

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接