]>
Commit | Line | Data |
---|---|---|
1c4293cb VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/propgrid/propgridpagestate.cpp | |
3 | // Purpose: wxPropertyGridPageState class | |
4 | // Author: Jaakko Salli | |
5 | // Modified by: | |
6 | // Created: 2008-08-24 | |
1c4293cb | 7 | // Copyright: (c) Jaakko Salli |
526954c5 | 8 | // Licence: wxWindows licence |
1c4293cb VZ |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // For compilers that support precompilation, includes "wx/wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
f4bc1aa2 JS |
18 | #if wxUSE_PROPGRID |
19 | ||
1c4293cb VZ |
20 | #ifndef WX_PRECOMP |
21 | #include "wx/defs.h" | |
22 | #include "wx/object.h" | |
23 | #include "wx/hash.h" | |
24 | #include "wx/string.h" | |
25 | #include "wx/log.h" | |
26 | #include "wx/event.h" | |
27 | #include "wx/window.h" | |
28 | #include "wx/panel.h" | |
29 | #include "wx/dc.h" | |
30 | #include "wx/dcmemory.h" | |
1c4293cb VZ |
31 | #include "wx/pen.h" |
32 | #include "wx/brush.h" | |
1c4293cb | 33 | #include "wx/intl.h" |
af276477 | 34 | #include "wx/stopwatch.h" |
1c4293cb VZ |
35 | #endif |
36 | ||
37 | // This define is necessary to prevent macro clearing | |
38 | #define __wxPG_SOURCE_FILE__ | |
39 | ||
3b211af1 SC |
40 | #include "wx/propgrid/propgridpagestate.h" |
41 | #include "wx/propgrid/propgrid.h" | |
42 | #include "wx/propgrid/editors.h" | |
1c4293cb | 43 | |
1c4293cb VZ |
44 | #define wxPG_DEFAULT_SPLITTERX 110 |
45 | ||
46 | ||
47 | // ----------------------------------------------------------------------- | |
48 | // wxPropertyGridIterator | |
49 | // ----------------------------------------------------------------------- | |
50 | ||
51 | void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState* state, int flags, wxPGProperty* property, int dir ) | |
52 | { | |
53 | wxASSERT( dir == 1 || dir == -1 ); | |
54 | ||
55 | m_state = state; | |
56 | m_baseParent = state->DoGetRoot(); | |
57 | if ( !property && m_baseParent->GetChildCount() ) | |
58 | property = m_baseParent->Item(0); | |
59 | ||
60 | m_property = property; | |
61 | ||
62 | wxPG_ITERATOR_CREATE_MASKS(flags, m_itemExMask, m_parentExMask) | |
63 | ||
64 | // Need to skip first? | |
65 | if ( property && (property->GetFlags() & m_itemExMask) ) | |
66 | { | |
67 | if ( dir == 1 ) | |
68 | Next(); | |
69 | else | |
70 | Prev(); | |
71 | } | |
72 | } | |
73 | ||
74 | void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState* state, int flags, int startPos, int dir ) | |
75 | { | |
b7bc9d80 | 76 | wxPGProperty* property = NULL; |
1c4293cb VZ |
77 | |
78 | if ( startPos == wxTOP ) | |
79 | { | |
1c4293cb VZ |
80 | if ( dir == 0 ) |
81 | dir = 1; | |
82 | } | |
83 | else if ( startPos == wxBOTTOM ) | |
84 | { | |
85 | property = state->GetLastItem(flags); | |
86 | if ( dir == 0 ) | |
87 | dir = -1; | |
88 | } | |
89 | else | |
90 | { | |
b7bc9d80 | 91 | wxFAIL_MSG("Only supported starting positions are wxTOP and wxBOTTOM"); |
1c4293cb VZ |
92 | } |
93 | ||
94 | Init( state, flags, property, dir ); | |
95 | } | |
96 | ||
97 | void wxPropertyGridIteratorBase::Assign( const wxPropertyGridIteratorBase& it ) | |
98 | { | |
99 | m_property = it.m_property; | |
100 | m_state = it.m_state; | |
101 | m_baseParent = it.m_baseParent; | |
102 | m_itemExMask = it.m_itemExMask; | |
103 | m_parentExMask = it.m_parentExMask; | |
104 | } | |
105 | ||
106 | void wxPropertyGridIteratorBase::Prev() | |
107 | { | |
108 | wxPGProperty* property = m_property; | |
6e82ecf9 JS |
109 | if ( !property ) |
110 | return; | |
1c4293cb VZ |
111 | |
112 | wxPGProperty* parent = property->GetParent(); | |
113 | wxASSERT( parent ); | |
114 | unsigned int index = property->GetIndexInParent(); | |
115 | ||
116 | if ( index > 0 ) | |
117 | { | |
118 | // Previous sibling | |
119 | index--; | |
120 | ||
121 | property = parent->Item(index); | |
122 | ||
123 | // Go to last children? | |
124 | if ( property->GetChildCount() && | |
125 | wxPG_ITERATOR_PARENTEXMASK_TEST(property, m_parentExMask) ) | |
126 | { | |
127 | // First child | |
128 | property = property->Last(); | |
129 | } | |
130 | } | |
131 | else | |
132 | { | |
133 | // Up to a parent | |
134 | if ( parent == m_baseParent ) | |
135 | { | |
136 | m_property = NULL; | |
137 | return; | |
138 | } | |
139 | else | |
140 | { | |
141 | property = parent; | |
142 | } | |
143 | } | |
144 | ||
145 | m_property = property; | |
146 | ||
147 | // If property does not match our criteria, skip it | |
148 | if ( property->GetFlags() & m_itemExMask ) | |
149 | Prev(); | |
150 | } | |
151 | ||
152 | void wxPropertyGridIteratorBase::Next( bool iterateChildren ) | |
153 | { | |
154 | wxPGProperty* property = m_property; | |
6e82ecf9 JS |
155 | if ( !property ) |
156 | return; | |
1c4293cb VZ |
157 | |
158 | if ( property->GetChildCount() && | |
159 | wxPG_ITERATOR_PARENTEXMASK_TEST(property, m_parentExMask) && | |
160 | iterateChildren ) | |
161 | { | |
162 | // First child | |
163 | property = property->Item(0); | |
164 | } | |
165 | else | |
166 | { | |
167 | wxPGProperty* parent = property->GetParent(); | |
168 | wxASSERT( parent ); | |
169 | unsigned int index = property->GetIndexInParent() + 1; | |
170 | ||
171 | if ( index < parent->GetChildCount() ) | |
172 | { | |
173 | // Next sibling | |
174 | property = parent->Item(index); | |
175 | } | |
176 | else | |
177 | { | |
178 | // Next sibling of parent | |
179 | if ( parent == m_baseParent ) | |
180 | { | |
181 | m_property = NULL; | |
182 | } | |
183 | else | |
184 | { | |
185 | m_property = parent; | |
186 | Next(false); | |
187 | } | |
188 | return; | |
189 | } | |
190 | } | |
191 | ||
192 | m_property = property; | |
193 | ||
194 | // If property does not match our criteria, skip it | |
195 | if ( property->GetFlags() & m_itemExMask ) | |
196 | Next(); | |
197 | } | |
198 | ||
199 | // ----------------------------------------------------------------------- | |
200 | // wxPropertyGridPageState | |
201 | // ----------------------------------------------------------------------- | |
202 | ||
203 | wxPropertyGridPageState::wxPropertyGridPageState() | |
204 | { | |
d3b9f782 | 205 | m_pPropGrid = NULL; |
1c4293cb VZ |
206 | m_regularArray.SetParentState(this); |
207 | m_properties = &m_regularArray; | |
d3b9f782 VZ |
208 | m_abcArray = NULL; |
209 | m_currentCategory = NULL; | |
1c4293cb VZ |
210 | m_width = 0; |
211 | m_virtualHeight = 0; | |
212 | m_lastCaptionBottomnest = 1; | |
213 | m_itemsAdded = 0; | |
214 | m_anyModified = 0; | |
215 | m_vhCalcPending = 0; | |
216 | m_colWidths.push_back( wxPG_DEFAULT_SPLITTERX ); | |
217 | m_colWidths.push_back( wxPG_DEFAULT_SPLITTERX ); | |
218 | m_fSplitterX = wxPG_DEFAULT_SPLITTERX; | |
58935d4a | 219 | |
fe01f16e JS |
220 | m_columnProportions.push_back(1); |
221 | m_columnProportions.push_back(1); | |
222 | ||
0da1f1c4 JS |
223 | m_isSplitterPreSet = false; |
224 | m_dontCenterSplitter = false; | |
225 | ||
58935d4a JS |
226 | // By default, we only have the 'value' column editable |
227 | m_editableColumns.push_back(1); | |
1c4293cb VZ |
228 | } |
229 | ||
230 | // ----------------------------------------------------------------------- | |
231 | ||
232 | wxPropertyGridPageState::~wxPropertyGridPageState() | |
233 | { | |
234 | delete m_abcArray; | |
235 | } | |
236 | ||
237 | // ----------------------------------------------------------------------- | |
238 | ||
239 | void wxPropertyGridPageState::InitNonCatMode() | |
240 | { | |
241 | if ( !m_abcArray ) | |
242 | { | |
94b8ecf1 | 243 | m_abcArray = new wxPGRootProperty(wxS("<Root_NonCat>")); |
1c4293cb VZ |
244 | m_abcArray->SetParentState(this); |
245 | m_abcArray->SetFlag(wxPG_PROP_CHILDREN_ARE_COPIES); | |
246 | } | |
247 | ||
248 | // Must be called when state::m_properties still points to regularArray. | |
249 | wxPGProperty* oldProperties = m_properties; | |
250 | ||
251 | // Must use temp value in state::m_properties for item iteration loop | |
252 | // to run as expected. | |
253 | m_properties = &m_regularArray; | |
254 | ||
255 | if ( m_properties->GetChildCount() ) | |
256 | { | |
91c818f8 JS |
257 | // |
258 | // Prepare m_abcArray | |
259 | wxPropertyGridIterator it( this, wxPG_ITERATE_PROPERTIES ); | |
1c4293cb VZ |
260 | |
261 | for ( ; !it.AtEnd(); it.Next() ) | |
262 | { | |
263 | wxPGProperty* p = it.GetProperty(); | |
264 | wxPGProperty* parent = p->GetParent(); | |
91c818f8 | 265 | if ( parent->IsCategory() || parent->IsRoot() ) |
1c4293cb | 266 | { |
48a32cf6 | 267 | m_abcArray->DoAddChild(p); |
1c4293cb VZ |
268 | p->m_parent = &m_regularArray; |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | m_properties = oldProperties; | |
274 | } | |
275 | ||
276 | // ----------------------------------------------------------------------- | |
277 | ||
278 | void wxPropertyGridPageState::DoClear() | |
279 | { | |
0c35994d | 280 | if ( m_pPropGrid && m_pPropGrid->GetState() == this ) |
8dee26e1 JS |
281 | { |
282 | m_pPropGrid->ClearSelection(false); | |
283 | } | |
284 | else | |
285 | { | |
fc72fab6 | 286 | m_selection.clear(); |
8dee26e1 JS |
287 | } |
288 | ||
1c4293cb VZ |
289 | m_regularArray.Empty(); |
290 | if ( m_abcArray ) | |
291 | m_abcArray->Empty(); | |
292 | ||
293 | m_dictName.clear(); | |
294 | ||
d3b9f782 | 295 | m_currentCategory = NULL; |
1c4293cb VZ |
296 | m_lastCaptionBottomnest = 1; |
297 | m_itemsAdded = 0; | |
298 | ||
299 | m_virtualHeight = 0; | |
300 | m_vhCalcPending = 0; | |
1c4293cb VZ |
301 | } |
302 | ||
303 | // ----------------------------------------------------------------------- | |
304 | ||
305 | void wxPropertyGridPageState::CalculateFontAndBitmapStuff( int WXUNUSED(vspacing) ) | |
306 | { | |
307 | wxPropertyGrid* propGrid = GetGrid(); | |
308 | ||
309 | VirtualHeightChanged(); | |
310 | ||
311 | // Recalculate caption text extents. | |
312 | unsigned int i; | |
313 | ||
314 | for ( i=0;i<m_regularArray.GetChildCount();i++ ) | |
315 | { | |
316 | wxPGProperty* p =m_regularArray.Item(i); | |
317 | ||
318 | if ( p->IsCategory() ) | |
319 | ((wxPropertyCategory*)p)->CalculateTextExtent(propGrid, propGrid->GetCaptionFont()); | |
320 | } | |
321 | } | |
322 | ||
323 | // ----------------------------------------------------------------------- | |
324 | ||
325 | void wxPropertyGridPageState::SetVirtualWidth( int width ) | |
326 | { | |
2f772c89 JS |
327 | // Sometimes width less than 0 is offered. Let's make things easy for |
328 | // everybody and deal with it here. | |
329 | if ( width < 0 ) | |
330 | width = 0; | |
331 | ||
1c4293cb VZ |
332 | wxPropertyGrid* pg = GetGrid(); |
333 | int gw = pg->GetClientSize().x; | |
334 | if ( width < gw ) | |
335 | width = gw; | |
336 | ||
337 | m_width = width; | |
338 | } | |
339 | ||
340 | // ----------------------------------------------------------------------- | |
341 | ||
342 | void wxPropertyGridPageState::OnClientWidthChange( int newWidth, int widthChange, bool fromOnResize ) | |
343 | { | |
344 | wxPropertyGrid* pg = GetGrid(); | |
345 | ||
346 | if ( pg->HasVirtualWidth() ) | |
347 | { | |
348 | if ( m_width < newWidth ) | |
349 | SetVirtualWidth( newWidth ); | |
350 | ||
351 | CheckColumnWidths(widthChange); | |
352 | } | |
353 | else | |
354 | { | |
355 | SetVirtualWidth( newWidth ); | |
356 | ||
357 | // This should be done before splitter auto centering | |
358 | // NOTE: Splitter auto-centering is done in this function. | |
359 | if ( !fromOnResize ) | |
360 | widthChange = 0; | |
361 | CheckColumnWidths(widthChange); | |
362 | ||
0da1f1c4 | 363 | if ( !m_isSplitterPreSet && m_dontCenterSplitter ) |
1c4293cb VZ |
364 | { |
365 | long timeSinceCreation = (::wxGetLocalTimeMillis() - GetGrid()->m_timeCreated).ToLong(); | |
366 | ||
367 | // If too long, don't set splitter | |
8034d81d | 368 | if ( timeSinceCreation < 250 ) |
1c4293cb | 369 | { |
8034d81d | 370 | if ( m_properties->GetChildCount() ) |
1c4293cb VZ |
371 | { |
372 | SetSplitterLeft( false ); | |
373 | } | |
374 | else | |
375 | { | |
376 | DoSetSplitterPosition( newWidth / 2 ); | |
0da1f1c4 | 377 | m_isSplitterPreSet = false; |
1c4293cb VZ |
378 | } |
379 | } | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | // ----------------------------------------------------------------------- | |
385 | // wxPropertyGridPageState item iteration methods | |
386 | // ----------------------------------------------------------------------- | |
387 | ||
388 | wxPGProperty* wxPropertyGridPageState::GetLastItem( int flags ) | |
389 | { | |
390 | if ( !m_properties->GetChildCount() ) | |
d3b9f782 | 391 | return NULL; |
1c4293cb VZ |
392 | |
393 | wxPG_ITERATOR_CREATE_MASKS(flags, int itemExMask, int parentExMask) | |
394 | ||
395 | // First, get last child of last parent | |
396 | wxPGProperty* pwc = (wxPGProperty*)m_properties->Last(); | |
397 | while ( pwc->GetChildCount() && | |
398 | wxPG_ITERATOR_PARENTEXMASK_TEST(pwc, parentExMask) ) | |
399 | pwc = (wxPGProperty*) pwc->Last(); | |
400 | ||
401 | // Then, if it doesn't fit our criteria, back up until we find something that does | |
402 | if ( pwc->GetFlags() & itemExMask ) | |
403 | { | |
404 | wxPropertyGridIterator it( this, flags, pwc ); | |
6bce2ad9 VZ |
405 | for ( ; !it.AtEnd(); it.Prev() ) |
406 | ; | |
1c4293cb VZ |
407 | pwc = (wxPGProperty*) it.GetProperty(); |
408 | } | |
409 | ||
410 | return pwc; | |
411 | } | |
412 | ||
413 | wxPropertyCategory* wxPropertyGridPageState::GetPropertyCategory( const wxPGProperty* p ) const | |
414 | { | |
415 | const wxPGProperty* parent = (const wxPGProperty*)p; | |
416 | const wxPGProperty* grandparent = (const wxPGProperty*)parent->GetParent(); | |
417 | do | |
418 | { | |
419 | parent = grandparent; | |
420 | grandparent = (wxPGProperty*)parent->GetParent(); | |
421 | if ( parent->IsCategory() && grandparent ) | |
422 | return (wxPropertyCategory*)parent; | |
423 | } while ( grandparent ); | |
424 | ||
d3b9f782 | 425 | return NULL; |
1c4293cb VZ |
426 | } |
427 | ||
428 | // ----------------------------------------------------------------------- | |
429 | // wxPropertyGridPageState GetPropertyXXX methods | |
430 | // ----------------------------------------------------------------------- | |
431 | ||
432 | wxPGProperty* wxPropertyGridPageState::GetPropertyByLabel( const wxString& label, | |
433 | wxPGProperty* parent ) const | |
434 | { | |
435 | ||
436 | size_t i; | |
437 | ||
438 | if ( !parent ) parent = (wxPGProperty*) &m_regularArray; | |
439 | ||
440 | for ( i=0; i<parent->GetChildCount(); i++ ) | |
441 | { | |
442 | wxPGProperty* p = parent->Item(i); | |
443 | if ( p->m_label == label ) | |
444 | return p; | |
445 | // Check children recursively. | |
446 | if ( p->GetChildCount() ) | |
447 | { | |
448 | p = GetPropertyByLabel(label,(wxPGProperty*)p); | |
449 | if ( p ) | |
450 | return p; | |
451 | } | |
452 | } | |
453 | ||
454 | return NULL; | |
455 | } | |
456 | ||
457 | // ----------------------------------------------------------------------- | |
458 | ||
459 | wxPGProperty* wxPropertyGridPageState::BaseGetPropertyByName( const wxString& name ) const | |
460 | { | |
461 | wxPGHashMapS2P::const_iterator it; | |
462 | it = m_dictName.find(name); | |
463 | if ( it != m_dictName.end() ) | |
464 | return (wxPGProperty*) it->second; | |
d3b9f782 | 465 | return NULL; |
1c4293cb VZ |
466 | } |
467 | ||
020b0041 JS |
468 | // ----------------------------------------------------------------------- |
469 | ||
470 | void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty* p, | |
471 | const wxString& newName ) | |
472 | { | |
473 | wxCHECK_RET( p, wxT("invalid property id") ); | |
474 | ||
26740086 JS |
475 | wxPGProperty* parent = p->GetParent(); |
476 | ||
477 | if ( parent->IsCategory() || parent->IsRoot() ) | |
478 | { | |
6636ef8d | 479 | if ( !p->GetBaseName().empty() ) |
26740086 | 480 | m_dictName.erase( p->GetBaseName() ); |
6636ef8d | 481 | if ( !newName.empty() ) |
26740086 JS |
482 | m_dictName[newName] = (void*) p; |
483 | } | |
020b0041 JS |
484 | |
485 | p->DoSetName(newName); | |
486 | } | |
487 | ||
1c4293cb VZ |
488 | // ----------------------------------------------------------------------- |
489 | // wxPropertyGridPageState global operations | |
490 | // ----------------------------------------------------------------------- | |
491 | ||
492 | // ----------------------------------------------------------------------- | |
493 | // Item iteration macros | |
494 | // NB: Nowadays only needed for alphabetic/categoric mode switching. | |
495 | // ----------------------------------------------------------------------- | |
496 | ||
b7bc9d80 | 497 | //#define II_INVALID_I 0x00FFFFFF |
1c4293cb VZ |
498 | |
499 | #define ITEM_ITERATION_VARIABLES \ | |
500 | wxPGProperty* parent; \ | |
501 | unsigned int i; \ | |
502 | unsigned int iMax; | |
503 | ||
504 | #define ITEM_ITERATION_INIT_FROM_THE_TOP \ | |
505 | parent = m_properties; \ | |
506 | i = 0; | |
507 | ||
b7bc9d80 | 508 | #if 0 |
1c4293cb VZ |
509 | #define ITEM_ITERATION_INIT(startparent, startindex, state) \ |
510 | parent = startparent; \ | |
511 | i = (unsigned int)startindex; \ | |
d3b9f782 | 512 | if ( parent == NULL ) \ |
1c4293cb VZ |
513 | { \ |
514 | parent = state->m_properties; \ | |
515 | i = 0; \ | |
516 | } | |
b7bc9d80 | 517 | #endif |
1c4293cb VZ |
518 | |
519 | #define ITEM_ITERATION_LOOP_BEGIN \ | |
520 | do \ | |
521 | { \ | |
522 | iMax = parent->GetChildCount(); \ | |
523 | while ( i < iMax ) \ | |
524 | { \ | |
525 | wxPGProperty* p = parent->Item(i); | |
526 | ||
527 | #define ITEM_ITERATION_LOOP_END \ | |
528 | if ( p->GetChildCount() ) \ | |
529 | { \ | |
530 | i = 0; \ | |
531 | parent = (wxPGProperty*)p; \ | |
532 | iMax = parent->GetChildCount(); \ | |
533 | } \ | |
534 | else \ | |
535 | i++; \ | |
536 | } \ | |
537 | i = parent->m_arrIndex + 1; \ | |
538 | parent = parent->m_parent; \ | |
539 | } \ | |
540 | while ( parent != NULL ); | |
541 | ||
542 | bool wxPropertyGridPageState::EnableCategories( bool enable ) | |
543 | { | |
544 | // | |
545 | // NB: We can't use wxPropertyGridIterator in this | |
546 | // function, since it depends on m_arrIndexes, | |
547 | // which, among other things, is being fixed here. | |
548 | // | |
549 | ITEM_ITERATION_VARIABLES | |
550 | ||
551 | if ( enable ) | |
552 | { | |
553 | // | |
554 | // Enable categories | |
555 | // | |
556 | ||
557 | if ( !IsInNonCatMode() ) | |
558 | return false; | |
559 | ||
560 | m_properties = &m_regularArray; | |
561 | ||
562 | // fix parents, indexes, and depths | |
563 | ITEM_ITERATION_INIT_FROM_THE_TOP | |
564 | ||
565 | ITEM_ITERATION_LOOP_BEGIN | |
566 | ||
567 | p->m_arrIndex = i; | |
568 | ||
569 | p->m_parent = parent; | |
570 | ||
571 | // If parent was category, and this is not, | |
572 | // then the depth stays the same. | |
573 | if ( parent->IsCategory() && | |
574 | !p->IsCategory() ) | |
575 | p->m_depth = parent->m_depth; | |
576 | else | |
577 | p->m_depth = parent->m_depth + 1; | |
578 | ||
579 | ITEM_ITERATION_LOOP_END | |
580 | ||
581 | } | |
582 | else | |
583 | { | |
584 | // | |
585 | // Disable categories | |
586 | // | |
587 | ||
588 | if ( IsInNonCatMode() ) | |
589 | return false; | |
590 | ||
591 | // Create array, if necessary. | |
592 | if ( !m_abcArray ) | |
593 | InitNonCatMode(); | |
594 | ||
595 | m_properties = m_abcArray; | |
596 | ||
597 | // fix parents, indexes, and depths | |
598 | ITEM_ITERATION_INIT_FROM_THE_TOP | |
599 | ||
600 | ITEM_ITERATION_LOOP_BEGIN | |
601 | ||
602 | p->m_arrIndex = i; | |
603 | ||
604 | p->m_parent = parent; | |
605 | ||
606 | p->m_depth = parent->m_depth + 1; | |
607 | ||
608 | ITEM_ITERATION_LOOP_END | |
609 | } | |
610 | ||
611 | VirtualHeightChanged(); | |
612 | ||
613 | if ( m_pPropGrid->GetState() == this ) | |
614 | m_pPropGrid->RecalculateVirtualSize(); | |
615 | ||
616 | return true; | |
617 | } | |
618 | ||
619 | // ----------------------------------------------------------------------- | |
620 | ||
43396981 JS |
621 | static int wxPG_SortFunc_ByFunction(wxPGProperty **pp1, wxPGProperty **pp2) |
622 | { | |
623 | wxPGProperty *p1 = *pp1; | |
624 | wxPGProperty *p2 = *pp2; | |
625 | wxPropertyGrid* pg = p1->GetGrid(); | |
626 | wxPGSortCallback sortFunction = pg->GetSortFunction(); | |
627 | return sortFunction(pg, p1, p2); | |
628 | } | |
629 | ||
630 | static int wxPG_SortFunc_ByLabel(wxPGProperty **pp1, wxPGProperty **pp2) | |
1c4293cb | 631 | { |
43396981 JS |
632 | wxPGProperty *p1 = *pp1; |
633 | wxPGProperty *p2 = *pp2; | |
634 | return p1->GetLabel().CmpNoCase( p2->GetLabel() ); | |
1c4293cb VZ |
635 | } |
636 | ||
7f3f8f1e JS |
637 | #if 0 |
638 | // | |
639 | // For wxVector w/ wxUSE_STL=1, you would use code like this instead: | |
640 | // | |
641 | ||
642 | #include <algorithm> | |
643 | ||
644 | static bool wxPG_SortFunc_ByFunction(wxPGProperty *p1, wxPGProperty *p2) | |
645 | { | |
646 | wxPropertyGrid* pg = p1->GetGrid(); | |
647 | wxPGSortCallback sortFunction = pg->GetSortFunction(); | |
648 | return sortFunction(pg, p1, p2) < 0; | |
649 | } | |
650 | ||
651 | static bool wxPG_SortFunc_ByLabel(wxPGProperty *p1, wxPGProperty *p2) | |
652 | { | |
653 | return p1->GetLabel().CmpNoCase( p2->GetLabel() ) < 0; | |
654 | } | |
d8c74d04 JS |
655 | #endif |
656 | ||
43396981 | 657 | void wxPropertyGridPageState::DoSortChildren( wxPGProperty* p, |
0eb877f2 | 658 | int flags ) |
1c4293cb VZ |
659 | { |
660 | if ( !p ) | |
43396981 | 661 | p = m_properties; |
1c4293cb | 662 | |
0eb877f2 | 663 | // Can only sort items with children |
1c4293cb VZ |
664 | if ( !p->GetChildCount() ) |
665 | return; | |
666 | ||
0eb877f2 JS |
667 | // Never sort children of aggregate properties |
668 | if ( p->HasFlag(wxPG_PROP_AGGREGATE) ) | |
669 | return; | |
670 | ||
671 | if ( (flags & wxPG_SORT_TOP_LEVEL_ONLY) | |
672 | && !p->IsCategory() && !p->IsRoot() ) | |
1c4293cb VZ |
673 | return; |
674 | ||
7f3f8f1e JS |
675 | if ( GetGrid()->GetSortFunction() ) |
676 | p->m_children.Sort( wxPG_SortFunc_ByFunction ); | |
677 | else | |
678 | p->m_children.Sort( wxPG_SortFunc_ByLabel ); | |
679 | ||
680 | #if 0 | |
681 | // | |
682 | // For wxVector w/ wxUSE_STL=1, you would use code like this instead: | |
683 | // | |
43396981 JS |
684 | if ( GetGrid()->GetSortFunction() ) |
685 | std::sort(p->m_children.begin(), p->m_children.end(), | |
686 | wxPG_SortFunc_ByFunction); | |
687 | else | |
688 | std::sort(p->m_children.begin(), p->m_children.end(), | |
689 | wxPG_SortFunc_ByLabel); | |
d8c74d04 | 690 | #endif |
1c4293cb | 691 | |
0eb877f2 | 692 | // Fix indices |
43396981 | 693 | p->FixIndicesOfChildren(); |
1c4293cb | 694 | |
0eb877f2 | 695 | if ( flags & wxPG_RECURSE ) |
43396981 | 696 | { |
0eb877f2 | 697 | // Apply sort recursively |
43396981 | 698 | for ( unsigned int i=0; i<p->GetChildCount(); i++ ) |
0eb877f2 | 699 | DoSortChildren(p->Item(i), flags); |
43396981 | 700 | } |
1c4293cb VZ |
701 | } |
702 | ||
703 | // ----------------------------------------------------------------------- | |
704 | ||
0eb877f2 | 705 | void wxPropertyGridPageState::DoSort( int flags ) |
1c4293cb | 706 | { |
0eb877f2 | 707 | DoSortChildren( m_properties, flags | wxPG_RECURSE ); |
1c4293cb | 708 | |
94b8ecf1 JS |
709 | // We used to sort categories as well here also if in non-categorized |
710 | // mode, but doing would naturally cause child indices to become | |
711 | // corrupted. | |
1c4293cb VZ |
712 | } |
713 | ||
0eb877f2 JS |
714 | // ----------------------------------------------------------------------- |
715 | ||
716 | bool wxPropertyGridPageState::PrepareAfterItemsAdded() | |
717 | { | |
718 | if ( !m_itemsAdded ) return false; | |
719 | ||
720 | wxPropertyGrid* pg = GetGrid(); | |
721 | ||
722 | m_itemsAdded = 0; | |
723 | ||
724 | if ( pg->HasFlag(wxPG_AUTO_SORT) ) | |
725 | DoSort(wxPG_SORT_TOP_LEVEL_ONLY); | |
726 | ||
727 | return true; | |
728 | } | |
729 | ||
1c4293cb VZ |
730 | // ----------------------------------------------------------------------- |
731 | // wxPropertyGridPageState splitter, column and hittest functions | |
732 | // ----------------------------------------------------------------------- | |
733 | ||
734 | wxPGProperty* wxPropertyGridPageState::DoGetItemAtY( int y ) const | |
735 | { | |
736 | // Outside? | |
737 | if ( y < 0 ) | |
d3b9f782 | 738 | return NULL; |
1c4293cb VZ |
739 | |
740 | unsigned int a = 0; | |
741 | return m_properties->GetItemAtY(y, GetGrid()->m_lineHeight, &a); | |
742 | } | |
743 | ||
744 | // ----------------------------------------------------------------------- | |
745 | ||
0ee31682 JS |
746 | wxPropertyGridHitTestResult |
747 | wxPropertyGridPageState::HitTest( const wxPoint&pt ) const | |
1c4293cb VZ |
748 | { |
749 | wxPropertyGridHitTestResult result; | |
0ee31682 JS |
750 | result.m_column = HitTestH( pt.x, &result.m_splitter, |
751 | &result.m_splitterHitOffset ); | |
752 | result.m_property = DoGetItemAtY( pt.y ); | |
1c4293cb VZ |
753 | return result; |
754 | } | |
755 | ||
756 | // ----------------------------------------------------------------------- | |
757 | ||
758 | // Used by SetSplitterLeft() and DotFitColumns() | |
759 | int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC& dc, | |
760 | wxPGProperty* pwc, | |
761 | unsigned int col, | |
762 | bool subProps) const | |
763 | { | |
764 | wxPropertyGrid* pg = m_pPropGrid; | |
765 | size_t i; | |
766 | int maxW = 0; | |
767 | int w, h; | |
768 | ||
769 | for ( i=0; i<pwc->GetChildCount(); i++ ) | |
770 | { | |
771 | wxPGProperty* p = pwc->Item(i); | |
772 | if ( !p->IsCategory() ) | |
773 | { | |
d7e2b522 JS |
774 | const wxPGCell* cell = NULL; |
775 | wxString text; | |
776 | p->GetDisplayInfo(col, -1, 0, &text, &cell); | |
777 | dc.GetTextExtent(text, &w, &h); | |
1c4293cb VZ |
778 | if ( col == 0 ) |
779 | w += ( ((int)p->m_depth-1) * pg->m_subgroup_extramargin ); | |
780 | ||
890defb4 VZ |
781 | // account for the bitmap |
782 | if ( col == 1 ) | |
783 | w += p->GetImageOffset(pg->GetImageRect(p, -1).GetWidth()); | |
784 | ||
1c4293cb VZ |
785 | |
786 | w += (wxPG_XBEFORETEXT*2); | |
787 | ||
788 | if ( w > maxW ) | |
789 | maxW = w; | |
790 | } | |
791 | ||
792 | if ( p->GetChildCount() && | |
793 | ( subProps || p->IsCategory() ) ) | |
794 | { | |
795 | w = GetColumnFitWidth( dc, p, col, subProps ); | |
796 | ||
797 | if ( w > maxW ) | |
798 | maxW = w; | |
799 | } | |
800 | } | |
801 | ||
802 | return maxW; | |
803 | } | |
804 | ||
33d953e7 VZ |
805 | int wxPropertyGridPageState::GetColumnFullWidth( wxClientDC &dc, wxPGProperty *p, unsigned int col ) |
806 | { | |
807 | if ( p->IsCategory() ) | |
808 | return 0; | |
809 | ||
810 | const wxPGCell* cell = NULL; | |
811 | wxString text; | |
812 | p->GetDisplayInfo(col, -1, 0, &text, &cell); | |
813 | int w = dc.GetTextExtent(text).x; | |
814 | ||
815 | if ( col == 0 ) | |
816 | w += (int)p->m_depth * m_pPropGrid->m_subgroup_extramargin; | |
817 | ||
818 | // account for the bitmap | |
819 | if ( col == 1 ) | |
820 | w += p->GetImageOffset(m_pPropGrid->GetImageRect(p, -1).GetWidth()); | |
821 | ||
822 | w += (wxPG_XBEFORETEXT*2); | |
823 | return w; | |
824 | } | |
825 | ||
1c4293cb VZ |
826 | int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn ) const |
827 | { | |
828 | int n = GetGrid()->m_marginWidth; | |
829 | int i; | |
830 | for ( i=0; i<=splitterColumn; i++ ) | |
831 | n += m_colWidths[i]; | |
832 | return n; | |
833 | } | |
834 | ||
835 | int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column) ) const | |
836 | { | |
837 | return wxPG_DRAG_MARGIN; | |
838 | } | |
839 | ||
0da1f1c4 JS |
840 | void wxPropertyGridPageState::PropagateColSizeDec( int column, |
841 | int decrease, | |
842 | int dir ) | |
1c4293cb VZ |
843 | { |
844 | int origWidth = m_colWidths[column]; | |
845 | m_colWidths[column] -= decrease; | |
846 | int min = GetColumnMinWidth(column); | |
847 | int more = 0; | |
848 | if ( m_colWidths[column] < min ) | |
849 | { | |
850 | more = decrease - (origWidth - min); | |
851 | m_colWidths[column] = min; | |
852 | } | |
853 | ||
854 | // | |
855 | // FIXME: Causes erratic splitter changing, so as a workaround | |
856 | // disabled if two or less columns. | |
857 | ||
858 | if ( m_colWidths.size() <= 2 ) | |
859 | return; | |
860 | ||
861 | column += dir; | |
862 | if ( more && column < (int)m_colWidths.size() && column >= 0 ) | |
863 | PropagateColSizeDec( column, more, dir ); | |
864 | } | |
865 | ||
0da1f1c4 JS |
866 | void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos, |
867 | int splitterColumn, | |
f5254768 | 868 | int flags ) |
1c4293cb VZ |
869 | { |
870 | wxPropertyGrid* pg = GetGrid(); | |
871 | ||
872 | int adjust = newXPos - DoGetSplitterPosition(splitterColumn); | |
873 | ||
874 | if ( !pg->HasVirtualWidth() ) | |
875 | { | |
876 | // No virtual width | |
877 | int otherColumn; | |
878 | if ( adjust > 0 ) | |
879 | { | |
880 | otherColumn = splitterColumn + 1; | |
881 | if ( otherColumn == (int)m_colWidths.size() ) | |
882 | otherColumn = 0; | |
883 | m_colWidths[splitterColumn] += adjust; | |
884 | PropagateColSizeDec( otherColumn, adjust, 1 ); | |
885 | } | |
886 | else | |
887 | { | |
888 | otherColumn = splitterColumn + 1; | |
889 | if ( otherColumn == (int)m_colWidths.size() ) | |
890 | otherColumn = 0; | |
891 | m_colWidths[otherColumn] -= adjust; | |
892 | PropagateColSizeDec( splitterColumn, -adjust, -1 ); | |
893 | } | |
894 | } | |
895 | else | |
896 | { | |
897 | m_colWidths[splitterColumn] += adjust; | |
898 | } | |
899 | ||
900 | if ( splitterColumn == 0 ) | |
901 | m_fSplitterX = (double) newXPos; | |
902 | ||
f5254768 JS |
903 | if ( !(flags & wxPG_SPLITTER_FROM_AUTO_CENTER) && |
904 | !(flags & wxPG_SPLITTER_FROM_EVENT) ) | |
1c4293cb VZ |
905 | { |
906 | // Don't allow initial splitter auto-positioning after this. | |
0da1f1c4 | 907 | m_isSplitterPreSet = true; |
1c4293cb VZ |
908 | |
909 | CheckColumnWidths(); | |
910 | } | |
911 | } | |
912 | ||
913 | // Moves splitter so that all labels are visible, but just. | |
914 | void wxPropertyGridPageState::SetSplitterLeft( bool subProps ) | |
915 | { | |
916 | wxPropertyGrid* pg = GetGrid(); | |
917 | wxClientDC dc(pg); | |
2197ec80 | 918 | dc.SetFont(pg->GetFont()); |
1c4293cb VZ |
919 | |
920 | int maxW = GetColumnFitWidth(dc, m_properties, 0, subProps); | |
921 | ||
922 | if ( maxW > 0 ) | |
923 | { | |
924 | maxW += pg->m_marginWidth; | |
925 | DoSetSplitterPosition( maxW ); | |
926 | } | |
927 | ||
0da1f1c4 | 928 | m_dontCenterSplitter = true; |
1c4293cb VZ |
929 | } |
930 | ||
931 | wxSize wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize) ) | |
932 | { | |
933 | wxPropertyGrid* pg = GetGrid(); | |
934 | wxClientDC dc(pg); | |
2197ec80 | 935 | dc.SetFont(pg->GetFont()); |
1c4293cb VZ |
936 | |
937 | int marginWidth = pg->m_marginWidth; | |
938 | int accWid = marginWidth; | |
939 | int maxColWidth = 500; | |
940 | ||
941 | for ( unsigned int col=0; col < GetColumnCount(); col++ ) | |
942 | { | |
943 | int fitWid = GetColumnFitWidth(dc, m_properties, col, true); | |
944 | int colMinWidth = GetColumnMinWidth(col); | |
945 | if ( fitWid < colMinWidth ) | |
946 | fitWid = colMinWidth; | |
947 | else if ( fitWid > maxColWidth ) | |
948 | fitWid = maxColWidth; | |
949 | ||
950 | m_colWidths[col] = fitWid; | |
951 | ||
952 | accWid += fitWid; | |
953 | } | |
954 | ||
955 | // Expand last one to fill the width | |
956 | int remaining = m_width - accWid; | |
957 | m_colWidths[GetColumnCount()-1] += remaining; | |
958 | ||
0da1f1c4 | 959 | m_dontCenterSplitter = true; |
1c4293cb VZ |
960 | |
961 | int firstSplitterX = marginWidth + m_colWidths[0]; | |
962 | m_fSplitterX = (double) firstSplitterX; | |
963 | ||
964 | // Don't allow initial splitter auto-positioning after this. | |
965 | if ( pg->GetState() == this ) | |
966 | { | |
967 | pg->SetSplitterPosition(firstSplitterX, false); | |
968 | pg->Refresh(); | |
969 | } | |
970 | ||
971 | int x, y; | |
972 | pg->GetVirtualSize(&x, &y); | |
973 | ||
974 | return wxSize(accWid, y); | |
975 | } | |
976 | ||
977 | void wxPropertyGridPageState::CheckColumnWidths( int widthChange ) | |
978 | { | |
979 | if ( m_width == 0 ) | |
980 | return; | |
981 | ||
982 | wxPropertyGrid* pg = GetGrid(); | |
983 | ||
1c4293cb VZ |
984 | unsigned int i; |
985 | unsigned int lastColumn = m_colWidths.size() - 1; | |
986 | int width = m_width; | |
987 | int clientWidth = pg->GetClientSize().x; | |
988 | ||
989 | // | |
990 | // Column to reduce, if needed. Take last one that exceeds minimum width. | |
1c4293cb | 991 | int reduceCol = -1; |
1c4293cb | 992 | |
4b6a582b VZ |
993 | wxLogTrace("propgrid", |
994 | wxS("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"), | |
995 | width, clientWidth); | |
1c4293cb VZ |
996 | |
997 | // | |
998 | // Check min sizes | |
999 | for ( i=0; i<m_colWidths.size(); i++ ) | |
1000 | { | |
1001 | int min = GetColumnMinWidth(i); | |
1002 | if ( m_colWidths[i] <= min ) | |
1003 | { | |
1004 | m_colWidths[i] = min; | |
1c4293cb VZ |
1005 | } |
1006 | else | |
1007 | { | |
bd6ffa9f JS |
1008 | // Always reduce the last column that is larger than minimum size |
1009 | // (looks nicer, even with auto-centering enabled). | |
1010 | reduceCol = i; | |
1c4293cb VZ |
1011 | } |
1012 | } | |
1013 | ||
1014 | int colsWidth = pg->m_marginWidth; | |
1015 | for ( i=0; i<m_colWidths.size(); i++ ) | |
1016 | colsWidth += m_colWidths[i]; | |
1017 | ||
4b6a582b VZ |
1018 | wxLogTrace("propgrid", |
1019 | wxS(" HasVirtualWidth: %i colsWidth: %i"), | |
1020 | (int)pg->HasVirtualWidth(), colsWidth); | |
1c4293cb VZ |
1021 | |
1022 | // Then mode-based requirement | |
1023 | if ( !pg->HasVirtualWidth() ) | |
1024 | { | |
1025 | int widthHigher = width - colsWidth; | |
1026 | ||
1027 | // Adapt colsWidth to width | |
1028 | if ( colsWidth < width ) | |
1029 | { | |
1030 | // Increase column | |
4b6a582b VZ |
1031 | wxLogTrace("propgrid", |
1032 | wxS(" Adjust last column to %i"), | |
1033 | m_colWidths[lastColumn] + widthHigher); | |
1c4293cb VZ |
1034 | m_colWidths[lastColumn] = m_colWidths[lastColumn] + widthHigher; |
1035 | } | |
1036 | else if ( colsWidth > width ) | |
1037 | { | |
1038 | // Reduce column | |
1039 | if ( reduceCol != -1 ) | |
1040 | { | |
4b6a582b VZ |
1041 | wxLogTrace("propgrid", |
1042 | wxT(" Reduce column %i (by %i)"), | |
1043 | reduceCol, -widthHigher); | |
1044 | ||
1c4293cb VZ |
1045 | // Reduce widest column, and recheck |
1046 | m_colWidths[reduceCol] = m_colWidths[reduceCol] + widthHigher; | |
1047 | CheckColumnWidths(); | |
1048 | } | |
1049 | } | |
1050 | } | |
1051 | else | |
1052 | { | |
1053 | // Only check colsWidth against clientWidth | |
1054 | if ( colsWidth < clientWidth ) | |
1055 | { | |
1056 | m_colWidths[lastColumn] = m_colWidths[lastColumn] + (clientWidth-colsWidth); | |
1057 | } | |
1058 | ||
1059 | m_width = colsWidth; | |
1060 | ||
1061 | // If width changed, recalculate virtual size | |
1062 | if ( pg->GetState() == this ) | |
1063 | pg->RecalculateVirtualSize(); | |
1064 | } | |
1065 | ||
4b6a582b VZ |
1066 | for ( i=0; i<m_colWidths.size(); i++ ) |
1067 | { | |
1068 | wxLogTrace("propgrid", wxS("col%i: %i"), i, m_colWidths[i]); | |
1069 | } | |
1c4293cb VZ |
1070 | |
1071 | // Auto center splitter | |
fe01f16e | 1072 | if ( !m_dontCenterSplitter ) |
1c4293cb | 1073 | { |
fe01f16e JS |
1074 | if ( m_colWidths.size() == 2 && |
1075 | m_columnProportions[0] == m_columnProportions[1] ) | |
1c4293cb | 1076 | { |
fe01f16e JS |
1077 | // |
1078 | // When we have two columns of equal proportion, then use this | |
1079 | // code. It will look nicer when the scrollbar visibility is | |
1080 | // toggled on and off. | |
1081 | // | |
1082 | // TODO: Adapt this to generic recenter code. | |
1083 | // | |
1084 | float centerX = (float)(pg->m_width/2); | |
1085 | float splitterX; | |
1086 | ||
1087 | if ( m_fSplitterX < 0.0 ) | |
1088 | { | |
1089 | splitterX = centerX; | |
1090 | } | |
1091 | else if ( widthChange ) | |
1092 | { | |
1093 | //float centerX = float(pg->GetSize().x) * 0.5; | |
1c4293cb | 1094 | |
fe01f16e JS |
1095 | // Recenter? |
1096 | splitterX = m_fSplitterX + (float(widthChange) * 0.5); | |
1097 | float deviation = fabs(centerX - splitterX); | |
1c4293cb | 1098 | |
fe01f16e JS |
1099 | // If deviating from center, adjust towards it |
1100 | if ( deviation > 20.0 ) | |
1101 | { | |
1102 | if ( splitterX > centerX) | |
1103 | splitterX -= 2; | |
1104 | else | |
1105 | splitterX += 2; | |
1106 | } | |
1107 | } | |
1108 | else | |
1c4293cb | 1109 | { |
fe01f16e JS |
1110 | // No width change, just keep sure we keep splitter position intact |
1111 | splitterX = m_fSplitterX; | |
1112 | float deviation = fabs(centerX - splitterX); | |
1113 | if ( deviation > 50.0 ) | |
1114 | { | |
1115 | splitterX = centerX; | |
1116 | } | |
1c4293cb | 1117 | } |
fe01f16e JS |
1118 | |
1119 | DoSetSplitterPosition((int)splitterX, 0, | |
1120 | wxPG_SPLITTER_FROM_AUTO_CENTER); | |
1121 | ||
1122 | m_fSplitterX = splitterX; // needed to retain accuracy | |
1c4293cb VZ |
1123 | } |
1124 | else | |
1125 | { | |
fe01f16e JS |
1126 | // |
1127 | // Generic re-center code | |
1128 | // | |
76733d4c JS |
1129 | ResetColumnSizes(wxPG_SPLITTER_FROM_AUTO_CENTER); |
1130 | } | |
1131 | } | |
1132 | } | |
fe01f16e | 1133 | |
76733d4c JS |
1134 | void wxPropertyGridPageState::ResetColumnSizes( int setSplitterFlags ) |
1135 | { | |
1136 | unsigned int i; | |
1137 | // Calculate sum of proportions | |
1138 | int psum = 0; | |
1139 | for ( i=0; i<m_colWidths.size(); i++ ) | |
1140 | psum += m_columnProportions[i]; | |
1141 | int puwid = (m_pPropGrid->m_width*256) / psum; | |
1142 | int cpos = 0; | |
fe01f16e | 1143 | |
76733d4c JS |
1144 | // Convert proportion to splitter positions |
1145 | for ( i=0; i<(m_colWidths.size() - 1); i++ ) | |
1146 | { | |
1147 | int cwid = (puwid*m_columnProportions[i]) / 256; | |
1148 | cpos += cwid; | |
1149 | DoSetSplitterPosition(cpos, i, | |
1150 | setSplitterFlags); | |
1c4293cb VZ |
1151 | } |
1152 | } | |
1153 | ||
1154 | void wxPropertyGridPageState::SetColumnCount( int colCount ) | |
1155 | { | |
1156 | wxASSERT( colCount >= 2 ); | |
1157 | m_colWidths.SetCount( colCount, wxPG_DRAG_MARGIN ); | |
fe01f16e | 1158 | m_columnProportions.SetCount( colCount, 1 ); |
1c4293cb | 1159 | if ( m_colWidths.size() > (unsigned int)colCount ) |
9dac189e JS |
1160 | m_colWidths.RemoveAt( m_colWidths.size()-1, |
1161 | m_colWidths.size() - colCount ); | |
1c4293cb VZ |
1162 | |
1163 | if ( m_pPropGrid->GetState() == this ) | |
1164 | m_pPropGrid->RecalculateVirtualSize(); | |
1165 | else | |
1166 | CheckColumnWidths(); | |
1167 | } | |
1168 | ||
fe01f16e JS |
1169 | void wxPropertyGridPageState::DoSetColumnProportion( unsigned int column, |
1170 | int proportion ) | |
1171 | { | |
1172 | wxASSERT_MSG( proportion >= 1, | |
1173 | "Column proportion must 1 or higher" ); | |
1174 | ||
1175 | if ( proportion < 1 ) | |
1176 | proportion = 1; | |
1177 | ||
1178 | while ( m_columnProportions.size() <= column ) | |
1179 | m_columnProportions.push_back(1); | |
1180 | ||
1181 | m_columnProportions[column] = proportion; | |
1182 | } | |
1183 | ||
1c4293cb VZ |
1184 | // Returns column index, -1 for margin |
1185 | int wxPropertyGridPageState::HitTestH( int x, int* pSplitterHit, int* pSplitterHitOffset ) const | |
1186 | { | |
1187 | int cx = GetGrid()->m_marginWidth; | |
1188 | int col = -1; | |
1189 | int prevSplitter = -1; | |
1190 | ||
1191 | while ( x > cx ) | |
1192 | { | |
1193 | col++; | |
1194 | if ( col >= (int)m_colWidths.size() ) | |
1195 | { | |
1196 | *pSplitterHit = -1; | |
1197 | return col; | |
1198 | } | |
1199 | prevSplitter = cx; | |
1200 | cx += m_colWidths[col]; | |
1201 | } | |
1202 | ||
1203 | // Near prev. splitter | |
1204 | if ( col >= 1 ) | |
1205 | { | |
1206 | int diff = x - prevSplitter; | |
1207 | if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 ) | |
1208 | { | |
1209 | *pSplitterHit = col - 1; | |
1210 | *pSplitterHitOffset = diff; | |
1211 | return col; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | // Near next splitter | |
1216 | int nextSplitter = cx; | |
1217 | if ( col < (int)(m_colWidths.size()-1) ) | |
1218 | { | |
1219 | int diff = x - nextSplitter; | |
1220 | if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 ) | |
1221 | { | |
1222 | *pSplitterHit = col; | |
1223 | *pSplitterHitOffset = diff; | |
1224 | return col; | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | *pSplitterHit = -1; | |
1229 | return col; | |
1230 | } | |
1231 | ||
169dc975 JS |
1232 | bool wxPropertyGridPageState::ArePropertiesAdjacent( wxPGProperty* prop1, |
1233 | wxPGProperty* prop2, | |
1234 | int iterFlags ) const | |
1235 | { | |
1236 | const wxPGProperty* ap1 = | |
1237 | wxPropertyGridConstIterator::OneStep(this, | |
1238 | iterFlags, | |
1239 | prop1, | |
1240 | 1); | |
1241 | if ( ap1 && ap1 == prop2 ) | |
1242 | return true; | |
1243 | ||
1244 | const wxPGProperty* ap2 = | |
1245 | wxPropertyGridConstIterator::OneStep(this, | |
1246 | iterFlags, | |
1247 | prop1, | |
1248 | -1); | |
1249 | if ( ap2 && ap2 == prop2 ) | |
1250 | return true; | |
1251 | ||
1252 | return false; | |
1253 | } | |
1254 | ||
1c4293cb VZ |
1255 | // ----------------------------------------------------------------------- |
1256 | // wxPropertyGridPageState property value setting and getting | |
1257 | // ----------------------------------------------------------------------- | |
1258 | ||
1259 | bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty* p, const wxString& value ) | |
1260 | { | |
1261 | if ( p ) | |
1262 | { | |
f275b5db | 1263 | int flags = wxPG_REPORT_ERROR|wxPG_FULL_VALUE|wxPG_PROGRAMMATIC_VALUE; |
1c4293cb VZ |
1264 | |
1265 | wxVariant variant = p->GetValueRef(); | |
1266 | bool res; | |
1267 | ||
1268 | if ( p->GetMaxLength() <= 0 ) | |
1269 | res = p->StringToValue( variant, value, flags ); | |
1270 | else | |
1271 | res = p->StringToValue( variant, value.Mid(0,p->GetMaxLength()), flags ); | |
1272 | ||
1273 | if ( res ) | |
1274 | { | |
1275 | p->SetValue(variant); | |
fc72fab6 JS |
1276 | if ( p == m_pPropGrid->GetSelection() && |
1277 | this == m_pPropGrid->GetState() ) | |
a6353fe8 | 1278 | m_pPropGrid->RefreshEditor(); |
1c4293cb VZ |
1279 | } |
1280 | ||
1281 | return true; | |
1282 | } | |
1283 | return false; | |
1284 | } | |
1285 | ||
1286 | // ----------------------------------------------------------------------- | |
1287 | ||
1288 | bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty* p, wxVariant& value ) | |
1289 | { | |
1290 | if ( p ) | |
1291 | { | |
1292 | p->SetValue(value); | |
fc72fab6 JS |
1293 | if ( p == m_pPropGrid->GetSelection() && |
1294 | this == m_pPropGrid->GetState() ) | |
a6353fe8 | 1295 | m_pPropGrid->RefreshEditor(); |
1c4293cb VZ |
1296 | |
1297 | return true; | |
1298 | } | |
1299 | return false; | |
1300 | } | |
1301 | ||
1302 | // ----------------------------------------------------------------------- | |
1303 | ||
1304 | bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty* p, wxObject* value ) | |
1305 | { | |
1306 | if ( p ) | |
1307 | { | |
1308 | // wnd_primary has to be given so the control can be updated as well. | |
1309 | wxVariant v(value); | |
1310 | DoSetPropertyValue(p, v); | |
1311 | return true; | |
1312 | } | |
1313 | return false; | |
1314 | } | |
1315 | ||
1c4293cb VZ |
1316 | // ----------------------------------------------------------------------- |
1317 | // wxPropertyGridPageState property operations | |
1318 | // ----------------------------------------------------------------------- | |
1319 | ||
fc72fab6 JS |
1320 | bool wxPropertyGridPageState::DoIsPropertySelected( wxPGProperty* prop ) const |
1321 | { | |
8d2c7041 JS |
1322 | if ( wxPGFindInVector(m_selection, prop) != wxNOT_FOUND ) |
1323 | return true; | |
fc72fab6 JS |
1324 | |
1325 | return false; | |
1326 | } | |
1327 | ||
1328 | // ----------------------------------------------------------------------- | |
1329 | ||
7f3f8f1e JS |
1330 | void wxPropertyGridPageState::DoRemoveFromSelection( wxPGProperty* prop ) |
1331 | { | |
1332 | for ( unsigned int i=0; i<m_selection.size(); i++ ) | |
1333 | { | |
1334 | if ( m_selection[i] == prop ) | |
1335 | { | |
afaf3b70 JS |
1336 | wxPropertyGrid* pg = m_pPropGrid; |
1337 | if ( i == 0 && pg->GetState() == this ) | |
1338 | { | |
1339 | // If first item (ie. one with the active editor) was | |
1340 | // deselected, then we need to take some extra measures. | |
1341 | wxArrayPGProperty sel = m_selection; | |
1342 | sel.erase( sel.begin() + i ); | |
1343 | ||
1344 | wxPGProperty* newFirst; | |
1345 | if ( sel.size() ) | |
1346 | newFirst = sel[0]; | |
1347 | else | |
1348 | newFirst = NULL; | |
1349 | ||
1350 | pg->DoSelectProperty(newFirst, | |
1351 | wxPG_SEL_DONT_SEND_EVENT); | |
1352 | ||
1353 | m_selection = sel; | |
1354 | ||
1355 | pg->Refresh(); | |
1356 | } | |
1357 | else | |
1358 | { | |
1359 | m_selection.erase( m_selection.begin() + i ); | |
1360 | } | |
7f3f8f1e JS |
1361 | return; |
1362 | } | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | // ----------------------------------------------------------------------- | |
1367 | ||
1c4293cb VZ |
1368 | bool wxPropertyGridPageState::DoCollapse( wxPGProperty* p ) |
1369 | { | |
1370 | wxCHECK_MSG( p, false, wxT("invalid property id") ); | |
1371 | ||
1372 | if ( !p->GetChildCount() ) return false; | |
1373 | ||
1374 | if ( !p->IsExpanded() ) return false; | |
1375 | ||
1376 | p->SetExpanded(false); | |
1377 | ||
1378 | VirtualHeightChanged(); | |
1379 | ||
1380 | return true; | |
1381 | } | |
1382 | ||
1383 | // ----------------------------------------------------------------------- | |
1384 | ||
1385 | bool wxPropertyGridPageState::DoExpand( wxPGProperty* p ) | |
1386 | { | |
1387 | wxCHECK_MSG( p, false, wxT("invalid property id") ); | |
1388 | ||
1389 | if ( !p->GetChildCount() ) return false; | |
1390 | ||
1391 | if ( p->IsExpanded() ) return false; | |
1392 | ||
1393 | p->SetExpanded(true); | |
1394 | ||
1395 | VirtualHeightChanged(); | |
1396 | ||
1397 | return true; | |
1398 | } | |
1399 | ||
1400 | // ----------------------------------------------------------------------- | |
1401 | ||
1402 | bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty* p, unsigned int flags ) | |
1403 | { | |
1404 | if ( this == m_pPropGrid->GetState() ) | |
1405 | return m_pPropGrid->DoSelectProperty( p, flags ); | |
1406 | ||
fc72fab6 | 1407 | DoSetSelection(p); |
1c4293cb VZ |
1408 | return true; |
1409 | } | |
1410 | ||
1411 | // ----------------------------------------------------------------------- | |
1412 | ||
1413 | bool wxPropertyGridPageState::DoHideProperty( wxPGProperty* p, bool hide, int flags ) | |
1414 | { | |
3ded4b22 | 1415 | p->DoHide(hide, flags); |
1c4293cb VZ |
1416 | VirtualHeightChanged(); |
1417 | ||
1418 | return true; | |
1419 | } | |
1420 | ||
1c4293cb VZ |
1421 | // ----------------------------------------------------------------------- |
1422 | // wxPropertyGridPageState wxVariant related routines | |
1423 | // ----------------------------------------------------------------------- | |
1424 | ||
1425 | // Returns list of wxVariant objects (non-categories and non-sub-properties only). | |
1426 | // Never includes sub-properties (unless they are parented by wxParentProperty). | |
1427 | wxVariant wxPropertyGridPageState::DoGetPropertyValues( const wxString& listname, | |
1428 | wxPGProperty* baseparent, | |
1429 | long flags ) const | |
1430 | { | |
1431 | wxPGProperty* pwc = (wxPGProperty*) baseparent; | |
1432 | ||
1433 | // Root is the default base-parent. | |
1434 | if ( !pwc ) | |
1435 | pwc = m_properties; | |
1436 | ||
1437 | wxVariantList tempList; | |
1438 | wxVariant v( tempList, listname ); | |
1439 | ||
1440 | if ( pwc->GetChildCount() ) | |
1441 | { | |
1442 | if ( flags & wxPG_KEEP_STRUCTURE ) | |
1443 | { | |
1444 | wxASSERT( !pwc->HasFlag(wxPG_PROP_AGGREGATE) ); | |
1445 | ||
1446 | size_t i; | |
1447 | for ( i=0; i<pwc->GetChildCount(); i++ ) | |
1448 | { | |
1449 | wxPGProperty* p = pwc->Item(i); | |
1450 | if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) ) | |
1451 | { | |
1452 | wxVariant variant = p->GetValue(); | |
1453 | variant.SetName( p->GetBaseName() ); | |
1454 | v.Append( variant ); | |
1455 | } | |
1456 | else | |
1457 | { | |
1458 | v.Append( DoGetPropertyValues(p->m_name,p,flags|wxPG_KEEP_STRUCTURE) ); | |
1459 | } | |
1460 | if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() ) | |
1461 | v.Append( p->GetAttributesAsList() ); | |
1462 | } | |
1463 | } | |
1464 | else | |
1465 | { | |
1466 | wxPropertyGridConstIterator it( this, wxPG_ITERATE_DEFAULT, pwc->Item(0) ); | |
1467 | it.SetBaseParent( pwc ); | |
1468 | ||
1469 | for ( ; !it.AtEnd(); it.Next() ) | |
1470 | { | |
1471 | const wxPGProperty* p = it.GetProperty(); | |
1472 | ||
1473 | // Use a trick to ignore wxParentProperty itself, but not its sub-properties. | |
1474 | if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) ) | |
1475 | { | |
1476 | wxVariant variant = p->GetValue(); | |
1477 | variant.SetName( p->GetName() ); | |
1478 | v.Append( variant ); | |
1479 | if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() ) | |
1480 | v.Append( p->GetAttributesAsList() ); | |
1481 | } | |
1482 | } | |
1483 | } | |
1484 | } | |
1485 | ||
1486 | return v; | |
1487 | } | |
1488 | ||
1489 | // ----------------------------------------------------------------------- | |
1490 | ||
1491 | void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList& list, wxPGProperty* defaultCategory ) | |
1492 | { | |
1493 | unsigned char origFrozen = 1; | |
1494 | ||
1495 | if ( m_pPropGrid->GetState() == this ) | |
1496 | { | |
1497 | origFrozen = m_pPropGrid->m_frozen; | |
1498 | if ( !origFrozen ) m_pPropGrid->Freeze(); | |
1499 | } | |
1500 | ||
1501 | wxPropertyCategory* use_category = (wxPropertyCategory*)defaultCategory; | |
1502 | ||
1503 | if ( !use_category ) | |
1504 | use_category = (wxPropertyCategory*)m_properties; | |
1505 | ||
1506 | // Let's iterate over the list of variants. | |
1507 | wxVariantList::const_iterator node; | |
1508 | int numSpecialEntries = 0; | |
1509 | ||
1510 | // | |
1511 | // Second pass for special entries | |
b7bc9d80 | 1512 | for ( node = list.begin(); node != list.end(); ++node ) |
1c4293cb VZ |
1513 | { |
1514 | wxVariant *current = (wxVariant*)*node; | |
1515 | ||
1516 | // Make sure it is wxVariant. | |
1517 | wxASSERT( current ); | |
1518 | wxASSERT( wxStrcmp(current->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 ); | |
1519 | ||
1520 | const wxString& name = current->GetName(); | |
6636ef8d | 1521 | if ( !name.empty() ) |
1c4293cb VZ |
1522 | { |
1523 | // | |
1524 | // '@' signified a special entry | |
1525 | if ( name[0] == wxS('@') ) | |
1526 | { | |
1527 | numSpecialEntries++; | |
1528 | } | |
1529 | else | |
1530 | { | |
1531 | wxPGProperty* foundProp = BaseGetPropertyByName(name); | |
1532 | if ( foundProp ) | |
1533 | { | |
1534 | wxPGProperty* p = foundProp; | |
1535 | ||
1536 | // If it was a list, we still have to go through it. | |
1537 | if ( wxStrcmp(current->GetType(), wxS("list")) == 0 ) | |
1538 | { | |
1539 | DoSetPropertyValues( current->GetList(), | |
d3b9f782 | 1540 | p->IsCategory()?p:(NULL) |
1c4293cb VZ |
1541 | ); |
1542 | } | |
1543 | else | |
1544 | { | |
4b6a582b VZ |
1545 | wxASSERT_LEVEL_2_MSG( |
1546 | wxStrcmp(current->GetType(), p->GetValue().GetType()) == 0, | |
1547 | wxString::Format( | |
1548 | wxS("setting value of property \"%s\" from variant"), | |
1549 | p->GetName().c_str()) | |
1550 | ); | |
1c4293cb VZ |
1551 | |
1552 | p->SetValue(*current); | |
1553 | } | |
1554 | } | |
1555 | else | |
1556 | { | |
1557 | // Is it list? | |
1558 | if ( current->GetType() != wxS("list") ) | |
1559 | { | |
1560 | // Not. | |
1561 | } | |
1562 | else | |
1563 | { | |
1564 | // Yes, it is; create a sub category and append contents there. | |
1565 | wxPGProperty* newCat = DoInsert(use_category,-1,new wxPropertyCategory(current->GetName(),wxPG_LABEL)); | |
1566 | DoSetPropertyValues( current->GetList(), newCat ); | |
1567 | } | |
1568 | } | |
1569 | } | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | if ( numSpecialEntries ) | |
1574 | { | |
b7bc9d80 | 1575 | for ( node = list.begin(); node != list.end(); ++node ) |
1c4293cb VZ |
1576 | { |
1577 | wxVariant *current = (wxVariant*)*node; | |
1578 | ||
1579 | const wxString& name = current->GetName(); | |
6636ef8d | 1580 | if ( !name.empty() ) |
1c4293cb VZ |
1581 | { |
1582 | // | |
1583 | // '@' signified a special entry | |
1584 | if ( name[0] == wxS('@') ) | |
1585 | { | |
1586 | numSpecialEntries--; | |
1587 | ||
1588 | size_t pos2 = name.rfind(wxS('@')); | |
1589 | if ( pos2 > 0 && pos2 < (name.size()-1) ) | |
1590 | { | |
1591 | wxString propName = name.substr(1, pos2-1); | |
1592 | wxString entryType = name.substr(pos2+1, wxString::npos); | |
1593 | ||
1594 | if ( entryType == wxS("attr") ) | |
1595 | { | |
1596 | // | |
1597 | // List of attributes | |
1598 | wxPGProperty* foundProp = BaseGetPropertyByName(propName); | |
1599 | if ( foundProp ) | |
1600 | { | |
0372d42e | 1601 | wxASSERT( current->GetType() == wxPG_VARIANT_TYPE_LIST ); |
1c4293cb VZ |
1602 | |
1603 | wxVariantList& list2 = current->GetList(); | |
1604 | wxVariantList::const_iterator node2; | |
1605 | ||
b7bc9d80 | 1606 | for ( node2 = list2.begin(); node2 != list2.end(); ++node2 ) |
1c4293cb VZ |
1607 | { |
1608 | wxVariant *attr = (wxVariant*)*node2; | |
1609 | foundProp->SetAttribute( attr->GetName(), *attr ); | |
1610 | } | |
1611 | } | |
1612 | else | |
1613 | { | |
1614 | // ERROR: No such property: 'propName' | |
1615 | } | |
1616 | } | |
1617 | } | |
1618 | else | |
1619 | { | |
1620 | // ERROR: Special entry requires name of format @<propname>@<entrytype> | |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | if ( !numSpecialEntries ) | |
1626 | break; | |
1627 | } | |
1628 | } | |
1629 | ||
1630 | if ( !origFrozen ) | |
1631 | { | |
1632 | m_pPropGrid->Thaw(); | |
1633 | ||
1634 | if ( this == m_pPropGrid->GetState() ) | |
a6353fe8 | 1635 | m_pPropGrid->RefreshEditor(); |
1c4293cb VZ |
1636 | } |
1637 | ||
1638 | } | |
1639 | ||
1640 | // ----------------------------------------------------------------------- | |
1641 | // wxPropertyGridPageState property adding and removal | |
1642 | // ----------------------------------------------------------------------- | |
1643 | ||
2fd4a524 JS |
1644 | bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty* property, |
1645 | wxPGProperty* scheduledParent ) | |
1c4293cb VZ |
1646 | { |
1647 | wxPropertyGrid* propGrid = m_pPropGrid; | |
1648 | ||
4c51a665 | 1649 | // This will allow better behaviour. |
1c4293cb | 1650 | if ( scheduledParent == m_properties ) |
d3b9f782 | 1651 | scheduledParent = NULL; |
1c4293cb | 1652 | |
d665918b JS |
1653 | if ( scheduledParent && !scheduledParent->IsCategory() ) |
1654 | { | |
1655 | wxASSERT_MSG( property->GetBaseName().length(), | |
1656 | "Property's children must have unique, non-empty names within their scope" ); | |
1657 | } | |
1658 | ||
1c4293cb VZ |
1659 | property->m_parentState = this; |
1660 | ||
1661 | if ( property->IsCategory() ) | |
1662 | { | |
1663 | ||
1664 | // Parent of a category must be either root or another category | |
1665 | // (otherwise Bad Things might happen). | |
1666 | wxASSERT_MSG( scheduledParent == NULL || | |
1667 | scheduledParent == m_properties || | |
1668 | scheduledParent->IsCategory(), | |
1669 | wxT("Parent of a category must be either root or another category.")); | |
1670 | ||
1671 | // If we already have category with same name, delete given property | |
1672 | // and use it instead as most recent caption item. | |
1673 | wxPGProperty* found_id = BaseGetPropertyByName( property->GetBaseName() ); | |
1674 | if ( found_id ) | |
1675 | { | |
1676 | wxPropertyCategory* pwc = (wxPropertyCategory*) found_id; | |
1677 | if ( pwc->IsCategory() ) // Must be a category. | |
1678 | { | |
1679 | delete property; | |
1680 | m_currentCategory = pwc; | |
2fd4a524 | 1681 | return false; |
1c4293cb VZ |
1682 | } |
1683 | } | |
1684 | } | |
1685 | ||
657a8a35 | 1686 | #if wxDEBUG_LEVEL |
1c4293cb VZ |
1687 | // Warn for identical names in debug mode. |
1688 | if ( BaseGetPropertyByName(property->GetName()) && | |
1689 | (!scheduledParent || scheduledParent->IsCategory()) ) | |
1690 | { | |
657a8a35 VZ |
1691 | wxFAIL_MSG(wxString::Format( |
1692 | "wxPropertyGrid item with name \"%s\" already exists", | |
1693 | property->GetName())); | |
1694 | ||
1c4293cb VZ |
1695 | wxPGGlobalVars->m_warnings++; |
1696 | } | |
657a8a35 | 1697 | #endif // wxDEBUG_LEVEL |
1c4293cb | 1698 | |
2fd4a524 JS |
1699 | // NULL parent == root parent |
1700 | if ( !scheduledParent ) | |
1701 | scheduledParent = DoGetRoot(); | |
1c4293cb | 1702 | |
2fd4a524 | 1703 | property->m_parent = scheduledParent; |
1c4293cb | 1704 | |
2fd4a524 | 1705 | property->InitAfterAdded(this, propGrid); |
1c4293cb | 1706 | |
2fd4a524 | 1707 | if ( property->IsCategory() ) |
1c4293cb | 1708 | { |
2fd4a524 | 1709 | wxPropertyCategory* pc = wxStaticCast(property, wxPropertyCategory); |
1c4293cb | 1710 | |
2fd4a524 | 1711 | m_currentCategory = pc; |
1c4293cb | 1712 | |
2fd4a524 | 1713 | // Calculate text extent for category caption |
1c4293cb VZ |
1714 | if ( propGrid ) |
1715 | pc->CalculateTextExtent(propGrid, propGrid->GetCaptionFont()); | |
1c4293cb | 1716 | } |
2fd4a524 JS |
1717 | |
1718 | return true; | |
1c4293cb VZ |
1719 | } |
1720 | ||
1721 | // ----------------------------------------------------------------------- | |
1722 | ||
1723 | wxPGProperty* wxPropertyGridPageState::DoAppend( wxPGProperty* property ) | |
1724 | { | |
1725 | wxPropertyCategory* cur_cat = m_currentCategory; | |
1726 | if ( property->IsCategory() ) | |
d3b9f782 | 1727 | cur_cat = NULL; |
1c4293cb VZ |
1728 | |
1729 | return DoInsert( cur_cat, -1, property ); | |
1730 | } | |
1731 | ||
1732 | // ----------------------------------------------------------------------- | |
1733 | ||
1734 | wxPGProperty* wxPropertyGridPageState::DoInsert( wxPGProperty* parent, int index, wxPGProperty* property ) | |
1735 | { | |
1736 | if ( !parent ) | |
1737 | parent = m_properties; | |
1738 | ||
1739 | wxCHECK_MSG( !parent->HasFlag(wxPG_PROP_AGGREGATE), | |
1740 | wxNullProperty, | |
1741 | wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") ); | |
1742 | ||
2fd4a524 | 1743 | bool res = PrepareToAddItem( property, (wxPropertyCategory*)parent ); |
1c4293cb | 1744 | |
4c51a665 | 1745 | // PrepareToAddItem() may just decide to use current category |
2fd4a524 JS |
1746 | // instead of adding new one. |
1747 | if ( !res ) | |
1c4293cb VZ |
1748 | return m_currentCategory; |
1749 | ||
94b8ecf1 JS |
1750 | bool parentIsRoot = parent->IsRoot(); |
1751 | bool parentIsCategory = parent->IsCategory(); | |
1752 | ||
1c4293cb VZ |
1753 | // Note that item must be added into current mode later. |
1754 | ||
1755 | // If parent is wxParentProperty, just stick it in... | |
1756 | // If parent is root (m_properties), then... | |
1757 | // In categoric mode: Add as last item in m_abcArray (if not category). | |
1758 | // Add to given index in m_regularArray. | |
1759 | // In non-cat mode: Add as last item in m_regularArray. | |
1760 | // Add to given index in m_abcArray. | |
1761 | // If parent is category, then... | |
1762 | // 1) Add to given category in given index. | |
1763 | // 2) Add as last item in m_abcArray. | |
1764 | ||
94b8ecf1 | 1765 | if ( m_properties == &m_regularArray ) |
1c4293cb | 1766 | { |
94b8ecf1 | 1767 | // We are currently in Categorized mode |
1c4293cb | 1768 | |
94b8ecf1 JS |
1769 | // Only add non-categories to m_abcArray. |
1770 | if ( m_abcArray && !property->IsCategory() && | |
1771 | (parentIsCategory || parentIsRoot) ) | |
1c4293cb | 1772 | { |
48a32cf6 | 1773 | m_abcArray->DoAddChild( property, -1, false ); |
1c4293cb | 1774 | } |
1c4293cb | 1775 | |
94b8ecf1 | 1776 | // Add to current mode. |
48a32cf6 | 1777 | parent->DoAddChild( property, index, true ); |
94b8ecf1 JS |
1778 | } |
1779 | else | |
1780 | { | |
1781 | // We are currently in Non-categorized/Alphabetic mode | |
1782 | ||
1783 | if ( parentIsCategory ) | |
1784 | // Parent is category. | |
48a32cf6 | 1785 | parent->DoAddChild( property, index, false ); |
94b8ecf1 JS |
1786 | else if ( parentIsRoot ) |
1787 | // Parent is root. | |
48a32cf6 | 1788 | m_regularArray.DoAddChild( property, -1, false ); |
94b8ecf1 JS |
1789 | |
1790 | // Add to current mode | |
1791 | if ( !property->IsCategory() ) | |
48a32cf6 | 1792 | m_abcArray->DoAddChild( property, index, true ); |
1c4293cb VZ |
1793 | } |
1794 | ||
1795 | // category stuff | |
1796 | if ( property->IsCategory() ) | |
1797 | { | |
1798 | // This is a category caption item. | |
1799 | ||
1800 | // Last caption is not the bottom one (this info required by append) | |
1801 | m_lastCaptionBottomnest = 0; | |
1802 | } | |
1803 | ||
1804 | // Only add name to hashmap if parent is root or category | |
6636ef8d | 1805 | if ( !property->m_name.empty() && |
94b8ecf1 | 1806 | (parentIsCategory || parentIsRoot) ) |
1c4293cb VZ |
1807 | m_dictName[property->m_name] = (void*) property; |
1808 | ||
1809 | VirtualHeightChanged(); | |
1810 | ||
1811 | property->UpdateParentValues(); | |
1812 | ||
1813 | m_itemsAdded = 1; | |
1814 | ||
1815 | return property; | |
1816 | } | |
1817 | ||
1818 | // ----------------------------------------------------------------------- | |
1819 | ||
f915d44b | 1820 | void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete ) |
1c4293cb VZ |
1821 | { |
1822 | wxCHECK_RET( item->GetParent(), | |
1823 | wxT("this property was already deleted") ); | |
1824 | ||
1825 | wxCHECK_RET( item != &m_regularArray && item != m_abcArray, | |
1826 | wxT("wxPropertyGrid: Do not attempt to remove the root item.") ); | |
1827 | ||
f231df8a JS |
1828 | wxPropertyGrid* pg = GetGrid(); |
1829 | ||
1830 | // Must defer deletion? Yes, if handling a wxPG event. | |
1831 | if ( pg && pg->m_processedEvent ) | |
1832 | { | |
1833 | if ( doDelete ) | |
1834 | pg->m_deletedProperties.push_back(item); | |
1835 | else | |
1836 | pg->m_removedProperties.push_back(item); | |
9493cc02 JS |
1837 | |
1838 | // Rename the property so it won't remain in the way | |
1839 | // of the user code. | |
1840 | ||
1841 | // Let's trust that no sane property uses prefix like | |
1842 | // this. It would be anyway fairly inconvenient (in | |
1843 | // current code) to check whether a new name is used | |
1844 | // by another property with parent (due to the child | |
1845 | // name notation). | |
1846 | wxString newName = wxS("_&/_%$") + item->GetBaseName(); | |
1847 | DoSetPropertyName(item, newName); | |
1848 | ||
f231df8a JS |
1849 | return; |
1850 | } | |
1851 | ||
1c4293cb VZ |
1852 | unsigned int indinparent = item->GetIndexInParent(); |
1853 | ||
1854 | wxPGProperty* pwc = (wxPGProperty*)item; | |
91c818f8 | 1855 | wxPGProperty* parent = item->GetParent(); |
1c4293cb | 1856 | |
91c818f8 | 1857 | wxCHECK_RET( !parent->HasFlag(wxPG_PROP_AGGREGATE), |
1c4293cb VZ |
1858 | wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") ); |
1859 | ||
fc72fab6 JS |
1860 | wxASSERT( item->GetParentState() == this ); |
1861 | ||
fc72fab6 JS |
1862 | if ( DoIsPropertySelected(item) ) |
1863 | { | |
1864 | if ( pg && pg->GetState() == this ) | |
1865 | { | |
1866 | pg->DoRemoveFromSelection(item, | |
1867 | wxPG_SEL_DELETING|wxPG_SEL_NOVALIDATE); | |
1868 | } | |
1869 | else | |
1870 | { | |
7f3f8f1e | 1871 | DoRemoveFromSelection(item); |
fc72fab6 JS |
1872 | } |
1873 | } | |
1874 | ||
1875 | item->SetFlag(wxPG_PROP_BEING_DELETED); | |
1876 | ||
91c818f8 JS |
1877 | // Delete children |
1878 | if ( item->GetChildCount() && !item->HasFlag(wxPG_PROP_AGGREGATE) ) | |
1c4293cb VZ |
1879 | { |
1880 | // deleting a category | |
91c818f8 | 1881 | if ( item->IsCategory() ) |
1c4293cb | 1882 | { |
91c818f8 | 1883 | if ( pwc == m_currentCategory ) |
d3b9f782 | 1884 | m_currentCategory = NULL; |
1c4293cb VZ |
1885 | } |
1886 | ||
91c818f8 | 1887 | item->DeleteChildren(); |
1c4293cb VZ |
1888 | } |
1889 | ||
1890 | if ( !IsInNonCatMode() ) | |
1891 | { | |
1892 | // categorized mode - non-categorized array | |
1893 | ||
91c818f8 JS |
1894 | // Remove from non-cat array |
1895 | if ( !item->IsCategory() && | |
1896 | (parent->IsCategory() || parent->IsRoot()) ) | |
1c4293cb VZ |
1897 | { |
1898 | if ( m_abcArray ) | |
d8c74d04 | 1899 | m_abcArray->RemoveChild(item); |
1c4293cb VZ |
1900 | } |
1901 | ||
1902 | // categorized mode - categorized array | |
91c818f8 | 1903 | wxArrayPGProperty& parentsChildren = parent->m_children; |
d8c74d04 | 1904 | parentsChildren.erase( parentsChildren.begin() + indinparent ); |
1b895132 | 1905 | item->m_parent->FixIndicesOfChildren(); |
1c4293cb VZ |
1906 | } |
1907 | else | |
1908 | { | |
1909 | // non-categorized mode - categorized array | |
1910 | ||
1911 | // We need to find location of item. | |
1912 | wxPGProperty* cat_parent = &m_regularArray; | |
1913 | int cat_index = m_regularArray.GetChildCount(); | |
1914 | size_t i; | |
1915 | for ( i = 0; i < m_regularArray.GetChildCount(); i++ ) | |
1916 | { | |
1917 | wxPGProperty* p = m_regularArray.Item(i); | |
1918 | if ( p == item ) { cat_index = i; break; } | |
1919 | if ( p->IsCategory() ) | |
1920 | { | |
1921 | int subind = ((wxPGProperty*)p)->Index(item); | |
1922 | if ( subind != wxNOT_FOUND ) | |
1923 | { | |
1924 | cat_parent = ((wxPGProperty*)p); | |
1925 | cat_index = subind; | |
1926 | break; | |
1927 | } | |
1928 | } | |
1929 | } | |
d8c74d04 | 1930 | cat_parent->m_children.erase(cat_parent->m_children.begin()+cat_index); |
1c4293cb VZ |
1931 | |
1932 | // non-categorized mode - non-categorized array | |
1933 | if ( !item->IsCategory() ) | |
1934 | { | |
1935 | wxASSERT( item->m_parent == m_abcArray ); | |
d8c74d04 JS |
1936 | wxArrayPGProperty& parentsChildren = item->m_parent->m_children; |
1937 | parentsChildren.erase(parentsChildren.begin() + indinparent); | |
1b895132 | 1938 | item->m_parent->FixIndicesOfChildren(indinparent); |
1c4293cb VZ |
1939 | } |
1940 | } | |
1941 | ||
6636ef8d | 1942 | if ( !item->GetBaseName().empty() && |
26740086 JS |
1943 | (parent->IsCategory() || parent->IsRoot()) ) |
1944 | m_dictName.erase(item->GetBaseName()); | |
1c4293cb | 1945 | |
03647350 VZ |
1946 | // We need to clear parent grid's m_propHover, if it matches item |
1947 | if ( pg && pg->m_propHover == item ) | |
1948 | pg->m_propHover = NULL; | |
4d18ddc7 | 1949 | |
2ac06991 JS |
1950 | // Mark the property as 'unattached' |
1951 | item->m_parentState = NULL; | |
1952 | item->m_parent = NULL; | |
1953 | ||
1c4293cb | 1954 | // We can actually delete it now |
f915d44b JS |
1955 | if ( doDelete ) |
1956 | delete item; | |
ed8b46bb JS |
1957 | else |
1958 | item->OnDetached(this, pg); | |
1c4293cb VZ |
1959 | |
1960 | m_itemsAdded = 1; // Not a logical assignment (but required nonetheless). | |
1961 | ||
1962 | VirtualHeightChanged(); | |
1963 | } | |
1964 | ||
1965 | // ----------------------------------------------------------------------- | |
f4bc1aa2 JS |
1966 | |
1967 | #endif // wxUSE_PROPGRID |