Bug 4414 - DCR - Platform inconsistency of Shell.open (1FS69OC)
Summary: DCR - Platform inconsistency of Shell.open (1FS69OC)
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 2.0   Edit
Hardware: All All
: P5 normal (vote)
Target Milestone: ---   Edit
Assignee: Steve Northover CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 96700
  Show dependency tree
 
Reported: 2001-10-11 14:16 EDT by Andre Weinand CLA
Modified: 2005-05-27 14:40 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andre Weinand CLA 2001-10-11 14:16:23 EDT
AW (21.03.00 18:58:21)
	SWT 0.39:
	If I open a Shell which is already open it is brought to front under Windows but not on Motif.
	What is the intended behavior?
	Should I always use a Shell.moveAbove(null) after calling Shell.open()?

SN (3/21/00 5:49:05 PM)
	This is a bug.  The intent of shell.open () is that it be only called once but when called
	multiple times, it should bring the shell to the front like Windows (I guess ... ?).  You
	shouldn't really be calling it multiple times but it's not harmful.  To change the Z-order,
	you should be using the Z-order calls (moveAbove etc.).

NOTES:

	McQ (27/11/2000 2:11:02 PM) -
		Clarification: It is a bug in application code to call Shell.open() more than once.
		Applications should not rely on this behavior. We should probably make open
		throw an SWTException if it is called, but this is low priority. Moving to inactive.
Comment 1 Mike Wilson CLA 2002-03-20 14:24:39 EST
Inactive.
Comment 2 Veronika Irvine CLA 2002-09-11 14:07:01 EDT
Moving from Later.
Comment 3 Carolyn MacLeod CLA 2004-11-26 15:24:36 EST
Investigating, for consistency work.
Ran the following snippet on XP, GTK, Motif(Linux), and Mac.
Here are the results:
XP - Shell 1 brought to front on 2nd open
GTK - Shell 2 stays in front
Motif - Shell 2 stays in front
Mac - Shell 1 brought to front on 2nd open

Will consult with SN on what we think the "correct" behavior is.

import org.eclipse.swt.widgets.*;

public class Main {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Shell 1");
		shell.open();
		Shell shell2 = new Shell(display);
		shell2.setText("Shell 2");
		shell2.open();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
Comment 4 Steve Northover CLA 2004-11-26 15:36:49 EST
The spec is pretty clear on this one for once.  I had a go at fixing this 
recently on GTK and Motif but ran into trouble (a subtle case that I can't 
remember right now).  Feel free to try again.  I could have been on crack.
Comment 5 Steve Northover CLA 2004-11-26 15:37:42 EST
I think you need to run an event loop before X knows which shell is on top.
Comment 6 Carolyn MacLeod CLA 2004-11-30 15:48:05 EST
The spec for Shell.open says that it:
 * Moves the receiver to the top of the drawing order
 * marks it visible
 * sets the focus
 * asks the window manager to make the shell active

So we will try to make sure it does all of those things on all platforms, even 
on the 2nd invokation. In addition, it probably makes sense to de-iconify it 
if it happens to be iconified...? Steve?

The above snippet needs more code. Here it is again:

import org.eclipse.swt.widgets.*;

public class Main {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Shell 1");
		shell.open();
		Shell shell2 = new Shell(display);
		shell2.setText("Shell 2");
		shell2.open();
		shell.setMinimized(true);
		shell.setVisible(false);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
Comment 7 Carolyn MacLeod CLA 2004-11-30 16:03:19 EST
Steve, what do you think about a plain-and-simple "set a flag" solution that 
would be basically the same code in Shell on all platforms...?

For example,
1) Add a field:
	boolean openCalled = false;

2) Check and set the flag after the checkWidget() in the open() method:
	if (openCalled) {
		reopen ();
		return;
	}
	openCalled = true;


3) ... where "reopen()" simply does the following (all API calls):
void reopen () {
	if (isDisposed ()) return;
	if (!getVisible ()) setVisible (true);
	if (getMinimized ()) setMinimized (false);
	bringToTop ();  // or moveAbove (null);
	setActive ();
	setFocus ();
}


It's kind of "brute force" but the advantages are:
- no subtle cases
- no event loops on X
- it's already implemented, and it works <g>

Disadvantages:
- extra field (but Shells are only order n)
- extra method (but it's small)
- widget makes a bunch of api calls (but very few people will run this code)
Comment 8 Steve Northover CLA 2004-11-30 16:36:22 EST
You need to determine why the current code doesn't work before adding bogus 
flags to remember state.
Comment 9 Nick Edgar CLA 2005-05-25 13:28:46 EDT
It's not clear to me that open should do a setFocus().  It should just restore
focus to the control that had it previously in that shell.
Comment 10 Nick Edgar CLA 2005-05-25 13:39:03 EDT
Still a problem on GTK using 3.1 M7.

I've changed the Workbench uses (in the two Workbench.setPerspective methods) to
use a new internal method, WorkbenchWindow.makeVisible(), which uses
shell.setMinimized(false) and shell.setActive().
Comment 11 Steve Northover CLA 2005-05-25 15:58:46 EDT
Shell.open() does attempt to restore the previous focus.  CAR's code sample is 
wrong.
Comment 12 Steve Northover CLA 2005-05-25 17:18:46 EDT
I'm looking at this again.
Comment 13 Steve Northover CLA 2005-05-25 19:46:46 EDT
Fixed > 20050525
Comment 14 Steve Northover CLA 2005-05-25 19:47:11 EDT
Now it's fixed.
Comment 15 Nick Edgar CLA 2005-05-26 18:39:58 EDT
In bug 96700, Doug says Shell.open() still does not have the desired effect of
bringing the window to top.

Comment 16 Steve Northover CLA 2005-05-27 10:00:55 EDT
Does the test code in this bug report fail for him?
Comment 17 Nick Edgar CLA 2005-05-27 10:17:46 EDT
The code above does not test the scenario where the window needs to be brought
to front, either due to another SWT window being frontmost, or another app.
Doug, can you clarify which scenario is still a problem on GTK?
Comment 18 Douglas Pollock CLA 2005-05-27 14:40:31 EDT
See Bug 96700 comment #6.