StringGadget right alignment

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: StringGadget right alignment

Post by Shardik »

Fred wrote:Using other gadget flags is not supported, use SetWindowLong_() on Windows if you want to change the style
Unfortunately it's a little bit more complicated. In Windows XP the alignment is only possible on creation of the StringGadget. Beginning with Windows Vista it's possible to change the alignment of a StringGadget's content dynamically:
http://forum.purebasic.com/english/view ... 13&t=36853

Try the following cross-platform code which I tested on Windows XP x86 SP3, Windows 7 x86 SP1, Kubuntu 13.04 x86 and Linux Mint 15 x86 Cinnamon. It redefines the StringGadget on Windows XP with the attribute #ES_RIGHT and changes the alignment dynamically on Windows Vista, Windows 7, Linux and MacOS X:

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC ""
    gtk_entry_set_alignment(Entry.I, XAlign.F)
  EndImport
CompilerEndIf

OpenWindow(0, 100, 100, 280, 70, "Right-aligned StringGadget", #PB_Window_SystemMenu)
StringGadget(0, 60, 20, 150, 21, "123")

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    gtk_entry_set_alignment(GadgetID(0), 1)
  CompilerCase #PB_OS_MacOS
    CocoaMessage(0, GadgetID(0), "setAlignment:", 1)
  CompilerCase #PB_OS_Windows
    If OSVersion() > #PB_OS_Windows_XP
      SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) & $FFFFFFFC | #ES_RIGHT) 
    Else
      StringGadget(0, 60, 20, 150, 21, "123", #ES_RIGHT)
    EndIf
CompilerEndSelect

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by Shardik on Tue Jun 25, 2013 1:46 pm, edited 1 time in total.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: StringGadget right alignment

Post by BorisTheOld »

@Fred
Fred wrote:Using other gadget flags is not supported, use SetWindowLong_() on Windows if you want to change the style
We use code similar to Shardik to make our GUI classes cross-platform. It's not a big problem for us, but it can be for less experienced programmers who are trying to write cross-platform applications.

Given the ease with which string gadgets can be aligned with user code, can #PB_STRING_LEFT and #PB_STRING_RIGHT be allowed when creating a string gadget?

Thanks
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: StringGadget right alignment

Post by ken_anthony »

Well done. It worked. This is the code I needed.

Code: Select all

ImportC ""
    gtk_entry_set_alignment(Entry.I, XAlign.F)
EndImport
gtk_entry_set_alignment(GadgetID(0), 1)
Post Reply