File: cocoatoolbar.inc

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (851 lines) | stat: -rw-r--r-- 25,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
{%MainUnit cocoatoolbar.pas}

{$mode objfpc}{$H+}
{$modeswitch objectivec2}
{$interfaces corba}

interface

uses
  Classes, SysUtils,
  Forms, Menus,
  CocoaAll, CocoaPrivate, CocoaConfig, CocoaWindows, CocoaMenus,
  Cocoa_Extra, CocoaUtils;

type
  PCocoaConfigToolBar = ^TCocoaConfigToolBar;

  PCocoaConfigToolBarItemBase = ^TCocoaConfigToolBarItemBase;
  PCocoaConfigToolBarItemWithUI = ^TCocoaConfigToolBarItemWithUI;
  PCocoaConfigToolBarItemWithAction = ^TCocoaConfigToolBarItemWithAction;
  PCocoaConfigToolBarItem = ^TCocoaConfigToolBarItem;

  { TCocoaConfigToolBarItemClassBase }

  // NSToolbarDelegateProtocol.toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar()
  // requires NSToolBarItem to be created each time it is called,
  // corresponding to TCocoaConfigToolBarItemClassAbstract.createItem().
  //
  // so TCocoaConfigToolBarItemClassAbstract and its subclass should NOT
  // reference the created NSToolBarItem. that is, there should be no
  // other fields in it except the Config Data itself.
  //
  // use TCocoaToolBarUtils.findItemByIdentifier() to get the NSToolBarItem instance.
  TCocoaConfigToolBarItemClassBase = class( TCocoaConfigToolBarItemClassAbstract )
  protected
    _identifier: String;
    _priority: NSInteger;
    _navigational: Boolean;
  protected
    procedure toClassConfig( const pItemConfig: PCocoaConfigToolBarItemBase );
  public
    procedure setItemAttribs( const item: NSToolBarItem ); virtual;
    function identifier: NSString; override;
  end;

  { TCocoaConfigToolBarItemClassWithUI }

  TCocoaConfigToolBarItemClassWithUI = class( TCocoaConfigToolBarItemClassBase )
  protected
    _iconName: String;
    _title: String;
    _tips: String;
    _bordered: Boolean;
  protected
    procedure toClassConfig( const pItemConfig: PCocoaConfigToolBarItemWithUI );
  public
    procedure setItemAttribs( const item: NSToolBarItem ); override;
    function iconName: NSString; virtual;
    function title: NSString; virtual;
    function tips: NSString; virtual;
  end;

  { TCocoaConfigToolBarItemClassWithAction }

  TCocoaConfigToolBarItemClassWithAction = class( TCocoaConfigToolBarItemClassWithUI )
  protected
    _onAction: TCocoaToolBarItemActionHandler;
  protected
    procedure toClassConfig( const pItemConfig: PCocoaConfigToolBarItemWithAction );
  public
    procedure setItemAttribs( const item: NSToolBarItem ); override;
  end;

  { TCocoaConfigToolBarItemClass }

  TCocoaConfigToolBarItemClass = class( TCocoaConfigToolBarItemClassWithAction )
    constructor Create( const itemConfig: TCocoaConfigToolBarItem );
    function createItem: NSToolBarItem; override;
  end;

  { TCocoaConfigToolBarItemClassSharing }

  TCocoaConfigToolBarItemClassSharing = class( TCocoaConfigToolBarItemClassWithUI )
  protected
    _onGetItems: TCocoaToolBarItemSharingOnGetItems;
  public
    constructor Create( const itemConfig: TCocoaConfigToolBarItemSharing );
    function createItem: NSToolBarItem; override;
  end;

  { TCocoaConfigToolBarItemClassSearch }

  TCocoaConfigToolBarItemClassSearch = class( TCocoaConfigToolBarItemClassWithAction )
  protected
    _sendWhole: Boolean;
    _sendImmediately: Boolean;
    _resignsWithCancel: Boolean;
    _preferredWidth: Double;
  public
    constructor Create( const itemConfig: TCocoaConfigToolBarItemSearch );
    function createItem: NSToolBarItem; override;
  end;

  { TCocoaConfigToolBarItemClassMenu }

  TCocoaConfigToolBarItemClassMenu = class( TCocoaConfigToolBarItemClassWithAction )
  protected
    _showsIndicator: Boolean;
    _dynamic: Boolean;
    _menu: TMenu;
    _onGetMenu: TCocoaToolBarItemMenuOnGetMenu;
  public
    constructor Create( const itemConfig: TCocoaConfigToolBarItemMenu );
    function createItem: NSToolBarItem; override;
  end;

  { TCocoaConfigToolBarItemClassGroup }

  TCocoaConfigToolBarItemClassGroup = class( TCocoaConfigToolBarItemClassWithAction )
  protected
    _representation: NSToolbarItemGroupControlRepresentation;
    _selectionMode: NSToolbarItemGroupSelectionMode;
    _selectedIndex: NSInteger;
    _subitems: TCocoaConfigToolBarItems;
  public
    constructor Create( const itemConfig: TCocoaConfigToolBarItemGroup );
    function createItem: NSToolBarItem; override;
  end;

  { TCocoaToolBarItem }

  TCocoaToolBarItem = objcclass( NSToolBarItem )
  private
    _handler: TCocoaToolBarItemActionHandler;
    procedure lclItemAction( const sender: id ); message 'lclItemAction:';
  public
    procedure lclSetHandler( const handler: TCocoaToolBarItemActionHandler );
      message 'lclSetHandler:';
  end;

  { TCocoaToolBarItemSharing }

  TCocoaToolBarItemSharingDelegate = objcclass( NSObject, NSSharingServicePickerToolbarItemDelegateProtocol )
  private
    _onGetItems: TCocoaToolBarItemSharingOnGetItems;
  public
    function itemsForSharingServicePickerToolbarItem(
      pickerToolbarItem: NSSharingServicePickerToolbarItem ): NSArray;
  public
    procedure lclSetOnGetItems( const onGetItems: TCocoaToolBarItemSharingOnGetItems );
      message 'lclSetOnGetItems:';
  end;

  TCocoaToolBarItemSharing  = objcclass( NSSharingServicePickerToolbarItem )
  public
    procedure dealloc; override;
  end;

  { TCocoaToolBarItemSearch }

  TCocoaToolBarItemSearch = objcclass( NSSearchToolBarItem, NSSearchFieldDelegateProtocol )
  private
    _isSearching: Boolean;
    _handler: TCocoaToolBarItemActionHandler;
    procedure lclItemAction( sender: id ); message 'lclItemAction:';
  public
    procedure searchFieldDidStartSearching( sender: NSSearchField );
    procedure searchFieldDidEndSearching( sender: NSSearchField );
  public
    procedure lclSetHandler( const handler: TCocoaToolBarItemActionHandler );
      message 'lclSetHandler:';
  end;

  { TCocoaToolBarItemMenu }

  TCocoaToolBarItemMenu = objcclass( NSMenuToolBarItem )
  private
    _handler: TCocoaToolBarItemActionHandler;
    _dynamic: Boolean;
    _onGetMenu: TCocoaToolBarItemMenuOnGetMenu;
    procedure lclItemAction( sender: id ); message 'lclItemAction:';
  public
    procedure lclSetHandler( const handler: TCocoaToolBarItemActionHandler );
      message 'lclSetHandler:';
    procedure lclSetDynamic( const dynamic: Boolean ); message 'lclSetDynamic:';
    procedure lclSetOnGetMenu( const onGetMenu: TCocoaToolBarItemMenuOnGetMenu);
      message 'lclSetOnGetMenu:';
  end;

  { TCocoaToolBarItemGroupWrapper }

  TCocoaToolBarItemGroupWrapper = objcclass( NSObject )
  private
    _itemGroup: NSToolbarItemGroup;
    _handler: TCocoaToolBarItemActionHandler;
    procedure lclItemAction( const sender: id ); message 'lclItemAction:';
    function lclGetSelectedItem: NSToolBarItem; message 'lclGetSelectedItem';
  public
    procedure lclSetHandler( const handler: TCocoaToolBarItemActionHandler );
      message 'lclSetHandler:';
    procedure lclSetItemGroup( const itemGroup: NSToolbarItemGroup );
      message 'lclSetItemGroup:';
    procedure lclSetSelectedIndex( const index: NSInteger );
      message 'lclSetSelectedIndex:';
  end;

  { TCocoaToolBar }

  TCocoaToolBar = objcclass( NSToolBar, NSToolbarDelegateProtocol )
  private
    _itemCreator: TCocoaToolBarItemCreator;
    _defaultItemIdentifiers: NSArray;
    _allowedItemIdentifiers: NSArray;
    _pToolBarItems: Pointer;
  public
    function initWithIdentifier( aIdentifier: NSString ): id; override;
    procedure dealloc; override;
  public
    // NSToolbarDelegate
    function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(
      toolbar: NSToolbar; itemIdentifier: NSString; flag: ObjCBOOL ): NSToolbarItem;
    function toolbarDefaultItemIdentifiers( toolbar: NSToolbar ): NSArray;
    function toolbarAllowedItemIdentifiers( toolbar: NSToolbar ): NSArray;
  public
    procedure lclSetConfig( const pConfig: Pointer );
      message 'lclSetConfig:';
    procedure lclSetItemCreator( const itemCreator: TCocoaToolBarItemCreator );
      message 'lclSetItemCreator:';
    procedure lclSetDefaultItemIdentifiers( identifiers: NSArray );
      message 'lclSetDefaultItemIdentifiers:';
    procedure lclSetAllowedItemIdentifiers( identifiers: NSArray );
      message 'lclSetAllowedItemIdentifiers:';
  end;

  { TCocoaToolBarUtils }

  TCocoaToolBarUtils = class
  public
    class function toClass( const itemConfig: TCocoaConfigToolBarItem ):
      TCocoaConfigToolBarItemClassAbstract;
    class function toClass( const itemConfig: TCocoaConfigToolBarItemSharing ):
      TCocoaConfigToolBarItemClassAbstract;
    class function toClass( const itemConfig: TCocoaConfigToolBarItemSearch ):
      TCocoaConfigToolBarItemClassAbstract;
    class function toClass( const itemConfig: TCocoaConfigToolBarItemMenu ):
      TCocoaConfigToolBarItemClassAbstract;
    class function toClass( const itemConfig: TCocoaConfigToolBarItemGroup ):
      TCocoaConfigToolBarItemClassAbstract;
  public
    class function createItem( const identifier: String;
      const itemsConfig: TCocoaConfigToolBarItems ): NSToolbarItem;
    class function createToolBar( const toolBarConfig: TCocoaConfigToolBar ):
      TCocoaToolBar;
    class function findItemByIdentifier( const form: TCustomForm;
      const identifier: String ): NSToolbarItem;
    class function getNSView( const cocoaItem: NSToolBarItem ): NSView;
  end;

  function defaultToolBarItemCreatorImplement( const identifier: String;
    const items: TCocoaConfigToolBarItems ): NSToolbarItem;

