YYXMENU.CPP
上传用户:wtrl82617
上传日期:2007-01-07
资源大小:187k
文件大小:15k
源码类别:

界面编程

开发平台:

DOS

  1. // 1993 (c) ALL RIGHTS RESERVED
  2. // AUTHOR  BY XuYongYong
  3. /*  yyxmenu.cpp
  4. */
  5. #ifndef __YYXMENU_H
  6. #include "yyxmenu.h"
  7. #endif
  8. #ifndef __YYXWIN_H
  9. #include "yyxwin.h"
  10. #endif
  11. #ifndef __APPLICAT_H
  12. #include "applicat.h"
  13. #endif
  14. void menu_class::select()
  15. {
  16. setport (menu_viewport);
  17. PaintRect ( bounds, MENU_ITEM_SELECT_COLOR ,COPY_PUT );
  18. // if (->pfather_menu != NULL ) //The Only exception caused the mouse dead
  19. pfather_menu->psub_menu_selected  =this  ;
  20. draw_help_texts (help);
  21. draw ();
  22. }
  23. void menu_class::unselect ()
  24. {
  25. setport (menu_viewport);
  26. PaintRect ( bounds, MENU_RECT_COLOR ,COPY_PUT );
  27. pfather_menu->psub_menu_selected =NULL;
  28. draw ();
  29. }
  30. void menu_class::offsetmenu(int dx,int dy )
  31. { Tmenu   * ptmp;
  32.   int i;
  33. OffsetRect (&bounds,dx,dy );
  34. if ((ptmp =pson_menu) == NULL ) return;
  35. OffsetRect (&save_bitmap.bounds,dx,dy );
  36. for (i=0; i<sub_menu_nums ; i++ ){
  37. ptmp=(Tmenu *) ptmp->pright;
  38. ptmp->offsetmenu(dx,dy);
  39. }
  40. }
  41. void menu_class::draw_sub_menu_rect ()
  42. { struct Rect therect;
  43. setport (menu_viewport);
  44. FillRect ( save_bitmap.bounds , MENU_RECT_COLOR);
  45. therect = save_bitmap.bounds;
  46. // InsetRect (&therect,-5,-5 );
  47. setcolor (BLACK);
  48. if ( pfather_menu !=NULL ) FrameRect ( therect);
  49. }
  50. void menu_class::enter_menu ()
  51. {   Tobject * ptmp;
  52. int i;
  53. if ( pcurrent_menu->psub_menu_selected==NULL )  return ;
  54. if (pcurrent_menu->psub_menu_selected->status & DISABLE ) return;
  55. if ( pcurrent_menu->psub_menu_selected->pson_menu == NULL ) {
  56. if (pcurrent_menu->pfather_menu == NULL ) {
  57. pcurrent_menu = pcurrent_menu->psub_menu_selected;
  58. if (pcurrent_menu->psub_menu_selected !=NULL )
  59. pcurrent_menu->psub_menu_selected->select
  60. ();
  61. return;
  62. } else {
  63. // thequeue.SendMessage(pcurrent_menu->psub_menu_selected->ID
  64. // ,MenuActionMSG,pcurrent_menu->psub_menu_selected );
  65. // Note: this is a way of communication mode
  66. if (in_menu_trap)
  67. menu_save_queue.SendMessage(pcurrent_menu->psub_menu_selected->ID
  68. ,MenuActionMSG,pcurrent_menu->psub_menu_selected );
  69. else
  70. thequeue.SendMessage(pcurrent_menu->psub_menu_selected->ID
  71. ,MenuActionMSG,pcurrent_menu->psub_menu_selected );
  72. if (pcurrent_menu !=NULL ) pcurrent_menu->esc_all_menus();
  73. // menu_func ( pcurrent_menu->psub_menu_selected->ID  );
  74. return;
  75. } /* execute a command */
  76. }
  77. else   {
  78. pcurrent_menu = pcurrent_menu->psub_menu_selected;
  79. pcurrent_menu->save_bitmap.size
  80. =(unsigned long) RectSize (pcurrent_menu->save_bitmap.bounds);
  81. pcurrent_menu->save_bitmap.pbitmap
  82. = myfarmalloc (pcurrent_menu->save_bitmap.size );
  83. getimage ( pcurrent_menu->save_bitmap.bounds.left,
  84. pcurrent_menu->save_bitmap.bounds.top,
  85. pcurrent_menu->save_bitmap.bounds.right,
  86. pcurrent_menu->save_bitmap.bounds.bottom,
  87. pcurrent_menu->save_bitmap.pbitmap);
  88. }
  89. /* used by main menu and those selection which have sub_menus   */
  90. pcurrent_menu->draw_sub_menu_rect( );
  91. ptmp=pcurrent_menu->pson_menu;
  92. for (i=0;i<pcurrent_menu->sub_menu_nums ; i++ ) {
  93. if (! ( ptmp->status & INVISIBLE) )
  94. ptmp->draw ();
  95. ptmp=ptmp->pright ;
  96. }
  97. if (pcurrent_menu->psub_menu_selected !=NULL )
  98. pcurrent_menu->psub_menu_selected->select
  99. ();
  100. }
  101. void menu_class::draw ()
  102. {
  103. setport (menu_viewport);
  104. moveto
  105. (bounds.left+5,bounds.top);
  106. if (status & DISABLE ) {
  107. setcolor ( MENU_ITEM_DISABLED_COLOR );
  108. outtext  (title);
  109. }else if (hotkey == 0xff ) {
  110. if (this !=pfather_menu->psub_menu_selected )
  111. setcolor (MENU_ITEM_ENABLED_COLOR );
  112. else setcolor ( MENU_ITEM_SELECTED_COLOR );
  113. outtext (title );
  114. } else  {
  115. if (this !=pfather_menu->psub_menu_selected )
  116. setcolor (MENU_ITEM_ENABLED_COLOR );
  117. else setcolor ( MENU_ITEM_SELECTED_COLOR );
  118. outtext (title );
  119. draw_hotkey(title,hotkey,bounds.left+5,bounds.top);
  120. }
  121. }
  122. void  menu_class::draw_help_texts (char *help_texts)
  123. {
  124. setport (menu_viewport);
  125. FillRect ( on_line_help_rect,HELP_RECT_COLOR );
  126. moveto(on_line_help_rect.left+10,on_line_help_rect.top);
  127. setcolor (HELP_TEXT_COLOR);
  128. outtext (help_texts);
  129. }
  130. int  menu_class::esc_menu ()
  131. { // pcurrent_menu---<this
  132. if ( this->pfather_menu ==  NULL ) {
  133. if ( this->psub_menu_selected != NULL ) {
  134. this->psub_menu_selected->unselect
  135. ();
  136. // this->psub_menu_selected =NULL;
  137. /// if ( this->psub_menu_selected == NULL ) ///5/2/1994
  138. draw_help_texts(this-> help); /// else
  139. /// draw_help_texts(this->psub_menu_selected->help);
  140. }
  141. if (in_menu_trap){
  142. in_menu_trap =FALSE;  ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  143. sysmsg=menu_save_sysmsg;
  144. thequeue=menu_save_queue;
  145. }
  146. if ( pcurrent_selected_win !=NULL ) setport ( pcurrent_selected_win->viewport);
  147. /////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!
  148. return (0);
  149. }
  150. /* above is for main menu */
  151. if (this->save_bitmap.pbitmap != NULL ) {
  152. putimage ( this->save_bitmap.bounds.left ,
  153. this->save_bitmap.bounds.top,
  154. this->save_bitmap.pbitmap ,COPY_PUT  );
  155. farfree ( this->save_bitmap.pbitmap  );
  156. }
  157. pcurrent_menu= this ->pfather_menu ;
  158. /// if ( pcurrent_menu->psub_menu_selected == NULL )  ///5/2/1994
  159. draw_help_texts(pcurrent_menu-> help); /// else
  160. ///  draw_help_texts(pcurrent_menu->psub_menu_selected->help);
  161. return (1);
  162. }
  163. Tmenu * menu_class::get_next_sub_item (enum direction direct)
  164. { Tmenu *ptmp;
  165. ptmp =pcurrent_menu->psub_menu_selected;
  166. if (ptmp==NULL ) { if ( pcurrent_menu->pson_menu == NULL ) return NULL;
  167. else  ptmp =(Tmenu*)pcurrent_menu->pson_menu->pleft;
  168. }
  169. return (Tmenu*)ptmp->get_next_object( direct, TRUE);
  170. //determine whether to go to disabled item
  171. }
  172. int menu_class::Left_Right_key_handler(enum direction scroll_direction)
  173. {
  174. if (pcurrent_menu->pfather_menu->pfather_menu ==NULL)
  175. {
  176. pcurrent_menu->esc_menu ();
  177. change_select (pcurrent_menu->psub_menu_selected,
  178. pcurrent_menu->get_next_sub_item ( scroll_direction));
  179. pcurrent_menu->psub_menu_selected->enter_menu ();
  180. return TRUE;
  181.  }
  182.  else if ((pcurrent_menu->pfather_menu == NULL )&&
  183. (pcurrent_menu->psub_menu_selected !=NULL )) {
  184. change_select (pcurrent_menu->psub_menu_selected,
  185. pcurrent_menu->get_next_sub_item ( scroll_direction));
  186. return TRUE;
  187.  }
  188.  return FALSE;
  189. }
  190. void menu_class::esc_all_menus(void)
  191. {
  192. while ( pcurrent_menu->pfather_menu != NULL )
  193. pcurrent_menu->esc_menu ( );
  194. if (pcurrent_menu->psub_menu_selected !=NULL)
  195. pcurrent_menu->psub_menu_selected->unselect
  196. ();
  197. if (in_menu_trap){
  198. in_menu_trap =FALSE;  ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  199. sysmsg=menu_save_sysmsg;
  200. thequeue=menu_save_queue;
  201. }
  202. if ( pcurrent_selected_win !=NULL ) setport ( pcurrent_selected_win->viewport);
  203. // pcurrent_menu->psub_menu_selected=NULL;
  204. }
  205. int menu_class::bar_Imme_menu_key_handler( int key_scan_num)
  206. { Tobject * ptmp;
  207.   int i;
  208.   ptmp =pcurrent_menu->pson_menu;
  209.   for(i=0;i<pcurrent_menu->sub_menu_nums;i++ ){
  210. ptmp=ptmp->pright;
  211. if ( (    (pcurrent_menu->pfather_menu ==NULL)
  212.    && (alt_key[ toupper(ptmp->title[ptmp->hotkey])-'A'] ==key_scan_num)
  213.  )
  214.  ||
  215.  (    (pcurrent_menu->pfather_menu !=NULL)
  216.    && (  (toupper( ptmp->title[ptmp->hotkey])) ==
  217.  (toupper( lo (key_scan_num))
  218.   )
  219.  )
  220.  ||
  221.  (    (toupper( ptmp->title[ptmp->hotkey])) ==' ' )
  222.    && ( key_scan_num == ALT_SPACE )
  223.  )
  224.    ) {
  225. change_select (
  226. pcurrent_menu->psub_menu_selected, ptmp );
  227.   if (pcurrent_menu->psub_menu_selected != NULL ) {
  228. if (!in_menu_trap){
  229. in_menu_trap =TRUE; ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  230. menu_save_sysmsg=sysmsg;
  231. menu_save_queue=thequeue;
  232. }
  233. pcurrent_menu->psub_menu_selected->enter_menu ();
  234.   }
  235. return TRUE;
  236. }
  237.   }
  238.   for (i=0;i<Max_Imme_key_nums ;i++) {
  239. if (Immediate_key[i] ==key_scan_num ) {
  240. if (! Imme_Menu_fptr[i] ->status & DISABLE )
  241. // thequeue.SendMessage(Imme_Menu_ID[i],MenuActionMSG,NULL);
  242. if (in_menu_trap)
  243. menu_save_queue.SendMessage(Imme_Menu_ID[i],MenuActionMSG,NULL);
  244. else
  245. thequeue.SendMessage(Imme_Menu_ID[i],MenuActionMSG,NULL);
  246. if (pcurrent_menu !=NULL ) pcurrent_menu->esc_all_menus();
  247. return TRUE;
  248. }
  249.   }
  250.   return FALSE;
  251. }
  252. int  menu_class::key_pressed_handler( int key_scan_num )
  253. {
  254. switch (key_scan_num) {
  255. case LEFTKEY : return Left_Right_key_handler(TO_UP);
  256. case RIGHTKEY : return Left_Right_key_handler(TO_DOWN);
  257. case UPKEY :
  258. if (pcurrent_menu->pfather_menu !=NULL) {
  259. change_select (pcurrent_menu->psub_menu_selected,
  260. pcurrent_menu->get_next_sub_item (TO_UP));
  261. return TRUE;
  262. } else return FALSE;
  263. case DOWNKEY :
  264. if (pcurrent_menu->pfather_menu !=NULL)  {
  265. change_select (pcurrent_menu->psub_menu_selected,
  266. pcurrent_menu->get_next_sub_item (TO_DOWN));
  267. return TRUE;
  268. }  /* !!!!!! */
  269. case ENTERKEY :
  270. if (pcurrent_menu->psub_menu_selected != NULL ) {
  271. pcurrent_menu->psub_menu_selected->enter_menu ();
  272. return TRUE;
  273. } else return FALSE;
  274. case ESCKEY :   /* note: isn't same as ENTERKEY */
  275. if (in_menu_trap) {
  276. pcurrent_menu->esc_menu ();
  277. return TRUE;
  278. } else return FALSE;
  279. }
  280. if (   bar_Imme_menu_key_handler(key_scan_num)  == TRUE ) return TRUE;
  281. /* normal_key_handler (key_scan_num ); */
  282. esc_all_menus();
  283. return FALSE;
  284. }
  285. menu_class::menu_class (char *init_title,char *init_help)
  286. :object_class (ID,init_title,NORMAL, NORMAL,0,0,0,0 )
  287. {
  288. // title =init_title;
  289. help  =init_help;
  290. hotkey=1;
  291. Imme_key_num =0;
  292. // status =NORMAL;
  293. sub_menu_nums =1;
  294. // SetRect (&bounds ,0,0,0,0 );
  295. SetRect (&save_bitmap.bounds ,0,0,0,0);
  296. psub_menu_selected  =NULL;
  297. pfather_menu =NULL;
  298. pson_menu    =NULL;
  299. pright =NULL;
  300. pleft =NULL;
  301. // pmenu_operation = std_menu_operation;
  302. save_bitmap.pbitmap =NULL;
  303. pcurrent_menu =this;
  304. }
  305. void menu_class::createmenu_class
  306. (Tmenu *pfather,int ID,char *title,char *help,byte hotkey,int Imme_key_num,
  307.  byte status,int savebit_left,int savebit_top,int savebit_right,int savebit_bottom
  308.  )
  309. {
  310. int text_width=textwidth(title)+5;
  311. if ( pfather !=NULL )
  312. SetRect (&(this->bounds),0,0,0,0); //used for later
  313. this->ID = ID;
  314. // strcpy (this->title,title);
  315. this->help =help;
  316. // this->hotkey=hotkey;
  317. this->Imme_key_num =Imme_key_num;
  318. this->status =status;
  319. SetRect (&(this->save_bitmap.bounds),
  320.  savebit_left,savebit_top,savebit_right,savebit_bottom );
  321. this->pfather_menu =pfather;
  322. // this->pmenu_operation = std_menu_operation;
  323. this->save_bitmap.pbitmap =NULL;
  324. this->pson_menu     =NULL;
  325. this->psub_menu_selected  =NULL;
  326. this->sub_menu_nums  =0;
  327. this->pleft   =this;
  328. this->pright   =this;
  329. if (pfather !=NULL ){
  330. pfather->sub_menu_nums++;
  331. Tmenu *ptemp1;
  332. ptemp1=pfather->pson_menu;
  333. if (ptemp1==NULL ) {
  334. pfather->pson_menu=this;
  335. } else {
  336. this->pright =ptemp1;
  337. this->pleft  =ptemp1->pleft;
  338. ptemp1->pleft->pright =this;
  339. ptemp1->pleft  =this;
  340. }
  341. if (pfather->pfather_menu == NULL )
  342. // use 1st line of the procedure of clearing , rely on above
  343. SetRect (&(this->bounds),
  344. pleft->bounds.right,
  345. pfather->save_bitmap.bounds.top,
  346. pleft->bounds.right + text_width,
  347. pfather->save_bitmap.bounds.bottom
  348. );
  349. else {
  350. SetRect (&(this->bounds),
  351. pfather->save_bitmap.bounds.left,
  352. pfather->save_bitmap.bounds.bottom -bar_height,
  353. pfather->save_bitmap.bounds.right,
  354. pfather->save_bitmap.bounds.bottom
  355. );
  356. InsetRect (&(this->bounds),-2,-1);
  357. }
  358. }
  359. // return (this);
  360. }
  361. menu_class::menu_class (int ID,char *title_hotkey,char *help,Tmenu *pfather )
  362. :object_class (ID,title_hotkey,NORMAL, NORMAL,0,0,0,0 )
  363. { int i,j;
  364. //byte hotkey=0xff, status =NORMAL;
  365.   int Imme_key_num;
  366.   int savebit_left=0,savebit_right=0,savebit_top=0,savebit_bottom=0;
  367. //  get_title_hotkey(title_hotkey,hotkey);
  368.   int text_width =textwidth (title_hotkey) +5;
  369. // my_menu[0], i.e. the screen Menu
  370. if (pfather==NULL ) {
  371. SetRect(&bounds,0,0,menu_viewport.right-menu_viewport.left,bar_height);
  372. savebit_left=0;
  373. savebit_top =bar_height;
  374. savebit_right=menu_viewport.right-menu_viewport.left;
  375. savebit_bottom=bar_height+bar_height;
  376. }
  377. else if (pfather->pfather_menu ==NULL );
  378. //  menu in the menu_bar
  379. else if (pfather->pfather_menu->pfather_menu == NULL ){
  380. //  menu below the menu_bar
  381. if ( pfather->pson_menu==NULL) {
  382. pfather->save_bitmap.bounds =pfather->bounds;
  383. OffsetRect( &pfather->save_bitmap.bounds,0,bar_height );
  384. // pfather->save_bitmap.bounds.top -=1;
  385. } else pfather->save_bitmap.bounds.bottom +=bar_height;
  386. if ( pfather->save_bitmap.bounds.right-pfather->save_bitmap.bounds.left
  387. < text_width )
  388.  pfather->save_bitmap.bounds.right =
  389.  pfather->save_bitmap.bounds.left+text_width;
  390. }
  391. else {      // other case
  392. //  menu of the other case
  393. if ( pfather->pson_menu==NULL) {
  394. pfather->save_bitmap.bounds =pfather->bounds;
  395. OffsetRect( &pfather->save_bitmap.bounds,
  396. pfather->bounds.right-pfather->bounds.left ,0);
  397. pfather->save_bitmap.bounds.top -=2;
  398. } else pfather->save_bitmap.bounds.bottom +=bar_height;
  399. if ( pfather->save_bitmap.bounds.right-pfather->save_bitmap.bounds.left
  400. < text_width )
  401.  pfather->save_bitmap.bounds.right =
  402.  pfather->save_bitmap.bounds.left+text_width;
  403. }
  404. if (  (pfather != NULL ) && (pfather->pfather_menu !=NULL ) ) //down_menu
  405. if (pfather->pson_menu !=NULL)  {
  406. Tobject *ptemp=pfather->pson_menu;
  407. for (i=1;i<=pfather->sub_menu_nums;i++, ptemp=ptemp->pright)
  408. if (ptemp->bounds.right<save_bitmap.bounds.right)
  409. ptemp->bounds.right=pfather->save_bitmap.bounds.right-1;
  410. }
  411. createmenu_class
  412. (pfather,ID,title_hotkey,help,hotkey,Imme_key_num,status,savebit_left,
  413. savebit_top,savebit_right,savebit_bottom);
  414. }
  415. menu_class::~menu_class (void)
  416. {
  417. if (save_bitmap.pbitmap != NULL )
  418. farfree (save_bitmap.pbitmap );
  419. }
  420. int  menu_class::msg_handler( MSG& message )
  421. {
  422. switch ( message .Action){
  423. case KeyPressedMSG:
  424. return key_pressed_handler( key_code );
  425. case MouseLButtonDownMSG:
  426.   int x,y;
  427.   Tmenu *ptmp;
  428. sysmouse->get_posn();
  429. x=mouse_x; y=mouse_y;
  430. if (Global2LocalPort(x,y,menu_viewport)) {
  431. //  in menu_viewport
  432. Tmenu * ptmp1;
  433. ptmp =pcurrent_menu;
  434. ptmp1=NULL;
  435. // if ((ptmp!=NULL) && (PtInRect(x,y,pcurrent_menu->save_bitmap.bounds)) )
  436. if ( (ptmp!=NULL) )
  437. while (ptmp1==NULL)
  438. {
  439. if ((ptmp->save_bitmap.pbitmap !=NULL) &&
  440. (PtInRect (x,y,ptmp->save_bitmap.bounds))
  441.    )
  442. ptmp1=(Tmenu*)ptmp->pson_menu->get_object_thru_point (x,y,TRUE);
  443. else ptmp ->esc_menu();
  444. if (ptmp->pfather_menu ==NULL) break;
  445. ptmp =ptmp->pfather_menu;
  446. }
  447. if (ptmp1 !=NULL ) {
  448. if (ptmp1==pcurrent_menu->psub_menu_selected) ptmp1->enter_menu();
  449. else { change_select(pcurrent_menu->psub_menu_selected,ptmp1);
  450. if (!in_menu_trap) {
  451. in_menu_trap=TRUE;
  452. menu_save_sysmsg=sysmsg;
  453. menu_save_queue=thequeue;
  454. }
  455. }
  456. return TRUE;
  457. }
  458. return FALSE;
  459. } // in menu_viewport
  460. break;
  461. }
  462. return FALSE;
  463. }
  464. void menu_class::dispose_all_submenus()
  465. {   Tmenu *ptemp, *ptemp1;
  466. if (pson_menu==NULL ) return ;
  467. for (ptemp =pson_menu;1; ptemp =ptemp1) {
  468. ptemp1 =(Tmenu*) ( ((Tobject *)ptemp)->pright) ;
  469. if (ptemp  != NULL ) {
  470. ptemp->dispose_all_submenus();
  471. delete (ptemp);
  472. }
  473. if (ptemp1 ==pson_menu) break;
  474. }
  475. pson_menu=NULL;
  476. psub_menu_selected=NULL;
  477. }