2 Copyright © 2006-2011, The AROS Development Team. All rights reserved.
6 #define MUIMASTER_YES_INLINE_STDARG
9 #include <aros/debug.h>
11 #include <proto/alib.h>
12 #include <proto/exec.h>
13 #include <proto/intuition.h>
14 #include <proto/muimaster.h>
15 #include <proto/workbench.h>
16 #include <proto/dos.h>
17 #include <proto/icon.h>
20 #include <libraries/mui.h>
21 #include <workbench/startup.h>
29 char versionstring
[] = "$VER: WBNewDrawer 0.7 (6.4.2011) ©2006-2011 AROS Dev Team";
31 static STRPTR
AllocateNameFromLock(BPTR lock
);
32 static void bt_ok_hook_function(void);
33 static void Cleanup(STRPTR s
);
34 static BOOL
doNewDrawer(void);
35 static void MakeGUI(void);
36 static CONST_STRPTR
SelectDefaultName(STRPTR basename
);
38 static Object
*app
, *window
, *bt_ok
, *bt_cancel
, *cm_icons
, *str_name
;
39 static struct Hook bt_ok_hook
;
40 static BPTR dirlock
= (BPTR
)-1;
41 static BPTR oldlock
= (BPTR
)-1;
42 static STRPTR illegal_chars
= "/:";
44 int main(int argc
, char **argv
)
46 struct WBStartup
*startup
;
50 PutStr(_(MSG_WB_ONLY
));
53 startup
= (struct WBStartup
*) argv
;
55 D(bug("[NewDrawer] Args %d\n", startup
->sm_NumArgs
));
57 if (startup
->sm_NumArgs
!= 2)
58 Cleanup(_(MSG_NEEDS_MORE_ARGS
));
60 dirlock
= startup
->sm_ArgList
[1].wa_Lock
;
62 Cleanup(_(MSG_INVALID_LOCK
));
64 oldlock
= CurrentDir(dirlock
);
67 STRPTR fullname
= AllocateNameFromLock(dirlock
);
68 UpdateWorkbenchObject(fullname
, WBDRAWER
, TAG_DONE
);
76 static void MakeGUI(void)
78 CONST_STRPTR defname
= SelectDefaultName("");
79 bt_ok_hook
.h_Entry
= (APTR
)bt_ok_hook_function
;
80 app
= (Object
*)ApplicationObject
,
81 MUIA_Application_Title
, __(MSG_TITLE
),
82 MUIA_Application_Version
, (IPTR
) versionstring
,
83 MUIA_Application_Copyright
, __(MSG_COPYRIGHT
),
84 MUIA_Application_Author
, (IPTR
) "The AROS Development Team",
85 MUIA_Application_Description
, __(MSG_DESCRIPTION
),
86 MUIA_Application_Base
, (IPTR
) "NEWDRAWER",
87 MUIA_Application_UseCommodities
, FALSE
,
88 MUIA_Application_UseRexx
, FALSE
,
89 SubWindow
, (IPTR
)(window
= (Object
*)WindowObject
,
90 MUIA_Window_Title
, __(MSG_WINDOW_TITLE
),
91 MUIA_Window_NoMenus
, TRUE
,
92 MUIA_Window_CloseGadget
, FALSE
,
93 WindowContents
, (IPTR
) (VGroup
,
94 MUIA_Frame
, MUIV_Frame_Group
,
95 Child
, (IPTR
) (HGroup
,
96 Child
, (IPTR
) HVSpace
,
97 Child
, (IPTR
) Label2(__(MSG_LINE
)),
99 Child
, (IPTR
) (ColGroup(2),
100 Child
, (IPTR
) Label2(__(MSG_NAME
)),
101 Child
, (IPTR
)(str_name
= (Object
*)StringObject
,
103 MUIA_String_Contents
, (IPTR
) defname
,
104 MUIA_String_MaxLen
, MAXFILENAMELENGTH
,
105 MUIA_String_Reject
, (IPTR
) illegal_chars
,
106 MUIA_String_Columns
, -1,
107 MUIA_Frame
, MUIV_Frame_String
,
109 Child
, (IPTR
) Label2(__(MSG_ICON
)),
110 Child
, (IPTR
)(HGroup
,
111 Child
, (IPTR
) (cm_icons
= MUI_MakeObject(MUIO_Checkmark
, NULL
)),
112 Child
, (IPTR
) HVSpace
,
115 Child
, (IPTR
) (RectangleObject
,
116 MUIA_Rectangle_HBar
, TRUE
,
119 Child
, (IPTR
) (HGroup
,
120 Child
, (IPTR
) (bt_ok
= ImageButton(__(MSG_OK
), "THEME:Images/Gadgets/OK")),
121 Child
, (IPTR
) (bt_cancel
= ImageButton(__(MSG_CANCEL
),"THEME:Images/Gadgets/Cancel")),
126 FreeVec((APTR
) defname
);
129 Cleanup(_(MSG_FAILED_CREATE_APP
));
131 DoMethod(window
, MUIM_Notify
, MUIA_Window_CloseRequest
, TRUE
,
132 app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
133 DoMethod(bt_cancel
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
134 app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
135 DoMethod(bt_ok
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
136 app
, 2, MUIM_CallHook
, (IPTR
)&bt_ok_hook
);
137 set(cm_icons
, MUIA_CycleChain
, 1);
138 set(cm_icons
, MUIA_Selected
, TRUE
);
139 set(str_name
, MUIA_CycleChain
, 1);
140 set(window
, MUIA_Window_Open
, TRUE
);
141 set(window
, MUIA_Window_ActiveObject
, str_name
);
142 set(window
, MUIA_Window_DefaultObject
, bt_ok
);
143 DoMethod(app
, MUIM_Application_Execute
);
147 static void bt_ok_hook_function(void)
151 DoMethod(app
, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
155 set(window
, MUIA_Window_Activate
, TRUE
);
156 set(window
, MUIA_Window_ActiveObject
, str_name
);
161 static BOOL
doNewDrawer(void)
164 BOOL icon
= XGET(cm_icons
, MUIA_Selected
);
165 STRPTR name
= (STRPTR
) XGET(str_name
, MUIA_String_Contents
);
167 struct DiskObject
*dob
= NULL
;
168 D(bug("WBNewDrawer name %s icon %d\n", name
, icon
));
171 if(strlen(name
) == 0)
176 if (strpbrk(name
, illegal_chars
))
178 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_ILLEGAL_CHARS
), name
);
182 test
= Lock(name
, ACCESS_READ
);
186 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_ALREADY_EXIST
), name
);
190 dob
= GetDiskObject(name
);
191 // if icon exists it must be of type WBDRAWER
192 if (dob
&& (dob
->do_Type
!= WBDRAWER
))
194 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_WRONG_ICON_TYPE
), name
);
200 if ((dstLock
= CreateDir(name
)))
204 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_CANT_CREATE
), name
);
209 // if the icon already exists and is of right type then no actions happens.
210 if (icon
&& (dob
== NULL
))
212 dob
= GetDiskObjectNew(name
);
213 PutDiskObject(name
, dob
);
216 UpdateWorkbenchObject(name
, WBDRAWER
, TAG_DONE
);
226 * Try to find an unique file name.
227 * You have to free the return value with FreeVec.
230 static CONST_STRPTR
SelectDefaultName(STRPTR basename
)
232 if (basename
== NULL
)
233 basename
= "Rename_Me";
237 STRPTR buffer
= AllocVec(strlen(basename
) + 3, MEMF_ANY
);
246 strcpy(buffer
, basename
);
248 sprintf(buffer
,"%s_%d", basename
, (int)number
);
250 test
= Lock(buffer
, ACCESS_READ
);
253 } while ((number
< 10) && (test
!= BNULL
));
257 // Empty buffer by default
258 sprintf(buffer
,"%s", basename
);
264 static STRPTR
AllocateNameFromLock(BPTR lock
)
267 STRPTR buffer
= NULL
;
274 buffer
= AllocVec(length
, MEMF_ANY
);
277 if (NameFromLock(lock
, buffer
, length
))
284 if (IoErr() == ERROR_LINE_TOO_LONG
)
297 SetIoErr(ERROR_NO_FREE_STORE
);
314 static void Cleanup(STRPTR s
)
316 MUI_DisposeObject(app
);
318 if (oldlock
!= (BPTR
)-1)
325 struct EasyStruct es
;
326 es
.es_StructSize
= sizeof(struct EasyStruct
);
328 es
.es_Title
= _(MSG_ERROR_TITLE
);
329 es
.es_TextFormat
= s
;
330 es
.es_GadgetFormat
= _(MSG_OK
);
331 EasyRequest(NULL
, &es
, NULL
, NULL
);