[gnome-games/sudoku-css-theming: 1/4] Initial port to GtkStyleContext and gtk_reder_* functions



commit ab8bb1bb1511613fdcfbc4bb9ffa08f0eb5f6c3b
Author: John Stowers <john stowers gmail com>
Date:   Wed May 11 22:16:35 2011 +1200

    Initial port to GtkStyleContext and gtk_reder_* functions

 gnome-sudoku/src/lib/gsudoku.py    |    3 +
 gnome-sudoku/src/lib/number_box.py |  223 +++++++++++-------------------------
 2 files changed, 68 insertions(+), 158 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/gsudoku.py b/gnome-sudoku/src/lib/gsudoku.py
index 79a0e21..d0ca390 100644
--- a/gnome-sudoku/src/lib/gsudoku.py
+++ b/gnome-sudoku/src/lib/gsudoku.py
@@ -64,6 +64,9 @@ class SudokuNumberGrid (Gtk.AspectFrame):
             logging.critical("set_bg_color handed Bad color: %s" % color, exc_info=True)
             return
 
+        print "NOT OVERRIDING"
+        return
+
         self.eb.override_color(Gtk.StateFlags.NORMAL, color)
         self.eb.override_background_color(Gtk.StateFlags.NORMAL, color)
         self.table.override_color(Gtk.StateFlags.NORMAL, color)
diff --git a/gnome-sudoku/src/lib/number_box.py b/gnome-sudoku/src/lib/number_box.py
index 1b3a8ce..db120f0 100644
--- a/gnome-sudoku/src/lib/number_box.py
+++ b/gnome-sudoku/src/lib/number_box.py
@@ -21,17 +21,6 @@ NOTE_FONT_SIZE = Pango.SCALE * 6
 BORDER_WIDTH = 9.0 # The size of space we leave for a box
 NORMAL_LINE_WIDTH = 1 # The size of the line we draw around a box
 
