1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet218" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2004 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * Port to the D programming language: 46 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) 47 *******************************************************************************/ 48 49 module org.eclipse.swt.snippets.Snippet218; 50 /* 51 * SWT StyledText snippet: use gradient background. 52 * 53 * For a list of all SWT example snippets see 54 * http://www.eclipse.org/swt/snippets/ 55 * 56 * @since 3.2 57 */ 58 59 import org.eclipse.swt.SWT; 60 import org.eclipse.swt.custom.StyledText; 61 import org.eclipse.swt.custom.StyleRange; 62 import org.eclipse.swt.layout.FillLayout; 63 import org.eclipse.swt.widgets.Display; 64 import org.eclipse.swt.widgets.Shell; 65 import org.eclipse.swt.widgets.Event; 66 import org.eclipse.swt.widgets.Listener; 67 import org.eclipse.swt.graphics.FontData; 68 import org.eclipse.swt.graphics.Font; 69 import org.eclipse.swt.graphics.Rectangle; 70 import org.eclipse.swt.graphics.GC; 71 import org.eclipse.swt.graphics.Image; 72 73 import java.lang.all; 74 75 version(Tango){ 76 import Math = tango.math.Math; 77 } else { // Phobos 78 import Math = std.algorithm; 79 } 80 81 version(JIVE){ 82 import jive.stacktrace; 83 } 84 85 void main() { 86 static String text = "Plans do not materialize out of nowhere, nor are they entirely static. To ensure the planning process is " ~ 87 "transparent and open to the entire Eclipse community, we (the Eclipse PMC) post plans in an embryonic " ~ 88 "form and revise them throughout the release cycle. \n" ~ 89 "The first part of the plan deals with the important matters of release deliverables, release milestones, target " ~ 90 "operating environments, and release-to-release compatibility. These are all things that need to be clear for " ~ 91 "any release, even if no features were to change. \n"; 92 static Image oldImage; 93 94 95 Display display = new Display(); 96 Shell shell = new Shell(display); 97 shell.setLayout(new FillLayout()); 98 StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); 99 styledText.setText(text); 100 FontData data = display.getSystemFont().getFontData()[0]; 101 Font font = new Font(display, data.getName(), 16f, SWT.BOLD); 102 styledText.setFont(font); 103 styledText.setForeground(display.getSystemColor (SWT.COLOR_BLUE)); 104 105 void onResize (Event event) { 106 Rectangle rect = styledText.getClientArea (); 107 Image newImage = new Image (display, 1, Math.max (1, rect.height)); 108 GC gc = new GC (newImage); 109 gc.setForeground (display.getSystemColor (SWT.COLOR_WHITE)); 110 gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW)); 111 gc.fillGradientRectangle (rect.x, rect.y, 1, rect.height, true); 112 gc.dispose (); 113 styledText.setBackgroundImage (newImage); 114 if (oldImage !is null) oldImage.dispose (); 115 oldImage = newImage; 116 } 117 118 styledText.addListener (SWT.Resize, dgListener(&onResize)); 119 120 shell.setSize(700, 400); 121 shell.open(); 122 while (!shell.isDisposed()) { 123 if (!display.readAndDispatch()) 124 display.sleep(); 125 } 126 if (oldImage !is null) oldImage.dispose (); 127 font.dispose(); 128 display.dispose(); 129 }