implementation

class function TCocoaToolBarUtils.toClass( const itemConfig: TCocoaConfigToolBarItem
  ): TCocoaConfigToolBarItemClassAbstract;
begin
  Result:= TCocoaConfigToolBarItemClass.Create( itemConfig );
end;

class function TCocoaToolBarUtils.toClass( const itemConfig: TCocoaConfigToolBarItemSharing
  ): TCocoaConfigToolBarItemClassAbstract;
begin
  Result:= TCocoaConfigToolBarItemClassSharing.Create( itemConfig );
end;

class function TCocoaToolBarUtils.toClass( const itemConfig: TCocoaConfigToolBarItemSearch
  ): TCocoaConfigToolBarItemClassAbstract;
begin
  Result:= TCocoaConfigToolBarItemClassSearch.Create( itemConfig );
end;

class function TCocoaToolBarUtils.toClass( const itemConfig: TCocoaConfigToolBarItemMenu
  ): TCocoaConfigToolBarItemClassAbstract;
begin
  Result:= TCocoaConfigToolBarItemClassMenu.Create( itemConfig );
end;

class function TCocoaToolBarUtils.toClass( const itemConfig: TCocoaConfigToolBarItemGroup
  ): TCocoaConfigToolBarItemClassAbstract;
begin
  Result:= TCocoaConfigToolBarItemClassGroup.Create( itemConfig );
end;

class function TCocoaToolBarUtils.createItem( const identifier: String;
  const itemsConfig: TCocoaConfigToolBarItems ): NSToolbarItem;
var
  i: Integer;
  count: Integer;
  identifierNSString: NSString;
begin
  Result:= nil;
  count:= length( itemsConfig );
  identifierNSString:= StrToNSString( identifier );
  for i:=0 to count-1 do begin
    if identifierNSString.isEqualToString(itemsConfig[i].identifier) then begin
      Result:= itemsConfig[i].createItem;
      Exit;
    end;
  end;
end;

class function TCocoaToolBarUtils.createToolBar(
  const toolBarConfig: TCocoaConfigToolBar ): TCocoaToolBar;
var
  toolBar: TCocoaToolBar;
  defaultArray: NSArray;
  allowedArray: NSArray;
begin
  toolBar:= TCocoaToolBar.alloc.initWithIdentifier(
    StrToNSString(toolBarConfig.identifier) );
  toolBar.lclSetConfig( @toolBarConfig );

  defaultArray:= StringArrayFromLCLToNS( toolBarConfig.defaultItemsIdentifiers );
  allowedArray:= StringArrayFromLCLToNS( toolBarConfig.allowedItemsIdentifiers );

  toolBar.setDisplayMode( toolBarConfig.displayMode );
  toolBar.setAllowsUserCustomization( toolBarConfig.allowsUserCustomization );
  toolBar.setAutosavesConfiguration( toolBarConfig.autosavesConfiguration );
  toolBar.lclSetDefaultItemIdentifiers( defaultArray );
  toolBar.lclSetAllowedItemIdentifiers( allowedArray );

  if Assigned(toolBarConfig.itemCreator) then
    toolBar.lclSetItemCreator( TCocoaToolBarItemCreator(toolBarConfig.itemCreator) );

  Result:= toolBar;
