~ubuntu-branches/ubuntu/natty/geany/natty

« back to all changes in this revision

Viewing changes to src/ui_utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-08-07 03:23:12 UTC
  • mfrom: (1.4.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: james.westby@ubuntu.com-20100807032312-ot70ac9d50cn79we
Tags: upstream-0.19
ImportĀ upstreamĀ versionĀ 0.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *      along with this program; if not, write to the Free Software
19
19
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
20
 *
21
 
 * $Id: ui_utils.c 4630 2010-01-31 21:54:47Z eht16 $
 
21
 * $Id: ui_utils.c 4942 2010-05-22 19:39:14Z eht16 $
22
22
 */
23
23
 
24
24
/** @file ui_utils.h
28
28
#include "geany.h"
29
29
 
30
30
#include <string.h>
 
31
#include <ctype.h>
 
32
#include <gdk/gdkkeysyms.h>
31
33
 
32
34
#include "ui_utils.h"
33
35
#include "prefs.h"
41
43
#include "callbacks.h"
42
44
#include "encodings.h"
43
45
#include "images.c"
44
 
#include "treeviews.h"
 
46
#include "sidebar.h"
45
47
#include "win32.h"
46
48
#include "project.h"
47
49
#include "editor.h"
70
72
        GtkWidget       *undo_items[3];
71
73
        GtkWidget       *save_buttons[4];
72
74
        GtkWidget       *config_files_menu;
 
75
        GtkWidget       *commands_menu;
 
76
        GtkWidget       *format_menu;
73
77
}
74
78
widgets;
75
79
 
132
136
}
133
137
 
134
138
 
135
 
/** Display text on the statusbar.
 
139
/** Displays text on the statusbar.
136
140
 * @param log Whether the message should be recorded in the Status window.
137
141
 * @param format A @c printf -style string. */
138
142
void ui_set_statusbar(gboolean log, const gchar *format, ...)
139
143
{
140
 
        gchar string[512];
 
144
        gchar *string;
141
145
        va_list args;
142
146
 
143
147
        va_start(args, format);
144
 
        g_vsnprintf(string, 512, format, args);
 
148
        string = g_strdup_vprintf(format, args);
145
149
        va_end(args);
146
150
 
147
151
        if (! prefs.suppress_status_messages)
149
153
 
150
154
        if (log || prefs.suppress_status_messages)
151
155
                msgwin_status_add("%s", string);
 
156
 
 
157
        g_free(string);
152
158
}
153
159
 
154
160
 
185
191
                        col = 0;
186
192
 
187
193
                /* Status bar statistics: col = column, sel = selection. */
188
 
                g_string_printf(stats_str, _("line: %d\t col: %d\t sel: %d\t "),
189
 
                        (line + 1), col,
 
194
                g_string_printf(stats_str, _("line: %d / %d\t col: %d\t sel: %d\t "),
 
195
                        (line + 1), sci_get_line_count(doc->editor->sci), col,
190
196
                        sci_get_selected_text_length(doc->editor->sci) - 1);
191
197
 
192
198
                g_string_append(stats_str,
646
652
        add_doc_widget("menu_select_all1");
647
653
        add_doc_widget("insert_date1");
648
654
        add_doc_widget("menu_format1");
 
655
        add_doc_widget("commands2");
649
656
        add_doc_widget("menu_open_selected_file1");
650
657
        add_doc_widget("page_setup1");
651
658
        add_doc_widget("find1");
677
684
        add_doc_toolitem("Color");
678
685
        add_doc_toolitem("Goto");
679
686
        add_doc_toolitem("GotoEntry");
 
687
        add_doc_toolitem("Replace");
 
688
        add_doc_toolitem("Print");
680
689
}
681
690
 
682
691
 
702
711
}
703
712
 
704
713
 
705
 
/** Add a widget to the list of widgets that should be set sensitive/insensitive
 
714
/** Adds a widget to the list of widgets that should be set sensitive/insensitive
706
715
 * when some documents are present/no documents are open.
707
716
 * It will be removed when the widget is destroyed.
708
717
 * @param widget The widget to add.
764
773
 
765
774
void ui_document_show_hide(GeanyDocument *doc)
766
775
{
767
 
        gchar *widget_name;
 
776
        const gchar *widget_name;
768
777
        GtkWidget *item;
769
778
        const GeanyIndentPrefs *iprefs;
770
779
 
870
879
/* Note: remember to unref the pixbuf once an image or window has added a reference. */
871
880
GdkPixbuf *ui_new_pixbuf_from_inline(gint img)
872
881
{
873
 
        switch(img)
 
882
        switch (img)
874
883
        {
875
884
                case GEANY_IMAGE_LOGO:
876
885
                        return gdk_pixbuf_new_from_inline(-1, aladin_inline, FALSE, NULL);
1109
1118
        item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1110
1119
        if (item != NULL)
1111
1120
                gtk_widget_destroy(GTK_WIDGET(item->data));
 
1121
        g_list_free(children);
1112
1122
 
1113
1123
        if (grf->toolbar != NULL)
1114
1124
        {
1116
1126
                item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1117
1127
                if (item != NULL)
1118
1128
                        gtk_widget_destroy(GTK_WIDGET(item->data));
 
1129
                g_list_free(children);
1119
1130
        }
1120
1131
        /* now prepend a new menuitem for the filename,
1121
1132
         * first for the recent files menu in the menu bar */
1158
1169
                        item = g_list_next(item);
1159
1170
                }
1160
1171
        }
 
1172
        g_list_free(children);
 
1173
 
1161
1174
        /* create item for the menu bar menu */
1162
1175
        tmp = gtk_menu_item_new_with_label(filename);
1163
1176
        gtk_widget_show(tmp);
1178
1191
                                item = g_list_next(item);
1179
1192
                        }
