Debian Codegraph @

src/af/xap/gtk/xap_UnixDialogHelper.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Program Utilities
4 * Copyright (C) 1998-2000 AbiSource, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA.
20 */
21
22 /*
23 * Port to Maemo Development Platform
24 * Author: INdT - Renato Araujo <[email protected]>
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35
36 #include <string>
37
38 #include <glib.h>
39 #include <gtk/gtk.h>
40 #include <gdk/gdk.h>
41 #include <gdk/gdkkeysyms.h>
42 #include <goffice/gtk/goffice-gtk.h>
43
44 #include "ut_debugmsg.h"
45 #include "ut_assert.h"
46 #include "ut_string.h"
47 #include "ut_std_string.h"
48 #include "xav_View.h"
49 #include "xap_Frame.h"
50 #include "xap_UnixFrameImpl.h"
51 #include "xap_App.h"
52 #include "xap_UnixDialogHelper.h"
53 #include "xap_Dialog.h"
54 #include "xap_Gtk2Compat.h"
55
56 /*****************************************************************/
57 /*****************************************************************/
58
59 static gboolean focus_in_event(GtkWidget *widget,GdkEvent */*event*/,gpointer /*user_data*/)
60 {
61 XAP_Frame *pFrame=static_cast<XAP_Frame *>(g_object_get_data(G_OBJECT(widget), "frame"));
62 if (pFrame && pFrame->getCurrentView())
63 pFrame->getCurrentView()->focusChange(AV_FOCUS_NEARBY);
64 return FALSE;
65 }
66
67 static gboolean destroy_event(GtkWidget * /*widget*/ ,GdkEvent */*event*/,gpointer /*user_data*/)
68 {
69 return FALSE;
70 }
71
72 static gboolean focus_out_event(GtkWidget *widget,GdkEvent */*event*/,gpointer /*user_data*/)
73 {
74 XAP_Frame *pFrame=static_cast<XAP_Frame *>(g_object_get_data(G_OBJECT(widget), "frame"));
75 if(pFrame == NULL) return FALSE;
76 AV_View * pView = pFrame->getCurrentView();
77 if(pView!= NULL)
78 {
79 pView->focusChange(AV_FOCUS_NONE);
80 }
81 return FALSE;
82 }
83
84 static gboolean focus_out_event_Modeless(GtkWidget *widget,GdkEvent */*event*/,gpointer /*user_data*/)
85 {
86 XAP_App *pApp = static_cast<XAP_App *>(g_object_get_data(G_OBJECT(widget), "pApp"));
87 XAP_Frame *pFrame = pApp->getLastFocussedFrame();
88 if(pFrame ==static_cast<XAP_Frame *>(NULL))
89 {
90 UT_uint32 nframes = pApp->getFrameCount();
91 if(nframes > 0 && nframes < 10)
92 {
93 pFrame = pApp->getFrame(0);
94 }
95 else
96 {
97 return FALSE;
98 }
99 }
100 if(pFrame == static_cast<XAP_Frame *>(NULL)) return FALSE;
101 AV_View * pView = pFrame->getCurrentView();
102 UT_ASSERT_HARMLESS(pView);
103 if(pView!= NULL)
104 {
105 pView->focusChange(AV_FOCUS_NONE);
106 }
107 return FALSE;
108 }
109
110
111 static gboolean focus_in_event_Modeless(GtkWidget *widget,GdkEvent */*event*/,gpointer /*user_data*/)
112 {
113 XAP_App *pApp=static_cast<XAP_App *>(g_object_get_data(G_OBJECT(widget), "pApp"));
114 XAP_Frame *pFrame= pApp->getLastFocussedFrame();
115 if(pFrame ==static_cast<XAP_Frame *>(NULL))
116 {
117 UT_uint32 nframes = pApp->getFrameCount();
118 if(nframes > 0 && nframes < 10)
119 {
120 pFrame = pApp->getFrame(0);
121 }
122 else
123 {
124 return FALSE;
125 }
126 }
127 if(pFrame == static_cast<XAP_Frame *>(NULL)) return FALSE;
128 AV_View * pView = pFrame->getCurrentView();
129 if(pView!= NULL)
130 {
131 pView->focusChange(AV_FOCUS_MODELESS);
132 }
133 return FALSE;
134 }
135
136
137 static gboolean focus_in_event_ModelessOther(GtkWidget *widget,GdkEvent */*event*/,
138 std::pointer_to_unary_function<int, gboolean> *other_function)
139 {
140 XAP_App *pApp=static_cast<XAP_App *>(g_object_get_data(G_OBJECT(widget), "pApp"));
141 XAP_Frame *pFrame= pApp->getLastFocussedFrame();
142 if(pFrame == static_cast<XAP_Frame *>(NULL))
143 {
144 UT_uint32 nframes = pApp->getFrameCount();
145 if(nframes > 0 && nframes < 10)
146 {
147 pFrame = pApp->getFrame(0);
148 }
149 else
150 {
151 return FALSE;
152 }
153 }
154 if(pFrame == static_cast<XAP_Frame *>(NULL))
155 return FALSE;
156 AV_View * pView = pFrame->getCurrentView();
157 if(pView!= NULL)
158 {
159 pView->focusChange(AV_FOCUS_MODELESS);
160 (*other_function)(0);
161 }
162 return FALSE;
163 }
164
165 /*****************************************************************/
166
167 GtkBuilder * newDialogBuilder(const char * name)
168 {
169 UT_ASSERT(name);
170 std::string ui_path = static_cast<XAP_UnixApp*>(XAP_App::getApp())->getAbiSuiteAppUIDir() + "/" + name;
171
172 // load the dialog from the UI file
173 GtkBuilder* builder = gtk_builder_new();
174 GError * error = NULL;
175 guint result = gtk_builder_add_from_file(builder, ui_path.c_str(), &error);
176 if(result == 0) {
177 if(error) {
178 UT_DEBUGMSG(("gtk_builder_add() for %s failed with '%s'.",
179 ui_path.c_str(), error->message));
180 g_error_free(error);
181 }
182 g_object_unref((GObject*)builder);
183 return NULL;
184 }
185 return builder;
186 }
187
188
189 /*****************************************************************/
190
191 void connectFocus(GtkWidget *widget,const XAP_Frame *frame)
192 {
193 g_object_set_data(G_OBJECT(widget), "frame",
194 const_cast<void *>(static_cast<const void *>(frame)));
195 g_signal_connect(G_OBJECT(widget), "focus_in_event",
196 G_CALLBACK(focus_in_event), NULL);
197 g_signal_connect(G_OBJECT(widget), "focus_out_event",
198 G_CALLBACK(focus_out_event), NULL);
199 g_signal_connect(G_OBJECT(widget), "destroy",
200 G_CALLBACK(destroy_event), NULL);
201 }
202
203 void connectFocusModelessOther(GtkWidget *widget,const XAP_App * pApp,
204 std::pointer_to_unary_function<int, gboolean> *other_function)
205 {
206 g_object_set_data(G_OBJECT(widget), "pApp",
207 const_cast<void *>(static_cast<const void *>(pApp)));
208 g_signal_connect(G_OBJECT(widget), "focus_in_event",
209 G_CALLBACK(focus_in_event_ModelessOther),
210 (gpointer) other_function); // leave as C-style cast
211 g_signal_connect(G_OBJECT(widget), "focus_out_event",
212 G_CALLBACK(focus_out_event_Modeless), NULL);
213 g_signal_connect(G_OBJECT(widget), "destroy",
214 G_CALLBACK(focus_out_event_Modeless), NULL);
215 }
216
217
218 void connectFocusModeless(GtkWidget *widget,const XAP_App * pApp)
219 {
220 g_object_set_data(G_OBJECT(widget), "pApp",
221 const_cast<void *>(static_cast<const void *>(pApp)));
222 g_signal_connect(G_OBJECT(widget), "focus_in_event",
223 G_CALLBACK(focus_in_event_Modeless), NULL);
224 g_signal_connect(G_OBJECT(widget), "focus_out_event",
225 G_CALLBACK(focus_out_event_Modeless), NULL);
226 g_signal_connect(G_OBJECT(widget), "destroy",
227 G_CALLBACK(destroy_event), NULL);
228 }
229
230
231 bool isTransientWindow(GtkWindow *window,GtkWindow *parent)
232 {
233 GtkWindow *transient;
234 if(window)
235 {
236 while((transient=gtk_window_get_transient_for(window)))
237 {
238 window=transient;
239 if(window==parent)
240 return true;
241 }
242 }
243 return false;
244 }
245
246 /****************************************************************/
247 /****************************************************************/
248
249 // in ap_editmethods.cpp
250 extern bool helpLocalizeAndOpenURL(const char* pathBeforeLang, const char* pathAfterLang, const char *remoteURLbase);
251
252 static void sDoHelp ( XAP_Dialog * pDlg )
253 {
254 // should always be valid, but just in case...
255 if (!pDlg)
256 return;
257
258 // open the url
259 if ( pDlg->getHelpUrl().size () > 0 )
260 {
261 helpLocalizeAndOpenURL ("help", pDlg->getHelpUrl().c_str(), NULL );
262 }
263 else
264 {
265 // TODO: warn no help on this topic
266 UT_DEBUGMSG(("NO HELP FOR THIS TOPIC!!\n"));
267 }
268 }
269
270 /*!
271 * Catch F1 keypress over a dialog and open up the help file, if any
272 */
273 static gint modal_keypress_cb ( GtkWidget * /*wid*/, GdkEventKey * event,
274 XAP_Dialog * pDlg )
275 {
276 // propegate keypress up if not F1
277 if ( event->keyval == GDK_KEY_F1 || event->keyval == GDK_KEY_Help )
278 {
279 sDoHelp( pDlg ) ;
280
281 // stop F1 propegation
282 return TRUE ;
283 }
284
285 return FALSE ;
286 }
287
288 /*!
289 * Catch F1 keypress over a dialog and open up the help file, if any
290 */
291 static gint nonmodal_keypress_cb ( GtkWidget * /*wid*/, GdkEventKey * event,
292 XAP_Dialog * pDlg )
293 {
294 // propegate keypress up if not F1
295 if ( event->keyval == GDK_KEY_F1 || event->keyval == GDK_KEY_Help )
296 {
297 sDoHelp( pDlg ) ;
298 return TRUE ;
299 }
300
301 return FALSE ;
302 }
303
304 #if !defined(EMBEDDED_TARGET) || EMBEDDED_TARGET != EMBEDDED_TARGET_HILDON
305 static void help_button_cb (GObject * /*button*/, XAP_Dialog * pDlg)
306 {
307 if (pDlg)
308 sDoHelp (pDlg);
309 }
310 #endif
311
312 static void sAddHelpButton (GtkDialog * me, XAP_Dialog * pDlg)
313 {
314 #if defined(EMBEDDED_TARGET) && EMBEDDED_TARGET == EMBEDDED_TARGET_HILDON
315 UT_UNUSED(me);
316 UT_UNUSED(pDlg);
317 #else
318 // prevent help button from being added twice
319 gint has_button = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (me), "has-help-button"));
320
321 if (has_button)
322 return;
323
324 if (pDlg && pDlg->getHelpUrl().size () > 0) {
325
326 #ifdef HAVE_SDI
327 GtkWidget * image = gtk_image_new_from_stock(GTK_STOCK_HELP, GTK_ICON_SIZE_BUTTON);
328 GtkWidget * button = gtk_button_new();
329 gtk_container_add(GTK_CONTAINER(button), image);
330 GtkWidget * alignment = gtk_alignment_new (0, 0.5, 0, 0);
331 gtk_container_add (GTK_CONTAINER(alignment), button);
332 #else
333 GtkWidget * alignment = gtk_button_new_from_stock (GTK_STOCK_HELP);
334 GtkWidget * button = alignment;
335 #endif
336 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_action_area(me)),
337 alignment, FALSE, FALSE, 0);
338 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX(gtk_dialog_get_action_area(me)),
339 alignment, TRUE);
340 g_signal_connect (G_OBJECT (button), "clicked",
341 G_CALLBACK(help_button_cb), pDlg);
342 gtk_widget_show_all (alignment);
343
344 g_object_set_data (G_OBJECT (me), "has-help-button", GINT_TO_POINTER (1));
345 }
346 #endif
347 }
348
349 /*!
350 * Centers a dialog, makes it transient, sets up the right window icon
351 */
352 void centerDialog(GtkWidget * parent, GtkWidget * child, bool set_transient_for)
353 {
354 UT_return_if_fail(parent);
355 UT_return_if_fail(child);
356
357 if (GTK_IS_DIALOG(child))
358 go_dialog_guess_alternative_button_order(GTK_DIALOG(child));
359 if(GTK_IS_WINDOW(parent) != TRUE)
360 parent = gtk_widget_get_parent(parent);
361 xxx_UT_DEBUGMSG(("center IS WIDGET_TOP_LEVL %d \n",(GTK_WIDGET_TOPLEVEL(parent))));
362 xxx_UT_DEBUGMSG(("center IS WIDGET WINDOW %d \n",(GTK_IS_WINDOW(parent))));
363 xxx_UT_DEBUGMSG(("center child IS WIDGET WINDOW %d \n",(GTK_IS_WINDOW(child))));
364 if (set_transient_for)
365 gtk_window_set_transient_for(GTK_WINDOW(child),
366 GTK_WINDOW(parent));
367
368 GdkPixbuf * icon = gtk_window_get_icon(GTK_WINDOW(parent));
369 if ( NULL != icon )
370 {
371 gtk_window_set_icon(GTK_WINDOW(child), icon);
372 }
373 }
374
375 void abiSetupModalDialog(GtkDialog * dialog, XAP_Frame *pFrame, XAP_Dialog * pDlg, gint defaultResponse)
376 {
377 GtkWidget *popup = GTK_WIDGET (dialog);
378 gtk_dialog_set_default_response (GTK_DIALOG (popup), defaultResponse);
379 gtk_window_set_modal (GTK_WINDOW(popup), TRUE);
380
381 // To center the dialog, we need the frame of its parent.
382 if (pFrame)
383 {
384 XAP_UnixFrameImpl * pUnixFrameImpl = static_cast<XAP_UnixFrameImpl *>(pFrame->getFrameImpl());
385 GtkWidget * parentWindow = pUnixFrameImpl->getTopLevelWindow();
386 if (GTK_IS_WINDOW(parentWindow) != TRUE)
387 parentWindow = gtk_widget_get_parent(parentWindow);
388 centerDialog (parentWindow, GTK_WIDGET(popup));
389 }
390 connectFocus (GTK_WIDGET(popup), pFrame);
391
392 // connect F1 to the help subsystem
393 g_signal_connect (G_OBJECT(popup), "key-press-event",
394 G_CALLBACK(modal_keypress_cb), pDlg);
395
396 // set the default response
397 sAddHelpButton (GTK_DIALOG (popup), pDlg);
398
399 // show the window
400 gtk_widget_show (GTK_WIDGET (popup));
401 }
402
403 gint abiRunModalDialog(GtkDialog * me, bool destroyDialog, AtkRole role)
404 {
405 atk_object_set_role (gtk_widget_get_accessible (GTK_WIDGET (me)), role);
406
407 // now run the dialog
408 gint result = gtk_dialog_run ( me ) ;
409
410 // destroy the dialog
411 if ( destroyDialog )
412 abiDestroyWidget ( GTK_WIDGET ( me ) );
413
414 return result ;
415 }
416
417 /*!
418 * Runs the dialog \me as a modal dialog
419 * 1) Connect focus to toplevel frame
420 * 2) Centers dialog over toplevel window
421 * 3) Connects F1 to help system
422 * 4) Makes dialog modal
423 * 5) Sets the default button to defaultResponse, sets ESC to close
424 * 6) Returns value of gtk_dialog_run(me)
425 * 7) If \destroyDialog is true, destroys the dialog, else you have to call abiDestroyWidget()
426 */
427 gint abiRunModalDialog(GtkDialog * me, XAP_Frame *pFrame, XAP_Dialog * pDlg,
428 gint defaultResponse, bool destroyDialog, AtkRole role)
429 {
430 abiSetupModalDialog(me, pFrame, pDlg, defaultResponse);
431 gint ret = abiRunModalDialog(me, destroyDialog, role);
432 if( pDlg )
433 {
434 pDlg->maybeReallowPopupPreviewBubbles();
435 }
436 return ret;
437 }
438
439 /*!
440 * Sets up the dialog \me as a modeless dialog
441 * 1) Connect focus to toplevel frame
442 * 2) Centers dialog over toplevel window
443 * 3) Makes the App remember this modeless dialog
444 * 4) Connects F1 to help system
445 * 5) Makes dialog non-modal (modeless)
446 *
447 6) Sets the default button to defaultResponse, sets ESC to close
448 */
449 void abiSetupModelessDialog(GtkDialog * me, XAP_Frame * pFrame, XAP_Dialog * pDlg,
450 gint defaultResponse, bool abi_modeless, AtkRole /*role*/ )
451 {
452 if (abi_modeless)
453 {
454 // remember the modeless id
455 XAP_App::getApp()->rememberModelessId( pDlg->getDialogId(), static_cast<XAP_Dialog_Modeless *>(pDlg));
456
457 // connect focus to our parent frame
458 connectFocusModeless(GTK_WIDGET(me), XAP_App::getApp());
459 }
460
461 // To center the dialog, we need the frame of its parent.
462 if (pFrame)
463 {
464 XAP_UnixFrameImpl * pUnixFrameImpl = static_cast<XAP_UnixFrameImpl *>(pFrame->getFrameImpl());
465 GtkWidget * parentWindow = gtk_widget_get_toplevel (pUnixFrameImpl->getTopLevelWindow());
466 centerDialog ( parentWindow, GTK_WIDGET(me), false ) ;
467 }
468
469 // connect F1 to the help subsystem
470 g_signal_connect (G_OBJECT(me), "key-press-event",
471 G_CALLBACK(nonmodal_keypress_cb), pDlg);
472
473 // set the default response
474 gtk_dialog_set_default_response ( me, defaultResponse ) ;
475 sAddHelpButton (me, pDlg);
476
477 // and mark it as modeless
478 gtk_window_set_modal ( GTK_WINDOW(me), FALSE ) ;
479 // FIXME: shouldn't we pass role here?
480 atk_object_set_role (gtk_widget_get_accessible (GTK_WIDGET (me)), ATK_ROLE_ALERT);
481
482 pDlg->maybeClosePopupPreviewBubbles();
483
484 // show the window
485 gtk_widget_show ( GTK_WIDGET(me) ) ;
486 }
487
488 /*!
489 * Create a new GtkDialog
490 */
491 GtkWidget * abiDialogNew(const char * role, gboolean resizable)
492 {
493 GtkWidget * dlg = gtk_dialog_new () ;
494 if ( role )
495 gtk_window_set_role ( GTK_WINDOW(dlg), role ) ;
496 gtk_window_set_resizable ( GTK_WINDOW(dlg), resizable ) ;
497 gtk_container_set_border_width ( GTK_CONTAINER (dlg), 5 ) ;
498 gtk_box_set_spacing ( GTK_BOX ( gtk_dialog_get_content_area(GTK_DIALOG (dlg))), 2 ) ;
499 return dlg ;
500 }
501
502 /*!
503 * Create a new GtkDialog with this title
504 */
505 GtkWidget * abiDialogNew(const char * role, gboolean resizable, const char * title, ...)
506 {
507 GtkWidget * dlg = abiDialogNew(role, resizable);
508
509 if ( title != NULL && strlen ( title ) )
510 {
511 UT_String titleStr ( "" ) ;
512
513 va_list args;
514 va_start (args, title);
515 UT_String_vprintf (titleStr, title, args);
516 va_end (args);
517
518 // create the title
519 gtk_window_set_title ( GTK_WINDOW(dlg), titleStr.c_str() ) ;
520 }
521
522 return dlg ;
523 }
524
525 /*!
526 * Returns a GtkMenu with items having label fetched from UTF-8 CStr
527 * from UT_Vector.
528 * All menu item will have the index of the item stored in its user-data
529 */
530 GtkWidget * abiGtkMenuFromCStrVector(const UT_GenericVector<const char*> & vec, GCallback cb, gpointer data)
531 {
532 UT_sint32 i;
533 GtkWidget * menu = gtk_menu_new();
534
535 for (i = 0; i < vec.getItemCount(); i++) {
536 GtkWidget * menuItem = gtk_menu_item_new_with_label(vec[i]);
537 g_object_set_data(G_OBJECT(menuItem), "user_data", reinterpret_cast<void*>(i));
538 g_signal_connect(G_OBJECT(menuItem), "activate", G_CALLBACK(cb), data);
539 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuItem);
540 }
541 gtk_widget_show_all(menu);
542 return menu;
543 }
544
545
546 /*!
547 * Set the title of a gtk dialog
548 */
549 void abiDialogSetTitle(GtkWidget * dlg, const char * title, ...)
550 {
551 if ( title != NULL && strlen ( title ) )
552 {
553 UT_String titleStr ( "" ) ;
554
555 va_list args;
556 va_start (args, title);
557 UT_String_vprintf (titleStr, title, args);
558 va_end (args);
559
560 // create the title
561 gtk_window_set_title ( GTK_WINDOW(dlg), titleStr.c_str() ) ;
562 }
563 }
564
565
566 /*!
567 * Add this stock button to the dialog and make it sensitive
568 */
569 GtkWidget * abiAddStockButton (GtkDialog * me, const gchar * btn_id,
570 gint response_id)
571 {
572 UT_return_val_if_fail(me, NULL);
573 UT_return_val_if_fail(btn_id, NULL);
574
575 GtkWidget * wid = gtk_dialog_add_button(me, btn_id, response_id);
576 gtk_dialog_set_response_sensitive(me, response_id, TRUE);
577
578 return wid;
579 }
580
581 /*!
582 * Add this locale-sensitive button to the dialog and
583 * make it sensitive
584 */
585 GtkWidget* abiAddButton(GtkDialog * me, const gchar * btn_id,
586 gint response_id)
587 {
588 UT_return_val_if_fail(me, NULL);
589 UT_return_val_if_fail(btn_id, NULL);
590
591 // todo: possibly make me locale sensitive -> UTF-8
592
593 GtkWidget * wid = gtk_dialog_add_button(me, btn_id, response_id);
594 gtk_dialog_set_response_sensitive(me, response_id, TRUE);
595
596 return wid ;
597 }
598
599 /*!
600 * Calls gtk_widget_destroy on \me if \me is non-null
601 * and GTK_IS_WIDGET(me)
602 */
603 void abiDestroyWidget(GtkWidget * me)
604 {
605 if(me && GTK_IS_WIDGET(me))
606 gtk_widget_destroy(me);
607 }
608
609 /*!
610 * Localizes a label given the string id
611 */
612 void localizeLabel(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
613 {
614 gchar * unixstr = NULL; // used for conversions
615 std::string s;
616 pSS->getValueUTF8(id,s);
617 UT_XML_cloneNoAmpersands(unixstr, s.c_str());
618 gtk_label_set_text (GTK_LABEL(widget), unixstr);
619 FREEP(unixstr);
620 }
621
622 void convertMnemonics(gchar * s)
623 {
624 UT_return_if_fail(s);
625
626 for (UT_uint32 i = 0; s[i] != 0; i++)
627 {
628 if ( s[i] == '&' ) {
629 if (i > 0 && s[i-1] == '\\')
630 {
631 s[i-1] = '&';
632 strcpy( &s[i], &s[i+1]);
633 i--;
634 }
635 else
636 s[i] = '_';
637 }
638 }
639 }
640
641
642 // probably much slower....
643 std::string & convertMnemonics(std::string & s)
644 {
645 for (UT_uint32 i = 0; s[i] != 0; i++)
646 {
647 if ( s[i] == '&' ) {
648 if (i > 0 && s[i-1] == '\\')
649 {
650 s[i-1] = '&';
651 s.erase(i);
652 i--;
653 }
654 else
655 s[i] = '_';
656 }
657 }
658
659 return s;
660 }
661
662 /*!
663 * Localizes the label of a widget given the string id
664 * Ampersands will be converted to underscores/mnemonics
665 */
666 void localizeLabelUnderline(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
667 {
668 std::string s;
669 pSS->getValueUTF8(id,s);
670 gchar * newlbl = g_strdup(s.c_str());
671 UT_ASSERT(newlbl);
672 convertMnemonics(newlbl);
673 gtk_label_set_text_with_mnemonic (GTK_LABEL(widget), newlbl);
674 FREEP(newlbl);
675 }
676
677 /*!
678 * Localizes the label of a widget given the string id
679 * It formats the label using the current label of the widget as a format
680 * string. The current label is assumed to be something like
681 * "<span size="larger">%s</span>".
682 */
683 void localizeLabelMarkup(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
684 {
685 gchar * unixstr = NULL; // used for conversions
686 std::string s;
687 pSS->getValueUTF8(id,s);
688 UT_XML_cloneNoAmpersands(unixstr, s.c_str());
689 std::string markupStr = UT_std_string_sprintf(gtk_label_get_label (GTK_LABEL(widget)), unixstr);
690 gtk_label_set_markup (GTK_LABEL(widget), markupStr.c_str());
691 FREEP(unixstr);
692 }
693
694 /*!
695 * Localizes a button given the string id
696 */
697 void localizeButton(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
698 {
699 gchar * unixstr = NULL; // used for conversions
700 std::string s;
701 pSS->getValueUTF8(id,s);
702 UT_XML_cloneNoAmpersands(unixstr, s.c_str());
703 gtk_button_set_label (GTK_BUTTON(widget), unixstr);
704 FREEP(unixstr);
705 }
706
707 /*!
708 * Localizes a button given the string id
709 * Ampersands will be converted to underscores/mnemonics
710 */
711 void localizeButtonUnderline(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
712 {
713 std::string s;
714 pSS->getValueUTF8(id,s);
715 gchar * newlbl = g_strdup(s.c_str());
716 UT_ASSERT(newlbl);
717 convertMnemonics(newlbl);
718 gtk_button_set_use_underline (GTK_BUTTON(widget), TRUE);
719 gtk_button_set_label (GTK_BUTTON(widget), newlbl);
720 FREEP(newlbl);
721 }
722
723 /*!
724 * Localizes a button given the string id
725 * It formats its label using the current button label as a format
726 * string. It is assumed to be something like
727 * "<span size="larger">%s</span>".
728 * Note that in addition to doing markup, ampersands will be converted
729 * to underscores/mnemonic since this makes sense for buttons
730 */
731 void localizeButtonMarkup(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
732 {
733 std::string s;
734 pSS->getValueUTF8(id,s);
735 gchar * newlbl = g_strdup(s.c_str());
736 UT_ASSERT(newlbl);
737 convertMnemonics(newlbl);
738 std::string markupStr = UT_std_string_sprintf(gtk_button_get_label (GTK_BUTTON(widget)), newlbl);
739 gtk_button_set_use_underline (GTK_BUTTON(widget), TRUE);
740 gtk_button_set_label (GTK_BUTTON(widget), markupStr.c_str());
741
742 // by default, they don't like markup, so we teach them
743 GtkWidget * button_child = gtk_bin_get_child (GTK_BIN(widget));
744 if (GTK_IS_LABEL (button_child))
745 gtk_label_set_use_markup (GTK_LABEL(button_child), TRUE);
746
747 FREEP(newlbl);
748 }
749 /*!
750 * Localizes the label of a Menu widget given the string id
751 */
752 void localizeMenu(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
753 {
754 // UT_ASSERT(GTK_IS_MENU(widget));
755 gchar * unixstr = NULL; // used for conversions
756 std::string s;
757 pSS->getValueUTF8(id,s);
758 UT_XML_cloneNoAmpersands(unixstr, s.c_str());
759 gtk_menu_set_title (GTK_MENU(widget), unixstr);
760 FREEP(unixstr);
761 }
762
763 /*!
764 * Localizes the label of a Menu Item widget given the string id
765 */
766 void localizeMenuItem(GtkWidget * widget, const XAP_StringSet * pSS, XAP_String_Id id)
767 {
768 gchar *unixstr = NULL;
769 std::string s;
770 pSS->getValueUTF8(id, s);
771 UT_XML_cloneConvAmpersands(unixstr, s.c_str());
772 gtk_menu_item_set_label(GTK_MENU_ITEM(widget), unixstr);
773 FREEP(unixstr);
774 }
775
776 /*!
777 * Sets the label of "widget" to "str".
778 * It formats the label using the current label of the widget as a format string. The
779 * current label is assumed to be something like "<span size="larger">%s</span>".
780 */
781 void setLabelMarkup(GtkWidget * widget, const gchar * str)
782 {
783 std::string markupStr = UT_std_string_sprintf(gtk_label_get_label (GTK_LABEL(widget)), str);
784 gtk_label_set_markup (GTK_LABEL(widget), markupStr.c_str());
785 }
786
787 #if !GTK_CHECK_VERSION(3,0,0)
788 /*!
789 * For a parented/displayed widget, this will just return
790 * gtk_widget_ensure_style(w). For a non-displayed widgets,
791 * This will return a valid GtkStyle for that widget
792 */
793 GtkStyle *
794 get_ensured_style (GtkWidget * w)
795 {
796 GtkStyle * style = NULL;
797 GtkWidget * hidden_window = NULL;
798
799 if (w->parent == NULL)
800 {
801 hidden_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
802 gtk_container_add (GTK_CONTAINER (hidden_window), w);
803 }
804
805 gtk_widget_ensure_style (w);
806 gtk_widget_realize (w);
807
808 style = gtk_widget_get_style (w);
809 UT_ASSERT(style);
810
811 if (hidden_window)
812 {
813 // now we destroy the hidden window
814 gtk_container_remove (GTK_CONTAINER(hidden_window), w);
815 gtk_widget_destroy (hidden_window);
816 }
817
818 return style;
819 }
820 #endif
821
822 /*!
823 * Creates a new GdkDrawingArea with the proper colormap and visual
824 * This returns a single-buffered widget.
825 * DEPRECATED: please use gtk_drawing_area_new() directly and make your
826 * implementation work with double-buffering in new code!
827 */
828 GtkWidget *createDrawingArea ()
829 {
830 GtkWidget *area;
831
832 area = gtk_drawing_area_new ();
833 gtk_widget_set_double_buffered(area, FALSE);
834
835 return area;
836 }
837
838 /*!
839 * This is a small message box for startup warnings and/or
840 * errors. Please do NOT use this for normal system execution
841 * user messages; use the XAP_UnixDialog_MessageBox class for that.
842 * We can't use that here because there is no parent frame, etc.
843 */
844 void messageBoxOK(const char * message)
845 {
846 GtkWidget * msg = gtk_message_dialog_new ( NULL,
847 GTK_DIALOG_MODAL,
848 GTK_MESSAGE_INFO,
849 GTK_BUTTONS_OK,
850 "%s", message ) ;
851
852 gtk_window_set_title(GTK_WINDOW(msg), "AbiWord");
853 gtk_window_set_role(GTK_WINDOW(msg), "message dialog");
854
855 gtk_widget_show ( msg ) ;
856 gtk_dialog_run ( GTK_DIALOG(msg) ) ;
857 gtk_widget_destroy ( msg ) ;
858 }
859
860 /****************************************************************/
861 /****************************************************************/
862
863 GdkWindow * getRootWindow(GtkWidget * widget)
864 {
865 UT_return_val_if_fail(widget, NULL);
866 return gdk_get_default_root_window() ;
867 }
868
869
870 static void activate_button( GtkEntry * /*entry*/, gpointer user_data )
871 {
872 // UT_DEBUGMSG(("activate_button() ud:%x\n", user_data ));
873
874 GtkWidget* w = GTK_WIDGET(user_data);
875 gtk_widget_activate(w);
876 }
877
878 /**
879 * When the source widget gets the activate signal, sent activate to the button.
880 * This allows GtkEntry widgets to explicitly close the dialog with the OK button
881 * when the user presses return while leaving the default dialog action to be CANCEL.
882 */
883 void abiSetActivateOnWidgetToActivateButton( GtkWidget* source, GtkWidget* button )
884 {
885 g_signal_connect( G_OBJECT( source ), "activate",
886 G_CALLBACK(activate_button), button );
887 }
888