wxDataViewTreeCtrl remove selection but retain item focus

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Thu May 16, 2019 7:28 am

Hi,
I have this nagging problem where I need to unselect item but still retain focus to show that it is the "current" item. I can use Unselect and does what I want except it does not retain item focus. I have checked documentation, I cannot find similar thing.

All I want is to be able to select same Item twice. If I click it twice then it should send selected event

Any idea is appreciated!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

User avatar
doublemax
Moderator
Moderator
Posts: 13899
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by doublemax » Thu May 16, 2019 8:20 am

wxDataViewCtrl::SetCurrentItem
https://docs.wxwidgets.org/trunk/classw ... bd91727ac6

It has some limitations though as mentioned in the docs.
Use the source, Luke!

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Thu May 16, 2019 11:08 am

Hi DM,
doublemax wrote:
Thu May 16, 2019 8:20 am
wxDataViewCtrl::SetCurrentItem
https://docs.wxwidgets.org/trunk/classw ... bd91727ac6

It has some limitations though as mentioned in the docs.
I have tried but it emits SELECTION_CHANGED event so it keeps the loop endless, that is Handler --> SetCurrentItem -->Handler... ad infinitum

I'm not sure what to do about it. Morever docs says it selects item, somthing i don't want to do.

Did I miss anything?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

User avatar
doublemax
Moderator
Moderator
Posts: 13899
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by doublemax » Thu May 16, 2019 11:42 am

So you are using a single-selection control? I don't really see how it makes sense to have an item "active" but not selected. What's the difference in behavior when the item is active in comparison to being selected?
Use the source, Luke!

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Thu May 16, 2019 11:56 am

doublemax wrote:
Thu May 16, 2019 11:42 am
So you are using a single-selection control? I don't really see how it makes sense to have an item "active" but not selected. What's the difference in behavior when the item is active in comparison to being selected?
Well it is multiselect. May be I have tried too much and lost a sight of simple thing I want to do.
Basically I want to Expand node when selected and collapse when selected again. Currently wxDVC does not support. I have to select item, select another item and re-select the same. I want to avoid that.

Here is a summary:
- Node clicked - Expand
- Same node clicked again - Collapse

only that
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

User avatar
doublemax
Moderator
Moderator
Posts: 13899
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by doublemax » Thu May 16, 2019 1:05 pm

Try this (patch to the dataview sample):

Code: Select all

diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp
index 53a80d920d5e5ec02372e806aef982d948d75f6a..ebe87c1f9ed7eba554f89d2035d361acbe18a1ba 100644
--- a/samples/dataview/dataview.cpp
+++ b/samples/dataview/dataview.cpp
@@ -156,6 +156,7 @@ private:
 #endif // wxUSE_DRAG_AND_DROP
 
     void OnDataViewChar(wxKeyEvent& event);
+    void OnDataViewLeftUp(wxMouseEvent& event);
 
     // helper used by both OnDeleteSelected() and OnDataViewChar()
     void DeleteSelectedItems();
@@ -878,6 +879,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
                 new wxDataViewTreeCtrl( parent, wxID_ANY, wxDefaultPosition,
                                         wxDefaultSize, style | wxDV_NO_HEADER );
             m_ctrl[3] = tc;
+            m_ctrl[3]->GetMainWindow()->Bind(wxEVT_LEFT_UP, &MyFrame::OnDataViewLeftUp, this);
 
             wxImageList *ilist = new wxImageList( 16, 16 );
             ilist->Add( wxIcon(wx_small_xpm) );
@@ -1480,6 +1482,20 @@ void MyFrame::OnDataViewChar(wxKeyEvent& event)
         event.Skip();
 }
 
+void MyFrame::OnDataViewLeftUp(wxMouseEvent& event)
+{
+  wxDataViewItem item;
+  wxDataViewColumn *col;
+  wxDataViewTreeCtrl *dv = (wxDataViewTreeCtrl *)m_ctrl[3];
+  dv->HitTest( event.GetPosition(), item, col );
+  if( item && dv->IsContainer(item) ) {
+    dv->IsExpanded(item) ? dv->Collapse( item ) : dv->Expand( item );
+    return;
+  }
+
+  event.Skip();
+}
+
 // ----------------------------------------------------------------------------
 // MyFrame - event handlers for the second page
 // ----------------------------------------------------------------------------
Use the source, Luke!

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Fri May 17, 2019 9:10 am

doublemax wrote:
Thu May 16, 2019 1:05 pm
Try this (patch to the dataview sample):

Code: Select all

diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp
index 53a80d920d5e5ec02372e806aef982d948d75f6a..ebe87c1f9ed7eba554f89d2035d361acbe18a1ba 100644
--- a/samples/dataview/dataview.cpp
+++ b/samples/dataview/dataview.cpp
@@ -156,6 +156,7 @@ private:
 #endif // wxUSE_DRAG_AND_DROP
 
     void OnDataViewChar(wxKeyEvent& event);
+    void OnDataViewLeftUp(wxMouseEvent& event);
 
     // helper used by both OnDeleteSelected() and OnDataViewChar()
     void DeleteSelectedItems();
