82
81
# T: option for plugin preferences
83
82
('floating', 'bool', _('Show ToC as floating widget instead of in sidepane'), True),
84
83
# T: option for plugin preferences
85
#~ ('show_h1', 'bool', _('Show the page title heading in the ToC'), False),
84
('show_h1', 'bool', _('Show the page title heading in the ToC'), False),
86
85
# T: option for plugin preferences
88
87
# TODO disable pane setting if not embedded
90
def __init__(self, ui):
91
PluginClass.__init__(self, ui)
92
self.floating_widget = None
93
self.sidepane_widget = None
95
def finalize_notebook(self, ui):
96
self.do_preferences_changed()
99
self.disconnect_sidepane()
100
self.disconnect_floating()
101
PluginClass.destroy(self)
103
def do_preferences_changed(self):
104
if self.ui.ui_type != 'gtk':
89
def __init__(self, config=None):
90
PluginClass.__init__(self, config)
91
self.on_preferences_changed(self.preferences)
92
self.preferences.connect('changed', self.on_preferences_changed)
94
def on_preferences_changed(self, preferences):
95
if preferences['floating']:
96
self.set_extension_class('MainWindow', MainWindowExtensionFloating)
98
self.set_extension_class('MainWindow', MainWindowExtensionEmbedded)
101
@extends('MainWindow', autoload=False)
102
class MainWindowExtensionEmbedded(WindowExtension):
104
def __init__(self, plugin, window):
105
WindowExtension.__init__(self, plugin, window)
106
self.widget = SidePaneToC(self.window.ui, self.window.pageview) # XXX
108
self.on_preferences_changed(plugin.preferences)
109
self.connectto(plugin.preferences, 'changed', self.on_preferences_changed)
111
def on_preferences_changed(self, preferences):
112
if self.widget is None:
107
if self.preferences['floating']:
108
self.disconnect_sidepane()
109
self.connect_floating()
111
self.disconnect_floating()
112
self.connect_sidepane()
114
def connect_sidepane(self):
115
if not self.sidepane_widget:
116
self.sidepane_widget = SidePaneToC(self.ui)
118
self.ui.mainwindow.remove(self.sidepane_widget)
120
self.ui.mainwindow.add_tab(
121
_('ToC'), self.sidepane_widget, self.preferences['pane'])
116
self.window.remove(self.widget)
121
_('ToC'), self.widget, preferences['pane'])
122
122
# T: widget label
123
self.sidepane_widget.show_all()
125
def disconnect_sidepane(self):
126
if self.sidepane_widget:
127
self.ui.mainwindow.remove(self.sidepane_widget)
128
self.sidepane_widget.disconnect_all()
129
self.sidepane_widget.destroy()
130
self.sidepane_widget = None
132
def connect_floating(self):
133
if not self.floating_widget:
134
textview = self.ui.mainwindow.pageview.view
135
self.floating_widget = FloatingToC(self.ui, textview)
137
def disconnect_floating(self):
138
if self.floating_widget:
139
self.floating_widget.disconnect_all()
140
self.floating_widget.destroy()
141
self.floating_widget = None
123
self.widget.show_all()
125
self.widget.set_show_h1(preferences['show_h1'])
128
self.window.remove(self.widget)
129
self.widget.disconnect_all()
133
@extends('MainWindow', autoload=False)
134
class MainWindowExtensionFloating(WindowExtension):
136
def __init__(self, plugin, window):
137
WindowExtension.__init__(self, plugin, window)
138
self.widget = FloatingToC(self.window.ui, self.window.pageview) # XXX
140
self.on_preferences_changed(plugin.preferences)
141
self.connectto(plugin.preferences, 'changed', self.on_preferences_changed)
143
def on_preferences_changed(self, preferences):
144
self.widget.set_show_h1(preferences['show_h1'])
147
self.widget.disconnect_all()
148
self.widget.destroy()
187
198
stack.append((level, iter))
190
203
class ToCWidget(ConnectorMixin, gtk.ScrolledWindow):
192
def __init__(self, ui, ellipsis):
205
def __init__(self, ui, pageview, ellipsis, show_h1=False):
193
206
gtk.ScrolledWindow.__init__(self)
207
self.show_h1 = show_h1
198
209
self.treeview = ToCTreeView(ellipsis)
199
self.add(self.treeview)
201
210
self.treeview.connect('row-activated', self.on_heading_activated)
202
211
self.treeview.connect('populate-popup', self.on_populate_popup)
212
self.add(self.treeview)
214
# XXX remove ui - use signals from pageview for this
204
215
self.connectto(ui, 'open-page')
205
216
self.connectto(ui.notebook, 'stored-page')
207
self.on_open_page(ui, ui.page, Path(ui.page.name))
218
self.pageview = pageview
219
if self.pageview.page:
220
self.load_page(self.pageview.page)
222
def set_show_h1(self, show_h1):
223
if show_h1 != self.show_h1:
224
self.show_h1 = show_h1
225
if self.pageview.page:
226
self.load_page(self.pageview.page)
209
228
def on_open_page(self, ui, page, path):
211
self._load_page(page)
213
231
def on_stored_page(self, notebook, page):
214
if page == self.page:
215
self._load_page(page)
232
if page == self.pageview.page:
217
def _load_page(self, page):
235
def load_page(self, page):
218
236
model = self.treeview.get_model()
219
237
tree = page.get_parsetree()
241
model.populate(tree, self.show_h1)
224
242
self.treeview.expand_all()
226
244
def on_heading_activated(self, treeview, path, column):
359
385
for i in self._walk(model, child):
361
child = model.iter_next(iter)
387
child = model.iter_next(child)
363
389
def _format(self, path, level):
364
390
assert level > 0 and level < 7
365
391
if self.select_heading(path):
366
self.ui.mainwindow.pageview.toggle_format('h' + str(level))
392
self.pageview.toggle_format('h' + str(level))
369
395
class SidePaneToC(ToCWidget):
371
def __init__(self, ui):
372
ToCWidget.__init__(self, ui, ellipsis=True)
397
def __init__(self, ui, pageview):
398
ToCWidget.__init__(self, ui, pageview, ellipsis=True)
373
399
self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
374
400
self.set_shadow_type(gtk.SHADOW_IN)
375
401
self.set_size_request(-1, 200) # Fixed Height
461
485
# Need to wrap in event box to make widget visible
462
486
# probably because Containers normally don't have their own
463
487
# gdk window. So would paint directly on background window.
464
self.textview = textview
465
self._text_view_allocation = (textview.allocation.width, textview.allocation.height)
488
self.textview = pageview.view
489
self._text_view_allocation = (
490
self.textview.allocation.width,
491
self.textview.allocation.height
466
493
self._event_box = gtk.EventBox()
467
494
self._event_box.add(self)
469
textview.add_child_in_window(self._event_box, gtk.TEXT_WINDOW_WIDGET, 0, 0)
470
self.connectto(textview,
496
self.textview.add_child_in_window(self._event_box, gtk.TEXT_WINDOW_WIDGET, 0, 0)
497
self.connectto(self.textview,
472
499
handler=self.on_size_allocate_textview,