Set GUI Interface Font (code snippet)

Description

This IUP GUI popup allows a user to choose the font face style of every IUP GUI in a plugin.

It sets the IntFontSet, StrFontHead & StrFontBody global variables according to the chosen font face style.

The StrFontHead & StrFontBody variables can then be assigned to any GUI font attributes.

The IntFontSet variable could be made 'sticky' by using the Preserve User Settings (code snippet).

This snippet also illustrates the use of LUA tables (arrays) in two scenarios.
The resultant table driven code is often easier to maintain, and can eliminate complex if then else structures.

  1. The two level dictionary table tblFontSet is used to lookup Font Sets.
  2. A dictionary table of indexed tables define GUI attributes in a for … in pairs loop.

Requires: iuplua

Code

SetInterfaceFont.fh_lua
--[[
@Title:		Set Interface Font Snippet
@Author:	Mike Tate
@LastUpdated:	May 2012
@Version:	1.2
@Description:	Allows the GUI interface font to be chosen
]]
 
	require "iuplua"				-- To access GUI window builder
 
	StrPlugin	= "Interface Font Snippet"	-- Plugin title & version
	StrIssue	= " V1.2"
	StrRed		= "255 000 000"			-- Color attributes
	StrGreen	= "000 120 000"
	StrBlue		= "000 000 255"
	StrBlack	= "000 000 000"
	StrWhite	= "255 255 255"
	StrBigMargin	= "10x10"			-- Layout attributes
	StrMinMargin	= "1x1"
	StrGap		= "10"
 
	StrFontSetTitle	= "Set Interface Font"
	IntFontPlain	= 1				-- Font Face & Style values for IntFontSet
	IntFontBold	= 2
	IntArialPlain	= 3
	IntArialBold	= 4
	IntTahomaPlain	= 5
	IntTahomaBold	= 6
	StrFontFace	= string.gsub(iup.GetGlobal("DEFAULTFONT"),",.*","")
 
	IntFontSet	= IntFontPlain			-- Initial Font Face & Style default
 
