ui/motif/Grid.c

changeset 101
7b3a3130be44
parent 100
d2bd73d28ff1
equal deleted inserted replaced
100:d2bd73d28ff1 101:7b3a3130be44
50 //static char defaultTranslations[] = "<BtnDown>: mousedown()\n"; 50 //static char defaultTranslations[] = "<BtnDown>: mousedown()\n";
51 static char defaultTranslations[] = "\ 51 static char defaultTranslations[] = "\
52 <EnterWindow>: getfocus()\n\ 52 <EnterWindow>: getfocus()\n\
53 <LeaveWindow>: loosefocus()\n"; 53 <LeaveWindow>: loosefocus()\n";
54 54
55 static XtResource resources[] =
56 {
57 {
58 gridColumnSpacing,
59 gridColumnSpacing,
60 XmRDimension,
61 sizeof (Dimension),
62 XtOffsetOf( GridRec,
63 mywidget.columnspacing),
64 XmRImmediate,
65 (XtPointer) 0
66 },
67 {
68 gridRowSpacing,
69 gridRowSpacing,
70 XmRDimension,
71 sizeof (Dimension),
72 XtOffsetOf( GridRec,
73 mywidget.rowspacing),
74 XmRImmediate,
75 (XtPointer) 0
76 },
77 {
78 gridMargin,
79 gridMargin,
80 XmRDimension,
81 sizeof (Dimension),
82 XtOffsetOf( GridRec,
83 mywidget.margin),
84 XmRImmediate,
85 (XtPointer) 0
86 }
87 };
55 88
56 ///* 89 ///*
57 static XtResource constraints[] = 90 static XtResource constraints[] =
58 { 91 {
59 { 92 {
173 sizeof (Boolean), 206 sizeof (Boolean),
174 XtOffsetOf( GridConstraintRec, 207 XtOffsetOf( GridConstraintRec,
175 grid.vfill), 208 grid.vfill),
176 XmRImmediate, 209 XmRImmediate,
177 (XtPointer) 0 210 (XtPointer) 0
178 } 211 },
179 212 {
213 gridMinWidth,
214 gridMinWidth,
215 XmRDimension,
216 sizeof (Dimension),
217 XtOffsetOf( GridConstraintRec,
218 grid.min_width),
219 XmRImmediate,
220 (XtPointer) 0
221 }
180 }; 222 };
181 //*/ 223 //*/
182 //static XtResource constraints[] = {};
183 224
184 GridClassRec gridClassRec = { 225 GridClassRec gridClassRec = {
185 // Core Class 226 // Core Class
186 { 227 {
187 //(WidgetClass)&constraintClassRec, // superclass 228 //(WidgetClass)&constraintClassRec, // superclass
188 (WidgetClass)&xmManagerClassRec, 229 (WidgetClass)&xmManagerClassRec,
189 "Grid", // class_name 230 "Grid", // class_name
190 sizeof(GridRec), // widget_size 231 sizeof(GridRec), // widget_size
191 grid_class_initialize, // class_initialize 232 grid_class_initialize, // class_initialize
192 NULL, // class_part_initialize 233 NULL, // class_part_initialize
193 FALSE, // class_inited 234 FALSE, // class_inited
194 (XtInitProc)grid_initialize, // initialize 235 (XtInitProc)grid_initialize, // initialize
195 NULL, // initialize_hook 236 NULL, // initialize_hook
196 grid_realize, // realize 237 grid_realize, // realize
197 actionslist, // actions 238 actionslist, // actions
198 XtNumber(actionslist), // num_actions 239 XtNumber(actionslist), // num_actions
199 NULL, // resources 240 resources, // resources
200 0, // num_resources 241 XtNumber(resources), // num_resources
201 NULLQUARK, // xrm_class 242 NULLQUARK, // xrm_class
202 True, // compress_motion 243 True, // compress_motion
203 True, // compress_exposure 244 True, // compress_exposure
204 True, // compress_enterleave 245 True, // compress_enterleave
205 False, // visible_interest 246 False, // visible_interest
238 NULL, /* extension */ 279 NULL, /* extension */
239 }, 280 },
240 // XmManager Class 281 // XmManager Class
241 ///* 282 ///*
242 { 283 {
243 NULL, 284 XtInheritTranslations,
244 NULL, 285 NULL,
245 0, 286 0,
246 NULL, 287 NULL,
247 0, 288 0,
248 NULL, 289 XmInheritParentProcess,
249 NULL 290 NULL
250 }, 291 },
251 //*/ 292 //*/
252 // MyWidget Class 293 // MyWidget Class
253 { 294 {
365 int num_cols_expanding = 0; 406 int num_cols_expanding = 0;
366 int num_rows_expanding = 0; 407 int num_rows_expanding = 0;
367 int req_width = 0; 408 int req_width = 0;
368 int req_height = 0; 409 int req_height = 0;
369 410
411 //printf("container width: %d\n", (int)w->core.width);
412
370 // calculate the minimum size requirements for all columns and rows 413 // calculate the minimum size requirements for all columns and rows
371 // we need to run this 2 times: for widgets without colspan/rowspan first 414 // we need to run this 2 times: for widgets without colspan/rowspan first
372 // and then again for colspan/rowspan > 1 415 // and then again for colspan/rowspan > 1
373 int span_max = 1; 416 int span_max = 1;
374 for(int r=0;r<2;r++) { 417 for(int r=0;r<2;r++) {
375 for(int i=0;i<w->composite.num_children;i++) { 418 for(int i=0;i<w->composite.num_children;i++) {
376 Widget child = w->composite.children[i]; 419 Widget child = w->composite.children[i];
377 GridConstraintRec *constraints = child->core.constraints; 420 GridConstraintRec *constraints = child->core.constraints;
421 if(constraints->grid.pref_width == 0) {
422 constraints->grid.pref_width = child->core.width;
423 }
424 if(constraints->grid.pref_height == 0) {
425 constraints->grid.pref_height = child->core.height;
426 }
427 if(constraints->grid.pref_width < constraints->grid.min_width) {
428 constraints->grid.pref_width = constraints->grid.min_width;
429 }
378 430
379 if(constraints->grid.colspan > span_max || constraints->grid.rowspan > span_max) { 431 if(constraints->grid.colspan > span_max || constraints->grid.rowspan > span_max) {
380 continue; 432 continue;
381 } 433 }
382 434
461 } 513 }
462 } 514 }
463 span_max = 50000; // not sure if this is unreasonable low or high 515 span_max = 50000; // not sure if this is unreasonable low or high
464 } 516 }
465 517
466 518 // calc required size
467 for(int i=0;i<ncols;i++) { 519 for(int i=0;i<ncols;i++) {
468 if(cols[i].expand) { 520 if(cols[i].expand) {
469 num_cols_expanding++; 521 num_cols_expanding++;
470 } 522 }
471 req_width += cols[i].size; 523 req_width += cols[i].size;
476 } 528 }
477 req_height += rows[i].size; 529 req_height += rows[i].size;
478 } 530 }
479 531
480 if(req_width > 0 && req_height > 0) { 532 if(req_width > 0 && req_height > 0) {
533 // add col/row spacing
534 req_width += (ncols-1)*w->mywidget.columnspacing;
535 req_height += (nrows-1)*w->mywidget.rowspacing;
536
481 Widget parent = w->core.parent; 537 Widget parent = w->core.parent;
482 Dimension rwidth = req_width; 538 Dimension rwidth = req_width;
483 Dimension rheight = req_height; 539 Dimension rheight = req_height;
484 if(rwidth < w->core.width) { 540 if(rwidth < w->core.width) {
485 //rwidth = w->core.width; 541 //rwidth = w->core.width;
489 } 545 }
490 546
491 if(!w->mywidget.sizerequest) { 547 if(!w->mywidget.sizerequest) {
492 Dimension actual_width, actual_height; 548 Dimension actual_width, actual_height;
493 w->mywidget.sizerequest = TRUE; 549 w->mywidget.sizerequest = TRUE;
550
551 //XtWidgetGeometry request;
552 //request.width = req_width;
553 //request.request_mode = CWWidth;
554 //XtWidgetGeometry reply;
555 //XtGeometryResult result = XtMakeGeometryRequest((Widget)w, &request, &reply);
556
494 XtMakeResizeRequest((Widget)w, req_width, req_height, &actual_width, &actual_height); 557 XtMakeResizeRequest((Widget)w, req_width, req_height, &actual_width, &actual_height);
495 w->mywidget.sizerequest = FALSE; 558 w->mywidget.sizerequest = FALSE;
496 //printf("size request: %d %d\n", (int)actual_width, (int)actual_height); 559 //printf("size request: %d %d\n", (int)actual_width, (int)actual_height);
497 } 560 }
498 561
499 562
500 563
501 } 564 }
502 565
566 // how much space can we add to each expanding col/row
503 int hexpand = 0; 567 int hexpand = 0;
504 int width_diff = (int)w->core.width - req_width; 568 int width_diff = (int)w->core.width - req_width;
505 int hexpand2 = 0; 569 int hexpand2 = 0;
506 if(width_diff > 0 && num_cols_expanding > 0) { 570 if(width_diff > 0 && num_cols_expanding > 0) {
507 hexpand = width_diff / num_cols_expanding; 571 hexpand = width_diff / num_cols_expanding;
511 for(int i=0;i<ncols;i++) { 575 for(int i=0;i<ncols;i++) {
512 cols[i].pos = x; 576 cols[i].pos = x;
513 if(cols[i].expand) { 577 if(cols[i].expand) {
514 cols[i].size += hexpand + hexpand2; 578 cols[i].size += hexpand + hexpand2;
515 } 579 }
516 x += cols[i].size; 580 x += cols[i].size + w->mywidget.columnspacing;
517 581
518 hexpand2 = 0; 582 hexpand2 = 0;
519 } 583 }
520 584
521 int vexpand = 0; 585 int vexpand = 0;
529 for(int i=0;i<nrows;i++) { 593 for(int i=0;i<nrows;i++) {
530 rows[i].pos = y; 594 rows[i].pos = y;
531 if(rows[i].expand) { 595 if(rows[i].expand) {
532 rows[i].size += vexpand + vexpand2; 596 rows[i].size += vexpand + vexpand2;
533 } 597 }
534 y += rows[i].size; 598 y += rows[i].size += w->mywidget.rowspacing;
535 599
536 vexpand2 = 0; 600 vexpand2 = 0;
537 } 601 }
538 602
539 for(int i=0;i<w->composite.num_children;i++) { 603 for(int i=0;i<w->composite.num_children;i++) {
548 if(constraints->grid.hfill) { 612 if(constraints->grid.hfill) {
549 if(constraints->grid.colspan > 1) { 613 if(constraints->grid.colspan > 1) {
550 Dimension cwidth = 0; 614 Dimension cwidth = 0;
551 for(int j=0;j<constraints->grid.colspan;j++) { 615 for(int j=0;j<constraints->grid.colspan;j++) {
552 if(constraints->grid.x+j < ncols) { 616 if(constraints->grid.x+j < ncols) {
553 cwidth += cols[constraints->grid.x+j].size; 617 cwidth += cols[constraints->grid.x+j].size + (j > 0 ? w->mywidget.columnspacing : 0);
554 } 618 }
555 } 619 }
556 width = cwidth; 620 width = cwidth;
557 } else { 621 } else {
558 width = c.size; 622 width = c.size - w->mywidget.columnspacing;
559 } 623 }
560 } 624 }
561 if(constraints->grid.vfill) { 625 if(constraints->grid.vfill) {
562 if(constraints->grid.rowspan > 1) { 626 if(constraints->grid.rowspan > 1) {
563 Dimension cheight = 0; 627 Dimension cheight = 0;
564 for(int j=0;j<constraints->grid.rowspan;j++) { 628 for(int j=0;j<constraints->grid.rowspan;j++) {
565 if(constraints->grid.y+j < nrows) { 629 if(constraints->grid.y+j < nrows) {
566 cheight += rows[constraints->grid.y+j].size; 630 cheight += rows[constraints->grid.y+j].size + (j > 0 ? w->mywidget.rowspacing : 0);
567 } 631 }
568 } 632 }
569 height = cheight; 633 height = cheight;
570 } else { 634 } else {
571 height = r.size; 635 height = r.size - w->mywidget.rowspacing;
572 } 636 }
573 } 637 }
574 638
575 XtConfigureWidget(child, x, y, width, height, child->core.border_width); 639 if(width > 0 && height > 0) {
640 XtConfigureWidget(child, x, y, width, height, child->core.border_width);
641 }
576 //printf("child %d %d - %d %d\n", (int)child->core.x, (int)child->core.y, (int)child->core.width, (int)child->core.height); 642 //printf("child %d %d - %d %d\n", (int)child->core.x, (int)child->core.y, (int)child->core.width, (int)child->core.height);
577 } 643 }
578 644
579 free(cols); 645 free(cols);
580 free(rows); 646 free(rows);

mercurial