Page 1 of 1

[Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 6:20 am
by PB
How can I change a button's text alignment at runtime?

[Edit] Never mind, I got it sorted. :) Observe:

Code: Select all

OpenWindow(0,200,200,200,100,"test")
ButtonGadget(0,10,10,180,80,"Click for left")
bg=GadgetID(0)
t=1

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Gadget
    t=1-t
    If t=0
      SetGadgetText(0,"Click for center")
    Else
      SetGadgetText(0,"Click for left")
    EndIf
    SetWindowLongPtr_(bg,#GWL_STYLE,GetWindowLongPtr_(bg,#GWL_STYLE)!#BS_LEFT)
  EndIf
Until ev=#PB_Event_CloseWindow
Thanks to Jassing's post here: http://www.purebasic.fr/english/viewtop ... =5&t=52663 :)

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 8:11 am
by IdeasVacuum
Without API:

Code: Select all

Enumeration
#Win
#Btn
EndEnumeration

  OpenWindow(#Win,200,200,200,100,"test")
ButtonGadget(#Btn,10,10,180,80,"Click for left")

t = 1

Repeat
     ev = WaitWindowEvent()
  If ev = #PB_Event_Gadget

           iGdgID = EventGadget()
    Select iGdgID

            Case #Btn

                 If t = 0
                          t = 1
                          FreeGadget(#Btn)
                        ButtonGadget(#Btn,10,10,180,80,"Click for left")
                Else
                          t = 0
                          FreeGadget(#Btn)
                        ButtonGadget(#Btn,10,10,180,80,"Click for centre",#PB_Button_Left)
                EndIf
    EndSelect
  EndIf

Until ev = #PB_Event_CloseWindow

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 8:39 am
by PB
I don't like FreeGadget() because then you lose any tooltip,
gadget data, etc. You basically have to rebuild everything
associated with the gadget. Plus it gives a noticeable flash
when the gadget disappears and reappears on a slow PC,
and loses the gadget's Z order if it was coded with that in
mind. Too many disadvantages if the app is Windows only.

For cross-platform, yes; FreeGadget() is the only way. :)

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 9:39 am
by RASHAD
Solved some glitches

Code: Select all

OpenWindow(0,200,200,200,100,"test")
ButtonGadget(0,10,10,180,80,"Click for center")
bg=GadgetID(0)

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Gadget
    t ! 1
    If t=1
      SetGadgetText(0,"Click for center")
      SetWindowLongPtr_(bg,#GWL_STYLE,GetWindowLongPtr_(bg,#GWL_STYLE) |#BS_CENTER &~ #BS_RIGHT )
      InvalidateRect_(bg,0,1)
    Else
      SetGadgetText(0,"Click for left")
      SetWindowLongPtr_(bg,#GWL_STYLE,GetWindowLongPtr_(bg,#GWL_STYLE) |#BS_RIGHT &~ #BS_CENTER)
      InvalidateRect_(bg,0,1)
    EndIf
  EndIf
Until ev=#PB_Event_CloseWindow


Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 2:22 pm
by BorisTheOld
PB wrote:For cross-platform, yes; FreeGadget() is the only way. :)
Not strictly true. :wink:

With very little effort you could write a custom Button using the Canvas gadget. This would give you control over all button properties, including a full range of events that are not normally available with buttons.

We've written our own cross-platform versions of many gadgets, using the Container and Canvas gadgets.

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 3:28 pm
by IdeasVacuum
Another non-Api way:

Code: Select all

Enumeration
#Win
#Btn1
#Btn2
EndEnumeration

  OpenWindow(#Win,200,200,200,100,"test")
ButtonGadget(#Btn1,10,10,180,80,"Click for left")
ButtonGadget(#Btn2,10,10,180,80,"Click for centre",#PB_Button_Left)
  HideGadget(#Btn2, #True)

t = 1

Repeat
     ev = WaitWindowEvent()
  If ev = #PB_Event_Gadget

           iGdgID = EventGadget()
    Select iGdgID

            Case #Btn1, #Btn2

                 If t = 0
                          t = 1
                          HideGadget(#Btn2, #True)
                          HideGadget(#Btn1, #False)
                Else
                          t = 0
                          HideGadget(#Btn1, #True)
                          HideGadget(#Btn2, #False)
                EndIf
    EndSelect
  EndIf

Until ev = #PB_Event_CloseWindow

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 3:38 pm
by PB
Thanks IdeasVacuum, but I love the API way best. :)

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 4:32 pm
by Shardik
PB wrote:For cross-platform, yes; FreeGadget() is the only way. :)
BorisTheOld wrote:Not strictly true. :wink:
Not true at all! :P

It's always possible to use the respective API functions of the 3 operating systems supported by PureBasic. I have already collected links to cross-platform examples supporting Windows, Linux and MacOS using API functions. The biggest advantage: the devs might implement these functions natively in PureBasic because it's less work for them. And they have done that already with some examples... :wink:

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 9:25 pm
by BorisTheOld
Shardik wrote:It's always possible to use the respective API functions of the 3 operating systems supported by PureBasic. I have already collected links to cross-platform examples supporting Windows, Linux and MacOS using API functions.
The whole point of being cross-platform is that one doesn't need to use API functions.

I've already spent many years coding in different languages on different platforms, each with its own API functions. Had I wanted to continue on that road I would never have converted to PB.

The current manifestation of PB is extremely powerful. A little bit of creativity allows it to be used without the need for any API assistance. That's why we've been able to convert our COBOL, Visual Basic, and PowerBasic code to PB with almost no problems. We use OOP and have custom classes for GUI functionality. And we do it all without needing to support API code, or needing to wait for features to be added to the language.

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Sat Dec 28, 2013 10:00 pm
by Shardik
BorisTheOld wrote:The whole point of being cross-platform is that one doesn't need to use API functions.
I agree. But in some areas there still exist features which would be nice to have, but are currently not available in PB and are not realizable without API functions. Unfortunately you didn't cite the most important part of my posting:
Shardik wrote:The biggest advantage: the devs might implement these functions natively in PureBasic because it's less work for them. And they have done that already with some examples... :wink:
BorisTheOld wrote:A little bit of creativity allows it to be used without the need for any API assistance.
That's true. But not in all cases it's possible to find solutions without the need of API functions. Try to read some system settings like the double click time of the mouse button without API (meanwhile already implemented natively in PureBasic).

A further argument is that non-API solutions are mostly clumsy and slow. Why should they not be implemented in PureBasic natively? In fact PB is only cross-platform because most PB functions internally wrap the API functions of the respective operating systems...

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Mon Dec 30, 2013 9:13 am
by BorisTheOld
@Shardik

I have two concerns about mixing API code with PB code - reliability and ease of maintenance.

The more one "improves" PB code with API functions, the more chance there is that conflicts will occur with PB internals. And the chance of this happening will increase whenever PB internals are changed.

Maintenance is tricky at the best of times, but supporting different code for multiple platforms is almost impossible, especially if a few million lines of code are involved.

In the great scheme of things, customers don't worry about a few extra machine cycles or how pretty the screen looks. They worry about functionality and reliability. And over the years I've found that the best way to achieve this is by using only built-in language features with, perhaps, the support of self-contained libraries. But never have I found it desirable to make API calls from high-level languages. I prefer to use built-in language features to create the functionality that I need.

When coding for a single platform, it might be desirable to use API functions in certain situations. But we're now supporting three different platforms, so it's very important that we write strict cross-platform code. I don't want my guys wandering off into the API "weeds". :)

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Mon Dec 30, 2013 2:52 pm
by Shardik
BorisTheOld wrote:In the great scheme of things, customers don't worry about a few extra machine cycles or how pretty the screen looks.
BorisTheOld wrote:But never have I found it desirable to make API calls from high-level languages.
Do your applications use windows and gadgets to communicate with your users or are they only doing calculations in the background? As soon as you present PB gadgets in your forms you might run into trouble. Did you never need some pretty elementary tasks like to justify text in table columns (ListIconGadget), center text in a StringGadget or display tree items with icons or check boxes (in PB's MacOS version icons and check boxes can only be displayed at the left edge of the TreeGadget: API-workaround) and many more (take a look into my cross-platform examples). For these tasks I needed to use API functions on some or all of the 3 OS platforms and in most cases there doesn't exist a non-API way. Or do you sacrifice a user friendly GUI only in order to not having to use API?

You have already presented examples of your code which consists of thousands of modules. Why should it be so hard to also put API code into modules and change these modules as soon as the missing functionality is implemented natively in PureBasic?

Sorry for hijacking this thread. As a compensation I have taken PB's example code and added the API parts for Linux and MacOS... :twisted:

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC ""
    gtk_button_set_alignment(*Button.GtkButton, xAlign.F, yAlign.F)
  EndImport
CompilerEndIf

OpenWindow(0,200,200,200,100,"test")
ButtonGadget(0,10,10,180,80,"Click for left")
bg=GadgetID(0)
t=1

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Gadget
    t=1-t
    If t=0
      SetGadgetText(0,"Click for center")
    Else
      SetGadgetText(0,"Click for left")
    EndIf

    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        gtk_button_get_alignment_(bg, @xAlign.F, @yAlign.F)
        gtk_button_set_alignment(bg, Abs(xAlign - 0.5), yAlign)
      CompilerCase #PB_OS_MacOS
        CocoaMessage(0, bg, "setAlignment:", CocoaMessage(0, bg, "alignment") ! 1) 
      CompilerCase #PB_OS_Windows
        SetWindowLongPtr_(bg,#GWL_STYLE,GetWindowLongPtr_(bg,#GWL_STYLE)!#BS_LEFT)
    CompilerEndSelect
  EndIf
Until ev=#PB_Event_CloseWindow

Re: [Solved] ButtonGadget text alignment at runtime?

Posted: Wed Jan 01, 2014 4:38 am
by BorisTheOld
Shardik wrote:Do your applications use windows and gadgets to communicate with your users.......
Yes
Shardik wrote:Did you never need some pretty elementary tasks like to justify text in table columns (ListIconGadget), center text in a StringGadget......
Yes, we do all those things, but with custom gadgets. Such as the custom Button/ButtonBar example that I posted two months ago: http://www.purebasic.fr/english/viewtop ... 40&t=57143

Using the Canvas control, we can create just about any custom gadget we need with the functionality that we need. Be it List gadgets that can handle millions of items, or gadgets that have absolute control over contents, text position, text selection, and cursor position. The possibilities are endless, and it's all done without needing any API code.

It's just a matter of thinking outside the box, and using PB's standard, but powerful, features.