1180
1193
                }
 
1194
                g_list_free(children);
 
1195
 
1181
1196
                /* create item for the tool bar menu */
1182
1197
                tmp = gtk_menu_item_new_with_label(filename);
1183
1198
                gtk_widget_show(tmp);
1193
1208
        gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1194
1209
        GeanyDocument *doc;
1195
1210
 
1196
 
        for(i = 0; i < max; i++)
 
1211
        for (i = 0; i < max; i++)
1197
1212
        {
1198
1213
                doc = document_get_from_page(i);
1199
1214
 
1238
1253
GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1239
1254
{
1240
1255
        GtkWidget *label, *align;
1241
 
        GtkWidget *frame = gtk_frame_new (NULL);
1242
 
        gchar *label_markup;
1243
 
 
1244
 
        gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
1245
 
 
1246
 
        align = gtk_alignment_new (0.5, 0.5, 1, 1);
1247
 
        gtk_container_add (GTK_CONTAINER (frame), align);
1248
 
        gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, 12, 0);
1249
 
 
1250
 
        label_markup = g_strconcat("<b>", label_text, "</b>", NULL);
1251
 
        label = gtk_label_new (label_markup);
1252
 
        gtk_frame_set_label_widget (GTK_FRAME (frame), label);
1253
 
        gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1254
 
        g_free(label_markup);
 
1256
        GtkWidget *frame = gtk_frame_new(NULL);
 
1257
 
 
1258
        gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
 
1259
 
 
1260
        align = gtk_alignment_new(0.5, 0.5, 1, 1);
 
1261
        gtk_container_add(GTK_CONTAINER(frame), align);
 
1262
        gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
 
1263
 
 
1264
        label = ui_label_new_bold(label_text);
 
1265
        gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1255
1266
 
1256
1267
        *alignment = align;
1257
1268
        return frame;
1258
1269
}
1259
1270
 
1260
1271
 
1261
 
const gint BUTTON_BOX_BORDER = 5;
1262
 
 
1263
 
/** Convenience function for getting a fixed border for dialogs that doesn't
1264
 
 * increase the button box border.
 
1272
/** Makes a fixed border for dialogs without increasing the button box border.
1265
1273
 * @param dialog The parent container for the @c GtkVBox.
1266
1274
 * @return The packed @c GtkVBox. */
1267
1275
GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1268
1276
{
1269
1277
        GtkWidget *vbox = gtk_vbox_new(FALSE, 12);      /* need child vbox to set a separate border. */
1270
1278
 
1271
 
        gtk_container_set_border_width(GTK_CONTAINER(vbox), BUTTON_BOX_BORDER);
 
1279
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1272
1280
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1273
1281
        return vbox;
1274
1282
}
1275
1283
 
1276
1284
 
