Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add drag-and-drop support #63

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/MiniFB.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void mfb_set_char_input_callback(struct mfb_window *window, mfb_c
void mfb_set_mouse_button_callback(struct mfb_window *window, mfb_mouse_button_func callback);
void mfb_set_mouse_move_callback(struct mfb_window *window, mfb_mouse_move_func callback);
void mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func callback);
void mfb_set_file_drag_callback(struct mfb_window *window, mfb_file_drag_func callback);
void mfb_set_file_drop_callback(struct mfb_window *window, mfb_file_drop_func callback);

// Getters
const char * mfb_get_key_name(mfb_key key);
Expand Down
2 changes: 2 additions & 0 deletions include/MiniFB_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,6 @@ typedef void(*mfb_char_input_func)(struct mfb_window *window, unsigned int code)
typedef void(*mfb_mouse_button_func)(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed);
typedef void(*mfb_mouse_move_func)(struct mfb_window *window, int x, int y);
typedef void(*mfb_mouse_scroll_func)(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY);
typedef void(*mfb_file_drag_func)(struct mfb_window *window, int x, int y);
typedef void(*mfb_file_drop_func)(struct mfb_window *window, char *file_list, int x, int y);

39 changes: 37 additions & 2 deletions src/MiniFB_common.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MiniFB.h"
#include "../include/MiniFB.h"
#include "WindowData.h"
#include <MiniFB_internal.h>
#include "MiniFB_internal.h"
#include <string.h>

//-------------------------------------
short int g_keycodes[512] = { 0 };
Expand Down Expand Up @@ -87,6 +88,22 @@ mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func c
}
}

//-------------------------------------
void mfb_set_file_drag_callback(struct mfb_window *window, mfb_file_drag_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->file_drag_func = callback;
}
}

//-------------------------------------
void mfb_set_file_drop_callback(struct mfb_window *window, mfb_file_drop_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->file_drop_func = callback;
}
}

//-------------------------------------
void
mfb_set_user_data(struct mfb_window *window, void *user_data) {
Expand Down Expand Up @@ -224,6 +241,24 @@ mfb_get_key_buffer(struct mfb_window *window) {
return 0;
}

//-------------------------------------
/* get next filename in file list (returns 0 when there are none) */
char *mfb_get_dropped_file(char *file_list)
{
const char *delim = "\n\r";

if (!file_list)
return 0;

/* a new file list has been provided: tokenize it and return first token */
if (strchr(file_list, '\n'))
return strtok(file_list, delim);

/* return next token from previous file list */
else
return strtok(0, delim);
}

//-------------------------------------
const char *
mfb_get_key_name(mfb_key key) {
Expand Down
2 changes: 1 addition & 1 deletion src/MiniFB_internal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <MiniFB.h>
#include "../include/MiniFB.h"
#include "WindowData.h"

#define kCall(func, ...) if(window_data && window_data->func) window_data->func((struct mfb_window *) window_data, __VA_ARGS__);
Expand Down
6 changes: 3 additions & 3 deletions src/MiniFB_linux.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if defined(__linux__)

#include <time.h>
#include <MiniFB.h>
#include "../include/MiniFB.h"

extern double g_timer_frequency;
extern double g_timer_resolution;
Expand All @@ -10,7 +10,7 @@ extern double g_timer_resolution;
//#define kClock CLOCK_REALTIME

uint64_t
mfb_timer_tick() {
mfb_timer_tick(void) {
struct timespec time;

if (clock_gettime(kClock, &time) != 0) {
Expand All @@ -21,7 +21,7 @@ mfb_timer_tick() {
}

void
mfb_timer_init() {
mfb_timer_init(void) {
struct timespec res;

if (clock_getres(kClock, &res) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/MiniFB_timer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "MiniFB.h"
#include "../include/MiniFB.h"
#include "MiniFB_internal.h"
#include <stdlib.h>

Expand Down
4 changes: 3 additions & 1 deletion src/WindowData.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <stdint.h>
#include <stdbool.h>
#include <MiniFB_enums.h>
#include "../include/MiniFB_enums.h"

//-------------------------------------
typedef struct {
Expand All @@ -16,6 +16,8 @@ typedef struct {
mfb_mouse_button_func mouse_btn_func;
mfb_mouse_move_func mouse_move_func;
mfb_mouse_scroll_func mouse_wheel_func;
mfb_file_drag_func file_drag_func;
mfb_file_drop_func file_drop_func;

uint32_t window_width;
uint32_t window_height;
Expand Down
Loading