1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
/********************************************************************************
* *
* M a t r i x C o n t a i n e r O b j e c t *
* *
*********************************************************************************
* Copyright (C) 1997,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXMatrix.h"
/*
Notes:
- Need to observe FILL vs CENTER options.
- Filled items should shrink as well as stretch.
- Stretch should be proportional.
- Center mode, non-stretch should be observed.
- Row/Column stretchable iff all elements in row/column have row/column
stretch hint.
- Row/Column may be 0 pixels wide
- We should probably change layout so hidden children do not
affect numbering (but if all children in a row/column are
hidden, make the whole row disappear!).
- Navigating around
- Packing order:
MATRIX_BY_ROWS:
[1] [4] [7]
[2] [5] [8]
[3] [6] [9]
MATRIX_BY_COLUMNS:
[1] [2] [3]
[4] [5] [6]
[7] [8] [9]
- Possible solution for spanning rows/columns: have a table
containing mapping from FXWindow* to (nr,nc) span. Consult
table during layout. All children not listed are 1x1, special
API add/remove items to the table. Each layout, any item in the
table for which there is no corresponding child is removed.
Advantage: no need to add any special info into FXWindow. Also,
no need for any special API to add item into matrix.
*/
#define MAXNUM 512 // Maximum number of columns/rows
using namespace FX;
/*******************************************************************************/
namespace FX {
// Map
FXDEFMAP(FXMatrix) FXMatrixMap[]={
FXMAPFUNC(SEL_FOCUS_UP,0,FXMatrix::onFocusUp),
FXMAPFUNC(SEL_FOCUS_DOWN,0,FXMatrix::onFocusDown),
FXMAPFUNC(SEL_FOCUS_LEFT,0,FXMatrix::onFocusLeft),
FXMAPFUNC(SEL_FOCUS_RIGHT,0,FXMatrix::onFocusRight),
};
// Object implementation
FXIMPLEMENT(FXMatrix,FXPacker,FXMatrixMap,ARRAYNUMBER(FXMatrixMap))
// Make a vertical one
FXMatrix::FXMatrix(FXComposite* p,FXint n,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb,FXint hs,FXint vs):FXPacker(p,opts,x,y,w,h,pl,pr,pt,pb,hs,vs){
num=FXCLAMP(1,n,MAXNUM);
}
// Find child at given row, column
FXWindow* FXMatrix::childAtRowCol(FXint r,FXint c) const {
if(options&MATRIX_BY_COLUMNS){
return (0<=c && c<num) ? childAtIndex(num*r+c) : nullptr;
}
else{
return (0<=r && r<num) ? childAtIndex(r+num*c) : nullptr;
}
}
// Get child's row
FXint FXMatrix::rowOfChild(const FXWindow* child) const {
FXint i=indexOfChild(child);
return (options&MATRIX_BY_COLUMNS) ? i/num : i%num;
}
// Get child's column
FXint FXMatrix::colOfChild(const FXWindow* child) const {
FXint i=indexOfChild(child);
return (options&MATRIX_BY_COLUMNS) ? i%num : i/num;
}
// Focus moved up
long FXMatrix::onFocusUp(FXObject*,FXSelector,void* ptr){
FXWindow *child;
FXint r,c;
if(getFocus()){
r=rowOfChild(getFocus());
c=colOfChild(getFocus());
while((child=childAtRowCol(--r,c))!=nullptr){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_UP,0),ptr)) return 1;
}
}
}
else{
child=getLast();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_UP,0),ptr)) return 1;
}
child=child->getPrev();
}
}
return 0;
}
// Focus moved down
long FXMatrix::onFocusDown(FXObject*,FXSelector,void* ptr){
FXWindow *child;
FXint r,c;
if(getFocus()){
r=rowOfChild(getFocus());
c=colOfChild(getFocus());
while((child=childAtRowCol(++r,c))!=nullptr){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_DOWN,0),ptr)) return 1;
}
}
}
else{
child=getFirst();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_DOWN,0),ptr)) return 1;
}
child=child->getNext();
}
}
return 0;
}
// Focus moved to left
long FXMatrix::onFocusLeft(FXObject*,FXSelector,void* ptr){
FXWindow *child;
FXint r,c;
if(getFocus()){
r=rowOfChild(getFocus());
c=colOfChild(getFocus());
while((child=childAtRowCol(r,--c))!=nullptr){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_LEFT,0),ptr)) return 1;
}
}
}
else{
child=getLast();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_LEFT,0),ptr)) return 1;
}
child=child->getPrev();
}
}
return 0;
}
// Focus moved to right
long FXMatrix::onFocusRight(FXObject*,FXSelector,void* ptr){
FXWindow *child;
FXint r,c;
if(getFocus()){
r=rowOfChild(getFocus());
c=colOfChild(getFocus());
while((child=childAtRowCol(r,++c))!=nullptr){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_RIGHT,0),ptr)) return 1;
}
}
}
else{
child=getFirst();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
if(child->handle(this,FXSEL(SEL_FOCUS_RIGHT,0),ptr)) return 1;
}
child=child->getNext();
}
}
return 0;
}
// Set number of rows (if possible)
void FXMatrix::setNumRows(FXint nr){
if(nr<1 || nr>=MAXNUM){ fxerror("%s::setNumRows: bad number of rows specified.\n",getClassName()); }
if(!(options&MATRIX_BY_COLUMNS) && num!=nr){
num=nr;
recalc();
}
}
// Get number of rows
FXint FXMatrix::getNumRows() const {
return (num && (options&MATRIX_BY_COLUMNS)) ? (numChildren()+num-1)/num : num;
}
// Set number of columns (if possible)
void FXMatrix::setNumColumns(FXint nc){
if(nc<1 || nc>=MAXNUM){ fxerror("%s::setNumColumns: bad number of columns specified.\n",getClassName()); }
if((options&MATRIX_BY_COLUMNS) && num!=nc){
num=nc;
recalc();
}
}
// Get number of columns
FXint FXMatrix::getNumColumns() const {
return (num && !(options&MATRIX_BY_COLUMNS)) ? (numChildren()+num-1)/num : num;
}
// Compute minimum width based on child layout hints
FXint FXMatrix::getDefaultWidth(){
FXint c,n,w,nzcol=0,wmax=0,mw=0;
FXWindow *child;
FXuint hints;
FXint colw[MAXNUM];
for(c=0; c<MAXNUM; c++) colw[c]=0;
if(options&PACK_UNIFORM_WIDTH) mw=maxChildWidth();
for(child=getFirst(),n=0; child; child=child->getNext(),n++){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
else if(options&PACK_UNIFORM_WIDTH) w=mw;
else w=child->getDefaultWidth();
c=(options&MATRIX_BY_COLUMNS)?n%num:n/num;
FXASSERT(c<MAXNUM);
if(w>colw[c]){
if(colw[c]==0) nzcol++; // Count non-zero columns
wmax+=w-colw[c];
colw[c]=w;
}
}
}
if(nzcol>1) wmax+=(nzcol-1)*hspacing;
return padleft+padright+wmax+(border<<1);
}
// Compute minimum height based on child layout hints
FXint FXMatrix::getDefaultHeight(){
FXint r,n,h,nzrow=0,hmax=0,mh=0;
FXWindow *child;
FXuint hints;
FXint rowh[MAXNUM];
for(r=0; r<MAXNUM; r++) rowh[r]=0;
if(options&PACK_UNIFORM_HEIGHT) mh=maxChildHeight();
for(child=getFirst(),n=0; child; child=child->getNext(),n++){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
else if(options&PACK_UNIFORM_HEIGHT) h=mh;
else h=child->getDefaultHeight();
r=(options&MATRIX_BY_COLUMNS)?n/num:n%num;
FXASSERT(r<MAXNUM);
if(h>rowh[r]){
if(rowh[r]==0) nzrow++; // Count non-zero rows
hmax+=h-rowh[r];
rowh[r]=h;
}
}
}
if(nzrow>1) hmax+=(nzrow-1)*vspacing;
return padtop+padbottom+hmax+(border<<1);
}
// Recalculate layout
void FXMatrix::layout(){
FXint ncol,nrow,nzcol,nzrow,r,c,x,y,w,h,n,e,t;
FXint rowh[MAXNUM],colw[MAXNUM];
FXuchar srow[MAXNUM],scol[MAXNUM];
FXint left,right,top,bottom,cw,rh;
FXint mw=0,mh=0;
FXint hremain,vremain;
FXint hsumexpand,hnumexpand;
FXint vsumexpand,vnumexpand;
FXWindow *child;
FXuint hints;
// Placement rectangle; right/bottom non-inclusive
left=border+padleft;
right=width-border-padright;
top=border+padtop;
bottom=height-border-padbottom;
hremain=right-left;
vremain=bottom-top;
// Non-zero rows/columns
nzrow=0;
nzcol=0;
// Clear column/row sizes
for(n=0; n<MAXNUM; n++){
colw[n]=rowh[n]=0; // Columns may be 0 size
srow[n]=scol[n]=1;
}
// Get maximum child size
if(options&PACK_UNIFORM_WIDTH) mw=maxChildWidth();
if(options&PACK_UNIFORM_HEIGHT) mh=maxChildHeight();
// Find expandable columns and rows
for(child=getFirst(),n=0; child; child=child->getNext(),n++){
if(child->shown()){
hints=child->getLayoutHints();
if(options&MATRIX_BY_COLUMNS){r=n/num;c=n%num;}else{r=n%num;c=n/num;}
FXASSERT(r<MAXNUM && c<MAXNUM);
if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
else if(options&PACK_UNIFORM_WIDTH) w=mw;
else w=child->getDefaultWidth();
if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
else if(options&PACK_UNIFORM_HEIGHT) h=mh;
else h=child->getDefaultHeight();
FXASSERT(w>=0);
FXASSERT(h>=0);
if(w>colw[c]){ if(colw[c]==0) nzcol++; colw[c]=w; }
if(h>rowh[r]){ if(rowh[r]==0) nzrow++; rowh[r]=h; }
if(!(hints&LAYOUT_FILL_COLUMN)) scol[c]=0;
if(!(hints&LAYOUT_FILL_ROW)) srow[r]=0;
}
}
// Get number of rows and columns
if(options&MATRIX_BY_COLUMNS){
ncol=num;
nrow=(n+num-1)/num;
}
else{
ncol=(n+num-1)/num;
nrow=num;
}
// Find stretch in columns
for(c=hsumexpand=hnumexpand=0; c<ncol; c++){
if(colw[c]){
if(scol[c]){
hsumexpand+=colw[c];
hnumexpand++;
}
else{
hremain-=colw[c];
}
}
}
// Find stretch in rows
for(r=vsumexpand=vnumexpand=0; r<nrow; r++){
if(rowh[r]){
if(srow[r]){
vsumexpand+=rowh[r];
vnumexpand++;
}
else{
vremain-=rowh[r];
}
}
}
// Subtract spacing for non-zero rows/columns
if(nzcol>1) hremain-=(nzcol-1)*hspacing;
if(nzrow>1) vremain-=(nzrow-1)*vspacing;
// Disburse space horizontally
for(c=e=0,x=border+padleft; c<ncol; c++){
w=colw[c];
colw[c]=x;
if(w){
if(scol[c]){
if(hsumexpand>0){ // Divide proportionally
t=w*hremain;
w=t/hsumexpand;
e+=t%hsumexpand;
if(e>=hsumexpand){w++;e-=hsumexpand;}
}
else{ // Divide equally
FXASSERT(hnumexpand>0);
w=hremain/hnumexpand;
e+=hremain%hnumexpand;
if(e>=hnumexpand){w++;e-=hnumexpand;}
}
}
x+=w+hspacing;
}
}
colw[ncol]=x;
// Disburse space vertically
for(r=e=0,y=border+padtop; r<nrow; r++){
h=rowh[r];
rowh[r]=y;
if(h){
if(srow[r]){
if(vsumexpand>0){ // Divide proportionally
t=h*vremain;
h=t/vsumexpand;
e+=t%vsumexpand;
if(e>=vsumexpand){h++;e-=vsumexpand;}
}
else{ // Divide equally
FXASSERT(vnumexpand>0);
h=vremain/vnumexpand;
e+=vremain%vnumexpand;
if(e>=vnumexpand){h++;e-=vnumexpand;}
}
}
y+=h+vspacing;
}
}
rowh[nrow]=y;
// Do the layout
for(child=getFirst(),n=0; child; child=child->getNext(),n++){
if(child->shown()){
hints=child->getLayoutHints();
if(options&MATRIX_BY_COLUMNS){r=n/num;c=n%num;}else{r=n%num;c=n/num;}
cw=colw[c+1]-colw[c]-hspacing;
rh=rowh[r+1]-rowh[r]-vspacing;
if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
else if(hints&LAYOUT_FILL_X) w=cw;
else if(options&PACK_UNIFORM_WIDTH) w=mw;
else w=child->getDefaultWidth();
if(hints&LAYOUT_CENTER_X) x=colw[c]+(cw-w)/2;
else if(hints&LAYOUT_RIGHT) x=colw[c]+cw-w;
else x=colw[c];
if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
else if(hints&LAYOUT_FILL_Y) h=rh;
else if(options&PACK_UNIFORM_HEIGHT) h=mh;
else h=child->getDefaultHeight();
if(hints&LAYOUT_CENTER_Y) y=rowh[r]+(rh-h)/2;
else if(hints&LAYOUT_BOTTOM) y=rowh[r]+rh-h;
else y=rowh[r];
child->position(x,y,w,h);
}
}
flags&=~FLAG_DIRTY;
}
// Change matrix style
void FXMatrix::setMatrixStyle(FXuint style){
FXuint opts=(options&~MATRIX_BY_COLUMNS) | (style&MATRIX_BY_COLUMNS);
if(opts!=options){
options=opts;
recalc();
update();
}
}
// Return matrix style
FXuint FXMatrix::getMatrixStyle() const {
return options&MATRIX_BY_COLUMNS;
}
}
|