Programmatically creating a ListBox (and a ListDefSpec)

Programmatically creating a ListBox (and a ListDefSpec)

Post by work » Sat, 31 Jul 2004 23:18:27

Sorry for the double post. Realized I should probably start a new
thread for this question.

I am trying to (programmatically) put a listbox control in my
application, but am finding very poor documentation and virtually no
code examples of how to use CreateListBoxControl in conjunction with
creating my own ListDefSpec. I have read the apple documentation, but
cannot find any sound examples of implementation. Has anyone
implemented their own ListDefSpec and used it to initialize
CreateListBoxControl? If so, do you have some code snippets I can see?

Programmatically creating a ListBox (and a ListDefSpec)

Post by work » Sun, 01 Aug 2004 06:02:37

Well, I seem to be managing all right with creating the ListBox now.
I'm actually having more trouble with the Popup Buttons. They aren't
crashing the program, but neither are they visible. I populate the
menu using:

osError = CreateNewMenu(myMenuId, 0, myMenuRef);

if(osError == noErr)
InsertMenuItem((*myMenuRef), "\pDefault Project", (short)1);
InsertMenuItem((*myMenuRef), "\pNon Default Project", 2);
InsertMenuItem((*myMenuRef), "\pHot Chics Project", 3);

osError = CreatePopupButtonControl(theWindow,
CFSTR("Project:"), myMenuId, true, 76, popupTitleLeftJust,
popupTitleNoStyle, &popupButtonRef);
if(osError == noErr)

but so far no visible button. Going to start debugging and checking my
error codes now. If I get anything meaningful I will post it.

Meanwhile, any examples dealing with this or with CreateListBoxControl
are appreciated and welcome.

Hanley Loller


Programmatically creating a ListBox (and a ListDefSpec)

Post by Thomas Eng » Sun, 01 Aug 2004 19:52:21

Read slowly:

MacOS toolbox references are passed by value, except to functions that
return a reference.

I would suggest you switch on the C++ compiler as it has far stricter
type checking.

Being ignorant to return codes as newbie programmer is about the worst
you can do. Exactly for that reason beginner-friendly languages like
Java disallow it.


This address is valid in its unmodified form but expires soon.

Programmatically creating a ListBox (and a ListDefSpec)

Post by work » Wed, 04 Aug 2004 01:47:07

homas Engelmeier < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...

Insults are unnecessary. I am a professional programmer working in
C++, not C. I am new to the Mac, but not to C-based languages or
reference errors. The above code conforms to the models of working
code that I have scavenged to assist me in rapidly learning the
workings of a new platform.

From Apple:
PROCEDURE InsertMenuItem (theMenu: MenuHandle; itemString: Str255;
afterItem: Integer);

myMenuRef is a MenuHandle pointer, and it is properly dereferenced.
Therefore, the handle (which is a pointer to a pointer) should be
passed by value (i.e. the value of the masterpointer that points to
the pointer that points to the previously created Menu object.)

As stated above, I am working in C++. For compatability with third
party projects, I am limited to using the CodeWarrior IDE and the
Carbon environment, which necessitates the use of quite a few C calls
(unless I have misread the documentation I have thus far been using).
I am also passing objects and values back and forth between our
existing API (consisting of over a half-million lines of code and
somewhere around 100 classes) and the test project that instantiates
the API, and that will embed controls provided by it. In some
instances this will force me to use dynamically created objects
referenced by pointers rather than automatically instantiated objects
of limited scope, even if the function I am currently working with
requires a pass-by-value parameter.

Dereferencing a pointer to an object and passing it is no different
than passing by value, unless CodeWarrior has developed a strange new
paradigm that I am unaware of.

If I am passing a pointer as a value in the code snippet above when I
should not be, please point out my error rather than simply insulting
me. Again, I am following the documentation as closely as I can, but I
am discovering that many of the functions that I am using have
dependancies that are poorly documented. For instance, I have found
that many of my recent problems (including the one above) stem from
the lack of the CarbonFrameworkLib in my project. I created my sample
project from CodeWarrior's Mac OS Stationary C++ Carbon Toolbox Nib
Bundle, which does not include this library.

C++ disallows it too. As I stated before, this code returns no
compiler errors. Prehaps you did not notice the asterisk.


The pointer is dereferenced before it is passed, therefore the
myMenuRef variable it points to is passed by value and not by
reference. Again, If this is an overloaded function and I need to use
a different form of it, tell me so. Don't just assume that I have
passed it the wrong argument.

If you have any constructive criticism for me, I am all eyes and ears.
I have no problem confessing my ignorance on a new platform, but I
dislike being scolded like a child by someone who obviously didn't
read my post very carefully.

Hanley Loller


Programmatically creating a ListBox (and a ListDefSpec)

Post by work » Wed, 04 Aug 2004 02:56:24

