From 8d09fca35aca1aecdcb1e06205ad315215a4cc29 Mon Sep 17 00:00:00 2001
From: Aneesh Dogra <lionaneesh@gmail.com>
Date: Fri, 30 Nov 2012 18:20:32 +0530
Subject: [PATCH] Retain font-family and font-size. Fix #1690.
Note: self.abiword_canvas.get_selection('text/plain')[1] gives random values
if the file is empty. Therefore, I used len(self.abiword_canvas.get_content('text/plain')) instead to check for empty files.
---
AbiWordActivity.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++-----
font.cfg | 3 +++
toolbar.py | 4 ++--
widgets.py | 4 ----
4 files changed, 52 insertions(+), 11 deletions(-)
create mode 100644 font.cfg
diff --git a/AbiWordActivity.py b/AbiWordActivity.py
index b272f04..c86ab5e 100644
a
|
b
|
|
1 | 1 | # Copyright (C) 2006 by Martin Sevior |
2 | 2 | # Copyright (C) 2006-2007 Marc Maurer <uwog@uwog.net> |
3 | 3 | # Copyright (C) 2007, One Laptop Per Child |
| 4 | # Copyright (C) 2012, Aneesh Dogra <lionaneesh@gmail.com> |
4 | 5 | # |
5 | 6 | # This program is free software; you can redistribute it and/or modify |
6 | 7 | # it under the terms of the GNU General Public License as published by |
… |
… |
gobject.threads_init() |
27 | 28 | import gtk |
28 | 29 | import telepathy |
29 | 30 | import telepathy.client |
| 31 | import ConfigParser |
30 | 32 | |
31 | 33 | from abiword import Canvas |
32 | 34 | |
… |
… |
import speech |
53 | 55 | from speechtoolbar import SpeechToolbar |
54 | 56 | |
55 | 57 | logger = logging.getLogger('write-activity') |
| 58 | DEFAULT_CONFIG_PATH = os.path.join(get_bundle_path(), 'font.cfg') |
56 | 59 | |
57 | 60 | |
58 | 61 | class AbiWordActivity(activity.Activity): |
… |
… |
class AbiWordActivity(activity.Activity): |
60 | 63 | def __init__(self, handle): |
61 | 64 | activity.Activity.__init__(self, handle) |
62 | 65 | |
| 66 | # Defaults, only used when no configuration is present |
| 67 | self.font = 'Sans' |
| 68 | self.fontsize = '12' |
| 69 | |
| 70 | if os.path.exists('/etc/font.cfg'): |
| 71 | self.font, self.fontsize = \ |
| 72 | self._read_configuration('/etc/font.cfg') |
| 73 | elif os.path.exists(DEFAULT_CONFIG_PATH): |
| 74 | self.font, self.fontsize = \ |
| 75 | self._read_configuration(DEFAULT_CONFIG_PATH) |
| 76 | |
63 | 77 | # abiword uses the current directory for all its file dialogs |
64 | 78 | os.chdir(os.path.expanduser('~')) |
65 | 79 | |
… |
… |
class AbiWordActivity(activity.Activity): |
97 | 111 | text_toolbar.props.icon_name = 'format-text' |
98 | 112 | text_toolbar.props.label = _('Text') |
99 | 113 | toolbar_box.toolbar.insert(text_toolbar, -1) |
| 114 | fontsize_combo = text_toolbar.props.page.font_size_combo.combo |
| 115 | |
| 116 | for i, s in enumerate(fontsize_combo._font_sizes): |
| 117 | if s == self.fontsize: |
| 118 | logging.debug("Setting fontsize to: %s, Index: %d", s, i) |
| 119 | fontsize_combo.set_active(i) |
| 120 | break |
100 | 121 | |
101 | 122 | para_toolbar = ToolbarButton() |
102 | 123 | para_toolbar.props.page = ParagraphToolbar(self.abiword_canvas) |
… |
… |
class AbiWordActivity(activity.Activity): |
189 | 210 | self._buddy_joined_cb) |
190 | 211 | self._shared_activity.connect('buddy-left', self._buddy_left_cb) |
191 | 212 | if self.get_shared(): |
192 | | # # oh, OK, we've already joined |
| 213 | # oh, OK, we've already joined |
193 | 214 | self._joined_cb() |
194 | 215 | else: |
195 | 216 | # we are creating the activity |
196 | 217 | logger.error("We are creating an activity") |
197 | 218 | |
| 219 | def _read_configuration(self, filepath): |
| 220 | logging.debug('Reading configuration from file %s' % filepath) |
| 221 | fp = open(filepath) |
| 222 | config = ConfigParser.ConfigParser() |
| 223 | config.readfp(fp) |
| 224 | fp.close() |
| 225 | if (config.has_section('Config') and \ |
| 226 | config.has_option('Config', 'fontsize')): |
| 227 | fontsize = config.get('Config', 'fontsize') |
| 228 | else: |
| 229 | fontsize = self.fontsize |
| 230 | |
| 231 | if (config.has_section('Config') and \ |
| 232 | config.has_option('Config', 'font')): |
| 233 | font = config.get('Config', 'font') |
| 234 | else: |
| 235 | font = self.font |
| 236 | |
| 237 | return (font, fontsize) |
| 238 | |
198 | 239 | def __map_activity_event_cb(self, event, activity): |
199 | 240 | # set custom keybindings for Write |
200 | 241 | # we do it later because have problems if done before - OLPC #11049 |
… |
… |
class AbiWordActivity(activity.Activity): |
205 | 246 | keybindings_file, 0, 0) |
206 | 247 | # set default font |
207 | 248 | self.abiword_canvas.select_all() |
208 | | # get_selection return content and length |
209 | | if self.abiword_canvas.get_selection('text/plain')[1] == 0: |
210 | | logging.error('Setting default font to Sans in new documents') |
211 | | self.abiword_canvas.set_font_name('Sans') |
| 249 | |
| 250 | if len(self.abiword_canvas.get_content('text/plain')) == 0: |
| 251 | logging.debug('Setting default font to %s in new documents' % \ |
| 252 | self.font) |
| 253 | self.abiword_canvas.set_font_name(self.font) |
212 | 254 | self.abiword_canvas.moveto_bod() |
213 | 255 | |
214 | 256 | def get_preview(self): |
diff --git a/font.cfg b/font.cfg
new file mode 100644
index 0000000..2e85939
-
|
+
|
|
| 1 | [Config] |
| 2 | font = Sans |
| 3 | fontsize = 12 |
diff --git a/toolbar.py b/toolbar.py
index 49c15be..78b9377 100644
a
|
b
|
class TextToolbar(gtk.Toolbar): |
420 | 420 | self._font_family_cb) |
421 | 421 | self.insert(ToolComboBox(self.font_name_combo), -1) |
422 | 422 | |
423 | | font_size = ToolComboBox(FontSizeCombo(abiword_canvas)) |
424 | | self.insert(font_size, -1) |
| 423 | self.font_size_combo = ToolComboBox(FontSizeCombo(abiword_canvas)) |
| 424 | self.insert(self.font_size_combo, -1) |
425 | 425 | |
426 | 426 | bold = ToggleToolButton('format-text-bold') |
427 | 427 | bold.set_tooltip(_('Bold')) |
diff --git a/widgets.py b/widgets.py
index f12544a..c44d2a9 100644
a
|
b
|
class FontSizeCombo(ComboBox): |
98 | 98 | |
99 | 99 | def __init__(self, abi): |
100 | 100 | ComboBox.__init__(self) |
101 | | |
102 | 101 | self._abi_handler = abi.connect('font-size', self._font_size_cb) |
103 | | |
104 | 102 | self._font_sizes = ['8', '9', '10', '11', '12', '14', '16', '20', \ |
105 | 103 | '22', '24', '26', '28', '36', '48', '72'] |
106 | 104 | self._changed_id = self.connect('changed', self._font_size_changed_cb, |
… |
… |
class FontSizeCombo(ComboBox): |
108 | 106 | |
109 | 107 | for i, s in enumerate(self._font_sizes): |
110 | 108 | self.append_item(i, s, None) |
111 | | if s == '12': |
112 | | self.set_active(i) |
113 | 109 | |
114 | 110 | def _font_size_changed_cb(self, combobox, abi): |
115 | 111 | if self.get_active() != -1: |