Is that possible to have a spingadget vertically centered ?

Windows specific forum
Mesa
Enthusiast
Enthusiast
Posts: 345
Joined: Fri Feb 24, 2012 10:19 am

Is that possible to have a spingadget vertically centered ?

Post by Mesa »

Code: Select all

Enumeration 
  #Win 
  
  #Spin 
  
EndEnumeration 



Procedure StrGdgtVertCtr(iGdgtID.i, iLeftMargin.i) 
  ;#------------------------------------------------ 
  ;NOTE, for this to work, the StringGadget MUST have the #ES_MULTILINE flag applied 
  ; 
  Protected      rRect.RECT 
  Protected      fSize.SIZE 
  Protected      iHwnd.i = GadgetID(iGdgtID) 
  Protected   iLineCnt.i = SendMessage_(iHwnd, #EM_GETLINECOUNT, 0, 0) 
  Protected       iHdc.i = GetDC_(iHwnd) 
  Protected       sStr.s = "ABgy"        ;length = 4 chars 
  Protected    iStrLen.i = Len(sStr) 
  Protected iVertTweak.i = 0 ;corrects the displayed result (requires more logic!) 
  
  SelectObject_(iHdc, GetGadgetFont(iGdgtID)) 
  GetTextExtentPoint32_(iHdc, sStr, iStrLen, fSize) ;width & height of sStr 
  GetClientRect_(iHwnd, rRect) 
  ReleaseDC_(iHwnd, iHdc) 
  
  rRect\left = iLeftMargin 
  rRect\top = ((GadgetHeight(iGdgtID) - (fSize\cy * iLineCnt)) / 2) 
  rRect\bottom = (rRect\top + (fSize\cy * iLineCnt) + iVertTweak) 
  
  Debug " Text-->" + GetGadgetText(iGdgtID) + "<--" 
  Debug " iGdgtID-->" + Str(iGdgtID) + "<--" 
  Debug " fSize\cy-->" + Str(fSize\cy) + "<--" 
  Debug " iLineCnt-->" + Str(iLineCnt) + "<--" 
  Debug " rRect\Top-->" + Str(rRect\Top) + "<--" 
  Debug " rRect\Bottom-->" + Str(rRect\Bottom) + "<--" 
  Debug "GadgetHeight(iGdgtID)-->" + Str(GadgetHeight(iGdgtID)) + "<--" 
  Debug "=========================================" 
  Debug " " 
  
  If rRect\bottom < GadgetHeight(iGdgtID) 
    
    SendMessage_(iHwnd, #EM_SETRECT, 0, rRect) 
  EndIf 
EndProcedure 

If OpenWindow(#Win,0,0,500,250,"SpinGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
  
  
  
  SpinGadget(#Spin,10,80,450,50,0,10, #PB_Spin_Numeric|#ES_MULTILINE) 
  
  StrGdgtVertCtr(#Spin, 6) ; =>works with stringgadget but not with spingadget
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf
M.
Mesa
Enthusiast
Enthusiast
Posts: 345
Joined: Fri Feb 24, 2012 10:19 am

Re: Is that possible to have a spingadget vertically centere

Post by Mesa »

Trick with stringgadget but if you have a solution with a native spingadget, it would be better:

Code: Select all


Procedure vertical(StringGadgetID)
  CompilerSelect #PB_Compiler_OS 
    CompilerCase #PB_OS_Linux 
      ; 				MessageRequester("Info", 
      ; 					"On Linux the text in a StringGadget (GtkEntry) is always " + 
      ; 					"vertically centered by default and this cannot be changed!") 
    CompilerCase #PB_OS_MacOS 
      CocoaMessage(0, CocoaMessage(0, GadgetID(StringGadgetID), "cell"), 
                   "_setVerticallyCentered:", CenterVertically) 
      CocoaMessage(0, GadgetID(StringGadgetID), "setNeedsDisplay:", #True) 
    CompilerCase #PB_OS_Windows 
      Protected ClientRect.RECT 
      Protected DCHandle.I 
      Protected Text.S = GetGadgetText(StringGadgetID) 
      Protected TextXY.SIZE 
      
      
      ;Remove internal margin
      SendMessage_(GadgetID(StringGadgetID), #EM_SETMARGINS, #EC_LEFTMARGIN, 00|0 << 16)
      ;       SetGadgetFont(\Gadget[#PropertyChild_Label],FontID(Font))
      SetWindowLongPtr_(GadgetID(StringGadgetID), #GWL_STYLE, GetWindowLongPtr_(GadgetID(StringGadgetID), #GWL_STYLE) | #SS_CENTERIMAGE) 
      ;  
      DCHandle = GetDC_(GadgetID(StringGadgetID)) 
      SelectObject_(DCHandle, GetGadgetFont(StringGadgetID)) 
      GetTextExtentPoint32_(DCHandle, Text, Len(Text), @TextXY) 
      ReleaseDC_(GadgetID(StringGadgetID), DCHandle) 
      GetClientRect_(GadgetID(StringGadgetID), ClientRect) 
      
      ClientRect\top = (ClientRect\bottom - TextXY\cy) / 2 - 1 

      SendMessage_(GadgetID(StringGadgetID), #EM_SETRECT, 0, ClientRect) 
      InvalidateRect_(GadgetID(StringGadgetID), 0, #True) 
  CompilerEndSelect 
EndProcedure

Procedure AlignmentRight(StringGadgetID)
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    ImportC ""
      gtk_entry_set_alignment(Entry.I, XAlign.F)
    EndImport
  CompilerEndIf
  
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      gtk_entry_set_alignment(GadgetID(StringGadgetID), 1)
    CompilerCase #PB_OS_MacOS
      CocoaMessage(0, GadgetID(StringGadgetID), "setAlignment:", 1)
    CompilerCase #PB_OS_Windows
      If OSVersion() > #PB_OS_Windows_XP
        SetWindowLongPtr_(GadgetID(StringGadgetID), #GWL_STYLE, GetWindowLongPtr_(GadgetID(StringGadgetID), #GWL_STYLE) & $FFFFFFFC | #ES_RIGHT|#ES_MULTILINE) 
      Else
        x=GadgetX(StringGadgetID)
        y=GadgetY(StringGadgetID)
        w=GadgetWidth(StringGadgetID)
        h=GadgetHeight(StringGadgetID)
        text$=GetGadgetText(StringGadgetID)
        FreeGadget(StringGadgetID)
        StringGadget(StringGadgetID, x, y, w, h, text$, #ES_MULTILINE|#ES_RIGHT)
      EndIf
  CompilerEndSelect
  
EndProcedure


Procedure BindVScrollDatas()
  Debug GetGadgetState(1)
  SetGadgetText(3,Str(GetGadgetState(1)*-1))
EndProcedure


If OpenWindow(0, 0, 0, 400, 400, "ScrollBarGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  StringGadget    (3,  10, 30, 30, 30, "0", #ES_MULTILINE);| #ES_RIGHT
  ScrollBarGadget  (1, 40, 30, 25, 30 ,0, 100, 1, #PB_ScrollBar_Vertical)
  SetGadgetState   (1, 0) 
  SetGadgetAttribute(1, #PB_ScrollBar_Minimum, -100)
  
  AlignmentRight(3)
  vertical(3)
  
  BindGadgetEvent(1, @BindVScrollDatas())
  
  Repeat 
    Select WaitWindowEvent() 
      Case  #PB_Event_CloseWindow 
        End 
      Case  #PB_Event_Gadget 
        Select EventGadget()
            
          Case 1
            
            
        EndSelect 
    EndSelect
  ForEver 
EndIf
M.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Is that possible to have a spingadget vertically centere

Post by IdeasVacuum »

Remove the SpinGadget Frame:

Code: Select all

 SetWindowLongPtr_(GadgetID(#Spin), #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID(1), #GWL_EXSTYLE) &(~#WS_EX_CLIENTEDGE))
  SetWindowPos_(GadgetID(#Spin), 0, 0, 0, 0, 0, #SWP_SHOWWINDOW | #SWP_NOZORDER | #SWP_NOSIZE | #SWP_NOMOVE | #SWP_FRAMECHANGED)
.... and set it in/on a white container.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Is that possible to have a spingadget vertically centere

Post by RASHAD »

Hi
- Change the font size if you like

# 1:

Code: Select all

LoadFont(0,"Tahoma",14)
If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  SpinGadget (0, 10, 10, 55, 30, 0, 99, #PB_Spin_Numeric)
  SetGadgetFont(0,FontID(0))
  h = GadgetHeight(0,#PB_Gadget_RequiredSize)
  ResizeGadget(0,#PB_Ignore, #PB_Ignore,#PB_Ignore,h)
  SetGadgetColor(0,#PB_Gadget_BackColor,$BEFEFD)
  SetGadgetColor(0,#PB_Gadget_FrontColor,$0000FF)
  SetGadgetState (0, 50)
  Repeat
    event = WaitWindowEvent()
  Until event = #PB_Event_CloseWindow
EndIf 
# 2:
Hal. & Val. for Windows

Code: Select all

LoadFont(0,"Tahoma",14)
If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  SpinGadget (0, 10, 10, 55, 30, 0, 99, #PB_Spin_Numeric)
  SetGadgetFont(0,FontID(0))
  h = GadgetHeight(0,#PB_Gadget_RequiredSize)
  ResizeGadget(0,#PB_Ignore, #PB_Ignore,#PB_Ignore,h)
  SetGadgetColor(0,#PB_Gadget_BackColor,$BEFEFD)
  SetGadgetColor(0,#PB_Gadget_FrontColor,$0000FF)
  SetGadgetState (0, 50)
  SetWindowLongPtr_(GadgetID(0),#GWL_STYLE, GetWindowLongPtr_(GadgetID(0),#GWL_STYLE)|#ES_LEFT)
  Repeat
    event = WaitWindowEvent()
  Until event = #PB_Event_CloseWindow
EndIf
Egypt my love
Post Reply