Class: WxExtensions::MessageDialog

Inherits:
Wx::Dialog
  • Object
show all
Defined in:
lib/reactive-wx/wx_ext/message_dialog.rb

Overview

A class to allow modeless message dialogs The code is ugly as it is a quick translation of the generic MessageDialog C source code

Instance Method Summary collapse

Constructor Details

#initialize(parent, *args) ⇒ MessageDialog

Returns a new instance of MessageDialog.



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
# File 'lib/reactive-wx/wx_ext/message_dialog.rb', line 6

def initialize(parent, *args)
  message = args[0] if args[0]
  caption = args[1] ? args[1] : 'Message'
  style = args[2] ? args[2] : Wx::OK | Wx::CENTRE
  pos = args[3] ? args[3] : Wx::DEFAULT_POSITION
  if (options = args.last).is_a? Hash
    message = options[:message] if options[:message]
    caption = options[:caption] if options[:caption]
    style = options[:style] if options[:style]
    pos = options[:pos] if options[:pos]
  end

  dialog_style = (style & (Wx::OK|Wx::CANCEL|Wx::YES|Wx::NO|Wx::HELP|Wx::NO_DEFAULT)) == 0 ? Wx::CAPTION : Wx::DEFAULT_DIALOG_STYLE
  super(parent, Wx::ID_ANY, caption, pos, Wx::DEFAULT_SIZE, dialog_style)
  @parent = parent          # for SemiModalDialog
  
  top_sizer = Wx::BoxSizer.new(Wx::VERTICAL)
  icon_text = Wx::BoxSizer.new(Wx::HORIZONTAL)
  
  if (style & Wx::ICON_MASK) != 0
    @@icon_map ||= {Wx::ICON_ERROR => Wx::ART_ERROR, Wx::ICON_INFORMATION => Wx::ART_INFORMATION, Wx::ICON_WARNING => Wx::ART_WARNING, Wx::ICON_QUESTION => Wx::ART_QUESTION}
    @@icon_map.default ||= Wx::ART_ERROR
    icon_text.add(Wx::StaticBitmap.new(self, Wx::ID_ANY, Wx::ArtProvider.get_icon(@@icon_map[style & Wx::ICON_MASK], Wx::ART_MESSAGE_BOX)), 0, Wx::CENTER)
  end

  icon_text.add(Wx::StaticText.new(self, Wx::ID_ANY, message.gsub('&','&&')), 0, Wx::ALIGN_CENTER | Wx::LEFT, 10)
  top_sizer.add(icon_text, 1, Wx::CENTER | Wx::LEFT|Wx::RIGHT|Wx::TOP, 10)
  
  center_flag = Wx::EXPAND
  center_flag = Wx::ALIGN_CENTRE if (style & Wx::YES_NO) != 0
  center_flag = Wx::ALIGN_CENTRE if RUBY_PLATFORM =~ /win/
  sizer_btn = create_button_sizer(style & (Wx::OK|Wx::CANCEL|Wx::YES|Wx::NO|Wx::HELP|Wx::NO_DEFAULT))
  top_sizer.add(sizer_btn, 0, center_flag | Wx::ALL, 10)
  
  set_auto_layout(true)
  set_sizer(top_sizer)

  top_sizer.set_size_hints(self)
  top_sizer.fit(self)
  size = get_size
  if size.get_width < size.get_height*3/2
    size.set_width(size.get_height*3/2)
    set_size(size)
  end

  centre(Wx::BOTH | Wx::CENTER_FRAME)
  
  evt_button(Wx::ID_YES) { end_modal(Wx::ID_YES) }
  evt_button(Wx::ID_NO) { end_modal(Wx::ID_NO) }
  evt_button(Wx::ID_CANCEL) do
    # Allow cancellation via ESC/Close button except if only YES and NO are specified.
    end_modal(Wx::ID_CANCEL) if (style & Wx::YES_NO) != Wx::YES_NO || (style & Wx::CANCEL) != 0
  end
end