comparison gtk4/dw.c @ 3003:dc33d380d614

GTK4: Fix more deprecation warnings in GTK 4.10 and later. Migrate to GtkFontDialog for 4.10 from GtkFontChooserDialog. Fix file dialog issues with the last commit, didn't save file.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 06 Dec 2023 23:21:03 +0000
parents a70d8ce6151c
children 0ea8d055e7df
comparison
equal deleted inserted replaced
3002:a70d8ce6151c 3003:dc33d380d614
2331 _dw_override_font(handle2, font); 2331 _dw_override_font(handle2, font);
2332 2332
2333 DW_FUNCTION_RETURN_THIS(retval); 2333 DW_FUNCTION_RETURN_THIS(retval);
2334 } 2334 }
2335 2335
2336 /* Internal function to convert from a pango font description to Dynamic Windows style font string */
2337 char *_dw_font_from_pango_font_description(PangoFontDescription *pfont)
2338 {
2339 char *retfont = NULL;
2340
2341 if(pfont)
2342 {
2343 char *font = pango_font_description_to_string(pfont);
2344 int len, x;
2345
2346 retfont = strdup(font);
2347 len = strlen(font);
2348
2349 /* Convert to Dynamic Windows format if we can... */
2350 if(len > 0 && isdigit(font[len-1]))
2351 {
2352 int size;
2353
2354 x=len-1;
2355 while(x > 0 && font[x] != ' ')
2356 {
2357 x--;
2358 }
2359 size = atoi(&font[x]);
2360 /* If we were able to find a valid size... */
2361 if(size > 0)
2362 {
2363 /* Null terminate after the name...
2364 * and create the Dynamic Windows style font.
2365 */
2366 font[x] = 0;
2367 snprintf(retfont, len+1, "%d.%s", size, font);
2368 }
2369 }
2370 g_free(font);
2371 }
2372 return retfont;
2373 }
2374
2375 #if GTK_CHECK_VERSION(4,10,0)
2376 static void _dw_font_choose_response(GObject *gobject, GAsyncResult *result, gpointer data)
2377 {
2378 DWDialog *tmp = data;
2379 GError *error = NULL;
2380 char *fontname = NULL;
2381 PangoFontDescription *pfd = gtk_font_dialog_choose_font_finish(GTK_FONT_DIALOG(gobject), result, &error);
2382
2383 if(error == NULL && pfd != NULL)
2384 {
2385 fontname = _dw_font_from_pango_font_description(pfd);
2386 pango_font_description_free(pfd);
2387 }
2388 dw_dialog_dismiss(tmp, fontname);
2389 }
2390 #endif
2391
2336 /* Allows the user to choose a font using the system's font chooser dialog. 2392 /* Allows the user to choose a font using the system's font chooser dialog.
2337 * Parameters: 2393 * Parameters:
2338 * currfont: current font 2394 * currfont: current font
2339 * Returns: 2395 * Returns:
2340 * A malloced buffer with the selected font or NULL on error. 2396 * A malloced buffer with the selected font or NULL on error.
2342 DW_FUNCTION_DEFINITION(dw_font_choose, char *, const char *currfont) 2398 DW_FUNCTION_DEFINITION(dw_font_choose, char *, const char *currfont)
2343 DW_FUNCTION_ADD_PARAM1(currfont) 2399 DW_FUNCTION_ADD_PARAM1(currfont)
2344 DW_FUNCTION_RETURN(dw_font_choose, char *) 2400 DW_FUNCTION_RETURN(dw_font_choose, char *)
2345 DW_FUNCTION_RESTORE_PARAM1(currfont, const char *) 2401 DW_FUNCTION_RESTORE_PARAM1(currfont, const char *)
2346 { 2402 {
2347 GtkFontChooser *fd; 2403 char *retfont = NULL;
2404 DWDialog *tmp = dw_dialog_new(NULL);
2405 #if GTK_CHECK_VERSION(4,10,0)
2406 char *font = _dw_convert_font(currfont);
2407 PangoFontDescription *pfd = font ? pango_font_description_from_string(font) : NULL;
2408 GtkFontDialog *fd = gtk_font_dialog_new();
2409
2410 gtk_font_dialog_choose_font(fd, NULL, pfd, NULL, (GAsyncReadyCallback)_dw_font_choose_response, tmp);
2411
2412 retfont = dw_dialog_wait(tmp);
2413 #else
2348 char *font = currfont ? strdup(currfont) : NULL; 2414 char *font = currfont ? strdup(currfont) : NULL;
2349 char *name = font ? strchr(font, '.') : NULL; 2415 char *name = font ? strchr(font, '.') : NULL;
2350 char *retfont = NULL; 2416 GtkFontChooser *fd;
2351 DWDialog *tmp = dw_dialog_new(NULL);
2352 2417
2353 /* Detect Dynamic Windows style font name... 2418 /* Detect Dynamic Windows style font name...
2354 * Format: ##.Fontname 2419 * Format: ##.Fontname
2355 * and convert to a Pango name 2420 * and convert to a Pango name
2356 */ 2421 */
2401 g_free(fontname); 2466 g_free(fontname);
2402 } 2467 }
2403 } 2468 }
2404 if(GTK_IS_WINDOW(fd)) 2469 if(GTK_IS_WINDOW(fd))
2405 gtk_window_destroy(GTK_WINDOW(fd)); 2470 gtk_window_destroy(GTK_WINDOW(fd));
2471 #endif
2406 DW_FUNCTION_RETURN_THIS(retfont); 2472 DW_FUNCTION_RETURN_THIS(retfont);
2407 } 2473 }
2408 2474
2409 /* 2475 /*
2410 * Gets the font used by a specified window (widget) handle. 2476 * Gets the font used by a specified window (widget) handle.
2438 2504
2439 pcontext = gtk_widget_get_pango_context(handle2); 2505 pcontext = gtk_widget_get_pango_context(handle2);
2440 if(pcontext) 2506 if(pcontext)
2441 { 2507 {
2442 pfont = pango_context_get_font_description(pcontext); 2508 pfont = pango_context_get_font_description(pcontext);
2443 if(pfont) 2509 retfont = _dw_font_from_pango_font_description(pfont);
2444 {
2445 int len, x;
2446
2447 font = pango_font_description_to_string(pfont);
2448 retfont = strdup(font);
2449 len = strlen(font);
2450 /* Convert to Dynamic Windows format if we can... */
2451 if(len > 0 && isdigit(font[len-1]))
2452 {
2453 int size;
2454
2455 x=len-1;
2456 while(x > 0 && font[x] != ' ')
2457 {
2458 x--;
2459 }
2460 size = atoi(&font[x]);
2461 /* If we were able to find a valid size... */
2462 if(size > 0)
2463 {
2464 /* Null terminate after the name...
2465 * and create the Dynamic Windows style font.
2466 */
2467 font[x] = 0;
2468 snprintf(retfont, len+1, "%d.%s", size, font);
2469 }
2470 }
2471 g_free(font);
2472 }
2473 } 2510 }
2474 DW_FUNCTION_RETURN_THIS(retfont); 2511 DW_FUNCTION_RETURN_THIS(retfont);
2475 } 2512 }
2476 2513
2477 void _dw_free_gdk_colors(HWND handle) 2514 void _dw_free_gdk_colors(HWND handle)
10311 return; 10348 return;
10312 10349
10313 switch(DW_POINTER_TO_INT(tmp->data)) 10350 switch(DW_POINTER_TO_INT(tmp->data))
10314 { 10351 {
10315 case DW_DIRECTORY_OPEN: 10352 case DW_DIRECTORY_OPEN:
10316 gtk_file_dialog_select_folder_finish(GTK_FILE_DIALOG(gobject), result, &error); 10353 file = gtk_file_dialog_select_folder_finish(GTK_FILE_DIALOG(gobject), result, &error);
10317 break; 10354 break;
10318 case DW_FILE_OPEN: 10355 case DW_FILE_OPEN:
10319 gtk_file_dialog_open_finish(GTK_FILE_DIALOG(gobject), result, &error); 10356 file = gtk_file_dialog_open_finish(GTK_FILE_DIALOG(gobject), result, &error);
10320 break; 10357 break;
10321 case DW_FILE_SAVE: 10358 case DW_FILE_SAVE:
10322 gtk_file_dialog_save_finish(GTK_FILE_DIALOG(gobject), result, &error); 10359 file = gtk_file_dialog_save_finish(GTK_FILE_DIALOG(gobject), result, &error);
10323 break; 10360 break;
10324 default: 10361 default:
10325 break; 10362 break;
10326 } 10363 }
10327 10364
10328 if(error == NULL) 10365 if(error == NULL && file != NULL)
10329 { 10366 {
10330 filename = g_file_get_path(file); 10367 filename = g_file_get_path(file);
10331 g_object_unref(G_OBJECT(file)); 10368 g_object_unref(G_OBJECT(file));
10332 } 10369 }
10333 dw_dialog_dismiss(tmp, filename); 10370 dw_dialog_dismiss(tmp, filename);