Add File Manager

This commit is contained in:
daleclack 2021-10-09 14:05:57 +08:00
parent d35779f2ee
commit 22ea82446f
16 changed files with 805 additions and 28 deletions

View File

@ -48,11 +48,11 @@ if(WIN32)
)
add_executable(My_GtkUi WIN32 ${app_WINRC} src/main.cpp src/background.cpp src/game.cpp
src/TextEditor.cpp src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/drawing.cpp
${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
src/FileWindow.cpp ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
else()
add_executable(My_GtkUi src/main.cpp src/background.cpp src/game.cpp src/TextEditor.cpp
src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/drawing.cpp
${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
src/FileWindow.cpp ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
endif(WIN32)
set_source_files_properties(

BIN
Gtk3/res/dialog-error.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -10,6 +10,11 @@
<file>graphics.png</file>
<file>system.png</file>
<file>log-out.png</file>
<file>gnome-fs-directory.png</file>
<file>gnome-fs-regular.png</file>
<file>view-grid-symbolic.png</file>
<file>view-list-symbolic.png</file>
<file>dialog-error.png</file>
<file preprocess="xml-stripblanks">window.ui</file>
<file preprocess="xml-stripblanks">leftpanel.ui</file>
<file preprocess="xml-stripblanks">toppanel.ui</file>

View File

@ -10,6 +10,11 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
<object class="GtkPopover" id="popover1">
<property name="width-request">300</property>
<property name="height-request">400</property>
@ -156,11 +161,12 @@
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButton" id="btngame">
<property name="label" translatable="yes">Guess Game</property>
<object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image3</property>
<property name="relief">none</property>
</object>
<packing>
@ -170,8 +176,8 @@
</packing>
</child>
<child>
<object class="GtkButton" id="btneditor">
<property name="label" translatable="yes">Simple Text Editor</property>
<object class="GtkButton" id="btnfiles">
<property name="label" translatable="yes">File Manager</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
@ -198,12 +204,11 @@
</packing>
</child>
<child>
<object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property>
<object class="GtkButton" id="btngame">
<property name="label" translatable="yes">Guess Game</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image3</property>
<property name="relief">none</property>
</object>
<packing>
@ -213,7 +218,18 @@
</packing>
</child>
<child>
<placeholder/>
<object class="GtkButton" id="btneditor">
<property name="label" translatable="yes">Simple Text Editor</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
<packing>
@ -271,9 +287,4 @@
<placeholder/>
</child>
</object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
</interface>

View File

@ -10,6 +10,11 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
<object class="GtkPopover" id="popover1">
<property name="width-request">300</property>
<property name="height-request">400</property>
@ -156,11 +161,12 @@
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButton" id="btngame">
<property name="label" translatable="yes">Guess Game</property>
<object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image3</property>
<property name="relief">none</property>
</object>
<packing>
@ -170,8 +176,8 @@
</packing>
</child>
<child>
<object class="GtkButton" id="btneditor">
<property name="label" translatable="yes">Simple Text Editor</property>
<object class="GtkButton">
<property name="label" translatable="yes">File Manager</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
@ -198,12 +204,11 @@
</packing>
</child>
<child>
<object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property>
<object class="GtkButton" id="btngame">
<property name="label" translatable="yes">Guess Game</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image3</property>
<property name="relief">none</property>
</object>
<packing>
@ -213,7 +218,18 @@
</packing>
</child>
<child>
<placeholder/>
<object class="GtkButton" id="btneditor">
<property name="label" translatable="yes">Simple Text Editor</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
<packing>
@ -271,9 +287,4 @@
<placeholder/>
</child>
</object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
</interface>

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 B

561
Gtk3/src/FileWindow.cpp Normal file
View File

@ -0,0 +1,561 @@
#include "FileWindow.h"
#define FOLDER_NAME "/gtk42/gnome-fs-directory.png"
#define FILE_NAME "/gtk42/gnome-fs-regular.png"
#define ERROR_IMAGE "/gtk42/dialog-error.png"
#define ICON_IMAGE "/gtk42/view-grid-symbolic.png"
#define LIST_IMAGE "/gtk42/view-list-symbolic.png"
//IconView and TreeView Model
enum{
COL_PATH,
COL_DISPLAY_NAME,
COL_PIXBUF,
COL_IS_DIRECTORY,
NUM_COLS
};
typedef enum _ViewMode{
MODE_ICON,
MODE_LIST
}ViewMode;
struct _FileWindow{
GtkWindow parent;
GdkPixbuf *file_pixbuf, *folder_pixbuf;
char *parent_dir;
GtkToolItem *up_button;
GtkWidget *stack, *show_hidden, *view_button, *tree_view, *icon_view, *btn_image;
GtkListStore *store;
GtkTreeSelection *selection;
GList *selected_items;
ViewMode view_mode;
};
G_DEFINE_TYPE(FileWindow,file_window,GTK_TYPE_WINDOW)
static void file_window_load_pixbufs(int size,FileWindow *win){
GdkPixbuf *tmp1,*tmp2;
if(win->file_pixbuf){ //Already loaded
return ;
}
tmp1=gdk_pixbuf_new_from_resource(FILE_NAME,NULL);
win->file_pixbuf=gdk_pixbuf_scale_simple(tmp1,size,size,GDK_INTERP_BILINEAR);
//Check load state
g_assert(win->file_pixbuf);
g_object_unref(tmp1);
tmp2=gdk_pixbuf_new_from_resource(FOLDER_NAME,NULL);
win->folder_pixbuf=gdk_pixbuf_scale_simple(tmp2,size,size,GDK_INTERP_BILINEAR);
g_assert(win->folder_pixbuf);
g_object_unref(tmp2);
}
static void file_window_fill_store(FileWindow *win){
GDir * dir;
const gchar *name;
GtkTreeIter iter;
//Clear the store
gtk_list_store_clear(win->store);
//Go through the directory and get information
dir=g_dir_open(win->parent_dir,0,NULL);
if(!dir){
return;
}
/* Ignore the files start with '.' when the button is not toggled */
name=g_dir_read_name(dir);
while(name!=NULL){
gchar *path, *display_name;
gboolean is_dir;
if(name[0]=='.' && !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->show_hidden))){
name=g_dir_read_name(dir);
continue;
}
path = g_build_filename(win->parent_dir, name, NULL);
is_dir = g_file_test(path, G_FILE_TEST_IS_DIR);
display_name = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
gtk_list_store_append(win->store, &iter);
gtk_list_store_set(win->store, &iter,
COL_PATH, path,
COL_DISPLAY_NAME, display_name,
COL_IS_DIRECTORY, is_dir,
COL_PIXBUF, is_dir ? win->folder_pixbuf : win->file_pixbuf,
-1);
g_free (path);
g_free (display_name);
name=g_dir_read_name(dir);
}
g_dir_close(dir);
}
static int sort_func(GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer user_data)
{
gboolean is_dir_a,is_dir_b;
gchar *name_a,*name_b;
int ret;
//Sort Folders before files,
//and sort files and folders starts with a '.' in front of other files and folders
gtk_tree_model_get(model,a,COL_IS_DIRECTORY,&is_dir_a,COL_DISPLAY_NAME,&name_a,-1);
gtk_tree_model_get(model,b,COL_IS_DIRECTORY,&is_dir_b,COL_DISPLAY_NAME,&name_b,-1);
if(!is_dir_a && is_dir_b){
ret = 1;
}else if(is_dir_a && !is_dir_b){
ret = -1;
}else if(name_a[0]!='.' && name_b[0]=='.'){
ret = 1;
}else if(name_a[0]=='.' && name_b[0]!='.'){
ret = -1;
}else{
ret = g_utf8_collate(name_a,name_b);
}
g_free(name_a);
g_free(name_b);
return ret;
}
static GtkListStore* create_store(void){
GtkListStore *store;
store=gtk_list_store_new(NUM_COLS,
G_TYPE_STRING,
G_TYPE_STRING,
GDK_TYPE_PIXBUF,
G_TYPE_BOOLEAN);
gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(store),
sort_func,NULL,NULL);
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
return store;
}
static void row_activated(GtkTreeView *view,GtkTreePath *tree_path,GtkTreeViewColumn *column,FileWindow *win){
gchar *path;
GtkTreeIter iter;
gboolean is_dir;
gtk_tree_model_get_iter(GTK_TREE_MODEL(win->store),&iter,tree_path);
gtk_tree_model_get(GTK_TREE_MODEL(win->store),&iter,COL_PATH,&path,COL_IS_DIRECTORY,&is_dir,-1);
if(!is_dir)
{
g_free(path);
return;
}
/* Replace parent with path and re-fill the model*/
g_free(win->parent_dir);
win->parent_dir = path;
file_window_fill_store(win);
/* Sensitize the up button */
gtk_widget_set_sensitive(GTK_WIDGET(win->up_button),TRUE);
}
static void item_activated(GtkIconView *view,GtkTreePath *tree_path,FileWindow *win){
gchar *path;
GtkTreeIter iter;
gboolean is_dir;
gtk_tree_model_get_iter(GTK_TREE_MODEL(win->store),&iter,tree_path);
gtk_tree_model_get(GTK_TREE_MODEL(win->store),&iter,COL_PATH,&path,COL_IS_DIRECTORY,&is_dir,-1);
if(!is_dir)
{
g_free(path);
return;
}
/* Replace parent with path and re-fill the model*/
g_free(win->parent_dir);
win->parent_dir = path;
file_window_fill_store(win);
/* Sensitize the up button */
gtk_widget_set_sensitive(GTK_WIDGET(win->up_button),TRUE);
}
static void up_clicked(GtkToolItem *item,FileWindow *win){
gchar *dir_name;
dir_name = g_path_get_dirname(win->parent_dir);
g_free(win->parent_dir);
win->parent_dir = dir_name;
file_window_fill_store(win);
//Set Up button to sensitive
gtk_widget_set_sensitive(GTK_WIDGET(win->up_button),TRUE);
}
static void home_clicked(GtkToolItem *item,FileWindow *win){
g_free(win->parent_dir);
win->parent_dir = g_strdup(g_get_home_dir());
file_window_fill_store(win);
//Set Up button to sensitive
gtk_widget_set_sensitive(GTK_WIDGET(win->up_button),TRUE);
}
static void btnview_clicked(GtkButton *widget,FileWindow *win){
if(win->view_mode == MODE_ICON){
gtk_image_set_from_resource(GTK_IMAGE(win->btn_image),LIST_IMAGE);
gtk_stack_set_visible_child_name(GTK_STACK(win->stack),"List_view");
win->view_mode = MODE_LIST;
}else{
gtk_image_set_from_resource(GTK_IMAGE(win->btn_image),ICON_IMAGE);
gtk_stack_set_visible_child_name(GTK_STACK(win->stack),"Icon_view");
win->view_mode = MODE_ICON;
}
}
static void make_directory(GtkWidget *widget,int response,GtkEntry *entry){
//Make a new directory
if(response == GTK_RESPONSE_OK){
FileWindow *win=FILE_WINDOW(gtk_window_get_transient_for(GTK_WINDOW(widget)));
const char *folder=gtk_entry_get_text(entry);
char *path, *parent_path;
parent_path=win->parent_dir;
if(strlen(parent_path) == 1 && parent_path[0] == '/'){
path=g_strdup_printf("%s%s",parent_path,folder);
}else{
path=g_strdup_printf("%s/%s",parent_path,folder);
}
if(g_mkdir_with_parents(path,0755) == -1){
g_print("Error Occured!");
}else{
file_window_fill_store(win);
}
g_free(path);
}
gtk_widget_destroy(widget);
}
static void btnnew_clicked(GtkToolItem *item,GtkWindow *parent){
//Create Dialog
GtkWidget *dialog, *content_area, *entry;
dialog = gtk_dialog_new_with_buttons("Create a folder",parent,
GTK_DIALOG_USE_HEADER_BAR,"New",GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog),GTK_RESPONSE_OK);
//Add entry
content_area=gtk_dialog_get_content_area(GTK_DIALOG(dialog));
entry=gtk_entry_new();
gtk_entry_set_activates_default(GTK_ENTRY(entry),TRUE);
gtk_container_add(GTK_CONTAINER(content_area),entry);
g_signal_connect(dialog,"response",G_CALLBACK(make_directory),entry);
gtk_widget_show_all(dialog);
}
static GtkWidget * create_list_view(FileWindow *win){
GtkWidget * tree_view;
GtkCellRenderer *icon_render,*text_render;
GtkTreeViewColumn *column;
tree_view=gtk_tree_view_new_with_model(GTK_TREE_MODEL(win->store));
//gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(tree_view),GTK_SELECTION_MULTIPLE);
/* We now set which model columns that correspond to the text
* and pixbuf of each item
*/
icon_render=gtk_cell_renderer_pixbuf_new();
text_render=gtk_cell_renderer_text_new();
column=gtk_tree_view_column_new_with_attributes("",icon_render,"pixbuf",COL_PIXBUF,NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view),column);
column=gtk_tree_view_column_new_with_attributes("Name",text_render,"text",COL_DISPLAY_NAME,NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view),column);
/* Connect to the "item-activated" signal */
g_signal_connect(tree_view, "row-activated",
G_CALLBACK (row_activated), win);
return tree_view;
}
static GtkWidget * create_icon_view(FileWindow *win){
GtkWidget *icon_view;
icon_view=gtk_icon_view_new_with_model(GTK_TREE_MODEL(win->store));
gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(icon_view),GTK_SELECTION_MULTIPLE);
/* We now set which model columns that correspond to the text
* and pixbuf of each item
*/
gtk_icon_view_set_text_column(GTK_ICON_VIEW(icon_view),COL_DISPLAY_NAME);
gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(icon_view),COL_PIXBUF);
/* Connect to the "item-activated" signal */
g_signal_connect(icon_view, "item-activated",
G_CALLBACK (item_activated), win);
return icon_view;
}
static void create_view_button(FileWindow * win){
win->view_button=gtk_button_new();
win->btn_image=gtk_image_new_from_resource(ICON_IMAGE);
gtk_button_set_relief(GTK_BUTTON(win->view_button),GTK_RELIEF_NONE);
gtk_button_set_image(GTK_BUTTON(win->view_button),win->btn_image);
}
static GtkWidget * create_delete_dialog(FileWindow *win,const char *msg){
//Message Dialog
GtkWidget *dialog, *hbox, *content_area, *label, *error_image, *label1, *vbox;
char * msg_str=g_strdup_printf("Delete Failed:%s",msg);
if(!msg){
return NULL;
}
dialog = gtk_dialog_new_with_buttons("File",GTK_WINDOW(win),GTK_DIALOG_DESTROY_WITH_PARENT,
"OK",GTK_RESPONSE_OK,NULL);
gtk_window_set_default_size(GTK_WINDOW(dialog),300,150);
gtk_window_set_icon_name(GTK_WINDOW(dialog),"org.gtk.daleclack");
gtk_window_set_title(GTK_WINDOW(dialog),"Error");
//Child Widgets
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
label1 = gtk_label_new(" ");
gtk_box_pack_start(GTK_BOX(vbox),label1,FALSE,FALSE,0);
error_image = gtk_image_new_from_resource(ERROR_IMAGE);
gtk_box_pack_start(GTK_BOX(hbox),error_image,FALSE,FALSE,0);
label = gtk_label_new(msg_str);
gtk_box_pack_end(GTK_BOX(hbox),label,FALSE,FALSE,0);
gtk_widget_set_valign(hbox,GTK_ALIGN_CENTER);
gtk_widget_set_halign(hbox,GTK_ALIGN_CENTER);
gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,0);
gtk_container_add(GTK_CONTAINER(content_area),vbox);
g_signal_connect_swapped(dialog,"response",G_CALLBACK(gtk_widget_destroy),dialog);
g_free(msg_str);
return dialog;
}
static GtkToolItem * create_menubtn(FileWindow *win){
//Create Button and item for toolbar
GtkToolItem *menuitem;
GtkWidget *menubtn, *popover, *vbox, *btnexit, *label_exit;
menuitem = gtk_tool_item_new();
menubtn = gtk_menu_button_new();
gtk_button_set_relief(GTK_BUTTON(menubtn),GTK_RELIEF_NONE);
gtk_container_add(GTK_CONTAINER(menuitem),menubtn);
//Create Menu
popover = gtk_popover_new(menubtn);
gtk_popover_set_modal(GTK_POPOVER(popover),TRUE);
gtk_menu_button_set_popover(GTK_MENU_BUTTON(menubtn),popover);
//CheckButton for show hidden dir and files
win->show_hidden = gtk_check_button_new_with_label("Show Hidden Files");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->show_hidden),FALSE);
//"Exit" Button
btnexit = gtk_button_new();
label_exit = gtk_label_new("Exit");
gtk_button_set_relief(GTK_BUTTON(btnexit),GTK_RELIEF_NONE);
gtk_widget_set_halign(label_exit,GTK_ALIGN_START);
gtk_container_add(GTK_CONTAINER(btnexit),label_exit);
g_signal_connect_swapped(btnexit,"clicked",G_CALLBACK(gtk_widget_destroy),win);
//Add Widgets to popover
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
gtk_box_pack_start(GTK_BOX(vbox),win->show_hidden,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(vbox),btnexit,TRUE,TRUE,0);
gtk_container_add(GTK_CONTAINER(popover),vbox);
gtk_widget_show_all(vbox);
return menuitem;
}
static void btndel_clicked(GtkToolItem *item,FileWindow *win){
GtkTreeIter iter;
char * select_name = NULL;
GtkWidget *dialog=NULL;
switch(win->view_mode){
case MODE_ICON: //Iconfied Mode
GList *list,*header;
list = gtk_icon_view_get_selected_items(GTK_ICON_VIEW(win->icon_view));
header=list;
while(list!=NULL){
GtkTreePath *path = (GtkTreePath*)(list->data);
if(gtk_tree_model_get_iter(GTK_TREE_MODEL(win->store),&iter,path)){
gtk_tree_model_get(GTK_TREE_MODEL(win->store),&iter,COL_DISPLAY_NAME,&select_name,-1);
dialog = create_delete_dialog(win,select_name);
}
list=list->next;
}
g_list_free_full(header, (GDestroyNotify) gtk_tree_path_free);
break;
case MODE_LIST: //Listed Mode
GtkTreeModel * model = gtk_tree_view_get_model(GTK_TREE_VIEW(win->tree_view));
if(gtk_tree_selection_get_selected(win->selection,&model,&iter)){
gtk_tree_model_get(model,&iter,COL_DISPLAY_NAME,&select_name,-1);
}
//g_object_unref(model);
dialog = create_delete_dialog(win,select_name);
break;
}
if(dialog){
gtk_widget_show_all(dialog);
}
g_free(select_name);
}
static void file_window_destroy(GtkWidget *widget){
gtk_widget_destroy(widget);
widget=NULL;
//g_object_unref(FILE_WINDOW(widget)->file_pixbuf);
//FILE_WINDOW(widget)->file_pixbuf=NULL;
//g_object_unref(FILE_WINDOW(widget)->folder_pixbuf);
//FILE_WINDOW(widget)->folder_pixbuf=NULL;
}
static void file_window_init(FileWindow *window){
GtkWidget *sw,*vbox,*tool_bar,*btnbox,*view_bar;
GtkToolItem *home_button,*new_button,*delete_button,*view_item;
//Initalize window
gtk_window_set_default_size(GTK_WINDOW(window),650,400);
gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack");
gtk_window_set_title(GTK_WINDOW(window),"File Manager");
file_window_load_pixbufs(48,window);
//Create child widgets
vbox=gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
btnbox=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
gtk_container_add(GTK_CONTAINER(vbox),btnbox);
gtk_container_add(GTK_CONTAINER(window),vbox);
tool_bar=gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(tool_bar),GTK_TOOLBAR_ICONS);
gtk_box_pack_start(GTK_BOX(btnbox),tool_bar,FALSE,FALSE,0);
//"Up" Button
window->up_button=gtk_tool_button_new(NULL,NULL);
gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(window->up_button),"go-up");
gtk_tool_item_set_is_important(window->up_button,TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(window->up_button),FALSE);
gtk_toolbar_insert(GTK_TOOLBAR(tool_bar),window->up_button,-1);
//"Home" Button
home_button = gtk_tool_button_new(NULL,NULL);
gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(home_button),"go-home");
gtk_tool_item_set_is_important(home_button,TRUE);
gtk_toolbar_insert(GTK_TOOLBAR(tool_bar),home_button,-1);
//"New Folder" Button
new_button = gtk_tool_button_new(NULL,NULL);
gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(new_button),"folder-new");
gtk_tool_item_set_is_important(new_button,TRUE);
gtk_toolbar_insert(GTK_TOOLBAR(tool_bar),new_button,-1);
//"Delete" Button
delete_button = gtk_tool_button_new(NULL,NULL);
gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(delete_button),"edit-delete");
gtk_tool_item_set_is_important(delete_button,TRUE);
gtk_toolbar_insert(GTK_TOOLBAR(tool_bar),delete_button,-1);
//"View Mode" Button
view_bar = gtk_toolbar_new();
view_item = gtk_tool_item_new();
window->view_mode = MODE_ICON;
create_view_button(window);
gtk_container_add(GTK_CONTAINER(view_item),window->view_button);
gtk_toolbar_insert(GTK_TOOLBAR(view_bar),view_item,-1);
//Menu Button
GtkToolItem *menubtn = create_menubtn(window);
gtk_toolbar_insert(GTK_TOOLBAR(view_bar),menubtn,-1);
gtk_toolbar_set_style(GTK_TOOLBAR(view_bar),GTK_TOOLBAR_ICONS);
gtk_box_pack_end(GTK_BOX(btnbox),view_bar,FALSE,FALSE,0);
//Folder Container
sw = gtk_scrolled_window_new(NULL,NULL);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
gtk_box_pack_start(GTK_BOX(vbox),sw,TRUE,TRUE,0);
window->stack=gtk_stack_new();
//Create Store and fill it
window->parent_dir=g_strdup("/");
window->store=create_store();
file_window_fill_store(window);
window->tree_view=create_list_view(window);
window->icon_view=create_icon_view(window);
gtk_stack_add_named(GTK_STACK(window->stack),window->icon_view,"Icon_view");
gtk_stack_add_named(GTK_STACK(window->stack),window->tree_view,"List_view");
g_object_unref(window->store);
window->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(window->tree_view));
/* Connect to the "clicked" signal of the "Up" tool button */
g_signal_connect(window->up_button, "clicked",
G_CALLBACK (up_clicked), window);
/* Connect to the "clicked" signal of the "Home" tool button */
g_signal_connect(home_button, "clicked",
G_CALLBACK (home_clicked), window);
/* Connect to the "changed" signal of the "View Mode" Button */
g_signal_connect(window->view_button, "clicked",
G_CALLBACK (btnview_clicked), window);
/* Connect to the "clicked" signal of the "Show hidden files" tool button */
g_signal_connect_swapped(window->show_hidden, "clicked",
G_CALLBACK (file_window_fill_store), window);
/* Connect to the "clicked" signal of the "New Directory" tool button */
g_signal_connect(new_button, "clicked",
G_CALLBACK (btnnew_clicked), window);
/* Connect to the "clicked" signal of the "Delete" tool button */
g_signal_connect(delete_button, "clicked",
G_CALLBACK (btndel_clicked), window);
gtk_container_add(GTK_CONTAINER(sw),window->stack);
}
static void file_window_class_init(FileWindowClass *win_class){
GTK_WIDGET_CLASS(file_window_parent_class)->destroy=file_window_destroy;
}
FileWindow * file_window_new(){
return (FileWindow*)g_object_new(file_window_get_type(),NULL);
}

