Skip to content

Commit 1663721

Browse files
committed
refs#147: add MenuItem.context for free use
1 parent bc15200 commit 1663721

File tree

6 files changed

+53
-17
lines changed

6 files changed

+53
-17
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Revision history for Perl module Prima
33
1.77 2025-XX-XX
44
- Hint colors can be set for each widget individually
55
- Add JXL support
6+
- Add MenuItem.context for generic use
67

78
1.76 2025-03-12
89
- Implement gt::Grid geometry emulating Tk::grid

class/AbstractMenu.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,21 @@ AbstractMenu_checked( Handle self, Bool set, char * varName, Bool checked)
979979
}
980980
return checked;
981981
}
982+
SV*
983+
AbstractMenu_context( Handle self, Bool set, char * varName, SV* context)
984+
{
985+
PMenuItemReg m;
986+
if ( var-> stage > csFrozen) return 0;
987+
m = find_menuitem( self, varName, true);
988+
if ( m == NULL) return 0;
989+
if ( !set) return m-> context ? newSVsv(m->context) : NULL_SV;
990+
m-> context = (context && SvOK(context)) ? newSVsv(context) : NULL;
991+
notify( self, "<ssUS", "Change", "context",
992+
m->variable ? m-> variable : varName,
993+
m->variable ? m-> flags.utf8_variable : 0,
994+
m->context ? m->context : NULL_SV);
995+
return m->context;
996+
}
982997

983998
int
984999
AbstractMenu_group( Handle self, Bool set, char * varName, int group)

class/AbstractMenu.cls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ object Prima::AbstractMenu ( Prima::Component)
66
property SV * action ( char * varName);
77
property Bool autoToggle( char * varName);
88
property Bool checked( char * varName);
9+
property SV * context( char * varName);
910
property Handle icon ( char * varName);
1011
property int group ( char * varName);
1112
property SV * hint ( char * varName);

include/apricot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define POLLUTE_NAME_SPACE 1
77
#endif
88

9-
#define PRIMA_CORE_VERSION 2025031501
9+
#define PRIMA_CORE_VERSION 2025051901
1010