@@ -878,6 +879,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
                 new wxDataViewTreeCtrl( parent, wxID_ANY, wxDefaultPosition,
                                         wxDefaultSize, style | wxDV_NO_HEADER );
             m_ctrl[3] = tc;
+            m_ctrl[3]->GetMainWindow()->Bind(wxEVT_LEFT_UP, &MyFrame::OnDataViewLeftUp, this);
 
             wxImageList *ilist = new wxImageList( 16, 16 );
             ilist->Add( wxIcon(wx_small_xpm) );
@@ -1480,6 +1482,20 @@ void MyFrame::OnDataViewChar(wxKeyEvent& event)
         event.Skip();
 }
 
+void MyFrame::OnDataViewLeftUp(wxMouseEvent& event)
+{
+  wxDataViewItem item;
+  wxDataViewColumn *col;
+  wxDataViewTreeCtrl *dv = (wxDataViewTreeCtrl *)m_ctrl[3];
+  dv->HitTest( event.GetPosition(), item, col );
+  if( item && dv->IsContainer(item) ) {
+    dv->IsExpanded(item) ? dv->Collapse( item ) : dv->Expand( item );
+    return;
+  }
+
+  event.Skip();
+}
+
 // ----------------------------------------------------------------------------
 // MyFrame - event handlers for the second page
 // ----------------------------------------------------------------------------
Stange thing happened here, no event is detected on Mac.
Does it work on Windows?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

User avatar
doublemax
Moderator
Moderator
Posts: 13899
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by doublemax » Fri May 17, 2019 9:20 am

Stange thing happened here, no event is detected on Mac.
Does it work on Windows?
Yes, it works under Windows. I can't test under OSX.

However, Windows uses a generic implementation, OSX a native one. And especially under OSX, i wouldn't try to change the native behavior.
Use the source, Luke!

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Fri May 17, 2019 9:24 am

Actually none of the mouse event (left or right) is working
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

User avatar
doublemax
Moderator
Moderator
Posts: 13899
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by doublemax » Fri May 17, 2019 9:31 am

evstevemd wrote:
Fri May 17, 2019 9:24 am
Actually none of the mouse event (left or right) is working
The OSX control probably isn't a composite control.

Try replacing

Code: Select all

m_ctrl[3]->GetMainWindow()->Bind(...)
with

Code: Select all

m_ctrl[3]->Bind(...)
You could also try to catch wxEVT_LEFT_DOWN instead of wxEVT_LEFT_UP.

And this only works on the 4th page of the sample, the wxDataViewTreeCtrl page.
Use the source, Luke!

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Fri May 17, 2019 9:53 am

doublemax wrote:
Fri May 17, 2019 9:31 am
The OSX control probably isn't a composite control.
What does that mean? I have checked its code, it is difficult to follow Objective-C for me.
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Fri May 17, 2019 9:54 am

doublemax wrote:
Fri May 17, 2019 9:31 am
Try replacing

Code: Select all

m_ctrl[3]->GetMainWindow()->Bind(...)
with

Code: Select all

m_ctrl[3]->Bind(...)
You could also try to catch wxEVT_LEFT_DOWN instead of wxEVT_LEFT_UP.

And this only works on the 4th page of the sample, the wxDataViewTreeCtrl page.
All these don't emit any event at all
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

User avatar
doublemax
Moderator
Moderator
Posts: 13899
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by doublemax » Fri May 17, 2019 10:57 am

evstevemd wrote:
Fri May 17, 2019 9:53 am
doublemax wrote:
Fri May 17, 2019 9:31 am
The OSX control probably isn't a composite control.
What does that mean? I have checked its code, it is difficult to follow Objective-C for me.
The generic implementation of DVC is composite control, meaning it's a composition of several windows, for the header, the column labels, the row labels and the main content. (Similar to wxGrid). In these cases it's important to catch the mouse events from the correct window.
Use the source, Luke!

User avatar
doublemax
Moderator
Moderator
Posts: 13899
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by doublemax » Fri May 17, 2019 11:07 am

evstevemd wrote:
Fri May 17, 2019 9:54 am
doublemax wrote:
Fri May 17, 2019 9:31 am
Try replacing

Code: Select all

m_ctrl[3]->GetMainWindow()->Bind(...)
with

Code: Select all

m_ctrl[3]->Bind(...)
You could also try to catch wxEVT_LEFT_DOWN instead of wxEVT_LEFT_UP.

And this only works on the 4th page of the sample, the wxDataViewTreeCtrl page.
All these don't emit any event at all
Sorry, i have no idea what to do about this. Maybe it's just not possible with the OSX control.
Use the source, Luke!

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxDataViewTreeCtrl remove selection but retain item focus

Post by evstevemd » Fri May 17, 2019 11:45 am

doublemax wrote:
Fri May 17, 2019 11:07 am
Sorry, i have no idea what to do about this. Maybe it's just not possible with the OSX control.
Thank you for your help. Let me ask on the dev-list!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

Post Reply