www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - TextLayout.d:2915 causes EXCEPTION_ACCESS_VIOLATION ?

reply yidabu <yidabu.spam gmail.com> writes:
Environment: dwt-win svn.

I porting styledtext.Snippet212, cause runtime exception:

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at ntdll.dll
(0x7c90316c) thread(1480)
->us



dwt\graphics\TextLayout.d:2915

dwt\graphics\TextLayout.d:267

dwt.graphics.TextLayout.TextLayout.getBounds () at
dwt\graphics\TextLayout.d:1387

dwt.custom.StyledText.StyledText.computeSize () at dwt\custom\StyledText.d:1784

dwt\layout\GridData.d:484

dwt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:232

dwt\layout\GridLayout.d:208

dwt\widgets\Composite.d:1170

dwt\widgets\Composite.d:879

dwt\widgets\Composite.d:924

dwt\widgets\Decorations.d:888

dwt\widgets\Shell.d:1460

dwt\widgets\Control.d:2639

dwt\widgets\Control.d:3153


() from dmain2

() from dmain2





Code:

/*******************************************************************************
 * Copyright (c) 2000, 2004 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:
 *     yidabu at gmail dot com  ( D China
http://www.d-programming-language-china.org/ )
 *******************************************************************************/
/*
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at ntdll.dll
(0x7c90316c) thread(1480)
->us



dwt\graphics\TextLayout.d:2915

dwt\graphics\TextLayout.d:267

dwt.graphics.TextLayout.TextLayout.getBounds () at
dwt\graphics\TextLayout.d:1387

dwt.custom.StyledText.StyledText.computeSize () at dwt\custom\StyledText.d:1784

dwt\layout\GridData.d:484

dwt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:232

dwt\layout\GridLayout.d:208

dwt\widgets\Composite.d:1170

dwt\widgets\Composite.d:879

dwt\widgets\Composite.d:924

dwt\widgets\Decorations.d:888

dwt\widgets\Shell.d:1460

dwt\widgets\Control.d:2639

dwt\widgets\Control.d:3153


() from dmain2

() from dmain2



*/
module styledtext.Snippet212;
/**
 * StyledText snippet: embed images
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 *  since 3.2
 */

import dwt.DWT;
import dwt.custom.StyledText;
import dwt.custom.StyleRange;
import dwt.layout.GridLayout;
import dwt.layout.GridData;
import dwt.widgets.Display;
import dwt.widgets.Shell;
import dwt.widgets.Button;
import dwt.widgets.FileDialog;
import dwt.widgets.Event;
import dwt.widgets.Listener;
import dwt.custom.PaintObjectEvent;
import dwt.custom.PaintObjectListener;
import dwt.events.VerifyEvent;
import dwt.events.VerifyListener;
import dwt.graphics.FontData;
import dwt.graphics.Font;
import dwt.graphics.Rectangle;
import dwt.graphics.GC;
import dwt.graphics.Image;
import dwt.graphics.GlyphMetrics;
import dwt.dwthelper.utils;
import dwt.dwthelper.System;

