diff mbox series

[-,hwmixvolume,v2,3/7] hwmixvolume: switch to GTK+ 3.0 and GLib 2.0

Message ID 20180918134237.8489-4-linkmauve@jabberfr.org (mailing list archive)
State New, archived
Headers show
Series [-,hwmixvolume,v2,1/7] hwmixvolume: use four spaces instead of one tab for indent | expand

Commit Message

Emmanuel Gil Peyrot Sept. 18, 2018, 1:42 p.m. UTC
From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

This replaces VBox and HBox with Grid (using Gtk.Orientation), HScale
with Scale, creates labels with mnemonics, set hexpand and vexpand
properly, use the correct enum container classes, use the correct getter
for size request, and finally update to the correct GLib watch function.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

Comments

Takashi Sakamoto Sept. 19, 2018, 1:22 p.m. UTC | #1
Hi,

On Sep 18 2018 22:42, Emmanuel Gil Peyrot wrote:
> From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> 
> This replaces VBox and HBox with Grid (using Gtk.Orientation), HScale
> with Scale, creates labels with mnemonics, set hexpand and vexpand
> properly, use the correct enum container classes, use the correct getter
> for size request, and finally update to the correct GLib watch function.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> 
> diff --git a/hwmixvolume/hwmixvolume b/hwmixvolume/hwmixvolume
> index 64d232c..8e0b6b8 100755
> --- a/hwmixvolume/hwmixvolume
> +++ b/hwmixvolume/hwmixvolume
> @@ -60,18 +60,18 @@ class Stream:
>           value = alsahcontrol.Value(self.element)
>           value.read()
>           values = value.get_tuple(TYPE_INTEGER, info.count)
> -        self.label = Gtk.Label(self.get_label(info))
> +        self.label = Gtk.Label.new(self.get_label(info))
>           self.label.set_single_line_mode(True)
> -        self.parent.scales_vbox.pack_start(self.label, expand=False)
> +        self.parent.scales_vbox.add(self.label)
>           for i in range(info.count):
>               adj = Gtk.Adjustment(value=values[i],
>                       lower=info.min, upper=info.max,
>                       step_incr=1,
>                       page_incr=(info.max-info.min+1)/8)
>               adj.connect('value-changed', self.update_ctl_from_scale, i)
> -            scale = Gtk.HScale(adj)
> +            scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, adjustment=adj)
>               scale.set_draw_value(False)
> -            self.parent.scales_vbox.pack_start(scale, expand=False)
> +            self.parent.scales_vbox.add(scale)
>               self.scales.append(scale)
>               self.adjustments.append(adj)
>           self.parent.scales_vbox.show_all()
> @@ -174,45 +174,48 @@ class MixerWindow(Gtk.Window):
>           self.connect('destroy', lambda w: Gtk.main_quit())
>           self.set_title("Hardware Mixer Volumes")
>   
> -        vbox = Gtk.VBox()
> +        vbox = Gtk.Grid()
> +        vbox.set_orientation(Gtk.Orientation.VERTICAL)

As long as I know, g-i of Gtk+3 has both of 'Gtk.VBox' and 'Gtk.HBox'.
I don't object to this patchset if they satisfy your demand, however
from my curiosity would I ask you the reason to use 'Gtk.Grid' instead
of them? This patch includes no lines to add rows/colums and to me no
requirement to use grid in this point.