end;

class function TCocoaToolBarUtils.findItemByIdentifier( const form: TCustomForm;
  const identifier: String ): NSToolbarItem;
var
  content: TCocoaWindowContent;
  win: NSWindow;
  toolBar: NSToolBar;
  item: NSToolBarItem;
  cocoaIdentifier: NSString;
begin
  Result:= nil;
  content:= TCocoaWindowContent( form.handle );
  if NOT Assigned(content) then
    Exit;
  if content.isembedded then
    Exit;
  win:= content.window;
  if NOT Assigned(win) then
    Exit;
  toolBar:= win.toolbar;
  if NOT Assigned(toolBar) then
    Exit;

  cocoaIdentifier:= StrToNSString( identifier );
  for item in toolBar.items do begin
    if item.itemIdentifier.isEqualToString(cocoaIdentifier) then begin
      Result:= item;
      Exit;
    end;
  end;
end;

class function TCocoaToolBarUtils.getNSView(const cocoaItem: NSToolBarItem
  ): NSView;
begin
  Result:= cocoaItem.valueForKey( NSSTR('_itemViewer') );
end;

function defaultToolBarItemCreatorImplement( const identifier: String;
  const items: TCocoaConfigToolBarItems ): NSToolbarItem;
begin
  Result:= TCocoaToolBarUtils.createItem( identifier, items );
end;

{ TCocoaToolBarItem }

procedure TCocoaToolBarItem.lclItemAction( const sender: id );
begin
  _handler( sender );
end;

procedure TCocoaToolBarItem.lclSetHandler( const handler: TCocoaToolBarItemActionHandler );
begin
  _handler:= handler;
