comparison gtk4/dw.c @ 3002:a70d8ce6151c

GTK4: Fix a number of deprecation warnings in GTK 4.10 and later. Migrate to GtkFileDialog for 4.10 from GtkFileChooserDialog. Migrate to gtk_widget_get_width/height() instead of GtkAllocation funcions.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 06 Dec 2023 02:16:05 +0000
parents 4861e9f49907
children dc33d380d614
comparison
equal deleted inserted replaced
3001:a464a74505f7 3002:a70d8ce6151c
4224 /* 4224 /*
4225 * Gets the text used for a given window. 4225 * Gets the text used for a given window.
4226 * Parameters: 4226 * Parameters:
4227 * handle: Handle to the window. 4227 * handle: Handle to the window.
4228 * Returns: 4228 * Returns:
4229 * text: The text associsated with a given window. 4229 * text: The text associated with a given window.
4230 */ 4230 */
4231 DW_FUNCTION_DEFINITION(dw_window_get_text, char *, HWND handle) 4231 DW_FUNCTION_DEFINITION(dw_window_get_text, char *, HWND handle)
4232 DW_FUNCTION_ADD_PARAM1(handle) 4232 DW_FUNCTION_ADD_PARAM1(handle)
4233 DW_FUNCTION_RETURN(dw_window_get_text, char *) 4233 DW_FUNCTION_RETURN(dw_window_get_text, char *)
4234 DW_FUNCTION_RESTORE_PARAM1(handle, HWND) 4234 DW_FUNCTION_RESTORE_PARAM1(handle, HWND)
9927 GtkWidget *widget = data; 9927 GtkWidget *widget = data;
9928 float *percent = (float *)g_object_get_data(G_OBJECT(widget), "_dw_percent"); 9928 float *percent = (float *)g_object_get_data(G_OBJECT(widget), "_dw_percent");
9929 9929
9930 if(percent) 9930 if(percent)
9931 { 9931 {
9932 int width, height;
9933 #if GTK_CHECK_VERSION(4,12,0)
9934 width = gtk_widget_get_width(widget);
9935 height = gtk_widget_get_height(widget);
9936 #else
9932 GtkAllocation alloc; 9937 GtkAllocation alloc;
9933 9938
9934 gtk_widget_get_allocation(widget, &alloc); 9939 gtk_widget_get_allocation(widget, &alloc);
9935 9940 width = alloc.width;
9936 if(alloc.width > 10 && alloc.height > 10) 9941 height = alloc.height;
9942 #endif
9943
9944 if(width > 10 && height > 10)
9937 { 9945 {
9938 if(gtk_orientable_get_orientation(GTK_ORIENTABLE(widget)) == GTK_ORIENTATION_HORIZONTAL) 9946 if(gtk_orientable_get_orientation(GTK_ORIENTABLE(widget)) == GTK_ORIENTATION_HORIZONTAL)
9939 gtk_paned_set_position(GTK_PANED(widget), (int)(alloc.width * (*percent / 100.0))); 9947 gtk_paned_set_position(GTK_PANED(widget), (int)(width * (*percent / 100.0)));
9940 else 9948 else
9941 gtk_paned_set_position(GTK_PANED(widget), (int)(alloc.height * (*percent / 100.0))); 9949 gtk_paned_set_position(GTK_PANED(widget), (int)(height * (*percent / 100.0)));
9942 g_object_set_data(G_OBJECT(widget), "_dw_percent", NULL); 9950 g_object_set_data(G_OBJECT(widget), "_dw_percent", NULL);
9943 free(percent); 9951 free(percent);
9944 } 9952 }
9945 else 9953 else
9946 return TRUE; 9954 return TRUE;
10005 { 10013 {
10006 float *mypercent = (float *)dw_window_get_data(handle, "_dw_percent"); 10014 float *mypercent = (float *)dw_window_get_data(handle, "_dw_percent");
10007 int size = 0, position; 10015 int size = 0, position;
10008 10016
10009 if(gtk_orientable_get_orientation(GTK_ORIENTABLE(handle)) == GTK_ORIENTATION_HORIZONTAL) 10017 if(gtk_orientable_get_orientation(GTK_ORIENTABLE(handle)) == GTK_ORIENTATION_HORIZONTAL)
10018 {
10019 #if GTK_CHECK_VERSION(4,12,0)
10020 size = gtk_widget_get_width(handle);
10021 #else
10010 size = gtk_widget_get_allocated_width(handle); 10022 size = gtk_widget_get_allocated_width(handle);
10023 #endif
10024 }
10011 else 10025 else
10026 {
10027 #if GTK_CHECK_VERSION(4,12,0)
10028 size = gtk_widget_get_height(handle);
10029 #else
10012 size = gtk_widget_get_allocated_height(handle); 10030 size = gtk_widget_get_allocated_height(handle);
10031 #endif
10032 }
10013 10033
10014 if(mypercent) 10034 if(mypercent)
10015 *mypercent = percent; 10035 *mypercent = percent;
10016 10036
10017 if(size > 10) 10037 if(size > 10)
10276 return; 10296 return;
10277 } 10297 }
10278 env->MajorVersion = atoi(tempbuf); 10298 env->MajorVersion = atoi(tempbuf);
10279 } 10299 }
10280 10300
10301 #if GTK_CHECK_VERSION(4,10,0)
10302 static void _dw_file_browse_response(GObject *gobject, GAsyncResult *result, gpointer data)
10303 {
10304 DWDialog *tmp = data;
10305 GError *error = NULL;
10306 char *filename = NULL;
10307 GFile *file = NULL;
10308
10309 /* Bail out if there is no DWDialog */
10310 if(!tmp)
10311 return;
10312
10313 switch(DW_POINTER_TO_INT(tmp->data))
10314 {
10315 case DW_DIRECTORY_OPEN:
10316 gtk_file_dialog_select_folder_finish(GTK_FILE_DIALOG(gobject), result, &error);
10317 break;
10318 case DW_FILE_OPEN:
10319 gtk_file_dialog_open_finish(GTK_FILE_DIALOG(gobject), result, &error);
10320 break;
10321 case DW_FILE_SAVE:
10322 gtk_file_dialog_save_finish(GTK_FILE_DIALOG(gobject), result, &error);
10323 break;
10324 default:
10325 break;
10326 }
10327
10328 if(error == NULL)
10329 {
10330 filename = g_file_get_path(file);
10331 g_object_unref(G_OBJECT(file));
10332 }
10333 dw_dialog_dismiss(tmp, filename);
10334 }
10335 #endif
10336
10281 /* 10337 /*
10282 * Opens a file dialog and queries user selection. 10338 * Opens a file dialog and queries user selection.
10283 * Parameters: 10339 * Parameters:
10284 * title: Title bar text for dialog. 10340 * title: Title bar text for dialog.
10285 * defpath: The default path of the open dialog. 10341 * defpath: The default path of the open dialog.
10286 * ext: Default file extention. 10342 * ext: Default file extension.
10287 * flags: DW_FILE_OPEN or DW_FILE_SAVE or DW_DIRECTORY_OPEN 10343 * flags: DW_FILE_OPEN or DW_FILE_SAVE or DW_DIRECTORY_OPEN
10288 * Returns: 10344 * Returns:
10289 * NULL on error. A malloced buffer containing 10345 * NULL on error. A malloced buffer containing
10290 * the file path on success. 10346 * the file path on success.
10291 * 10347 *
10293 DW_FUNCTION_DEFINITION(dw_file_browse, char *, const char *title, const char *defpath, const char *ext, int flags) 10349 DW_FUNCTION_DEFINITION(dw_file_browse, char *, const char *title, const char *defpath, const char *ext, int flags)
10294 DW_FUNCTION_ADD_PARAM4(title, defpath, ext, flags) 10350 DW_FUNCTION_ADD_PARAM4(title, defpath, ext, flags)
10295 DW_FUNCTION_RETURN(dw_file_browse, char *) 10351 DW_FUNCTION_RETURN(dw_file_browse, char *)
10296 DW_FUNCTION_RESTORE_PARAM4(title, const char *, defpath, const char *, ext, const char *, flags, int) 10352 DW_FUNCTION_RESTORE_PARAM4(title, const char *, defpath, const char *, ext, const char *, flags, int)
10297 { 10353 {
10354 char buf[1001] = {0};
10355 char *filename = NULL;
10356 DWDialog *tmp = dw_dialog_new(DW_INT_TO_POINTER(flags));
10357 #if GTK_CHECK_VERSION(4,10,0)
10358 GtkFileDialog *dialog = gtk_file_dialog_new();
10359
10360 gtk_file_dialog_set_title(dialog, title);
10361 if(defpath)
10362 {
10363 GFile *path = g_file_new_for_path(defpath);
10364
10365 /* See if the path exists */
10366 if(path)
10367 {
10368 /* If the path is a directory... set the current folder */
10369 if(g_file_query_file_type(path, G_FILE_QUERY_INFO_NONE, NULL) == G_FILE_TYPE_DIRECTORY)
10370 gtk_file_dialog_set_initial_folder(dialog, path);
10371 else
10372 gtk_file_dialog_set_initial_file(dialog, path);
10373
10374 g_object_unref(G_OBJECT(path));
10375 }
10376 }
10377 if(ext)
10378 {
10379 GListStore *filters = g_list_store_new (GTK_TYPE_FILE_FILTER);
10380 GtkFileFilter *filter = gtk_file_filter_new();
10381 snprintf(buf, 1000, "*.%s", ext);
10382 gtk_file_filter_add_pattern(filter, (gchar *)buf);
10383 snprintf(buf, 1000, "\"%s\" files", ext);
10384 gtk_file_filter_set_name(filter, (gchar *)buf);
10385 g_list_store_append(filters, filter);
10386 filter = gtk_file_filter_new();
10387 gtk_file_filter_add_pattern(filter, (gchar *)"*");
10388 gtk_file_filter_set_name(filter, (gchar *)"All Files");
10389 g_list_store_append(filters, filter);
10390 gtk_file_dialog_set_filters(dialog, G_LIST_MODEL(filters));
10391 }
10392
10393 switch(flags)
10394 {
10395 case DW_DIRECTORY_OPEN:
10396 gtk_file_dialog_select_folder(dialog, NULL, NULL, (GAsyncReadyCallback)_dw_file_browse_response, tmp);
10397 break;
10398 case DW_FILE_OPEN:
10399 gtk_file_dialog_open(dialog, NULL, NULL, (GAsyncReadyCallback)_dw_file_browse_response, tmp);
10400 break;
10401 case DW_FILE_SAVE:
10402 gtk_file_dialog_save(dialog, NULL, NULL, (GAsyncReadyCallback)_dw_file_browse_response, tmp);
10403 break;
10404 default:
10405 dw_messagebox( "Coding error", DW_MB_OK|DW_MB_ERROR, "dw_file_browse() flags argument invalid.");
10406 tmp = NULL;
10407 break;
10408 }
10409
10410 if(tmp)
10411 filename = dw_dialog_wait(tmp);
10412 #else
10298 GtkWidget *filew; 10413 GtkWidget *filew;
10299
10300 GtkFileChooserAction action; 10414 GtkFileChooserAction action;
10301 GtkFileFilter *filter1 = NULL;
10302 GtkFileFilter *filter2 = NULL;
10303 gchar *button = NULL; 10415 gchar *button = NULL;
10304 char *filename = NULL;
10305 char buf[1001] = {0};
10306 DWDialog *tmp = dw_dialog_new(NULL);
10307 10416
10308 switch(flags) 10417 switch(flags)
10309 { 10418 {
10310 case DW_DIRECTORY_OPEN: 10419 case DW_DIRECTORY_OPEN:
10311 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER; 10420 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
10333 button, GTK_RESPONSE_ACCEPT, 10442 button, GTK_RESPONSE_ACCEPT,
10334 NULL); 10443 NULL);
10335 10444
10336 if(ext) 10445 if(ext)
10337 { 10446 {
10338 filter1 = gtk_file_filter_new(); 10447 GtkFileFilter *filter = gtk_file_filter_new();
10339 snprintf(buf, 1000, "*.%s", ext); 10448 snprintf(buf, 1000, "*.%s", ext);
10340 gtk_file_filter_add_pattern( filter1, (gchar *)buf); 10449 gtk_file_filter_add_pattern(filter, (gchar *)buf);
10341 snprintf(buf, 1000, "\"%s\" files", ext); 10450 snprintf(buf, 1000, "\"%s\" files", ext);
10342 gtk_file_filter_set_name(filter1, (gchar *)buf); 10451 gtk_file_filter_set_name(filter, (gchar *)buf);
10343 filter2 = gtk_file_filter_new(); 10452 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter);
10344 gtk_file_filter_add_pattern(filter2, (gchar *)"*"); 10453 filter = gtk_file_filter_new();
10345 gtk_file_filter_set_name(filter2, (gchar *)"All Files"); 10454 gtk_file_filter_add_pattern(filter, (gchar *)"*");
10346 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter1); 10455 gtk_file_filter_set_name(filter, (gchar *)"All Files");
10347 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter2); 10456 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter);
10348 } 10457 }
10349 10458
10350 if(defpath) 10459 if(defpath)
10351 { 10460 {
10352 GFile *path = g_file_new_for_path(defpath); 10461 GFile *path = g_file_new_for_path(defpath);
10375 } 10484 }
10376 10485
10377 if(GTK_IS_WINDOW(filew)) 10486 if(GTK_IS_WINDOW(filew))
10378 gtk_window_destroy(GTK_WINDOW(filew)); 10487 gtk_window_destroy(GTK_WINDOW(filew));
10379 } 10488 }
10489 #endif
10380 DW_FUNCTION_RETURN_THIS(filename); 10490 DW_FUNCTION_RETURN_THIS(filename);
10381 } 10491 }
10382 10492
10383 static void _dw_exec_launched(GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data) 10493 static void _dw_exec_launched(GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
10384 { 10494 {