1277
 
/** Create a @c GtkButton with custom text and a stock image, aligned like
 
1285
/** Creates a @c GtkButton with custom text and a stock image similar to
1278
1286
 * @c gtk_button_new_from_stock().
1279
1287
 * @param stock_id A @c GTK_STOCK_NAME string.
1280
1288
 * @param text Button label text, can include mnemonics.
1282
1290
 */
1283
1291
GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1284
1292
{
1285
 
        GtkWidget *image, *label, *align, *hbox, *button;
 
1293
        GtkWidget *image, *button;
1286
1294
 
1287
 
        hbox = gtk_hbox_new(FALSE, 2);
 
1295
        button = gtk_button_new_with_mnemonic(text);
 
1296
        gtk_widget_show(button);
1288
1297
        image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1289
 
        label = gtk_label_new_with_mnemonic(text);
1290
 
        gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
1291
 
        gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1292
 
 
1293
 
        button = gtk_button_new();
1294
 
        align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
1295
 
        gtk_container_add(GTK_CONTAINER(align), hbox);
1296
 
        gtk_container_add(GTK_CONTAINER(button), align);
1297
 
        gtk_widget_show_all(align);
 
1298
        gtk_button_set_image(GTK_BUTTON(button), image);
 
1299
        /* note: image is shown by gtk */
1298
1300
        return button;
1299
1301
}
1300
1302
 
1301
1303
 
1302
 
/** Create a @c GtkImageMenuItem with a stock image and a custom label.
 
1304
/** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1303
1305
 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1304
1306
 * @param label Menu item label, can include mnemonics.
1305
1307
 * @return The new @c GtkImageMenuItem.
1324
1326
        if (event->button.button == 1 && icon_pos == 1)
1325
1327
        {
1326
1328
                gtk_entry_set_text(entry, "");
 
1329
                gtk_widget_grab_focus(GTK_WIDGET(entry));
1327
1330
        }
1328
1331
}
1329
1332
 
1330
1333
 
1331
 
/** Convenience function to add a small clear icon to the right end of the passed @a entry.
 
1334
/** Adds a small clear icon to the right end of the passed @a entry.
1332
1335
 *  A callback to clear the contents of the GtkEntry is automatically added.
1333
1336
 *
1334
1337
 *  This feature is only available with GTK 2.16 but implemented as a runtime check,
1339
1342
 *
1340
1343
 *  @since 0.16
1341
1344
 */
1342
 
void ui_entry_add_clear_icon(GtkWidget *entry)
 
1345
void ui_entry_add_clear_icon(GtkEntry *entry)
1343
1346
{
1344
1347
        if (gtk_check_version(2, 15, 2) == NULL)
1345
1348
        {
1445
1448
        gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_NORMAL, color);
1446
1449
        gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_ACTIVE, color);
1447
1450
 
1448
 
        treeviews_openfiles_update(doc);
 
1451
        sidebar_openfiles_update(doc);
1449
1452
}
1450
1453
 
1451
1454
 
1488
1491
        while (TRUE)
1489
1492
        {
1490
1493
                gtk_tree_selection_select_iter(treesel, &iter);
1491
 
                if (cb())
 
1494
                if (cb(0))
1492
1495
                        break;  /* found next message */
1493
1496
 
1494
1497
                if (! tree_model_iter_get_next(model, &iter, down))
1521
1524
}
1522
1525
 
1523
1526
 
1524
 
void ui_widget_modify_font_from_string(GtkWidget *wid, const gchar *str)
 
1527
/**
 
1528
 * Modifies the font of a widget using gtk_widget_modify_font().
 
1529
 *
 
1530
 * @param widget The widget.
 
1531
 * @param str The font name as expected by pango_font_description_from_string().
 
1532
 */
 
1533
void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1525
1534
{
1526
1535
        PangoFontDescription *pfd;
1527
1536
 
1528
1537
        pfd = pango_font_description_from_string(str);
1529
 
        gtk_widget_modify_font(wid, pfd);
 
1538
        gtk_widget_modify_font(widget, pfd);
1530
1539
        pango_font_description_free(pfd);
1531
1540
}
1532
1541
 
1610
1619
                if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1611
1620
                        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1612
1621
        }
 
1622
        else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
 