void main() {
	static StyledText styledText;
	static String text = 
		"This snippet shows how to embed images in a StyledText.\n"
		"Here is one: \uFFFC, and here is another: \uFFFC."
		"Use the add button to add an image from your filesystem to the StyledText at
the current caret offset.";
	static Image[] images;
	static int[] offsets;

	static void addImage(Image image, int offset) {
		StyleRange style = new StyleRange ();
		style.start = offset;
		style.length = 1;
		Rectangle rect = image.getBounds();
		style.metrics = new GlyphMetrics(rect.height, 0, rect.width);
		styledText.setStyleRange(style);		
	}
	

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER);
    styledText.setLayoutData(new GridData(DWT.FILL, DWT.FILL, true, true));
    styledText.setText(text);
    images = [
        display.getSystemImage(DWT.ICON_QUESTION),
        display.getSystemImage(DWT.ICON_INFORMATION)
    ];
    offsets = new int[images.length];
    int lastOffset = 0;
    for (int i = 0; i < images.length; i++) {
        int offset = text.indexOf("\uFFFC", lastOffset);
        offsets[i] = offset;
        addImage(images[i], offset);
        lastOffset = offset + 1;
    }
    
    void onVerify(Event e) {
        int start = e.start;
        int replaceCharCount = e.end - e.start;
        int newCharCount = e.text.length;
        for (int i = 0; i < offsets.length; i++) {
            int offset = offsets[i];
            if (start <= offset && offset < start + replaceCharCount) {
                // this image is being deleted from the text
                if (images[i] !is null && !images[i].isDisposed()) {
                    images[i].dispose();
                    images[i] = null;
                }
                offset = -1;
            }
            if (offset != -1 && offset >= start) offset += newCharCount -
replaceCharCount;
            offsets[i] = offset;
        }
    }    
    // use a verify listener to keep the offsets up to date
    styledText.addListener(DWT.Verify, dgListener(&onVerify));

    styledText.addPaintObjectListener(new class(images, offsets)
PaintObjectListener {
        Image[] images;
        int[] offsets;
        this( Image[] images_, int[] offsets_) {
            this.images = images_;
            this.offsets = offsets_;
        }
        public void paintObject(PaintObjectEvent event) {
            GC gc = event.gc;
            StyleRange style = event.style;
            int start = style.start;
            for (int i = 0; i < offsets.length; i++) {
                int offset = offsets[i];
                if (start == offset) {
                    Image image = images[i];
                    int x = event.x;
                    int y = event.y + event.ascent - style.metrics.ascent;						
                    gc.drawImage(image, x, y);
                }
            }
        }
    });    
    
    Button button = new Button (shell, DWT.PUSH);
    button.setText("Add Image");
    button.setLayoutData(new GridData(DWT.CENTER, DWT.CENTER, false, false));
    
    void onSelection(Event e)
    {
           FileDialog dialog = new FileDialog(shell);
            String filename = dialog.open();
            if (filename !is null) {
                try {
                    Image image = new Image(display, filename);
                    int offset = styledText.getCaretOffset();
                    styledText.replaceTextRange(offset, 0, "\uFFFC");
                    int index = 0;
                    while (index < offsets.length) {
                        if (offsets[index] == -1 && images[index] is null)
break;
                        index++;
                    }
                    if (index == offsets.length) {
                        int[] tmpOffsets = new int[index + 1];
                        System.arraycopy(offsets, 0, tmpOffsets, 0,
offsets.length);
                        offsets = tmpOffsets;
                        Image[] tmpImages = new Image[index + 1];
                        System.arraycopy(images, 0, tmpImages, 0,
images.length);
                        images = tmpImages;
                    }
                    offsets[index] = offset;
                    images[index] = image;
                    addImage(image, offset);
                } catch (Exception e) {
                    ExceptionPrintStackTrace(e);
                }
            }	        
    }
    button.addListener(DWT.Selection, dgListener(&onSelection));
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    for (int i = 0; i < images.length; i++) {
        Image image = images[i];
        if (image !is null && !image.isDisposed()) {
            image.dispose();
        }
    }
    display.dispose();

}





-- 
yidabu <yidabu.spam gmail.com>
http://www.dsource.org/projects/dwin

D ÓïÑÔ-ÖÐÎÄ(D Chinese):
http://www.d-programming-language-china.org/
http://bbs.d-programming-language-china.org/
http://dwin.d-programming-language-china.org/
http://scite4d.d-programming-language-china.org/
Aug 09 2008
next sibling parent Frank Benoit <keinfarbton googlemail.com> writes:
yidabu schrieb:
 Environment: dwt-win svn.
 
 I porting styledtext.Snippet212, cause runtime exception:
 
 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at ntdll.dll
(0x7c90316c) thread(1480)
 ->us



dwt\graphics\TextLayout.d:2915

dwt\graphics\TextLayout.d:267

