Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / diskimage / zune_gui / drivelist.c
blobe16d976e40418498e7279f95c42f691743ea73f1
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
27 #include "diskimagegui.h"
28 #include <proto/exec.h>
29 #include <proto/intuition.h>
30 #include <proto/muimaster.h>
31 #include <proto/diskimage.h>
32 #include <clib/alib_protos.h>
33 #include <SDI_hook.h>
35 #define DRIVE_COL_FMT "MINWIDTH=16 BAR,P=\33r BAR,BAR,MINWIDTH=16 BAR,"
36 enum {
37 DRIVE_COL_ICON,
38 DRIVE_COL_UNIT,
39 DRIVE_COL_DEVICE,
40 DRIVE_COL_WP,
41 DRIVE_COL_DISKIMAGE,
42 DRIVE_COL_MAX
45 enum {
46 DRIVE_ICO_DISK,
47 DRIVE_ICO_CD,
48 DRIVE_ICO_WP,
49 DRIVE_ICO_MAX
52 struct DriveListData {
53 Object *image[DRIVE_ICO_MAX];
54 IPTR handle[DRIVE_ICO_MAX];
57 static CONST CONST_STRPTR image_name[DRIVE_ICO_MAX] = {
58 "Disk",
59 "CD",
60 "Crypt"
63 static TEXT image_path[DRIVE_ICO_MAX][IMG_PATH_LEN];
65 static TEXT image_str[DRIVE_ICO_MAX][16];
67 DISPATCHERPROTO(DriveList_Dispatch);
69 struct MUI_CustomClass *DriveList_CreateClass (void) {
70 return MUI_CreateCustomClass(NULL, MUIC_List, NULL, sizeof(struct DriveListData),
71 ENTRY(DriveList_Dispatch));
74 void DriveList_FreeClass (struct MUI_CustomClass *cl) {
75 MUI_DeleteCustomClass(cl);
78 static IPTR DriveList_New(Class *cl, Object *o, struct opSet *ops);
79 static IPTR DriveList_Setup(Class *cl, Object *o, Msg msg);
80 static IPTR DriveList_Cleanup(Class *cl, Object *o, Msg msg);
82 DISPATCHER(DriveList_Dispatch) {
83 switch (msg->MethodID) {
84 case OM_NEW: return DriveList_New(cl, obj, (struct opSet *)msg);
85 case MUIM_Setup: return DriveList_Setup(cl, obj, msg);
86 case MUIM_Cleanup: return DriveList_Cleanup(cl, obj, msg);
88 return DoSuperMethodA(cl, obj, msg);
91 HOOKPROTO(DriveList_ConstructFunc, IPTR, APTR pool, struct DriveEntry *e);
92 MakeStaticHook(DriveList_ConstructHook, DriveList_ConstructFunc);
93 HOOKPROTO(DriveList_DestructFunc, IPTR, APTR pool, struct DriveEntry *e);
94 MakeStaticHook(DriveList_DestructHook, DriveList_DestructFunc);
95 HOOKPROTO(DriveList_CompareFunc, IPTR, const struct DriveEntry *e2, const struct DriveEntry *e1);
96 MakeStaticHook(DriveList_CompareHook, DriveList_CompareFunc);
97 HOOKPROTO(DriveList_DisplayFunc, IPTR, CONST_STRPTR *array, struct DriveEntry *e);
98 MakeStaticHook(DriveList_DisplayHook, DriveList_DisplayFunc);
100 static IPTR DriveList_New(Class *cl, Object *o, struct opSet *ops) {
101 Object *res;
102 struct TagItem tags[] = {
103 { MUIA_List_Title, TRUE },
104 { MUIA_List_Format, (IPTR)DRIVE_COL_FMT },
105 { MUIA_List_ConstructHook, (IPTR)&DriveList_ConstructHook },
106 { MUIA_List_DestructHook, (IPTR)&DriveList_DestructHook },
107 { MUIA_List_CompareHook, (IPTR)&DriveList_CompareHook },
108 { MUIA_List_DisplayHook, (IPTR)&DriveList_DisplayHook },
109 { TAG_MORE, (IPTR)ops->ops_AttrList }
111 ops->ops_AttrList = tags;
112 res = (Object *)DoSuperMethodA(cl, o, (Msg)ops);
113 ops->ops_AttrList = (struct TagItem *)tags[6].ti_Data;
114 return (IPTR)res;
117 static IPTR DriveList_Setup(Class *cl, Object *o, Msg msg) {
118 struct DriveListData *data = INST_DATA(cl, o);
119 int i;
120 if (!DoSuperMethodA(cl, o, msg)) {
121 return FALSE;
123 for (i = 0; i < DRIVE_ICO_MAX; i++) {
124 data->image[i] = LoadImage(image_name[i], image_path[i], NULL);
125 if (data->image[i]) {
126 data->handle[i] = DoMethod(o, MUIM_List_CreateImage, data->image[i], 0);
127 } else {
128 data->handle[i] = (IPTR)NULL;
130 SNPrintf(image_str[i], sizeof(image_str[i]), "\33O[%08lx]", data->handle[i]);
132 return TRUE;
135 static IPTR DriveList_Cleanup(Class *cl, Object *o, Msg msg) {
136 struct DriveListData *data = INST_DATA(cl, o);
137 int i;
138 for (i = 0; i < DRIVE_ICO_MAX; i++) {
139 DoMethod(o, MUIM_List_DeleteImage, data->handle[i]);
140 MUI_DisposeObject(data->image[i]);
141 image_str[i][0] = 0;
142 data->handle[i] = 0;
143 data->image[i] = NULL;
145 return DoSuperMethodA(cl, o, msg);
148 HOOKPROTO(DriveList_ConstructFunc, IPTR, APTR pool, struct DriveEntry *e) {
149 if (e) {
150 UnitControl(e->unit_num,
151 DITAG_GetDeviceType, (IPTR)&e->device_type,
152 DITAG_GetImageName, (IPTR)&e->diskimage,
153 DITAG_GetWriteProtect, (IPTR)&e->writeprotect,
154 TAG_END);
156 return (IPTR)e;
159 HOOKPROTO(DriveList_DestructFunc, IPTR, APTR pool, struct DriveEntry *e) {
160 if (e) {
161 FreeVecPooled(pool, e->unit);
162 FreeVecPooled(pool, e->drive);
163 FreeVec(e->diskimage);
164 FreeVecPooled(pool, e);
166 return 0;
169 HOOKPROTO(DriveList_CompareFunc, IPTR, const struct DriveEntry *e2, const struct DriveEntry *e1) {
170 return ((LONG)e1->unit_num - (LONG)e2->unit_num);
173 static inline LONG GetDeviceIcon (UBYTE devtype) {
174 if (devtype == DG_CDROM) {
175 return DRIVE_ICO_CD;
177 return DRIVE_ICO_DISK;
180 HOOKPROTO(DriveList_DisplayFunc, IPTR, CONST_STRPTR *array, struct DriveEntry *e) {
181 if (e) {
182 e->list_pos = (IPTR)array[-1];
183 array[DRIVE_COL_ICON] = image_str[GetDeviceIcon(e->device_type)];
184 array[DRIVE_COL_UNIT] = e->unit ? e->unit : (STRPTR)"";
185 array[DRIVE_COL_DEVICE] = e->drive ? e->drive : (STRPTR)"";
186 array[DRIVE_COL_WP] = e->writeprotect ? image_str[DRIVE_ICO_WP] : (STRPTR)"";
187 array[DRIVE_COL_DISKIMAGE] = e->diskimage ? e->diskimage : GetString(&LocaleInfo, MSG_NO_DISK);
188 } else {
189 array[DRIVE_COL_ICON] = "";
190 array[DRIVE_COL_UNIT] = GetString(&LocaleInfo, MSG_UNIT_LBL);
191 array[DRIVE_COL_DEVICE] = GetString(&LocaleInfo, MSG_DEVICE_LBL);
192 array[DRIVE_COL_WP] = GetString(&LocaleInfo, MSG_WRITEPROTECT_LBL);
193 array[DRIVE_COL_DISKIMAGE] = GetString(&LocaleInfo, MSG_FILENAME_LBL);
195 return 0;
198 HOOKPROTO(DriveList_ActiveFunc, IPTR, Object *app, IPTR *params);
199 MakeHook(DriveList_ActiveHook, DriveList_ActiveFunc);
200 HOOKPROTO(DriveList_DoubleClickFunc, IPTR, Object *app, IPTR *params);
201 MakeHook(DriveList_DoubleClickHook, DriveList_DoubleClickFunc);
203 HOOKPROTO(DriveList_ActiveFunc, IPTR, Object *app, IPTR *params) {
204 Object **gad = Gui.gad;
205 struct DriveEntry *e;
206 DoMethod(gad[GID_DRIVELIST], MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &e);
207 set(gad[GID_INSERT], MUIA_Disabled, !e);
208 set(gad[GID_EJECT], MUIA_Disabled, !e || !e->diskimage);
209 set(gad[GID_WRITEPROTECT], MUIA_Disabled, !e);
210 set(gad[GID_SETDEVICETYPE], MUIA_Disabled, !e);
211 return 0;
214 HOOKPROTO(DriveList_DoubleClickFunc, IPTR, Object *app, IPTR *params) {
215 Object **gad = Gui.gad;
216 struct DriveEntry *e;
217 DoMethod(gad[GID_DRIVELIST], MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &e);
218 if (e) {
219 if (e->diskimage) {
220 DoMethod(app, MUIM_CallHook, &EjectHook);
221 } else {
222 DoMethod(app, MUIM_CallHook, &InsertHook);
225 return 0;