// Open Dlg for file selection bool showSelectFileBox(const std::string &title, std::string &selectedFile, const bool &fileOptions/*=true*/) { // Create the selector GtkWidget *file_selector = gtk_file_selection_new(title.c_str()); // Set current filename if (selectedFile!="") { // Converts a filename from UTF-8 to on-disk encoding, and sets it in the // GtkFileSelection. gchar *on_disk_filename; on_disk_filename = g_filename_from_utf8(selectedFile.c_str(), -1, NULL, NULL, NULL); gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_selector), on_disk_filename); g_free(on_disk_filename); } if (fileOptions) gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(file_selector)); else gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(file_selector)); // Display that dialog gtk_widget_show (file_selector); gint result=runModalBox(file_selector); if (result==GTK_RESPONSE_OK) { // Retrieve selected file const gchar *on_disk_filename; gchar *filename; on_disk_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector)); filename = g_filename_to_utf8 (on_disk_filename, -1, NULL, NULL, NULL); if (filename) { selectedFile=filename; g_free(filename); } gtk_widget_destroy(file_selector); return true; } else { gtk_widget_destroy(file_selector); return false; } }
gboolean gw_file_selection_box_show_fileops ( GtkWidget *widget, GtkFileSelection *fs) { gboolean result = FALSE; #ifdef GW_DEBUG_GUI_COMPONENT g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__); #endif if ( fs != NULL ) { gtk_file_selection_show_fileop_buttons ( fs); result = TRUE; } return result; }
/* * Sets the properties of the widget. This is used for both applying the * properties changed in the property editor, and also for loading. */ static void gb_file_selection_set_properties (GtkWidget * widget, GbWidgetSetArgData * data) { gboolean show_fileops; gb_window_set_standard_properties (widget, data, Title, Type, Position, Modal, DefaultWidth, DefaultHeight, Shrink, Grow, AutoShrink, WMName, WMClass, Resizable, DestroyWithParent, Icon); show_fileops = gb_widget_input_bool (data, FileOps); if (data->apply) { if (show_fileops) gtk_file_selection_show_fileop_buttons (GTK_FILE_SELECTION (widget)); else gtk_file_selection_hide_fileop_buttons (GTK_FILE_SELECTION (widget)); } }
char *dir_window_handler(enum InterfaceCommon ops, const char *cur_dir) { static GtkWidget *filew; static int id; static char buf[ MAX_FILE_PATH_LENGTH ]; static const char *saved_cur_dir; switch(ops) { case WIDGET_CREATE : { struct stat st; char check_dir[ MAX_FILE_PATH_LENGTH + 1]; char *checked_dir; saved_cur_dir = cur_dir; checked_dir = check_dir; strncpy(check_dir, cur_dir, MAX_FILE_PATH_LENGTH); /* Add a final / to input directory string if missing and input string is only a directory (i.e. no filename); but pass through any other strings. This allows GtkFileSelection widget to show proper directory level upon entering. */ if((lstat(checked_dir, &st) >= 0) && (S_ISDIR(st.st_mode))) { if((check_dir[ strlen(checked_dir) - 1 ] != '/') && (strlen(checked_dir) > 0) && (strlen(checked_dir) < MAX_FILE_PATH_LENGTH)) { checked_dir = strcat(check_dir, "/"); } } filew = gtk_file_selection_new(_("Select a directory")); gtk_window_set_position(GTK_WINDOW(filew), GTK_WIN_POS_MOUSE); gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(filew)); id = g_signal_connect(G_OBJECT(filew), "destroy", G_CALLBACK(dw_cancel_clicked), NULL); g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filew) ->ok_button), "clicked", G_CALLBACK(dw_ok_clicked), NULL); g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filew) ->cancel_button), "clicked", G_CALLBACK(dw_cancel_clicked), NULL); gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), checked_dir); gtk_widget_hide(GTK_WIDGET(GTK_FILE_SELECTION(filew) ->fileop_del_file)); gtk_widget_hide(GTK_WIDGET(GTK_FILE_SELECTION(filew) ->fileop_ren_file)); gtk_widget_set_sensitive(GTK_WIDGET(GTK_FILE_SELECTION(filew) ->file_list), FALSE); gtk_widget_show(filew); gtk_main(); g_signal_handler_disconnect(G_OBJECT(filew), id); strncpy(buf, gtk_file_selection_get_filename(GTK_FILE_SELECTION(filew)), sizeof(buf)); gtk_widget_destroy(filew); return buf; } case OP_OK : { struct stat st; //FIXME - should be const, make copy?? gchar *temp = (gchar*)gtk_file_selection_get_filename(GTK_FILE_SELECTION(filew)); if(lstat(temp, &st) < 0) { err_handler(INVALID_FILE_SELECTION_ERR, NULL); return NULL; } if(!S_ISDIR(st.st_mode)) { gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), file_path_without_name(temp)); temp = (gchar*)gtk_file_selection_get_filename(GTK_FILE_SELECTION(filew)); } /* remove final directory ../ from directory string if present */ if(strlen(temp) > 3) { if((temp[ strlen(temp) - 1 ] == '/') && (temp[ strlen(temp) - 2 ] == '.') && (temp[ strlen(temp) - 3 ] == '.') && (temp[ strlen(temp) - 4 ] == '/')) { temp[ strlen(temp) - 3 ] = '\0'; gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), temp); } } /* remove final directory ./ from directory string if present */ if(strlen(temp) > 2) { if((temp[ strlen(temp) - 1 ] == '/') && (temp[ strlen(temp) - 2 ] == '.') && (temp[ strlen(temp) - 3 ] == '/')) { temp[ strlen(temp) - 2 ] = '\0'; gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), temp); } } gtk_main_quit(); return NULL; } case OP_CANCEL : gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), saved_cur_dir); gtk_main_quit(); return NULL; } return NULL; }
/* * GetFilename * * Show a dialog with a title and if "Ok" is selected * call the function with the name of the file. */ void GetFilename (char *sTitle, void (*callback) (char *),char *selected, char *filter, int opid) { typFileSelectionData *data; GtkWidget *filterbutton; GtkWidget *optionmenu1, *optionmenu1_menu, *menuitem; filew=NULL; tbflag=TRUE; /* --- Create a new file selection widget --- */ filew = gtk_file_selection_new (sTitle); gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(filew)); data = g_malloc (sizeof (typFileSelectionData)); data->func = callback; data->filesel = filew; data->opid = opid; gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew),selected); gtk_signal_connect (GTK_OBJECT (filew), "destroy", (GtkSignalFunc) destroy, data); ///////////////////////////////////////////////////////////////// *filt=0; strcat(filt, "show only "); strcat(filt, filter); strcat(filt, " files / show all"); if (strcmp(filter,"*.ats")==0) { filterbutton= gtk_button_new_with_label (filt); *filt=0; filt = g_strdup (filter); gtk_box_pack_start (GTK_BOX(GTK_FILE_SELECTION(filew)->action_area), filterbutton, TRUE, TRUE, 0); gtk_widget_show(filterbutton); gtk_signal_connect (GTK_OBJECT (filterbutton), "clicked",GTK_SIGNAL_FUNC(set_filter),(char *)filt); } if (strcmp(filter,"*.apf")==0) { filterbutton= gtk_button_new_with_label (filt); *filt=0; filt = g_strdup (filter); gtk_box_pack_start (GTK_BOX(GTK_FILE_SELECTION(filew)->action_area), filterbutton, TRUE, TRUE, 0); gtk_widget_show(filterbutton); gtk_signal_connect (GTK_OBJECT (filterbutton), "clicked",GTK_SIGNAL_FUNC(set_filter),(char *)filt); } if (strcmp(filter,"*.wav")==0 || strcmp(filter,"*.aif")==0 || strcmp(filter,"*.snd")==0 ) { if(opid!=RES_OUTSEL) { ////////OPTION MENU/////////////////////////////////////// optionmenu1 = gtk_option_menu_new (); gtk_widget_ref (optionmenu1); gtk_object_set_data_full (GTK_OBJECT (filew), "optionmenu1", optionmenu1, (GtkDestroyNotify) gtk_widget_unref); gtk_widget_show (optionmenu1); gtk_box_pack_start (GTK_BOX(GTK_FILE_SELECTION(filew)->action_area),optionmenu1, TRUE, TRUE, 0); optionmenu1_menu = gtk_menu_new (); menuitem = create_men_snd("16 bits shorts WAV file", 0, optionmenu1_menu); menuitem = create_men_snd("32 bits floats WAV file", 1, optionmenu1_menu); menuitem = create_men_snd("16 bits shorts AIF file", 2, optionmenu1_menu); menuitem = create_men_snd("16 bits shorts SND file", 3, optionmenu1_menu); gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu1), optionmenu1_menu); gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu1), (int)outype); gtk_widget_set_usize (optionmenu1, 100, 25); } else { //residual output soundfile can only be WAV filterbutton= gtk_button_new_with_label (filt); *filt=0; filt = g_strdup (filter); gtk_box_pack_start (GTK_BOX(GTK_FILE_SELECTION(filew)->action_area), filterbutton, TRUE, TRUE, 0); gtk_widget_show(filterbutton); gtk_signal_connect (GTK_OBJECT (filterbutton), "clicked",GTK_SIGNAL_FUNC(set_filter),(char *)filt); } } //////////////////////////////////////////////////////////// slabel= gtk_label_new (selected); gtk_box_pack_start (GTK_BOX(GTK_FILE_SELECTION(filew)->action_area), slabel, TRUE, TRUE, 0); gtk_widget_show(slabel); /* --- Connect the "ok" button --- */ gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION (filew)->ok_button), "clicked", (GtkSignalFunc) FileOk, data); /* --- Connect the cancel button --- */ gtk_signal_connect_object ( GTK_OBJECT (GTK_FILE_SELECTION (filew)->cancel_button), "clicked", (GtkSignalFunc) gtk_widget_destroy, (gpointer) filew); gtk_window_set_position(GTK_WINDOW(filew),GTK_WIN_POS_CENTER); /* --- Show the dialog --- */ gtk_widget_show (filew); /* --- Grab the focus. --- */ gtk_grab_add (filew); }