1623
        {
 
1624
                if (g_path_is_absolute(locale_path))
 
1625
                        gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
 
1626
        }
1613
1627
        g_free(locale_path);
1614
1628
 
1615
1629
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1634
1648
        GtkEntry *entry =
1635
1649
                (GtkEntry *) g_object_get_data(G_OBJECT(path_box), "entry");
1636
1650
        const gchar *title = g_object_get_data(G_OBJECT(path_box), "title");
1637
 
        gchar *utf8_path;
 
1651
        gchar *utf8_path = NULL;
1638
1652
 
1639
1653
        /* TODO: extend for other actions */
1640
 
        g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
 
1654
        g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
 
1655
                                         action == GTK_FILE_CHOOSER_ACTION_OPEN);
1641
1656
 
1642
1657
        if (title == NULL)
1643
1658
                title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
1644
1659
                        _("Select Folder") : _("Select File");
1645
1660
 
1646
 
#ifdef G_OS_WIN32
1647
 
        utf8_path = win32_show_project_folder_dialog(ui_widgets.prefs_dialog, title,
1648
 
                                                gtk_entry_get_text(GTK_ENTRY(entry)));
1649
 
#else
1650
 
        utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
1651
 
#endif
 
1661
        if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
 
1662
        {
 
1663
#ifdef G_OS_WIN32
 
1664
                utf8_path = win32_show_file_dialog(GTK_WINDOW(ui_widgets.prefs_dialog), title,
 
1665
                                                gtk_entry_get_text(GTK_ENTRY(entry)));
 
1666
#else
 
1667
                utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
 
1668
#endif
 
1669
        }
 
1670
        else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
 
1671
        {
 
1672
                gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
 
1673
#ifdef G_OS_WIN32
 
1674
                utf8_path = win32_show_folder_dialog(ui_widgets.prefs_dialog, title,
 
1675
                                                gtk_entry_get_text(GTK_ENTRY(entry)));
 
1676
#else
 
1677
                utf8_path = run_file_chooser(title, action, path);
 
1678
#endif
 
1679
                g_free(path);
 
1680
        }
1652
1681
 
1653
1682
        if (utf8_path != NULL)
1654
1683
        {
1671
1700
}
1672
1701
 
1673
1702
 
1674
 
/** Pack all @c GtkWidgets passed after the row argument into a table, using
 
1703
/** Packs all @c GtkWidgets passed after the row argument into a table, using
1675
1704
 * one widget per cell. The first widget is not expanded as the table grows,
1676
1705
 * as this is usually a label.
1677
1706
 * @param table
1707
1736
                document_open_file(file_name, FALSE, ft, NULL);
1708
1737
        else
1709
1738
        {
1710
 
                gchar *utf8 = utils_get_utf8_from_locale(file_name);
 
1739
                gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
1711
1740
                gchar *base_name = g_path_get_basename(file_name);
1712
1741
                gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
1713
1742
                gchar *global_content = NULL;
1717
1746
                if (g_file_test(global_file, G_FILE_TEST_EXISTS))
1718
1747
                        g_file_get_contents(global_file, &global_content, NULL, NULL);
1719
1748
 
1720
 
                document_new_file(utf8, ft, global_content);
1721
 
                utils_free_pointers(4, utf8, base_name, global_file, global_content, NULL);
 
1749
                document_new_file(utf8_filename, ft, global_content);
 
1750
 
 
1751
                utils_free_pointers(4, utf8_filename, base_name, global_file, global_content, NULL);
1722
1752
        }
1723
1753
}
1724
1754
 
1764
1794
 
1765
1795
        widgets.config_files_menu = menu = gtk_menu_new();
1766
1796
 
1767
 
        item = ui_image_menu_item_new(GTK_STOCK_FILE, _("C_onfiguration Files"));
1768
 
        gtk_widget_show(item);
 
1797
        item = ui_lookup_widget(main_widgets.window, "configuration_files1");
1769
1798
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
1770
 
        gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), item);
1771
1799
 
1772
1800
        /* sort menu after all items added */
1773
1801
        g_idle_add(sort_menu, widgets.config_files_menu);
1813
1841
}
1814
1842
 
1815
1843
 
 
1844
void ui_swap_sidebar_pos(void)
 
