March 28, 2024, 04:37:19 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


OnClose() event

Started by Haim, March 03, 2006, 10:12:19 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Haim

I would appreciate some help on the following:
Can I intercept the onclose event, before the window closes?
For example, when the close icon of the window is clicked - I woould like to show a messagebox and than react to the users response to that messagebox - not just close the window automatically.

another related question I have is concerning the meaning of the returned value from OnClose. I have not noticed it's influence. Is there a difference betwenn return 1 or return 0?

Ionic Wind Support Team

Actually there is a bug there.  The WM_CLOSE message gets translated to AWM_CLOSE so we can intercept the event.  However the default handler of my window class is still returning 0 which causes the window to close no matter what you return.

So give me a few hours and tonights update will have the fix.

Paul.
Ionic Wind Support Team

Parker

I had that problem a couple weeks ago and thought I was doing something wrong... thanks for bringing that up, now I know it's not a problem with my code. But my solution was to override OnDestroy and return, er, I forget what, but whatever means that I handled it myself.

Haim

Thanks,
I am looking forward for the update.

Mike Stefanik

By the way, I believe that the standard procedure is to return true from a message handler if you've handled it, and don't want the default handler to do anything with it; otherwise return false.

One thing I'm not sure of is if he (currently) allows you to consume a message; I think it's always passed through so the only way to do that now would be to subclass it. It would be nice if he added a PreTranslateMessage override similar to MFC that would let you do that.
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation

Ionic Wind Support Team

Actually I do.  If you return TRUE the default windows handler won't get the message.  Unlike MFC that always passes the message through.  I've always hated the MFC implementation of it.

As fo what to return it depends on the message.  For WM_CLOSE the docs say to return 0 if you process the message.  But after testing it doesn't make any difference anyway.

For OnNotify it depends on the control.  With a TAB control you have to return false or the tabs won't switch.  Menu's are the same, returning FALSE from OnMenuInit is required if your main window is a frame window.

Paul.
Ionic Wind Support Team

Ionic Wind Support Team

The latest update (alpha 2 rev 9) has fixed this problem.  Here is some sample code showing how to ask the user if the window should be closed.


//MyWindow definition
class MyWindow : CWindow
{
declare virtual OnClose(),int;
}

global sub main()
{
MyWindow w;
w.Create(0,0,200,200,AWS_SYSMENU|AWS_VISIBLE,0,"Close Me",NULL);
do{ wait();} until w.m_hWnd = 0;
}


//MyWindow Implementation
MyWindow::OnClose(),int
{
if( MessageBox(this,"Are You Sure","Question",4) = 6)
{
Destroy();
}
return false;
}
Ionic Wind Support Team

ExMember001

This is an old post, but i'm having the same problem...
in the code below, why if i click a checkbox then the window close button my messagebox is not display?
but if i call onclose from a button like in this case apply, the messagebox is display...


//Dialog Controls ID
#define O_TREEVIEW 1
#define O_CancelB  2
#define O_ApplyB   3
#define O_TABS     4

//FileTypes Control ID
enum FT_ControlID
{
FT_FTGROUP = 100,
FT_CBMP1,
FT_CBMP2,
FT_CBMP3,
FT_CBM3U,
FT_CBOGG,
FT_CBWAV,
FT_CBAIF,
FT_CBADDPLUG
}

  #define TV_FIRST 0x1100
  #define TVM_GETNEXTITEM         (TV_FIRST + 10)
  #define TVM_EXPAND              (TV_FIRST + 2)
  #define TVE_EXPAND              0x0002
  #define TVGN_PARENT             0x0003
  #define TVGN_ROOT               0x0000
 
import INT SendMessage alias SendMessageA(INT hWnd,UNSIGNED INT Msg,UNSIGNED INT wParam,INT lParam);

//Treeview notification codes
#define TVCLICK     0xFFFFFFFE
#define TVDCLICK    0xFFFFFFFD
#define TVRCLICK    0xFFFFFFFB
#define TVDRCLICK   0xFFFFFFFA

class OptionsDLG:CDialog
{
declare  OnInitDialog(),int;
declare  OnClose(),int;
declare  OnControl(int nID, int nNotifyCode, unsigned int hControl),int;
declare  OnNotify(INT code,INT nID,NMHDR *pnmhdr),INT;

declare OpenDLG(CWINDOW *Parent);
declare OpenTabFileTypes();
declare CloseTabFileTypes();

CTABCTRL *tab;
CTreeView *tree;
CBUTTON *apply;
CControl *ftg,*mp1,*mp2,*mp3,*ogg,*wav,*aif,*m3u,*plug;

int filetypeview;
Int modification;
}

