00001
00002
00003
00004
00005
00006
00007 #include "stdinc.h"
00008 #include "icon.h"
00009 #include "eventhandler.h"
00010 #include "widgets.h"
00011 #include "menu.h"
00012 #include "widget.h"
00013 #include "exception.h"
00014 namespace MWidgets
00015 {
00016
00017
00018 ContextMenu::ContextMenu()
00019 {
00020 m_hMenu=CreatePopupMenu();
00021 };
00022
00023 void ContextMenu::TrackMenu(int x,int y,Widget* pParent)
00024 {
00025 pParent->m_hContextMenu=GetHMENU();
00026 POINT pt={x,y};
00027 ClientToScreen(pParent->GetHwnd(),&pt);
00028 TrackPopupMenu(m_hMenu,0,pt.x,pt.y,0,pParent->GetHwnd(),NULL);
00029 };
00030
00031 Menu::Menu()
00032 {
00033 m_hMenu=CreateMenu();
00034 };
00035
00036 Menu::~Menu()
00037 {
00038 std::map<std::string,BaseEventHandler*>::iterator i;
00039 for(i=m_mapHandlers.begin();i!=m_mapHandlers.end();i++)
00040 {
00041 if(i->second)
00042 delete i->second;
00043 }
00044
00045 DestroyMenu(m_hMenu);
00046 };
00047
00048 HMENU Menu::GetHMENU()
00049 {
00050 return m_hMenu;
00051 };
00052
00053 void Menu::InsertSubMenu(string path,int position, string label)
00054 {
00055 char* cpath=NULL;
00056 try
00057 {
00058 HMENU hCurMenu=m_hMenu;
00059 cpath=new char[path.length()+1];
00060 strcpy(cpath,path.c_str());
00061 char* tok=strtok(cpath,":");
00062 while(tok)
00063 {
00064 int id;
00065 sscanf(tok,"%d",&id);
00066 if(id>=0)
00067 {
00068 hCurMenu=GetSubMenu(hCurMenu,id);
00069 if(!hCurMenu)
00070 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00071 }
00072 else
00073 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00074 tok=strtok(NULL,":");
00075 }
00076 delete[] cpath;
00077 cpath=NULL;
00078 HMENU hSubMenu=CreatePopupMenu();
00079 if(!InsertMenu(hCurMenu,position,MF_STRING|MF_POPUP|MF_BYPOSITION,(UINT_PTR)hSubMenu,label.c_str()))
00080 EXCEPT("GetSubMenu - Wrong menu position %d!",position);
00081 }
00082 catch(Exception e)
00083 {
00084 if(cpath)
00085 delete[] cpath;
00086 CATCHEXCEPT(e,"MenuBar::InsertSubMenu");
00087 };
00088 };
00089
00090 void Menu::UpdateSubMenu(string path, string label)
00091 {
00092 char* cpath=NULL;
00093 try
00094 {
00095 HMENU hPrevMenu=m_hMenu;
00096 HMENU hCurMenu=m_hMenu;
00097 cpath=new char[path.length()+1];
00098 strcpy(cpath,path.c_str());
00099 char* tok=strtok(cpath,":");
00100 int id=-1;
00101 while(tok)
00102 {
00103 sscanf(tok,"%d",&id);
00104 if(id>=0)
00105 {
00106 hPrevMenu=hCurMenu;
00107 hCurMenu=GetSubMenu(hCurMenu,id);
00108 if(!hCurMenu)
00109 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00110 }
00111 else
00112 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00113 tok=strtok(NULL,":");
00114 }
00115 delete[] cpath;
00116 cpath=NULL;
00117 if(!ModifyMenu(hPrevMenu,id,MF_STRING|MF_POPUP|MF_BYPOSITION,(UINT_PTR)hCurMenu,label.c_str()))
00118 EXCEPT("GetSubMenu - Wrong menu position %d!",id);
00119 }
00120 catch(Exception e)
00121 {
00122 if(cpath)
00123 delete[] cpath;
00124 CATCHEXCEPT(e,"MenuBar::UpdateSubMenu");
00125 };
00126 };
00127
00128 void Menu::RemoveSubMenu(string path)
00129 {
00130 char* cpath=NULL;
00131 try
00132 {
00133 HMENU hPrevMenu=m_hMenu;
00134 HMENU hCurMenu=m_hMenu;
00135 cpath=new char[path.length()+1];
00136 strcpy(cpath,path.c_str());
00137 char* tok=strtok(cpath,":");
00138 int id=-1;
00139 while(tok)
00140 {
00141 sscanf(tok,"%d",&id);
00142 if(id>=0)
00143 {
00144 hPrevMenu=hCurMenu;
00145 hCurMenu=GetSubMenu(hCurMenu,id);
00146 if(!hCurMenu)
00147 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00148 }
00149 else
00150 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00151 tok=strtok(NULL,":");
00152 }
00153 delete[] cpath;
00154 cpath=NULL;
00155 if(!RemoveMenu(hPrevMenu,id,MF_BYPOSITION))
00156 EXCEPT("GetSubMenu - Wrong menu position %d!",id);
00157 else
00158 DestroyMenu(hCurMenu);
00159 }
00160 catch(Exception e)
00161 {
00162 if(cpath)
00163 delete[] cpath;
00164 CATCHEXCEPT(e,"MenuBar::RemoveSubMenu");
00165 };
00166 };
00167
00168 void Menu::EnableSubMenu(string path, BOOL enable)
00169 {
00170 char* cpath=NULL;
00171 try
00172 {
00173 HMENU hPrevMenu=m_hMenu;
00174 HMENU hCurMenu=m_hMenu;
00175 cpath=new char[path.length()+1];
00176 strcpy(cpath,path.c_str());
00177 char* tok=strtok(cpath,":");
00178 int id=-1;
00179 while(tok)
00180 {
00181 sscanf(tok,"%d",&id);
00182 if(id>=0)
00183 {
00184 hPrevMenu=hCurMenu;
00185 hCurMenu=GetSubMenu(hCurMenu,id);
00186 if(!hCurMenu)
00187 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00188 }
00189 else
00190 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00191 tok=strtok(NULL,":");
00192 }
00193 delete[] cpath;
00194 cpath=NULL;
00195 MENUITEMINFO info;
00196 info.cbSize=sizeof( MENUITEMINFO );
00197 info.fMask=MIIM_STATE;
00198
00199
00200 if(enable==FALSE)
00201 {
00202 GetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00203 info.fState|=MFS_GRAYED;
00204 SetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00205 }
00206 else
00207 {
00208 GetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00209 info.fState|=MFS_ENABLED;
00210 info.fState^=MFS_GRAYED;
00211 SetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00212 }
00213 }
00214 catch(Exception e)
00215 {
00216 if(cpath)
00217 delete[] cpath;
00218 CATCHEXCEPT(e,"MenuBar::UpdateSubMenu");
00219 };
00220 };
00221
00222 void Menu::InsertSubMenuItem(string path,int position, string label,BaseEventHandler* pEventHandler)
00223 {
00224 char* cpath=NULL;
00225 try
00226 {
00227 HMENU hCurMenu=m_hMenu;
00228 cpath=new char[path.length()+1];
00229 strcpy(cpath,path.c_str());
00230 char* tok=strtok(cpath,":");
00231 while(tok)
00232 {
00233 int id;
00234 sscanf(tok,"%d",&id);
00235 if(id>=0)
00236 {
00237 hCurMenu=GetSubMenu(hCurMenu,id);
00238 if(!hCurMenu)
00239 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00240 }
00241 else
00242 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00243 tok=strtok(NULL,":");
00244 }
00245 delete[] cpath;
00246 cpath=NULL;
00247 MENUITEMINFO info;
00248 info.cbSize=sizeof( MENUITEMINFO );
00249 info.fMask=MIIM_FTYPE|MIIM_STRING|MIIM_DATA|MIIM_ID;
00250 info.fType=MFT_STRING;
00251 info.wID=Widgets::Instance()->GetNewId();
00252 info.dwItemData=reinterpret_cast<DWORD>(pEventHandler);
00253 info.dwTypeData=(LPSTR)label.c_str();
00254 info.cch=(UINT)label.length();
00255
00256 char buf[10];
00257 itoa(position,buf,10);
00258 path+=":";
00259 path+=buf;
00260 m_mapHandlers[path]=pEventHandler;
00261 if(!InsertMenuItem(hCurMenu,position,TRUE,&info))
00262 EXCEPT("GetSubMenu - Wrong menu position %d!",position);
00263 }
00264 catch(Exception e)
00265 {
00266 if(cpath)
00267 delete[] cpath;
00268 CATCHEXCEPT(e,"MenuBar::InsertSubMenuItem");
00269 };
00270 };
00271
00272 void Menu::InsertSubMenuSeparator(string path,int position)
00273 {
00274 char* cpath=NULL;
00275 try
00276 {
00277 HMENU hCurMenu=m_hMenu;
00278 cpath=new char[path.length()+1];
00279 strcpy(cpath,path.c_str());
00280 char* tok=strtok(cpath,":");
00281 while(tok)
00282 {
00283 int id;
00284 sscanf(tok,"%d",&id);
00285 if(id>=0)
00286 {
00287 hCurMenu=GetSubMenu(hCurMenu,id);
00288 if(!hCurMenu)
00289 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00290 }
00291 else
00292 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00293 tok=strtok(NULL,":");
00294 }
00295 delete[] cpath;
00296 cpath=NULL;
00297 MENUITEMINFO info;
00298 info.cbSize=sizeof( MENUITEMINFO );
00299 info.fMask=MIIM_FTYPE;
00300 info.fType=MFT_SEPARATOR;
00301
00302 if(!InsertMenuItem(hCurMenu,position,TRUE,&info))
00303 EXCEPT("GetSubMenu - Wrong menu position %d!",position);
00304 }
00305 catch(Exception e)
00306 {
00307 if(cpath)
00308 delete[] cpath;
00309 CATCHEXCEPT(e,"MenuBar::InsertSubMenuSeparator");
00310 };
00311 };
00312
00313 void Menu::RemoveSubMenuItem(string path)
00314 {
00315 char* cpath=NULL;
00316 try
00317 {
00318 HMENU hPrevMenu=m_hMenu;
00319 HMENU hCurMenu=m_hMenu;
00320 cpath=new char[path.length()+1];
00321 strcpy(cpath,path.c_str());
00322 char* tok=strtok(cpath,":");
00323 int id=-1;
00324 while(tok)
00325 {
00326 sscanf(tok,"%d",&id);
00327 if(id>=0)
00328 {
00329 hPrevMenu=hCurMenu;
00330 hCurMenu=GetSubMenu(hCurMenu,id);
00331 if(!hCurMenu)
00332 break;
00333 }
00334 else
00335 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00336 tok=strtok(NULL,":");
00337 }
00338 delete[] cpath;
00339 cpath=NULL;
00340 if(!RemoveMenu(hPrevMenu,id,MF_BYPOSITION))
00341 EXCEPT("GetSubMenu - Wrong menu position %d!",id);
00342 else
00343 DestroyMenu(hCurMenu);
00344 }
00345 catch(Exception e)
00346 {
00347 if(cpath)
00348 delete[] cpath;
00349 CATCHEXCEPT(e,"MenuBar::RemoveSubMenuItem");
00350 };
00351 };
00352
00353 void Menu::EnableSubMenuItem(string path, BOOL enable)
00354 {
00355 char* cpath=NULL;
00356 try
00357 {
00358 HMENU hPrevMenu=m_hMenu;
00359 HMENU hCurMenu=m_hMenu;
00360 cpath=new char[path.length()+1];
00361 strcpy(cpath,path.c_str());
00362 char* tok=strtok(cpath,":");
00363 int id=-1;
00364 while(tok)
00365 {
00366 sscanf(tok,"%d",&id);
00367 if(id>=0)
00368 {
00369 hPrevMenu=hCurMenu;
00370 hCurMenu=GetSubMenu(hCurMenu,id);
00371 if(!hCurMenu)
00372 break;
00373 }
00374 else
00375 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00376 tok=strtok(NULL,":");
00377 }
00378 delete[] cpath;
00379 cpath=NULL;
00380 MENUITEMINFO info;
00381 info.cbSize=sizeof( MENUITEMINFO );
00382 info.fMask=MIIM_STATE;
00383
00384
00385 if(enable==FALSE)
00386 {
00387 GetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00388 info.fState|=MFS_GRAYED;
00389 SetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00390 }
00391 else
00392 {
00393 GetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00394 info.fState|=MFS_ENABLED;
00395 info.fState^=MFS_GRAYED;
00396 SetMenuItemInfo(hPrevMenu,id,TRUE,&info);
00397 }
00398 }
00399 catch(Exception e)
00400 {
00401 if(cpath)
00402 delete[] cpath;
00403 CATCHEXCEPT(e,"MenuBar::UpdateSubMenu");
00404 };
00405 };
00406
00407 void Menu::UpdateSubMenuItem(string path, string label)
00408 {
00409 char* cpath=NULL;
00410 try
00411 {
00412 HMENU hPrevMenu=m_hMenu;
00413 HMENU hCurMenu=m_hMenu;
00414 cpath=new char[path.length()+1];
00415 strcpy(cpath,path.c_str());
00416 char* tok=strtok(cpath,":");
00417 int id=-1;
00418 while(tok)
00419 {
00420 sscanf(tok,"%d",&id);
00421 if(id>=0)
00422 {
00423 hPrevMenu=hCurMenu;
00424 hCurMenu=GetSubMenu(hCurMenu,id);
00425 if(!hCurMenu)
00426 break;
00427 }
00428 else
00429 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00430
00431 tok=strtok(NULL,":");
00432 }
00433 delete[] cpath;
00434 cpath=NULL;
00435 MENUITEMINFO info;
00436 info.cbSize=sizeof( MENUITEMINFO );
00437 info.fMask=MIIM_FTYPE|MIIM_STRING;
00438 info.fType=MFT_STRING;
00439 info.dwTypeData=(LPSTR)label.c_str();
00440 info.cch=(UINT)label.length();
00441
00442 if(!SetMenuItemInfo(hPrevMenu,id,TRUE,&info))
00443 EXCEPT("GetSubMenu - Wrong menu position %d!",id);
00444 }
00445 catch(Exception e)
00446 {
00447 if(cpath)
00448 delete[] cpath;
00449 CATCHEXCEPT(e,"MenuBar::UpdateSubMenu");
00450 };
00451 };
00452
00453 void Menu::UpdateSubMenuItem(string path, BaseEventHandler* pEventHandler)
00454 {
00455 char* cpath=NULL;
00456 try
00457 {
00458 HMENU hPrevMenu=m_hMenu;
00459 HMENU hCurMenu=m_hMenu;
00460 cpath=new char[path.length()+1];
00461 strcpy(cpath,path.c_str());
00462 char* tok=strtok(cpath,":");
00463 int id=-1;
00464 while(tok)
00465 {
00466 sscanf(tok,"%d",&id);
00467 if(id>=0)
00468 {
00469 hPrevMenu=hCurMenu;
00470 hCurMenu=GetSubMenu(hCurMenu,id);
00471 if(!hCurMenu)
00472 break;
00473 }
00474 else
00475 EXCEPT("GetSubMenu - Wrong path position %d!",id);
00476
00477 tok=strtok(NULL,":");
00478 }
00479 delete[] cpath;
00480 cpath=NULL;
00481 MENUITEMINFO info;
00482 info.cbSize=sizeof( MENUITEMINFO );
00483 info.fMask=MIIM_DATA;
00484 info.dwItemData=reinterpret_cast<DWORD>(pEventHandler);
00485
00486
00487 if(!SetMenuItemInfo(hPrevMenu,id,TRUE,&info))
00488 EXCEPT("GetSubMenu - Wrong menu position %d!",id);
00489 else
00490 {
00491 if(m_mapHandlers[path])
00492 delete m_mapHandlers[path];
00493 m_mapHandlers[path]=pEventHandler;
00494 }
00495 }
00496 catch(Exception e)
00497 {
00498 if(cpath)
00499 delete[] cpath;
00500 CATCHEXCEPT(e,"MenuBar::UpdateSubMenu");
00501 };
00502 };
00503
00504 };