Mercurial > projects > dwt-linux
annotate dwt/dnd/TreeDragSourceEffect.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | ce446666f5a2 |
children |
rev | line source |
---|---|
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
92 | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
92 | 12 *******************************************************************************/ |
13 module dwt.dnd.TreeDragSourceEffect; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.graphics.Image; | |
18 import dwt.internal.gtk.OS; | |
19 import dwt.widgets.Display; | |
20 import dwt.widgets.Tree; | |
21 import dwt.dnd.DragSourceEffect; | |
22 import dwt.dnd.DragSourceEvent; | |
23 | |
24 import Math = tango.math.Math; | |
25 | |
26 /** | |
27 * This class provides default implementations to display a source image | |
28 * when a drag is initiated from a <code>Tree</code>. | |
29 * | |
30 * <p>Classes that wish to provide their own source image for a <code>Tree</code> can | |
31 * extend <code>TreeDragSourceEffect</code> class and override the <code>TreeDragSourceEffect.dragStart</code> | |
32 * method and set the field <code>DragSourceEvent.image</code> with their own image.</p> | |
33 * | |
34 * Subclasses that override any methods of this class must call the corresponding | |
35 * <code>super</code> method to get the default drag under effect implementation. | |
36 * | |
37 * @see DragSourceEffect | |
38 * @see DragSourceEvent | |
259 | 39 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
92 | 40 * |
41 * @since 3.3 | |
42 */ | |
43 public class TreeDragSourceEffect : DragSourceEffect { | |
44 Image dragSourceImage = null; | |
45 | |
46 /** | |
47 * Creates a new <code>TreeDragSourceEffect</code> to handle drag effect | |
48 * from the specified <code>Tree</code>. | |
49 * | |
50 * @param tree the <code>Tree</code> that the user clicks on to initiate the drag | |
51 */ | |
52 public this(Tree tree) { | |
53 super(tree); | |
54 } | |
55 | |
56 /** | |
57 * This implementation of <code>dragFinished</code> disposes the image | |
58 * that was created in <code>TreeDragSourceEffect.dragStart</code>. | |
59 * | |
60 * Subclasses that override this method should call <code>super.dragFinished(event)</code> | |
61 * to dispose the image in the default implementation. | |
62 * | |
63 * @param event the information associated with the drag finished event | |
64 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
65 public override void dragFinished(DragSourceEvent event) { |
92 | 66 if (dragSourceImage !is null) dragSourceImage.dispose(); |
67 dragSourceImage = null; | |
68 } | |
69 | |
70 /** | |
71 * This implementation of <code>dragStart</code> will create a default | |
72 * image that will be used during the drag. The image should be disposed | |
73 * when the drag is completed in the <code>TreeDragSourceEffect.dragFinished</code> | |
74 * method. | |
75 * | |
76 * Subclasses that override this method should call <code>super.dragStart(event)</code> | |
77 * to use the image from the default implementation. | |
78 * | |
79 * @param event the information associated with the drag start event | |
80 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
81 public override void dragStart(DragSourceEvent event) { |
92 | 82 event.image = getDragSourceImage(event); |
83 } | |
84 | |
85 Image getDragSourceImage(DragSourceEvent event) { | |
86 if (dragSourceImage !is null) dragSourceImage.dispose(); | |
87 dragSourceImage = null; | |
88 | |
89 Tree tree = cast(Tree) control; | |
90 if (OS.GTK_VERSION < OS.buildVERSION (2, 2, 0)) return null; | |
240 | 91 //TEMPORARY CODE |
92 if (tree.isListening(DWT.EraseItem) || tree.isListening (DWT.PaintItem)) return null; | |
92 | 93 /* |
94 * Bug in GTK. gtk_tree_selection_get_selected_rows() segmentation faults | |
95 * in versions smaller than 2.2.4 if the model is NULL. The fix is | |
96 * to give a valid pointer instead. | |
97 */ | |
98 auto handle = tree.handle; | |
99 auto selection = OS.gtk_tree_view_get_selection (handle); | |
100 int dummy; | |
101 void* model = OS.GTK_VERSION < OS.buildVERSION (2, 2, 4) ? &dummy : null; | |
102 auto list = OS.gtk_tree_selection_get_selected_rows (selection, &model); | |
103 if (list is null) return null; | |
104 int count = Math.min(10, OS.g_list_length (list)); | |
105 | |
106 Display display = tree.getDisplay(); | |
107 if (count is 1) { | |
108 auto path = OS.g_list_nth_data (list, 0); | |
109 auto pixmap = OS.gtk_tree_view_create_row_drag_icon(handle, path); | |
110 dragSourceImage = Image.gtk_new(display, DWT.ICON, pixmap, null); | |
111 } else { | |
112 int width = 0, height = 0; | |
113 int w , h; | |
114 int[] yy = new int[count], hh = new int[count]; | |
115 GdkDrawable* [] pixmaps = new GdkDrawable*[count]; | |
116 GdkRectangle rect; | |
117 for (int i=0; i<count; i++) { | |
118 auto path = OS.g_list_nth_data (list, i); | |
119 OS.gtk_tree_view_get_cell_area (handle, path, null, &rect); | |
120 pixmaps[i] = OS.gtk_tree_view_create_row_drag_icon(handle, path); | |
121 OS.gdk_drawable_get_size(pixmaps[i], &w, &h); | |
122 width = Math.max(width, w); | |
123 height = rect.y + h - yy[0]; | |
124 yy[i] = rect.y; | |
125 hh[i] = h; | |
126 } | |
127 auto source = OS.gdk_pixmap_new(OS.GDK_ROOT_PARENT(), width, height, -1); | |
128 auto gcSource = OS.gdk_gc_new(source); | |
129 auto mask = OS.gdk_pixmap_new(OS.GDK_ROOT_PARENT(), width, height, 1); | |
130 auto gcMask = OS.gdk_gc_new(mask); | |
131 GdkColor color; | |
132 color.pixel = 0; | |
133 OS.gdk_gc_set_foreground(gcMask, &color); | |
134 OS.gdk_draw_rectangle(mask, gcMask, 1, 0, 0, width, height); | |
135 color.pixel = 1; | |
136 OS.gdk_gc_set_foreground(gcMask, &color); | |
137 for (int i=0; i<count; i++) { | |
138 OS.gdk_draw_drawable(source, gcSource, pixmaps[i], 0, 0, 0, yy[i] - yy[0], -1, -1); | |
139 OS.gdk_draw_rectangle(mask, gcMask, 1, 0, yy[i] - yy[0], width, hh[i]); | |
140 OS.g_object_unref(pixmaps[i]); | |
141 } | |
142 OS.g_object_unref(gcSource); | |
143 OS.g_object_unref(gcMask); | |
144 dragSourceImage = Image.gtk_new(display, DWT.ICON, source, mask); | |
145 } | |
146 OS.g_list_free (list); | |
147 | |
148 return dragSourceImage; | |
149 } | |
150 } |