Module ToolBarStandardButton for all OS and PB v6.00

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Module ToolBarStandardButton for all OS and PB v6.00

Post by mk-soft »

Replacement for macOS and for all OS (PB v6.00 ToolBarStandardButton removed)

Update v1.6.1
- Added all images
- All OS

Update v1.6.3
- Fix linux large icons

Update v1.6.4
- Fix macOS Ventura. Use small size of toolbar not longer exits.

Code: Select all

;-TOP

; Comment: Module MyToolBarStandard
; Author : mk-soft
; Version: v1.6.5
; Created: 28.01.2017 (MacToolBarStandard)
; Updated: 26.03.2023
; Link En: https://www.purebasic.fr/english/viewtopic.php?f=12&t=67622
; Link De: 

; ***************************************************************************************

DeclareModule MyToolBarStandard
  
  CompilerIf Not Defined(PB_ToolBarIcon_Cut, #PB_Constant)
    Enumeration
      #PB_ToolBarIcon_Cut ; 0
      #PB_ToolBarIcon_Copy ; 1
      #PB_ToolBarIcon_Paste ; 2
      #PB_ToolBarIcon_Undo ; 3
      #PB_ToolBarIcon_Redo ; 4
      #PB_ToolBarIcon_Delete ; 5
      #PB_ToolBarIcon_New ; 6
      #PB_ToolBarIcon_Open ; 7
      #PB_ToolBarIcon_Save ; 8
      #PB_ToolBarIcon_PrintPreview ; 9
      #PB_ToolBarIcon_Properties ; 10
      #PB_ToolBarIcon_Help ; 11
      #PB_ToolBarIcon_Find ; 12
      #PB_ToolBarIcon_Replace ; 13
      #PB_ToolBarIcon_Print ; 14
    EndEnumeration
  CompilerEndIf
  
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    #NSWindowToolbarStyleAutomatic = 0 ; 
    #NSWindowToolbarStyleExpanded = 1 ; Top Left
    #NSWindowToolbarStylePreference = 2 ; Top Center
    #NSWindowToolbarStyleUnified = 3 ; TitleBar Large
    #NSWindowToolbarStyleUnifiedCompact = 4 ; TitleBar without text
  CompilerEndIf
  
  Declare MyCreateToolBar(ToolBar, WindowID, Flags = 0)
  Declare MyToolBarStandardButton(ButtonID, ButtonIcon, Mode = 0, Text.s = "")
  Declare SetToolBarStyle(Window, Style)
  
EndDeclareModule

Module MyToolBarStandard
  
  UsePNGImageDecoder()
  
  Global IsLarge
  
  Procedure CenterImage(Image, NewImage, Width, Height, Depth = 32, BackColor = #PB_Image_Transparent)
    Protected r1, x, y, dx, dy, image2
    
    r1 = CreateImage(NewImage, Width, Height, Depth, BackColor)
    If r1
      If NewImage = #PB_Any
        NewImage = r1
      EndIf
      dx = ImageWidth(Image)
      dy = ImageHeight(Image)
      x = (Width - dx) / 2
      y = (Height - dy) / 2
      If StartDrawing(ImageOutput(NewImage))
        DrawAlphaImage(ImageID(Image), x, y)
        StopDrawing()
      EndIf
    EndIf
    ProcedureReturn r1
  EndProcedure

  Procedure MyCreateToolBar(ToolBar, WindowID, Flags = 0)
    IsLarge = Flags & #PB_ToolBar_Large
    ProcedureReturn CreateToolBar(ToolBar, WindowID, Flags)
  EndProcedure
  
  Procedure MyToolBarStandardButton(ButtonID, ButtonIcon, Mode = 0, Text.s = "")
    Protected Image, Image2
    
    Select ButtonIcon
      Case #PB_ToolBarIcon_New
        Image = CatchImage(#PB_Any, ?ToolBarIcon_New)
      Case #PB_ToolBarIcon_Open
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Open)
      Case #PB_ToolBarIcon_Save
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Save)
      Case #PB_ToolBarIcon_Print
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Print)
      Case #PB_ToolBarIcon_PrintPreview
        Image = CatchImage(#PB_Any, ?ToolBarIcon_PrintPreView)
      Case #PB_ToolBarIcon_Find
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Find)
      Case #PB_ToolBarIcon_Replace
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Replace)
      Case #PB_ToolBarIcon_Cut
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Cut)
      Case #PB_ToolBarIcon_Copy
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Copy)
      Case #PB_ToolBarIcon_Paste
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Paste)
      Case #PB_ToolBarIcon_Undo
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Undo)
      Case #PB_ToolBarIcon_Redo
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Redo)
      Case #PB_ToolBarIcon_Delete
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Delete)
      Case #PB_ToolBarIcon_Properties
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Properties)
      Case #PB_ToolBarIcon_Help
        Image = CatchImage(#PB_Any, ?ToolBarIcon_Help)
    EndSelect
    
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        If IsLarge
          Image2 = CenterImage(Image, #PB_Any, 36, 28)
          FreeImage(Image)
          Image = Image2
        EndIf
      CompilerCase #PB_OS_MacOS
        If OSVersion() > #PB_OS_MacOSX_12
          If Not IsLarge
            Image2 = CenterImage(Image, #PB_Any, 24, 24)
            FreeImage(Image)
            Image = Image2
          EndIf
        EndIf
    CompilerEndSelect
    
    CompilerIf #PB_Compiler_Version <= 551
      result = ToolBarImageButton(ButtonID, ImageID(Image), Mode)
    CompilerElse
      result = ToolBarImageButton(ButtonID, ImageID(Image), Mode, Text)
    CompilerEndIf
    FreeImage(Image)
    ProcedureReturn result
    
  EndProcedure
  
  ; ----
  
  Procedure SetToolBarStyle(Window, Style)
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      If OSVersion() > #PB_OS_MacOSX_10_14
        CocoaMessage(0, WindowID(Window), "setToolbarStyle:", Style)
      EndIf
    CompilerEndIf
  EndProcedure
  
  ; ----
  
  DataSection
    ToolBarIcon_New:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/New.png"
    ToolBarIcon_Open:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"
    ToolBarIcon_Save:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Save.png"
    ToolBarIcon_Print:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Print.png"
    ToolBarIcon_PrintPreview:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/PrintPreview.png"
    ToolBarIcon_Find:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Find.png"
    ToolBarIcon_Replace:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Replace.png"
    ToolBarIcon_Cut:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Cut.png"
    ToolBarIcon_Copy:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Copy.png"
    ToolBarIcon_Paste:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Paste.png"
    ToolBarIcon_Undo:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Undo.png"
    ToolBarIcon_Redo:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Redo.png"
    ToolBarIcon_Delete:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Delete.png"
    ToolBarIcon_Properties:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Properties.png"
    ToolBarIcon_Help:
    IncludeBinary #PB_Compiler_Home + "examples/sources/Data/ToolBar/Help.png"
  EndDataSection
  
EndModule

; ***************************************************************************************

UseModule MyToolBarStandard

CompilerIf Not #PB_Compiler_OS = #PB_OS_Windows
  Macro CreateToolBar(ToolBar, WindowID, Flags = 0)
    MyCreateToolBar(ToolBar, WindowID, Flags)
  EndMacro
CompilerEndIf

Macro ToolBarStandardButton(ButtonID, ButtonIcon, Mode = 0, Text = "")
  MyToolBarStandardButton(ButtonID, ButtonIcon, Mode, Text)
EndMacro

; ***************************************************************************************

;- Example

CompilerIf #PB_Compiler_IsMainFile
  
  
  If OpenWindow(0, 0, 0, 800, 150, "ToolBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      ; Fix on Big Sur to old style expanded
      SetToolBarStyle(0, #NSWindowToolbarStyleExpanded)
    CompilerEndIf
    
    ;If CreateToolBar(0, WindowID(0), #PB_ToolBar_Large | #PB_ToolBar_Text)
    If CreateToolBar(0, WindowID(0));, #PB_ToolBar_Text)
      ToolBarStandardButton(0, #PB_ToolBarIcon_New, 0, "New")
      ToolBarStandardButton(1, #PB_ToolBarIcon_Open, 0, "Open")
      ToolBarStandardButton(2, #PB_ToolBarIcon_Save, 0, "Save")
      ToolBarSeparator()
      ToolBarStandardButton(10, #PB_ToolBarIcon_Copy, 0, "Copy")
      ToolBarStandardButton(11, #PB_ToolBarIcon_Cut, 0, "Cut")
      ToolBarStandardButton(12, #PB_ToolBarIcon_Delete, 0, "Delete")
      ToolBarStandardButton(13, #PB_ToolBarIcon_Paste, 0, "Paste")
      ToolBarStandardButton(14, #PB_ToolBarIcon_Replace, 0, "Replace")
      ToolBarStandardButton(15, #PB_ToolBarIcon_Find, 0, "Find")
      ToolBarSeparator()
      ToolBarStandardButton(20, #PB_ToolBarIcon_Redo, 0, "Redo")
      ToolBarStandardButton(21, #PB_ToolBarIcon_Undo, 0, "Undo")
      ToolBarSeparator()
      ToolBarStandardButton(30, #PB_ToolBarIcon_Print, 0, "Print")
      ToolBarStandardButton(31, #PB_ToolBarIcon_PrintPreview, 0, "Preview")
      ToolBarSeparator()
      ToolBarStandardButton(40, #PB_ToolBarIcon_Properties, 0, "Prop")
      ToolBarStandardButton(41, #PB_ToolBarIcon_Help, 0, "Help")
      
    EndIf
    
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow 
  EndIf
  
CompilerEndIf
Last edited by mk-soft on Sat May 20, 2023 1:00 pm, edited 15 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module ToolBarStandard for Mac

Post by mk-soft »

Update v1.2
- Use standard constants
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Module ToolBarStandard for Mac

Post by Danilo »

Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module ToolBarStandard for Mac

Post by mk-soft »

Update v1.4
- Big Sur: Set ToolBar style expanded
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module ToolBarStandardButton for MacOS and PB v6.00

Post by mk-soft »

Update v1.6.1
- Added all images
- All OS

Update v1.6.2
- Fix linux large icons
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
dige
Addict
Addict
Posts: 1254
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by dige »

👍 Thx!
"Daddy, I'll run faster, then it is not so far..."
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by infratec »

Hm ...

in my ToolBar folder (Windows) are only the following png files:

Code: Select all

04.06.2009  13:38               663 Copy.png
04.06.2009  13:38               648 Cut.png
04.06.2009  13:38               659 Find.png
04.06.2009  13:38               635 New.png
04.06.2009  13:38               688 Open.png
04.06.2009  13:38               703 Paste.png
04.06.2009  13:38               620 Save.png
So I get an error when I try to compile it.
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by mk-soft »

Complete png files since Beta 8 ;)

For older PB versions copy them. (Do not forget to include the licence)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by infratec »

Hi, hi,

I was still at beta 7.

Unfortunately the silk icons are only available in 16x16 and not 24x24.
So the Large flag results in unsharp pictures :cry:
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by mk-soft »

This is also the reason why Fred removed the function.

Maybe you have a link to a free icon (png) with a suitable size.
You can exchange them now.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by Marc56us »

infratec wrote: Wed Jun 01, 2022 7:24 am Unfortunately the silk icons are only available in 16x16 and not 24x24.
So the Large flag results in unsharp pictures :cry:
I recommend the Fugue icon 3.5.6 pack from Yusuke Kamiyamane
https://p.yusukekamiyamane.com
There are 3500 16x16, 259 24x24, and 78 32x32 icons (in directory bonus) In others still
Free use with citation of the author.
:wink:
Last edited by Marc56us on Wed Jun 01, 2022 8:02 am, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by infratec »

I'm already searching.
But I found only 16x16 OR 24x24 and not 16x16 AND 24x24 :cry:
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by infratec »

I found this:

https://sourceforge.net/projects/openiconlibrary/

inside are usable icons and they are free.

https://sourceforge.net/projects/openic ... p/download

For example in the folder:

icons/ico/16x16/actions/

icons/ico/24x24/actions/
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by infratec »

Hm ....

I already wrote a tool to draw SVG graphics with the PB VectorLibrary.
But not all stuff of SVG standard is included.
Maybe the use of SVG pictures would be more useful: only one file for all resolutions.
And the result should be fine too.

If Small is used draw them 16 by 16 , if Large draw them 24 by 24.

If I have some 'free' time, I will give it a try.

Ups ....

I totally forgot:
we have already free Vector Icons:

https://www.purebasic.fr/english/viewtopic.php?t=65091

And there is alreday a ToolBar example inside
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Module ToolBarStandardButton for all OS and PB v6.00

Post by infratec »

Modified to be like the standard ToolBar:

Code: Select all

EnableExplicit

XIncludeFile "../vectoricons.pbi" 


Define.i event, tbIconSize, ToolBarFlags

If OpenWindow(0, 100, 200, 650, 150, "Vectoricons toolbar example (PB 5.60+)", #PB_Window_SystemMenu | #PB_Window_SizeGadget) = 0
  MessageRequester("Fatal error", "Can't open main window.")
  End   
EndIf

;ToolBarFlags = #PB_ToolBar_Small|#PB_ToolBar_Text
ToolBarFlags = #PB_ToolBar_Large|#PB_ToolBar_Text

If CreateToolBar(0, WindowID(0), ToolBarFlags)
  
  If ToolBarFlags & #PB_ToolBar_Large
    tbIconSize = 24
  Else
    tbIconSize = 16
  EndIf
  
  ToolBarImageButton(0, ImageID(VectorIcons::NewDocument("", #PB_Any, tbIconSize, VectorIcons::#CSS_White, VectorIcons::#CSS_Navy, VectorIcons::#CSS_Black)), #PB_ToolBar_Normal, "New")
  ToolBarImageButton(1, ImageID(VectorIcons::Open2("", #PB_Any, tbIconSize, VectorIcons::#CSS_GoldenRod, VectorIcons::#CSS_Navy, VectorIcons::#CSS_White)), #PB_ToolBar_Normal, "Open")
  ToolBarImageButton(2, ImageID(VectorIcons::Diskette("", #PB_Any, tbIconSize, VectorIcons::#CSS_Navy, VectorIcons::#VI_GuardsmanRed, VectorIcons::#CSS_White)), #PB_ToolBar_Normal, "Save")
  
  ToolBarSeparator()
  
  ToolBarImageButton(3, ImageID(VectorIcons::Copy("", #PB_Any, tbIconSize, VectorIcons::#CSS_Blue, VectorIcons::#CSS_White)), #PB_ToolBar_Normal, "Copy")
  ToolBarImageButton(4, ImageID(VectorIcons::Cut("", #PB_Any, tbIconSize, VectorIcons::#CSS_Blue)), #PB_ToolBar_Normal, "Cut")
  ToolBarImageButton(5, ImageID(VectorIcons::NoEntry("", #PB_Any, tbIconSize, VectorIcons::#CSS_White, VectorIcons::#CSS_Red)), #PB_ToolBar_Normal, "Delete")
  ToolBarImageButton(6, ImageID(VectorIcons::Paste("", #PB_Any, tbIconSize, VectorIcons::#CSS_Gold, VectorIcons::#CSS_LightBlue)), #PB_ToolBar_Normal, "Paste")
  ToolBarImageButton(7, ImageID(VectorIcons::Refresh("", #PB_Any, tbIconSize, VectorIcons::#CSS_Green)), #PB_ToolBar_Normal, "Replace")
  ToolBarImageButton(8, ImageID(VectorIcons::Find("", #PB_Any, tbIconSize, VectorIcons::#CSS_Black)), #PB_ToolBar_Normal, "Find")
  
  ToolBarSeparator()
  
  ToolBarImageButton(9, ImageID(VectorIcons::Redo("", #PB_Any, tbIconSize, VectorIcons::#CSS_Green)), #PB_ToolBar_Normal, "Redo")
  ToolBarImageButton(10, ImageID(VectorIcons::Undo("", #PB_Any, tbIconSize, VectorIcons::#CSS_Green)), #PB_ToolBar_Normal, "Undo")
  
  ToolBarSeparator()
  
  ToolBarImageButton(11, ImageID(VectorIcons::Printer1("", #PB_Any, tbIconSize, VectorIcons::#CSS_Grey, VectorIcons::#CSS_White)), #PB_ToolBar_Normal, "Print")
  ToolBarImageButton(12, ImageID(VectorIcons::PrinterError1("", #PB_Any, tbIconSize, VectorIcons::#CSS_Grey, VectorIcons::#CSS_White, VectorIcons::#CSS_Green)), #PB_ToolBar_Normal, "Preview")
  
  ToolBarSeparator()
  
  ToolBarImageButton(13, ImageID(VectorIcons::Settings("", #PB_Any, tbIconSize, VectorIcons::#CSS_Grey)), #PB_ToolBar_Normal, "Prop")
  ToolBarImageButton(14, ImageID(VectorIcons::Question("", #PB_Any, tbIconSize, VectorIcons::#CSS_White, VectorIcons::#CSS_Blue)), #PB_ToolBar_Normal, "Help")
  
EndIf



Repeat
  event = WaitWindowEvent()
  
  Select event
    Case #PB_Event_Menu
      MessageRequester("Information", "ToolBar ID: " + Str(EventMenu()))
  EndSelect
Until event = #PB_Event_CloseWindow    ; If the user has clicked on the close button
Post Reply