# Main window class for GetAClue player
+import cPickle
import pygtk
pygtk.require20 ()
import gtk
+import pango
+import crosswordpuzzle
class MainWindow:
def gtk_main_quit (self, *args):
gtk.main_quit ()
+ # callback for puzzle grid mouse button release event
+ def on_puzzlegrid_button_release_event (self, drawarea, event):
+ self.window.set_focus (drawarea)
+ return True
+
+ # moving the current selection in grid by one up or down
+ def move_selection_updown (self, step):
+ # increase or reduce the row by step until an occupied grid is found
+ # black block
+ last_occupied_row = self.selected_row
+ while True:
+ self.selected_row += step
+ if self.selected_row < 0 or self.selected_row >= self.puzzle.rows:
+ self.selected_row = last_occupied_row
+ break
+ if (self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True
+ or self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True):
+ break
+
+ # moving the current selection in grid by one across either way
+ def move_selection_across (self, step):
+ # increase or reduce the row by step until an occupied grid is found
+ # black block
+ last_occupied_col = self.selected_col
+ while True:
+ self.selected_col += step
+ if self.selected_col < 0 or self.selected_col >= self.puzzle.cols:
+ self.selected_col = last_occupied_col
+ break
+ if (self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True
+ or self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True):
+ break
+
+ # callback for puzzle grid key release event
+ def on_puzzlegrid_key_press_event (self, drawarea, event):
+ key = gtk.gdk.keyval_name (event.keyval)
+
+ if event.state == gtk.gdk.SHIFT_MASK and key == "Up":
+ # reduce the row by 1 until you find an occupied grid and not a
+ # black block
+ self.move_selection_updown (-1)
+ drawarea.queue_draw ()
+ elif event.state == gtk.gdk.SHIFT_MASK and key == "Down":
+ # increase the row by 1 until you find an occupied grid and not a
+ # black block
+ self.move_selection_updown (1)
+ drawarea.queue_draw ()
+ elif event.state == gtk.gdk.SHIFT_MASK and key == "Right":
+ # increase the column by 1 until you find an occupied grid and not
+ # a black block
+ self.move_selection_across (1)
+ drawarea.queue_draw ()
+ elif event.state == gtk.gdk.SHIFT_MASK and key == "Left":
+ # decrease the column by 1 until you find an occupied grid and not
+ # a black block
+ self.move_selection_across (-1)
+ drawarea.queue_draw ()
+ return False
+
+ # puzzle grid focus in event
+ def on_puzzlegrid_focus_out_event (self, drawarea, event):
+ col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("gray"))
+ drawarea.window.set_background (col)
+
+ return False
+
+ # puzzle grid focus out event
+ def on_puzzlegrid_focus_in_event (self, drawarea, event):
+ col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("white"))
+ drawarea.window.set_background (col)
+ return False
+
+ # callback for drawing the puzzle grid
+ def on_puzzlegrid_expose_event (self, drawarea, event):
+ # if puzzle is loaded
+ if self.puzzle:
+ # size the area
+ drawarea.set_size_request (self.puzzle.cols*30+2, self.puzzle.rows*30+2)
+
+ #numlayout = gtk.PrintContext().create_pango_layout ()
+ #numlayout.set_font_description (pango.FontDescription ("Sans 8"))
+ ctx = drawarea.window.cairo_create ()
+ ctx.set_line_width (1.5)
+
+ # run through the grid
+ for row in range (self.puzzle.rows):
+ for col in range (self.puzzle.cols):
+ # (re)set foreground color
+ ctx.set_source_rgb (0, 0, 0)
+ # if the area is not occupied
+ if (self.puzzle.data[row][col].occupied_across is False and
+ self.puzzle.data[row][col].occupied_down is False):
+ ctx.rectangle (col*30, row*30, 30, 30)
+ ctx.fill ()
+ else:
+ # if selected row/column
+ if row == self.selected_row and col == self.selected_col:
+ ctx.set_source_rgb (1, 1, 0.6)
+ ctx.rectangle (col*30,row*30, 30, 30)
+ ctx.fill ()
+ else:
+ ctx.set_source_rgb (1, 1, 1)
+ ctx.rectangle (col*30, row*30, 30, 30)
+ ctx.fill ()
+ ctx.set_source_rgb (0, 0, 0)
+ ctx.rectangle (col*30, row*30, 30, 30)
+ ctx.stroke ()
+
+ # if numbered
+ if self.puzzle.data[row][col].numbered <> 0:
+ ctx.set_source_rgb (0, 0, 0)
+ ctx.select_font_face ("Sans 7")
+ ctx.move_to (col*30+2, row*30+10)
+ ctx.show_text (str(self.puzzle.data[row][col].numbered))
+
+ return False
+
+ def load_clues (self):
+ # get the clues list store objects
+ across = self.ui.get_object ("clues_across")
+ down = self.ui.get_object ("clues_down")
+ across.clear ()
+ down.clear ()
+
+ # if puzzle is loaded
+ if self.puzzle:
+ clues_across = self.puzzle.get_clues_across ()
+ clues_down = self.puzzle.get_clues_down ()
+ # insert the numbers and the clues for across
+ for word, clue in clues_across:
+ across.append ([str(self.puzzle.data[word[1]][word[2]].numbered),
+ clue])
+ # insert the numbers and the clues for down
+ for word, clue in clues_down:
+ down.append ([ str(self.puzzle.data[word[1]][word[2]].numbered),
+ clue])
+
+ def open_file (self, file):
+ self.puzzle = cPickle.load (open (file, "rb"))
+ self.selected_row = 0
+ self.selected_col = 0
+ self.window.set_title ("GetAClue player - " + file)
+ self.load_clues ()
+
def __init__ (self, file_to_play = None):
# load the user interface
self.ui = gtk.Builder ()
# connect the signals
self.ui.connect_signals (self)
- # set the window title
+ # set the puzzle to None
+ self.puzzle = None
+
+ # open the file if it is set
if file_to_play:
- self.window.set_title ("GetAClue player - " + file_to_play)
+ self.open_file (file_to_play)
gtk.main ()