OptionsDLG::OpenDLG(CWINDOW *Parent)
{
Create(0,0,513,368,AWS_CAPTION|AWS_SYSMENU|AWS_TABSTOP|AWS_VISIBLE,0,"AmpKC - Options",Parent);
AddControl(CTTREEVIEW,"Static",15,15,136,298,0x50810037,0x200,O_TREEVIEW);
AddControl(CTDEFBUTTON,"Cancel",427,326,70,28,0x50010001,0x0,O_CancelB);
AddControl(CTBUTTON,"Apply",346,326,70,28,0x58010000,0x0,O_ApplyB);
AddControl(CTTABCTRL,"Tab Control",168,15,329,298,0x54010040,0x0,O_TABS);

AddControl(CTGROUPBOX,"Select the file types to open with AmpKC",175,45,313,140,0,0,FT_FTGROUP);
AddControl(CTCHECKBOX,"*.mp1",190,75,50,20,0,0,FT_CBMP1);
AddControl(CTCHECKBOX,"*.mp2",190,100,50,20,0,0,FT_CBMP2);
AddControl(CTCHECKBOX,"*.mp3",190,125,50,20,0,0,FT_CBMP3);
AddControl(CTCHECKBOX,"*.m3u",190,150,50,20,0,0,FT_CBM3U);
AddControl(CTCHECKBOX,"*.ogg",350,75,50,20,0,0,FT_CBOGG);
AddControl(CTCHECKBOX,"*.wav",350,100,50,20,0,0,FT_CBWAV);
AddControl(CTCHECKBOX,"*.aif",350,125,50,20,0,0,FT_CBAIF);

AddControl(CTCHECKBOX,"Register Automaticaly Formats Plugins",190,200,200,20,0,0,FT_CBADDPLUG);

DoModal();
return;
}

OptionsDLG::OnClose(),int
{
if modification = true
{
messagebox(this,"Some modifications has been made, do you want to apply changes before closing?","Warning!!");
}
CloseDialog(1);

return 0;
}

OptionsDLG::OnInitDialog(),int
{
/* Initialize any controls here */
centerwindow();

tree = GetControl(O_TREEVIEW);
tab = GetControl(O_TABS);
apply = GetControl(O_ApplyB);

ftg = GetControl(FT_FTGROUP);
mp1 = GetControl(FT_CBMP1);
mp2 = GetControl(FT_CBMP2);
mp3 = GetControl(FT_CBMP3);
ogg = GetControl(FT_CBOGG);
wav = GetControl(FT_CBWAV);
aif = GetControl(FT_CBAIF);
m3u = GetControl(FT_CBM3U);
plug = GetControl(FT_CBADDPLUG);

unsigned int root1 = tree->InsertItem("General",0);
unsigned int item = tree->InsertItem("File Types",root1);
tree->SelectItem(item);
Expand(tree->m_hwnd,root1);
string text;
tree->GetItemText(item,text,32);
tab->DeleteAllTabs();
tab->InsertTab(0, text);
OpenTabFileTypes();
filetypeview = true;

unsigned int root2 = tree->InsertItem("Plugins",0);
unsigned int root3 = tree->InsertItem("Skins",0);

modification = false;

return true;
}

OptionsDLG::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
select nID
{
case O_CancelB:
if(nNotifyCode = 0)
{
CloseDialog(1);
}

case O_ApplyB:
if(nNotifyCode = 0)
{
onclose();
}

case O_TABS:
/* respond to control notifications here */

case FT_CBMP1:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBMP2:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBMP3:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBOGG:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBWAV:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBAIF:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBM3U:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBADDPLUG:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}
}
return true;
}

OptionsDLG::OnNotify(INT code,INT nID,NMHDR *pnmhdr),INT
{
select nID
{
case O_TREEVIEW:
    select(code)
{
case TVDCLICK:
    unsigned int  item = tree->GetSelectedItem();
string text;
tree->GetItemText(item,text,32);
select text
{
case "File Types":
    if filetypeview <> true
{
tab->DeleteAllTabs();
    tab->InsertTab(0, text);
    OpenTabFileTypes();
}
default:
    if filetypeview <> false
{
        tab->DeleteAllTabs();
    CloseTabFileTypes();
}
}
}
}
return true;
}

OptionsDLG::OpenTabFileTypes()
{
ftg->ShowWindow(SWRESTORE);
mp1->ShowWindow(SWRESTORE);
mp2->ShowWindow(SWRESTORE);
mp3->ShowWindow(SWRESTORE);
m3u->ShowWindow(SWRESTORE);
ogg->ShowWindow(SWRESTORE);
wav->ShowWindow(SWRESTORE);
aif->ShowWindow(SWRESTORE);
plug->ShowWindow(SWRESTORE);
filetypeview = true;

return;
}

