чување f7f5a9ce3fb8976b64b452610ac5eae69238d262
родитељ 5dbbdc92f8f589c7bbb7c813a6ba2cdd69ca67cc
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Thu, 14 Dec 2023 11:46:53 +0100
Move main window into a separate struct; handle resize
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | app.h | | | 3 | +-- |
| M | main.c | | | 34 | ++++++++++++++++++---------------- |
| M | mainwindow.c | | | 66 | +++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
| M | mainwindow.h | | | 11 | +++++++++++ |
измењених датотека: 4, додавања: 79(+), брисања: 35(-)
diff --git a/app.h b/app.h
@@ -18,8 +18,7 @@ struct WMNewApp {
Display* dpy;
XWMHints* hints;
WMMenu* main_menu;
- WMWindow* mainwin;
- char mainwin_title[4096];
+ struct WMMainWindow* mainwin;
RContext* rctx;
WMScreen* scr;
WMPixmap* wmpixmap;
diff --git a/main.c b/main.c
@@ -67,7 +67,7 @@ exit_menu_action(void* data, int code, Time timestamp)
{
struct WMNewApp* wma = (struct WMNewApp*)data;
WMLog(wma, "exit_menu_action called\n");
- exit_action(wma->mainwin, wma);
+ exit_action(wma->mainwin->window, wma);
}
void
@@ -101,19 +101,19 @@ handle_keypress(XEvent* event, void* data)
switch (ksym)
{
case XK_F1:
- info_panel_action(wma->mainwin, wma);
+ info_panel_action(wma->mainwin->window, wma);
break;
case XK_o:
if (ctrl_pressed && !meta_pressed && !shift_pressed)
- open_action(wma->mainwin, wma);
+ open_action(wma->mainwin->window, wma);
break;
case XK_s:
if (ctrl_pressed && !meta_pressed && !shift_pressed)
- save_action(wma->mainwin, wma);
+ save_action(wma->mainwin->window, wma);
break;
case XK_q:
if (ctrl_pressed && !meta_pressed && !shift_pressed)
- exit_action(wma->mainwin, wma);
+ exit_action(wma->mainwin->window, wma);
break;
}
}
@@ -131,7 +131,7 @@ info_panel_menu_action(void* data, int code, Time timestamp)
{
struct WMNewApp* wma = (struct WMNewApp*)data;
WMLog(wma, "info_menu_action called\n");
- info_panel_action(wma->mainwin, wma);
+ info_panel_action(wma->mainwin->window, wma);
}
void
@@ -139,6 +139,7 @@ init_state(struct State* state)
{
state->dirty = 0;
*state->filename = 0;
+ state->file_open = 0;
}
void
@@ -160,7 +161,7 @@ open_action(WMWidget* self, void* data)
WMLog(wma, "open_action called\n");
getcwd(wd, PATH_MAX);
if (!WMRunModalFilePanelForDirectory(WMGetOpenPanel(wma->scr),
- wma->mainwin, wd, "Open file", NULL))
+ wma->mainwin->window, wd, "Open file", NULL))
return;
filename = WMGetFilePanelFileName(WMGetOpenPanel(wma->scr));
last_slash = strrchr(filename, '/');
@@ -171,8 +172,7 @@ open_action(WMWidget* self, void* data)
wma->state.file_open = 1;
set_mainwin_title(wma);
WMLog(wma, "opened file: %s (full path: %s)\n",
- last_slash ? last_slash + 1 : filename,
- filename);
+ last_slash ? last_slash + 1 : filename, filename);
free(filename);
}
@@ -181,7 +181,7 @@ open_menu_action(void* data, int code, Time timestamp)
{
struct WMNewApp* wma = (struct WMNewApp*)data;
WMLog(wma, "open_menu_action called\n");
- open_action(wma->mainwin, wma);
+ open_action(wma->mainwin->window, wma);
}
void
@@ -197,7 +197,7 @@ save_menu_action(void* data, int code, Time timestamp)
{
struct WMNewApp* wma = (struct WMNewApp*)data;
WMLog(wma, "save_menu_action called\n");
- save_action(wma->mainwin, wma);
+ save_action(wma->mainwin->window, wma);
}
void
@@ -260,15 +260,15 @@ main(int argc, char** argv)
create_main_window(&wma);
wma.app = WMAppCreateWithMain(wma.dpy, DefaultScreen(wma.dpy),
- WMWidgetXID(wma.mainwin));
- XSetCommand(wma.dpy, WMWidgetXID(wma.mainwin), argv, argc);
- WMSetWindowCloseAction(wma.mainwin, exit_action, &wma);
+ WMWidgetXID(wma.mainwin->window));
+ XSetCommand(wma.dpy, WMWidgetXID(wma.mainwin->window), argv, argc);
+ WMSetWindowCloseAction(wma.mainwin->window, exit_action, &wma);
create_icon(&wma);
create_main_menu(&wma);
- WMSetFocusToWidget(wma.mainwin);
- WMCreateEventHandler(WMWidgetView(wma.mainwin), KeyPressMask,
+ WMSetFocusToWidget(wma.mainwin->window);
+ WMCreateEventHandler(WMWidgetView(wma.mainwin->window), KeyPressMask,
handle_keypress, &wma);
XFlush(wma.dpy);
@@ -281,5 +281,7 @@ main(int argc, char** argv)
WMProcessEvent(wma.app, &event);
}
+ destroy_main_window(&wma);
+
return 0;
}
diff --git a/mainwindow.c b/mainwindow.c
@@ -4,6 +4,7 @@
#include "app.h"
#include "appicon.xpm"
+#define INCLUDE_STATIC_FUNCS
#include "mainwindow.h"
#include "windowmanager.h"
@@ -32,11 +33,11 @@ create_icon(struct WMNewApp* wma)
wma->hints->icon_pixmap = wma->app_icon;
wma->hints->icon_mask = wma->app_mask;
wma->hints->flags |= (IconPixmapHint | IconMaskHint);
- XSetWMHints(wma->dpy, WMWidgetXID(wma->mainwin), wma->hints);
+ XSetWMHints(wma->dpy, WMWidgetXID(wma->mainwin->window), wma->hints);
if (!(wma->wmpixmap
= WMCreatePixmapFromRImage(wma->scr, wma->app_image, 0)))
- exit(error(1, "WMCreatePixmapFromRImage failed"));
+ exit(error(1, "WMCreatePixmapFromRImage failed\n"));
WMSetApplicationIconPixmap(wma->scr, wma->wmpixmap);
}
@@ -46,24 +47,31 @@ create_main_window(struct WMNewApp* wma)
MyGNUstepWMAttributes attributes;
if (!(wma->class_hint = XAllocClassHint()))
- exit(error(1, "XAllocClassHint failed"));
+ exit(error(1, "XAllocClassHint failed\n"));
wma->class_hint->res_name = "wmnewapp";
wma->class_hint->res_class = "WMNewApp";
- wma->mainwin = WMCreateWindow(wma->scr, wma->class_hint->res_name);
- WMRealizeWidget(wma->mainwin);
+ if (!(wma->mainwin
+ = (struct WMMainWindow*)calloc(sizeof(struct WMMainWindow),
+ 1)))
+ exit(error(1, "calloc failed\n"));
+ wma->mainwin->app = wma;
+ wma->mainwin->window
+ = WMCreateWindow(wma->scr, wma->class_hint->res_name);
+ WMRealizeWidget(wma->mainwin->window);
if (!(wma->hints = XAllocWMHints()))
- exit(error(1, "XAllocWMHints failed"));
- wma->hints->window_group = WMWidgetXID(wma->mainwin);
+ exit(error(1, "XAllocWMHints failed\n"));
+ wma->hints->window_group = WMWidgetXID(wma->mainwin->window);
wma->hints->flags = WindowGroupHint;
set_mainwin_title(wma);
- WMResizeWidget(wma->mainwin, 640, 480);
- WMSetWindowCloseAction(wma->mainwin, exit_action, wma);
+ WMResizeWidget(wma->mainwin->window, 640, 480);
+ WMSetWindowCloseAction(wma->mainwin->window, exit_action, wma);
- XSetClassHint(wma->dpy, WMWidgetXID(wma->mainwin), wma->class_hint);
- XSetWMHints(wma->dpy, WMWidgetXID(wma->mainwin), wma->hints);
+ XSetClassHint(wma->dpy, WMWidgetXID(wma->mainwin->window),
+ wma->class_hint);
+ XSetWMHints(wma->dpy, WMWidgetXID(wma->mainwin->window), wma->hints);
memset((void*)&attributes, 0, sizeof(MyGNUstepWMAttributes));
attributes.window_style = (WMClosableWindowMask | WMTitledWindowMask
@@ -72,18 +80,42 @@ create_main_window(struct WMNewApp* wma)
attributes.extra_flags = GSFullKeyboardEventsFlag;
attributes.flags
= (GSWindowStyleAttr | GSWindowLevelAttr | GSExtraFlagsAttr);
- MyWMSetWindowAttributes(wma->dpy, WMWidgetXID(wma->mainwin),
+ MyWMSetWindowAttributes(wma->dpy, WMWidgetXID(wma->mainwin->window),
&attributes);
- WMMapSubwidgets(wma->mainwin);
- WMMapWidget(wma->mainwin);
+ WMMapSubwidgets(wma->mainwin->window);
+ WMMapWidget(wma->mainwin->window);
+ WMSetViewNotifySizeChanges(WMWidgetView(wma->mainwin->window), True);
+ WMAddNotificationObserver(handle_mainwin_resize, wma,
+ WMViewSizeDidChangeNotification,
+ WMWidgetView(wma->mainwin->window));
+}
+
+void
+destroy_main_window(struct WMNewApp* wma)
+{
+ if (wma->mainwin)
+ free(wma->mainwin);
+}
+
+void
+handle_mainwin_resize(void* data, WMNotification* notification)
+{
+ struct WMNewApp* wma = (struct WMNewApp*)data;
+ WMSize new_size = WMGetViewSize(WMWidgetView(wma->mainwin->window));
+ WMLog(wma, "handle_mainwin_resize called (\n");
+ WMLog(wma, "\tname: {%s}\n", WMGetNotificationName(notification));
+ WMLog(wma, "\tnew size: (w = %d, h = %d)\n", new_size.width,
+ new_size.height);
+ WMLog(wma, ")\n");
+ /* Resize widgets... */
}
void
set_mainwin_title(struct WMNewApp* wma)
{
if (wma->state.file_open)
- snprintf(wma->mainwin_title,
+ snprintf(wma->mainwin->title,
strlen(mainwin_title_fmt) - 2 - 2 + PATH_MAX
+ strlen(program_name) + 1,
mainwin_title_fmt, program_name,
@@ -91,7 +123,7 @@ set_mainwin_title(struct WMNewApp* wma)
*(wma->state.filename) ? wma->state.filename
: default_filename);
else
- snprintf(wma->mainwin_title, strlen(program_name) + 1, "%s",
+ snprintf(wma->mainwin->title, strlen(program_name) + 1, "%s",
program_name);
- WMSetWindowTitle(wma->mainwin, wma->mainwin_title);
+ WMSetWindowTitle(wma->mainwin->window, wma->mainwin->title);
}
diff --git a/mainwindow.h b/mainwindow.h
@@ -1,3 +1,14 @@
+struct WMMainWindow {
+ struct WMNewApp* app;
+ WMWindow* window;
+ char title[4096];
+};
+
void create_icon(struct WMNewApp* wma);
void create_main_window(struct WMNewApp* wma);
+void destroy_main_window(struct WMNewApp* wma);
void set_mainwin_title(struct WMNewApp* wma);
+
+#ifdef INCLUDE_STATIC_FUNCS
+static void handle_mainwin_resize(void* data, WMNotification* notification);
+#endif