| 1 | #!/usr/bin/env python |
| 2 | # $Id: TextEditor.py 536 2020-10-30 15:20:32Z andreasheld $ |
| 3 | # Author: Chris Green <[email protected]> |
| 4 | # Purpose: test text editor |
| 5 | # Created: Thu Jan 22 14:06:24 EST 2004 |
| 6 | # |
| 7 | # This has a goal of http://www.fltk.org/documentation.php/doc-1.1/editor.html#editor |
| 8 | # |
| 9 | # Text Editor test program for pyFLTK the Python bindings |
| 10 | # for the Fast Light Tool Kit (FLTK). |
| 11 | # |
| 12 | # FLTK copyright 1998-1999 by Bill Spitzak and others. |
| 13 | # pyFLTK copyright 2003 by Andreas Held and others. |
| 14 | # |
| 15 | # This library is free software you can redistribute it and/or |
| 16 | # modify it under the terms of the GNU Library General Public |
| 17 | # License, version 2.0 as published by the Free Software Foundation. |
| 18 | # |
| 19 | # This library is distributed in the hope that it will be useful, |
| 20 | # but WITHOUT ANY WARRANTY without even the implied warranty of |
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | # Library General Public License for more details. |
| 23 | # |
| 24 | # You should have received a copy of the GNU Library General Public |
| 25 | # License along with this library if not, write to the Free Software |
| 26 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 27 | # USA. |
| 28 | # |
| 29 | # Please report all bugs and problems to "[email protected]". |
| 30 | # |
| 31 | |
| 32 | |
| 33 | import sys |
| 34 | from fltk import * |
| 35 | |
| 36 | def onFileQuit(ptr, data): |
| 37 | #menuItem = Fl_Menu_ItemPtr(ptr) |
| 38 | menuItem = ptr |
| 39 | print(f'onFileQuit({str(menuItem)}, "{str(data)}")') |
| 40 | import sys # code |
| 41 | sys.exit(0) # code |
| 42 | |
| 43 | |
| 44 | def onFileSave(ptr , data): |
| 45 | #menuItem = Fl_Menu_ItemPtr(ptr) |
| 46 | menuItem = ptr |
| 47 | print(f'onFileSave({str(menuItem)}, "{str(data)}")') |
| 48 | |
| 49 | textEditor = data |
| 50 | print(textEditor.buffer().text()) |
| 51 | |
| 52 | def tb_changed_cb(pos, nInserted, nDeleted, nRestyled, deleted_text, ptr): |
| 53 | print("in callback: ", pos, nDeleted) |
| 54 | |
| 55 | def main(): |
| 56 | |
| 57 | o_1_0 = Fl_Window(810, 23, 200, 200) |
| 58 | textEditor = Fl_Text_Editor(0,100, 100, 100) |
| 59 | |
| 60 | # create a 1000 byte long text buffer |
| 61 | textBuffer = Fl_Text_Buffer(1000) |
| 62 | textBuffer.text("Wee") |
| 63 | |
| 64 | # "hello world!" |
| 65 | textBuffer.add_modify_callback(tb_changed_cb, textEditor); |
| 66 | #textBuffer.call_modify_callbacks(); |
| 67 | |
| 68 | # place the buffer inside the editor |
| 69 | textEditor.buffer(textBuffer) |
| 70 | |
| 71 | o_2_0 = Fl_Menu_Bar(0, 0, 105, 25) |
| 72 | |
| 73 | userData = "User callback data" |
| 74 | |
| 75 | o_2_0.menu(( |
| 76 | ("&File", 0, 0, 0, 64), |
| 77 | ("&Quit", 0, onFileQuit, userData, 0), |
| 78 | ("&Save", 0, onFileSave, textEditor, 0), |
| 79 | (None, ), |
| 80 | (None, ) )) |
| 81 | |
| 82 | o_1_0.end() |
| 83 | |
| 84 | return o_1_0 |
| 85 | |
| 86 | |
| 87 | |
| 88 | if __name__=='__main__': |
| 89 | window = main() |
| 90 | window.show(len(sys.argv), sys.argv) |
| 91 | Fl.run() |