Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / glui / glui_filebrowser.cpp
1 /****************************************************************************
2   
3   GLUI User Interface Toolkit
4   ---------------------------
5
6      glui_filebrowser.cpp - GLUI_FileBrowser control class
7
8
9           --------------------------------------------------
10
11   Copyright (c) 1998 Paul Rademacher
12
13   This program is freely distributable without licensing fees and is
14   provided without guarantee or warrantee expressed or implied. This
15   program is -not- in the public domain.
16
17 *****************************************************************************/
18
19 #include "GL/glui.h"
20 #include "glui_internal.h"
21 #include <sys/types.h>
22
23 #ifdef __GNUC__
24 #include <dirent.h>
25 #include <unistd.h>
26 #endif
27
28 #ifdef _WIN32
29 #include <windows.h>
30 #endif
31
32 #include <sys/stat.h>
33
34 GLUI_FileBrowser::GLUI_FileBrowser( GLUI_Node *parent, 
35                                     const char *name,
36                                     int type,
37                                     int id,
38                                     GLUI_CB cb)
39 {
40   common_init();
41
42   set_name( name );
43   user_id    = id;
44   int_val    = type;
45   callback   = cb;
46
47   parent->add_control( this );
48   list = new GLUI_List(this, true, 1);
49   list->set_object_callback( GLUI_FileBrowser::dir_list_callback, this );
50   list->set_click_type(GLUI_DOUBLE_CLICK);
51   this->fbreaddir(this->current_dir.c_str());
52 }
53
54 /****************************** GLUI_FileBrowser::draw() **********/
55
56 void GLUI_FileBrowser::dir_list_callback(GLUI_Control *glui_object) {
57   GLUI_List *list = glui_object->dynamicCastGLUI_List();
58   if (!list) 
59     return;
60   GLUI_FileBrowser* me = list->associated_object->dynamicCastGLUI_FileBrowser();
61   if (!me)
62     return;
63   int this_item;
64   const char *selected;
65   this_item = list->get_current_item();
66   if (this_item > 0) { /* file or directory selected */
67     selected = list->get_item_ptr( this_item )->text.c_str();
68     if (selected[0] == '/' || selected[0] == '\\') {
69       if (me->allow_change_dir) {
70 #ifdef __GNUC__
71         chdir(selected+1);
72 #endif
73 #ifdef _WIN32
74         SetCurrentDirectory(selected+1);
75 #endif
76         me->fbreaddir(".");
77       }
78     } else {
79       me->file = selected;
80       me->execute_callback();
81     }
82   }
83 }
84
85
86
87 void GLUI_FileBrowser::fbreaddir(const char *d) {
88   GLUI_String item;
89   int i = 0;
90         
91         if (!d)
92     return;
93
94 #ifdef _WIN32
95
96   WIN32_FIND_DATA FN;
97   HANDLE hFind;
98   //char search_arg[MAX_PATH], new_file_path[MAX_PATH];
99   //sprintf(search_arg, "%s\\*.*", path_name);
100   
101   hFind = FindFirstFile("*.*", &FN);
102   if (list) {
103     list->delete_all();
104     if (hFind != INVALID_HANDLE_VALUE) {
105       do {
106         int len = int(strlen(FN.cFileName));
107         if (FN.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
108           item = '\\';
109           item += FN.cFileName;
110         } else {
111           item = FN.cFileName;
112         }
113         list->add_item(i,item.c_str());
114         i++;
115       } while (FindNextFile(hFind, &FN) != 0);
116       
117       if (GetLastError() == ERROR_NO_MORE_FILES)
118         FindClose(&FN);
119       else
120         perror("fbreaddir");
121     }
122   }
123
124 #elif defined(__GNUC__)
125
126   DIR *dir;
127   struct dirent *dirp;
128   struct stat dr;
129
130   if (list) {
131     list->delete_all();
132     if ((dir = opendir(d)) == NULL)
133       perror("fbreaddir:");
134     else {
135       while ((dirp = readdir(dir)) != NULL)   /* open directory     */
136       { 
137         if (!lstat(dirp->d_name,&dr) && S_ISDIR(dr.st_mode)) /* dir is directory   */
138           item = dirp->d_name + GLUI_String("/");
139         else
140           item = dirp->d_name;
141
142         list->add_item(i,item.c_str());
143         i++;
144       }
145       closedir(dir);
146     }
147   }
148 #endif
149 }
150
151 void ProcessFiles(const char *path_name)
152 {       
153
154 }
155
156
157 void GLUI_FileBrowser::set_w(int w) 
158
159   if (list) list->set_w(w);
160 }
161
162 void GLUI_FileBrowser::set_h(int h) 
163 {
164   if (list) list->set_h(h);
165 }