|
|
Home » U++ Library support » TopWindow&PopUp, TrayIcon » Multiple windows and focus
Multiple windows and focus [message #10368] |
Wed, 04 July 2007 12:41  |
 |
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   |
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   |
 |
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 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 
Thanks for your help and advices 
|
|
|
|
|
|
Goto Forum:
Current Time: Sun Apr 27 02:40:18 CEST 2025
Total time taken to generate the page: 0.02710 seconds
|
|
|