Создание диалогов с помощью GTK

Данный пример показывает создание диалогов:

  • Окно выбора цвета
  • Окно выбора шрифта
  • Окно выбора файла
  • Окно вывода сообщений

Платформы: Windows, Linux
Автор: vladimir777, создано в 2011 году.

GTK

'#LIBPATH "C:\opt\GTK-3.0.9\lib"

#INCLUDE Once "gtk\gtk.bi"

#DEFINE NULL 0

Sub show_info Cdecl(Byval widget As GtkWidget Ptr, Byval www As GtkWidget Ptr)
    Dim dialog As GtkWidget Ptr = gtk_message_dialog_new(GTK_WINDOW(www), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Download Completed", "title")
    gtk_window_set_title(GTK_WINDOW(dialog), "Information")
    gtk_dialog_run(GTK_DIALOG(dialog))
    gtk_widget_destroy(dialog)
End Sub
Sub show_error Cdecl(Byval widget As GtkWidget Ptr, Byval www As GtkWidget Ptr)
    Dim dialog As GtkWidget Ptr = gtk_message_dialog_new(GTK_WINDOW(www), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error loading file")
    gtk_window_set_title(GTK_WINDOW(dialog), "Error")
    gtk_dialog_run(GTK_DIALOG(dialog))
    gtk_widget_destroy(dialog)
End Sub
Sub show_question Cdecl(Byval widget As GtkWidget Ptr, Byval www As GtkWidget Ptr)
    Print "how do i read answer"
    Dim dialog As GtkWidget Ptr = gtk_message_dialog_new(GTK_WINDOW(www), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Are you sure to quit?")
    gtk_window_set_title(GTK_WINDOW(dialog), "Question")
    gtk_dialog_run(GTK_DIALOG(dialog))
    gtk_widget_destroy(dialog)
End Sub
Sub show_warning Cdecl(Byval widget As GtkWidget Ptr, Byval www As GtkWidget Ptr)
    Dim dialog As GtkWidget Ptr = gtk_message_dialog_new(GTK_WINDOW(www), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Unallowed operation")
    gtk_window_set_title(GTK_WINDOW(dialog), "Warning")
    gtk_dialog_run(GTK_DIALOG(dialog))
    gtk_widget_destroy(dialog)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Dialog1 Cdecl (Byval widget As GtkWidget Ptr, Byval www As GtkWidget Ptr)
    Print "ok "
    Dim s As String
    Dim dialog As GtkWidget Ptr = gtk_message_dialog_new( GTK_WINDOW(www), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, Strptr( s ))
    gtk_dialog_run( GTK_DIALOG( dialog ) )
    gtk_widget_destroy( dialog )
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Dialog2 Cdecl (Byval widget As GtkWidget Ptr, Byval www As GtkWidget Ptr)
    Print "This is OK"
    Dim s As String
    Dim filename As String
    filename="FN"
    Dim errno As Integer
    errno = 1167
    Dim dialog As GtkWidget Ptr = gtk_message_dialog_new (GTK_WINDOW(www),  GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Error loading file '%s': %s", filename, g_strerror (errno))
    gtk_dialog_run( GTK_DIALOG( dialog ) )
    gtk_widget_destroy( dialog )
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Dialog3 Cdecl (Byval widget As GtkWidget Ptr, Byval www As GtkWidget Ptr)
    Print "problem 111"
    'dim as GdkPixbuf ptr pixbuf = gdk_pixbuf_new_from_file("mypic.png", NULL)
    'dim as GtkWidget ptr dialog = gtk_about_dialog_new(GTK_WINDOW(www))
    'gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(dialog), "yyyyyyyyyyy")
    'gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "0.9")
    'gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog), "OOOOOOOOOOOOOOO");
    'gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog), "dddddddddddddddddddddddddddddddddddddddd")
    'gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog), "http://www.ssss.net")
    'gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), pixbuf)
    'g_object_unref(pixbuf), pixbuf = NULL
    'gtk_dialog_run(GTK_DIALOG (dialog))
    'gtk_widget_destroy(dialog)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub show_ColorDialog Cdecl (Byval widget As GtkWidget Ptr, Byval label As GtkWidget Ptr)
    Dim As GtkResponseType result
    Dim As GtkColorSelection Ptr colorsel

    Dim As GtkWidget Ptr dialog = gtk_color_selection_dialog_new("Color")
    result = gtk_dialog_run(GTK_DIALOG(dialog))
    Print "problem 100"

    ' RESPONSE_HELP        Returned by Help buttons in GTK+ dialogs.
    ' -10        Gtk::RESPONSE_APPLY        Returned by Apply buttons in GTK+ dialogs.
    ' -9        Gtk::RESPONSE_NO        Returned by No buttons in GTK+ dialogs.
    ' -8        Gtk::RESPONSE_YES        Returned by Yes buttons in GTK+ dialogs.
    ' -7        Gtk::RESPONSE_CLOSE        Returned by Close buttons in GTK+ dialogs.
    ' -6        Gtk::RESPONSE_CANCEL        Returned by Cancel buttons in GTK+ dialogs.
    ' -5        Gtk::RESPONSE_OK        Returned by OK buttons in GTK+ dialogs.
    ' -4        Gtk::RESPONSE_DELETE_EVENT        Returned if the dialog is deleted.
    ' -3        Gtk::RESPONSE_ACCEPT        Generic response id, not used by GTK+ dialogs.
    ' -2        Gtk::RESPONSE_REJECT        Generic response id, not used by GTK+ dialogs.
    ' -1        Gtk::RESPONSE_NONE


    If (result = GTK_RESPONSE_OK) Then
        'dim as GdkColor color
        'colorsel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dialog)->colorsel)
        'gtk_color_selection_get_current_color(colorsel, @color)
        'gtk_widget_modify_fg(GTK_WIDGET(label), GTK_STATE_NORMAL, color)
    End If

    gtk_widget_destroy(dialog)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub show_FileDialog Cdecl (Byval widget As GtkWidget Ptr, Byval label As GtkWidget Ptr)
    Print "Test 5 "
    'Dim chooser As GtkWidget Ptr = gtk_file_chooser_dialog_new ("TITLE", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, "aa")
    'GTK_FILE_CHOOSER_ACTION_OPEN,GTK_FILE_CHOOSER_ACTION_SAVE,GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
    'gtk_dialog_run(GTK_FILE_CHOOSER_DIALOG( chooser ))
    'gtk_widget_destroy( chooser )
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub show_FontDialog Cdecl (Byval widget As GtkWidget Ptr, Byval label As GtkWidget Ptr)
    Print "Test 6 "
    Dim As GtkResponseType result

    Dim As GtkWidget Ptr dialog = gtk_font_selection_dialog_new("Select Font")
    result = gtk_dialog_run(GTK_DIALOG(dialog))

    If (result = GTK_RESPONSE_OK Or result = GTK_RESPONSE_APPLY) Then


        Dim As PangoFontDescription Ptr font_desc
        Dim As gchar Ptr fontname = gtk_font_selection_dialog_get_font_name(GTK_FONT_SELECTION_DIALOG(dialog))

        font_desc = pango_font_description_from_string(fontname)

        'gtk_widget_modify_font(GTK_WIDGET(label), font_desc)
        gtk_label_set_markup(GTK_LABEL(label), fontname)
        g_free(fontname)
    End If
    gtk_widget_destroy(dialog)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ColorDataDump Cdecl (Byval widget As GtkWidget Ptr, Byval pr1 As GtkWidget Ptr)
    Print "== COLOR BUTTON DATA =="
    'print gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER(pr1))
End Sub

Sub FileDataDump Cdecl (Byval widget As GtkWidget Ptr, Byval pr1 As GtkWidget Ptr)
    Print "== FILE BUTTON DATA =="
    'print gtk_file_chooser_get_current_folder_file (GTK_COLOR_CHOOSER(pr1))
End Sub

Sub FontDataDump Cdecl (Byval widget As GtkWidget Ptr, Byval pr1 As GtkWidget Ptr)
    Print "== FONT BUTTON DATA =="
    'print gtk_f_chooser_get_current_folder_file (GTK_FONT_CHOOSER(pr1))
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
gtk_init(@__FB_ARGC__, @__FB_ARGV__)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim As GtkWidget Ptr win = gtk_window_new(GTK_WINDOW_TOPLEVEL)
gtk_window_set_title(GTK_WINDOW(win), "DIALOG DEMO")
gtk_widget_set_size_request(win, 700, 450)
gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER)
Dim As GtkWidget Ptr fixed = gtk_fixed_new()
gtk_container_add(GTK_CONTAINER(win), fixed)

Dim As GtkWidget Ptr button1 = gtk_button_new_with_label("Dialog 1")
gtk_widget_set_size_request(button1, 80, 30)
gtk_fixed_put(GTK_FIXED(fixed), button1, 50, 10)
Dim As GtkWidget Ptr button2 = gtk_button_new_with_label("Dialog 2")
gtk_widget_set_size_request(button2, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), button2, 150, 10)
Dim As GtkWidget Ptr button3 = gtk_button_new_with_label("About")
gtk_widget_set_size_request(button3, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), button3, 350, 10)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim As GtkWidget Ptr button111 = gtk_button_new_with_label("INFO")
gtk_widget_set_size_request(button111, 100, 30)
gtk_fixed_put(GTK_FIXED(fixed), button111, 50, 50)
Dim As GtkWidget Ptr button222 = gtk_button_new_with_label("ERROR")
gtk_widget_set_size_request(button222, 100, 30)
gtk_fixed_put(GTK_FIXED(fixed), button222, 160, 50)
Dim As GtkWidget Ptr button333 = gtk_button_new_with_label("QUESTION")
gtk_widget_set_size_request(button333, 100, 30)
gtk_fixed_put(GTK_FIXED(fixed), button333, 280, 50)
Dim As GtkWidget Ptr button444 = gtk_button_new_with_label("WARNING")
gtk_widget_set_size_request(button444, 100, 30)
gtk_fixed_put(GTK_FIXED(fixed), button444, 400, 50)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim As GtkWidget Ptr showColorbutton4 = gtk_button_new_with_label("Color Dialog")
gtk_widget_set_size_request(showColorbutton4, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), showColorbutton4, 500, 100)
Dim As GtkWidget Ptr showFilebutton5 = gtk_button_new_with_label("File Dialog")
gtk_widget_set_size_request(showFilebutton5, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), showFilebutton5, 500, 140)
Dim As GtkWidget Ptr showFontbutton6 = gtk_button_new_with_label("Font Dialog")
gtk_widget_set_size_request(showFontbutton6, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), showFontbutton6, 500, 180)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim As GtkWidget Ptr buttonColorData = gtk_button_new_with_label("Color Chooser Data Print")
gtk_widget_set_size_request(buttonColorData, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), buttonColorData, 250, 100)
Dim As GtkWidget Ptr buttonFileData = gtk_button_new_with_label("File Chooser Data Print")
gtk_widget_set_size_request(buttonFileData, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), buttonFileData, 250, 140)
Dim As GtkWidget Ptr buttonFontData = gtk_button_new_with_label("Font Chooser Data Print")
gtk_widget_set_size_request(buttonFontData, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), buttonFontData, 250, 180)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim As GtkWidget Ptr ColorButton = gtk_color_button_new ()
gtk_widget_set_size_request(ColorButton, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), ColorButton, 50, 100)
gtk_color_button_set_title(GTK_COLOR_BUTTON(ColorButton), "Choose Color")
Dim As GtkWidget Ptr FileButton = gtk_file_chooser_button_new ("Please say something here", GTK_FILE_CHOOSER_ACTION_OPEN)
'GTK_FILE_CHOOSER_ACTION_OPEN, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
gtk_widget_set_size_request(FileButton, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), FileButton, 50, 140)
'gtk_color_button_set_title(GTK_FILE_CHOOSER_BUTTON(FileButton), "Choose File")
Dim As GtkWidget Ptr FontButton = gtk_font_button_new ()
gtk_widget_set_size_request(FontButton, 180, 30)
gtk_fixed_put(GTK_FIXED(fixed), FontButton, 50, 180)
'gtk_color_button_set_title(GTK_FONT_BUTTON(FontButton), "Choose Font")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim As GtkWidget Ptr label = gtk_label_new_with_mnemonic ("LABELABELLABELABELLABELABELLABELABEL")
gtk_widget_set_size_request(label, 150, 15)
gtk_fixed_put(GTK_FIXED(fixed), label, 10, 350)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK (@Dialog1), win)
g_signal_connect(G_OBJECT(button2), "clicked", G_CALLBACK (@Dialog2), win)
g_signal_connect(G_OBJECT(button3), "clicked", G_CALLBACK (@Dialog3), win)