-DEBUG_COLORS = False
-
-def debug_set_color_rgba(cr, rgba):
-    COLORS = ("red","green","blue","yellow","purple","wheat","maroon","gray")
-
-    if DEBUG_COLORS:
-        rgba = Gdk.RGBA()
-        rgba.parse(COLORS[random.randint(0,len(COLORS)-1)])
-
-    Gdk.cairo_set_source_rgba(cr, rgba)
-
 class NumberSelector (Gtk.EventBox):
 
     __gsignals__ = {
@@ -94,7 +83,6 @@ class NumberBox (Gtk.DrawingArea):
     text_color = None
     highlight_color = None
     shadow_color = None
-    custom_background_color = None
     border_color = None
 
     __gsignals__ = {
@@ -108,7 +96,6 @@ class NumberBox (Gtk.DrawingArea):
         'notes-changed':(GObject.SignalFlags.RUN_LAST, None, ()),
         }
 
-    base_state = Gtk.StateFlags.NORMAL
     npicker = None
     draw_boxes = False
 
@@ -117,15 +104,6 @@ class NumberBox (Gtk.DrawingArea):
         self.upper = upper
         self.parent_win = None
         self.timer = None
-        self.font = self.get_style().font_desc
-        self.font.set_size(BASE_FONT_SIZE)
-        self.note_font = self.font.copy()
-        self.note_font.set_size(NOTE_FONT_SIZE)
-        self._top_note_layout = Pango.Layout(self.create_pango_context())
-        self._top_note_layout.set_font_description(self.note_font)
-        self._bottom_note_layout = Pango.Layout(self.create_pango_context())
-        self._bottom_note_layout.set_font_description(self.note_font)
-        self._base_stateflags = Gtk.StateFlags.NORMAL
         self.top_note_list = []
         self.bottom_note_list = []
         self.tinfo = tracker_info.TrackerInfo()
@@ -138,8 +116,15 @@ class NumberBox (Gtk.DrawingArea):
         self.connect('focus-in-event', self.focus_in_cb)
         self.connect('focus-out-event', self.focus_out_cb)
         self.connect('motion-notify-event', self.motion_notify_cb)
-        self.set_text(text)
 
+        #should probbably do this in style-updated (but that gets called heaps
+        #because of how the background override is done...
+        style_ctx = self.get_style_context()
+        style_ctx.add_class(Gtk.STYLE_CLASS_BUTTON)
+        self._layout = self.create_pango_layout(text)
+        self._top_note_layout = Pango.Layout(self.create_pango_context())
+        self._bottom_note_layout = Pango.Layout(self.create_pango_context())
+        
     def set_parent_win(self, new_parent):
         self.parent_win = new_parent
 
@@ -147,20 +132,17 @@ class NumberBox (Gtk.DrawingArea):
         self.timer = new_timer
 
     def pointer_enter_cb (self, *args):
-        if not self.is_focus():
-            self.set_state_flags(Gtk.StateFlags.PRELIGHT, False)
+        self.set_state_flags(Gtk.StateFlags.PRELIGHT, False)
 
     def pointer_leave_cb (self, *args):
-        self.set_state_flags(self._base_stateflags, True)
+        self.unset_state_flags(Gtk.StateFlags.PRELIGHT)
         self._toggle_box_drawing_(False)
 
     def focus_in_cb (self, *args):
-        self.set_state_flags(Gtk.StateFlags.SELECTED, True)
-        self._base_stateflags = Gtk.StateFlags.SELECTED
+        self.set_state_flags(Gtk.StateFlags.SELECTED, False)
 
     def focus_out_cb (self, *args):
-        self.set_state_flags(Gtk.StateFlags.NORMAL, True)
-        self._base_stateflags = Gtk.StateFlags.NORMAL
+        self.unset_state_flags(Gtk.StateFlags.SELECTED)
         self.destroy_npicker()
 
     def destroy_npicker (self):
@@ -346,26 +328,9 @@ class NumberBox (Gtk.DrawingArea):
         self.queue_draw()
         self.emit('changed')
 
-    def set_font (self, font):
-        if type(font) == str:
-            font = Pango.FontDescription(font)
-        self.font = font
-        if self.text:
-            self.set_text(self.text)
-        self.queue_draw()
-
-    def set_note_font (self, font):
-        if type(font) == str:
-            font = Pango.FontDescription(font)
-        self.note_font = font
-        self._top_note_layout.set_font_description(font)
-        self._bottom_note_layout.set_font_description(font)
-        self.queue_draw()
-
     def set_text (self, text):
         self.text = text
-        self._layout = self.create_pango_layout(text)
-        self._layout.set_font_description(self.font)
+        self._layout.set_text(text, -1)
 
     def show_note_text (self):
         '''Display the notes for the current view
@@ -527,10 +492,16 @@ class NumberBox (Gtk.DrawingArea):
         h = self.get_allocated_height()
         style_ctx = self.get_style_context()
 
-        self.draw_background_color(cr, style_ctx, w, h)
+        print style_ctx.list_classes()
+        print self.get_state_flags()
+
+        Gtk.render_background(style_ctx, cr, 0, 0, w, h)
+
         if self.is_focus():
-            self.draw_highlight_box(cr, style_ctx, w, h)
+            Gtk.render_focus(style_ctx, cr, 0, 0, w, h)
+
         if self.border_color is not None:
+            print "border"
             border_width = 3.0
             cr.set_source_rgb(*self.border_color)
             cr.rectangle(border_width*0.5, border_width*0.5, w-border_width, h-border_width)
@@ -547,82 +518,24 @@ class NumberBox (Gtk.DrawingArea):
         if self.draw_boxes and self.is_focus():
             self.draw_note_area_highlight_box(cr, style_ctx)
 
-    def draw_background_color (self, cr, style_ctx, w, h):
-        if self.read_only:
-            if self.custom_background_color:
-                r, g, b = self.custom_background_color
-                cr.set_source_rgb(
-                    r*0.6, g*0.6, b*0.6
-                    )
-            else:
-                #cr.set_source_color(self.style.base[Gtk.StateFlags.INSENSITIVE])
-                #Gdk.cairo_set_source_rgba(
-                debug_set_color_rgba(
-                        cr, style_ctx.get_color(Gtk.StateFlags.INSENSITIVE))
-        elif self.is_focus():
-            #cr.set_source_color(self.style.base[Gtk.StateFlags.SELECTED])
-            #Gdk.cairo_set_source_rgba(
-            debug_set_color_rgba(
-                    cr, style_ctx.get_color(Gtk.StateFlags.SELECTED))
-        elif self.custom_background_color:
-            cr.set_source_rgb(*self.custom_background_color)
-        else:
-            #cr.set_source_color(
-            #    self.style.base[self.state]
-            #    )
-            #Gdk.cairo_set_source_rgba(
-            debug_set_color_rgba(
-                    cr, style_ctx.get_color(self.get_state_flags()))
-        cr.rectangle(
-            0, 0, w, h,
-            )
-        cr.fill()
-
-    def draw_highlight_box (self, cr, style_ctx, w, h):
-        #cr.set_source_color(
-        #    self.style.base[Gtk.StateFlags.SELECTED]
-        #    )
-        #Gdk.cairo_set_source_rgba(
-        debug_set_color_rgba(
-                    cr, style_ctx.get_color(Gtk.StateFlags.SELECTED))
-
-        border = 4 * w / BASE_SIZE
-        cr.rectangle(
-            # left-top
-            border*0.5,
-            border*0.5,
-            # bottom-right
-            w-border,
-            h-border,
-            )
-        cr.set_line_width(border)
-        cr.stroke()
-
     def draw_note_area_highlight_box (self, cr, style_ctx):
-        # set up our paint brush...
-        #cr.set_source_color(
-        #    self.style.mid[self.state]
-        #    )
-        #Gdk.cairo_set_source_rgba(
-        debug_set_color_rgba(
-                cr, style_ctx.get_color(self.get_state_flags()))
-
-        cr.set_line_width(NORMAL_LINE_WIDTH)
         # top rectangle
-        cr.rectangle(NORMAL_LINE_WIDTH*0.5,
-                     NORMAL_LINE_WIDTH*0.5,
-                     BASE_SIZE-NORMAL_LINE_WIDTH,
-                     BORDER_WIDTH-NORMAL_LINE_WIDTH)
-        cr.stroke()
+        Gtk.render_focus(style_ctx, cr,
+                    NORMAL_LINE_WIDTH*0.5,
+                    NORMAL_LINE_WIDTH*0.5,
+                    BASE_SIZE-NORMAL_LINE_WIDTH,
+                    BORDER_WIDTH-NORMAL_LINE_WIDTH)
         # bottom rectangle
-        cr.rectangle(NORMAL_LINE_WIDTH*0.5, #x
-                     BASE_SIZE - BORDER_WIDTH-(NORMAL_LINE_WIDTH*0.5), #y
-                     BASE_SIZE-NORMAL_LINE_WIDTH, #x2
-                     BASE_SIZE-NORMAL_LINE_WIDTH #y2
-                     )
-        cr.stroke()
+        Gtk.render_focus(style_ctx, cr,
+                    NORMAL_LINE_WIDTH*0.5,
+                    BASE_SIZE - BORDER_WIDTH-(NORMAL_LINE_WIDTH*0.5),
+                    BASE_SIZE-NORMAL_LINE_WIDTH,
+                    BASE_SIZE-NORMAL_LINE_WIDTH)
 
     def draw_text (self, cr, style_ctx):
+        font = style_ctx.get_font(self.get_state_flags())
+        font.set_size(BASE_FONT_SIZE)
+        self._layout.set_font_description(font)
         fontw, fonth = self._layout.get_pixel_size()
         # Draw a shadow for tracked conflicts.  This is done to
         # differentiate between tracked and untracked conflicts.
@@ -633,48 +546,33 @@ class NumberBox (Gtk.DrawingArea):
                 PangoCairo.show_layout(cr, self._layout)
         if self.text_color:
             cr.set_source_rgb(*self.text_color)
-        elif self.read_only:
-            #cr.set_source_color(self.style.text[Gtk.StateFlags.NORMAL])
-            #Gdk.cairo_set_source_rgba(
-            debug_set_color_rgba(
-                cr, style_ctx.get_color(Gtk.StateFlags.NORMAL))
-        else:
-            #cr.set_source_color(self.style.text[self.state])
-            #Gdk.cairo_set_source_rgba(
-            debug_set_color_rgba(
-                cr, style_ctx.get_color(Gtk.StateFlags.NORMAL))
 
         # And draw the text in the middle of the allocated space
         if self._layout:
-            cr.move_to(
-                (BASE_SIZE/2)-(fontw/2),
+            Gtk.render_layout(style_ctx, cr,
+                (BASE_SIZE/2) - (fontw/2),
                 (BASE_SIZE/2) - (fonth/2),
-                )
-            PangoCairo.update_layout(cr, self._layout)
-            PangoCairo.show_layout(cr, self._layout)
-
-        #cr.set_source_color(self.style.text[self.state])
-        #Gdk.cairo_set_source_rgba(
-        debug_set_color_rgba(
-            cr, style_ctx.get_color(Gtk.StateFlags.NORMAL))
+                self._layout)
 
         # And draw any note text...
+        if self._top_note_layout or self._bottom_note_layout:
+            note_font = font.copy()
+            note_font.set_size(NOTE_FONT_SIZE)
+
         if self._top_note_layout:
+            self._top_note_layout.set_font_description(note_font)
             fontw, fonth = self._top_note_layout.get_pixel_size()
-            cr.move_to(
+            Gtk.render_layout(style_ctx, cr,
                 NORMAL_LINE_WIDTH,
                 0,
-                )
-            PangoCairo.update_layout(cr, self._top_note_layout)
-            PangoCairo.show_layout(cr, self._top_note_layout)
+                self._top_note_layout)
         if self._bottom_note_layout:
+            self._bottom_note_layout.set_font_description(note_font)
             fontw, fonth = self._bottom_note_layout.get_pixel_size()
-            cr.move_to(
+            Gtk.render_layout(style_ctx, cr,
                 NORMAL_LINE_WIDTH,
                 BASE_SIZE-fonth,
-                )
-            PangoCairo.update_layout(cr, self._bottom_note_layout)
-            PangoCairo.show_layout(cr, self._bottom_note_layout)
+                self._bottom_note_layout)
 
     def set_text_color (self, color, shadow = None):
         self.shadow_color = shadow
@@ -682,8 +580,20 @@ class NumberBox (Gtk.DrawingArea):
         self.queue_draw()
 
     def set_background_color (self, color):
-        self.custom_background_color = color
-        self.queue_draw()
+        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 (Gtk.StateFlags.INSENSITIVE, Gtk.StateFlags.NORMAL):
+            if color:
+                self.override_background_color(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, style_ctx.get_color(s))
+
 
     def set_border_color (self, color):
         self.border_color = color
@@ -757,14 +667,11 @@ class SudokuNumberBox (NumberBox):
 
     def set_read_only (self, val):
         self.read_only = val
-        if not hasattr(self, 'bold_font'):
-            self.normal_font = self.font
-            self.bold_font = self.font.copy()
-            self.bold_font.set_weight(Pango.Weight.BOLD)
-        if self.read_only:
-            self.set_font(self.bold_font)
+        style_ctx = self.get_style_context()
+        if val:
+            self.set_state_flags(Gtk.StateFlags.INSENSITIVE, False)
         else:
-            self.set_font(self.normal_font)
+            self.unset_state_flags(Gtk.StateFlags.INSENSITIVE)
         self.queue_draw()
 
     def set_impossible (self, val):



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