Advertisement
Guest User

QNX PtDivider childs width problem

a guest
Jun 6th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.25 KB | None | 0 0
  1. #include <Pt.h>
  2. #include <assert.h>
  3. #include <stdbool.h>
  4.  
  5. int cb_divider_drag00(PtWidget_t *divider, void *d, PtCallbackInfo_t *cbinfo) {
  6.   d = d;
  7.  
  8.   static bool running = false;  // avoid recursive invocation
  9.   if (running) {
  10.     return Pt_CONTINUE;
  11.   }
  12.  
  13.   /* divider child is being resized using PtSetResource() */
  14.   if (cbinfo->reason_subtype == Pt_CB_DIVIDER_SETRESOURCES) {
  15.     return Pt_CONTINUE;
  16.   }
  17.  
  18.   running = true;
  19.  
  20.   PtDividerCallback_t *dvdr_cb = cbinfo->cbdata;
  21.  
  22.   /* find the index of the widget with changed dimension */
  23.   PtWidget_t *tmp = dvdr_cb->left;
  24.   int pos = 0;
  25.   /* this callback is called only when there are at least 2 child widgets
  26.      in PtDivider */
  27.   while ((tmp = PtWidgetBrotherBehind(tmp)) != NULL)
  28.     pos++;
  29.  
  30.   /* recalculate the width of all PtDivider brothers in front and behind */
  31.   PtWidget_t *row = divider;
  32.   while ((row = PtWidgetBrotherBehind(row)) != NULL) {
  33.     /* last internally added widget in PtDivider is always PtGroup
  34.         containing the user-defined widgets */
  35.     tmp = PtWidgetChildBack(row);
  36.     tmp = PtWidgetChildBack(tmp);
  37.  
  38.     int i = 0;
  39.     for (i = 0; i < pos; i++) {
  40.       tmp = PtWidgetBrotherInFront(tmp);
  41.     }
  42.  
  43.     /* only to eye-test if `tmp' is the right widget */
  44.     PtSetResource(tmp, Pt_ARG_TEXT_STRING, "XXX", 0);
  45.  
  46.     /* this does nothing :( */
  47.     PtSetResource(tmp, Pt_ARG_MIN_WIDTH,
  48.         dvdr_cb->sizes[pos].to - dvdr_cb->sizes[pos -1].to, 0);
  49.     PtSetResource(tmp, Pt_ARG_WIDTH,
  50.         dvdr_cb->sizes[pos].to - dvdr_cb->sizes[pos -1].to, 0);
  51.   }
  52.  
  53.   running = false;
  54.   return Pt_CONTINUE;
  55. }
  56.  
  57. int cb_resize00(PtWidget_t *w, void *d, PtCallbackInfo_t *cbinfo) {
  58.   w = w;
  59.   cbinfo = cbinfo;
  60.   PtWidget_t *group = d;
  61.  
  62.   static int run = 0;
  63.   /* just guessing; jump over a few initial internal calls */
  64.   if (run < 5) {
  65.     run++;
  66.     return Pt_CONTINUE;
  67.   }
  68.  
  69.   /* really nasty hack to overcome the fixed size/geometry issue (the
  70.      value is unimportant) */
  71.   PhDim_t dim;
  72.   /* unfortunately, this needs to be called each time the width increases
  73.      because one call to this callback extends the width by 1px (disregarding
  74.      the dim variable) */
  75.   if (PtSetResource(group, Pt_ARG_DIM, &dim, 0))
  76.     PtExit(EXIT_FAILURE);
  77.  
  78.   return Pt_CONTINUE;
  79. }
  80.  
  81. int main( int argc, char *argv[] ) {
  82.   PtWidget_t *window, *container, *scroll, *group,
  83.              *divider00, *divider01;
  84.   PtArg_t args[20];
  85.   int i = 0;
  86.  
  87.   PhRect_t rect;
  88.   PhPoint_t point;
  89.   PhDim_t dim;
  90.  
  91.   rect.ul.x = 0; rect.ul.y = 0; // upper left
  92.   rect.lr.x = 9; rect.lr.y = 9; // lower right
  93.   point.x = 0; point.y = 0;
  94.   dim.w = 350; dim.h = 250;
  95.  
  96.   /* Create a window */
  97.   i = 0;
  98.   PtSetArg( &args[i++], Pt_ARG_MAX_WIDTH, 350, 0);
  99.   PtSetArg( &args[i++], Pt_ARG_MAX_HEIGHT, 350, 0);
  100.   PtSetArg( &args[i++], Pt_ARG_WINDOW_TITLE, "demo window", 0 );
  101.   if ( NULL == ( window = PtAppInit( NULL, &argc, argv, i, args ) ) ) {
  102.     perror( "PtAppInit()" );
  103.     return 1;
  104.   }
  105.  
  106.   /* mimic the real setup (app where we have the nested widget hierarchy
  107.      like the following) */
  108.   i = 0;
  109.   PtSetArg( &args[i++], Pt_ARG_ANCHOR_FLAGS,
  110.       Pt_BOTTOM_ANCHORED_BOTTOM | Pt_TOP_ANCHORED_TOP |
  111.       Pt_LEFT_ANCHORED_LEFT     | Pt_RIGHT_ANCHORED_RIGHT,
  112.       Pt_IS_ANCHORED);
  113.   container = PtCreateWidget( PtContainer, window, i, args );
  114.  
  115.   i = 0;
  116.   PtSetArg( &args[i++], Pt_ARG_ANCHOR_FLAGS,
  117.       Pt_BOTTOM_ANCHORED_BOTTOM | Pt_TOP_ANCHORED_TOP |
  118.       Pt_LEFT_ANCHORED_LEFT     | Pt_RIGHT_ANCHORED_RIGHT,
  119.       Pt_IS_ANCHORED);
  120.   scroll = PtCreateWidget( PtScrollContainer, container, i, args );
  121.  
  122.   i = 0;
  123.   PtSetArg( &args[i++], Pt_ARG_GROUP_ORIENTATION, Pt_GROUP_VERTICAL, 0 );
  124.   /* TODO exchange PtGroup for PtDivider to gain vertically adjustable
  125.        heigh of rows
  126.      wtf? in this case PtDivider does not get the size of it's childs (
  127.        PtDivider widgets) and it misplaces them anyway */
  128.   group = PtCreateWidget( PtGroup, scroll, i, args );
  129.  
  130.   PtCallback_t callbacks_resize[1];
  131.   PtCallback_t callbacks_divider_drag[1];
  132.   callbacks_resize[0].event_f = cb_resize00;
  133.   callbacks_resize[0].data = group;
  134.   callbacks_divider_drag[0].event_f = cb_divider_drag00;
  135.   callbacks_divider_drag[0].data = group;
  136.  
  137.   i = 0;
  138.   PtSetArg(&args[i++], Pt_CB_RESIZE, callbacks_resize,
  139.       sizeof(callbacks_resize)/sizeof(callbacks_resize[0]));
  140.   PtSetArg(&args[i++], Pt_CB_DIVIDER_DRAG, callbacks_divider_drag,
  141.       sizeof(callbacks_divider_drag)/sizeof(callbacks_divider_drag[0]));
  142.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  143.   PtSetArg( &args[i++], Pt_ARG_DIVIDER_FLAGS, Pt_FALSE, Pt_DIVIDER_RESIZE_BOTH );
  144.   /* invoke CB if the subordinate childs changed geometry themselves */
  145.   PtSetArg( &args[i++], Pt_ARG_FLAGS, Pt_TRUE, Pt_CALLBACKS_ACTIVE );
  146.   /* disable PtDivider reaction on it's child's position&dimension changes */
  147.   //PtSetArg( &args[i++], Pt_ARG_CONTAINER_FLAGS, Pt_FALSE, Pt_CHILD_MOVED_RESIZED);
  148.   divider00 = PtCreateWidget( PtDivider, group, i, args );
  149.  
  150.   i = 0;
  151.   PtSetArg(&args[i++], Pt_CB_RESIZE, callbacks_resize,
  152.       sizeof(callbacks_resize)/sizeof(callbacks_resize[0]));
  153.   PtSetArg(&args[i++], Pt_CB_DIVIDER_DRAG, callbacks_divider_drag,
  154.       sizeof(callbacks_divider_drag)/sizeof(callbacks_divider_drag[0]));
  155.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  156.   PtSetArg( &args[i++], Pt_ARG_DIVIDER_FLAGS, Pt_FALSE, Pt_DIVIDER_RESIZE_BOTH );
  157.   PtSetArg( &args[i++], Pt_ARG_FLAGS, Pt_TRUE, Pt_CALLBACKS_ACTIVE );
  158.   //PtSetArg( &args[i++], Pt_ARG_CONTAINER_FLAGS, Pt_FALSE, Pt_CHILD_MOVED_RESIZED);
  159.   divider01 = PtCreateWidget( PtDivider, group, i, args );
  160.  
  161.   PtWidget_t *row00 = divider00,
  162.              *row01 = divider01;
  163.  
  164.   /* row00 buttons */
  165.  
  166.   i = 0;
  167.   PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "T1", 0 );
  168.   /* wtf? why does PtText get so narrow that no character is to be seen? */
  169.   PtSetArg( &args[i++], Pt_ARG_WIDTH, 40, 0);
  170.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  171.   PtCreateWidget( PtText, row00, i, args );
  172.  
  173.   i = 0;
  174.   PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Button 2 (two)", 0);
  175.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  176.   PtCreateWidget( PtButton, row00, i, args );
  177.  
  178.   i = 0;
  179.   PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Butt 3", 0 );
  180.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  181.   PtCreateWidget( PtButton, row00, i, args );
  182.  
  183.   /* row01 buttons */
  184.  
  185.   i = 0;
  186.   PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "B4", 0 );
  187.   /* this is ignored by PtDivider */
  188.   PtSetArg( &args[i++], Pt_ARG_MIN_WIDTH, 70, 0);
  189.   /* because PtDivider ignores MIN_WIDTH, this is necessary to set the initial
  190.      width */
  191.   PtSetArg( &args[i++], Pt_ARG_WIDTH, 70, 0);
  192.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  193.   PtCreateWidget( PtButton, row01, i, args );
  194.  
  195.   i = 0;
  196.   PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Butt 5", 0 );
  197.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  198.   PtCreateWidget( PtButton, row01, i, args );
  199.  
  200.   i = 0;
  201.   PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Buttonchen 6", 0 );
  202.   PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
  203.   PtCreateWidget( PtButton, row01, i, args );
  204.  
  205.   PtRealizeWidget( window );
  206.   PtMainLoop();
  207.  
  208.   return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement