Esempio n. 1
0
class Window(hildon.Window):
  def __init__(self,filepath=None,caller=None,prefs=None,theme=None):
    hildon.Window.__init__ (self)
    self.prefs = prefs
    self.theme = theme

    if self.prefs.prefs_dict['auto_rotate']==True:
      self.rotation = FremantleRotation('net.khertan.pygtkeditor',self,mode=FremantleRotation.AUTOMATIC)
    else:
      self.rotation = FremantleRotation('net.khertan.pygtkeditor',self,mode=FremantleRotation.NEVER)

    self.is_fullscreen = False

#    self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(theme['widget']['background']) )

    self._parent = caller
    self.filepath=filepath

    self.code_editor=pge_editor.Editor(self._detect_language(self.filepath),font_name=self.prefs.prefs_dict['font_name'],font_size=self.prefs.prefs_dict['font_size'],prefs=self.prefs.prefs_dict,theme=self.theme)

    self.toolbar = self.create_toolbar()
    self.add_toolbar(self.toolbar)

    menu = self.create_menu()
    menu.show_all()
    self.set_app_menu(menu)

    self.create_findtoolbar()

    p1 = hildon.PannableArea()
    p1.set_property('mov_mode',hildon.MOVEMENT_MODE_BOTH)

#    self.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(theme['widget']['background']) )
#    self.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(theme['widget']['background']) )
#    self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(theme['widget']['background']) )

    p1.add(self.code_editor)

    #self.code_editor
    self.add(p1)
    self.show_all()
    self.saved=True

#    print filepath
    if filepath!=None:
      self.set_title(os.path.basename(filepath))
      self.open_file(filepath)
    else:
      self.set_title('*Untitled')
      self.saved=False

    self.connect("key-press-event", self.on_key_press)
    self.code_editor.buffer.connect ('changed', self.on_modified)

    #self.app.add_window(self)

  def create_findtoolbar(self):
    self.findToolBar = hildon.FindToolbar("Search for :")
    self.add_toolbar(self.findToolBar)

    self.findToolBar.connect("close", self.onHideFind)
    self.findToolBar.connect("search", self.onSearchFind)
    self.findToolBar.set_no_show_all(True)

  def onShowFind(self, widget):
    self.findToolBar.show()
    for fchild in self.findToolBar.get_children():
      try:
        for achild in fchild.get_children():
          try:
            for cchild in achild.get_children():
              if type(cchild)==gtk.ComboBoxEntry:
                self.set_focus(cchild.get_children()[0])
          except:
            pass
      except:
        pass

  def apply_prefs(self,prefs,theme):
    self.prefs = prefs
    self.theme = theme

    #widget
#    self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(theme['widget']['background']) )

    #rotate
    if self.prefs.prefs_dict['auto_rotate']==True:
      self.rotation.set_mode(FremantleRotation.AUTOMATIC)
#      FremantleRotation('net.khertan.pygtkeditor',self,mode=FremantleRotation.AUTOMATIC)
    else:
      self.rotation.set_mode(FremantleRotation.NEVER)
#      FremantleRotation('net.khertan.pygtkeditor',self,mode=FremantleRotation.NEVER)

    #Apply prefs
