Beginning Wimp Programming in C (RESET)
François Vanzeveren (2221) 241 posts |
Dear all I am very glad to see how a “simple” question related to beginning programming in C turned into a frenetic discussion about licensing issues :) Let’s rewind a bit and set up the context. Voilà. My idea here is to post issues I encounter in this exercice in search for help and solutions. I hope this can help beginners like me in the future. I intend to post the code somewhere as I progress. So, let’s start with my first issue. Chapter 3 – Getting on the Icon Bar
Here is how I translated it: void create_icon(void) { } The application run perfectly well. The application is on the icon bar, but the icon is not visible. Indeed, I can adjust click in the area where the icon should display and this cause the aplication to quit as expected! Thank you for your help. François |
François Vanzeveren (2221) 241 posts |
My god… I can I format properly the code?? Newlines are not taken into account, underscores are disappearing… |
Jan-Jaap van der Geer (123) 63 posts |
You do have an icon called “Test” in the wimp spritepool? (I note that in the BASIC example it’s called “!Test” and not “Test”… Is that intentional?) I question your decision not to use OSLib. I understand not wanting to have a library with all kinds of magic in between, but OSLib is not like that. It is very close to the SWI API, and so you don’t need to define tedious structures and stuff like you are doing now. But it’s your call:) |
Colin (478) 2433 posts |
When entering your code start the first line with ‘bc.. ’ without the quotes but including the space and start the first paragraph after your code with ’p. ’ Then textile won’t mess with your code. Using your definition of IconBlk you need
replaced with
|
David Pitt (102) 743 posts |
What is the name of the icon? It is “!Test” in the BASIC but “Test” in the C, unless the mangling has mangled something. Wrapping the code <pre> and </pre> tags is the way to go. |
François Vanzeveren (2221) 241 posts |
Dear all, Thank you for your help. sprintf((char*)icon_blk.icon_data,“!test”); Now, chapter 4 😁 |
Steve Fryatt (216) 2105 posts |
What about bounds checking on the data block? How do you know that you haven’t written over the end of the available memory? I’d use something like
when using OSLib (which kindly defines osspriteop_NAME_LIMIT for me); you’ll need to define your own constants if you want to “stay close to the PRM”, but I’d strongly recommend not assuming. And yes, strncpy() is correct here (I had to think about it when I copied that code, too). If the sprite name is 12 characters, you don’t need to terminate it. ETA: Yes, in this case with an in-code constant, it probably doesn’t matter. Good habits are useful, though. :-) |
Rick Murray (539) 13840 posts |
Or any library for that matter. The point of a library is to abstract you away from the low-level stuff. Here’s how it is done in DeskLib:
The first line will create an iconbar sprite from the “!appname” sprite (change as required) in the private sprite area previously created, referred to by “resource_sprites”. It looks like this does the same thing, RISC_OSLib style:
I include the information about handling clicks on the icon, as that’s the next part and useful libraries will make this all much easier for you… |
Rick Murray (539) 13840 posts |
Just out of interest – what happens if your app is called !MyMarvellousApp? Just tried it. App name is “!MyMarvellousApp”. Sprite name is “!mymarvellou”. IconSpriting the sprites file gives the app a little icon. |
Chris Hall (132) 3554 posts |
Probably you would find out when you tried to register it. |
Chris Mahoney (1684) 2165 posts |
According to the docs, app name allocations have no length restriction so long as the first nine characters are unique. |
John Williams (567) 768 posts |
This, of course, has serious implications for the naming of sequels! You’d get away with !Rocky2, but more complex sequels will require careful planning! !RockyApp2 for example! |
Colin (478) 2433 posts |
You are just being silly now – have I done something to upset you? The op didn’t want all this library evangelism he just wanted an answer to his question. Unfortunately he hasn’t got his wish. My solution is perfectly adequate the string size is constant and the buffer size known. Your solution won’t work for him because he’s not using your library. I’ll say no more and leave it to you to help him. |
Steve Fryatt (216) 2105 posts |
Apologies for not realising that every thread on this forum was about you, Colin. I’ll be more careful to stroke your ego the right way in future.
Sort of. In the exact example, yes (as I did, in fact, note). However, I was under the impression that this was about helping the OP to write proper software, and in any real app the sprite name will almost certainly be used in more than just this one location. As such, unless you’re advocating duplicating string constants around the source code, the literal constant will eventually be replaced by either a #defined constant or a pointer to a string. You’ve then lost the visual link between constant and buffer, and the bounds check becomes useful. It’s also, as I also noted, better practice in the general case to bounds check when writing stuff into icon blocks. Again, I thought we were trying to impart more general advice which might be useful.
strncpy() is part of the standard library, at least if you’re compiling to a recent1 C standard; ditto snprintf(). Encouraging their (correct2) use in modern software is sensible. There’s usually no place for sprintf() and strcpy() in a modern desktop application. I noted that the OP would need to supply their own constant giving the size of the buffer; that’s where using a library helps, as magic numbers are never good.
You’re welcome. 1 In RISC OS terms. 2 As noted, this is a rare case where ensuring there’s a terminator isn’t required. |
Rick Murray (539) 13840 posts |
I didn’t reply to this yesterday as I thought it somewhat rude, so I went to bed instead. Today? Today I will calmly ask a question: It is common with BASIC to roll your own Wimp routines. You practically never have to do this in C with the libraries that exist. As such, when you are writing programs you can concentrate on your program and not worry about the intricacies of how the wimp does what it does. If the original poster wants to perform low level wimp stuff by hand; yes, we can all help. However if he is looking at a book about BASIC to learn how to write programs in C, it’s a bit like learning about combustion in order to learn how to drive… |
David Pitt (102) 743 posts |
That’s just you being condescending and rude. |
Rick Murray (539) 13840 posts |
You would rather somebody define their own structures and call SWIs in preference to a library where all this has already been done and provided? The point about the low level stuff was not intended to be condescending. It depends upon why he is doing it this way – if to learn how the Wimp works, then that makes sense. I probably could have worded it better. If to actually write programs, then…it seems rather like reinventing the wheel… |
Chris Mahoney (1684) 2165 posts |
I agree. I didn’t learn any of this stuff (other than a quick glance) because the Toolbox handles it all for me. It’s much easier to call toolbox_initialise and let it handle my icon bar icon, its menu, the opening of the window, etc. Of course, as you point out, if the intention is to learn how it works at a low level then that’s fine. I found that perfectly clear in your earlier post and didn’t find it condescending or rude at all. It can be difficult to tell intent over text though. |