gtmenu.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Program GadtoolsMenu;
  2. {* gadtoolsmenu.p
  3. ** Example showing the basic usage of the menu system with a window.
  4. ** Menu layout is done with GadTools, as is recommended for applications.
  5. **
  6. *}
  7. uses Exec, Intuition, Utility, GadTools;
  8. {$I tagutils.inc}
  9. const
  10. mynewmenu : array[0..15] of tNewMenu = (
  11. (nm_Type: NM_TITLE; nm_Label:'Project'; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  12. (nm_Type: NM_ITEM; nm_Label:'Open...'; nm_CommKey:'O'; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  13. (nm_Type: NM_ITEM; nm_Label:'Save'; nm_CommKey:'S'; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  14. (nm_Type: NM_ITEM; nm_Label:nil; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  15. (nm_Type: NM_ITEM; nm_Label:'Print'; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  16. (nm_Type: NM_SUB; nm_Label:'Draft'; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  17. (nm_Type: NM_SUB; nm_Label:'NLQ'; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  18. (nm_Type: NM_ITEM; nm_Label:nil; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  19. (nm_Type: NM_ITEM; nm_Label:'Quit...'; nm_CommKey:'Q'; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  20. (nm_Type: NM_TITLE; nm_Label:'Edit'; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  21. (nm_Type: NM_ITEM; nm_Label:'Cut'; nm_CommKey:'X'; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  22. (nm_Type: NM_ITEM; nm_Label:'Copy'; nm_CommKey:'C'; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  23. (nm_Type: NM_ITEM; nm_Label:'Paste'; nm_CommKey:'V'; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  24. (nm_Type: NM_ITEM; nm_Label:nil; nm_CommKey: NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  25. (nm_Type: NM_ITEM; nm_Label:'Undo'; nm_CommKey:'Z'; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL),
  26. (nm_Type: NM_END; nm_Label:NIL; nm_CommKey:NIL; nm_Flags:0; nm_MutualExclude:0; nm_UserData:NIL));
  27. var
  28. win : pWindow;
  29. myVisualInfo : Pointer;
  30. menuStrip : pMenu;
  31. tags : array[0..6] of tTagItem;
  32. msg : pMessage;
  33. done : boolean;
  34. Procedure Die;
  35. begin
  36. if MenuStrip <> nil then begin
  37. ClearMenuStrip(win);
  38. FreeMenus(MenuStrip);
  39. end;
  40. if myVisualInfo <> nil then FreeVisualInfo(myVisualInfo);
  41. if win <> Nil then CloseWindow(win);
  42. if GadToolsBase <> nil then CloseLibrary(GadToolsBase);
  43. Halt(0);
  44. end;
  45. {*
  46. ** Watch the menus and wait for the user to select the close gadget
  47. ** or quit from the menus.
  48. *}
  49. PROCEDURE ProcessIDCMP;
  50. VAR
  51. IMessage : tIntuiMessage;
  52. IPtr : pIntuiMessage;
  53. Procedure ProcessMenu;
  54. var
  55. MenuNumber : Word;
  56. ItemNumber : Word;
  57. SubItemNumber : Word;
  58. begin
  59. if IMessage.Code = MENUNULL then
  60. Exit;
  61. MenuNumber := MenuNum(IMessage.Code);
  62. ItemNumber := ItemNum(IMessage.Code);
  63. SubItemNumber := SubNum(IMessage.Code);
  64. if (MenuNumber = 0) and (ItemNumber = 5) then done := true;
  65. end;
  66. begin
  67. IPtr := pIntuiMessage(Msg);
  68. IMessage := IPtr^;
  69. ReplyMsg(Msg);
  70. case IMessage.IClass of
  71. IDCMP_MENUPICK : ProcessMenu;
  72. IDCMP_CLOSEWINDOW : done := True;
  73. end;
  74. end;
  75. {*
  76. ** Open all of the required libraries and set-up the menus.
  77. *}
  78. begin
  79. GadToolsBase := OpenLibrary(PChar('gadtools.library'#0), 37);
  80. if GadToolsBase = nil then die;
  81. tags[0] := TagItem(WA_Width, 400);
  82. tags[1] := TagItem(WA_Activate, 1);
  83. tags[2] := TagItem(WA_Height, 100);
  84. tags[3] := TagItem(WA_CloseGadget, 1);
  85. tags[4] := TagItem(WA_Title, Long(PChar('Menu Test Window'#0)));
  86. tags[5] := TagItem(WA_IDCMP, IDCMP_CLOSEWINDOW or IDCMP_MENUPICK);
  87. tags[6].ti_Tag := TAG_END;
  88. win := OpenWindowTagList(NIL, @tags);
  89. if win = nil then die;
  90. myVisualInfo := GetVisualInfoA(win^.WScreen,nil);
  91. if myVisualInfo = nil then die;
  92. {
  93. make the barlabels
  94. }
  95. mynewmenu[3].nm_Label := PChar(NM_BARLABEL);
  96. mynewmenu[7].nm_Label := PChar(NM_BARLABEL);
  97. mynewmenu[13].nm_Label := PChar(NM_BARLABEL);
  98. if pExecBase(_ExecBase)^.LibNode.Lib_Version >= 39 then begin
  99. tags[0] := TagItem(GTMN_FrontPen, 1);
  100. tags[1].ti_Tag := TAG_END;
  101. MenuStrip := CreateMenusA(@mynewmenu,@tags);
  102. end else MenuStrip := CreateMenusA(@mynewmenu,NIL);
  103. if menuStrip = nil then die;
  104. if not LayoutMenusA(menuStrip, myVisualInfo,nil) then die;
  105. if not SetMenuStrip(win,menuStrip) then die;
  106. repeat
  107. Msg := WaitPort(win^.UserPort);
  108. Msg := GetMsg(win^.UserPort);
  109. ProcessIDCMP;
  110. until done;
  111. die;
  112. end.