Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* Removed code relating to having differing documents in the split vi…

…ew since Gedit 3 already has tab groups.

* Added GPLv2 header.
  • Loading branch information...
commit a4482069396193b52b64f0c393017e2d3f668e2c 1 parent 797cd64
Jono authored

Showing 2 changed files with 27 additions and 396 deletions. Show diff stats Hide diff stats

  1. +1 2  SplitView.plugin
  2. +26 394 SplitView.py
3  SplitView.plugin
@@ -3,8 +3,7 @@ Loader=python
3 3 Module=SplitView
4 4 IAge=3
5 5 Name=Split View
6   -Description=Create a split view of a document
  6 +Description=Show a split view of a single document
7 7 Authors=Mike Doty <mike@psyguygames.com>, Jono Finger <jono@foodnotblogs.com>
8 8 Copyright=© Mike Doty 2010, © Jono Finger 2012
9 9 Website=https://github.com/jonocodes/GeditSplitView
10   -
420 SplitView.py
... ... @@ -1,16 +1,28 @@
1   -# Released in May 2010 by Mike Doty
2   -# www.psyguygames.com
  1 +#!/usr/bin/python
3 2 #
4   -# License? What license? Sure...
  3 +# Gedit Split View
  4 +# https://github.com/jonocodes/GeditSplitView
5 5 #
6   -# I'm releasing this under the "The I Really Could Care Less About You Public License." Read it here: http://www.revinc.org/pages/license
7   -
  6 +# Copyright (C) Mike Doty 2010 and Jono Finger 2012 <jono@foodnotblogs.com>
  7 +#
  8 +# The program is free software: you can redistribute it and/or modify it
  9 +# under the terms of the GNU General Public License as published by the
  10 +# Free Software Foundation, either version 2 of the License, or
  11 +# (at your option) any later version.
  12 +#
  13 +# The program is distributed in the hope that it will be useful, but
  14 +# WITHOUT ANY WARRANTY; without even the implied warranty of
  15 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16 +# See the GNU General Public License for more details.
  17 +#
  18 +# You should have received a copy of the GNU General Public License along
  19 +# with this program. If not, see <http://www.gnu.org/licenses/>.
8 20
9 21 import os
10 22 import time
11 23 import urllib
12 24
13   -from gi.repository import Gtk, Gedit, GObject, Gio
  25 +from gi.repository import Gtk, Gedit, GObject
