WindowStack.c
上传用户:duwei1288
上传日期:2009-12-27
资源大小:451k
文件大小:4k
源码类别:

BREW编程

开发平台:

Visual C++

  1. #include "WindowStack.h"
  2. #include "IBookMark.h"
  3. #include "Push.h"
  4. #include "IHistory.h"
  5. #include "IInputWindow.h"
  6. #include "IMenuWindow.h"
  7. ////////////////defination of static var///////////////////////
  8. //定义一个窗口时,定义其相关函数然后在下面的数组中增加一条纪录
  9. static IWinFunctionDef atWinDef[]=
  10. {
  11. {WAPWINID_MAINMENU,IMENUWINDOW_Create,IMENUWINDOW_Release,IMENUWINDOW_Redraw,IMENUWINDOW_EventHandle},
  12. {WAPWINID_INPUT,IINPUTWINDOW_Create,IINPUTWINDOW_Release,IINPUTWINDOW_Redraw,IINPUTWINDOW_EventHandle},
  13. {WAPWINID_BOOKMARK,IBOOKMARKWIN_Create,IBOOKMARKWIN_Release,IBOOKMARKWIN_Redraw,IBOOKMARKWIN_EventHandle},
  14. {WAPWINID_PUSH,PUSHWIN_Create,PUSHWIN_Release,PUSHWIN_Redraw,NULL},
  15. {WAPWINID_HISTORY,HistoryWIN_Create,HistoryWIN_Release,HistoryWIN_Redraw,HISTORYWIN_EventHandle},
  16. {WAPWINID_NULL,NULL,NULL,NULL,NULL}
  17. };
  18. static IWindowStack*  pWinStack=NULL;
  19. ////////////////declaration of static function///////////////////
  20. //将栈顶窗口出栈
  21. static boolean IWINSTACK_DestroyTopWin();
  22. //绘制栈顶窗口
  23. static boolean IWINSTACK_DrawWindow();
  24. //根据窗口ID得到窗口处理函数
  25. static IWinFunctionDef* IWINSTACK_GetWindow(TWindowID nWinID);
  26. ////////////////defination of interface function/////////////////
  27. boolean IWINSTACK_CreateInstance()
  28. {
  29. AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
  30. if(!pApp) return FALSE;
  31. //为窗口分配空间
  32. if(NULL==pWinStack)
  33. pWinStack=MALLOCREC(IWindowStack);
  34. if(NULL==pWinStack)
  35. return FALSE;
  36. //为栈分配空间
  37. pWinStack->m_pStack=ISTACK_CreateInstance();
  38. if(NULL==pWinStack->m_pStack)
  39. {
  40. FREEIF(pWinStack);
  41. return FALSE;
  42. }
  43. pWinStack->m_pShell=pApp->m_pIShell;
  44. return TRUE;
  45. }
  46. boolean IWINSTACK_Release()
  47. {
  48. if(pWinStack)
  49. {
  50. //释放栈的空间
  51. while(TRUE==IWINSTACK_DestroyTopWin());
  52. ISTACK_Release(&pWinStack->m_pStack);
  53. //释放窗口的空间
  54. FREE(pWinStack);
  55. pWinStack=NULL;
  56. }
  57. return TRUE;
  58. }
  59. boolean IWINSTACK_HandleEvent(AEEEvent eCode, uint16 wParam, uint32 dwParam)
  60. {
  61. TWinElement* pWin;
  62. IWinFunctionDef* pFunc;
  63. if(!pWinStack||!pWinStack->m_pStack)
  64. return FALSE;
  65. //得到栈顶窗口ID
  66. pWin=ISTACK_GetTopElement(pWinStack->m_pStack);
  67. if(!pWin) return FALSE;
  68. //根据ID得到窗口处理函数
  69. pFunc=IWINSTACK_GetWindow(pWin->nWinID);
  70. //执行事件处理函数
  71. if(pFunc&&pFunc->pEventHdl)
  72. return pFunc->pEventHdl(pWin->m_pWin,eCode,wParam,dwParam);
  73. return FALSE;
  74. }
  75. boolean IWINSTACK_OpenWindow(TWindowID nWinID,void* pParam)
  76. {
  77. TWinElement* pWin;
  78. IWinFunctionDef* pFunc;
  79. if(!pWinStack)
  80. if(!IWINSTACK_CreateInstance())
  81. return FALSE;
  82. //查找与窗口相关的处理函数
  83. pFunc=IWINSTACK_GetWindow(nWinID);
  84. if(!pFunc||!pFunc->pCreate)
  85. return FALSE;
  86. //创建窗口资源并入栈
  87. pWin=MALLOCREC(TWinElement);
  88. if(!pWin) return FALSE;
  89. pWin->m_pWin=pFunc->pCreate(pWinStack->m_pShell,pParam);
  90. pWin->nWinID=nWinID;
  91. if(!pWin->m_pWin)
  92. {
  93. FREE(pWin);
  94. return FALSE;
  95. }
  96. if(!ISTACK_PushOnStack(pWinStack->m_pStack,pWin))
  97. {
  98. if(pFunc->pRelease)
  99. pFunc->pRelease(pWinStack->m_pShell,&pWin->m_pWin);
  100. FREE(pWin);
  101. return FALSE;
  102. }
  103. //绘制栈顶窗口
  104. return IWINSTACK_DrawWindow();
  105. }
  106. boolean IWINSTACK_CloseWindow(boolean bRedraw)
  107. {
  108. //栈顶窗口出栈
  109. IWINSTACK_DestroyTopWin();
  110. //绘制当前栈顶窗口
  111. if(bRedraw)
  112. IWINSTACK_DrawWindow();
  113. return TRUE;
  114. }
  115. ////////////////defination of static function/////////////////
  116. static boolean IWINSTACK_DestroyTopWin()
  117. {
  118. TWinElement* pWin; 
  119. IWinFunctionDef* pFunc;
  120. if(NULL==pWinStack)
  121. return FALSE;
  122. //将栈顶指针出栈
  123. pWin=ISTACK_PopFromStack(pWinStack->m_pStack);
  124. if(NULL==pWin)
  125. return FALSE;
  126. //释放出栈的栈顶窗口的资源
  127. pFunc=IWINSTACK_GetWindow(pWin->nWinID);
  128. if(NULL==pFunc)
  129. return FALSE;
  130. if(pFunc->pRelease)
  131. pFunc->pRelease(pWinStack->m_pShell,&pWin->m_pWin);
  132. //释放栈顶窗口的空间
  133. FREEIF(pWin);
  134. return TRUE;
  135. }
  136. static IWinFunctionDef* IWINSTACK_GetWindow(TWindowID nWinID)
  137. {
  138. int nLoop=0;
  139. while(WAPWINID_NULL!=atWinDef[nLoop].nWinID)
  140. {
  141. if(nWinID==atWinDef[nLoop].nWinID)
  142. return atWinDef+nLoop;
  143. else
  144. nLoop++;
  145. }
  146. return NULL;
  147. }
  148. static boolean IWINSTACK_DrawWindow()
  149. {
  150. TWinElement* pWin; 
  151. IWinFunctionDef* pFunc;
  152. if(NULL==pWinStack)
  153. return FALSE;
  154. //得到栈顶窗口
  155. pWin=ISTACK_GetTopElement(pWinStack->m_pStack);
  156. if(NULL==pWin)
  157. return FALSE;
  158. //得到栈顶窗口的处理函数
  159. pFunc=IWINSTACK_GetWindow(pWin->nWinID);
  160. if(NULL==pFunc)
  161. return FALSE;
  162. //绘制栈顶窗口
  163. if(pFunc->pRedraw)
  164. return pFunc->pRedraw(pWinStack->m_pShell,pWin->m_pWin);
  165. return FALSE;
  166. }