Skip to content

Instantly share code, notes, and snippets.

@darealshinji
Last active July 21, 2017 16:31
Show Gist options
  • Save darealshinji/fe1845d4c90d62eee92c6bcf2860d2c3 to your computer and use it in GitHub Desktop.
Save darealshinji/fe1845d4c90d62eee92c6bcf2860d2c3 to your computer and use it in GitHub Desktop.
/*
* The MIT License (MIT)
*
* Copyright (c) 2017, djcj <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Check_Button.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_PNG_Image.H>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dialog_images.h"
#define BUF_SIZE 128
enum {
RET_LAUNCH_APP = 0,
RET_MENU_ENTRY = 1,
RET_DISABLE_MESSAGE = 2,
RET_CLOSE_WINDOW = 3
};
Fl_Double_Window *win;
int rv = 0;
bool checkbutton_set = false;
/* Writes string src in reversed order to the buffer pointed to by dest;
* dest will be terminated by a null byte ('\0')
*/
static inline
void revstr(char *dest, const char *src)
{
size_t i = 0;
size_t j = strlen(src) - 1;
for (; i <= j; ++i)
{
dest[i] = src[j - i];
}
dest[i] = '\0';
}
/* Compares the last n bytes of str1 and str2 */
static inline
int strlastcmp(const char *str1, const char *str2, size_t n)
{
size_t size = strlen(str1);
char *str1r = (char *)malloc(size);
memset(str1r, '\0', size);
revstr(str1r, str1);
size = strlen(str2);
char *str2r = (char *)malloc(size);
memset(str2r, '\0', size);
revstr(str2r, str2);
int ret = strncmp(str1r, str2r, n);
free(str1r);
free(str2r);
return ret;
}
std::string get_system_font()
{
FILE *fp;
char buf[BUF_SIZE] = "";
fp = popen("gsettings get org.mate.interface font-name 2>/dev/null || "
"gsettings get org.cinnamon.desktop.interface font-name 2>/dev/null || "
"gsettings get org.gnome.desktop.interface font-name 2>/dev/null", "r");
if (fp != NULL)
{
char *tmp = fgets(buf, BUF_SIZE, fp);
(void)tmp;
pclose(fp);
}
char *token = strtok(buf, " ");
std::string font = std::string(token+1);
std::string prev;
for (size_t i = 0; token != NULL; ++i)
{
if (i > 1)
{
font = font + " " + prev;
}
if (token != NULL)
{
prev = std::string(token);
token = strtok(NULL, " ");
}
}
if (strlastcmp(font.c_str(), " Bold Italic", strlen(" Bold Italic")) == 0)
{
font = "P" + font.substr(0, font.length() - strlen(" Bold Italic"));
}
else if (strlastcmp(font.c_str(), " Bold", strlen(" Bold")) == 0)
{
font = "B" + font.substr(0, font.length() - strlen(" Bold"));
}
else if (strlastcmp(font.c_str(), " Italic", strlen(" Italic")) == 0)
{
font = "I" + font.substr(0, font.length() - strlen(" Italic"));
}
return font;
}
void close_cb(Fl_Widget *, long p)
{
win->hide();
rv = (int)p;
}
void launch_cb(Fl_Widget *, long p)
{
win->hide();
rv = (checkbutton_set) ? RET_DISABLE_MESSAGE : (int)p;
}
void checkbutton_cb(Fl_Widget *)
{
checkbutton_set = (checkbutton_set) ? false : true;
}
int launcher(const char *title)
{
const char *msg_launch = "\nLaunch app\n\n";
const char *msg_menu = "\nCreate menu entry\nand launch app";
const char *msg_checkbox = " Don't show this message again";
char *lang = getenv("LANG");
if (lang == NULL)
{
lang = getenv("LANGUAGE");
}
if (lang != NULL && strcmp(lang, "C") != 0 && strncmp(lang, "en", 2) != 0)
{
if (strncmp(lang, "de", 2) == 0)
{
msg_launch = "\nStarte App\n\n";
msg_menu = "\nMenüeintrag erstellen\nund App starten";
msg_checkbox = " Dieses Fenster nicht erneut anzeigen";
}
}
win = new Fl_Double_Window(480, 286, title);
win->callback(close_cb, RET_CLOSE_WINDOW);
{
{ Fl_Button *o = new Fl_Button(40, 40, 180, 180, msg_launch);
o->image(new Fl_PNG_Image(NULL, LAUNCH_ICON, LAUNCH_ICON_LEN));
o->box(FL_GLEAM_THIN_UP_BOX);
o->down_box(FL_GLEAM_THIN_DOWN_BOX);
o->clear_visible_focus();
o->callback(launch_cb, RET_LAUNCH_APP); }
{ Fl_Button *o = new Fl_Button(260, 40, 180, 180, msg_menu);
o->image(new Fl_PNG_Image(NULL, MENU_ICON, MENU_ICON_LEN));
o->box(FL_GLEAM_THIN_UP_BOX);
o->down_box(FL_GLEAM_THIN_DOWN_BOX);
o->clear_visible_focus();
o->callback(close_cb, RET_MENU_ENTRY); }
{ Fl_Check_Button *o = new Fl_Check_Button(40, 240, 400, 26, msg_checkbox);
o->clear_visible_focus();
o->callback(checkbutton_cb); }
}
win->position((Fl::w() - win->w()) / 2, (Fl::h() - win->h()) / 2);
win->end();
win->show();
Fl::run();
return rv;
}
int main(int argc, char **argv)
{
const char *title = "AppImage";
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0)
{
printf("Usage: %s WINDOWTITLE\n\n"
"Return values:\n\tlaunch app -> %d\n\tmenu entry -> %d\n\tdisable message + launch app -> %d\n\tclose window -> %d\n"
/* identify usage as required per FLTK license LGPL exception condition 4 */
"\n\nThis program is using FLTK v%d.%d.%d (http://www.fltk.org)\n",
argv[0], RET_LAUNCH_APP, RET_MENU_ENTRY, RET_DISABLE_MESSAGE, RET_CLOSE_WINDOW, FL_MAJOR_VERSION, FL_MINOR_VERSION, FL_PATCH_VERSION);
return 0;
}
title = argv[1];
}
Fl::get_system_colors();
Fl_Window::default_icon(new Fl_PNG_Image(NULL, WINDOW_ICON, WINDOW_ICON_LEN));
if (getenv("KDE_FULL_SESSION") == NULL)
{
std::string font = get_system_font();
Fl::set_font(FL_HELVETICA, font.c_str());
}
return launcher(title);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment