[gnome-games/sudoku-css-theming] More CSS work



commit 24b5b3d5a4886babfbdbac5f0fe70d43fda00e1e
Author: John Stowers <john stowers gmail com>
Date:   Tue May 24 12:26:59 2011 +1200

    More CSS work
    
    	* Set color overrides the forground (text color)
    	* Set junction-sides on NumberBox

 gnome-sudoku/src/lib/number_box.py |   97 +++++++++++++++++++++--------------
 1 files changed, 58 insertions(+), 39 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/number_box.py b/gnome-sudoku/src/lib/number_box.py
index 819e9f9..fb142f6 100644
--- a/gnome-sudoku/src/lib/number_box.py
+++ b/gnome-sudoku/src/lib/number_box.py
@@ -25,49 +25,58 @@ class NumberSelector (Gtk.EventBox):
         'changed':(GObject.SignalFlags.RUN_LAST, None, ()),
         }
 
+    JUNCTION_SIDES_Y = (Gtk.JunctionSides.BOTTOM, Gtk.JunctionSides.BOTTOM | Gtk.JunctionSides.TOP, Gtk.JunctionSides.TOP)
+    JUNCTION_SIDES_X = (Gtk.JunctionSides.RIGHT, Gtk.JunctionSides.LEFT | Gtk.JunctionSides.RIGHT, Gtk.JunctionSides.LEFT)
+
     def __init__ (self, default = None, upper = 9):
-        self.value = default
+        self._value = default
         GObject.GObject.__init__(self)
         self.table = Gtk.Table()
         self.add(self.table)
         side = int(math.sqrt(upper))
-        n = 1
         for y in range(side):
             for x in range(side):
-                b = Gtk.Button()
+                n = (y)*side + (x+1)
+
                 l = Gtk.Label()
-                if n == self.value:
+                l.set_padding(0, 0)
+                if n == self._value:
                     l.set_markup('<b><span size="x-small">%s</span></b>'%n)
                 else:
                     l.set_markup('<span size="x-small">%s</span>'%n)
+
+                b = Gtk.Button()
+                #add junction sides (and extra for the clear button)
+                if y == (side-1):
+                    extra_junction = Gtk.JunctionSides.BOTTOM
+                else:
+                    extra_junction = Gtk.JunctionSides.NONE
+                b.get_style_context().set_junction_sides(
+                    self.JUNCTION_SIDES_Y[y] | self.JUNCTION_SIDES_X[x] | extra_junction)
                 b.add(l)
-                b.set_relief(Gtk.ReliefStyle.HALF)
-                l = b.get_children()[0]
-                b.set_border_width(0)
-                l.set_padding(0, 0)
-                l.get_alignment()
                 b.connect('clicked', self.number_clicked, n)
+
                 self.table.attach(b, x, x+1, y, y+1)
-                n += 1
-        if self.value:
-            db = Gtk.Button()
+        if self._value:
             l = Gtk.Label()
             l.set_markup_with_mnemonic('<span size="x-small">'+_('_Clear')+'</span>')
-            db.add(l)
-            l.show()
-            db.connect('clicked', self.number_clicked, 0)
-            self.table.attach(db, 0, side, side + 1, side + 2)
+            b = Gtk.Button()
+            b.get_style_context().set_junction_sides(Gtk.JunctionSides.TOP)
+            b.add(l)
+            b.connect('clicked', self.number_clicked, 0)
+            self.table.attach(b, 0, side, side + 1, side + 2)
+
         self.show_all()
 
     def number_clicked (self, button, n):
-        self.value = n
+        self._value = n
         self.emit('changed')
 
     def get_value (self):
-        return self.value
+        return self._value
 
     def set_value (self, n):
-        self.value = n
+        self._value = n
 
 class NumberBox (Gtk.DrawingArea):
 
@@ -78,12 +87,13 @@ class NumberBox (Gtk.DrawingArea):
     _layout = None
     _top_note_layout = None
     _bottom_note_layout = None
-    text_color = None
     highlight_color = None
     shadow_color = None
     border_color = None
 
     NOTE_STYLE_CLASS = Gtk.STYLE_CLASS_ENTRY
+
+    OVERRIDE_COLOR_STATES = (Gtk.StateFlags.INSENSITIVE, Gtk.StateFlags.NORMAL)
     OVERRIDE_BACKGROUND_COLOR_STATES = (Gtk.StateFlags.INSENSITIVE, Gtk.StateFlags.NORMAL)
 
     __gtype_name__ = 'NumberBox'
