Google
ResExcellence www
Download a free demo of REALbasic!
Download a free demo of REALbasic!
Recent Articles...
3D
3D Photo Gallery (Part 1)
3D Photo Gallery (Part 2)

Audio
iPod Tricks (Part 1)
iPod Tricks (Part 2)
iPod Tricks (Part 3)
Laugh Track Machine
Audio Player with Reverb
Shepard Melody
RB Phone Home
Build a Drum Machine

Custom Controls
Custom Buttons
Custom Buttons Part II
iTunes-style Listboxes
Custom Controls

General RB
Wiggle Window
JPEG in PDF
Hey! You got your Checkbox in my Listbox!
Background Applications
Listbox Auto-Find
Virtual Volumes
Time Tracker
Software Distribution (Part 1)
Software Distribution (Part 2)
Software Distribution (Part 3)
Software Distribution (Part 4)
Exceptions
Living on the Edge
Tips and Tricks
Review of REALbasic 3.0
Text Clippings Made Easy

Graphics
The SpriteSurface: Space Game
Image Spinner
Cropping Graphics (Part 1)
Cropping Graphics (Part 2)
Cropping Graphics (Part 3)
Cropping Graphics (Part 4)
Shimmer Graphics
Lissajous Figures
Simple Screen Capture
Vector Graphics
Kaleidoscope Images
Stegonography
Spirals!
Image Table
RB Magnifying Lens
Screen Capture
Color Picker Tutorial

Hacks
Ghost Grab
Speedy Mouse Extension
iTunes Plugins
iTunes Skinner

Internet
Display Web Image In Canvas
HTML IMG Tags
Version Tracking
Even Smarter Instant Messaging
Web Tiler
JavaScript and REALbasic
Stock Ticker (Part I)
Stock Ticker (Part 2)
AIM Mate

Mac OS X
Mac OS X Preferences Window
Using Sheets in REALbasic
Build a Bundle (Part 1)
Build a Bundle (Part 2)
Dock Your Passwords
Mac OS X Debugging
REALbasic Mac OS X Icon Tutorial
Animate Your Dock
RB and the Command Line

Menus
Templates Menu
Listbox Menu

Novelty
Guessing Game
Calendar Trivia
Tile Mixer
Zip Code Finder
Happy Valentine's Day
Merlin Simulator (Part 1)
Merlin Simulator (Part 2)
Merlin Simulator (Part 3)
Buzzword Machine
AppleSoft BASIC

Printing
Print to PDF

Registration
Registration Code Validation
Network Registration Codes

Resources
Picture Extractor (Part 1)
Picture Extractor (Part 2)

Serial
Caller ID (Part 1)
Caller ID (Part 2)
Caller ID (Part 3)

Speech
Speech Recognition

Video
Big Brother Video Capture

resexc2.gif (20k)

Newest Dev Tools!




Book Alert !
REALbasic for Dummies
by Erick Tejkowski
$19.99 @ Amazon

Made with REALbasic!
[an error occurred while processing this directive]

Problems?
Downloads are in StuffIt 5 format (free download).
Tell me about a bad link (Thanks!).
Submission Policy

10-08-03

Hierarchial File Browser by Seth Willits





This week we're going to do something fairly simple in comparison to the past month. In this tutorial we're going to use a Hierarchial listbox to create a file browser like the "list mode" in Finder. If you haven't used a Hierarchial listbox before, this is a pretty good way to learn since it's a practical application and not a cheesy little "Hello world!" type of example.

For our file browser, we're going to create a custom Listbox subclass so we can maximize reusability of code. As part of the design, we'll be able to specify which folder is the "root" folder of the list and even be able to change it on the fly if we need to.

To start off, create a listbox subclass named "FileBrowser". Next add three new properties:

  • Files(-1) as FolderItem
  • Items(-1) as Integer
  • RootFolder as FolderItem

The Files() array will be used to hold the folderitems for each of the files and folders listed in the file browser. For instance, Files(2) will be the folderitem for the file/folder in row 3 of the list. Each index in the Items() will contain a number which represents how many files/folders are inside Files(index). If the folderitem at Files(index) is not a folder, then Items(index) will be 0. If the folderitem is a folder which contains 3 other files, then Items(index) will be 3. The last property, RootFolder, is the root folder of the list. For example, if you wanted the list to show all of the files on your desktop, RootFolder would be a folderitem pointing to your desktop folder.

