g4tools  5.4.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tools::WinTk::Component Class Reference
Inheritance diagram for tools::WinTk::Component:
Inheritance graph
[legend]
Collaboration diagram for tools::WinTk::Component:
Collaboration graph
[legend]

Public Member Functions

 Component (const std::string &aType)
 
 Component (const std::string &aType, Component &aParent)
 
virtual ~Component ()
 
std::string name () const
 
void setName (const std::string &aName)
 
const std::string & type () const
 
HWND nativeWindow ()
 
Componentparent () const
 
virtual void show ()
 
void hide ()
 
virtual bool size (unsigned int &aWidth, unsigned int &aHeight)
 
virtual bool position (int &aX, int &aY)
 
ComponentfindFather (const std::string &aType)
 
bool setBackground (double aR, double aG, double aB)
 
void addCallback (const std::string &aName, Callback aFunction, void *aTag)
 
void removeCallback (const std::string &aName, Callback aFunction, void *aTag)
 
bool executeCallbacks (const std::string &aName, CallbackData &aData)
 
bool hasCallbacks (const std::string &aName) const
 

Protected Member Functions

 Component (Component &)
 
Componentoperator= (Component &)
 
void destroy ()
 
void rubberDrawLine (POINT &aBegin, POINT &aEnd)
 
void rubberDrawRect (POINT &aBegin, POINT &aEnd)
 

Static Protected Member Functions

static void wm__destroy (HWND aWindow)
 
static LRESULT CALLBACK containerProc (HWND aWindow, UINT aMessage, WPARAM aWParam, LPARAM aLParam)
 

Protected Attributes

HWND fWindow
 
ComponentfParent
 
std::vector< NamedCallbacks > fCallbacks
 
std::string fName
 

Detailed Description

Definition at line 35 of file WinTk.

Constructor & Destructor Documentation

◆ Component() [1/3]

tools::WinTk::Component::Component ( const std::string &  aType)
inline

Definition at line 42 of file WinTk.

42  :fType(aType),fWindow(0),fParent(0){
43 #ifdef TOOLS_MEM
44  tools::mem::increment(s_class().c_str());
45 #endif
46  }

◆ Component() [2/3]

tools::WinTk::Component::Component ( const std::string &  aType,
Component aParent 
)
inline

Definition at line 47 of file WinTk.

47  :fType(aType),fWindow(0),fParent(&aParent) {
48 #ifdef TOOLS_MEM
49  tools::mem::increment(s_class().c_str());
50 #endif
51  }

◆ ~Component()

virtual tools::WinTk::Component::~Component ( )
inlinevirtual

Definition at line 52 of file WinTk.

52  {
53 #ifdef TOOLS_MEM
54  tools::mem::decrement(s_class().c_str());
55 #endif
56  }

◆ Component() [3/3]

tools::WinTk::Component::Component ( Component )
inlineprotected

Definition at line 58 of file WinTk.

58  {
59 #ifdef TOOLS_MEM
60  tools::mem::increment(s_class().c_str());
61 #endif
62  }

Member Function Documentation

◆ addCallback()

void tools::WinTk::Component::addCallback ( const std::string &  aName,
Callback  aFunction,
void *  aTag 
)
inline

Definition at line 158 of file WinTk.

158  {
159  NamedCallbacks* cbks = 0;
160  std::vector<NamedCallbacks>::iterator it;
161  for(it=fCallbacks.begin();it!=fCallbacks.end();++it) {
162  if(aName==(*it).first) {
163  cbks = &(*it);
164  break;
165  }
166  }
167  if(!cbks) {
168  fCallbacks.push_back(std::pair<std::string,Callbacks>(aName,Callbacks()));
169  cbks = &(fCallbacks.back());
170  }
171  cbks->second.push_back(std::pair<Callback,void*>(aFunction,aTag));
172  }

◆ containerProc()

static LRESULT CALLBACK tools::WinTk::Component::containerProc ( HWND  aWindow,
UINT  aMessage,
WPARAM  aWParam,
LPARAM  aLParam 
)
inlinestaticprotected

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//

Definition at line 249 of file WinTk.

