Skip to content

Commit 8d27125

Browse files
authored
Merge pull request #153 from MightyCreak/dont-use-new
Don't use new() in GTK classes
2 parents a0225ff + 23c6576 commit 8d27125

File tree

6 files changed

+90
-62
lines changed

6 files changed

+90
-62
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Documentation: added release process instructions
1111
- Translation: added French translation
1212

13+
### Fixed
14+
- Cleanups: use constructors instead of `new()` whenever possible in GTK
15+
classes
16+
1317
## 0.7.4 - 2022-04-03
1418

1519
### Added

src/diffuse/dialogs.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ def __init__(self, title, parent, prefs, action, accept, rev=False):
7878
self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
7979
self.add_button(accept, Gtk.ResponseType.OK)
8080
self.prefs = prefs
81-
hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
82-
hbox.set_border_width(5)
83-
label = Gtk.Label.new(_('Encoding: '))
81+
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0, border_width=5)
82+
label = Gtk.Label(label=_('Encoding: '))
8483
hbox.pack_start(label, False, False, 0)
8584
label.show()
8685
self.encoding = entry = utils.EncodingMenu(
@@ -89,10 +88,10 @@ def __init__(self, title, parent, prefs, action, accept, rev=False):
8988
hbox.pack_start(entry, False, False, 5)
9089
entry.show()
9190
if rev:
92-
self.revision = entry = Gtk.Entry.new()
91+
self.revision = entry = Gtk.Entry()
9392
hbox.pack_end(entry, False, False, 0)
9493
entry.show()
95-
label = Gtk.Label.new(_('Revision: '))
94+
label = Gtk.Label(label=_('Revision: '))
9695
hbox.pack_end(label, False, False, 0)
9796
label.show()
9897

@@ -122,15 +121,21 @@ def __init__(self, parent, title, text, val, lower, upper, step=1, page=0):
122121
self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT)
123122
self.add_button(Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT)
124123

125-
vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
124+
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
126125
vbox.set_border_width(10)
127126

128-
hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
129-
label = Gtk.Label.new(text)
127+
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
128+
label = Gtk.Label(label=text)
130129
hbox.pack_start(label, False, False, 0)
131130
label.show()
132-
adj = Gtk.Adjustment.new(val, lower, upper, step, page, 0)
133-
self.button = button = Gtk.SpinButton.new(adj, 1.0, 0)
131+
adj = Gtk.Adjustment(
132+
value=val,
133+
lower=lower,
134+
upper=upper,
135+
step_increment=step,
136+
page_increment=page,
137+
page_size=0)
138+
self.button = button = Gtk.SpinButton(adjustment=adj, climb_rate=1.0, digits=0)
134139
button.connect('activate', self.button_cb)
135140
hbox.pack_start(button, True, True, 0)
136141
button.show()
@@ -152,11 +157,11 @@ def __init__(self, parent, pattern=None, history=None):
152157
self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT)
153158
self.add_button(Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT)
154159

155-
vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
160+
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
156161
vbox.set_border_width(10)
157162

158-
hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
159-
label = Gtk.Label.new(_('Search For: '))
163+
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
164+
label = Gtk.Label(label=_('Search For: '))
160165
hbox.pack_start(label, False, False, 0)
161166
label.show()
162167
combo = Gtk.ComboBoxText.new_with_entry()
@@ -167,7 +172,7 @@ def __init__(self, parent, pattern=None, history=None):
167172
self.entry.set_text(pattern)
168173

169174
if history is not None:
170-
completion = Gtk.EntryCompletion.new()
175+
completion = Gtk.EntryCompletion()
171176
liststore = Gtk.ListStore(GObject.TYPE_STRING)
172177
completion.set_model(liststore)
173178
completion.set_text_column(0)