end;

{ TCocoaToolBarItemSharing }

function TCocoaToolBarItemSharingDelegate.itemsForSharingServicePickerToolbarItem(
  pickerToolbarItem: NSSharingServicePickerToolbarItem): NSArray;
var
  lclArray: TStringArray;
begin
  lclArray:= _onGetItems( pickerToolbarItem );
  Result:= UrlArrayFromLCLToNS( lclArray );
end;

procedure TCocoaToolBarItemSharingDelegate.lclSetOnGetItems(
  const onGetItems: TCocoaToolBarItemSharingOnGetItems );
begin
  _onGetItems:= onGetItems;
end;

procedure TCocoaToolBarItemSharing.dealloc;
begin
  NSObject(delegate).release;
  Inherited;
end;

{ TCocoaToolBarItemSearch }

procedure TCocoaToolBarItemSearch.lclItemAction(sender: id);
begin
  if _isSearching then
    _handler( sender );
end;

procedure TCocoaToolBarItemSearch.searchFieldDidStartSearching(
  sender: NSSearchField);
begin
  _isSearching:= True;
end;

procedure TCocoaToolBarItemSearch.searchFieldDidEndSearching(
  sender: NSSearchField);
begin
  _isSearching:= False;
end;

procedure TCocoaToolBarItemSearch.lclSetHandler(
  const handler: TCocoaToolBarItemActionHandler);
begin
  _handler:= handler;
end;

{ TCocoaToolBarItemMenu }

procedure TCocoaToolBarItemMenu.lclItemAction(sender: id);
  procedure popupDynamicMenu;
  var
    menu: TPopupMenu;
  begin
    menu:= TPopupMenu.Create( nil );
    _onGetMenu( menu );
    NSMenu(menu.handle).popUpMenuPositioningItem_atLocation_inView(
      nil, NSMakePoint(0,0), TCocoaToolBarUtils.getNSView(self) );
    menu.Free;
  end;

begin
  if _dynamic then
    popupDynamicMenu
  else
    _handler( sender );
end;

procedure TCocoaToolBarItemMenu.lclSetHandler( const handler: TCocoaToolBarItemActionHandler );
begin
  _handler:= handler;
end;

procedure TCocoaToolBarItemMenu.lclSetDynamic(const dynamic: Boolean);
begin
  _dynamic:= dynamic;
end;

procedure TCocoaToolBarItemMenu.lclSetOnGetMenu(
  const onGetMenu: TCocoaToolBarItemMenuOnGetMenu);
begin
  _onGetMenu:= onGetMenu;
end;

{ TCocoaToolBarItemGroupWrapper }

procedure TCocoaToolBarItemGroupWrapper.lclItemAction( const sender: id );
var
  index: NSInteger;
begin
  index:= _itemGroup.selectedIndex;
  self.lclSetSelectedIndex( index );
  _handler( _itemGroup );
end;

function TCocoaToolBarItemGroupWrapper.lclGetSelectedItem: NSToolBarItem;
var
  index: NSInteger;
begin
  Result:= nil;
  index:= _itemGroup.selectedIndex;
  if (index>=0) and (index<_itemGroup.subitems.count) then
    Result:= _itemGroup.subitems.objectAtIndex( index );
end;

procedure TCocoaToolBarItemGroupWrapper.lclSetHandler(
  const handler: TCocoaToolBarItemActionHandler );
begin
  _handler:= handler;
end;

procedure TCocoaToolBarItemGroupWrapper.lclSetItemGroup(
  const itemGroup: NSToolbarItemGroup);
begin
  _itemGroup:= itemGroup;
end;

procedure TCocoaToolBarItemGroupWrapper.lclSetSelectedIndex(
  const index: NSInteger);
var
  item: NSToolBarItem;
begin
  if (index>=0) and (index<_itemGroup.subitems.count) then begin
    _itemGroup.setSelected_atIndex( True, index );
    item:= self.lclGetSelectedItem;
    _itemGroup.setImage( item.image );
  end;
end;

