test

Ruby code posted by michele
created at 26 Nov 14:09

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

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
3.68 KB in 5 ms with coderay