; ------------------------------------------------------------------------------------------------

FUNCTION search_db_item::get_value

; This method returns the actual value of the compound widget.
; If the widget doesn't have a value for some reason, then the
; undefined value is returned.

IF self.searchlst->size () NE 0 THEN BEGIN

   self->create_db_querry

   RETURN, *self.value

ENDIF ELSE BEGIN

   RETURN, *self.undefined

ENDELSE

END

; ------------------------------------------------------------------------------------------------

PRO search_db_item::create_db_querry

; Get the value from each of the search fields and string them together to
; form a database search string.

; First set str to be a string variable.

str = ""

; Now call the list method to get a list of field names.

fields = self.searchlst->list ()

n_fields = N_ELEMENTS (fields)

FOR i = 0, n_fields - 1 DO BEGIN

   fobj = self.searchlst->get (fields [i])

   IF i GT 0 THEN str = str + ', '

   str = str + fobj->get_value ()

ENDFOR

self.value = PTR_NEW (str)

RETURN

END

; ------------------------------------------------------------------------------------------------

PRO search_db_item::add_search_field, val

IF self.searchlst->search (val) THEN RETURN

pos = STRPOS (val, "::")

IF pos EQ -1 THEN BEGIN

   label = val
   db    = ""

ENDIF ELSE BEGIN

   label = STRMID (val, pos + 2)
   db    = STRMID (val, 0, pos)

ENDELSE

obj = OBJ_NEW (                      $
        "DB_SEARCH_FIELD",           $
        self.lst_id,                 $
        NAME = val,                  $
        LABEL = label,               $
        DB    = db,                  $
        DESTROY_ON_EXIT = 0,         $
        FLDFONT = self.fieldfont,    $
        LBLFONT = self.labelfont,    $
        BTNFONT = self.buttonfnt     $
        )

self.searchlst->add, obj

END

; ------------------------------------------------------------------------------------------------

PRO search_db_item::del_search_field, val

obj = self.searchlst->unlink (val)

IF obj NE OBJ_NEW () THEN OBJ_DESTROY, obj

END

; ------------------------------------------------------------------------------------------------

PRO search_db_item::del_all_field

self.searchlst->clear

END

; ------------------------------------------------------------------------------------------------

FUNCTION search_db_item::init,  $ ; The compound widget SEARCH_DB_ITEM INIT method..
   parent,                      $ ; The parent widget. Required for all compound widgets.
   COLUMN = column,             $ ; Align the widget in columns.
   ROW = row,                   $ ; Align the widget in rows.
   _EXTRA = extra                 ; Passes along extra keywords to the text widget.

; Preprocess the row and column keywords.

row    = KEYWORD_SET (row)
column = KEYWORD_SET (column)

; Make sure that either row x/or column is enabled (row is default).

IF row EQ 1 AND column EQ 1 THEN row = 0
IF row EQ 0 AND column EQ 0 THEN column = 1

; Call the default widget intitialization routine

res = self->def_widget_obj::init (parent, _EXTRA = extra, COLUMN = column, ROW = row)

; Reset the widget title to the default widget title if needed.

IF self.title EQ "" THEN title = "Dababase Search List"

self.lbl_id = WIDGET_LABEL (self.align, VALUE =  self.title, FONT =  self.labelfont)

lst  = WIDGET_BASE (self.align, /COLUMN)

self.lst_id = lst

self.searchlst = OBJ_NEW ('OBJ_LIST')

RETURN, 1

END

; ------------------------------------------------------------------------------------------------

PRO search_db_item::cleanup

; Free the value pointer, if is actually pointing to a value..

PTR_FREE, self.value

; Call the default version of this procedure to do the rest of the cleanup.

self->def_widget_obj::cleanup

END

; ------------------------------------------------------------------------------------------------

PRO search_db_item_event__define

; The SEARCH_DB_ITEM Event Structure. Sent only if EVENT_PRO or EVENT_FUNC keywords
; have defined an event handler for the top-level base of the compound widget.

event = { SEARCH_DB_ITEM_EVENT,         $ ; The name of the event structure.
          INHERITS DEF_WIDGET_EVENT,    $ ; Default Widget Events Structure
          INDEX:   0,                   $ ; Returns the index of the selected item.
          CLICKS:  0,                   $ ; Returns either 1 or 2 depending on how the item was selected.
          NUM:     0,                   $ ; Returns the number of selected items.
          TYPE:    ""                   $ ; Event type: either "SELECT" or "CANCEL" or "CLICK".
        }

END

; ------------------------------------------------------------------------------------------------

PRO search_db_item__define

obj = { SEARCH_DB_ITEM,         $ ; The object class name.
        lst_id: 0L,             $ ; The list widget ID containing list of database fields.
        value: PTR_NEW (),      $ ; The value of the search widget.
        searchlst: OBJ_NEW (),  $ ; The names of the database items displayed in the list widget.
        INHERITS DEF_WIDGET_OBJ $ ; Default window object.
      }

END