-- GUI Font Face & Style Dialogue --
function GUI_FontDialogue(intFontSet)
 
	-- Note: Pixel sizes -21 = -20 & -17 = -16 & -14 = -13 and pixel sizes -22, -18 & -13 have no point size equivalent.
	local tblFontSet = {}		-- Lookup table for StrFontHead and StrFontBody font sets
	tblFontSet[IntFontPlain]	=	{ Head=StrFontFace..", Bold -16",	Body=StrFontFace..",      -16", }
	tblFontSet[IntFontBold]		=	{ Head=StrFontFace..", Bold -16",	Body=StrFontFace..", Bold -15", }
	tblFontSet[IntArialPlain]	=	{ Head="Arial,         Bold -16",	Body="Arial,              -16", }
	tblFontSet[IntArialBold]	=	{ Head="Arial,         Bold -16",	Body="Arial,         Bold -15", }
	tblFontSet[IntTahomaPlain]	=	{ Head="Tahoma,        Bold -15",	Body="Tahoma,             -16", }
	tblFontSet[IntTahomaBold]	=	{ Head="Tahoma,        Bold -15",	Body="Tahoma,        Bold -14", }
 
	-- Assign font set global variables
	local function doAssignFontSet(intFontSet)
		IntFontSet = intFontSet
		StrFontHead = tblFontSet[intFontSet]["Head"] -- Font for all GUI dialog header text
		StrFontBody = tblFontSet[intFontSet]["Body"] -- Font for all GUI dialog body text
	end -- local function doAssignFontSet
 
	-- If parameter exists, simply set it as current font set
	if intFontSet then doAssignFontSet(intFontSet) return end
 
	local strAnswer = "Change"
 
	local strFontPlainTitle	 = StrFontFace.." Plain"
	local strFontBoldTitle	 = StrFontFace.." Bold"
	local strArialPlainTitle = "Arial Plain"
	local strArialBoldTitle	 = "Arial Bold"
	local strTahomaPlainTitle= "Tahoma Plain"
	local strTahomaBoldTitle = "Tahoma Bold"
 
	-- Create each GUI label and button with title and tooltip
	local	lblHeadName	= iup.label	{ title=" Name :"		, tip="Names of the available Fonts" }
	local	lblHeadPlain	= iup.label	{ title=" Plain :"		, tip="Plain versions of the Fonts" }
	local	lblHeadBold	= iup.label	{ title=" Bold :"		, tip="Bold versions of the Fonts" }
	local	lblFontName	= iup.label	{ title="Font "..StrFontFace	, tip="Default Windows fontface" }
	local	btnFontPlain	= iup.button	{ title=strFontPlainTitle	, tip="Choose "..strFontPlainTitle.." style" }
	local	btnFontBold	= iup.button	{ title=strFontBoldTitle	, tip="Choose "..strFontBoldTitle.." style" }
	local	lblArialName	= iup.label	{ title="Font Arial"		, tip="Arial alternative fontface" }
	local	btnArialPlain	= iup.button	{ title=strArialPlainTitle	, tip="Choose "..strArialPlainTitle.." style" }
	local	btnArialBold	= iup.button	{ title=strArialBoldTitle	, tip="Choose "..strArialBoldTitle.." style" }
	local	lblTahomaName	= iup.label	{ title="Font Tahoma"		, tip="Tahoma alternative fontface" }
	local	btnTahomaPlain	= iup.button	{ title=strTahomaPlainTitle	, tip="Choose "..strTahomaPlainTitle.." style" }
	local	btnTahomaBold	= iup.button	{ title=strTahomaBoldTitle	, tip="Choose "..strTahomaBoldTitle.." style" }
	local	lblChoose	= iup.label	{ title="Choose your interface font style or", }
	local	btnClose	= iup.button	{ title="Close"			, tip="Close this Font Style window" }
 
	-- Create dialogue and turn off resize, maximize, minimize, and menubox except Close button
	local	dialogFont	= iup.dialog { title=StrPlugin.." Font Style", dialogframe="YES", background=StrWhite, startfocus=btnClose,
					iup.vbox { alignment="ACENTER", gap=StrGap, margin=StrBigMargin,
						iup.frame { font=StrFontHead, fgcolor=StrBlack, active="YES", title="Font Style",
							iup.vbox { margin=StrMinMargin,
								iup.hbox { homogeneous="YES", lblHeadName,	lblHeadPlain,	lblHeadBold,	},
								iup.hbox { homogeneous="YES", lblFontName,	btnFontPlain,	btnFontBold,	},
								iup.hbox { homogeneous="YES", lblArialName,	btnArialPlain,	btnArialBold,	},
								iup.hbox { homogeneous="YES", lblTahomaName,	btnTahomaPlain,	btnTahomaBold,	},
								iup.hbox { lblChoose, btnClose, },
							},
						},
					},
					close_cb=function() strAnswer="Ignore" return iup.CLOSE end,
				}
 
	-- Assign font styles for this GUI labels and buttons
	local strFontPlain	= tblFontSet	[IntFontPlain]	["Body"]
	local strFontBold	= tblFontSet	[IntFontBold]	["Body"]
	local strArialPlain	= tblFontSet	[IntArialPlain]	["Body"]
	local strArialBold	= tblFontSet	[IntArialBold]	["Body"]
	local strTahomaPlain	= tblFontSet	[IntTahomaPlain]["Body"]
	local strTahomaBold	= tblFontSet	[IntTahomaBold]	["Body"]
 
	-- Set other GUI attributes for labels and buttons
	for iupName, tblAttr in pairs( {
		-- Control	= 1~fgcolor	, 2~font	, 3~FontSet	, 4~action function()
		[lblHeadName]	= { StrBlue	, StrFontBody	, false		, false	},
		[lblHeadPlain]	= { StrBlue	, strFontPlain	, false		, false	},
		[lblHeadBold]	= { StrBlue	, strFontBold	, false		, false	},
		[lblFontName]	= { StrBlue	, strFontPlain	, false		, false	},
		[btnFontPlain]	= { StrGreen	, strFontPlain	, IntFontPlain	, false	},
		[btnFontBold]	= { StrGreen	, strFontBold	, IntFontBold	, false	},
		[lblArialName]	= { StrBlue	, strArialPlain	, false		, false	},
		[btnArialPlain]	= { StrGreen	, strArialPlain	, IntArialPlain	, false	},
		[btnArialBold]	= { StrGreen	, strArialBold	, IntArialBold	, false	},
		[lblTahomaName]	= { StrBlue	, strTahomaPlain, false		, false	},
		[btnTahomaPlain]= { StrGreen	, strTahomaPlain, IntTahomaPlain, false	},
		[btnTahomaBold]	= { StrGreen	, strTahomaBold	, IntTahomaBold	, false	},
		[lblChoose]	= { StrBlue	, StrFontBody	, false		, false	},
		[btnClose]	= { StrRed	, StrFontBody	, false		, dialogFont.close_cb },
		} ) do
		iupName.expand	= "YES"
		iupName.fgcolor	= tblAttr[1]
		iupName.font	= tblAttr[2]
		if tblAttr[3] then
			if tblAttr[3] == IntFontSet then iupName.active = "NO" end	-- Disable button for currently selected font
			iupName.action = function() doAssignFontSet(tblAttr[3]) return iup.CLOSE end
		end
		if tblAttr[4] then iupName.action = tblAttr[4] end
	end
 
	dialogFont:popup(iup.CENTER,iup.CENTER)
 
	if (iup.MainLoopLevel()==0) then iup.MainLoop() end
 
	return strAnswer
