Potentially useful TreeView checkbox additions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Potentially useful TreeView checkbox additions

Don Rylander-3
I've often wished I could use those little checkboxes on a TreeView for some
useful, lightweight thing, but I didn't have the time to do anything with them
until now.  In case it's of use to anyone else, here's what I've got.  Standard
software disclaimers apply ;-).  Helpful comments or amendments are welcome.

Don

============ Workspace code for playing with everything ===================
"1 - Select and evaluate this section first"
tp := TreePresenter show.
tp view
 hasLinesAtRoot: false;
 hasCheckBoxes: true;
 topView extent: 200@150.

(contents := OrderedCollection new) "since I want them to stay in this order"
 add: ('technicalDefinition' -> false);
 add: ('calculation' -> true);
 add: ('caveats' -> true);
 add: ('synonyms' -> false);
 add: ('relatedMeasures' -> false);
 add: ('suggestedCharts' -> true).

contents do: [:a| tp model add: a key asChildOf: nil].
tp model: tp model. "I always have to do this to refresh the view"

"2 - Everything has to be fully created for this to work."
tp view hasLines: (tp view hasLines) not. "get rid of the horizontal scrollbar"
contents do: [:a| tp view setCheckedStateOf: a key to: a value].
tp selection: tp model roots first.

"3 - Display this"
tp view isItemChecked: tp selection

"4 - Evaluate these one at a time to see the change."
tp view beChecked: tp selection
tp view beUnChecked: tp selection

================= File-in the 4 methods below ===================
!TreeView methodsFor!

beChecked: anObject

 #drAdded.
 self setCheckedStateOf: anObject to: true! !
!TreeView categoriesFor: #beChecked:!accessing!public! !

!TreeView methodsFor!

beUnChecked: anObject

 #drAdded.
 self setCheckedStateOf: anObject to: false! !
!TreeView categoriesFor: #beUnChecked:!accessing!public! !
!TreeView methodsFor!

isItemChecked: anObject
 "This is lifted pretty much directly from the MSDN article entitled
''Tree-View Controls,''
 last seen (8 August, 2001) at this URL:


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platfo
rm/CommCtls/TreeView/TreeView.asp

 The only difference is that getting the item's state happens with #dwState,
rather than
 #state.  Any other problems there might be with it are their fault, not mine.

 NB: For now, assume that the object should be in the model, and that its
absence is an error."

 |hItem tvItem|
 #drAdded.
 self hasCheckBoxes ifFalse: [^false].
 hItem := self handleFromObject: anObject ifAbsent: [self errorNotFound:
anObject].
 (tvItem := TVITEM new)
  hItem: hItem;
  mask: (TVIF_HANDLE | TVIF_STATE);
  stateMask: TVIS_STATEIMAGEMASK.
 self tvmGetItem: tvItem.
 ^((tvItem dwState >> 12) - 1) asBoolean
! !
!TreeView categoriesFor: #isItemChecked:!accessing!public! !

!TreeView methodsFor!

setCheckedStateOf: anObject to: aBoolean
 "This is lifted pretty much directly from the MSDN article entitled
''Tree-View Controls,''
 last seen (8 August, 2001) at this URL:


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platfo
rm/CommCtls/TreeView/TreeView.asp

 The only difference is that getting (and setting) the item's state happens
with #dwState(:),
 rather than #state(:).  Any other problems there might be with it are their
fault, not mine.

 NB: We assume that the object should be in the model, and that its absence is
an error."

 |hItem tvItem newState|
 #drAdded.
 self hasCheckBoxes ifFalse: [^self].
 hItem := self handleFromObject: anObject ifAbsent: [^self errorNotFound:
anObject].
 (tvItem := TVITEM new)
  hItem: hItem;
  mask: (TVIF_HANDLE | TVIF_STATE);
  stateMask: TVIS_STATEIMAGEMASK.
 self tvmGetItem: tvItem.
 newState := (aBoolean asDword + 1) << 12.
 tvItem dwState: newState.
 self tvmSetItem: tvItem.! !
!TreeView categoriesFor: #setCheckedStateOf:to:!accessing!public! !