home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************/
- /* Folder View - Drag and drop directory viewer */
- /* Public Domain (P) 1992 Benjamin Cooley */
- /* folders.cpp - FolderView Folder Classes */
- /********************************************************************/
-
- #include "include.h"
- #pragma hdrstop
-
- #include "fldrview.h"
-
- static PTFolderView MatchSkipView = NULL;
-
- /*****************************************/
- /* TMainView - Main folder window object */
- /*****************************************/
-
- TStreamableClass RegMainView("MainView", TMainView::build, __DELTA(TMainView));
-
- TMainView::TMainView(Pchar APath)
- : TMDIFrame(APath, MNU_Main)
- {
- ChildMenuPos = 2;
-
- strcpy(title, APath);
- if (title[strlen(title) - 1] != '\\')
- {
- Pchar ptr = title + strlen(title) - 1;
- while (ptr != title && *ptr != '\\') ptr--;
- ptr++;
- char save = *ptr;
- *ptr = NULL;
- strcpy(dir, title);
- *ptr = save;
- strcpy(wildcard, ptr);
- }
- else
- {
- strcpy(dir, title);
- wildcard[0] = NULL;
- }
-
- FolderView = this;
- focusedicon = NULL;
-
- /* Allocate all icon */
- alliconsarray = new TkUnsortedArray(0, 0, 50);
-
- /* Create Other View */
- otherview = new TFolderView(this, "Other Files", "*.*");
- }
-
- #pragma argsused
- TMainView::TMainView(StreamableInit s)
- : TMDIFrame("", MNU_Main)
- {
- ChildMenuPos = 2;
-
- title[0] = NULL;
- dir[0] = NULL;
- wildcard[0] = NULL;
-
- FolderView = this;
- focusedicon = NULL;
-
- /* Initialize all icon array */
- alliconsarray = NULL;
- }
-
- TMainView::~TMainView()
- {
- alliconsarray->flush(TShouldDelete::Delete);
- delete alliconsarray;
- }
-
- void TMainView::SetupWindow()
- {
- TMDIFrame::SetupWindow();
-
- SetCaption(title);
- UpdateAllIcons();
- }
-
- void TMainView::ShutDownWindow()
- {
- ShuttingDown = TRUE;
-
- SaveWindow();
-
- TMDIFrame::ShutDownWindow();
- }
-
- int TMainView::ViewType()
- {
- if (strlen(dir) > 3) return FOLDERVIEW_DIRECTORY;
- else return FOLDERVIEW_DRIVE;
- }
-
- Pchar TMainView::GetClassName()
- {
- if (ViewType() == FOLDERVIEW_DIRECTORY) return "FOLDERVIEW_DIR";
- else if (ViewType() == FOLDERVIEW_DRIVE) return "FOLDERVIEW_DRIVE";
- else return TMDIFrame::GetClassName();
- }
-
- void TMainView::GetWindowClass(WNDCLASS _FAR &AWndClass)
- {
- TMDIFrame::GetWindowClass(AWndClass);
-
- if (ViewType() == FOLDERVIEW_DIRECTORY)
- AWndClass.hIcon = LoadIcon(
- GetModule()->hInstance, MAKEINTRESOURCE(ICO_Directory));
- else AWndClass.hIcon = LoadIcon(
- GetModule()->hInstance, MAKEINTRESOURCE(ICO_HardDrive));
- AWndClass.style |= CS_DBLCLKS;
- }
-
- void TMainView::SaveWindow()
- {
- char filepath[MAXPATH];
- char filename[FILELEN];
-
- /* Find directory name (create new entry if one not already there) */
- GetPrivateProfileString(ProfileDirectories, title, "",
- filename, FILELEN, ProfileName);
- if (!*filename)
- {
- ffblk affblk;
-
- int newfilenum = 0;
- strcpy(filepath, FolderInfoPath);
- strcat(filepath, FolderInfoWildcard);
- int found = !findfirst(filepath, &affblk, 0);
- while (found)
- {
- int filenum = atoi(affblk.ff_name + FolderInfoFileCountPos);
- if (filenum > newfilenum) newfilenum = filenum;
- found = !findnext(&affblk);
- }
- newfilenum++;
- strcpy(filename, FolderInfoFileName);
- char temp[10];
- itoa(newfilenum + 10000, temp, 10);
- memcpy(filename + FolderInfoFileCountPos,
- temp + 5 - FolderInfoFileCountLen,
- FolderInfoFileCountLen);
- }
-
- /* Make sure directory exists */
- strcpy(filepath, FolderInfoPath);
- filepath[strlen(filepath) - 1] = NULL;
- unsigned attr;
- BOOL failed = FALSE;
- if (_dos_getfileattr(filepath, &attr))
- {
- if (mkdir(filepath)) failed = TRUE;
- } else if (!(attr & FA_DIREC)) failed = TRUE;
- if (failed)
- {
- MessageBox(HWindow, "Couldn't create FLDRINFO directory. "
- "Folder setup saved.", "FolderView",
- MB_ICONSTOP | MB_OK);
- return;
- }
-
- /* Make filename and save file */
- strcpy(filepath, FolderInfoPath);
- strcat(filepath, filename);
- ofpstream os(filepath, ios::out | ios::trunc);
- if (!os.fail())
- {
- Pchar buf = new char[FILEBUFSIZE];
- os.setbuf(buf, FILEBUFSIZE);
- os << this;
- os.close();
- delete buf;
- }
- WritePrivateProfileString(
- ProfileDirectories, title, filename, ProfileName);
-
- }
-
- int TMainView::FindAllIconIndex(Pchar name)
- {
- for (int c = 0; c < NumAllIcons(); c++)
- {
- PTIcon icon = GetAllIcon(c);
- if (!strcmp(name, icon->Name())) return c;
- }
- return -1;
- }
-
- PTIcon TMainView::FindAllIcon(Pchar name)
- {
- for (int c = 0; c < NumAllIcons(); c++)
- {
- PTIcon icon = GetAllIcon(c);
- if (!strcmp(name, icon->Name())) return icon;
- }
- return NULL;
- }
-
- PTFolderView TMainView::GetFileView(Pchar name, int attrib)
- {
- PTFolderView view, firstview;
- view = firstview = (PTFolderView)GetFirstChild();
- if (view)
- {
- do
- {
- if (view != MatchSkipView)
- {
- if (view != otherview)
- if (view->WildcardMatch(name, attrib)) return view;
- }
- view = (PTFolderView)view->Next();
- } while (view != firstview);
- }
- return otherview;
- }
-
- #pragma argsused
- static void ArrangeViewIcons(Pvoid view, Pvoid data)
- {
- ((PTFolderView)((PTWindowsObject)view))->ArrangeIcons();
- }
-
- void TMainView::ArrangeAllIcons()
- {
- ForEach(ArrangeViewIcons, NULL);
- ArrangeIcons();
- }
-
- void TMainView::UpdateAllIcons(PTFolderView newview)
- {
- int c;
- PTIcon icon;
- PTFolderView view;
-
- /* Save focus icon name */
- char focusedname[FILELEN];
- if (focusedicon)
- strcpy(focusedname, focusedicon->Name());
- else *focusedname = NULL;
-
- char dirbuf[MAXDIR];
- Pchar ptr = stpcpy(dirbuf, title);
- if (*(ptr - 1) == '\\') strcpy(ptr, "*.*");
-
- ffblk affblk;
- PTkUnsortedArray newicons = new TkUnsortedArray(0, 0, 50);
- BOOL *iconunchanged = new BOOL[NumAllIcons()];
- memset(iconunchanged, 0, sizeof(BOOL) * NumAllIcons());
-
- int found = !findfirst(dirbuf, &affblk, FA_DIREC);
- while (found)
- {
- if (affblk.ff_name[0] != '.')
- {
- strlwr(affblk.ff_name);
- int iconindex = FindAllIconIndex(affblk.ff_name);
- if (iconindex == -1)
- {
- view = GetFileView(affblk.ff_name, affblk.ff_attrib);
- icon = TIcon::NewIcon(*view, affblk);
- newicons->add(*icon);
- }
- else
- {
- icon = GetAllIcon(iconindex);
- if (!icon->FolderView()) // Set to new view if old view NULL
- {
- view = GetFileView(icon->Name(), icon->Attrib());
- }
- else // Check if should be in new view
- if (newview && newview->WildcardMatch(affblk.ff_name, affblk.ff_attrib))
- view = newview;
- else view = icon->FolderView();
- if (icon->ShouldUpdate(affblk) || icon->FolderView() != view)
- {
- icon = TIcon::NewIcon(*view, affblk);
- newicons->add(*icon);
- }
- else
- {
- iconunchanged[iconindex] = TRUE;
- }
- }
- }
- found = !findnext(&affblk);
- }
-
- /* Delete all changed icons */
- for (c = NumAllIcons() - 1; c >= 0; c--)
- {
- if (!iconunchanged[c])
- {
- icon = GetAllIcon(c);
- icon->FolderView()->DeleteIcon(icon);
- }
- }
-
- /* Add new icons */
- for (c = 0; c < newicons->itemsInArray(); c++)
- {
- icon = &((RTIcon)((*newicons)[c]));
- alliconsarray->add(*icon);
- icon->FolderView()->AddIcon(icon);
- }
-
- /* Delete intermediate arrays */
- newicons->flush(TShouldDelete::NoDelete);
- delete newicons;
- delete iconunchanged;
-
- /* Reset focus icon */
- focusedicon = NULL;
- if (*focusedname)
- {
- focusedicon = FindAllIcon(focusedname);
- if (focusedicon) FocusIcon(focusedicon);
- }
- if (!focusedicon && NumAllIcons() > 0)
- FocusIcon(GetAllIcon(0));
- ArrangeAllIcons();
- }
-
- void TMainView::UpdateMenu(PTFolderView view)
- {
- int sortorder = view->GetSortOrder();
- int viewmode = view->GetView();
-
- HMENU menu = GetMenu(HWindow);
- if (sortorder == SORT_BYNAME)
- CheckMenuItem(menu, CM_SortByName, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_SortByName, MF_BYCOMMAND | MF_UNCHECKED);
- if (sortorder == SORT_BYEXT)
- CheckMenuItem(menu, CM_SortByExtension, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_SortByExtension, MF_BYCOMMAND | MF_UNCHECKED);
- if (sortorder == SORT_BYDATE)
- CheckMenuItem(menu, CM_SortByDateTime, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_SortByDateTime, MF_BYCOMMAND | MF_UNCHECKED);
- if (sortorder == SORT_BYSIZE)
- CheckMenuItem(menu, CM_SortBySize, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_SortBySize, MF_BYCOMMAND | MF_UNCHECKED);
- if (sortorder == SORT_BYDESC)
- CheckMenuItem(menu, CM_SortByDescription, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_SortByDescription, MF_BYCOMMAND | MF_UNCHECKED);
-
- if (viewmode == VIEW_ICON)
- CheckMenuItem(menu, CM_ViewIcons, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_ViewIcons, MF_BYCOMMAND | MF_UNCHECKED);
- if (viewmode == VIEW_NAME)
- CheckMenuItem(menu, CM_ViewNames, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_ViewNames, MF_BYCOMMAND | MF_UNCHECKED);
- if (viewmode == VIEW_ALL)
- CheckMenuItem(menu, CM_ViewAll, MF_BYCOMMAND | MF_CHECKED);
- else CheckMenuItem(menu, CM_ViewAll, MF_BYCOMMAND | MF_UNCHECKED);
- }
-
- void TMainView::FocusIcon(PTIcon icon)
- {
- if (icon == focusedicon) return;
- if (focusedicon)
- focusedicon->SetStateFlag(IS_FOCUSED, FALSE);
- focusedicon = icon;
- if (focusedicon)
- {
- focusedicon->SetStateFlag(IS_FOCUSED, TRUE);
- focusedicon->FolderView()->ScrollToIcon(focusedicon);
- }
- }
-
- void TMainView::WMCommand(RTMessage Msg)
- {
- PTDialog dialog;
-
- if (!Msg.LP.Lo)
- {
- switch (Msg.WParam)
- {
- case CM_SortByName:
- case CM_SortByExtension:
- case CM_SortByDateTime:
- case CM_SortBySize:
- case CM_SortByDescription:
- if (ActiveChild)
- ((PTFolderView)ActiveChild)->
- MenuCommand(Msg.WParam);
- break;
- case CM_ViewIcons:
- case CM_ViewNames:
- case CM_ViewAll:
- if (ActiveChild)
- ((PTFolderView)ActiveChild)->
- MenuCommand(Msg.WParam);
- break;
- case CM_About:
- dialog = new TDialog(this, DLG_About);
- GetModule()->ExecDialog(dialog);
- break;
- case CM_NewSubView:
- struct {
- char title[MAXPATH];
- char wildcard[MAXPATH];
- } buf;
- dialog = new TDialog(this, DLG_NewSubView);
- new TEdit(dialog, 101, MAXPATH);
- new TEdit(dialog, 102, MAXPATH);
- buf.title[0] = NULL;
- buf.wildcard[0] = NULL;
- dialog->SetTransferBuffer(&buf);
- dialog->EnableTransfer();
- if (GetModule()->ExecDialog(dialog) == IDOK)
- {
- if (buf.title[0] == NULL)
- strcpy(buf.title, "SubView");
- if (buf.wildcard[0] == NULL)
- strcpy(buf.wildcard, "*.*");
- PTFolderView view = new TFolderView(this, buf.title, buf.wildcard);
- UpdateAllIcons(view);
- GetModule()->MakeWindow(view);
- }
- break;
- }
- }
- TMDIFrame::WMCommand(Msg);
- }
-
- static void SetAllMainViews(Pvoid view, Pvoid mainview)
- {
- ((PTFolderView)((PTWindowsObject)view))->
- SetMainView((PTMainView)mainview);
- }
-
- Pvoid TMainView::read(Ripstream is)
- {
- is.readString(title, TITLELEN);
- is.readString(dir, MAXPATH);
- is.readString(wildcard, MAXPATH);
- BOOL iszoomed;
- is >> Attr.X >> Attr.Y >> Attr.W >> Attr.H >> iszoomed;
- if (iszoomed) Attr.Style |= WS_MAXIMIZE;
- else Attr.Style &= ~((long)WS_MAXIMIZE);
-
- int numicons;
- is >> numicons;
- alliconsarray = new TkUnsortedArray(numicons + 25, 0, 50);
- for (int c = 0; c < numicons; c++)
- {
- PTIcon icon = NULL;
- is >> icon;
- alliconsarray->add(*icon);
- }
-
- char filename[FILELEN];
- is.readString(filename, FILELEN);
- if (*filename) focusedicon = FindAllIcon(filename);
-
- GetChildren(is);
- otherview = NULL;
- is >> otherview;
-
- ForEach(SetAllMainViews, (Pvoid)this);
-
- return this;
- }
-
- void TMainView::write(Ropstream os)
- {
- os.writeString(title);
- os.writeString(dir);
- os.writeString(wildcard);
- BOOL iszoomed;
- if(HWindow) iszoomed = IsZoomed(HWindow);
- else iszoomed = FALSE;
- os << Attr.X << Attr.Y << Attr.W << Attr.H << iszoomed;
-
- os << (int)NumAllIcons();
- for (int c = 0; c < NumAllIcons(); c++)
- {
- os << GetAllIcon(c);
- }
-
- if (focusedicon)
- os.writeString(focusedicon->Name());
- else os.writeString("");
-
- PutChildren(os);
- os << otherview;
-
- }
-
- /*************************************************/
- /* TFolderView - Subwindow in main folder window */
- /*************************************************/
-
- TStreamableClass RegSubView("FolderView", TFolderView::build, __DELTA(TFolderView));
-
- void TFolderView::InitFolderView()
- {
-
- /* Set Window Attributes */
- Attr.Style |= WS_VSCROLL | WS_HSCROLL;
-
- /* Allocate new icon array */
- oldfont = NULL;
- sortorder = SORT_BYNAME;
- view = VIEW_ICON;
- displayitems = DISPLAY_NAME | DISPLAY_DESC | DISPLAY_DATE |
- DISPLAY_TIME | DISPLAY_SIZE | DISPLAY_ATTRIB;
-
- /* Set up scroller */
- Scroller = new TScroller(this, IconSpaceWidth, IconSpaceHeight, 0, 0);
- Scroller->AutoMode = FALSE;
- Scroller->TrackMode = FALSE;
- }
-
- TFolderView::TFolderView(PTMainView AMainView, Pchar ATitle, Pchar AWildCard)
- : TWindow(AMainView, ATitle)
- {
-
- /* Set Subview Attributes */
- Attr.Style = WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_OVERLAPPED |
- WS_VSCROLL | WS_HSCROLL | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME;
-
- /* Set Title */
- strcpy(title, ATitle);
- strcpy(wildcard, AWildCard);
-
- /* Init window */
- InitFolderView();
-
- /* View icon array */
- icons = new TkSortedArray(0, 0, 50);
-
- /* Set Main View */
- mainview = AMainView;
-
- /* Set allicons pointer */
- allicons = AMainView->AllIconsPtr();
- }
-
- #pragma argsused
- TFolderView::TFolderView(StreamableInit s)
- : TWindow(NULL, "")
- {
- SetFlags(WB_MDICHILD, TRUE); // Make sure window is MDI child
-
- /* Set Subview Attributes */
- Attr.Style = WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_OVERLAPPED |
- WS_VSCROLL | WS_HSCROLL | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME;
-
- /* Set window desc */
- title[0] = NULL;
- wildcard[0] = NULL;
-
- /* Init window */
- InitFolderView();
-
- /* Set pointers */
- icons = NULL;
- mainview = NULL;
- allicons = NULL;
- }
-
- TFolderView::~TFolderView()
- {
- icons->flush(TShouldDelete::NoDelete);
- delete icons;
- }
-
- void TFolderView::ShutDownWindow()
- {
- PTMainView mv = mainview; // Keep copy for use after shutdown
-
- if (!ShuttingDown) // If not closing all windows
- {
- MatchSkipView = this;
- for (int c = 0; c < NumIcons(); c++)
- {
- PTIcon icon = GetIcon(c);
- PTFolderView view = mainview->GetFileView(icon->Name(), icon->Attrib());
- view->AddIcon(icon);
- }
- MatchSkipView = NULL;
- icons->flush(TShouldDelete::NoDelete);
- }
-
- TWindow::ShutDownWindow();
-
- if (!ShuttingDown) // If not closing all windows
- mv->ArrangeAllIcons(); // Remember: can't use 'mainview' after
- // object has been deleted
-
- }
-
- Pchar TFolderView::GetClassName()
- {
- return "FOLDERVIEW_VIEW";
- }
-
- void TFolderView::GetWindowClass(WNDCLASS _FAR &AWndClass)
- {
- TWindow::GetWindowClass(AWndClass);
- AWndClass.style |= CS_DBLCLKS;
- AWndClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ICO_View));
- }
-
- #define SHOWMODE_NORMAL (0)
- #define SHOWMODE_ZOOMED (1)
- #define SHOWMODE_ICON (2)
-
- Pvoid TFolderView::read(Ripstream is)
- {
- is.readString(title, TITLELEN);
- is.readString(wildcard, MAXPATH);
- int showmode;
- is >> Attr.X >> Attr.Y >> Attr.W >> Attr.H >>
- showmode >> sortorder >> view >> displayitems;
- if (showmode == SHOWMODE_ZOOMED) Attr.Style |= WS_MAXIMIZE;
- else if (showmode == SHOWMODE_ICON) Attr.Style |= WS_MINIMIZE;
- else Attr.Style &= ~((long)WS_MAXIMIZE | (long)WS_MINIMIZE);
- int numicons;
- is >> numicons;
- icons = new TkSortedArray(numicons + 25, 0, 50);
- for (int c = 0; c < numicons; c++)
- {
- PTIcon icon = NULL;
- is >> icon;
- icon->SetFolderView(this);
- icons->add(*icon);
- }
- mainview = FolderView;
-
- return this;
- }
-
- void TFolderView::write(Ropstream os)
- {
- os.writeString(title);
- os.writeString(wildcard);
- int showmode = SHOWMODE_NORMAL;
- if (HWindow)
- {
- if (IsZoomed(HWindow)) showmode = SHOWMODE_ZOOMED;
- else if (IsIconic(HWindow)) showmode = SHOWMODE_ICON;
- }
- os << Attr.X << Attr.Y << Attr.W << Attr.H <<
- showmode << sortorder << view << displayitems;
- os << NumIcons();
- for (int c = 0; c < NumIcons(); c++)
- {
- os << GetIcon(c);
- }
- }
-
- void TFolderView::SetMainView(PTMainView newmainview)
- {
- mainview = newmainview;
- allicons = mainview->AllIconsPtr();
- }
-
- void TFolderView::SetScroller()
- {
- if (!IsIconic(HWindow))
- {
- if (view == VIEW_ICON)
- {
- RECT R;
- GetClientRect(HWindow, &R);
- int lines = 0;
- if (NumIcons() > 0)
- lines =
- (GetIcon(NumIcons() - 1)->PosY() / IconSpaceHeight + 1) -
- (R.bottom / IconSpaceHeight);
- Scroller->SetUnits(1, IconSpaceHeight);
- Scroller->SetRange(0, lines);
- }
- else if (view == VIEW_NAME)
- {
- RECT R;
- GetClientRect(HWindow, &R);
- int columns = 0;
- if (NumIcons() > 0)
- columns =
- (GetIcon(NumIcons() - 1)->PosX() / IconNameWidth + 1) -
- (R.right / IconNameWidth);
- if (columns < 1) columns = 0;
- Scroller->SetUnits(IconNameWidth, 1);
- Scroller->SetRange(columns, 0);
- }
- if (view == VIEW_ALL)
- {
- RECT R;
- GetClientRect(HWindow, &R);
- int lines = NumIcons() - (R.bottom / IconNameHeight);
- if (lines < 1) lines = 0;
- Scroller->SetUnits(1, IconNameHeight);
- Scroller->SetRange(0, lines);
- }
-
- }
- }
-
- void TFolderView::SetupWindow()
- {
- TWindow::SetupWindow();
-
- SetCaption(title);
- ArrangeIcons();
- mainview->UpdateMenu(this);
- SetScroller();
- }
-
- void TFolderView::AddIcon(PTIcon icon)
- {
- icon->SetFolderView(this);
- icons->add(*icon);
- }
-
- void TFolderView::DetachIcon(PTIcon icon)
- {
- int index = -1;
- for (int c = 0; c < NumIcons(); c++)
- if (GetIcon(c) == icon) index = c;
- if (index >= 0) icons->detach(index, TShouldDelete::NoDelete);
- icon->SetFolderView(NULL);
- }
-
- void TFolderView::DeleteIcon(PTIcon icon)
- {
- int c;
-
- int index = -1;
- for (c = 0; c < NumIcons(); c++)
- if (GetIcon(c) == icon) index = c;
- if (index >= 0) icons->detach(index, TShouldDelete::NoDelete);
- index = -1;
- for (c = 0; c < mainview->NumAllIcons(); c++)
- if (mainview->GetAllIcon(c) == icon) index = c;
- if (index >= 0) (*allicons)->detach(index, TShouldDelete::Delete);
- }
-
- void TFolderView::Paint(HDC PaintDC, PAINTSTRUCT &PaintInfo)
- {
- TWindow::Paint(PaintDC, PaintInfo);
-
- HFONT oldfont = SelectObject(PaintDC, Font);
-
- for (int c = 0; c < NumIcons(); c++)
- {
- GetIcon(c)->Paint(PaintDC);
- }
-
- SelectObject(PaintDC, oldfont);
- }
-
- int TFolderView::GetAdjacentIcon(int startnum, int dir)
- {
- int delta;
- int numicons = NumIcons();
- PTIcon start = GetIcon(startnum);
- if ((dir == ADJACENTICON_LEFT) ||
- (dir == ADJACENTICON_UP)) delta = -1;
- else delta = 1;
- int c = startnum + delta;
- while (c >= 0 && c < numicons)
- {
- PTIcon icon = GetIcon(c);
- switch (dir)
- {
- case ADJACENTICON_LEFT:
- if (icon->PosX() < start->PosX())
- if (icon->PosY() <= start->PosY())
- return c;
- break;
- case ADJACENTICON_UP:
- if (icon->PosY() < start->PosY())
- if (icon->PosX() <= start->PosX())
- return c;
- break;
- case ADJACENTICON_RIGHT:
- if (icon->PosX() > start->PosX())
- if (icon->PosY() >= start->PosY())
- return c;
- break;
- case ADJACENTICON_DOWN:
- if (icon->PosY() > start->PosY())
- if (icon->PosX() >= start->PosX())
- return c;
- break;
- }
- c += delta;
- }
- return startnum;
- }
-
- void TFolderView::ScrollToIcon(PTIcon icon)
- {
- RECT w, *i;
-
- i = icon->GetRect();
- GetClientRect(HWindow, &w);
- int width = w.right;
- int height = w.bottom;
- OffsetRect(&w,
- (int)(Scroller->XPos * Scroller->XUnit),
- (int)(Scroller->YPos * Scroller->YUnit));
-
- int newposx = (int)Scroller->XPos;
- int newposy = (int)Scroller->YPos;
- if (i->left < w.left)
- newposx = i->left / Scroller->XUnit;
- else if (i->right > w.right)
- newposx = (i->right + Scroller->XUnit - width)
- / Scroller->XUnit;
-
- if (i->top < w.top)
- newposy = i->top / Scroller->YUnit;
- else if (i->bottom > w.bottom)
- newposy = (i->bottom + Scroller->YUnit - height)
- / Scroller->YUnit;
-
- if ((newposx != Scroller->XPos) ||
- (newposy != Scroller->YPos))
- Scroller->ScrollTo(newposx, newposy);
- }
-
- void TFolderView::WMKeyDown(RTMessage Msg)
- {
- if (FocusedIcon() &&
- FocusedIcon()->FolderView() != this)
- {
- FocusedIcon()->FolderView()->WMKeyDown(Msg);
- return;
- }
-
- int focusednum = GetIconNumber(FocusedIcon());
- if (focusednum < 0) return;
-
- switch (Msg.WParam)
- {
- case VK_LEFT:
- focusednum = GetAdjacentIcon(focusednum, ADJACENTICON_LEFT);
- break;
- case VK_RIGHT:
- focusednum = GetAdjacentIcon(focusednum, ADJACENTICON_RIGHT);
- break;
- case VK_UP:
- focusednum = GetAdjacentIcon(focusednum, ADJACENTICON_UP);
- break;
- case VK_DOWN:
- focusednum = GetAdjacentIcon(focusednum, ADJACENTICON_DOWN);
- break;
- case VK_HOME:
- focusednum = 0;
- break;
- case VK_END:
- focusednum = NumIcons() - 1;
- break;
- case VK_SPACE:
- SelectIcons(GetIcon(focusednum), SELECT_MULTIPLE);
- return;
- }
-
- FocusIcon(GetIcon(focusednum));
- }
-
- void TFolderView::WMSize(RTMessage Msg)
- {
- if ( Msg.WParam == SIZENORMAL )
- {
- RECT WndRect;
- GetWindowRect(HWindow, &WndRect);
- Attr.H = WndRect.bottom - WndRect.top;
- Attr.W = WndRect.right - WndRect.left;
- }
-
- DefWndProc(Msg);
-
- SetScroller();
- ArrangeIcons();
- }
-
- void TFolderView::WMLButtonDown(RTMessage Msg)
- {
- POINT p = MAKEPOINT(Msg.LParam);
- p.x += (int)(Scroller->XPos * Scroller->XUnit);
- p.y += (int)(Scroller->YPos * Scroller->YUnit);
- PTIcon icon = OnIcon(p);
- if (icon)
- {
- if (Msg.WParam & MK_SHIFT)
- SelectIcons(icon, SELECT_RANGE);
- else if (Msg.WParam & MK_CONTROL)
- SelectIcons(icon, SELECT_MULTIPLE);
- else
- SelectIcons(icon, SELECT_SINGLE);
-
- /* Set MouseDown stuff */
- MouseDown = TRUE;
- MouseDownIcon = icon;
- MouseDownIconX = p.x - icon->PosX() - IconLeft;
- MouseDownIconY = p.y - icon->PosY() - IconTop;
- }
- }
-
- #pragma argsused
- void TFolderView::WMMouseMove(RTMessage Msg)
- {
- if (MouseDown)
- {
- if (!Dragging)
- {
- Dragging = TRUE;
- DragIconCursor =
- MouseDownIcon->CreateCursor(MouseDownIconX, MouseDownIconY);
- DragCursor = DragIconCursor;
- SetCursor(DragIconCursor);
- SetCapture(HWindow);
- }
- }
- }
-
- #pragma argsused
- void TFolderView::WMLButtonUp(RTMessage Msg)
- {
- if (Dragging)
- {
- ReleaseCapture();
- SetCursor(LoadCursor(NULL, IDC_ARROW));
- if (DragIconCursor) DestroyCursor(DragIconCursor);
- }
- Dragging = FALSE;
- MouseDown = FALSE;
- }
-
- #pragma argsused
- void TFolderView::WMLButtonDblClk(RTMessage Msg)
- {
- POINT p = MAKEPOINT(Msg.LParam);
- p.x += (int)(Scroller->XPos * Scroller->XUnit);
- p.y += (int)(Scroller->YPos * Scroller->YUnit);
- PTIcon icon = OnIcon(p);
- if (icon) icon->Open();
- }
-
- int TFolderView::GetIconNumber(PTIcon icon)
- {
- if (!icon) return -1;
- int iconnum = -1;
- int numicons = NumIcons();
- if (numicons > 0)
- {
- int c = 0;
- while ((GetIcon(c) != icon) && (c < numicons)) c++;
- if (c < numicons) iconnum = c;
- }
- return iconnum;
- }
-
- void TFolderView::MenuCommand(WORD command)
- {
- switch (command)
- {
- case CM_SortByName:
- case CM_SortByExtension:
- case CM_SortByDateTime:
- case CM_SortBySize:
- case CM_SortByDescription:
- SetSortOrder(command - CM_SortByName + 1);
- break;
- case CM_ViewIcons:
- case CM_ViewNames:
- case CM_ViewAll:
- SetView(command - CM_ViewIcons + 1);
- break;
- }
- }
-
- void TFolderView::WMMDIActivate(RTMessage Msg)
- {
- TWindow::WMMDIActivate(Msg);
-
- if (Msg.WParam)
- mainview->UpdateMenu(this);
- }
-
- HDC TFolderView::GetDC()
- {
- HDC dc = ::GetDC(HWindow);
- oldfont = SelectObject(dc, Font);
- SetViewportOrg(dc,
- -(int)(Scroller->XPos * Scroller->XUnit),
- -(int)(Scroller->YPos * Scroller->YUnit));
- return dc;
- }
-
- void TFolderView::ReleaseDC(HDC dc)
- {
- SelectObject(dc, oldfont);
- ::ReleaseDC(HWindow, dc);
- }
-
-
- int TFolderView::FindIconIndex(Pchar name)
- {
- for (int c = 0; c < NumIcons(); c++)
- {
- PTIcon icon = GetIcon(c);
- if (!strcmp(name, icon->Name())) return c;
- }
- return -1;
- }
-
- PTIcon TFolderView::FindIcon(Pchar name)
- {
- for (int c = 0; c < NumIcons(); c++)
- {
- PTIcon icon = GetIcon(c);
- if (!strcmp(name, icon->Name())) return icon;
- }
- return NULL;
- }
-
- void TFolderView::ArrangeIcons()
- {
- if (!HWindow) return;
-
- RECT R, rect;
- GetClientRect(HWindow, &R);
-
- int x = R.left;
- int y = R.top;
-
- if (view == VIEW_ICON)
- {
- int c = 0;
- while (c < NumIcons())
- {
- rect.left = x;
- rect.top = y;
- rect.right = x + IconSpaceWidth - 1;
- rect.bottom = y + IconSpaceHeight - 1;
- GetIcon(c)->SetRect(rect);
- c++;
- x += IconSpaceWidth;
- if ((x + IconSpaceHeight - 1) > R.right)
- {
- x = R.left;
- y += IconSpaceHeight;
- }
- }
- }
-
- else if (view == VIEW_NAME)
- {
- int c = 0;
- while (c < NumIcons())
- {
- rect.left = x;
- rect.top = y;
- rect.right = x + IconNameWidth - 1;
- rect.bottom = y + IconNameHeight - 1;
- GetIcon(c)->SetRect(rect);
- c++;
- y += IconNameHeight;
- if ((y + IconNameHeight - 1) > R.bottom)
- {
- y = R.top;
- x += IconNameWidth;
- }
- }
- }
-
- else if (view == VIEW_ALL)
- {
- for (int c = 0; c < NumIcons(); c++)
- {
- rect.left = R.left;
- rect.top = y;
- rect.right = R.right - 1;
- rect.bottom = y + IconNameHeight - 1;
- GetIcon(c)->SetRect(rect);
- y += IconNameHeight;
- }
- }
-
- InvalidateRect(HWindow, NULL, TRUE);
- }
-
- void TFolderView::SetSortOrder(int neworder)
- {
- sortorder = neworder;
-
- PTkSortedArray newicons = new TkSortedArray(0, 0, 50);
- for (int c = 0; c < NumIcons(); c++)
- {
- PTIcon icon = GetIcon(c);
- newicons->add(*icon);
- }
- icons->flush(TShouldDelete::NoDelete);
- delete icons;
- icons = newicons;
-
- ArrangeIcons();
- mainview->UpdateMenu(this);
- }
-
- void TFolderView::SetView(int newview)
- {
- view = newview;
- if (view == VIEW_ICON)
- Scroller->SetUnits(IconSpaceWidth, IconSpaceHeight);
- else if (view == VIEW_NAME)
- Scroller->SetUnits(IconNameWidth, IconNameHeight);
- else if (view == VIEW_ALL)
- Scroller->SetUnits(1, IconNameHeight);
- Scroller->XPos = 0;
- Scroller->YPos = 0;
- SetScroller();
- ArrangeIcons();
- mainview->UpdateMenu(this);
- }
-
- PTIcon TFolderView::OnIcon(POINT p)
- {
- for (int c = 0; c < NumIcons(); c++)
- {
- PTIcon icon = GetIcon(c);
- if (icon->PointOn(p))
- return icon;
- }
- return NULL;
- }
-
- void TFolderView::RedrawIcon(PTIcon icon)
- {
- if (icon) icon->RedrawIcon();
- }
-
- void TFolderView::SelectIcons(PTIcon selicon, int mode)
- {
- PTIcon icon;
- int c;
-
- switch (mode)
- {
- case SELECT_SINGLE:
- FocusIcon(NULL);
- for (c = 0; c < NumAllIcons(); c++)
- {
- icon = GetAllIcon(c);
- if (selicon == icon)
- icon->SetState(icon->GetState() | IS_FOCUSED | IS_SELECTED);
- else icon->SetState(icon->GetState() &
- ~(IS_FOCUSED | IS_SELECTED));
- }
- FocusIcon(selicon);
- break;
- case SELECT_RANGE:
- BOOL select = FALSE;
- PTIcon ficon = FocusedIcon();
- FocusIcon(NULL);
- PTIcon sicon = selicon;
- for (c = 0; c < NumIcons(); c++)
- {
- icon = GetIcon(c);
- if (((icon == ficon) || (icon == sicon)) && !select)
- {
- if ((icon == ficon) && (icon != sicon)) ficon = NULL;
- else if ((icon == sicon) && (icon != ficon)) sicon = NULL;
- select = TRUE;
- }
- if (select)
- {
- if (icon == selicon)
- icon->SetState(
- icon->GetState() | IS_FOCUSED | IS_SELECTED);
- else icon->SetState(
- (icon->GetState() & ~IS_FOCUSED) | IS_SELECTED);
- }
- else
- icon->SetState(icon->GetState() & ~(IS_FOCUSED | IS_SELECTED));
- if (((icon == ficon) || (icon == sicon)) && select)
- select = FALSE;
- }
- FocusIcon(selicon);
- break;
- case SELECT_MULTIPLE:
- FocusIcon(NULL);
- int flags = selicon->GetState() | IS_FOCUSED;
- if (flags & IS_SELECTED) flags &= ~IS_SELECTED;
- else flags |= IS_SELECTED;
- selicon->SetState(flags);
- if (FocusedIcon())
- if (selicon != FocusedIcon())
- FocusedIcon()->SetState(FocusedIcon()->GetState() & ~IS_FOCUSED);
- FocusIcon(selicon);
- break;
- }
- }
-
- void TFolderView::SelectIcon(PTIcon icon)
- {
- if (icon) icon->SetStateFlag(IS_SELECTED, TRUE);
- }
-
- void TFolderView::DeselectIcon(PTIcon icon)
- {
- if (icon) icon->SetStateFlag(IS_SELECTED, FALSE);
- }
-
- void TFolderView::ToggleIconSelection(PTIcon icon)
- {
- if (icon) icon->SetStateFlag(IS_SELECTED, !icon->GetStateFlag(IS_SELECTED));
- }
-
- BOOL TFolderView::WildcardMatch(Pchar name, int attrib)
- {
- Pchar wild = wildcard;
- Pchar file;
- BOOL filenot;
- BOOL match = FALSE;
-
- while (*wild)
- {
- while (*wild && *wild == ' ') wild++;
- if (*wild == '-')
- {
- filenot = TRUE;
- wild++;
- } else filenot = FALSE;
-
- file = name;
- while (*file && *wild)
- {
-
- if (*file == '.')
- {
- file++;
- while (*wild && *wild != '.') wild++;
- if (*wild) wild++;
- }
-
- if (*wild == '(')
- {
- if (attrib == FA_DIREC && !strcmp(wild, "(dir)"))
- {
- wild += 5;
- goto matched;
- }
- else if (attrib == FA_SYSTEM && !strcmp(wild, "(system)"))
- {
- wild += 8;
- goto matched;
- }
- else if (attrib == FA_HIDDEN && !strcmp(wild, "(hidden)"))
- {
- wild += 8;
- goto matched;
- }
- else if (attrib == FA_RDONLY && !strcmp(wild, "(rdonly)"))
- {
- wild += 8;
- goto matched;
- }
- else if (attrib == FA_ARCH && !strcmp(wild, "(archive)"))
- {
- wild += 9;
- goto matched;
- }
- }
-
- if (*wild == '*')
- {
- file++;
- }
- else if (*wild == '?')
- {
- wild++;
- file++;
- }
- else if (*wild != *file)
- {
- goto failed;
- }
- else
- {
- wild++;
- file++;
- }
- }
- while (*wild && *wild == '?') wild++;
- if (*wild == '*') wild++;
- if ((*wild != ' ') &&
- (*wild != *file)) goto failed;
-
- matched:
- if (filenot) match = FALSE;
- else match = TRUE;
-
- failed:
- while (*wild && *wild != ' ') wild++;
- }
-
- return match;
- }
-