|
|
Home » U++ Library support » TopWindow&PopUp, TrayIcon » how to communicate between windows?
how to communicate between windows? [message #29591] |
Tue, 02 November 2010 10:18 |
|
bonami
Messages: 186 Registered: June 2007 Location: Beijing
|
Experienced Member |
|
|
I have two classes inherited from TopWindow.
class 1 need to tell class 2 to go back or forward in processing.
they OpenMain() then I use Ctrl::EventLoop().
Solution 1,
class 1 envoke a member of class 2. class 2 needs a private lock. I would rather not use this solution.
Solution 2,
From MS' view, class 1 can SendMessage() or send an event to class 2. How to achieve this in U++? How to change class 2 (TopWindow)'s processing?
Thank you.
[Updated on: Tue, 02 November 2010 10:23] Report message to a moderator
|
|
|
Re: how to communicate between windows? [message #29594 is a reply to message #29591] |
Tue, 02 November 2010 11:29 |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
Is this a threading problem?
How about a simple event interface:
class EventHandler
{
private:
static Vector<EventHandler *> clients;
public:
typedef enum { SOME_EVENT, ANOTHER_EVENT } EventType;
public:
EventHandler() {
clients.Add(this);
}
virtual ~EventHandler() {
for (int i = 0; i < clients.Getcount(); i++)
if (clients[i] == this) {
clients[i].Remove(i);
return;
}
}
static void SendEvent(EventType event, int param1)
{
for (int i = 0; i < clients.GetCount(); i++)
clients[i]->HandleEvent(event, param1);
}
virtual void HandleEvent(EventType event, int param1) { }
};
Any class that inherits from EventHandler would be able to recieve global events.
[Updated on: Tue, 02 November 2010 12:43] Report message to a moderator
|
|
|
|
|
Re: how to communicate between windows? [message #29615 is a reply to message #29604] |
Thu, 04 November 2010 04:29 |
|
bonami
Messages: 186 Registered: June 2007 Location: Beijing
|
Experienced Member |
|
|
sorry i did not make it clear, since i thought it is simple.
here's my case, i have two TopWindows A & B. If user click a button in A, B shows something. If user click the other button in A, B shows something else. I need TopWindow::OpenMain(), then I donno whether it is multi-threaded. Anyway, my real case is more complicated and it IS multi-threaded and I want the code below is multi-thread compatible, too.
class B : public TopWindow
...
class A : public TopWindow
{
B b;
void shown() { b.OpenMain(); }
Button ButA;
Button ButB;
void Button_A();
void Button_B();
...
GUI_APP_MAIN
{
A a;
a.OpenMain();
a.shown();
Ctrl::EventLoop();
}
in Button_A() or _B(), A can tell B about which button is clicked, such as setting a flag. But how can B notice this? If i derive A & B from your EventHandler, how can I implement B's HandleEvent? This IS a threading problem. Maybe in a word, it is how to add my own procedure in TopWindow's main thread processing.
Thank you.
|
|
|
Re: how to communicate between windows? [message #29617 is a reply to message #29615] |
Thu, 04 November 2010 08:33 |
andrei_natanael
Messages: 262 Registered: January 2009
|
Experienced Member |
|
|
Hello Bonami,
Here is your example, modified to notify other window about a event. I've used PostCallback to do that, no MT
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
class B : public TopWindow
{
public:
B()
{
SetRect(220, 10, 200, 200);
Add(l.VCenterPos(30).HCenterPos(180));
l.SetText("Test");
}
Label l;
void ShowMessage(const String& msg)
{
l.SetText(msg);
}
};
class A : public TopWindow
{
typedef A CLASSNAME;
public:
A()
{
SetRect(10, 10, 200, 200);
ButA.SetLabel("Button A");
ButB.SetLabel("Button B");
Add(ButA.LeftPos(5, 80).TopPos(5, 25));
Add(ButB.LeftPos(5, 80).TopPos(35, 25));
ButA <<= THISBACK(Button_A);
ButB <<= THISBACK(Button_B);
}
B b;
void shown() { b.OpenMain(); }
Button ButA;
Button ButB;
void Button_A()
{
b.PostCallback(callback1(&b, &B::ShowMessage, "Button A pressed"));
}
void Button_B()
{
b.PostCallback(callback1(&b, &B::ShowMessage, "Button B pressed"));
}
};
GUI_APP_MAIN
{
A a;
a.OpenMain();
a.shown();
Ctrl::EventLoop();
}
Andrei
|
|
|
Re: how to communicate between windows? [message #29618 is a reply to message #29617] |
Thu, 04 November 2010 10:27 |
|
bonami
Messages: 186 Registered: June 2007 Location: Beijing
|
Experienced Member |
|
|
It looks like the thing I wanted. This callback will be executed in class b's thread/execution, right?
my code generates error LNK2019: unresolved external symbol "public: void __thiscall B::set(enum B::tp)"
class B: public TopWindow {
public:
enum tp
{
TPA,
TPB
};
void set(enum tp);
...
}
class A: public TopWindow {
...
private:
B *b;
void Button_A()
{
b->PostCallback(callback1(b, &B::set, B::TPA));
}
...
}
[Updated on: Thu, 04 November 2010 10:37] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Nov 01 00:09:09 CET 2024
Total time taken to generate the page: 0.02430 seconds
|
|
|