Text and image inside a gadget?

Just starting out? Need help? Post your questions and find answers here.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Text and image inside a gadget?

Post by doctorized »

I want to make an app that will send emails to a list of recipients. The user will have a white space where will be able to write text and upply some basic formatting (font mane, size, bold or underline), add an image if necessary, and a link to a webpage or even an unsubscribe option. I guess I know how to create the code to make and send the email with TLS and so on, I have no idea what gadget to use for email preparing. What gadget can have all the above mentioned and formatted, text, images and links? A webgadget would be nice as I could take the code directrly from it and use it but how can I add stuff in it?
flashbob
User
User
Posts: 92
Joined: Sat May 11, 2024 4:04 pm

Re: Text and image inside a gadget?

Post by flashbob »

I think you could use the webgadget. To format text and insert images, you could read the current code and add/insert the web code (tags, images, etc.). You can display the code again in the WebGadget after adding html code.

Here is a small example to write/format an HTML email. You have to double click on the word to format it and of course, you still have to adapt the code, for example to include images:

Code: Select all

EnableExplicit
Enumeration FormGadget
  #web_gadget
  #Btn_bold
  #Btn_italics
  #Btn_underline
  #Btn_read_html
EndEnumeration

Define event
Define HtmlCode$= "<html><body>" +
                  "<div contenteditable>" + 
                  "Hello World. Try to edit / format Text ..." +
                  "<br><br><br><br><br>" + 
                  "</div>" +
                  "</body></html>"

Procedure.s get_html_Text(WebGadget)            ; Read (changed) html code
  Protected sResult.s
  
  ; Workaround MAC / Windows
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    
    Protected URL$, NSString
    ; --- examples to read html code
    ; URL$ = "document.getElementsByTagName('body')[0].innerHTML;"   ; get inner HTML (<body> ... </body>)
    ; URL$ = "document.getElementsByTagName('div')[0].innerHTML;"    ; get inner HTML (<div> ... </div>)
    URL$     = "document.documentElement.outerHTML;"                 ; get entire document ( <html> ... </html>)
    NSString = CocoaMessage(0, GadgetID(#web_gadget), "stringByEvaluatingJavaScriptFromString:$", @URL$)
    sResult  = PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
    
  CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows
    
    ; workaround Win - thx to breeze4me   
    Protected *OuterHTML, Browser.IWebBrowser2 = GetWindowLong_(GadgetID(WebGadget), #GWL_USERDATA)
    Protected DocumentDispatch.IDispatch, DocElement.IHTMLElement, Document3.IHTMLDocument3
    If Browser
      If Browser\get_Document(@DocumentDispatch) = #S_OK And DocumentDispatch
        If DocumentDispatch\QueryInterface(?IID_IHTMLDocument3, @Document3) = #S_OK And Document3
          If Document3\get_documentElement(@DocElement) = #S_OK And DocElement
            If DocElement\get_outerHTML(@*OuterHTML) = #S_OK
              If *OuterHTML
                sResult = PeekS(*OuterHTML)
                SysFreeString_(*OuterHTML)
              EndIf
            EndIf
            DocElement\Release()
          EndIf
          Document3\Release()
        EndIf
        DocumentDispatch\Release()
      EndIf
    EndIf
 
    DataSection
      IID_IHTMLDocument3:
      Data.l $3050F485 
      Data.w $98B5,$11CF
      Data.b $BB,$82,$00,$AA,$00,$BD,$CE,$0B
    EndDataSection
  CompilerEndIf
  
  ProcedureReturn sResult
EndProcedure

Procedure format_Text(tag_begin$, tag_end$)     ; format selected text
  Protected selected_text$ = GetGadgetItemText(#web_gadget, #PB_Web_SelectedText)
  Protected html_code$
  Protected new_code$      = tag_begin$ + selected_text$ + tag_end$
  Protected new_html_code$ = ""
  Protected start_pos, text_pos
  
  ; read changed text (Mac/Win)
  html_code$ = get_html_Text(#web_gadget)
  ; start position of 'visible' text
  start_pos = FindString(html_code$, "<div contenteditable>")+21-1
  ; start position of selected text
  text_pos = FindString(html_code$, selected_text$, start_pos)
  
  ; insert tags
  new_html_code$ = ReplaceString(html_code$, selected_text$, new_code$, #PB_String_CaseSensitive, text_pos,1)
  
  ; refresh inline Text
  SetGadgetItemText(#web_gadget, #PB_Web_HtmlCode, new_html_code$)
EndProcedure

OpenWindow(0, 0, 0, 600, 400, "Editable HTML Content", #PB_Window_SystemMenu)
ButtonGadget(#Btn_read_html,  5, 5, 85, 25, "get html")
ButtonGadget(#Btn_bold,      95, 5, 85, 25, "bold")
ButtonGadget(#Btn_italics,  185, 5, 85, 25, "italics")
ButtonGadget(#Btn_underline,275, 5, 85, 25, "underline")

WebGadget(#web_gadget, 0, 35, 600, 400, "")
SetGadgetItemText(#web_gadget, #PB_Web_HtmlCode, HtmlCode$)

Repeat
  event = WaitWindowEvent() 
  Select event   
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Btn_bold      : format_Text("<b>","</b>")
        Case #Btn_italics   : format_text("<i>","</i>")
        Case #Btn_underline : format_text("<u>","</u>")
        Case #Btn_read_html : Debug get_html_Text(#web_gadget)
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow
Is that what you were looking for?
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Text and image inside a gadget?

Post by doctorized »

flashbob wrote: Mon Sep 23, 2024 9:16 pm Is that what you were looking for?
Yes, I am looking for something like that! I tried it and works only with english chars. With greek one it shows the text truncated after applying formatting. You can try it by yourself with the phrase: Μία πρόταση για δοκιμή. (= One sentence for testing.)
I can use your code to add links too, maybe images, I will give it a try the next few days. I guess undo and redo are needed to complete it.
Post Reply