Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

gtklib: native save dialog improvements

Changeset b4902d3060c5

Parent 0c57710e2add

by Steve Borho

Changes to 2 files · Browse files at b4902d3060c5 Showing diff from parent 0c57710e2add Diff from another changeset...

Change 1 of 3 Show Changes Only hggtk/​gtklib.py Stacked
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 
 
 
 
 
205
206
207
208
209
 
210
211
 
 
212
213
214
215
216
217
218
219
220
221
 
222
223
224
225
226
227
228
229
230
231
 
 
232
233
234
235
236
237
238
239
240
241
 
 
 
 
 
 
 
 
 
242
243
244
245
246
247
 
 
 
248
249
250
 
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
 
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
 
 
 
202
203
204
205
206
207
208
209
210
 
211
212
213
214
215
216
 
 
 
 
 
 
 
 
 
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
 
 
 
 
 
 
 
 
232
233
234
235
236
237
238
239
240
241
242
243
 
 
 
244
245
246
247
248
 
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
 #  # miscellaneous PyGTK classes and functions for TortoiseHg  #  # Copyright (C) 2007 TK Soh <teekaysoh@gmail.com>  #    import os  import sys  import gtk  import gobject  import pango    from thgutil.i18n import _  from thgutil import paths, hglib    from hggtk import hgtk    def set_tortoise_icon(window, thgicon):   ico = paths.get_tortoise_icon(thgicon)   if ico: window.set_icon_from_file(ico)    def get_thg_modifier():   if sys.platform == 'darwin':   return '<Mod1>'   else:   return '<Control>'    def set_tortoise_keys(window):   'Set default TortoiseHg keyboard accelerators'   if sys.platform == 'darwin':   mask = gtk.accelerator_get_default_mod_mask()   mask |= gtk.gdk.MOD1_MASK;   gtk.accelerator_set_default_mod_mask(mask)   mod = get_thg_modifier()   accelgroup = gtk.AccelGroup()   window.add_accel_group(accelgroup)   key, modifier = gtk.accelerator_parse(mod+'w')   window.add_accelerator('thg-close', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)   key, modifier = gtk.accelerator_parse(mod+'q')   window.add_accelerator('thg-exit', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)   key, modifier = gtk.accelerator_parse('F5')   window.add_accelerator('thg-refresh', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)   key, modifier = gtk.accelerator_parse(mod+'Return')   window.add_accelerator('thg-accept', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)     # connect ctrl-w and ctrl-q to every window   window.connect('thg-close', thgclose)   window.connect('thg-exit', thgexit)    def thgexit(window):   if thgclose(window):   gobject.idle_add(hgtk.thgexit, window)    def thgclose(window):   if hasattr(window, 'should_live'):   if window.should_live():   return False   window.destroy()   return True    class StatusBar(gtk.HBox):   def __init__(self, extra=None):   gtk.HBox.__init__(self)   self.pbar = gtk.ProgressBar()   self.sttext = gtk.Label("")   self.sttext.set_ellipsize(pango.ELLIPSIZE_END)   self.sttext.set_alignment(0, 0.5)     self.pbox = gtk.HBox()   self.pbox.pack_start(gtk.VSeparator(), False, False)   self.pbox.pack_start(self.pbar, False, False)     self.pack_start(self.sttext, padding=1)   if extra:   self.pack_end(extra, False, False)   self.pack_end(self.pbox, False, False, padding=1)   self.pbox.set_child_visible(False)   self.show_all()     def _pulse_timer(self, now=False):   self.pbar.pulse()   return True     def begin(self, msg=_('Running'), timeout=100):   self.pbox.set_child_visible(True)   self.pbox.map()   self.set_status_text(msg)   self._timeout_event = gobject.timeout_add(timeout, self._pulse_timer)     def end(self, msg=_('Done'), unmap=True):   gobject.source_remove(self._timeout_event)   self.set_status_text(msg)   if unmap:   self.pbox.unmap()   else:   self.pbar.set_fraction(1.0)     def set_status_text(self, msg):   self.sttext.set_text(str(msg))     def set_pulse_step(self, val):   self.pbar.set_pulse_step(val)      class MessageDialog(gtk.Dialog):   button_map = {   gtk.BUTTONS_NONE: None,   gtk.BUTTONS_OK: (gtk.STOCK_OK, gtk.RESPONSE_OK),   gtk.BUTTONS_CLOSE : (gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE),   gtk.BUTTONS_CANCEL: (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),   gtk.BUTTONS_YES_NO : (gtk.STOCK_YES, gtk.RESPONSE_YES,   gtk.STOCK_NO, gtk.RESPONSE_NO),   gtk.BUTTONS_OK_CANCEL: (gtk.STOCK_OK, gtk.RESPONSE_OK,   gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),   }   image_map = {   gtk.MESSAGE_INFO : gtk.STOCK_DIALOG_INFO,   gtk.MESSAGE_WARNING : gtk.STOCK_DIALOG_WARNING,   gtk.MESSAGE_QUESTION : gtk.STOCK_DIALOG_QUESTION,   gtk.MESSAGE_ERROR : gtk.STOCK_DIALOG_ERROR,   }     def __init__(self, parent=None, flags=0, type=gtk.MESSAGE_INFO,   buttons=gtk.BUTTONS_NONE, message_format=None):   gtk.Dialog.__init__(self,   parent=parent,   flags=flags | gtk.DIALOG_NO_SEPARATOR,   buttons=MessageDialog.button_map[buttons])   self.set_resizable(False)     hbox = gtk.HBox()   self._image_frame = gtk.Frame()   self._image_frame.set_shadow_type(gtk.SHADOW_NONE)   self._image = gtk.Image()   self._image.set_from_stock(MessageDialog.image_map[type],   gtk.ICON_SIZE_DIALOG)   self._image_frame.add(self._image)   hbox.pack_start(self._image_frame, padding=5)     lblbox = gtk.VBox(spacing=10)   self._primary = gtk.Label("")   self._primary.set_alignment(0.0, 0.5)   self._primary.set_line_wrap(True)   lblbox.pack_start(self._primary)     self._secondary = gtk.Label()   lblbox.pack_end(self._secondary)   self._secondary.set_line_wrap(True)   hbox.pack_start(lblbox, padding=5)     self.vbox.pack_start(hbox, False, False, 10)   self.show_all()     def set_markup(self, s):   self._primary.set_markup(s)     def format_secondary_markup(self, message_format):   self._secondary.set_markup(message_format)     def format_secondary_text(self, message_format):   self._secondary.set_text(message_format)     def set_image(self, image):   self._image_frame.remove(self._image)   self._image = image   self._image_frame.add(self._image)   self._image.show()    class NativeSaveFileDialogWrapper:   """Wrap the windows file dialog, or display default gtk dialog if   that isn't available"""   def __init__(self, InitialDir = None, Title = _('Save File'), - Filter = (_("All files"), "*.*"), FilterIndex = 1, + Filter = ((_('All files'), '*.*'),), FilterIndex = 1,   FileName = '', Open=False):   if InitialDir == None:   InitialDir = os.path.expanduser("~")   self.InitialDir = InitialDir   self.FileName = FileName   self.Title = Title   self.Filter = Filter   self.FilterIndex = FilterIndex   self.Open = Open     def run(self):   """run the file dialog, either return a file name, or False if   the user aborted the dialog"""   try:   import win32gui, win32con, pywintypes   return self.runWindows()   except ImportError:   return self.runCompatible()     def runWindows(self):   import win32gui, win32con, pywintypes   cwd = os.getcwd()   fname = None   try: - if self.Open: - fname, customfilter, flags=win32gui.GetOpenFileNameW( - InitialDir=self.InitialDir, + f = '' + for name, mask in self.Filter: + '\0'.join(f, name, mask) + f.append('\0') + opts = dict(InitialDir=self.InitialDir,   Flags=win32con.OFN_EXPLORER,   File=self.FileName,   DefExt=None,   Title=hglib.fromutf(self.Title), - Filter= hglib.fromutf('\0'.join(self.Filter)+'\0'), + Filter= hglib.fromutf(f),   CustomFilter=None,   FilterIndex=self.FilterIndex) + if self.Open: + fname, _, _ = win32gui.GetOpenFileNameW(**opts)   else: - fname, customfilter, flags=win32gui.GetSaveFileNameW( - InitialDir=self.InitialDir, - Flags=win32con.OFN_EXPLORER, - File=self.FileName, - DefExt=None, - Title=hglib.fromutf(self.Title), - Filter= hglib.fromutf('\0'.join(self.Filter)+'\0'), - CustomFilter=None, - FilterIndex=self.FilterIndex) + fname, _, _ = win32gui.GetSaveFileNameW(**opts)   if fname:   fname = os.path.abspath(fname)   except pywintypes.error:   pass   os.chdir(cwd)   return fname     def runCompatible(self):   if self.Open:   action = gtk.FILE_CHOOSER_ACTION_OPEN + buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + gtk.STOCK_OPEN, gtk.RESPONSE_OK)   else:   action = gtk.FILE_CHOOSER_ACTION_SAVE - file_save = gtk.FileChooserDialog(self.Title, None, action, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_SAVE, gtk.RESPONSE_OK)) - file_save.set_do_overwrite_confirmation(True) - file_save.set_default_response(gtk.RESPONSE_OK) - file_save.set_current_folder(self.InitialDir) - file_save.set_current_name(self.FileName) - for name, pattern in dict(self.Filter).iteritems(): + buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + gtk.STOCK_SAVE, gtk.RESPONSE_OK) + dlg = gtk.FileChooserDialog(self.Title, None, action, buttons) + dlg.set_do_overwrite_confirmation(True) + dlg.set_default_response(gtk.RESPONSE_OK) + dlg.set_current_folder(self.InitialDir) + if not self.Open: + dlg.set_current_name(self.FileName) + for name, pattern in self.Filter:   fi = gtk.FileFilter()   fi.set_name(name)   fi.add_pattern(pattern) - file_save.add_filter(fi) - if file_save.run() == gtk.RESPONSE_OK: - result = file_save.get_filename(); + dlg.add_filter(fi) + if dlg.run() == gtk.RESPONSE_OK: + result = dlg.get_filename();   else:   result = False - file_save.destroy() + dlg.destroy()   return result    class NativeFolderSelectDialog:   """Wrap the windows folder dialog, or display default gtk dialog if   that isn't available"""   def __init__(self, initial = None, title = _('Select Folder')):   self.initial = initial or os.getcwd()   self.title = title     def run(self):   """run the file dialog, either return a file name, or False if   the user aborted the dialog"""   try:   import win32com, win32gui, pywintypes   return self.runWindows()   except ImportError, e:   return self.runCompatible()     def runWindows(self):   from win32com.shell import shell, shellcon   import win32gui, pywintypes     def BrowseCallbackProc(hwnd, msg, lp, data):   if msg== shellcon.BFFM_INITIALIZED:   win32gui.SendMessage(hwnd, shellcon.BFFM_SETSELECTION, 1, data)   elif msg == shellcon.BFFM_SELCHANGED:   # Set the status text of the   # For this message, 'lp' is the address of the PIDL.   pidl = shell.AddressAsPIDL(lp)   try:   path = shell.SHGetPathFromIDList(pidl)   win32gui.SendMessage(hwnd, shellcon.BFFM_SETSTATUSTEXT, 0, path)   except shell.error:   # No path for this PIDL   pass     fname = None   try:   flags = shellcon.BIF_EDITBOX | 0x40 #shellcon.BIF_NEWDIALOGSTYLE   pidl, _, _ = shell.SHBrowseForFolder(0,   None,   hglib.fromutf(self.title),   flags,   BrowseCallbackProc, # callback function   self.initial) # 'data' param for the callback   if pidl:   fname = hglib.toutf(shell.SHGetPathFromIDList(pidl))   except (pywintypes.error, pywintypes.com_error):   pass   return fname     def runCompatible(self):   dialog = gtk.FileChooserDialog(title=None,   action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,   buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,   gtk.STOCK_OPEN,gtk.RESPONSE_OK))   dialog.set_default_response(gtk.RESPONSE_OK)   response = dialog.run()   fname = dialog.get_filename()   dialog.destroy()   if response == gtk.RESPONSE_OK:   return fname   return None
Change 1 of 1 Show Entire File hggtk/​synch.py Stacked
 
355
356
357
358
359
 
 
360
361
362
 
355
356
357
 
 
358
359
360
361
362
@@ -355,8 +355,8 @@
  response = gtklib.NativeSaveFileDialogWrapper(   InitialDir=self.root,   Title=_('Select Bundle'), - Filter=(_('Bundle (*.hg)'), '*.hg', - _('Bundle (*)'), '*.*'), + Filter=((_('Bundle (*.hg)'), '*.hg'), + (_('Bundle (*)'), '*.*')),   Open=True).run()   if response:   self.pathtext.set_text(response)