Since the first thing that is going to be done by the file browser is display the files/folders in the RootFolder, lets create and implement a "ShowRootFolder" method which accepts no parameters and returns no value.

Sub ShowRootFolder()
   dim iteration, count as Integer

   // Remove all Rows
   DeleteAllRows
   redim Files(-1)
   redim Items(-1)

   // If no folder specified, show volumes
   if RootFolder = nil then
      Heading(0) = "Computer"

      count = VolumeCount - 1
      for iteration = 0 to count
         AddFolder Volume(iteration).Name
         Files.Append Volume(iteration)
         Items.Append Volume(iteration).Count
      next
   else
      // Show specified root folder

      Heading(0) = RootFolder.Name
      AddRowsInFolder RootFolder, -1
   end if
End Sub

The first thing we're going to do is delete all of the existing rows in the list and remove anything in the Files() and Items() arrays. (If the application just started up then obviously nothing will be in the list or the arrays, but if the list is already open and you want to show a different root folder than it is currently displaying, this accounts for that.) The next thing to do is check whether RootFolder is nil or not. If it is nil, then we're going to show all of the mounted volumes on the computer otherwise we'll show all of the folderitems inside the given RootFolder.

To add all of the mounted volumes into the list, we loop through each index in the Volume() array and add it as folder to the list since volumes are (for practical purposes) folders. We also add the folderitem for the volume to the end of the Files() array, and the number of files/folders inside the volume to the Items() array.

If RootFolder isn't nil, then we're going to call the AddRowsInFolder method (which we'll create in a second) which will add all of the files and folders in the passed folderitem (in this case RootFolder) to the list and also add the appropriate values to the Files() and Items() arrays. The second parameter of the AddRowsInFolder method is the row index to which we're going to add all of the files/folders to the list at. The value -1 which we're passing in the ShowRootFolder method, signifies that the rows that will be added to the list should be added before all existing rows. Seeing the code for the AddRowsInFolder method will make this a little clearer.

The lines which assign values to Heading(0) make the listbox's header the name of the root folder. If there is no root folder, then the heading is "Computer" in an attempt to show that the root folder isn't really a folder, but that the list is showing all of the hard drives, network volumes, and any other volumes (CDs, DMGs, IMGs, etc.).

Sub AddRowsInFolder(folder as folderItem, row as integer)
   dim iteration, count as Integer

   count = folder.Count
   for iteration = 1 to count
      if folder.Item(iteration).Directory then
         me.AddFolder folder.Item(iteration).Name
         Items.Insert row + iteration, folder.Item(iteration).Count
      else
         me.AddRow folder.Item(iteration).Name
         Items.Insert row + iteration, 0
      end if

      Files.Insert row + iteration, folder.Item(iteration)
   next
End Sub

The AddRowsInFolder method is where the majority of the code is, but is actually only half of the implementation. (The other half is actually about 5 lines of code but we'll get there in a while.) What this method does, is loop through all of the files and folders in the "folder" parameter and either insert a simple row or a folder into the list at row index "row".

Recall that FolderItem.Count is a 1-based value signifying how many files and folders are inside the folderitem. Our for-next statement, loops through each of the folderitems inside the folder using the FolderItem.Item(index) method which is also 1-based.

The first step taken inside the loop is to determine whether folder.Item(iteration), (the current file/folder in the folder we're looping through) is a folder or not. If it is a folder, we add a folder to the list for the item, and insert the number of files/folders inside that folder into the Items() array for later use. If folder.Item(iteration) is not a folder, we use the AddRow method and insert a 0 into the Items() array since neither files nor folders can exist inside a file. The last step taken is inserting the FolderItem folder.Item(iteration) into the Files() array.

When to AddRow or Insert Row
If you noticed, when adding rows or folders to the listbox I simply use AddRow or AddFolder but when adding the number of items in a file or folder to the Items() array or when adding the folderitem to the Files() array I use the Insert method. The reason for this is that when AddRow or AddFolder are called from inside of the ExpandRow event of a listbox, REALbasic is smart enough to know that when you say "AddRow" it should actually add the row after the row which is being expanded, however since the arrays don't know that a row is being expanded (arrays don't know listboxes even exist), we have to explicitly insert the value into the appropriate index which is "row + iteration". Recall that the Insert method for any array inserts the value AT the row index passed in the first parameter.

For example, if I Insert a row at index 2, then the current value at index 2 and all those below it get pushed down one index (to index 3, 4, 5 etc.) and the index 2 becomes the value of the second paramter to the Insert method. So imagine that we have a folder named "Images" in the list at index 2 and we want to expand it. When the AddRowsInFolder method is called, it will loop through each file or folder in the "Images" folder and add either a row or a folder to the list in the correct spot automatically since REALbasic is that clever. When it puts the values into the Files() and Items() array however, it will insert them at "row + iteration" where row is the index of the row being expanded, and iteration is the number from 1 to the number of items in the "Images" folder. Therefore, the first item inserted into the Files() and Items() arrays, will be inserted at index "2 + 1", or index 3, just below the value of the folder being expanded. When done this way, the index number of any row in the listbox is also the index number used to get the folderitem or file count from the Files() and Items() array.

I should also point out now, that, the reason we use a Items() array to keep track of the number of files/folders for each of the rows is because what we're really keeping track of is how many rows were added to the list when that row was expanded. Imagine if we expand an item and then add a file to the folder which we just expanded. When you go to collapse the row, normally we would remove just as many rows as we added, but if we used folderItem.Count to determine how many rows to move, then we would actually be removing one more row than we want. Using an array to keep track of the numbers of rows which were actually added to the list prevents this from happening.

The ExpandRow Event
The most difficult part about using a Hierarchial listbox is understanding how you are supposed to add and remove rows to and from the list. Adding rows to the list is actually quite easy. When an item is expanded (the disclosure triangle is clicked to point down), the ExpandRow event is fired once, and the "row" parameter to the event is the row index for the row being expanded. What you need to do in the ExpandRow event is first determine what is going to be added to the list, and then use the AddRow and AddFolder methods to add it. In our case, the items we're going to add to the list are the files and folders inside the folder which is being expanded. Luckily, we've already written a method which does this; the AddRowsInFolder method. As we know, the parameters to the method are 1) a FolderItem for the folder being expanded, and 2) the row number which items will be added after. We can get the folderitem for the folder being expanded using Files(row), and the row number items will be added after is the row number of the row being expanded, or "row". Thus the code for the ExpandRow event is this:

Sub ExpandRow(row As Integer)
   AddRowsInFolder Files(row), row
End Sub

The CollapseRow Event
Removing rows from the list when a row should be collapsed is a bit more difficult, but only slightly (and that's because REALbasic doesn't do something which it probably should). When the disclosure triangle is clicked to point to the right, the CollapseRow event is fired and the value of the "row" parameter is the row index of the row which is collapsing. REALbasic automatically removes the correct rows from the list when a row is collapsed, but the CollapseRow is only ever called once. What this means is that if you have expanded rows inside the row being collapsed, those other expanded rows will not get a CollapseRow event. In (what I imagine to be) a small number of cases this doesn't matter, but if you're relying on the CollapseRow event to know when you need to remove items from any array or do anything else associated with the rows being removed, then you've got to figure out how to do it all at once within a single CollapseRow event...... sort of. The workaround "trick" is to collapse any expanded rows before they're removed so that a CollapseRow event is called for those rows too. In code, this would look like this:

Sub CollapseRow(row As Integer)
   dim iteration as integer

   for iteration = row + Items(row) DownTo row + 1
      Expanded(iteration) = false
   next
End Sub

What this does is loops through each of the rows inside the expanded folder and collapse them by setting Expanded() to false. When Expanded(index) is set to false and the row at index "index" is a folder which is expanded, the CollapseRow event is fired for that row allowing it to collapse the rows in its folder, and the rows in those rows and so on and so forth. However, the reason we need to do this isn't because have to collapse rows before they're removed, it's because we need to remove the corresponding values for those rows from the Files() and Items() arrays. So lets to do that:

Sub CollapseRow(row As Integer)
   dim iteration as integer

   for iteration = row + Items(row) DownTo row + 1
      Expanded(iteration) = false
      Items.Remove iteration
      Files.Remove iteration
   next
End Sub

