comparison libgui/src/m-editor/file-editor-tab.cc @ 21058:759fcdf3666d

Test GUI marker flags correctly, and omit unnecessary test finding next marker * file-editor-tab.cc (handle_margin_clicked, toggle_bookmark): & not && * file-editor-tab.cc (next_bookmark, previous_bookmark, next_breakpoint, previous_breakpoint): Skip current line wether or not it has a marker. * file-editor-tab.{cc,h} (message_cannot_breakpoint_changed_file): replace by unchanged_or_saved(), to save if necessary before breakpointing * file-editor-tab.cc (handle_margin_clicked, toggle_bookmark): call unchanged_of_saved().
author Lachlan Andrew <lachlanbis@gmail.com>
date Wed, 13 Jan 2016 17:28:42 +1100
parents 221847e5f488
children e8c3590da9ff
comparison
equal deleted inserted replaced
21057:b0afe1993268 21058:759fcdf3666d
18 You should have received a copy of the GNU General Public License 18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see 19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>. 20 <http://www.gnu.org/licenses/>.
21 21
22 */ 22 */
23
24 /**
25 @file A single GUI file tab.
26 This interfaces QSciScintilla with the rest of Octave.
27 */
23 28
24 #ifdef HAVE_CONFIG_H 29 #ifdef HAVE_CONFIG_H
25 #include <config.h> 30 #include <config.h>
26 #endif 31 #endif
27 32
71 #include "defaults.h" 76 #include "defaults.h"
72 #include <oct-map.h> 77 #include <oct-map.h>
73 78
74 bool file_editor_tab::_cancelled = false; 79 bool file_editor_tab::_cancelled = false;
75 80
81 /**
82 A file_editor_tab object consists of a text area and three left margins.
83 The first holds breakpoints, bookmarks, and the debug program counter.
84 The second holds line numbers.
85 The third holds "fold" marks, to hide sections of text.
86 */
76 // Make parent null for the file editor tab so that warning 87 // Make parent null for the file editor tab so that warning
77 // WindowModal messages don't affect grandparents. 88 // WindowModal messages don't affect grandparents.
78 file_editor_tab::file_editor_tab (const QString& directory_arg) 89 file_editor_tab::file_editor_tab (const QString& directory_arg)
79 { 90 {
80 _lexer_apis = 0; 91 _lexer_apis = 0;
319 update_lexer (); 330 update_lexer ();
320 331
321 // update the file editor with current editing directory 332 // update the file editor with current editing directory
322 emit editor_state_changed (_copy_available, _is_octave_file); 333 emit editor_state_changed (_copy_available, _is_octave_file);
323 334
324 // add the new file to the mru list 335 // add the new file to the most-recently-used list
325 emit mru_add_file (_file_name, _encoding); 336 emit mru_add_file (_file_name, _encoding);
326 } 337 }
327 338
328 // valid_file_name (file): checks whether "file" names a file 339 // valid_file_name (file): checks whether "file" names a file.
329 // by default, "file" is empty, then _file_name is checked 340 // By default, "file" is empty; then _file_name is checked
330 bool 341 bool
331 file_editor_tab::valid_file_name (const QString& file) 342 file_editor_tab::valid_file_name (const QString& file)
332 { 343 {
333 if (file.isEmpty ()) 344 if (file.isEmpty ())
334 { 345 {
339 } 350 }
340 351
341 return true; 352 return true;
342 } 353 }
343 354
344 void 355 // We cannot create a breakpoint when the file is modified
345 file_editor_tab::message_cannot_breakpoint_changed_file (void) 356 // because the line number the editor is providing might
346 { 357 // not match what Octave core is interpreting in the
347 // Cannot create a breakpoint when the file is modified 358 // file on disk. This function gives the user the option
348 // because the line number the editor is providing might 359 // to save before creating the breakpoint.
349 // not match what Octave core is interpretting in the 360 bool
350 // file on disk. 361 file_editor_tab::unchanged_or_saved (void)
351 QMessageBox* msgBox = new QMessageBox (QMessageBox::Critical, 362 {
352 tr ("Octave Editor"), 363 bool retval = true;
353 tr ("Cannot add breakpoint to modified file."), 364 if (_edit_area->isModified ())
354 QMessageBox::Ok, 0); 365 {
355 msgBox->setWindowModality (Qt::ApplicationModal); 366 int ans = QMessageBox::question (0, tr ("Octave Editor"),
356 msgBox->exec (); 367 tr ("Cannot add breakpoint to modified file.\n"
357 delete msgBox; 368 "Save and add breakpoint, or canel?"),
358 } 369 QMessageBox::Save | QMessageBox::Cancel, QMessageBox::Save);
359 370
371 if (ans == QMessageBox::Save)
372 save_file (_file_name, false);
373 else
374 retval = false;
375 }
376
377 return retval;
378 }
379
380 // Toggle a breakpoint at the editor_linenr or, if this was called by
381 // a click with CTRL pressed, toggle a bookmark at that point.
360 void 382 void
361 file_editor_tab::handle_margin_clicked (int margin, int editor_linenr, 383 file_editor_tab::handle_margin_clicked (int margin, int editor_linenr,
362 Qt::KeyboardModifiers state) 384 Qt::KeyboardModifiers state)
363 { 385 {
364 if (margin == 1) 386 if (margin == 1)
365 { 387 {
366 unsigned int markers_mask = _edit_area->markersAtLine (editor_linenr); 388 unsigned int markers_mask = _edit_area->markersAtLine (editor_linenr);
367 389
368 if (state & Qt::ControlModifier) 390 if (state & Qt::ControlModifier)
369 { 391 {
370 if (markers_mask && (1 << marker::bookmark)) 392 if (markers_mask & (1 << marker::bookmark))
371 _edit_area->markerDelete (editor_linenr, marker::bookmark); 393 _edit_area->markerDelete (editor_linenr, marker::bookmark);
372 else 394 else
373 _edit_area->markerAdd (editor_linenr, marker::bookmark); 395 _edit_area->markerAdd (editor_linenr, marker::bookmark);
374 } 396 }
375 else 397 else
376 { 398 {
377 if (markers_mask && (1 << marker::breakpoint)) 399 if (markers_mask & (1 << marker::breakpoint))
378 handle_request_remove_breakpoint (editor_linenr + 1); 400 handle_request_remove_breakpoint (editor_linenr + 1);
379 else 401 else
380 { 402 {
381 if (_edit_area->isModified ()) 403 if (unchanged_or_saved ())
382 message_cannot_breakpoint_changed_file ();
383 else
384 handle_request_add_breakpoint (editor_linenr + 1); 404 handle_request_add_breakpoint (editor_linenr + 1);
385 } 405 }
386 } 406 }
387 } 407 }
388 } 408 }
763 return; 783 return;
764 784
765 int line, cur; 785 int line, cur;
766 _edit_area->getCursorPosition (&line, &cur); 786 _edit_area->getCursorPosition (&line, &cur);
767 787
768 if (_edit_area->markersAtLine (line) && (1 << marker::bookmark)) 788 if (_edit_area->markersAtLine (line) & (1 << marker::bookmark))
769 _edit_area->markerDelete (line, marker::bookmark); 789 _edit_area->markerDelete (line, marker::bookmark);
770 else 790 else
771 _edit_area->markerAdd (line, marker::bookmark); 791 _edit_area->markerAdd (line, marker::bookmark);
772 } 792 }
773 793
794 // Move the text cursor to the closest bookmark
795 // after the current line.
774 void 796 void
775 file_editor_tab::next_bookmark (const QWidget *ID) 797 file_editor_tab::next_bookmark (const QWidget *ID)
776 { 798 {
777 if (ID != this) 799 if (ID != this)
778 return; 800 return;
779 801
780 int line, cur; 802 int line, cur;
781 _edit_area->getCursorPosition (&line, &cur); 803 _edit_area->getCursorPosition (&line, &cur);
782 804
783 if (_edit_area->markersAtLine (line) && (1 << marker::bookmark)) 805 line++; // Find bookmark strictly after the current line.
784 line++; // we have a breakpoint here, so start search from next line
785 806
786 int nextline = _edit_area->markerFindNext (line, (1 << marker::bookmark)); 807 int nextline = _edit_area->markerFindNext (line, (1 << marker::bookmark));
787 808
788 _edit_area->setCursorPosition (nextline, 0); 809 _edit_area->setCursorPosition (nextline, 0);
789 } 810 }
790 811
812 // Move the text cursor to the closest bookmark
813 // before the current line.
791 void 814 void
792 file_editor_tab::previous_bookmark (const QWidget *ID) 815 file_editor_tab::previous_bookmark (const QWidget *ID)
793 { 816 {
794 if (ID != this) 817 if (ID != this)
795 return; 818 return;
796 819
797 int line, cur; 820 int line, cur;
798 _edit_area->getCursorPosition (&line, &cur); 821 _edit_area->getCursorPosition (&line, &cur);
799 822
800 if (_edit_area->markersAtLine (line) && (1 << marker::bookmark)) 823 line--; // Find bookmark strictly before the current line.
801 line--; // we have a breakpoint here, so start search from prev line
802 824
803 int prevline = _edit_area->markerFindPrevious (line, (1 << marker::bookmark)); 825 int prevline = _edit_area->markerFindPrevious (line, (1 << marker::bookmark));
804 826
805 _edit_area->setCursorPosition (prevline, 0); 827 _edit_area->setCursorPosition (prevline, 0);
806 } 828 }
896 return; 918 return;
897 919
898 int editor_linenr, cur; 920 int editor_linenr, cur;
899 _edit_area->getCursorPosition (&editor_linenr, &cur); 921 _edit_area->getCursorPosition (&editor_linenr, &cur);
900 922
901 if (_edit_area->markersAtLine (editor_linenr) && (1 << marker::breakpoint)) 923 if (_edit_area->markersAtLine (editor_linenr) & (1 << marker::breakpoint))
902 request_remove_breakpoint_via_editor_linenr (editor_linenr); 924 request_remove_breakpoint_via_editor_linenr (editor_linenr);
903 else 925 else
904 { 926 {
905 if (_edit_area->isModified ()) 927 if (unchanged_or_saved ())
906 message_cannot_breakpoint_changed_file ();
907 else
908 handle_request_add_breakpoint (editor_linenr + 1); 928 handle_request_add_breakpoint (editor_linenr + 1);
909 } 929 }
910 } 930 }
911 931
932 // Move the text cursor to the closest breakpoint
933 // after the current line.
912 void 934 void
913 file_editor_tab::next_breakpoint (const QWidget *ID) 935 file_editor_tab::next_breakpoint (const QWidget *ID)
914 { 936 {
915 if (ID != this) 937 if (ID != this)
916 return; 938 return;
917 939
918 int line, cur; 940 int line, cur;
919 _edit_area->getCursorPosition (&line, &cur); 941 _edit_area->getCursorPosition (&line, &cur);
920 942
921 if (_edit_area->markersAtLine (line) && (1 << marker::breakpoint)) 943 line++; // Find breakpoint strictly after the current line.
922 line++; // we have a breakpoint here, so start search from next line
923 944
924 int nextline = _edit_area->markerFindNext (line, (1 << marker::breakpoint)); 945 int nextline = _edit_area->markerFindNext (line, (1 << marker::breakpoint));
925 946
926 _edit_area->setCursorPosition (nextline, 0); 947 _edit_area->setCursorPosition (nextline, 0);
927 } 948 }
928 949
950 // Move the text cursor to the closest breakpoint
951 // before the current line.
929 void 952 void
930 file_editor_tab::previous_breakpoint (const QWidget *ID) 953 file_editor_tab::previous_breakpoint (const QWidget *ID)
931 { 954 {
932 if (ID != this) 955 if (ID != this)
933 return; 956 return;
934 957
935 int line, cur, prevline; 958 int line, cur, prevline;
936 _edit_area->getCursorPosition (&line, &cur); 959 _edit_area->getCursorPosition (&line, &cur);
937 960
938 if (_edit_area->markersAtLine (line) && (1 << marker::breakpoint)) 961 line--; // Find breakpoint strictly before the current line.
939 line--; // we have a breakpoint here, so start search from prev line
940 962
941 prevline = _edit_area->markerFindPrevious (line, (1 << marker::breakpoint)); 963 prevline = _edit_area->markerFindPrevious (line, (1 << marker::breakpoint));
942 964
943 _edit_area->setCursorPosition (prevline, 0); 965 _edit_area->setCursorPosition (prevline, 0);
944 } 966 }
2184 file_editor_tab::insert_debugger_pointer (const QWidget *ID, int line) 2206 file_editor_tab::insert_debugger_pointer (const QWidget *ID, int line)
2185 { 2207 {
2186 if (ID != this || ID == 0) 2208 if (ID != this || ID == 0)
2187 return; 2209 return;
2188 2210
2189 emit remove_all_positions (); // remove all positions 2211 emit remove_all_positions (); // debugger_position, unsure_debugger_position
2190 2212
2191 if (line > 0) 2213 if (line > 0)
2192 { 2214 {
2193 marker *dp; 2215 marker *dp;
2194 2216