Page 1 of 1

Posted: Fri Oct 25, 2002 7:54 am
by BackupUser
Restored from previous forum. Originally posted by fweil.

Hello,

Here is a sample code I could not decide to place in the tricks'n tips section, because it does not work exactly as I would like.

Running this program, you can select colors by clicking BG1 and BG2 and TC1, TC2, which are background and foreground colors of a 2 states button with a label. It is called also a Cell.

Also a feature for changing the Cell's width and height.

By clicking the Cell you can select a font with a size, and bold and / or italic attributes.

Also 2 check boxes are there to change bold / italic attributes separately.

When changing any color, attribute or size, the Cell is updated.

When moving the pointer over the Cell, you will see it changes from BG1/TC1 colors to BG2/TC2.

The state's change is managed in the callback procedure.

I suppose everything is nice and works well, except that I discovered the bold / italic attributes are not so easy to process.

Just read finally comments at start of the source to understand.

If anyone find interest in it and a good solution for managing bold / italic attributes properly I will move this post to tricks'n tips.

Code: Select all

;
; Test code from F.Weil : 20021025
;
; Play and use this code as you want.
;
; This piece of code is part of a larger program. It makes possible to
; select 2 backgrounds and 2 foreground colors to build a 2 states button
; changing state when mouse is over.
;
; Click this button opens a font requester to change the buttons label.
;
; It actually works fine except for displaying correctly bold and italic
; on my PC. It should work as any selected Font with Bold or Italic
; attributes are named : 'Font' Bold Italic.
;
; On my PC the font listing displays 'Font' Gras Italique (in french).
;
; If I change Bold to Gras and Italic to Italique in constant strings of
; Refresh2StateButton procedure, it works to me.
;
; US / english users should find this sample code working as it is.
;

;
; Gadgets numbers defined using constants for readability
;
#Gadget_Color1 = 105
#Gadget_Color2 = 106
#Gadget_Bold = 111
#Gadget_Italic = 112
#Gadget_CellHeight = 122
#Gadget_CellWidth = 123
#Gadget_TextColor1 = 131
#Gadget_TextColor2 = 132
#Gadget_2StateButton1 = 133
#Gadget_2StateButton2 = 134

;
; onMouseOver2StateButton is a signal set to #TRUE when the mouse passes over the 2 states button
;
onMouseOver2StateButton.l

Global onMouseOver2StateButton

Procedure.l IMin(a.l, b.l)
  If a  ""
        FrontColor(Red(TextColor), Green(TextColor), Blue(TextColor))
        DrawingFont(LoadFont(10, Font, FontSize))
        DrawingMode(1)
        If TextLength(Label) < Width
            XPos.l = (Width - TextLength(Label)) / 2
          Else
            XPos.l = 4
        EndIf
        If TextLength("M") < Height
            YPos.l = (Height - TextLength("M")) / 2 - 2
          Else
            YPos.l = 0
        EndIf
        Locate(XPos, YPos)
        DrawText(Label)
    EndIf
  StopDrawing()
  ProcedureReturn ImageID
EndProcedure

