package org.eclipse.swt.widgets; /* * Copyright (c) 2000, 2002 IBM Corp. All rights reserved. * This file is made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html */ import org.eclipse.swt.*; import org.eclipse.swt.internal.gtk.*; import org.eclipse.swt.graphics.*; /** * This class is the abstract superclass of all classes which * represent controls that have standard scroll bars. *
*
Styles:
*
H_SCROLL, V_SCROLL
*
Events: *
(none)
*
*

* IMPORTANT: This class is intended to be subclassed only * within the SWT implementation. *

*/ public abstract class Scrollable extends Control { int scrolledHandle; ScrollBar horizontalBar, verticalBar; /** * Prevents uninitialized instances from being created outside the package. */ Scrollable () {} /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. *

* The style value is either one of the style constants defined in * class SWT which is applicable to instances of this * class, or must be built by bitwise OR'ing together * (that is, using the int "|" operator) two or more * of those SWT style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. *

* * @param parent a composite control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * * @exception IllegalArgumentException * @exception SWTException * * @see SWT#H_SCROLL * @see SWT#V_SCROLL * @see Widget#checkSubclass * @see Widget#getStyle */ public Scrollable (Composite parent, int style) { super (parent, style); } int clientHandle () { return handle; } /** * Given a desired client area for the receiver * (as described by the arguments), returns the bounding * rectangle which would be required to produce that client * area. *

* In other words, it returns a rectangle such that, if the * receiver's bounds were set to that rectangle, the area * of the receiver which is capable of displaying data * (that is, not covered by the "trimmings") would be the * rectangle described by the arguments (relative to the * receiver's parent). *

* * @return the required bounds to produce the given client area * * @exception SWTException * * @see #getClientArea */ public Rectangle computeTrim (int x, int y, int width, int height) { checkWidget(); int border = getBorderWidth (); int trimX = x - border, trimY = y - border; int trimWidth = width + (border * 2), trimHeight = height + (border * 2); trimHeight += hScrollBarWidth (); trimWidth += vScrollBarWidth (); if (scrolledHandle != 0) { if (OS.gtk_scrolled_window_get_shadow_type (scrolledHandle) != OS.GTK_SHADOW_NONE) { GtkStyle style = new GtkStyle (); OS.memmove (style, OS.gtk_widget_get_style (scrolledHandle)); trimX -= style.xthickness; trimY -= style.ythickness; trimWidth += style.xthickness * 2; trimHeight += style.ythickness * 2; } } return new Rectangle (trimX, trimY, trimWidth, trimHeight); } ScrollBar createScrollBar (int style) { if (scrolledHandle == 0) return null; ScrollBar bar = new ScrollBar (); bar.parent = this; bar.style = style; bar.state |= HANDLE; if ((style & SWT.H_SCROLL) != 0) { bar.handle = OS.gtk_scrolled_window_get_hadjustment (scrolledHandle); } else { bar.handle = OS.gtk_scrolled_window_get_vadjustment (scrolledHandle); } bar.hookEvents (); bar.register (); return bar; } void createWidget (int index) { super.createWidget (index); if ((style & SWT.H_SCROLL) != 0) horizontalBar = createScrollBar (SWT.H_SCROLL); if ((style & SWT.V_SCROLL) != 0) verticalBar = createScrollBar (SWT.V_SCROLL); } void deregister () { super.deregister (); if (scrolledHandle != 0) WidgetTable.remove (scrolledHandle); } /** * Returns a rectangle which describes the area of the * receiver which is capable of displaying data (that is, * not covered by the "trimmings"). * * @return the client area * * @exception SWTException * * @see #computeTrim */ public Rectangle getClientArea () { checkWidget (); //FIXME - List, Table, Tree, ... int clientHandle = clientHandle (); int width = OS.GTK_WIDGET_WIDTH (clientHandle); int height = OS.GTK_WIDGET_HEIGHT (clientHandle); if ((state & CANVAS) != 0) { return new Rectangle (0, 0, width, height); } int x = OS.GTK_WIDGET_X (clientHandle); int y = OS.GTK_WIDGET_Y (clientHandle); return new Rectangle (x, y, width, height); } /** * Returns the receiver's horizontal scroll bar if it has * one, and null if it does not. * * @return the horizontal scroll bar (or null) * * @exception SWTException */ public ScrollBar getHorizontalBar () { checkWidget (); return horizontalBar; } /** * Returns the receiver's vertical scroll bar if it has * one, and null if it does not. * * @return the vertical scroll bar (or null) * * @exception SWTException */ public ScrollBar getVerticalBar () { checkWidget (); return verticalBar; } int hScrollBarWidth() { if (horizontalBar==null) return 0; int hBarHandle = OS.GTK_SCROLLED_WINDOW_HSCROLLBAR(scrolledHandle); if (hBarHandle==0) return 0; GtkRequisition requisition = new GtkRequisition(); OS.gtk_widget_size_request(hBarHandle, requisition); int spacing = OS.GTK_SCROLLED_WINDOW_SCROLLBAR_SPACING(scrolledHandle); return requisition.height + spacing; } boolean isTabGroup() { if ((state & CANVAS) != 0) return true; return super.isTabGroup(); } void register () { super.register (); if (scrolledHandle != 0) WidgetTable.put (scrolledHandle, this); } void releaseHandle () { super.releaseHandle (); scrolledHandle = 0; } void releaseWidget () { if (horizontalBar != null) horizontalBar.releaseResources (); if (verticalBar != null) verticalBar.releaseResources (); horizontalBar = verticalBar = null; super.releaseWidget (); } void resizeHandle (int width, int height) { if (fixedHandle != 0) { OS.gtk_widget_set_size_request (fixedHandle, width, height); } /* * Feature in GTK. Some widgets do not allocate the size * of their internal children in gtk_widget_size_allocate(). * Instead this is done in gtk_widget_size_request(). This * means that the client area of the widget is not correct. * The fix is to call gtk_widget_size_request() (and throw * the results away). * * Note: The following widgets rely on this feature: * GtkScrolledWindow * GtkNotebook * GtkFrame * GtkCombo */ GtkRequisition requisition = new GtkRequisition (); if (scrolledHandle != 0) { OS.gtk_widget_set_size_request (scrolledHandle, width, height); OS.gtk_widget_size_request (scrolledHandle, requisition); } else { OS.gtk_widget_set_size_request (handle, width, height); OS.gtk_widget_size_request (handle, requisition); } } int topHandle () { if (fixedHandle != 0) return fixedHandle; if (scrolledHandle != 0) return scrolledHandle; return super.topHandle (); } int vScrollBarWidth() { if (verticalBar==null) return 0; int vBarHandle = OS.GTK_SCROLLED_WINDOW_VSCROLLBAR(scrolledHandle); if (vBarHandle==0) return 0; GtkRequisition requisition = new GtkRequisition(); OS.gtk_widget_size_request(vBarHandle, requisition); int spacing = OS.GTK_SCROLLED_WINDOW_SCROLLBAR_SPACING(scrolledHandle); return requisition.width + spacing; } }