How to capitalize input string in string / editor Gadget

Just starting out? Need help? Post your questions and find answers here.
davido28
New User
New User
Posts: 6
Joined: Thu Feb 10, 2011 2:27 pm

How to capitalize input string in string / editor Gadget

Post by davido28 »

Hello,

I wish there was a Capitalize function for strings. Please can someone help me solve these problems?
Is there a way to force capitalization of a string text even if the user has typed in lower case?

For example if the user types this

Code: Select all

harry genks 
I want this

Code: Select all

Harry Genks
as he types.

I have tried building this procedure

Code: Select all

Procedure Capitalize_Input(GadgetID.i)
  
 Protected CurrentText.s = GetGadgetText(GadgetID)
 Static Text.s
 Static Left.s
 Left = UCase(LTrim(Left(CurrentText,1)))
 Text = Left+Mid(Text,2)
 SetGadgetText(GadgetID,Text) 
 
EndProcedure  
..which I called inside the #PB_Event_Gadget event in Event loop but it doesn't work :(
(I'm a newbie to this wonderful language so please don't laugh :) )

Also I want another function to capitalize a string of text, as long as it comes after a
full stop. For example if a user types in the following in a string or editor gadget

Code: Select all

hello and welcome to pure programming. i am a newbie willing to learn. please teach me
I want that to be changed dynamically (as he types) to:

Code: Select all

Hello and welcome to pure programming. I am a newbie willing to learn. Please teach me
Can anyone put me through?
Thanks for your help
User avatar
Shardik
Addict
Addict
Posts: 2062
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to capitalize input string in string / editor Gadget

Post by Shardik »

Hello davido28,

at first welcome in the PureBasic Forum...:wink:

This is a simple Windows example to automatically capitalize a letter at the
beginning and after each space character. Try it for example with your
"harry genks":

Code: Select all

EnableExplicit

Define CurrentText.S
Define CurrentTextLength.I

