1+ import customtkinter as ctk
12from tkinter import filedialog
3+ import os
24
35def new_file(state):
4- state["current_file"] = None
5- state["editor"].delete("1.0", "end")
6- state["output"].delete("1.0", "end")
6+ """Create a new file and clear the editor"""
7+ if state["editor"]:
8+ state["editor"].delete("1.0", "end")
9+ state["current_file"] = None
10+
11+ # Update status bar if available
12+ if "status_bar" in state and state["status_bar"]:
13+ state["status_bar"].update_file_info()
14+
15+ # Set window title
16+ if "app" in state and state["app"]:
17+ state["app"].title("ChiX - C Code Editor & Runner")
718
8- def open_file(state, highlight):
9- path = filedialog.askopenfilename(filetypes=[("C Files", "*.c")])
10- if path:
11- with open(path, "r") as file:
12- code = file.read()
13- state["editor"].delete("1.0", "end")
14- state["editor"].insert("1.0", code)
15- state["current_file"] = path
16- highlight(state["editor"])
19+ def open_file(state, highlight_func=None):
20+ """Open a file and load it into the editor"""
21+ file_path = filedialog.askopenfilename(
22+ filetypes=[
23+ ("C Files", "*.c"),
24+ ("Header Files", "*.h"),
25+ ("All Files", "*.*")
26+ ]
27+ )
28+
29+ if file_path:
30+ try:
31+ with open(file_path, "r") as file:
32+ content = file.read()
33+ if state["editor"]:
34+ state["editor"].delete("1.0", "end")
35+ state["editor"].insert("1.0", content)
36+ state["current_file"] = file_path
37+
38+ # Apply syntax highlighting if function provided
39+ if highlight_func:
40+ highlight_func(state["editor"])
41+
42+ # Update status bar if available
43+ if "status_bar" in state and state["status_bar"]:
44+ state["status_bar"].update_file_info(file_path)
45+
46+ # Set window title
47+ if "app" in state and state["app"]:
48+ filename = os.path.basename(file_path)
49+ state["app"].title(f"ChiX - {filename}")
50+
51+ except Exception as e:
52+ if state["output"]:
53+ state["output"].insert("end", f"Error opening file: {e}\n")
1754
1855def save_file(state):
56+ """Save the current file"""
1957 if state["current_file"]:
20- with open(state["current_file"], "w") as file:
21- file.write(state["editor"].get("1.0", "end-1c"))
58+ try:
59+ content = state["editor"].get("1.0", "end-1c")
60+ with open(state["current_file"], "w") as file:
61+ file.write(content)
62+
63+ if state["output"]:
64+ state["output"].delete("1.0", "end")
65+ state["output"].insert("end", f"File saved: {state['current_file']}\n")
66+
67+ except Exception as e:
68+ if state["output"]:
69+ state["output"].insert("end", f"Error saving file: {e}\n")
2270 else:
2371 save_file_as(state)
2472
2573def save_file_as(state):
26- path = filedialog.asksaveasfilename(defaultextension=".c", filetypes=[("C Files", "*.c")])
27- if path:
28- state["current_file"] = path
29- with open(path, "w") as file:
30- file.write(state["editor"].get("1.0", "end-1c"))
74+ """Save the current file as a new file"""
75+ file_path = filedialog.asksaveasfilename(
76+ defaultextension=".c",
77+ filetypes=[
78+ ("C Files", "*.c"),
79+ ("Header Files", "*.h"),
80+ ("All Files", "*.*")
81+ ]
82+ )
83+
84+ if file_path:
85+ state["current_file"] = file_path
86+ save_file(state)
87+
88+ # Update status bar if available
89+ if "status_bar" in state and state["status_bar"]:
90+ state["status_bar"].update_file_info(file_path)
91+
92+ # Set window title
93+ if "app" in state and state["app"]:
94+ filename = os.path.basename(file_path)
95+ state["app"].title(f"ChiX - {filename}")
0 commit comments