1845
{
 
1846
        GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
 
1847
        GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
 
1848
        GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
 
1849
        GtkWidget *box = ui_lookup_widget(main_widgets.window, "vbox1");
 
1850
 
 
1851
        /* reparenting avoids scintilla problem with middle click paste */
 
1852
        gtk_widget_reparent(left, box);
 
1853
        gtk_widget_reparent(right, box);
 
1854
        gtk_widget_reparent(right, pane);
 
1855
        gtk_widget_reparent(left, pane);
 
1856
 
 
1857
        gtk_paned_set_position(GTK_PANED(pane), pane->allocation.width
 
1858
                - gtk_paned_get_position(GTK_PANED(pane)));
 
1859
}
 
1860
 
 
1861
 
1816
1862
static void init_recent_files(void)
1817
1863
{
1818
1864
        GtkWidget *toolbar_recent_files_menu;
1831
1877
}
1832
1878
 
1833
1879
 
 
1880
static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
 
1881
{
 
1882
        g_object_ref(menu);
 
1883
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
 
1884
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
 
1885
        g_object_unref(menu);
 
1886
}
 
1887
 
 
1888
 
 
1889
static void on_editor_menu_show(GtkWidget *item)
 
1890
{
 
1891
        GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, "commands1");
 
1892
        GtkWidget *bar = ui_lookup_widget(main_widgets.window, "commands2");
 
1893
 
 
1894
        ui_menu_move(widgets.commands_menu, bar, popup);
 
1895
 
 
1896
        popup = ui_lookup_widget(main_widgets.editor_menu, "menu_format2");
 
1897
        bar = ui_lookup_widget(main_widgets.window, "menu_format1");
 
1898
        ui_menu_move(widgets.format_menu, bar, popup);
 
1899
}
 
1900
 
 
1901
 
 
1902
static void on_editor_menu_hide(GtkWidget *item)
 
1903
{
 
1904
        GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, "commands1");
 
1905
        GtkWidget *bar = ui_lookup_widget(main_widgets.window, "commands2");
 
1906
 
 
1907
        ui_menu_move(widgets.commands_menu, popup, bar);
 
1908
 
 
1909
        popup = ui_lookup_widget(main_widgets.editor_menu, "menu_format2");
 
1910
        bar = ui_lookup_widget(main_widgets.window, "menu_format1");
 
1911
        ui_menu_move(widgets.format_menu, popup, bar);
 
1912
}
 
1913
 
 
1914
 
1834
1915
void ui_init(void)
1835
1916
{
 
1917
        GtkWidget *item;
 
1918
 
1836
1919
        init_recent_files();
1837
1920
 
1838
1921
        ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
1859
1942
        widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
1860
1943
        widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
1861
1944
 
 
1945
        item = ui_lookup_widget(main_widgets.window, "menu_format1");
 
1946
        widgets.format_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item));
 
1947
        item = ui_lookup_widget(main_widgets.window, "commands2");
 
1948
        widgets.commands_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item));
 
1949
 
 
1950
        /* reparent edit submenus as needed */
 
1951
        item = main_widgets.editor_menu;
 
1952
        g_signal_connect(item, "show", G_CALLBACK(on_editor_menu_show), NULL);
 
1953
        g_signal_connect(item, "hide", G_CALLBACK(on_editor_menu_hide), NULL);
 
1954
 
1862
1955
        ui_init_toolbar_widgets();
1863
1956
        init_document_widgets();
1864
1957
        create_config_files_menu();
1943
2036
}
1944
2037
 
1945
2038
 
1946
 
/** This function returns a widget from a name in a component, usually created by Glade.
 
2039
/** Returns a widget from a name in a component, usually created by Glade.
1947
2040
 * Call it with the toplevel widget in the component (i.e. a window/dialog),
1948
2041
 * or alternatively any widget in the component, and the name of the widget
1949
2042
 * you want returned.
1981
2074
}
1982
2075
 
1983
2076
 
1984
 
 
1985
2077
/* Progress Bar */
1986
2078
static guint progress_bar_timer_id = (guint) -1;
1987
2079
 
2088
2180
        g_list_free(list);
2089
2181
}
2090
2182
 
 
2183
 
 
2184
/* return value is for macros */
 
2185
GtkWidget *ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
 
