Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/number_box.py
blob: a95c378a4c03614776085d6ca5fa064c38eb7c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# -*- coding: utf-8 -*-
# This code is from GNOME Sudoku
# Adapted by: Daniel Francis

from gi.repository import Gtk,Gdk,GObject,Pango,PangoCairo
import math
import random
import tracker_info
from gettext import gettext as _

# simple_debug
import logging
logger = logging.getLogger('number_box')
simple_debug = logger.debug

ERROR_HIGHLIGHT_COLOR = (1.0, 0, 0)

BASE_SIZE = 35 # The "normal" size of a box (in pixels)

# And the standard font-sizes -- these should fit nicely with the
# BASE_SIZE
BASE_FONT_SIZE = Pango.SCALE * 13
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__ = {
        'changed':(GObject.SignalFlags.RUN_LAST, None, ()),
        }

    def __init__ (self, default = None, upper = 9):
        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()
                l = Gtk.Label()
                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.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()
            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)
        self.show_all()

    def number_clicked (self, button, n):
        self.value = n
        self.emit('changed')

    def get_value (self):
        return self.value

    def set_value (self, n):
        self.value = n

class NumberBox (Gtk.DrawingArea):

    text = ''
    top_note_text = ''
    bottom_note_text = ''
    read_only = False
    _layout = None
    _top_note_layout = None
    _bottom_note_layout = None
    text_color = None
    highlight_color = None
    shadow_color = None
    custom_background_color = None
    border_color = None

    __gsignals__ = {
        'value-about-to-change':(GObject.SignalFlags.RUN_LAST, None, ()),
        'notes-about-to-change':(GObject.SignalFlags.RUN_LAST, None, ()),
        'changed':(GObject.SignalFlags.RUN_LAST, None, ()),
        # undo-change - A hacky way to handle the fact that we want to
        # respond to undo's changes but we don't want undo to respond
        # to itself...
        'undo-change':(GObject.SignalFlags.RUN_LAST, None, ()),
        'notes-changed':(GObject.SignalFlags.RUN_LAST, None, ()),
        }

    base_state = Gtk.StateFlags.NORMAL
    npicker = None
    draw_boxes = False

    def __init__ (self, upper = 9, text = ''):
        Gtk.DrawingArea.__init__(self)
        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()
        self.set_property('can-focus', True)
        self.set_property('events', Gdk.EventMask.ALL_EVENTS_MASK)
        self.connect('button-press-event', self.button_press_cb)
        self.connect('key-release-event', self.key_press_cb)
        self.connect('enter-notify-event', self.pointer_enter_cb)
        self.connect('leave-notify-event', self.pointer_leave_cb)
        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)

    def set_parent_win(self, new_parent):
        self.parent_win = new_parent

    def set_timer(self, new_timer):
        self.timer = new_timer

    def pointer_enter_cb (self, *args):
        if not self.is_focus():
            self.set_state_flags(Gtk.StateFlags.PRELIGHT, False)

    def pointer_leave_cb (self, *args):
        self.set_state_flags(self._base_stateflags, True)
        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

    def focus_out_cb (self, *args):
        self.set_state_flags(Gtk.StateFlags.NORMAL, True)
        self._base_stateflags = Gtk.StateFlags.NORMAL
        self.destroy_npicker()

    def destroy_npicker (self):
        if self.npicker:
            self.npicker.destroy()
            self.npicker = None

    def motion_notify_cb (self, *args):
        if self.is_focus() and not self.read_only:
            self._toggle_box_drawing_(True)
        else:
            self._toggle_box_drawing_(False)

    def _toggle_box_drawing_ (self, val):
        if val and not self.draw_boxes:
            self.draw_boxes = True
            self.queue_draw()
        if (not val) and self.draw_boxes:
            self.draw_boxes = False
            self.queue_draw()

    def button_press_cb (self, w, e):
        if self.read_only:
            return
        if e.type == Gdk.EventType._2BUTTON_PRESS:
            # ignore second click (this makes a double click in the
            # middle of a cell get us a display of the numbers, rather
            # than selecting a number.
            return
        if self.is_focus():
            x, y = e.get_coords()
            alloc = self.get_allocation()
            my_w = alloc.width
            my_h = alloc.height
            border_height = float(BORDER_WIDTH)/BASE_SIZE

            if float(y)/my_h < border_height:
                self.show_note_editor(top = True)
            elif float(y)/my_h > (1-border_height):
                self.show_note_editor(top = False)
            elif not self.npicker:
                # In this case we're a normal old click...
                # makes sure there is only one numer selector
                self.show_number_picker()
        else:
            self.grab_focus()

    def key_press_cb (self, w, e):
        if self.read_only:
            return
        if self.npicker: # kill number picker no matter what is pressed
            self.destroy_npicker()
        txt = Gdk.keyval_name(e.keyval)
        if type(txt) == type(None):
            # Make sure we don't trigger on unplugging the A/C charger etc
            return
        txt = txt.replace('KP_', '')

        # Add the new value if need be
        if txt in [str(n) for n in range(1, self.upper+1)]:
            if e.state & Gdk.ModifierType.CONTROL_MASK:
                self.add_note_text(txt, top = True)
            elif e.state & Gdk.ModifierType.MOD1_MASK:
                self.remove_note_text(txt, top = True)
            elif self.get_text() != txt or \
                (self.tracker_id != tracker_info.NO_TRACKER and
                 self.tinfo.current_tracker == tracker_info.NO_TRACKER):
                # If there's no change, do nothing unless the player wants to
                # change a tracked item while not tracking(ie commit a tracked
                # change)
                self.set_text_interactive(txt)
        elif txt in ['0', 'Delete', 'BackSpace']:
            self.set_text_interactive('')
        elif txt in ['n', 'N']:
            if e.state & Gdk.ModifierType.MOD1_MASK:
                self.set_note_text_interactive(top_text = '')
            else:
                self.show_note_editor(top = True)
        elif txt in ['m', 'M']:
            if e.state & Gdk.ModifierType.MOD1_MASK:
                self.set_note_text_interactive(bottom_text = '')
            else:
                self.show_note_editor(top = False)

    def add_note_text(self, txt, top = False):
        if top:
            note = self.top_note_text
        else:
            note = self.bottom_note_text
        if txt not in note:
            tmp = list(note)
            tmp.append(txt)
            tmp.sort()
            note = ''.join(tmp)
            if top:
                self.set_note_text_interactive(top_text = note)
            else:
                self.set_note_text_interactive(bottom_text = note)

    def remove_note_text(self, txt, top = False):
        if top:
            note = self.top_note_text
        else:
            note = self.bottom_note_text
        if txt in note:
            note = note.replace(txt,'')
            if top:
                self.set_note_text_interactive(top_text = note)
            else:
                self.set_note_text_interactive(bottom_text = note)

    def note_changed_cb (self, w, top = False):
        if top:
            self.set_note_text_interactive(top_text = w.get_text())
        else:
            self.set_note_text_interactive(bottom_text = w.get_text())

    def note_focus_in(self, win, evt):
        if (self.timer):
            self.timer.resume_timing()

    def note_focus_out(self, wgt, evt):
        if (self.timer):
            self.timer.pause_timing()

    def show_note_editor (self, top = True):
        alloc = self.get_allocation()
        w = Gtk.Window()
        w.set_property('skip-pager-hint', True)
        w.set_property('skip-taskbar-hint', True)
        w.set_decorated(False)
        w.set_position(Gtk.WindowPosition.MOUSE)
        w.set_size_request(alloc.width, alloc.height/2)
        if self.parent_win:
            w.set_transient_for(self.parent_win)
        f = Gtk.Frame()
        e = Gtk.Entry()
        f.add(e)
        if top:
            e.set_text(self.top_note_text)
        else:
            e.set_text(self.bottom_note_text)
        w.add(f)
        e.connect('changed', self.note_changed_cb, top)
        e.connect('focus-in-event', self.note_focus_in)
        e.connect('focus-out-event', lambda e, ev, w: w.destroy(), w)
        e.connect('focus-out-event', self.note_focus_out)
        e.connect('activate', lambda e, w: w.destroy(), w)
        _, x, y = self.get_window().get_origin()
        if top:
            w.move(x, y)
        else:
            w.move(x, y+int(alloc.height*0.6))
        w.show_all()
        e.grab_focus()

    def number_changed_cb (self, num_selector):
        self.destroy_npicker()
        newval = num_selector.get_value()
        if newval:
            self.set_text_interactive(str(newval))
        else:
            self.set_text_interactive('')            

    def show_number_picker (self):
        w = Gtk.Window(type = Gtk.WindowType.POPUP)
        ns = NumberSelector(upper = self.upper, default = self.get_value())
        ns.connect('changed', self.number_changed_cb)
        w.grab_focus()
        w.add(ns)
        _, xorigin, yorigin = self.get_window().get_origin()
        x, y = (self.get_allocated_width(), self.get_allocated_height())
        popupx, popupy = w.get_size()
        overlapx = popupx-x
        overlapy = popupy-y
        w.move(xorigin - (overlapx/2), yorigin - (overlapy/2))
        w.show()
        self.npicker = w

    def set_text_interactive (self, text):
        self.emit('value-about-to-change')
        self.set_text(text)
        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)

    def show_note_text (self):
        '''Display the notes for the current view
        '''
        self.top_note_text = self.get_note_display(self.top_note_list)[1]
        self._top_note_layout.set_markup(self.get_note_display(self.top_note_list)[2], -1)
        self.bottom_note_text = self.get_note_display(self.bottom_note_list)[1]
        self._bottom_note_layout.set_markup(self.get_note_display(self.bottom_note_list)[2], -1)
        self.queue_draw()

    def set_note_text (self, top_text = None, bottom_text = None, for_hint = False):
        '''Change the notes
        '''
        if top_text is not None:
            self.update_notelist(self.top_note_list, top_text)
        if bottom_text is not None:
            self.update_notelist(self.bottom_note_list, bottom_text, for_hint)
        self.show_note_text()

    def set_note_text_interactive (self, *args, **kwargs):
        self.emit('notes-about-to-change')
        self.set_note_text(*args, **kwargs)
        self.emit('notes-changed')

    def set_notelist(self, top_notelist, bottom_notelist):
        '''Assign new note lists
        '''
        if top_notelist:
            self.top_note_list = top_notelist
        if bottom_notelist:
            self.bottom_note_list = bottom_notelist

    def get_note_display(self, notelist, tracker_id = None, include_untracked = True):
        '''Parse a notelist for display

        Parse a notelist for the display.
        notelist - This method works on one notelist at a time, so
            top_note_list or bottom_note_list must be passed in.
        tracker_id - can specify a particular tracker.  The default is to use
            tracker that is currently showing.
        include_untracked - When set to True(default), the untracked notes will
            be included in the output.  Set it to false to exclude untracked
            notes.

        The output is returned in 3 formats:
        display_list - is tuple list in the format (notelist_index, tid, note)
            notelist_index - the index within the notelist
            tid - tracker id
            note - value of the note
        display_text - vanilla string representing all the values
        markup_text - pango markup string that colors each note for its tracker
        '''
        display_list = []
        display_text = ''
        markup_text = ''
        if tracker_id == None:
            tracker_id = self.tinfo.showing_tracker
        if include_untracked:
            track_filter = [tracker_info.NO_TRACKER, tracker_id]
        else:
            track_filter = [tracker_id]
        last_tracker = tracker_info.NO_TRACKER
        for notelist_index, (tid, note) in enumerate(notelist[:]):
            if tid not in track_filter:
                continue
            display_list.append((notelist_index, tid, note))
            display_text += note
            if tid != last_tracker:
                if self.tinfo.get_color_markup(last_tracker):
                    markup_text += '</span>'
                if self.tinfo.get_color_markup(tid):
                    markup_text += '<span foreground="' + str(self.tinfo.get_color_markup(tid)) + '">'
                last_tracker = tid
            markup_text += note
        if self.tinfo.get_color_markup(last_tracker):
            markup_text += '</span>'
        return((display_list, display_text, markup_text))

    def update_notelist(self, notelist, new_notes, for_hint = False):
        '''Parse notes for a notelist

        A notelist stores individual notes in the format (tracker, note).  The
        sequence is also meaningful - it dictates the order in which the notes
        are displayed.  One notelist is maintained for the top
        notes(top_note_list), and one for the bottom(bottom_note_list).  This
        method is responsible for maintaining those lists.

        When updating for hints(for_hint == True), the old notes are replaced
        completely by the new notes and set with NO_TRACKER.
        '''
        # Remove any duplicates
        unique_notes = ""
        for note in new_notes:
            if note not in unique_notes:
                unique_notes += note
        # Create a list and text version of the notelist
        display_list = self.get_note_display(notelist)[0]
        display_text = self.get_note_display(notelist)[1]
        if display_text == unique_notes:
            return
        # Remove deleted values from the notelist
        del_offset = 0
        for display_index, (notelist_index, tid, old_note) in enumerate(display_list[:]):
            if old_note not in unique_notes or for_hint:
                del notelist[notelist_index + del_offset]
                del display_list[display_index + del_offset]
                del_offset -= 1
            else:
                # Adjust the display_list index
                display_list[display_index + del_offset] = (notelist_index + del_offset, tid, old_note)
        # Insert any new values into the notelist
        ins_offset = 0
        display_index = 0
        for new_index, new_note in enumerate(unique_notes):
            add_note = False
            # if the new notes are longer than the current ones - append
            if len(display_list) <= display_index:
                notelist_index = len(notelist)
                ins_offset = 0
                add_note = True
            # Otherwise - advance until we find the appropriate place to insert
            else:
                old_note = display_list[display_index][2]
                if new_note != old_note:
                    notelist_index = display_list[display_index][0]
                    add_note = True
                display_index += 1
            if add_note:
                if for_hint:
                    use_tracker = tracker_info.NO_TRACKER
                else:
                    use_tracker = self.tinfo.current_tracker
                notelist.insert(notelist_index + ins_offset, (use_tracker, new_note))
                display_list.insert(new_index, (notelist_index + ins_offset, self.tinfo.current_tracker, new_note))
                ins_offset = ins_offset + 1
        self.trim_untracked_notes(notelist)

    def trim_untracked_notes(self, notelist):
        untracked_text = self.get_note_display(notelist, tracker_info.NO_TRACKER)[1]
        for tid, note in notelist[:]:
            if note in untracked_text and tid != tracker_info.NO_TRACKER:
                notelist.remove((tid, note))

    def get_notes_for_undo(self):
        '''Return the top and bottom notelists
        '''
        return((self.top_note_list[:], self.bottom_note_list[:]))

    def set_notes_for_undo(self, notelists):
        '''Reset the top and bottom notelists from an undo
        '''
        self.top_note_list, self.bottom_note_list = notelists
        self.show_note_text()

    @simple_debug
    def do_draw(self, cr):

        w = self.get_allocated_width()
        h = self.get_allocated_height()
        style_ctx = self.get_style_context()

        self.draw_background_color(cr, style_ctx, w, h)
        if self.is_focus():
            self.draw_highlight_box(cr, style_ctx, w, h)
        if self.border_color is not None:
            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)
            cr.set_line_width(border_width)
            cr.stroke()

        if h < w:
            scale = h/float(BASE_SIZE)
        else:
            scale = w/float(BASE_SIZE)
        cr.scale(scale, scale)

        self.draw_text(cr, style_ctx)
        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(
            cr.set_source_rgb(1.0, 1.0, 1.0)
        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_background_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_border_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()
        # 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()

    def draw_text (self, cr, style_ctx):
        fontw, fonth = self._layout.get_pixel_size()
        # Draw a shadow for tracked conflicts.  This is done to
        # differentiate between tracked and untracked conflicts.
        if self.shadow_color:
            cr.set_source_rgb(*self.shadow_color)
            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)
        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),
                (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))

        # And draw any note text...
        if self._top_note_layout:
            fontw, fonth = self._top_note_layout.get_pixel_size()
            cr.move_to(
                NORMAL_LINE_WIDTH,
                0,
                )
            PangoCairo.update_layout(cr, self._top_note_layout)
            PangoCairo.show_layout(cr, self._top_note_layout)
        if self._bottom_note_layout:
            fontw, fonth = self._bottom_note_layout.get_pixel_size()
            cr.move_to(
                NORMAL_LINE_WIDTH,
                BASE_SIZE-fonth,
                )
            PangoCairo.update_layout(cr, self._bottom_note_layout)
            PangoCairo.show_layout(cr, self._bottom_note_layout)

    def set_text_color (self, color, shadow = None):
        self.shadow_color = shadow
        self.text_color = color
        self.queue_draw()

    def set_background_color (self, color):
        self.custom_background_color = color
        self.queue_draw()

    def set_border_color (self, color):
        self.border_color = color
        self.queue_draw()

    def hide_notes (self):
        pass

    def show_notes (self):
        pass

    def set_value (self, v):
        if 0 < v <= self.upper:
            self.set_text(str(v))
        else:
            self.set_text('')
        self.queue_draw()

    def get_value (self):
        try:
            return int(self.text)
        except:
            return None

    def get_text (self):
        return self.text

    def get_note_text (self):
        return self.top_note_text, self.bottom_note_text

