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 » how to communicate between windows?
icon1.gif  how to communicate between windows? [message #29591] Tue, 02 November 2010 10:18 Go to next message
bonami is currently offline  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 Go to previous messageGo to next message
mrjt is currently offline  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 #29603 is a reply to message #29594] Wed, 03 November 2010 02:45 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
My question is exactly how to implement this HandleEvent for a TopWindow derived class.
Re: how to communicate between windows? [message #29604 is a reply to message #29603] Wed, 03 November 2010 11:22 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Then I don't understand the question. 'How to change class 2 (TopWindow)'s processing?' is too vague a question.

And you haven't answered whether this is a threading problem or not.

You will need to provide substantially more information to get any sort of useful help.
Re: how to communicate between windows? [message #29615 is a reply to message #29604] Thu, 04 November 2010 04:29 Go to previous messageGo to next message
bonami is currently offline  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 Go to previous messageGo to next message
andrei_natanael is currently offline  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 Smile

#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 Go to previous messageGo to next message
bonami is currently offline  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

Re: how to communicate between windows? [message #29629 is a reply to message #29618] Thu, 04 November 2010 21:47 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
In class B set function signature should be:
void set(tp x) // note, no enum before tp


Quote:

This callback will be executed in class b's thread/execution, right?

Yes.

Andrei
Re: how to communicate between windows? [message #29635 is a reply to message #29629] Fri, 05 November 2010 03:05 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
all right. with or without enum have no differences. it compiles right. i just did not provide set()'s implementation. can i be more stupid?
I think b->PostCallback() and ::PostCallback() are same. right?
thank you very much.
Re: how to communicate between windows? [message #29637 is a reply to message #29635] Fri, 05 November 2010 06:16 Go to previous message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
They are same. Only difference is that ::PostCallback get executed 1 ms later than b->PostCallback Smile (that's what code say)
Previous Topic: Crash on ToolTip painting
Next Topic: TrayIcon Tool tips text lenght limited
Goto Forum:
  


Current Time: Fri Nov 01 00:09:09 CET 2024

Total time taken to generate the page: 0.02430 seconds