From d290917e982ba4473a03e5a7e1acaa1e50c59f3d Mon Sep 17 00:00:00 2001
From: Sebastian Koslowski <koslowski@kit.edu>
Date: Fri, 27 May 2016 15:19:11 +0200
Subject: grc: gtk3: update props dialog

---
 grc/gui/Param.py | 149 ++++++++++++++++++++++++++++---------------------------
 1 file changed, 75 insertions(+), 74 deletions(-)

(limited to 'grc/gui/Param.py')

diff --git a/grc/gui/Param.py b/grc/gui/Param.py
index 1c5b0c9c8c..a087d4b337 100644
--- a/grc/gui/Param.py
+++ b/grc/gui/Param.py
@@ -19,12 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
 import os
 
-import gi
-gi.require_version('Gtk', '3.0')
-from gi.repository import Gtk
-from gi.repository import Gdk
-from gi.repository import GObject
-
+from gi.repository import Gtk, Gdk
 
 from . import Colors, Utils, Constants
 from .Element import Element
@@ -37,17 +32,20 @@ class InputParam(Gtk.HBox):
     expand = False
 
     def __init__(self, param, changed_callback=None, editing_callback=None):
-        GObject.GObject.__init__(self)
+        Gtk.HBox.__init__(self)
+
         self.param = param
         self._changed_callback = changed_callback
         self._editing_callback = editing_callback
-        self.label = Gtk.Label() #no label, markup is added by set_markup
+
+        self.label = Gtk.Label()
         self.label.set_size_request(150, -1)
-        self.pack_start(self.label, False)
-        self.set_markup = lambda m: self.label.set_markup(m)
+        self.label.show()
+        self.pack_start(self.label, False, False, 0)
+
         self.tp = None
         self._have_pending_changes = False
-        #connect events
+
         self.connect('show', self._update_gui)
 
     def set_color(self, color):
@@ -63,40 +61,17 @@ class InputParam(Gtk.HBox):
         """
         Set the markup, color, tooltip, show/hide.
         """
-        param = self.param
+        self.label.set_markup(self.param.format_label_markup(self._have_pending_changes))
 
-        has_callback = \
-            hasattr(param.get_parent(), 'get_callbacks') and \
-            any(param.get_key() in callback for callback in param.get_parent()._callbacks)
+        # fixme: find a non-deprecated way to change colors
+        # self.set_color(Colors.PARAM_ENTRY_COLORS.get(
+        #     self.param.get_type(), Colors.PARAM_ENTRY_DEFAULT_COLOR)
+        # )
 
-        self.set_markup('<span underline="{line}" foreground="{color}" font_desc="Sans 9">{label}</span>'.format(
-            line='low' if has_callback else 'none',
-            color='blue' if self._have_pending_changes else
-                  'black' if param.is_valid() else
-                  'red',
-            label=Utils.encode(self.param.get_name())
-        ))
-
-        self.set_color(Colors.PARAM_ENTRY_COLORS.get(
-            self.param.get_type(), Colors.PARAM_ENTRY_DEFAULT_COLOR)
-        )
-
-        errors = param.get_error_messages()
-        tooltip_lines = ['Key: ' + param.get_key(), 'Type: ' + param.get_type()]
-        if param.is_valid():
-            value = str(param.get_evaluated())
-            if len(value) > 100:
-                value = '{}...{}'.format(value[:50], value[-50:])
-            tooltip_lines.append('Value: ' + value)
-        elif len(errors) == 1:
-            tooltip_lines.append('Error: ' + errors[0])
-        elif len(errors) > 1:
-            tooltip_lines.append('Error:')
-            tooltip_lines.extend(' * ' + msg for msg in errors)
-        self.set_tooltip_text('\n'.join(tooltip_lines))
+        self.set_tooltip_text(self.param.format_tooltip_text())
 
         if self.param.get_hide() == 'all':
-            self.hide_all()
+            self.hide()
         else:
             self.show_all()
 
@@ -146,7 +121,7 @@ class EntryParam(InputParam):
         self._input.connect('changed', self._mark_changed)
         self._input.connect('focus-out-event', self._apply_change)
         self._input.connect('key-press-event', self._handle_key_press)
-        self.pack_start(self._input, True)
+        self.pack_start(self._input, True, True, 0)
 
     def get_text(self):
         return self._input.get_text()
@@ -155,10 +130,7 @@ class EntryParam(InputParam):
         self._input.override_background_color(Gtk.StateType.NORMAL, color)
 
     def set_tooltip_text(self, text):
-        try:
-            self._input.set_tooltip_text(text)
-        except AttributeError:
-            pass  # no tooltips for old GTK
+        self._input.set_tooltip_text(text)
 
 
 class MultiLineEntryParam(InputParam):
@@ -171,29 +143,29 @@ class MultiLineEntryParam(InputParam):
         self._buffer.set_text(self.param.get_value())
         self._buffer.connect('changed', self._mark_changed)
 
-        self._view = Gtk.TextView(self._buffer)
+        self._view = Gtk.TextView()
+        self._view.set_buffer(self._buffer)
         self._view.connect('focus-out-event', self._apply_change)
         self._view.connect('key-press-event', self._handle_key_press)
