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 » U++ Widgets - General questions or Mixed problems » how to add runtime StaticText and hook mouse events for it
how to add runtime StaticText and hook mouse events for it [message #3366] Mon, 22 May 2006 10:21 Go to next message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
...tried few methods, but all fail, including overriding virtual metods.
Experimenting with something like that:

class myApp : public WithterminalLayout<TopWindow> {   // just a plain window
public:

    class Item {                        // class consist of two widgets
    public:
        bool moving;                    // want to move them on window :)
			
        // only wants to move, if click on first widget
        class Info : public StaticText {
        public:
            Item * pItem;

            virtual void LeftDown(Point p, dword keyflags) {
                pItem->moving = true;
            }

            virtual void LeftUp(Point p, dword keyflags) {
                pItem->moving = false;
            }
            
            virtual void MouseMove(Point p, dword keyflags) {
                if(pItem->moving) {
                    HSizePos().TopPos(p.y);
                }
            }
        };

        // our two widgets
        Info info;
        StaticText data;

        Item(int x, int y, Ctrl * q) {
            info.pItem = this;              // ...becouse of 'moving' var

            q->Add(info.LeftPos(x, 40).TopPos(y, 16));
            q->Add(data.LeftPos(x + 41, 40).TopPos(y, 16));

            info.SetText("box").SetFrame(ButtonFrame());
            data.SetText("BOX").SetFrame(BlackFrame());
        }
    };

    Array<Item> items;

    virtual void LeftDown(Point pos, dword flags) {
        items.Add(new Item((int)pos.x, (int)pos.y, this));
    };

    typedef myApp CLASSNAME;

    myApp() {
        CtrlLayout(*this, "Window title");
    }
};


GUI_APP_MAIN
{
    myApp().Run();
}





It wont even react on clicks.... am I missing something?

[Updated on: Mon, 22 May 2006 10:22]

Report message to a moderator

Re: how to add runtime StaticText and hook mouse events for it [message #3367 is a reply to message #3366] Mon, 22 May 2006 10:39 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
ok, my experimental drag&drop control....
#include <CtrlLib/CtrlLib.h>
#define LLOG(x)

class DragDropCtrl : public Button
{
private:
	Point        start, last, pdelta, pstartdelta;
	LogPos       pos;
	Rect         RC_last, RC_next, RC_start; 
	dword        last_key;
	char         state;
	enum { OFF, ON, POSSIBLE, HIDDEN };
	
public:
	DragDropCtrl();

	// drag & drop interface
	virtual bool Push(Point pt, dword keyflags);
	virtual void Drag(Point pt, Point last, Point next, dword keyflags);
	virtual void Drop(Point pt, Point end, dword keyflags);
	virtual void Click(Point pt, dword keyflags);
	static const int DBLCLK = 0x40000000;

	virtual void DragRect(const Rect& last, const Rect& next, dword keyflags);
	virtual void DropRect(const Rect& rc, dword keyflags);

	void         DragShow(bool _show = true);
	void         DragHide()                    { DragShow(false); }
	void         DragStop(bool accept, dword keyflags);
	void         DragStop(bool accept = false) { DragStop(accept, last_key); }

	bool         IsDragging() const            { return state == ON || state == HIDDEN; }
	bool         IsPushed() const              { return state != OFF; }

	// control overrides
	virtual void LeftDown(Point pt, dword keyflags);
	virtual void LeftDouble(Point pt, dword keyflags);
	virtual void LeftUp(Point pt, dword keyflags);
	virtual void MouseMove(Point pt, dword keyflags);
	virtual bool Key(dword key, int repcnt);

};


//=============helpers start==================
inline Rect SortRect(Point p1, Point p2)
{
	return Rect(min(p1.x, p2.x), min(p1.y, p2.y), max(p1.x, p2.x) + 1, max(p1.y, p2.y) + 1);
}

bool IsDragDistance(Point pt1, Point pt2)
{
#ifdef PLATFORM_WIN32
	return tabs(pt1.x - pt2.x) >= GetSystemMetrics(SM_CXDRAG)
		|| tabs(pt1.y - pt2.y) >= GetSystemMetrics(SM_CYDRAG);
#endif
#ifdef PLATFORM_POSIX
	enum { CXDRAG = 4, CYDRAG = 4 };
	// todo? are there any CXDRAG / CYDRAG system metrics in LINUX?
	return tabs(pt1.x - pt2.x) >= CXDRAG || tabs(pt1.y - pt2.y) >= CYDRAG;
#endif
}

//what does this do?
int GetRectDragMask(Rect rc, Point pt, int tolerance)
{
	Point center = rc.CenterPoint();
	int m = (tabs(rc.left   - pt.x) <= tolerance ?  1 : 0)
		|   (tabs(rc.top    - pt.y) <= tolerance ?  2 : 0)
		|   (tabs(rc.right  - pt.x) <= tolerance ?  4 : 0)
		|   (tabs(rc.bottom - pt.y) <= tolerance ?  8 : 0);
	if(m & 5)
		if((m & 10) || tabs(center.y - pt.y) <= tolerance)
			return m;
	if(m & 10)
		if((m & 5) || tabs(center.x - pt.x) <= tolerance)
			return m;
	return 0;
}

//==========helpers end=====================

DragDropCtrl::DragDropCtrl()
: state(0)
{
}

bool DragDropCtrl::Push(Point pt, dword keyflags)
{
	return true;
}

void DragDropCtrl::DragRect(const Rect& last, const Rect& next, dword keyflags)
{
	ViewDraw draw(this);
	DrawDragRect(draw, last, next, draw.GetClip(), 1, Yellow(), NULL);
}

void DragDropCtrl::LeftDown(Point pt, dword keyflags)
{
	SetWantFocus();
	LLOG("DragDropCtrl::LeftDown -> " << pt << ", keyflags " << FormatIntHex(keyflags));
	if(Push(pt, last_key = keyflags))
	{ // begin drag & drop
		state = POSSIBLE;
		start = last = pt;
		SetCapture();
		pstartdelta=GetRect().TopLeft()-start; //aris
		RC_start=GetRect();
//		SetLabel("leftdown "+AsString(pstartdelta));

	}

}

void DragDropCtrl::Drag(Point pt, Point last, Point next, dword keyflags)
{
	Rect rc_last = Null, rc_next = Null;
	if(!IsNull(last))
		rc_last = RectSort(pt, last);
	if(!IsNull(next))
		rc_next = RectSort(pt, next);
	if(rc_last != rc_next){
		DragRect(rc_last, rc_next, keyflags);
		pdelta=next-last; //aris
		
	Rect newrc= RC_start+next-pt;// + pdelta;//pdelta;
	SetLabel("DRAG last-next:"+AsString(pstartdelta));
		SetRect(newrc);
		RC_start=GetRect();
	}
}

void DragDropCtrl::Drop(Point pt, Point end, dword keyflags)
{
	DropRect(RectSort(pt, end), keyflags);
}

void DragDropCtrl::Click(Point pt, dword keyflags)
{
	// no-op, should be implemented in derived class
	//PromptOK("click");
//	DragShow(); //aris was not
}


void DragDropCtrl::DropRect(const Rect& rc, dword keyflags)
{
	// no-op, should be implemented in derived class
}

void DragDropCtrl::DragShow(bool _show)
{
	if(_show && state == HIDDEN) {
		Drag(start, Null, last, last_key);
		state = ON;
	}
	if(!_show && state == ON) {
		Drag(start, last, Null, last_key);
		state = HIDDEN;
	}

}


void DragDropCtrl::LeftDouble(Point pt, dword keyflags)
{
	SetWantFocus();
	Click(pt, keyflags | DBLCLK);
}

//
void DragDropCtrl::DragStop(bool accept, dword keyflags)
{
	ReleaseCapture();
	DragHide();
	if(state == HIDDEN && accept)
		Drop(start, last, last_key = keyflags);
	else if(state == POSSIBLE && accept)
		Click(start, last_key = keyflags);
	state = OFF;
//	SetLabel("dragstop "+AsString(start)+" "+AsString(last));
}

//should we call overriden???
void DragDropCtrl::LeftUp(Point pt, dword keyflags)
{
	LLOG("DragDropCtrl::LeftUp -> " << pt);
	DragStop(true, keyflags);
}

void DragDropCtrl::MouseMove(Point pt, dword keyflags)
{
	LLOG("DragDropCtrl::MouseMove -> " << pt);
	if(keyflags != last_key)
		DragHide();
	if(state == POSSIBLE && IsDragDistance(pt, start))
	{
//		SetLabel("possible "+AsString(start)+" "+AsString(last));

		state = ON;
		Drag(start, Null, last = pt, last_key = keyflags);
	}
	else if(state == ON || state == HIDDEN)
	{
//	SetLabel("ON "+AsString(start)+" "+AsString(last));

		Point plast = (state == ON ? last : Point(Null));
		last = pt;
		last_key = keyflags;
		state = ON;
		Drag(start, plast, pt, last_key);
		
	}
}

//not important
bool DragDropCtrl::Key(dword key, int repcnt)
{
	if(key == K_ESCAPE)
	{
		DragStop(false);
		return true;
	}
	return Ctrl::Key(key, repcnt);
}
//===========================================

class App : public TopWindow {
	DragDropCtrl dr;
public:
    typedef App CLASSNAME;
    App();
};


App::App() {
	dr.SetRect(150,100,300,200);
    Add(dr);
//	dr.Color(SRed());
 //   btn1.WhenAction = THISBACK(testback);

    Sizeable().Zoomable();
    Title("DragDropCtrl");
}


GUI_APP_MAIN
{
    App().Run();
}

[Updated on: Mon, 22 May 2006 10:41]

Report message to a moderator

Re: how to add runtime StaticText and hook mouse events for it [message #3368 is a reply to message #3366] Mon, 22 May 2006 10:50 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
Do you really can't live without * and -> ?...
Re: how to add runtime StaticText and hook mouse events for it [message #3369 is a reply to message #3366] Mon, 22 May 2006 10:59 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
qwerty wrote on Mon, 22 May 2006 04:21

...tried few methods, but all fail, including overriding virtual metods.
Experimenting with something like that:
It wont even react on clicks.... am I missing something?


StaticText has "IgnoreMouse" flag, that is why it does not recieve any mouse events. If you insist on deriving from it and overriding mouse virtuals, you should call NoIgnoreMouse in constructor...

(BTW, the reason for IgnoreMouse is that this way, labels and label boxes do not prevent other ctrls, whose rectangles they often intersect, recieving mouse events).

Mirek
Re: how to add runtime StaticText and hook mouse events for it [message #3370 is a reply to message #3368] Mon, 22 May 2006 11:15 Go to previous messageGo to next message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
...wont live without pointers 'till master C++.

your post needs more deeper researching for me.... thanx
Re: how to add runtime StaticText and hook mouse events for it [message #3371 is a reply to message #3369] Mon, 22 May 2006 11:17 Go to previous messageGo to next message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
it works(for now), thanx luzr
Re: how to add runtime StaticText and hook mouse events for it [message #3372 is a reply to message #3370] Mon, 22 May 2006 11:43 Go to previous message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
qwerty wrote on Mon, 22 May 2006 10:15

...wont live without pointers 'till master C++.

your post needs more deeper researching for me.... thanx


sorry, I didn't study carefully your post and didn't notice you were adding new items. My post is nothing special - just a draggable button.
Previous Topic: Could anyone create a small example with the new ScrollArea?
Next Topic: changing Ctr pos, react on mouse evnt
Goto Forum:
  


Current Time: Sun May 05 09:23:08 CEST 2024

Total time taken to generate the page: 0.01695 seconds