Intuition simple menu example

35 posts / 0 new
Last post
AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
Intuition simple menu example

Hello everyone.

can someone post a simple menu example?

I tried with these examples here: http://www.pcguru.plus.com/tutorial/menus2.html

i get four warnings for the following:

initialization makes integer from pointer without a cast

struct MenuItem item1 =
{ NULL, 0, 0, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text4, &text4, NULL, NULL, 0 };
struct MenuItem item2 =
{ &item1, 0, 12, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text3, &text3, NULL, NULL, 0 };
struct MenuItem item3 =
{ &item2, 0, 24, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text2, &text2, NULL, NULL, 0 };
struct MenuItem item4 =
{ &item3, 0, 36, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text1, &text1, NULL, NULL, 0 };

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
The Command field of struct

The Command field of struct MenuItem is a BYTE. It should be initialized with 0, not with NULL.

Actually this way of creating menus is from Kickstart 1.3 times. It needs manual layout and is not font-sensitive (unless you do font-sensitive layouting).
Since Kickstart 2.0 (and AFAIK still in OS 4.1) one should use GadTools to create menus. Here is an example: http://thomas-rapp.homepage.t-online.de/examples/gtmenu.c

hypex
hypex's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-09-09 16:20
That example is funny as it

That example is funny as it uses OpenWindowTags(). Well funny to me. It also uses the complicated Wait() method. I always used WaitPort() in those circumstances. It also uses GadTools messaging functions. :-? If you ask me thay example is buggy! :-)

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
@thomas thank you for the

@thomas

thank you for the example and the explaination. Going to try the code.

@Hypex
can you post, please, and example of the alternative methods instead of openwindowtags and wait?

hypex
hypex's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-09-09 16:20
Sure. I can modify the

Sure. I can modify the example code. Note there is no problem using OpenWindowTags() as it is easier to use. It was just unusual for a program doing old style menus. But using GadTools functions for Intuition events looks like a no-no to me if GadTools gadgets aren't even used. Looks like the programmer was trying to make some things easier. I also added some missing pointer checks :-)

  1. /* Menu Example 2 */
  2.  
  3. #include <proto/intuition.h>
  4. #include <proto/gadtools.h>
  5. #include <proto/exec.h>
  6. #include <proto/dos.h>
  7. #include <intuition/intuition.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. /* Menu definition */
  12.  
  13. struct IntuiText text1 =
  14. { 0, 1, JAM2, 4, 2, NULL, "Open", NULL };
  15. struct IntuiText text2 =
  16. { 0, 1, JAM2, 4, 2, NULL, "Save", NULL };
  17. struct IntuiText text3 =
  18. { 0, 1, JAM2, 4, 2, NULL, "Print", NULL };
  19. struct IntuiText text4 =
  20. { 0, 1, JAM2, 4, 2, NULL, "Quit", NULL };
  21.  
  22. struct MenuItem item4 =
  23. { NULL, 0, 36, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text4, &text4, NULL, NULL, 0 };
  24. struct MenuItem item3 =
  25. { &item4, 0, 24, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text3, &text3, NULL, NULL, 0 };
  26. struct MenuItem item2 =
  27. { &item3, 0, 12, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text2, &text2, NULL, NULL, 0 };
  28. struct MenuItem item1 =
  29. { &item2, 0, 0, 48, 12, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, &text1, &text1, NULL, NULL, 0 };
  30.  
  31. struct Menu menu1 =
  32. { NULL, 0, 0, 48, 12, MENUENABLED, "File", &item1, 0, 0, 0, 0 };
  33.  
  34. /* For printing in Window */
  35. UBYTE result[255];
  36.  
  37. struct IntuiText WinText =
  38. {1, 0, JAM1, 0, 0, NULL, &result[0], NULL};
  39.  
  40. struct NewWindow NewWindow =
  41. {
  42. 20, 20, /* LeftEdge and TopEdge */
  43. 300, 200, /* Width and Height */
  44. 0, 1, /* DetailPen and BlockPen */
  45. IDCMP_CLOSEWINDOW | IDCMP_MENUPICK, /* IDCMP Flags */
  46. WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_ACTIVATE, /* Flags */
  47. NULL, NULL, /* Gadget and Image pointers */
  48. "My Window", /* Window Title */
  49. NULL, /* Screen pointer */
  50. NULL, /* BitMap pointer */
  51. 0, 0, /* MinWidth and MinHeight */
  52. 0, 0, /* MaxWidth and MaxHeight */
  53. WBENCHSCREEN /* Type of window */
  54. };
  55.  
  56. /* Main program */
  57. int main()
  58. {
  59. struct Window *myWindow;
  60. int closewin = FALSE;
  61. struct IntuiMessage *msg;
  62. UWORD menuNumber, menuNum, itemNum, subNum;
  63. ULONG msgClass, line=12;
  64.  
  65. if (myWindow = OpenWindow(&NewWindow))
  66. {
  67.  
  68. /* Attach menu to window */
  69. SetMenuStrip (myWindow, &menu1);
  70.  
  71. while (closewin == FALSE) {
  72. WaitPort(myWindow->UserPort);
  73. if (msg = (struct IntuiMessage *)GetMsg(myWindow->UserPort))
  74. {
  75. msgClass = msg->Class;
  76. menuNumber = msg->Code; /* Get menu number */
  77. ReplyMsg((struct Message *)msg);
  78.  
  79. switch (msgClass) {
  80. case IDCMP_CLOSEWINDOW: /* Close window? */
  81. closewin = TRUE;
  82. break;
  83. case IDCMP_MENUPICK: /* Menu selected? */
  84. menuNum = MENUNUM(menuNumber); /* Split code into menus, items, subitems */
  85. itemNum = ITEMNUM(menuNumber);
  86. subNum = SUBNUM(menuNumber);
  87. /* Print selection in window */
  88. sprintf(result, "Menu %d Item %d", menuNum, itemNum);
  89. PrintIText(myWindow->RPort, &WinText, 5, line);
  90. line += 10; /* Next line */
  91. if (menuNum == 0 && itemNum == 3) /* Test for quit */
  92. closewin = TRUE;
  93. break;
  94. }
  95. }
  96. }
  97. Delay(50);
  98. ClearMenuStrip(myWindow); /* remove menu */
  99. CloseWindow(myWindow);
  100. }
  101. return(0);
  102. }
AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
Thank you for this new

Thank you for this new example.

So what should i use? OpenWindowTags or the window class?

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
@AmigaBlitter So what

@AmigaBlitter


So what should i use? OpenWindowTags or the window class?

Depends what you want to do. If you want to have font sensitive GUI elements then definitely window.class. If you want your window to have an iconify gadget then window.class helps with that too.

You should definitely use gadtools.library for menus though as in thomas' example code or even better use the WINDOW_NewMenu tag of window.class which makes things even easier since you won't have to mess around with gadtools.library even as window.class will do most of this work for you (you just need to init the NewMenu array and pass it to window.class using the WINDOW_NewMenu tag).

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Intuition simple menu example

@thomas

Since Kickstart 2.0 (and AFAIK still in OS 4.1) one should use GadTools to create menus.

Yes, this is true. Even object-oriented GUI toolkits like ReAction wrap around the GadTools menu creation and layouting system.

The new WINDOW_NewMenu tag in Window Class is trying to make things more straightforward but, in my view, somewhat misses the target. The tag takes a pointer to a NewMenu array that defines the menu's structure and initial state. So far so good, but my gripe is that each WM_OPEN (re)sets the menu to this initial state. In reality it means that the programmer must save the state of all menu items before iconification: the current state will be lost because uniconification entails a WM_OPEN = a menu reset. WINDOW_NewMenu cares to make things easier but then gives you more work to do. What you gain on the swings you lose on the roundabouts :-) In other words, bad implementation.

So I still prefer using GadTools for menus in my Window Class-based windows.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Intuition simple menu example

@AmigaBlitter

So what should i use? OpenWindowTags or the window class?

You should read this introductory article to give you an overview of how GUI programming is done in AmigaOS.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
@trixie So far so good, but

@trixie


So far so good, but my gripe is that each WM_OPEN (re)sets the menu to this initial state. In reality it means that the programmer must save the state of all menu items before iconification: the current state will be lost because uniconification entails a WM_OPEN = a menu reset. WINDOW_NewMenu cares to make things easier but then gives you more work to do. What you gain on the swings you lose on the roundabouts :-) In other words, bad implementation.

Not really that much trouble, but of course it would be easier if window.class would just update the state of the NewMenu structures before freeing the menus in WM_CLOSE. As a measure against programs that might declare the NewMenu array as const this feature could be enabled using a tag like WINDOW_UpdateNewMenuOnClose.

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Intuition simple menu example

@salass00

Not really that much trouble

It does bother me because I regularly use checkmarked or disabled items in menus, and the class is always resetting them to defaults at iconification time. Preserving them means extra work. The toolkit should help me, not give me more work on top of what I'm already busy enough doing.

if window.class would just update the state of the NewMenu structures before freeing the menus in WM_CLOSE

WM_ICONIFY you mean. The method should definitely care to preserve the menu state because iconification is not program termination but, rather, hibernation. There's absolutely no logic in resetting any state to its initial value when the program merely goes to sleep. Just like in life: if you fall asleep naked you'll likely wake up naked as well :-)

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
WM_ICONIFY you mean. The


WM_ICONIFY you mean. The method should definitely care to preserve the menu state because iconification is not program termination but, rather, hibernation. There's absolutely no logic in resetting any state to its initial value when the program merely goes to sleep. Just like in life: if you go to bed naked you'll likely wake up naked as well :-)

No, I mean both WM_CLOSE and WM_ICONIFY.

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Intuition simple menu example

@salass00

Whatever, as long as it works properly :-)

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

hypex
hypex's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-09-09 16:20
@AmigaBlitter Well you've

@AmigaBlitter