249  {
251  // Some child send notification message to their parent (instead of sending
252  // the message to themselves !). This procedure is used by containers
253  // to forward these messages to the child.
255  switch(aMessage) {
256  case WM_PARENTNOTIFY:{
257  if(aWParam==WM_CREATE) {
258  ::PostMessage(aWindow,WM_TK_CHILDCREATED,TK_KEY,0);
259  return 0;
260  } else if(aWParam==WM_DESTROY) {
261  ::PostMessage(aWindow,WM_TK_CHILDDELETED,TK_KEY,0);
262  return 0;
263  }
264  }break;
265  case WM_TK_CHILDCREATED:{
266  if(aWParam==TK_KEY) {
267  RECT rect;
268  ::GetClientRect(aWindow,&rect);
269  LPARAM size = MAKELPARAM(rect.right-rect.left,rect.bottom-rect.top);
270  ::SendMessage(aWindow,WM_SIZE,(WPARAM)0,size);
271  return 0;
272  }
273  }break;
274  case WM_TK_CHILDDELETED:{
275  if(aWParam==TK_KEY) {
276  }
277  }break;
278  case WM_NOTIFY:{ //Coming from a child (combobox, scrollbar, toolbar).
279  NMHDR* nmhdr = (NMHDR*)aLParam;
280  HWND from_win = nmhdr->hwndFrom;
281  if(from_win!=aWindow) {
282  ::SendMessage(from_win,aMessage,aWParam,aLParam);
283  }
284  }return 0;
285  case WM_COMMAND:
286  case WM_VSCROLL:
287  case WM_HSCROLL:{
288  ::SendMessage((HWND)aLParam,aMessage,aWParam,(LPARAM)0);
289  }return 0;
290  }
291  return TRUE; //Not handled.
292  }

◆ destroy()

void tools::WinTk::Component::destroy ( )
inlineprotected

Definition at line 297 of file WinTk.

297  {
298  if(hasCallbacks("delete")) {
299  CallbackData data;
300  executeCallbacks("delete",data);
301  }
302  if(fWindow) {
303  ::SetWindowLongPtr(fWindow,GWLP_USERDATA,LONG_PTR(NULL));
304  ::DestroyWindow(fWindow);
305  fWindow = 0;
306  }
307  }

◆ executeCallbacks()

bool tools::WinTk::Component::executeCallbacks ( const std::string &  aName,
CallbackData aData 
)
inline

Definition at line 193 of file WinTk.

193  {
194  std::vector<NamedCallbacks>::const_iterator it;
195  for(it=fCallbacks.begin();it!=fCallbacks.end();++it) {
196  if(aName==(*it).first) {
197  const Callbacks& cbks = (*it).second;
198  Callbacks::const_iterator it2;
199  for(it2=cbks.begin();it2!=cbks.end();++it2) {
200  Callback cbk = (*it2).first;
201  if(cbk) {
202  HWND back_fWindow = fWindow;
203  cbk(*this,aData,(*it2).second);
204  // The callback may have destroyed the Component and
205  // the native HWND. The below checks that.
206  // In a proc, it is better to not use the HWND
207  // (or the Component) after calling executeCallbacks.
208  if(back_fWindow && !::GetWindowLongPtr(back_fWindow,GWLP_USERDATA)) {
209  //::printf("WinTk::Component::executeCallbacks : WARNING : A callback destroyed the native HWND window !\n");
210  return false;
211  }
212  }
213  }
214  return true;
215  }
216  }
217  return false;
218  }

◆ findFather()

Component* tools::WinTk::Component::findFather ( const std::string &  aType)
inline

Definition at line 100 of file WinTk.

100  {
101  if(aType==type()) return this;
103  while(parent) {
104  if(aType==parent->type()) return parent;
105  parent = parent->fParent;
106  }
107  return 0;
108  }

◆ hasCallbacks()

bool tools::WinTk::Component::hasCallbacks ( const std::string &  aName) const
inline

Definition at line 219 of file WinTk.

219  {
220  std::vector<NamedCallbacks>::const_iterator it;
221  for(it=fCallbacks.begin();it!=fCallbacks.end();++it) {
222  if(aName==(*it).first) return true;
223  }
224  return false;
225  }

◆ hide()

void tools::WinTk::Component::hide ( )
inline

Definition at line 75 of file WinTk.

75  {
76  if(!fWindow) return;
77  ::ShowWindow(fWindow,SW_HIDE);
78  }

◆ name()

std::string tools::WinTk::Component::name ( ) const
inline

Definition at line 65 of file WinTk.

65 {return fName;}

◆ nativeWindow()

HWND tools::WinTk::Component::nativeWindow ( )
inline

Definition at line 69 of file WinTk.

69 {return fWindow;}

◆ operator=()

Component& tools::WinTk::Component::operator= ( Component )
inlineprotected

Definition at line 63 of file WinTk.

63 {return *this;}

◆ parent()

Component* tools::WinTk::Component::parent ( ) const
inline

Definition at line 70 of file WinTk.

70 {return fParent;}

◆ position()

virtual bool tools::WinTk::Component::position ( int &  aX,
int &  aY 
)
inlinevirtual

Definition at line 90 of file WinTk.

90  {
91  aX = 0;
92  aY = 0;
93  if(!fWindow) return false;
94  RECT rect;
95  ::GetClientRect(fWindow,&rect);
96  aX = rect.left;
97  aY = rect.top;
98  return true;
99  }

◆ removeCallback()

void tools::WinTk::Component::removeCallback ( const std::string &  aName,
Callback  aFunction,
void *  aTag 
)
inline

Definition at line 173 of file WinTk.

173  {
174  NamedCallbacks* cbks = 0;
175  std::vector<NamedCallbacks>::iterator it;
176  for(it=fCallbacks.begin();it!=fCallbacks.end();++it) {
177  if(aName==(*it).first) {
178  cbks = &(*it);
179  break;
180  }
181  }
182  if(cbks) {
183  Callbacks::iterator it;
184  for(it=cbks->second.begin();it!=cbks->second.end();++it) {
185  if( (aFunction==(*it).first) && (aTag==(*it).second) ) {
186  cbks->second.erase(it);
187  return;
188  }
189  }
190  }
191  }

◆ rubberDrawLine()

void tools::WinTk::Component::rubberDrawLine ( POINT &  aBegin,
POINT &  aEnd 
)
inlineprotected

Definition at line 308 of file WinTk.

308  {
309  if(!fWindow) return;
310  HPEN pen = ::CreatePen(PS_SOLID,5,RGB(0,0,0));
311  HDC hdc = ::GetWindowDC(fWindow);
312  HPEN oldPen = SelectPen(hdc,pen);
313  int oldROP = ::SetROP2(hdc,R2_NOT);
314  POINT pt;
315  ::MoveToEx(hdc,aBegin.x,aBegin.y,&pt);
316  ::LineTo(hdc,aEnd.x,aEnd.y);
317  ::SetROP2(hdc,oldROP);
318  SelectPen(hdc,oldPen);
319  ::ReleaseDC(fWindow,hdc);
320  ::DeletePen(pen);
321  }

◆ rubberDrawRect()

void tools::WinTk::Component::rubberDrawRect ( POINT &  aBegin,
POINT &  aEnd 
)
inlineprotected

Definition at line 322 of file WinTk.

322  {
323  if(!fWindow) return;
324  HPEN pen = ::CreatePen(PS_SOLID,0,RGB(0,0,0));
325  HDC hdc = ::GetWindowDC(fWindow);
326  HPEN oldPen = SelectPen(hdc,pen);
327  int oldROP = ::SetROP2(hdc,R2_NOT);
328  POINT pt;
329  ::MoveToEx(hdc,aBegin.x,aBegin.y,&pt);
330  ::LineTo(hdc,aBegin.x,aEnd.y);
331  ::LineTo(hdc,aEnd.x,aEnd.y);
332  ::LineTo(hdc,aEnd.x,aBegin.y);
333  ::LineTo(hdc,aBegin.x,aBegin.y);
334  ::SetROP2(hdc,oldROP);
335  SelectPen(hdc,oldPen);
336  ::ReleaseDC(fWindow,hdc);
337  ::DeletePen(pen);
338  }

◆ setBackground()

bool tools::WinTk::Component::setBackground ( double  aR,
double  aG,
double  aB 
)
inline

Definition at line 110 of file WinTk.

110  {
111  if(!fWindow) return false;
112 
113  int r = int(aR*255.0);
114  if((r<0)||(r>255)) return false;
115  int g = int(aG*255.0);
116  if((g<0)||(g>255)) return false;
117  int b = int(aB*255.0);
118  if((b<0)||(b>255)) return false;
119 
120 /*
121  //COLORREF cr = RGB(r,g,b);
122  HBRUSH brush = ::CreateSolidBrush(RGB(r,g,b));
123  ::printf("debug : WinTk::Component::setBackground : brush %lu\n",brush);
124 
125  HBRUSH prev = (HBRUSH)::GetClassLongPtr(fWindow,GCLP_HBRBACKGROUND);
126  ::printf("debug : WinTk::Component::setBackground : prev brush %lu\n",prev);
127 
128  {WNDCLASSEX wc;
129  ::GetClassInfoEx(::GetModuleHandle(NULL),WC_STATIC,&wc);
130  ::printf("debug : prev brush xxx %lu\n",wc.hbrBackground);}
131 
132  ULONG_PTR stat =
133  ::SetClassLongPtr(fWindow,GCLP_HBRBACKGROUND,(LONG_PTR)brush);
134  if(prev && !stat) {
135  ::printf("debug : WinTk::Component::setBackground : SetClassLongPtr failed.\n");
136  return false;
137  }
138 
139  {ULONG_PTR stat =
140  ::SetClassLongPtr(fWindow,GCLP_HBRBACKGROUND,(LONG_PTR)brush);
141  ::printf("debug : WinTk::Component::setBackground : SetClassLongPtr failed.yyy %lu.\n",stat);}
142 
143  {WNDCLASSEX wc;
144  ::GetClassInfoEx(::GetModuleHandle(NULL),WC_STATIC,&wc);
145  ::printf("debug : prev brush yyy %lu\n",wc.hbrBackground);}
146 
147  if(!::InvalidateRect(fWindow,NULL,TRUE)) {
148  ::printf("debug : WinTk::Component::setBackground : InvalidateRect failed.\n");
149  return false;
150  }
151 
152  //::DeleteObject(brush);
153  return true;
154  */
155  return false;
156  }

