May 03, 2024, 10:41:28 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


What I've Learned

Started by Bruce Peaslee, December 30, 2005, 04:21:07 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Bruce Peaslee

December 30, 2005, 04:21:07 PM Last Edit: February 25, 2006, 11:26:57 AM by peaslee
My first project is coming along nicely, thanks to everyone's kind assistance. This project has a long way to go, but it is already kind of long for a thread; so I'll post what I've done so far.


// +===========================================================================+
// *                HoodMap.src (c) 2006 Campanile Data Systems                *
// *---------------------------------------------------------------------------*
// *                          For the Aurora Compiler                          *
// *===========================================================================*
//
// Version 4 (pre-alpha) 12/30/05
// Moved datawindow variable into mapwindow class

#AutoDefine "Off"

// *****************************************************************************
// *                           CONSTANT DECLARATIONS                           *
// *****************************************************************************

const NUMBER_OF_LOTS = 3;

// TrackMouseEvent constants
const TME_HOVER = 1;
const TME_LEAVE = 2;
const WM_MOUSEHOVER = 0x2A1;
const WM_MOUSELEAVE = 0x2A3;

// Color constants
const RED    = 0x0000FF;
const GREEN  = 0x00FF00;
const BLUE   = 0xFF0000;
const YELLOW = RED|GREEN;
const BLACK  = 0x000000;
const WHITE  = 0xFFFFFF;

// *****************************************************************************
// *                                 STRUCTURES                                *
// *****************************************************************************
struct LOTS
{
string owner1;
string owner2;
string address;
string homePhone;
string cellPhone;
string plan;
int sellingPrice;
int askingPrice; // if for sale
int ForSale;
pointer hwnd;
}
LOTS Lot[NUMBER_OF_LOTS+1];

struct TRACKMOUSEEVENT
{
  unsigned int cbSize;
  unsigned int dwFlags;
  unsigned int hwndTrack;
  unsigned int dwHoverTime;
}

// *****************************************************************************
// *                              API DECLARATIONS                             *
// *****************************************************************************
declare import, GetSysColor(index as int),int;
declare import,_TrackMouseEvent(tme as TRACKMOUSEEVENT),int;

// *****************************************************************************
// *                             CLASS DEFINITIONS                             *
// *****************************************************************************


// class ----------------------------------------------------- DataWindow:window
class myDataWindow:cwindow
{
}

// class ------------------------------------------------------- MyWindow:window
class MyWindow:cwindow
{
// Constructor/Destructor
declare MyWindow();
// Overridden methods
declare OnClose(), int;
declare OnCreate(), int;
// Additional methods

// Additional variables
int b_run;

}

MyWindow::MyWindow()
{
b_run = 1;
return;
}

MyWindow::OnClose(),int
{
b_run = 0;
return true;
}

MyWindow::OnCreate(), int
{
CenterWindow();
return true;
}

// class ------------------------------------------------------ MapWindow:Window
class MapWindow:cwindow
{
declare MapWindow();
declare OnMouseMove(int x,int y,int flags),int;
declare WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int;
int bHover;
int bDataWinOpen;
myDataWindow *pDataWindow;
}

MapWindow::MapWindow()
{
bHover = false;
bDataWinOpen = false;
pDataWindow = new(MyDataWindow,1);
pDataWindow->Create(10,10,200,200,AWS_BORDER|AWS_AUTODRAW,0,"Data",0);
pDataWindow->FrontPen(GREEN);
pDataWindow->DrawMode(TRANSPARENT);
return;
}

MapWindow::OnMouseMove(int x,int y,int flags),int
{
if(bHover = false)
{
TRACKMOUSEEVENT tme;
tme.cbSize      = LEN(TRACKMOUSEEVENT);
tme.dwFlags     = TME_HOVER | TME_LEAVE;
tme.hwndTrack   = m_hwnd;
tme.dwHoverTime = 25;
_TrackMouseEvent(tme);
}
return true;
}

MapWindow::WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int
{
int i;
select message
{
case WM_MOUSEHOVER:
bHover = true;
if (bDataWinOpen = false)
{
pDataWindow->ShowWindow(SWSHOW);
pDataWindow->SetWindowColor(BLACK); // also clears window of text
// locate record
for (i=1; i<NUMBER_OF_LOTS+1;i++)
{
if (Lot[i].hwnd = this)
{
pDataWindow->WriteText(5,5,Using("(#)",i));
pDataWindow->WriteText(35,5,Lot[i].address);
pDataWindow->Line(5,25,190,25,GREEN);
pDataWindow->WriteText(5,40,Lot[i].owner1);
pDataWindow->WriteText(5,55,Lot[i].owner2);
pDataWindow->Line(5,75,190,75,GREEN);
pDataWindow->WriteText(5, 95,"H:  " + Lot[i].homePhone);
pDataWindow->WriteText(5,110,"C:  " + Lot[i].cellPhone);
break;
}
}
bDataWinOpen = true;
}
case WM_MOUSELEAVE:
bHover = false;
pDataWindow->ShowWindow(SWHIDE);
bDataWinOpen = false;
}
return CWindow!!WndProc(message,wParam,lParam);
}

