1 /*	Public domain	*/
2 /*
3  * This demonstrates the use of AG_Variable(3) with the agConfig object
4  * for saving general configuration settings.
5  */
6 
7 #include "agartest.h"
8 
9 int someInt = 1234;
10 int someBool = 0;
11 char someString[64];
12 
13 static void
LoadConfig(AG_Event * event)14 LoadConfig(AG_Event *event)
15 {
16 	AG_TestInstance *ti = AG_PTR(1);
17 
18 	if (agConfig->archivePath != NULL) {
19 		TestMsg(ti, "Loading from %s", agConfig->archivePath);
20 	} else {
21 		char path[AG_PATHNAME_MAX];
22 		if (AG_ObjectCopyFilename(agConfig, path, sizeof(path)) == 0)
23 			TestMsg(ti, "Loading from %s", path);
24 	}
25 
26 	if (AG_ConfigLoad() == 0) {
27 		TestMsg(ti, "Loaded configuration successfully");
28 	} else {
29 		TestMsg(ti, "AG_ConfigLoad: %s", AG_GetError());
30 	}
31 }
32 
33 static void
SaveConfig(AG_Event * event)34 SaveConfig(AG_Event *event)
35 {
36 	AG_TestInstance *ti = AG_PTR(1);
37 
38 	if (agConfig->archivePath != NULL) {
39 		TestMsg(ti, "Saving to %s", agConfig->archivePath);
40 	} else {
41 		char path[AG_PATHNAME_MAX];
42 		if (AG_ObjectCopyFilename(agConfig, path, sizeof(path)) == 0)
43 			TestMsg(ti, "Saving to %s", path);
44 	}
45 
46 	if (AG_ConfigSave() == 0) {
47 		TestMsg(ti, "Saved configuration successfully");
48 	} else {
49 		TestMsg(ti, "AG_ConfigSave: %s", AG_GetError());
50 	}
51 }
52 
53 static int
TestGUI(void * obj,AG_Window * win)54 TestGUI(void *obj, AG_Window *win)
55 {
56 	char path[AG_PATHNAME_MAX];
57 	AG_TestInstance *ti = obj;
58 	AG_Box *box;
59 	AG_Textbox *tb;
60 	AG_Label *lbl;
61 
62 	someString[0] = '\0';
63 
64 	AG_GetString(agConfig, "load-path", path, sizeof(path));
65 	lbl = AG_LabelNew(win, 0, "load-path: %s", path);
66 	AG_SetStyle(lbl, "font-size", "80%");
67 
68 	AG_GetString(agConfig, "save-path", path, sizeof(path));
69 	lbl = AG_LabelNew(win, 0, "save-path: %s", path);
70 	AG_SetStyle(lbl, "font-size", "80%");
71 
72 	/* Tie some globals to the config settings */
73 	AG_BindInt(agConfig, "some-int", &someInt);
74 	AG_BindInt(agConfig, "some-bool", &someBool);
75 	AG_BindString(agConfig, "some-string", someString, sizeof(someString));
76 	AG_SetInt(agConfig, "some-int", 2345);
77 
78 	/* Create some widgets */
79 	AG_NumericalNewInt(win, 0, NULL, "Some int: ", &someInt);
80 	AG_CheckboxNewInt(win, 0, "Some bool", &someBool);
81 	tb = AG_TextboxNew(win, AG_TEXTBOX_HFILL, "Some string: ");
82 	AG_TextboxBindUTF8(tb, someString, sizeof(someString));
83 
84 	box = AG_BoxNewHoriz(win, AG_BOX_EXPAND);
85 	{
86 		AG_ButtonNewFn(box, 0, "Load configuration", LoadConfig, "%p", ti);
87 		AG_ButtonNewFn(box, 0, "Save configuration", SaveConfig, "%p", ti);
88 	}
89 	return (0);
90 }
91 
92 const AG_TestCase configSettingsTest = {
93 	"configSettings",
94 	N_("Test user-specified AG_Config(3) parameters"),
95 	"1.4.2",
96 	0,
97 	sizeof(AG_TestInstance),
98 	NULL,		/* init */
99 	NULL,		/* destroy */
100 	NULL,		/* test */
101 	TestGUI,
102 	NULL		/* bench */
103 };
104