#    #font / size
#    print self.prefs.prefs_dict['font_name']+" "+str(self.prefs.prefs_dict['font_size'])
#    self.code_editor.modify_font(pango.FontDescription (self.prefs.prefs_dict['font_name']+" "+str(self.prefs.prefs_dict['font_size'])))
#
#    if self.prefs.prefs_dict['show_lines'] == True:
#      self.code_editor.set_border_window_size(gtk.TEXT_WINDOW_LEFT, 45)
# #     self.set_border_window_size(gtk.TEXT_WINDOW_LEFT, 50)
#
#      self.code_editor.line_view = self.code_editor.get_window(gtk.TEXT_WINDOW_LEFT)
#      if self.code_editor.expose_event_cb==None:
#        self.code_editor.expose_even_cb = self.code_editor.connect("expose_event", self.code_editor.line_numbers_expose)
#    else:
#      self.code_editor.set_border_window_size(gtk.TEXT_WINDOW_LEFT, 0)
##      self.code_editor.line_view = None
#      if self.code_editor.expose_event_cb!=None:
#        self.code_editor.disconnect(self.code_editor.expose_event_cb)
    self.code_editor.apply_theme(self.theme)
    self.code_editor.apply_prefs(prefs.prefs_dict)

  def onHideFind(self, widget):
    self.findToolBar.hide()
    self.searched_text = None

  def onSearchFind(self,widget,data=None):
    text = widget.get_property("prefix")
    self.code_editor.search(text)

  def on_modified(self,buf,data=None):
    self.saved=False
    title = self.get_title()
    if title.startswith('*'):
      return
    else:
      self.set_title('*'+title)

  def execute(self):
    #ask for save if unsaved
    if self.saved == False:
      noteconf = hildon.hildon_note_new_confirmation(self,'Did you want to save this file before launching it ?')
      if noteconf.run() == gtk.RESPONSE_OK:
        self.save()
      noteconf.destroy()

    if self.filepath != None:
      note = osso.SystemNote(self._parent.context)
      result = note.system_note_infoprint("Launching "+ self.filepath +" ...")

      fileHandle = open('/tmp/pygtkeditor.tmp', 'w')
      fileHandle.write('#!/bin/sh\n')
      fileHandle.write('cd '+os.path.dirname(self.filepath)+' \n')
      fileHandle.write(self._detect_launch_language()+" \'"+self.filepath + "\'\n")
      fileHandle.write('read -p "Press ENTER to continue ..." foo')
      fileHandle.write('\nexit')
      fileHandle.close()
      commands.getoutput("chmod 777 /tmp/pygtkeditor.tmp")
      Popen('/usr/bin/osso-xterm /tmp/pygtkeditor.tmp',shell=True,stdout=None)
    else:
      note = osso.SystemNote(self._parent.context)
      result = note.system_note_infoprint("Unsaved file cannot be launched.")

  def _detect_launch_language(self):
    return 'python'

  def _detect_language(self,filepath):

    if filepath==None:
      return self.prefs.prefs_dict['default_language']

    for extension,lang in LANGUAGES:
      if filepath.endswith(extension.lower()):
        return lang
    return self.prefs.prefs_dict['default_language']

  def on_key_press(self, widget, event, *args):
    if (event.state==gtk.gdk.CONTROL_MASK):
      #New : CTRL-N
      if (event.keyval == gtk.keysyms.s):
        self._parent.create_window()
      #Save : CTRL-S
      if (event.keyval == gtk.keysyms.s):
        self.save()
      #Undo : CTRL-Z
      elif (event.keyval == gtk.keysyms.z):
        self.code_editor.buffer.undo()
      #Redo : CTRL-Y
      elif (event.keyval == gtk.keysyms.y):
        self.code_editor.buffer.redo()
      #Open : CTRL-O
      elif (event.keyval == gtk.keysyms.o):
        self.open_dialog()
      #Searh : CTRL-F
      elif (event.keyval == gtk.keysyms.f):
        self.onShowFind(widget)
      #Duplicate line : CTRL-D
      elif (event.keyval == gtk.keysyms.d):
        self.code_editor.duplicate_line()
      #Close : CTRL-W
      elif (event.keyval == gtk.keysyms.w):
        self.destroy()
      #Show Info : CTRL-I
      elif (event.keyval == gtk.keysyms.i):
        info = hildon.FileDetailsDialog(self,self.filepath)
        info.run()
        info.destroy()
      #Show Help
      elif (event.keyval == gtk.keysyms.h):
        self.show_help()
      #Execute
      elif (event.keyval == gtk.keysyms.e):
        self.execute()

  def show_help(self):
    import pge_help
    pge_help.Help()

  def save_file(self,filepath):
    try:
      if self.prefs.prefs_dict['auto_clean_line_end']==True:
        self.code_editor.clean_line_end()
      f = open(filepath,'w')
      buf = self.code_editor.get_buffer()
      start,end = buf.get_bounds()
      f.write(buf.get_text(start,end))
      f.close()
      self.set_title(os.path.basename(filepath))
      self.saved=True
      self.filepath = filepath
      note = osso.SystemNote(self._parent.context)
      result = note.system_note_infoprint(self.filepath+' Saved')
      language = self._detect_language(self.filepath)
      if self.code_editor.language != language:
        self.code_editor.reset_language(language)
    except StandardError,e:
      note = osso.SystemNote(self._parent.context)
      result = note.system_note_dialog('An error occurs saving file :\n'+str(e))
