1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
|
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "guiutils.h"
#include "fprompt.h"
#include "images/icon_ok_20x20.xpm"
#include "images/icon_cancel_20x20.xpm"
#include "images/icon_browse_20x20.xpm"
/*
* Floating Prompt:
*/
typedef struct _FPromptData FPromptData;
struct _FPromptData {
GtkWidget *toplevel, /* Popup GtkWindow */
*main_hbox,
*label,
*entry,
*browse_btn,
*ok_btn,
*cancel_btn,
*shadow; /* Popup GtkWindow */
GdkPixmap *shadow_pm;
gint x, y, /* Geometry of toplevel */
width, height;
fprompt_shadow shadow_style;
gpointer client_data;
gchar *(*browse_cb)(
gpointer, /* Data */
const gchar * /* Value */
);
void (*apply_cb)(
gpointer, /* Data */
const gchar * /* Value */
);
void (*cancel_cb)(
gpointer /* Data */
);
};
#define FPROMPT_DATA(p) ((FPromptData *)(p))
static FPromptData *fprompt = NULL;
static gint FPromptEntryEventCB(
GtkWidget *widget, GdkEvent *event, gpointer data
);
static gint FPromptEventCB(
GtkWidget *widget, GdkEvent *event, gpointer data
);
static void FPromptBrowseCB(GtkWidget *widget, gpointer data);
static void FPromptOKCB(GtkWidget *widget, gpointer data);
static void FPromptCancelCB(GtkWidget *widget, gpointer data);
static gint FPromptShadowPaintCB(FPromptData *p);
static void FPromptShadowDraw(FPromptData *p);
static void FPromptShadowConfigure(
FPromptData *p, gint x, gint y, gint width, gint height
);
gint FPromptInit(void);
void FPromptSetTransientFor(GtkWidget *w);
gboolean FPromptIsQuery(void);
void FPromptBreakQuery(void);
void FPromptSetPosition(gint x, gint y);
void FPromptSetShadow(fprompt_shadow shadow);
gint FPromptMapQuery(
const gchar *label,
const gchar *value,
const gchar *tooltip_message,
fprompt_map_code map_code, /* One of FPROMPT_MAP_* */
gint width, gint height,
fprompt_flags flags, /* Any of FPROMPT_FLAG_* */
gpointer client_data,
gchar *(*browse_cb)(gpointer, const gchar *),
void (*apply_cb)(gpointer, const gchar *),
void (*cancel_cb)(gpointer)
);
void FPromptMap(void);
void FPromptUnmap(void);
void FPromptShutdown(void);
#define ATOI(s) (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s) (((s) != NULL) ? atol(s) : 0)
#define ATOF(s) (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s) (((s) != NULL) ? g_strdup(s) : NULL)
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h) (MIN(MAX((a),(l)),(h)))
#define STRLEN(s) (((s) != NULL) ? strlen(s) : 0)
#define STRISEMPTY(s) (((s) != NULL) ? (*(s) == '\0') : TRUE)
#define FPROMPT_SHADOW_OFFSET_X 5
#define FPROMPT_SHADOW_OFFSET_Y 5
/*
* Floating Prompt GtkEntry event signal callback.
*/
static gint FPromptEntryEventCB(
GtkWidget *widget, GdkEvent *event, gpointer data
)
{
gint status = FALSE;
gboolean press;
GdkEventKey *key;
GdkEventButton *button;
GtkWidget *w;
FPromptData *p = FPROMPT_DATA(data);
if((widget == NULL) || (event == NULL) || (p == NULL))
return(status);
if(!FPromptIsQuery())
return(status);
switch((gint)event->type)
{
case GDK_FOCUS_CHANGE:
/* Focus events are never sent */
break;
case GDK_KEY_PRESS:
case GDK_KEY_RELEASE:
key = (GdkEventKey *)event;
press = (key->type == GDK_KEY_PRESS) ? TRUE : FALSE;
switch(key->keyval)
{
case GDK_Escape:
if(press)
{
/* Break query on escape, then return immediately
* so that the widget is not grabbed any more
*/
FPromptBreakQuery();
status = TRUE;
return(status);
}
status = TRUE;
break;
case GDK_Return:
case GDK_KP_Enter:
case GDK_ISO_Enter:
case GDK_3270_Enter:
if(press)
{
/* Call ok callback and return immediately so that
* the widget is not grabbed any more
*/
FPromptOKCB(NULL, data);
status = TRUE;
return(status);
}
status = TRUE;
break;
}
break;
case GDK_BUTTON_PRESS:
button = (GdkEventButton *)event;
w = p->toplevel;
if(w != NULL)
{
gint x, y;
GdkModifierType mask;
GdkWindow *window = w->window;
/* Clicked outside of the toplevel window? Check if the
* event GdkWindow differs from the toplevel GdkWindow
*/
gdk_window_get_pointer(window, &x, &y, &mask);
if((x < 0) || (x >= p->width) ||
(y < 0) || (y >= p->height)
)
{
/* Since a button event occured outside of the
* Floating Prompt's window we need to break query
* and return immediately so that the widget is not
* grabbed any more
*/
FPromptBreakQuery();
status = TRUE;
return(status);
}
}
status = TRUE;
break;
case GDK_BUTTON_RELEASE:
button = (GdkEventButton *)event;
status = TRUE;
break;
}
/* Need to regrab the widget, this is so that the next event
* such as a "key_press_event" is sent to this widget
*/
w = p->entry;
if((w != NULL) && (w != gtk_grab_get_current()))
gtk_grab_add(w);
return(status);
}
/*
* Toplevel GtkWindow "delete_event" signal callback.
*/
static gint FPromptEventCB(
GtkWidget *widget, GdkEvent *event, gpointer data
)
{
gint status = FALSE;
GdkEventConfigure *configure;
FPromptData *p = FPROMPT_DATA(data);
if((widget == NULL) || (event == NULL) || (p == NULL))
return(status);
if(!FPromptIsQuery())
return(status);
switch((gint)event->type)
{
case GDK_DELETE:
FPromptCancelCB(widget, data);
status = TRUE;
break;
case GDK_CONFIGURE:
configure = (GdkEventConfigure *)event;
/* Update toplevel geometry */
p->x = configure->x;
p->y = configure->y;
p->width = configure->width;
p->height = configure->height;
/* Update shadow */
FPromptShadowConfigure(
p,
configure->x, configure->y,
configure->width, configure->height
);
status = TRUE;
break;
}
return(status);
}
/*
* Browse callback.
*/
static void FPromptBrowseCB(GtkWidget *widget, gpointer data)
{
FPromptData *p = FPROMPT_DATA(data);
if((widget == NULL) || (p == NULL))
return;
/* Browse callback set? */
if(p->browse_cb != NULL)
{
GtkWidget *w = p->entry;
/* Get current value */
gchar *v = STRDUP((w != NULL) ?
gtk_entry_get_text(GTK_ENTRY(w)) : NULL
);
/* Call browse callback and get a statically allocated
* return string
*/
const gchar *rtn_v = p->browse_cb(
p->client_data, /* Data */
v /* Value */
);
g_free(v);
/* Set new value obtained from the browse callback */
if((w != NULL) && (rtn_v != NULL))
gtk_entry_set_text(GTK_ENTRY(w), rtn_v);
}
}
/*
* OK callback.
*/
static void FPromptOKCB(GtkWidget *widget, gpointer data)
{
FPromptData *p = FPROMPT_DATA(data);
if(p == NULL)
return;
/* Apply callback set? */
if(p->apply_cb != NULL)
{
GtkWidget *w = p->entry;
gchar *v = STRDUP((w != NULL) ?
gtk_entry_get_text(GTK_ENTRY(w)) : NULL
);
if(v != NULL)
{
/* Call apply callback */
p->apply_cb(
p->client_data, /* Data */
v /* Value */
);
g_free(v);
}
p->apply_cb = NULL;
p->client_data = NULL;
}
/* Reset values */
p->client_data = NULL;
p->browse_cb = NULL;
p->apply_cb = NULL;
p->cancel_cb = NULL;
FPromptUnmap();
}
/*
* Cancel callback.
*/
static void FPromptCancelCB(GtkWidget *widget, gpointer data)
{
FPromptData *p = FPROMPT_DATA(data);
if(p == NULL)
return;
/* Cancel callback set? */
if(p->cancel_cb != NULL)
{
/* Call cancel callback */
p->cancel_cb(p->client_data);
}
/* Reset values */
p->client_data = NULL;
p->browse_cb = NULL;
p->apply_cb = NULL;
p->cancel_cb = NULL;
FPromptUnmap();
}
/*
* Floading Prompt Shadow paint signal callback.
*/
static gint FPromptShadowPaintCB(FPromptData *p)
{
FPromptShadowDraw(p);
return(FALSE);
}
/*
* Draws the Floading Prompt Shadow.
*/
static void FPromptShadowDraw(FPromptData *p)
{
gint width, height;
GdkPixmap *pixmap;
GdkWindow *window;
GtkStyle *style;
GtkWidget *w = (p != NULL) ? p->shadow : NULL;
if(w == NULL)
return;
window = w->window;
pixmap = p->shadow_pm;
if((window == NULL) || (pixmap == NULL))
return;
gdk_window_get_size(pixmap, &width, &height);
style = gtk_widget_get_style(w);
gdk_draw_pixmap(
window, style->white_gc, pixmap,
0, 0, 0, 0, width, height
);
}
/*
* Configures the Floating Prompt's Shadow to the new geometry.
*
* The shadow cast offset will be applied to the specified
* position.
*/
static void FPromptShadowConfigure(
FPromptData *p, gint x, gint y, gint width, gint height
)
{
GtkWidget *w = (p != NULL) ? p->shadow : NULL;
if(w == NULL)
return;
if((width <= 0) || (height <= 0))
return;
/* Update shadow geometry */
gtk_widget_set_uposition(
w,
x + FPROMPT_SHADOW_OFFSET_X,
y + FPROMPT_SHADOW_OFFSET_Y
);
gtk_widget_set_usize(
w, width, height
);
gtk_widget_queue_resize(w);
}
/*
* Initializes the Floating Prompt.
*/
gint FPromptInit(void)
{
const gint border_minor = 2;
GdkWindow *window;
GtkWidget *w, *parent;
FPromptData *p;
/* Already initialized */
if(fprompt != NULL)
return(0);
/* Allocate new Floating Prompt */
p = FPROMPT_DATA(g_malloc0(sizeof(FPromptData)));
if(p == NULL)
return(-3);
/* Reset values */
p->shadow_pm = NULL;
p->x = 0;
p->y = 0;
p->width = FPROMPT_DEF_WIDTH;
p->height = FPROMPT_DEF_HEIGHT;
p->shadow_style = FPROMPT_SHADOW_SHADED;
p->client_data = NULL;
p->browse_cb = NULL;
p->apply_cb = NULL;
p->cancel_cb = NULL;
/* Begin creating widgets */
/* Toplevel */
p->toplevel = w = gtk_window_new(GTK_WINDOW_POPUP);
gtk_window_set_policy(GTK_WINDOW(w), TRUE, TRUE, TRUE);
gtk_widget_set_uposition(w, p->x, p->y);
gtk_widget_set_usize(w, p->width, p->height);
gtk_widget_realize(w);
window = w->window;
if(window != NULL)
{
/* No decorations */
gdk_window_set_decorations(window, 0);
/* No functions */
gdk_window_set_functions(window, 0);
#if !defined(_WIN32)
gdk_window_set_override_redirect(window, TRUE);
#endif
}
gtk_widget_add_events(
w,
GDK_STRUCTURE_MASK
);
gtk_signal_connect(
GTK_OBJECT(w), "delete_event",
GTK_SIGNAL_FUNC(FPromptEventCB), p
);
gtk_signal_connect(
GTK_OBJECT(w), "configure_event",
GTK_SIGNAL_FUNC(FPromptEventCB), p
);
gtk_container_border_width(GTK_CONTAINER(w), 0);
parent = w;
/* Main outer frame */
w = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(w), GTK_SHADOW_OUT);
gtk_container_add(GTK_CONTAINER(parent), w);
gtk_widget_show(w);
parent = w;
/* Main hbox */
p->main_hbox = w = gtk_hbox_new(FALSE, border_minor);
gtk_container_add(GTK_CONTAINER(parent), w);
gtk_widget_show(w);
parent = w;
/* Label */
p->label = w = gtk_label_new("Value:");
gtk_label_set_justify(GTK_LABEL(w), GTK_JUSTIFY_RIGHT);
gtk_box_pack_start(GTK_BOX(parent), w, FALSE, FALSE, 0);
gtk_widget_show(w);
/* Entry */
p->entry = w = gtk_entry_new();
GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT | GTK_CAN_FOCUS);
/* When connecting signals it is important to connect certain
* signals after in order for the widget to to be grabbed
* again in the event callback function
*/
gtk_widget_add_events(
w,
GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK
);
gtk_signal_connect(
GTK_OBJECT(w), "key_press_event",
GTK_SIGNAL_FUNC(FPromptEntryEventCB), p
);
gtk_signal_connect(
GTK_OBJECT(w), "key_release_event",
GTK_SIGNAL_FUNC(FPromptEntryEventCB), p
);
gtk_signal_connect(
GTK_OBJECT(w), "button_press_event",
GTK_SIGNAL_FUNC(FPromptEntryEventCB), p
);
gtk_signal_connect_after(
GTK_OBJECT(w), "button_release_event",
GTK_SIGNAL_FUNC(FPromptEntryEventCB), p
);
gtk_signal_connect_after(
GTK_OBJECT(w), "motion_notify_event",
GTK_SIGNAL_FUNC(FPromptEventCB), p
);
gtk_signal_connect_after(
GTK_OBJECT(w), "enter_notify_event",
GTK_SIGNAL_FUNC(FPromptEventCB), p
);
gtk_signal_connect_after(
GTK_OBJECT(w), "leave_notify_event",
GTK_SIGNAL_FUNC(FPromptEventCB), p
);
gtk_box_pack_start(GTK_BOX(parent), w, TRUE, TRUE, 0);
gtk_widget_show(w);
/* Browse button */
p->browse_btn = w = GUIButtonPixmap(
(guint8 **)icon_browse_20x20_xpm
);
gtk_box_pack_start(GTK_BOX(parent), w, FALSE, FALSE, 0);
gtk_signal_connect(
GTK_OBJECT(w), "clicked",
GTK_SIGNAL_FUNC(FPromptBrowseCB), p
);
/* OK button */
p->ok_btn = w = (GtkWidget *)GUIButtonPixmap(
(guint8 **)icon_ok_20x20_xpm
);
gtk_box_pack_start(GTK_BOX(parent), w, FALSE, FALSE, 0);
gtk_signal_connect(
GTK_OBJECT(w), "clicked",
GTK_SIGNAL_FUNC(FPromptOKCB), p
);
/* Cancel Button */
p->cancel_btn = w = (GtkWidget *)GUIButtonPixmap(
(guint8 **)icon_cancel_20x20_xpm
);
gtk_box_pack_start(GTK_BOX(parent), w, FALSE, FALSE, 0);
gtk_signal_connect(
GTK_OBJECT(w), "clicked",
GTK_SIGNAL_FUNC(FPromptCancelCB), p
);
/* Shadow */
p->shadow = w = gtk_window_new(GTK_WINDOW_POPUP);
gtk_widget_set_uposition(w, p->x, p->y);
gtk_widget_set_usize(w, p->width, p->height);
gtk_widget_set_app_paintable(w, TRUE);
gtk_signal_connect_object(
GTK_OBJECT(w), "expose_event",
GTK_SIGNAL_FUNC(FPromptShadowPaintCB), (GtkObject *)p
);
gtk_signal_connect_object(
GTK_OBJECT(w), "draw",
GTK_SIGNAL_FUNC(FPromptShadowPaintCB), (GtkObject *)p
);
gtk_window_set_policy(
GTK_WINDOW(w), TRUE, TRUE, TRUE
);
gtk_widget_realize(w);
window = w->window;
if(window != NULL)
{
/* No decorations */
gdk_window_set_decorations(window, 0);
/* No functions */
gdk_window_set_functions(window, 0);
#if !defined(_WIN32)
gdk_window_set_override_redirect(window, TRUE);
#endif
}
fprompt = p;
return(0);
}
/*
* Sets Floating Prompt as a transient for the given window.
*
* This function has no affect since the Floating Prompt's
* GtkWindow is a GTK_WINDOW_POPUP and its entry grabs input,
* so there is no need to set it modal or transient for.
*/
void FPromptSetTransientFor(GtkWidget *w)
{
}
/*
* Returns TRUE if currently blocking for query.
*/
gboolean FPromptIsQuery(void)
{
FPromptData *p = fprompt;
GtkWidget *w = (p != NULL) ? p->toplevel : NULL;
return((w != NULL) ? GTK_WIDGET_MAPPED(w) : FALSE);
}
/*
* Ends query if any and returns a not available response.
*/
void FPromptBreakQuery(void)
{
FPromptData *p = fprompt;
if(p == NULL)
return;
/* If still in query then call the cancel callback to report
* cancel, reset values, and unmap the Floating Prompt
*/
if(FPromptIsQuery())
FPromptCancelCB(NULL, p);
/* Reset values */
p->client_data = NULL;
p->browse_cb = NULL;
p->apply_cb = NULL;
p->cancel_cb = NULL;
}
/*
* Moves the Floating Prompt to the specified coordinates the
* next time it is mapped.
*/
void FPromptSetPosition(gint x, gint y)
{
GtkWidget *toplevel, *shadow;
FPromptData *p = fprompt;
if(p == NULL)
return;
toplevel = p->toplevel;
shadow = p->shadow;
gtk_widget_set_uposition(toplevel, x, y);
gdk_window_move(toplevel->window, x, y);
gtk_widget_set_uposition(shadow, x, y);
gdk_window_move(shadow->window, x, y);
p->x = x;
p->y = y;
}
/*
* Sets the Floating Prompt's shadow style.
*/
void FPromptSetShadow(fprompt_shadow shadow)
{
FPromptData *p = fprompt;
if(p == NULL)
return;
p->shadow_style = shadow;
}
/*
* Maps the Floating Prompt and sets it up with the given values.
*
* Does not block.
*
* Returns 0 on success or non-zero on error.
*/
gint FPromptMapQuery(
const gchar *label, /* Hides label if NULL */
const gchar *value,
const gchar *tooltip_message,
fprompt_map_code map_code, /* One of FPROMPT_MAP_* */
gint width, gint height,
fprompt_flags flags, /* Any of FPROMPT_FLAG_* */
gpointer client_data,
gchar *(*browse_cb)(gpointer, const gchar *),
void (*apply_cb)(gpointer, const gchar *),
void (*cancel_cb)(gpointer)
)
{
GtkWidget *w, *toplevel, *shadow;
FPromptData *p = fprompt;
if(p == NULL)
return(-1);
toplevel = p->toplevel;
shadow = p->shadow;
/* Set new label */
w = p->label;
if(w != NULL)
{
if(label == NULL)
{
gtk_widget_hide(w);
}
else
{
gtk_label_set_text(GTK_LABEL(w), label);
gtk_widget_show(w);
}
}
/* Set new entry value */
w = p->entry;
if(w != NULL)
{
if(value != NULL)
{
gint len = STRLEN(value);
gtk_entry_set_text(GTK_ENTRY(w), value);
if(flags & FPROMPT_FLAG_SELECT_VALUE)
gtk_entry_select_region(GTK_ENTRY(w), 0, len);
gtk_entry_set_position(GTK_ENTRY(w), len);
}
else
{
/* Leave value it was */
}
/* Set new tooltip message */
GUISetWidgetTip(w, tooltip_message);
}
/* Show/hide buttons */
/* Browse */
w = p->browse_btn;
if(w != NULL)
{
if(flags & FPROMPT_FLAG_BROWSE_BTN)
gtk_widget_show(w);
else
gtk_widget_hide(w);
}
/* OK */
w = p->ok_btn;
if(w != NULL)
{
if(flags & FPROMPT_FLAG_OK_BTN)
gtk_widget_show(w);
else
gtk_widget_hide(w);
}
/* Cancel */
w = p->cancel_btn;
if(w != NULL)
{
if(flags & FPROMPT_FLAG_CANCEL_BTN)
gtk_widget_show(w);
else
gtk_widget_hide(w);
}
/* Update toplevel & shadow */
if((toplevel != NULL) && (shadow != NULL))
{
gint x = 0, y = 0, lwidth, lheight;
GdkWindow *window,
*root = (toplevel->window != NULL) ?
gdk_window_get_parent(toplevel->window) : NULL;
/* Resize of toplevel & shadow */
p->width = (width > 0) ? width : FPROMPT_DEF_WIDTH;
p->height = (height > 0) ? height : FPROMPT_DEF_HEIGHT;
gtk_widget_set_usize(toplevel, width, height);
gdk_window_get_size(toplevel->window, &lwidth, &lheight);
gdk_window_resize(
toplevel->window,
(width > 0) ? width : lwidth,
(height > 0) ? height : lheight
);
gtk_widget_set_usize(shadow, width, height);
gdk_window_resize(shadow->window, 5, 5);
/* Move the toplevel's GdkWindow, calculate x and y
* position relative to parent (root) GdkWindow by the
* specified map code
*/
switch(map_code)
{
case FPROMPT_MAP_TO_POINTER_WINDOW:
window = gdk_window_at_pointer(&x, &y);
if(window != NULL)
{
gint wwidth, wheight;
gdk_window_get_root_position(window, &x, &y);
gdk_window_get_size(window, &wwidth, &wheight);
x += (wwidth / 2) - (p->width / 2);
y += (wheight / 2) - (p->height / 2);
gtk_widget_set_uposition(toplevel, x, y);
gdk_window_move(toplevel->window, x, y);
gtk_widget_set_uposition(shadow, x, y);
gdk_window_move(shadow->window, x, y);
}
break;
case FPROMPT_MAP_TO_POINTER:
if(root != NULL)
{
gdk_window_get_pointer(root, &x, &y, 0);
x -= (p->width / 2);
y -= (p->height / 2);
}
gtk_widget_set_uposition(toplevel, x, y);
gdk_window_move(toplevel->window, x, y);
gtk_widget_set_uposition(shadow, x, y);
gdk_window_move(shadow->window, x, y);
break;
case FPROMPT_MAP_NO_MOVE:
break;
}
/* Create shadow pixmap */
if(root != NULL)
{
GtkWidget *w = p->shadow;
GdkWindow *window = w->window;
GdkPixmap *pixmap = GDK_PIXMAP_NEW(p->width, p->height);
GtkStyle *style = gtk_widget_get_style(w);
GDK_PIXMAP_UNREF(p->shadow_pm);
p->shadow_pm = pixmap;
if(pixmap != NULL)
{
gint mask_width = 8, mask_height = 2;
guint8 bm_data_8x2[] = { 0x55, 0xaa };
GdkColor *c, shadow_color;
GdkColormap *colormap = gdk_window_get_colormap(window);
GdkGC *gc;
GdkBitmap *mask;
switch(p->shadow_style)
{
case FPROMPT_SHADOW_NONE:
break;
case FPROMPT_SHADOW_DITHERED:
gc = GDK_GC_NEW();
gdk_gc_set_subwindow(gc, GDK_INCLUDE_INFERIORS);
gdk_window_copy_area(
pixmap, gc,
0, 0,
root,
p->x + FPROMPT_SHADOW_OFFSET_X,
p->y + FPROMPT_SHADOW_OFFSET_Y,
p->width, p->height
);
gdk_gc_set_subwindow(gc, GDK_CLIP_BY_CHILDREN);
mask = gdk_bitmap_create_from_data(
root, (const gchar *)bm_data_8x2,
mask_width, mask_height
);
if(mask != NULL)
{
gint x, y;
gdk_gc_set_clip_mask(gc, mask);
gdk_gc_set_foreground(gc, &style->black);
for(y = 0; y < p->height; y += mask_height)
{
for(x = 0; x < p->width; x += mask_width)
{
gdk_gc_set_clip_origin(gc, x, y);
gdk_draw_rectangle(
pixmap, gc, TRUE,
x, y, mask_width, mask_height
);
}
}
gdk_gc_set_clip_mask(gc, NULL);
}
GDK_GC_UNREF(gc);
GDK_BITMAP_UNREF(mask);
break;
case FPROMPT_SHADOW_SOLID:
gdk_draw_rectangle(
pixmap, style->black_gc, TRUE,
0, 0, p->width, p->height
);
break;
case FPROMPT_SHADOW_SHADED:
gc = GDK_GC_NEW();
c = &shadow_color;
c->red = 0x5fff;
c->green = 0x5fff;
c->blue = 0x5fff;
GDK_COLORMAP_ALLOC_COLOR(colormap, c);
gdk_gc_set_subwindow(gc, GDK_INCLUDE_INFERIORS);
gdk_window_copy_area(
pixmap, gc,
0, 0,
root,
p->x + FPROMPT_SHADOW_OFFSET_X,
p->y + FPROMPT_SHADOW_OFFSET_Y,
p->width, p->height
);
gdk_gc_set_subwindow(gc, GDK_CLIP_BY_CHILDREN);
gdk_gc_set_function(gc, GDK_AND);
gdk_gc_set_foreground(gc, c);
gdk_draw_rectangle(
pixmap, gc, TRUE,
0, 0, p->width, p->height
);
gdk_gc_set_function(gc, GDK_COPY);
GDK_GC_UNREF(gc);
GDK_COLORMAP_FREE_COLOR(colormap, c);
break;
}
}
}
}
/* Set callbacks */
p->client_data = client_data;
p->browse_cb = browse_cb;
p->apply_cb = apply_cb;
p->cancel_cb = cancel_cb;
/* Map Floating Prompt */
FPromptMap();
return(0);
}
/*
* Maps the Floating Prompt.
*/
void FPromptMap(void)
{
GtkWidget *w;
FPromptData *p = fprompt;
if(p == NULL)
return;
if(p->shadow_style != FPROMPT_SHADOW_NONE)
gtk_widget_show_raise(p->shadow);
gtk_widget_show_raise(p->toplevel);
w = p->entry;
if(w != NULL)
{
gtk_widget_grab_focus(w);
gtk_widget_grab_default(w);
if(w != gtk_grab_get_current())
gtk_grab_add(w);
GTK_WIDGET_SET_FLAGS(w, GTK_HAS_FOCUS);
}
}
/*
* Unmaps the Floating Prompt.
*/
void FPromptUnmap(void)
{
GtkWidget *w;
FPromptData *p = fprompt;
if(p == NULL)
return;
w = p->entry;
if((w != NULL) && (w == gtk_grab_get_current()))
gtk_grab_remove(w);
p->x = 0;
p->y = 0;
p->width = 0;
p->height = 0;
gtk_widget_hide(p->toplevel);
gtk_widget_hide(p->shadow);
GDK_PIXMAP_UNREF(p->shadow_pm);
p->shadow_pm = NULL;
}
/*
* Shuts down the Floating Prompt.
*/
void FPromptShutdown(void)
{
FPromptData *p = fprompt;
if(p == NULL)
return;
GTK_WIDGET_DESTROY(p->shadow);
GTK_WIDGET_DESTROY(p->cancel_btn);
GTK_WIDGET_DESTROY(p->ok_btn);
GTK_WIDGET_DESTROY(p->browse_btn);
GTK_WIDGET_DESTROY(p->entry);
GTK_WIDGET_DESTROY(p->label);
GTK_WIDGET_DESTROY(p->main_hbox);
GTK_WIDGET_DESTROY(p->toplevel);
GDK_PIXMAP_UNREF(p->shadow_pm);
fprompt = NULL;
g_free(p);
}
|