10
Gtk3/src/FileWindow.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __FILE_WINDOW_H_
#define __FILE_WINDOW_H_
#include <gtk/gtk.h>
G_DECLARE_FINAL_TYPE(FileWindow,file_window,FILE,WINDOW,GtkWindow)
FileWindow * file_window_new();
#endif

View File

@ -17,6 +17,8 @@ static void gtkmain(GtkApplication *app,gpointer user_data){
//Home button
GObject *img_home=gtk_builder_get_object(builder,"image1");
gtk_image_set_from_resource(GTK_IMAGE(img_home),"/gtk42/user-home.png");
GObject *btn_home=gtk_builder_get_object(builder,"btn_home");
g_signal_connect(btn_home,"clicked",G_CALLBACK(btnfiles_clicked),window);
//Get button for change background(moved to panel 1)
/*GObject *btn_back=gtk_builder_get_object(builder,"btnback");

View File

@ -5,6 +5,7 @@
#include "game.h"
#include "TextEditor.h"
#include "drawing.h"
#include "FileWindow.h"
static void btnvlc_clicked(GtkWidget *widget,gpointer data){
std::thread first(system,"vlc");
@ -31,6 +32,12 @@ static void btnvlc_win32(GtkWidget *widget,gpointer data){
fifth.detach();
}
void btnfiles_clicked(GtkWidget *widget,GtkWindow *parent){
FileWindow * window1 = file_window_new();
gtk_window_set_transient_for(GTK_WINDOW(window1),parent);
gtk_widget_show_all(GTK_WIDGET(window1));
}
void add_leftpanel(GtkBuilder *builder,GtkFixed *fixed){
//Get Left panel
GtkBuilder *panel2=gtk_builder_new_from_resource("/gtk42/leftpanel.ui");
@ -91,6 +98,10 @@ void add_leftpanel(GtkBuilder *builder,GtkFixed *fixed){
GObject *btn_about=gtk_builder_get_object(panel2,"btnabout");
g_signal_connect(btn_about,"clicked",G_CALLBACK(win1_init),window);
g_signal_connect_swapped(btn_about,"clicked",G_CALLBACK(gtk_widget_hide),popover);
//File Manager
GObject *btnfiles=gtk_builder_get_object(panel2,"btnfiles");
g_signal_connect(btnfiles,"clicked",G_CALLBACK(btnfiles_clicked),window);
g_signal_connect_swapped(btnfiles,"clicked",G_CALLBACK(gtk_widget_hide),popover);
gtk_fixed_put(fixed,GTK_WIDGET(panel),0,25);
g_object_unref(pixbuf);
g_object_unref(sized);

View File

@ -5,6 +5,8 @@
void add_leftpanel(GtkBuilder *builder,GtkFixed *fixed);
void btnfiles_clicked(GtkWidget *widget,GtkWindow *parent);
//void btnvlc_clicked(GtkWidget *widget,gpointer data);
//void btngedit_clicked(GtkWidget *widget,gpointer data);

154
Gtkmm3/cfgfile2/cfgfile.cc Normal file
View File

@ -0,0 +1,154 @@
// A Test to read Config files
// This is a modified version
// The Config File Should be a text file
// and the content as follows
// key=value
#include "cfgfile.hh"
static void Trim(std::string &str){
if(str.empty()){//String is empty
return;
}
size_t i,start_pos,end_pos;
for(i=0;i<str.size();i++){//Get Start Position
if(!isspace(str[i])){
break;
}
}
if(i==str.size()){//Only Space and Tabs in string
return;
}
start_pos=i;
for(i=str.size()-1;i>=0;i--){//Get End Position
if(!isspace(str[i])){
break;
}
}
end_pos=i;
str=str.substr(start_pos,end_pos+1);
}
bool readCfgFile(std::string &cfgfilePath,conf_map &configs){
//Open The Config File and load config to map
std::fstream cfgfile;
cfgfile.open(cfgfilePath);
if(!cfgfile.is_open()){
std::cout<<"Failed to open the file!"<<std::endl;
return false;
}
//If Map is not empty,clear it
if(!configs.empty()){
configs.clear();
}
//Read each line of config file and get value
char tmp[1000];
while(!cfgfile.eof()){
cfgfile.getline(tmp,1000);
std::string line(tmp),value;
std::size_t pos = line.find('=');//Get the position of '='
if(pos == std::string::npos){//No Configs
continue;
}
std::string key=line.substr(0,pos);
value=line.substr(pos+1);
//Clear Spaces
Trim(key);Trim(value);
configs[key]=value;
}
cfgfile.close();
return true;
}
// Some Test Codes,for Test Purpose only.
// void readcfg(){
// //Input Config File Path and Key
// char filename[1000];
// std::string cfgkey;
// conf_map test_config;
// std::cout<<"Input Config File Name:";//Config File Name
// std::cin.get();//Clear the cin to read line
// std::cin.getline(filename,1000);
// std::cout<<"Input Config Key:";//Config Key
// std::cin>>cfgkey;
// //Get The Config
// if(readCfgFile(filename,test_config)){
// std::cout<<"The Config Of "<<cfgkey<<" is "<<test_config[cfgkey]<<std::endl;
// }else{
// std::cout<<"Failed to Read Config!"<<std::endl;
// }
// }
// void appendcfg(){
// //Input Config File Path and Key
// char filename[1000];
// std::string cfgkey,cfgvalue;
// std::cout<<"Input Config File Name:";
// std::cin.get();//Clear the cin to read line
// std::cin.getline(filename,1000);
// //Append Config
// std::fstream cfgfile;
// cfgfile.open(filename,std::ios_base::app);
// if(cfgfile.is_open()){
// std::cout<<"Input Config Key And Config Value:";
// std::cin>>cfgkey>>cfgvalue;
// cfgfile<<std::endl;
// cfgfile<<cfgkey<<"="<<cfgvalue;
// cfgfile.close();
// }else{
// std::cout<<"Config File Open Failed!"<<std::endl;
// }
// }
// void show_configs(){
// //Input Config File Path and Key
// char filename[1000];
// conf_map test_config;
// std::cout<<"Input Config File Name"<<std::endl;//Config File Name
// std::cin.get();//Clear the cin to read line
// std::cin.getline(filename,1000);
// //Get The Config and Show all Config in map
// if(readCfgFile(filename,test_config)){
// auto mite=test_config.begin();
// for(;mite!=test_config.end();mite++){
// std::cout<<mite->first<<"="<<mite->second<<std::endl;
// }
// }else{
// std::cout<<"Failed to Read Config!"<<std::endl;
// }
// }
// int main(int argc,char **argv){
// //Function Pointer to select mode
// pfun feature[]={readcfg,appendcfg,show_configs};
// //Get Selection and execute a mode
// int selection;
// std::cout<<"0.Read a Config 1.Append a Config 2.Show all Configs"<<std::endl<<"Selection a mode:";
// std::cin>>selection;
// feature[selection]();
// return 0;
// }

View File

@ -0,0 +1,10 @@
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <map>
typedef std::map<std::string,std::string> conf_map;//Container of configs
bool readCfgFile(std::string &cfgfilePath,conf_map &configs);