class SudokuNumberBox (NumberBox):

    normal_color = None
    tracker_id = None
    error_color = (1.0, 0, 0)
    highlight_color = ERROR_HIGHLIGHT_COLOR

    def set_value(self, val, tracker_id = None):
        if tracker_id == None:
            self.tracker_id = self.tinfo.current_tracker
        else:
            self.tracker_id = tracker_id
        self.normal_color = self.tinfo.get_color(self.tracker_id)
        self.set_text_color(self.normal_color)
        super(SudokuNumberBox, self).set_value(val)

    def get_value_for_undo(self):
        return(self.tracker_id, self.get_value(), self.tinfo.get_trackers_for_cell(self.x, self.y))

    def set_value_for_undo (self, undo_val):
        tracker_id, value, all_traces = undo_val
        # When undo sets a value, switch to that tracker
        if value:
            self.tinfo.ui.select_tracker(tracker_id)
        self.set_value(value, tracker_id)
        self.tinfo.reset_trackers_for_cell(self.x, self.y, all_traces)
        self.emit('undo_change')

    def recolor(self, tracker_id):
        self.normal_color = self.tinfo.get_color(tracker_id)
        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)
            else:
                self.set_text_color(self.error_color)
        else:
            self.set_text_color(self.normal_color)

    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)
        else:
            self.set_font(self.normal_font)
        self.queue_draw()

    def set_impossible (self, val):
        if val:
            if not self.get_text():
                self.set_text('X')
                self.set_text_color(self.error_color)
        elif self.get_text() == 'X':
            self.set_text('')
            self.set_text_color(self.normal_color)
        self.queue_draw()


GObject.type_register(NumberBox)

if __name__ == '__main__':
    window = Gtk.Window()
    window.connect('delete-event', Gtk.main_quit)

    def test_number_selector ():
        nselector = NumberSelector(default = 3)
        def tell_me (b):
            print 'value->', b.get_value()
        nselector.connect('changed', tell_me)
        window.add(nselector)

    def test_number_box ():
        window.set_size_request(100, 100)
        nbox = NumberBox()
        window.add(nbox)

#    test_number_selector()
    test_number_box()
    window.show_all()
    Gtk.main()