Skip to content

Commit a10d2c8

Browse files
author
jossonsmith
committed
Support CoolItem horizontal DND
1 parent 854058f commit a10d2c8

3 files changed

Lines changed: 202 additions & 50 deletions

File tree

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/CoolBar.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
margin:0;
1313
white-space:nowrap;
1414
height:24px;
15-
padding-left:10px;
16-
padding-right:4px;
15+
padding-left:7px;
16+
padding-right:2px;
1717
/*float:left;*/
1818
}
1919
* html .cool-item-default {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/CoolBar.java

Lines changed: 171 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
410534
protected 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

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/CoolItem.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class CoolItem extends Item {
4343

4444
Element contentHandle, moreHandle;
4545
int idealWidth, idealHeight;
46+
int minimumWidth, minimumHeight;
47+
int preferredWidth, preferredHeight;
4648
int lastCachedWidth, lastCachedHeight;
4749
boolean wrap;
4850

@@ -429,10 +431,10 @@ public Point getPreferredSize () {
429431
return new Point (width, rbBand.cyMinChild);
430432
*/
431433
if (ideal) {
432-
return new Point (idealWidth + parent.getMargin (index), idealHeight);
434+
return new Point(preferredWidth, preferredHeight);
433435
} else {
434-
return getSize();
435-
}
436+
return new Point(parent.getMargin(index), (minimum ? minimumHeight : 0));
437+
}
436438
}
437439

438440
/**
@@ -453,8 +455,10 @@ public void setPreferredSize (int width, int height) {
453455
width = Math.max (0, width);
454456
height = Math.max (0, height);
455457
ideal = true;
456-
idealWidth = Math.max (0, width - parent.getMargin (index));
458+
idealWidth = Math.max (0, width - 9);
457459
idealHeight = height;
460+
preferredWidth = width;
461+
preferredHeight = height;
458462
/*
459463
int hwnd = parent.handle;
460464
REBARBANDINFO rbBand = new REBARBANDINFO ();
@@ -535,10 +539,11 @@ public Point getSize() {
535539
int height = rect.bottom - rect.top;
536540
return new Point (width, height);
537541
*/
538-
int width = 13;
542+
int width = 0;
539543
int height = 0;
544+
545+
height = idealHeight;
540546
if (ideal) {
541-
width += idealWidth;
542547
height = idealHeight;
543548
if (control == null) {
544549
height = 4;
@@ -548,11 +553,18 @@ public Point getSize() {
548553
} else {
549554
height = 4;
550555
}
551-
if (!parent.isLastItemOfRow (index)) {
552-
width += (parent.style & SWT.FLAT) == 0 ? CoolBar.SEPARATOR_WIDTH : 0;
553-
} else if (ideal) {
556+
if (!parent.isLastItemOfRow (parent.indexOf(this))) {
557+
if (minimumWidth != 0) {
558+
width = 9 + Math.max(idealWidth, minimumWidth + 4) + 2;
559+
} else if (!ideal) {
560+
width = 9 + 4 + 2;
561+
} else {
562+
width = 9 + idealWidth + 2;
563+
}
564+
} else {
554565
width = parent.width - 2 * parent.getBorderWidth() - getPosition().x;
555566
}
567+
556568
lastCachedWidth = width;
557569
lastCachedHeight = height;
558570
return new Point (width, height);
@@ -665,8 +677,12 @@ public Point getMinimumSize () {
665677
rbBand.fMask = OS.RBBIM_CHILDSIZE;
666678
OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand);
667679
return new Point (rbBand.cxMinChild, rbBand.cyMinChild);
668-
*/
669-
return new Point(32, 16);
680+
*/
681+
if (minimum) {
682+
return new Point(minimumWidth, minimumHeight);
683+
} else {
684+
return new Point(0, 0);
685+
}
670686
}
671687

672688
/**
@@ -690,6 +706,8 @@ public void setMinimumSize (int width, int height) {
690706
width = Math.max (0, width);
691707
height = Math.max (0, height);
692708
minimum = true;
709+
minimumWidth = width;
710+
minimumHeight = height;
693711
/*
694712
int hwnd = parent.handle;
695713
REBARBANDINFO rbBand = new REBARBANDINFO ();

0 commit comments

Comments
 (0)