StringGadget padding

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

StringGadget padding

Post by captain_skank »

I swear to god that i've asked this before but cannot for the life of me find my post.

Anyhoo, I can get text height and/or width using the Textheight/TextWidth commands.

But how can I find out or change the padding ( left/right/top/bottom ) of a StringGadget ( or EditorGadget )

I thought it was an ES_ type command but can't seem to find what i'm looking for.

As usual, any help apreaciated.

cheers
User avatar
Michael Vogel
Addict
Addict
Posts: 2680
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: StringGadget padding

Post by Michael Vogel »

Hm, quite sure Rashad has posted a perfect solution, here are two snippets (which should help to find suitable threads in the forum)....

Code: Select all

Procedure AlignedInput(x, y, w, h, text.s)
	
	ContainerGadget(1,x,y,w,h,#PB_Container_Double)
	SetGadgetColor(1,#PB_Gadget_BackColor,#White)
	i = CreateImage(#PB_Any, 1, 1)
	StartDrawing(ImageOutput(i))
	DrawingFont(GetGadgetFont(#PB_Default))
	fh = TextHeight("Wg")
	StopDrawing()
	FreeImage(i)
	ty = h/2-fh/2-2
	StringGadget(2, 2, ty, w-4, fh, text, #PB_String_BorderLess)
	CloseGadgetList()

EndProcedure

#W = 512
#H = 384

OpenWindow(0, 0, 0, #W, #H, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
AlignedInput(10, 10, 100, 35, "Hello")

Repeat
	Select WaitWindowEvent()
	Case #PB_Event_CloseWindow
		Break
	EndSelect
ForEver

StringGadget #1 should be what you are interested in (uncomment the 'width' related lines for padding left and right)...

Code: Select all

Procedure StringGadgetVCenter(GadgetID)

	Protected hwndEdit
	Protected hdc
	Protected fsz.SIZE
	Protected erect.RECT
	Protected height
	;Protected width

	hwndEdit=GadgetID(GadgetID)
	hdc=GetDC_(hwndEdit)
	SelectObject_(hdc,GetGadgetFont(GadgetID))
	GetTextExtentPoint32_(hdc,"ABC",3,fsz)
	ReleaseDC_(hwndEdit,hdc)

	GetClientRect_(hwndEdit,eRect)
	height=GadgetHeight(GadgetID)
	;width=GadgetWidth(GadgetID)
	;eRect\left=10
	;eRect\right=width-10
	eRect\top=height>>1-fsz\cy<<4/21;		cy*16/20 (Text tends to go to the top) cy*16/22 (...or to the bottom)
	eRect\bottom=height+fsz\cy>>1

	SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)

EndProcedure

Text$ = "Test Vertical and Horizontal Centered Text"

OpenWindow(0,0,0,300,200,"Edit Control VCenter",#WS_SYSMENU|#WS_CAPTION)
LoadFont(0,"arial",010)
StringGadget(0,10,10,280,30,Text$,#ES_MULTILINE)
StringGadget(1,10,50,280,30,Text$,#ES_MULTILINE)
SetGadgetFont(1,FontID(0))
StringGadget(2,10,90,280,30,Text$,#ES_MULTILINE|#PB_String_ReadOnly)
TextGadget(3,10,130,280,30,Text$,#PB_Text_Border|#SS_CENTERIMAGE)
ButtonGadget(9,10,170,280,30,"Hallo a&a b&&b c&&&c")

SetGadgetColor(0,#PB_Gadget_FrontColor,$0103FE)
SetGadgetColor(0,#PB_Gadget_BackColor,$D0FFFF)
StringGadgetVCenter(1)
StringGadgetVCenter(2)

Repeat
	EventID = WaitWindowEvent()
	Select EventGadget()
	EndSelect
Until EventID = #PB_Event_CloseWindow
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget padding

Post by RASHAD »

Hi

Code: Select all

Procedure SetMargin(Gad, PageW,PageH, LeftM, TopM, RightM, BottomM)
  r.RECT
  r\left = LeftM
  r\top = TopM
  r\right = PageW - RightM
  r\bottom = PageH - BottomM
  SendMessage_(GadgetID(Gad), #EM_SETRECT, 0, r)
EndProcedure

LoadFont(0,"Tahoma",14)

If OpenWindow(0, 0, 0, 600, 300, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 10,10, 580,60,"tuuty uiyiyu  iuiuyiy  iyuiyiyiy" ,#ES_MULTILINE)
  SetGadgetFont(0,FontID(0))
  SetMargin(0, 580,580,30, 10, 10, 10)
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
  EndSelect                             
Until Quit = 1
EndIf
Egypt my love
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: StringGadget padding

Post by captain_skank »

Thanks for that, really helps.

Is there a way to get the padding of a existing gadget though.

I thought EM_GETRECT might give me something but according to microsofts own doc what it returns doesn't mean anything usefull.

Anyhoo, again thanks for the code.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget padding

Post by RASHAD »

You can use #EM_GETRECT if you used #EM_SETRECT

Code: Select all

Procedure SetMargin(Gad, PageW,PageH, LeftM, TopM, RightM, BottomM)
  r.RECT
  r\left = LeftM
  r\top = TopM
  r\right = PageW - RightM
  r\bottom = PageH - BottomM
  SendMessage_(GadgetID(Gad), #EM_SETRECT, 0, r)
EndProcedure

LoadFont(0,"Tahoma",24)

If OpenWindow(0, 0, 0, 600, 300, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Text$ = "tuuty uiyiyu  iuiuyiy  iyuiyiyiy"
  StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    tWidth = TextWidth(Text$) 
    tHeight = TextHeight(Text$)
  StopDrawing()
  StringGadget(0, 10,10, 580,60, Text$ ,#WS_BORDER | #ES_READONLY | #ES_MULTILINE)
  SetGadgetColor(0,#PB_Gadget_BackColor,$ABFBFF)
  SetGadgetColor(0,#PB_Gadget_FrontColor,$1B25FE)
  SetGadgetFont(0,FontID(0))
  SetMargin(0, 580,580,GadgetWidth(0)/2-tWidth/2, GadgetHeight(0)/2-tHeight/2 - 6, 10, 10)

  ButtonGadget(1,10,270,60,20,"RUN")
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1

    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          SendMessage_(GadgetID(0),#EM_GETRECT,0,gr.RECT)
          Debug "Left Margin : "+Str(gr\left)
          Debug "Top Margin : "+Str(gr\top)
        
      EndSelect
  EndSelect                             
Until Quit = 1
EndIf
You can use #EM_GETMARGINS if you used #EM_SETMARGINS
I think that this is your case

Code: Select all

LoadFont(0,"Tahoma",24)

If OpenWindow(0, 0, 0, 600, 300, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Text$ = "tuuty uiyiyu  iuiuyiy  iyuiyiyiy"
  StringGadget(0, 10,10, 580,60, Text$ ,#WS_BORDER | #ES_READONLY | #ES_MULTILINE)
  SetGadgetColor(0,#PB_Gadget_BackColor,$ABFBFF)
  SetGadgetColor(0,#PB_Gadget_FrontColor,$1B25FE)
  SetGadgetFont(0,FontID(0))
  SendMessage_(GadgetID(0),#EM_SETMARGINS,#EC_LEFTMARGIN,30|0 << 16)

  ButtonGadget(1,10,270,60,20,"RUN")
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1

    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          margins = SendMessage_(GadgetID(0),#EM_GETMARGINS,0,0)
          Debug "Left Margin : "+Str(margins & $FFFF)
        
      EndSelect
  EndSelect                             
Until Quit = 1
EndIf
Egypt my love
Post Reply