MessageRequester_EX_DrawText_EX_based.pb (updated on 19th Nov. 2018)
Code: Select all
EnableExplicit
; === MessageRequester_EX ===
; Same the PB function, but very enhanced with auto resizing - Without OS Icon output
; But you can include any images or use DrawTextCreateImage_EX (_BF) - Or very nice, images with FitImage_BF
; Works also with GFX-Wizzard_BF functions and the enhanced DrawText_BF
; === Module DrawText_EX ===
DeclareModule DrawText_EX
Declare DrawText_EX(txt_x, txt_y, txt$, f_col=#White, b_col=#Black, flag=0, mode=0, stretch_x=0, stretch_y=0)
; All OS - Author(C) : Werner Albus - www.nachtoptik.de - www.quick-aes-256.de
; Flags : Text center=1 - Text right=2 - Text vertical=3
; Modes : Revers text mode=1 - RTL text mode=2 - Give only back the needed width mode=3 , height mode=4
; Mono line output give back the xx output coordinate - Multi line output give back the yy output coordinate
; For stretched text on a textured background often DrawingMode (#PB_2DDrawing_Transparent) is needed
; Create a image from the output, for post processing or using as image, give back a image (ID) - Padding -1 is automatic padding
Declare DrawTextCreateImage_EX(txt$, f_col=#White, b_col=#Black, flag=0, mode=0, font_ID=0, stretch_x=0, stretch_y=0, padding=0)
Declare PresetLineWidth_EX(max_1_) ; Preset the min output length for easy centric or right align outputs - Deactivate = 0
; Important for permanent changed outputs
Declare DrawText_EX_AutoBox(padding_=-1, ; Automatic padding = -1 - Deactivate = -2
box_color_=0,
offset_x_=0,
offset_y_=0,
offset_xx_=0,
offset_yy_=0)
Declare DrawText_EX_Box(activate_=1, box_color_1_=0, offset_x_1=0, offset_y_1_=0, width_=0, height_=0) ; Activate = 1 - Deactivate = 0
EndDeclareModule
Module DrawText_EX
; (C) Werner Albus - www.nachtoptik.de
EnableExplicit
Global max_1, padding=-2, box_color, offset_x, offset_y, offset_xx, offset_yy
Global activate, box_color_1, offset_x_1, offset_y_1, width, height, from_image, image_color
Procedure DrawText_EX(txt_x, txt_y, txt$, f_col=#White, b_col=#Black, flag=0, mode=0, stretch_x=0, stretch_y=0)
If txt$="" : ProcedureReturn -1 : EndIf
Protected i1=TextHeight(" ")+stretch_y, i2, i3, i4, i5, i6, i7, padding_1
Protected result, max, max_len, max_2, max_3, txt_x_0=txt_x, txt_y_0=txt_y, amount_lines
Protected txt1$, chr$, ln$, ln1$
Protected Dim f1$(0), Dim f2$(0)
txt1$=ReplaceString(ReplaceString(ReplaceString(txt$, #CRLF$, #LF$), #CR$, #LF$), #LFCR$, #LF$)
i7=Len(txt1$)
If flag<>3 And FindString(txt1$, #LF$)
amount_lines=CountString(txt1$, #LF$)+1
txt_y-i1
For result=1 To i7
ln$=StringField(txt1$, result, #LF$)
i4=Len(ln$)
If i4
max_2=TextWidth(ln$)+stretch_x*i4
If max_2>max_3
max_3=max_2
ln1$=ln$
i2+i1
max=TextWidth(ln$)+i4
max_len=i4
EndIf
EndIf
Next
If max_1>0
max=max_1
EndIf
If flag=1 ; Centered
txt_x+max_len*(stretch_x>>1)-max_len>>1
ElseIf flag=2 ; Right
txt_x+max_len*(stretch_x)-max_len
EndIf
EndIf
If mode=3 And flag<>3
If i2
If Not stretch_x
ProcedureReturn TextWidth(ln1$)+2
Else
i6=Len(ln1$)
For i4=1 To i6
i5+TextWidth(Mid(ln1$, i4, 1))
Next
ProcedureReturn i5+stretch_x*i6+stretch_x/12
EndIf
Else
If Not stretch_x
ProcedureReturn TextWidth(txt1$)+2
Else
For i4=1 To i7
i5+TextWidth(Mid(txt1$, i4, 1))
Next
ProcedureReturn i5+stretch_x*Len(txt1$)+stretch_x/12
EndIf
EndIf
EndIf
If mode=4 And flag<>3
If i2
ProcedureReturn amount_lines*(i1+1)-stretch_y
Else
ProcedureReturn i1+1-stretch_y
EndIf
EndIf
Macro first_char_width
i5=0
For i4=1 To i7
i6=TextWidth(Mid(txt1$, i4, 1))
If i6>i5
i5=i6
EndIf
Next
EndMacro
If padding=-1
padding_1=TextWidth(Space(3))
Else
padding_1=padding
EndIf
If padding<-1
padding_1=0
EndIf
If mode=3 And flag=3
first_char_width
ProcedureReturn i5
EndIf
If mode=4 And flag=3
ProcedureReturn i1*i7-stretch_y
EndIf
If activate=1
Box(txt_x_0+offset_x_1,
txt_y_0+offset_y_1,
width,
height,
box_color_1)
EndIf
If Not from_image
If padding<>-2
If i2
Box(txt_x_0-padding_1+offset_x,
txt_y_0-padding_1+offset_y,
max+max_len*(stretch_x)-stretch_x-max_len+padding_1<<1+offset_xx,
amount_lines*i1-stretch_y+padding_1<<1+offset_yy,
box_color)
Else
If flag<>3
Box(txt_x_0-padding_1+offset_x,
txt_y_0-padding_1+offset_y,
TextWidth(txt1$)+stretch_x*(i7-1)+padding_1<<1+offset_xx,
i1+padding_1<<1+offset_yy,
box_color)
Else ; Vertical box
first_char_width
Box(txt_x_0-padding_1+offset_x>>1,
txt_y_0-padding_1+offset_y,
i5+padding_1<<1+offset_xx,
i1*i7+padding_1<<1+offset_yy,
box_color)
EndIf
EndIf
EndIf
Else
DrawingMode(#PB_2DDrawing_Transparent)
EndIf
Macro spread_x(txt_x, txt_y, ln)
i6=0
i4=Len(ln)
For i5=1 To Len(ln)
ln1$=Mid(ln, i5, 1)
If flag=1 And i2
DrawText(txt_x+i6-i4*stretch_x>>1, txt_y, ln1$, f_col, b_col)
ElseIf flag=2 And i2
DrawText(txt_x+i6-i4*stretch_x, txt_y, ln1$, f_col, b_col)
Else
DrawText(txt_x+i6, txt_y, ln1$, f_col, b_col)
EndIf
i6+TextWidth(ln1$)+stretch_x
Next
EndMacro
Macro rtl(txt)
i5=0
i3=Len(txt)
For i4=1 To i3
If Mid(txt, i4, 1)=" "
i6+1
EndIf
ln1$=StringField(txt, i4, " ")
If ln1$<>""
ReDim f1$(i5)
ReDim f2$(i5)
f1$(i5)=ln1$
f2$(i5)=ReverseString(ln1$)
i5+1
EndIf
Next
txt=ReverseString(txt)
For i4=0 To i5-1
txt=ReplaceString(txt, f2$(i4), f1$(i4))
Next
i3=0 : i5=0 : i6=0
EndMacro
Macro txt_out
If mode=1
ln$=ReverseString(ln$)
ElseIf mode=2
rtl(ln$)
EndIf
If flag=2 ; Right
If stretch_x
spread_x(txt_x+max-TextWidth(ln$), txt_y+i2, ln$)
Else
DrawText(txt_x+max-TextWidth(ln$), txt_y+i2, ln$, f_col, b_col)
EndIf
ElseIf flag=1 ; Centered
If stretch_x
spread_x(txt_x+max>>1-TextWidth(ln$)>>1, txt_y+i2, ln$)
Else
DrawText(txt_x+max>>1-TextWidth(ln$)>>1, txt_y+i2, ln$, f_col, b_col) : EndIf
Else ; Left
If stretch_x
spread_x(txt_x, txt_y+i2, ln$)
Else
DrawText(txt_x, txt_y+i2, ln$, f_col, b_col)
EndIf
EndIf
EndMacro
If flag=3 And mode=1
rtl(txt1$)
EndIf
If flag=3 ; Vertical
txt1$=ReplaceString(txt1$, #LF$, " ")
If mode
txt1$=ReverseString(txt1$)
EndIf
For result=1 To i7
chr$=Mid(txt1$, result, 1)
first_char_width
DrawText(i5>>1+txt_x-TextWidth(chr$)>>1, txt_y+i3, chr$, f_col, b_col)
i3+i1
Next
ProcedureReturn txt_y+i3
Else ; Horizontal
If i2
i2=0 : ln$=""
For result=1 To i7
chr$=Mid(txt1$, result, 1)
If chr$=#LF$
i2+i1
txt_out
ln$=""
Else
ln$+chr$
EndIf
Next
i2+i1
txt_out
ProcedureReturn txt_y+i2+i1
Else ; One line
If mode=1
txt$=ReverseString(txt$)
ElseIf mode=2
rtl(txt$)
EndIf
If stretch_x
spread_x(txt_x, txt_y, txt$)
Else
DrawText(txt_x, txt_y, txt$, f_col, b_col)
EndIf
EndIf
EndIf
If i6
ProcedureReturn txt_x+i6
Else
ProcedureReturn txt_x+TextWidth(txt$)
EndIf
EndProcedure
Procedure DrawTextCreateImage_EX(txt$, f_col=#White, b_col=#Black, flag=0, mode=0, font_ID=0, stretch_x=0, stretch_y=0, padding=0)
If txt$="" : ProcedureReturn -2 : EndIf
Protected image_ID=CreateImage(#PB_Any, 1, 1)
If Not image_ID : ProcedureReturn -3 : EndIf
If Not StartDrawing(ImageOutput(image_ID)) ; Get the needed width and length for the output
FreeImage(image_ID)
ProcedureReturn -4
EndIf
If IsFont(font_ID) : DrawingFont(FontID(font_ID)) : EndIf
If padding=-1
padding=TextWidth(Space(3))
EndIf
If max_1>0
Protected output_width=max_1 ; Get the needed width
Else
output_width=DrawText_EX(0, 0, txt$, 0, 0, flag, 3, stretch_x, stretch_y)-stretch_x ; Get the needed width
EndIf
Protected output_height=DrawText_EX(0, 0, txt$, 0, 0, flag, 4, stretch_x, stretch_y)-stretch_y ; Get the needed height
StopDrawing()
If mode=3
ProcedureReturn output_width+padding
EndIf
If mode=4
ProcedureReturn output_height+padding
EndIf
If Not ResizeImage(image_ID, output_width+padding<<1, output_height+padding<<1)
FreeImage(image_ID)
ProcedureReturn -5
EndIf
If Not StartDrawing(ImageOutput(image_ID)) ; Draw the text in the image
FreeImage(image_ID)
ProcedureReturn -6
EndIf
Box(0, 0, output_width+padding<<1, output_height+padding<<1, b_col)
If IsFont(font_ID) : DrawingFont(FontID(font_ID)) : EndIf
Protected old_activate=activate
activate=0
from_image=1
image_color=b_col
DrawText_EX(padding, padding, txt$, f_col, 0, flag, mode, stretch_x, stretch_y)
activate=old_activate
from_image=0
StopDrawing()
ProcedureReturn image_ID
EndProcedure
Procedure PresetLineWidth_EX(max_1_)
max_1=max_1_
EndProcedure
Procedure DrawText_EX_AutoBox(padding_=-1,
box_color_=0,
offset_x_=0,
offset_y_=0,
offset_xx_=0,
offset_yy_=0)
padding=padding_
box_color=box_color_
offset_x=offset_x_
offset_y=offset_y_
offset_xx=offset_xx_
offset_yy=offset_yy_
EndProcedure
Procedure DrawText_EX_Box(activate_=1, box_color_1_=0, offset_x_1_=0, offset_y_1_=0, width_=0, height_=0)
activate=activate_
box_color_1=box_color_1_
offset_x_1=offset_x_1_
offset_y_1=offset_y_1_
width=width_
height=height_
EndProcedure
EndModule
UseModule DrawText_EX
DeclareModule MessageRequester_EX
EnableExplicit
UseModule DrawText_EX
Declare MessageRequester_EX(title_line$="",
text$="",
flags.a=#PB_MessageRequester_Ok,
text_button_yes$="OK",
text_button_no$="No",
text_button_cancel$="Cancel",
text_color=0,
text_background_color=$F0F0F0,
background_color=$F0F0F0,
buttons_width=160,
buttons_height=40,
text_font_ID=0, ; Presetted is font ID = 0
button_font_ID=0, ; Presetted is font ID = 0
image_ID=-1,
padding_vertical=5)
EndDeclareModule
UseModule MessageRequester_EX
Module MessageRequester_EX
; (C) Werner Albus - www.nachtoptik.de
Procedure MessageRequester_EX(title_line$="",
text$="",
flags.a=#PB_MessageRequester_Ok,
text_button_yes$="OK",
text_button_no$="No",
text_button_cancel$="Cancel",
text_color=0,
text_background_color=$F0F0F0,
background_color=$F0F0F0,
buttons_width=160,
buttons_height=40,
text_font_ID=0, ; Presetted is font ID = 0
button_font_ID=0, ; Presetted is font ID = 0
image_ID=-1,
padding_vertical=5)
padding_vertical+10
Protected image_width_text_image_ID=100, image_height_text_image_ID=100, image_width_image_ID, image_height_image_ID
Protected text_flag=#PB_Text_Center ; You can change
flags << 5 : flags >> 5
Protected window_ID, button_yes_ID, button_no_ID, button_cancel_ID, canvas_ID, text_image_ID, result
; Create a image from the output, for post processing or using as image, give back a image (ID) - Padding -1 is automatic padding
; Declare DrawTextCreateImage_EX(txt$, f_col=#White, b_col=#Black, flag=0, mode=0, font_ID=0, stretch_x=0, stretch_y=0, padding=0)
If text$<>""
text_image_ID=DrawTextCreateImage_EX(text$+#LF$, text_color, text_background_color, 1, 0, text_font_ID, 0, 0, -1)
EndIf
If IsImage(image_ID)
image_width_image_ID=ImageWidth(image_ID)
image_height_image_ID=ImageHeight(image_ID)
If image_width_image_ID>buttons_width
buttons_width=image_width_image_ID
EndIf
EndIf
If IsImage(text_image_ID)
image_width_text_image_ID=ImageWidth(text_image_ID)
image_height_text_image_ID=ImageHeight(text_image_ID)+image_height_image_ID
Else
image_width_text_image_ID=image_width_image_ID
image_height_text_image_ID=image_height_image_ID+padding_vertical
EndIf
If flags=#PB_MessageRequester_Ok
If image_width_text_image_ID<buttons_width+20
window_ID=OpenWindow(#PB_Any, 0, 0, buttons_width+20, image_height_text_image_ID+buttons_height+padding_vertical, " "+title_line$, #PB_Window_Tool|#PB_Window_ScreenCentered)
If Not IsWindow(window_ID) : ProcedureReturn -9 : EndIf
canvas_ID=CanvasGadget(#PB_Any, 0, 0, buttons_width+20, image_height_text_image_ID)
If Not IsGadget(canvas_ID) : ProcedureReturn -9 : EndIf
Else
window_ID=OpenWindow(#PB_Any, 0, 0, image_width_text_image_ID+20, image_height_text_image_ID+buttons_height+padding_vertical, " "+title_line$, #PB_Window_Tool|#PB_Window_ScreenCentered)
If Not IsWindow(window_ID) : ProcedureReturn -9 : EndIf
canvas_ID=CanvasGadget(#PB_Any, 0, 0, image_width_text_image_ID+20, image_height_text_image_ID)
If Not IsGadget(canvas_ID) : ProcedureReturn -9 : EndIf
EndIf
ElseIf flags=#PB_MessageRequester_YesNo
If image_width_text_image_ID<buttons_width*2+50
window_ID=OpenWindow(#PB_Any, 0, 0, buttons_width*2+50, image_height_text_image_ID+buttons_height+padding_vertical, " "+title_line$, #PB_Window_Tool|#PB_Window_ScreenCentered)
If Not IsWindow(window_ID) : ProcedureReturn -9 : EndIf
canvas_ID=CanvasGadget(#PB_Any, 0, 0, buttons_width*2+50, image_height_text_image_ID)
Else
window_ID=OpenWindow(#PB_Any, 0, 0, image_width_text_image_ID+50, image_height_text_image_ID+buttons_height+padding_vertical, " "+title_line$, #PB_Window_Tool|#PB_Window_ScreenCentered)
If Not IsWindow(window_ID) : ProcedureReturn -9 : EndIf
canvas_ID=CanvasGadget(#PB_Any, 0, 0, image_width_text_image_ID+50, image_height_text_image_ID)
If Not IsGadget(canvas_ID) : ProcedureReturn -9 : EndIf
EndIf
Else
If image_width_text_image_ID<buttons_width*3+60
window_ID=OpenWindow(#PB_Any, 0, 0, buttons_width*3+60, image_height_text_image_ID+buttons_height+padding_vertical, " "+title_line$, #PB_Window_Tool|#PB_Window_ScreenCentered)
If Not IsWindow(window_ID) : ProcedureReturn -9 : EndIf
canvas_ID=CanvasGadget(#PB_Any, 0, 0, buttons_width*3+60, image_height_text_image_ID)
Else
window_ID=OpenWindow(#PB_Any, 0, 0, image_width_text_image_ID+60, image_height_text_image_ID+buttons_height+padding_vertical, " "+title_line$, #PB_Window_Tool|#PB_Window_ScreenCentered)
If Not IsWindow(window_ID) : ProcedureReturn -9 : EndIf
canvas_ID=CanvasGadget(#PB_Any, 0, 0, image_width_text_image_ID+60, image_height_text_image_ID)
If Not IsGadget(canvas_ID) : ProcedureReturn -9 : EndIf
EndIf
EndIf
SetWindowColor(window_ID, background_color)
Protected temp_ID=CreateImage(#PB_Any, WindowWidth(window_ID), image_height_image_ID+image_height_text_image_ID+padding_vertical, 24, background_color)
If Not IsImage(temp_ID) : ProcedureReturn -4 : EndIf
If Not StartDrawing(ImageOutput(temp_ID)) : ProcedureReturn -4 : EndIf
If IsImage(image_ID)
DrawImage(ImageID(image_ID), WindowWidth(window_ID)/2-image_width_image_ID/2, 10)
EndIf
If IsImage(text_image_ID)
DrawImage(ImageID(text_image_ID), WindowWidth(window_ID)/2-image_width_text_image_ID/2, image_height_image_ID+padding_vertical)
EndIf
StopDrawing()
If Not StartDrawing(CanvasOutput(canvas_ID)) : ProcedureReturn -5 : EndIf
DrawImage(ImageID(temp_ID), 0, 0)
StopDrawing()
If IsFont(button_font_ID)
SetGadgetFont(#PB_Default, FontID(button_font_ID))
EndIf
button_yes_ID=ButtonGadget(#PB_Any, WindowWidth(window_ID)/2-buttons_width-buttons_width/2-10, WindowHeight(window_ID)-buttons_height-10, buttons_width, buttons_height, text_button_yes$, #PB_Button_MultiLine)
button_no_ID=ButtonGadget(#PB_Any, WindowWidth(window_ID)/2-buttons_width/2, WindowHeight(window_ID)-buttons_height-10, buttons_width, buttons_height, text_button_no$, #PB_Button_MultiLine)
button_cancel_ID=ButtonGadget(#PB_Any, WindowWidth(window_ID)/2+buttons_width/2+10, WindowHeight(window_ID)-buttons_height-10, buttons_width, buttons_height, text_button_cancel$, #PB_Button_MultiLine)
If IsFont(button_font_ID)
SetGadgetFont(#PB_Default, #PB_Default)
EndIf
If Not canvas_ID Or Not button_yes_ID Or Not button_no_ID Or Not button_cancel_ID
ProcedureReturn -9
EndIf
If flags=#PB_MessageRequester_Ok
HideGadget(button_no_ID, 1)
HideGadget(button_cancel_ID, 1)
ResizeGadget(button_yes_ID, WindowWidth(window_ID)/2-GadgetWidth(button_yes_ID)/2, #PB_Ignore, #PB_Ignore, #PB_Ignore)
ElseIf flags=#PB_MessageRequester_YesNo
HideGadget(button_cancel_ID, 1)
ResizeGadget(button_yes_ID, WindowWidth(window_ID)/2-GadgetWidth(button_yes_ID)-5, #PB_Ignore, #PB_Ignore, #PB_Ignore)
ResizeGadget(button_no_ID, WindowWidth(window_ID)/2+5, #PB_Ignore, #PB_Ignore, #PB_Ignore)
EndIf
Repeat
If WaitWindowEvent()=#PB_Event_Gadget
Select EventGadget()
Case button_yes_ID
result=#PB_MessageRequester_Yes
Case button_no_ID
result=#PB_MessageRequester_No
Case button_cancel_ID
result=#PB_MessageRequester_Cancel
EndSelect
EndIf
Until result
If IsImage(temp_ID)
FreeImage(temp_ID)
EndIf
If IsImage(text_image_ID)
FreeImage(text_image_ID)
EndIf
CloseWindow(window_ID)
ProcedureReturn result
EndProcedure
EndModule
Define title_line$="Message Requester"
Define text$="Because the OS message requesters look not good"+#LF$+#LF$+
"I made another one,"+#LF$+
"that can do much more"+#LF$+#LF$+
"GFX_Wizzard_BF"+#LF$+
"Simple a little more"
Define flags.a=#PB_MessageRequester_YesNo
Define text_button_yes$="OK"
Define text_button_no$="No"
Define text_button_cancel$="Cancel"
Define text_color=$FF
Define text_background_color=$D0D0D0
Define background_color=$F0F0F0
Define buttons_width=240
Define buttons_height=50
Define padding_vertical=15
Define text_font_ID=LoadFont(#PB_Any, "Arial", 18, #PB_Font_Bold)
Define button_font_ID=LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold)
Define headline_font_ID=LoadFont(#PB_Any, "Arial", 30, #PB_Font_Bold)
; Create a image from the output, for post processing or using as image, give back a image (ID) - Padding -1 is automatic padding
; Declare DrawTextCreateImage_EX(txt$, f_col=#White, b_col=#Black, flag=0, mode=0, font_ID=0, stretch_x=0, stretch_y=0, padding=0)
Define image_ID=DrawTextCreateImage_EX("STOP", #Black, #Red, 1, 0, headline_font_ID, 15, 0, -1) ; And stretch the created text a little (15)
; = Demo with DrawTextCreateImage =========================
MessageRequester_EX(title_line$,
text$,
flags.a,
text_button_yes$,
text_button_no$,
text_button_cancel$,
text_color,
text_background_color,
background_color,
buttons_width,
buttons_height,
text_font_ID,
button_font_ID,
image_ID,
padding_vertical)
; = Demo with a little Image =========================
Define image_ID=LoadImage(#PB_Any, #PB_Compiler_Home+"Examples/Sources/Data/PureBasicLogo.bmp")
Define text_font_ID=LoadFont(#PB_Any, "Arial", 18, #PB_Font_Bold)
Define flags.a=#PB_MessageRequester_Ok
Define text_button_yes$="OK"
Define text_button_no$="No"
Define text_button_cancel$="Cancel"
Define text_color=$FF
Define text_background_color=$FFFF
Define background_color=$FFFF
Define buttons_width=180
Define buttons_height=50
Define padding_vertical=0
MessageRequester_EX(title_line$,
text$,
flags.a,
text_button_yes$,
text_button_no$,
text_button_cancel$,
text_color,
text_background_color,
background_color,
buttons_width,
buttons_height,
text_font_ID,
button_font_ID,
image_ID,
padding_vertical)
; = Demo simple =========================================
MessageRequester_EX(title_line$, #LF$+text$+#LF$, #PB_MessageRequester_YesNoCancel, "Yeah", "Oh no", "So far")
; = Demo simple replace the PB MessageRequester =========
MessageRequester_EX(title_line$, #LF$+text$+#LF$, #PB_MessageRequester_Ok)