indexing description: "[ Message dialog to display a string message and selection buttons. ]" date: "$Date$" revision: "$Revision$" class EM_MESSAGE_DIALOG inherit EM_DIALOG redefine show end EM_SHARED_STANDARD_FONTS export {NONE} all end create make_from_text_image, make_from_message, make_from_question, make_from_warning, make_from_error feature {NONE} -- Initialisation make_from_text_image (a_text: like text; an_image: like image) is -- Initialise dialgo with `a_text' and `an_image'. require a_text_not_void: a_text /= Void an_image_not_void: an_image /= Void do make_empty set_keyboard_sensitive (True) hide_close_button key_down_event.subscribe (agent handle_key_down_event) create button_clicked_event text := a_text image := an_image create text_label.make_from_text (a_text) text_label.set_position (10, 30) add_widget (text_label) create ok_button.make_from_text ("Ok") ok_button.set_dimension (80, 20) ok_button.clicked_event.subscribe (agent handle_ok_button_clicked) add_widget (ok_button) create cancel_button.make_from_text ("Cancel") cancel_button.set_dimension (80, 20) cancel_button.clicked_event.subscribe (agent handle_cancel_button_clicked) add_widget (cancel_button) create yes_button.make_from_text ("Yes") yes_button.set_dimension (80, 20) yes_button.clicked_event.subscribe (agent handle_yes_button_clicked) add_widget (yes_button) create no_button.make_from_text ("No") no_button.set_dimension (80, 20) no_button.clicked_event.subscribe (agent handle_no_button_clicked) add_widget (no_button) create image_panel.make_from_bitmap (an_image) add_widget (image_panel) do_layout center_on_screen ensure text_set: text = a_text image_set: image = an_image end make_from_message (a_message: like text) is -- Initialise dialog with `a_message' and a message image. require a_message_not_void: a_message /= Void do make_from_text_image (a_message, message_image) set_title ("Message") display_ok center_on_screen ensure text_set: text = a_message end make_from_question (a_question: like text) is -- Initialise dialog with `a_question' and a question image. require a_question_not_void: a_question /= Void do make_from_text_image (a_question, question_image) set_title ("Question") display_yes_no center_on_screen ensure text_set: text = a_question end make_from_warning (a_warning: like text) is -- Initialise dialog with `a_warning' and a warning image. require a_warning_not_void: a_warning /= Void do make_from_text_image (a_warning, warning_image) set_title ("Warning") display_ok center_on_screen ensure text_set: text = a_warning end make_from_error (an_error: like text) is -- Initialise dialog with `a_text' and an error image. require an_error_not_void: an_error /= Void do make_from_text_image (an_error, error_image) set_title ("Error") display_ok center_on_screen ensure text_set: text = an_error end feature -- Access text: STRING -- Text to display image: EM_BITMAP -- Image to display ok_button: EM_BUTTON -- OK button cancel_button: EM_BUTTON -- Cancel button yes_button: EM_BUTTON -- Yes button no_button: EM_BUTTON -- No button text_label: EM_LABEL -- Label to display text feature -- Status report was_ok_pressed: BOOLEAN -- Was ok button pressed? was_cancel_pressed: BOOLEAN -- Was cancel pressed? was_no_pressed: BOOLEAN -- Was no pressed? was_yes_pressed: BOOLEAN -- Was yes pressed? feature -- Status setting show is -- Show dialog. do was_ok_pressed := False was_cancel_pressed := False was_yes_pressed := False was_no_pressed := False Precursor {EM_DIALOG} end display_ok is -- Display ok button. do ok_button.show cancel_button.hide yes_button.hide no_button.hide do_layout end display_ok_cancel is -- Display ok and cancel buttons. do ok_button.show cancel_button.show yes_button.hide no_button.hide do_layout end display_yes_no is -- Display yes and no buttons. do ok_button.hide cancel_button.hide yes_button.show no_button.show do_layout end display_yes_no_cancel is -- Display yes, no and cancel buttons. do ok_button.hide cancel_button.show yes_button.show no_button.show do_layout end feature -- Element change set_text (a_text: like text) is -- Set `text' to `a_text'. require a_text_not_void: a_text /= Void do text := a_text do_layout ensure text_set: text = a_text end feature -- Events button_clicked_event: EM_EVENT_CHANNEL [TUPLE []] -- Any button clicked event. feature {NONE} -- Implementation image_panel: EM_IMAGEPANEL -- Panel to display image message_image: EM_BITMAP is -- Message image local a_font: EM_TTF_FONT do Bitmap_factory.create_empty_bitmap (40, 40) Result := Bitmap_factory.last_bitmap Result.set_drawing_color (create {EM_COLOR}.make_with_rgb (0, 0, 255)) Result.fill (Theme_colors.window_background) a_font := Standard_ttf_fonts.bitstream_vera_sans_bold (35) a_font.draw ('#', Result, 10, 5) end question_image: EM_BITMAP is -- Question image local a_font: EM_TTF_FONT do Bitmap_factory.create_empty_bitmap (40, 40) Result := Bitmap_factory.last_bitmap Result.set_drawing_color (create {EM_COLOR}.make_with_rgb (0, 255, 0)) Result.fill (Theme_colors.window_background) a_font := Standard_ttf_fonts.bitstream_vera_sans_bold (35) a_font.draw ('?', Result, 10, 5) end warning_image: EM_BITMAP is -- Warning image local a_font: EM_TTF_FONT do Bitmap_factory.create_empty_bitmap (40, 40) Result := Bitmap_factory.last_bitmap Result.set_drawing_color (create {EM_COLOR}.make_with_rgb (255, 128, 0)) Result.fill (Theme_colors.window_background) a_font := Standard_ttf_fonts.bitstream_vera_sans_bold (35) a_font.draw ('*', Result, 10, 15) end error_image: EM_BITMAP is -- Error image local a_font: EM_TTF_FONT do Bitmap_factory.create_empty_bitmap (40, 40) Result := Bitmap_factory.last_bitmap Result.set_drawing_color (create {EM_COLOR}.make_with_rgb (255, 0, 0)) Result.fill (Theme_colors.window_background) a_font := Standard_ttf_fonts.bitstream_vera_sans_bold (35) a_font.draw ('!', Result, 15, 5) end handle_ok_button_clicked is -- Handle ok button clicked. do was_ok_pressed := True hide button_clicked_event.publish ([]) end handle_cancel_button_clicked is -- Handle cancel button clicked. do was_cancel_pressed := True hide button_clicked_event.publish ([]) end handle_yes_button_clicked is -- Handle cancel button clicked. do was_yes_pressed := True hide button_clicked_event.publish ([]) end handle_no_button_clicked is -- Handle cancel button clicked. do was_no_pressed := True hide button_clicked_event.publish ([]) end handle_key_down_event (an_event: EM_KEYBOARD_EVENT) is -- Handle key down event. do if an_event.key = an_event.sdlk_escape then cancel_button.fire_clicked_event end end do_layout is -- Layout components. local dialog_width, dialog_height: INTEGER do text_label.enable_multilined text_label.resize_to_optimal_dimension image_panel.resize_to_optimal_dimension dialog_width := 15 if ok_button.is_visible then dialog_width := dialog_width + 80 + 5 end if cancel_button.is_visible then dialog_width := dialog_width + 80 + 5 end if yes_button.is_visible then dialog_width := dialog_width + 80 + 5 end if no_button.is_visible then dialog_width := dialog_width + 80 + 5 end dialog_width := dialog_width.max (10 + text_label.width + 10 + image_panel.width + 10) dialog_height := 30 + text_label.height.max (image_panel.height) + 40 set_dimension (dialog_width, dialog_height) text_label.set_position (10, 30+(dialog_height-30-40-text_label.height)//2) image_panel.set_position (dialog_width-image_panel.width-10, 30+(dialog_height-30-40-image_panel.height)//2) dialog_width := dialog_width - 10 if cancel_button.is_visible then cancel_button.set_position (dialog_width-80, dialog_height-25) dialog_width := dialog_width - 80 - 5 end if ok_button.is_visible then ok_button.set_position (dialog_width-80, dialog_height-25) dialog_width := dialog_width - 80 - 5 end if no_button.is_visible then no_button.set_position (dialog_width-80, dialog_height-25) dialog_width := dialog_width - 80 - 5 end if yes_button.is_visible then yes_button.set_position (dialog_width-80, dialog_height-25) dialog_width := dialog_width - 80 - 5 end end end