1 11 package org.eclipse.swt.widgets; 12 13 import org.eclipse.swt.graphics.*; 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 17 35 public class ExpandItem extends Item { 36 ExpandBar parent; 37 Control control; 38 boolean expanded, hover; 39 int x, y, width, height; 40 int imageHeight, imageWidth; 41 static final int TEXT_INSET = 6; 42 static final int BORDER = 1; 43 static final int CHEVRON_SIZE = 24; 44 45 72 public ExpandItem (ExpandBar parent, int style) { 73 this (parent, style, checkNull (parent).getItemCount ()); 74 } 75 76 106 public ExpandItem (ExpandBar parent, int style, int index) { 107 super (parent, style); 108 this.parent = parent; 109 parent.createItem (this, style, index); 110 } 111 112 static ExpandBar checkNull (ExpandBar control) { 113 if (control == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 114 return control; 115 } 116 117 private void drawChevron (int hDC, RECT rect) { 118 int oldBrush = OS.SelectObject (hDC, OS.GetSysColorBrush (OS.COLOR_BTNFACE)); 119 OS.PatBlt (hDC, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, OS.PATCOPY); 120 OS.SelectObject (hDC, oldBrush); 121 rect.left += 4; 122 rect.top += 4; 123 rect.right -= 4; 124 rect.bottom -= 4; 125 int hPen = OS.CreatePen (OS.PS_SOLID, 1, parent.foreground); 126 int oldPen = OS.SelectObject (hDC, hPen); 127 int [] polyline1, polyline2; 128 if (expanded) { 129 int px = rect.left + 5; 130 int py = rect.top + 7; 131 polyline1 = new int [] { 132 px,py, px+1,py, px+1,py-1, px+2,py-1, px+2,py-2, px+3,py-2, px+3,py-3, 133 px+3,py-2, px+4,py-2, px+4,py-1, px+5,py-1, px+5,py, px+7,py}; 134 py += 4; 135 polyline2 = new int [] { 136 px,py, px+1,py, px+1,py-1, px+2,py-1, px+2,py-2, px+3,py-2, px+3,py-3, 137 px+3,py-2, px+4,py-2, px+4,py-1, px+5,py-1, px+5,py, px+7,py}; 138 } else { 139 int px = rect.left + 5; 140 int py = rect.top + 4; 141 polyline1 = new int[] { 142 px,py, px+1,py, px+1,py+1, px+2,py+1, px+2,py+2, px+3,py+2, px+3,py+3, 143 px+3,py+2, px+4,py+2, px+4,py+1, px+5,py+1, px+5,py, px+7,py}; 144 py += 4; 145 polyline2 = new int [] { 146 px,py, px+1,py, px+1,py+1, px+2,py+1, px+2,py+2, px+3,py+2, px+3,py+3, 147 px+3,py+2, px+4,py+2, px+4,py+1, px+5,py+1, px+5,py, px+7,py}; 148 } 149 OS.Polyline (hDC, polyline1, polyline1.length / 2); 150 OS.Polyline (hDC, polyline2, polyline2.length / 2); 151 if (hover) { 152 int whitePen = OS.CreatePen (OS.PS_SOLID, 1, OS.GetSysColor (OS.COLOR_3DHILIGHT)); 153 int darkGrayPen = OS.CreatePen (OS.PS_SOLID, 1, OS.GetSysColor (OS.COLOR_3DSHADOW)); 154 OS.SelectObject (hDC, whitePen); 155 int [] points1 = { 156 rect.left, rect.bottom, 157 rect.left, rect.top, 158 rect.right, rect.top}; 159 OS.Polyline (hDC, points1, points1.length / 2); 160 OS.SelectObject (hDC, darkGrayPen); 161 int [] points2 = { 162 rect.right, rect.top, 163 rect.right, rect.bottom, 164 rect.left, rect.bottom}; 165 OS.Polyline (hDC, points2, points2.length / 2); 166 OS.SelectObject (hDC, oldPen); 167 OS.DeleteObject (whitePen); 168 OS.DeleteObject (darkGrayPen); 169 } else { 170 OS.SelectObject (hDC, oldPen); 171 } 172 OS.DeleteObject (hPen); 173 } 174 175 void drawItem (GC gc, int hTheme, RECT clipRect, boolean drawFocus) { 176 int hDC = gc.handle; 177 int headerHeight = parent.getBandHeight (); 178 RECT rect = new RECT (); 179 OS.SetRect (rect, x, y, x + width, y + headerHeight); 180 if (hTheme != 0) { 181 OS.DrawThemeBackground (hTheme, hDC, OS.EBP_NORMALGROUPHEAD, 0, rect, clipRect); 182 } else { 183 int oldBrush = OS.SelectObject (hDC, OS.GetSysColorBrush (OS.COLOR_BTNFACE)); 184 OS.PatBlt (hDC, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, OS.PATCOPY); 185 OS.SelectObject (hDC, oldBrush); 186 } 187 if (image != null) { 188 rect.left += ExpandItem.TEXT_INSET; 189 if (imageHeight > headerHeight) { 190 gc.drawImage (image, rect.left, rect.top + headerHeight - imageHeight); 191 } else { 192 gc.drawImage (image, rect.left, rect.top + (headerHeight - imageHeight) / 2); 193 } 194 rect.left += imageWidth; 195 } 196 if (text.length () > 0) { 197 rect.left += ExpandItem.TEXT_INSET; 198 TCHAR buffer = new TCHAR (parent.getCodePage (), text, false); 199 if (hTheme != 0) { 200 OS.DrawThemeText (hTheme, hDC, OS.EBP_NORMALGROUPHEAD, 0, buffer.chars, buffer.length(), OS.DT_VCENTER | OS.DT_SINGLELINE, 0, rect); 201 } else { 202 int oldBkMode = OS.SetBkMode (hDC, OS.TRANSPARENT); 203 OS.DrawText (hDC, buffer, buffer.length (), rect, OS.DT_VCENTER | OS.DT_SINGLELINE); 204 OS.SetBkMode (hDC, oldBkMode); 205 } 206 } 207 int chevronSize = ExpandItem.CHEVRON_SIZE; 208 rect.left = rect.right - chevronSize; 209 rect.top = y + (headerHeight - chevronSize) / 2; 210 rect.bottom = rect.top + chevronSize; 211 if (hTheme != 0) { 212 int partID = expanded ? OS.EBP_NORMALGROUPCOLLAPSE : OS.EBP_NORMALGROUPEXPAND; 213 int stateID = hover ? OS.EBNGC_HOT : OS.EBNGC_NORMAL; 214 OS.DrawThemeBackground (hTheme, hDC, partID, stateID, rect, clipRect); 215 } else { 216 drawChevron (hDC, rect); 217 } 218 if (drawFocus) { 219 OS.SetRect (rect, x + 1, y + 1, x + width - 2, y + headerHeight - 2); 220 OS.DrawFocusRect (hDC, rect); 221 } 222 if (expanded) { 223 if (!parent.isAppThemed ()) { 224 int pen = OS.CreatePen (OS.PS_SOLID, 1, OS.GetSysColor (OS.COLOR_BTNFACE)); 225 int oldPen = OS.SelectObject (hDC, pen); 226 int [] points = { 227 x, y + headerHeight, 228 x, y + headerHeight + height, 229 x + width - 1, y + headerHeight + height, 230 x + width - 1, y + headerHeight - 1}; 231 OS.Polyline (hDC, points, points.length / 2); 232 OS.SelectObject (hDC, oldPen); 233 OS.DeleteObject (pen); 234 } 235 } 236 } 237 238 void destroyWidget () { 239 parent.destroyItem (this); 240 releaseHandle (); 241 } 242 243 254 public Control getControl () { 255 checkWidget (); 256 return control; 257 } 258 259 270 public boolean getExpanded () { 271 checkWidget (); 272 return expanded; 273 } 274 275 285 public int getHeaderHeight () { 286 checkWidget (); 287 return Math.max (parent.getBandHeight (), imageHeight); 288 } 289 290 300 public int getHeight () { 301 checkWidget (); 302 return height; 303 } 304 305 315 public ExpandBar getParent () { 316 checkWidget (); 317 return parent; 318 } 319 320 int getPreferredWidth (int hTheme, int hDC) { 321 int width = ExpandItem.TEXT_INSET * 2 + ExpandItem.CHEVRON_SIZE; 322 if (image != null) { 323 width += ExpandItem.TEXT_INSET + imageWidth; 324 } 325 if (text.length() > 0) { 326 RECT rect = new RECT (); 327 TCHAR buffer = new TCHAR (parent.getCodePage (), text, false); 328 if (hTheme != 0) { 329 OS.GetThemeTextExtent (hTheme, hDC, OS.EBP_NORMALGROUPHEAD, 0, buffer.chars, buffer.length(), OS.DT_SINGLELINE, null, rect); 330 } else { 331 OS.DrawText (hDC, buffer, buffer.length (), rect, OS.DT_CALCRECT); 332 } 333 width += (rect.right - rect.left); 334 } 335 return width; 336 } 337 338 boolean isHover (int x, int y) { 339 int bandHeight = parent.getBandHeight (); 340 return this.x < x && x < (this.x + width) && this.y < y && y < (this.y + bandHeight); 341 } 342 343 void redraw (boolean all) { 344 int parentHandle = parent.handle; 345 int headerHeight = parent.getBandHeight (); 346 RECT rect = new RECT (); 347 int left = all ? x : x + width - headerHeight; 348 OS.SetRect (rect, left, y, x + width, y + headerHeight); 349 OS.InvalidateRect (parentHandle, rect, true); 350 if (imageHeight > headerHeight) { 351 OS.SetRect (rect, x + ExpandItem.TEXT_INSET, y + headerHeight - imageHeight, x + ExpandItem.TEXT_INSET + imageWidth, y); 352 OS.InvalidateRect (parentHandle, rect, true); 353 } 354 if (!parent.isAppThemed ()) { 355 OS.SetRect (rect, x, y + headerHeight, x + width, y + headerHeight + height + 1); 356 OS.InvalidateRect (parentHandle, rect, true); 357 } 358 } 359 360 void releaseHandle () { 361 super.releaseHandle (); 362 parent = null; 363 } 364 365 void releaseWidget () { 366 super.releaseWidget (); 367 control = null; 368 } 369 370 void setBounds (int x, int y, int width, int height, boolean move, boolean size) { 371 redraw (true); 372 int headerHeight = parent.getBandHeight (); 373 if (move) { 374 if (imageHeight > headerHeight) { 375 y += (imageHeight - headerHeight); 376 } 377 this.x = x; 378 this.y = y; 379 redraw (true); 380 } 381 if (size) { 382 this.width = width; 383 this.height = height; 384 redraw (true); 385 } 386 if (control != null && !control.isDisposed ()) { 387 if (!parent.isAppThemed ()) { 388 x += BORDER; 389 width = Math.max (0, width - BORDER * 2); 390 height = Math.max (0, height - BORDER); 391 } 392 if (move && size) control.setBounds (x, y + headerHeight, width, height); 393 if (move && !size) control.setLocation (x, y + headerHeight); 394 if (!move && size) control.setSize (width, height); 395 } 396 } 397 398 412 public void setControl (Control control) { 413 checkWidget (); 414 if (control != null) { 415 if (control.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); 416 if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT); 417 } 418 this.control = control; 419 if (control != null) { 420 int headerHeight = parent.getBandHeight (); 421 control.setVisible (expanded); 422 if (!parent.isAppThemed ()) { 423 int width = Math.max (0, this.width - BORDER * 2); 424 int height = Math.max (0, this.height - BORDER); 425 control.setBounds (x + BORDER, y + headerHeight, width, height); 426 } else { 427 control.setBounds (x, y + headerHeight, width, height); 428 } 429 } 430 } 431 432 442 public void setExpanded (boolean expanded) { 443 checkWidget (); 444 this.expanded = expanded; 445 parent.showItem (this); 446 } 447 448 459 public void setHeight (int height) { 460 checkWidget (); 461 if (height < 0) return; 462 setBounds (0, 0, width, height, false, true); 463 if (expanded) parent.layoutItems (parent.indexOf (this) + 1, true); 464 } 465 466 public void setImage (Image image) { 467 super.setImage (image); 468 int oldImageHeight = imageHeight; 469 if (image != null) { 470 Rectangle bounds = image.getBounds (); 471 imageHeight = bounds.height; 472 imageWidth = bounds.width; 473 } else { 474 imageHeight = imageWidth = 0; 475 } 476 if (oldImageHeight != imageHeight) { 477 parent.layoutItems (parent.indexOf (this), true); 478 } else { 479 redraw (true); 480 } 481 } 482 483 public void setText (String string) { 484 super.setText (string); 485 redraw (true); 486 } 487 } 488 | Popular Tags |