message_textview.py 4.17 KB
Newer Older
1 2
##	message_textview.py
##
3
## Contributors for this file:
Yann Leboulanger's avatar
Yann Leboulanger committed
4
## - Yann Leboulanger <asterix@lagaule.org>
5 6
## - Nikos Kouremenos <kourem@gmail.com>
##
Yann Leboulanger's avatar
Yann Leboulanger committed
7
## Copyright (C) 2003-2004 Yann Leboulanger <asterix@lagaule.org>
8
##                         Vincent Hanquez <tab@snarc.org>
Yann Leboulanger's avatar
Yann Leboulanger committed
9
## Copyright (C) 2005 Yann Leboulanger <asterix@lagaule.org>
10
##                    Vincent Hanquez <tab@snarc.org>
11
##                    Nikos Kouremenos <kourem@gmail.com>
12 13 14
##                    Dimitur Kirov <dkirov@gmail.com>
##                    Travis Shirk <travis@pobox.com>
##                    Norman Rasmussen <norman@rasmussen.co.za>
15
##
16 17 18
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
19
## it under the terms of the GNU General Public License as published
20
## by the Free Software Foundation; version 3 only.
21
##
22
## Gajim is distributed in the hope that it will be useful,
23 24 25 26
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
27 28 29
## You should have received a copy of the GNU General Public License
## along with Gajim.  If not, see <http://www.gnu.org/licenses/>.
##
30 31 32 33 34

import gtk
import gobject

class MessageTextView(gtk.TextView):
nkour's avatar
nkour committed
35 36
	'''Class for the message textview (where user writes new messages)
	for chat/groupchat windows'''
37 38 39
	__gsignals__ = dict(
		mykeypress = (gobject.SIGNAL_RUN_LAST | gobject.SIGNAL_ACTION,
				None, # return value
40
				(int, gtk.gdk.ModifierType ) # arguments
41 42 43 44
			)
		)
		
	def __init__(self):
45 46
		gtk.TextView.__init__(self)
		
47 48 49 50 51
		# set properties
		self.set_border_width(1)
		self.set_accepts_tab(True)
		self.set_editable(True)
		self.set_cursor_visible(True)
52
		self.set_wrap_mode(gtk.WRAP_WORD_CHAR)
nkour's avatar
nkour committed
53 54 55 56
		self.set_left_margin(2)
		self.set_right_margin(2)
		self.set_pixels_above_lines(2)
		self.set_pixels_below_lines(2)
57

58 59
		self.lang = None # Lang used for spell checking

60 61 62
	def destroy(self):
		import gc
		gobject.idle_add(lambda:gc.collect())
63 64 65 66 67 68 69

	def clear(self, widget = None):
		'''clear text in the textview'''
		buffer = self.get_buffer()
		start, end = buffer.get_bounds()
		buffer.delete(start, end)

70 71 72

# We register depending on keysym and modifier some bindings
# but we also pass those as param so we can construct fake Event
nkour's avatar
nkour committed
73 74 75 76 77
# Here we register bindings for those combinations that there is NO DEFAULT
# action to be done by gtk TextView. In such case we should not add a binding
# as the default action comes first and our bindings is useless. In that case
# we catch and do stuff before default action in normal key_press_event
# and we also return True there to stop the default action from running
78 79 80 81 82 83 84 85 86 87

# CTRL + SHIFT + TAB
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.ISO_Left_Tab,
	gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.ISO_Left_Tab,
	gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)

# CTRL + TAB
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Tab, 
	gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Tab,
	gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)
88 89 90 91
	
# TAB
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Tab, 
	0, 'mykeypress', int, gtk.keysyms.Tab,	gtk.gdk.ModifierType, 0)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

# CTRL + UP
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Up, 
	gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Up,
	gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)

# CTRL + DOWN
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Down, 
	gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Down,
	gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)

# ENTER
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Return, 
	0, 'mykeypress', int, gtk.keysyms.Return,
	gtk.gdk.ModifierType, 0)

108
# Ctrl + Enter
109 110 111 112 113 114 115 116 117
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Return, 
	gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Return,
	gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)

# Keypad Enter
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.KP_Enter, 
	0, 'mykeypress', int, gtk.keysyms.KP_Enter,
	gtk.gdk.ModifierType, 0)

118
# Ctrl + Keypad Enter
119 120 121
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.KP_Enter, 
	gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.KP_Enter,
	gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)