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