dwt.graphics.TextLayout.TextLayout.getBounds () at
dwt\graphics\TextLayout.d:1387

dwt.custom.StyledText.StyledText.computeSize () at dwt\custom\StyledText.d:1784

dwt\layout\GridData.d:484

dwt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:232

dwt\layout\GridLayout.d:208

dwt\widgets\Composite.d:1170

dwt\widgets\Composite.d:879

dwt\widgets\Composite.d:924

dwt\widgets\Decorations.d:888

dwt\widgets\Shell.d:1460

dwt\widgets\Control.d:2639

dwt\widgets\Control.d:3153


runMain(void*) () from dmain2

() from dmain2



 
 
 Code:
 
 /*******************************************************************************
  * Copyright (c) 2000, 2004 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:
  *     yidabu at gmail dot com  ( D China
http://www.d-programming-language-china.org/ )
  *******************************************************************************/
 /*
 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at ntdll.dll
(0x7c90316c) thread(1480)
 ->us



dwt\graphics\TextLayout.d:2915

dwt\graphics\TextLayout.d:267

dwt.graphics.TextLayout.TextLayout.getBounds () at
dwt\graphics\TextLayout.d:1387

dwt.custom.StyledText.StyledText.computeSize () at dwt\custom\StyledText.d:1784

dwt\layout\GridData.d:484

dwt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:232

dwt\layout\GridLayout.d:208

dwt\widgets\Composite.d:1170

dwt\widgets\Composite.d:879

dwt\widgets\Composite.d:924

dwt\widgets\Decorations.d:888

dwt\widgets\Shell.d:1460

dwt\widgets\Control.d:2639

dwt\widgets\Control.d:3153


runMain(void*) () from dmain2

() from dmain2



 */
 module styledtext.Snippet212;
 /**
  * StyledText snippet: embed images
  *
  * For a list of all SWT example snippets see
  * http://www.eclipse.org/swt/snippets/
  * 
  *  since 3.2
  */
 
 import dwt.DWT;
 import dwt.custom.StyledText;
 import dwt.custom.StyleRange;
 import dwt.layout.GridLayout;
 import dwt.layout.GridData;
 import dwt.widgets.Display;
 import dwt.widgets.Shell;
 import dwt.widgets.Button;
 import dwt.widgets.FileDialog;
 import dwt.widgets.Event;
 import dwt.widgets.Listener;
 import dwt.custom.PaintObjectEvent;
 import dwt.custom.PaintObjectListener;
 import dwt.events.VerifyEvent;
 import dwt.events.VerifyListener;
 import dwt.graphics.FontData;
 import dwt.graphics.Font;
 import dwt.graphics.Rectangle;
 import dwt.graphics.GC;
 import dwt.graphics.Image;
 import dwt.graphics.GlyphMetrics;
 import dwt.dwthelper.utils;
 import dwt.dwthelper.System;
 
 void main() {
 	static StyledText styledText;
 	static String text = 
 		"This snippet shows how to embed images in a StyledText.\n"
 		"Here is one: \uFFFC, and here is another: \uFFFC."
 		"Use the add button to add an image from your filesystem to the StyledText
at the current caret offset.";
 	static Image[] images;
 	static int[] offsets;
 
 	static void addImage(Image image, int offset) {
 		StyleRange style = new StyleRange ();
 		style.start = offset;
 		style.length = 1;
 		Rectangle rect = image.getBounds();
 		style.metrics = new GlyphMetrics(rect.height, 0, rect.width);
 		styledText.setStyleRange(style);		
 	}
 	
 
     Display display = new Display();
     Shell shell = new Shell(display);
     shell.setLayout(new GridLayout());
     styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER);
     styledText.setLayoutData(new GridData(DWT.FILL, DWT.FILL, true, true));
     styledText.setText(text);
     images = [
         display.getSystemImage(DWT.ICON_QUESTION),
         display.getSystemImage(DWT.ICON_INFORMATION)
     ];
     offsets = new int[images.length];
     int lastOffset = 0;
     for (int i = 0; i < images.length; i++) {
         int offset = text.indexOf("\uFFFC", lastOffset);
         offsets[i] = offset;
         addImage(images[i], offset);
         lastOffset = offset + 1;
     }
     
     void onVerify(Event e) {
         int start = e.start;
         int replaceCharCount = e.end - e.start;
         int newCharCount = e.text.length;
         for (int i = 0; i < offsets.length; i++) {
             int offset = offsets[i];
             if (start <= offset && offset < start + replaceCharCount) {
                 // this image is being deleted from the text
                 if (images[i] !is null && !images[i].isDisposed()) {
                     images[i].dispose();
                     images[i] = null;
                 }
                 offset = -1;
             }
             if (offset != -1 && offset >= start) offset += newCharCount -
replaceCharCount;
             offsets[i] = offset;
         }
     }    
     // use a verify listener to keep the offsets up to date
     styledText.addListener(DWT.Verify, dgListener(&onVerify));
 
     styledText.addPaintObjectListener(new class(images, offsets)
PaintObjectListener {
         Image[] images;
         int[] offsets;
         this( Image[] images_, int[] offsets_) {
             this.images = images_;
             this.offsets = offsets_;
         }
         public void paintObject(PaintObjectEvent event) {
             GC gc = event.gc;
             StyleRange style = event.style;
             int start = style.start;
             for (int i = 0; i < offsets.length; i++) {
                 int offset = offsets[i];
                 if (start == offset) {
                     Image image = images[i];
                     int x = event.x;
                     int y = event.y + event.ascent -
style.metrics.ascent;						
                     gc.drawImage(image, x, y);
                 }
             }
         }
     });    
     
     Button button = new Button (shell, DWT.PUSH);
     button.setText("Add Image");
     button.setLayoutData(new GridData(DWT.CENTER, DWT.CENTER, false, false));
     
     void onSelection(Event e)
     {
            FileDialog dialog = new FileDialog(shell);
             String filename = dialog.open();
             if (filename !is null) {
                 try {
                     Image image = new Image(display, filename);
                     int offset = styledText.getCaretOffset();
                     styledText.replaceTextRange(offset, 0, "\uFFFC");
                     int index = 0;
                     while (index < offsets.length) {
                         if (offsets[index] == -1 && images[index] is null)
break;
                         index++;
                     }
                     if (index == offsets.length) {
                         int[] tmpOffsets = new int[index + 1];
                         System.arraycopy(offsets, 0, tmpOffsets, 0,
offsets.length);
                         offsets = tmpOffsets;
                         Image[] tmpImages = new Image[index + 1];
                         System.arraycopy(images, 0, tmpImages, 0,
images.length);
                         images = tmpImages;
                     }
                     offsets[index] = offset;
                     images[index] = image;
                     addImage(image, offset);
                 } catch (Exception e) {
                     ExceptionPrintStackTrace(e);
                 }
             }	        
     }
     button.addListener(DWT.Selection, dgListener(&onSelection));
     shell.setSize(400, 400);
     shell.open();
     while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
             display.sleep();
     }
     for (int i = 0; i < images.length; i++) {
         Image image = images[i];
         if (image !is null && !image.isDisposed()) {
             image.dispose();
         }
     }
     display.dispose();
 
 }
 
 
 
 
 
Complicated, takes more time.
Aug 11 2008
prev sibling parent reply Frank Benoit <keinfarbton googlemail.com> writes:
This is now no more crashing. A window is shown with the embedded
images. But there are still the "obj" marker chars and when selecting
text there is strange behaviour.
Aug 21 2008
parent Frank Benoit <keinfarbton googlemail.com> writes:
Frank Benoit schrieb:
 This is now no more crashing. A window is shown with the embedded
 images. But there are still the "obj" marker chars and when selecting
 text there is strange behaviour.
The obj marker is fixed in the Snippet212.
Aug 21 2008