1128277c9SStephan Aßmus /* 2128277c9SStephan Aßmus * Copyright 2006, Haiku. 3128277c9SStephan Aßmus * Distributed under the terms of the MIT License. 4128277c9SStephan Aßmus * 5128277c9SStephan Aßmus * Authors: 6128277c9SStephan Aßmus * Stephan Aßmus <superstippi@gmx.de> 7128277c9SStephan Aßmus */ 8128277c9SStephan Aßmus 9128277c9SStephan Aßmus #include "Selection.h" 10128277c9SStephan Aßmus 11128277c9SStephan Aßmus #include <stdio.h> 12128277c9SStephan Aßmus 13128277c9SStephan Aßmus #include <debugger.h> 14128277c9SStephan Aßmus 15128277c9SStephan Aßmus #include "Selectable.h" 16128277c9SStephan Aßmus 17128277c9SStephan Aßmus #define DEBUG 1 18128277c9SStephan Aßmus 19128277c9SStephan Aßmus // constructor 20128277c9SStephan Aßmus Selection::Selection() 21128277c9SStephan Aßmus : fSelected(20) 22128277c9SStephan Aßmus { 23128277c9SStephan Aßmus } 24128277c9SStephan Aßmus 25128277c9SStephan Aßmus // destructor 26128277c9SStephan Aßmus Selection::~Selection() 27128277c9SStephan Aßmus { 28128277c9SStephan Aßmus } 29128277c9SStephan Aßmus 30128277c9SStephan Aßmus // Select 31128277c9SStephan Aßmus bool 32128277c9SStephan Aßmus Selection::Select(Selectable* object, bool extend) 33128277c9SStephan Aßmus { 34128277c9SStephan Aßmus AutoNotificationSuspender _(this); 35128277c9SStephan Aßmus 36128277c9SStephan Aßmus if (!extend) 37128277c9SStephan Aßmus _DeselectAllExcept(object); 38128277c9SStephan Aßmus 39128277c9SStephan Aßmus bool success = false; 40128277c9SStephan Aßmus 41128277c9SStephan Aßmus if (!object->IsSelected()) { 42128277c9SStephan Aßmus 43128277c9SStephan Aßmus #if DEBUG 44128277c9SStephan Aßmus if (fSelected.HasItem((void*)object)) 45128277c9SStephan Aßmus debugger("Selection::Select() - " 46128277c9SStephan Aßmus "unselected object in list!"); 47128277c9SStephan Aßmus #endif 48128277c9SStephan Aßmus 49128277c9SStephan Aßmus if (fSelected.AddItem((void*)object)) { 50*2ce8d31dSStephan Aßmus object->_SetSelected(true); 51128277c9SStephan Aßmus success = true; 52128277c9SStephan Aßmus 53128277c9SStephan Aßmus Notify(); 54128277c9SStephan Aßmus } else { 55128277c9SStephan Aßmus fprintf(stderr, "Selection::Select() - out of memory\n"); 56128277c9SStephan Aßmus } 57128277c9SStephan Aßmus } else { 58128277c9SStephan Aßmus 59128277c9SStephan Aßmus #if DEBUG 60128277c9SStephan Aßmus if (!fSelected.HasItem((void*)object)) 61128277c9SStephan Aßmus debugger("Selection::Select() - " 62128277c9SStephan Aßmus "already selected object not in list!"); 63128277c9SStephan Aßmus #endif 64128277c9SStephan Aßmus 65128277c9SStephan Aßmus success = true; 66128277c9SStephan Aßmus // object already in list 67128277c9SStephan Aßmus } 68128277c9SStephan Aßmus 69128277c9SStephan Aßmus return success; 70128277c9SStephan Aßmus } 71128277c9SStephan Aßmus 72128277c9SStephan Aßmus // Deselect 73128277c9SStephan Aßmus void 74128277c9SStephan Aßmus Selection::Deselect(Selectable* object) 75128277c9SStephan Aßmus { 76128277c9SStephan Aßmus if (object->IsSelected()) { 77128277c9SStephan Aßmus if (!fSelected.RemoveItem((void*)object)) 78128277c9SStephan Aßmus debugger("Selection::Deselect() - " 79128277c9SStephan Aßmus "selected object not within list!"); 80*2ce8d31dSStephan Aßmus object->_SetSelected(false); 81128277c9SStephan Aßmus 82128277c9SStephan Aßmus Notify(); 83128277c9SStephan Aßmus } 84128277c9SStephan Aßmus } 85128277c9SStephan Aßmus 86128277c9SStephan Aßmus // DeselectAll 87128277c9SStephan Aßmus void 88128277c9SStephan Aßmus Selection::DeselectAll() 89128277c9SStephan Aßmus { 90128277c9SStephan Aßmus _DeselectAllExcept(NULL); 91128277c9SStephan Aßmus } 92128277c9SStephan Aßmus 93128277c9SStephan Aßmus // #pragma mark - 94128277c9SStephan Aßmus 95128277c9SStephan Aßmus // SelectableAt 96128277c9SStephan Aßmus Selectable* 97128277c9SStephan Aßmus Selection::SelectableAt(int32 index) const 98128277c9SStephan Aßmus { 99128277c9SStephan Aßmus return (Selectable*)fSelected.ItemAt(index); 100128277c9SStephan Aßmus } 101128277c9SStephan Aßmus 102128277c9SStephan Aßmus // SelectableAtFast 103128277c9SStephan Aßmus Selectable* 104128277c9SStephan Aßmus Selection::SelectableAtFast(int32 index) const 105128277c9SStephan Aßmus { 106128277c9SStephan Aßmus return (Selectable*)fSelected.ItemAtFast(index); 107128277c9SStephan Aßmus } 108128277c9SStephan Aßmus 109128277c9SStephan Aßmus // CountSelected 110128277c9SStephan Aßmus int32 111128277c9SStephan Aßmus Selection::CountSelected() const 112128277c9SStephan Aßmus { 113128277c9SStephan Aßmus return fSelected.CountItems(); 114128277c9SStephan Aßmus } 115128277c9SStephan Aßmus 116128277c9SStephan Aßmus // #pragma mark - 117128277c9SStephan Aßmus 118128277c9SStephan Aßmus // _DeselectAllExcept 119128277c9SStephan Aßmus void 120128277c9SStephan Aßmus Selection::_DeselectAllExcept(Selectable* except) 121128277c9SStephan Aßmus { 122128277c9SStephan Aßmus bool notify = false; 123128277c9SStephan Aßmus bool containedExcept = false; 124128277c9SStephan Aßmus 125128277c9SStephan Aßmus int32 count = fSelected.CountItems(); 126128277c9SStephan Aßmus for (int32 i = 0; i < count; i++) { 127128277c9SStephan Aßmus Selectable* object = (Selectable*)fSelected.ItemAtFast(i); 128128277c9SStephan Aßmus if (object != except) { 129*2ce8d31dSStephan Aßmus object->_SetSelected(false); 130128277c9SStephan Aßmus notify = true; 131128277c9SStephan Aßmus } else { 132128277c9SStephan Aßmus containedExcept = true; 133128277c9SStephan Aßmus } 134128277c9SStephan Aßmus } 135128277c9SStephan Aßmus 136128277c9SStephan Aßmus fSelected.MakeEmpty(); 137128277c9SStephan Aßmus 138128277c9SStephan Aßmus // if the "except" object was previously 139128277c9SStephan Aßmus // in the selection, add it again after 140128277c9SStephan Aßmus // making the selection list empty 141128277c9SStephan Aßmus if (containedExcept) 142128277c9SStephan Aßmus fSelected.AddItem(except); 143128277c9SStephan Aßmus 144128277c9SStephan Aßmus if (notify) 145128277c9SStephan Aßmus Notify(); 146128277c9SStephan Aßmus } 147128277c9SStephan Aßmus 148