1 # -*- coding: latin-1; -*-
3 # Snippet Extension to pgw
4 # Copyright © 2007 Yves Fischer, yvesf at xapek org
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details (read LICENSE.txt).
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 class NoticeWidget(gtk
.VBox
):
30 def __init__(self
, sqlview
):
31 gtk
.VBox
.__init
__(self
);
32 self
.set_property("width-request", 150);
33 self
.sqlview
= sqlview
;
38 self
.h
= gtk
.HBox(homogeneous
=False, spacing
=2);
40 self
.h_b_hide
= gtk
.Button(">");
41 self
.h_b_hide
.connect("pressed", self
.hide_snippets
)
42 self
.h
.add(self
.h_b_hide
)
44 self
.h_b_add
= gtk
.Button("Add Snippet");
45 self
.h_b_add
.connect('pressed', self
.button_add
);
46 self
.h
.add(self
.h_b_add
);
48 self
.h_b_del
= gtk
.Button("Del Snippet");
49 self
.h_b_del
.connect("pressed", self
.button_del
);
50 self
.h
.add(self
.h_b_del
);
52 self
.pack_start(self
.h
, expand
=False, fill
=True, padding
=0);
54 self
.vpane
= gtk
.VPaned();
56 scroll
= gtk
.ScrolledWindow()
57 scroll
.set_policy(gtk
.POLICY_AUTOMATIC
, gtk
.POLICY_AUTOMATIC
)
58 scroll
.set_property("height-request", 100)
59 scroll
.set_shadow_type(gtk
.SHADOW_NONE
)
61 self
.storeconn
= gtk
.ListStore(str, str);
62 self
.viewconn
= gtk
.TreeView(self
.storeconn
);
63 self
.viewconn
.connect("cursor-changed", self
.treeview_cursor
)
65 cell
= gtk
.CellRendererText()
66 column
= gtk
.TreeViewColumn(_('Snippets'))
67 column
.pack_start(cell
, True)
68 column
.add_attribute(cell
, 'text', 0)
69 column
.set_sort_column_id(0)
70 self
.viewconn
.append_column(column
)
72 scroll
.add(self
.viewconn
)
73 self
.vpane
.pack1(scroll
, True, False);
76 scroll2
= gtk
.ScrolledWindow()
77 scroll2
.set_policy(gtk
.POLICY_AUTOMATIC
, gtk
.POLICY_AUTOMATIC
)
78 scroll2
.set_property("height-request", 30)
79 scroll2
.set_shadow_type(gtk
.SHADOW_NONE
)
80 self
.sqlbuffer
= gtk
.TextBuffer()
81 self
.sqlbuffer
.connect("modified-changed", self
.sqlbuffer_modified
)
82 self
.syntax
= pgw
.Syntax
.Syntax(self
.sqlbuffer
)
84 self
.sqlbuffer
.connect('delete-range', self
.syntax
.text_deleted
)
85 self
.sqlbuffer
.connect('insert-text', self
.syntax
.text_inserted
)
86 self
.sqlbuffer
.connect('changed', self
.syntax
.text_changed
)
89 self
.snippetview
= gtk
.TextView(self
.sqlbuffer
)
91 scroll2
.add(self
.snippetview
)
92 self
.vpane
.add2(scroll2
);
95 self
.vpane
.set_position(50)
99 self
.h2
.set_property("height-request",35);
101 self
.h2_b_save_changes
= gtk
.Button("Save Changes");
102 self
.h2_b_save_changes
.connect("pressed", self
.sqlbuffer_save_changes
)
103 self
.h2
.add(self
.h2_b_save_changes
);
108 self
.sqlbuffer
.set_text("Click on a Snippet to see it here!")
109 self
.sqlbuffer
.set_modified(False)
110 self
.h2_b_save_changes
.set_property("visible", False);
112 def sqlbuffer_save_changes(self
, widget
, data
=None):
113 tuple, treeview
= self
.viewconn
.get_cursor()
114 if tuple == None: #No entry selected->New snipped (can only happen after start)
115 dialog
= self
.GtkInputDialog("Snippet Name?");
116 dialog
.connect_ok(self
.sqlbuffer_save_changes_addNew
);
117 dialog
.connect_validation(self
.add_validation
);
119 self
.sqlbuffer
.set_modified(False)
123 self
.snippets
[index
][1] = self
.sqlbuffer
.get_text(self
.sqlbuffer
.get_start_iter(), self
.sqlbuffer
.get_end_iter())
125 self
.sqlbuffer
.set_modified(False)
127 def sqlbuffer_save_changes_addNew(self
, text
):
128 self
.snippets
.append([text
, self
.sqlbuffer
.get_text(self
.sqlbuffer
.get_start_iter(), self
.sqlbuffer
.get_end_iter())])
131 self
.viewconn
.set_cursor(self
.storeconn
.get_path( self
.storeconn
.get_iter_first()));
135 def sqlbuffer_modified(self
, sqlbuffer
):
136 if sqlbuffer
.get_modified():
137 self
.pack_end(self
.h2
, expand
=False, fill
=True, padding
=0)
142 def hide_snippets(self
, widget
, data
=None):
143 self
.get_parent().set_property("position-set", True)
144 self
.get_parent().set_property("position", self
.get_parent().get_property("max-position"))
146 def add_validation(self
, text
):
152 def treeview_cursor(self
, treeview
):
153 tuple, treeview
= self
.viewconn
.get_cursor()
154 snippet
= self
.snippets
[tuple[0]][1]
155 self
.sqlbuffer
.set_text(snippet
)
156 self
.sqlbuffer
.set_modified(False)
159 def button_add(self
, widget
, data
=None):
160 dialog
= self
.GtkInputDialog("Snippet Name?");
161 dialog
.connect_ok(self
.button_add2
);
162 dialog
.connect_validation(self
.add_validation
);
164 def button_add2(self
, text
):
165 self
.snippets
.append([text
, self
.sqlview
.get_text(self
.sqlview
.get_start_iter(), self
.sqlview
.get_end_iter())])
168 self
.viewconn
.set_cursor(self
.storeconn
.get_path( self
.storeconn
.get_iter_first()));
170 def button_del(self
, widget
, data
=None):
171 tuple, treeview
= self
.viewconn
.get_cursor()
172 if tuple == None: #nothin selected
174 self
.snippets
.pop(tuple[0])
175 self
.save_snippets();
179 #saves Array self.snippets to pgworksheet Config
180 def save_snippets(self
):
181 print "Save Snippets"
182 cp
= ConfigParser
.ConfigParser();
184 cp
.readfp(open(pgw
.get_config_path(), 'r'));
186 print "IOerror ;) Cant save Snippets :(";
189 if (not cp
.has_section("snippets")):
190 cp
.add_section("snippets");
191 else: #um alte einträge zu entfenerne
192 cp
.remove_section("snippets")
193 cp
.add_section("snippets")
195 for snippet
in self
.snippets
:
196 cp
.set("snippets", "snippet%d_name" % n
, snippet
[0])
197 cp
.set("snippets", "snippet%d_content" % n
, snippet
[1])
199 print "%d Snippets saved" % (n
-1)
200 cp
.write(open(pgw
.get_config_path(), 'w'))
202 def read_snippets(self
):
203 cp
= ConfigParser
.ConfigParser();
205 cp
.readfp(open(pgw
.get_config_path(), 'r'));
207 print "IOerror ;) therefore no snippets :(";
212 snippet_content
= cp
.get("snippets", \
213 "snippet"+str(n
)+"_content")
214 snippet_name
= cp
.get("snippets", ("snippet%d_name" % n
))
215 self
.snippets
.append([snippet_name
, snippet_content
])
217 except Exception, inst
:
218 print "%d snippets read" % (n
-1)
220 def set_snippets(self
):
221 self
.storeconn
.clear()
222 for snippet
in self
.snippets
:
223 self
.storeconn
.append(snippet
)
225 class GtkInputDialog(gtk
.Dialog
):
226 def __init__(self
, title
=None, parent
=None, flags
=0, buttons
=None):
227 gtk
.Dialog
.__init
__(self
, title
, parent
, flags
, buttons
);
229 self
.entry
= gtk
.Entry()
230 self
.entry
.connect("activate", self
.pressed_ok
)
232 self
.vbox
.add(self
.entry
)
234 self
.button_ok
= gtk
.Button("OK")
235 self
.vbox
.add(self
.button_ok
)
236 self
.button_ok_action
= lambda x
:()
237 self
.button_ok
.connect("pressed", self
.pressed_ok
)
239 self
.button_cancel
= gtk
.Button("Cancel")
240 self
.vbox
.add(self
.button_cancel
)
241 self
.button_cancel_action
= lambda x
:()
242 self
.button_cancel
.connect("pressed", self
.pressed_cancel
)
244 self
.validation_action
= lambda x
:()
246 def connect_ok(self
, connection
):
247 self
.button_ok_action
= connection
249 def connect_cancel(self
, connection
):
250 self
.button_ok_cancel
= connection
251 def connect_validation(self
, connection
):
252 self
.validation_action
= connection
254 def pressed_ok(self
, widget
, data
=None):
255 if self
.validation_action(self
.entry
.get_text()):
256 self
.button_ok_action(self
.entry
.get_text())
258 def pressed_cancel(self
, widget
, data
=None):
259 self
.button_cancel_action(self
.entry
.get_text())