comparison gtk3/dw.c @ 1561:46e34bd92336

Implemented guessing size of MLE on GTK... not as accurate as the other platforms... but until the bugs are fixed in GTK this is the best we can do. Also make dw_window_get_preferred_size() return our guesses. Added a piece of safety code on Windows... just in case.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 17 Jan 2012 18:18:00 +0000
parents 4a9c574d5c17
children 9a8aa230a538
comparison
equal deleted inserted replaced
1560:ee47bda26916 1561:46e34bd92336
8335 void dw_exit(int exitcode) 8335 void dw_exit(int exitcode)
8336 { 8336 {
8337 exit(exitcode); 8337 exit(exitcode);
8338 } 8338 }
8339 8339
8340 /* Internal function to get the recommended size of scrolled items */
8341 void _get_scrolled_size(GtkWidget *item, gint *thiswidth, gint *thisheight)
8342 {
8343 GtkWidget *widget = g_object_get_data(G_OBJECT(item), "_dw_user");
8344
8345 *thisheight = *thiswidth = 0;
8346
8347 if(widget)
8348 {
8349 if(g_object_get_data(G_OBJECT(widget), "_dw_tree_type") == GINT_TO_POINTER(_DW_TREE_TYPE_TREE))
8350 {
8351 *thiswidth = _DW_SCROLLED_MAX_WIDTH;
8352 *thisheight = _DW_SCROLLED_MAX_HEIGHT;
8353 }
8354 else if(GTK_IS_TEXT_VIEW(widget))
8355 {
8356 unsigned long bytes;
8357 int height, width;
8358 char *buf, *ptr;
8359 int wrap = (gtk_text_view_get_wrap_mode(GTK_TEXT_VIEW(widget)) == GTK_WRAP_WORD);
8360 static char testtext[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
8361 int hscrolled = FALSE;
8362
8363 *thiswidth = *thisheight = 0;
8364
8365 dw_mle_get_size(item, &bytes, NULL);
8366
8367 ptr = buf = alloca(bytes + 2);
8368 dw_mle_export(item, buf, 0, (int)bytes);
8369 buf[bytes] = 0;
8370 strcat(buf, "\n");
8371
8372 /* MLE */
8373 while((ptr = strstr(buf, "\r")))
8374 {
8375 ptr[0] = 0;
8376 width = 0;
8377 if(strlen(buf))
8378 dw_font_text_extents_get(item, NULL, buf, &width, &height);
8379 else
8380 dw_font_text_extents_get(item, NULL, testtext, NULL, &height);
8381
8382 if(wrap && width > _DW_SCROLLED_MAX_WIDTH)
8383 {
8384 *thiswidth = _DW_SCROLLED_MAX_WIDTH;
8385 *thisheight += height * (width / _DW_SCROLLED_MAX_WIDTH);
8386 }
8387 else
8388 {
8389 if(width > *thiswidth)
8390 {
8391 if(width > _DW_SCROLLED_MAX_WIDTH)
8392 {
8393 *thiswidth = _DW_SCROLLED_MAX_WIDTH;
8394 hscrolled = TRUE;
8395 }
8396 else
8397 *thiswidth = width;
8398 }
8399 }
8400 *thisheight += height;
8401 if(ptr[1] == '\n')
8402 buf = &ptr[2];
8403 else
8404 buf = &ptr[1];
8405 }
8406 if(hscrolled)
8407 *thisheight += 20;
8408 }
8409 else
8410 {
8411 gtk_widget_get_preferred_height(GTK_WIDGET(widget), NULL, thisheight);
8412 gtk_widget_get_preferred_width(GTK_WIDGET(widget), NULL, thiswidth);
8413
8414 *thisheight += 20;
8415 *thiswidth += 20;
8416 }
8417 }
8418 if(*thiswidth < _DW_SCROLLED_MIN_WIDTH)
8419 *thiswidth = _DW_SCROLLED_MIN_WIDTH;
8420 if(*thiswidth > _DW_SCROLLED_MAX_WIDTH)
8421 *thiswidth = _DW_SCROLLED_MAX_WIDTH;
8422 if(*thisheight < _DW_SCROLLED_MIN_HEIGHT)
8423 *thisheight = _DW_SCROLLED_MIN_HEIGHT;
8424 if(*thisheight > _DW_SCROLLED_MAX_HEIGHT)
8425 *thisheight = _DW_SCROLLED_MAX_HEIGHT;
8426 }
8427
8340 /* Internal box packing function called by the other 3 functions */ 8428 /* Internal box packing function called by the other 3 functions */
8341 void _dw_box_pack(HWND box, HWND item, int index, int width, int height, int hsize, int vsize, int pad, char *funcname) 8429 void _dw_box_pack(HWND box, HWND item, int index, int width, int height, int hsize, int vsize, int pad, char *funcname)
8342 { 8430 {
8343 int warn = FALSE, _locked_by_me = FALSE; 8431 int warn = FALSE, _locked_by_me = FALSE;
8344 GtkWidget *tmp, *tmpitem, *image = NULL; 8432 GtkWidget *tmp, *tmpitem, *image = NULL;
8465 } 8553 }
8466 g_object_set_data(G_OBJECT(box), "_dw_boxcount", GINT_TO_POINTER(boxcount + 1)); 8554 g_object_set_data(G_OBJECT(box), "_dw_boxcount", GINT_TO_POINTER(boxcount + 1));
8467 /* Special case for scrolled windows */ 8555 /* Special case for scrolled windows */
8468 if(GTK_IS_SCROLLED_WINDOW(item)) 8556 if(GTK_IS_SCROLLED_WINDOW(item))
8469 { 8557 {
8558 gint scrolledwidth = 0, scrolledheight = 0;
8559
8560 /* Pre-run the calculation code for MLE/Container/Tree if needed */
8561 if((width < 1 && !hsize) || (height < 1 && !vsize))
8562 _get_scrolled_size(item, &scrolledwidth, &scrolledheight);
8563
8470 if(width > 0) 8564 if(width > 0)
8471 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(item), width); 8565 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(item), width);
8472 else if(!hsize) 8566 else if(!hsize)
8473 { 8567 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(item), scrolledwidth);
8474 /* If we aren't expandable set the minimum width */
8475 gint thiswidth = 0;
8476 GtkWidget *widget = g_object_get_data(G_OBJECT(item), "_dw_user");
8477
8478 if(widget)
8479 {
8480 if(g_object_get_data(G_OBJECT(widget), "_dw_tree_type") == GINT_TO_POINTER(_DW_TREE_TYPE_TREE))
8481 thiswidth = _DW_SCROLLED_MAX_WIDTH;
8482 else
8483 gtk_widget_get_preferred_width(GTK_WIDGET(widget), NULL, &thiswidth);
8484 }
8485 if(thiswidth < _DW_SCROLLED_MIN_WIDTH)
8486 thiswidth = _DW_SCROLLED_MIN_WIDTH;
8487 if(thiswidth > _DW_SCROLLED_MAX_WIDTH)
8488 thiswidth = _DW_SCROLLED_MAX_WIDTH;
8489 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(item), thiswidth);
8490 }
8491 if(height > 0) 8568 if(height > 0)
8492 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(item), height); 8569 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(item), height);
8493 else if(!vsize) 8570 else if(!vsize)
8494 { 8571 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(item), scrolledheight);
8495 /* If we aren't expandable set the minimum height */
8496 gint thisheight = 0;
8497 GtkWidget *widget = g_object_get_data(G_OBJECT(item), "_dw_user");
8498
8499 if(widget)
8500 {
8501 if(g_object_get_data(G_OBJECT(widget), "_dw_tree_type") == GINT_TO_POINTER(_DW_TREE_TYPE_TREE))
8502 thisheight = _DW_SCROLLED_MAX_HEIGHT;
8503 else
8504 gtk_widget_get_preferred_height(GTK_WIDGET(widget), NULL, &thisheight);
8505 }
8506 if(thisheight < _DW_SCROLLED_MIN_HEIGHT)
8507 thisheight = _DW_SCROLLED_MIN_HEIGHT;
8508 if(thisheight > _DW_SCROLLED_MAX_HEIGHT)
8509 thisheight = _DW_SCROLLED_MAX_HEIGHT;
8510 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(item), thisheight);
8511 }
8512 } 8572 }
8513 else 8573 else
8514 { 8574 {
8515 /* Set the requested size of the widget */ 8575 /* Set the requested size of the widget */
8516 if(width == -1 && (GTK_IS_COMBO_BOX(item) || GTK_IS_ENTRY(item))) 8576 if(width == -1 && (GTK_IS_COMBO_BOX(item) || GTK_IS_ENTRY(item)))
8763 void API dw_window_get_preferred_size(HWND handle, int *width, int *height) 8823 void API dw_window_get_preferred_size(HWND handle, int *width, int *height)
8764 { 8824 {
8765 int _locked_by_me = FALSE; 8825 int _locked_by_me = FALSE;
8766 8826
8767 DW_MUTEX_LOCK; 8827 DW_MUTEX_LOCK;
8768 if(width) 8828 if(GTK_IS_SCROLLED_WINDOW(handle))
8769 gtk_widget_get_preferred_width(handle, NULL, width); 8829 {
8770 if(height) 8830 gint scrolledwidth, scrolledheight;
8771 gtk_widget_get_preferred_height(handle, NULL, height); 8831
8832 _get_scrolled_size(handle, &scrolledwidth, &scrolledheight);
8833
8834 if(width)
8835 *width = scrolledwidth;
8836 if(height)
8837 *height = scrolledheight;
8838 }
8839 else
8840 {
8841 if(width)
8842 gtk_widget_get_preferred_width(handle, NULL, width);
8843 if(height)
8844 gtk_widget_get_preferred_height(handle, NULL, height);
8845 }
8772 DW_MUTEX_UNLOCK; 8846 DW_MUTEX_UNLOCK;
8773 } 8847 }
8774 8848
8775 /* 8849 /*
8776 * Returns the width of the screen. 8850 * Returns the width of the screen.