Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / utils / NamingViewer / NamingTreeCtrl.cpp
blob9ffe80324baadcf1564f6f6ec7bc86b39fc30433
1 #include "stdafx.h"
2 #include "NamingViewer.h"
3 #include "NamingTreeCtrl.h"
4 #include "ViewIORDialog.h"
5 #include "NamingObject.h"
6 #include "BindDialog.h"
7 #include "BindNewContext.h"
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
15 /////////////////////////////////////////////////////////////////////////////
16 // CNamingTreeCtrl
18 CNamingTreeCtrl::CNamingTreeCtrl ()
20 m_ContextPopup.LoadMenu (IDR_CONTEXT_POPUP);
21 m_ObjectPopup.LoadMenu (IDR_OBJECT_POPUP);
24 CNamingTreeCtrl::~CNamingTreeCtrl ()
28 BEGIN_MESSAGE_MAP(CNamingTreeCtrl, CTreeCtrl)
29 //{{AFX_MSG_MAP(CNamingTreeCtrl)
30 ON_WM_RBUTTONDOWN()
31 ON_COMMAND(ID_CONTEXT_POPUP_VIEWREFERENCE, OnContextPopupViewreference)
32 ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
33 ON_COMMAND(ID_CONTEXT_POPUP_REFRESH, OnContextPopupRefresh)
34 ON_COMMAND(ID_CONTEXT_POPUP_UNBIND, OnContextPopupUnbind)
35 ON_COMMAND(ID_CONTEXT_POPUP_DESTROY, OnContextPopupDestroy)
36 ON_COMMAND(ID_CONTEXT_POPUP_BIND_CONTEXT, OnContextPopupBindContext)
37 ON_COMMAND(ID_CONTEXT_POPUP_BINDOBJECT, OnContextPopupBindobject)
38 ON_WM_DESTROY()
39 ON_COMMAND(ID_CONTEXTPOPUP_BINDNEWCONTEXT, OnContextpopupBindnewcontext)
40 ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
41 ON_COMMAND(ID_OBJECTPOPUP_UNBIND, OnObjectpopupUnbind)
42 ON_COMMAND(ID_OBJECTPOPUP_VIEWREFRENCE, OnObjectpopupViewrefrence)
43 //}}AFX_MSG_MAP
44 END_MESSAGE_MAP()
46 /////////////////////////////////////////////////////////////////////////////
47 // CNamingTreeCtrl message handlers
49 void CNamingTreeCtrl::OnRButtonDown (UINT nFlags, CPoint point)
51 // TODO: Add your message handler code here and/or call default
52 // Special case here - this causes the entry to be selected when the
53 // right button is the first to be hit. strange
54 OnLButtonDown (nFlags, point);
56 // Now find the item were hitting
57 HTREEITEM hItem = HitTest(point);
58 if(!hItem)
60 return;
62 SelectItem(hItem);
64 POINT Point = point;
65 ClientToScreen(&Point);
66 CNamingObject* pObject = GetTreeObject(hItem);
68 // If this is not a context, show the object popup
69 if(!pObject->IsContext())
71 if(!m_ObjectPopup.GetSubMenu(0)->TrackPopupMenu(
72 #if defined (TPM_RIGHTBUTTON)
73 TPM_RIGHTBUTTON |
74 #endif
75 TPM_LEFTALIGN,
76 Point.x, Point.y, this))
78 TRACE0("TrackPopupMenu Failed");
81 else
83 if(!m_ContextPopup.GetSubMenu(0)->TrackPopupMenu(
84 #if defined (TPM_RIGHTBUTTON)
85 TPM_RIGHTBUTTON |
86 #endif
87 TPM_LEFTALIGN,
88 Point.x, Point.y, this))
90 TRACE0("TrackPopupMenu Failed");
95 void CNamingTreeCtrl::OnContextPopupViewreference()
97 // TODO: Add your command handler code here
98 ViewIORDialog Dialog(m_pORB, GetTreeObject()->Object());
99 Dialog.DoModal();
102 void CNamingTreeCtrl::SetpORB(CORBA::ORB_ptr pORB)
104 m_pORB = pORB;
107 CNamingObject* CNamingTreeCtrl::GetTreeObject(HTREEITEM hItem)
109 if(hItem == 0)
111 hItem = GetSelectedItem();
112 if(!hItem)
114 return 0;
117 CNamingObject* pObject = (CNamingObject*)GetItemData(hItem);
118 return pObject;
121 void CNamingTreeCtrl::ClearChildren(HTREEITEM hItem)
123 if(hItem == 0)
125 HTREEITEM hItem = GetRootItem();
126 if(hItem)
128 //CORBA::Object_var Object = (CORBA::Object_ptr)GetItemData(hItem);
129 ClearChildren(hItem);
130 delete GetTreeObject(hItem);
131 DeleteItem(hItem);
133 return;
136 HTREEITEM hChild;
137 while(hChild = GetNextItem(hItem, TVGN_CHILD))
139 // Remove our reference count on the object reference
140 ClearChildren(hChild);
141 delete GetTreeObject(hChild);
142 DeleteItem(hChild);
147 #define LISTQUANTUM 40
149 void CNamingTreeCtrl::ListContext(HTREEITEM hItem)
151 CWaitCursor Waiter;
154 // Get the items object and make sure we have a context
155 CNamingObject* pObject = GetTreeObject(hItem);
156 CosNaming::NamingContext_var Context = pObject->NamingContext();
157 if(CORBA::is_nil(Context))
159 return;
162 // List the contexts entries
163 CosNaming::BindingList_var bl;
164 CosNaming::BindingIterator_var bi;
165 Context->list(LISTQUANTUM, bl, bi);
166 ListBindingList(hItem, Context, bl);
168 if(!CORBA::is_nil(bi))
170 while(bl->length())
172 CString Text;
173 Text.Format(ACE_TEXT ("This context contains more than %d entries, list the next %d?"), LISTQUANTUM, LISTQUANTUM);
174 if(MessageBox(Text, ACE_TEXT ("Question"), MB_YESNO) == IDNO)
176 return;
178 bi->next_n(LISTQUANTUM, bl);
179 ListBindingList(hItem, Context, bl);
181 bi->destroy();
184 catch(CORBA::Exception& ex)
186 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
190 void CNamingTreeCtrl::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult)
192 NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
193 // TODO: Add your control notification handler code here
194 *pResult = 0;
195 // If this item has a child, it has already been listed so nothing to do..
196 if(GetChildItem(pNMTreeView->itemNew.hItem))
198 return;
200 ListContext(pNMTreeView->itemNew.hItem);
203 void CNamingTreeCtrl::OnContextPopupRefresh()
205 // TODO: Add your command handler code here
206 HTREEITEM hItem = GetSelectedItem();
207 ClearChildren(hItem);
208 ListContext(hItem);
211 void CNamingTreeCtrl::OnContextPopupUnbind()
213 // TODO: Add your command handler code here
214 if(MessageBox(ACE_TEXT ("Are you sure you want to unbind this object?"),
215 ACE_TEXT ("Confirm"), MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
217 return;
219 HTREEITEM hItem = GetSelectedItem();
220 HTREEITEM hParent = GetParentItem(hItem);
221 if(!hParent)
223 return;
225 CNamingObject* pObject = GetTreeObject(hItem);
226 CNamingObject* pParent= GetTreeObject(hParent);
227 CosNaming::NamingContext_var Context = pParent->NamingContext();
230 Context->unbind(pObject->Name());
231 ClearChildren(hItem);
232 delete pObject;
233 DeleteItem(hItem);
235 catch(CORBA::Exception& ex)
237 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
241 void CNamingTreeCtrl::Resolve(CosNaming::NamingContext_ptr pRootContext)
243 ClearChildren();
244 if(!CORBA::is_nil(pRootContext))
246 HTREEITEM hItem = InsertItem(ACE_TEXT ("Root"));
247 CosNaming::Name Name;
248 Name.length(1);
249 Name[0].id = CORBA::string_dup("Root");
250 SetItemData(hItem, (DWORD)new CNamingObject(Name, pRootContext, true));
251 ListContext(hItem);
255 void CNamingTreeCtrl::OnContextPopupDestroy()
257 // TODO: Add your command handler code here
258 if(MessageBox(ACE_TEXT ("Are you sure you want to destroy this context?"),
259 ACE_TEXT ("Confirm"), MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
261 return;
263 HTREEITEM hItem = GetSelectedItem();
264 HTREEITEM hParent = GetParentItem(hItem);
265 if(!hParent)
267 return;
269 CNamingObject* pObject = GetTreeObject(hItem);
270 CNamingObject* pParent= GetTreeObject(hParent);
271 CosNaming::NamingContext_var Parent = pParent->NamingContext();
274 // First try to destroy, it will raise exception if its not empty
275 CosNaming::NamingContext_var Context = pObject->NamingContext();
276 Context->destroy();
277 // Ok its destroyed, clean up any children we might have laying around
278 ClearChildren(hItem);
279 DeleteItem(hItem);
280 // do the unbind
281 Parent->unbind(pObject->Name());
282 delete pObject;
284 catch(CORBA::Exception& ex)
286 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
290 void CNamingTreeCtrl::OnContextPopupBindContext()
292 // TODO: Add your command handler code here
293 CBindDialog Dialog(true, m_pORB);
294 if(Dialog.DoModal() != IDOK)
296 return;
300 CNamingObject* pObject = GetTreeObject();
301 CosNaming::NamingContext_var Context = pObject->NamingContext();
302 if(CORBA::is_nil(Context.in ()))
304 return;
306 CosNaming::NamingContext_var NewContext = CosNaming::NamingContext::_narrow(Dialog.GetObject());
307 if(CORBA::is_nil(NewContext.in ()))
309 AfxMessageBox(ACE_TEXT ("Object is not a CosNaming::NamingContext"));
310 return;
312 Context->bind_context(Dialog.GetName(), NewContext);
313 OnContextPopupRefresh();
315 catch(CORBA::Exception& ex)
317 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
321 void CNamingTreeCtrl::OnContextPopupBindobject()
323 // TODO: Add your command handler code here
324 CBindDialog Dialog(false, m_pORB);
325 if(Dialog.DoModal() != IDOK)
327 return;
331 HTREEITEM hItem = GetSelectedItem();
332 CNamingObject* pObject = GetTreeObject(hItem);
333 CosNaming::NamingContext_var Context = pObject->NamingContext();
334 if(CORBA::is_nil(Context.in ()))
336 return;
338 Context->bind(Dialog.GetName(), Dialog.GetObject());
339 OnContextPopupRefresh();
341 catch(CORBA::Exception& ex)
343 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
347 void CNamingTreeCtrl::OnDestroy()
349 CTreeCtrl::OnDestroy();
351 // TODO: Add your message handler code here
352 ClearChildren();
356 void CNamingTreeCtrl::OnContextpopupBindnewcontext()
358 // TODO: Add your command handler code here
359 HTREEITEM hItem = GetSelectedItem();
360 CNamingObject* pObject = GetTreeObject(hItem);
361 CosNaming::NamingContext_var Context = pObject->NamingContext();
362 if(CORBA::is_nil(Context.in ()))
364 return;
366 CBindNewContext Dialog;
367 if(Dialog.DoModal() != IDOK)
369 return;
373 CosNaming::NamingContext_var NewContext = Context->new_context();
374 Context->bind_context(Dialog.GetName(), NewContext);
375 OnContextPopupRefresh();
377 catch(CORBA::Exception& ex)
379 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
383 void CNamingTreeCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
385 // TODO: Add your control notification handler code here
386 CNamingObject* pObject = GetTreeObject();
387 // Only display non contexts
388 if(!pObject->IsContext())
390 ViewIORDialog Dialog(m_pORB, pObject->Object());
391 Dialog.DoModal();
394 *pResult = 0;
397 void CNamingTreeCtrl::OnCopy()
399 // TODO: Add your command handler code here
400 CNamingObject* pObject = GetTreeObject();
403 CString IOR = m_pORB->object_to_string(pObject->Object());
404 // Copy to the clipboard by using the CEdit control. This is easier
405 // that doing it the right way
406 CEdit Temp;
407 CRect None(0,0, 1, 1);
408 Temp.Create(0, None, this, 0);
409 Temp.SetWindowText(IOR);
410 Temp.SetSel(0, IOR.GetLength());
411 Temp.Copy();
412 Temp.PostMessage(WM_CLOSE);
414 catch(CORBA::Exception& ex)
416 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
420 LRESULT CNamingTreeCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
422 // TODO: Add your specialized code here and/or call the base class
423 if(message == WM_HOTKEY)
425 // To trap control-c (for copying) we registered a hotkey. For some reason
426 // MFC wasn't calling my OnHotKey() function that I registered so I am forcing
427 // it this way. Anyone know the right way to do this?
428 OnCopy();
430 return CTreeCtrl::WindowProc(message, wParam, lParam);
433 void CNamingTreeCtrl::OnObjectpopupUnbind()
435 // TODO: Add your command handler code here
436 if(MessageBox(ACE_TEXT ("Are you sure you want to unbind this object?"),
437 ACE_TEXT ("Confirm"), MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
439 return;
441 HTREEITEM hItem = GetSelectedItem();
442 HTREEITEM hParent = GetParentItem(hItem);
443 if(!hParent)
445 return;
447 CNamingObject* pObject = GetTreeObject(hItem);
448 CNamingObject* pParent= GetTreeObject(hParent);
449 CosNaming::NamingContext_var Context = pParent->NamingContext();
452 Context->unbind(pObject->Name());
453 ClearChildren(hItem);
454 delete pObject;
455 DeleteItem(hItem);
457 catch(CORBA::Exception& ex)
459 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
463 void CNamingTreeCtrl::OnObjectpopupViewrefrence()
465 // TODO: Add your command handler code here
466 ViewIORDialog Dialog(m_pORB, GetTreeObject()->Object());
467 Dialog.DoModal();
470 void CNamingTreeCtrl::ListBindingList(HTREEITEM hItem, CosNaming::NamingContext_ptr pContext, CosNaming::BindingList_var& bl)
474 for(unsigned int i=0; i < bl->length(); i++)
476 // Add each entry into the tree control
477 CORBA::Object_var Object = pContext->resolve(bl[i].binding_name);
478 bool Context = (bl[i].binding_type == CosNaming::ncontext);
479 CNamingObject* pNewObject = new CNamingObject(bl[i].binding_name, Object, Context);
480 CString Name;
481 const char* pKind = (bl[i].binding_name[0]).kind;
482 if(*pKind)
484 Name.Format(ACE_TEXT ("%s | %s"), (bl[i].binding_name[0]).id, pKind);
486 else
488 Name.Format(ACE_TEXT ("%s"), (bl[i].binding_name[0]).id);
490 HTREEITEM hContext = InsertItem(Name, hItem);
491 SetItemData(hContext, (DWORD)pNewObject);
492 switch(bl[i].binding_type)
494 case CosNaming::ncontext:
496 // Set the children flag so the + button is displayed
497 TV_ITEM Item;
498 Item.mask = TVIF_CHILDREN | TVIF_HANDLE;
499 Item.cChildren = 1;
500 Item.hItem = hContext;
501 SetItem(&Item);
503 break;
504 case CosNaming::nobject:
505 break;
509 catch(CORBA::Exception& ex)
511 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));