1# -*- coding: utf-8 -*-
2
3# ####################################################################
4#  Copyright (C) 2005-2019 by the FIFE team
5#  http://www.fifengine.net
6#  This file is part of FIFE.
7#
8#  FIFE is free software; you can redistribute it and/or
9#  modify it under the terms of the GNU Lesser General Public
10#  License as published by the Free Software Foundation; either
11#  version 2.1 of the License, or (at your option) any later version.
12#
13#  This library is distributed in the hope that it will be useful,
14#  but WITHOUT ANY WARRANTY; without even the implied warranty of
15#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16#  Lesser General Public License for more details.
17#
18#  You should have received a copy of the GNU Lesser General Public
19#  License along with this library; if not, write to the
20#  Free Software Foundation, Inc.,
21#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22# ####################################################################
23
24from __future__ import print_function
25from __future__ import absolute_import
26from builtins import str
27from fife import fife
28from fife import fifechan
29from fife.extensions.pychan import tools
30from fife.extensions.pychan import events
31from fife.extensions.pychan.exceptions import *
32from fife.extensions.pychan.attrs import Attr,UnicodeAttr, PointAttr,ColorAttr,BoolAttr,IntAttr,FloatAttr,ListAttr
33from fife.extensions.pychan.properties import ColorProperty
34
35# These used to be defined in here, duplicating the definitions in .layout
36# Retain for backwards compatibility of any code importing them from .common
37from .layout import AlignTop, AlignBottom, AlignLeft, AlignRight, AlignCenter
38from .layout import isLayouted
39
40
41def get_manager():
42	from fife.extensions import pychan
43	return pychan.manager
44
45def text2gui(text):
46	"""
47	This function is applied to all text set on widgets.
48	It replaces tabs by four spaces.
49	It assumes the text to be a unicode object.
50	"""
51	try:
52		return text.encode("utf8",*get_manager().unicodePolicy).replace("\t"," "*4).replace("[br]","\n")
53	except TypeError:
54		return text.replace("\t"," "*4).replace("[br]","\n")
55
56def gui2text(text):
57	"""
58	This function is applied to all text get from widgets.
59	Translates the encoded string into a unicode object.
60	"""
61	try:
62		return str(text,"utf8",*get_manager().unicodePolicy)
63	except TypeError:
64		return text
65
66def gui2str(text):
67	"""
68	This function returns an 8-bit representation of the
69	unicode string. This is useful for passing strings
70	to SWIG functions.
71	"""
72	try:
73		return text.__str__()
74	except:
75		# String contains non-ascii characters
76		return text.encode("utf-8")
77