delint type punned pointer
[AROS.git] / workbench / system / Wanderer / panel_toolbar.c
blobd16aa11f2d0c336ad20d17b095cd43551ee68a23
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 #define ICONWINDOW_OPTION_NOSEARCHBUTTON
13 #ifdef __AROS__
14 #define DEBUG 0
15 #include <aros/debug.h>
16 #endif
18 #include <exec/types.h>
19 #include <libraries/mui.h>
21 #ifdef __AROS__
22 #include <zune/customclasses.h>
23 #else
24 #include <zune_AROS/customclasses.h>
25 #endif
27 #include <proto/utility.h>
29 #include <proto/graphics.h>
31 #include <proto/exec.h>
32 #include <proto/datatypes.h>
34 #include <dos/dos.h>
35 #include <proto/dos.h>
37 #include <proto/icon.h>
39 #include <stdio.h>
40 #include <string.h>
42 #include <intuition/screens.h>
43 #include <datatypes/pictureclass.h>
44 #include <clib/macros.h>
46 #ifdef __AROS__
47 #include <clib/alib_protos.h>
49 #include <prefs/wanderer.h>
50 #else
51 #include <prefs_AROS/wanderer.h>
52 #endif
54 #if defined(__AMIGA__) && !defined(__PPC__)
55 #define NO_INLINE_STDARG
56 #endif
57 #ifndef _PROTO_INTUITION_H
58 #include <proto/intuition.h>
59 #endif
60 #include <proto/muimaster.h>
62 #include "Classes/iconlist.h"
63 #include "Classes/iconlistview.h"
64 #include "Classes/iconlist_attributes.h"
65 #include "wanderer.h"
66 #include "wandererprefs.h"
68 #include "iconwindow.h"
69 #include "iconwindow_attributes.h"
71 extern struct List iconwindow_Extensions;
73 /*** Private Data *********************************************************/
75 static CONST_STRPTR extension_Name = "IconWindow ToolBar Extension";
76 static CONST_STRPTR extension_PrefsFile = "ENV:SYS/Wanderer/toolbar.prefs";
77 static STRPTR extension_PrefsData;
78 static struct iconWindow_Extension panelToolBar__Extension;
79 struct List panelToolBar__ToolBars;
80 struct NotifyRequest panelToolBar__PrefsNotifyRequest;
81 Object *panelToolBar__PrefsNotificationObject;
83 struct panel_ToolBar_DATA
85 struct Node iwp_Node;
87 Object *iwp_ToolBar_ToolBarObj;
88 Object *iwp_ToolBar_LocationStringObj;
89 #ifdef __AROS__
90 struct Hook iwp_ToolBar_LocationStrHook;
91 #else
92 struct Hook *iwp_ToolBar_LocationStrHook;
93 #endif
96 ///ExpandEnvName()
97 /* Expand a passed in env: string to its full location */
98 /* Wanderer doesnt free this mem at the moment but should
99 incase it is every closed */
100 STRPTR ExpandEnvName(CONST_STRPTR env_path)
102 BOOL ok = FALSE;
103 char tmp_envbuff[1024];
104 STRPTR fullpath = NULL;
105 BPTR env_lock = (BPTR) NULL;
107 env_lock = Lock("ENV:", SHARED_LOCK);
108 if (env_lock)
110 if (NameFromLock(env_lock, tmp_envbuff, 256)) ok = TRUE;
111 UnLock(env_lock);
114 if (ok)
116 if ((fullpath = AllocVec(strlen(tmp_envbuff) + strlen(env_path) + 1 + 1 - 4, MEMF_CLEAR | MEMF_PUBLIC)) != NULL)
118 strcpy(fullpath, tmp_envbuff);
119 AddPart(fullpath, env_path + 4, 1019);
120 return fullpath;
124 // We couldn't expand it so just use as is, but the
125 // caller is expecting a newly allocated string...
126 fullpath = AllocVec(strlen(env_path) + 1, MEMF_PUBLIC);
127 CopyMem(env_path, fullpath, strlen(env_path) + 1);
129 return fullpath;
133 /*** Hook functions *********************************************************/
135 ///panelToolBar__HookFunc_LocationStringFunc()
136 #ifdef __AROS__
137 AROS_UFH3(
138 void, panelToolBar__HookFunc_LocationStringFunc,
139 AROS_UFHA(struct Hook *, hook, A0),
140 AROS_UFHA(APTR *, obj, A2),
141 AROS_UFHA(APTR, param, A1)
144 #else
145 HOOKPROTO(panelToolBar__HookFunc_LocationStringFunc, void, APTR *obj, APTR param)
147 #endif
148 AROS_USERFUNC_INIT
150 /* Get data */
151 Object *self = ( Object *)obj;
152 Class *CLASS = *( Class **)param;
153 STRPTR str = NULL;
154 BPTR fp = (BPTR) NULL;
155 struct FileInfoBlock *fib;
156 struct panel_ToolBar_DATA *panelToolBarPrivate = NULL;
158 SETUP_ICONWINDOW_INST_DATA;
160 /* Only change dir if it is a valid directory/volume */
161 if ((panelToolBarPrivate = (struct panel_ToolBar_DATA *)data->iwd_TopPanel.iwp_PanelPrivate) != NULL)
163 if (panelToolBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
164 return;
166 GET(panelToolBarPrivate->iwp_ToolBar_LocationStringObj, MUIA_String_Contents, &str);
168 /* TODO: Signal that it is a wrong path */
169 /* so that the user understands (here where we abort with return) */
171 fib = AllocDosObject(DOS_FIB, NULL);
172 if (!fib)
173 return;
175 if (!(fp = Lock(str, ACCESS_READ)))
177 FreeDosObject(DOS_FIB, fib);
178 return;
181 if (!(Examine(fp, fib)))
183 UnLock (fp );
184 FreeDosObject(DOS_FIB, fib);
185 return;
188 /* Change directory! */
189 if (fib->fib_DirEntryType >= 0)
191 SET(self, MUIA_IconWindow_Location, (IPTR)str);
194 UnLock(fp);
196 FreeDosObject(DOS_FIB, fib);
199 AROS_USERFUNC_EXIT
201 #ifndef __AROS__
202 MakeStaticHook(ToolBar_locationstrHook, panelToolBar__HookFunc_LocationStringFunc);
203 #endif
205 #define TOOLBAR_PREFSSIZE 1024
206 IPTR panelToolBar__HandleFSUpdate(Object *WandererObj, struct NotifyMessage *msg)
208 if (GetVar(extension_PrefsFile, extension_PrefsData, TOOLBAR_PREFSSIZE, GVF_GLOBAL_ONLY) != -1)
210 D(bug("[IW.toolbar] %s: Prefs contain '%s'\n", __PRETTY_FUNCTION__, extension_PrefsData));
211 if ((strcasecmp(extension_PrefsData, "True")) == 0)
213 SET(panelToolBar__PrefsNotificationObject, MUIA_ShowMe, TRUE);
215 else
217 SET(panelToolBar__PrefsNotificationObject, MUIA_ShowMe, FALSE);
220 return 0;
223 /*** Main Functions ****************************************************************/
225 ///panelToolBar__PrefsSetup()
226 IPTR panelToolBar__PrefsSetup(Class *CLASS, Object *self, struct opSet *message)
228 IPTR panelToolBarFSNotifyPort = 0;
229 struct panel_ToolBar_DATA *panelToolBarPrivate = NULL;
230 struct List *panelToolBarFSNotifyList = NULL;
232 SETUP_ICONWINDOW_INST_DATA;
234 panelToolBarFSNotifyPort = GetTagData(MUIA_Wanderer_FileSysNotifyPort, (IPTR) NULL, message->ops_AttrList);
235 panelToolBarFSNotifyList = (APTR)GetTagData(MUIA_Wanderer_FileSysNotifyList, (IPTR) NULL, message->ops_AttrList);
237 D(bug("[IW.toolbar]: %s()\n", __PRETTY_FUNCTION__));
239 if ((panelToolBarPrivate = (struct panel_ToolBar_DATA *)data->iwd_TopPanel.iwp_PanelPrivate) != NULL)
241 if (panelToolBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
242 return 0;
244 // FIXME: this is never freed
245 extension_PrefsData = AllocVec(TOOLBAR_PREFSSIZE, MEMF_CLEAR);
246 if (extension_PrefsData == NULL)
247 return 0;
249 /* Setup notification on prefs file --------------------------------*/
250 struct Wanderer_FSHandler *_prefsNotifyHandler = NULL;
252 if ((_prefsNotifyHandler = AllocMem(sizeof(struct Wanderer_FSHandler), MEMF_CLEAR)) != NULL)
254 _prefsNotifyHandler->fshn_Node.ln_Name = ExpandEnvName(extension_PrefsFile);
255 panelToolBar__PrefsNotifyRequest.nr_Name = _prefsNotifyHandler->fshn_Node.ln_Name;
256 panelToolBar__PrefsNotifyRequest.nr_Flags = NRF_SEND_MESSAGE;
257 panelToolBar__PrefsNotifyRequest.nr_stuff.nr_Msg.nr_Port = (struct MsgPort *)panelToolBarFSNotifyPort;
258 _prefsNotifyHandler->HandleFSUpdate = panelToolBar__HandleFSUpdate;
260 if (StartNotify(&panelToolBar__PrefsNotifyRequest))
262 D(bug("[IW.toolbar]%s: Prefs-notification setup on '%s'\n", __PRETTY_FUNCTION__, panelToolBar__PrefsNotifyRequest.nr_Name));
264 else
266 D(bug("[IW.toolbar] %s: FAILED to setup Prefs-notification!\n", __PRETTY_FUNCTION__));
268 AddTail(panelToolBarFSNotifyList, &_prefsNotifyHandler->fshn_Node);
270 #ifdef __AROS__
271 panelToolBar__PrefsNotificationObject = (Object *)NotifyObject, End;
272 #else
273 panelToolBar__PrefsNotificationObject = MUI_NewObject(MUIC_Notify, TAG_DONE);
274 #endif
275 if (GetVar(extension_PrefsFile, extension_PrefsData, TOOLBAR_PREFSSIZE, GVF_GLOBAL_ONLY) != -1)
277 D(bug("[IW.toolbar] %s: Prefs contain '%s'\n", __PRETTY_FUNCTION__, extension_PrefsData));
280 return 0;
284 ///panelToolBar__Setup()
285 IPTR panelToolBar__Setup(Class *CLASS, Object *self, struct opSet *message)
287 SETUP_ICONWINDOW_INST_DATA;
289 Object *panel_ToolBar;
290 Object *panelToolBar_ButtonDirUp = NULL,
291 *panelToolBar_ButtonSearch = NULL,
292 *panelToolBar_StringLocation = NULL;
294 struct panel_ToolBar_DATA *panelToolBarPrivate = NULL;
296 if (!(!(data->iwd_Flags & IWDFLAG_ISROOT) && data->iwd_TopPanel.iwp_PanelContainerObj))
297 return (IPTR)NULL;
299 #if !defined(ICONWINDOW_OPTION_NOSEARCHBUTTON)
300 panelToolBar_ButtonSearch = ImageButton("", "THEME:Images/Gadgets/Search");
301 #endif
303 D(bug("[IW.toolbar]: %s()\n", __PRETTY_FUNCTION__));
305 if (data->iwd_TopPanel.iwp_PanelPrivate == (IPTR)NULL)
307 if ((data->iwd_TopPanel.iwp_PanelPrivate = (IPTR)AllocVec(sizeof(struct panel_ToolBar_DATA), MEMF_CLEAR)) == (IPTR)NULL)
308 return 0;
310 panelToolBarPrivate = (struct panel_ToolBar_DATA *)data->iwd_TopPanel.iwp_PanelPrivate;
311 panelToolBarPrivate->iwp_Node.ln_Name = (char *)extension_Name;
313 /* Create the "ToolBar" panel object .. */
314 panel_ToolBar = MUI_NewObject(MUIC_Group,
315 MUIA_InnerLeft,(0),
316 MUIA_InnerRight,(0),
317 MUIA_InnerTop,(0),
318 MUIA_InnerBottom,(0),
319 MUIA_Frame, MUIV_Frame_None,
320 Child, (IPTR)MUI_NewObject(MUIC_Group, MUIA_Group_Horiz, TRUE ,
321 MUIA_InnerLeft,(4),
322 MUIA_InnerRight,(4),
323 MUIA_InnerTop,(4),
324 MUIA_InnerBottom,(4),
325 MUIA_Frame, MUIV_Frame_None,
326 MUIA_Weight, 100,
327 Child, (IPTR)MUI_NewObject(MUIC_Group, MUIA_Group_Horiz, TRUE ,
328 MUIA_InnerLeft,(0),
329 MUIA_InnerRight,(0),
330 MUIA_InnerTop,(0),
331 MUIA_InnerBottom,(0),
332 MUIA_Weight, 100,
333 Child, (IPTR)( panelToolBar_StringLocation = MUI_NewObject(MUIC_String,
334 MUIA_String_Contents, (IPTR)"",
335 MUIA_CycleChain, 1,
336 MUIA_Frame, MUIV_Frame_String,
337 TAG_DONE) ),
338 TAG_DONE),
339 Child, (IPTR)MUI_NewObject(MUIC_Group, MUIA_Group_Horiz, TRUE ,
340 MUIA_InnerLeft,(0),
341 MUIA_InnerRight,(0),
342 MUIA_InnerTop,(0),
343 MUIA_InnerBottom,(0),
344 MUIA_HorizWeight, 0,
345 MUIA_VertWeight, 100,
346 Child, (IPTR) (panelToolBar_ButtonDirUp = ImageButton("", "THEME:Images/Gadgets/DirUp")),
347 (panelToolBar_ButtonSearch ? Child : TAG_IGNORE), (IPTR) (panelToolBar_ButtonSearch),
348 TAG_DONE),
349 TAG_DONE),
350 Child, (IPTR)MUI_NewObject(MUIC_Group, MUIA_Group_Horiz, TRUE ,
351 MUIA_InnerLeft,(0),
352 MUIA_InnerRight,(0),
353 MUIA_InnerTop,(0),
354 MUIA_InnerBottom,(0),
355 MUIA_Group_Spacing, 0,
356 MUIA_FixHeight, 1,
357 MUIA_Frame, MUIV_Frame_None,
358 MUIA_Background, MUII_SHADOW,
359 Child, (IPTR)MUI_NewObject(MUIC_Rectangle,
360 MUIA_Frame, MUIV_Frame_None,
361 TAG_DONE),
362 TAG_DONE),
363 TAG_DONE);
365 /* Got a toolbarpanel? setup notifies and other values are
366 copied to the data of the object */
367 if ( panel_ToolBar != NULL )
369 D(bug("[IW.toolbar] %s: ToolBar Obj @ 0x%p\n", __PRETTY_FUNCTION__, panel_ToolBar));
371 SET(panelToolBar_ButtonDirUp, MUIA_Background, XGET( panel_ToolBar, MUIA_Background ) );
372 SET(panelToolBar_ButtonDirUp, MUIA_CycleChain, 1);
373 SET(panelToolBar_ButtonDirUp, MUIA_Frame, MUIV_Frame_None );
374 #if !defined(ICONWINDOW_OPTION_NOSEARCHBUTTON)
375 SET(panelToolBar_ButtonSearch, MUIA_Background, XGET( panel_ToolBar, MUIA_Background ) );
376 SET(panelToolBar_ButtonSearch, MUIA_CycleChain, 1);
377 SET(panelToolBar_ButtonSearch, MUIA_Frame, MUIV_Frame_None );
378 #endif
379 panelToolBarPrivate->iwp_ToolBar_ToolBarObj = panel_ToolBar;
381 if (DoMethod(data->iwd_TopPanel.iwp_PanelGroupObj, MUIM_Group_InitChange ))
383 DoMethod(data->iwd_TopPanel.iwp_PanelGroupObj, OM_ADDMEMBER, (IPTR)panel_ToolBar);
384 if (data->iwd_TopPanel.iwp_PanelGroupSpacerObj)
386 DoMethod(data->iwd_TopPanel.iwp_PanelGroupObj, OM_REMMEMBER, (IPTR)data->iwd_TopPanel.iwp_PanelGroupSpacerObj);
387 data->iwd_TopPanel.iwp_PanelGroupSpacerObj = NULL;
390 DoMethod(data->iwd_TopPanel.iwp_PanelGroupObj, MUIM_Group_ExitChange);
393 if (panelToolBarPrivate->iwp_ToolBar_ToolBarObj)
395 DoMethod(
396 panelToolBar_ButtonDirUp, MUIM_Notify, MUIA_Pressed, FALSE,
397 (IPTR)self, 1, MUIM_IconWindow_DirectoryUp
400 panelToolBarPrivate->iwp_ToolBar_LocationStringObj = panelToolBar_StringLocation;
401 #ifdef __AROS__
402 panelToolBarPrivate->iwp_ToolBar_LocationStrHook.h_Entry = ( HOOKFUNC )panelToolBar__HookFunc_LocationStringFunc;
403 #else
404 panelToolBarPrivate->iwp_ToolBar_LocationStrHook= &ToolBar_locationstrHook;
405 #endif
407 NNSET(
408 panelToolBarPrivate->iwp_ToolBar_LocationStringObj, MUIA_String_Contents,
409 XGET(data->iwd_IconListObj, MUIA_IconDrawerList_Drawer)
412 /* Make changes to string contents change dir on enter */
413 DoMethod (
414 panelToolBarPrivate->iwp_ToolBar_LocationStringObj, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
415 (IPTR)self, 3, MUIM_CallHook, &panelToolBarPrivate->iwp_ToolBar_LocationStrHook, (IPTR)CLASS
418 if (!(panelToolBar__PrefsNotificationObject))
419 panelToolBar__PrefsSetup(CLASS, self, message);
421 DoMethod
423 panelToolBar__PrefsNotificationObject, MUIM_Notify, MUIA_ShowMe, MUIV_EveryTime,
424 (IPTR)data->iwd_TopPanel.iwp_PanelContainerObj, 3, MUIM_Set, MUIA_ShowMe, MUIV_TriggerValue
427 if ((strcasecmp(extension_PrefsData, "True")) == 0)
429 SET(data->iwd_TopPanel.iwp_PanelContainerObj, MUIA_ShowMe, TRUE);
432 AddTail(&panelToolBar__ToolBars, &panelToolBarPrivate->iwp_Node);
435 else
437 panelToolBarPrivate->iwp_ToolBar_ToolBarObj = NULL;
440 return 0;
444 IPTR panelToolBar__Cleanup(Class *CLASS, Object *self, Msg msg)
446 SETUP_ICONWINDOW_INST_DATA;
448 struct panel_ToolBar_DATA *panelToolBarPrivate;
450 D(bug("[IW.toolbar]: %s()\n", __PRETTY_FUNCTION__));
452 if ((panelToolBarPrivate = (struct panel_ToolBar_DATA *)data->iwd_TopPanel.iwp_PanelPrivate) != NULL)
454 if (panelToolBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
455 return 0;
457 if (panelToolBar__PrefsNotificationObject)
459 DoMethod
461 panelToolBar__PrefsNotificationObject, MUIM_KillNotifyObj, MUIA_ShowMe,
462 (IPTR)data->iwd_TopPanel.iwp_PanelContainerObj
465 if (panelToolBarPrivate->iwp_ToolBar_LocationStringObj)
467 DoMethod
469 panelToolBarPrivate->iwp_ToolBar_LocationStringObj, MUIM_KillNotifyObj, MUIA_String_Acknowledge,
470 (IPTR)self
474 return 0;
477 ///OM_GET()
478 IPTR panelToolBar__OM_GET(Class *CLASS, Object *self, struct opGet *message)
480 SETUP_ICONWINDOW_INST_DATA;
482 struct panel_ToolBar_DATA *panelToolBarPrivate = NULL;
483 // IPTR *store = message->opg_Storage;
484 IPTR rv = FALSE;
486 D(bug("[IW.toolbar]: %s()\n", __PRETTY_FUNCTION__));
488 if ((panelToolBarPrivate = (struct panel_ToolBar_DATA *)data->iwd_TopPanel.iwp_PanelPrivate) != NULL)
490 if (panelToolBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
491 return rv;
493 switch (message->opg_AttrID)
498 return rv;
501 ///OM_SET()
502 IPTR panelToolBar__OM_SET(Class *CLASS, Object *self, struct opSet *message)
504 SETUP_ICONWINDOW_INST_DATA;
506 struct panel_ToolBar_DATA *panelToolBarPrivate = NULL;
507 struct TagItem *tstate = message->ops_AttrList, *tag;
508 // BOOL UpdateIconlist = FALSE;
509 // IPTR focusicon = (IPTR) NULL;
510 IPTR rv = FALSE;
512 D(bug("[IW.toolbar]: %s()\n", __PRETTY_FUNCTION__));
514 if ((panelToolBarPrivate = (struct panel_ToolBar_DATA *)data->iwd_TopPanel.iwp_PanelPrivate) != NULL)
516 if (panelToolBarPrivate->iwp_Node.ln_Name != (char *)extension_Name)
517 return rv;
519 while ((tag = NextTagItem((TAGITEM)&tstate)) != NULL)
521 switch (tag->ti_Tag)
523 case MUIA_IconWindow_Location:
524 D(bug("[IW.toolbar] %s: MUIA_IconWindow_Location '%s'\n", __PRETTY_FUNCTION__, data->iwd_DirectoryPath));
526 SET(panelToolBarPrivate->iwp_ToolBar_LocationStringObj, MUIA_String_Contents, (IPTR)data->iwd_DirectoryPath);
528 break;
533 return rv;
536 #define PANELTOOLBAR_PRIORITY 10
538 IPTR panelToolBar__Init()
540 D(bug("[IW.toolbar]: %s()\n", __PRETTY_FUNCTION__));
542 panelToolBar__Extension.iwe_Node.ln_Pri = PANELTOOLBAR_PRIORITY;
543 panelToolBar__Extension.iwe_Node.ln_Name = (char *)extension_Name;
544 panelToolBar__Extension.iwe_Setup = panelToolBar__Setup;
545 panelToolBar__Extension.iwe_Cleanup = panelToolBar__Cleanup;
546 panelToolBar__Extension.iwe_Set = panelToolBar__OM_SET;
547 panelToolBar__Extension.iwe_Get = panelToolBar__OM_GET;
549 NewList(&panelToolBar__ToolBars);
551 Enqueue(&iconwindow_Extensions, (struct Node *)&panelToolBar__Extension);
553 D(bug("[IconWindow] %s: Added Extension '%s' @ %p to list @ %p\n", __PRETTY_FUNCTION__, panelToolBar__Extension.iwe_Node.ln_Name, &panelToolBar__Extension.iwe_Node, &iconwindow_Extensions));
555 return TRUE;
558 ADD2INIT(panelToolBar__Init, PANELTOOLBAR_PRIORITY);