| 1208 | } |
| 1209 | |
| 1210 | static void stb_text_undo(STB_TEXTEDIT_STRING* str, STB_TexteditState* state) |
| 1211 | { |
| 1212 | StbUndoState* s = &state->undostate; |
| 1213 | StbUndoRecord u, * r; |
| 1214 | if (s->undo_point == 0) |
| 1215 | return; |
| 1216 | |
| 1217 | // we need to do two things: apply the undo record, and create a redo record |
| 1218 | u = s->undo_rec[s->undo_point - 1]; |
| 1219 | r = &s->undo_rec[s->redo_point - 1]; |
| 1220 | r->char_storage = -1; |
| 1221 | |
| 1222 | r->insert_length = u.delete_length; |
| 1223 | r->delete_length = u.insert_length; |
| 1224 | r->where = u.where; |
| 1225 | |
| 1226 | if (u.delete_length) { |
| 1227 | // if the undo record says to delete characters, then the redo record will |
| 1228 | // need to re-insert the characters that get deleted, so we need to store |
| 1229 | // them. |
| 1230 | |
| 1231 | // there are three cases: |
| 1232 | // there's enough room to store the characters |
| 1233 | // characters stored for *redoing* don't leave room for redo |
| 1234 | // characters stored for *undoing* don't leave room for redo |
| 1235 | // if the last is true, we have to bail |
| 1236 | |
| 1237 | if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) { |
| 1238 | // the undo records take up too much character space; there's no space to store the redo characters |
| 1239 | r->insert_length = 0; |
| 1240 | } |
| 1241 | else { |
| 1242 | int i; |
| 1243 | |
| 1244 | // there's definitely room to store the characters eventually |
| 1245 | while (s->undo_char_point + u.delete_length > s->redo_char_point) { |
| 1246 | // should never happen: |
| 1247 | if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) |
| 1248 | return; |
| 1249 | // there's currently not enough room, so discard a redo record |
| 1250 | stb_textedit_discard_redo(s); |
| 1251 | } |
| 1252 | r = &s->undo_rec[s->redo_point - 1]; |
| 1253 | |
| 1254 | r->char_storage = s->redo_char_point - u.delete_length; |
| 1255 | s->redo_char_point = s->redo_char_point - u.delete_length; |
| 1256 | |
| 1257 | // now save the characters |
| 1258 | for (i = 0; i < u.delete_length; ++i) |
| 1259 | s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i); |
| 1260 | } |
| 1261 | |
| 1262 | // now we can carry out the deletion |
| 1263 | STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length); |
| 1264 | } |
| 1265 | |
| 1266 | // check type of recorded action: |
| 1267 | if (u.insert_length) { |
no test coverage detected