Re: Can I get best practice of writing to menubar in modern gtk3?



Please help.

I try writing menuber in gtk3.
I need menu setting is in variables.
But gtk_ui_manager + gtk_action_group is deprecated.

I writing wrapper header.
(sorry long post.)
Can I get like this?
Am I writing it by oneself in this?

Thanks.

pv_menubar.h
==
/** @brief Menubar wrapper for gtk3
 *
 * Author: michinari nukazawa gmail com in project daisy bell
 * Licence: BSD Clause-2
 *
 * ** supported
 * * menuitem definition of variables like a breadcrumb.
 * * automatic branch menuitem.
 * * mnemonic
 * * accel
 * * kicking callback trigger of active(menuitem setect).
 *
 * ** Not supported this version
 * * RadioMenuItem
 * (https://developer.gnome.org/gtk3/stable/GtkRadioMenuItem.html)
 * * CheckMenuItem
 * (https://developer.gnome.org/gtk3/stable/GtkCheckMenuItem.html)
 * * append new menuitem
 * * disabled menuitem
 * * re ordering menuitem
 * * delete menubar
 *
 * ** Not supported this wrapper
 * * callback of branch node
 * * callback with data
 */

#ifndef __PV_MENUBAR_H__
#define __PV_MENUBAR_H__


#include <gtk/gtk.h>

/** @brief struct of definition menumber items.
* breadcrumb menu item with route. ex. "_File>_Quit", "_File>_Export>_Jpeg"
 * accelerator  keyboard shortcut.    ex. "<control>Q", "<control><shift>J"
 * cbActive     callback
 */
typedef struct{
    const char          *breadcrumb;
    const char          *accelerator;
    GCallback           cbActive;
}PvMenubaritem;

struct _PvMenubar;
typedef struct _PvMenubar PvMenubar;

PvMenubar *pv_menubar_new();
bool pv_menubar_set_items(PvMenubar *menubar, const PvMenubaritem *menubaritems, int numMenubaritems);
/** @brief draw menubar and set accel
 * @param menubar
 * @param container GtkBox already packing in window.
 */
bool pv_menubar_apply(PvMenubar *menubar, GtkWidget *container);

#ifdef __PV_TEST__
#endif // __PV_TEST__

#endif // __PV_MENUBAR_H__

==

sample_pv_menubar.c
==
/**/
/** @brief Menubar wrapper for gtk3
 *
 * Author: michinari nukazawa gmail com in project daisy bell
 * Licence: BSD Clause-2
 *
 */

#include "pv_menubar.h"
#include <gtk/gtk.h>

void callback_export_svg( GtkWidget *widget,
                    gpointer   callback_data)
{
    g_print("call export_svg!\n");
}

void callback_about()
{
    g_print("call about!\n");
}

bool do_menubar(GtkWidget *container)
{
// notice: set NULL to callback is nothing situation in real app. (this is sample)
    PvMenubaritem menubaritems[] = {
        {"_File>_Open",         "<control>O",   NULL,},
        {"_File>_Save",         "<control>S",   NULL,},
        {"_File>Save _As", "<control><shift>A", NULL,},
        {"_File>_Export>_Jpeg", "<control><shift>E", NULL,},
        {"_File>_Export>_SVG",  "", G_CALLBACK(callback_export_svg),},
        {"_File>_Quit",         "<control>Q", G_CALLBACK(gtk_main_quit),},
        {"_Help>_About",        "", G_CALLBACK(callback_about),},
    };
    int numMenubaritems = (sizeof(menubaritems) / sizeof(PvMenubaritem));

    PvMenubar *menubar = pv_menubar_new();
    if(NULL == menubar){
        return false;
    }
    if(!pv_menubar_set_items(menubar, menubaritems, numMenubaritems))){
        return false;
    }
    if(!pv_menubar_apply(menubar, container)){
        return false;
    }

    return true;
}

int main(int argc, char **argv)
{
    GtkWidget *window;

    gtk_init(&argc, &argv);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *box;
    box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add (GTK_CONTAINER (window), box);

    if(!do_menubar(box)){
        return -1;
    }

    gtk_widget_show_all (window);
    gtk_main();

    return 0;
}

==

--
==========
Michinari.Nukazawa

in the project "daisy bell"
https://daisy-bell.booth.pm/
==========



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]