1515using  System . Windows . Forms ; 
1616using  Rubberduck . UI . Command ; 
1717using  Rubberduck . UI . Command . MenuItems . CommandBars ; 
18+ using  Rubberduck . VBEditor . Events ; 
1819using  Rubberduck . VBEditor . SafeComWrappers ; 
1920using  Rubberduck . VBEditor . SafeComWrappers . Abstract ; 
2021using  Rubberduck . VBEditor . SafeComWrappers . MSForms ; 
2122using  Rubberduck . VBEditor . SafeComWrappers . Office . Core . Abstract ; 
2223using  Rubberduck . VersionCheck ; 
24+ using  Application  =  System . Windows . Forms . Application ; 
2325
2426namespace  Rubberduck 
2527{ 
@@ -61,7 +63,9 @@ public App(IVBE vbe,
6163            _version  =  version ; 
6264            _checkVersionCommand  =  checkVersionCommand ; 
6365
64-             _hooks . MessageReceived  +=  _hooks_MessageReceived ; 
66+             VBEEvents . SelectionChanged  +=  _vbe_SelectionChanged ; 
67+             VBEEvents . WindowFocusChange  +=  _vbe_FocusChanged ; 
68+ 
6569            _configService . SettingsChanged  +=  _configService_SettingsChanged ; 
6670            _parser . State . StateChanged  +=  Parser_StateChanged ; 
6771            _parser . State . StatusMessageUpdate  +=  State_StatusMessageUpdate ; 
@@ -81,17 +85,49 @@ private void State_StatusMessageUpdate(object sender, RubberduckStatusMessageEve
8185            _stateBar . SetStatusLabelCaption ( message ,  _parser . State . ModuleExceptions . Count ) ; 
8286        } 
8387
84-         private  void  _hooks_MessageReceived ( object  sender ,  HookEventArgs  e ) 
88+         private  void  _vbe_SelectionChanged ( object  sender ,  SelectionChangedEventArgs  e ) 
8589        { 
86-             RefreshSelection ( ) ; 
90+             RefreshSelection ( e . CodePane ) ; 
91+         } 
92+ 
93+         private  void  _vbe_FocusChanged ( object  sender ,  WindowChangedEventArgs  e ) 
94+         { 
95+             if  ( e . EventType  ==  WindowChangedEventArgs . FocusType . GotFocus ) 
96+             { 
97+                 RefreshSelection ( e . Window ) ; 
98+             }           
8799        } 
88100
89101        private  ParserState  _lastStatus ; 
90102        private  Declaration  _lastSelectedDeclaration ; 
91- 
92-         private  void  RefreshSelection ( ) 
103+         private  void  RefreshSelection ( ICodePane  pane ) 
93104        { 
105+             Declaration  selectedDeclaration  =  null ; 
106+             if  ( ! pane . IsWrappingNullReference ) 
107+             { 
108+                 selectedDeclaration  =  _parser . State . FindSelectedDeclaration ( pane ) ; 
109+                 var  refCount  =  selectedDeclaration  ==  null  ?  0  :  selectedDeclaration . References . Count ( ) ; 
110+                 var  caption  =  _stateBar . GetContextSelectionCaption ( _vbe . ActiveCodePane ,  selectedDeclaration ) ; 
111+                 _stateBar . SetContextSelectionCaption ( caption ,  refCount ) ; 
112+             } 
94113
114+             var  currentStatus  =  _parser . State . Status ; 
115+             if  ( ShouldEvaluateCanExecute ( selectedDeclaration ,  currentStatus ) ) 
116+             { 
117+                 _appMenus . EvaluateCanExecute ( _parser . State ) ; 
118+                 _stateBar . EvaluateCanExecute ( _parser . State ) ; 
119+             } 
120+ 
121+             _lastStatus  =  currentStatus ; 
122+             _lastSelectedDeclaration  =  selectedDeclaration ; 
123+         } 
124+ 
125+         private  void  RefreshSelection ( IWindow  window ) 
126+         { 
127+             if  ( window . IsWrappingNullReference  ||  window . Type  !=  WindowKind . Designer ) 
128+             { 
129+                 return ; 
130+             } 
95131            var  caption  =  String . Empty ; 
96132            var  refCount  =  0 ; 
97133
@@ -103,7 +139,7 @@ private void RefreshSelection()
103139
104140            //TODO - I doubt this is the best way to check if the SelectedVBComponent and the ActiveCodePane are the same component. 
105141            if  ( windowKind  ==  WindowKind . CodeWindow  ||  ( ! _vbe . SelectedVBComponent . IsWrappingNullReference 
106-                                                         &&  component . ParentProject . ProjectId  ==  pane . CodeModule . Parent . ParentProject . ProjectId   
142+                                                         &&  component . ParentProject . ProjectId  ==  pane . CodeModule . Parent . ParentProject . ProjectId 
107143                                                        &&  component . Name  ==  pane . CodeModule . Parent . Name ) ) 
108144            { 
109145                selectedDeclaration  =  _parser . State . FindSelectedDeclaration ( pane ) ; 
@@ -120,13 +156,13 @@ private void RefreshSelection()
120156                { 
121157                    //The user might have selected the project node in Project Explorer 
122158                    //If they've chosen a folder, we'll return the project anyway 
123-                     caption  =    ! _vbe . ActiveVBProject . IsWrappingNullReference 
159+                     caption  =  ! _vbe . ActiveVBProject . IsWrappingNullReference 
124160                        ?  _vbe . ActiveVBProject . Name 
125161                        :  null ; 
126162                } 
127163                else 
128164                { 
129-                     caption  =  component . Type  ==  ComponentType . UserForm  &&    component . HasOpenDesigner 
165+                     caption  =  component . Type  ==  ComponentType . UserForm  &&  component . HasOpenDesigner 
130166                        ?  GetComponentControlsCaption ( component ) 
131167                        :  String . Format ( "{0}.{1} ({2})" ,  component . ParentProject . Name ,  component . Name ,  component . Type ) ; 
132168                } 
@@ -322,10 +358,8 @@ public void Dispose()
322358                _parser . State . StatusMessageUpdate  -=  State_StatusMessageUpdate ; 
323359            } 
324360
325-             if  ( _hooks  !=  null ) 
326-             { 
327-                 _hooks . MessageReceived  -=  _hooks_MessageReceived ; 
328-             } 
361+             VBEEvents . SelectionChanged  +=  _vbe_SelectionChanged ; 
362+             VBEEvents . WindowFocusChange  +=  _vbe_FocusChanged ; 
329363
330364            if  ( _configService  !=  null ) 
331365            { 
0 commit comments