g_signal_connect(G_OBJECT(button111), "clicked", G_CALLBACK (@show_info), win)
g_signal_connect(G_OBJECT(button222), "clicked", G_CALLBACK (@show_error), win)
g_signal_connect(G_OBJECT(button333), "clicked", G_CALLBACK (@show_question), win)
g_signal_connect(G_OBJECT(button444), "clicked", G_CALLBACK (@show_warning), win)

g_signal_connect(G_OBJECT(buttonColorData), "clicked", G_CALLBACK (@ColorDataDump), ColorButton)
g_signal_connect(G_OBJECT(buttonFileData), "clicked", G_CALLBACK (@FileDataDump), FileButton)
g_signal_connect(G_OBJECT(buttonFontData), "clicked", G_CALLBACK (@FontDataDump), FontButton)

g_signal_connect(G_OBJECT(showColorbutton4), "clicked", G_CALLBACK (@show_ColorDialog), label)
g_signal_connect(G_OBJECT(showFilebutton5), "clicked", G_CALLBACK (@show_FileDialog), label)
g_signal_connect(G_OBJECT(showFontbutton6), "clicked", G_CALLBACK (@show_FontDialog), label)

g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK (@gtk_main_quit), NULL)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
gtk_widget_show_all(win)
gtk_main()
End 0