From Apple:
OSStatus CreateNewMenu (
MenuID inMenuID,
MenuAttributes inMenuAttributes,
MenuRef * outMenuRef

Parameter Descriptions

The menu ID to use for the new menu. Note that zero is a valid ID in

The menu attributes to use for the new menu. See "Menu Item Attribute
Constants" for a list of possible values.

On exit, contains the new menu.

In other words, this function is returning a reference.

Programmatically creating a ListBox (and a ListDefSpec)

Post by Thomas Eng » Wed, 04 Aug 2004 06:49:37

In article < XXXX@XXXXX.COM >,

Which is why I wrote "read slowly" (and maybe again). Nothing in you
snippet contradicts the line from me above.
The code snippet in your original posting, OTOH, indicates some bugs and
confusion in that area.

This address is valid in its unmodified form but expires soon.

Programmatically creating a ListBox (and a ListDefSpec)

Post by Thomas Eng » Wed, 04 Aug 2004 07:57:28

n article < XXXX@XXXXX.COM >,
XXXX@XXXXX.COM (Hanley) wrote:

If you feel insulted, bad luck. If you post kind of trivial errors and
feel insulted by an correcting suggestion, [insert whatever here]. If
you feel unrelated subject lines, MacOS beginner style coding
conventions and an pseudonymous name indicate professionalism and you
have to be treated with due respect - *shrug* YMMV.

Ambiguous syntax. Do not expect that your mix of three distinct memory
allocation concepts (opaque ref[erence], pointer, Handle) is understood.

Then it is an misnomer. A Handle is distinct from a pointer, _similar_
to an pointer to a pointer. It is, however, not only an pointer to an
pointer. GetHandleSize et al. can only be used on something that was
(internally) allocated by NewHandle etc...
In Carbon many Handles were replaced with _opaque_ xxxxRef and accessor
functions. IOW: xxxxRef usually means it can or should NOT be

xxxxHandle != &xxxxPtr [and theoretically] != xxxxRef

There is a difference between calling C functions from an C compiler and
calling the same function with an C++ compiler. The C++ compiler handles
typechecking far stricter.

Your naming conventions are kind of out of sync with the intended code -




to an function that expects for sure a MenuRef is a recipe for disaster.
Either code wise or coding style wise.

Either you want to learn the concepts, or not - and there you might get
some advise. If you want superpatient consultancy, it is offered as an
paid service.

Those depencencies are called concepts. But - as you feel insulted when
you are directed to the underlying, misconveived concept - bad luck.

likely - as that stationary produces PEF binaries it uses CarbonLib and
not Mach-O only frameworks.

It is unlikely you manage to execute code that produces compiler

I noticed exactly the asterix. I am not aware of any MacOS Toolbox
function that takes an _dereferenced MenuRef_.

C APIs do not have the concept of overloading functions as a C linker
does not know the concept of name mangling.

Goodbye and thanks for all the fish..

This address is valid in its unmodified form but expires soon.


Programmatically creating a ListBox (and a ListDefSpec)

Post by work » Thu, 05 Aug 2004 00:12:29

>If you feel unrelated subject lines, MacOS beginner style coding

Hanley Loller is my real name.

Okay, I'm going to go out on a limb here and apologise. I don't want
to start a flame war with anyone here and I am genuinely looking for
help. The snippet I posted is under-detailed and simplistic, and I am
not the uber-programmer. I am a professional and I do know my way
around a C++ compiler, but I did not grow up using computers so my
knowledge can sometimes be lopsided. I began studying computer science
at the age of 30 at East Tennessee State University, C++ and object
oriented design. I graduated with my BS at 35, spent two years working
for a company that mostly had me doing odd database and ColdFusion
chores, then got my current job which I love. I am sharp enough to
pick up new technologies quickly, but I can also easily miss that
which may be overly obvious to someone who has been using a mouse
since nursery school.

Mostly I work in the Windows world using Visual Studio and I have some
experience compiling on IRIX with makefiles, but I don't want to get
into any ego-fueled debates about the superiority of one OS or set of
tools over another. I simply want to learn my way around the Mac using
Codewarrior and Carbon. We are not a Mac shop, so we don't have a pool
of Mac/Codewarrior expertise in-house. I am the only person working on
this project.

I am using Codewarrior 9 on OS X 10.3. Final implementation will not
be required to run on anything less than OS X 10.1

I am successfully creating a wide range of programmatic controls now,
but I am still having trouble with the popup buttons. The error code
that CreatePopupButtonControl returns is -192 resNotFound, which I am
assuming is referring to the menu resource. The MenuRef appears to be
properly initialized and CreateNewMenu returns an error code of 0
noErr. I have also considered that I need to somehow initialize the
ControlRef that I am passing to CreatePopupButtonControl, but I am
using static declaration and that seems to be all the initialization
that the other CreateXXXControl functions need. I am as certain as it
is possible to be that there is nothing wrong with the other
parameters, and my implementation closely parallels that found in
Chapter 17 of Bricknell's "Carbon Programming", Demonstration Program
CarbonEvents1 (which is a very simple implementation). The main
difference is that Bricknell uses a menu created with Resourcerer and
I am creating the menu programmatically. Any suggestions?

FYI: I am not expecting a miraculously accurate answer. I am used to
slogging through new technology and deciphering it as I go, but any
suggestions that can point me in a more productive direction are
greatly appreciated. Also, any miraculously accurate responses are, of
course, welcome.

Hanley Loller