1 /* 2 * Copyright 2006, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 */ 8 9 #include "ChangePointCommand.h" 10 11 #include <new> 12 #include <stdio.h> 13 14 #include "VectorPath.h" 15 16 using std::nothrow; 17 18 // constructor 19 ChangePointCommand::ChangePointCommand(VectorPath* path, 20 int32 index, 21 const int32* selected, 22 int32 count) 23 : PathCommand(path), 24 fIndex(index), 25 fOldSelection(NULL), 26 fOldSelectionCount(count) 27 { 28 if (fPath && !fPath->GetPointsAt(fIndex, fPoint, fPointIn, fPointOut, &fConnected)) 29 fPath = NULL; 30 if (fOldSelectionCount > 0 && selected) { 31 fOldSelection = new (nothrow) int32[fOldSelectionCount]; 32 memcpy(fOldSelection, selected, fOldSelectionCount * sizeof(int32)); 33 } 34 } 35 36 // destructor 37 ChangePointCommand::~ChangePointCommand() 38 { 39 delete[] fOldSelection; 40 } 41 42 // InitCheck 43 status_t 44 ChangePointCommand::InitCheck() 45 { 46 // TODO: figure out if selection changed!!! 47 // (this command is also used to undo changes to the selection) 48 // (but tracking the selection does not yet work in Icon-O-Matic) 49 50 status_t ret = PathCommand::InitCheck(); 51 if (ret < B_OK) 52 return ret; 53 54 BPoint point; 55 BPoint pointIn; 56 BPoint pointOut; 57 bool connected; 58 if (!fPath->GetPointsAt(fIndex, point, pointIn, pointOut, &connected)) 59 return B_ERROR; 60 61 if (point != fPoint || pointIn != fPointIn 62 || pointOut != fPointOut || connected != fConnected) 63 return B_OK; 64 65 return B_ERROR; 66 } 67 68 // Perform 69 status_t 70 ChangePointCommand::Perform() 71 { 72 // path point is already changed 73 return B_OK; 74 } 75 76 // Undo 77 status_t 78 ChangePointCommand::Undo() 79 { 80 status_t status = InitCheck(); 81 if (status < B_OK) 82 return status; 83 84 // set the point to the remembered state and 85 // save the previous state of the point 86 BPoint point; 87 BPoint pointIn; 88 BPoint pointOut; 89 bool connected; 90 if (fPath->GetPointsAt(fIndex, point, pointIn, pointOut, &connected) 91 && fPath->SetPoint(fIndex, fPoint, fPointIn, fPointOut, fConnected)) { 92 // toggle the remembered settings 93 fPoint = point; 94 fPointIn = pointIn; 95 fPointOut = pointOut; 96 fConnected = connected; 97 // restore old selection 98 _Select(fOldSelection, fOldSelectionCount); 99 } else { 100 status = B_ERROR; 101 } 102 103 return status; 104 } 105 106 // Redo 107 status_t 108 ChangePointCommand::Redo() 109 { 110 status_t status = Undo(); 111 if (status >= B_OK) 112 _Select(&fIndex, 1); 113 return status; 114 } 115 116 // GetName 117 void 118 ChangePointCommand::GetName(BString& name) 119 { 120 // name << _GetString(MODIFY_CONTROL_POINT, "Modify Control Point"); 121 name << "Modify Control Point"; 122 } 123