{ TCocoaToolBar }

function TCocoaToolBar.initWithIdentifier( aIdentifier: NSString): id;
begin
  Result:= inherited;
  _itemCreator:= @defaultToolBarItemCreatorImplement;
  TCocoaToolBar(Result).setDelegate( Result );
end;

procedure TCocoaToolBar.dealloc;
begin
  if Assigned(_defaultItemIdentifiers) then
    _defaultItemIdentifiers.release;
  if Assigned(_allowedItemIdentifiers) then
    _allowedItemIdentifiers.release;
end;

function TCocoaToolBar.toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(
  toolbar: NSToolbar; itemIdentifier: NSString; flag: ObjCBOOL): NSToolbarItem;
begin
  Result:= _itemCreator( itemIdentifier.UTF8String, _pToolBarItems );
end;

function TCocoaToolBar.toolbarDefaultItemIdentifiers(toolbar: NSToolbar
  ): NSArray;
begin
  Result:= _defaultItemIdentifiers;
end;

function TCocoaToolBar.toolbarAllowedItemIdentifiers(toolbar: NSToolbar
  ): NSArray;
begin
  Result:= _allowedItemIdentifiers;
end;

procedure TCocoaToolBar.lclSetConfig( const pConfig: Pointer );
begin
  _pToolBarItems:= Pointer( PCocoaConfigToolBar(pConfig)^.Items );
end;

procedure TCocoaToolBar.lclSetItemCreator(
  const itemCreator: TCocoaToolBarItemCreator );
begin
  _itemCreator:= itemCreator;
end;

procedure TCocoaToolBar.lclSetDefaultItemIdentifiers(identifiers: NSArray);
begin
  if Assigned(_defaultItemIdentifiers) then
    _defaultItemIdentifiers.release;
  _defaultItemIdentifiers := identifiers;
  _defaultItemIdentifiers.retain;
end;

procedure TCocoaToolBar.lclSetAllowedItemIdentifiers(identifiers: NSArray);
begin
  if Assigned(_allowedItemIdentifiers) then
    _allowedItemIdentifiers.release;
  _allowedItemIdentifiers := identifiers;
  _allowedItemIdentifiers.retain;
end;

{ TCocoaConfigToolBarItemClassBase }

procedure TCocoaConfigToolBarItemClassBase.toClassConfig(
  const pItemConfig: PCocoaConfigToolBarItemBase);
begin
  _identifier:= pItemConfig^.identifier;
  _priority:= pItemConfig^.priority;
  _navigational:= pItemConfig^.navigational;
end;

procedure TCocoaConfigToolBarItemClassBase.setItemAttribs(
  const item: NSToolBarItem );
begin
  item.setVisibilityPriority( _priority );
  item.setNavigational( _navigational );
end;

function TCocoaConfigToolBarItemClassBase.identifier: NSString;
begin
  Result:= StrToNSString( _identifier );
end;

{ TCocoaConfigToolBarItemClassWithUI }

procedure TCocoaConfigToolBarItemClassWithUI.toClassConfig(
  const pItemConfig: PCocoaConfigToolBarItemWithUI );
begin
  Inherited toClassConfig( pItemConfig );
  _iconName:= pItemConfig^.iconName;
  _title:= pItemConfig^.title;
  _tips:= pItemConfig^.tips;
  _bordered:= pItemConfig^.bordered;
end;

procedure TCocoaConfigToolBarItemClassWithUI.setItemAttribs(
  const item: NSToolBarItem );
var
  cocoaImageName: NSString;
  cocoaLabel: NSString;
begin
  Inherited;

  cocoaImageName:= self.iconName;
  cocoaLabel:= self.title;

  if cocoaImageName.length > 0 then begin
    item.setImage( NSImage.imageWithSystemSymbolName_accessibilityDescription(
      cocoaImageName, nil ) );
  end;
  item.setLabel( cocoaLabel );
  item.setPaletteLabel( cocoaLabel );
  item.setToolTip( self.tips );
  item.setBordered( _bordered );
end;