src/diffuse/main.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,24 @@ class NotebookTab(Gtk.EventBox):
6161
def __init__(self, name: str, stock: str) -> None:
6262
Gtk.EventBox.__init__(self)
6363
self.set_visible_window(False)
64-
hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
64+
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
6565
if stock is not None:
66-
image = Gtk.Image.new()
66+
image = Gtk.Image()
6767
image.set_from_stock(stock, Gtk.IconSize.MENU)
6868
hbox.pack_start(image, False, False, 5)
6969
image.show()
7070

71-
label = Gtk.Label.new(name)
71+
label = Gtk.Label(label=name)
7272
# left justify the widget
7373
label.set_xalign(0.0)
7474
label.set_yalign(0.5)
7575
hbox.pack_start(label, True, True, 0)
7676
label.show()
7777
self.label = label
7878

79-
button = Gtk.Button.new()
79+
button = Gtk.Button()
8080
button.set_relief(Gtk.ReliefStyle.NONE)
81-
image = Gtk.Image.new()
81+
image = Gtk.Image()
8282
image.set_from_stock(Gtk.STOCK_CLOSE, Gtk.IconSize.MENU)
8383
button.add(image)
8484
image.show()
@@ -134,7 +134,7 @@ def __init__(self) -> None:
134134
[Gtk.STOCK_SAVE, self.button_cb, 'save', _('Save File')],
135135
[Gtk.STOCK_SAVE_AS, self.button_cb, 'save_as', _('Save File As...')]])
136136

137-
self.label = label = Gtk.Label.new()
137+
self.label = label = Gtk.Label()
138138
label.set_selectable(True)
139139
label.set_ellipsize(Pango.EllipsizeMode.START)
140140
label.set_max_width_chars(1)
@@ -181,23 +181,23 @@ def setEdits(self, has_edits: bool) -> None:
181181
class PaneFooter(Gtk.Box):
182182
def __init__(self) -> None:
183183
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
184-
self.cursor = label = Gtk.Label.new()
184+
self.cursor = label = Gtk.Label()
185185
self.cursor.set_size_request(-1, -1)
186186
self.pack_start(label, False, False, 0)
187187

188-
separator = Gtk.Separator.new(Gtk.Orientation.VERTICAL)
188+
separator = Gtk.Separator(orientation=Gtk.Orientation.VERTICAL)
189189
self.pack_end(separator, False, False, 10)
190190

191-
self.encoding = label = Gtk.Label.new()
191+
self.encoding = label = Gtk.Label()
192192
self.pack_end(label, False, False, 0)
193193

194-
separator = Gtk.Separator.new(Gtk.Orientation.VERTICAL)
194+
separator = Gtk.Separator(orientation=Gtk.Orientation.VERTICAL)
195195
self.pack_end(separator, False, False, 10)
196196

197-
self.format = label = Gtk.Label.new()
197+
self.format = label = Gtk.Label()
198198
self.pack_end(label, False, False, 0)
199199

200-
separator = Gtk.Separator.new(Gtk.Orientation.VERTICAL)
200+
separator = Gtk.Separator(orientation=Gtk.Orientation.VERTICAL)
201201
self.pack_end(separator, False, False, 10)
202202

203203
self.set_size_request(0, self.get_size_request()[1])
@@ -905,7 +905,7 @@ def __init__(self, rc_dir):
905905
menu_bar.show()
906906

