[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.tools] Re: Problem with Canvas widget in SWT
|
Here is a simple example of what I meant.
In this example, the canvas turns red when it has focus and turns blue when
it loses focus.
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
Combo combo = new Combo(shell, SWT.BORDER);
combo.setBounds(10, 10, 100, 50);
final Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setBounds(10, 100, 100, 100);
canvas.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed "+e.character+" "+e.keyCode);
}
});
canvas.addFocusListener (new FocusAdapter() {
public void focusGained(FocusEvent e){
canvas.setBackground(canvas.getDisplay().getSystemColor(SWT.COLOR_RED));
}
public void focusLost(FocusEvent e) {
canvas.setBackground(canvas.getDisplay().getSystemColor(SWT.COLOR_BLUE));
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
If you comment out the lines :
canvas.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed "+e.character+" "+e.keyCode);
}
});
the canvas will no longer take focus when you tab around between the
widgets. The assumption is that if your widget can't handle keyboard input
then it really doesn't want to take focus.
Now your case is special because your C code is processing the events.
Are you handling WM_GETDLGCODE? If so, you may not be getting the Canvas
widget's handling. For the Canvas widget, we have
LRESULT WM_GETDLGCODE (int wParam, int lParam) {
int flags = OS.DLGC_WANTALLKEYS | OS.DLGC_WANTARROWS | OS.DLGC_WANTTAB;
return new LRESULT (flags);
}
You may need to do something similar if you are processing this message
before us and therefore SWT is not getting this message.
The question I would like to know is what mechanism are you using to get the
events in your C code (specifically, are you getting in before or after SWT)
because this will very much affect the behaviour you are seeing?
You might want to have a look at the article
http://www.eclipsecorner.org/articles/Article-Writing%20Your%20Own%20Widget/
Writing%20Your%20Own%20Widget.htm. In there, the concept of wrapping a new
widget (written in C or whatever) in a thin SWT layer is discussed.