fltkfonts.bmx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. Strict
  2. Import MaxGUI.MaxGUI
  3. Import "fltkimports.bmx"
  4. Private
  5. Include "fltkdecls.bmx"
  6. Public
  7. Type TFLGUIFont Extends TGUIFont
  8. Field flfamily:TFLFontFamily
  9. Function LoadFont:TFLGUIFont( name$,height:Double,flags )
  10. Local tmpFLGUIFont:TFLGUIFont = New TFLGUIFont.SetFont(name,height,flags)
  11. tmpFLGUIFont.Initialize()
  12. Return tmpFLGUIFont
  13. EndFunction
  14. Method SetFont:TFLGUIFont( name$,height:Double,flags )
  15. Self.name = name
  16. Self.style = flags
  17. Self.size = height
  18. Return Self
  19. EndMethod
  20. Method CharWidth( charcode )
  21. Return 0
  22. EndMethod
  23. Method GetSizeForFl:Double()
  24. ?Win32
  25. Return size+2
  26. ?Not Win32
  27. Return size
  28. ?
  29. EndMethod
  30. Method Initialize()
  31. flfamily = TFLFontFamily.GetFamily( name )
  32. handle = flfamily.GetFontID(style)
  33. EndMethod
  34. 'Sort by family name, then by size and finally by style.
  35. Method Compare( o:Object )
  36. Local f:TFLGUIFont = TFLGUIFont(o)
  37. If Not f Then Return Super.Compare(o)
  38. Local tmpComparison% = flFamily.Compare(f.flfamily)
  39. If tmpComparison Then Return tmpComparison
  40. If (size = f.size) Then
  41. Return (style-f.style)
  42. Else
  43. Return (size-f.size)
  44. EndIf
  45. EndMethod
  46. EndType
  47. Type TFLFontFamily
  48. Const IDSTYLEMASK% = (FL_BOLD|FL_ITALIC)
  49. Global arrFamilies:TFLFontFamily[], intLoadedAll% = False
  50. Global defaultSizes:Int[] = [8, 10, 11, 12, 14, 16, 18, 20, 24, 32, 36, 42, 48, 64, 72]
  51. Global fmyDefault:TFLFontFamily
  52. Field strName$, strLowName$, intSizes[], intStyles, ids[IDSTYLEMASK+1]
  53. Function Initialize()
  54. fmyDefault = TFLFontFamily.GetFamily( TFLFontFamily.FriendlyNameFromID( FL_HELVETICA ) )
  55. EndFunction
  56. Function GetFamily:TFLFontFamily( name$ )
  57. Local tmpFamily:TFLFontFamily = LoadFamily( name$ )
  58. If tmpFamily Then Return tmpFamily Else Return fmyDefault
  59. EndFunction
  60. Function FindFamily:TFLFontFamily( name$ )
  61. name = name.ToLower()
  62. For Local tmpFamily:TFLFontFamily = EachIn arrFamilies
  63. If name = tmpFamily.strLowName Then Return tmpFamily
  64. Next
  65. EndFunction
  66. Function LoadAll:TFLFontFamily[]()
  67. Local tmpLastName$ = "", tmpFamily:TFLFontFamily
  68. If Not intLoadedAll Then intLoadedAll = True Else Return arrFamilies
  69. For Local id=0 Until flCountFonts()
  70. Local f$=FriendlyNameFromID(id)
  71. If f <> tmpLastName Then
  72. tmpFamily = FindFamily( f )
  73. If Not tmpFamily Then
  74. tmpFamily = FamilyFromSingleID(id)
  75. EndIf
  76. EndIf
  77. Local tmpStyle% = StyleFromID(id)
  78. tmpFamily.intStyles:|tmpStyle
  79. tmpFamily.ids[tmpStyle] = id
  80. tmpLastName = f
  81. Next
  82. arrFamilies.Sort()
  83. Return arrFamilies
  84. EndFunction
  85. Function LoadFamily:TFLFontFamily( name$ )
  86. Local tmpFamily:TFLFontFamily = FindFamily( name$ )
  87. If tmpFamily Then Return tmpFamily
  88. name = name.ToLower()
  89. For Local id=0 Until flCountFonts()
  90. Local f$=FriendlyNameFromID(id).ToLower()
  91. If f = name Then
  92. If Not tmpFamily Then tmpFamily = FamilyFromSingleID(id)
  93. Local tmpStyle% = StyleFromID(id)
  94. tmpFamily.intStyles:|tmpStyle
  95. tmpFamily.ids[tmpStyle] = id
  96. EndIf
  97. Next
  98. arrFamilies.Sort()
  99. Return tmpFamily
  100. EndFunction
  101. Function FamilyFromSingleID:TFLFontFamily(id)
  102. Local tmpFamily:TFLFontFamily = New TFLFontFamily
  103. tmpFamily.strName = FriendlyNameFromID(id)
  104. tmpFamily.strLowName = tmpFamily.strName.ToLower()
  105. Local tmpSizes:Int
  106. For Local i% = 0 Until flFontSizes(id,Varptr tmpSizes)
  107. If i = 0 And Int Ptr(tmpSizes)[i] = 0 Then
  108. tmpFamily.intSizes = defaultSizes
  109. Exit
  110. EndIf
  111. tmpFamily.intSizes:+[Int Ptr(tmpSizes)[i]]
  112. Next
  113. arrFamilies:+[tmpFamily]
  114. Return tmpFamily
  115. EndFunction
  116. Function NameFromID$(id)
  117. Return flFontName(id)
  118. End Function
  119. Function FriendlyNameFromID$(id)
  120. Local tmpName$ = flFriendlyFontName(id)
  121. If tmpName.EndsWith(" bold") Then tmpName = tmpName[..tmpName.length-5]
  122. If tmpName.EndsWith(" bold italic") Then tmpName = tmpName[..tmpName.length-12]
  123. If tmpName.EndsWith(" italic") Then tmpName = tmpName[..tmpName.length-7]
  124. Return tmpName
  125. End Function
  126. Function StyleFromID(id)
  127. Return flFriendlyFontAttributes(id)
  128. EndFunction
  129. Method GetFontID(style)
  130. style:&IDSTYLEMASK
  131. If (intStyles&style)=style Then Return ids[style]
  132. For Local id = EachIn ids
  133. If id Then Return id
  134. Next
  135. End Method
  136. Method GetFamilySizes:Int[]()
  137. Return intSizes
  138. EndMethod
  139. Method Compare( o:Object )
  140. Local f:TFLFontFamily=TFLFontFamily(o)
  141. If Not f Then Return Super.Compare(o)
  142. Return strLowName.Compare( f.strLowName )
  143. End Method
  144. End Type
  145. Type TFLFontRequest
  146. Field open, currentfont:TFLGUIFont
  147. Field window:TGadget
  148. Field fontbox:TGadget,stylebox:TGadget,sizebox:TGadget,sizetext:TGadget,samplebox:TGadget
  149. Field ok:TGadget,cancel:TGadget
  150. Method New()
  151. Initialize()
  152. EndMethod
  153. Method Refresh(font:TFLGUIFont)
  154. SetFamily( font.flfamily )
  155. SetStyle( font.style )
  156. SetSize( Int(font.size) )
  157. SetGadgetFont samplebox, font
  158. End Method
  159. Field currentSize% = -1
  160. Method GetSize()
  161. If currentSize < 0 Then Return 12 Else Return currentSize
  162. EndMethod
  163. Method SetSize( size% )
  164. currentSize = size
  165. SetGadgetText( sizetext, size )
  166. For Local i% = CountGadgetItems( sizebox )-1 To 0 Step -1
  167. If Int(GadgetItemText( sizebox, i )) = size Then
  168. SelectGadgetItem sizebox, i
  169. Return
  170. EndIf
  171. Next
  172. If SelectedGadgetItem( sizebox ) > -1 Then DeselectGadgetItem( sizebox, SelectedGadgetItem ( sizebox ) )
  173. EndMethod
  174. Field currentStyle% = -1
  175. Method GetStyle()
  176. Return Max( currentStyle, FONT_NORMAL )
  177. EndMethod
  178. Method SetStyle( style% )
  179. currentStyle = style
  180. If SelectedGadgetItem( styleBox ) <> currentStyle Then
  181. If currentStyle < 0 Then DeselectGadgetItem( stylebox, SelectedGadgetItem( stylebox ) ) Else SelectGadgetItem( stylebox, Min(currentStyle, CountGadgetItems( stylebox )-1 ) )
  182. EndIf
  183. EndMethod
  184. Method GetFont:TFLGUIFont()
  185. Local tmpFamily:TFLFontFamily = GetFamily()
  186. Local tmpFont:TFLGUIFont = New TFLGUIFont.SetFont( tmpFamily.strName, GetSize(), GetStyle() )
  187. tmpFont.flfamily = tmpFamily;tmpFont.handle = tmpFamily.GetFontID(GetStyle())
  188. Return tmpFont
  189. End Method
  190. Method GetFamily:TFLFontFamily()
  191. Local tmpFamily:TFLFontFamily = TFLFontFamily.fmyDefault
  192. If SelectedGadgetItem(fontbox) > -1 Then
  193. tmpFamily = TFLFontFamily ( GadgetItemExtra( fontbox, SelectedGadgetItem( fontbox ) ) )
  194. EndIf
  195. Return tmpFamily
  196. EndMethod
  197. Method SetFamily( family:TFLFontFamily )
  198. For Local i% = 0 Until CountGadgetItems( fontbox )
  199. Local tmpItemFamily:TFLFontFamily = TFLFontFamily(GadgetItemExtra( fontbox, i ))
  200. If tmpItemFamily = family Then
  201. SelectGadgetItem fontbox, i
  202. ClearGadgetItems stylebox
  203. AddGadgetItem stylebox, "Regular", 0, -1, "", String(FONT_NORMAL)
  204. If family.intStyles&FONT_BOLD Then AddGadgetItem stylebox, "Bold", 0, -1, "", String(FONT_BOLD)
  205. If family.intStyles&FONT_ITALIC Then AddGadgetItem stylebox, "Italic", 0, -1, "", String(FONT_ITALIC)
  206. If (family.intStyles&(FONT_ITALIC|FONT_BOLD))=FONT_BOLD|FONT_ITALIC Then
  207. AddGadgetItem stylebox, "Bold & Italic", 0, -1, "", String(FONT_BOLD|FONT_ITALIC)
  208. EndIf
  209. currentStyle = Min(currentStyle,family.intStyles)
  210. ClearGadgetItems sizebox
  211. For Local tmpSize% = EachIn family.intSizes
  212. AddGadgetItem sizebox, tmpSize
  213. Next
  214. Return
  215. EndIf
  216. Next
  217. EndMethod
  218. Function RequestHandler:Object(id,data:Object,context:Object)
  219. Local this:TFLFontRequest
  220. Local event:TEvent
  221. event=TEvent(data)
  222. If event
  223. this=TFLFontRequest(context)
  224. If this this.OnEvent event
  225. EndIf
  226. End Function
  227. Method OnEvent(event:TEvent)
  228. Local item = event.data
  229. Select event.id
  230. Case EVENT_GADGETSELECT, EVENT_GADGETACTION
  231. Select event.source
  232. Case fontbox
  233. SetFamily TFLFontFamily(event.extra)
  234. Refresh(GetFont())
  235. Case stylebox
  236. If event.data < 0 Then
  237. currentStyle = -1
  238. Else
  239. currentStyle = Int( String( GadgetItemExtra( stylebox, event.data ) ) )
  240. EndIf
  241. Refresh(GetFont())
  242. Case sizebox
  243. If event.data < 0 Then
  244. currentSize = -1
  245. Else
  246. currentSize = Int( GadgetItemText( sizebox, event.data ) )
  247. EndIf
  248. Refresh(GetFont())
  249. Case cancel
  250. currentfont = Null
  251. open=False
  252. Case ok
  253. currentfont = GetFont()
  254. open=False
  255. End Select
  256. Case EVENT_GADGETLOSTFOCUS
  257. Select event.source
  258. Case sizetext
  259. Local tmpText$ = GadgetText(sizetext)
  260. If tmpText Then
  261. Local tmpInt% = Max(Int( tmpText ), 0)
  262. If tmpInt = tmpText Then
  263. currentSize = tmpInt
  264. Refresh(GetFont())
  265. EndIf
  266. EndIf
  267. End Select
  268. Case EVENT_WINDOWCLOSE
  269. Select event.source
  270. Case window
  271. currentfont = Null
  272. open=False
  273. EndSelect
  274. EndSelect
  275. End Method
  276. Method Request:TFLGUIFont(font:TFLGUIFont)
  277. open=True;currentfont = Null
  278. AddHook EmitEventHook,RequestHandler,Self,100000
  279. Local tmpParent:TGadget = ActiveGadget()
  280. While tmpParent And tmpParent.Class() <> GADGET_WINDOW
  281. tmpParent = GadgetGroup(tmpParent)
  282. Wend
  283. If Not tmpParent Then tmpParent = Desktop()
  284. SetGadgetShape( window, GadgetX(tmpParent)+50, GadgetY(tmpParent)+50, ClientWidth(window), ClientHeight(window) )
  285. ShowGadget window
  286. If font Then Refresh(font)
  287. While open
  288. WaitSystem()
  289. Wend
  290. RemoveHook EmitEventHook,RequestHandler,Self
  291. HideGadget window
  292. Return currentfont
  293. End Method
  294. Method Initialize()
  295. window=CreateWindow("Choose a font...",0,0,392,284,Null,WINDOW_TITLEBAR|WINDOW_HIDDEN|WINDOW_CLIENTCOORDS|WINDOW_CENTER)
  296. flSetModal(QueryGadget(window,QUERY_FLWIDGET))
  297. CreateLabel "Font:",4,4,200,24,window
  298. fontbox=CreateListBox(4,28,200,ClientHeight(window)-134,window)
  299. CreateLabel "Style:",214,4,100,24,window
  300. stylebox=CreateListBox(214,28,100,ClientHeight(window)-134,window)
  301. CreateLabel "Size:",324,4,64,24,window
  302. sizetext=CreateTextField(324,28,64,21,window)
  303. sizebox=CreateListBox(324,49,64,ClientHeight(window)-155,window)
  304. SetGadgetFilter(sizetext,NumberFilter)
  305. Local y=ClientHeight(window)-102
  306. samplebox=CreateLabel("Sample Text",4,y,ClientWidth(window)-8,64,window,LABEL_CENTER|LABEL_SUNKENFRAME)
  307. cancel=CreateButton("Cancel",4,ClientHeight(window)-30,80,26,window,BUTTON_CANCEL)
  308. ok=CreateButton("OK",ClientWidth(window)-4-80,ClientHeight(window)-30,80,26,window,BUTTON_OK)
  309. For Local tmpFamily:TFLFontFamily = EachIn TFLFontFamily.LoadAll()
  310. AddGadgetItem fontbox,tmpFamily.strName, 0, -1, "", tmpFamily
  311. Next
  312. End Method
  313. Function NumberFilter( event:TEvent, context:Object )
  314. Select event.id
  315. Case EVENT_KEYCHAR
  316. If (event.data > "9"[0] Or event.data < "0"[0]) And (event.data <> 8) Then
  317. Return False
  318. Else
  319. Return True
  320. EndIf
  321. EndSelect
  322. Return True
  323. EndFunction
  324. EndType