comparison gtk3/dw.c @ 2027:ed9217a95924

GTK3: Rewrite dw_messagebox() to use the GTK dialog system. Cancel button for YESNOCANCEL might require internationalization.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 11 Nov 2019 22:31:43 +0000
parents 617a78dc70aa
children fb292c267f52
comparison
equal deleted inserted replaced
2026:a9809db1ddbf 2027:ed9217a95924
50 # define __func__ __FUNCTION__ 50 # define __func__ __FUNCTION__
51 # else 51 # else
52 # define __func__ "<unknown>" 52 # define __func__ "<unknown>"
53 # endif 53 # endif
54 #endif 54 #endif
55
56 #include "gtk/messagebox_error.xpm"
57 #include "gtk/messagebox_warning.xpm"
58 #include "gtk/messagebox_information.xpm"
59 #include "gtk/messagebox_question.xpm"
60 55
61 /* These are used for resource management */ 56 /* These are used for resource management */
62 #if defined(DW_RESOURCES) && !defined(BUILD_DLL) 57 #if defined(DW_RESOURCES) && !defined(BUILD_DLL)
63 extern DWResources _resources; 58 extern DWResources _resources;
64 #endif 59 #endif
2199 tmp = dialog->result; 2194 tmp = dialog->result;
2200 free(dialog); 2195 free(dialog);
2201 return tmp; 2196 return tmp;
2202 } 2197 }
2203 2198
2204 static int _dw_ok_func(HWND window, void *data)
2205 {
2206 DWDialog *dwwait = (DWDialog *)data;
2207
2208 if(!dwwait)
2209 return FALSE;
2210
2211 dw_window_destroy((HWND)dwwait->data);
2212 dw_dialog_dismiss((DWDialog *)data, (void *)DW_MB_RETURN_OK);
2213 return FALSE;
2214 }
2215
2216 int _dw_yes_func(HWND window, void *data)
2217 {
2218 DWDialog *dwwait = (DWDialog *)data;
2219
2220 if(!dwwait)
2221 return FALSE;
2222
2223 dw_window_destroy((HWND)dwwait->data);
2224 dw_dialog_dismiss((DWDialog *)data, (void *)DW_MB_RETURN_YES);
2225 return FALSE;
2226 }
2227
2228 int _dw_no_func(HWND window, void *data)
2229 {
2230 DWDialog *dwwait = (DWDialog *)data;
2231
2232 if(!dwwait)
2233 return FALSE;
2234
2235 dw_window_destroy((HWND)dwwait->data);
2236 dw_dialog_dismiss((DWDialog *)data, (void *)DW_MB_RETURN_NO);
2237 return FALSE;
2238 }
2239
2240 int _dw_cancel_func(HWND window, void *data)
2241 {
2242 DWDialog *dwwait = (DWDialog *)data;
2243
2244 if(!dwwait)
2245 return FALSE;
2246
2247 dw_window_destroy((HWND)dwwait->data);
2248 dw_dialog_dismiss((DWDialog *)data, (void *)DW_MB_RETURN_CANCEL);
2249 return FALSE;
2250 }
2251
2252 /* 2199 /*
2253 * Displays a debug message on the console... 2200 * Displays a debug message on the console...
2254 * Parameters: 2201 * Parameters:
2255 * format: printf style format string. 2202 * format: printf style format string.
2256 * ...: Additional variables for use in the format. 2203 * ...: Additional variables for use in the format.
2275 * format: printf style format string. 2222 * format: printf style format string.
2276 * ...: Additional variables for use in the format. 2223 * ...: Additional variables for use in the format.
2277 */ 2224 */
2278 int dw_messagebox(char *title, int flags, char *format, ...) 2225 int dw_messagebox(char *title, int flags, char *format, ...)
2279 { 2226 {
2280 HWND entrywindow, texttargetbox, imagetextbox, mainbox, okbutton, nobutton, yesbutton, cancelbutton, buttonbox, stext; 2227 GtkMessageType gtkicon = GTK_MESSAGE_OTHER;
2281 ULONG flStyle = DW_FCF_TITLEBAR | DW_FCF_SHELLPOSITION | DW_FCF_SIZEBORDER; 2228 GtkButtonsType gtkbuttons = GTK_BUTTONS_OK;
2282 DWDialog *dwwait; 2229 GtkWidget *dialog;
2230 int response, _locked_by_me = FALSE;
2283 va_list args; 2231 va_list args;
2284 char outbuf[1025] = {0}; 2232 char outbuf[1025] = {0};
2285 char **xpm_data = NULL;
2286 int x, y, extra_width=0,text_width,text_height;
2287 int width,height;
2288 2233
2289 va_start(args, format); 2234 va_start(args, format);
2290 vsnprintf(outbuf, 1024, format, args); 2235 vsnprintf(outbuf, 1024, format, args);
2291 va_end(args); 2236 va_end(args);
2292 2237
2293 entrywindow = dw_window_new(HWND_DESKTOP, title, flStyle);
2294 mainbox = dw_box_new(DW_VERT, 10);
2295 dw_box_pack_start(entrywindow, mainbox, 0, 0, TRUE, TRUE, 0);
2296
2297 /* determine if an icon is to be used - if so we need another HORZ box */
2298 if((flags & DW_MB_ERROR) | (flags & DW_MB_WARNING) | (flags & DW_MB_INFORMATION) | (flags & DW_MB_QUESTION))
2299 {
2300 imagetextbox = dw_box_new(DW_HORZ, 0);
2301 dw_box_pack_start(mainbox, imagetextbox, 0, 0, TRUE, TRUE, 2);
2302 texttargetbox = imagetextbox;
2303 }
2304 else
2305 {
2306 imagetextbox = NULL;
2307 texttargetbox = mainbox;
2308 }
2309
2310 if(flags & DW_MB_ERROR) 2238 if(flags & DW_MB_ERROR)
2311 xpm_data = (char **)_dw_messagebox_error; 2239 gtkicon = GTK_MESSAGE_ERROR;
2312 else if(flags & DW_MB_WARNING) 2240 else if(flags & DW_MB_WARNING)
2313 xpm_data = (char **)_dw_messagebox_warning; 2241 gtkicon = GTK_MESSAGE_WARNING;
2314 else if(flags & DW_MB_INFORMATION) 2242 else if(flags & DW_MB_INFORMATION)
2315 xpm_data = (char **)_dw_messagebox_information; 2243 gtkicon = GTK_MESSAGE_INFO;
2316 else if(flags & DW_MB_QUESTION) 2244 else if(flags & DW_MB_QUESTION)
2317 xpm_data = (char **)_dw_messagebox_question; 2245 gtkicon = GTK_MESSAGE_QUESTION;
2318 2246
2319 if(xpm_data) 2247 if(flags & DW_MB_OKCANCEL)
2320 extra_width = 32; 2248 gtkbuttons = GTK_BUTTONS_OK_CANCEL;
2321 2249 else if(flags & (DW_MB_YESNO | DW_MB_YESNOCANCEL))
2322 if(texttargetbox == imagetextbox) 2250 gtkbuttons = GTK_BUTTONS_YES_NO;
2323 { 2251
2324 HWND handle = dw_bitmap_new( 100 ); 2252 DW_MUTEX_LOCK;
2325 GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)xpm_data); 2253 dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL | GTK_DIALOG_USE_HEADER_BAR, gtkicon, gtkbuttons, "%s", title);
2326 2254 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), outbuf);
2327 gtk_image_set_from_pixbuf(GTK_IMAGE(handle), icon_pixbuf); 2255 if(flags & DW_MB_YESNOCANCEL)
2328 2256 gtk_dialog_add_button(GTK_DIALOG(dialog), "Cancel", GTK_RESPONSE_CANCEL);
2329 dw_box_pack_start( texttargetbox, handle, 32, 32, FALSE, FALSE, 2); 2257 response = gtk_dialog_run(GTK_DIALOG(dialog));
2330 } 2258 gtk_widget_destroy(dialog);
2331 2259 DW_MUTEX_UNLOCK;
2332 /* Create text */ 2260 switch(response)
2333 text_width = 240; 2261 {
2334 text_height = 0; 2262 case GTK_RESPONSE_OK:
2335 stext = dw_text_new(outbuf, 0); 2263 return DW_MB_RETURN_OK;
2336 dw_window_set_style(stext, DW_DT_WORDBREAK, DW_DT_WORDBREAK); 2264 case GTK_RESPONSE_CANCEL:
2337 dw_font_text_extents_get(stext, NULL, outbuf, &width, &height); 2265 return DW_MB_RETURN_CANCEL;
2338 2266 case GTK_RESPONSE_YES:
2339 text_width = min( width, dw_screen_width() - extra_width - 100 ); 2267 return DW_MB_RETURN_YES;
2340 text_height = min( height, dw_screen_height() ); 2268 case GTK_RESPONSE_NO:
2341 2269 return DW_MB_RETURN_NO;
2342 dw_box_pack_start(texttargetbox, stext, text_width, text_height, TRUE, TRUE, 2); 2270 default:
2343 2271 {
2344 /* Buttons */ 2272 /* Handle the destruction of the dialog result */
2345 buttonbox = dw_box_new(DW_HORZ, 10); 2273 if(flags & (DW_MB_OKCANCEL | DW_MB_YESNOCANCEL))
2346 2274 return DW_MB_RETURN_CANCEL;
2347 dw_box_pack_start(mainbox, buttonbox, 0, 0, TRUE, FALSE, 0); 2275 else if(flags & DW_MB_YESNO)
2348 2276 return DW_MB_RETURN_NO;
2349 dwwait = dw_dialog_new((void *)entrywindow); 2277 }
2350 2278 }
2351 /* which buttons ? */ 2279 return DW_MB_RETURN_OK;
2352 if(flags & DW_MB_OK)
2353 {
2354 okbutton = dw_button_new("Ok", 1001L);
2355 dw_box_pack_start(buttonbox, okbutton, 50, 30, TRUE, FALSE, 2);
2356 dw_signal_connect(okbutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_ok_func), (void *)dwwait);
2357 }
2358 else if(flags & DW_MB_OKCANCEL)
2359 {
2360 okbutton = dw_button_new("Ok", 1001L);
2361 dw_box_pack_start(buttonbox, okbutton, 50, 30, TRUE, FALSE, 2);
2362 dw_signal_connect(okbutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_ok_func), (void *)dwwait);
2363 cancelbutton = dw_button_new("Cancel", 1002L);
2364 dw_box_pack_start(buttonbox, cancelbutton, 50, 30, TRUE, FALSE, 2);
2365 dw_signal_connect(cancelbutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_cancel_func), (void *)dwwait);
2366 }
2367 else if(flags & DW_MB_YESNO)
2368 {
2369 yesbutton = dw_button_new("Yes", 1001L);
2370 dw_box_pack_start(buttonbox, yesbutton, 50, 30, TRUE, FALSE, 2);
2371 dw_signal_connect(yesbutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_yes_func), (void *)dwwait);
2372 nobutton = dw_button_new("No", 1002L);
2373 dw_box_pack_start(buttonbox, nobutton, 50, 30, TRUE, FALSE, 2);
2374 dw_signal_connect(nobutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_no_func), (void *)dwwait);
2375 }
2376 else if(flags & DW_MB_YESNOCANCEL)
2377 {
2378 yesbutton = dw_button_new("Yes", 1001L);
2379 dw_box_pack_start(buttonbox, yesbutton, 50, 30, TRUE, FALSE, 2);
2380 dw_signal_connect(yesbutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_yes_func), (void *)dwwait);
2381 nobutton = dw_button_new("No", 1002L);
2382 dw_box_pack_start(buttonbox, nobutton, 50, 30, TRUE, FALSE, 2);
2383 dw_signal_connect(nobutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_no_func), (void *)dwwait);
2384 cancelbutton = dw_button_new("Cancel", 1003L);
2385 dw_box_pack_start(buttonbox, cancelbutton, 50, 30, TRUE, FALSE, 2);
2386 dw_signal_connect(cancelbutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_cancel_func), (void *)dwwait);
2387 }
2388
2389 height = max(50,text_height)+100;
2390 x = (dw_screen_width() - (text_width+60+extra_width))/2;
2391 y = (dw_screen_height() - height)/2;
2392
2393 dw_window_set_pos_size(entrywindow, x, y, (text_width+60+extra_width), height);
2394
2395 dw_window_show(entrywindow);
2396
2397 return GPOINTER_TO_INT(dw_dialog_wait(dwwait));
2398 } 2280 }
2399 2281
2400 /* 2282 /*
2401 * Minimizes or Iconifies a top-level window. 2283 * Minimizes or Iconifies a top-level window.
2402 * Parameters: 2284 * Parameters: