# MovingBallMaemo.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-08-10 File created. # 2006-10-02 Last modification. # This program works on Maemo 2.0 (OS2006) and later versions. import gtk import hildon class MovingBallApplication( hildon.Program ) : def __init__( self ) : hildon.Program.__init__( self ) self.application_window = hildon.Window() self.application_window.set_title( "Move the Ball with Buttons" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 720, 420 ) self.drawing_area = gtk.DrawingArea() self.drawing_area.set_size_request( 720, 350 ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) left_button = gtk.Button( " < " ) up_button = gtk.Button( " Up " ) down_button = gtk.Button( " Down " ) right_button = gtk.Button( " > " ) left_button.connect( "clicked", self.left_button_clicked ) up_button.connect( "clicked", self.up_button_clicked ) down_button.connect( "clicked", self.down_button_clicked ) right_button.connect( "clicked", self.right_button_clicked ) operations_panel = gtk.HBox( False, 0 ) operations_panel.pack_start( left_button ) operations_panel.pack_start( up_button ) operations_panel.pack_start( down_button ) operations_panel.pack_start( right_button ) color_selection_menu = gtk.combo_box_entry_new_text() color_selection_menu.append_text( "red" ) color_selection_menu.append_text( "orange" ) color_selection_menu.append_text( "yellow" ) color_selection_menu.append_text( "green" ) color_selection_menu.append_text( "blue" ) color_selection_menu.append_text( "magenta" ) color_selection_menu.append_text( "cyan" ) color_selection_menu.append_text( "pink" ) color_selection_menu.append_text( "lightGray" ) operations_panel.pack_start( color_selection_menu ) color_selection_menu.set_active( 0 ) color_selection_menu.child.connect( "changed", self.color_selection_menu_used ) main_vertical_box = gtk.VBox( False, 0 ) main_vertical_box.pack_start( self.drawing_area, False, False, 3 ) main_vertical_box.pack_start( operations_panel, False, False, 3 ) self.application_window.add( main_vertical_box ) self.current_ball_color = "red" window_width, window_height = self.application_window.get_size() self.ball_position_x = window_width / 2 - 40 ; self.ball_position_y = window_height / 2 - 40 ; self.drawing_area.show() operations_panel.show() left_button.show() up_button.show() down_button.show() right_button.show() color_selection_menu.show() main_vertical_box.show() self.application_window.show() def left_button_clicked( self, widget, data=None ) : self.ball_position_x = self.ball_position_x - 3 self.drawing_area.queue_draw() def up_button_clicked( self, widget, data=None ) : self.ball_position_y = self.ball_position_y - 3 self.drawing_area.queue_draw() def down_button_clicked( self, widget, data=None ) : self.ball_position_y = self.ball_position_y + 3 self.drawing_area.queue_draw() def right_button_clicked( self, widget, data=None ) : self.ball_position_x = self.ball_position_x + 3 self.drawing_area.queue_draw() def color_selection_menu_used( self, menu_selection ) : self.current_ball_color = menu_selection.get_text() self.drawing_area.queue_draw() def drawing_area_exposed( self, area, event ) : graphics_context = self.drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = self.drawing_area.window graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( self.current_ball_color ) # Note that angles are expressed in 1/64ths of a degree. # 360 degrees is thus represented by value 360*64 this_drawable.draw_arc( graphics_context, True, self.ball_position_x, self.ball_position_y, 80, 80, 0, 360*64 ) graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "black" ) this_drawable.draw_arc( graphics_context, False, self.ball_position_x, self.ball_position_y, 80, 80, 0, 360*64 ) text_to_show = self.drawing_area.create_pango_layout( "" ) text_to_show.set_text( "(%d, %d)" % \ ( self.ball_position_x, self.ball_position_y ) ) this_drawable.draw_layout( graphics_context, 20, 20, text_to_show ) return True def run( self ) : gtk.main() if __name__ == "__main__": this_application = MovingBallApplication() this_application.run()