1111
#define PRIMA_VERSION_BOOTCHECK \
1212
if(apc_get_core_version()!=PRIMA_CORE_VERSION) \
@@ -2876,6 +2876,7 @@ typedef struct _MenuItemReg { /* Menu item registration record */
28762876
SV * hint; /* hint if not NULL */
28772877
Handle icon; /* custom checked bitmap */
28782878
int group; /* radio group */
2879+
SV * context; /* custom scalar */
28792880
SV * onPaint; /* callbacks */
28802881
SV * onMeasure;
28812882
struct _MenuItemReg* down; /* pointer to submenu */

pod/Prima/Menu.pod

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ The toolkit reserves the following keys for internal use:
235235

236236
=over
237237

238+
=item context SCALAR
239+
240+
A single scalar for free generic use, f ex like this:
241+
242+
[
243+
menu
244+
...
245+
sub {
246+
my ( $self, $item ) = @_;
247+
my $my_scalar = $self->menu->context($item);
248+
}
249+
...
250+
{ context => $my_scalar },
251+
]
252+
238253
=item group INTEGER
239254

240255
Same as the C<group> property.
@@ -312,27 +327,27 @@ combination is treated differently:
312327

313328
=over
314329

315-
=item six - [ NAME, TEXT/IMAGE, ACCEL, KEY, ACTION/SUBMENU, DATA ]
330+
=item six - [ NAME, TEXT/IMAGE, ACCEL, KEY, ACTION/SUBMENU, OPTIONS ]
316331

317332
A six-scalar array is a fully qualified text-item description.
318333
All fields correspond to the described above scalars.
319334

320335
=item five [ NAME, TEXT/IMAGE, ACCEL, KEY, ACTION/SUBMENU ]
321336

322-
Same as the six-scalar syntax, but without the DATA field.
323-
If DATA is skipped then it is set to C<undef>.
337+
Same as the six-scalar syntax, but without the OPTIONS field.
338+
If OPTIONS is skipped then it is set to C<undef>.
324339

325-
=item four [ TEXT/IMAGE, ACCEL, KEY, ACTION/SUBMENU ] or [ NAME, TEXT/IMAGE, ACTION/SUBMENU, DATA ]
340+
=item four [ TEXT/IMAGE, ACCEL, KEY, ACTION/SUBMENU ] or [ NAME, TEXT/IMAGE, ACTION/SUBMENU, OPTIONS ]
326341

327342
One of the two definitions, depending on whether the last item is a hashref or not.
328343

329344
If the last item is not a hashref, then treated the same as the five-scalar
330345
syntax, but without the NAME field. When NAME is skipped it is assigned to a
331346
unique string within the menu object.
332347

333-
Otherwise same as the three-scalar syntax plus the DATA hashref.
348+
Otherwise same as the three-scalar syntax plus the OPTIONS hashref.
334349

335-
=item three [ NAME, TEXT/IMAGE, ACTION/SUBMENU ] or [ TEXT/IMAGE, ACTION/SUBMENU, DATA ]
350+
=item three [ NAME, TEXT/IMAGE, ACTION/SUBMENU ] or [ TEXT/IMAGE, ACTION/SUBMENU, OPTIONS ]
336351

337352
One of the two definitions, depending on whether the last item is a hashref or not.
338353

@@ -341,16 +356,16 @@ syntax, but without the ACCEL and the KEY fields. KEY is C<kb::NoKey> by
341356
default, so no keyboard combination is bound to the item. The default ACCEL value
342357
is an empty string.
343358

344-
Otherwise the same as the two-scalar syntax plus DATA hashref.
359+
Otherwise the same as the two-scalar syntax plus OPTIONS hashref.
345360

346-
=item two [ TEXT/IMAGE, ACTION/SUBMENU ] or [ NAME, DATA ]
361+
=item two [ TEXT/IMAGE, ACTION/SUBMENU ] or [ NAME, OPTIONS ]
347362

348363
One of the two definitions, depending on whether the last item is a hashref or not.
349364

350365
If the last item is not a hashref, then treated the same as the three-scalar
351366
syntax, but without the NAME field.
352367

353-
Otherwise treated as the menu items with the data reference. Useful for custom
368+
Otherwise treated as the menu items with the options reference. Useful for custom
354369
menu items that need at least the C<'?'> flag in the NAME.
355370

356371
=item one and zero [ NAME ]
@@ -399,7 +414,7 @@ reader is advised to run the example and learn the menu mechanics.
399414

400415
As briefly mentioned above, all menu items can be accessed using the following
401416
properties: C<::accel>, C<::text>, C<::image>, C<::checked>, C<::enabled>,
402-
C<::action>, C<::data>. These, plus some other methods can be also called in an
417+
C<::action>, C<::options>. These, plus some other methods can be also called in an
403418
alternative way, resembling name-based component calls of L<Prima::Object>. For
404419
example, the call
405420

@@ -516,9 +531,9 @@ usage with the sub-menu items is possible, although discouraged.
516531

517532
NAME is the name of the menu item.
518533

519-
=item data NAME, HASH / Prima::MenuItem::data HASH
534+
=item context NAME, SCALAR / Prima::MenuItem::context SCALAR
520535

521-
Manages the user data hash. NAME is the name of the menu item.
536+
Manages the user context scalar. NAME is the name of the menu item.
522537

523538
=item enabled NAME, BOOLEAN / Prima::MenuItem::enabled BOOLEAN
524539

@@ -557,7 +572,7 @@ structure, with the syntax described in L<Menu items>.
557572
The C<::items> property is an ultimate tool for reading and writing the menu
558573
items tree, but often it is too powerful, so there exist several easier-to-use
559574
properties C<::accel>, C<::text>, C<::image>, C<::checked>, C<::enabled>,
560-
C<::action>, C<::data>, that can access menu items individually.
575+
C<::action>, C<::options> that can access menu items individually.
561576

562577
=item key NAME, KEY / Prima::MenuItem::key KEY
563578

@@ -568,6 +583,10 @@ the literal key format - strings such as '^C' or 'F5'.
568583

569584
NAME is the name of the menu item; KEY is an integer value.
570585

586+
=item options NAME, HASH / Prima::MenuItem::options HASH
587+
588+
Manages the options hash. NAME is the name of the menu item.
589+
571590
=item selected BOOLEAN
572591

573592
If set to 1, the menu object is granted extra functionality from a window or

pod/Prima/tutorial.pod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ three to six items in the array. One can set the left-aligned text string for
318318
the item, the right-aligned text string for the display of the hotkey, if any,
319319
the definition of the hotkey itself, and the action to be taken if the user has
320320
pressed either the menu item or the hotkey combination. Also, as in the
321-
previous cases, an explicit menu item ID can be set, and also an arbitrary data scalar,
322-
for the generic needs of the programmer.
321+
previous cases, an explicit menu item ID can be set, along with extra options
323322

324323
Here are the combinations of scalars in an array that are allowed for defining
325324
a text menu item:
@@ -330,7 +329,7 @@ Four items - [ text, hotkey text, hotkey, action ]
330329

331330
Five items - [ ID, text, hotkey text, hotkey, action ]
332331

333-
Six items - [ ID, text, hotkey text, hotkey, action, data ]
332+
Six items - [ ID, text, hotkey text, hotkey, action, options ]
334333

335334
The image menu items are fully analogous to the text items, except that instead
336335
of the text string, an image object is supplied:

0 commit comments

Comments
 (0)