2186
{
 
2187
        va_list a;
 
2188
        gchar *text;
 
2189
 
 
2190
        va_start(a, format);
 
2191
        text = g_strdup_vprintf(format, a);
 
2192
        va_end(a);
 
2193
 
 
2194
        gtk_label_set_text(label, text);
 
2195
        gtk_label_set_use_markup(label, TRUE);
 
2196
        g_free(text);
 
2197
        return GTK_WIDGET(label);
 
2198
}
 
2199
 
 
2200
 
 
2201
/** Adds a list of document items to @a menu.
 
2202
 * @param menu Menu.
 
2203
 * @param active Which document to highlight, or @c NULL.
 
2204
 * @param callback is used for each menu item's @c "activate" signal and will be passed
 
2205
 * the corresponding document pointer as @c user_data.
 
2206
 * @warning You should check @c doc->is_valid in the callback.
 
2207
 * @since 0.19 */
 
2208
void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
 
2209
{
 
2210
        GtkWidget *menu_item, *menu_item_label;
 
2211
        const GdkColor *color;
 
2212
        GeanyDocument *doc;
 
2213
        guint i, len;
 
2214
        gchar *base_name;
 
2215
 
 
2216
        len = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
 
2217
        for (i = 0; i < len; i++)
 
2218
        {
 
2219
                doc = document_get_from_page(i);
 
2220
                if (! DOC_VALID(doc))
 
2221
                        continue;
 
2222
 
 
2223
                base_name = g_path_get_basename(DOC_FILENAME(doc));
 
2224
                menu_item = gtk_menu_item_new_with_label(base_name);
 
2225
                gtk_widget_show(menu_item);
 
2226
                gtk_container_add(GTK_CONTAINER(menu), menu_item);
 
2227
                g_signal_connect(menu_item, "activate", callback, doc);
 
2228
 
 
2229
                color = document_get_status_color(doc);
 
2230
                menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
 
2231
                gtk_widget_modify_fg(menu_item_label, GTK_STATE_NORMAL, color);
 
2232
                gtk_widget_modify_fg(menu_item_label, GTK_STATE_ACTIVE, color);
 
2233
 
 
2234
                if (doc == active)
 
2235
                        ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", base_name);
 
2236
 
 
2237
                g_free(base_name);
 
2238
        }
 
2239
}
 
2240
 
 
2241
 
 
2242
/** Checks whether the passed @a keyval is the Enter or Return key.
 
2243
 * There are three different Enter/Return key values
 
2244
 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
 
2245
 * This is just a convenience function.
 
2246
 * @param keyval A keyval.
 
2247
 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
 
2248
 * @since 0.19 */
 
2249
gboolean ui_is_keyval_enter_or_return(guint keyval)
 
2250
{
 
2251
        return (keyval == GDK_Return || keyval == GDK_ISO_Enter|| keyval == GDK_KP_Enter);
 
2252
}
 
2253
 
 
2254
 
 
2255
/** Reads an integer from the GTK default settings registry
 
2256
 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
 
2257
 * @param property_name The property to read.
 
2258
 * @param default_value The default value in case the value could not be read.
 
2259
 * @return The value for the property if it exists, otherwise the @a default_value.
 
2260
 * @since 0.19 */
 
2261
gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
 
2262
{
 
2263
        if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
 
2264
                gtk_settings_get_default())), property_name))
 
2265
        {
 
2266
                gint value;
 
2267
                g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
 
2268
                return value;
 
2269
        }
 
2270
        else
 
2271
                return default_value;
 
2272
}
 
2273
 
 
2274
 
 
2275
void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
 
2276
                                                                          gint new_text_len, gint *position, gpointer data)
 
2277
{
 
2278
        gboolean stop_signal = FALSE;
 
2279
        const gchar c = *new_text;
 
2280
 
 
2281
        /* allow inserting '+' and '-' as the first character */
 
2282
        if (position != NULL && *position == 0)
 
2283
        {
 
2284
                if (c != '+' && c != '-' && ! isdigit(c))
 
2285
                        stop_signal = TRUE;
 
2286
        }
 
2287
        /* don't insert any text when it is not a digit */
 
2288
        else if (! isdigit(c))
 
2289
                stop_signal = TRUE;
 
2290
 
 
2291
        if (stop_signal)
 
2292
                g_signal_stop_emission_by_name(editable, "insert-text");
 
2293
}
 
2294