Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » TopWindow&PopUp, TrayIcon » Multiple windows and focus
Multiple windows and focus [message #10368] Wed, 04 July 2007 12:41 Go to next message
jibe is currently offline  jibe
Messages: 294
Registered: February 2007
Location: France
Experienced Member
Hi,

I need to have several windows in an application, one for each part. Those windows are created on demand, several can be opened in the same time. When I need one, it must be either created, or focus must be given to it if existing. Let's say that it's somehow similar to MS Windows' MDI apps.

I tried this :

#ifndef _MultiWin_MultiWin_h
#define _MultiWin_MultiWin_h

#include <CtrlLib/CtrlLib.h>

using namespace Upp;


class Win1 : public TopWindow {
public:
	typedef Win1 CLASSNAME;
	Win1();
	~Win1();
	void Close();
};

class Win2 : public TopWindow {
public:
	typedef Win2 CLASSNAME;
	Win2();
	~Win2();
	void Close();
};

class MultiWin : public TopWindow {
protected:
	MenuBar menu;
public:
	typedef MultiWin CLASSNAME;
	MultiWin();
	void MainMenu(Bar& bar);
	void Window1();
	void Window2();
};

Win1 *win1;
Win2 *win2;

#endif

#include "MultiWin.h"

MultiWin::MultiWin()
{
	Title("Test multiple windows").MinimizeBox().Sizeable();
	AddFrame(menu);
	AddFrame(TopSeparatorFrame());
    menu.Set(THISBACK(MainMenu));
}

void MultiWin::MainMenu(Bar& bar) {
    bar.Add("Window 1", THISBACK(Window1));
    bar.Add("Window 2", THISBACK(Window2));
}

void MultiWin::Window1() {
	if (win1==NULL) {
		win1=new Win1();
		win1->OpenMain();
	}
	else {
		this->LostFocus();
		win1->SetFocus();
	}
}

void MultiWin::Window2() {
	if (win2==NULL) {
		win2=new Win2();
		win2->OpenMain();
	}
	else {
		this->LostFocus();
		win2->SetFocus();
	}
}

Win1::Win1() {
	Title("Window 1").MinimizeBox().Sizeable();
}

Win1::~Win1() {
	win1=NULL;
}

void Win1::Close() {
	delete this;
}

Win2::Win2() {
	Title("Window 2").MinimizeBox().Sizeable();
}

Win2::~Win2() {
	win2=NULL;
}

void Win2::Close() {
	delete this;
}

GUI_APP_MAIN
{
	MultiWin().Run();
}


It works well, but when I try to re-open an existing window, it seems to get the focus but don't comes on the top of all others !

I tried a lot of things, but was unable find a solution... What can I do ? Is there a better way to do what I need to stay in "UPP spirit" ?

Thanks.
Re: Multiple windows and focus [message #10369 is a reply to message #10368] Wed, 04 July 2007 14:07 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
		win->SetForeground();
		win->SetFocus();

Works fine for me. Also you don't need to call LostFocus, this will be done for you.

Now, some other suggestions:
'delete this' should never be used as it relies on the object being created by 'new'. Have the owner set the TopWindow::WhenClose callback to a function that deletes it instead.

You might want to use the Upp class Ptr<TopWindow> instead of the pointers. Then you don't need to worry about setting them back to NULL. Personally I'd keep them in an array of Ptrs.

Do you really need to create/delete the windows when they are opened/closed? Unless you will have many of them you could just have them as members and not have to worry about it.

Or you could google for 'Singleton class C++', which might do what you're looking for.

James

[Updated on: Wed, 04 July 2007 14:10]

Report message to a moderator

Re: Multiple windows and focus [message #10373 is a reply to message #10369] Wed, 04 July 2007 16:10 Go to previous messageGo to next message
jibe is currently offline  jibe
Messages: 294
Registered: February 2007
Location: France
Experienced Member
mrjt wrote on Wed, 04 July 2007 14:07

		win->SetForeground();
		win->SetFocus();

Works fine for me. Also you don't need to call LostFocus, this will be done for you.

Yes, it's working fine, thanks Smile I tried a lot of things, but I missed this method !

mrjt wrote on Wed, 04 July 2007 14:07

You might want to use the Upp class Ptr<TopWindow> instead of the pointers. Then you don't need to worry about setting them back to NULL. Personally I'd keep them in an array of Ptrs.

I'm still new with UPP and don't know how to use this (even if I gess a little...). Is there a document explaining that, or I must study the sources ?

mrjt wrote on Wed, 04 July 2007 14:07

Do you really need to create/delete the windows when they are opened/closed? Unless you will have many of them you could just have them as members and not have to worry about it.

Sometimes, I have few simple windows, so I could effectively create them all at the begining. But sometimes I need several complex windows, so I think that it's better to create/delete to save memory...

mrjt wrote on Wed, 04 July 2007 14:07

Or you could google for 'Singleton class C++', which might do what you're looking for.

Yes, I could use a singleton. But is there a singleton base class in UPP ? For me, singleton is more especially interesting for a team of developpers, and in most cases I'm developping alone.

Anyway, if there is not yet, I think that a singleton base class would be interresting in UPP, even if it's not difficult to create one's own : no need to re-invent the wheel each time... Maybe to think for a next version Wink

Thanks for your help and advices Smile

Re: Multiple windows and focus [message #10374 is a reply to message #10373] Wed, 04 July 2007 16:45 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
jibe wrote on Wed, 04 July 2007 15:10


Is there a document explaining that, or I must study the sources ?

In the manual. AFAIK they are basically just a container for pointers, and a bit simpler than the std:: variety.
Re: Multiple windows and focus [message #10376 is a reply to message #10374] Wed, 04 July 2007 18:25 Go to previous messageGo to next message
jibe is currently offline  jibe
Messages: 294
Registered: February 2007
Location: France
Experienced Member
Nice Smile
Thanks.
Re: Multiple windows and focus [message #10386 is a reply to message #10369] Thu, 05 July 2007 07:36 Go to previous message
mirek is currently offline  mirek
Messages: 11991
Registered: November 2005
Ultimate Member
mrjt wrote on Wed, 04 July 2007 08:07

		win->SetForeground();
		win->SetFocus();

Works fine for me. Also you don't need to call LostFocus, this will be done for you.

Now, some other suggestions:
'delete this' should never be used as it relies on the object being created by 'new'. Have the owner set the TopWindow::WhenClose callback to a function that deletes it instead.



Actually, multiple peer windows scenario is one of exceptions to U++ common scope based resource management.

In this case is often the simplest thing to 'new' main windows and to use 'delete this' to close them. See UWord example.

Of course, it is also possible to deal with this using Array of windows or something like that, but we have found that in this particular situation, it is only more complex...
Previous Topic: Can't get rid of minimize button on Linux, okay on Win32
Next Topic: System tray on windows
Goto Forum:
  


Current Time: Sun Aug 18 15:28:09 CEST 2019

Total time taken to generate the page: 0.00747 seconds