[PB5.62b1] The text doesn't appear centred

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB5.62b1] The text doesn't appear centred

Post by Shardik »

marcoagpinto wrote:The text doesn't appear centred (old bug).
It's not a bug. It's simply not implemented in PB's EditorGadget to center the text.

To center the whole text in the EditorGadget in Windows you have to use the flag ES_CENTER when creating the EditorGadget. In Linux and MacOS you have to use the following API functions to center the whole text in an existing EditorGadget:

Code: Select all

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      gtk_text_view_set_justification_(GadgetID(Editor), #GTK_JUSTIFY_CENTER)
    CompilerCase #PB_OS_MacOS
      CocoaMessage(0, GadgetID(Editor), "setAlignment:", #NSCenterTextAlignment)
  CompilerEndSelect
For your conveniance I have programmed the following example which displays a PureBasic about window with centered text in the EditorGadget on Linux, MacOS and Windows. I have tested it successfully on the following operating systems:

- Linux Mint 18.1 "Serena" x64 with PB 5.61 x64 in GTK2 und GTK3

Image


- MacOS 10.13.1 "High Sierra" with PB 5.61 x86 and x64
- MacOS 10.6.8 "Snow Leopard" with PB 5.61 x86
Image
- Windows 7 x64 SP1 with PB 5.61 x86 und x64

Image

Code: Select all

EnableExplicit

UsePNGImageDecoder()

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  ; ----- Only necessary for extracting PureBasic's 48x48 icon from
  ;       PureBasic.Exe

  ImportC ""
    SHGetImageList_(ImageType.I, ImageListID.I, *ImageList) As "SHGetImageList"
  EndImport

  DataSection
    IID_IImageList:
    Data.L $46EB5926
    Data.W $582E, $4017
    Data.B $9F, $DF, $E8, $99, $8D, $AA, $09, $50
  EndDataSection
CompilerEndIf

Procedure DisplayAboutWindow()
  Protected AboutWindow.I
  Protected Editor.I
  Protected EditorFlags.I
  Protected FontArialBold.I
  Protected FontSize.I
  Protected Height.I
  Protected PBLogo.I
  Protected PBIcon.I
  Protected PBVersion.S
  Protected Text.S
  Protected Width.I

  EditorFlags = #PB_Editor_ReadOnly | #PB_Editor_WordWrap
  PBVersion = Str(#PB_Compiler_Version)
  PBVersion = Left(PBVersion, Len(PBVersion) - 2) + "." + Right(PBVersion, 2)

  AboutWindow = OpenWindow(#PB_Any, 0, 0, 1, 1, "About PureBasic",
    #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)

  ; ----- Load PureBasic's big logo with 381x68 pixels

  PBLogo = LoadImage(#PB_Any, #PB_Compiler_Home +
    "examples/3d/Data/PureBasicLogo.bmp")
  ImageGadget(#PB_Any, 10, 10, ImageWidth(PBLogo), ImageHeight(PBLogo),
    ImageID(PBLogo))
   
  ; ----- Load PureBasic's Z logo with 48x48 pixels

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      PBIcon = LoadImage(#PB_Any, #PB_Compiler_Home + "logo.png") 
      FontSize = 13
    CompilerCase #PB_OS_MacOS
      PBIcon = LoadImage(#PB_Any, #PB_Compiler_Home + "logo.png") 
      FontSize = 16
    CompilerCase #PB_OS_Windows
      ; ----- In PB's Windows version the file logo.png is missing, so the
      ;       PureBasic icon has to be extracted from PureBasic.Exe

      Protected FileInfo.SHFILEINFO
      Protected IconHandle.I
      Protected ImageList.iImageList
      Protected Shell32.I

      SHGetFileInfo_(#PB_Compiler_Home + "PureBasic.Exe", -1, @FileInfo,
        SizeOf(SHFILEINFO), #SHGFI_SYSICONINDEX)
      Shell32 = OpenLibrary(#PB_Any, "Shell32.DLL")

      If Shell32
        If CallFunction(Shell32, "SHGetImageList", #SHIL_EXTRALARGE,
          ?IID_IImageList, @ImageList) = 0
          ImageList\GetIcon(FileInfo\iIcon, #ILD_TRANSPARENT, @IconHandle)
          PBIcon = CreateImage(#PB_Any, 48, 48, 32, #PB_Image_Transparent)

          If StartDrawing(ImageOutput(PBIcon))
            DrawImage(IconHandle, 0, 0, 48, 48)
            DestroyIcon_(IconHandle)
            StopDrawing()
          EndIf

          CloseLibrary(Shell32)
        EndIf
      EndIf

      FontSize = 13

      ; ----- Add Windows flag to center text in EditorGadget
      EditorFlags | #ES_CENTER
  CompilerEndSelect

  ImageGadget(#PB_Any, ImageWidth(PBLogo) + 20, 20, ImageWidth(PBIcon),
    ImageWidth(PBIcon), ImageID(PBIcon))
  Width = ImageWidth(PBLogo) + ImageWidth(PBIcon) + 20
  Editor = EditorGadget(#PB_Any, 10, ImageHeight(PBLogo) + 10, Width, 130,
    EditorFlags)
  Height = ImageHeight(PBLogo) + GadgetHeight(Editor)
  FontArialBold = LoadFont(#PB_Any, "Arial", FontSize, #PB_Font_Bold)

  If FontArialBold
    SetGadgetFont(Editor, FontID(FontArialBold))
  EndIf

  Text = #LF$ +
    "Coded/Compiled with PureBasic " + PBVersion + #LF$ +
    Chr(34) + "Feel the ..Pure.. Power" + Chr(34) + #LF$ + #LF$ +
    "© 1998-" + FormatDate("%yyyy", #PB_Compiler_Date) +
    " Fantaisie Software." + #LF$ +
    "www.purebasic.com"
  SetGadgetText(Editor, Text)

  ; ----- Center contents of EditorGadget

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      gtk_text_view_set_justification_(GadgetID(Editor), #GTK_JUSTIFY_CENTER)
    CompilerCase #PB_OS_MacOS
      CocoaMessage(0, GadgetID(Editor), "setAlignment:", #NSCenterTextAlignment)
  CompilerEndSelect

  ; ----- Resize window to contain images and EditorGadget

  ExamineDesktops()
  ResizeWindow(AboutWindow, (DesktopWidth(0) - Width + 20) / 2,
    (DesktopHeight(0) - Height + 20) / 2, Width + 20, Height + 20)
  HideWindow(AboutWindow, #False)
  SetActiveGadget(-1)

  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndProcedure

DisplayAboutWindow()
Last edited by Shardik on Tue Jan 30, 2018 9:30 am, edited 1 time in total.
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: [PB5.62b1] The text doesn't appear centred

Post by marcoagpinto »

Thank you guys for all the help!
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: [PB5.62b1] The text doesn't appear centred

Post by walbus »

It look right and centered works not on gtk3

Code: Select all

If OpenWindow(0, 0, 0, 270, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0, 10,  10, 250, 20, "TextGadget Standard (Left)")
    TextGadget(1, 10,  70, 250, 20, "TextGadget Center", #PB_Text_Center)
    TextGadget(2, 10,  40, 250, 20, "TextGadget Right", #PB_Text_Right)
    TextGadget(3, 10, 100, 250, 20, "TextGadget Border", #PB_Text_Border)
    TextGadget(4, 10, 130, 250, 20, "TextGadget Center + Border", #PB_Text_Center | #PB_Text_Border)
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
As a Hint : DrawText_EX and DrawText_BF can handle this worry free on all OS :wink:
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB5.62b1] The text doesn't appear centred

Post by Shardik »

walbus wrote:It look right and centered works not on gtk3
It's fixed in PB 5.46 and PB 5.62 final!

Did you test my example code from above which utilizes the EditorGadget (not the TextGadget as in your example). My example code works on Linux Mint 18.1 'Serena' with Cinnamon in PB 5.46, PB 5.61 and PB 5.62 final like a charm on both GTK2 and GTK3.
Post Reply