OpenWindow(0, 0, 0, 230, 40, "Capitalize leading characters", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 10, 10, WindowWidth(0) - 20, 20, "")
SetActiveGadget(0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #WM_KEYUP
      If EventGadget() = 0
        CurrentText = GetGadgetText(0)
        CurrentTextLength = Len(CurrentText)
        
        If CurrentTextLength > 0
          If CurrentTextLength = 1
            CurrentText = UCase(CurrentText)
          Else
            If Mid(CurrentText, CurrentTextLength - 1, 1) = " "
              CurrentText = Left(CurrentText, Len(CurrentText) - 1) + UCase(Right(CurrentText, 1))
            EndIf
          EndIf

          SetGadgetText(0, CurrentText)
          SendMessage_(GadgetID(0), #EM_SETSEL, $FFFF, $FFFF) ; Set cursor to end of string
        EndIf
      EndIf
  EndSelect
ForEver
For the capitalization of a complete sentence in the English language you
would have to change the space character in the line

Code: Select all

            If Mid(CurrentText, CurrentTextLength - 1, 1) = " "
to a full stop and check the second character before the last one (instead of
just the character before the last one)

Code: Select all

            If Mid(CurrentText, CurrentTextLength - 2, 2) = ". "
so that after each full stop followed by a space the next character will be
capitalized. For longer text passages this method doesn't work well because
after each character entry the whole text line has to be rewritten and thus
producing some flicker. In that case you would have to utilize a callback which
probably is a little bit of overkill for a beginner... :wink:

This example won't work in Linux or MacOS X because I had to use the Windows
specific API function SendMessage_() to position the cursor at the end of the
text. If you want to know how to set the cursor position in Linux or MacOS X
you may take a look into this thread:
http://www.purebasic.fr/english/viewtopic.php?t=38488
davido28
New User
New User
Posts: 6
Joined: Thu Feb 10, 2011 2:27 pm

Re: How to capitalize input string in string / editor Gadget

Post by davido28 »

Thanks Shardik, worked perfect.

A question or two if you please though,

Code: Select all

 #WM_KEYUP
Is this a Windows-only event constant? Could not find it anywhere
in the docs. Where can I read up on this and other non-PB constants?
Shardik wrote:For longer text passages this method doesn't work well because
after each character entry the whole text line has to be rewritten and thus
producing some flicker. In that case you would have to utilize a callback which
probably is a little bit of overkill for a beginner...
Hmm.. yes I can see it breaks. I have to find examples and read up on callbacks then. I'm not entirely
new to programming (I'm a web programmer), just starting up on the desktop and with PB and I must
say I'm enjoying it so far. Just a few more weeks and I'll shell out money to buy the full version.. :D

I will look into the solution for MacOS X at the thread you pointed out.

Thanks again.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: How to capitalize input string in string / editor Gadget

Post by luis »

davido28 wrote: A question or two if you please though,

Code: Select all

 #WM_KEYUP
Is this a Windows-only event constant? Could not find it anywhere
in the docs. Where can I read up on this and other non-PB constants?
It's one of the many constants representing a window's message. Yes, it's Windows only.

Type #WM_ in the PB ide to see some of those const.

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

If you are new to API stuff and the "Windows message loop", just look up in the Internet or buy a book like "Programming for Windows" by Charles Petzold.

All the stuff you will read here it's still usable today and it's important even if you want to use a more modern language in the future. It's not time wasted.
"Have you tried turning it off and on again ?"
davido28
New User
New User
Posts: 6
Joined: Thu Feb 10, 2011 2:27 pm

Re: How to capitalize input string in string / editor Gadget

Post by davido28 »

Thanks luis! Oh my I gotta lot of reading to do. :lol:
User avatar
Shardik
Addict
Addict
Posts: 2062
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to capitalize input string in string / editor Gadget

Post by Shardik »

davido28,

sorry for forgetting to state that #WM_KEYUP is a also a Windows specific
constant but luis already explained it nicely...

If you want to see an improved example utilizing a callback take a look into
this new example (again only for Windows). It subclasses the StringGadget
and intercepts key entry before the character is actually displayed in the
gadget allowing us to change a character to uppercase during key entry
and thus eliminating the need to redisplay a long text only because we have
to change one single character... :wink:

Code: Select all

Procedure CustomStringGadgetCallback(WindowHandle.I, Msg.I, LParam.I, WParam.I)
  Shared DefaultStringGadgetCallback.I

  Protected CurrentText.S
  Protected CurrentTextLength.I

  If Msg = #WM_CHAR
    CurrentText = GetGadgetText(0)
    CurrentTextLength = Len(CurrentText)

    If LParam >= 'a' And LParam <= 'z'
      If CurrentTextLength = 0
        LParam = LParam & $DF
      Else
        If Mid(CurrentText, CurrentTextLength - 1, 2) = ". "
          LParam = LParam & $DF
        EndIf
      EndIf
    EndIf
  EndIf

  ProcedureReturn CallWindowProc_(DefaultStringGadgetCallback, WindowHandle, Msg, LParam, WParam) 
EndProcedure

OpenWindow(0, 0, 0, 270, 40, "Capitalize characters after full stop", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 10, 10, WindowWidth(0) - 20, 20, "")
SetActiveGadget(0)

; ----- Subclass StringGadget
DefaultStringGadgetCallback = GetWindowLongPtr_(GadgetID(0), #GWL_WNDPROC) 
SetWindowLongPtr_(GadgetID(0), #GWL_WNDPROC, @CustomStringGadgetCallback()) 

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
davido28
New User
New User
Posts: 6
Joined: Thu Feb 10, 2011 2:27 pm

Re: How to capitalize input string in string / editor Gadget

Post by davido28 »

Shardik wrote:If you want to see an improved example utilizing a callback take a look into
this new example (again only for Windows). It subclasses the StringGadget
and intercepts key entry before the character is actually displayed in the
gadget allowing us to change a character to uppercase during key entry
and thus eliminating the need to redisplay a long text only because we have
to change one single character...
Shardik,
Cool 8) !

Was wondering where to get my first PB tutorial on callbacks.
Studying your code now. Hope I can come running to you if/when I
get stuck..:wink:
Thanks man.
Post Reply