907907
# create button bar
908-
hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
908+
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
909909
_append_buttons(hbox, Gtk.IconSize.LARGE_TOOLBAR, [
910910
[DIFFUSE_STOCK_NEW_2WAY_MERGE, self.new_2_way_file_merge_cb, None, _('New 2-Way File Merge')], # noqa: E501
911911
[DIFFUSE_STOCK_NEW_3WAY_MERGE, self.new_3_way_file_merge_cb, None, _('New 3-Way File Merge')], # noqa: E501
@@ -936,14 +936,14 @@ def __init__(self, rc_dir):
936936
hbox.show()
937937

938938
self.closed_tabs = []
939-
self.notebook = notebook = Gtk.Notebook.new()
939+
self.notebook = notebook = Gtk.Notebook()
940940
notebook.set_scrollable(True)
941941
notebook.connect('switch-page', self.switch_page_cb)
942942
vbox.pack_start(notebook, True, True, 0)
943943
notebook.show()
944944

945945
# Add a status bar to the bottom
946-
self.statusbar = statusbar = Gtk.Statusbar.new()
946+
self.statusbar = statusbar = Gtk.Statusbar()
947947
vbox.pack_start(statusbar, False, False, 0)
948948
statusbar.show()
949949

@@ -1056,15 +1056,15 @@ def confirmCloseViewers(self, viewers: List[FileDiffViewer]) -> bool:
10561056
dialog.set_resizable(True)
10571057
dialog.set_title(constants.APP_NAME)
10581058
# add list of files with unsaved changes
1059-
sw = Gtk.ScrolledWindow.new()
1059+
sw = Gtk.ScrolledWindow()
10601060
sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
10611061
treeview = Gtk.TreeView.new_with_model(model)
1062-
r = Gtk.CellRendererToggle.new()
1062+
r = Gtk.CellRendererToggle()
10631063
r.connect('toggled', self._confirmClose_toggle_cb, model)
10641064
column = Gtk.TreeViewColumn(None, r)
10651065
column.add_attribute(r, 'active', 0)
10661066
treeview.append_column(column)
1067-
r = Gtk.CellRendererText.new()
1067+
r = Gtk.CellRendererText()
10681068
column = Gtk.TreeViewColumn(_('Tab'), r, text=1)
10691069
column.set_resizable(True)
10701070
column.set_expand(True)
@@ -1137,7 +1137,7 @@ def notebooktab_button_press_cb(self, widget, event, data):
11371137
self.remove_tab_cb(widget, data)
11381138
elif event.button == 3:
11391139
# create a popup to pick a tab for focus on RMB
1140-
menu = Gtk.Menu.new()
1140+
menu = Gtk.Menu()
11411141
nb = self.notebook
11421142
for i in range(nb.get_n_pages()):
11431143
viewer = nb.get_nth_page(i)
@@ -1685,7 +1685,7 @@ def about_cb(self, widget, data):
16851685

16861686
# convenience method for creating a menu bar according to a template
16871687
def _create_menu_bar(specs, radio, accel_group):
1688-
menu_bar = Gtk.MenuBar.new()
1688+
menu_bar = Gtk.MenuBar()
16891689
for label, spec in specs:
16901690
menu = Gtk.MenuItem.new_with_mnemonic(label)
16911691
menu.set_submenu(createMenu(spec, radio, accel_group))
@@ -1700,10 +1700,10 @@ def _create_menu_bar(specs, radio, accel_group):
17001700
def _append_buttons(box, size, specs):
17011701
for spec in specs:
17021702
if len(spec) > 0:
1703-
button = Gtk.Button.new()
1703+
button = Gtk.Button()
17041704
button.set_relief(Gtk.ReliefStyle.NONE)
17051705
button.set_can_focus(False)
1706-
image = Gtk.Image.new()
1706+
image = Gtk.Image()
17071707
image.set_from_stock(spec[0], size)
17081708
button.add(image)
17091709
image.show()
@@ -1714,7 +1714,7 @@ def _append_buttons(box, size, specs):
17141714
box.pack_start(button, False, False, 0)
17151715
button.show()
17161716
else:
1717-
separator = Gtk.Separator.new(Gtk.Orientation.VERTICAL)
1717+
separator = Gtk.Separator(orientation=Gtk.Orientation.VERTICAL)
17181718
box.pack_start(separator, False, False, 5)
17191719
separator.show()
17201720

src/diffuse/preferences.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ def runDialog(self, parent: Gtk.Widget) -> None:
306306
def _buildPrefsDialog(self, parent, widgets, template):
307307
tpl_section = template[0]
308308
if tpl_section == 'FolderSet':
309-
notebook = Gtk.Notebook.new()
309+
notebook = Gtk.Notebook()
310310
notebook.set_border_width(10)
311311
i = 1
312312
while i < len(template):
313-
label = Gtk.Label.new(template[i])
313+
label = Gtk.Label(label=template[i])
314314
i += 1
315315
w = self._buildPrefsDialog(parent, widgets, template[i])
316316
i += 1
@@ -319,7 +319,7 @@ def _buildPrefsDialog(self, parent, widgets, template):
319319
label.show()
320320
return notebook
321321

322-
table = Gtk.Grid.new()
322+
table = Gtk.Grid()
323323
table.set_border_width(10)
324324
for i, tpl in enumerate(template[1:]):
325325
tpl_section = tpl[0]
@@ -335,21 +335,28 @@ def _buildPrefsDialog(self, parent, widgets, template):
335335
button.connect('toggled', self._toggled_cb, widgets, tpl[1])
336336
button.show()
337337
else:
338-
label = Gtk.Label.new(tpl[3] + ': ')
338+
label = Gtk.Label(label=tpl[3] + ': ')
339339
label.set_xalign(1.0)
340340
label.set_yalign(0.5)
341341
table.attach(label, 0, i, 1, 1)
342342
label.show()
343343
if tpl[0] in ['Font', 'Integer']:
344-
entry = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
344+
entry = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
345345
if tpl[0] == 'Font':
346346
button = Gtk.FontButton()
347347
button.set_font(self.string_prefs[tpl[1]])
348348
else:
349-
button = Gtk.SpinButton.new(
350-
Gtk.Adjustment.new(self.int_prefs[tpl[1]], tpl[4], tpl[5], 1, 0, 0),
351-
1.0,
352-
0)
349+
adj = Gtk.Adjustment(
350+
value=self.int_prefs[tpl[1]],
351+
lower=tpl[4],
352+
upper=tpl[5],
353+
step_increment=1,
354+
page_increment=0,
355+
page_size=0)
356+
button = Gtk.SpinButton(
357+
adjustment=adj,
358+
climb_rate=1.0,
359+
digits=0)
353360
widgets[tpl[1]] = button
354361
entry.pack_start(button, False, False, 0)
355362
button.show()
@@ -360,7 +367,7 @@ def _buildPrefsDialog(self, parent, widgets, template):
360367
elif tpl[0] == 'File':
361368
entry = _FileEntry(parent, tpl[3])
362369
else:
363-
entry = Gtk.Entry.new()
370+
entry = Gtk.Entry()
364371
widgets[tpl[1]] = entry
365372
entry.set_text(self.string_prefs[tpl[1]])
366373
table.attach(entry, 1, i, 1, 1)
@@ -443,11 +450,11 @@ def __init__(self, parent: Gtk.Widget, title: str) -> None:
443450
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
444451
self.toplevel = parent
445452
self.title = title
446-
self.entry = entry = Gtk.Entry.new()
453+
self.entry = entry = Gtk.Entry()
447454
self.pack_start(entry, True, True, 0)
448455
entry.show()
449-
button = Gtk.Button.new()
450-
image = Gtk.Image.new()
456+
button = Gtk.Button()
457+
image = Gtk.Image()
451458
image.set_from_stock(Gtk.STOCK_OPEN, Gtk.IconSize.MENU)
452459
button.add(image)
453460
image.show()

src/diffuse/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, parent: Gtk.Widget, message_type: Gtk.MessageType, s: str) ->
5757
class EncodingMenu(Gtk.Box):
5858
def __init__(self, prefs: Preferences, autodetect: bool = False) -> None:
5959
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
60-
self.combobox = combobox = Gtk.ComboBoxText.new()
60+
self.combobox = combobox = Gtk.ComboBoxText()
6161
self.encodings = prefs.getEncodings()[:]
6262
for e in self.encodings:
6363
combobox.append_text(e)

0 commit comments

Comments
 (0)