//----------------------------------------------------------------------------
//  File:         editlist.h
//
//  Description:  An editable listbox. Double clicking an item lets you edit
//                it. An EN_CHANGE notification is sent to the parent when
//                the user finishes editing.
//
//  Author:       Christopher Kohlhoff (chris at tenermerx dot com)
//
//  Date:         30 May 1997
//
//----------------------------------------------------------------------------
#ifndef editlist_h
#define editlist_h

#include <owl/listbox.h>

class _OWLCLASS TEdit;

class TEditList : public TListBox
{
  public: // construction / destruction
    TEditList(TWindow* parent, int id, int x, int y, int w, int h, TModule* module = 0);
    TEditList(TWindow* parent, int id, TModule* module = 0);
    virtual ~TEditList();

  public: // member access
    void BeginEdit();
    void EndEdit();
    TEdit* GetEdit() { return Edit; }

  public: // virtual function overrides
    virtual void SetupWindow();

  protected: // response table
    void EvLButtonDblClk(uint modKeys, TPoint& point);
    void EditKillFocus();
    DECLARE_RESPONSE_TABLE(TEditList);

  protected: // data members
    TEdit* Edit;
};


#endif  // editlist_h sentry.



//----------------------------------------------------------------------------
//  File:         editlist.cpp
//
//  Description:  An editable listbox. Double clicking an item lets you edit
//                it. An EN_CHANGE notification is sent to the parent when
//                the user finishes editing.
//
//  Author:       Christopher Kohlhoff (chris at tenermerx dot com)
//
//  Date:         30 May 1997
//
//----------------------------------------------------------------------------
#include <owl/pch.h>
#include <owl/edit.h>
#include <stdio.h>
#include "editlist.h"


const int EditId = 1234;


DEFINE_RESPONSE_TABLE1(TEditList, TListBox)
  EV_WM_LBUTTONDBLCLK,
  EV_EN_KILLFOCUS(EditId, EditKillFocus),
END_RESPONSE_TABLE;


//
// Dynamic creation.
//
TEditList::TEditList(TWindow* parent, int id, int x, int y, int w, int h, TModule* module)
: TListBox(parent, id, x, y, w, h, module)
{
  Edit = new TEdit(this, EditId, 0, 0, 0, 0, 0);
  Edit->Attr.Style &= ~WS_VISIBLE;
}


//
// Dialog control creation.
//
TEditList::TEditList(TWindow* parent, int id, TModule* module)
: TListBox(parent, id, module)
{
  Edit = new TEdit(this, EditId, 0, 0, 0, 0, 0);
  Edit->Attr.Style &= ~WS_VISIBLE;
}


TEditList::~TEditList()
{
  Destroy(IDCANCEL);
}


void TEditList::SetupWindow()
{
  TListBox::SetupWindow();

  // Give the edit box the same font as the list box.
  //
  Edit->SetWindowFont(GetWindowFont(), false);
}


//
// Display the edit box to allow user editing.
//
void TEditList::BeginEdit()
{
  int sel = GetSelIndex();
  if (sel >= 0)
  {
    // Set the text of the edit box from the list item.
    //
    TAPointer<char> text = new char[GetStringLen(sel) + 1];
    GetString(text, sel);
    Edit->SetText(text);

    // Update the position of the edit box.
    //
    TRect rect;
    GetItemRect(sel, rect);
    Edit->MoveWindow(rect, true);

    // Activate the edit box.
    //
    Edit->Show(SW_SHOW);
    Edit->SetSelection(0, -1);
    Edit->SetFocus();
  }
}


//
// Hide the edit box and make changes to list.
//
void TEditList::EndEdit()
{
  int sel = GetSelIndex();
  if (sel >= 0)
  {
    // Get the text from the edit box.
    //
    int length = Edit->GetWindowTextLength() + 1;
    TAPointer<char> text = new char[length];
    Edit->GetText(text, length);

    // Replace the line in the list.
    //
    if(Attr.Style & LBS_SORT)
    {
      DeleteString(sel);
      SetSelIndex(AddString(text));
    }
    else
    {
      InsertString(text, sel);
      DeleteString(sel + 1);
      SetSelIndex(sel);
    }

    // Hide the edit box.
    //
    Edit->Show(SW_HIDE);
    Edit->MoveWindow(0, 0, 0, 0, true);

    // Inform the parent of the change.
    //
    if (Parent)
      Parent->SendMessage(WM_COMMAND, MAKEWPARAM(Attr.Id, EN_CHANGE), (LPARAM) HWindow);
  }
}


//
// Double click lets user edit an item.
//
void TEditList::EvLButtonDblClk(uint modKeys, TPoint& point)
{
  TListBox::EvLButtonDblClk(modKeys, point);
  BeginEdit();
}


//
// Whenever the edit box loses focus, stop editing.
//
void TEditList::EditKillFocus()
{
  EndEdit();
}