#######################################################################
#
# $Id: lsBaseDialog1.py,v 1.2 2008-02-07 05:45:25 lorenz Exp $
# 
# Copyright (C) 2004-2008 Petr Lorenz
# All rights reserved.
# 
# ##### BEGIN LICENSE BLOCK #####
#
# Version: MPL 1.1 / GPL 2.0 / LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is Petr Lorenz emGine Graphical User Interface (GUI).
# 
# The Initial Developer of the Original Code is Petr Lorenz.
# Portions created by the Initial Developer are Copyright (C) 2004-2008.
# All Rights Reserved.
# 
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the  "GPL"), or the
# GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
# case the provisions of the GPL or the LGPL are applicable instead of those
# above.  If you wish to allow use of your version of this file only under the
# terms of either the GPL or the LGPL and not to allow others to use your
# version of this file under the MPL, indicate your decision by deleting the
# provisions above and replace them with the notice and other provisions
# required by the GPL or LGPL.  If you do not delete the provisions above, a
# recipient may use your version of this file under the terms of any one of the
# MPL License, the GPL or the LGPL.
#
# ##### END LICENSE BLOCK #####
#
#######################################################################
#
# $Log: lsBaseDialog1.py,v $
# Revision 1.2  2008-02-07 05:45:25  lorenz
# * Fixed 'Simulation Boundary View' check problem
#   when closing a project.
#
# * Fixed Bounding Box problems when moving/editing a geometry.
#
# Revision 1.1.1.1  2008-01-28 06:15:35  lorenz
# Import emGine GUI v. 0.7.1
#
#######################################################################

# July 2007 - January 2008

# abstract class of base dialog 1

import wx

from emGineValidators import UniqueNameValidator

class lsBaseDialog1(wx.Dialog):
  def __init__(self, title):
    wx.Dialog.__init__(self, None, -1, title) #, pos = (0,0))
    self.sizer = None
    self.box = None
    self.sb_sizer = None	# sizer for static box

    # initialize list of entries containing user data objects
    self.text_entries = None
    self.item_entries = list()
    self.items_lists = list()
    self.materials_choice = None

    # Show choice of materials
    self.show_materials = False
    self.materials = None
    self.mat_list = None
    self.mat_choices_data_index = None
    self.sel_material = None		# material selected per default

    # Store name entry widget
    self.name_entry = None

  # overridden method
  def ShowModal(self):
    sizer = self.sizer

    if self.show_materials:
      mat_choices = None
      if self.mat_list:
	mat_choices_list = self.mat_list
      else:
	# build the list of material names
	self.mat_list = mat_choices_list = self.materials.get_list_of_names()

      materials_text = wx.StaticText(self, -1, "Material:")
      self.materials_choice = materials_choice = wx.Choice(self, -1, (150, -1), choices = mat_choices_list)
      choice = 0
      if self.sel_material:
	choice = self.materials.get_index_by_name(self.sel_material.name)
	if choice == None:
	  choice = 0
      materials_choice.SetSelection(choice)

      #box_mat = wx.StaticBox(self, -1, "Material choice")
      #sb_sizer_mat = wx.StaticBoxSizer(box_mat, wx.VERTICAL)

      mat_sizer = wx.FlexGridSizer(1, 3, 10, 6)	# rows, cols, hgap, vgap
      mat_sizer.Add((10,0), 1)
      mat_sizer.Add(materials_text, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
      mat_sizer.Add(materials_choice, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)

      #sb_sizer_mat.Add(mat_sizer, 0, wx.EXPAND | wx.ALL, 5)

      sizer.Add(mat_sizer, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 5)
      #sizer.Add(sb_sizer_mat, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 5)

    # Realize OK/Cancel buttons
    ok_butt = wx.Button(self, wx.ID_OK)
    ok_butt.SetDefault()
    cancel = wx.Button(self, wx.ID_CANCEL)

    btns = wx.StdDialogButtonSizer()
    btns.AddButton(ok_butt)
    btns.AddButton(cancel)
    btns.Realize()

    sizer.Add(btns, 0, wx.EXPAND | wx.ALL, 5)

    self.SetSizer(sizer)
    sizer.Fit(self)

    return wx.Dialog.ShowModal(self)

  def create_static_box(self, title):
    self.box = wx.StaticBox(self, -1, title)
    self.sb_sizer = wx.StaticBoxSizer(self.box, wx.VERTICAL)

  def set_user_sizer(self, user_sizer):
   #sizer = 
    if (self.sizer == None):
      sizer = self.sizer = wx.BoxSizer(wx.VERTICAL)
      if (self.sb_sizer):
	self.sb_sizer.Add(user_sizer, 0, wx.EXPAND | wx.ALL, 5)
	sizer.Add(self.sb_sizer, 0, wx.EXPAND | wx.ALL, 5)
      else:
	sizer.Add(user_sizer, 0, wx.EXPAND | wx.ALL, 5)

  ### Name checking
  def set_name_entry(self, entry_widget):
    self.name_entry = entry_widget

  def turn_on_unique_name_checking(self, used_names = None):
    if not self.name_entry:
      return False

    self.name_entry.SetValidator(UniqueNameValidator(used_names))
    return True

  ### Materials
  def set_materials(self, materials):
    self.show_materials = True
    self.materials = materials

  def set_selected_material(self, sel_material):
    self.sel_material = sel_material

  def set_material_list(self, mat_list):
    self.mat_list = mat_list

  ### Data
  def set_data_text_fields(self, text_entries):
    #print "Setting text fields data..."
    self.text_entries = text_entries

  def set_data_items(self, item_entries):
    self.item_entries = item_entries

  # return the index of the new item
  def add_data_items(self, item_entries):
    if isinstance(self.item_entries, list):
      self.item_entries.append(item_entries)
      return len(self.item_entries)-1
    return None

  def set_items_lists(self, items_lists):
    self.items_lists = items_lists

  # return the index of the new item
  def add_items_lists(self, items_lists):
    if isinstance(self.items_lists, list):
      self.items_lists.append(items_lists)
      return len(self.items_lists)-1
    return None

  def set_data_grid_plain(self, grid_entries = []):
    self.grid_entries = grid_entries

  def get_data(self):
    #print "Getting data..."
    data = list()
    if (self.text_entries):
      for entry in self.text_entries:
	data.append(entry.GetValue())

    if (self.item_entries):
      for ind in range(len(self.item_entries)):
	entry = self.item_entries[ind]
	sel_index = entry.GetSelection()
	data.append(self.items_lists[ind][sel_index])

    # last entry is always a chosen material, if material can be chosen
    mat_name = self.get_data_material()
    if mat_name:
      data.append(mat_name)

    return data

  def get_data_material(self):
    if self.materials_choice:
      sel_index = self.materials_choice.GetSelection()
      #print "***** sel_index:\n" + str(sel_index) + "\n"
      return self.mat_list[sel_index]
    return None

  def get_data_grid(self):
    datas = list()
    for grid in self.grid_entries:
      data = list()
      cols = grid.GetNumberCols()
      rows = grid.GetNumberRows()
      for r_ind in range(rows):
	r_entry = list()
	for c_ind in range(cols):
	  val = grid.GetCellValue(r_ind, c_ind) 
	  if val:
	    r_entry.append(val)
	data.append(r_entry)
      datas.append(data)
    return datas

