Skip to content

Commit

Permalink
Merge pull request #215 from ttytm/example/text-editor-dialog-fix
Browse files Browse the repository at this point in the history
Fix file dialog in text editor example
  • Loading branch information
ttytm authored Sep 22, 2023
2 parents ee9e8f9 + 5a6df39 commit e06d972
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 291 deletions.
120 changes: 0 additions & 120 deletions examples/C/text-editor/cross_platform_open_file.h

This file was deleted.

99 changes: 9 additions & 90 deletions examples/C/text-editor/text-editor.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Text Editor in C using WebUI

#include "webui.h"
#include "cross_platform_open_file.h"

#include <stdio.h>
#include <stdlib.h>

void Close(webui_event_t* e) {
printf("Exit.\n");
Expand All @@ -13,97 +9,20 @@ void Close(webui_event_t* e) {
webui_exit();
}

void Save(webui_event_t* e) {
printf("Save.\n");

// Save data received from the UI
FILE *file = fopen(FILE_PATH, "w");
int results = fputs(e->data, file);
if(results == EOF) {
// Failed to write
}
fclose(file);
}

void Open(webui_event_t* e) {
printf("Open.\n");

// Open a new file

// Open file and save the path to FILE_PATH
if(show_file_open_dialog() == NULL)
return;

// Read the full content to FULL_FILE_BUFFER
size_t bytes_read = read_file_into_buffer(FILE_PATH, FULL_FILE_BUFFER, FULL_FILE_BUFFER_SIZE);
if(bytes_read < 1)
return;

// Send file content
// Encode the full content to base64
char* file_encoded = webui_encode(FULL_FILE_BUFFER);
if(file_encoded == NULL)
return;
sprintf(JAVASCRIPT_BUFFER, "addText('%s')", file_encoded);
webui_run(e->window, JAVASCRIPT_BUFFER);

// Send file name
// Encode the file path to base64
char* path_encoded = webui_encode(FILE_PATH);
if(path_encoded == NULL)
return;
sprintf(JAVASCRIPT_BUFFER, "SetFile('%s')", path_encoded);
webui_run(e->window, JAVASCRIPT_BUFFER);

// Clean
webui_free(file_encoded);
webui_free(path_encoded);

/*
// Add line by line example:
FILE *file = fopen(FILE_PATH, "r");
if(file == NULL)
return;
char line[1024] = {0};
while (fgets(line, 1024, file) != NULL) {
// Line
char* line_encoded = webui_encode(line);
if(line_encoded != NULL) {
char js[1024] = {0};
// JS
sprintf(js, "addLine('%s')", line_encoded);
// Send
webui_run(e->window, js);
// Clean
webui_free(line_encoded);
}
}
fclose(file);
*/
}

int main() {

// Create new windows
// Create a new window
int MainWindow = webui_new_window();

// Bind HTML element IDs with a C functions
webui_bind(MainWindow, "Open", Open);
webui_bind(MainWindow, "Save", Save);
webui_bind(MainWindow, "Close", Close);

// Show a new window
// Set the root folder for the UI
webui_set_root_folder(MainWindow, "ui");
webui_show(MainWindow, "MainWindow.html");

// Bind HTML elements with the specified ID to C functions
webui_bind(MainWindow, "close-button", Close);

// Show the window, preferably in a chromium based browser
if (!webui_show_browser(MainWindow, "MainWindow.html", ChromiumBased))
webui_show(MainWindow, "MainWindow.html");

// Wait until all windows get closed
webui_wait();
Expand Down
22 changes: 11 additions & 11 deletions examples/C/text-editor/ui/MainWindow.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,30 @@
<html>
<head>
<meta charset="UTF-8">
<script src="webui.js"></script>
<script src="/webui.js"></script>

<link rel="icon" type="image/png" href="img/icon.png" />
<title>Text Editor in C using WebUI</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/lucario.css">
<link rel="stylesheet" href="css/codemirror.min.css">
<link rel="stylesheet" href="css/all.min.css">
<script src="js/codemirror.min.js"></script>
<script src="js/xml.min.js"></script>
<script src="js/css.min.js"></script>
<script src="js/javascript.min.js"></script>
<script src="js/clike.min.js"></script>
<script src="js/python.min.js"></script>
</head>
<body>
<div class="topbar"></div>
<nav>
<ul>
<li>
<a id="Open"><i class="fas fa-folder-open"></i></a>
<a onclick="OpenFile();"><i class="fas fa-folder-open"></i></a>
</li>
<li id="SaveLi" style="pointer-events: none; color: #7ca0df;">
<li id="save-button" style="pointer-events: none; color: #7ca0df;">
<a onclick="SaveFile();"><i class="fa fa-floppy-disk"></i></a>
</li>
<li>
<a id="Close"><i class="fas fa-circle-xmark"></i></a>
<a id="close-button"><i class="fas fa-circle-xmark"></i></a>
</li>
<li>
<a id="About"><i class="fas fa-question-circle"></i></a>
<a id="about-button"><i class="fas fa-question-circle"></i></a>
</li>
</ul>
</nav>
Expand All @@ -46,6 +40,12 @@ <h1>WebUI Text Editor</h1>
<p><a href="https://webui.me" target="_blank">webui.me</a> | (C)2023 Hassan Draga</p>
</div>
</div>
<script src="js/codemirror.min.js"></script>
<script src="js/xml.min.js"></script>
<script src="js/css.min.js"></script>
<script src="js/javascript.min.js"></script>
<script src="js/clike.min.js"></script>
<script src="js/python.min.js"></script>
<script src="js/ui.js"></script>
</body>
</html>
Loading

0 comments on commit e06d972

Please sign in to comment.