function TCocoaConfigToolBarItemClassWithUI.iconName: NSString;
begin
  Result:= StrToNSString( _iconName );
end;

function TCocoaConfigToolBarItemClassWithUI.title: NSString;
begin
  Result:= StrToNSString( _title );
end;

function TCocoaConfigToolBarItemClassWithUI.tips: NSString;
begin
  Result:= StrToNSString( _tips );
end;

{ TCocoaConfigToolBarItemClassWithAction }

procedure TCocoaConfigToolBarItemClassWithAction.toClassConfig(
  const pItemConfig: PCocoaConfigToolBarItemWithAction );
begin
  Inherited toClassConfig( pItemConfig );
  _onAction:= TCocoaToolBarItemActionHandler( pItemConfig^.onAction );
end;

procedure TCocoaConfigToolBarItemClassWithAction.setItemAttribs(
  const item: NSToolBarItem);
begin
  inherited setItemAttribs(item);
  item.setTarget( item );
  item.setAction( ObjCSelector('lclItemAction:') );
  if item.respondsToSelector( ObjCSelector('lclSetHandler:') ) then
    item.performSelector_withObject( ObjCSelector('lclSetHandler:'), id(_onAction) );
end;

{ TCocoaConfigToolBarItemClass }

constructor TCocoaConfigToolBarItemClass.Create(
  const itemConfig: TCocoaConfigToolBarItem );
begin
  Inherited toClassConfig( @ItemConfig );
end;

function TCocoaConfigToolBarItemClass.createItem: NSToolBarItem;
var
  cocoaItem: TCocoaToolBarItem;
begin
  cocoaItem:= TCocoaToolBarItem.alloc.initWithItemIdentifier( self.identifier );
  self.setItemAttribs( cocoaItem );
  cocoaItem.autorelease;
  Result:= cocoaItem;
end;

{ TCocoaConfigToolBarItemClassSharing }

constructor TCocoaConfigToolBarItemClassSharing.Create(
  const itemConfig: TCocoaConfigToolBarItemSharing);
begin
  self.toClassConfig( @itemConfig );
  _onGetItems:= TCocoaToolBarItemSharingOnGetItems( itemConfig.onGetItems );
end;

function TCocoaConfigToolBarItemClassSharing.createItem: NSToolBarItem;
var
  cocoaItem: TCocoaToolBarItemSharing;
  cocoaItemDelegate: TCocoaToolBarItemSharingDelegate;
begin
  cocoaItem:= TCocoaToolBarItemSharing.alloc.initWithItemIdentifier( self.identifier );
  self.setItemAttribs( cocoaItem );
  cocoaItem.setAutovalidates( False );

  // release in TCocoaToolBarItemSharing
  cocoaItemDelegate:= TCocoaToolBarItemSharingDelegate.new;
  cocoaItemDelegate.lclSetOnGetItems( _onGetItems );
  cocoaItem.setDelegate( cocoaItemDelegate );

  cocoaItem.autorelease;
  Result:= cocoaItem;
end;

{ TCocoaConfigToolBarItemClassSearch }

constructor TCocoaConfigToolBarItemClassSearch.Create(
  const itemConfig: TCocoaConfigToolBarItemSearch);
begin
  self.toClassConfig( @itemConfig );
  _sendWhole:= itemConfig.sendWhole;
  _sendImmediately:= itemConfig.sendImmediately;
  _resignsWithCancel:= itemConfig.resignsWithCancel;
  _preferredWidth:= itemConfig.preferredWidth;
end;

function TCocoaConfigToolBarItemClassSearch.createItem: NSToolBarItem;
var
  cocoaItem: TCocoaToolBarItemSearch;
begin
  cocoaItem:= TCocoaToolBarItemSearch.alloc.initWithItemIdentifier( self.identifier );
  cocoaItem.searchField.setSendsWholeSearchString( _sendWhole );
  cocoaItem.searchField.setSendsSearchStringImmediately( _sendImmediately );
  cocoaItem.searchField.setDelegate( {%H-}NSTextFieldDelegateProtocol(cocoaItem) );
  cocoaItem.setResignsFirstResponderWithCancel( _resignsWithCancel );
  if _preferredWidth > 0 then
    cocoaItem.setPreferredWidthForSearchField( _preferredWidth );
  self.setItemAttribs( cocoaItem );
  cocoaItem.autorelease;
  Result:= cocoaItem;
