All pastes #57503 Raw Edit

Casanova

public python v1 · immutable
#57503 ·published 2006-05-21 08:23 UTC
rendered paste body
#!/usr/bin/env pythonimport sys,osimport dialogsimport webbrowsertry:    import gtkexcept ImportError:    print 'No PyGtk Installed'    sys.exit (1)try:        import scintillaexcept ImportError:    print 'No PyGtkScintilla Installed'    sys.exit (1)class LDTPUIManager:    ui = '''<ui>        <menubar name="Menubar">            <menu action="File">                <menuitem action="New"/>                <menuitem action="Open"/>                <menuitem action="Save"/>                <menuitem action="Save As"/>                <separator/>                <!--<menuitem action="Close"/>-->                <menuitem action="Quit"/>            </menu>            <menu action="Edit">                <menuitem action="Undo" />                <menuitem action="Redo" />                <separator/>                <menuitem action="Cut"/>                <menuitem action="Copy"/>                <menuitem action="Paste"/>                <menuitem action="Delete"/>                <separator/>                <menuitem action="Select All"/>                <!-- <menuitem action="Insert Date/Time"/> -->            </menu>            <menu action="View">                <menuitem action="Script Selection"/>            </menu>		            <menu action="Format">                <menuitem action="Indent Region"/>                <menuitem action="Dedent Region"/>            </menu>            <menu action="Tools">                <menuitem action="Record"/>                <menuitem action="Data XML"/>            </menu>            <menu action="Help">                <menuitem action="Contents"/>                <menuitem action="About"/>            </menu>        </menubar>        <toolbar name="Toolbar">            <toolitem action="New"/>            <toolitem action="Open"/>            <toolitem action="Save"/>            <separator/>            <toolitem action="Cut"/>            <toolitem action="Copy"/>            <toolitem action="Paste"/>		            <separator/>            <toolitem action="Record"/>        </toolbar>    </ui>'''    sl_ui = '''<ui>     <menubar name="Menubar">       <menu action="Options">         <menuitem action="Add Group"/>         <menuitem action="Remove Group"/>         <menuitem action="Add Script(s)"/>         <menuitem action="Remove Script"/>	 <menuitem action="Add Datafile"/>       </menu>     </menubar>	     <toolbar name="Toolbar">     </toolbar>    </ui> '''	    def create_actiongroup (self):        actiongroup = gtk.ActionGroup('LDTPUIManager')                actiongroup.add_toggle_actions([('Script Selection', None, 'Scri_pt Selection', '<Control>p',                                    'Show Script Selection Window', self.script_selection_cb )])                # Create actions        actiongroup.add_actions([('New', gtk.STOCK_NEW, '_New', None,                              'Create a new script', self.new_cb),                             ('Open', gtk.STOCK_OPEN, '_Open', None,                              'Open a file', self.open_cb),                             ('Save', gtk.STOCK_SAVE, '_Save', None,                              'Save the current file', self.save_cb),                             ('Save As', gtk.STOCK_SAVE_AS, 'Save _As', '<Shift><Control>s',                              None, self.saveas_cb),                             ('Quit', gtk.STOCK_QUIT, '_Quit', None,                              'Quit the Program', self.quit_cb),                             ('Undo', gtk.STOCK_UNDO, '_Undo','<Ctrl>Z', None, self.undo_cb),                             ('Redo',gtk.STOCK_REDO, '_Redo','<Shift><Ctrl>Z', None, self.redo_cb),                             ('Cut', gtk.STOCK_CUT, 'Cu_t', None,                              'Cut the selection',self.on_cut_activate_cb),                             ('Copy', gtk.STOCK_COPY, '_Copy', None,                              'Copy the selection', self.on_copy_activate_cb),                             ('Paste', gtk.STOCK_PASTE, '_Paste', None,                              'Paste the Clipboard', self.on_paste_activate_cb),                             ('Delete', gtk.STOCK_DELETE, '_Delete',None , None, self.delete_cb),                             ('Select All',None, 'Select _All', '<Ctrl>A', None, self.selectall_cb),                             ('Indent Region', None, '_Increase Indent', '<Control>i',                              None, self.indent_region_cb),                             ('Dedent Region', None, '_Decrease Indent', '<Control>d',                              None, self.dedent_region_cb),                             ('Record', gtk.STOCK_HARDDISK, '_Record', '<Control>r',                              'Start recording', self.record_cb),                             ('Data XML', None, 'Data XML', None,                              'Create Data XML file', self.create_xml_data_file_cb),                             ('Contents', gtk.STOCK_HELP, '_Contents', 'F1',                              None, None),                             ('About', gtk.STOCK_ABOUT, '_About', None,                              None, self.about_cb),                             ('File', None, '_File'),                             ('Edit', None, '_Edit'),                             ('View', None, '_View'),                             ('Format', None, 'Fo_rmat'),                             ('Tools', None, '_Tools'),                             ('Help', None, '_Help')])        return actiongroup    def set_style (self, lang='python'):        self.notebook.SetWrapMode (scintilla.SC_WRAP_WORD)        self.notebook.SetVScrollBar (1)        self.notebook.SetTabWidth (4)        self.notebook.SetUseTabs (0)        if lang == 'python':            self.notebook.SetLexer (scintilla.SCLEX_PYTHON)            self.notebook.SetLexerLanguage ('python')            self.notebook.StyleClearAll ()            self.notebook.StyleSetFore (scintilla.SCE_P_DEFNAME, 0x0000FF)            self.notebook.StyleSetFore (scintilla.SCE_P_COMMENTLINE, 0x00FF00)            self.notebook.StyleSetFore (scintilla.SCE_P_COMMENTBLOCK, 0x00FF00)            self.notebook.StyleSetFore (scintilla.SCE_P_NUMBER, 0xFF00FF)            self.notebook.StyleSetFore (scintilla.SCE_P_CLASSNAME, 0x0000FF)            self.notebook.StyleSetItalic (scintilla.SCE_P_COMMENTBLOCK,1)            self.notebook.StyleSetItalic (scintilla.SCE_P_STRING,1)            self.notebook.StyleSetBold (scintilla.SCE_P_OPERATOR,1)            self.notebook.StyleSetBold (scintilla.SCE_P_CLASSNAME,1)            self.notebook.StyleSetBold (scintilla.SCE_P_WORD,1)            self.notebook.SetKeyWords (0,"and assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while yield")            self.notebook.Colourise (0,-1)        elif lang == 'xml':            self.notebook.SetLexer (scintilla.SCLEX_XML)            self.notebook.StyleClearAll ()            self.notebook.StyleSetFore (scintilla.SCE_H_COMMENT, 0x00FF00)            self.notebook.StyleSetBold (scintilla.SCE_H_TAGUNKNOWN,1)                            def text_changed_cb (self, note, object):        self.modified = True        #print type (a), type (b)        print dir (object)        print object.flags, object.name, object.nick, object.owner_type, object.value_type        self.notebook.SetModEventMask( scintilla.SC_MOD_INSERTTEXT)        print 'over'        return True            def __init__(self):        # Create the toplevel window        self.window = gtk.Window()	self.window.set_title('LDTP Editor')        self.window.set_default_size(650, 500)        vbox = gtk.VBox()        self.window.add(vbox)        # Create a LDTPUIManager instance        self.uimanager = gtk.UIManager()        # Add the accelerator group to the toplevel self.window        accelgroup = self.uimanager.get_accel_group()        self.window.add_accel_group(accelgroup)        # Create an ActionGroup        actiongroup = self.create_actiongroup ()        self.actiongroup = actiongroup        # Add the actiongroup to the self.uimanager        self.uimanager.insert_action_group(actiongroup, 0)        # Add a UI description        self.uimanager.add_ui_from_string(self.ui)        # Create a MenuBar        menubar = self.uimanager.get_widget('/Menubar')        vbox.pack_start(menubar, False)        # Create a Toolbar        toolbar = self.uimanager.get_widget ('/Toolbar')        vbox.pack_start (toolbar, False)		# Create an Horizontal Pane	self.hpane = gtk.HPaned ()	vbox.pack_start (self.hpane, True)	# FIXME Have to make use of this status bar to show line, col numbers etc..	# Create Statusbar	self.statusbar = gtk.Statusbar()	vbox.pack_end(self.statusbar, False)	self.context_id = self.statusbar.get_context_id('My Editor')	# Create Clipboard	self.clipboard = gtk.Clipboard(gtk.gdk.display_get_default(), 'CLIPBOARD')        #Create the Text Object (Scintilla)        self.notebook = scintilla.Scintilla ()        self.notebook.SetModEventMask( scintilla.SC_MOD_INSERTTEXT)        self.hpane.add2 (self.notebook)        # editing eye candy :-)        self.set_style ()        self.window.connect('delete_event', self.quit_cb)        self.notebook.set_size_request (650,500)        self.window.set_default_size(650, 500)        self.window.show_all()        self.modified = False        self.docs = []        self.notebook.connect ("notify", self.text_changed_cb)        self.notebook.GrabFocus ()        return            def open_cb (self, action):	open_dialog = dialogs.file_dialog ('Open File', self.window, gtk.FILE_CHOOSER_ACTION_OPEN)	response = open_dialog.run()	if response == gtk.RESPONSE_OK:		file = open_dialog.get_filename()		infile = open (file, 'r')                		if infile:			file_content = infile.read()		infile.close()		# Get the file name and set it as the label of page		file_name = os.path.basename (file)                if file_name [-4:] == '.xml':                    self.set_style ('xml')                elif file_name [-3:] == '.py':                    self.set_style ('python')                    		self.window.set_title(file_name + ' - LDTP Editor')		                self.notebook.SetText (file_content)                self.notebook.set_label (open_dialog.get_filename())		open_dialog.destroy()	else:		open_dialog.destroy()        return    def get_file_to_save (self, save_dialog):    	file = None	replace = False    	while True:		response = save_dialog.run()					if response == gtk.RESPONSE_OK:			file = save_dialog.get_filename()							if os.path.exists(file):				prim_text = 'A file named \"' + file + '\" already exists.'					sec_text = 'Do you want to replace it with the one you are saving?'				buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,				           'Replace', gtk.RESPONSE_ACCEPT)				message_dialog = dialogs.message_dialog (save_dialog, gtk.MESSAGE_QUESTION,							buttons, gtk.RESPONSE_CANCEL, prim_text, sec_text)				response = message_dialog.run()								if response == gtk.RESPONSE_ACCEPT:					message_dialog.destroy()					replace = True				else:						message_dialog.destroy()					file = None					continue				save_dialog.destroy()		else:			save_dialog.destroy()				break	return (file, replace)    def saveas_cb (self, action):        prev_label = self.notebook.get_label()        if prev_label == None:            prev_label = ''        save_dialog = dialogs.file_dialog ('Save As...', self.window, gtk.FILE_CHOOSER_ACTION_SAVE, default_file=prev_label)                filename, replace = self.get_file_to_save (save_dialog)	        if file:            fp = open (filename, 'w')            contents = self.notebook.GetText()[1]            fp.write (contents)            fp.close ()            self.modified = False            self.notebook.set_label (filename)            # Get the file name and set it as the label of page            file_name = os.path.basename (filename)            self.window.set_title (file_name + ' - LDTP Editor')    def save_cb (self, action):#         if self.modified == False:#             return        filename = self.notebook.get_label()        if filename == None:            self.saveas_cb (action)            pass        else:            contents = self.notebook.GetText()[1]            fp = open (filename, 'w')            fp.write (contents)            fp.close ()        self.modified = False        return    def quit_cb (self, window, event):        global no_of_docs        no_of_docs -= 1        if no_of_docs == 0:            gtk.main_quit ()        return    def new_cb (self, action):        global no_of_docs        no_of_docs += 1        self.docs.append(LDTPUIManager ())        return    def undo_cb (self, action):        self.undo_history += 1        self.notebook.Undo ()        # Fix making menus disabled and enabled        return    def redo_cb (self, action):        self.notebook.Redo ()        return    def on_cut_activate_cb (self, action):        self.notebook.Cut ()        return    def on_copy_activate_cb (self, action):        self.notebook.Copy ()        return    def on_paste_activate_cb (self, action):        self.notebook.Paste ()        return    def delete_cb (self, action):        self.notebook.DeleteBack ()        return    def selectall_cb (self, action):        self.notebook.SelectAll ()        return        def indent_region_cb (self, action):        pass    def dedent_region_cb (self, action):        pass    def record_cb (self, action):        pass    def create_xml_data_file_cb (self, action):        pass    def script_selection_cb (self, action):        pass    def about_cb(self, b):	# Create about dialog	image_path = '.'	about_dialog = gtk.AboutDialog()	about_dialog.set_name('LDTP Editor')	about_dialog.set_version('0.2.0')	about_dialog.set_comments('LDTP Editor is a lightweight text editor for\nLinux Desktop Testing Project')        about_dialog.set_copyright('Copyright \xc2\xa9 2005 Linux Desktop Testing Project')	gtk.about_dialog_set_url_hook(webbrowser.open ('http://ldtp.freedesktop.org'), None)	about_dialog.set_website_label('http://ldtp.freedesktop.org')	about_dialog.set_authors(['Prashanth Mohan <prashmohan@gmail.com>','Khasim Shaheed <sshaik@novell.com>'])		if '/usr/lib/ldtpeditor' in sys.path:		image_path = '/usr/lib/ldtpeditor'			logo = gtk.gdk.pixbuf_new_from_file(image_path + '/ldtp-logo-small.png')	about_dialog.set_logo (logo)	about_dialog.show()def main ():    gtk.main()no_of_docs = 1document = LDTPUIManager ()main()