indexing
	description: "Input dialog box with modal capabilities and animated icon."

class FINALIZE_DIALOG_BOX inherit

  PUZZLE_COMPONENT
  WEL_FRAME_WINDOW
  redefine 
    on_control_command, parent, on_get_min_max_info, closeable, on_paint, background_brush
  end 

creation  
	make

feature{NONE} -- private

  make( a_parent: like parent; title, msg : STRING; time : STRING; m_on : BOOLEAN ) is 
  require
    proper_parent: a_parent /= Void
    proper_string_input: title /= Void and msg /= Void
  do

    make_child( a_parent, title )
    set_gaps
    create_icon
    set_width_height
    set_positions

    create wp.make( box_width, box_height )

    move_and_resize( box_x_pos, box_y_pos, box_width, box_height, false )

    create message.make( Current, msg, message_x_pos, message_y_pos, message_width, message_height, - 1 )
    create name_label.make( Current, "Your Name:", name_label_x_pos, name_label_y_pos, name_label_width, name_label_height, - 1 )
    create title_label.make( Current, "Puzzle Title:", title_label_x_pos, title_label_y_pos, title_label_width, title_label_height, - 1 )

    create my_log_font.make( 16, "Arial" )
    create my_font.make_indirect( my_log_font )
    message.set_font( my_font )
    name_label.set_font( my_font )
    title_label.set_font( my_font )

    create name_text_field.make( Current, "", name_text_field_x_pos, name_text_field_y_pos, name_text_field_width, name_text_field_height, - 1 )

    create title_text_field.make( Current, "", title_text_field_x_pos, title_text_field_y_pos, title_text_field_width, title_text_field_height, - 1 )

    create ok_button.make( Current, "OK", ok_button_x_pos, ok_button_y_pos, ok_button_width, ok_button_height, - 1 )
    ok_button.set_font( my_font )
    create cancel_button.make( Current, "Cancel", cancel_button_x_pos, cancel_button_y_pos, cancel_button_width, cancel_button_height, - 1 )
    cancel_button.set_font( my_font )
    
    final_time := clone( time )
    modal_on := m_on
    check_modal

  end

  my_log_font : WEL_LOG_FONT 
  my_font : WEL_FONT

  dialog_image : WEL_BITMAP
  -- Image reference in the dialog box

  parent : V_DESIGN_SHELL

  name_text_field, title_text_field : WEL_SINGLE_LINE_EDIT
  -- text field which user enters in data
			
  message, name_label, title_label : WEL_STATIC
  -- Text displayed in the window

  ok_button, cancel_button : WEL_PUSH_BUTTON
  -- Buttons to accept the user input or annul operation

  modal_on : BOOLEAN
  -- Is the dialog box modal?

feature -- Access
			
  name_text_input, title_text_input, final_time : STRING
  -- Text the user entered and their score

feature -- Visibility

  show_finalized_dialog_box is
  -- Display the dialog box
  do
    set_initial_text_fields
    show
  end

feature -- Status

  response_available : BOOLEAN is
  -- Has the user entered some data in the text field?
  do
    Result := name_text_input /= Void and title_text_input /= Void
  end

  closeable : BOOLEAN is false
  -- Prevent the user from closing the window via the title bar close button

feature -- Background color

  background_brush : WEL_BRUSH is
  -- Current window background color used to refresh the window when
  -- requested by the WM_ERASEBKGND windows message.
  do
    create Result.make_by_sys_color( Color_background )
  end

feature -- Messages

  on_get_min_max_info( min_max_info: WEL_MIN_MAX_INFO ) is
  -- Prevent resizing by the user.
  do
     min_max_info.set_max_track_size( wp )
     min_max_info.set_min_track_size( wp )
    min_max_info.set_max_size( wp )
  end

  on_paint (paint_dc: WEL_PAINT_DC; invalid_rect: WEL_RECT) is
  -- Draw the icon bitmap
  do
    if dialog_image /= Void then
      paint_dc.draw_bitmap ( dialog_image, icon_x_pos, icon_y_pos, dialog_icon_width, dialog_icon_height )
    end
  end

  on_control_command (control: WEL_CONTROL) is 
  -- Process ok_button and cancel_button selection.
  do
    if control = ok_button then
      process_ok_button_press
    elseif control = cancel_button then
      name_text_input := Void
      title_text_input := Void
      reset_on_exit
    end
  end

