Browse Source

Split out FLTK utility functions

These have nothing to do with layout, so let's split them to their own
file.
pull/1529/head
Pierre Ossman 1 year ago
parent
commit
7f0abbaed3

+ 1
- 0
vncviewer/OptionsDialog.cxx View File

@@ -41,6 +41,7 @@
#include "parameters.h"

#include "fltk/layout.h"
#include "fltk/util.h"
#include "fltk/MonitorArrangement.h"

#include <FL/Fl.H>

+ 1
- 0
vncviewer/UserDialog.cxx View File

@@ -39,6 +39,7 @@
#include <rfb/Exception.h>

#include "fltk/layout.h"
#include "fltk/util.h"
#include "i18n.h"
#include "parameters.h"
#include "UserDialog.h"

+ 1
- 0
vncviewer/Viewport.cxx View File

@@ -56,6 +56,7 @@
#endif

#include "fltk/layout.h"
#include "fltk/util.h"
#include "Viewport.h"
#include "CConn.h"
#include "OptionsDialog.h"

+ 0
- 82
vncviewer/fltk/layout.h View File

@@ -25,7 +25,6 @@
#define __FLTK_LAYOUT_H__

#include <FL/fl_draw.H>
#include <FL/Fl_Menu_.H>

/* Calculates the width of a string as printed by FLTK (pixels) */
static inline int gui_str_len(const char *str)
@@ -39,87 +38,6 @@ static inline int gui_str_len(const char *str)
return (int)(len + 0.5f);
}

/* Escapes all @ in text as those have special meaning in labels */
static inline size_t fltk_escape(const char *in, char *out, size_t maxlen)
{
size_t len;

len = 0;

while (*in != '\0') {
if (*in == '@') {
if (maxlen >= 3) {
*out++ = '@';
*out++ = '@';
maxlen -= 2;
}

len += 2;
} else {
if (maxlen >= 2) {
*out++ = *in;
maxlen--;
}

len += 1;
}

in++;
}

if (maxlen)
*out = '\0';

return len;
}

/* Filter out unsafe characters for menu entries */
static inline size_t fltk_menu_escape(const char *in, char *out, size_t maxlen)
{
size_t len;

len = 0;

while (*in != '\0') {
if (*in == '/') {
if (maxlen >= 3) {
*out++ = '\\';
*out++ = '/';
maxlen -= 2;
}

len += 2;
} else {
if (maxlen >= 2) {
*out++ = *in;
maxlen--;
}

len += 1;
}

in++;
}

if (maxlen)
*out = '\0';

return len;
}

/* Helper to add menu entries safely */
static inline void fltk_menu_add(Fl_Menu_ *menu, const char *text,
int shortcut, Fl_Callback *cb,
void *data = 0, int flags = 0)
{
char buffer[1024];

if (fltk_menu_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
return;

menu->add(buffer, shortcut, cb, data, flags);
}

/**** MARGINS ****/

#define OUTER_MARGIN 10

+ 110
- 0
vncviewer/fltk/util.h View File

@@ -0,0 +1,110 @@
/* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
*
* 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.
*/

#ifndef __FLTK_UTIL_H__
#define __FLTK_UTIL_H__

#include <FL/Fl_Menu_.H>

/* Escapes all @ in text as those have special meaning in labels */
static inline size_t fltk_escape(const char *in, char *out, size_t maxlen)
{
size_t len;

len = 0;

while (*in != '\0') {
if (*in == '@') {
if (maxlen >= 3) {
*out++ = '@';
*out++ = '@';
maxlen -= 2;
}

len += 2;
} else {
if (maxlen >= 2) {
*out++ = *in;
maxlen--;
}

len += 1;
}

in++;
}

if (maxlen)
*out = '\0';

return len;
}

/* Filter out unsafe characters for menu entries */
static inline size_t fltk_menu_escape(const char *in, char *out, size_t maxlen)
{
size_t len;

len = 0;

while (*in != '\0') {
if (*in == '/') {
if (maxlen >= 3) {
*out++ = '\\';
*out++ = '/';
maxlen -= 2;
}

len += 2;
} else {
if (maxlen >= 2) {
*out++ = *in;
maxlen--;
}

len += 1;
}

in++;
}

if (maxlen)
*out = '\0';

return len;
}

/* Helper to add menu entries safely */
static inline void fltk_menu_add(Fl_Menu_ *menu, const char *text,
int shortcut, Fl_Callback *cb,
void *data = 0, int flags = 0)
{
char buffer[1024];

if (fltk_menu_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
return;

menu->add(buffer, shortcut, cb, data, flags);
}

#endif

+ 1
- 1
vncviewer/vncviewer.cxx View File

@@ -67,7 +67,7 @@
#include <FL/fl_ask.H>
#include <FL/x.H>

#include "fltk/layout.h"
#include "fltk/util.h"
#include "i18n.h"
#include "parameters.h"
#include "CConn.h"

Loading…
Cancel
Save