// *****************************************************************************
// *                              MAIN SUBROUTINE                              *
// *****************************************************************************

global sub main()
{
int i;

MyWindow win;
MapWindow *pTemp;

// test data
Lot[1].address = "9200 Hallmark Pl";
Lot[2].address = "9240 Hallmark Pl";
Lot[3].address = "9280 Hallmark Pl";
Lot[1].homePhone = "(707) 555-1111";
Lot[2].homePhone = "(707) 555-2222";
Lot[3].homePhone = "(707) 555-3333";
Lot[1].plan = "Hampton";
Lot[2].plan = "Sterling";
Lot[3].plan = "Hampton";
Lot[1].owner1 = "Bruce Peaslee";
Lot[1].owner2 = "Judy Mecham";
Lot[2].owner1 = "Paul";
Lot[2].owner2 = "Docmann";
Lot[3].owner1 = "Dick Cheney";
Lot[3].owner2 = "Al Qaida";
Lot[1].forSale = false;
Lot[2].forSale = false;
Lot[3].forSale = true;
Lot[3].askingPrice = 200000;


win.Create(0,0,500,500,
AWS_CAPTION | AWS_SYSMENU | AWS_AUTODRAW | AWS_VISIBLE | AWS_BORDER,0,"Hood Map",0);

for (i=1; i < NUMBER_OF_LOTS+1; i++) // create lot squares
{
pTemp = new(MapWindow,1);
pTemp->Create(10,10+115*(i-1),110,110,AWS_VISIBLE|AWS_BORDER|AWS_CHILD|AWS_AUTODRAW,0,"",win);
pTemp->SetWindowColor(GetSysColor(15));
pTemp->DrawMode(TRANSPARENT);
pTemp->SetFont("",8,500);
pTemp->WriteText(5,5,using("#",i));
pTemp->WriteText(55,90,Lot[i].plan);
pTemp->WriteText(5,20,Lot[i].address);
if (Lot[i].forSale = true)
{
pTemp->DrawRect(10,70,10,10,BLACK,RED);
pTemp->WriteText(30,68,Using("$#,",Lot[i].askingPrice));
}
Lot[i].hwnd = pTemp;
}
// message loop
do
{
wait();
}until win.b_run = 0;
return;
}

Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

Not bad.  Just a bit of cleanup needed.

Your Mapwindow class needs a destructor


MapWindow::_MapWindow()
{
  if(pDataWindow <> NULL)
       delete pDataWindow;
}


Don't forget to add the declare for it in the class definition.

And you need to delete the map window as well when your program exits.


  // message loop
  do
  {
    wait();
  }until win.b_run = 0;
   delete pTemp;
...


Remember everytime you use NEW your allocating memory.  It has to be deleted somewhere.  Especially on Win 9x systems as that memory will never be returned to the system pool unless you reboot.


Ionic Wind Support Team

Bruce Peaslee

December 30, 2005, 10:15:16 PM #2 Last Edit: December 31, 2005, 12:19:45 PM by peaslee
Thanks. I always needed help cleaning up after myself.ÂÃ,  ;)

Quote from: Ionic Wizard on December 30, 2005, 07:20:59 PM
Remember everytime you use NEW your allocating memory.ÂÃ,  It has to be deleted somewhere.ÂÃ,  Especially on Win 9x systems as that memory will never be returned to the system pool unless you reboot.

Wouldn't it be easier to find an API call that forced a reboot when the user exits? It's the Microsoft way.ÂÃ,  :D

EDIT: Seriously:  in my program I call NEW once for each data record (currently 3 times). Do I have to do multiple deletions or just the one?
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

For each pointer you allocate, you have to delete it. For example,
pointer p;
p = new(int, 1);
p = new(int, 1); // MEMORY LOSS!!!
delete p; // The second allocation doesn't lose any memory, but the first does.

but if you do this:
pointer p;
p = new(int, 1);
delete p;
p = new(int, 1);
delete p; // This is completely safe, no memory loss here


So calling new on a previously allocated pointer doesn't automatically delete the old value. You have to do it yourself.

Another issue to consider is this:
struct something
{
int index;
string *value;
}
pointer p;
p = new(something, 1);
*(something)p.value = new(dstring, 255);

When you want to destroy "p", you have to first destroy the value field first, otherwise that memory will be lost.