FXCalendar class

The following is some FXRuby stuff I’ve found useful… if the Ruby Cookbook
was still around, I’d put it on that, but here it is anyway.

<—CODE START—>
require ‘fox’

module Fox
class FXCalendar < FXDialogBox
attr_reader :selected

def initialize(owner, initial_date = Time.now)
  super(owner, "Calendar")
  top = FXVerticalFrame.new(self, LAYOUT_FILL_X|LAYOUT_FILL_Y)
  @headerBGColor = FXRGB(0,0,0)
  @matrixBGColor = FXRGB(100,100,220)
  @headerFGColor = FXRGB(255,255,255)
  
  @date_showing = initial_date
  
  #header row
  header = FXHorizontalFrame.new(top, LAYOUT_FILL_X)
  header.setBackColor(@headerBGColor)
  @backBtn = FXButton.new(header, " << ")
  @backBtn.connect(SEL_COMMAND) do |send, sel, ev|
    @date_showing = _last_month
    _build_date_matrix
    @current_month.text = _header_date
  end  
  @current_month = FXLabel.new(header, _header_date, nil,

LAYOUT_FILL_X|JUSTIFY_CENTER_X|LAYOUT_FILL_Y)
@current_month.setBackColor(@headerBGColor)
@current_month.setTextColor(@headerFGColor)
@foreBtn = FXButton.new(header, " >> ")
@foreBtn.connect(SEL_COMMAND) do |send, sel, ev|
@date_showing = _next_month
_build_date_matrix
@current_month.text = _header_date
end

  @matrix = FXMatrix.new(top, 7,

MATRIX_BY_COLUMNS|LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_RAISED, 0,0,0,0,0,0,0,0)
[‘SUN’, ‘MON’, ‘TUE’, ‘WED’, ‘THU’, ‘FRI’, ‘SAT’].each {|day|
_add_matrix_label(day)}
(7*6 - 1).times do
s = FXSwitcher.new(@matrix,LAYOUT_FILL_X|LAYOUT_FILL_Y,
0,0,0,0,0,0,0,0)
FXFrame.new(s, LAYOUT_FILL_X|LAYOUT_FILL_Y,0,0,0,0,0,0,0,0)
FXButton.new(s, ‘99’, nil, nil, 0,

LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_RAISED|FRAME_THICK,0,0,0,0,0,0,0,0).connec
t(SEL_COMMAND) do |send, sel, ev|
@selected = Time.local(@date_showing.year, @date_showing.month,
send.text.to_i)
self.handle(self, MKUINT(ID_ACCEPT, SEL_COMMAND), nil)
end
end
_build_date_matrix()
end

private   
def _header_date()
    @date_showing.strftime("%B, %Y")
end

def _first_day
    Time.local(@date_showing.year, @date_showing.month, 1)
end

def _last_day
  year = @date_showing.year
  month = @date_showing.month+1
  if month > 12
    year += 1
    month = 1
  end
  Time.local(year, month, 1) - (60*60*24)
end

def _last_month
  year = @date_showing.year
  month = @date_showing.month - 1
  if month < 1
    year -= 1
    month = 12
  end
  Time.local(year, month)
end

def _next_month
  year = @date_showing.year
  month = @date_showing.month + 1
  if month > 12
    year +=1
    month = 1
  end
  Time.local(year, month)
end

def _build_date_matrix()
  (0...6*7-1).each { |index| @matrix.childAtRowCol(index/7+1,

index.modulo(7)).setCurrent(0) }
(_first_day.wday… _last_day.day+_first_day.wday).each do |index|
day = index - _first_day.wday + 1
switcher = @matrix.childAtRowCol(index/7 + 1, index.modulo(7))
switcher.setCurrent(1)
switcher.childAtIndex(1).text = day.to_s
end
end

def _add_matrix_label(label)
  l = FXLabel.new(@matrix, label, nil,

LAYOUT_FILL_X|JUSTIFY_CENTER_X|FRAME_SUNKEN)
l.setBackColor(@headerBGColor)
l.setTextColor(@headerFGColor)
l.setFont(FXFont.new(getApp, “helvetica”, 7))
end
end
end

if FILE == $0
include Fox
class TestMW < FXMainWindow
def initialize(app, dialog_to_test, *args)
super(app, ‘test’, nil, nil, DECOR_ALL, 0, 0, 500, 500)
@d = dialog_to_test.new(self, *args)
end
def create
super
show(PLACEMENT_CURSOR)
if @d.execute(PLACEMENT_OWNER) != 0
p @d.selected.to_s
end
end
end

application = FXApp.new(‘eOne test’, ‘eONE’)
mw = TestMW.new(application, FXCalendar, Time.now)
application.init(ARGV)
application.create
application.run

end
<—CODE END—>
David

“David Naseby” david.naseby@eonesolutions.com.au wrote in message
news:C1B5ED61365AD511805500D0B7B697C613D3B1@sydeone.eonesolutions.com.au…

The following is some FXRuby stuff I’ve found useful… if the Ruby
Cookbook
was still around, I’d put it on that, but here it is anyway.

David,

Very, very nice. Would it be OK for me to include this in the FXRuby
“library” (with all appropriate credits to you)? I’ll probably change it a
little, to make the calendar a component that you can place anywhere a child
widget would go, and enable it for sending SEL_COMMAND messages to a target
and so forth. But otherwise it looks great.

Let me know what you think,

Lyle