libui/examples/controlgallery/main.c

233 lines
5.4 KiB
C
Raw Normal View History

2015-09-02 20:12:24 -05:00
// 2 september 2015
#include <stdio.h>
#include <string.h>
#include "../../ui.h"
// TODOs
// - rename variables in main()
// - make both columns the same size?
2015-09-02 20:12:24 -05:00
static uiWindow *mainwin;
static int onClosing(uiWindow *w, void *data)
{
uiControlDestroy(uiControl(mainwin));
uiQuit();
return 0;
}
static int shouldQuit(void *data)
{
uiControlDestroy(uiControl(mainwin));
return 1;
}
static void openClicked(uiMenuItem *item, uiWindow *w, void *data)
{
char *filename;
filename = uiOpenFile(mainwin);
2015-09-02 20:12:24 -05:00
if (filename == NULL) {
uiMsgBoxError(mainwin, "No file selected", "Don't be alarmed!");
2015-09-02 20:12:24 -05:00
return;
}
uiMsgBox(mainwin, "File selected", filename);
2015-09-02 20:12:24 -05:00
uiFreeText(filename);
}
static void saveClicked(uiMenuItem *item, uiWindow *w, void *data)
{
char *filename;
filename = uiSaveFile(mainwin);
2015-09-02 20:12:24 -05:00
if (filename == NULL) {
uiMsgBoxError(mainwin, "No file selected", "Don't be alarmed!");
2015-09-02 20:12:24 -05:00
return;
}
uiMsgBox(mainwin, "File selected (don't worry, it's still there)", filename);
2015-09-02 20:12:24 -05:00
uiFreeText(filename);
}
static uiSpinbox *spinbox;
static uiSlider *slider;
static uiProgressBar *progressbar;
static void update(intmax_t value)
{
uiSpinboxSetValue(spinbox, value);
uiSliderSetValue(slider, value);
uiProgressBarSetValue(progressbar, value);
}
static void onSpinboxChanged(uiSpinbox *s, void *data)
{
update(uiSpinboxValue(spinbox));
}
static void onSliderChanged(uiSlider *s, void *data)
{
update(uiSliderValue(slider));
}
int main(void)
{
uiInitOptions o;
const char *err;
uiMenu *menu;
uiMenuItem *item;
uiBox *box;
uiBox *hbox;
2015-09-02 20:12:24 -05:00
uiGroup *group;
uiBox *inner;
uiBox *inner2;
uiEntry *entry;
uiCombobox *cbox;
2016-05-23 16:41:15 -05:00
uiEditableCombobox *ecbox;
2015-09-02 20:12:24 -05:00
uiRadioButtons *rb;
uiTab *tab;
memset(&o, 0, sizeof (uiInitOptions));
err = uiInit(&o);
if (err != NULL) {
fprintf(stderr, "error initializing ui: %s\n", err);
uiFreeInitError(err);
return 1;
}
menu = uiNewMenu("File");
item = uiMenuAppendItem(menu, "Open");
uiMenuItemOnClicked(item, openClicked, NULL);
item = uiMenuAppendItem(menu, "Save");
uiMenuItemOnClicked(item, saveClicked, NULL);
item = uiMenuAppendQuitItem(menu);
uiOnShouldQuit(shouldQuit, NULL);
menu = uiNewMenu("Edit");
item = uiMenuAppendCheckItem(menu, "Checkable Item");
uiMenuAppendSeparator(menu);
item = uiMenuAppendItem(menu, "Disabled Item");
uiMenuItemDisable(item);
item = uiMenuAppendPreferencesItem(menu);
menu = uiNewMenu("Help");
item = uiMenuAppendItem(menu, "Help");
item = uiMenuAppendAboutItem(menu);
mainwin = uiNewWindow("libui Control Gallery", 640, 480, 1);
uiWindowSetMargined(mainwin, 1);
uiWindowOnClosing(mainwin, onClosing, NULL);
box = uiNewVerticalBox();
uiBoxSetPadded(box, 1);
uiWindowSetChild(mainwin, uiControl(box));
hbox = uiNewHorizontalBox();
uiBoxSetPadded(hbox, 1);
uiBoxAppend(box, uiControl(hbox), 1);
group = uiNewGroup("Basic Controls");
2015-09-02 20:12:24 -05:00
uiGroupSetMargined(group, 1);
uiBoxAppend(hbox, uiControl(group), 0);
2015-09-02 20:12:24 -05:00
inner = uiNewVerticalBox();
2015-09-02 20:12:24 -05:00
uiBoxSetPadded(inner, 1);
uiGroupSetChild(group, uiControl(inner));
uiBoxAppend(inner,
2015-09-02 20:12:24 -05:00
uiControl(uiNewButton("Button")),
0);
uiBoxAppend(inner,
2015-09-02 20:12:24 -05:00
uiControl(uiNewCheckbox("Checkbox")),
0);
entry = uiNewEntry();
uiEntrySetText(entry, "Entry");
uiBoxAppend(inner,
2015-09-02 20:12:24 -05:00
uiControl(entry),
0);
uiBoxAppend(inner,
2015-09-02 20:12:24 -05:00
uiControl(uiNewLabel("Label")),
0);
uiBoxAppend(inner,
uiControl(uiNewHorizontalSeparator()),
0);
2015-09-02 20:12:24 -05:00
uiBoxAppend(inner,
2015-09-02 20:12:24 -05:00
uiControl(uiNewDatePicker()),
0);
uiBoxAppend(inner,
2015-09-02 20:12:24 -05:00
uiControl(uiNewTimePicker()),
0);
uiBoxAppend(inner,
2015-09-02 20:12:24 -05:00
uiControl(uiNewDateTimePicker()),
0);
uiBoxAppend(inner,
uiControl(uiNewFontButton()),
0);
uiBoxAppend(inner,
uiControl(uiNewColorButton()),
0);
inner2 = uiNewVerticalBox();
uiBoxSetPadded(inner2, 1);
uiBoxAppend(hbox, uiControl(inner2), 1);
2015-09-02 20:12:24 -05:00
group = uiNewGroup("Numbers");
uiGroupSetMargined(group, 1);
uiBoxAppend(inner2, uiControl(group), 0);
2015-09-02 20:12:24 -05:00
inner = uiNewVerticalBox();
uiBoxSetPadded(inner, 1);
uiGroupSetChild(group, uiControl(inner));
spinbox = uiNewSpinbox(0, 100);
uiSpinboxOnChanged(spinbox, onSpinboxChanged, NULL);
uiBoxAppend(inner, uiControl(spinbox), 0);
slider = uiNewSlider(0, 100);
uiSliderOnChanged(slider, onSliderChanged, NULL);
uiBoxAppend(inner, uiControl(slider), 0);
progressbar = uiNewProgressBar();
uiBoxAppend(inner, uiControl(progressbar), 0);
group = uiNewGroup("Lists");
uiGroupSetMargined(group, 1);
uiBoxAppend(inner2, uiControl(group), 0);
2015-09-02 20:12:24 -05:00
inner = uiNewVerticalBox();
2015-09-02 20:12:24 -05:00
uiBoxSetPadded(inner, 1);
uiGroupSetChild(group, uiControl(inner));
cbox = uiNewCombobox();
uiComboboxAppend(cbox, "Combobox Item 1");
uiComboboxAppend(cbox, "Combobox Item 2");
uiComboboxAppend(cbox, "Combobox Item 3");
uiBoxAppend(inner, uiControl(cbox), 0);
2015-09-02 20:12:24 -05:00
2016-05-23 16:41:15 -05:00
ecbox = uiNewEditableCombobox();
uiEditableComboboxAppend(ecbox, "Editable Item 1");
uiEditableComboboxAppend(ecbox, "Editable Item 2");
uiEditableComboboxAppend(ecbox, "Editable Item 3");
uiBoxAppend(inner, uiControl(ecbox), 0);
2015-09-02 20:12:24 -05:00
rb = uiNewRadioButtons();
uiRadioButtonsAppend(rb, "Radio Button 1");
uiRadioButtonsAppend(rb, "Radio Button 2");
uiRadioButtonsAppend(rb, "Radio Button 3");
uiBoxAppend(inner, uiControl(rb), 1);
tab = uiNewTab();
uiTabAppend(tab, "Page 1", uiControl(uiNewHorizontalBox()));
uiTabAppend(tab, "Page 2", uiControl(uiNewHorizontalBox()));
uiTabAppend(tab, "Page 3", uiControl(uiNewHorizontalBox()));
2016-04-24 08:50:06 -05:00
uiBoxAppend(inner2, uiControl(tab), 1);
2015-09-02 20:12:24 -05:00
uiControlShow(uiControl(mainwin));
uiMain();
uiUninit();
return 0;
}