1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 import org.eclipse.swt.graphics.*; 17 import org.eclipse.swt.events.*; 18 19 33 34 public class CoolItem extends Item { 35 CoolBar parent; 36 Control control; 37 int id; 38 boolean ideal, minimum; 39 40 70 public CoolItem (CoolBar parent, int style) { 71 super (parent, style); 72 this.parent = parent; 73 parent.createItem (this, parent.getItemCount ()); 74 } 75 76 108 public CoolItem (CoolBar parent, int style, int index) { 109 super (parent, style); 110 this.parent = parent; 111 parent.createItem (this, index); 112 } 113 114 144 public void addSelectionListener(SelectionListener listener) { 145 checkWidget(); 146 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 147 TypedListener typedListener = new TypedListener (listener); 148 addListener (SWT.Selection,typedListener); 149 addListener (SWT.DefaultSelection,typedListener); 150 } 151 152 protected void checkSubclass () { 153 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); 154 } 155 156 183 public Point computeSize (int wHint, int hHint) { 184 checkWidget (); 185 int index = parent.indexOf (this); 186 if (index == -1) return new Point (0, 0); 187 int width = wHint, height = hHint; 188 if (wHint == SWT.DEFAULT) width = 32; 189 if (hHint == SWT.DEFAULT) height = 32; 190 if ((parent.style & SWT.VERTICAL) != 0) { 191 height += parent.getMargin (index); 192 } else { 193 width += parent.getMargin (index); 194 } 195 return new Point (width, height); 196 } 197 198 void destroyWidget () { 199 parent.destroyItem (this); 200 releaseHandle (); 201 } 202 203 214 public Rectangle getBounds () { 215 checkWidget (); 216 int index = parent.indexOf (this); 217 if (index == -1) return new Rectangle (0, 0, 0, 0); 218 int hwnd = parent.handle; 219 RECT rect = new RECT (); 220 OS.SendMessage (hwnd, OS.RB_GETRECT, index, rect); 221 if (OS.COMCTL32_MAJOR >= 6) { 222 MARGINS margins = new MARGINS (); 223 OS.SendMessage (hwnd, OS.RB_GETBANDMARGINS, 0, margins); 224 rect.left -= margins.cxLeftWidth; 225 rect.right += margins.cxRightWidth; 226 } 227 if (!parent.isLastItemOfRow (index)) { 228 rect.right += (parent.style & SWT.FLAT) == 0 ? CoolBar.SEPARATOR_WIDTH : 0; 229 } 230 int width = rect.right - rect.left; 231 int height = rect.bottom - rect.top; 232 if ((parent.style & SWT.VERTICAL) != 0) { 233 return new Rectangle (rect.top, rect.left, height, width); 234 } 235 return new Rectangle (rect.left, rect.top, width, height); 236 } 237 238 Rectangle getClientArea () { 239 checkWidget (); 240 int index = parent.indexOf (this); 241 if (index == -1) return new Rectangle (0, 0, 0, 0); 242 int hwnd = parent.handle; 243 RECT insetRect = new RECT (); 244 OS.SendMessage (hwnd, OS.RB_GETBANDBORDERS, index, insetRect); 245 RECT rect = new RECT (); 246 OS.SendMessage (hwnd, OS.RB_GETRECT, index, rect); 247 int x = rect.left + insetRect.left; 248 int y = rect.top; 249 int width = rect.right - rect.left - insetRect.left; 250 int height = rect.bottom - rect.top; 251 if ((parent.style & SWT.FLAT) == 0) { 252 y += insetRect.top; 253 width -= insetRect.right; 254 height -= insetRect.top + insetRect.bottom; 255 } 256 if (index == 0) { 257 REBARBANDINFO rbBand = new REBARBANDINFO (); 258 rbBand.cbSize = REBARBANDINFO.sizeof; 259 rbBand.fMask = OS.RBBIM_HEADERSIZE; 260 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 261 width = width - rbBand.cxHeader + 1; 262 } 263 return new Rectangle (x, y, Math.max (0, width), Math.max (0, height)); 264 } 265 266 276 public Control getControl () { 277 checkWidget (); 278 return control; 279 } 280 281 291 public CoolBar getParent () { 292 checkWidget (); 293 return parent; 294 } 295 296 void releaseHandle () { 297 super.releaseHandle (); 298 parent = null; 299 id = -1; 300 control = null; 301 } 302 303 318 public void setControl (Control control) { 319 checkWidget (); 320 if (control != null) { 321 if (control.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT); 322 if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT); 323 } 324 int index = parent.indexOf (this); 325 if (index == -1) return; 326 if (this.control != null && this.control.isDisposed ()) { 327 this.control = null; 328 } 329 Control oldControl = this.control, newControl = control; 330 int hwnd = parent.handle; 331 int hwndChild = newControl != null ? control.topHandle () : 0; 332 REBARBANDINFO rbBand = new REBARBANDINFO (); 333 rbBand.cbSize = REBARBANDINFO.sizeof; 334 rbBand.fMask = OS.RBBIM_CHILD; 335 rbBand.hwndChild = hwndChild; 336 this.control = newControl; 337 338 344 int hwndAbove = 0; 345 if (newControl != null) { 346 hwndAbove = OS.GetWindow (hwndChild, OS.GW_HWNDPREV); 347 } 348 boolean hideNew = newControl != null && !newControl.getVisible (); 349 boolean showOld = oldControl != null && oldControl.getVisible (); 350 OS.SendMessage (hwnd, OS.RB_SETBANDINFO, index, rbBand); 351 if (hideNew) newControl.setVisible (false); 352 if (showOld) oldControl.setVisible (true); 353 if (hwndAbove != 0 && hwndAbove != hwndChild) { 354 int flags = OS.SWP_NOSIZE | OS.SWP_NOMOVE | OS.SWP_NOACTIVATE; 355 SetWindowPos (hwndChild, hwndAbove, 0, 0, 0, 0, flags); 356 } 357 } 358 359 371 public Point getPreferredSize () { 372 checkWidget (); 373 int index = parent.indexOf (this); 374 if (index == -1) return new Point (0, 0); 375 int hwnd = parent.handle; 376 REBARBANDINFO rbBand = new REBARBANDINFO (); 377 rbBand.cbSize = REBARBANDINFO.sizeof; 378 rbBand.fMask = OS.RBBIM_CHILDSIZE | OS.RBBIM_IDEALSIZE; 379 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 380 int width = rbBand.cxIdeal + parent.getMargin (index); 381 if ((parent.style & SWT.VERTICAL) != 0) { 382 return new Point (rbBand.cyMaxChild, width); 383 } 384 return new Point (width, rbBand.cyMaxChild); 385 } 386 387 398 public void setPreferredSize (int width, int height) { 399 checkWidget (); 400 int index = parent.indexOf (this); 401 if (index == -1) return; 402 width = Math.max (0, width); 403 height = Math.max (0, height); 404 ideal = true; 405 int hwnd = parent.handle; 406 int cxIdeal, cyMaxChild; 407 if ((parent.style & SWT.VERTICAL) != 0) { 408 cxIdeal = Math.max (0, height - parent.getMargin (index)); 409 cyMaxChild = width; 410 } else { 411 cxIdeal = Math.max (0, width - parent.getMargin (index)); 412 cyMaxChild = height; 413 } 414 REBARBANDINFO rbBand = new REBARBANDINFO (); 415 rbBand.cbSize = REBARBANDINFO.sizeof; 416 417 418 rbBand.fMask = OS.RBBIM_CHILDSIZE; 419 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 420 421 422 rbBand.fMask = OS.RBBIM_CHILDSIZE | OS.RBBIM_IDEALSIZE; 423 rbBand.cxIdeal = cxIdeal; 424 rbBand.cyMaxChild = cyMaxChild; 425 if (!minimum) rbBand.cyMinChild = cyMaxChild; 426 OS.SendMessage (hwnd, OS.RB_SETBANDINFO, index, rbBand); 427 } 428 429 442 public void setPreferredSize (Point size) { 443 checkWidget (); 444 if (size == null) error(SWT.ERROR_NULL_ARGUMENT); 445 setPreferredSize (size.x, size.y); 446 } 447 448 461 public Point getSize() { 462 checkWidget (); 463 int index = parent.indexOf (this); 464 if (index == -1) new Point (0, 0); 465 int hwnd = parent.handle; 466 RECT rect = new RECT (); 467 OS.SendMessage (hwnd, OS.RB_GETRECT, index, rect); 468 if (OS.COMCTL32_MAJOR >= 6) { 469 MARGINS margins = new MARGINS (); 470 OS.SendMessage (hwnd, OS.RB_GETBANDMARGINS, 0, margins); 471 rect.left -= margins.cxLeftWidth; 472 rect.right += margins.cxRightWidth; 473 } 474 if (!parent.isLastItemOfRow (index)) { 475 rect.right += (parent.style & SWT.FLAT) == 0 ? CoolBar.SEPARATOR_WIDTH : 0; 476 } 477 int width = rect.right - rect.left; 478 int height = rect.bottom - rect.top; 479 if ((parent.style & SWT.VERTICAL) != 0) { 480 return new Point (height, width); 481 } 482 return new Point (width, height); 483 } 484 485 501 public void setSize (int width, int height) { 502 checkWidget (); 503 int index = parent.indexOf (this); 504 if (index == -1) return; 505 width = Math.max (0, width); 506 height = Math.max (0, height); 507 int hwnd = parent.handle; 508 int cx, cyChild, cxIdeal; 509 if ((parent.style & SWT.VERTICAL) != 0) { 510 cx = height; 511 cyChild = width; 512 cxIdeal = Math.max (0, height - parent.getMargin (index)); 513 } else { 514 cx = width; 515 cyChild = height; 516 cxIdeal = Math.max (0, width - parent.getMargin (index)); 517 } 518 REBARBANDINFO rbBand = new REBARBANDINFO (); 519 rbBand.cbSize = REBARBANDINFO.sizeof; 520 521 522 rbBand.fMask = OS.RBBIM_CHILDSIZE | OS.RBBIM_IDEALSIZE; 523 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 524 525 526 if (!ideal) rbBand.cxIdeal = cxIdeal; 527 if (!minimum) rbBand.cyMinChild = cyChild; 528 rbBand.cyChild = cyChild; 529 530 533 if (!parent.isLastItemOfRow (index)) { 534 if (OS.COMCTL32_MAJOR >= 6) { 535 MARGINS margins = new MARGINS (); 536 OS.SendMessage (hwnd, OS.RB_GETBANDMARGINS, 0, margins); 537 cx -= margins.cxLeftWidth + margins.cxRightWidth; 538 } 539 int separator = (parent.style & SWT.FLAT) == 0 ? CoolBar.SEPARATOR_WIDTH : 0; 540 rbBand.cx = cx - separator; 541 rbBand.fMask |= OS.RBBIM_SIZE; 542 } 543 OS.SendMessage (hwnd, OS.RB_SETBANDINFO, index, rbBand); 544 } 545 546 564 public void setSize (Point size) { 565 if (size == null) error(SWT.ERROR_NULL_ARGUMENT); 566 setSize (size.x, size.y); 567 } 568 569 582 public Point getMinimumSize () { 583 checkWidget (); 584 int index = parent.indexOf (this); 585 if (index == -1) return new Point (0, 0); 586 int hwnd = parent.handle; 587 REBARBANDINFO rbBand = new REBARBANDINFO (); 588 rbBand.cbSize = REBARBANDINFO.sizeof; 589 rbBand.fMask = OS.RBBIM_CHILDSIZE; 590 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 591 if ((parent.style & SWT.VERTICAL) != 0) { 592 return new Point (rbBand.cyMinChild, rbBand.cxMinChild); 593 } 594 return new Point (rbBand.cxMinChild, rbBand.cyMinChild); 595 } 596 597 611 public void setMinimumSize (int width, int height) { 612 checkWidget (); 613 int index = parent.indexOf (this); 614 if (index == -1) return; 615 width = Math.max (0, width); 616 height = Math.max (0, height); 617 minimum = true; 618 int hwnd = parent.handle; 619 int cxMinChild, cyMinChild; 620 if ((parent.style & SWT.VERTICAL) != 0) { 621 cxMinChild = height; 622 cyMinChild = width; 623 } else { 624 cxMinChild = width; 625 cyMinChild = height; 626 } 627 REBARBANDINFO rbBand = new REBARBANDINFO (); 628 rbBand.cbSize = REBARBANDINFO.sizeof; 629 630 631 rbBand.fMask = OS.RBBIM_CHILDSIZE; 632 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 633 634 635 rbBand.cxMinChild = cxMinChild; 636 rbBand.cyMinChild = cyMinChild; 637 OS.SendMessage (hwnd, OS.RB_SETBANDINFO, index, rbBand); 638 } 639 640 656 public void setMinimumSize (Point size) { 657 checkWidget (); 658 if (size == null) error (SWT.ERROR_NULL_ARGUMENT); 659 setMinimumSize (size.x, size.y); 660 } 661 662 boolean getWrap() { 663 int index = parent.indexOf (this); 664 int hwnd = parent.handle; 665 REBARBANDINFO rbBand = new REBARBANDINFO (); 666 rbBand.cbSize = REBARBANDINFO.sizeof; 667 rbBand.fMask = OS.RBBIM_STYLE; 668 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 669 return (rbBand.fStyle & OS.RBBS_BREAK) != 0; 670 } 671 672 void setWrap(boolean wrap) { 673 int index = parent.indexOf (this); 674 int hwnd = parent.handle; 675 REBARBANDINFO rbBand = new REBARBANDINFO (); 676 rbBand.cbSize = REBARBANDINFO.sizeof; 677 rbBand.fMask = OS.RBBIM_STYLE; 678 OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand); 679 if (wrap) { 680 rbBand.fStyle |= OS.RBBS_BREAK; 681 } else { 682 rbBand.fStyle &= ~OS.RBBS_BREAK; 683 } 684 OS.SendMessage (hwnd, OS.RB_SETBANDINFO, index, rbBand); 685 } 686 687 706 public void removeSelectionListener(SelectionListener listener) { 707 checkWidget(); 708 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 709 if (eventTable == null) return; 710 eventTable.unhook (SWT.Selection, listener); 711 eventTable.unhook (SWT.DefaultSelection,listener); 712 } 713 714 } 715 | Popular Tags |