home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <windows.h>
- #include <owl\applicat.h>
- #include <owl\button.h>
- #include <owl\edit.h>
- #include <owl\framewin.h>
- #include <owl\listbox.h>
- #include <owl\static.h>
- #include <owl\window.h>
- #include <owl\window.rh>
-
- #include "ctllst1.h"
-
- class TMyWindow : public TWindow
- {
- public:
- TMyWindow(TWindow *parent = 0);
- virtual ~TMyWindow();
-
- protected:
- virtual void SetupWindow();
-
- void CbAdd();
- void CbDel();
- void CbGetSelStr();
- void CbSetSelStr();
- void CbGetSelIdx();
- void CbSetSelIdx();
- void CbGetStr();
- void CmExit();
-
- private:
- TListBox *list;
- TEdit *strbox, *idxbox;
-
- DECLARE_RESPONSE_TABLE(TMyWindow);
- };
- DEFINE_RESPONSE_TABLE1(TMyWindow, TWindow)
- EV_COMMAND(CM_EXIT, CmExit),
- EV_BN_CLICKED(IDB_ADD, CbAdd),
- EV_BN_CLICKED(IDB_DEL, CbDel),
- EV_BN_CLICKED(IDB_GETSELSTR, CbGetSelStr),
- EV_BN_CLICKED(IDB_SETSELSTR, CbSetSelStr),
- EV_BN_CLICKED(IDB_GETSELIDX, CbGetSelIdx),
- EV_BN_CLICKED(IDB_SETSELIDX, CbSetSelIdx),
- EV_BN_CLICKED(IDB_GETSTR, CbGetStr),
- EV_BN_CLICKED(IDB_EXIT, CmExit),
- END_RESPONSE_TABLE;
-
- TMyWindow::TMyWindow(TWindow *parent)
- {
- Init(parent, 0, 0);
-
- int lowvspacing = 5,
- hivspacing = 25,
- hspacing = 50,
- wctl = 150,
- hctl = 30,
- x0 = 30,
- y0 = 50,
- y1;
- int wbox = 2 * wctl + hspacing;
- int hlist = hctl + lowvspacing + 4 * (hctl + hivspacing);
-
- int x = x0, y = y0;
-
- // Create the listbox and its label
- //
- new TStatic(this, -1, "List Box", x, y, wctl, hctl);
- y += hctl + lowvspacing;
- list = new TListBox(this, IDL_STRINGS, x, y, wctl, hlist);
-
- // Create the edit boxes and their labels
- //
- x += wctl + hspacing;
- y = y0;
- new TStatic(this, -1, "String Box", x, y, wctl, hctl);
- y += hctl + lowvspacing;
- strbox = new TEdit(this, IDE_STRING, "", x, y, wbox, hctl);
- y += hctl + hivspacing;
- new TStatic(this, -1, "Index Box", x, y, wctl, hctl);
- y += hctl + lowvspacing;
- idxbox = new TEdit(this, IDE_INDEX, "", x, y, wbox, hctl);
-
- // Create first column of buttons
- //
- y1 = y += hctl + hivspacing;
- new TButton(this, IDB_ADD, "Add String", x, y, wctl, hctl);
- y += hctl + hivspacing;
- new TButton(this, IDB_DEL, "Delete String", x, y, wctl, hctl);
- y += hctl + hivspacing;
- new TButton(this, IDB_GETSELSTR, "Get Selected String", x, y, wctl, hctl);
- y += hctl + hivspacing;
- new TButton(this, IDB_SETSELSTR, "Set Selected String", x, y, wctl, hctl);
-
- // Create the second column of buttons
- y = y1;
- x += wctl + hspacing;
- new TButton(this, IDB_GETSELIDX, "Get Selected Index", x, y, wctl, hctl);
- y += hctl + hivspacing;
- new TButton(this, IDB_SETSELIDX, "Set Selected Index", x, y, wctl, hctl);
- y += hctl + hivspacing;
- new TButton(this, IDB_GETSTR, "Get String by Index", x, y, wctl, hctl);
- y += hctl + hivspacing;
- new TButton(this, IDB_EXIT, "Exit", x, y, wctl, hctl);
- }
-
- TMyWindow::~TMyWindow()
- {
- }
-
- void TMyWindow::SetupWindow()
- {
- TWindow::SetupWindow(); // Initialize the visual element
-
- // Initialize the list box with some data and
- // select the second item
- //
- if (list)
- {
- list->AddString("Keith");
- list->AddString("Kevin");
- list->AddString("Ingrid");
- list->AddString("Roger");
- list->AddString("Rick");
- list->AddString("Beth");
- list->AddString("Kate");
- list->AddString("James");
- list->SetSelIndex(1);
- }
- }
-
- void TMyWindow::CbAdd()
- {
- if (strbox && list)
- {
- char *str;
- int size = strbox->GetWindowTextLength() + 1;
- if ((size > 1) && (NULL != (str = new char[size])))
- {
- strbox->GetWindowText(str, size);
- if (list->FindExactString(str, -1) >= 0)
- MessageBox("Cannot add duplicate names", "Bad Data");
- else
- {
- int ix = list->AddString(str);
- list->SetSelIndex(ix);
- }
- delete str;
- }
- }
- }
-
- void TMyWindow::CbDel()
- {
- if (list)
- {
- int ix = list->GetSelIndex();
- list->DeleteString(ix);
- list->SetSelIndex((ix > 0) ? (ix - 1) : 0);
- }
- }
-
- void TMyWindow::CbGetSelStr()
- {
- if (list && strbox)
- {
- char *str;
- int ix = list->GetSelIndex();
- if (ix >= 0)
- {
- if (NULL != (str = new char[list->GetStringLen(ix) + 1]))
- {
- list->GetString(str, ix);
- strbox->SetWindowText(str);
- delete str;
- }
- }
- }
- }
-
- void TMyWindow::CbSetSelStr()
- {
- if (list && strbox)
- {
- int ix = list->GetSelIndex();
-
- char *str;
- int size = strbox->GetWindowTextLength() + 1;
- if ((size > 1) && (NULL != (str = new char[size])))
- {
- strbox->GetWindowText(str, size);
- if (list->FindExactString(str, -1) >= 0)
- MessageBox("Cannot add duplicate names", "Bad Data");
- else
- {
- list->DeleteString(ix);
- ix = list->AddString(str);
- list->SetSelIndex(ix);
- }
- delete str;
- }
- }
- }
-
- void TMyWindow::CbGetSelIdx()
- {
- if (list && idxbox)
- {
- char str[15];
- sprintf(str, "%d", list->GetSelIndex());
- idxbox->SetWindowText(str);
- }
- }
-
- void TMyWindow::CbSetSelIdx()
- {
- if (list && idxbox)
- {
- char *str;
- int size = idxbox->GetWindowTextLength() + 1;
- if ((size > 1) && (NULL != (str = new char[size])))
- {
- idxbox->GetWindowText(str, size);
- list->SetSelIndex(atoi(str));
- delete str;
- }
- }
- }
-
- void TMyWindow::CbGetStr()
- {
- if (list && idxbox && strbox)
- {
- char *str;
- int ix = -1;
- int size = idxbox->GetWindowTextLength() + 1;
- if ((size > 1) && (NULL != (str = new char[size])))
- {
- idxbox->GetWindowText(str, size);
- ix = atoi(str);
- delete str;
- }
- if ((ix >= 0) && (NULL != (str = new char[list->GetStringLen(ix) + 1])))
- {
- list->GetString(str, ix);
- strbox->SetWindowText(str);
- delete str;
- }
- }
- }
-
-
- void TMyWindow::CmExit()
- {
- SendMessage(WM_CLOSE);
- }
-
- class TListApp : public TApplication
- {
- public:
- TListApp() : TApplication()
- { nCmdShow = SW_SHOWMAXIMIZED; }
-
- void InitMainWindow()
- {
- SetMainWindow(new TFrameWindow( 0,
- "Simple List Box Tester Application",
- new TMyWindow ));
- GetMainWindow()->AssignMenu("EXITMENU");
- }
- };
-
- int OwlMain(int, char *[])
- {
- return TListApp().Run();
- }
-