So now, whenever a folder is collapsed, the values in the Files() and Items() arrays which pertain to the rows in that folder, are removed. Also, any folders inside the folder are collapsed which then causes the values for those rows to be removed from the arrays. So what we're doing is adding and removing values to these two arrays whenever rows or folders are added or removed from the list.

Showing The RootFolder
Now since we want to show the items in the RootFolder when the listbox opens, we need to call ShowRootFolder from the Open event, but call the Open event of the instance before doing so, so that you have a chance to set the RootFolder to something before it displays the default values which are the mounted volumes. To do this, create a new event named "Open", and in the Open event in the FileBrowser class, call Open, then call ShowRootFolder:

Sub Open()
   Open
   
   ShowRootFolder
End Sub

Test It!
After that, you're done implementing the FileBrowser class, so now lets use it. Drag an instance of the FileBrowser class to the main window in the project, and drag a static text field just below it. (You may wish to resize the controls to make them nice and pretty and resize correctly using the Lock properties.) In the Open event of the FileBrowser instance, we can set the RootFolder property if we desire (I've set mine to the DesktopFolder in the downloadable project), and using the Change event, I can get the absolute path from the folderItem for the currently selected row like this:

Sub Change()
   if me.ListIndex <> -1 then
      StaticText1.Text = me.Files(me.ListIndex).AbsolutePath
   else
      StaticText1.Text = ""
   end if
End Sub

That's it! Run the project and play with collapsing and expanding different rows. Make sure FileBrowser was written correctly and doesn't goof at all. You may wish to expand on the code here to give the FileBrowser class the option of showing invisible files (currently is shows all files). You may wish to include row pictures for the folder items. Do whatever you wish. Hope this was intriguing.






10-04-03

REALbasic News by



REALbasic Items

Operator_Compare Notes
Charles Yeomans has "banged out a quick article" on the Operator_Compare function in REALbasic 5.5. The 2,200 word article explains what Operator_Compare is, has indepth example s of how to use it, and tells why you would use it.

ComplexMatrix Plugin 1.2
The ComplexMatrix plugin adds new functionality to handle complex numbers and matrices, and thanks to the new operator overloading, like Operator_Compare mentioned above, it can all be done much in the same way that doubles and integers are handled.New in version 2 is the ability to find the eigenvalues and eigenvectors of Hermiltian complex matrices, as well as a new Polynomial type.The plugin is available for Carbon, Classic, and Windows.

DataGrid 2.0.2 with Valentina Example
Einhugur Software has released an update to the DataGrid plugin which now officially supports the Valentina database engine. The DataGrid 2.0.2 download includes an example demonstrating the use of more than 100,000 rows from a Valentina database. The DataGrid plugin can be found on Einhugur Software's website.

Single-Window Application Project Template
Hate having to recreate the same basic objects in every project you start? Freak Software has released a project template which has several necessary things that every application should have. Included in the template are an "About" menu item, their "dual behavior" standard Mac OS X About window, a Select All menu item which is followed by a custom listbox and editfield class which support Select All, and a Preferences menu item and associated window all linked together. In addition, the editfield subclass also responds properly to Command-Arrow key combinations for moving around in fields.

Cheetah3d Beta 2
Cheetah3d is a new 3d modeling application for Mac OS X. Beta 2 now adds the ability to export models into 3dmf which can be used in a RB3dSpace in a REALbasic application. I like it.


Made With REALbasic:

RealCADD 3.5
RealCADD is a professional 2D CAD software, easy to use but with many features and shareware price. Version 3.5 has several optimizations and enhancements including an object hierarchy via layers, the ability to import bitmap or vector-based pictures, find and replace text, several new drawing tools, and new user options. RealCADD is available for Mac OS 9, Mac OS X, and Windows for $95. Upgrading to 3.5 is $20.

Days Away v1.0
Paraiahware released the first version of Days Away, an application which periodically reminds the user of important days such as a project due date or your anniversary. To add a date, simply click on the calendar and give the day a name. Days Away will then fade in, pause, and fade out every user-specified amount of time in order to remind you of that day. Days Away is a $10 shareware application available only for Mac OS X.





resexc1.gif (8k)

Seth Willits


Maintained by the Staff of ResExcellence. This entire site ©1997-2003 ResExcellence
Privacy Statement? Sure we gotta Privacy Statement.

[an error occurred while processing this directive]

[an error occurred while processing this directive]on the ResEx LinuxPPC Server