end -- function GUI_FontDialogue

Usage

-- GUI Main Dialogue --
function GUI_MainDialogue()
 
	-- Create each GUI label and button with title and tooltip, etc
	local	lblMessage	= iup.label	{ title="Example of Set Interface Font", alignment="ACENTER", font=StrFontHead, }
	local	btnFontSet	= iup.button	{ title=StrFontSetTitle }
	local	btnCancel	= iup.button	{ title="Cancel Plugin" }
	local	vboxMain	= iup.vbox { font=StrFontBody, expandchildren="YES", alignment="ACENTER", gap=StrGap, margin=StrBigMargin,
					lblMessage, btnFontSet, btnCancel,
				}
	-- Create dialogue and turn off resize, menubox, maximize and minimize
	local	dialogMain	= iup.dialog { title=StrPlugin..StrIssue, dialogframe="YES", background=StrWhite, startfocus=btnCancel, size="QUARTERxQUARTER", vboxMain,
					close_cb=function() return iup.CLOSE end,
				}
 
	local function doFontSet()
		if GUI_FontDialogue() == "Change" then
			lblMessage.font	= StrFontHead
			vboxMain.font	= StrFontBody
		end
	end -- local function doFontSet
 
	-- Set other GUI attributes for labels and buttons
	for iupName, tblAttr in pairs( {
		-- Control	= 1~fgcolor	, 2~action function()
		[lblMessage]	= { StrBlack	, false				},
		[btnFontSet]	= { StrBlue	, function() doFontSet() end	},
		[btnCancel]	= { StrRed	, dialogMain.close_cb 		},
		} ) do
		iupName.expand	= "YES"
		iupName.fgcolor	= tblAttr[1]
		if tblAttr[2] then iupName.action = tblAttr[2] end
	end
 
	dialogMain:showxy(100,100)
 
	if (iup.MainLoopLevel()==0) then iup.MainLoop() end
 
end -- function GUI_MainDialogue
 
GUI_FontDialogue(IntFontSet) -- Assign initial font set
 
GUI_MainDialogue()