;
; Refresh2StateButton changes the two buttons with modified colors and label
;
Procedure Refresh2StateButton(CellWidth.l, CellHeight.l, Color1.l, TextColor1.l, Color2.l, TextColor2.l, CellFont.s, CellFontSize, CellFontBold.l, CellFontItalic.l)
  Bold.s = ""
  Italic.s = ""
  If CellFontBold
      Bold.s = " Bold"
  EndIf
  If CellFontItalic
      Italic.s = " Italic"
  EndIf
  MaxWidth.l = 110
  MaxHeight.l = 35
  FreeGadget(#Gadget_2StateButton1)
  ButtonImageGadget(#Gadget_2StateButton1, 480 - IMin(CellWidth, MaxWidth) / 2, MaxWidth - IMin(CellHeight, MaxHeight) / 2, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), MyLabeledImage(5, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), Color1, TextColor1, "Text font", CellFont + Bold + Italic, CellFontSize))
  GadgetToolTip(#Gadget_2StateButton1, "Click here to change text font (actually " + CellFont + Bold + Italic + " " + Str(CellFontSize) + ")")
  FreeGadget(#Gadget_2StateButton2)
  ButtonImageGadget(#Gadget_2StateButton2, 480 - IMin(CellWidth, MaxWidth) / 2, MaxWidth - IMin(CellHeight, MaxHeight) / 2, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), MyLabeledImage(6, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), Color2, TextColor2, "Text font", CellFont + Bold + Italic, CellFontSize))
  GadgetToolTip(#Gadget_2StateButton2, "Click here to change text font (actually " + CellFont + Bold + Italic + " " + Str(CellFontSize) + ")")
  HideGadget(#Gadget_2StateButton2, 1)
EndProcedure

;
; Main starts here
;

  Quit.l = #FALSE

  WindowXSize.l = 640
  WindowYSize.l = 280
  Color1.l = $804000
  Color2.l = $007D7D
  TextColor1.l = $80FFFF
  TextColor2.l = $FF0000
  CellHeight.l = 30
  Centered.l = #TRUE
  CellWidth.l = 160
  CellFont.s = "Verdana"
  CellFontSize.l = 12
  
  hWnd.l = OpenWindow(0, 200, 200, WindowXSize, WindowYSize, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar, "F.Weil - Font selection and decorated buttons")
  If hWnd
      AddKeyboardShortcut(0, #PB_Shortcut_Escape, 99)
      
      LoadFont(0, "Verdana", 8)
      
      If CreateGadgetList(WindowID())

          SetGadgetFont(FontID())
          
          CheckBoxGadget(#Gadget_Bold, 430, 160, 50, 20, "Bold")
          GadgetToolTip(#Gadget_Bold, "Check this to use bold labels")
          
          CheckBoxGadget(#Gadget_Italic, 510, 160, 50, 20, "Italic")
          GadgetToolTip(#Gadget_Italic, "Check this to use italic labels")

          Frame3DGadget(#Gadget_CellHeight + 100, 340, 10, 100, 60, "CellHeight", 0)
          StringGadget(#Gadget_CellHeight, 360, 30, 60, 20, Str(CellHeight))
          GadgetToolTip(#Gadget_CellHeight, "Enter subcells minimum height")

          Frame3DGadget(#Gadget_CellWidth + 100, 450, 10, 100, 60, "CellWidth", 0)
          StringGadget(#Gadget_CellWidth, 470, 30, 60, 20, Str(CellWidth))
          GadgetToolTip(#Gadget_CellWidth, "Enter cells width")

          Frame3DGadget(#Gadget_Color1 + 100, 100, 80, 80, 60, "BG1", 0)
          ButtonImageGadget(#Gadget_Color1, 120, 100, 40, 20, MyLabeledImage(1, 40, 20, Color1, 0, "", "", 0))
          GadgetToolTip(#Gadget_Color1, "Click here to change first background color (actually" + RGB2DecList(Color1) + ")")

          Frame3DGadget(#Gadget_Color2 + 100, 180, 80, 80, 60, "BG2", 0)
          ButtonImageGadget(#Gadget_Color2, 200, 100, 40, 20, MyLabeledImage(2, 40, 20, Color2, 0, "", "", 0))
          GadgetToolTip(#Gadget_Color2, "Click here to change second background color (actually" + RGB2DecList(Color2) + ")")

          Frame3DGadget(#Gadget_TextColor1 + 100, 260, 80, 80, 60, "TC1", 0)
          ButtonImageGadget(#Gadget_TextColor1, 280, 100, 40, 20, MyLabeledImage(3, 40, 20, TextColor1, 0, "", "", 0))
          GadgetToolTip(#Gadget_TextColor1, "Click here to change first text color (actually" + RGB2DecList(TextColor1) + ")")

          Frame3DGadget(#Gadget_TextColor2 + 100, 340, 80, 80, 60, "TC2", 0)
          ButtonImageGadget(#Gadget_TextColor2, 360, 100, 40, 20, MyLabeledImage(4, 40, 20, TextColor2, 0, "", "", 0))
          GadgetToolTip(#Gadget_TextColor2, "Click here to change second text color (actually" + RGB2DecList(TextColor2) + ")")
          
          Frame3DGadget(#Gadget_2StateButton1 + 100, 420, 80, 120, 60, "Font", 0)
          ButtonImageGadget(#Gadget_2StateButton1, 430, 95, 100, 30, MyLabeledImage(5, 100, 30, Color1, TextColor1, "Text font", CellFont, CellFontSize))
          GadgetToolTip(#Gadget_2StateButton1, "Click here to change text font (actually " + CellFont + " " + Str(CellFontSize) + ")")

          Frame3DGadget(#Gadget_2StateButton2 + 100, 420, 80, 120, 60, "Font", 0)
          ButtonImageGadget(#Gadget_2StateButton2, 430, 95, 100, 30, MyLabeledImage(6, 100, 30, Color2, TextColor2, "Text font", CellFont, CellFontSize))
          GadgetToolTip(#Gadget_2StateButton2, "Click here to change text font (actually " + CellFont + " " + Str(CellFontSize) + ")")
          
          HideGadget(#Gadget_2StateButton2, 1)

      EndIf
      
      SetWindowCallback(@MyWindowCallback())

      Repeat
        Select WaitWindowEvent()
          Case #PB_EventCloseWindow
            Quit = #TRUE
          Case #PB_EventMenu
            Select EventMenuID()
              Case 99
                Quit = #TRUE
            EndSelect
          Case #PB_EventGadget
            Select EventGadgetID()
              Case #Gadget_Color1                                              ; First background color
                FreeGadget(#Gadget_Color1)
                Color1 = MyColorRequester(Color1)
                ButtonImageGadget(#Gadget_Color1, 120, 100, 40, 20, MyLabeledImage(1, 40, 20, Color1, 0, "", "", 0))
                GadgetToolTip(#Gadget_Color1, "Click here to change first background color (actually" + RGB2DecList(Color1) + ")")
              Case #Gadget_Color2                                              ; Second background color
                FreeGadget(#Gadget_Color2)
                Color2 = MyColorRequester(Color2)
                ButtonImageGadget(#Gadget_Color2, 200, 100, 40, 20, MyLabeledImage(2, 40, 20, Color2, 0, "", "", 0))
                GadgetToolTip(#Gadget_Color2, "Click here to change second background color (actually" + RGB2DecList(Color2) + ")")
              Case #Gadget_Bold                                                ; Bold CheckBox
                CellFontBold = GetGadgetState(#Gadget_Bold)
              Case #Gadget_Italic                                              ; Italic CheckBox
                CellFontItalic = GetGadgetState(#Gadget_Italic)
              Case #Gadget_CellHeight                                          ; CellHeight StringGadget
                CellHeight = Val(GetGadgetText(#Gadget_CellHeight))
              Case #Gadget_CellWidth                                           ; CellWidth StringGadget
                CellWidth = Val(GetGadgetText(#Gadget_CellWidth))
              Case #Gadget_TextColor1                                          ; First Text Color
                FreeGadget(#Gadget_TextColor1)
                TextColor1 = MyColorRequester(TextColor1)
                ButtonImageGadget(#Gadget_TextColor1, 280, 100, 40, 20, MyLabeledImage(3, 40, 20, TextColor1, 0, "", "", 0))
                GadgetToolTip(#Gadget_TextColor1, "Click here to change first text color (actually" + RGB2DecList(TextColor1) + ")")
              Case #Gadget_TextColor2                                          ; Second Text Color
                FreeGadget(#Gadget_TextColor2)
                TextColor2 = MyColorRequester(TextColor2)
                ButtonImageGadget(#Gadget_TextColor2, 360, 100, 40, 20, MyLabeledImage(4, 40, 20, TextColor2, 0, "", "", 0))
                GadgetToolTip(#Gadget_TextColor2, "Click here to change first text color (actually" + RGB2DecList(TextColor2) + ")")
              Case #Gadget_2StateButton2                                       ; Font selector
                Result = FontRequester(CellFont, CellFontSize, 0)
                If SelectedFontStyle() & #PB_Font_Bold
                    CellFontBold = #TRUE
                    CellFont = CellFont + " Bold"
                    SetGadgetState(#Gadget_Bold, CellFontBold)
                  Else
                    CellFontBold = #FALSE
                EndIf
                If SelectedFontStyle() & #PB_Font_Italic
                    CellFontItalic = #TRUE
                    CellFont = CellFont + " Italic"
                    SetGadgetState(#Gadget_Italic, CellFontItalic)
                  Else
                    CellFontItalic = #FALSE
                EndIf
                CellFont = SelectedFontName()
                CellFontSize = SelectedFontSize()
            EndSelect
            Refresh2StateButton(CellWidth, CellHeight, Color1, TextColor1, Color2, TextColor2, CellFont, CellFontSize, CellFontBold, CellFontItalic)
        EndSelect
      Until Quit
  EndIf
End
Francois Weil
14, rue Douer
F64100 Bayonne

Posted: Fri Oct 25, 2002 7:57 am
by BackupUser
Restored from previous forum. Originally posted by fweil.

...,

Anyway, you know what ? I am happy.

I got my second star with this post

Francois Weil
14, rue Douer
F64100 Bayonne

Posted: Fri Oct 25, 2002 3:27 pm
by BackupUser
Restored from previous forum. Originally posted by Andre.

Small suggestion: you should check the CellHeight and CellWidth StringGadgets, if the given value is smaller than 1 (0 and negative values give an error)...

Regards
André

*** German PureBasic Support ***

Posted: Fri Oct 25, 2002 4:30 pm
by BackupUser
Restored from previous forum. Originally posted by fweil.

OK thanks Andre

Francois Weil
14, rue Douer
F64100 Bayonne

Posted: Fri Oct 25, 2002 6:55 pm
by BackupUser
Restored from previous forum. Originally posted by MrVainSCL.

Hi
Here is a fixed version (check for width and heigh) ... You all where faster, hehe...

Original Code by: fweil

Code: Select all

;
; Gadgets numbers defined using constants for readability
;
#Gadget_Color1 = 105
#Gadget_Color2 = 106
#Gadget_Bold = 111
#Gadget_Italic = 112
#Gadget_CellHeight = 122
#Gadget_CellWidth = 123
#Gadget_TextColor1 = 131
#Gadget_TextColor2 = 132
#Gadget_2StateButton1 = 133
#Gadget_2StateButton2 = 134

;
; onMouseOver2StateButton is a signal set to #TRUE when the mouse passes over the 2 states button
;
onMouseOver2StateButton.l

Global onMouseOver2StateButton

Procedure.l IMin(a.l, b.l)
  If a  0 And Width.l > 0
    ImageID.l = CreateImage(ImageNumber, Width, Height)
    StartDrawing(ImageOutput())
      Box(0, 0, Width, Height, Color)
      If Label  ""
        FrontColor(Red(TextColor), Green(TextColor), Blue(TextColor))
        DrawingFont(LoadFont(10, Font, FontSize))
        DrawingMode(1)
        If TextLength(Label)  0
    ButtonImageGadget(#Gadget_2StateButton1, 480 - IMin(CellWidth, MaxWidth) / 2, MaxWidth - IMin(CellHeight, MaxHeight) / 2, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), MyLabeledImage(5, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), Color1, TextColor1, "Text font", CellFont + Bold + Italic, CellFontSize))
  EndIf
  
  GadgetToolTip(#Gadget_2StateButton1, "Click here to change text font (actually " + CellFont + Bold + Italic + " " + Str(CellFontSize) + ")")
  FreeGadget(#Gadget_2StateButton2)
  ButtonImageGadget(#Gadget_2StateButton2, 480 - IMin(CellWidth, MaxWidth) / 2, MaxWidth - IMin(CellHeight, MaxHeight) / 2, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), MyLabeledImage(6, IMin(CellWidth, MaxWidth), IMin(CellHeight, MaxHeight), Color2, TextColor2, "Text font", CellFont + Bold + Italic, CellFontSize))
  GadgetToolTip(#Gadget_2StateButton2, "Click here to change text font (actually " + CellFont + Bold + Italic + " " + Str(CellFontSize) + ")")
  HideGadget(#Gadget_2StateButton2, 1)
EndProcedure

;
; Main starts here
;

  Quit.l = #FALSE

  WindowXSize.l = 640
  WindowYSize.l = 280
  Color1.l = $804000
  Color2.l = $007D7D
  TextColor1.l = $80FFFF
  TextColor2.l = $FF0000
  CellHeight.l = 30
  Centered.l = #TRUE
  CellWidth.l = 160
  CellFont.s = "Verdana"
  CellFontSize.l = 12
  
  hWnd.l = OpenWindow(0, 200, 200, WindowXSize, WindowYSize, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar, "F.Weil - Font selection and decorated buttons")
  If hWnd
      AddKeyboardShortcut(0, #PB_Shortcut_Escape, 99)
      
      LoadFont(0, "Verdana", 8)
      
      If CreateGadgetList(WindowID())

          SetGadgetFont(FontID())
          
          CheckBoxGadget(#Gadget_Bold, 430, 160, 50, 20, "Bold")
          GadgetToolTip(#Gadget_Bold, "Check this to use bold labels")
          
          CheckBoxGadget(#Gadget_Italic, 510, 160, 50, 20, "Italic")
          GadgetToolTip(#Gadget_Italic, "Check this to use italic labels")

          Frame3DGadget(#Gadget_CellHeight + 100, 340, 10, 100, 60, "CellHeight", 0)
          StringGadget(#Gadget_CellHeight, 360, 30, 60, 20, Str(CellHeight))
          GadgetToolTip(#Gadget_CellHeight, "Enter subcells minimum height")

          Frame3DGadget(#Gadget_CellWidth + 100, 450, 10, 100, 60, "CellWidth", 0)
          StringGadget(#Gadget_CellWidth, 470, 30, 60, 20, Str(CellWidth))
          GadgetToolTip(#Gadget_CellWidth, "Enter cells width")

          Frame3DGadget(#Gadget_Color1 + 100, 100, 80, 80, 60, "BG1", 0)
          ButtonImageGadget(#Gadget_Color1, 120, 100, 40, 20, MyLabeledImage(1, 40, 20, Color1, 0, "", "", 0))
          GadgetToolTip(#Gadget_Color1, "Click here to change first background color (actually" + RGB2DecList(Color1) + ")")

          Frame3DGadget(#Gadget_Color2 + 100, 180, 80, 80, 60, "BG2", 0)
          ButtonImageGadget(#Gadget_Color2, 200, 100, 40, 20, MyLabeledImage(2, 40, 20, Color2, 0, "", "", 0))
          GadgetToolTip(#Gadget_Color2, "Click here to change second background color (actually" + RGB2DecList(Color2) + ")")

          Frame3DGadget(#Gadget_TextColor1 + 100, 260, 80, 80, 60, "TC1", 0)
          ButtonImageGadget(#Gadget_TextColor1, 280, 100, 40, 20, MyLabeledImage(3, 40, 20, TextColor1, 0, "", "", 0))
          GadgetToolTip(#Gadget_TextColor1, "Click here to change first text color (actually" + RGB2DecList(TextColor1) + ")")

          Frame3DGadget(#Gadget_TextColor2 + 100, 340, 80, 80, 60, "TC2", 0)
          ButtonImageGadget(#Gadget_TextColor2, 360, 100, 40, 20, MyLabeledImage(4, 40, 20, TextColor2, 0, "", "", 0))
          GadgetToolTip(#Gadget_TextColor2, "Click here to change second text color (actually" + RGB2DecList(TextColor2) + ")")
          
          Frame3DGadget(#Gadget_2StateButton1 + 100, 420, 80, 120, 60, "Font", 0)
          ButtonImageGadget(#Gadget_2StateButton1, 430, 95, 100, 30, MyLabeledImage(5, 100, 30, Color1, TextColor1, "Text font", CellFont, CellFontSize))
          GadgetToolTip(#Gadget_2StateButton1, "Click here to change text font (actually " + CellFont + " " + Str(CellFontSize) + ")")

          Frame3DGadget(#Gadget_2StateButton2 + 100, 420, 80, 120, 60, "Font", 0)
          ButtonImageGadget(#Gadget_2StateButton2, 430, 95, 100, 30, MyLabeledImage(6, 100, 30, Color2, TextColor2, "Text font", CellFont, CellFontSize))
          GadgetToolTip(#Gadget_2StateButton2, "Click here to change text font (actually " + CellFont + " " + Str(CellFontSize) + ")")
          
          HideGadget(#Gadget_2StateButton2, 1)

      EndIf
      
      SetWindowCallback(@MyWindowCallback())

      Repeat
        Select WaitWindowEvent()
          Case #PB_EventCloseWindow
            Quit = #TRUE
          Case #PB_EventMenu
            Select EventMenuID()
              Case 99
                Quit = #TRUE
            EndSelect
          Case #PB_EventGadget
            Select EventGadgetID()
              Case #Gadget_Color1                                              ; First background color
                FreeGadget(#Gadget_Color1)
                Color1 = MyColorRequester(Color1)
                ButtonImageGadget(#Gadget_Color1, 120, 100, 40, 20, MyLabeledImage(1, 40, 20, Color1, 0, "", "", 0))
                GadgetToolTip(#Gadget_Color1, "Click here to change first background color (actually" + RGB2DecList(Color1) + ")")
              Case #Gadget_Color2                                              ; Second background color
                FreeGadget(#Gadget_Color2)
                Color2 = MyColorRequester(Color2)
                ButtonImageGadget(#Gadget_Color2, 200, 100, 40, 20, MyLabeledImage(2, 40, 20, Color2, 0, "", "", 0))
                GadgetToolTip(#Gadget_Color2, "Click here to change second background color (actually" + RGB2DecList(Color2) + ")")
              Case #Gadget_Bold                                                ; Bold CheckBox
                CellFontBold = GetGadgetState(#Gadget_Bold)
              Case #Gadget_Italic                                              ; Italic CheckBox
                CellFontItalic = GetGadgetState(#Gadget_Italic)
              Case #Gadget_CellHeight                                          ; CellHeight StringGadget
                CellHeight = Val(GetGadgetText(#Gadget_CellHeight))
          ;      If CellHeight = 
PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten

Posted: Fri Oct 25, 2002 7:06 pm
by BackupUser
Restored from previous forum. Originally posted by MrVainSCL.

Hi
I am not sure, but seems that italic does not work here on my system!?
The font is always ||| instead of ///

PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten