diff options
author | Felipe Heidrich <fheidric> | 2006-04-07 20:36:50 +0000 |
---|---|---|
committer | Felipe Heidrich <fheidric> | 2006-04-07 20:36:50 +0000 |
commit | 25e834c280324b3e7770e2b3f82a7d7ca7a1f981 (patch) | |
tree | 64ed5a02aadca6f1d294c87c9c5c42b307401603 /bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ExpandItem.java | |
parent | 564af48002efb7dde4608cffc2bddfed66929de1 (diff) | |
download | eclipse.platform.swt-25e834c280324b3e7770e2b3f82a7d7ca7a1f981.tar.gz eclipse.platform.swt-25e834c280324b3e7770e2b3f82a7d7ca7a1f981.tar.xz eclipse.platform.swt-25e834c280324b3e7770e2b3f82a7d7ca7a1f981.zip |
Bug 130853 - Implement ExpandBar on GTK 2.2.x
Diffstat (limited to 'bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ExpandItem.java')
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ExpandItem.java | 318 |
1 files changed, 236 insertions, 82 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ExpandItem.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ExpandItem.java index 58c0d1a176..198689071f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ExpandItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ExpandItem.java @@ -20,7 +20,13 @@ public class ExpandItem extends Item { Control control; ImageList imageList; int /*long*/ clientHandle, boxHandle, labelHandle, imageHandle; - + boolean expanded; + int x, y, width, height; + int imageHeight, imageWidth; + static final int TEXT_INSET = 6; + static final int BORDER = 1; + static final int CHEVRON_SIZE = 24; + public ExpandItem (ExpandBar parent, int style) { super (parent, style); this.parent = parent; @@ -30,10 +36,6 @@ public ExpandItem (ExpandBar parent, int style) { public ExpandItem (ExpandBar parent, int style, int index) { super (parent, style); this.parent = parent; - int count = parent.getItemCount (); - if (!(0 <= index && index <= count)) { - error (SWT.ERROR_ITEM_NOT_ADDED); - } createWidget (index); } @@ -42,42 +44,107 @@ protected void checkSubclass () { } void createHandle (int index) { - state |= HANDLE; - handle = OS.gtk_expander_new (null); - if (handle == 0) error (SWT.ERROR_NO_HANDLES); - clientHandle = OS.g_object_new (display.gtk_fixed_get_type (), 0); - if (clientHandle == 0) error (SWT.ERROR_NO_HANDLES); - OS.gtk_container_add (handle, clientHandle); - boxHandle = OS.gtk_hbox_new (false, 4); - if (boxHandle == 0) error (SWT.ERROR_NO_HANDLES); - labelHandle = OS.gtk_label_new (null); - if (labelHandle == 0) error (SWT.ERROR_NO_HANDLES); - imageHandle = OS.gtk_image_new (); - if (imageHandle == 0) error (SWT.ERROR_NO_HANDLES); - OS.gtk_container_add (boxHandle, imageHandle); - OS.gtk_container_add (boxHandle, labelHandle); - OS.gtk_expander_set_label_widget (handle, boxHandle); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + state |= HANDLE; + handle = OS.gtk_expander_new (null); + if (handle == 0) error (SWT.ERROR_NO_HANDLES); + clientHandle = OS.g_object_new (display.gtk_fixed_get_type (), 0); + if (clientHandle == 0) error (SWT.ERROR_NO_HANDLES); + OS.gtk_container_add (handle, clientHandle); + boxHandle = OS.gtk_hbox_new (false, 4); + if (boxHandle == 0) error (SWT.ERROR_NO_HANDLES); + labelHandle = OS.gtk_label_new (null); + if (labelHandle == 0) error (SWT.ERROR_NO_HANDLES); + imageHandle = OS.gtk_image_new (); + if (imageHandle == 0) error (SWT.ERROR_NO_HANDLES); + OS.gtk_container_add (boxHandle, imageHandle); + OS.gtk_container_add (boxHandle, labelHandle); + OS.gtk_expander_set_label_widget (handle, boxHandle); + } } void createWidget (int index) { super.createWidget (index); showWidget (index); - parent.relayout (); + parent.createItem (this, style, index); } void deregister() { super.deregister(); - display.removeWidget (clientHandle); - display.removeWidget (boxHandle); - display.removeWidget (labelHandle); - display.removeWidget (imageHandle); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + display.removeWidget (clientHandle); + display.removeWidget (boxHandle); + display.removeWidget (labelHandle); + display.removeWidget (imageHandle); + } +} + +void destroyWidget () { + parent.destroyItem (this); + super.destroyWidget (); +} + +void drawChevron (GC gc, int x, int y) { + int [] polyline1, polyline2; + if (expanded) { + int px = x + 4 + 5; + int py = y + 4 + 7; + polyline1 = new int [] { + 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, + px+3,py-2, px+4,py-2, px+4,py-1, px+5,py-1, px+5,py, px+6,py}; + py += 4; + polyline2 = new int [] { + 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, + px+3,py-2, px+4,py-2, px+4,py-1, px+5,py-1, px+5,py, px+6,py}; + } else { + int px = x + 4 + 5; + int py = y + 4 + 4; + polyline1 = new int[] { + 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, + px+3,py+2, px+4,py+2, px+4,py+1, px+5,py+1, px+5,py, px+6,py}; + py += 4; + polyline2 = new int [] { + 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, + px+3,py+2, px+4,py+2, px+4,py+1, px+5,py+1, px+5,py, px+6,py}; + } + gc.setForeground (display.getSystemColor (SWT.COLOR_TITLE_FOREGROUND)); + gc.drawPolyline (polyline1); + gc.drawPolyline (polyline2); } -public void dispose () { - if (isDisposed ()) return; - ExpandBar parent = this.parent; - super.dispose (); - parent.relayout (); +void drawItem (GC gc, boolean drawFocus) { + int headerHeight = parent.getBandHeight (); + Display display = getDisplay (); + gc.setForeground (display.getSystemColor (SWT.COLOR_TITLE_BACKGROUND)); + gc.setBackground (display.getSystemColor (SWT.COLOR_TITLE_BACKGROUND_GRADIENT)); + gc.fillGradientRectangle (x, y, width, headerHeight, true); + if (expanded) { + gc.setForeground (display.getSystemColor (SWT.COLOR_TITLE_BACKGROUND_GRADIENT)); + gc.drawLine (x, y + headerHeight, x, y + headerHeight + height - 1); + gc.drawLine (x, y + headerHeight + height - 1, x + width - 1, y + headerHeight + height - 1); + gc.drawLine (x + width - 1, y + headerHeight + height - 1, x + width - 1, y + headerHeight); + } + int drawX = x; + if (image != null) { + drawX += ExpandItem.TEXT_INSET; + if (imageHeight > headerHeight) { + gc.drawImage (image, drawX, y + headerHeight - imageHeight); + } else { + gc.drawImage (image, drawX, y + (headerHeight - imageHeight) / 2); + } + drawX += imageWidth; + } + if (text.length() > 0) { + drawX += ExpandItem.TEXT_INSET; + Point size = gc.stringExtent (text); + gc.setForeground (parent.getForeground ()); + gc.drawString (text, drawX, y + (headerHeight - size.y) / 2, true); + } + int chevronSize = ExpandItem.CHEVRON_SIZE; + drawChevron (gc, x + width - chevronSize, y + (headerHeight - chevronSize) / 2); + if (drawFocus) { + gc.drawFocus (x + 1, y + 1, width - 2, headerHeight - 2); + } } public Control getControl () { @@ -87,28 +154,39 @@ public Control getControl () { public boolean getExpanded () { checkWidget (); - return OS.gtk_expander_get_expanded (handle); + return expanded; } public int getHeaderHeight () { checkWidget (); - int /*long*/ widget = OS.gtk_expander_get_label_widget (handle); - return OS.GTK_WIDGET_HEIGHT (widget); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + int /*long*/ widget = OS.gtk_expander_get_label_widget (handle); + return OS.GTK_WIDGET_HEIGHT (widget); + } + return Math.max (parent.getBandHeight (), imageHeight); } public int getHeight () { checkWidget (); - GtkRequisition requisition = new GtkRequisition (); - OS.gtk_widget_size_request (clientHandle, requisition); - return requisition.height; + return height; } public ExpandBar getParent () { checkWidget(); - if (parent == null) error (SWT.ERROR_WIDGET_DISPOSED); return parent; } +int getPreferredWidth (GC gc) { + int width = ExpandItem.TEXT_INSET * 2 + ExpandItem.CHEVRON_SIZE; + if (image != null) { + width += ExpandItem.TEXT_INSET + imageWidth; + } + if (text.length() > 0) { + width += gc.stringExtent (text).x; + } + return width; +} + int /*long*/ gtk_activate (int /*long*/ widget) { Event event = new Event (); event.item = this; @@ -118,35 +196,49 @@ int /*long*/ gtk_activate (int /*long*/ widget) { } int gtk_size_allocate (int widget, int allocation) { - parent.relayout (); + parent.layoutItems (0, false); return 0; } void hookEvents () { - super.hookEvents(); - OS.g_signal_connect_closure (handle, OS.activate, display.closures [ACTIVATE], false); - OS.g_signal_connect_closure (handle, OS.activate, display.closures [ACTIVATE_INVERSE], true); - OS.g_signal_connect_closure (clientHandle, OS.size_allocate, display.closures [SIZE_ALLOCATE], true); + super.hookEvents (); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + OS.g_signal_connect_closure (handle, OS.activate, display.closures [ACTIVATE], false); + OS.g_signal_connect_closure (handle, OS.activate, display.closures [ACTIVATE_INVERSE], true); + OS.g_signal_connect_closure (clientHandle, OS.size_allocate, display.closures [SIZE_ALLOCATE], true); + } } -void register() { - super.register(); - display.addWidget (clientHandle, this); - display.addWidget (boxHandle, this); - display.addWidget (labelHandle, this); - display.addWidget (imageHandle, this); +void redraw () { + if (OS.GTK_VERSION < OS.VERSION (2, 4, 0)) { + int headerHeight = parent.getBandHeight (); + if (imageHeight > headerHeight) { + parent.redraw (x + ExpandItem.TEXT_INSET, y + headerHeight - imageHeight, imageWidth, imageHeight, false); + } + parent.redraw (x, y, width, headerHeight + height, false); + } +} + +void register () { + super.register (); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + display.addWidget (clientHandle, this); + display.addWidget (boxHandle, this); + display.addWidget (labelHandle, this); + display.addWidget (imageHandle, this); + } } void releaseHandle () { super.releaseHandle (); clientHandle = boxHandle = labelHandle = imageHandle = 0; + parent = null; } void releaseWidget () { super.releaseWidget (); if (imageList != null) imageList.dispose (); imageList = null; - parent = null; control = null; } @@ -166,6 +258,28 @@ void resizeControl (int yScroll) { } } +void setBounds (int x, int y, int width, int height, boolean move, boolean size) { + redraw (); + int headerHeight = parent.getBandHeight (); + if (move) { + if (imageHeight > headerHeight) { + y += (imageHeight - headerHeight); + } + this.x = x; + this.y = y; + redraw (); + } + if (size) { + this.width = width; + this.height = height; + redraw (); + } + if (control != null && !control.isDisposed ()) { + if (move) control.setLocation (x + BORDER, y + headerHeight); + if (size) control.setSize (Math.max (0, width - 2 * BORDER), Math.max (0, height - BORDER)); + } +} + public void setControl (Control control) { checkWidget (); if (control != null) { @@ -175,15 +289,26 @@ public void setControl (Control control) { if (this.control == control) return; this.control = control; if (control != null) { - control.setVisible (OS.gtk_expander_get_expanded (handle)); + control.setVisible (expanded); + if (OS.GTK_VERSION < OS.VERSION (2, 4, 0)) { + int headerHeight = parent.getBandHeight (); + control.setBounds (x + BORDER, y + headerHeight, Math.max (0, width - 2 * BORDER), Math.max (0, height - BORDER)); + } + } + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + parent.layoutItems (0, true); } - parent.relayout (); } public void setExpanded (boolean expanded) { checkWidget (); - OS.gtk_expander_set_expanded (handle, expanded); - parent.relayout (); + this.expanded = expanded; + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + OS.gtk_expander_set_expanded (handle, expanded); + parent.layoutItems (0, true); + } else { + parent.showItem (parent.indexOf (this)); + } } void setFontDescription (int /*long*/ font) { @@ -198,51 +323,80 @@ void setForegroundColor (GdkColor color) { if (imageHandle != 0) OS.gtk_widget_modify_fg (imageHandle, OS.GTK_STATE_NORMAL, color); } -public void setImage (Image image) { - super.setImage (image); - if (imageList != null) imageList.dispose (); - imageList = null; - if (image != null) { - if (image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); - imageList = new ImageList (); - int imageIndex = imageList.add (image); - int /*long*/ pixbuf = imageList.getPixbuf (imageIndex); - OS.gtk_image_set_from_pixbuf (imageHandle, pixbuf); - if (text.length () == 0) OS.gtk_widget_hide (labelHandle); - OS.gtk_widget_show (imageHandle); +public void setHeight (int height) { + checkWidget (); + if (height < 0) return; + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + this.height = height; + OS.gtk_widget_set_size_request (clientHandle, -1, height); + parent.layoutItems (0, false); } else { - OS.gtk_image_set_from_pixbuf (imageHandle, 0); - OS.gtk_widget_show (labelHandle); - OS.gtk_widget_hide (imageHandle); + setBounds (0, 0, width, height, false, true); + if (expanded) parent.layoutItems (parent.indexOf (this) + 1, true); } } -public void setHeight (int height) { - checkWidget (); - if (height < 0) return; - OS.gtk_widget_set_size_request (clientHandle, -1, height); - parent.relayout (); +public void setImage (Image image) { + super.setImage (image); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + if (imageList != null) imageList.dispose (); + imageList = null; + if (image != null) { + if (image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); + imageList = new ImageList (); + int imageIndex = imageList.add (image); + int /*long*/ pixbuf = imageList.getPixbuf (imageIndex); + OS.gtk_image_set_from_pixbuf (imageHandle, pixbuf); + if (text.length () == 0) OS.gtk_widget_hide (labelHandle); + OS.gtk_widget_show (imageHandle); + } else { + OS.gtk_image_set_from_pixbuf (imageHandle, 0); + OS.gtk_widget_show (labelHandle); + OS.gtk_widget_hide (imageHandle); + } + } else { + int oldImageHeight = imageHeight; + if (image != null) { + Rectangle bounds = image.getBounds (); + imageHeight = bounds.height; + imageWidth = bounds.width; + } else { + imageHeight = imageWidth = 0; + } + if (oldImageHeight != imageHeight) { + parent.layoutItems (parent.indexOf (this), true); + } else { + redraw (); + } + } } public void setText (String string) { super.setText (string); - byte [] buffer = Converter.wcsToMbcs (null, string, true); - OS.gtk_label_set_text (labelHandle, buffer); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + byte [] buffer = Converter.wcsToMbcs (null, string, true); + OS.gtk_label_set_text (labelHandle, buffer); + } else { + redraw (); + } } void showWidget (int index) { - OS.gtk_widget_show (handle); - OS.gtk_widget_show (clientHandle); - OS.gtk_container_add (parent.handle, handle); - OS.gtk_box_set_child_packing (parent.handle, handle, false, false, 0, OS.GTK_PACK_START); - if (boxHandle != 0) OS.gtk_widget_show (boxHandle); - if (labelHandle != 0) OS.gtk_widget_show (labelHandle); + if (OS.GTK_VERSION >= OS.VERSION (2, 4, 0)) { + OS.gtk_widget_show (handle); + OS.gtk_widget_show (clientHandle); + OS.gtk_container_add (parent.handle, handle); + OS.gtk_box_set_child_packing (parent.handle, handle, false, false, 0, OS.GTK_PACK_START); + if (boxHandle != 0) OS.gtk_widget_show (boxHandle); + if (labelHandle != 0) OS.gtk_widget_show (labelHandle); + } } int /*long*/ windowProc (int /*long*/ handle, int /*long*/ user_data) { switch ((int)/*64*/user_data) { case ACTIVATE_INVERSE: { - parent.relayout (); + expanded = OS.gtk_expander_get_expanded (handle); + parent.layoutItems (0, false); return 0; } } |