>           self.add(vbox)
>   
> -        hbox = Gtk.HBox()
> -        vbox.pack_start(hbox, expand=False)
> +        hbox = Gtk.Grid()
> +        vbox.add(hbox)
>   
> -        label = Gtk.Label("_Sound Card: ")
> -        label.set_use_underline(True)
> -        hbox.pack_start(label, expand=False)
> +        label = Gtk.Label.new_with_mnemonic("_Sound Card: ")
> +        hbox.add(label)
>   
> -        combo = Gtk.combo_box_new_text()
> +        combo = Gtk.ComboBoxText()
> +        combo.set_hexpand(True)
>           for i in self.card_numbers:
>               str = "%d: %s" % (i, alsacard.card_get_name(i))
>               combo.append_text(str)
>           if len(self.card_numbers) > 0:
>               combo.set_active(0)
>           combo.connect('changed', lambda c: self.change_card(self.card_numbers[combo.get_active()]))
> -        hbox.pack_start(combo)
> +        hbox.add(combo)
>           label.set_mnemonic_widget(combo)
>   
> -        self.lock_check = Gtk.CheckButton(label="_Lock Channels")
> +        self.lock_check = Gtk.CheckButton.new_with_mnemonic(label="_Lock Channels")
>           self.lock_check.set_active(True)
> -        vbox.pack_start(self.lock_check, expand=False)
> +        vbox.add(self.lock_check)
>   
>           scrollwin = Gtk.ScrolledWindow()
> -        scrollwin.set_policy(hscrollbar_policy=Gtk.POLICY_NEVER, vscrollbar_policy=Gtk.POLICY_AUTOMATIC)
> -        scrollwin.set_shadow_type(Gtk.SHADOW_NONE)
> -        vbox.pack_start(scrollwin)
> +        scrollwin.set_policy(hscrollbar_policy=Gtk.PolicyType.NEVER, vscrollbar_policy=Gtk.PolicyType.AUTOMATIC)
> +        scrollwin.set_shadow_type(Gtk.ShadowType.NONE)
> +        scrollwin.set_vexpand(True)
> +        vbox.add(scrollwin)
>   
> -        self.scales_vbox = Gtk.VBox()
> -        scrollwin.add_with_viewport(self.scales_vbox)
> +        self.scales_vbox = Gtk.Grid()
> +        self.scales_vbox.set_orientation(Gtk.Orientation.VERTICAL)
> +        scrollwin.add(self.scales_vbox)
>   
>           label = Gtk.Label()
>           label.set_single_line_mode(True)
> -        line_height = label.size_request()[1]
> +        line_height = max(label.get_size_request().height, 0)
>           label.destroy()
> -        scale = Gtk.HScale()
> +        scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL)
>           scale.set_draw_value(False)
> -        line_height += scale.size_request()[1]
> +        line_height += max(scale.get_size_request().height, 0)
>           scale.destroy()
>           # always have space for at least four sliders
>           scrollwin.set_size_request(width=-1, height=line_height*4+4)
> @@ -252,7 +255,7 @@ class MixerWindow(Gtk.Window):
>               self.streams.append(stream)
>   
>           for fd,condition in self.hcontrol.poll_fds:
> -            self.hctl_sources.append(GLib.io_add_watch(fd, condition, self.hctl_io_callback))
> +            self.hctl_sources.append(GLib.io_add_watch(fd, 0, GLib.IOCondition(condition), self.hctl_io_callback))
>   
>           self.update_msg_label()
>   
> @@ -270,8 +273,9 @@ class MixerWindow(Gtk.Window):
>               else:
>                   msg = "This card does not have stream controls."
>               if not has_msg:
> -                self.msg_label = Gtk.Label(msg)
> -                self.scales_vbox.pack_start(self.msg_label)
> +                self.msg_label = Gtk.Label.new(msg)
> +                self.msg_label.set_vexpand(True)
> +                self.scales_vbox.add(self.msg_label)
>                   self.scales_vbox.show_all()
>               elif self.msg_label.get_text() != msg:
>                   self.msg_label.set_text(msg)
> @@ -284,8 +288,8 @@ class MixerWindow(Gtk.Window):
>           except:
>               # TODO: alsa error msg
>               dlg = Gtk.MessageDialog(self,
> -                    Gtk.DIALOG_MODAL | Gtk.DIALOG_DESTROY_WITH_PARENT,
> -                    Gtk.MESSAGE_ERROR, Gtk.BUTTONS_OK,
> +                    Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
> +                    Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
>                       "Cannot open sound card control device.")
>               dlg.run()
>               dlg.destroy()

Thanks

Takashi Sakamoto
Emmanuel Gil Peyrot Sept. 19, 2018, 1:36 p.m. UTC | #2
On Wed, Sep 19, 2018 at 10:22:48PM +0900, Takashi Sakamoto wrote:
> Hi,

Hi,

> 
> On Sep 18 2018 22:42, Emmanuel Gil Peyrot wrote:
> > From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
[…]
> > @@ -174,45 +174,48 @@ class MixerWindow(Gtk.Window):
> >           self.connect('destroy', lambda w: Gtk.main_quit())
> >           self.set_title("Hardware Mixer Volumes")
> > -        vbox = Gtk.VBox()
> > +        vbox = Gtk.Grid()
> > +        vbox.set_orientation(Gtk.Orientation.VERTICAL)
> 
> As long as I know, g-i of Gtk+3 has both of 'Gtk.VBox' and 'Gtk.HBox'.
> I don't object to this patchset if they satisfy your demand, however
> from my curiosity would I ask you the reason to use 'Gtk.Grid' instead
> of them? This patch includes no lines to add rows/colums and to me no
> requirement to use grid in this point.

The Gtk.VBox documentation[1] says:

“Deprecated since version 3.2: You can use Gtk.Box.new() with
Gtk.Orientation.VERTICAL instead, which is a quick and easy change. But
the recommendation is to switch to Gtk.Grid, since Gtk.Box is going to
go away eventually. See Migrating from other containers to GtkGrid.”

