@@ -190,17 +190,17 @@ public Point computeSize (int wHint, int hHint, boolean changed) {
190190 rowHeight = 0 ;
191191 }
192192 if (items [i ].ideal ) {
193- rowWidth += items [i ].idealWidth + getMargin ( i ) + separator ;
193+ rowWidth += 7 + 2 + Math . max ( items [i ].idealWidth , items [ i ]. minimumWidth + 2 ) + separator ;
194194 if (items [i ].control == null ) {
195195 rowHeight = Math .max (rowHeight , 4 );
196196 } else {
197197 rowHeight = Math .max (rowHeight , items [i ].idealHeight );
198198 }
199199 } else {
200200 if (items [i ].control != null ) {
201- rowWidth += items [i ].control .getSize ().x + getMargin ( i ) + separator ;
201+ rowWidth += items [i ].control .getSize ().x + 9 + 2 + 2 + separator ;
202202 } else {
203- rowWidth += 0 + getMargin ( i ) + separator ;
203+ rowWidth += 9 + 2 + 2 + separator ;
204204 rowHeight = Math .max (rowHeight , 4 );
205205 }
206206 }
@@ -332,39 +332,21 @@ void createItem (final CoolItem item, int index) {
332332 dnd .addDragListener (new DragAdapter () {
333333 int dxx ;
334334 public boolean dragging (DragEvent e ) {
335- Rectangle b1 = getBounds ();
336- Rectangle b2 = item .getBounds ();
335+ // Rectangle b1 = getBounds();
336+ // Rectangle b2 = item.getBounds();
337337 int idx = indexOf (item );
338338 int dx = e .deltaX () - dxx ;
339- if (dx != 0 && idx != 0 ) {
340- CoolItem prevItem = items [idx - 1 ];
341- Point size = prevItem .getPreferredSize ();
342- size .x += dx ;
343- if (size .x > 13 && size .x + prevItem .getPosition ().x < b1 .width ) {
344- prevItem .setPreferredSize (size );
345- size = item .getPreferredSize ();
346- size .x -= dx ;
347- if (size .x > 13 && size .x + b2 .x < b1 .width ) {
348- item .setPreferredSize (size );
349- SetWindowPos (handle , null , left , top , width , height , 0 );
350- dxx = e .deltaX ();
351- } else {
352-
353- }
339+ if (dx != 0 ) {
340+ //System.out.println("//" + dx);
341+ if (moveDelta (idx , 0 , 0 , dx , 0 )) {
342+ dxx = e .deltaX ();
354343 }
355344 }
356- // System.out.println(b1);
357- // System.out.println(b2);
358- // System.out.println(e.currentX + ":" + e.currentY);
359345 return true ;
360346 }
361347 public boolean dragBegan (DragEvent e ) {
362- dxx = e .deltaX ();
363- return true ;
364- }
365- public boolean dragEnded (DragEvent e ) {
366348 dxx = 0 ;
367- return super . dragEnded ( e ) ;
349+ return true ;
368350 }
369351 });
370352 dnd .bind (el );
@@ -407,6 +389,148 @@ public boolean dragEnded(DragEvent e) {
407389 //originalItems[index] = item;
408390}
409391
392+ boolean moveDelta (int index , int cx , int cy , int dx , int dy ) {
393+ if (dx == 0 && dy == 0 ) return false ;
394+ boolean needLayout = false ;
395+ if (dy == 0 ) {
396+ int [] ws = new int [items .length ];
397+ for (int i = 0 ; i < ws .length ; i ++) {
398+ ws [i ] = items [i ].idealWidth ;
399+ }
400+ boolean needCalculate = false ;
401+
402+ CoolItem item = items [index ];
403+ if (item .wrap && (dx < 0 || isLastItemOfRow (index ))) {
404+ return false ;
405+ }
406+ if ((index == 0 && items .length > 1 ) // The first cool item
407+ || (item .wrap && !isLastItemOfRow (index ))) { // The second+ line item
408+ if (dx >= item .lastCachedWidth ) {
409+ CoolItem next = items [index + 1 ];
410+ items [index ] = next ;
411+ items [index + 1 ] = item ;
412+ int width = next .idealWidth ;
413+ next .idealWidth = item .idealWidth ;
414+ item .idealWidth = width ;
415+ dx = dx - item .lastCachedWidth ;
416+ index ++;
417+ needLayout = true ;
418+ }
419+ }
420+ if (dx != 0 && index > 0 && !(item .wrap && !isLastItemOfRow (index ))) {
421+ CoolItem cur = item ;
422+ CoolItem prev = items [index - 1 ];
423+ int idx = index - 1 ;
424+ while (dx < 0 ) { // Move left
425+ if (prev .lastCachedWidth + dx < minWidth (prev )) {
426+ int ddx = prev .lastCachedWidth - minWidth (prev ); // > 0
427+ prev .idealWidth -= ddx ;
428+ item .idealWidth += ddx ;
429+ needCalculate = true ;
430+ dx += ddx ;
431+ if (dx < 0 ) {
432+ if (idx - 1 >= 0 && !items [idx ].wrap ) {
433+ idx --;
434+ prev = items [idx ];
435+ } else {
436+ if (dx + 11 <= 0 ) {
437+ CoolItem swpItem = prev ;
438+ int swpIndex = index ;
439+ while (dx + minWidth (swpItem ) <= 0 ) {
440+ dx += minWidth (swpItem );
441+ swpItem = items [swpIndex - 1 ];
442+ items [swpIndex - 1 ] = items [swpIndex ];
443+ items [swpIndex ] = swpItem ;
444+ if (swpItem .wrap ) {
445+ items [swpIndex - 1 ].wrap = true ;
446+ swpItem .wrap = false ;
447+ }
448+ needLayout = true ;
449+ swpIndex --;
450+ if (swpIndex == 0 || swpItem .wrap ) {
451+ break ;
452+ }
453+ }
454+ }
455+ //prev.idealWidth -= dx;
456+ dx = 0 ; // break dx < 0 loop
457+ break ;
458+ }
459+ }
460+ } else {
461+ break ;
462+ }
463+ }
464+ CoolItem next = null ;
465+ idx = index ;
466+ while (dx > 0 && cur .lastCachedWidth - dx < minWidth (cur )) {
467+ // reach current item's minimum size
468+ int dxx = cur .lastCachedWidth - minWidth (cur );
469+ prev .idealWidth += dxx ;
470+ cur .idealWidth -= dxx ;
471+ needCalculate = true ;
472+ dx -= dxx ;
473+ if (dx > 0 ) { // need to push right more, select the next item
474+ if (idx + 1 < items .length && !isLastItemOfRow (idx )) {
475+ idx ++;
476+ cur = items [idx ];
477+ if (next == null ) {
478+ next = cur ;
479+ }
480+ } else {
481+ // Already piled up at the end. Try to swap the selected item
482+ if (dx >= 11 && next != null ) {
483+ CoolItem swpItem = next ;
484+ int swpIndex = index ;
485+ while (dx >= minWidth (swpItem )) {
486+ items [swpIndex + 1 ] = items [swpIndex ];
487+ items [swpIndex ] = swpItem ;
488+ swpItem = items [swpIndex + 1 ];
489+ needLayout = true ;
490+ dx -= minWidth (swpItem );
491+ swpIndex ++;
492+ if (swpIndex >= items .length || isLastItemOfRow (swpIndex )) {
493+ break ;
494+ }
495+ }
496+ }
497+ dx = 0 ;
498+ break ; // break while loop
499+ }
500+ }
501+ }
502+ prev .idealWidth += dx ;
503+ if (dx != 0 ) {
504+ needCalculate = true ;
505+ }
506+ if (item != cur ) {
507+ if (cur .idealWidth - dx < 0 ) {
508+ if (cur .idealWidth != 0 ) {
509+ needCalculate = true ;
510+ }
511+ cur .idealWidth = 0 ;
512+ } else {
513+ cur .idealWidth -= dx ;
514+ }
515+ } else {
516+ item .idealWidth -= dx ;
517+ }
518+ }
519+ if (needCalculate && !needLayout ) {
520+ for (int i = 0 ; i < ws .length ; i ++) {
521+ if (ws [i ] != items [i ].idealWidth ) {
522+ needLayout = true ;
523+ break ;
524+ }
525+ }
526+ }
527+ }
528+ if (needLayout ) {
529+ SetWindowPos (handle , null , 0 , 0 , width , height , -1 );
530+ }
531+ return needLayout ;
532+ }
533+
410534protected void createWidget () {
411535 super .createWidget ();
412536 items = new CoolItem [0 ];
@@ -497,21 +621,27 @@ int getMargin (int index) {
497621 }
498622 RECT rect = new RECT ();
499623 OS.SendMessage (handle, OS.RB_GETBANDBORDERS, index, rect);
500- */
501624 if ((style & SWT.FLAT) != 0) {
502625 /*
503626 * Bug in Windows. When the style bit RBS_BANDBORDERS is not set
504627 * the rectangle returned by RBS_BANDBORDERS is four pixels too small.
505628 * The fix is to add four pixels to the result.
506- */
507- //margin += rect.left + 4;
508- margin += 13 ;
629+ *-/
630+ margin += rect.left + 4;
509631 } else {
510- //margin += rect.left + rect.right;
511- margin += 13 ;
632+ margin += rect.left + rect.right;
633+ }
634+ */
635+ margin = 7 + 2 ;
636+ if (!isLastItemOfRow (index )) {
637+ margin += 2 ;
512638 }
513639 return margin ;
514640}
641+
642+ int minWidth (CoolItem item ) {
643+ return 7 + 2 + item .minimumWidth + (item .minimumWidth != 0 ? 4 : 0 ) + (!isLastItemOfRow (indexOf (item )) ? 2 : 0 );
644+ }
515645
516646/* (non-Javadoc)
517647 * @see org.eclipse.swt.widgets.Control#enableWidget(boolean)
@@ -1153,16 +1283,20 @@ protected boolean SetWindowPos(Object hWnd, Object hWndInsertAfter, int X, int Y
11531283 Rectangle bounds = item .getBounds ();
11541284 s .left = bounds .x + "px" ;
11551285 s .top = bounds .y + "px" ;
1156- int w = bounds .width - getMargin (i ) - bw ;
1286+ //int w = bounds.width - getMargin(i) - bw;
1287+ int w = bounds .width - 11 ;
11571288 s .width = (w > 0 ? w : 0 ) + "px" ;
11581289 s .height = bounds .height + "px" ;
11591290 if (item .control != null ) {
1160- item .control .setSize (w + bw - (! isLastItemOfRow (i ) ? 2 : 0 ), bounds .height );
1291+ item .control .setSize (w - 2 - (isLastItemOfRow (i ) ? 0 : 2 ), bounds .height );
11611292 Point pt = item .getPosition ();
1162- item .control .left = pt .x + getMargin ( i ) - 4 ;
1293+ item .control .left = pt .x + 9 ;
11631294 item .control .top = pt .y ;
11641295 }
11651296 }
1297+ if (uFlags == -1 ) {
1298+ return false ;
1299+ }
11661300 return super .SetWindowPos (hWnd , hWndInsertAfter , X , Y , cx , cy , uFlags );
11671301}
11681302
0 commit comments