Esempio n. 2
0
class Window(hildon.Window):
  def __init__(self,caller=None):
    hildon.Window.__init__ (self)
    self.prefs = pge_preferences.Prefs()
    self.prefs.load()

    if self.prefs.prefs_dict['auto_rotate']==True:
      self.rotation = FremantleRotation('net.khertan.pygtkeditor',self,mode=FremantleRotation.AUTOMATIC)
    else:
      self.rotation = FremantleRotation('net.khertan.pygtkeditor',self,mode=FremantleRotation.NEVER)

    self.is_fullscreen = False
    self._parent = caller

    self.set_title('PyGTKEditor')
    self.connect("key-press-event", self.on_key_press)

    menu = self.create_menu()
    menu.show_all()
    self.set_app_menu(menu)

    self.add(self._create_ui())
    self.show_all()

  def _create_ui(self):
    p1 = hildon.PannableArea()
    vbox = gtk.VBox()
    p1.add(vbox)

    vbox.add(gtk.Label('PyGTKEditor %s' % (self._parent.VERSION,)))
    i = gtk.Image()
    i.set_from_icon_name('pygtkeditor',gtk.ICON_SIZE_LARGE_TOOLBAR)
    vbox.add(i)

    hbox = gtk.HBox()
    new_button = hildon.Button(0,0)
    new_button.set_label('New')
    new_button.connect("clicked", self.menu_button_clicked, 'New')

    open_button = hildon.Button(0,0)
    open_button.set_label('Open')
    open_button.connect("clicked", self.open_dialog)

    hbox.add(new_button)
    hbox.add(open_button)
    vbox.add(hbox)

    self.recent_manager = gtk.recent_manager_get_default()
#    self.recent_manager.set_limit(3)
    self.recent_manager.connect('changed',self.recent_manager_changed)
    ritems=self.recent_manager.get_items()
    ritems.sort(lambda x,y: y.get_modified()-x.get_modified())

    if (len(ritems)>2):
      vbox.add(gtk.Label('Recent Files'))
      self.recent_button_1=hildon.Button(0,1)
      label = ritems[0].get_uri_display()
      self.recent_button_1.set_title(os.path.basename(label))
      self.recent_button_1.set_value(label)
      self.recent_button_1.set_alignment(0.0,0.0,0.0,0.5)
      vbox.add(self.recent_button_1)
      self.recent_button_1.connect("clicked", self.recent_button_clicked, label)
      self.recent_button_2=hildon.Button(0,1)
      label = ritems[1].get_uri_display()
      self.recent_button_2.set_title(os.path.basename(label))
      self.recent_button_2.set_value(label)
      self.recent_button_2.set_alignment(0.0,0.0,0.0,0.5)
      vbox.add(self.recent_button_2)
      self.recent_button_2.connect("clicked", self.recent_button_clicked, label)
      self.recent_button_3=hildon.Button(0,1)
      label = ritems[2].get_uri_display()
      self.recent_button_3.set_title(os.path.basename(label))
      self.recent_button_3.set_value(label)
      self.recent_button_3.set_alignment(0.0,0.0,0.0,0.5)
      vbox.add(self.recent_button_3)
      self.recent_button_3.connect("clicked", self.recent_button_clicked, label)

    return p1

  def recent_manager_changed(self,widget,*data):
    ritems=self.recent_manager.get_items()
    ritems.sort(lambda x,y: y.get_modified()-x.get_modified())
    if (len(ritems)>2) and (self.hasattr('recent_button_1')):
      label = ritems[0].get_uri_display()
      self.recent_button_1.set_title(os.path.basename(label))
      self.recent_button_1.set_value(label)
      label = ritems[1].get_uri_display()
      self.recent_button_2.set_title(os.path.basename(label))
      self.recent_button_2.set_value(label)
      label = ritems[2].get_uri_display()
      self.recent_button_3.set_title(os.path.basename(label))
      self.recent_button_3.set_value(label)


  def recent_button_clicked(self,button,url):
    url = button.get_value()
    self._parent.create_window(url)

  def menu_button_clicked(self,button, label):
    if label == 'New':
      self._parent.create_window()
    elif label=='Recent':
      filepath = pge_recentchooser.Dialog().get()
      if filepath!=None:
        self._parent.create_window(filepath)