feature{NONE} -- Implementation

  process_ok_button_press is
  local
    name_text_fix, title_text_fix : STRING
  do
    name_text_fix := clone( name_text_field.text )
    name_text_fix.left_adjust
    name_text_fix.right_adjust
    title_text_fix := clone( title_text_field.text )
    title_text_fix.left_adjust
    title_text_fix.right_adjust
    if title_text_fix.empty then
      title_text_field.clear
      title_text_field.set_focus
    end
    if name_text_fix.empty then
      name_text_field.clear
      name_text_field.set_focus
    end
    if not name_text_fix.empty and not title_text_fix.empty then
      name_text_input := name_text_fix
      title_text_input := title_text_fix
      reset_on_exit
    end
  end

  set_initial_text_fields is
  -- Set fields and focus
  do
    name_text_field.clear
    title_text_field.clear
    name_text_field.set_focus
  end

  check_modal is
  do
    if modal_on then
      if parent.enabled then
        parent.disable
      else
        parent.enable
      end
    end
  end

  reset_on_exit is
  do
    check_modal
    hide
    parent.finalize_box_notify
  end

  create_icon is
  do
    dialog_image := configuration.finalize_dialog_box_pixmap( Current )
  end

  wp : WEL_POINT
  -- Needed as the ordered pair (width, height) to prevent resizing.

  dialog_icon_width, dialog_icon_height, gap_north, gap_south, gap_east, gap_west,
  name_text_field_width, name_text_field_height, title_text_field_width, title_text_field_height,
  ok_button_width, ok_button_height, cancel_button_width, cancel_button_height,
  message_width, message_height : INTEGER

  icon_x_pos, icon_y_pos, name_text_field_x_pos, name_text_field_y_pos,
  title_text_field_x_pos, title_text_field_y_pos, ok_button_x_pos, ok_button_y_pos,
  cancel_button_x_pos, cancel_button_y_pos, message_x_pos, message_y_pos : INTEGER

  box_width, box_height, box_x_pos, box_y_pos : INTEGER

  name_label_x_pos, name_label_y_pos, name_label_width, name_label_height : INTEGER
  title_label_x_pos, title_label_y_pos, title_label_width, title_label_height : INTEGER

  set_gaps is
  do
    gap_north := 10
    gap_south := 25
    gap_east  := 10
    gap_west  := 10
  end

  set_width_height is
  -- Need a layout manager.
  do
    dialog_icon_width := dialog_image.width
    dialog_icon_height := dialog_image.height
    name_text_field_height := 23
    title_text_field_height := name_text_field_height
    ok_button_width := 80
    ok_button_height := 23
    cancel_button_width := ok_button_width
    cancel_button_height := ok_button_height

    message_width  := 200
    message_height := 50 -- The window should grow based to message height.

    box_width  := gap_west + dialog_icon_width + message_width + gap_east
    box_height := gap_north + 40 + name_text_field_height + title_text_field_height + ok_button_height + gap_south
    if message_height > dialog_icon_height then
      box_height := box_height + message_height
    else
      box_height := box_height + dialog_icon_height
    end

    name_text_field_width := box_width // 2
    title_text_field_width := name_text_field_width
    name_label_width := 75
    name_label_height := 23
    title_label_width := name_label_width
    title_label_height := name_label_height

  end

  set_positions is
  local
    y1, y2 : INTEGER
  do
    box_x_pos := 20
    box_y_pos := 20
    icon_x_pos := gap_west
    icon_y_pos := gap_north
    message_x_pos := gap_east * 2 + dialog_icon_width
    message_y_pos := gap_north  + 20
    name_text_field_x_pos := message_x_pos
    title_text_field_x_pos := name_text_field_x_pos

    y1 := message_y_pos + message_height + 5
    y2 := icon_y_pos + dialog_icon_height + 5
    if y1 > y2 then
      name_text_field_y_pos := y1
    else
      name_text_field_y_pos := y2
    end

    title_text_field_y_pos := name_text_field_y_pos + name_text_field_height

    ok_button_x_pos := box_width // 3 - ok_button_width // 2
    ok_button_y_pos := title_text_field_y_pos + title_text_field_height + ok_button_height 

    cancel_button_x_pos := box_width * 2 // 3 - cancel_button_width // 2
    cancel_button_y_pos := ok_button_y_pos

    name_label_x_pos := name_text_field_x_pos - 75
    name_label_y_pos := name_text_field_y_pos + 5
    title_label_x_pos := name_label_x_pos
    title_label_y_pos := title_text_field_y_pos + 5

  end
	
end  -- class FINALIZE_DIALOG_BOX