Since no backwards incompatible change can be made in GTK+ 3.x, it is
still present as of 3.24, but it has been removed in 4.0 and it’d be
nice to support this one already when it’ll be released. :)

> Thanks
> 
> Takashi Sakamoto

Thanks,

[1] https://lazka.github.io/pgi-docs/Gtk-3.0/classes/VBox.html
Takashi Sakamoto Sept. 19, 2018, 2:14 p.m. UTC | #3
On Sep 19 2018 22:36, Emmanuel Gil Peyrot wrote:
> On Wed, Sep 19, 2018 at 10:22:48PM +0900, Takashi Sakamoto wrote:
>> On Sep 18 2018 22:42, Emmanuel Gil Peyrot wrote:
>>> From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> […]
>>> @@ -174,45 +174,48 @@ class MixerWindow(Gtk.Window):
>>>            self.connect('destroy', lambda w: Gtk.main_quit())
>>>            self.set_title("Hardware Mixer Volumes")
>>> -        vbox = Gtk.VBox()
>>> +        vbox = Gtk.Grid()
>>> +        vbox.set_orientation(Gtk.Orientation.VERTICAL)
>>
>> As long as I know, g-i of Gtk+3 has both of 'Gtk.VBox' and 'Gtk.HBox'.
>> I don't object to this patchset if they satisfy your demand, however
>> from my curiosity would I ask you the reason to use 'Gtk.Grid' instead
>> of them? This patch includes no lines to add rows/colums and to me no
>> requirement to use grid in this point.
> 
> The Gtk.VBox documentation[1] says:
> 
> “Deprecated since version 3.2: You can use Gtk.Box.new() with
> Gtk.Orientation.VERTICAL instead, which is a quick and easy change. But
> the recommendation is to switch to Gtk.Grid, since Gtk.Box is going to
> go away eventually. See Migrating from other containers to GtkGrid.”
> 
> Since no backwards incompatible change can be made in GTK+ 3.x, it is
> still present as of 3.24, but it has been removed in 4.0 and it’d be
> nice to support this one already when it’ll be released. :)

Thanks for your explanation. Indeed, gtk+ community obsoleted
gtk_vbox[1] and gtk_hbox[2] in their v3.89.1 release.

(g-i is a specification for metadata format for API of library.
PyGobject is a Python binding to handle the metadata and library. The
status of public API is decided by the library itself.)

Furthermore, gtk+ community published an instruction to use gtk_grid
instead of gtk_box[3].

Totally, your changes are good enough.

[1] Delete gtkvbox.{c,h} (fe24fcbc) · Commits · GNOME / gtk · GitLab
https://gitlab.gnome.org/GNOME/gtk/commit/fe24fcbc3e71bcf7e222a4106bf6e3f7ec114e28
[2] Remove GtkHBox (fb3d9022) · Commits · GNOME / gtk · GitLab
https://gitlab.gnome.org/GNOME/gtk/commit/fb3d9022ad98049c887cec5aeffd6b73deb043ba
[3] Migrating from other containers to GtkGrid: GTK+ 3 Reference Manual
https://developer.gnome.org/gtk3/stable/gtk-migrating-GtkGrid.html


Thanks

Takashi Sakamoto
diff mbox series

Patch

diff --git a/hwmixvolume/hwmixvolume b/hwmixvolume/hwmixvolume
index 64d232c..8e0b6b8 100755
--- a/hwmixvolume/hwmixvolume
+++ b/hwmixvolume/hwmixvolume
@@ -60,18 +60,18 @@  class Stream:
         value = alsahcontrol.Value(self.element)
         value.read()
         values = value.get_tuple(TYPE_INTEGER, info.count)
-        self.label = Gtk.Label(self.get_label(info))
+        self.label = Gtk.Label.new(self.get_label(info))
         self.label.set_single_line_mode(True)
-        self.parent.scales_vbox.pack_start(self.label, expand=False)
+        self.parent.scales_vbox.add(self.label)
         for i in range(info.count):
             adj = Gtk.Adjustment(value=values[i],
                     lower=info.min, upper=info.max,
                     step_incr=1,
                     page_incr=(info.max-info.min+1)/8)
             adj.connect('value-changed', self.update_ctl_from_scale, i)
-            scale = Gtk.HScale(adj)
+            scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, adjustment=adj)
             scale.set_draw_value(False)
-            self.parent.scales_vbox.pack_start(scale, expand=False)
+            self.parent.scales_vbox.add(scale)
             self.scales.append(scale)
             self.adjustments.append(adj)
         self.parent.scales_vbox.show_all()
