revert between 56095 -> 55830 in arch
[AROS.git] / workbench / prefs / boingiconbar / bibeditor.c
blob149296c305284a9742eea8d82de0fae6878ba941
1 /*
2 Copyright © 2003-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 // #define DEBUG 1
7 #include <zune/customclasses.h>
8 #include <zune/prefseditor.h>
10 #include <proto/alib.h>
11 #include <proto/intuition.h>
12 #include <proto/muimaster.h>
13 #include <proto/asl.h>
14 #include <proto/dos.h>
16 #include <stdlib.h>
18 #include <aros/debug.h>
20 #include "locale.h"
21 #include "bibeditor.h"
22 #include "prefs.h"
24 /*** Instance Data **********************************************************/
26 struct BibEditor_DATA
28 Object *dock_lst, *add_dock_btn, *del_dock_btn;
29 Object *dock_name_str;
30 Object *program_lst, *add_program_btn, *del_program_btn;
31 struct Hook add_dock_hook, del_dock_hook, change_dock_hook;
32 struct Hook add_program_hook, del_program_hook, change_program_hook;
35 STATIC VOID BibPrefs2Gadgets(struct BibEditor_DATA *data);
36 STATIC VOID Gadgets2BibPrefs(struct BibEditor_DATA *data);
37 STATIC CONST_STRPTR AllocExpandPath(CONST_STRPTR path, CONST_STRPTR file);
39 /*** Macros *****************************************************************/
40 #define SETUP_INST_DATA struct BibEditor_DATA *data = INST_DATA(CLASS, self)
42 /*** Hook Functions *********************************************************/
44 AROS_UFH3S(void, add_dock_func,
45 AROS_UFHA(struct Hook *, h, A0),
46 AROS_UFHA(Object *, obj, A2),
47 AROS_UFHA(APTR, msg, A1))
49 AROS_USERFUNC_INIT
51 D(bug("[add_dock_func] called\n"));
53 struct BibEditor_DATA *data = h->h_Data;
55 LONG i;
56 STRPTR newentry = (STRPTR)XGET(data->dock_name_str, MUIA_String_Contents);
57 LONG dock_count = XGET(data->dock_lst, MUIA_List_Entries);
59 if (dock_count < BIB_MAX_DOCKS - 1 && newentry[0] != '\0')
61 DoMethod
63 data->dock_lst,
64 MUIM_List_InsertSingle, newentry, MUIV_List_Insert_Bottom
66 strcpy(bibprefs.docks[dock_count].name, newentry);
67 for (i = 0; i < BIB_MAX_PROGRAMS; i++)
69 bibprefs.docks[dock_count].programs[i][0] = '\0';
71 SET(data->dock_lst, MUIA_List_Active, MUIV_List_Active_Bottom);
72 SET(obj, MUIA_PrefsEditor_Changed, TRUE);
75 AROS_USERFUNC_EXIT
78 AROS_UFH3S(void, del_dock_func,
79 AROS_UFHA(struct Hook *, h, A0),
80 AROS_UFHA(Object *, obj, A2),
81 AROS_UFHA(APTR, msg, A1))
83 AROS_USERFUNC_INIT
85 D(bug("[del_dock_func] called\n"));
87 struct BibEditor_DATA *data = h->h_Data;
89 LONG current_dock = XGET(data->dock_lst, MUIA_List_Active);
90 LONG dock_count = XGET(data->dock_lst, MUIA_List_Entries);
92 if( current_dock != MUIV_List_Active_Off)
94 int i;
96 for (i = current_dock; i < dock_count; i++ )
98 if (bibprefs.docks[i + 1].name[0] != '\0')
100 memcpy(&bibprefs.docks[i], &bibprefs.docks[i + 1], sizeof(struct Dock));
102 else
104 bibprefs.docks[i].name[0] = 0;
108 for (i = 0; i < BIB_MAX_PROGRAMS; i++ )
110 if (bibprefs.docks[dock_count].programs[i][0] != '\0')
112 bibprefs.docks[dock_count].programs[i][0] = '\0';
115 DoMethod(data->dock_lst, MUIM_List_Remove, current_dock);
116 SET(obj, MUIA_PrefsEditor_Changed, TRUE);
119 AROS_USERFUNC_EXIT
122 AROS_UFH3S(void, change_dock_func,
123 AROS_UFHA(struct Hook *, h, A0),
124 AROS_UFHA(Object *, obj, A2),
125 AROS_UFHA(APTR, msg, A1))
127 AROS_USERFUNC_INIT
129 struct BibEditor_DATA *data = h->h_Data;
131 LONG i;
132 LONG current_dock = XGET(data->dock_lst, MUIA_List_Active);
134 D(bug("[change_dock_func] current_dock %d\n", current_dock));
136 SET(data->program_lst, MUIA_List_Quiet, TRUE);
137 DoMethod(data->program_lst, MUIM_List_Clear);
139 if (current_dock != MUIV_List_Active_Off)
143 i = 0;
144 i < BIB_MAX_PROGRAMS && bibprefs.docks[current_dock].programs[i][0] != '\0';
148 DoMethod
150 data->program_lst,
151 MUIM_List_InsertSingle, bibprefs.docks[current_dock].programs[i],
152 MUIV_List_Insert_Bottom
155 SET(data->del_dock_btn, MUIA_Disabled, FALSE);
156 SET(data->add_program_btn, MUIA_Disabled, FALSE);
158 else
160 SET(data->del_dock_btn, MUIA_Disabled, TRUE);
161 SET(data->add_program_btn, MUIA_Disabled, TRUE);
163 SET(data->program_lst, MUIA_List_Quiet, FALSE);
165 AROS_USERFUNC_EXIT
168 AROS_UFH3S(void, add_program_func,
169 AROS_UFHA(struct Hook *, h, A0),
170 AROS_UFHA(Object *, obj, A2),
171 AROS_UFHA(APTR, msg, A1))
173 AROS_USERFUNC_INIT
175 D(bug("[add_program_func] called\n"));
177 struct BibEditor_DATA *data = h->h_Data;
179 struct FileRequester *freq;
180 CONST_STRPTR name = NULL;
182 LONG current_dock = XGET(data->dock_lst, MUIA_List_Active);
183 LONG program_count = XGET(data->program_lst, MUIA_List_Entries);
185 if (current_dock == MUIV_List_Active_Off)
187 D(bug("[IconBarPrefs] Dock not selected, cannot add program!\n"));
188 return;
191 if (program_count >= BIB_MAX_PROGRAMS)
193 D(bug("[IconBarPrefs] Too many programs in dock!\n"));
194 return;
197 if ((freq = AllocAslRequestTags(ASL_FileRequest, TAG_END)) != NULL)
201 AslRequestTags
203 freq,
204 ASLFR_TitleText, _(MSG_ADD_P),
205 ASLFR_DoPatterns, TRUE,
206 ASLFR_RejectIcons, TRUE,
207 ASLFR_InitialDrawer, "SYS:",
208 TAG_END
212 if ((name = AllocExpandPath(freq->fr_Drawer, freq->fr_File)) != NULL)
214 DoMethod(data->program_lst, MUIM_List_InsertSingle, name, MUIV_List_Insert_Bottom);
215 strcpy(bibprefs.docks[current_dock].programs[program_count], name);
216 SET(obj, MUIA_PrefsEditor_Changed, TRUE);
218 FreeVec((APTR)name);
220 FreeAslRequest(freq);
223 AROS_USERFUNC_EXIT
226 AROS_UFH3S(void, del_program_func,
227 AROS_UFHA(struct Hook *, h, A0),
228 AROS_UFHA(Object *, obj, A2),
229 AROS_UFHA(APTR, msg, A1))
231 AROS_USERFUNC_INIT
233 D(bug("[del_program_func] called\n"));
235 struct BibEditor_DATA *data = h->h_Data;
237 LONG current_dock = XGET(data->dock_lst, MUIA_List_Active);
238 LONG delete_program = XGET(data->program_lst, MUIA_List_Active);
239 LONG program_count = XGET(data->program_lst, MUIA_List_Entries);
241 if ((current_dock != MUIV_List_Active_Off) && (delete_program != MUIV_List_Active_Off))
243 LONG i;
245 for (i = delete_program; i < program_count; i++ )
247 if (bibprefs.docks[current_dock].programs[i + 1][0] != '\0')
249 strcpy
251 bibprefs.docks[current_dock].programs[i],
252 bibprefs.docks[current_dock].programs[i + 1]
255 else
257 bibprefs.docks[current_dock].programs[i][0] = '\0';
260 DoMethod(data->program_lst, MUIM_List_Remove, MUIV_List_Remove_Selected);
261 SET(obj, MUIA_PrefsEditor_Changed, TRUE);
264 AROS_USERFUNC_EXIT
267 AROS_UFH3S(void, change_program_func,
268 AROS_UFHA(struct Hook *, h, A0),
269 AROS_UFHA(Object *, obj, A2),
270 AROS_UFHA(APTR, msg, A1))
272 AROS_USERFUNC_INIT
274 struct BibEditor_DATA *data = h->h_Data;
276 LONG current_program = XGET(data->program_lst, MUIA_List_Active);
278 D(bug("[change_dock_func] current_program %d\n", current_program));
280 if (current_program != MUIV_List_Active_Off)
282 SET(data->del_program_btn, MUIA_Disabled, FALSE);
284 else
286 SET(data->del_program_btn, MUIA_Disabled, TRUE);
289 AROS_USERFUNC_EXIT
292 /*** Methods ****************************************************************/
293 Object *BibEditor__OM_NEW(Class *CLASS, Object *self, struct opSet *message)
295 D(bug("[bibedit class] BibEdit Class New\n"));
297 Object *dock_lst, *add_dock_btn, *del_dock_btn, *dock_name_str;
298 Object *program_lst, *add_program_btn, *del_program_btn;
300 self = (Object *) DoSuperNewTags
302 CLASS, self, NULL,
303 MUIA_PrefsEditor_Name, _(MSG_TITLE),
304 MUIA_PrefsEditor_Path, (IPTR) "Iconbar.prefs",
305 MUIA_PrefsEditor_IconTool, (IPTR) "SYS:Prefs/BoingIconBar",
306 //MUIA_Group_Horiz, TRUE,
307 MUIA_Group_Columns, 2,
308 Child, VGroup,
309 Child, TextObject,
310 MUIA_Text_Contents, _(MSG_DOCK),
311 End,
312 Child, dock_lst = ListviewObject,
313 MUIA_Listview_List, ListObject,
314 MUIA_Frame, MUIV_Frame_InputList,
315 MUIA_List_ConstructHook, MUIV_List_ConstructHook_String,
316 MUIA_List_DestructHook, MUIV_List_DestructHook_String,
317 End,
318 End,
319 End,
320 Child, VGroup,
321 Child, TextObject,
322 MUIA_Text_Contents, _(MSG_PROGRAMS),
323 End,
324 Child, program_lst = ListviewObject,
325 MUIA_Listview_List, ListObject,
326 MUIA_Frame, MUIV_Frame_InputList,
327 MUIA_List_ConstructHook, MUIV_List_ConstructHook_String,
328 MUIA_List_DestructHook, MUIV_List_DestructHook_String,
329 End,
330 End,
331 End,
332 Child, VGroup,
333 Child, dock_name_str = StringObject,
334 MUIA_String_AdvanceOnCR, TRUE,
335 StringFrame,
336 End,
337 Child, HGroup,
338 Child, add_dock_btn = SimpleButton(_(MSG_ADD_DOCK)),
339 Child, del_dock_btn = SimpleButton(_(MSG_DEL_DOCK)),
340 End,
341 End,
342 Child, VGroup,
343 Child, HVSpace,
344 Child, HGroup,
345 Child, add_program_btn = SimpleButton(_(MSG_ADD_P)),
346 Child, del_program_btn = SimpleButton(_(MSG_DEL_P)),
347 End,
348 End,
349 TAG_DONE
352 if (self)
354 SETUP_INST_DATA;
356 data->dock_lst = dock_lst;
357 data->add_dock_btn = add_dock_btn;
358 data->del_dock_btn = del_dock_btn;
359 data->dock_name_str = dock_name_str;
360 data->program_lst = program_lst;
361 data->add_program_btn = add_program_btn;
362 data->del_program_btn = del_program_btn;
363 data->dock_name_str = dock_name_str;
365 data->add_dock_hook.h_Entry = (HOOKFUNC)add_dock_func;
366 data->add_dock_hook.h_Data = data;
367 data->del_dock_hook.h_Entry = (HOOKFUNC)del_dock_func;
368 data->del_dock_hook.h_Data = data;
369 data->change_dock_hook.h_Entry = (HOOKFUNC)change_dock_func;
370 data->change_dock_hook.h_Data = data;
371 data->add_program_hook.h_Entry = (HOOKFUNC)add_program_func;
372 data->add_program_hook.h_Data = data;
373 data->del_program_hook.h_Entry = (HOOKFUNC)del_program_func;
374 data->del_program_hook.h_Data = data;
375 data->change_program_hook.h_Entry = (HOOKFUNC)change_program_func;
376 data->change_program_hook.h_Data = data;
378 SET(data->del_dock_btn, MUIA_Disabled, TRUE);
379 SET(data->add_program_btn, MUIA_Disabled, TRUE);
380 SET(data->del_program_btn, MUIA_Disabled, TRUE);
382 DoMethod
384 data->add_dock_btn, MUIM_Notify, MUIA_Pressed, FALSE,
385 self, 2, MUIM_CallHook, &data->add_dock_hook
388 DoMethod
390 data->del_dock_btn, MUIM_Notify, MUIA_Pressed, FALSE,
391 self, 2, MUIM_CallHook, &data->del_dock_hook
394 DoMethod
396 data->dock_lst, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime,
397 self, 2, MUIM_CallHook, &data->change_dock_hook
400 DoMethod
402 data->add_program_btn, MUIM_Notify, MUIA_Pressed, FALSE,
403 self, 2, MUIM_CallHook, &data->add_program_hook
406 DoMethod
408 data->del_program_btn, MUIM_Notify, MUIA_Pressed, FALSE,
409 self, 2, MUIM_CallHook, &data->del_program_hook
412 DoMethod
414 data->program_lst, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime,
415 self, 2, MUIM_CallHook, &data->change_program_hook
418 BibPrefs2Gadgets(data);
421 return self;
425 * update struct bibprefs with actual data selected in gadgets
427 STATIC VOID Gadgets2BibPrefs (struct BibEditor_DATA *data)
429 D(bug("Gadgets2BibPrefs\n"));
431 // no-op because we are immediately changing bibprefs
435 * update gadgets with values of struct bibprefs
437 STATIC VOID BibPrefs2Gadgets(struct BibEditor_DATA *data)
439 LONG dock;
441 DoMethod(data->dock_lst, MUIM_List_Clear);
442 DoMethod(data->program_lst, MUIM_List_Clear);
444 for (dock = 0; bibprefs.docks[dock].name[0] != '\0'; dock++)
446 DoMethod
448 data->dock_lst,
449 MUIM_List_InsertSingle, bibprefs.docks[dock].name, MUIV_List_Insert_Bottom
452 SET(data->dock_lst, MUIA_List_Active, 0);
456 * combine path and file and expand it to an absolute path if necessary
458 STATIC CONST_STRPTR AllocExpandPath(CONST_STRPTR path, CONST_STRPTR file)
460 STRPTR result = NULL;
461 BPTR lock;
463 if ((result = AllocVec(BIB_MAX_PATH, MEMF_ANY | MEMF_CLEAR)) != NULL)
465 strlcpy(result, path, BIB_MAX_PATH);
466 if (file && (*file != '\0'))
468 AddPart(result, file, BIB_MAX_PATH);
470 if (strchr(result, ':') == NULL) // relative path
472 if ((lock = Lock(result, ACCESS_READ)) != BNULL)
474 NameFromLock(lock, result, BIB_MAX_PATH);
475 UnLock(lock);
479 return result;
482 IPTR BibEditor__MUIM_PrefsEditor_ImportFH (
483 Class *CLASS, Object *self,
484 struct MUIP_PrefsEditor_ImportFH *message
487 SETUP_INST_DATA;
488 BOOL success = TRUE;
490 D(bug("[bibedit class] BibEdit Class Import\n"));
492 success = Prefs_ImportFH(message->fh);
493 if (success) BibPrefs2Gadgets(data);
495 return success;
498 IPTR BibEditor__MUIM_PrefsEditor_ExportFH
500 Class *CLASS, Object *self,
501 struct MUIP_PrefsEditor_ExportFH *message
504 SETUP_INST_DATA;
505 BOOL success = TRUE;
507 D(bug("[bibedit class] BibEdit Class Export\n"));
509 Gadgets2BibPrefs(data);
510 success = Prefs_ExportFH(message->fh);
512 return success;
515 IPTR BibEditor__MUIM_PrefsEditor_SetDefaults
517 Class *CLASS, Object *self, Msg message
520 SETUP_INST_DATA;
521 BOOL success = TRUE;
523 D(bug("[bibedit class] BibEdit Class SetDefaults\n"));
525 success = Prefs_Default();
526 if (success) BibPrefs2Gadgets(data);
528 return success;
531 /*** Setup ******************************************************************/
532 ZUNE_CUSTOMCLASS_4
534 BibEditor, NULL, MUIC_PrefsEditor, NULL,
535 OM_NEW, struct opSet *,
536 MUIM_PrefsEditor_ImportFH, struct MUIP_PrefsEditor_ImportFH *,
537 MUIM_PrefsEditor_ExportFH, struct MUIP_PrefsEditor_ExportFH *,
538 MUIM_PrefsEditor_SetDefaults, Msg