Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "marktree.hxx"
21 : #include "dbu_control.hrc"
22 : #include <vcl/svapp.hxx>
23 :
24 : namespace dbaui
25 : {
26 : using namespace ::com::sun::star::uno;
27 : using namespace ::com::sun::star::lang;
28 :
29 : DBG_NAME(OMarkableTreeListBox)
30 :
31 0 : OMarkableTreeListBox::OMarkableTreeListBox( Window* pParent, WinBits nWinStyle )
32 0 : : DBTreeListBox(pParent, nWinStyle)
33 : {
34 : DBG_CTOR(OMarkableTreeListBox,NULL);
35 :
36 0 : InitButtonData();
37 0 : }
38 :
39 0 : OMarkableTreeListBox::OMarkableTreeListBox( Window* pParent, const ResId& rResId)
40 0 : : DBTreeListBox(pParent, rResId)
41 : {
42 : DBG_CTOR(OMarkableTreeListBox,NULL);
43 :
44 0 : InitButtonData();
45 0 : }
46 :
47 0 : OMarkableTreeListBox::~OMarkableTreeListBox()
48 : {
49 0 : delete m_pCheckButton;
50 :
51 : DBG_DTOR(OMarkableTreeListBox,NULL);
52 0 : }
53 :
54 0 : void OMarkableTreeListBox::Paint(const Rectangle& _rRect)
55 : {
56 0 : if (!IsEnabled())
57 : {
58 0 : Font aOldFont = GetFont();
59 0 : Font aNewFont(aOldFont);
60 :
61 0 : StyleSettings aSystemStyle = Application::GetSettings().GetStyleSettings();
62 0 : aNewFont.SetColor(aSystemStyle.GetDisableColor());
63 :
64 0 : SetFont(aNewFont);
65 0 : DBTreeListBox::Paint(_rRect);
66 0 : SetFont(aOldFont);
67 : }
68 : else
69 0 : DBTreeListBox::Paint(_rRect);
70 0 : }
71 :
72 0 : void OMarkableTreeListBox::InitButtonData()
73 : {
74 0 : m_pCheckButton = new SvLBoxButtonData( this );
75 0 : EnableCheckButton( m_pCheckButton );
76 0 : }
77 :
78 0 : void OMarkableTreeListBox::KeyInput( const KeyEvent& rKEvt )
79 : {
80 : // only if there are spaces
81 0 : if (rKEvt.GetKeyCode().GetCode() == KEY_SPACE && !rKEvt.GetKeyCode().IsShift() && !rKEvt.GetKeyCode().IsMod1())
82 : {
83 0 : SvTreeListEntry* pCurrentHandlerEntry = GetHdlEntry();
84 0 : if(pCurrentHandlerEntry)
85 : {
86 0 : SvButtonState eState = GetCheckButtonState( pCurrentHandlerEntry);
87 0 : if(eState == SV_BUTTON_CHECKED)
88 0 : SetCheckButtonState( pCurrentHandlerEntry, SV_BUTTON_UNCHECKED);
89 : else
90 0 : SetCheckButtonState( pCurrentHandlerEntry, SV_BUTTON_CHECKED);
91 :
92 0 : CheckButtonHdl();
93 : }
94 : else
95 0 : DBTreeListBox::KeyInput(rKEvt);
96 : }
97 : else
98 0 : DBTreeListBox::KeyInput(rKEvt);
99 0 : }
100 :
101 0 : SvButtonState OMarkableTreeListBox::implDetermineState(SvTreeListEntry* _pEntry)
102 : {
103 0 : SvButtonState eState = GetCheckButtonState(_pEntry);
104 0 : if (!GetModel()->HasChildren(_pEntry))
105 : // nothing to do in this bottom-up routine if there are no children ...
106 0 : return eState;
107 :
108 : // loop through the children and check their states
109 0 : sal_uInt16 nCheckedChildren = 0;
110 0 : sal_uInt16 nChildrenOverall = 0;
111 :
112 0 : SvTreeListEntry* pChildLoop = GetModel()->FirstChild(_pEntry);
113 0 : while (pChildLoop)
114 : {
115 0 : SvButtonState eChildState = implDetermineState(pChildLoop);
116 0 : if (SV_BUTTON_TRISTATE == eChildState)
117 0 : break;
118 :
119 0 : if (SV_BUTTON_CHECKED == eChildState)
120 0 : ++nCheckedChildren;
121 0 : ++nChildrenOverall;
122 :
123 0 : pChildLoop = GetModel()->NextSibling(pChildLoop);
124 : }
125 :
126 0 : if (pChildLoop)
127 : {
128 : // we did not finish the loop because at least one of the children is in tristate
129 0 : eState = SV_BUTTON_TRISTATE;
130 :
131 : // but this means that we did not finish all the siblings of pChildLoop,
132 : // so their checking may be incorrect at the moment
133 : // -> correct this
134 0 : while (pChildLoop)
135 : {
136 0 : implDetermineState(pChildLoop);
137 0 : pChildLoop = GetModel()->NextSibling(pChildLoop);
138 : }
139 : }
140 : else
141 : // none if the children are in tristate
142 0 : if (nCheckedChildren)
143 : // we have at least one child checked
144 0 : if (nCheckedChildren != nChildrenOverall)
145 : // not all children are checked
146 0 : eState = SV_BUTTON_TRISTATE;
147 : else
148 : // all children are checked
149 0 : eState = SV_BUTTON_CHECKED;
150 : else
151 : // no children are checked
152 0 : eState = SV_BUTTON_UNCHECKED;
153 :
154 : // finally set the entry to the state we just determined
155 0 : SetCheckButtonState(_pEntry, eState);
156 :
157 0 : return eState;
158 : }
159 :
160 0 : void OMarkableTreeListBox::CheckButtons()
161 : {
162 0 : SvTreeListEntry* pEntry = GetModel()->First();
163 0 : while (pEntry)
164 : {
165 0 : implDetermineState(pEntry);
166 0 : pEntry = GetModel()->NextSibling(pEntry);
167 : }
168 0 : }
169 :
170 0 : void OMarkableTreeListBox::CheckButtonHdl()
171 : {
172 0 : checkedButton_noBroadcast(GetHdlEntry());
173 0 : if (m_aCheckButtonHandler.IsSet())
174 0 : m_aCheckButtonHandler.Call(this);
175 0 : }
176 :
177 0 : void OMarkableTreeListBox::checkedButton_noBroadcast(SvTreeListEntry* _pEntry)
178 : {
179 0 : SvButtonState eState = GetCheckButtonState( _pEntry);
180 0 : if (GetModel()->HasChildren(_pEntry)) // if it has children, check those too
181 : {
182 0 : SvTreeListEntry* pChildEntry = GetModel()->Next(_pEntry);
183 0 : SvTreeListEntry* pSiblingEntry = GetModel()->NextSibling(_pEntry);
184 0 : while(pChildEntry && pChildEntry != pSiblingEntry)
185 : {
186 0 : SetCheckButtonState(pChildEntry, eState);
187 0 : pChildEntry = GetModel()->Next(pChildEntry);
188 : }
189 : }
190 :
191 0 : SvTreeListEntry* pEntry = IsSelected(_pEntry) ? FirstSelected() : NULL;
192 0 : while(pEntry)
193 : {
194 0 : SetCheckButtonState(pEntry,eState);
195 0 : if(GetModel()->HasChildren(pEntry)) // if it has children, check those too
196 : {
197 0 : SvTreeListEntry* pChildEntry = GetModel()->Next(pEntry);
198 0 : SvTreeListEntry* pSiblingEntry = GetModel()->NextSibling(pEntry);
199 0 : while(pChildEntry && pChildEntry != pSiblingEntry)
200 : {
201 0 : SetCheckButtonState(pChildEntry,eState);
202 0 : pChildEntry = GetModel()->Next(pChildEntry);
203 : }
204 : }
205 0 : pEntry = NextSelected(pEntry);
206 : }
207 0 : CheckButtons();
208 0 : }
209 :
210 12 : } // namespace
211 :
212 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|