◆ setName()

void tools::WinTk::Component::setName ( const std::string &  aName)
inline

Definition at line 66 of file WinTk.

66 {fName = aName;}

◆ show()

virtual void tools::WinTk::Component::show ( )
inlinevirtual

Reimplemented in tools::WinTk::Shell.

Definition at line 71 of file WinTk.

71  {
72  //if(!fWindow) return;
73  //::ShowWindow(fWindow,SW_SHOWDEFAULT);
74  }

◆ size()

virtual bool tools::WinTk::Component::size ( unsigned int &  aWidth,
unsigned int &  aHeight 
)
inlinevirtual

Definition at line 80 of file WinTk.

80  {
81  aWidth = 0;
82  aHeight = 0;
83  if(!fWindow) return false;
84  RECT rect;
85  ::GetClientRect(fWindow,&rect);
86  aWidth = rect.right-rect.left;
87  aHeight = rect.bottom-rect.top;
88  return true;
89  }

◆ type()

const std::string& tools::WinTk::Component::type ( ) const
inline

Definition at line 68 of file WinTk.

68 {return fType;}

◆ wm__destroy()

static void tools::WinTk::Component::wm__destroy ( HWND  aWindow)
inlinestaticprotected

Definition at line 227 of file WinTk.

227  {
228  Component* This = (Component*)::GetWindowLongPtr(aWindow,GWLP_USERDATA);
229  if(This) { //How to be sure that we have a Component* ???
230  if(This->fWindow!=aWindow) {
231  ::printf("WinTk::Component::wm_destroy : HWND mismatch !\n");
232  }
233  if(This->hasCallbacks("WM_DESTROY")) {
234  CallbackData data;
235  This->executeCallbacks("WM_DESTROY",data);
236  }
237  This->fWindow = 0;
238  }
239  ::SetWindowLongPtr(aWindow,GWLP_USERDATA,LONG_PTR(NULL));
240  }

Member Data Documentation

◆ fCallbacks

std::vector<NamedCallbacks> tools::WinTk::Component::fCallbacks
protected

Definition at line 344 of file WinTk.

◆ fName

std::string tools::WinTk::Component::fName
protected

Definition at line 345 of file WinTk.

◆ fParent

Component* tools::WinTk::Component::fParent
protected

Definition at line 343 of file WinTk.

◆ fWindow

HWND tools::WinTk::Component::fWindow
protected

Definition at line 342 of file WinTk.


The documentation for this class was generated from the following file:
tools::WinTk::Component::executeCallbacks
bool executeCallbacks(const std::string &aName, CallbackData &aData)
Definition: WinTk:193
WM_TK_CHILDDELETED
#define WM_TK_CHILDDELETED
Definition: WinTk:247
tools::WinTk::Component::size
virtual bool size(unsigned int &aWidth, unsigned int &aHeight)
Definition: WinTk:80
tools::WinTk::Callback
void(* Callback)(Component &, CallbackData &, void *)
Definition: WinTk:33
tools::WinTk::Component::type
const std::string & type() const
Definition: WinTk:68
WM_TK_CHILDCREATED
#define WM_TK_CHILDCREATED
Definition: WinTk:246
tools::WinTk::Component::fParent
Component * fParent
Definition: WinTk:343
tools::WinTk::Component::Component
Component(const std::string &aType)
Definition: WinTk:42
tools::WinTk::Component::hasCallbacks
bool hasCallbacks(const std::string &aName) const
Definition: WinTk:219
tools::WinTk::Component::fWindow
HWND fWindow
Definition: WinTk:342
TK_KEY
#define TK_KEY
Definition: WinTk:248
tools::WinTk::Component::parent
Component * parent() const
Definition: WinTk:70
tools::WinTk::Component::fName
std::string fName
Definition: WinTk:345
tools::WinTk::Component::fCallbacks
std::vector< NamedCallbacks > fCallbacks
Definition: WinTk:344