#       fc = gtk.RecentChooserDialog("Recent Documents", self, None,(gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
#       if fc.run()==gtk.RESPONSE_ACCEPT:
#         filepath = fc.get_current_item().get_uri()[7::]
#         fc.destroy()
#         self._parent.create_window(filepath)
#       else:
#         fc.destroy()
    elif label=='About':
      self._parent.onAbout(self)
    elif label == 'Open':
      self.open_dialog()
    elif label == 'Help':
      self.show_help()
    elif label == 'Settings':
      prefs = pge_preferences.Prefs()
      prefs.load()
      prefs.edit(self)
    else:
      print "Menu Button clicked: %s" % label

  def create_menu(self):
    menu = hildon.AppMenu()
    #New
    self.new_menu_button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
    label = 'New'
    self.new_menu_button.set_label(label)
    self.new_menu_button.connect("clicked", self.menu_button_clicked, label)
    menu.append(self.new_menu_button)
    self.open_menu_button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
    label = 'Open'
    self.open_menu_button.set_label(label)
    self.open_menu_button.connect("clicked", self.menu_button_clicked, label)
    menu.append(self.open_menu_button)
    self.saveas_menu_button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
    self.recent_menu_button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
    label = 'Recent'
    self.recent_menu_button.set_label(label)
    self.recent_menu_button.connect("clicked", self.menu_button_clicked, label)
    menu.append(self.recent_menu_button)
#    label = 'Save as'
#    self.saveas_menu_button.set_label(label)
#    self.saveas_menu_button.connect("clicked", self.menu_button_clicked, label)
#    menu.append(self.saveas_menu_button)
    self.about_menu_button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
    label = 'About'
    self.about_menu_button.set_label(label)
    self.about_menu_button.connect("clicked", self.menu_button_clicked, label)
    menu.append(self.about_menu_button)
    self.settings_menu_button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
    label = 'Settings'
    self.settings_menu_button.set_label(label)
    self.settings_menu_button.connect("clicked", self.menu_button_clicked, label)
    menu.append(self.settings_menu_button)
    self.help_menu_button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
    label = 'Help'
    self.help_menu_button.set_label(label)
    self.help_menu_button.connect("clicked", self.menu_button_clicked, label)
    menu.append(self.help_menu_button)

    return menu

  def open_dialog(self,*data):
      fc = gobject.new(hildon.FileChooserDialog, action=gtk.FILE_CHOOSER_ACTION_OPEN)
      fc.set_property('show-files',True)
      fc.set_current_folder(self._parent._last_opened_folder)
      if fc.run()==gtk.RESPONSE_OK:
        filepath = fc.get_filename()
        fc.destroy()
        self._parent._last_opened_folder = os.path.dirname(filepath)
        self._parent.create_window(filepath)
      else:
        fc.destroy()

  def show_help(self):
    import pge_help
    pge_help.Help()

  def on_key_press(self, widget, event, *args):
    if (event.state==gtk.gdk.CONTROL_MASK):
      #Open : CTRL-O
      if (event.keyval == gtk.keysyms.o):
        self.open_dialog()
      #Close : CTRL-W
      elif (event.keyval == gtk.keysyms.w):
        self.destroy()
      #Show Help
      elif (event.keyval == gtk.keysyms.h):
        self.show_help()

  def apply_prefs(self,prefs=None,theme=None):
    self.prefs = prefs
    #rotate
    if self.prefs.prefs_dict['auto_rotate']==True:
      self.rotation.set_mode(FremantleRotation.AUTOMATIC)
#      FremantleRotation('net.khertan.pygtkeditor',self,mode=FremantleRotation.AUTOMATIC)
    else:
      self.rotation.set_mode(FremantleRotation.NEVER)