OptionsDLG::CloseTabFileTypes()
{
ftg->ShowWindow(SWHIDE);
mp1->ShowWindow(SWHIDE);
mp2->ShowWindow(SWHIDE);
mp3->ShowWindow(SWHIDE);
m3u->ShowWindow(SWHIDE);
ogg->ShowWindow(SWHIDE);
wav->ShowWindow(SWHIDE);
aif->ShowWindow(SWHIDE);
plug->ShowWindow(SWHIDE);
filetypeview = false;

return;
}

global sub main()
{
OptionsDLG optsd;
optsd.OpenDLG(0);
}

Global Sub GetParentItem(UNSIGNED INT hTreeView,UNSIGNED INT hChildItem),UNSIGNED INT
{
     return SendMessage(hTreeView, TVM_GETNEXTITEM, TVGN_PARENT, hChildItem);
}

Global Sub Expand(UNSIGNED INT hTreeView,UNSIGNED INT hparent),INT
{
return SendMessage(hTreeView, TVM_EXPAND, TVE_EXPAND, hparent);
}

Ionic Wind Support Team

Dialogs don't work the same way that windows do.  Clicking on the 'x' causes the dialog to be canceled, which sends an ID of 2 to your OnControl handler.  IDOK = 1, IDCANCEL = 2.  You should not used those id's for anything else in a dialog.

Paul.
Ionic Wind Support Team

ExMember001

ok, i understand but its not clear enough ;)
if i change id 1 and 2 to 10 and 11, then put in Oncontrol, "case IDCANCEL: onclose();"
why is it still not working?

Ionic Wind Support Team

OK My mistake.  The OnClose even is only sent for Non-modal dialogs.  Modal dialogs return the ID of IDCANCEL from the DoModal method, which is how it is supposed to work.

Use ShowDialog and process messages like so:


//Dialog Controls ID
#define O_TREEVIEW 5
#define O_CancelB  6
#define O_ApplyB   7
#define O_TABS     8

//FileTypes Control ID
enum FT_ControlID
{
FT_FTGROUP = 100,
FT_CBMP1,
FT_CBMP2,
FT_CBMP3,
FT_CBM3U,
FT_CBOGG,
FT_CBWAV,
FT_CBAIF,
FT_CBADDPLUG
}

  #define TV_FIRST 0x1100
  #define TVM_GETNEXTITEM         (TV_FIRST + 10)
  #define TVM_EXPAND              (TV_FIRST + 2)
  #define TVE_EXPAND              0x0002
  #define TVGN_PARENT             0x0003
  #define TVGN_ROOT               0x0000
 
import INT SendMessage alias SendMessageA(INT hWnd,UNSIGNED INT Msg,UNSIGNED INT wParam,INT lParam);

//Treeview notification codes
#define TVCLICK     0xFFFFFFFE
#define TVDCLICK    0xFFFFFFFD
#define TVRCLICK    0xFFFFFFFB
#define TVDRCLICK   0xFFFFFFFA

class OptionsDLG:CDialog
{
declare  OnInitDialog(),int;
declare  OnClose(),int;
declare  OnControl(int nID, int nNotifyCode, unsigned int hControl),int;
declare  OnNotify(INT code,INT nID,NMHDR *pnmhdr),INT;

declare OpenDLG(CWINDOW *Parent);
declare OpenTabFileTypes();
declare CloseTabFileTypes();

CTABCTRL *tab;
CTreeView *tree;
CBUTTON *apply;
CControl *ftg,*mp1,*mp2,*mp3,*ogg,*wav,*aif,*m3u,*plug;

int filetypeview;
Int modification;
}

OptionsDLG::OpenDLG(CWINDOW *Parent)
{
Create(0,0,513,368,AWS_CAPTION|AWS_SYSMENU|AWS_TABSTOP|AWS_VISIBLE,0,"AmpKC - Options",Parent);
AddControl(CTTREEVIEW,"Static",15,15,136,298,0x50810037,0x200,O_TREEVIEW);
AddControl(CTDEFBUTTON,"Cancel",427,326,70,28,0x50010001,0x0,O_CancelB);
AddControl(CTBUTTON,"Apply",346,326,70,28,0x58010000,0x0,O_ApplyB);
AddControl(CTTABCTRL,"Tab Control",168,15,329,298,0x54010040,0x0,O_TABS);

AddControl(CTGROUPBOX,"Select the file types to open with AmpKC",175,45,313,140,0,0,FT_FTGROUP);
AddControl(CTCHECKBOX,"*.mp1",190,75,50,20,0,0,FT_CBMP1);
AddControl(CTCHECKBOX,"*.mp2",190,100,50,20,0,0,FT_CBMP2);
AddControl(CTCHECKBOX,"*.mp3",190,125,50,20,0,0,FT_CBMP3);
AddControl(CTCHECKBOX,"*.m3u",190,150,50,20,0,0,FT_CBM3U);
AddControl(CTCHECKBOX,"*.ogg",350,75,50,20,0,0,FT_CBOGG);
AddControl(CTCHECKBOX,"*.wav",350,100,50,20,0,0,FT_CBWAV);
AddControl(CTCHECKBOX,"*.aif",350,125,50,20,0,0,FT_CBAIF);

AddControl(CTCHECKBOX,"Register Automaticaly Formats Plugins",190,200,200,20,0,0,FT_CBADDPLUG);

ShowDialog();
return;
}

