All pastes #393256 Raw Edit

Mine

public text v1 · immutable
·published 2007-03-13 12:12 UTC
rendered paste body
 require 'gtk2'
require 'xmlreader'

=begin
    Kilka luźnych uwag:
    trzeba przerobić nieco kaczuszkę, tzn, musi być image a w nim pixmap (chyba). 
    Nie wiem w czym musi siedzieć kaczuszka, ale to umożliwi rysowanie po kaczuszce grafów.
    Acha, jutro zamieszczę nową wersję kaczuszki - watermarked, więc spoko wodza, będize dobrze :D
=end



class Interface
  
    
  def initialize()
    @window = Gtk::Window.new(Gtk::Window::TOPLEVEL)  
    @window.set_title("Rubber Duckies")
    @window.set_size_request( 600, 650 )
    @window.set_resizable(true)
    @window.signal_connect( "delete_event" ) { delete_event() }
    
    #resizing the drawing area
    @window.signal_connect( "size_allocate" ) {
      tempAlloc = @window.allocation
      @duckie.set_size_request(tempAlloc.width-100, tempAlloc.height-130)
    } 
    @clear = true
    #@filename=""
    create_layout()
    
    #making all components visible, instead of writing widget.show for every one
    @window.show_all    
  end
  
  def clear_all()
      @regExEntry.set_text("")     
    
    # tried to clear the drawing area, but didn't get through choice of colours.
    # Fede know how to, so I'll try to reach him
    @clear = false
    alloc = @duckie.allocation
    gc = @duckie.style.fg_gc(@duckie.state)


#      @clear = false
#      alloc = @area.allocation
#      gc = @area.style.fg_gc(@area.state)
#      gc.set_foreground(@white)
#      @area.window.draw_rectangle(gc, true, 0, 0, alloc.width, alloc.height)
#      gc.set_foreground(@black)
 
  end
  
  def draw_lines(alloc, gc)
     #a couple of lines..
      @duckie.window.draw_line(gc2, alloc.x + 50, alloc.y - 30, alloc.x + 100, alloc.y + 10)
      @duckie.window.draw_line(gc2, alloc.x + 50, alloc.y - 30, alloc.x, alloc.y + 10)
      @duckie.window.draw_line(gc2, alloc.x + 100, alloc.y + 10, alloc.x + 120, alloc.y + 50)
      @duckie.window.draw_line(gc2, alloc.x + 100, alloc.y + 10, alloc.x + 80, alloc.y + 50)
      @duckie.window.draw_line(gc2, alloc.x, alloc.y + 10, alloc.x + 20, alloc.y + 50)
      @duckie.window.draw_line(gc2, alloc.x, alloc.y + 10, alloc.x - 20, alloc.y + 50)
      #area.window.draw_arc(area.style.fg_gc(area.state), false, 
      #                     0, 0, alloc.width, alloc.height, 0, 64 * 360) 
  end
  
  def create_layout()
    # a vertical box, that the menu and the rest is contained in
    vbox = Gtk::VBox.new(false, 0)
    @window.add(vbox)
    #adding menu to vbox with padding = 0
    vbox.pack_start( create_menu(), false, false, 0) 
    # a horizontal box, that contains the left and right panels
    hbox = Gtk::HBox.new(false, 0)
    #adding of the left part
    hbox.pack_start( create_left(), false, false, 0)
    #separator between panels, padding = 2
    hbox.pack_start(Gtk::VSeparator.new(), false, false, 2)
    #adding of the right part
    hbox.pack_start(create_right(), false, false, 0)
    #addition of the hbox to vbox ;)
    vbox.pack_start(hbox, false, false, 0)
  end
  
  #The menu
  def create_menu ()    
    @xmlfilename
    menubar = Gtk::MenuBar.new
    top_menu = Gtk::MenuItem.new( "File" ) 
    menubar.append( top_menu )
    menuFile = Gtk::Menu.new
    
    itemNew = Gtk::MenuItem.new( "New" )
    itemOpen = Gtk::MenuItem.new( "Open" )
    itemSave = Gtk::MenuItem.new( "Save" )
    itemSaveAs = Gtk::MenuItem.new( "Save As..." )
    itemSep = Gtk::SeparatorMenuItem.new()
    itemExit = Gtk::MenuItem.new( "EXIT" )
    
    # I guess all that is obvious :)
    
    itemNew.signal_connect("activate") {
      clear_all() #this one should clear the text box and the drawing area
    }
    itemOpen.signal_connect("activate"){
      #dialog for choosing a file, however no opening is implemented
      openFileDialog = Gtk::FileChooserDialog.new("Open File",
                                     nil,
                                     Gtk::FileChooser::ACTION_OPEN,
                                     nil,
                                     [Gtk::Stock::CANCEL, Gtk::FileChooserDialog::RESPONSE_CANCEL],
                                     [Gtk::Stock::OPEN, Gtk::FileChooserDialog::RESPONSE_ACCEPT])
                                     
    if openFileDialog.run == Gtk::FileChooserDialog::RESPONSE_ACCEPT
      #puts "filename = #{openFileDialog.filename}"
      @xmlfilename=openFileDialog.filename;
      puts @xmlfilename
    end
      openFileDialog.destroy
      
      xr = XmlReader.new
      xr.go(@xmlfilename)
      xr.showAutomatons
    }
    itemSave.signal_connect("activate"){
      #check if there is some input file and save onto it.
    }
    itemSaveAs.signal_connect("activate"){
      #the same as with opening - only a dialog, without real saving (though you can create a new folder ;)
      saveAsFileDialog = Gtk::FileChooserDialog.new()
      saveAsFileDialog.set_action(1)
      saveAsFileDialog.show
    }
    itemExit.signal_connect("activate" ) { delete_event() }
    
    
    #additon of items to menu    
    
    menuFile.append( itemNew )
    menuFile.append( itemOpen )
    menuFile.append( itemSave )
    menuFile.append( itemSaveAs )
    menuFile.append(itemSep)
    menuFile.append( itemExit )
    
    
    top_menu.set_submenu( menuFile)   
    
    return menubar    
  end
  
  def create_left()
    #nothing to comment on here really
    vbox = Gtk::VBox.new(false, 0)
    sep = Gtk::HSeparator.new()
    vbox.pack_start(create_reButtons() , false, false, 5)
    vbox.pack_start( sep , false, false, 5)
    vbox.pack_start( create_graphButtons() , false, false, 5)
  end
  
  
  def create_right()
    vbox = Gtk::VBox.new()
    #textbox for regular expressions
    @regExEntry = Gtk::Entry.new()
    vbox.pack_start(@regExEntry, false, false, 2)
    #button :D
    button_toDFA = Gtk::Button.new("To DFA")  
    button_toDFA.signal_connect("clicked"){ }
    vbox.pack_start(button_toDFA, false, false, 2)
    vbox.pack_start(Gtk::HSeparator.new(), false, false, 2)
    #creating the drawing area
    @area = Gtk::DrawingArea.new()    
    @area.set_size_request(400, 300)
    @duckie = Gtk::Image.new("duckie.jpg")
    @duckie.set_size_request(500,500)
    
    
    @area.signal_connect("expose_event") do
      alloc = @area.allocation 
      #@area.style.fg_gc(@area.state)
      gc = Gdk::GC.new(@area.window)
      gc2 = @area.style.fg_gc(@area.state)
       
     
      colormap = Gdk::Colormap.system
      @white = Gdk::Color.new(65535, 65535, 65535)
      colormap.alloc_color(@white, false, true)
      @black = Gdk::Color.new(0, 0, 0)
      colormap.alloc_color(@black, false, true)
      gc2.set_foreground(@black)  
      gc.set_foreground(@white)      
      @area.window.draw_rectangle(gc, true, 0, 0, alloc.width, alloc.height)
      
      #@area.window.draw_image(gc2, duck2, 0, 0, 0, 0, duck.allocation.width, duck.allocation.height)
      
          
      
      if (@clear):
        draw_lines(alloc, gc2)
      end
      
      
    end
    
    vbox.pack_start(@duckie, true, true, 2)
#    vbox.pack_start(@area, true, true, 2)
    button_toRegEx = Gtk::Button.new("To RegEx")
    vbox.pack_start(button_toRegEx, false, true, 0)

    
  end
  
  def create_reButtons()
    # a guess no need to comment on this
    table = Gtk::Table.new(2, 2, false)
    button_plus = Gtk::Button.new("+")
    button_times = Gtk::Button.new("*")
    button_LBracket = Gtk::Button.new("(")
    button_RBracket = Gtk::Button.new(")")
    button_empty = Gtk::Button.new("e")
    button_plus.signal_connect("clicked"){ 
      temp = @regExEntry.text()+"+"
      @regExEntry.set_text(temp)
    }
    button_times.signal_connect("clicked"){ 
      temp = @regExEntry.text()+"*"
      @regExEntry.set_text(temp)
    }
    button_LBracket.signal_connect("clicked"){ 
      temp = @regExEntry.text()+"("
      @regExEntry.set_text(temp)
    }
    button_RBracket.signal_connect("clicked"){ 
      temp = @regExEntry.text()+")"
      @regExEntry.set_text(temp)
    }
    button_empty.signal_connect("clicked"){ 
      temp = @regExEntry.text()+"E"
      @regExEntry.set_text(temp)
    }
    hbox = Gtk::HBox.new(true, 0)
    hbox.pack_start(button_LBracket, false, false, 0)
    hbox.pack_start(button_RBracket, false, false, 0)
    table.attach(button_plus, 0, 1, 0, 1)
    table.attach(button_times, 1, 2, 0, 1)
    table.attach(hbox, 0, 1, 1, 2)
    table.attach(button_empty, 1, 2, 1, 2)
  end
  
  def create_graphButtons()
    vbox = Gtk::VBox.new(false, 0)
    
    button_vertex = Gtk::Button.new("vertex")
    button_edge = Gtk::Button.new("edge")
    button_move = Gtk::Button.new("MOVE")
    button_delete = Gtk::Button.new("DELETE")
    #button_toRegEx = Gtk::Button.new("To RegEx")
    sep = Gtk::HSeparator.new()
    sep2 = Gtk::HSeparator.new()
  #  duck = Gtk::Image.new("duckie.jpg")
    
    vbox.pack_start(button_vertex, false, false, 0)
    vbox.pack_start(button_edge, false, false, 0)
    vbox.pack_start(sep, false, false, 10)
    vbox.pack_start(button_move, false, false, 0)
    vbox.pack_start(button_delete, false, false, 0)
    vbox.pack_start(sep2, false, false, 10)
   # vbox.pack_start(duck, false, false, 20)
    #vbox.pack_start(button_toRegEx, false, false, 0)
    
    
  end
  
  
  
  
end

def delete_event()
    #closing of the window..
    Gtk.main_quit
    return false
  end

Gtk.init

Interface.new()

Gtk.main