change on-disk names
[AROS.git] / workbench / system / Wanderer / panel_statusbar.c
blob486a8244458c8361702048b430ed69eb03563886
1 /*
2 Copyright 2004-2010, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "portable_macros.h"
7 #ifdef __AROS__
8 #define MUIMASTER_YES_INLINE_STDARG
9 #endif
11 #ifdef __AROS__
12 #define DEBUG 0
13 #include <aros/debug.h>
14 #endif
16 #include <exec/types.h>
17 #include <libraries/mui.h>
19 #ifdef __AROS__
20 #include <zune/customclasses.h>
21 #else
22 #include <zune_AROS/customclasses.h>
23 #endif
25 #include <proto/utility.h>
27 #include <proto/graphics.h>
29 #include <proto/exec.h>
30 #include <proto/datatypes.h>
32 #include <dos/dos.h>
33 #include <proto/dos.h>
35 #include <proto/icon.h>
37 #include <stdio.h>
38 #include <string.h>
40 #include <intuition/screens.h>
41 #include <datatypes/pictureclass.h>
42 #include <clib/macros.h>
44 #ifdef __AROS__
45 #include <clib/alib_protos.h>
47 #include <prefs/wanderer.h>
48 #else
49 #include <prefs_AROS/wanderer.h>
50 #endif
52 #if defined(__AMIGA__) && !defined(__PPC__)
53 #define NO_INLINE_STDARG
54 #endif
55 #ifndef _PROTO_INTUITION_H
56 #include <proto/intuition.h>
57 #endif
58 #include <proto/muimaster.h>
60 #include "Classes/iconlist.h"
61 #include "Classes/iconlistview.h"
62 #include "Classes/iconlist_attributes.h"
63 #include "Classes/icon_attributes.h"
64 #include "wanderer.h"
65 #include "wandererprefs.h"
67 #include "iconwindow.h"
68 #include "iconwindow_attributes.h"
70 extern struct List iconwindow_Extensions;
72 /*** Private Data *********************************************************/
74 static CONST_STRPTR extension_Name = "IconWindow StatusBar Extension";
75 static CONST_STRPTR extension_PrefsFile = "ENV:SYS/Wanderer/statusbar.prefs";
76 static STRPTR extension_PrefsData;
77 static struct iconWindow_Extension panelStatusBar__Extension;
78 static struct List panelStatusBar__StatusBars;
79 static struct NotifyRequest panelStatusBar__PrefsNotifyRequest;
80 static Object *panelStatusBar__PrefsNotificationObject;
82 struct panel_StatusBar_DATA
84 struct Node iwp_Node;
85 IPTR iwp_Flags;
86 Object *iwp_StatusBar_StatusBarObj;
87 Object *iwp_StatusBar_StatusTextObj;
88 #ifdef __AROS__
89 struct Hook iwp_StatusBar_updateHook;
90 #else
91 struct Hook *iwp_StatusBar_updateHook;
92 #endif
95 /// From panel_toolbar
96 STRPTR ExpandEnvName(CONST_STRPTR env_path);
97 ///
99 const UBYTE MSG_MEM_G[] = "GB";
100 const UBYTE MSG_MEM_M[] = "MB";
101 const UBYTE MSG_MEM_K[] = "KB";
102 const UBYTE MSG_MEM_B[] = "Bytes";
104 ///FmtSizeToString()
105 static void FmtSizeToString(UBYTE *buf, ULONG num)
107 UQUAD d;
108 const UBYTE *ch;
109 struct
111 IPTR val;
112 IPTR dec;
113 } array =
115 num,
119 if (num >= 1073741824)
121 //Gigabytes
122 array.val = num >> 30;
123 d = ((UQUAD)num * 10 + 536870912) / 1073741824;
124 array.dec = d % 10;
125 ch = MSG_MEM_G;
127 else if (num >= 1048576)
129 //Megabytes
130 array.val = num >> 20;
131 d = ((UQUAD)num * 10 + 524288) / 1048576;
132 array.dec = d % 10;
133 ch = MSG_MEM_M;
135 else if (num >= 1024)
137 //Kilobytes
138 array.val = num >> 10;
139 d = (num * 10 + 512) / 1024;
140 array.dec = d % 10;
141 ch = MSG_MEM_K;
143 else
145 //Bytes
146 array.val = num;
147 array.dec = 0;
148 d = 0;
149 ch = MSG_MEM_B;
152 if (!array.dec && (d > array.val * 10))
154 array.val++;
157 RawDoFmt(array.dec ? "%lu.%lu" : "%lu", &array, NULL, buf);
159 while (*buf)
161 buf++;
164 sprintf(buf, " %s", ch);
168 /*** Hook functions *********************************************************/
169 static
170 ///panelStatusBar__HookFunc_UpdateStatusFunc()
171 #ifdef __AROS__
172 AROS_UFH3(
173 void, panelStatusBar__HookFunc_UpdateStatusFunc,
174 AROS_UFHA(struct Hook *, hook, A0),
175 AROS_UFHA(APTR *, obj, A2),
176 AROS_UFHA(APTR, param, A1)
179 #else
180 HOOKPROTO(panelStatusBar__HookFunc_UpdateStatusFunc, void, APTR *obj, APTR param)
182 #endif
183 AROS_USERFUNC_INIT
185 /* Get data */
186 Object *self = ( Object *)obj;
187 Class *CLASS = *( Class **)param;
188 struct panel_StatusBar_DATA *panelStatusBarPrivate = NULL;
190 SETUP_ICONWINDOW_INST_DATA;
192 D(bug("[IW.statusbar]: %s()\n", __PRETTY_FUNCTION__));
194 /* Only change dir if it is a valid directory/volume */
195 if ((panelStatusBarPrivate = (struct panel_StatusBar_DATA *)data->iwd_BottomPanel.iwp_PanelPrivate) != NULL)
197 if (panelStatusBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
198 return;
200 struct List *iconList = NULL;
201 struct IconEntry *icon = NULL;
202 UBYTE status_str[1024];
203 UBYTE size_str[256];
205 int files = 0, dirs = 0, hidden = 0;
206 ULONG size = 0;
208 GET(data->iwd_IconListObj, MUIA_Family_List, &iconList);
209 if (iconList)
211 ForeachNode(iconList, icon)
213 if (icon->ie_Flags & ICONENTRY_FLAG_VISIBLE)
215 if (icon->ie_IconListEntry.type == ST_USERDIR)
217 dirs += 1;
219 else
221 files += 1;
222 if (icon->ie_FileInfoBlock)
224 size += icon->ie_FileInfoBlock->fib_Size;
225 D(bug("[IW.statusbar] %s: '%s' FIB Size = %d bytes\n", __PRETTY_FUNCTION__, icon->ie_IconNode.ln_Name, icon->ie_FileInfoBlock->fib_Size));
229 else
231 hidden += 1;
235 int previous = 0;
236 if (files > 0)
238 FmtSizeToString(size_str, size);
239 sprintf(status_str, " %s in %d files", size_str, files);
240 previous = strlen(status_str);
242 if (dirs > 0)
244 sprintf(status_str + previous, "%s%d drawers", (previous > 0) ? ", " : " " , dirs);
245 previous = strlen(status_str);
247 if (hidden > 0)
249 sprintf(status_str + previous, " (%d hidden)", hidden);
250 previous = strlen(status_str);
253 if (previous > 0)
255 SET(panelStatusBarPrivate->iwp_StatusBar_StatusTextObj, MUIA_Text_Contents, (IPTR)status_str);
257 else
259 SET(panelStatusBarPrivate->iwp_StatusBar_StatusTextObj, MUIA_Text_Contents, (IPTR)"");
263 AROS_USERFUNC_EXIT
265 #ifndef __AROS__
266 MakeStaticHook(StatusBar_updateHook, panelStatusBar__HookFunc_UpdateStatusFunc);
267 #endif
269 /*** Main Functions ****************************************************************/
271 #define STATUSBAR_PREFSSIZE 1024
272 static IPTR panelStatusBar__HandleFSUpdate(Object *WandererObj, struct NotifyMessage *msg)
274 if (GetVar(extension_PrefsFile, extension_PrefsData, STATUSBAR_PREFSSIZE, GVF_GLOBAL_ONLY) != -1)
276 D(bug("[IW.statusbar] %s: Prefs contain '%s'\n", __PRETTY_FUNCTION__, extension_PrefsData));
277 if ((strcasecmp(extension_PrefsData, "True")) == 0)
279 SET(panelStatusBar__PrefsNotificationObject, MUIA_ShowMe, TRUE);
281 else
283 SET(panelStatusBar__PrefsNotificationObject, MUIA_ShowMe, FALSE);
286 return 0;
289 ///panelStatusBar__PrefsSetup()
290 static IPTR panelStatusBar__PrefsSetup(Class *CLASS, Object *self, struct opSet *message)
292 IPTR panelStatusBarFSNotifyPort = 0;
293 struct panel_StatusBar_DATA *panelStatusBarPrivate = NULL;
294 struct List *panelStatusBarFSNotifyList = NULL;
296 SETUP_ICONWINDOW_INST_DATA;
298 panelStatusBarFSNotifyPort = GetTagData(MUIA_Wanderer_FileSysNotifyPort, (IPTR) NULL, message->ops_AttrList);
299 panelStatusBarFSNotifyList = (APTR)GetTagData(MUIA_Wanderer_FileSysNotifyList, (IPTR) NULL, message->ops_AttrList);
301 D(bug("[IW.statusbar]: %s()\n", __PRETTY_FUNCTION__));
303 if ((panelStatusBarPrivate = (struct panel_StatusBar_DATA *)data->iwd_BottomPanel.iwp_PanelPrivate) != NULL)
305 if (panelStatusBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
306 return 0;
308 extension_PrefsData = AllocVec(STATUSBAR_PREFSSIZE, MEMF_CLEAR);
310 /* Setup notification on prefs file --------------------------------*/
311 struct Wanderer_FSHandler *_prefsNotifyHandler = NULL;
313 if ((_prefsNotifyHandler = AllocMem(sizeof(struct Wanderer_FSHandler), MEMF_CLEAR)) != NULL)
315 _prefsNotifyHandler->fshn_Node.ln_Name = ExpandEnvName(extension_PrefsFile);
316 panelStatusBar__PrefsNotifyRequest.nr_Name = _prefsNotifyHandler->fshn_Node.ln_Name;
317 panelStatusBar__PrefsNotifyRequest.nr_Flags = NRF_SEND_MESSAGE;
318 panelStatusBar__PrefsNotifyRequest.nr_stuff.nr_Msg.nr_Port = (struct MsgPort *)panelStatusBarFSNotifyPort;
319 _prefsNotifyHandler->HandleFSUpdate = panelStatusBar__HandleFSUpdate;
321 if (StartNotify(&panelStatusBar__PrefsNotifyRequest))
323 D(bug("[IW.statusbar]%s: Prefs-notification setup on '%s'\n", __PRETTY_FUNCTION__, panelStatusBar__PrefsNotifyRequest.nr_Name));
325 else
327 D(bug("[IW.statusbar] %s: FAILED to setup Prefs-notification!\n", __PRETTY_FUNCTION__));
329 AddTail(panelStatusBarFSNotifyList, &_prefsNotifyHandler->fshn_Node);
331 #ifdef __AROS__
332 panelStatusBar__PrefsNotificationObject = (Object *)NotifyObject, End;
333 #else
334 panelStatusBar__PrefsNotificationObject = MUI_NewObject(MUIC_Notify, TAG_DONE);
335 #endif
336 if (GetVar(extension_PrefsFile, extension_PrefsData, STATUSBAR_PREFSSIZE, GVF_GLOBAL_ONLY) != -1)
338 D(bug("[IW.statusbar] %s: Prefs contain '%s'\n", __PRETTY_FUNCTION__, extension_PrefsData));
341 return 0;
345 ///panelStatusBar__Setup()
346 static IPTR panelStatusBar__Setup(Class *CLASS, Object *self, struct opSet *message)
348 SETUP_ICONWINDOW_INST_DATA;
350 Object *panel_StatusBar = NULL,
351 *panelStatusBar_TextStatus = NULL;
353 struct panel_StatusBar_DATA *panelStatusBarPrivate = NULL;
355 if (!(!(data->iwd_Flags & IWDFLAG_ISROOT) && data->iwd_BottomPanel.iwp_PanelContainerObj))
356 return 0;
358 D(bug("[IW.statusbar]: %s()\n", __PRETTY_FUNCTION__));
360 if (data->iwd_BottomPanel.iwp_PanelPrivate == 0)
362 if ((data->iwd_BottomPanel.iwp_PanelPrivate = (IPTR)AllocVec(sizeof(struct panel_StatusBar_DATA), MEMF_CLEAR)) == (IPTR)NULL)
363 return 0;
365 panelStatusBarPrivate = (struct panel_StatusBar_DATA *)data->iwd_BottomPanel.iwp_PanelPrivate;
366 panelStatusBarPrivate->iwp_Node.ln_Name = (char *)extension_Name;
368 /* Create the "StatusBar" panel object .. */
369 panel_StatusBar = MUI_NewObject(MUIC_Group,
370 MUIA_InnerLeft,(0),
371 MUIA_InnerRight,(0),
372 MUIA_InnerTop,(0),
373 MUIA_InnerBottom,(0),
374 MUIA_Frame, MUIV_Frame_None,
375 Child, (IPTR)MUI_NewObject(MUIC_Group, MUIA_Group_Horiz, TRUE ,
376 MUIA_InnerLeft,(0),
377 MUIA_InnerRight,(0),
378 MUIA_InnerTop,(0),
379 MUIA_InnerBottom,(0),
380 MUIA_Group_Spacing, 0,
381 MUIA_FixHeight, 1,
382 MUIA_Frame, MUIV_Frame_None,
383 MUIA_Background, MUII_SHADOW,
384 Child, (IPTR)MUI_NewObject(MUIC_Rectangle,
385 MUIA_Frame, MUIV_Frame_None,
386 TAG_DONE),
387 TAG_DONE),
388 Child, (IPTR)MUI_NewObject(MUIC_Group, MUIA_Group_Horiz, TRUE ,
389 MUIA_InnerLeft,(4),
390 MUIA_InnerRight,(4),
391 MUIA_InnerTop,(4),
392 MUIA_InnerBottom,(4),
393 MUIA_Frame, MUIV_Frame_None,
394 MUIA_Weight, 100,
395 Child, (IPTR)MUI_NewObject(MUIC_Group, MUIA_Group_Horiz, TRUE ,
396 MUIA_InnerLeft,(0),
397 MUIA_InnerRight,(0),
398 MUIA_InnerTop,(0),
399 MUIA_InnerBottom,(0),
400 MUIA_Weight, 100,
401 Child, (IPTR)( panelStatusBar_TextStatus = MUI_NewObject(MUIC_Text,
402 MUIA_Font, MUIV_Font_Tiny,
403 MUIA_Text_Contents, (IPTR)"",
404 TAG_DONE) ),
405 TAG_DONE),
406 TAG_DONE),
407 TAG_DONE);
409 /* Got a StatusBarPanel? setup notifies and other values are
410 copied to the data of the object */
411 if ( panel_StatusBar != NULL )
413 D(bug("[IW.statusbar] %s: StatusBar Obj @ 0x%p\n", __PRETTY_FUNCTION__, panel_StatusBar));
415 panelStatusBarPrivate->iwp_StatusBar_StatusTextObj = panelStatusBar_TextStatus;
416 panelStatusBarPrivate->iwp_StatusBar_StatusBarObj = panel_StatusBar;
418 if (DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, MUIM_Group_InitChange ))
420 DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, OM_ADDMEMBER, (IPTR)panel_StatusBar);
421 if (data->iwd_BottomPanel.iwp_PanelGroupSpacerObj)
423 DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, OM_REMMEMBER, (IPTR)data->iwd_BottomPanel.iwp_PanelGroupSpacerObj);
424 data->iwd_BottomPanel.iwp_PanelGroupSpacerObj = NULL;
427 DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, MUIM_Group_ExitChange);
430 if (panelStatusBarPrivate->iwp_StatusBar_StatusBarObj)
432 #ifdef __AROS__
433 panelStatusBarPrivate->iwp_StatusBar_updateHook.h_Entry = ( HOOKFUNC )panelStatusBar__HookFunc_UpdateStatusFunc;
434 #else
435 panelStatusBarPrivate->iwp_StatusBar_updateHook= &StatusBar_updateHook;
436 #endif
437 DoMethod (
438 data->iwd_IconListObj, MUIM_Notify, MUIA_IconList_Changed, MUIV_EveryTime,
439 (IPTR)self, 3, MUIM_CallHook, &panelStatusBarPrivate->iwp_StatusBar_updateHook, (IPTR)CLASS
442 if (!(panelStatusBar__PrefsNotificationObject))
443 panelStatusBar__PrefsSetup(CLASS, self, message);
445 DoMethod
447 panelStatusBar__PrefsNotificationObject, MUIM_Notify, MUIA_ShowMe, MUIV_EveryTime,
448 (IPTR)data->iwd_BottomPanel.iwp_PanelContainerObj, 3, MUIM_Set, MUIA_ShowMe, MUIV_TriggerValue
451 if ((strcasecmp(extension_PrefsData, "True")) == 0)
453 SET(data->iwd_BottomPanel.iwp_PanelContainerObj, MUIA_ShowMe, TRUE);
456 AddTail(&panelStatusBar__StatusBars, &panelStatusBarPrivate->iwp_Node);
459 else
461 panelStatusBarPrivate->iwp_StatusBar_StatusBarObj = NULL;
464 return 0;
468 ///panelStatusBar__Cleanup()
469 static IPTR panelStatusBar__Cleanup(Class *CLASS, Object *self, Msg msg)
471 SETUP_ICONWINDOW_INST_DATA;
472 struct panel_StatusBar_DATA *panelStatusBarPrivate;
474 D(bug("[IW.statusbar]: %s()\n", __PRETTY_FUNCTION__));
476 if ((panelStatusBarPrivate = (struct panel_StatusBar_DATA *)data->iwd_TopPanel.iwp_PanelPrivate) != NULL)
478 if (panelStatusBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
479 return 0;
481 if (panelStatusBar__PrefsNotificationObject)
483 DoMethod
485 panelStatusBar__PrefsNotificationObject, MUIM_KillNotifyObj, MUIA_ShowMe,
486 (IPTR)data->iwd_BottomPanel.iwp_PanelContainerObj
490 return 0;
494 ///panelStatusBar__OM_GET()
495 static IPTR panelStatusBar__OM_GET(Class *CLASS, Object *self, struct opGet *message)
497 SETUP_ICONWINDOW_INST_DATA;
499 struct panel_StatusBar_DATA *panelStatusBarPrivate = NULL;
500 // IPTR *store = message->opg_Storage;
501 IPTR rv = FALSE;
503 D(bug("[IW.statusbar]: %s()\n", __PRETTY_FUNCTION__));
505 if ((panelStatusBarPrivate = (struct panel_StatusBar_DATA *)data->iwd_BottomPanel.iwp_PanelPrivate) != NULL)
507 if (panelStatusBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
508 return rv;
510 switch (message->opg_AttrID)
514 return rv;
518 ///OM_SET()
519 static IPTR panelStatusBar__OM_SET(Class *CLASS, Object *self, struct opSet *message)
521 SETUP_ICONWINDOW_INST_DATA;
523 struct panel_StatusBar_DATA *panelStatusBarPrivate = NULL;
524 struct TagItem *tstate = message->ops_AttrList, *tag;
525 // BOOL UpdateIconlist = FALSE;
526 // IPTR focusicon = (IPTR) NULL;
527 IPTR rv = FALSE;
529 D(bug("[IW.statusbar]: %s()\n", __PRETTY_FUNCTION__));
531 if ((panelStatusBarPrivate = (struct panel_StatusBar_DATA *)data->iwd_BottomPanel.iwp_PanelPrivate) != NULL)
533 if (panelStatusBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
534 return rv;
536 while ((tag = NextTagItem((TAGITEM)&tstate)) != NULL)
538 switch (tag->ti_Tag)
540 /*case MUIA_IconWindowExt_ToolBar_Enabled:
541 if ((!(data->iwd_Flags & IWDFLAG_ISROOT)) && (data->iwd_BottomPanel.iwp_PanelContainerObj))
543 // remove statusbar
544 if (!(( BOOL )tag->ti_Data))
546 if (panelStatusBarPrivate->iwp_StatusBar_StatusBarObj != NULL)
548 data->iwd_BottomPanel.iwp_PanelGroupSpacerObj = HSpace(0);
550 SET(data->iwd_BottomPanel.iwp_PanelContainerObj, MUIA_Frame, MUIV_Frame_None);
551 SET(data->iwd_BottomPanel.iwp_PanelContainerObj, MUIA_Group_Spacing, 0);
553 if ((data->iwd_BottomPanel.iwp_PanelGroupSpacerObj) && (DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, MUIM_Group_InitChange)))
555 DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, OM_REMMEMBER, (IPTR)panelStatusBarPrivate->iwp_StatusBar_StatusBarObj);
556 DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, OM_ADDMEMBER, (IPTR)data->iwd_BottomPanel.iwp_PanelGroupSpacerObj);
557 DoMethod(data->iwd_BottomPanel.iwp_PanelGroupObj, MUIM_Group_ExitChange);
558 panelStatusBarPrivate->iwp_StatusBar_StatusBarObj = NULL;
562 else
564 // setup statusbar
565 if (panelStatusBarPrivate->iwp_StatusBar_StatusBarObj == NULL)
567 Object *prefs = NULL;
568 GET(_app(self), MUIA_Wanderer_Prefs, &prefs);
569 panelStatusBar__Setup(CLASS, self, prefs);
573 break;*/
577 return rv;
580 #define PANELSTATUSBAR_PRIORITY 10
582 IPTR panelStatusBar__Init()
584 D(bug("[IW.statusbar]: %s()\n", __PRETTY_FUNCTION__));
586 panelStatusBar__Extension.iwe_Node.ln_Pri = PANELSTATUSBAR_PRIORITY;
587 panelStatusBar__Extension.iwe_Node.ln_Name = (char *)extension_Name;
588 panelStatusBar__Extension.iwe_Setup = panelStatusBar__Setup;
589 panelStatusBar__Extension.iwe_Cleanup = panelStatusBar__Cleanup;
590 panelStatusBar__Extension.iwe_Set = panelStatusBar__OM_SET;
591 panelStatusBar__Extension.iwe_Get = panelStatusBar__OM_GET;
593 NewList(&panelStatusBar__StatusBars);
595 Enqueue(&iconwindow_Extensions, (struct Node *)&panelStatusBar__Extension);
597 D(bug("[IconWindow] %s: Added Extension '%s' @ %p to list @ %p\n", __PRETTY_FUNCTION__, panelStatusBar__Extension.iwe_Node.ln_Name, &panelStatusBar__Extension.iwe_Node, &iconwindow_Extensions));
599 return TRUE;
602 ADD2INIT(panelStatusBar__Init, PANELSTATUSBAR_PRIORITY);