OptionsDLG::OnClose(),int
{
if modification = true
{
messagebox(this,"Some modifications has been made, do you want to apply changes before closing?","Warning!!");
}
CloseDialog(1);

return 0;
}

OptionsDLG::OnInitDialog(),int
{
/* Initialize any controls here */
centerwindow();

tree = GetControl(O_TREEVIEW);
tab = GetControl(O_TABS);
apply = GetControl(O_ApplyB);

ftg = GetControl(FT_FTGROUP);
mp1 = GetControl(FT_CBMP1);
mp2 = GetControl(FT_CBMP2);
mp3 = GetControl(FT_CBMP3);
ogg = GetControl(FT_CBOGG);
wav = GetControl(FT_CBWAV);
aif = GetControl(FT_CBAIF);
m3u = GetControl(FT_CBM3U);
plug = GetControl(FT_CBADDPLUG);

unsigned int root1 = tree->InsertItem("General",0);
unsigned int item = tree->InsertItem("File Types",root1);
tree->SelectItem(item);
Expand(tree->m_hwnd,root1);
string text;
tree->GetItemText(item,text,32);
tab->DeleteAllTabs();
tab->InsertTab(0, text);
OpenTabFileTypes();
filetypeview = true;

unsigned int root2 = tree->InsertItem("Plugins",0);
unsigned int root3 = tree->InsertItem("Skins",0);

modification = false;

return true;
}

OptionsDLG::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
select nID
{
case O_CancelB:
if(nNotifyCode = 0)
{
CloseDialog(1);
}

case O_ApplyB:
if(nNotifyCode = 0)
{
onclose();
}

case O_TABS:
/* respond to control notifications here */

case FT_CBMP1:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBMP2:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBMP3:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBOGG:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBWAV:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBAIF:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBM3U:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}

case FT_CBADDPLUG:
    if(nNotifyCode = 0)
{
modification = true;
apply->enable(true);
}
}
return true;
}

OptionsDLG::OnNotify(INT code,INT nID,NMHDR *pnmhdr),INT
{
select nID
{
case O_TREEVIEW:
    select(code)
{
case TVDCLICK:
    unsigned int  item = tree->GetSelectedItem();
string text;
tree->GetItemText(item,text,32);
select text
{
case "File Types":
    if filetypeview <> true
{
tab->DeleteAllTabs();
    tab->InsertTab(0, text);
    OpenTabFileTypes();
}
default:
    if filetypeview <> false
{
        tab->DeleteAllTabs();
    CloseTabFileTypes();
}
}
}
}
return true;
}

OptionsDLG::OpenTabFileTypes()
{
ftg->ShowWindow(SWRESTORE);
mp1->ShowWindow(SWRESTORE);
mp2->ShowWindow(SWRESTORE);
mp3->ShowWindow(SWRESTORE);
m3u->ShowWindow(SWRESTORE);
ogg->ShowWindow(SWRESTORE);
wav->ShowWindow(SWRESTORE);
aif->ShowWindow(SWRESTORE);
plug->ShowWindow(SWRESTORE);
filetypeview = true;

return;
}

OptionsDLG::CloseTabFileTypes()
{
ftg->ShowWindow(SWHIDE);
mp1->ShowWindow(SWHIDE);
mp2->ShowWindow(SWHIDE);
mp3->ShowWindow(SWHIDE);
m3u->ShowWindow(SWHIDE);
ogg->ShowWindow(SWHIDE);
wav->ShowWindow(SWHIDE);
aif->ShowWindow(SWHIDE);
plug->ShowWindow(SWHIDE);
filetypeview = false;

return;
}

global sub main()
{
OptionsDLG optsd;
optsd.OpenDLG(0);
do{
wait();
} until !optsd.IsValid();
}

Global Sub GetParentItem(UNSIGNED INT hTreeView,UNSIGNED INT hChildItem),UNSIGNED INT
{
     return SendMessage(hTreeView, TVM_GETNEXTITEM, TVGN_PARENT, hChildItem);
}

Global Sub Expand(UNSIGNED INT hTreeView,UNSIGNED INT hparent),INT
{
return SendMessage(hTreeView, TVM_EXPAND, TVE_EXPAND, hparent);
}
Ionic Wind Support Team

ExMember001

Thanx Paul, Thats clear in my head now  :)