def __init__(self): utils.get_user_config() Control.regex_patterns = History.load_history_from_file(utils.get_search_history_path()) Control.file_history = History.load_history_from_file(utils.get_file_history_path()) self.stored_items = NoteList() self.event_dict_commandline = dict({ gtk.keysyms.Escape: self.event_focus_list, gtk.keysyms.Return: self.event_execute_command, gtk.keysyms.Up: self.event_prev_search_regex, gtk.keysyms.Down: self.event_next_search_regex, gtk.keysyms.Home: self.event_commandline_home, }) self.event_dict_list = dict({ # List to commandline #gtk.keysyms.colon: self.event_focus_commandline_command_mode, gtk.keysyms.slash: self.event_focus_commandline_search_mode, gtk.keysyms.f: self.event_focus_commandline_search_mode, gtk.keysyms.numbersign: self.event_focus_commandline_search_mode, # List to editor gtk.keysyms.Escape: self.event_reset_search, #gtk.keysyms.Tab: self.event_toggle_view, gtk.keysyms.a: self.event_add_new_note, # List #gtk.keysyms.q: self.event_destroy, gtk.keysyms.j: self.event_select_next, gtk.keysyms.k: self.event_select_prev, gtk.keysyms.G: self.event_select_last, gtk.keysyms.M: self.event_select_middle_visible, gtk.keysyms.L: self.event_select_last_visible, gtk.keysyms.H: self.event_select_first_visible, gtk.keysyms.g: self.event_select_first, gtk.keysyms.n: self.event_next_tag_mode, gtk.keysyms.p: self.event_prev_tag_mode, gtk.keysyms.F3: self.event_edit_config, gtk.keysyms.F5: self.event_reload_config, }) self.event_dict_editor = dict({ gtk.keysyms.Escape: self.event_switch_to_list_view, gtk.keysyms.z: self.event_undo, gtk.keysyms.r: self.event_redo, gtk.keysyms.s: self.event_save, gtk.keysyms.d: self.event_insert_date, gtk.keysyms.F4: self.event_delete_note, #gtk.keysyms.b: self.event_bold, })
def main(self, stdscr, notes_root, recursive): utils.get_user_config() Control.regex_patterns = History.load_history_from_file(utils.get_search_history_path()) Control.file_history = History.load_history_from_file(utils.get_file_history_path()) if not os.path.exists(notes_root): curses_view.end_curses() print('No such directory "%s"' % notes_root) exit() self.stored_items = NoteList() self.recursive = recursive self.notes_root = notes_root Control.reload_notes = True self.is_searching = False # TODO convert this into a TixMode instead # {{{ funcs for key events def keypress_list_pageup(): Control.reload_notes = False if TixMode.current == TixMode.LIST: curses_view.list_scroll_top -= curses_view.get_list_capacity() Control.list_visible_index -= curses_view.get_list_capacity() curses_view.adjust_scroll(len(self.stored_items)) elif TixMode.current == TixMode.TAGS: curses_view.tags_scroll_top -= curses_view.get_list_capacity() Control.tags_visible_index -= curses_view.get_list_capacity() curses_view.adjust_scroll(len(self.stored_items.modes())) def keypress_list_pagedown(): Control.reload_notes = False if TixMode.current == TixMode.LIST: # TODO don't scroll if last item is in view curses_view.list_scroll_top += curses_view.get_list_capacity() Control.list_visible_index += curses_view.get_list_capacity() curses_view.adjust_scroll(len(self.stored_items)) elif TixMode.current == TixMode.TAGS: curses_view.tags_scroll_top += curses_view.get_list_capacity() Control.tags_visible_index += curses_view.get_list_capacity() curses_view.adjust_scroll(len(self.stored_items.modes())) def keypress_select_first_in_view(): Control.reload_notes = False if TixMode.current == TixMode.LIST: i = Control.list_visible_index Control.list_visible_index = curses_view.list_scroll_top if i != Control.list_visible_index: curses_view.adjust_scroll(len(self.stored_items)) elif TixMode.current == TixMode.TAGS: i = Control.tags_visible_index Control.tags_visible_index = curses_view.tags_scroll_top if i != Control.tags_visible_index: curses_view.adjust_scroll(len(self.stored_items.modes())) def keypress_select_last_in_view(): Control.reload_notes = False if TixMode.current == TixMode.LIST: i = Control.list_visible_index Control.list_visible_index = curses_view.list_scroll_top + curses_view.get_list_capacity() - 1 if i != Control.list_visible_index: curses_view.adjust_scroll(len(self.stored_items)) elif TixMode.current == TixMode.TAGS: i = Control.tags_visible_index Control.tags_visible_index = curses_view.tags_scroll_top + curses_view.get_list_capacity() - 1 if i != Control.tags_visible_index: curses_view.adjust_scroll(len(self.stored_items.modes())) def keypress_select_middle_in_view(): Control.reload_notes = False i_after = i_before = nbr_items = scroll_top = 0 max_items_in_screen = curses_view.get_list_capacity() - 1 if TixMode.current == TixMode.LIST: i_after = i_before = Control.list_visible_index scroll_top = curses_view.list_scroll_top nbr_items = len(self.stored_items) elif TixMode.current == TixMode.TAGS: i_after = i_before = Control.tags_visible_index scroll_top = curses_view.tags_scroll_top nbr_items = len(self.stored_items.modes()) nbr_items_visible = min(nbr_items, nbr_items - scroll_top, max_items_in_screen) i_after = scroll_top + nbr_items_visible / 2 if TixMode.current == TixMode.LIST: Control.list_visible_index = i_after elif TixMode.current == TixMode.TAGS: Control.tags_visible_index = i_after if i_before != i_after: curses_view.adjust_scroll(nbr_items) def keypress_select_next(): Control.reload_notes = False if TixMode.current == TixMode.LIST: Control.list_visible_index += 1 curses_view.adjust_scroll(len(self.stored_items)) elif TixMode.current == TixMode.TAGS: Control.tags_visible_index += 1 nbr_modes = len(self.stored_items.modes()) curses_view.adjust_scroll(nbr_modes) def keypress_select_prev(): Control.reload_notes = False if TixMode.current == TixMode.LIST: Control.list_visible_index -= 1 curses_view.adjust_scroll(len(self.stored_items)) elif TixMode.current == TixMode.TAGS: Control.tags_visible_index -= 1 curses_view.adjust_scroll(len(self.stored_items.modes())) def keypress_select_last(): Control.reload_notes = False if TixMode.current == TixMode.LIST: Control.list_visible_index = len(self.stored_items) - 1 curses_view.adjust_scroll(len(self.stored_items)) elif TixMode.current == TixMode.TAGS: Control.tags_visible_index = len(self.stored_items.modes()) - 1 curses_view.adjust_scroll(len(self.stored_items.modes())) def keypress_select_first(): Control.reload_notes = False if TixMode.current == TixMode.LIST: Control.list_visible_index = 0 curses_view.list_scroll_top = 0 elif TixMode.current == TixMode.TAGS: Control.tags_visible_index = 0 curses_view.tags_scroll_top = 0 def keypress_change_sorting_order(): Control.reload_notes = False if TixMode.current == TixMode.LIST: SortMode.current = (SortMode.current + 1) % len(SortMode.OPTIONS) if SortMode.current == SortMode.BY_SHUF: SortMode.current = (SortMode.current + 1) % len(SortMode.OPTIONS) item_id = self.stored_items.get_visible(Control.list_visible_index).id if SortMode.current == SortMode.BY_DATE: self.stored_items.sort_by_modification_date() #self.stored_items.group_todo() elif SortMode.current == SortMode.BY_FILENAME: self.stored_items.sort_by_filename() new_index = 0 for i, item in enumerate(self.stored_items): if item.id == item_id: new_index = i break Control.list_visible_index = new_index def keypress_shuf_order(): Control.reload_notes = False if TixMode.current == TixMode.LIST: SortMode.current = SortMode.BY_SHUF item_id = self.stored_items.get_visible(Control.list_visible_index).id random.shuffle(self.stored_items) #self.stored_items.group_todo() new_index = 0 for i, item in enumerate(self.stored_items): if item.id == item_id: new_index = i break Control.list_visible_index = new_index def keypress_insert(): Control.reload_notes = False if TixMode.current == TixMode.LIST: curses_view.end_curses() list_modes = self.stored_items.modes() current_mode = list_modes[UserMode.current] if current_mode in (UserMode.ALL, UserMode.NOTAG): current_mode = None else: current_mode = current_mode + "\n\n" n = utils.new_note(current_mode) curses_view.init_curses() if n: Control.list_visible_index = 0 curses_view.adjust_scroll(len(self.stored_items)) Control.reload_notes = True def keypress_toggle_filename_view(): Control.reload_notes = False if TixMode.current == TixMode.LIST: Control.list_view_mode += 1 Control.list_view_mode = Control.list_view_mode % len(Control.LIST_VIEWS) def keypress_cycle_modes_reverse(): keypress_cycle_modes(True) def keypress_cycle_modes(reverse=False): Control.reload_notes = False if TixMode.current == TixMode.LIST: list_modes = self.stored_items.modes() nbr_modes = len(list_modes) if nbr_modes == 0: return addition = 1 if not reverse else -1 UserMode.current = (UserMode.current + addition) % nbr_modes for note in self.stored_items: if list_modes[UserMode.current] == UserMode.ALL or list_modes[UserMode.current] in note.modes: note.visible(True) elif list_modes[UserMode.current] == UserMode.NOTAG and not note.modes: note.visible(True) else: note.visible(False) def keypress_read(): Control.reload_notes = False if TixMode.current == TixMode.LIST: curses_view.end_curses() filename = self.stored_items.get_visible(Control.list_visible_index).fullpath() utils.open_file_in_reader(filename) curses_view.init_curses() def keypress_enter(): if TixMode.current == TixMode.LIST and len(self.stored_items) > 0: curses_view.end_curses() note_before = self.stored_items.get_visible(Control.list_visible_index) note_after = note_before.edit() curses_view.init_curses() if note_before.text != note_after.text: Control.reload_notes = True Control.list_visible_index = 0 h = History(note_after.fullpath()) Control.file_history.append(h) h.append_to_file(utils.get_file_history_path()) else: Control.reload_notes = False elif TixMode.current == TixMode.TAGS: Control.reload_notes = False TixMode.current = TixMode.LIST if UserMode.current == Control.tags_visible_index: return else: UserMode.current = Control.tags_visible_index Control.list_visible_index = 0 list_modes = self.stored_items.modes() for note in self.stored_items: if list_modes[UserMode.current] == UserMode.ALL or list_modes[UserMode.current] in note.modes: note.visible(True) elif list_modes[UserMode.current] == UserMode.NOTAG and not note.modes: note.visible(True) else: note.visible(False) def keypress_cycle_tix_modes(): Control.reload_notes = False nbr_modes = len(TixMode.OPTIONS) if nbr_modes == 0: return reverse = False addition = 1 if not reverse else -1 TixMode.current = (TixMode.current + addition) % nbr_modes if TixMode.current == TixMode.EDIT: TixMode.current = (TixMode.current + addition) % nbr_modes def keypress_switch_to_list_mode(): Control.reload_notes = False TixMode.current = TixMode.LIST def keypress_switch_to_tags_mode(): Control.reload_notes = False if TixMode.current == TixMode.LIST: TixMode.current = TixMode.TAGS elif TixMode.current == TixMode.TAGS: keypress_enter() pass def pressed_slash(): Control.reload_notes = False if TixMode.current == TixMode.LIST: Control.reload_thread_lock.acquire() def validator(c): if c == 27: # ctrl A >then> ctrl K # regex = None return 7 elif c == 10: return 7 # RETURN key -- CTRL-g = 7 and CTRL-j = 10 else: curses_view.search_textbox.do_command(c) if c == curses.KEY_UP and Control.current_regex_index > 0: Control.current_regex_index -= 1 curses_view.footer_pad.clear() CursesView.add_str(curses_view.footer_pad, Control.regex_patterns[Control.current_regex_index].value) elif c == curses.KEY_DOWN and Control.current_regex_index < len(Control.regex_patterns) - 1: Control.current_regex_index += 1 curses_view.footer_pad.clear() CursesView.add_str(curses_view.footer_pad, Control.regex_patterns[Control.current_regex_index].value) elif c == curses.KEY_DOWN and Control.current_regex_index == len(Control.regex_patterns) - 1: Control.current_regex_index += 1 curses_view.footer_pad.clear() CursesView.add_str(curses_view.footer_pad, curses_view.search_prompt) if curses_view.search_textbox.gather() is '': return 7 regex = "" try: curses.curs_set(1) except curses.error: # iphone pass Control.current_regex_index = len(Control.regex_patterns) curses_view.footer_pad.clear() CursesView.add_str(curses_view.footer_pad, curses_view.search_prompt) self.is_searching = True regex = curses_view.search_textbox.edit(validator) self.is_searching = False try: curses.curs_set(0) except curses.error: # iphone pass if regex != None: regex = regex[len(curses_view.search_prompt):] if regex.strip(): h = History(curses_view.search_prompt + regex) h.append_to_file(utils.get_search_history_path()) Control.regex_patterns.append(h) Control.list_visible_index = 0 curses_view.adjust_scroll(len(self.stored_items)) list_modes = self.stored_items.modes() current_mode = list_modes[UserMode.current] for note in self.stored_items: if note.is_search_match(regex): if (current_mode == UserMode.ALL or current_mode in note.modes): note.visible(True) elif (current_mode == UserMode.NOTAG and not note.modes): note.visible(True) else: note.visible(False) Control.reload_thread_lock.release() key_to_action = dict({ ord('M'): keypress_select_middle_in_view, ord('H'): keypress_select_first_in_view, ord('L'): keypress_select_last_in_view, ord('j'): keypress_select_next, ord('k'): keypress_select_prev, #ord('h'): keypress_switch_to_list_mode, ord('h'): keypress_switch_to_tags_mode, ord('l'): keypress_switch_to_tags_mode, ord('g'): keypress_select_first, ord('G'): keypress_select_last, ord('a'): keypress_insert, ord('r'): keypress_read, ord('n'): keypress_cycle_modes, ord('p'): keypress_cycle_modes_reverse, ord('\t'): keypress_cycle_tix_modes, 9: keypress_cycle_tix_modes, # = TAB ord('#'): keypress_cycle_tix_modes, ord('@'): keypress_cycle_tix_modes, #353: keypress_cycle_modes_reverse, # = SHIFT + TAB ord('f'): keypress_toggle_filename_view, ord('s'): keypress_change_sorting_order, ord('S'): keypress_shuf_order, ord('/'): pressed_slash, 10: keypress_enter, curses.KEY_ENTER: keypress_enter, curses.KEY_DOWN: keypress_select_next, curses.KEY_UP: keypress_select_prev, curses.KEY_HOME: keypress_select_first, curses.KEY_END: keypress_select_last, curses.KEY_NPAGE: keypress_list_pagedown, curses.KEY_PPAGE: keypress_list_pageup, curses.KEY_LEFT: keypress_switch_to_list_mode, curses.KEY_RIGHT: keypress_switch_to_tags_mode, }) #}}} while True: curses_view.update_screen_size() curses_view.recalculate_widths() curses_view.create_footer_pad() if Control.reload_notes: if not Control.reload_thread_lock.locked(): t = self.Loader(self) t.start() elif not Control.reload_thread_lock.locked(): curses_view.complete_redraw(self.stored_items) Control.reload_notes = False c = curses_view.keyboard_pad.getch() if c == ord('q'): break f = key_to_action.get(c) if f: f() curses_view.end_curses()
def __init__(self): utils.get_user_config() Control.regex_patterns = History.load_history_from_file( utils.get_search_history_path()) Control.file_history = History.load_history_from_file( utils.get_file_history_path()) self.stored_items = NoteList() self.event_dict_commandline = dict({ gtk.keysyms.Escape: self.event_focus_list, gtk.keysyms.Return: self.event_execute_command, gtk.keysyms.Up: self.event_prev_search_regex, gtk.keysyms.Down: self.event_next_search_regex, gtk.keysyms.Home: self.event_commandline_home, }) self.event_dict_list = dict({ # List to commandline #gtk.keysyms.colon: self.event_focus_commandline_command_mode, gtk.keysyms.slash: self.event_focus_commandline_search_mode, gtk.keysyms.f: self.event_focus_commandline_search_mode, gtk.keysyms.numbersign: self.event_focus_commandline_search_mode, # List to editor gtk.keysyms.Escape: self.event_reset_search, #gtk.keysyms.Tab: self.event_toggle_view, gtk.keysyms.a: self.event_add_new_note, # List #gtk.keysyms.q: self.event_destroy, gtk.keysyms.j: self.event_select_next, gtk.keysyms.k: self.event_select_prev, gtk.keysyms.G: self.event_select_last, gtk.keysyms.M: self.event_select_middle_visible, gtk.keysyms.L: self.event_select_last_visible, gtk.keysyms.H: self.event_select_first_visible, gtk.keysyms.g: self.event_select_first, gtk.keysyms.n: self.event_next_tag_mode, gtk.keysyms.p: self.event_prev_tag_mode, gtk.keysyms.F3: self.event_edit_config, gtk.keysyms.F5: self.event_reload_config, }) self.event_dict_editor = dict({ gtk.keysyms.Escape: self.event_switch_to_list_view, gtk.keysyms.z: self.event_undo, gtk.keysyms.r: self.event_redo, gtk.keysyms.s: self.event_save, gtk.keysyms.d: self.event_insert_date, gtk.keysyms.F4: self.event_delete_note, #gtk.keysyms.b: self.event_bold, })
class GtkMain: def create_commandline(self): self.commandline = gtk.Entry(1024) self.commandline.connect('changed', self.keypress_reaction_commandline_changed) self.commandline.connect('key-press-event', self.keypress_reaction_commandline_keypressed) self.commandline.connect('focus-out-event', self.commandline_focus_out_event) self.commandline.set_editable(1) font_desc = pango.FontDescription('monospace') self.commandline.modify_font(font_desc) #self.commandline.set_has_frame(False) #self.commandline.set_activates_default(True) def create_editor(self): self.editor = Editor() self.editor.connect('key-press-event', self.keypress_reaction_editor) def create_list(self): self.tree_view = List(self.stored_items) self.tree_view.connect('row-activated', self.event_switch_to_edit_view) self.tree_view.connect('key-press-event', self.keypress_reaction_list) def create_statusbar(self): self.status_bar = StatusBar() self.status_bar.update("TIX") self.vbox.pack_end(self.status_bar, False, False, 0) self.status_bar.show() def create_window(self): self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.main_window.set_title("Tix") self.main_window.set_default_size(int(500 * 1.1), int(400 * 1.1)) self.main_window.set_border_width(10) #self.main_window.fullscreen() self.main_window.connect("delete-event", self.delete_event) self.main_window.connect("destroy", self.event_destroy, None) self.vbox = gtk.VBox(False, 1) self.main_window.add(self.vbox) #def create_toolbar(self): # toolbar = gtk.Toolbar() # toolbar.set_orientation(gtk.ORIENTATION_HORIZONTAL) # toolbar.set_tooltips(False) # toolbar.append_item("save", "save tooltip", "shhh... this is a private tooltip", None, self.event_undo, None) # toolbar.append_item("cancel", "cancel tooltip", "shhh... this is a private tooltip", None, self.event_redo, None) # return toolbar # {{{ Events def commandline_focus_out_event(self, widget, event, data=None): self.commandline.set_text("") def event_select_prev(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path, col = self.tree_view.get_cursor() if path: new_index = path[0] - 1 if new_index >= 0: self.tree_view.set_cursor(new_index) def event_select_next(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path, col = self.tree_view.get_cursor() if path: new_index = path[0] + 1 if new_index < len(self.tree_view.get_model()): self.tree_view.set_cursor(new_index) def event_reload_config(self, widget, event, data=None): utils.get_user_config() self.vbox.remove(self.tree_view.get_parent()) self.create_list() Control.reload_notes = True self.event_switch_to_list_view(None, None) def event_edit_config(self, widget, event, data=None): curr_note = Note('tix.cfg', utils.user_configurations['TIXPATH'], None) self.show_note_in_edit_mode(curr_note) def show_note_in_edit_mode(self, curr_note): TixMode.current = TixMode.EDIT self.editor.load_note(curr_note) self.status_bar.update('"%s"' % curr_note.fullpath()) self.vbox.remove(self.tree_view.get_parent()) self.vbox.add(self.editor.get_parent()) self.editor.grab_focus() self.main_window.show_all() def event_prev_tag_mode(self, widget, event, data=None): pass # TODO def event_next_tag_mode(self, widget, event, data=None): pass # TODO def event_select_first(self, widget, event, data=None): if TixMode.current == TixMode.LIST: self.tree_view.set_cursor(0) def event_select_last(self, widget, event, data=None): if TixMode.current == TixMode.LIST: s = len(self.tree_view.get_model()) if s > 0: self.tree_view.set_cursor(s - 1) def event_select_last_visible(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path_start, path_end = self.tree_view.get_visible_range() if path_end: new_index = path_end[0] self.tree_view.set_cursor(new_index) def event_select_first_visible(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path_start, path_end = self.tree_view.get_visible_range() if path_start: new_index = path_start[0] self.tree_view.set_cursor(new_index) def event_select_middle_visible(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path_start, path_end = self.tree_view.get_visible_range() if path_start and path_end: new_index = (path_start[0] + path_end[0]) / 2 self.tree_view.set_cursor(new_index) def event_add_new_note(self, widget, event, data=None): if TixMode.current == TixMode.LIST: TixMode.current = TixMode.EDIT #path, col = self.tree_view.get_cursor() #current_visible_index = path[0] self.editor.load_note(None) self.status_bar.update('(new file)') self.vbox.remove(self.tree_view.get_parent()) self.vbox.add(self.editor.get_parent()) self.editor.grab_focus() self.main_window.show_all() def event_execute_command(self, widget, event, data=None): regex = self.commandline.get_text() if len(regex) > 0: # Notice we're not striping '#' if regex[0] in ('/', '?'): regex = regex[1:] #if regex.strip(): h = History("/" + regex) h.append_to_file(utils.get_search_history_path()) Control.regex_patterns.append(h) Control.current_regex_index = len(Control.regex_patterns) nbr_visible = self.stored_items.filter() if nbr_visible == 1: curr_note = self.stored_items.get_visible(0) self.show_note_in_edit_mode(curr_note) else: # repopulate tree_view: self.vbox.remove(self.tree_view.get_parent()) self.create_list() Control.reload_notes = False self.event_switch_to_list_view(None, None) self.tree_view.grab_focus() def event_check_commandline_text(self, widget, data=None): if self.commandline.get_text_length() == 0: self.event_focus_list(None, None) def event_commandline_home(self, widget, event, data=None): l = self.commandline.get_text_length() if l > 0: self.commandline.set_position(1) widget.emit_stop_by_name("key-press-event") def event_next_search_regex(self, widget, event, data=None): # FIXME focus is grabbed by tree_view if Control.current_regex_index < len(Control.regex_patterns) - 1: Control.current_regex_index += 1 self.commandline.set_text(Control.regex_patterns[Control.current_regex_index].value) l = self.commandline.get_text_length() self.commandline.set_position(l) elif Control.current_regex_index == len(Control.regex_patterns) - 1: Control.current_regex_index += 1 self.commandline.set_text("/") l = self.commandline.get_text_length() self.commandline.set_position(l) return True #widget.emit_stop_by_name("key-press-event") def event_prev_search_regex(self, widget, event, data=None): # FIXME focus is grabbed by tree_view widget.emit_stop_by_name("key-press-event") if Control.current_regex_index > 0: Control.current_regex_index -= 1 self.commandline.set_text(Control.regex_patterns[Control.current_regex_index].value) l = self.commandline.get_text_length() self.commandline.set_position(l) return True #self.commandline.emit_stop_by_name("key-press-event") #widget.stop_emission("key-press-event") def event_reset_search(self, widget, event, data=None): Control.regex_patterns.append(History("/")) # see also: event_execute_command self.stored_items.filter() # repopulate tree_view: self.vbox.remove(self.tree_view.get_parent()) self.create_list() Control.reload_notes = False self.event_switch_to_list_view(None, None) self.commandline.set_text("") self.tree_view.grab_focus() def event_focus_list(self, widget, event, data=None): self.tree_view.grab_focus() def event_focus_commandline_search_mode(self, widget, event, data=None): if event.keyval == gtk.keysyms.numbersign \ or event.keyval == gtk.keysyms.slash \ or (event.keyval == gtk.keysyms.f and event.state == gtk.gdk.CONTROL_MASK): if event.keyval == gtk.keysyms.numbersign: self.commandline.set_text('/#') else: self.commandline.set_text('/') self.commandline.grab_focus() l = self.commandline.get_text_length() self.commandline.set_position(l) def event_focus_commandline_command_mode(self, widget, event, data=None): self.commandline.set_text(':') self.commandline.grab_focus() l = self.commandline.get_text_length() self.commandline.set_position(l) def event_toggle_view(self, widget, event, data=None): nbr_modes = len(TixMode.OPTIONS) reverse = False addition = 1 if not reverse else -1 TixMode.current = (TixMode.current + addition) % nbr_modes if TixMode.current == TixMode.TAGS: TixMode.current = (TixMode.current + addition) % nbr_modes if TixMode.current == TixMode.EDIT: self.event_switch_to_edit_view(None, None) elif TixMode.current == TixMode.LIST: self.event_switch_to_list_view(None, None) def event_switch_to_edit_view(self, widget, event, data=None): path, col = self.tree_view.get_cursor() if path: current_visible_index = path[0] curr_note = self.stored_items.get_visible(current_visible_index) self.show_note_in_edit_mode(curr_note) def event_switch_to_list_view(self, widget, event, data=None): TixMode.current = TixMode.LIST self.status_bar.update("- Search: %s" % Control.get_last_regex() if Control.get_last_regex() else "") if Control.reload_notes: self.stored_items.load(self.notes_root, self.recursive) self.create_list() Control.reload_notes = False self.vbox.remove(self.editor.get_parent()) self.vbox.pack_start(self.commandline, False, False, 0) self.vbox.add(self.tree_view.get_parent()) self.tree_view.grab_focus() self.main_window.show_all() def event_delete_note(self, widget, event, data=None): if event.state == 0: # no gtk.gdk.CONTROL_MASK or MOD1_MASK if self.editor.delete_current_file(): Control.reload_notes = True self.event_switch_to_list_view(None, None) def event_destroy(self, widget, event, data=None): gtk.main_quit() #if TixMode.current == TixMode.LIST: # gtk.main_quit() #else: # self.event_switch_to_list_view(None, None) def delete_event(self, widget, event, data=None): gtk.main_quit() return False #if TixMode.current == TixMode.LIST: # gtk.main_quit() # return False #else: # self.event_switch_to_list_view(None, None) # return True def event_insert_date(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: self.editor.insert_date() def event_save(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: Control.reload_notes = True n = self.editor.save() self.status_bar.update('"%s"' % n.fullpath()) h = History(n.fullpath()) Control.file_history.append(h) h.append_to_file(utils.get_file_history_path()) #def event_bold(self, widget, event, data=None): # if TixMode.current == TixMode.EDIT: # if event.state == gtk.gdk.CONTROL_MASK: # self.editor.make_bold() def event_undo(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: self.editor.undo() def event_redo(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: self.editor.redo() # }}} def keypress_reaction_list(self, widget, event, data=None): try: f = self.event_dict_list[event.keyval] f(widget, event, data) except KeyError: pass def keypress_reaction_commandline_changed(self, widget, data=None): self.event_check_commandline_text(widget, data) def keypress_reaction_commandline_keypressed(self, widget, event=None, data=None): #if self.commandline.get_position() < 1: # l = self.commandline.get_text_length() # if l > 0: # self.commandline.set_position(1) try: f = self.event_dict_commandline[event.keyval] f(widget, event, data) except KeyError: pass def keypress_reaction_editor(self, widget, event, data=None): try: f = self.event_dict_editor[event.keyval] f(widget, event, data) except KeyError: pass def __init__(self): utils.get_user_config() Control.regex_patterns = History.load_history_from_file(utils.get_search_history_path()) Control.file_history = History.load_history_from_file(utils.get_file_history_path()) self.stored_items = NoteList() self.event_dict_commandline = dict({ gtk.keysyms.Escape: self.event_focus_list, gtk.keysyms.Return: self.event_execute_command, gtk.keysyms.Up: self.event_prev_search_regex, gtk.keysyms.Down: self.event_next_search_regex, gtk.keysyms.Home: self.event_commandline_home, }) self.event_dict_list = dict({ # List to commandline #gtk.keysyms.colon: self.event_focus_commandline_command_mode, gtk.keysyms.slash: self.event_focus_commandline_search_mode, gtk.keysyms.f: self.event_focus_commandline_search_mode, gtk.keysyms.numbersign: self.event_focus_commandline_search_mode, # List to editor gtk.keysyms.Escape: self.event_reset_search, #gtk.keysyms.Tab: self.event_toggle_view, gtk.keysyms.a: self.event_add_new_note, # List #gtk.keysyms.q: self.event_destroy, gtk.keysyms.j: self.event_select_next, gtk.keysyms.k: self.event_select_prev, gtk.keysyms.G: self.event_select_last, gtk.keysyms.M: self.event_select_middle_visible, gtk.keysyms.L: self.event_select_last_visible, gtk.keysyms.H: self.event_select_first_visible, gtk.keysyms.g: self.event_select_first, gtk.keysyms.n: self.event_next_tag_mode, gtk.keysyms.p: self.event_prev_tag_mode, gtk.keysyms.F3: self.event_edit_config, gtk.keysyms.F5: self.event_reload_config, }) self.event_dict_editor = dict({ gtk.keysyms.Escape: self.event_switch_to_list_view, gtk.keysyms.z: self.event_undo, gtk.keysyms.r: self.event_redo, gtk.keysyms.s: self.event_save, gtk.keysyms.d: self.event_insert_date, gtk.keysyms.F4: self.event_delete_note, #gtk.keysyms.b: self.event_bold, }) def main(self, notes_root, recursive): self.notes_root = notes_root self.recursive = recursive self.stored_items.load(self.notes_root, self.recursive) Control.reload_notes = True self.is_searching = False self.create_window() self.create_list() self.create_editor() self.create_statusbar() self.create_commandline() self.event_switch_to_list_view(None, None, None) gtk.main()
class GtkMain: def create_commandline(self): self.commandline = gtk.Entry(1024) self.commandline.connect('changed', self.keypress_reaction_commandline_changed) self.commandline.connect('key-press-event', self.keypress_reaction_commandline_keypressed) self.commandline.connect('focus-out-event', self.commandline_focus_out_event) self.commandline.set_editable(1) font_desc = pango.FontDescription('monospace') self.commandline.modify_font(font_desc) #self.commandline.set_has_frame(False) #self.commandline.set_activates_default(True) def create_editor(self): self.editor = Editor() self.editor.connect('key-press-event', self.keypress_reaction_editor) def create_list(self): self.tree_view = List(self.stored_items) self.tree_view.connect('row-activated', self.event_switch_to_edit_view) self.tree_view.connect('key-press-event', self.keypress_reaction_list) def create_statusbar(self): self.status_bar = StatusBar() self.status_bar.update("TIX") self.vbox.pack_end(self.status_bar, False, False, 0) self.status_bar.show() def create_window(self): self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.main_window.set_title("Tix") self.main_window.set_default_size(int(500 * 1.1), int(400 * 1.1)) self.main_window.set_border_width(10) #self.main_window.fullscreen() self.main_window.connect("delete-event", self.delete_event) self.main_window.connect("destroy", self.event_destroy, None) self.vbox = gtk.VBox(False, 1) self.main_window.add(self.vbox) #def create_toolbar(self): # toolbar = gtk.Toolbar() # toolbar.set_orientation(gtk.ORIENTATION_HORIZONTAL) # toolbar.set_tooltips(False) # toolbar.append_item("save", "save tooltip", "shhh... this is a private tooltip", None, self.event_undo, None) # toolbar.append_item("cancel", "cancel tooltip", "shhh... this is a private tooltip", None, self.event_redo, None) # return toolbar # {{{ Events def commandline_focus_out_event(self, widget, event, data=None): self.commandline.set_text("") def event_select_prev(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path, col = self.tree_view.get_cursor() if path: new_index = path[0] - 1 if new_index >= 0: self.tree_view.set_cursor(new_index) def event_select_next(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path, col = self.tree_view.get_cursor() if path: new_index = path[0] + 1 if new_index < len(self.tree_view.get_model()): self.tree_view.set_cursor(new_index) def event_reload_config(self, widget, event, data=None): utils.get_user_config() self.vbox.remove(self.tree_view.get_parent()) self.create_list() Control.reload_notes = True self.event_switch_to_list_view(None, None) def event_edit_config(self, widget, event, data=None): curr_note = Note('tix.cfg', utils.user_configurations['TIXPATH'], None) self.show_note_in_edit_mode(curr_note) def show_note_in_edit_mode(self, curr_note): TixMode.current = TixMode.EDIT self.editor.load_note(curr_note) self.status_bar.update('"%s"' % curr_note.fullpath()) self.vbox.remove(self.tree_view.get_parent()) self.vbox.add(self.editor.get_parent()) self.editor.grab_focus() self.main_window.show_all() def event_prev_tag_mode(self, widget, event, data=None): pass # TODO def event_next_tag_mode(self, widget, event, data=None): pass # TODO def event_select_first(self, widget, event, data=None): if TixMode.current == TixMode.LIST: self.tree_view.set_cursor(0) def event_select_last(self, widget, event, data=None): if TixMode.current == TixMode.LIST: s = len(self.tree_view.get_model()) if s > 0: self.tree_view.set_cursor(s - 1) def event_select_last_visible(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path_start, path_end = self.tree_view.get_visible_range() if path_end: new_index = path_end[0] self.tree_view.set_cursor(new_index) def event_select_first_visible(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path_start, path_end = self.tree_view.get_visible_range() if path_start: new_index = path_start[0] self.tree_view.set_cursor(new_index) def event_select_middle_visible(self, widget, event, data=None): if TixMode.current == TixMode.LIST: path_start, path_end = self.tree_view.get_visible_range() if path_start and path_end: new_index = (path_start[0] + path_end[0]) / 2 self.tree_view.set_cursor(new_index) def event_add_new_note(self, widget, event, data=None): if TixMode.current == TixMode.LIST: TixMode.current = TixMode.EDIT #path, col = self.tree_view.get_cursor() #current_visible_index = path[0] self.editor.load_note(None) self.status_bar.update('(new file)') self.vbox.remove(self.tree_view.get_parent()) self.vbox.add(self.editor.get_parent()) self.editor.grab_focus() self.main_window.show_all() def event_execute_command(self, widget, event, data=None): regex = self.commandline.get_text() if len(regex) > 0: # Notice we're not striping '#' if regex[0] in ('/', '?'): regex = regex[1:] #if regex.strip(): h = History("/" + regex) h.append_to_file(utils.get_search_history_path()) Control.regex_patterns.append(h) Control.current_regex_index = len(Control.regex_patterns) nbr_visible = self.stored_items.filter() if nbr_visible == 1: curr_note = self.stored_items.get_visible(0) self.show_note_in_edit_mode(curr_note) else: # repopulate tree_view: self.vbox.remove(self.tree_view.get_parent()) self.create_list() Control.reload_notes = False self.event_switch_to_list_view(None, None) self.tree_view.grab_focus() def event_check_commandline_text(self, widget, data=None): if self.commandline.get_text_length() == 0: self.event_focus_list(None, None) def event_commandline_home(self, widget, event, data=None): l = self.commandline.get_text_length() if l > 0: self.commandline.set_position(1) widget.emit_stop_by_name("key-press-event") def event_next_search_regex(self, widget, event, data=None): # FIXME focus is grabbed by tree_view if Control.current_regex_index < len(Control.regex_patterns) - 1: Control.current_regex_index += 1 self.commandline.set_text( Control.regex_patterns[Control.current_regex_index].value) l = self.commandline.get_text_length() self.commandline.set_position(l) elif Control.current_regex_index == len(Control.regex_patterns) - 1: Control.current_regex_index += 1 self.commandline.set_text("/") l = self.commandline.get_text_length() self.commandline.set_position(l) return True #widget.emit_stop_by_name("key-press-event") def event_prev_search_regex(self, widget, event, data=None): # FIXME focus is grabbed by tree_view widget.emit_stop_by_name("key-press-event") if Control.current_regex_index > 0: Control.current_regex_index -= 1 self.commandline.set_text( Control.regex_patterns[Control.current_regex_index].value) l = self.commandline.get_text_length() self.commandline.set_position(l) return True #self.commandline.emit_stop_by_name("key-press-event") #widget.stop_emission("key-press-event") def event_reset_search(self, widget, event, data=None): Control.regex_patterns.append(History("/")) # see also: event_execute_command self.stored_items.filter() # repopulate tree_view: self.vbox.remove(self.tree_view.get_parent()) self.create_list() Control.reload_notes = False self.event_switch_to_list_view(None, None) self.commandline.set_text("") self.tree_view.grab_focus() def event_focus_list(self, widget, event, data=None): self.tree_view.grab_focus() def event_focus_commandline_search_mode(self, widget, event, data=None): if event.keyval == gtk.keysyms.numbersign \ or event.keyval == gtk.keysyms.slash \ or (event.keyval == gtk.keysyms.f and event.state == gtk.gdk.CONTROL_MASK): if event.keyval == gtk.keysyms.numbersign: self.commandline.set_text('/#') else: self.commandline.set_text('/') self.commandline.grab_focus() l = self.commandline.get_text_length() self.commandline.set_position(l) def event_focus_commandline_command_mode(self, widget, event, data=None): self.commandline.set_text(':') self.commandline.grab_focus() l = self.commandline.get_text_length() self.commandline.set_position(l) def event_toggle_view(self, widget, event, data=None): nbr_modes = len(TixMode.OPTIONS) reverse = False addition = 1 if not reverse else -1 TixMode.current = (TixMode.current + addition) % nbr_modes if TixMode.current == TixMode.TAGS: TixMode.current = (TixMode.current + addition) % nbr_modes if TixMode.current == TixMode.EDIT: self.event_switch_to_edit_view(None, None) elif TixMode.current == TixMode.LIST: self.event_switch_to_list_view(None, None) def event_switch_to_edit_view(self, widget, event, data=None): path, col = self.tree_view.get_cursor() if path: current_visible_index = path[0] curr_note = self.stored_items.get_visible(current_visible_index) self.show_note_in_edit_mode(curr_note) def event_switch_to_list_view(self, widget, event, data=None): TixMode.current = TixMode.LIST self.status_bar.update( "- Search: %s" % Control.get_last_regex() if Control.get_last_regex() else "") if Control.reload_notes: self.stored_items.load(self.notes_root, self.recursive) self.create_list() Control.reload_notes = False self.vbox.remove(self.editor.get_parent()) self.vbox.pack_start(self.commandline, False, False, 0) self.vbox.add(self.tree_view.get_parent()) self.tree_view.grab_focus() self.main_window.show_all() def event_delete_note(self, widget, event, data=None): if event.state == 0: # no gtk.gdk.CONTROL_MASK or MOD1_MASK if self.editor.delete_current_file(): Control.reload_notes = True self.event_switch_to_list_view(None, None) def event_destroy(self, widget, event, data=None): gtk.main_quit() #if TixMode.current == TixMode.LIST: # gtk.main_quit() #else: # self.event_switch_to_list_view(None, None) def delete_event(self, widget, event, data=None): gtk.main_quit() return False #if TixMode.current == TixMode.LIST: # gtk.main_quit() # return False #else: # self.event_switch_to_list_view(None, None) # return True def event_insert_date(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: self.editor.insert_date() def event_save(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: Control.reload_notes = True n = self.editor.save() self.status_bar.update('"%s"' % n.fullpath()) h = History(n.fullpath()) Control.file_history.append(h) h.append_to_file(utils.get_file_history_path()) #def event_bold(self, widget, event, data=None): # if TixMode.current == TixMode.EDIT: # if event.state == gtk.gdk.CONTROL_MASK: # self.editor.make_bold() def event_undo(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: self.editor.undo() def event_redo(self, widget, event, data=None): if TixMode.current == TixMode.EDIT: if event.state == gtk.gdk.CONTROL_MASK: self.editor.redo() # }}} def keypress_reaction_list(self, widget, event, data=None): try: f = self.event_dict_list[event.keyval] f(widget, event, data) except KeyError: pass def keypress_reaction_commandline_changed(self, widget, data=None): self.event_check_commandline_text(widget, data) def keypress_reaction_commandline_keypressed(self, widget, event=None, data=None): #if self.commandline.get_position() < 1: # l = self.commandline.get_text_length() # if l > 0: # self.commandline.set_position(1) try: f = self.event_dict_commandline[event.keyval] f(widget, event, data) except KeyError: pass def keypress_reaction_editor(self, widget, event, data=None): try: f = self.event_dict_editor[event.keyval] f(widget, event, data) except KeyError: pass def __init__(self): utils.get_user_config() Control.regex_patterns = History.load_history_from_file( utils.get_search_history_path()) Control.file_history = History.load_history_from_file( utils.get_file_history_path()) self.stored_items = NoteList() self.event_dict_commandline = dict({ gtk.keysyms.Escape: self.event_focus_list, gtk.keysyms.Return: self.event_execute_command, gtk.keysyms.Up: self.event_prev_search_regex, gtk.keysyms.Down: self.event_next_search_regex, gtk.keysyms.Home: self.event_commandline_home, }) self.event_dict_list = dict({ # List to commandline #gtk.keysyms.colon: self.event_focus_commandline_command_mode, gtk.keysyms.slash: self.event_focus_commandline_search_mode, gtk.keysyms.f: self.event_focus_commandline_search_mode, gtk.keysyms.numbersign: self.event_focus_commandline_search_mode, # List to editor gtk.keysyms.Escape: self.event_reset_search, #gtk.keysyms.Tab: self.event_toggle_view, gtk.keysyms.a: self.event_add_new_note, # List #gtk.keysyms.q: self.event_destroy, gtk.keysyms.j: self.event_select_next, gtk.keysyms.k: self.event_select_prev, gtk.keysyms.G: self.event_select_last, gtk.keysyms.M: self.event_select_middle_visible, gtk.keysyms.L: self.event_select_last_visible, gtk.keysyms.H: self.event_select_first_visible, gtk.keysyms.g: self.event_select_first, gtk.keysyms.n: self.event_next_tag_mode, gtk.keysyms.p: self.event_prev_tag_mode, gtk.keysyms.F3: self.event_edit_config, gtk.keysyms.F5: self.event_reload_config, }) self.event_dict_editor = dict({ gtk.keysyms.Escape: self.event_switch_to_list_view, gtk.keysyms.z: self.event_undo, gtk.keysyms.r: self.event_redo, gtk.keysyms.s: self.event_save, gtk.keysyms.d: self.event_insert_date, gtk.keysyms.F4: self.event_delete_note, #gtk.keysyms.b: self.event_bold, }) def main(self, notes_root, recursive): self.notes_root = notes_root self.recursive = recursive self.stored_items.load(self.notes_root, self.recursive) Control.reload_notes = True self.is_searching = False self.create_window() self.create_list() self.create_editor() self.create_statusbar() self.create_commandline() self.event_switch_to_list_view(None, None, None) gtk.main()