# HG changeset patch # User Frank Benoit # Date 1200424487 -3600 # Node ID feee0eb2824cf8f0a14c35e08ac39ea99d15285e # Parent c098425dc1a37b6b5eedec357b5ddc33d47e877a Sash diff -r c098425dc1a3 -r feee0eb2824c dwt/widgets/Sash.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/widgets/Sash.d Tue Jan 15 20:14:47 2008 +0100 @@ -0,0 +1,429 @@ +/******************************************************************************* + * Copyright (c) 2000, 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +module dwt.widgets.Sash; + + + +import dwt.DWT; +import dwt.DWTException; +import dwt.events.SelectionEvent; +import dwt.events.SelectionListener; +import dwt.graphics.Point; +import dwt.internal.gtk.OS; + +import dwt.widgets.Control; +import dwt.widgets.Composite; +import dwt.widgets.TypedListener; +import dwt.widgets.Event; + +import Math = tango.math.Math; +/** + * Instances of the receiver represent a selectable user interface object + * that allows the user to drag a rubber banded outline of the sash within + * the parent control. + *
+ *
Styles:
+ *
HORIZONTAL, VERTICAL, SMOOTH
+ *
Events:
+ *
Selection
+ *
+ *

+ * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified. + *

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

+ */ +public class Sash : Control { + bool dragging; + int startX, startY, lastX, lastY; + GtkWidget* defaultCursor; + + private final static int INCREMENT = 1; + private final static int PAGE_INCREMENT = 9; + +/** + * 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 DWT 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 DWT 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 DWTException + * + * @see DWT#HORIZONTAL + * @see DWT#VERTICAL + * @see Widget#checkSubclass + * @see Widget#getStyle + */ +public this (Composite parent, int style) { + super (parent, checkStyle (style)); +} + +/** + * Adds the listener to the collection of listeners who will + * be notified when the control is selected by the user, by sending + * it one of the messages defined in the SelectionListener + * interface. + *

+ * When widgetSelected is called, the x, y, width, and height fields of the event object are valid. + * If the receiver is being dragged, the event object detail field contains the value DWT.DRAG. + * widgetDefaultSelected is not called. + *

+ * + * @param listener the listener which should be notified when the control is selected by the user + * + * @exception IllegalArgumentException + * @exception DWTException + * + * @see SelectionListener + * @see #removeSelectionListener + * @see SelectionEvent + */ +public void addSelectionListener (SelectionListener listener) { + checkWidget (); + if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); + TypedListener typedListener = new TypedListener (listener); + addListener (DWT.Selection,typedListener); + addListener (DWT.DefaultSelection,typedListener); +} + +static int checkStyle (int style) { + return checkBits (style, DWT.HORIZONTAL, DWT.VERTICAL, 0, 0, 0, 0); +} + +public Point computeSize (int wHint, int hHint, bool changed) { + checkWidget (); + if (wHint !is DWT.DEFAULT && wHint < 0) wHint = 0; + if (hHint !is DWT.DEFAULT && hHint < 0) hHint = 0; + int border = getBorderWidth (); + int width = border * 2, height = border * 2; + if ((style & DWT.HORIZONTAL) !is 0) { + width += DEFAULT_WIDTH; height += 3; + } else { + width += 3; height += DEFAULT_HEIGHT; + } + if (wHint !is DWT.DEFAULT) width = wHint + (border * 2); + if (hHint !is DWT.DEFAULT) height = hHint + (border * 2); + return new Point (width, height); +} + +void createHandle (int index) { + state |= HANDLE | THEME_BACKGROUND; + handle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); + if (handle is null) error (DWT.ERROR_NO_HANDLES); + OS.gtk_fixed_set_has_window (handle, true); + OS.GTK_WIDGET_SET_FLAGS (handle, OS.GTK_CAN_FOCUS); + int type = (style & DWT.VERTICAL) !is 0 ? OS.GDK_SB_H_DOUBLE_ARROW : OS.GDK_SB_V_DOUBLE_ARROW; + defaultCursor = cast(GtkWidget*)OS.gdk_cursor_new (type); +} + +void drawBand (int x, int y, int width, int height) { + if ((style & DWT.SMOOTH) !is 0) return; + auto window = OS.GTK_WIDGET_WINDOW (parent.paintHandle()); + if (window is null) return; + char [] bits = cast(char[])[ cast(byte)-86, 85, -86, 85, -86, 85, -86, 85 ]; + auto stipplePixmap = OS.gdk_bitmap_create_from_data (cast(GdkDrawable*)window, bits.ptr, 8, 8); + auto gc = OS.gdk_gc_new (window); + auto colormap = OS.gdk_colormap_get_system(); + GdkColor color; + OS.gdk_color_white (colormap, &color); + OS.gdk_gc_set_foreground (gc, &color); + OS.gdk_gc_set_stipple (gc, stipplePixmap); + OS.gdk_gc_set_subwindow (gc, OS.GDK_INCLUDE_INFERIORS); + OS.gdk_gc_set_fill (gc, OS.GDK_STIPPLED); + OS.gdk_gc_set_function (gc, OS.GDK_XOR); + OS.gdk_draw_rectangle (window, gc, 1, x, y, width, height); + OS.g_object_unref (stipplePixmap); + OS.g_object_unref (gc); +} + +override int gtk_button_press_event (GtkWidget* widget, GdkEventButton* gdkEvent) { + auto result = super.gtk_button_press_event (widget, gdkEvent); + if (result !is 0) return result; + int button = gdkEvent.button; + if (button !is 1) return 0; + if (gdkEvent.type is OS.GDK_2BUTTON_PRESS) return 0; + if (gdkEvent.type is OS.GDK_3BUTTON_PRESS) return 0; + auto window = OS.GTK_WIDGET_WINDOW (widget); + int origin_x, origin_y; + OS.gdk_window_get_origin (window, &origin_x, &origin_y); + startX = cast(int) (gdkEvent.x_root - origin_x ); + startY = cast(int) (gdkEvent.y_root - origin_y ); + int x = OS.GTK_WIDGET_X (handle); + int y = OS.GTK_WIDGET_Y (handle); + int width = OS.GTK_WIDGET_WIDTH (handle); + int height = OS.GTK_WIDGET_HEIGHT (handle); + lastX = x; + lastY = y; + Event event = new Event (); + event.time = gdkEvent.time; + event.x = lastX; + event.y = lastY; + event.width = width; + event.height = height; + if ((style & DWT.SMOOTH) is 0) { + event.detail = DWT.DRAG; + } + sendEvent (DWT.Selection, event); + if (isDisposed ()) return 0; + if (event.doit) { + dragging = true; + lastX = event.x; + lastY = event.y; + parent.update (true, (style & DWT.SMOOTH) is 0); + drawBand (event.x, event.y, width, height); + if ((style & DWT.SMOOTH) !is 0) { + setBounds (event.x, event.y, width, height); + // widget could be disposed at this point + } + } + return result; +} + +override int gtk_button_release_event (GtkWidget* widget, GdkEventButton* gdkEvent) { + auto result = super.gtk_button_release_event (widget, gdkEvent); + if (result !is 0) return result; + int button = gdkEvent.button; + if (button !is 1) return 0; + if (!dragging) return 0; + dragging = false; + int width = OS.GTK_WIDGET_WIDTH (handle); + int height = OS.GTK_WIDGET_HEIGHT (handle); + Event event = new Event (); + event.time = gdkEvent.time; + event.x = lastX; + event.y = lastY; + event.width = width; + event.height = height; + drawBand (lastX, lastY, width, height); + sendEvent (DWT.Selection, event); + if (isDisposed ()) return result; + if (event.doit) { + if ((style & DWT.SMOOTH) !is 0) { + setBounds (event.x, event.y, width, height); + // widget could be disposed at this point + } + } + return result; +} + +override int gtk_focus_in_event (GtkWidget* widget, GdkEventFocus* event) { + auto result = super.gtk_focus_in_event (widget, event); + if (result !is 0) return result; + // widget could be disposed at this point + if (handle !is null) { + lastX = OS.GTK_WIDGET_X (handle); + lastY = OS.GTK_WIDGET_Y (handle); + } + return 0; +} + +override int gtk_key_press_event (GtkWidget* widget, GdkEventKey* gdkEvent) { + auto result = super.gtk_key_press_event (widget, gdkEvent); + if (result !is 0) return result; + int keyval = gdkEvent.keyval; + switch (keyval) { + case OS.GDK_Left: + case OS.GDK_Right: + case OS.GDK_Up: + case OS.GDK_Down: + int xChange = 0, yChange = 0; + int stepSize = PAGE_INCREMENT; + if ((gdkEvent.state & OS.GDK_CONTROL_MASK) !is 0) stepSize = INCREMENT; + if ((style & DWT.VERTICAL) !is 0) { + if (keyval is OS.GDK_Up || keyval is OS.GDK_Down) break; + xChange = keyval is OS.GDK_Left ? -stepSize : stepSize; + } else { + if (keyval is OS.GDK_Left ||keyval is OS.GDK_Right) break; + yChange = keyval is OS.GDK_Up ? -stepSize : stepSize; + } + + int width = OS.GTK_WIDGET_WIDTH (handle); + int height = OS.GTK_WIDGET_HEIGHT (handle); + int parentBorder = 0; + int parentWidth = OS.GTK_WIDGET_WIDTH (parent.handle); + int parentHeight = OS.GTK_WIDGET_HEIGHT (parent.handle); + int newX = lastX, newY = lastY; + if ((style & DWT.VERTICAL) !is 0) { + newX = Math.min (Math.max (0, lastX + xChange - parentBorder - startX), parentWidth - width); + } else { + newY = Math.min (Math.max (0, lastY + yChange - parentBorder - startY), parentHeight - height); + } + if (newX is lastX && newY is lastY) return result; + + /* Ensure that the pointer image does not change */ + auto window = OS.GTK_WIDGET_WINDOW (handle); + int grabMask = OS.GDK_POINTER_MOTION_MASK | OS.GDK_BUTTON_RELEASE_MASK; + auto gdkCursor = cursor !is null ? cursor.handle : cast(GdkCursor*)defaultCursor; + int ptrGrabResult = OS.gdk_pointer_grab (window, false, grabMask, window, gdkCursor, OS.GDK_CURRENT_TIME); + + /* The event must be sent because its doit flag is used. */ + Event event = new Event (); + event.time = gdkEvent.time; + event.x = newX; + event.y = newY; + event.width = width; + event.height = height; + sendEvent (DWT.Selection, event); + if (ptrGrabResult is OS.GDK_GRAB_SUCCESS) OS.gdk_pointer_ungrab (OS.GDK_CURRENT_TIME); + if (isDisposed ()) break; + + if (event.doit) { + lastX = event.x; + lastY = event.y; + if ((style & DWT.SMOOTH) !is 0) { + setBounds (event.x, event.y, width, height); + if (isDisposed ()) break; + } + int cursorX = event.x, cursorY = event.y; + if ((style & DWT.VERTICAL) !is 0) { + cursorY += height / 2; + } else { + cursorX += width / 2; + } + display.setCursorLocation (parent.toDisplay (cursorX, cursorY)); + } + break; + } + + return result; +} + +override int gtk_motion_notify_event (GtkWidget* widget, GdkEventMotion* gdkEvent) { + auto result = super.gtk_motion_notify_event (widget, gdkEvent); + if (result !is 0) return result; + if (!dragging) return 0; + int eventX, eventY, eventState; + if (gdkEvent.is_hint !is 0) { + int pointer_x, pointer_y, mask; + OS.gdk_window_get_pointer (gdkEvent.window, &pointer_x, &pointer_y, &mask); + eventX = pointer_x ; + eventY = pointer_y ; + eventState = mask ; + } else { + int origin_x, origin_y; + OS.gdk_window_get_origin (gdkEvent.window, &origin_x, &origin_y); + eventX = cast(int) (gdkEvent.x_root - origin_x ); + eventY = cast(int) (gdkEvent.y_root - origin_y ); + eventState = gdkEvent.state; + } + if ((eventState & OS.GDK_BUTTON1_MASK) is 0) return 0; + int x = OS.GTK_WIDGET_X (handle); + int y = OS.GTK_WIDGET_Y (handle); + int width = OS.GTK_WIDGET_WIDTH (handle); + int height = OS.GTK_WIDGET_HEIGHT (handle); + int parentBorder = 0; + int parentWidth = OS.GTK_WIDGET_WIDTH (parent.handle); + int parentHeight = OS.GTK_WIDGET_HEIGHT (parent.handle); + int newX = lastX, newY = lastY; + if ((style & DWT.VERTICAL) !is 0) { + newX = Math.min (Math.max (0, eventX + x - startX - parentBorder), parentWidth - width); + } else { + newY = Math.min (Math.max (0, eventY + y - startY - parentBorder), parentHeight - height); + } + if (newX is lastX && newY is lastY) return 0; + drawBand (lastX, lastY, width, height); + + Event event = new Event (); + event.time = gdkEvent.time; + event.x = newX; + event.y = newY; + event.width = width; + event.height = height; + if ((style & DWT.SMOOTH) is 0) { + event.detail = DWT.DRAG; + } + sendEvent (DWT.Selection, event); + if (isDisposed ()) return 0; + if (event.doit) { + lastX = event.x; + lastY = event.y; + } + parent.update (true, (style & DWT.SMOOTH) is 0); + drawBand (lastX, lastY, width, height); + if ((style & DWT.SMOOTH) !is 0) { + setBounds (lastX, lastY, width, height); + // widget could be disposed at this point + } + return result; +} + +override int gtk_realize (GtkWidget* widget) { + setCursor (cursor !is null ? cursor.handle : null); + return super.gtk_realize (widget); +} + +void hookEvents () { + super.hookEvents (); + OS.gtk_widget_add_events (handle, OS.GDK_POINTER_MOTION_HINT_MASK); +} + +void releaseWidget () { + super.releaseWidget (); + if (defaultCursor !is null) OS.gdk_cursor_destroy (cast(GdkCursor*)defaultCursor); + defaultCursor = null; +} + +/** + * Removes the listener from the collection of listeners who will + * be notified when the control is selected by the user. + * + * @param listener the listener which should no longer be notified + * + * @exception IllegalArgumentException + * @exception DWTException + * + * @see SelectionListener + * @see #addSelectionListener + */ +public void removeSelectionListener(SelectionListener listener) { + checkWidget(); + if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); + if (eventTable is null) return; + eventTable.unhook (DWT.Selection, listener); + eventTable.unhook (DWT.DefaultSelection,listener); +} + +void setCursor (GdkCursor* cursor) { + super.setCursor (cursor !is null ? cursor : cast(GdkCursor*)defaultCursor); +} + +int traversalCode (int key, GdkEventKey event) { + return 0; +} + +}