1 /********************************************************************************
2 *                                                                               *
3 *                         D r i v e   B o x   O b j e c t                       *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1999,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or                 *
9 * modify it under the terms of the GNU Lesser General Public                    *
10 * License as published by the Free Software Foundation; either                  *
11 * version 2.1 of the License, or (at your option) any later version.            *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
16 * Lesser General Public License for more details.                               *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public              *
19 * License along with this library; if not, write to the Free Software           *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
21 *********************************************************************************
22 * $Id: FXDriveBox.cpp 4937 2019-03-10 19:59:30Z arthurcnorman $                      *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "fxkeys.h"
28 #include "FXHash.h"
29 #include "FXThread.h"
30 #include "FXStream.h"
31 #include "FXString.h"
32 #include "FXSize.h"
33 #include "FXPoint.h"
34 #include "FXRectangle.h"
35 #include "FXSettings.h"
36 #include "FXRegistry.h"
37 #include "FXAccelTable.h"
38 #include "FXObjectList.h"
39 #include "FXApp.h"
40 #include "FXId.h"
41 #include "FXPath.h"
42 #include "FXSystem.h"
43 #include "FXDrawable.h"
44 #include "FXImage.h"
45 #include "FXIcon.h"
46 #include "FXGIFIcon.h"
47 #include "FXBMPIcon.h"
48 #include "FXFont.h"
49 #include "FXDC.h"
50 #include "FXWindow.h"
51 #include "FXComposite.h"
52 #include "FXShell.h"
53 #include "FXTopWindow.h"
54 #include "FXDialogBox.h"
55 #include "FXMessageBox.h"
56 #include "FXFrame.h"
57 #include "FXLabel.h"
58 #include "FXTextField.h"
59 #include "FXButton.h"
60 #include "FXMenuButton.h"
61 #include "FXComposite.h"
62 #include "FXPacker.h"
63 #include "FXShell.h"
64 #include "FXPopup.h"
65 #include "FXScrollBar.h"
66 #include "FXScrollArea.h"
67 #include "FXList.h"
68 #include "FXListBox.h"
69 #include "FXDriveBox.h"
70 #include "FXFileDict.h"
71 #include "icons.h"
72 
73 
74 /*
75   Notes:
76   - When setting path, it adds all directories from the top down to
77     the lowest directory.
78   - It also adds common places in the file system.
79   - Add API's to set current path [from root down to whereever]
80   - Add code to read path of selected item
81   - Add some better icons
82 */
83 
84 using namespace FX;
85 
86 /*******************************************************************************/
87 
88 namespace FX {
89 
90 
91 // Map
92 FXDEFMAP(FXDriveBox) FXDriveBoxMap[]={
93   FXMAPFUNC(SEL_CHANGED,FXDriveBox::ID_LIST,FXDriveBox::onListChanged),
94   FXMAPFUNC(SEL_CLICKED,FXDriveBox::ID_LIST,FXDriveBox::onListClicked),
95   FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETVALUE,FXDriveBox::onCmdSetValue),
96   FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETSTRINGVALUE,FXDriveBox::onCmdSetStringValue),
97   FXMAPFUNC(SEL_COMMAND,FXWindow::ID_GETSTRINGVALUE,FXDriveBox::onCmdGetStringValue),
98   };
99 
100 
101 // Implementation
FXIMPLEMENT(FXDriveBox,FXListBox,FXDriveBoxMap,ARRAYNUMBER (FXDriveBoxMap))102 FXIMPLEMENT(FXDriveBox,FXListBox,FXDriveBoxMap,ARRAYNUMBER(FXDriveBoxMap))
103 
104 
105 // Directory box
106 FXDriveBox::FXDriveBox(FXComposite *p,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):
107   FXListBox(p,tgt,sel,opts,x,y,w,h, pl,pr,pt,pb){
108   associations=NULL;
109   if(!(options&DRIVEBOX_NO_OWN_ASSOC)) associations=new FXFileDict(getApp());
110   foldericon=new FXGIFIcon(getApp(),minifolder);
111   cdromicon=new FXGIFIcon(getApp(),minicdrom);
112   harddiskicon=new FXGIFIcon(getApp(),miniharddisk);
113   netdriveicon=new FXGIFIcon(getApp(),mininetdrive);
114   floppyicon=new FXGIFIcon(getApp(),minifloppy);
115   nethoodicon=new FXGIFIcon(getApp(),mininethood);
116   zipdiskicon=new FXGIFIcon(getApp(),minizipdrive);
117   setDrive(FXSystem::getCurrentDrive());
118   }
119 
120 
121 // Create
create()122 void FXDriveBox::create(){
123   FXListBox::create();
124   foldericon->create();
125   cdromicon->create();
126   harddiskicon->create();
127   netdriveicon->create();
128   floppyicon->create();
129   nethoodicon->create();
130   zipdiskicon->create();
131   }
132 
133 
134 // Detach disconnects the icons
detach()135 void FXDriveBox::detach(){
136   FXListBox::detach();
137   foldericon->detach();
138   cdromicon->detach();
139   harddiskicon->detach();
140   netdriveicon->detach();
141   floppyicon->detach();
142   nethoodicon->detach();
143   zipdiskicon->detach();
144   }
145 
146 
147 // Destroy zaps the icons
destroy()148 void FXDriveBox::destroy(){
149   FXListBox::destroy();
150   foldericon->destroy();
151   cdromicon->destroy();
152   harddiskicon->destroy();
153   netdriveicon->destroy();
154   floppyicon->destroy();
155   nethoodicon->destroy();
156   zipdiskicon->destroy();
157   }
158 
159 
160 // Set the current item's text from the message
onCmdSetValue(FXObject *,FXSelector,void * ptr)161 long FXDriveBox::onCmdSetValue(FXObject*,FXSelector,void* ptr){
162   setDrive((char*)ptr);
163   return 1;
164   }
165 
166 
167 // Change value
onCmdSetStringValue(FXObject *,FXSelector,void * ptr)168 long FXDriveBox::onCmdSetStringValue(FXObject*,FXSelector,void* ptr){
169   setDrive(*((FXString*)ptr));
170   return 1;
171   }
172 
173 
174 // Obtain value
onCmdGetStringValue(FXObject *,FXSelector,void * ptr)175 long FXDriveBox::onCmdGetStringValue(FXObject*,FXSelector,void* ptr){
176   *((FXString*)ptr)=getDrive();
177   return 1;
178   }
179 
180 #ifndef WIN32           // UNIX flavor
181 
182 // Fill list with names of available drives
listDrives()183 void FXDriveBox::listDrives(){
184   FXFileAssoc *fileassoc;
185   FXIcon *icon;
186 
187   // Remove old items first
188   clearItems();
189 
190   // Determine associations, icons and type
191   icon=foldericon;
192   if(associations){
193     fileassoc=associations->findDirBinding("/");
194     if(fileassoc && fileassoc->miniicon) icon=fileassoc->miniicon;
195     }
196 
197   // Create icon
198   if(id()) icon->create();
199 
200   // Add item
201   appendItem("/",icon);
202   }
203 
204 #else                   // Windows flavor
205 
206 // Fill list with names of available drives
listDrives()207 void FXDriveBox::listDrives(){
208   FXFileAssoc *fileassoc;
209   FXIcon *icon;
210   FXchar drivename[10];
211   FXuint drivemask;
212 
213   // Remove old drives
214   clearItems();
215 
216   // Add all drives
217   drivemask=GetLogicalDrives();
218   drivename[1]=':';
219   drivename[2]=PATHSEP;
220   drivename[3]='\0';
221 
222   // Loop over drive letters
223   for(drivename[0]='A'; drivename[0]<='Z'; drivename[0]++){
224     if(drivemask&1){
225 
226       // Default icon based on hardware type
227       switch(GetDriveTypeA(drivename)){
228         case DRIVE_REMOVABLE: icon=(drivename[0]<='B') ? floppyicon : zipdiskicon; break;
229         case DRIVE_FIXED: icon=harddiskicon; break;
230         case DRIVE_REMOTE: icon=netdriveicon; break;
231         case DRIVE_CDROM: icon=cdromicon; break;
232         case DRIVE_RAMDISK: icon=harddiskicon; break;
233         case DRIVE_UNKNOWN: icon=foldericon; break;
234         case DRIVE_NO_ROOT_DIR: icon=foldericon; break;
235         default: icon=foldericon; break;
236         }
237 
238       // Maybe override from associations
239       if(associations){
240         fileassoc=associations->findDirBinding(drivename);
241         if(fileassoc && fileassoc->miniicon) icon=fileassoc->miniicon;
242         }
243 
244       // Create item
245       if(id()) icon->create();
246 
247       // Add another item
248       appendItem(drivename,icon);
249       }
250     drivemask>>=1;
251     }
252   }
253 
254 #endif
255 
256 
257 // Forward clicked message from list to target
onListClicked(FXObject *,FXSelector,void * ptr)258 long FXDriveBox::onListClicked(FXObject*,FXSelector,void* ptr){
259   button->handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),NULL);    // Unpost the list
260   if(0<=((FXint)(FXival)ptr)){
261     field->setText(getItemText((FXival)ptr));
262     field->setIcon(getItemIcon((FXival)ptr));
263     if(target){target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)getItemText((FXival)ptr).text());}
264     }
265   return 1;
266   }
267 
268 
269 // List has changed
onListChanged(FXObject *,FXSelector,void * ptr)270 long FXDriveBox::onListChanged(FXObject*,FXSelector,void* ptr){
271   return target && target->tryHandle(this,FXSEL(SEL_CHANGED,message),(void*)getItemText((FXival)ptr).text());
272   }
273 
274 
275 // Set directory
setDrive(const FXString & drive)276 FXbool FXDriveBox::setDrive(const FXString& drive){
277   listDrives();
278   setCurrentItem(findItem(FXPath::drive(FXPath::absolute(drive))));
279   return TRUE;
280   }
281 
282 
283 // Return current drive
getDrive() const284 FXString FXDriveBox::getDrive() const {
285   return getItemText(getCurrentItem());
286   }
287 
288 
289 // Change associations table; force regeneration of the items
290 // in the tree list so all the new bindings take effect
setAssociations(FXFileDict * assocs)291 void FXDriveBox::setAssociations(FXFileDict* assocs){
292   if(associations!=assocs){
293     associations=assocs;
294     listDrives();
295     }
296   }
297 
298 
299 // Save object to stream
save(FXStream & store) const300 void FXDriveBox::save(FXStream& store) const {
301   FXListBox::save(store);
302   store << associations;
303   store << foldericon;
304   store << cdromicon;
305   store << harddiskicon;
306   store << netdriveicon;
307   store << floppyicon;
308   store << nethoodicon;
309   store << zipdiskicon;
310   }
311 
312 
313 // Load object from stream
load(FXStream & store)314 void FXDriveBox::load(FXStream& store){
315   FXListBox::load(store);
316   store >> associations;
317   store >> foldericon;
318   store >> cdromicon;
319   store >> harddiskicon;
320   store >> netdriveicon;
321   store >> floppyicon;
322   store >> nethoodicon;
323   store >> zipdiskicon;
324   }
325 
326 
327 // Delete it
~FXDriveBox()328 FXDriveBox::~FXDriveBox(){
329   clearItems();
330   if(!(options&DRIVEBOX_NO_OWN_ASSOC)) delete associations;
331   delete foldericon;
332   delete cdromicon;
333   delete harddiskicon;
334   delete netdriveicon;
335   delete floppyicon;
336   delete nethoodicon;
337   delete zipdiskicon;
338   associations=(FXFileDict*)-1L;
339   foldericon=(FXIcon*)-1L;
340   cdromicon=(FXIcon*)-1L;
341   harddiskicon=(FXIcon*)-1L;
342   netdriveicon=(FXIcon*)-1L;
343   floppyicon=(FXIcon*)-1L;
344   nethoodicon=(FXIcon*)-1L;
345   zipdiskicon=(FXIcon*)-1L;
346   }
347 
348 }
349