- 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 ()
+ if self.puzzle:
+ 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
+
+ # set the guessed character in the grid at selected location and move the
+ # selection across or down as the case may be
+ def set_guess (self, guess_char):
+ if self.puzzle:
+ # set a guess only if not revealed
+ if self.puzzle.data[self.selected_row][self.selected_col].revealed is False:
+ self.puzzle.data[self.selected_row][self.selected_col].guess = guess_char
+ # across mode typing
+ if self.typing_mode == self.ACROSS:
+ # move by one character across but only if there is no block
+ # in between
+ old_col = self.selected_col
+ self.move_selection_across (1)
+ if abs (self.selected_col - old_col) > 1:
+ self.selected_col = old_col
+ # down mode typing
+ else:
+ # move by one character down but only if there is no block
+ # in between
+ old_row = self.selected_row
+ self.move_selection_updown (1)
+ if abs (self.selected_row - old_row) > 1:
+ self.selected_row = old_row
+
+ # delete the guessed char in the previous row/col depending on the input mode
+ # If input mode is ACROSS then delete guessed char at previous column else
+ # at previous row
+ def delete_prev_guess (self):
+ if self.puzzle:
+ if self.typing_mode == self.ACROSS:
+ # prevent deleting characters when there is a gap
+ old_sel_col = self.selected_col
+ self.move_selection_across (-1)
+ # only if there is no block inbetween delete
+ if abs (self.selected_col - old_sel_col) <= 1:
+ self.puzzle.data[self.selected_row][self.selected_col].guess = None
+ # reset selection
+ else:
+ self.selected_col = old_sel_col
+ elif self.typing_mode == self.DOWN:
+ # prevent deleting characters when there is a gap
+ old_sel_row = self.selected_row
+ self.move_selection_updown (-1)
+ # only if there is no block inbetween delete
+ if abs (self.selected_row - old_sel_row) <= 1:
+ self.puzzle.data[self.selected_row][self.selected_col].guess = None
+ # reset selection
+ else:
+ self.selected_row = old_sel_row
+
+ # callback for puzzle grid mouse button release event
+ def on_puzzlegrid_button_press_event (self, drawarea, event):
+ # set the focus on the puzzle grid
+ if self.puzzle:
+ self.window.set_focus (drawarea)
+
+ col = int (event.x / 30)
+ row = int (event.y / 30)
+
+ if col < self.puzzle.cols and row < self.puzzle.rows:
+ if (self.puzzle.data[row][col].occupied_across is True or
+ self.puzzle.data[row][col].occupied_down is True):
+ self.selected_col = col
+ self.selected_row = row
+ drawarea.queue_draw ()
+
+ return False
+
+ # callback for main window key release event
+ def on_mainwindow_key_press_event (self, window, event):
+ if self.puzzle:
+ drawarea = self.ui.get_object ("puzzlegrid")
+ key = gtk.gdk.keyval_name (event.keyval).lower ()
+
+ 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)
+ self.typing_mode = self.DOWN
+ 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)
+ self.typing_mode = self.DOWN
+ 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)
+ self.typing_mode = self.ACROSS
+ 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)
+ self.typing_mode = self.ACROSS
+ drawarea.queue_draw ()
+ # if it is A-Z or a-z then
+ elif len (key) == 1 and key.isalpha ():
+ guess_char = key.upper ()
+ self.set_guess (guess_char)
+ drawarea.queue_draw ()
+ # if it is the delete key then delete character at selected row/col
+ elif key == "delete":
+ self.puzzle.data[self.selected_row][self.selected_col].guess = None
+ drawarea.queue_draw ()
+ # if the key is space key then delete character and move across or
+ # down one step depending on the mode
+ elif key == "space":
+ self.set_guess (None)
+ drawarea.queue_draw ()
+ # if it is backspace key then delete character at previous row/col
+ # depending on the input mode. If across editing mode, then delete
+ # at previous column else at previous row
+ elif key == "backspace":
+ self.delete_prev_guess ()
+ drawarea.queue_draw ()
+