|
33 | 33 | APP_NAME: str = 'NotepadPy+'
|
34 | 34 | SELECTED_THEME: str = ''
|
35 | 35 | text_to_save: str = ''
|
| 36 | +selected_text: str = '' |
36 | 37 | # this is needed to control the displaying of the user prompt while closing.
|
37 | 38 | # If the user closes the document just before closing the document,
|
38 | 39 | # we do not want to display the prompt for save changes.
|
@@ -211,8 +212,15 @@ def rgb2hex(r, g, b):
|
211 | 212 |
|
212 | 213 | def new_file() -> str:
|
213 | 214 | ''' Reset body and info bar, and clear FILE_NAME variable '''
|
| 215 | + |
| 216 | +global text_last_saved_manually |
| 217 | +global text_to_save |
| 218 | + |
214 | 219 | WINDOW['-BODY-'].update(value='')
|
215 | 220 | WINDOW['-FILE_INFO-'].update(value='New File:')
|
| 221 | +WINDOW.set_title('untitled - ' + APP_NAME) |
| 222 | +text_last_saved_manually = '' |
| 223 | +text_to_save = '' |
216 | 224 |
|
217 | 225 | def open_file() -> str:
|
218 | 226 | ''' Open file and update the infobar '''
|
@@ -409,24 +417,48 @@ def AboutNotepadPyPlus():
|
409 | 417 |
|
410 | 418 | # edit menu events.
|
411 | 419 | if EVENT == edit_cut:
|
412 |
| -selected_text = WINDOW['-BODY-'].Widget.selection_get() |
413 |
| -tk.clipboard_clear() |
414 |
| -tk.clipboard_append(selected_text) |
415 |
| -tk.update() |
416 |
| -WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last") |
| 420 | +try: |
| 421 | +selected_text = WINDOW['-BODY-'].Widget.selection_get() |
| 422 | +tk.clipboard_clear() |
| 423 | +tk.clipboard_append(selected_text) |
| 424 | +tk.update() |
| 425 | +WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last") |
| 426 | +except: # pylint: disable=bare-except |
| 427 | +selected_text = '' |
| 428 | +ShowMessageBox(title='Text Selection Error', |
| 429 | +message='An active text selection was not found. Please select some text to cut.') |
417 | 430 |
|
418 | 431 | if EVENT == edit_copy:
|
419 |
| -selected_text = WINDOW['-BODY-'].Widget.selection_get() |
420 |
| -tk.clipboard_clear() |
421 |
| -tk.clipboard_append(selected_text) |
422 |
| -tk.update() # now it stays on the clipboard after the window is closed |
| 432 | +try: |
| 433 | +selected_text = WINDOW['-BODY-'].Widget.selection_get() |
| 434 | +tk.clipboard_clear() |
| 435 | +tk.clipboard_append(selected_text) |
| 436 | +tk.update() # now it stays on the clipboard after the window is closed |
| 437 | +except: # pylint: disable=bare-except |
| 438 | +selected_text = '' |
| 439 | +ShowMessageBox(title='Text Selection Error', |
| 440 | +message='An active text selection was not found. Please select some text to copy.') |
423 | 441 |
|
424 | 442 | if EVENT == edit_paste:
|
425 | 443 | clip_text = tk.clipboard_get()
|
| 444 | + |
| 445 | +try: |
| 446 | +selected_text = WINDOW['-BODY-'].Widget.selection_get() |
| 447 | +except: # pylint: disable=bare-except |
| 448 | +selected_text = '' |
| 449 | + |
| 450 | +if selected_text != '': |
| 451 | +WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last") |
| 452 | + |
426 | 453 | WINDOW['-BODY-'].Widget.insert("insert", clip_text)
|
427 | 454 |
|
428 | 455 | if EVENT == edit_delete:
|
429 |
| -WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last") |
| 456 | +try: |
| 457 | +WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last") |
| 458 | +except: |
| 459 | +selected_text = '' |
| 460 | +ShowMessageBox(title='Text Selection Error', |
| 461 | +message='An active text selection was not found. Please select some text to delete.') |
430 | 462 |
|
431 | 463 | if EVENT in ('Word Count',):
|
432 | 464 | WORDS = get_word_count()
|
@@ -484,6 +516,10 @@ def AboutNotepadPyPlus():
|
484 | 516 | # record the text after each event to ensure the
|
485 | 517 | # file/text is saved.
|
486 | 518 | try:
|
487 |
| -text_to_save = VALUES['-BODY-'] |
| 519 | +# if File -> New menu option is chosen and the new blank editor window is closed, then we do |
| 520 | +# not want to display the Save File prompt. Executing this block on the event of a new file |
| 521 | +# resets the 'text_to_save' variable to old text in the editor and causes to display the save prompt. |
| 522 | +if EVENT != file_new: |
| 523 | +text_to_save = VALUES['-BODY-'] |
488 | 524 | except: # pylint: disable=bare-except
|
489 | 525 | pass
|
0 commit comments