# HG changeset patch # User Thomas Graber # Date 1203728811 -3600 # Node ID f9c0133912fcc4b1a26e0b8f85a2a94b883fe602 # Parent 5c244d50e532072cd78464a07d2628666e04b783 adding texteditor example diff -r 5c244d50e532 -r f9c0133912fc dsss.conf --- a/dsss.conf Sat Feb 23 00:57:51 2008 +0100 +++ b/dsss.conf Sat Feb 23 02:06:51 2008 +0100 @@ -54,3 +54,10 @@ } buildflags+=-Jdwtexamples/controlexample buildflags+=-version=CUSTOM_CONTROL_EXAMPLE_MAIN + +[dwtexamples/texteditor/TextEditor.d] +buildflags+=-g -gc -debug +version(Windows){ + buildflags+= -L/SUBSYSTEM:console:5 +} +buildflags+=-Jdwtexamples/texteditor diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/Images.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtexamples/texteditor/Images.d Sat Feb 23 02:06:51 2008 +0100 @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2000, 2005 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 + * Port to the D programming language: + * Thomas Graber + *******************************************************************************/ +module dwtexamples.texteditor.Images; + +import dwt.dwthelper.InputStream; + +import dwt.graphics.Image; +import dwt.graphics.ImageData; +import dwt.widgets.Display; +import dwt.dwthelper.ByteArrayInputStream; + +import tango.core.Exception; +import tango.io.Stdout; + +public class Images { + + // Bitmap Images + public Image Bold; + public Image Italic; + public Image Underline; + public Image Strikeout; + public Image Red; + public Image Green; + public Image Blue; + public Image Erase; + + Image[] AllBitmaps; + + this () { + } + + public void freeAll () { + for (int i=0; i + *******************************************************************************/ +module dwtexamples.texteditor.TextEditor; + +import dwt.DWT; +import dwt.custom.ExtendedModifyEvent; +import dwt.custom.ExtendedModifyListener; +import dwt.custom.StyleRange; +import dwt.custom.StyledText; +import dwt.events.DisposeEvent; +import dwt.events.DisposeListener; +import dwt.events.SelectionAdapter; +import dwt.events.SelectionEvent; +import dwt.graphics.Color; +import dwt.graphics.Font; +import dwt.graphics.FontData; +import dwt.graphics.Point; +import dwt.graphics.RGB; +import dwt.layout.GridData; +import dwt.layout.GridLayout; +import dwt.widgets.Display; +import dwt.widgets.FontDialog; +import dwt.widgets.Menu; +import dwt.widgets.MenuItem; +import dwt.widgets.Shell; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; +import dwt.widgets.Widget; +import dwt.dwthelper.ResourceBundle; + +import tango.util.collection.ArraySeq; + +import dwtexamples.texteditor.Images; + +/** + */ +public class TextEditor { + Shell shell; + ToolBar toolBar; + StyledText text; + Images images; + alias ArraySeq!(StyleRange) StyleCache; + StyleCache cachedStyles; + + Color RED = null; + Color BLUE = null; + Color GREEN = null; + Font font = null; + ToolItem boldButton, italicButton, underlineButton, strikeoutButton; + + //string resources + static ResourceBundle resources; + private static const char[] resourceData = import( "examples_texteditor.properties" ); + + /* + * static ctor + */ + static this() + { + resources = ResourceBundle.getBundleFromData( resourceData ); + } + + /* + * ctor + */ + this() { + images = new Images(); + } + + /* + * creates edit menu + */ + Menu createEditMenu() { + Menu bar = shell.getMenuBar (); + Menu menu = new Menu (bar); + + MenuItem item = new MenuItem (menu, DWT.PUSH); + item.setText (resources.getString("Cut_menuitem")); + item.setAccelerator(DWT.MOD1 + 'X'); + item.addSelectionListener(new class() SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + handleCutCopy(); + text.cut(); + } + }); + item = new MenuItem (menu, DWT.PUSH); + item.setText (resources.getString("Copy_menuitem")); + item.setAccelerator(DWT.MOD1 + 'C'); + item.addSelectionListener(new class() SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + handleCutCopy(); + text.copy(); + } + }); + item = new MenuItem (menu, DWT.PUSH); + item.setText (resources.getString("Paste_menuitem")); + item.setAccelerator(DWT.MOD1 + 'V'); + item.addSelectionListener(new class() SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + text.paste(); + } + }); + new MenuItem (menu, DWT.SEPARATOR); + item = new MenuItem (menu, DWT.PUSH); + item.setText (resources.getString("Font_menuitem")); + item.addSelectionListener(new class() SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + setFont(); + } + }); + return menu; + } + + /* + * creates file menu + */ + Menu createFileMenu() { + Menu bar = shell.getMenuBar (); + Menu menu = new Menu (bar); + + MenuItem item = new MenuItem (menu, DWT.PUSH); + item.setText (resources.getString("Exit_menuitem")); + item.addSelectionListener(new class() SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + shell.close (); + } + }); + + return menu; + } + + /* + * Set a style + */ + void setStyle(Widget widget) { + Point sel = text.getSelectionRange(); + if ((sel is null) || (sel.y is 0)) return; + StyleRange style; + for (int i = sel.x; i 0) { + StyleRange lastStyle = cachedStyles.tail(); + if (lastStyle.similarTo(style) && lastStyle.start + lastStyle.length is style.start) { + lastStyle.length++; + } else { + cachedStyles.append(style); + } + } else { + cachedStyles.append(style); + } + } + } + } + + /* + * handle modify + */ + void handleExtendedModify(ExtendedModifyEvent event) { + if (event.length is 0) return; + StyleRange style; + if (event.length is 1 || text.getTextRange(event.start, event.length) == text.getLineDelimiter()) { + // Have the new text take on the style of the text to its right (during + // typing) if no style information is active. + int caretOffset = text.getCaretOffset(); + style = null; + if (caretOffset < text.getCharCount()) style = text.getStyleRangeAtOffset(caretOffset); + if (style !is null) { + style = cast(StyleRange) style.clone (); + style.start = event.start; + style.length = event.length; + } else { + style = new StyleRange(event.start, event.length, null, null, DWT.NORMAL); + } + if (boldButton.getSelection()) style.fontStyle |= DWT.BOLD; + if (italicButton.getSelection()) style.fontStyle |= DWT.ITALIC; + style.underline = underlineButton.getSelection(); + style.strikeout = strikeoutButton.getSelection(); + if (!style.isUnstyled()) text.setStyleRange(style); + } else { + // paste occurring, have text take on the styles it had when it was + // cut/copied + foreach (style; cachedStyles) { + StyleRange newStyle = cast(StyleRange)style.clone(); + newStyle.start = style.start + event.start; + text.setStyleRange(newStyle); + } + } + } + + /* + * opens the shell + */ + public Shell open (Display display) { + createShell (display); + createMenuBar (); + createToolBar (); + createStyledText (); + shell.setSize(500, 300); + shell.open (); + return shell; + } + + /* + * set the font for styled text widget + */ + void setFont() { + FontDialog fontDialog = new FontDialog(shell); + fontDialog.setFontList((text.getFont()).getFontData()); + FontData fontData = fontDialog.open(); + if (fontData !is null) { + Font newFont = new Font(shell.getDisplay(), fontData); + text.setFont(newFont); + if (font !is null) font.dispose(); + font = newFont; + } + } + + /* + * initialize the colors + */ + void initializeColors() { + Display display = Display.getDefault(); + RED = new Color (display, new RGB(255,0,0)); + BLUE = new Color (display, new RGB(0,0,255)); + GREEN = new Color (display, new RGB(0,255,0)); + } +} + +/* + * main function + */ +public void main (char[][] args) { + Display display = new Display (); + TextEditor example = new TextEditor (); + Shell shell = example.open (display); + while (!shell.isDisposed ()) + if (!display.readAndDispatch ()) display.sleep (); + display.dispose (); +} diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/blue.bmp Binary file dwtexamples/texteditor/blue.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/blue_mask.bmp Binary file dwtexamples/texteditor/blue_mask.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/bold.bmp Binary file dwtexamples/texteditor/bold.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/bold_mask.bmp Binary file dwtexamples/texteditor/bold_mask.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/erase.bmp Binary file dwtexamples/texteditor/erase.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/erase_mask.bmp Binary file dwtexamples/texteditor/erase_mask.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/examples_texteditor.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtexamples/texteditor/examples_texteditor.properties Sat Feb 23 02:06:51 2008 +0100 @@ -0,0 +1,22 @@ +############################################################################### +# Copyright (c) 2000, 2003 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 +############################################################################### +Exit_menuitem=E&xit +Cut_menuitem=Cu&t\tCtrl+X +Copy_menuitem=&Copy\tCtrl+C +Paste_menuitem=&Paste\tCtrl+V +Window_title=SWT Text Editor +Font_menuitem=Set &Font... +Edit_menuitem=&Edit +File_menuitem=&File +Bold=Bold +Italic=Italic +Underline=Underline +Strikeout=Strikeout \ No newline at end of file diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/green.bmp Binary file dwtexamples/texteditor/green.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/green_mask.bmp Binary file dwtexamples/texteditor/green_mask.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/italic.bmp Binary file dwtexamples/texteditor/italic.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/italic_mask.bmp Binary file dwtexamples/texteditor/italic_mask.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/red.bmp Binary file dwtexamples/texteditor/red.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/red_mask.bmp Binary file dwtexamples/texteditor/red_mask.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/strikeout.bmp Binary file dwtexamples/texteditor/strikeout.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/strikeout_mask.bmp Binary file dwtexamples/texteditor/strikeout_mask.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/underline.bmp Binary file dwtexamples/texteditor/underline.bmp has changed diff -r 5c244d50e532 -r f9c0133912fc dwtexamples/texteditor/underline_mask.bmp Binary file dwtexamples/texteditor/underline_mask.bmp has changed