@@ -135,8 +145,11 @@ class NumberBox (Gtk.DrawingArea):
         #FIXME: should not need to do this, see
         #https://bugzilla.gnome.org/show_bug.cgi?id=649779
         self._saved_background = {}
+        self._saved_colors = {}
         for s in self.OVERRIDE_BACKGROUND_COLOR_STATES:
             self._saved_background[s] = style_ctx.get_background_color(s)
+        for s in self.OVERRIDE_COLOR_STATES:
+            self._saved_colors[s] = style_ctx.get_color(s)
         
     def set_parent_win(self, new_parent):
         self.parent_win = new_parent
@@ -566,8 +579,6 @@ class NumberBox (Gtk.DrawingArea):
             for xoff, yoff in [(1,1),(2,2)]:
                 cr.move_to((BASE_SIZE/2)-(fontw/2) + xoff, (BASE_SIZE/2) - (fonth/2) + yoff)
                 PangoCairo.show_layout(cr, self._layout)
-        if self.text_color:
-            cr.set_source_rgb(*self.text_color)
 
         # And draw the text in the middle of the allocated space
         if self._layout:
@@ -592,25 +603,33 @@ class NumberBox (Gtk.DrawingArea):
                 self._bottom_note_layout)
         style_ctx.restore()
 
-    def set_text_color (self, color, shadow = None):
-        print "text -", color
-        self.shadow_color = shadow
-        self.text_color = color
-        self.queue_draw()
-
-    def set_background_color (self, color):
+    def _override_widget_color(self, color, override_func, states, saved):
         if color:
             #convert tuple to correct type, None means restore to default so fall through
             color = Gdk.RGBA(*color)
 
         style_ctx = self.get_style_context()
-        for s in self.OVERRIDE_BACKGROUND_COLOR_STATES:
+        for s in states:
             if color:
-                self.override_background_color(s, color)
+                override_func(s, color)
             else:
                 #FIXME: should not need to do this, see
                 #https://bugzilla.gnome.org/show_bug.cgi?id=649779
-                self.override_background_color(s, self._saved_background[s])
+                override_func(s, saved[s])
+
+
+    def _set_text_color (self, color, shadow = None):
+        self.shadow_color = shadow
+        self._override_widget_color(color,
+                self.override_color,
+                self.OVERRIDE_COLOR_STATES,
+                self._saved_colors)
+
+    def set_background_color (self, color):
+        self._override_widget_color(color,
+                self.override_background_color,
+                self.OVERRIDE_BACKGROUND_COLOR_STATES,
+                self._saved_background)
 
     def set_border_color (self, color):
         self.border_color = color
@@ -654,7 +673,7 @@ class SudokuNumberBox (NumberBox):
         else:
             self.tracker_id = tracker_id
         self.normal_color = self.tinfo.get_color(self.tracker_id)
-        self.set_text_color(self.normal_color)
+        self._set_text_color(self.normal_color)
         super(SudokuNumberBox, self).set_value(val)
 
     def get_value_for_undo(self):
@@ -671,16 +690,16 @@ class SudokuNumberBox (NumberBox):
 
     def recolor(self, tracker_id):
         self.normal_color = self.tinfo.get_color(tracker_id)
-        self.set_text_color(self.normal_color)
+        self._set_text_color(self.normal_color)
 
     def set_error_highlight (self, val):
         if val:
             if (self.tracker_id != tracker_info.NO_TRACKER):
-                self.set_text_color(self.error_color, self.normal_color)
+                self._set_text_color(self.error_color, self.normal_color)
             else:
-                self.set_text_color(self.error_color)
+                self._set_text_color(self.error_color)
         else:
-            self.set_text_color(self.normal_color)
+            self._set_text_color(self.normal_color)
 
     def set_read_only (self, val):
         self.read_only = val
@@ -695,10 +714,10 @@ class SudokuNumberBox (NumberBox):
         if val:
             if not self.get_text():
                 self.set_text('X')
-                self.set_text_color(self.error_color)
+                self._set_text_color(self.error_color)
         elif self.get_text() == 'X':
             self.set_text('')
-            self.set_text_color(self.normal_color)
+            self._set_text_color(self.normal_color)
         self.queue_draw()
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]