Well you've jumped from old style to new style. I think to use the window class you would need to learn about it where as OpenWindowTags() will let you directly open a window. You've started off with old style menus but do you want to stay that way on purpose or just implement a menu bar?

Either way it depends on what you want to create and if you need to keep it simple or need to go more low level.

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
@Trixie I readed your nice

@Trixie
I readed your nice article. It's really well written.

@hypex
Yes, i was a little confused using old, new, OO classes, macro and so on..

@Trixie @hypex
Could i have, please, a complete example that do the following:

* Open a screen (Pubscreen or custom screen)
* Open two windows on the opened screen
* attach different menu on the windows
* manage events for the two windows.

Thank you in advance.

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Intuition simple menu example

@AmigaBlitter

Could i have, please, a complete example

I don't have a ready example but if you study the User Interface Libraries section of the AmigaOS Documentation Wiki, you'll find some. For screen programming under AmigaOS4 I recommend having a look at an article I wrote for OS4coding.net.

Alternatively, if you want to learn to program GUIs in a object-oriented way, you will want to start with the BOOPSI section and then have a look at this source code, which shows (among other things) how to open a ReAction window using the Window Class.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
@Trixie I read many

@Trixie

I read many tutorial, but i haven't find a tutorial that explain how to open two (or more) windows under a customscreen with different menu for each window and different event listener.

thank you, however

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Here is an example:

Here is an example: http://thomas-rapp.homepage.t-online.de/examples/scrwin.c

Note that there are different ways to achieve the goal. Depending on the exact purpose, other methods might be better. You didn't describe detailed enough what you actually want.

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Intuition simple menu example

@AmigaBlitter

Sorry I can't be of more help, I haven't done any traditional Intuition programming since late 1990s (when OS3.5 introduced ReAction). I've been following the object-oriented path ever since.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
@thomas really thank you for

@thomas

really thank you for the example.

:)

hypex
hypex's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-09-09 16:20
@thomas Thanks. That saves

@thomas

Thanks. That saves me mocking up another example.

Unfortunately WaitPort() can't be used as complexities increase but that the way of the beast. :-)

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
@thomas The example works

@thomas
The example works fine.

What about if i need to attach some images on the different windows? Eventually even some graphical buttons.

Thank you very much.

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
And if i want to attach a

And if i want to attach a background image for every window? Moreover, i would like to know if is possible to use graphical buttons or use images as buttons.

Thank you

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
I saw other examples here to

I saw other examples here to load an image, but it's not done using the New OO methods. I would like to use the new methods suggested, to avoid the trixie anger :P

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Now you are exactly in the

Now you are exactly in the situation I had in mind when I wrote "Depending on the exact purpose, other methods might be better. You didn't describe detailed enough what you actually want".

You didn't tell us earlier what you want to do in the end and therefore we led you into a dead-end street. You got a simple example how to quickly open a window. But these basic windows do not allow to add "OO" objects.

If you had told us in the beginning that you want to add GUI objects to the windows, the example would have looked completely different. Now a complete new example needs to be developed.

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
Sorry if i've been not

Sorry if i've been not clear.

I want to learn reaction programming with the latest OO methods, with the methods described by trixie (what to do and not to do).

Your code works very well and it0s clear. Now i would like to know how to load an inmage in a window (background) and to load other images in this window (as graphical buttons), using your scrwin.

Thank you
Kind regards

P.S.

I've sent you some PM.

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Here is a ReAction example

Here is a ReAction example with screen, window, backfill graphics, graphical buttons and menu: http://thomas-rapp.homepage.t-online.de/examples/toolbar.c

Your home work is to add the second window :-)

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
Thank you.as i said in a

Thank you.as i said in a previous post, i'm a little confused with the current programming methods on os4, so forgive me for any mistake i do evem with names and terms. i would like. to learn the last shout on os4 programming so any help is welcome.
i read trixie tutorial and recomandations and studying your code
:)

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Well, my example compiles on

Well, my example compiles on OS4 but was made with OS3 in mind. If you want to use "the last shout" on OS4, you should check new functions and attributes for optimisation.

For example the make_menu function is unnecessary if you use WINDOW_NewMenu instead of WINDOW_MenuStrip. You need to GetAttr() WINDOW_MenuStrip later in the WMHI_MENUPICK case.

Similarly you might want to use CreateBackFillHook with BFHA_BitMap instead of using a custom backfill routine.

AmigaBlitter
AmigaBlitter's picture
Offline
Last seen: 6 years 9 months ago
Joined: 2012-02-18 19:55
Your home work is to add the

Your home work is to add the second window :-)

Nah, too hard for me. I'm in the "study" phase at the moment. I understood the more easy scrwin, but toolbar it's more complicated for me. Btw, no images are loaded for the toolbar (a requester appear instead).

Thank you. I continue to study and try.

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
The toolbar needs AISS.

The toolbar needs AISS. http://www.masonicons.info/6.html

How can it be that you want "the last shout" in programming but don't use "the last shout" regarding updates?

Pages

Log in or register to post comments