1*128277c9SStephan Aßmus /* 2*128277c9SStephan Aßmus * Copyright 2006, Haiku. 3*128277c9SStephan Aßmus * Distributed under the terms of the MIT License. 4*128277c9SStephan Aßmus * 5*128277c9SStephan Aßmus * Authors: 6*128277c9SStephan Aßmus * Stephan Aßmus <superstippi@gmx.de> 7*128277c9SStephan Aßmus */ 8*128277c9SStephan Aßmus 9*128277c9SStephan Aßmus #include "Selection.h" 10*128277c9SStephan Aßmus 11*128277c9SStephan Aßmus #include <stdio.h> 12*128277c9SStephan Aßmus 13*128277c9SStephan Aßmus #include <debugger.h> 14*128277c9SStephan Aßmus 15*128277c9SStephan Aßmus #include "Selectable.h" 16*128277c9SStephan Aßmus 17*128277c9SStephan Aßmus #define DEBUG 1 18*128277c9SStephan Aßmus 19*128277c9SStephan Aßmus // constructor 20*128277c9SStephan Aßmus Selection::Selection() 21*128277c9SStephan Aßmus : fSelected(20) 22*128277c9SStephan Aßmus { 23*128277c9SStephan Aßmus } 24*128277c9SStephan Aßmus 25*128277c9SStephan Aßmus // destructor 26*128277c9SStephan Aßmus Selection::~Selection() 27*128277c9SStephan Aßmus { 28*128277c9SStephan Aßmus } 29*128277c9SStephan Aßmus 30*128277c9SStephan Aßmus // Select 31*128277c9SStephan Aßmus bool 32*128277c9SStephan Aßmus Selection::Select(Selectable* object, bool extend) 33*128277c9SStephan Aßmus { 34*128277c9SStephan Aßmus AutoNotificationSuspender _(this); 35*128277c9SStephan Aßmus 36*128277c9SStephan Aßmus if (!extend) 37*128277c9SStephan Aßmus _DeselectAllExcept(object); 38*128277c9SStephan Aßmus 39*128277c9SStephan Aßmus bool success = false; 40*128277c9SStephan Aßmus 41*128277c9SStephan Aßmus if (!object->IsSelected()) { 42*128277c9SStephan Aßmus 43*128277c9SStephan Aßmus #if DEBUG 44*128277c9SStephan Aßmus if (fSelected.HasItem((void*)object)) 45*128277c9SStephan Aßmus debugger("Selection::Select() - " 46*128277c9SStephan Aßmus "unselected object in list!"); 47*128277c9SStephan Aßmus #endif 48*128277c9SStephan Aßmus 49*128277c9SStephan Aßmus if (fSelected.AddItem((void*)object)) { 50*128277c9SStephan Aßmus object->SetSelected(true); 51*128277c9SStephan Aßmus success = true; 52*128277c9SStephan Aßmus 53*128277c9SStephan Aßmus Notify(); 54*128277c9SStephan Aßmus } else { 55*128277c9SStephan Aßmus fprintf(stderr, "Selection::Select() - out of memory\n"); 56*128277c9SStephan Aßmus } 57*128277c9SStephan Aßmus } else { 58*128277c9SStephan Aßmus 59*128277c9SStephan Aßmus #if DEBUG 60*128277c9SStephan Aßmus if (!fSelected.HasItem((void*)object)) 61*128277c9SStephan Aßmus debugger("Selection::Select() - " 62*128277c9SStephan Aßmus "already selected object not in list!"); 63*128277c9SStephan Aßmus #endif 64*128277c9SStephan Aßmus 65*128277c9SStephan Aßmus success = true; 66*128277c9SStephan Aßmus // object already in list 67*128277c9SStephan Aßmus } 68*128277c9SStephan Aßmus 69*128277c9SStephan Aßmus return success; 70*128277c9SStephan Aßmus } 71*128277c9SStephan Aßmus 72*128277c9SStephan Aßmus // Deselect 73*128277c9SStephan Aßmus void 74*128277c9SStephan Aßmus Selection::Deselect(Selectable* object) 75*128277c9SStephan Aßmus { 76*128277c9SStephan Aßmus if (object->IsSelected()) { 77*128277c9SStephan Aßmus if (!fSelected.RemoveItem((void*)object)) 78*128277c9SStephan Aßmus debugger("Selection::Deselect() - " 79*128277c9SStephan Aßmus "selected object not within list!"); 80*128277c9SStephan Aßmus object->SetSelected(false); 81*128277c9SStephan Aßmus 82*128277c9SStephan Aßmus Notify(); 83*128277c9SStephan Aßmus } 84*128277c9SStephan Aßmus } 85*128277c9SStephan Aßmus 86*128277c9SStephan Aßmus // DeselectAll 87*128277c9SStephan Aßmus void 88*128277c9SStephan Aßmus Selection::DeselectAll() 89*128277c9SStephan Aßmus { 90*128277c9SStephan Aßmus _DeselectAllExcept(NULL); 91*128277c9SStephan Aßmus } 92*128277c9SStephan Aßmus 93*128277c9SStephan Aßmus // #pragma mark - 94*128277c9SStephan Aßmus 95*128277c9SStephan Aßmus // SelectableAt 96*128277c9SStephan Aßmus Selectable* 97*128277c9SStephan Aßmus Selection::SelectableAt(int32 index) const 98*128277c9SStephan Aßmus { 99*128277c9SStephan Aßmus return (Selectable*)fSelected.ItemAt(index); 100*128277c9SStephan Aßmus } 101*128277c9SStephan Aßmus 102*128277c9SStephan Aßmus // SelectableAtFast 103*128277c9SStephan Aßmus Selectable* 104*128277c9SStephan Aßmus Selection::SelectableAtFast(int32 index) const 105*128277c9SStephan Aßmus { 106*128277c9SStephan Aßmus return (Selectable*)fSelected.ItemAtFast(index); 107*128277c9SStephan Aßmus } 108*128277c9SStephan Aßmus 109*128277c9SStephan Aßmus // CountSelected 110*128277c9SStephan Aßmus int32 111*128277c9SStephan Aßmus Selection::CountSelected() const 112*128277c9SStephan Aßmus { 113*128277c9SStephan Aßmus return fSelected.CountItems(); 114*128277c9SStephan Aßmus } 115*128277c9SStephan Aßmus 116*128277c9SStephan Aßmus // #pragma mark - 117*128277c9SStephan Aßmus 118*128277c9SStephan Aßmus // _DeselectAllExcept 119*128277c9SStephan Aßmus void 120*128277c9SStephan Aßmus Selection::_DeselectAllExcept(Selectable* except) 121*128277c9SStephan Aßmus { 122*128277c9SStephan Aßmus bool notify = false; 123*128277c9SStephan Aßmus bool containedExcept = false; 124*128277c9SStephan Aßmus 125*128277c9SStephan Aßmus int32 count = fSelected.CountItems(); 126*128277c9SStephan Aßmus for (int32 i = 0; i < count; i++) { 127*128277c9SStephan Aßmus Selectable* object = (Selectable*)fSelected.ItemAtFast(i); 128*128277c9SStephan Aßmus if (object != except) { 129*128277c9SStephan Aßmus object->SetSelected(false); 130*128277c9SStephan Aßmus notify = true; 131*128277c9SStephan Aßmus } else { 132*128277c9SStephan Aßmus containedExcept = true; 133*128277c9SStephan Aßmus } 134*128277c9SStephan Aßmus } 135*128277c9SStephan Aßmus 136*128277c9SStephan Aßmus fSelected.MakeEmpty(); 137*128277c9SStephan Aßmus 138*128277c9SStephan Aßmus // if the "except" object was previously 139*128277c9SStephan Aßmus // in the selection, add it again after 140*128277c9SStephan Aßmus // making the selection list empty 141*128277c9SStephan Aßmus if (containedExcept) 142*128277c9SStephan Aßmus fSelected.AddItem(except); 143*128277c9SStephan Aßmus 144*128277c9SStephan Aßmus if (notify) 145*128277c9SStephan Aßmus Notify(); 146*128277c9SStephan Aßmus } 147*128277c9SStephan Aßmus 148