+        # fixme: add border to TextView
 
         self._sw = Gtk.ScrolledWindow()
         self._sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
         self._sw.add_with_viewport(self._view)
 
-        self.pack_start(self._sw, True)
+        self.pack_start(self._sw, True, True, True)
 
     def get_text(self):
         buf = self._buffer
-        return buf.get_text(buf.get_start_iter(),
-                            buf.get_end_iter()).strip()
+        text = buf.get_text(buf.get_start_iter(), buf.get_end_iter(),
+                            include_hidden_chars=False)
+        return text.strip()
 
     def set_color(self, color):
         self._view.override_background_color(Gtk.StateType.NORMAL, color)
 
     def set_tooltip_text(self, text):
-        try:
-            self._view.set_tooltip_text(text)
-        except AttributeError:
-            pass  # no tooltips for old GTK
+        self._view.set_tooltip_text(text)
 
 
 # try:
@@ -264,16 +236,13 @@ class EnumParam(InputParam):
         self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
         self._input.connect('changed', self._editing_callback)
         self._input.connect('changed', self._apply_change)
-        self.pack_start(self._input, False)
+        self.pack_start(self._input, False, False, 0)
 
     def get_text(self):
         return self.param.get_option_keys()[self._input.get_active()]
 
     def set_tooltip_text(self, text):
-        try:
-            self._input.set_tooltip_text(text)
-        except AttributeError:
-            pass  # no tooltips for old GTK
+        self._input.set_tooltip_text(text)
 
 
 class EnumEntryParam(InputParam):
@@ -281,17 +250,23 @@ class EnumEntryParam(InputParam):
 
     def __init__(self, *args, **kwargs):
         InputParam.__init__(self, *args, **kwargs)
-        self._input = Gtk.combo_box_entry_new_text()
-        for option in self.param.get_options(): self._input.append_text(option.get_name())
-        try: self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
-        except:
+        self._input = Gtk.ComboBoxText.new_with_entry()
+        for option in self.param.get_options():
+            self._input.append_text(option.get_name())
+
+        value = self.param.get_value()
+        try:
+            active_index = self.param.get_option_keys().index(value)
+            self._input.set_active(active_index)
+        except ValueError:
             self._input.set_active(-1)
-            self._input.get_child().set_text(self.param.get_value())
+            self._input.get_child().set_text(value)
+
         self._input.connect('changed', self._apply_change)
         self._input.get_child().connect('changed', self._mark_changed)
         self._input.get_child().connect('focus-out-event', self._apply_change)
         self._input.get_child().connect('key-press-event', self._handle_key_press)
-        self.pack_start(self._input, False)
+        self.pack_start(self._input, False, False, 0)
 
     @property
     def has_custom_value(self):
@@ -302,13 +277,10 @@ class EnumEntryParam(InputParam):
         return self.param.get_option_keys()[self._input.get_active()]
 
     def set_tooltip_text(self, text):
-        try:
-            if self._input.get_active() == -1: #custom entry
-                self._input.get_child().set_tooltip_text(text)
-            else:
-                self._input.set_tooltip_text(text)
-        except AttributeError:
-            pass  # no tooltips for old GTK
+        if self.has_custom_value:  # custom entry
+            self._input.get_child().set_tooltip_text(text)
+        else:
+            self._input.set_tooltip_text(text)
 
     def set_color(self, color):
         self._input.get_child().modify_base(
@@ -324,7 +296,7 @@ class FileParam(EntryParam):
         EntryParam.__init__(self, *args, **kwargs)
         input = Gtk.Button('...')
         input.connect('clicked', self._handle_clicked)
-        self.pack_start(input, False)
+        self.pack_start(input, False, False, 0)
 
     def _handle_clicked(self, widget=None):
         """
@@ -403,7 +375,36 @@ class Param(Element, _Param):
 
         return input_widget
 
-    def get_markup(self):
+    def format_label_markup(self, have_pending_changes=False):
+        block = self.get_parent()
+        has_callback = \
+            hasattr(block, 'get_callbacks') and \
+            any(self.get_key() in callback for callback in block._callbacks)
+
+        return '<span underline="{line}" foreground="{color}" font_desc="Sans 9">{label}</span>'.format(
+            line='low' if has_callback else 'none',
+            color='blue' if have_pending_changes else
+            'black' if self.is_valid() else
+            'red',
+            label=Utils.encode(self.get_name())
+        )
+
+    def format_tooltip_text(self):
+        errors = self.get_error_messages()
+        tooltip_lines = ['Key: ' + self.get_key(), 'Type: ' + self.get_type()]
+        if self.is_valid():
+            value = str(self.get_evaluated())
+            if len(value) > 100:
+                value = '{}...{}'.format(value[:50], value[-50:])
+            tooltip_lines.append('Value: ' + value)
+        elif len(errors) == 1:
+            tooltip_lines.append('Error: ' + errors[0])
+        elif len(errors) > 1:
+            tooltip_lines.append('Error:')
+            tooltip_lines.extend(' * ' + msg for msg in errors)
+        return '\n'.join(tooltip_lines)
+
+    def format_block_surface_markup(self):
         """
         Get the markup for this param.
 
-- 
cgit v1.2.3