@@ -174,45 +174,48 @@  class MixerWindow(Gtk.Window):
         self.connect('destroy', lambda w: Gtk.main_quit())
         self.set_title("Hardware Mixer Volumes")
 
-        vbox = Gtk.VBox()
+        vbox = Gtk.Grid()
+        vbox.set_orientation(Gtk.Orientation.VERTICAL)
         self.add(vbox)
 
-        hbox = Gtk.HBox()
-        vbox.pack_start(hbox, expand=False)
+        hbox = Gtk.Grid()
+        vbox.add(hbox)
 
-        label = Gtk.Label("_Sound Card: ")
-        label.set_use_underline(True)
-        hbox.pack_start(label, expand=False)
+        label = Gtk.Label.new_with_mnemonic("_Sound Card: ")
+        hbox.add(label)
 
-        combo = Gtk.combo_box_new_text()
+        combo = Gtk.ComboBoxText()
+        combo.set_hexpand(True)
         for i in self.card_numbers:
             str = "%d: %s" % (i, alsacard.card_get_name(i))
             combo.append_text(str)
         if len(self.card_numbers) > 0:
             combo.set_active(0)
         combo.connect('changed', lambda c: self.change_card(self.card_numbers[combo.get_active()]))
-        hbox.pack_start(combo)
+        hbox.add(combo)
         label.set_mnemonic_widget(combo)
 
-        self.lock_check = Gtk.CheckButton(label="_Lock Channels")
+        self.lock_check = Gtk.CheckButton.new_with_mnemonic(label="_Lock Channels")
         self.lock_check.set_active(True)
-        vbox.pack_start(self.lock_check, expand=False)
+        vbox.add(self.lock_check)
 
         scrollwin = Gtk.ScrolledWindow()
-        scrollwin.set_policy(hscrollbar_policy=Gtk.POLICY_NEVER, vscrollbar_policy=Gtk.POLICY_AUTOMATIC)
-        scrollwin.set_shadow_type(Gtk.SHADOW_NONE)
-        vbox.pack_start(scrollwin)
+        scrollwin.set_policy(hscrollbar_policy=Gtk.PolicyType.NEVER, vscrollbar_policy=Gtk.PolicyType.AUTOMATIC)
+        scrollwin.set_shadow_type(Gtk.ShadowType.NONE)
+        scrollwin.set_vexpand(True)
+        vbox.add(scrollwin)
 
-        self.scales_vbox = Gtk.VBox()
-        scrollwin.add_with_viewport(self.scales_vbox)
+        self.scales_vbox = Gtk.Grid()
+        self.scales_vbox.set_orientation(Gtk.Orientation.VERTICAL)
+        scrollwin.add(self.scales_vbox)
 
         label = Gtk.Label()
         label.set_single_line_mode(True)
-        line_height = label.size_request()[1]
+        line_height = max(label.get_size_request().height, 0)
         label.destroy()
-        scale = Gtk.HScale()
+        scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL)
         scale.set_draw_value(False)
-        line_height += scale.size_request()[1]
+        line_height += max(scale.get_size_request().height, 0)
         scale.destroy()
         # always have space for at least four sliders
         scrollwin.set_size_request(width=-1, height=line_height*4+4)
@@ -252,7 +255,7 @@  class MixerWindow(Gtk.Window):
             self.streams.append(stream)
 
         for fd,condition in self.hcontrol.poll_fds:
-            self.hctl_sources.append(GLib.io_add_watch(fd, condition, self.hctl_io_callback))
+            self.hctl_sources.append(GLib.io_add_watch(fd, 0, GLib.IOCondition(condition), self.hctl_io_callback))
 
         self.update_msg_label()
 
@@ -270,8 +273,9 @@  class MixerWindow(Gtk.Window):
             else:
                 msg = "This card does not have stream controls."
             if not has_msg:
-                self.msg_label = Gtk.Label(msg)
-                self.scales_vbox.pack_start(self.msg_label)
+                self.msg_label = Gtk.Label.new(msg)
+                self.msg_label.set_vexpand(True)
+                self.scales_vbox.add(self.msg_label)
                 self.scales_vbox.show_all()
             elif self.msg_label.get_text() != msg:
                 self.msg_label.set_text(msg)
@@ -284,8 +288,8 @@  class MixerWindow(Gtk.Window):
         except:
             # TODO: alsa error msg
             dlg = Gtk.MessageDialog(self,
-                    Gtk.DIALOG_MODAL | Gtk.DIALOG_DESTROY_WITH_PARENT,
-                    Gtk.MESSAGE_ERROR, Gtk.BUTTONS_OK,
+                    Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                    Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
                     "Cannot open sound card control device.")
             dlg.run()
             dlg.destroy()