# DVX Widget Modules Makefile for DJGPP cross-compilation
#
# Each widget lives in its own subdirectory with its .c, .res, and .bmp.
# Each .wgt is a DXE loaded by the loader at startup.

DJGPP_PREFIX  = $(HOME)/djgpp/djgpp
CC            = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-gcc
DXE3GEN       = PATH=$(DJGPP_PREFIX)/bin:$(PATH) DJDIR=$(DJGPP_PREFIX)/i586-pc-msdosdjgpp $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/dxe3gen
CFLAGS        = -O2 -Wall -Wextra -Wno-type-limits -Wno-sign-compare -march=i486 -mtune=i586 -I../core -I../core/platform -I../tasks -I../core/thirdparty
DVXRES        = ../bin/dvxres

OBJDIR = ../obj/widgets
WGTDIR = ../bin/widgets

# Widget name -> directory, source, short name for .wgt/.res
# Format: wgtShortName:dirName:srcFile:resFile
WIDGETS = \
	box:box:widgetBox:box \
	button:button:widgetButton:button \
	canvas:canvas:widgetCanvas:canvas \
	checkbox:checkbox:widgetCheckbox:checkbox \
	combobox:comboBox:widgetComboBox:combobox \
	dropdown:dropdown:widgetDropdown:dropdown \
	imgbtn:imageButton:widgetImageButton:imgbtn \
	image:image:widgetImage:image \
	label:label:widgetLabel:label \
	listbox:listBox:widgetListBox:listbox \
	listview:listView:widgetListView:listview \
	progress:progressBar:widgetProgressBar:progress \
	radio:radio:widgetRadio:radio \
	scrlpane:scrollPane:widgetScrollPane:scrlpane \
	separatr:separator:widgetSeparator:separatr \
	slider:slider:widgetSlider:slider \
	spacer:spacer:widgetSpacer:spacer \
	spinner:spinner:widgetSpinner:spinner \
	splitter:splitter:widgetSplitter:splitter \
	statbar:statusBar:widgetStatusBar:statbar \
	tabctrl:tabControl:widgetTabControl:tabctrl \
	terminal:ansiTerm:widgetAnsiTerm:terminal \
	textinpt:textInput:widgetTextInput:textinpt \
	timer:timer:widgetTimer:timer \
	datactrl:dataCtrl:widgetDataCtrl:datactr \
	dbgrid:dbGrid:widgetDbGrid:dbgrid \
	toolbar:toolbar:widgetToolbar:toolbar \
	treeview:treeView:widgetTreeView:treeview \
	wrapbox:wrapBox:widgetWrapBox:wrapbox

# Extract lists
WGT_NAMES = $(foreach w,$(WIDGETS),$(word 1,$(subst :, ,$w)))
WGT_MODS  = $(WGT_NAMES:%=$(WGTDIR)/%.wgt)
OBJS      = $(foreach w,$(WIDGETS),$(OBJDIR)/$(word 3,$(subst :, ,$w)).o)

# Per-widget extra DXE3GEN flags (e.g. additional -E exports for dlsym)
EXTRA_DXE_FLAGS_datactrl = -E _dataCtrl

DEPFILES = textinpt combobox spinner terminal listbox dropdown listview treeview
WGT_DEPS = $(DEPFILES:%=$(WGTDIR)/%.dep)

.PHONY: all clean

all: $(WGT_MODS) $(WGT_DEPS)

$(WGTDIR)/%.dep: ../config/%.dep | $(WGTDIR)
	sed 's/$$/\r/' $< > $@

# Compile: source is in subdirectory
WIDGET_DEPS_H = ../core/dvxWidgetPlugin.h ../core/dvxWidget.h ../core/dvxTypes.h ../core/dvxApp.h ../core/dvxDraw.h ../core/dvxWm.h ../core/dvxVideo.h ../core/platform/dvxPlatform.h

define WIDGET_RULES
$(OBJDIR)/$(word 3,$(subst :, ,$1)).o: $(word 2,$(subst :, ,$1))/$(word 3,$(subst :, ,$1)).c $$(WIDGET_DEPS_H) | $(OBJDIR)
	$$(CC) $$(CFLAGS) -c -o $$@ $$<

$(WGTDIR)/$(word 1,$(subst :, ,$1)).wgt: $(OBJDIR)/$(word 3,$(subst :, ,$1)).o | $(WGTDIR)
	$$(DXE3GEN) -o $(WGTDIR)/$(word 1,$(subst :, ,$1)).dxe -E _wgtRegister $$(EXTRA_DXE_FLAGS_$(word 1,$(subst :, ,$1))) -U $$<
	mv $(WGTDIR)/$(word 1,$(subst :, ,$1)).dxe $$@
	@if [ -f $(word 2,$(subst :, ,$1))/$(word 4,$(subst :, ,$1)).res ]; then \
		cd $(word 2,$(subst :, ,$1)) && ../$(DVXRES) build ../$$@ $(word 4,$(subst :, ,$1)).res; \
	fi
endef

$(foreach w,$(WIDGETS),$(eval $(call WIDGET_RULES,$w)))

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(WGTDIR):
	mkdir -p $(WGTDIR)

clean:
	rm -f $(OBJS) $(WGT_MODS) $(WGT_DEPS)
	-rmdir $(WGTDIR) 2>/dev/null
