Debian Codegraph @

src/pt2_textedit.c

raw symbols SCIP
1 // for finding memory leaks in debug mode with Visual Studio
2 #if defined _DEBUG && defined _MSC_VER
3 #include <crtdbg.h>
4 #endif
5
6 #include <stdint.h>
7 #include <stdbool.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include "pt2_textout.h"
11 #include "pt2_visuals.h"
12 #include "pt2_helpers.h"
13 #include "pt2_replayer.h"
14 #include "pt2_bmp.h"
15 #include "pt2_mouse.h"
16 #include "pt2_edit.h"
17 #include "pt2_config.h"
18 #include "pt2_diskop.h"
19 #include "pt2_sampler.h"
20 #include "pt2_audio.h"
21 #include "pt2_chordmaker.h"
22 #include "pt2_textedit.h"
23
24 // PATH_MAX is the absolute longest editable string possible
25 static char oldText[PATH_MAX+2];
26 static uint32_t oldTextLength;
27
28 void renderTextEditCursor(void)
29 {
30 if (!ui.editTextFlag)
31 return;
32
33 const int32_t x = textEdit.cursorStartX + (textEdit.cursorBlock * FONT_CHAR_W);
34 const int32_t y = textEdit.cursorStartY;
35 const int32_t w = 7;
36 const int32_t h = 2;
37
38 if (x < 0 || x+w >= SCREEN_W || y < 0 || y+h >= SCREEN_H)
39 return;
40
41 fillRect(x, y, w, h, video.palette[PAL_TEXTMARK]);
42 }
43
44 void removeTextEditCursor(void)
45 {
46 if (!ui.editTextFlag)
47 return;
48
49 const int32_t x = textEdit.cursorStartX + (textEdit.cursorBlock * FONT_CHAR_W);
50 const int32_t y = textEdit.cursorStartY;
51 const int32_t w = 7;
52 const int32_t h = 2;
53
54 if (x < 0 || x+w >= SCREEN_W || y < 0 || y+h >= SCREEN_H)
55 return;
56
57 if (textEdit.object == PTB_PE_PATT || textEdit.object == PTB_PE_PATTNAME)
58 {
59 // position editor text editing needs different handling
60
61 // rewrite border pixels (below second row of text edit cursor)
62 hLine(x, y, w, video.palette[PAL_GENBKG2]);
63
64 ui.updatePosEd = true; // this also erases the first row of the text edit cursor
65 }
66 else
67 {
68 // all others
69 fillRect(x, y, w, h, video.palette[PAL_GENBKG]);
70 }
71 }
72
73 static void moveTextCursorLeft(void)
74 {
75 if (textEdit.cursorBlock > 0)
76 {
77 removeTextEditCursor();
78 textEdit.cursorBlock--;
79 renderTextEditCursor();
80 }
81 else if (textEdit.scrollable)
82 {
83 if (textEdit.scrollOffset > 0)
84 {
85 textEdit.scrollOffset--;
86
87 if (textEdit.object == PTB_DO_DATAPATH)
88 ui.updateDiskOpPathText = true;
89 else if (textEdit.object == PTB_PE_PATTNAME)
90 ui.updatePosEd = true;
91 }
92 }
93 }
94
95 static void moveTextCursorRight(void)
96 {
97 if (textEdit.type == TEXT_EDIT_STRING)
98 {
99 if (textEdit.cursorBlock < textEdit.numBlocks-1)
100 {
101 removeTextEditCursor();
102 textEdit.cursorBlock++;
103 renderTextEditCursor();
104 }
105 else if (textEdit.scrollable)
106 {
107 if (textEdit.textPtr <= textEdit.textEndPtr)
108 {
109 textEdit.scrollOffset++;
110
111 if (textEdit.object == PTB_DO_DATAPATH)
112 ui.updateDiskOpPathText = true;
113 else if (textEdit.object == PTB_PE_PATTNAME)
114 ui.updatePosEd = true;
115 }
116 }
117 }
118 else
119 {
120 // we end up here when entering a number/hex digit
121
122 if (textEdit.cursorBlock < textEdit.numDigits)
123 removeTextEditCursor();
124
125 textEdit.cursorBlock++;
126
127 if (textEdit.cursorBlock < textEdit.numDigits)
128 renderTextEditCursor();
129
130 // don't clamp now, textEdit.cursorBlock is tested elsewhere
131 }
132 }
133
134 void editTextPrevChar(void)
135 {
136 if (textEdit.type != TEXT_EDIT_STRING)
137 {
138 if (textEdit.cursorBlock > 0)
139 {
140 removeTextEditCursor();
141 textEdit.cursorBlock--;
142 renderTextEditCursor();
143 }
144
145 return;
146 }
147
148 if (editor.mixFlag && textEdit.cursorBlock <= 4) // kludge...
149 return;
150
151 if (textEdit.textPtr > textEdit.textStartPtr)
152 {
153 removeTextEditCursor();
154
155 textEdit.textPtr--;
156 moveTextCursorLeft();
157
158 if (editor.mixFlag) // special case for "Mix" input field in Edit. Op.
159 {
160 if (textEdit.cursorBlock == 12)
161 {
162 textEdit.textPtr--; moveTextCursorLeft();
163 textEdit.textPtr--; moveTextCursorLeft();
164 textEdit.textPtr--; moveTextCursorLeft();
165 textEdit.textPtr--; moveTextCursorLeft();
166 }
167 else if (textEdit.cursorBlock == 6)
168 {
169 textEdit.textPtr--;
170 moveTextCursorLeft();
171 }
172 }
173
174 renderTextEditCursor();
175 }
176
177 textEdit.endReached = false;
178 }
179
180 void editTextNextChar(void)
181 {
182 if (textEdit.type != TEXT_EDIT_STRING)
183 {
184 if (textEdit.cursorBlock < textEdit.numDigits-1)
185 {
186 removeTextEditCursor();
187 textEdit.cursorBlock++;
188 renderTextEditCursor();
189 }
190
191 return;
192 }
193
194 if (editor.mixFlag && textEdit.cursorBlock >= 14) // kludge
195 return;
196
197 if (textEdit.textPtr < textEdit.textEndPtr)
198 {
199 if (*textEdit.textPtr != '\0')
200 {
201 removeTextEditCursor();
202
203 textEdit.textPtr++;
204 moveTextCursorRight();
205
206 if (editor.mixFlag) // special case for "Mix" input field in Edit. Op.
207 {
208 if (textEdit.cursorBlock == 9)
209 {
210 textEdit.textPtr++; moveTextCursorRight();
211 textEdit.textPtr++; moveTextCursorRight();
212 textEdit.textPtr++; moveTextCursorRight();
213 textEdit.textPtr++; moveTextCursorRight();
214 }
215 else if (textEdit.cursorBlock == 6)
216 {
217 textEdit.textPtr++;
218 moveTextCursorRight();
219 }
220 }
221
222 renderTextEditCursor();
223 }
224 else
225 {
226 textEdit.endReached = true;
227 }
228 }
229 else
230 {
231 textEdit.endReached = true;
232 }
233 }
234
235 void handleTextEditing(uint8_t mouseButton) // handle mouse while editing text/numbers
236 {
237 if (!ui.editTextFlag)
238 return;
239
240 if (textEdit.type != TEXT_EDIT_STRING)
241 {
242 if (mouseButton == SDL_BUTTON_RIGHT)
243 leaveTextEditMode(EDIT_TEXT_NO_UPDATE);
244 }
245 else if (mouseButton == SDL_BUTTON_LEFT && !editor.mixFlag) // string type
246 {
247 // jump to edit character under the mouse pointer
248
249 int32_t tmp32 = mouse.y - textEdit.cursorStartY;
250 if (tmp32 <= 2 && tmp32 >= -9)
251 {
252 ASSERT(textEdit.textStartPtr != NULL);
253 const int32_t textLength = (int32_t)strlen(textEdit.textStartPtr);
254 int32_t cursorBlock = (mouse.x - textEdit.cursorStartX) / FONT_CHAR_W;
255 const int32_t textPos = CLAMP(textEdit.scrollOffset + cursorBlock, 0, textLength);
256
257 cursorBlock = CLAMP(cursorBlock, 0, textEdit.numBlocks-1);
258 if (cursorBlock > textLength)
259 cursorBlock = textLength;
260
261 removeTextEditCursor();
262 textEdit.cursorBlock = (uint16_t)cursorBlock;
263 textEdit.textPtr = textEdit.textStartPtr + textPos;
264 renderTextEditCursor();
265 }
266 else
267 {
268 // if we clicked outside of the vertical edit area, stop edit mode
269
270 if (textEdit.object != PTB_PE_PATTNAME) // special case...
271 leaveTextEditMode(EDIT_TEXT_UPDATE);
272 }
273 }
274 else if (mouseButton == SDL_BUTTON_RIGHT)
275 {
276 if (editor.mixFlag)
277 {
278 leaveTextEditMode(EDIT_TEXT_UPDATE);
279 editor.mixFlag = false;
280 ui.updateMixText = true;
281 }
282 else
283 {
284 // delete text
285 const uint32_t charsToZero = (uint32_t)(textEdit.textEndPtr - textEdit.textStartPtr);
286 memset(textEdit.textStartPtr, '\0', charsToZero);
287
288 if (textEdit.object == PTB_DO_DATAPATH)
289 {
290 /* Don't exit text edit mode if the Disk Op. path was about to
291 ** be deleted with the right mouse button.
292 ** Set text cursor to beginning instead.
293 */
294 removeTextEditCursor();
295 textEdit.cursorBlock = 0;
296 textEdit.textPtr = textEdit.textStartPtr;
297 textEdit.scrollOffset = 0;
298 renderTextEditCursor();
299
300 ui.updateDiskOpPathText = true;
301 }
302 else
303 {
304 if (textEdit.object == PTB_SONGNAME)
305 ui.updateSongName = true;
306 else if (textEdit.object == PTB_SAMPLENAME)
307 ui.updateCurrSampleName = true;
308
309 leaveTextEditMode(EDIT_TEXT_UPDATE);
310 }
311 }
312 }
313 }
314
315 void enterTextEditMode(int16_t editObject)
316 {
317 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
318
319 textEdit.textPtr = textEdit.textStartPtr;
320 textEdit.cursorBlock = 0;
321 textEdit.scrollOffset = 0;
322 textEdit.type = TEXT_EDIT_STRING;
323 textEdit.object = editObject;
324 textEdit.endReached = false;
325 ui.editTextFlag = true;
326
327 // make backup of old text (for handling 'song modified' flag later)
328 oldTextLength = (uint32_t)strlen(textEdit.textPtr);
329 memset(oldText, '\0', sizeof (oldText));
330 memcpy(oldText, textEdit.textPtr, oldTextLength);
331
332 if (editor.mixFlag) // skip to editable text section
333 {
334 editTextNextChar();
335 editTextNextChar();
336 editTextNextChar();
337 editTextNextChar();
338 }
339
340 renderTextEditCursor();
341 SDL_StartTextInput();
342 }
343
344 void enterNumberEditMode(uint8_t type, int16_t editObject)
345 {
346 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
347
348 textEdit.cursorBlock = 0;
349 textEdit.type = type;
350 textEdit.object = editObject;
351 textEdit.endReached = false;
352 ui.editTextFlag = true;
353
354 renderTextEditCursor();
355 SDL_StartTextInput();
356 }
357
358 static void redrawTextEditObject(void)
359 {
360 switch (textEdit.object)
361 {
362 default: break;
363 case PTB_SONGNAME: ui.updateSongName = true; break;
364 case PTB_SAMPLENAME: ui.updateCurrSampleName = true; break;
365 case PTB_PE_PATT: ui.updatePosEd = true; break;
366 case PTB_PE_PATTNAME: ui.updatePosEd = true; break;
367 case PTB_EO_QUANTIZE: ui.updateQuantizeText = true; break;
368 case PTB_EO_METRO_1: ui.updateMetro1Text = true; break;
369 case PTB_EO_METRO_2: ui.updateMetro2Text = true; break;
370 case PTB_EO_FROM_NUM: ui.updateFromText = true; break;
371 case PTB_EO_TO_NUM: ui.updateToText = true; break;
372 case PTB_EO_MIX: ui.updateMixText = true; break;
373 case PTB_EO_POS_NUM: ui.updatePosText = true; break;
374 case PTB_EO_MOD_NUM: ui.updateModText = true; break;
375 case PTB_EO_VOL_NUM: ui.updateVolText = true; break;
376 case PTB_DO_DATAPATH: ui.updateDiskOpPathText = true; break;
377 case PTB_POSS: updateNewPos(); break;
378 case PTB_PATTERNS: ui.updateSongPattern = true; break;
379 case PTB_LENGTHS: ui.updateSongLength = true; break;
380 case PTB_SAMPLES: ui.updateCurrSampleNum = true; break;
381 case PTB_SVOLUMES: ui.updateCurrSampleVolume = true; break;
382 case PTB_SLENGTHS: ui.updateCurrSampleLength = true; break;
383 case PTB_SREPEATS: ui.updateCurrSampleRepeat = true; break;
384 case PTB_SREPLENS: ui.updateCurrSampleReplen = true; break;
385 case PTB_PATTDATA: ui.updateCurrPattText = true; break;
386 case PTB_SA_VOL_FROM_NUM: ui.updateVolFromText = true; break;
387 case PTB_SA_VOL_TO_NUM: ui.updateVolToText = true; break;
388 case PTB_SA_FIL_LP_CUTOFF: ui.updateLPText = true; break;
389 case PTB_SA_FIL_HP_CUTOFF: ui.updateHPText = true; break;
390 }
391 }
392
393 void leaveTextEditMode(bool updateValue)
394 {
395 int8_t tmp8;
396 int16_t tmp16;
397 int32_t tmp32;
398
399 SDL_StopTextInput();
400
401 // if user updated the disk op path text
402 if (ui.diskOpScreenShown && textEdit.object == PTB_DO_DATAPATH)
403 {
404 UNICHAR *pathU = (UNICHAR *)calloc(PATH_MAX + 2, sizeof (UNICHAR));
405 if (pathU != NULL)
406 {
407 #ifdef _WIN32
408 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, editor.currPath, -1, pathU, PATH_MAX);
409 #else
410 strcpy(pathU, editor.currPath);
411 #endif
412 diskOpSetPath(pathU, DISKOP_CACHE);
413 free(pathU);
414 }
415 }
416
417 if (textEdit.type != TEXT_EDIT_STRING)
418 {
419 if (textEdit.cursorBlock != textEdit.numDigits)
420 removeTextEditCursor();
421
422 redrawTextEditObject();
423 }
424 else
425 {
426 removeTextEditCursor();
427
428 // yet another kludge...
429 if (textEdit.object == PTB_PE_PATT || textEdit.object == PTB_PE_PATTNAME)
430 ui.updatePosEd = true;
431 }
432
433 ui.editTextFlag = false;
434
435 if (textEdit.type == TEXT_EDIT_STRING)
436 {
437 pointerSetPreviousMode();
438
439 // handle song modified state (only for some text edit objects)
440 if (textEdit.object != PTB_EO_MIX && textEdit.object != PTB_PE_PATTNAME && textEdit.object != PTB_DO_DATAPATH)
441 {
442 if (strcmp(textEdit.textStartPtr, oldText) != 0)
443 updateWindowTitle(MOD_IS_MODIFIED);
444 }
445 }
446 else
447 {
448 // set back GUI text pointers and update values (if requested)
449
450 moduleSample_t *s = &song->samples[editor.currSample];
451 switch (textEdit.object)
452 {
453 case PTB_SA_FIL_LP_CUTOFF:
454 {
455 editor.lpCutOffDisp = &editor.lpCutOff;
456
457 if (updateValue)
458 {
459 editor.lpCutOff = textEdit.tmpDisp16;
460 if (editor.lpCutOff > (uint16_t)(FILTERS_BASE_FREQ/2))
461 editor.lpCutOff = (uint16_t)(FILTERS_BASE_FREQ/2);
462
463 ui.updateLPText = true;
464 }
465 }
466 break;
467
468 case PTB_SA_FIL_HP_CUTOFF:
469 {
470 editor.hpCutOffDisp = &editor.hpCutOff;
471
472 if (updateValue)
473 {
474 editor.hpCutOff = textEdit.tmpDisp16;
475 if (editor.hpCutOff > (uint16_t)(FILTERS_BASE_FREQ/2))
476 editor.hpCutOff = (uint16_t)(FILTERS_BASE_FREQ/2);
477
478 ui.updateHPText = true;
479 }
480 }
481 break;
482
483 case PTB_SA_VOL_FROM_NUM:
484 {
485 editor.vol1Disp = &editor.vol1;
486
487 if (updateValue)
488 {
489 editor.vol1 = textEdit.tmpDisp16;
490 if (editor.vol1 > 200)
491 editor.vol1 = 200;
492
493 ui.updateVolFromText = true;
494 showVolFromSlider();
495 }
496 }
497 break;
498
499 case PTB_SA_VOL_TO_NUM:
500 {
501 editor.vol2Disp = &editor.vol2;
502
503 if (updateValue)
504 {
505 editor.vol2 = textEdit.tmpDisp16;
506 if (editor.vol2 > 200)
507 editor.vol2 = 200;
508
509 ui.updateVolToText = true;
510 showVolToSlider();
511 }
512 }
513 break;
514
515 case PTB_EO_VOL_NUM:
516 {
517 editor.sampleVolDisp = &editor.sampleVol;
518
519 if (updateValue)
520 {
521 editor.sampleVol = textEdit.tmpDisp16;
522 ui.updateVolText = true;
523 }
524 }
525 break;
526
527 case PTB_EO_POS_NUM:
528 {
529 editor.samplePosDisp = &editor.samplePos;
530
531 if (updateValue)
532 {
533 editor.samplePos = textEdit.tmpDisp32;
534 if (editor.samplePos > config.maxSampleLength)
535 editor.samplePos = config.maxSampleLength;
536
537 if (editor.samplePos > song->samples[editor.currSample].length)
538 editor.samplePos = song->samples[editor.currSample].length;
539
540 ui.updatePosText = true;
541 }
542 }
543 break;
544
545 case PTB_EO_QUANTIZE:
546 {
547 editor.quantizeValueDisp = &config.quantizeValue;
548
549 if (updateValue)
550 {
551 if (textEdit.tmpDisp16 > 63)
552 textEdit.tmpDisp16 = 63;
553
554 config.quantizeValue = textEdit.tmpDisp16;
555 ui.updateQuantizeText = true;
556 }
557 }
558 break;
559
560 case PTB_EO_METRO_1: // metronome speed
561 {
562 editor.metroSpeedDisp = &editor.metroSpeed;
563
564 if (updateValue)
565 {
566 if (textEdit.tmpDisp16 > 64)
567 textEdit.tmpDisp16 = 64;
568
569 editor.metroSpeed = textEdit.tmpDisp16;
570 ui.updateMetro1Text = true;
571 }
572 }
573 break;
574
575 case PTB_EO_METRO_2: // metronome channel
576 {
577 editor.metroChannelDisp = &editor.metroChannel;
578
579 if (updateValue)
580 {
581 if (textEdit.tmpDisp16 > 4)
582 textEdit.tmpDisp16 = 4;
583
584 editor.metroChannel = textEdit.tmpDisp16;
585 ui.updateMetro2Text = true;
586 }
587 }
588 break;
589
590 case PTB_EO_FROM_NUM:
591 {
592 editor.sampleFromDisp = &editor.sampleFrom;
593
594 if (updateValue)
595 {
596 editor.sampleFrom = textEdit.tmpDisp8;
597
598 // signed check + normal check
599 if (editor.sampleFrom < 0x00 || editor.sampleFrom > 0x1F)
600 editor.sampleFrom = 0x1F;
601
602 ui.updateFromText = true;
603 }
604 }
605 break;
606
607 case PTB_EO_TO_NUM:
608 {
609 editor.sampleToDisp = &editor.sampleTo;
610
611 if (updateValue)
612 {
613 editor.sampleTo = textEdit.tmpDisp8;
614
615 // signed check + normal check
616 if (editor.sampleTo < 0x00 || editor.sampleTo > 0x1F)
617 editor.sampleTo = 0x1F;
618
619 ui.updateToText = true;
620 }
621 }
622 break;
623
624 case PTB_PE_PATT:
625 {
626 int16_t posEdPos = song->currPos;
627 if (posEdPos > song->header.songLength-1)
628 posEdPos = song->header.songLength-1;
629
630 editor.currPosEdPattDisp = &song->header.patternTable[posEdPos];
631
632 if (updateValue)
633 {
634 if (textEdit.tmpDisp16 > MAX_PATTERNS-1)
635 textEdit.tmpDisp16 = MAX_PATTERNS-1;
636
637 song->header.patternTable[posEdPos] = textEdit.tmpDisp16;
638
639 updateWindowTitle(MOD_IS_MODIFIED);
640
641 if (ui.posEdScreenShown)
642 ui.updatePosEd = true;
643
644 ui.updateSongPattern = true;
645 ui.updateSongSize = true;
646 }
647 }
648 break;
649
650 case PTB_POSS:
651 {
652 editor.currPosDisp = &song->currPos;
653
654 if (updateValue)
655 {
656 tmp16 = textEdit.tmpDisp16;
657 if (tmp16 > 126)
658 tmp16 = 126;
659
660 if (song->currPos != tmp16)
661 {
662 song->currPos = tmp16;
663 editor.currPatternDisp = &song->header.patternTable[song->currPos];
664
665 if (ui.posEdScreenShown)
666 ui.updatePosEd = true;
667
668 ui.updateSongPos = true;
669 ui.updatePatternData = true;
670 }
671 }
672 }
673 break;
674
675 case PTB_PATTERNS:
676 {
677 editor.currPatternDisp = &song->header.patternTable[song->currPos];
678
679 if (updateValue)
680 {
681 tmp16 = textEdit.tmpDisp16;
682 if (tmp16 > MAX_PATTERNS-1)
683 tmp16 = MAX_PATTERNS-1;
684
685 if (song->header.patternTable[song->currPos] != tmp16)
686 {
687 song->header.patternTable[song->currPos] = tmp16;
688
689 updateWindowTitle(MOD_IS_MODIFIED);
690
691 if (ui.posEdScreenShown)
692 ui.updatePosEd = true;
693
694 ui.updateSongPattern = true;
695 ui.updateSongSize = true;
696 }
697 }
698 }
699 break;
700
701 case PTB_LENGTHS:
702 {
703 editor.currLengthDisp = &song->header.songLength;
704
705 if (updateValue)
706 {
707 tmp16 = CLAMP(textEdit.tmpDisp16, 1, 127);
708
709 if (song->header.songLength != tmp16)
710 {
711 song->header.songLength = tmp16;
712
713 int16_t posEdPos = song->currPos;
714 if (posEdPos > song->header.songLength-1)
715 posEdPos = song->header.songLength-1;
716
717 editor.currPosEdPattDisp = &song->header.patternTable[posEdPos];
718
719 if (ui.posEdScreenShown)
720 ui.updatePosEd = true;
721
722 ui.updateSongLength = true;
723 ui.updateSongSize = true;
724 updateWindowTitle(MOD_IS_MODIFIED);
725 }
726 }
727 }
728 break;
729
730 case PTB_PATTDATA:
731 {
732 editor.currEditPatternDisp = &song->currPattern;
733
734 if (updateValue)
735 {
736 if (song->currPattern != textEdit.tmpDisp16)
737 {
738 setPattern(textEdit.tmpDisp16);
739 ui.updatePatternData = true;
740 ui.updateCurrPattText = true;
741 }
742 }
743 }
744 break;
745
746 case PTB_SAMPLES:
747 {
748 editor.currSampleDisp = &editor.currSample;
749
750 if (updateValue)
751 {
752 tmp8 = textEdit.tmpDisp8;
753 if (tmp8 < 0x00) // (signed) if >0x7F was entered, clamp to 0x1F
754 tmp8 = 0x1F;
755
756 tmp8 = CLAMP(tmp8, 0x01, 0x1F) - 1;
757
758 if (tmp8 != editor.currSample)
759 {
760 editor.currSample = tmp8;
761 updateCurrSample();
762 }
763 }
764 }
765 break;
766
767 case PTB_SVOLUMES:
768 {
769 s->volumeDisp = &s->volume;
770
771 if (updateValue)
772 {
773 tmp8 = textEdit.tmpDisp8;
774
775 // signed check + normal check
776 if (tmp8 < 0x00 || tmp8 > 0x40)
777 tmp8 = 0x40;
778
779 if (s->volume != tmp8)
780 {
781 s->volume = tmp8;
782 ui.updateCurrSampleVolume = true;
783 updateWindowTitle(MOD_IS_MODIFIED);
784 }
785 }
786 }
787 break;
788
789 case PTB_SLENGTHS:
790 {
791 s->lengthDisp = &s->length;
792
793 if (updateValue)
794 {
795 tmp32 = textEdit.tmpDisp32 & ~1; // even'ify
796 if (tmp32 > config.maxSampleLength)
797 tmp32 = config.maxSampleLength;
798
799 if (s->loopStart+s->loopLength > 2)
800 {
801 if (tmp32 < s->loopStart+s->loopLength)
802 tmp32 = s->loopStart+s->loopLength;
803 }
804
805 tmp32 &= ~1;
806
807 if (s->length != tmp32)
808 {
809 turnOffVoices();
810 s->length = tmp32;
811
812 ui.updateCurrSampleLength = true;
813 ui.updateSongSize = true;
814 updateSamplePos();
815
816 if (ui.samplerScreenShown)
817 redrawSample();
818
819 recalcChordLength();
820 updateWindowTitle(MOD_IS_MODIFIED);
821 }
822 }
823 }
824 break;
825
826 case PTB_SREPEATS:
827 {
828 s->loopStartDisp = &s->loopStart;
829
830 if (updateValue)
831 {
832 tmp32 = textEdit.tmpDisp32 & ~1; // even'ify
833 if (tmp32 > config.maxSampleLength)
834 tmp32 = config.maxSampleLength;
835
836 if (s->length >= s->loopLength)
837 {
838 if (tmp32+s->loopLength > s->length)
839 tmp32 = s->length - s->loopLength;
840 }
841 else
842 {
843 tmp32 = 0;
844 }
845
846 tmp32 &= ~1;
847
848 if (s->loopStart != tmp32)
849 {
850 turnOffVoices();
851 s->loopStart = tmp32;
852 updatePaulaLoops();
853
854 ui.updateCurrSampleRepeat = true;
855
856 if (ui.editOpScreenShown && ui.editOpScreen == 3)
857 ui.updateChordLengthText = true;
858
859 if (ui.samplerScreenShown)
860 setLoopSprites();
861
862 updateWindowTitle(MOD_IS_MODIFIED);
863 }
864 }
865 }
866 break;
867
868 case PTB_SREPLENS:
869 {
870 s->loopLengthDisp = &s->loopLength;
871
872 if (updateValue)
873 {
874 tmp32 = textEdit.tmpDisp32 & ~1; // even'ify
875 if (tmp32 > config.maxSampleLength)
876 tmp32 = config.maxSampleLength;
877
878 if (s->length >= s->loopStart)
879 {
880 if (s->loopStart+tmp32 > s->length)
881 tmp32 = s->length - s->loopStart;
882 }
883 else
884 {
885 tmp32 = 2;
886 }
887
888 tmp32 &= ~1;
889
890 if (tmp32 < 2)
891 tmp32 = 2;
892
893 if (s->loopLength != tmp32)
894 {
895 turnOffVoices();
896 s->loopLength = tmp32;
897 updatePaulaLoops();
898
899 ui.updateCurrSampleReplen = true;
900 if (ui.editOpScreenShown && ui.editOpScreen == 3)
901 ui.updateChordLengthText = true;
902
903 if (ui.samplerScreenShown)
904 setLoopSprites();
905
906 updateWindowTitle(MOD_IS_MODIFIED);
907 }
908 }
909 }
910 break;
911
912 default: break;
913 }
914
915 pointerSetPreviousMode();
916 }
917
918 textEdit.type = 0;
919 }
920
921 void handleTextEditInputChar(char textChar)
922 {
923 // we only want certain keys
924 if (textChar < ' ' || textChar > '~')
925 return;
926
927 // A..Z -> a..z
928 if (textChar >= 'A' && textChar <= 'Z')
929 textChar = (char)tolower(textChar);
930
931 if (textEdit.type == TEXT_EDIT_STRING)
932 {
933 if (textEdit.textPtr < textEdit.textEndPtr)
934 {
935 if (!editor.mixFlag)
936 {
937 char *readTmp = textEdit.textEndPtr;
938 while (readTmp > textEdit.textPtr)
939 {
940 int8_t readTmpPrev = *--readTmp;
941 *(readTmp + 1) = readTmpPrev;
942 }
943
944 *textEdit.textEndPtr = '\0';
945 *textEdit.textPtr++ = textChar;
946
947 moveTextCursorRight();
948 }
949 else if ((textChar >= '0' && textChar <= '9') || (textChar >= 'a' && textChar <= 'f'))
950 {
951 if (textEdit.cursorBlock == 14) // hack for sample mix text
952 {
953 *textEdit.textPtr = textChar;
954 }
955 else
956 {
957 *textEdit.textPtr++ = textChar;
958 moveTextCursorRight();
959
960 // hack for sample mix text
961 if (textEdit.cursorBlock == 9)
962 {
963 for (int32_t i = 0; i < 4; i++)
964 {
965 textEdit.textPtr++;
966 moveTextCursorRight();
967 }
968 }
969 else if (textEdit.cursorBlock == 6)
970 {
971 textEdit.textPtr++;
972 moveTextCursorRight();
973 }
974 }
975 }
976 }
977 }
978 else
979 {
980 if (textEdit.type == TEXT_EDIT_DECIMAL)
981 {
982 if (textChar >= '0' && textChar <= '9')
983 {
984 uint8_t digit1, digit2, digit3, digit4;
985 uint32_t number;
986
987 textChar -= '0';
988
989 if (textEdit.numDigits == 4)
990 {
991 number = *textEdit.numPtr16;
992 digit4 = number % 10; number /= 10;
993 digit3 = number % 10; number /= 10;
994 digit2 = number % 10; number /= 10;
995 digit1 = (uint8_t)number;
996
997 if (textEdit.cursorBlock == 0) *textEdit.numPtr16 = (textChar * 1000) + (digit2 * 100) + (digit3 * 10) + digit4;
998 else if (textEdit.cursorBlock == 1) *textEdit.numPtr16 = (digit1 * 1000) + (textChar * 100) + (digit3 * 10) + digit4;
999 else if (textEdit.cursorBlock == 2) *textEdit.numPtr16 = (digit1 * 1000) + (digit2 * 100) + (textChar * 10) + digit4;
1000 else if (textEdit.cursorBlock == 3) *textEdit.numPtr16 = (digit1 * 1000) + (digit2 * 100) + (digit3 * 10) + textChar;
1001 }
1002 else if (textEdit.numDigits == 3)
1003 {
1004 number = *textEdit.numPtr16;
1005 digit3 = number % 10; number /= 10;
1006 digit2 = number % 10; number /= 10;
1007 digit1 = (uint8_t)number;
1008
1009 if (textEdit.cursorBlock == 0) *textEdit.numPtr16 = (textChar * 100) + (digit2 * 10) + digit3;
1010 else if (textEdit.cursorBlock == 1) *textEdit.numPtr16 = (digit1 * 100) + (textChar * 10) + digit3;
1011 else if (textEdit.cursorBlock == 2) *textEdit.numPtr16 = (digit1 * 100) + (digit2 * 10) + textChar;
1012 }
1013 else if (textEdit.numDigits == 2)
1014 {
1015 number = *textEdit.numPtr16;
1016 digit2 = number % 10; number /= 10;
1017 digit1 = (uint8_t)number;
1018
1019 if (textEdit.cursorBlock == 0) *textEdit.numPtr16 = (textChar * 10) + digit2;
1020 else if (textEdit.cursorBlock == 1) *textEdit.numPtr16 = (digit1 * 10) + textChar;
1021 }
1022
1023 moveTextCursorRight();
1024 if (textEdit.cursorBlock >= textEdit.numDigits)
1025 leaveTextEditMode(EDIT_TEXT_UPDATE);
1026 }
1027 }
1028 else
1029 {
1030 if ((textChar >= '0' && textChar <= '9') || (textChar >= 'a' && textChar <= 'f'))
1031 {
1032 if (textChar <= '9')
1033 textChar -= '0';
1034 else if (textChar <= 'f')
1035 textChar -= 'a'-10;
1036
1037 if (textEdit.numBits == 17)
1038 {
1039 *textEdit.numPtr32 &= ~(0xF0000 >> (textEdit.cursorBlock << 2));
1040 *textEdit.numPtr32 |= textChar << (16 - (textEdit.cursorBlock << 2));
1041 }
1042 else if (textEdit.numBits == 16)
1043 {
1044 if (textEdit.force32BitNumPtr)
1045 {
1046 *textEdit.numPtr32 &= ~(0xF000 >> (textEdit.cursorBlock << 2));
1047 *textEdit.numPtr32 |= textChar << (12 - (textEdit.cursorBlock << 2));
1048 }
1049 else
1050 {
1051 *textEdit.numPtr16 &= ~(0xF000 >> (textEdit.cursorBlock << 2));
1052 *textEdit.numPtr16 |= textChar << (12 - (textEdit.cursorBlock << 2));
1053 }
1054 }
1055 else if (textEdit.numBits == 8)
1056 {
1057 *textEdit.numPtr8 &= ~(0xF0 >> (textEdit.cursorBlock << 2));
1058 *textEdit.numPtr8 |= textChar << (4 - (textEdit.cursorBlock << 2));
1059 }
1060
1061 moveTextCursorRight();
1062 if (textEdit.cursorBlock >= textEdit.numDigits)
1063 leaveTextEditMode(EDIT_TEXT_UPDATE);
1064 }
1065 }
1066 }
1067
1068 redrawTextEditObject();
1069
1070 if (!keyb.repeatKey)
1071 keyb.delayCounter = 0;
1072
1073 keyb.repeatKey = true;
1074 keyb.delayKey = true;
1075 }
1076
1077 bool handleTextEditMode(SDL_Scancode scancode)
1078 {
1079 switch (scancode)
1080 {
1081 case SDL_SCANCODE_ESCAPE:
1082 {
1083 editor.blockMarkFlag = false;
1084 if (ui.editTextFlag)
1085 {
1086 leaveTextEditMode(EDIT_TEXT_NO_UPDATE);
1087 return false;
1088 }
1089 }
1090 break;
1091
1092 case SDL_SCANCODE_HOME:
1093 {
1094 if (ui.editTextFlag && !editor.mixFlag && textEdit.type == TEXT_EDIT_STRING)
1095 {
1096 while (textEdit.textPtr > textEdit.textStartPtr)
1097 editTextPrevChar();
1098 }
1099 }
1100 break;
1101
1102 case SDL_SCANCODE_END:
1103 {
1104 if (ui.editTextFlag && !editor.mixFlag && textEdit.type == TEXT_EDIT_STRING)
1105 {
1106 while (!textEdit.endReached)
1107 editTextNextChar();
1108 }
1109 }
1110 break;
1111
1112 case SDL_SCANCODE_LEFT:
1113 {
1114 if (ui.editTextFlag)
1115 {
1116 editTextPrevChar();
1117 if (!keyb.repeatKey)
1118 keyb.delayCounter = 0;
1119
1120 keyb.repeatKey = true;
1121 keyb.delayKey = false;
1122 }
1123 else
1124 {
1125 keyb.delayKey = false;
1126 keyb.repeatKey = true;
1127 }
1128 }
1129 break;
1130
1131 case SDL_SCANCODE_RIGHT:
1132 {
1133 if (ui.editTextFlag)
1134 {
1135 editTextNextChar();
1136 if (!keyb.repeatKey)
1137 keyb.delayCounter = 0;
1138
1139 keyb.repeatKey = true;
1140 keyb.delayKey = false;
1141 }
1142 else
1143 {
1144 keyb.delayKey = false;
1145 keyb.repeatKey = true;
1146 }
1147 }
1148 break;
1149
1150 case SDL_SCANCODE_DELETE:
1151 {
1152 if (ui.editTextFlag)
1153 {
1154 if (editor.mixFlag || textEdit.type != TEXT_EDIT_STRING)
1155 break;
1156
1157 char *readTmp = textEdit.textPtr;
1158 while (readTmp < textEdit.textEndPtr)
1159 {
1160 int8_t readTmpNext = *(readTmp + 1);
1161 *readTmp++ = readTmpNext;
1162 }
1163
1164 // kludge to prevent cloning last character if the song/sample name has one character too much
1165 if (textEdit.object == PTB_SONGNAME || textEdit.object == PTB_SAMPLENAME || textEdit.object == PTB_PE_PATTNAME)
1166 *textEdit.textEndPtr = '\0';
1167
1168 if (!keyb.repeatKey)
1169 keyb.delayCounter = 0;
1170
1171 keyb.repeatKey = true;
1172 keyb.delayKey = true;
1173
1174 redrawTextEditObject();
1175 }
1176 }
1177 break;
1178
1179 case SDL_SCANCODE_BACKSPACE:
1180 {
1181 if (ui.editTextFlag)
1182 {
1183 if (editor.mixFlag || textEdit.type != TEXT_EDIT_STRING)
1184 break;
1185
1186 if (textEdit.textPtr > textEdit.textStartPtr)
1187 {
1188 textEdit.textPtr--;
1189
1190 char *readTmp = textEdit.textPtr;
1191 while (readTmp < textEdit.textEndPtr)
1192 {
1193 int8_t readTmpNext = *(readTmp + 1);
1194 *readTmp++ = readTmpNext;
1195 }
1196
1197 // kludge to prevent cloning last character if the song/sample name has one character too much
1198 if (textEdit.object == PTB_SONGNAME || textEdit.object == PTB_SAMPLENAME || textEdit.object == PTB_PE_PATTNAME)
1199 *textEdit.textEndPtr = '\0';
1200
1201 moveTextCursorLeft();
1202 redrawTextEditObject();
1203 }
1204
1205 if (!keyb.repeatKey)
1206 keyb.delayCounter = 0;
1207
1208 keyb.repeatKey = true;
1209 keyb.delayKey = false;
1210 }
1211 else
1212 {
1213 if (ui.diskOpScreenShown)
1214 {
1215 #ifdef _WIN32
1216 diskOpSetPath(L"..", DISKOP_CACHE);
1217 #else
1218 diskOpSetPath("..", DISKOP_CACHE);
1219 #endif
1220 }
1221 else if (keyb.shiftPressed || keyb.leftAltPressed || keyb.leftCtrlPressed)
1222 {
1223 saveUndo();
1224 if (keyb.leftAltPressed && !keyb.leftCtrlPressed)
1225 {
1226 if (song->currRow > 0)
1227 {
1228 for (int32_t i = 0; i < PAULA_VOICES; i++)
1229 {
1230 for (int32_t j = (song->currRow - 1); j < MOD_ROWS; j++)
1231 {
1232 note_t *noteSrc = &song->patterns[song->currPattern][((j + 1) * PAULA_VOICES) + i];
1233 song->patterns[song->currPattern][(j * PAULA_VOICES) + i] = *noteSrc;
1234 }
1235
1236 // clear newly made row on very bottom
1237 note_t *noteDst = &song->patterns[song->currPattern][(63 * PAULA_VOICES) + i];
1238 noteDst->period = 0;
1239 noteDst->sample = 0;
1240 noteDst->command = 0;
1241 noteDst->param = 0;
1242 }
1243
1244 song->currRow--;
1245 ui.updatePatternData = true;
1246 }
1247 }
1248 else
1249 {
1250 if (song->currRow > 0)
1251 {
1252 for (int32_t i = song->currRow-1; i < MOD_ROWS-1; i++)
1253 {
1254 note_t *noteSrc = &song->patterns[song->currPattern][((i + 1) * PAULA_VOICES) + cursor.channel];
1255 note_t *noteDst = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel];
1256
1257 if (keyb.leftCtrlPressed)
1258 {
1259 noteDst->command = noteSrc->command;
1260 noteDst->param = noteSrc->param;
1261 }
1262 else
1263 {
1264 *noteDst = *noteSrc;
1265 }
1266 }
1267
1268 // clear newly made row on very bottom
1269 note_t *noteDst = &song->patterns[song->currPattern][(63 * PAULA_VOICES) + cursor.channel];
1270 noteDst->period = 0;
1271 noteDst->sample = 0;
1272 noteDst->command = 0;
1273 noteDst->param = 0;
1274
1275 song->currRow--;
1276 ui.updatePatternData = true;
1277 }
1278 }
1279 }
1280 else
1281 {
1282 const bool audioWasntLocked = !audio.locked;
1283 if (audioWasntLocked)
1284 lockAudio();
1285
1286 editor.stepPlayEnabled = true;
1287 editor.stepPlayBackwards = true;
1288
1289 editor.stepPlayLastMode = editor.currMode;
1290
1291 if (config.keepEditModeAfterStepPlay && editor.stepPlayLastMode == MODE_EDIT)
1292 doStopIt(false);
1293 else
1294 doStopIt(true);
1295
1296 playPattern((song->currRow - 1) & 63);
1297
1298 if (config.keepEditModeAfterStepPlay && editor.stepPlayLastMode == MODE_EDIT)
1299 {
1300 pointerSetMode(POINTER_MODE_EDIT, DO_CARRY);
1301 editor.playMode = PLAY_MODE_NORMAL;
1302 editor.currMode = MODE_EDIT;
1303 }
1304
1305 if (audioWasntLocked)
1306 unlockAudio();
1307 }
1308 }
1309 }
1310 break;
1311
1312 default: break;
1313 }
1314
1315 if (ui.editTextFlag)
1316 {
1317 if (scancode == SDL_SCANCODE_RETURN || scancode == SDL_SCANCODE_KP_ENTER)
1318 {
1319 // dirty hack
1320 if (textEdit.object == PTB_SAMPLES)
1321 textEdit.tmpDisp8++;
1322
1323 leaveTextEditMode(EDIT_TEXT_UPDATE);
1324
1325 if (editor.mixFlag)
1326 {
1327 editor.mixFlag = false;
1328 ui.updateMixText = true;
1329 doMix();
1330 }
1331 }
1332
1333 return false; // don't continue further key handling
1334 }
1335
1336 return true; // continue further key handling (we're not editing text)
1337 }