14 26
15 27 ui_string = """<ui>
16 28 <menubar name="MenuBar">
@@ -28,11 +40,6 @@ def __init__(self, plugin, window):
28 40 self.window = window
29 41 self.plugin = plugin
30 42
31   - self.ui_popup_id = None
32   -
33   - self.popup_action = None
34   - self.temp_actions = []
35   -
36 43 self.document_list = []
37 44
38 45 self.ui_id = None
@@ -40,28 +47,19 @@ def __init__(self, plugin, window):
40 47 # Add a "toggle split view" item to the View menu
41 48 self.insert_menu_item(window)
42 49
43   - # We're going to keep track of each tab's split view
44   - # and, if used, ALT view -- the view of a separate
45   - # document -- with a couple of dictionaries. We'll
  50 + # We're going to keep track of each tab's split view. We'll
46 51 # index each dictionary via the tab objects.
47 52 self.split_views = {}
48   - self.alt_views = {}
49 53
50 54 self.tabs_already_using_splitview = []
51 55
52   - # This keeps track of whether the user is viewing an ALT document.
53   - self.same_document = {}
54   -
55 56 # I hardly even know how this works, but it gets our encoding.
56 57 try: self.encoding = Gedit.encoding_get_current()
57 58 except: self.encoding = Gedit.gedit_encoding_get_current()
58   -
59   - self.idle_id = GObject.timeout_add(100, self.monitor_documents)
60 59
61   - def deactivate(self):
  60 + def deactivate(self):
  61 + print ("deactivate")
62 62 self.remove_menu_item()
63   -
64   - GObject.source_remove(self.idle_id)
65 63
66 64 self.window = None
67 65 self.plugin = None
@@ -69,75 +67,20 @@ def deactivate(self):
69 67 def update_ui(self):
70 68 return
71 69
72   - def monitor_documents(self):
73   -
74   - self.refresh_popup_submenu()
75   -
76   - return True
77   -
78   -
79 70 def toggle_split_view(self, unused):
80   - # Get the current tab...
81   - current_tab = self.window.get_active_tab()
82 71
83   - # Don't allow splitview of an unsaved document...
84   - if (self.window.get_active_document().is_untitled()):
85   - self.show_error_dialog()
86   - return # Can't split view on an unsaved document
  72 + current_tab = self.window.get_active_tab()
87 73
88   - # If we already have a split view defined for this tab,
89   - # then clearly the user wants to end the split view.
90 74 if (current_tab in self.split_views):
91 75 self.end_split_view(None)
92   -
93   - # Otherwise, let's start a split view!
94 76 else:
95 77 self.split_view(None)
96 78
97   - def build_popup_menu(self):
98   -
99   - # TEST
100   - manager = self.window.get_ui_manager()
101   -
102   - if (self.ui_popup_id):
103   - manager.remove_ui(self.ui_popup_id)
104   - self.ui_popup_id = None
105   -
106   - self.action_group.remove_action(self.popup_action)
107   -
108   - self.ui_popup_id = manager.new_merge_id()
109   -
110   - action_name = "splitview_popup_submenu"
111   - path = "/NotebookPopup/NotebookPupupOps_1"
112   -
113   - manager.add_ui(merge_id = self.ui_popup_id,
114   - path = path,
115   - name = action_name,
116   - action = action_name,
117   - type = Gtk.UIManagerItemType.MENU,
118   - top = False)
119   -
120   - self.popup_action = Gtk.Action(name = action_name, label = "Quick Splitview", tooltip = "Choose another document to view...", stock_id = None)
121   - self.popup_action.set_visible(True)
122   -
123   - self.action_group.add_action(self.popup_action)
124   -
125   - def destroy_popup_menu(self):
126   -
127   - manager = self.window.get_ui_manager()
128   -
129   - if (self.ui_popup_id):
130   - manager.remove_ui(self.ui_popup_id)
131   - self.ui_popup_id = None
132   -
133   - self.action_group.remove_action(self.popup_action)
134   -
135   - for each in self.temp_actions:
136   - self.action_group.remove_action(each)
137   -
138 79 # This function creates the split view.
139 80 def split_view(self, whatever = None, direction = "vertical", changing = False):
140 81
  82 + print ('split_view')
  83 +
141 84 # Get the tab / document
142 85 current_tab = self.window.get_active_tab()
143 86 current_document = self.window.get_active_document()
@@ -150,7 +93,6 @@ def split_view(self, whatever = None, direction = "vertical", changing = False):
150 93 else:
151 94 self.tabs_already_using_splitview.append( current_tab )
152 95
153   -
154 96 old_other_view = None
155 97 if (current_tab in self.split_views):
156 98 old_other_view = self.split_views[current_tab].get_child2()
@@ -164,9 +106,6 @@ def split_view(self, whatever = None, direction = "vertical", changing = False):
164 106
165 107 old_view = None
166 108
167   - if (not (current_tab in self.same_document)):
168   - self.same_document[current_tab] = True
169   -
170 109 # Here we just kind of loop through the child object of the tab
171 110 # and get rid of all of the existing GUI objects.
172 111 for each in current_tab.get_children():
@@ -182,16 +121,7 @@ def split_view(self, whatever = None, direction = "vertical", changing = False):
182 121 sw1.add_with_viewport(old_view)
183 122
184 123 # Set up a new View object
185   - new_view = None
186   -
187   - # If we are viewing a separate file, then just get that document
188   - if (current_tab in self.alt_views):
189   - new_view = Gedit.View.new(self.alt_views[current_tab])#.get_children()[0]
190   -
191   - # Otherwise, just share the same document as the active View.
192   - # This makes sure changes to either side reflect in the other side.
193   - else:
194   - new_view = Gedit.View.new(current_document)
  124 + new_view = Gedit.View.new(current_document)
195 125
196 126 # Second scrolled window.
197 127 sw2 = Gtk.ScrolledWindow()
@@ -210,20 +140,8 @@ def split_view(self, whatever = None, direction = "vertical", changing = False):
210 140 self.btn_flip = Gtk.Button("Vertical Splitview")
211 141 self.btn_flip.connect("clicked", self.flip_split_view)
212 142
213   - self.btn_view_other_file = Gtk.Button("View other file...")
214   - self.btn_view_other_file.connect("clicked", self.view_other_file)
215   -
216   - self.btn_save_alt_file = Gtk.Button("Save Alt Document")
217   - self.btn_save_alt_file.connect("clicked", self.save_other_file)
218   -
219   - self.label_other_document = Gtk.Label(os.path.basename(current_document.get_uri_for_display()))
220   - self.label_other_document.set_alignment(1.0, self.label_other_document.get_alignment()[1])
221   -
222 143 hbox.pack_start(self.btn_cancel, False, False, 0)
223 144 hbox.pack_start(self.btn_flip, False, False, 0)
224   - hbox.pack_start(self.btn_view_other_file, False, False, 0)
225   - hbox.pack_start(self.btn_save_alt_file, False, False, 0)
226   - hbox.pack_start(self.label_other_document, True, True, 0)
227 145
228 146 vbox = Gtk.VBox()
229 147
@@ -231,7 +149,6 @@ def split_view(self, whatever = None, direction = "vertical", changing = False):
231 149 vbox.pack_start(self.split_views[current_tab], True, True, 0)
232 150
233 151 each.add(vbox)
234   -# each.add_with_viewport(vbox)
235 152
236 153 # The trick of this whole thing is that you have to wait a second for the
237 154 # Paned object to figure out how much room it can take up. So, we're just
@@ -241,39 +158,6 @@ def split_view(self, whatever = None, direction = "vertical", changing = False):
241 158
242 159 current_tab.show_all()
243 160
244   - # If we're just using the same document twice, then we don't need to worry
245   - # about "saving" the "other" document.
246   - if (self.same_document[current_tab] == True):
247   - self.btn_save_alt_file.hide()
248   -
249   - # Oh, what does this do... oh yeah, this makes it so that if you're in the
250   - # "other" document and it's not the active document (you're viewing an alternate
251   - # file), you get the line / column information in the status bar at the bottom.
252   - def update_line_column_data(self, buffer, location, mark, user_data=None):
253   - if (mark != None and location != None):
254   - if ( not (mark.get_name() in (None, "None") ) ):
255   -
256   - (line, column) = (location.get_line(), location.get_line_offset())
257   -
258   - # I put this in a try / except just in case something strange happens.
259   - # GEdit just seems to throw the status bar in an element and add 5
260   - # children or something like that into it, so I just loop through
261   - # until I find a Gtk.Label that starts with " Ln". Seems hacky?
262   - # Yeah. Go make your own splitview if ya don't like it. :P
263   - try:
264   - for each in self.window.get_statusbar().get_children():
265   - if (type(each) == Gtk.Statusbar):
266   - frame = each.get_children()[0]
267   -
268   - frame_child = frame.get_children()[0]
269   -
270   - if (type(frame_child) == Gtk.Label):
271   - if (frame_child.get_label().lstrip().startswith("Ln ")):
272   - frame_child.set_label(" Ln %d, Col %d [2]" % (line, column))
273   -
274   - except:
275   - pass
276   -
277 161 # This of course ends the split view... though I call this when switching
278 162 # from left / right to top / bottom or vice versa. If I'm doing that then
279 163 # changing will be True I believe.
@@ -281,14 +165,6 @@ def end_split_view(self, unused, changing=False):
281 165 current_tab = self.window.get_active_tab()
282 166 current_document = current_tab.get_document()
283 167
284   - if (not changing):
285   - if ( (current_tab in self.alt_views) ):
286   - # For some reason it always claims you have modified the document. Oh well.
287   - if (self.alt_views[current_tab].is_untouched() == False):
288   - # See if they are sure about closing this alternate document
289   - if (not (self.show_save2_dialog(None)) ):
290   - return # Don't close the split view after all...
291   -
292 168 original_view = self.split_views[current_tab].get_child1().get_children()[0]
293 169
294 170 for each in current_tab.get_children():
@@ -303,17 +179,12 @@ def end_split_view(self, unused, changing=False):
303 179 self.split_views.pop(current_tab)
304 180
305 181 if (not changing):
306   - self.same_document.pop(current_tab)
307   -
308   - if (current_tab in self.alt_views):
309   - self.alt_views.pop(current_tab)
310 182
311 183 if (current_tab in self.tabs_already_using_splitview):
312 184 index = self.tabs_already_using_splitview.index(current_tab)
313 185
314 186 self.tabs_already_using_splitview.pop(index)
315 187
316   -
317 188 # Basically recreate the split view.
318 189 def flip_split_view(self, button):
319 190 if (self.btn_flip.get_label() == "Vertical Splitview"):
@@ -329,179 +200,6 @@ def flip_split_view(self, button):
329 200 self.btn_flip.set_label("Vertical Splitview")
330 201
331 202 current_tab = self.window.get_active_tab()
332   - if (current_tab in self.alt_views):
333   - self.label_other_document.set_label(os.path.basename(self.alt_views[current_tab].get_uri_for_display().replace("%20", " ")))
334   -
335   - # Create a dialog that lets the user select a different document to view...
336   - def view_other_file(self, button):
337   - dialog = Gtk.FileChooserDialog("Open...", None,
338   - Gtk.FileChooserAction.OPEN,
339   - (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
340   - Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
341   -
342   - current_document = self.window.get_active_document()
343   - current_path = urllib.unquote( current_document.get_uri_for_display().replace("file://", "") )
344   - dialog.set_current_folder( os.path.dirname(current_path) )
345   -
346   - filter_list = [
347   - {"label": "All Files", "patterns": ["*"], "mime-types": []},
348   - {"label": "Text Files", "patterns": [], "mime-types": ["text/html", "text/plain"]}
349   - ]
350   -
351   - for dict in filter_list:
352   - filter = Gtk.FileFilter()
353   - filter.set_name(dict["label"])
354   - for each in dict["patterns"]:
355   - filter.add_pattern(each)
356   -
357   - for each in dict["mime-types"]:
358   - filter.add_mime_type(each)
359   -
360   - dialog.add_filter(filter)
361   -
362   - response = dialog.run()
363   -
364   - if (response == Gtk.ResponseType.OK):
365   - self.load_other_file(dialog.get_filename())
366   -
367   - else:
368   - pass
369   -
370   - dialog.destroy()
371   -
372   - # Load a separate file into the alternate view.
373   - def load_other_file(self, source, raw_filename = True):
374   - current_tab = self.window.get_active_tab()
375   -
376   - self.same_document[current_tab] = False
377   -
378   - self.split_views[current_tab].remove(self.split_views[current_tab].get_children()[1])
379   -
380   - new_document = Gedit.Document()#.gedit_document_new()
381   -
382   - filepath = source.replace(" ", "%20")
383   -
384   - if not raw_filename:
385   - filepath = "file://" + str(filepath)
386   -
387   - gfile = Gio.File.new_for_path(filepath)
388   -
389   - new_document.load(gfile, self.encoding, 1, 1, True)
390   -
391   - new_view = Gedit.View.new(new_document)#.gedit_view_new(new_document)
392   -
393   - new_document.connect("mark-set", self.update_line_column_data)
394   -
395   - #new_document.save(0)
396   -
397   - self.alt_views[current_tab] = new_document
398   -
399   - sw = Gtk.ScrolledWindow()
400   - sw.add(new_view)
401   -
402   - self.split_views[current_tab].add2(sw)
403   -
404   - self.label_other_document.set_label(os.path.basename(source).replace("%20", " "))
405   -
406   - self.window.get_active_tab().show_all()
407   -
408   - def save_other_file(self, button):
409   - current_tab = self.window.get_active_tab()
410   -
411   - self.alt_views[current_tab].save(0)
412   -
413   - self.show_confirm_save_dialog(None)
414   -
415   - def show_save2_dialog(self, button):
416   - current_tab = self.window.get_active_tab()
417   -
418   - dialog = Gtk.Dialog(title = "Help me help you!",
419   - parent = None,
420   - flags = Gtk.DialogFlags.MODAL,
421   - buttons = (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
422   - Gtk.STOCK_NO, Gtk.ResponseType.NO,
423   - Gtk.STOCK_YES, Gtk.ResponseType.YES)
424   - )
425   -
426   - hbox = Gtk.HBox()
427   - hbox.pack_start(Gtk.Label("Save changes to '%s'?" % os.path.basename(self.alt_views[current_tab].get_uri_for_display().replace("%20", " "))))
428   -
429   - dialog.vbox.pack_start(hbox)
430   -
431   - dialog.vbox.show_all()
432   -
433   - response = dialog.run()
434   -
435   - if (response == Gtk.ResponseType.YES):
436   - self.alt_views[current_tab].save(0)
437   -
438   - self.show_confirm_save_dialog(None)
439   -
440   - elif (response == Gtk.ResponseType.CANCEL):
441   - dialog.destroy()
442   - return False
443   -
444   - else:
445   - pass
446   -
447   - dialog.destroy()
448   -
449   - return True
450   -
451   - def show_confirm_save_dialog(self, button):
452   - current_tab = self.window.get_active_tab()
453   -
454   - dialog = Gtk.Dialog(title = "Document saved!",
455   - parent = None,
456   - flags = Gtk.DialogFlags.MODAL,
457   - buttons = (Gtk.STOCK_OK, Gtk.ResponseType.OK,
458   - Gtk.STOCK_YES, Gtk.ResponseType.YES)
459   - )
460   -
461   - hbox = Gtk.HBox()
462   - hbox.pack_start(Gtk.Label("You have successfully saved"))
463   -
464   - dialog.vbox.pack_start(hbox)
465   -
466   - hbox = Gtk.HBox()
467   - hbox.pack_start(Gtk.Label("'%s'!" % os.path.basename(self.alt_views[current_tab].get_uri_for_display().replace("%20", " "))))
468   -
469   - dialog.vbox.pack_start(hbox)
470   -
471   - dialog.vbox.show_all()
472   -
473   - response = dialog.run()
474   -
475   - if (response == Gtk.ResponseType.OK):
476   - pass
477   -
478   - else:
479   - pass
480   -
481   - dialog.destroy()
482   -
483   - def show_error_dialog(self):
484   - dialog = Gtk.Dialog(title = "Error",
485   - parent = None,
486   - flags = Gtk.DialogFlags.MODAL,
487   - buttons = (Gtk.STOCK_OK, Gtk.RESPONSE_OK)
488   - )
489   -
490   - hbox = Gtk.HBox()
491   - hbox.pack_start(Gtk.Label("You can't splitview"))
492   -
493   - dialog.vbox.pack_start(hbox)
494   -
495   - hbox = Gtk.HBox()
496   - hbox.pack_start(Gtk.Label("an empty document."))
497   -
498   - dialog.vbox.pack_start(hbox)
499   -
500   - dialog.vbox.show_all()
501   -
502   - response = dialog.run()
503   -
504   - dialog.destroy()
505 203
506 204 # This function eventually sets the divider of the splitview at 50%.
507 205 # It waits until the gui object returns a reasonable width.
@@ -549,73 +247,6 @@ def remove_menu_item(self):
549 247
550 248 self.ui_id = None
551 249
552   - #panel = self.window.get_side_panel()
553   - #panel.remove_item(self.results_view)
554   -
555   - def refresh_popup_submenu(self):
556   -
557   - if (not self.window):
558   - return
559   -
560   - doc_list = self.window.get_documents()
561   -
562   - doc_list.sort()
563   -
564   - if (not (self.document_list == doc_list)):
565   -
566   - manager = self.window.get_ui_manager()
567   -
568   - self.build_popup_menu()
569   -
570   - # "temp" actions are the previous "click to launch splitview" actions...
571   - for each in self.temp_actions:
572   - self.action_group.remove_action(each)
573   -
574   - self.temp_actions = []
575   -
576   - for i in range(0, len(doc_list)):
577   -
578   - #print "Want to add '%s'" % doc_list[i].get_short_name_for_display()
579   -
580   - action_name = "splitview_submenu_option_%d" % i
581   - path = "/NotebookPopup/NotebookPupupOps_1/splitview_popup_submenu"
582   -
583   - manager.add_ui(merge_id = self.ui_popup_id,
584   - path = path,
585   - name = action_name,
586   - action = action_name,
587   - type = Gtk.UIManagerItemType.MENU,
588   - top = False)
589   -
590   - action = Gtk.Action(name = action_name, label = doc_list[i].get_short_name_for_display(), tooltip = "None!", stock_id = None)
591   - action.connect("activate", lambda a, doc = doc_list[i]: self.quick_splitview(doc))
592   - action.set_visible(True)
593   -
594   - self.action_group.add_action(action)
595   -
596   - self.temp_actions.append(action)
597   -
598   - self.document_list = doc_list
599   -
600   - def quick_splitview(self, doc):
601   -
602   - current_tab = self.window.get_active_tab()
603   -
604   - if (current_tab in self.alt_views):
605   - if (self.alt_views[current_tab].is_untouched() == False):
606   -
607   - # See if they are sure about closing this alternate document
608   - if (not (self.show_save2_dialog(None)) ):
609   -
610   - return # Don't close the split view after all...
611   -
612   - # This will fail "gracefully" if splitview is already active.
613   - self.split_view()
614   -
615   - # This code will work the same whether the previous line
616   - # "worked" or not.
617   - self.load_other_file(doc.get_uri(), raw_filename = False)
618   -
619 250
620 251 class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
621 252
@@ -635,3 +266,4 @@ def do_deactivate(self):
635 266 def update_ui(self):
636 267 if self.window in self.instances:
637 268 self.instances[self.window].update_ui()
  269 +

0 comments on commit a448206

Please sign in to comment.
Something went wrong with that request. Please try again.