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