end;

{ TCocoaConfigToolBarItemClassMenu }

constructor TCocoaConfigToolBarItemClassMenu.Create(
  const itemConfig: TCocoaConfigToolBarItemMenu);
begin
  self.toClassConfig( @itemConfig );
  _showsIndicator:= itemConfig.showsIndicator;
  _dynamic:= itemConfig.dynamic;
  _menu:= itemConfig.menu;
  _onGetMenu:= itemConfig.onGetMenu;
end;

function TCocoaConfigToolBarItemClassMenu.createItem: NSToolBarItem;
var
  cocoaItem: TCocoaToolBarItemMenu;

  procedure createStaticMenu;
  begin
    if NOT Assigned(_onAction) then
      cocoaItem.setAction( nil );

    if NOT Assigned(_menu) then begin
      if Assigned(_onGetMenu) then begin
        _menu:= TMenu.Create( nil );
        _onGetMenu( _menu );
      end;
    end;

    if Assigned(_menu) then begin
      cocoaItem.setMenu( NSMenu(_menu.Handle) );
    end;
  end;

begin
  cocoaItem:= TCocoaToolBarItemMenu.alloc.initWithItemIdentifier( self.identifier );
  self.setItemAttribs( cocoaItem );
  cocoaItem.lclSetDynamic( _dynamic );
  cocoaItem.lclSetOnGetMenu( _onGetMenu );

  // only static menu needs to be created in advance
  // for dynamic menu, it's created in TCocoaToolBarItemMenu.lclItemAction()
  if NOT _dynamic then
    createStaticMenu;

  cocoaItem.setShowsIndicator( _showsIndicator );
  cocoaItem.autorelease;
  Result:= cocoaItem;
end;

{ TCocoaConfigToolBarItemClassGroup }

constructor TCocoaConfigToolBarItemClassGroup.Create(
  const itemConfig: TCocoaConfigToolBarItemGroup);
begin
  self.toClassConfig( @itemConfig );
  _representation:= itemConfig.representation;
  _selectionMode:= itemConfig.selectionMode;
  _selectedIndex:= itemConfig.selectedIndex;
  _subitems:= itemConfig.subitems;
end;

function TCocoaConfigToolBarItemClassGroup.createItem: NSToolBarItem;
var
  groupWrapper: TCocoaToolBarItemGroupWrapper;
  toolbarItem: NSToolbarItemGroup;
  images: NSMutableArray;
  labels: NSMutableArray;

  procedure initSubitems;
  var
    i: Integer;
    count: Integer;
  begin
    count:= length( _subitems );
    images:= NSMutableArray.arrayWithCapacity( count );
    labels:= NSMutableArray.arrayWithCapacity( count );
    for i:=0 to count-1 do begin
      images.addObject( NSImage.imageWithSystemSymbolName_accessibilityDescription(
        TCocoaConfigToolBarItemClass(_subitems[i]).iconName, nil) );
      labels.addObject( TCocoaConfigToolBarItemClass(_subitems[i]).title );
    end;
  end;

begin
  initSubitems;
  toolbarItem:= NSToolbarItemGroup.groupWithItemIdentifier_images_selectionMode_labels_target_action(
    self.identifier,
    images,
    _selectionMode,
    labels,
    nil,
    nil );

  groupWrapper:= TCocoaToolBarItemGroupWrapper.new;
  groupWrapper.lclSetItemGroup( toolbarItem );
  groupWrapper.lclSetSelectedIndex( _selectedIndex );
  groupWrapper.lclSetHandler( _onAction );
  self.setItemAttribs( toolbarItem );
  toolbarItem.setTarget( groupWrapper );
  Result:= toolbarItem;
end;

end.