Title / Description
Code class GuiTest < Gtk::Window def initialize super set_title "Test" init_ui set_default_size 180, 100 set_border_width 5 set_resizable false stick set_window_position Gtk::Window::POS_CENTER signal_connect "destroy" do Gtk.main_quit end show_all end def init_ui @entry = Gtk::Entry.new @calendar = Gtk::Calendar.new @toggle_button = Gtk::ToggleButton.new("Show Calendar") @calendar_win = setup_calendar hbox = Gtk::HBox.new true, 10 add hbox hbox.pack_start(@entry, false, true, 4) hbox.pack_start(@toggle_button, false, true, 4) @toggle_button.signal_connect("toggled") do if @toggle_button.active? rect = @toggle_button.allocation main_window = @toggle_button.toplevel win_x, win_y = main_window.window.origin cal_x = win_x + rect.x cal_y = win_y + rect.y + rect.height x, y = self.apply_screen_coord_correction(cal_x, cal_y, @calendar_win, @toggle_button) @calendar_win.move(x, y) @calendar_win.show_all() @toggle_button.set_label("Hide Calendar") else @calendar_win.hide_all() @toggle_button.set_label("Show Calendar") end end @calendar.signal_connect("day-selected") do |widget,event| date_arr = @calendar.date year = date_arr[0] month = date_arr[1]# + 1 # gtk : months 0-indexed, Time.gm : 1-index day = date_arr[2] if @calendar time = Time.gm(year, month, day) @entry.text = format_date(time) end @calendar_win.hide_all @toggle_button.active = false @toggle_button.set_label("Show Calendar") end signal_connect("configure-event") do |widget,event| if @calendar_win.mapped? rect = @toggle_button.allocation cal_x = event.x + rect.x cal_y = event.y + rect.y + rect.height x, y = self.apply_screen_coord_correction(cal_x, cal_y, @calendar_win, @toggle_button) @calendar_win.move(x, y) end end end def setup_calendar cal_window = Gtk::Window.new(Gtk::Window::TOPLEVEL) cal_window.set_keep_above(true) cal_window.set_decorated(false) cal_window.set_resizable(false) cal_window.skip_taskbar_hint = true cal_window.skip_pager_hint = true cal_window.set_type_hint(Gdk::Window::TYPE_HINT_DOCK) cal_window.events = [Gdk::Event::FOCUS_CHANGE_MASK] cal_window.stick() cal_vbox = Gtk::VBox.new false, 10 cal_window.add(cal_vbox) cal_vbox.pack_start(@calendar, true, false, 0) return cal_window end def format_date(datetime) date_format = '%d/%m/%Y' datetime.strftime( date_format = '%d/%m/%Y') end def apply_screen_coord_correction(x, y, widget, relative_widget) corrected_y = y corrected_x = x rect = widget.allocation() screen_w = Gdk::screen_width screen_h = Gdk::screen_height() delta_x = screen_w - (x + rect.width) delta_y = screen_h - (y + rect.height) if delta_x < 0 corrected_x += delta_x end if corrected_x < 0 corrected_x = 0 end if delta_y < 0 corrected_y = y - rect.height - relative_widget.allocation().height end if corrected_y < 0 corrected_y = 0 else return corrected_x, corrected_y end end end #end=>GuiTest < Gtk::Window window = GuiTest.new Gtk.main
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code