Force UTF8 in EditorGadget in Windows in ASCII compile mode

Share your advanced PureBasic knowledge/code with the community.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Force UTF8 in EditorGadget in Windows in ASCII compile mode

Post by Keya »

Im creating an ASCII app, but i wanted to be able to display some UTF8 text in a textbox. Am I correct in assuming that the only practical way to do this is to use Unicode compile? (aside from manually using the "W" wide-char versions of the WinAPI)

Anyway on another note, while experimenting I found that the EDITORGADGET (but not String or Text or any other gadgets) - and only in WINDOWS (didnt work in Linux or Mac but tested fine in XP) - allows you to display UTF8 from our ASCII-compiled PB program if we simply prepend the 3-byte UTF8 header.

The UTF8 header is simply: Chr($EF)+Chr($BB)+Chr($BF)

Put that at the start of your string and the EditorGadget in Windows will correctly display the UTF8. Neato!

Here is the test i put together to simply display "Trademark" with the trademark symbol between "Trade" and "mark", which unfortunately shows that only the EditorGadget has this feature.

The code is: sTxt.s = "Trade" + Chr($E2)+Chr($84)+Chr($A2) + "mark"
Those three bytes are UTF8 code for the ™ Trademark symbol - it cant be displayed in ASCII.
In ASCII it looks like this: Tradeâ„¢mark
In UTF8 it looks like this: Trade™mark

Image
The extra bytes before the "Trade" word are the 3-byte UTF8 header, which as you can see isn't shown in the EditorGadget which correctly decodes it as UTF8

Code: Select all

CompilerIf #PB_Compiler_Unicode = 1
  MessageRequester("Compiler Options Error", "This test is for ASCII mode not Unicode")
  End
CompilerEndIf


#UTF8MARKER = Chr($EF)+Chr($BB)+Chr($BF)

Enumeration FormWindow
  #Window_0
EndEnumeration

Enumeration FormGadget
  #String_0
  #Text_0
  #Option_0
  #Checkbox_0
  #Editor_0
  #Combo_0
  #Button_0
  #ListIcon_0
  #ListView_0
  #Tree_0
  #String_1
  #Text_1
  #Option_1
  #Checkbox_1
  #Editor_1
  #Combo_1
  #Button_1
  #ListIcon_1
  #ListView_1
  #Tree_1
  #Text_With
  #Text_Without
EndEnumeration


Procedure OpenWindow_0(x = 0, y = 0, width = 890, height = 270)
  
  sTxt.s = "Trade" + Chr($E2)+Chr($84)+Chr($A2) + "mark"

  OpenWindow(#Window_0, x, y, width, height, sTxt, #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(#String_0, 10, 40, 210, 20, sTxt)
  TextGadget(#Text_0, 10, 70, 210, 20, sTxt)
  OptionGadget(#Option_0, 10, 100, 210, 20, sTxt)
  CheckBoxGadget(#Checkbox_0, 10, 120, 210, 20, sTxt)
  EditorGadget(#Editor_0, 10, 10, 210, 20)
  ComboBoxGadget(#Combo_0, 10, 150, 210, 40)
  ButtonGadget(#Button_0, 10, 200, 120, 30, sTxt)
  ListIconGadget(#ListIcon_0, 240, 80, 190, 60, sTxt, 100)
  ListViewGadget(#ListView_0, 240, 160, 190, 50)
  TreeGadget(#Tree_0, 240, 10, 190, 50)
  AddGadgetItem(#ListView_0, -1, sTxt)
  AddGadgetItem(#Tree_0, -1, sTxt)
  AddGadgetItem(#ListIcon_0, -1, sTxt)
  AddGadgetItem(#Combo_0, -1, sTxt)
  SetGadgetText (#Editor_0, sTxt)
  
  
sTxt.s = #UTF8MARKER + "Trade" + Chr($E2)+Chr($84)+Chr($A2) + "mark"


  StringGadget(#String_1, 460, 40, 210, 20, sTxt)
  TextGadget(#Text_1, 460, 70, 210, 20, sTxt)
  OptionGadget(#Option_1, 460, 100, 210, 20, sTxt)
  CheckBoxGadget(#Checkbox_1, 460, 120, 210, 20, sTxt)
  EditorGadget(#Editor_1, 460, 10, 210, 20)
  ComboBoxGadget(#Combo_1, 460, 150, 210, 40)
  ButtonGadget(#Button_1, 460, 200, 120, 30, sTxt)
  ListIconGadget(#ListIcon_1, 690, 80, 190, 60, sTxt, 100)
  ListViewGadget(#ListView_1, 690, 160, 190, 50)
  TreeGadget(#Tree_1, 690, 10, 190, 50) 
  AddGadgetItem(#ListView_1, -1, sTxt)
  AddGadgetItem(#Tree_1, -1, sTxt)
  AddGadgetItem(#ListIcon_1, -1, sTxt)
  AddGadgetItem(#Combo_1, -1, sTxt)
  SetGadgetText (#Editor_1, sTxt)
  
  TextGadget(#Text_With, 110, 240, 210, 20, "NO UTF8 HEADER", #PB_Text_Center)
  TextGadget(#Text_Without, 540, 240, 210, 20, "WITH UTF8 HEADER", #PB_Text_Center)
EndProcedure


OpenWindow_0()



Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow